1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2020 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_frame_section;
175 
176 /* Maximum size (in bytes) of an artificially generated label.  */
177 #define MAX_ARTIFICIAL_LABEL_BYTES	40
178 
179 /* According to the (draft) DWARF 3 specification, the initial length
180    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
181    bytes are 0xffffffff, followed by the length stored in the next 8
182    bytes.
183 
184    However, the SGI/MIPS ABI uses an initial length which is equal to
185    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
186 
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
190 
191 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
192 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
193 #endif
194 
195 /* Round SIZE up to the nearest BOUNDARY.  */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
198 
199 /* CIE identifier.  */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
206 
207 
208 /* A vector for a table that contains frame description
209    information for each routine.  */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
212 
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
214 
215 struct GTY((for_user)) indirect_string_node {
216   const char *str;
217   unsigned int refcount;
218   enum dwarf_form form;
219   char *label;
220   unsigned int index;
221 };
222 
223 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
224 {
225   typedef const char *compare_type;
226 
227   static hashval_t hash (indirect_string_node *);
228   static bool equal (indirect_string_node *, const char *);
229 };
230 
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
232 
233 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
234 
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236    main object file, rather than the dwo, similar to the force_direct
237    parameter elsewhere but with additional complications:
238 
239    1) The string is needed in both the main object file and the dwo.
240    That is, the comp_dir and dwo_name will appear in both places.
241 
242    2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
243    DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
244 
245    3) GCC chooses the form to use late, depending on the size and
246    reference count.
247 
248    Rather than forcing the all debug string handling functions and
249    callers to deal with these complications, simply use a separate,
250    special-cased string table for any attribute that should go in the
251    main object file.  This limits the complexity to just the places
252    that need it.  */
253 
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
255 
256 static GTY(()) int dw2_string_counter;
257 
258 /* True if the compilation unit places functions in more than one section.  */
259 static GTY(()) bool have_multiple_function_sections = false;
260 
261 /* Whether the default text and cold text sections have been used at all.  */
262 static GTY(()) bool text_section_used = false;
263 static GTY(()) bool cold_text_section_used = false;
264 
265 /* The default cold text section.  */
266 static GTY(()) section *cold_text_section;
267 
268 /* The DIE for C++14 'auto' in a function return type.  */
269 static GTY(()) dw_die_ref auto_die;
270 
271 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
272 static GTY(()) dw_die_ref decltype_auto_die;
273 
274 /* Forward declarations for functions defined in this file.  */
275 
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
278 
279 /* Personality decl of current unit.  Used only when assembler does not support
280    personality CFI.  */
281 static GTY(()) rtx current_unit_personality;
282 
283 /* Whether an eh_frame section is required.  */
284 static GTY(()) bool do_eh_frame = false;
285 
286 /* .debug_rnglists next index.  */
287 static unsigned int rnglist_idx;
288 
289 /* Data and reference forms for relocatable data.  */
290 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
291 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
292 
293 #ifndef DEBUG_FRAME_SECTION
294 #define DEBUG_FRAME_SECTION	".debug_frame"
295 #endif
296 
297 #ifndef FUNC_BEGIN_LABEL
298 #define FUNC_BEGIN_LABEL	"LFB"
299 #endif
300 
301 #ifndef FUNC_SECOND_SECT_LABEL
302 #define FUNC_SECOND_SECT_LABEL	"LFSB"
303 #endif
304 
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL		"LFE"
307 #endif
308 
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL	"LPE"
311 #endif
312 
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL	"LEB"
315 #endif
316 
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL	"Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL	"LSCIE"
321 #define CIE_END_LABEL		"LECIE"
322 #define FDE_LABEL		"LSFDE"
323 #define FDE_AFTER_SIZE_LABEL	"LASFDE"
324 #define FDE_END_LABEL		"LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL	"LSLT"
326 #define LINE_NUMBER_END_LABEL	"LELT"
327 #define LN_PROLOG_AS_LABEL	"LASLTP"
328 #define LN_PROLOG_END_LABEL	"LELTP"
329 #define DIE_LABEL_PREFIX	"DW"
330 
331 /* Match the base name of a file to the base name of a compilation unit. */
332 
333 static int
matches_main_base(const char * path)334 matches_main_base (const char *path)
335 {
336   /* Cache the last query. */
337   static const char *last_path = NULL;
338   static int last_match = 0;
339   if (path != last_path)
340     {
341       const char *base;
342       int length = base_of_path (path, &base);
343       last_path = path;
344       last_match = (length == main_input_baselength
345                     && memcmp (base, main_input_basename, length) == 0);
346     }
347   return last_match;
348 }
349 
350 #ifdef DEBUG_DEBUG_STRUCT
351 
352 static int
dump_struct_debug(tree type,enum debug_info_usage usage,enum debug_struct_file criterion,int generic,int matches,int result)353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 		   enum debug_struct_file criterion, int generic,
355 		   int matches, int result)
356 {
357   /* Find the type name. */
358   tree type_decl = TYPE_STUB_DECL (type);
359   tree t = type_decl;
360   const char *name = 0;
361   if (TREE_CODE (t) == TYPE_DECL)
362     t = DECL_NAME (t);
363   if (t)
364     name = IDENTIFIER_POINTER (t);
365 
366   fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
367 	   criterion,
368            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369            matches ? "bas" : "hdr",
370            generic ? "gen" : "ord",
371            usage == DINFO_USAGE_DFN ? ";" :
372              usage == DINFO_USAGE_DIR_USE ? "." : "*",
373            result,
374            (void*) type_decl, name);
375   return result;
376 }
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378   dump_struct_debug (type, usage, criterion, generic, matches, result)
379 
380 #else
381 
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383   (result)
384 
385 #endif
386 
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388    of the number.  Some constants have a large uniform precision, so
389    we get the precision needed for the actual value 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::min_precision (op, UNSIGNED);
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 	ref = dw2_force_const_mem (ref, true);
995 
996       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
997       output_addr_const (asm_out_file, ref);
998       fputc ('\n', asm_out_file);
999     }
1000 
1001   if (crtl->uses_eh_lsda)
1002     {
1003       char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1004 
1005       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1006       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1007 				   current_function_funcdef_no);
1008       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1009       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1010 
1011       if (enc & DW_EH_PE_indirect)
1012 	ref = dw2_force_const_mem (ref, true);
1013 
1014       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1015       output_addr_const (asm_out_file, ref);
1016       fputc ('\n', asm_out_file);
1017     }
1018 }
1019 
1020 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1021    this allocation may be done before pass_final.  */
1022 
1023 dw_fde_ref
dwarf2out_alloc_current_fde(void)1024 dwarf2out_alloc_current_fde (void)
1025 {
1026   dw_fde_ref fde;
1027 
1028   fde = ggc_cleared_alloc<dw_fde_node> ();
1029   fde->decl = current_function_decl;
1030   fde->funcdef_number = current_function_funcdef_no;
1031   fde->fde_index = vec_safe_length (fde_vec);
1032   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1033   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1034   fde->nothrow = crtl->nothrow;
1035   fde->drap_reg = INVALID_REGNUM;
1036   fde->vdrap_reg = INVALID_REGNUM;
1037 
1038   /* Record the FDE associated with this function.  */
1039   cfun->fde = fde;
1040   vec_safe_push (fde_vec, fde);
1041 
1042   return fde;
1043 }
1044 
1045 /* Output a marker (i.e. a label) for the beginning of a function, before
1046    the prologue.  */
1047 
1048 void
dwarf2out_begin_prologue(unsigned int line ATTRIBUTE_UNUSED,unsigned int column ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1049 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1050 			  unsigned int column ATTRIBUTE_UNUSED,
1051 			  const char *file ATTRIBUTE_UNUSED)
1052 {
1053   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1054   char * dup_label;
1055   dw_fde_ref fde;
1056   section *fnsec;
1057   bool do_frame;
1058 
1059   current_function_func_begin_label = NULL;
1060 
1061   do_frame = dwarf2out_do_frame ();
1062 
1063   /* ??? current_function_func_begin_label is also used by except.c for
1064      call-site information.  We must emit this label if it might be used.  */
1065   if (!do_frame
1066       && (!flag_exceptions
1067 	  || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1068     return;
1069 
1070   fnsec = function_section (current_function_decl);
1071   switch_to_section (fnsec);
1072   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1073 			       current_function_funcdef_no);
1074   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1075 			  current_function_funcdef_no);
1076   dup_label = xstrdup (label);
1077   current_function_func_begin_label = dup_label;
1078 
1079   /* We can elide FDE allocation if we're not emitting frame unwind info.  */
1080   if (!do_frame)
1081     return;
1082 
1083   /* Unlike the debug version, the EH version of frame unwind info is a per-
1084      function setting so we need to record whether we need it for the unit.  */
1085   do_eh_frame |= dwarf2out_do_eh_frame ();
1086 
1087   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1088      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1089      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1090      do so now.  */
1091   fde = cfun->fde;
1092   if (fde == NULL)
1093     fde = dwarf2out_alloc_current_fde ();
1094 
1095   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1096   fde->dw_fde_begin = dup_label;
1097   fde->dw_fde_current_label = dup_label;
1098   fde->in_std_section = (fnsec == text_section
1099 			 || (cold_text_section && fnsec == cold_text_section));
1100 
1101   /* We only want to output line number information for the genuine dwarf2
1102      prologue case, not the eh frame case.  */
1103 #ifdef DWARF2_DEBUGGING_INFO
1104   if (file)
1105     dwarf2out_source_line (line, column, file, 0, true);
1106 #endif
1107 
1108   if (dwarf2out_do_cfi_asm ())
1109     dwarf2out_do_cfi_startproc (false);
1110   else
1111     {
1112       rtx personality = get_personality_function (current_function_decl);
1113       if (!current_unit_personality)
1114         current_unit_personality = personality;
1115 
1116       /* We cannot keep a current personality per function as without CFI
1117 	 asm, at the point where we emit the CFI data, there is no current
1118 	 function anymore.  */
1119       if (personality && current_unit_personality != personality)
1120 	sorry ("multiple EH personalities are supported only with assemblers "
1121 	       "supporting %<.cfi_personality%> directive");
1122     }
1123 }
1124 
1125 /* Output a marker (i.e. a label) for the end of the generated code
1126    for a function prologue.  This gets called *after* the prologue code has
1127    been generated.  */
1128 
1129 void
dwarf2out_vms_end_prologue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1130 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1131 			    const char *file ATTRIBUTE_UNUSED)
1132 {
1133   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 
1135   /* Output a label to mark the endpoint of the code generated for this
1136      function.  */
1137   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1138 			       current_function_funcdef_no);
1139   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1140 			  current_function_funcdef_no);
1141   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1142 }
1143 
1144 /* Output a marker (i.e. a label) for the beginning of the generated code
1145    for a function epilogue.  This gets called *before* the prologue code has
1146    been generated.  */
1147 
1148 void
dwarf2out_vms_begin_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1149 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1150 			  const char *file ATTRIBUTE_UNUSED)
1151 {
1152   dw_fde_ref fde = cfun->fde;
1153   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1154 
1155   if (fde->dw_fde_vms_begin_epilogue)
1156     return;
1157 
1158   /* Output a label to mark the endpoint of the code generated for this
1159      function.  */
1160   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1161 			       current_function_funcdef_no);
1162   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1163 			  current_function_funcdef_no);
1164   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1165 }
1166 
1167 /* Output a marker (i.e. a label) for the absolute end of the generated code
1168    for a function definition.  This gets called *after* the epilogue code has
1169    been generated.  */
1170 
1171 void
dwarf2out_end_epilogue(unsigned int line ATTRIBUTE_UNUSED,const char * file ATTRIBUTE_UNUSED)1172 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1173 			const char *file ATTRIBUTE_UNUSED)
1174 {
1175   dw_fde_ref fde;
1176   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1177 
1178   last_var_location_insn = NULL;
1179   cached_next_real_insn = NULL;
1180 
1181   if (dwarf2out_do_cfi_asm ())
1182     fprintf (asm_out_file, "\t.cfi_endproc\n");
1183 
1184   /* Output a label to mark the endpoint of the code generated for this
1185      function.  */
1186   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1187 			       current_function_funcdef_no);
1188   ASM_OUTPUT_LABEL (asm_out_file, label);
1189   fde = cfun->fde;
1190   gcc_assert (fde != NULL);
1191   if (fde->dw_fde_second_begin == NULL)
1192     fde->dw_fde_end = xstrdup (label);
1193 }
1194 
1195 void
dwarf2out_frame_finish(void)1196 dwarf2out_frame_finish (void)
1197 {
1198   /* Output call frame information.  */
1199   if (targetm.debug_unwind_info () == UI_DWARF2)
1200     output_call_frame_info (0);
1201 
1202   /* Output another copy for the unwinder.  */
1203   if (do_eh_frame)
1204     output_call_frame_info (1);
1205 }
1206 
1207 /* Note that the current function section is being used for code.  */
1208 
1209 static void
dwarf2out_note_section_used(void)1210 dwarf2out_note_section_used (void)
1211 {
1212   section *sec = current_function_section ();
1213   if (sec == text_section)
1214     text_section_used = true;
1215   else if (sec == cold_text_section)
1216     cold_text_section_used = true;
1217 }
1218 
1219 static void var_location_switch_text_section (void);
1220 static void set_cur_line_info_table (section *);
1221 
1222 void
dwarf2out_switch_text_section(void)1223 dwarf2out_switch_text_section (void)
1224 {
1225   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1226   section *sect;
1227   dw_fde_ref fde = cfun->fde;
1228 
1229   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1230 
1231   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1232 			       current_function_funcdef_no);
1233 
1234   fde->dw_fde_second_begin = ggc_strdup (label);
1235   if (!in_cold_section_p)
1236     {
1237       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1238       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1239     }
1240   else
1241     {
1242       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1243       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1244     }
1245   have_multiple_function_sections = true;
1246 
1247   /* There is no need to mark used sections when not debugging.  */
1248   if (cold_text_section != NULL)
1249     dwarf2out_note_section_used ();
1250 
1251   if (dwarf2out_do_cfi_asm ())
1252     fprintf (asm_out_file, "\t.cfi_endproc\n");
1253 
1254   /* Now do the real section switch.  */
1255   sect = current_function_section ();
1256   switch_to_section (sect);
1257 
1258   fde->second_in_std_section
1259     = (sect == text_section
1260        || (cold_text_section && sect == cold_text_section));
1261 
1262   if (dwarf2out_do_cfi_asm ())
1263     dwarf2out_do_cfi_startproc (true);
1264 
1265   var_location_switch_text_section ();
1266 
1267   if (cold_text_section != NULL)
1268     set_cur_line_info_table (sect);
1269 }
1270 
1271 /* And now, the subset of the debugging information support code necessary
1272    for emitting location expressions.  */
1273 
1274 /* Data about a single source file.  */
1275 struct GTY((for_user)) dwarf_file_data {
1276   const char * filename;
1277   int emitted_number;
1278 };
1279 
1280 /* Describe an entry into the .debug_addr section.  */
1281 
1282 enum ate_kind {
1283   ate_kind_rtx,
1284   ate_kind_rtx_dtprel,
1285   ate_kind_label
1286 };
1287 
1288 struct GTY((for_user)) addr_table_entry {
1289   enum ate_kind kind;
1290   unsigned int refcount;
1291   unsigned int index;
1292   union addr_table_entry_struct_union
1293     {
1294       rtx GTY ((tag ("0"))) rtl;
1295       char * GTY ((tag ("1"))) label;
1296     }
1297   GTY ((desc ("%1.kind"))) addr;
1298 };
1299 
1300 typedef unsigned int var_loc_view;
1301 
1302 /* Location lists are ranges + location descriptions for that range,
1303    so you can track variables that are in different places over
1304    their entire life.  */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306   dw_loc_list_ref dw_loc_next;
1307   const char *begin; /* Label and addr_entry for start of range */
1308   addr_table_entry *begin_entry;
1309   const char *end;  /* Label for end of range */
1310   char *ll_symbol; /* Label for beginning of location list.
1311 		      Only on head of list.  */
1312   char *vl_symbol; /* Label for beginning of view list.  Ditto.  */
1313   const char *section; /* Section this loclist is relative to */
1314   dw_loc_descr_ref expr;
1315   var_loc_view vbegin, vend;
1316   hashval_t hash;
1317   /* True if all addresses in this and subsequent lists are known to be
1318      resolved.  */
1319   bool resolved_addr;
1320   /* True if this list has been replaced by dw_loc_next.  */
1321   bool replaced;
1322   /* True if it has been emitted into .debug_loc* / .debug_loclists*
1323      section.  */
1324   unsigned char emitted : 1;
1325   /* True if hash field is index rather than hash value.  */
1326   unsigned char num_assigned : 1;
1327   /* True if .debug_loclists.dwo offset has been emitted for it already.  */
1328   unsigned char offset_emitted : 1;
1329   /* True if note_variable_value_in_expr has been called on it.  */
1330   unsigned char noted_variable_value : 1;
1331   /* True if the range should be emitted even if begin and end
1332      are the same.  */
1333   bool force;
1334 } dw_loc_list_node;
1335 
1336 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1337 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1338 
1339 /* Convert a DWARF stack opcode into its string name.  */
1340 
1341 static const char *
dwarf_stack_op_name(unsigned int op)1342 dwarf_stack_op_name (unsigned int op)
1343 {
1344   const char *name = get_DW_OP_name (op);
1345 
1346   if (name != NULL)
1347     return name;
1348 
1349   return "OP_<unknown>";
1350 }
1351 
1352 /* Return TRUE iff we're to output location view lists as a separate
1353    attribute next to the location lists, as an extension compatible
1354    with DWARF 2 and above.  */
1355 
1356 static inline bool
dwarf2out_locviews_in_attribute()1357 dwarf2out_locviews_in_attribute ()
1358 {
1359   return debug_variable_location_views == 1;
1360 }
1361 
1362 /* Return TRUE iff we're to output location view lists as part of the
1363    location lists, as proposed for standardization after DWARF 5.  */
1364 
1365 static inline bool
dwarf2out_locviews_in_loclist()1366 dwarf2out_locviews_in_loclist ()
1367 {
1368 #ifndef DW_LLE_view_pair
1369   return false;
1370 #else
1371   return debug_variable_location_views == -1;
1372 #endif
1373 }
1374 
1375 /* Return a pointer to a newly allocated location description.  Location
1376    descriptions are simple expression terms that can be strung
1377    together to form more complicated location (address) descriptions.  */
1378 
1379 static inline dw_loc_descr_ref
new_loc_descr(enum dwarf_location_atom op,unsigned HOST_WIDE_INT oprnd1,unsigned HOST_WIDE_INT oprnd2)1380 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1381 	       unsigned HOST_WIDE_INT oprnd2)
1382 {
1383   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1384 
1385   descr->dw_loc_opc = op;
1386   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1387   descr->dw_loc_oprnd1.val_entry = NULL;
1388   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1389   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1390   descr->dw_loc_oprnd2.val_entry = NULL;
1391   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1392 
1393   return descr;
1394 }
1395 
1396 /* Add a location description term to a location description expression.  */
1397 
1398 static inline void
add_loc_descr(dw_loc_descr_ref * list_head,dw_loc_descr_ref descr)1399 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1400 {
1401   dw_loc_descr_ref *d;
1402 
1403   /* Find the end of the chain.  */
1404   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1405     ;
1406 
1407   *d = descr;
1408 }
1409 
1410 /* Compare two location operands for exact equality.  */
1411 
1412 static bool
dw_val_equal_p(dw_val_node * a,dw_val_node * b)1413 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1414 {
1415   if (a->val_class != b->val_class)
1416     return false;
1417   switch (a->val_class)
1418     {
1419     case dw_val_class_none:
1420       return true;
1421     case dw_val_class_addr:
1422       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1423 
1424     case dw_val_class_offset:
1425     case dw_val_class_unsigned_const:
1426     case dw_val_class_const:
1427     case dw_val_class_unsigned_const_implicit:
1428     case dw_val_class_const_implicit:
1429     case dw_val_class_range_list:
1430       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1431       return a->v.val_unsigned == b->v.val_unsigned;
1432 
1433     case dw_val_class_loc:
1434       return a->v.val_loc == b->v.val_loc;
1435     case dw_val_class_loc_list:
1436       return a->v.val_loc_list == b->v.val_loc_list;
1437     case dw_val_class_view_list:
1438       return a->v.val_view_list == b->v.val_view_list;
1439     case dw_val_class_die_ref:
1440       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1441     case dw_val_class_fde_ref:
1442       return a->v.val_fde_index == b->v.val_fde_index;
1443     case dw_val_class_symview:
1444       return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1445     case dw_val_class_lbl_id:
1446     case dw_val_class_lineptr:
1447     case dw_val_class_macptr:
1448     case dw_val_class_loclistsptr:
1449     case dw_val_class_high_pc:
1450       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1451     case dw_val_class_str:
1452       return a->v.val_str == b->v.val_str;
1453     case dw_val_class_flag:
1454       return a->v.val_flag == b->v.val_flag;
1455     case dw_val_class_file:
1456     case dw_val_class_file_implicit:
1457       return a->v.val_file == b->v.val_file;
1458     case dw_val_class_decl_ref:
1459       return a->v.val_decl_ref == b->v.val_decl_ref;
1460 
1461     case dw_val_class_const_double:
1462       return (a->v.val_double.high == b->v.val_double.high
1463 	      && a->v.val_double.low == b->v.val_double.low);
1464 
1465     case dw_val_class_wide_int:
1466       return *a->v.val_wide == *b->v.val_wide;
1467 
1468     case dw_val_class_vec:
1469       {
1470 	size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1471 	size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1472 
1473 	return (a_len == b_len
1474 		&& !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1475       }
1476 
1477     case dw_val_class_data8:
1478       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1479 
1480     case dw_val_class_vms_delta:
1481       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1482 	      && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1483 
1484     case dw_val_class_discr_value:
1485       return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1486 	      && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1487     case dw_val_class_discr_list:
1488       /* It makes no sense comparing two discriminant value lists.  */
1489       return false;
1490     }
1491   gcc_unreachable ();
1492 }
1493 
1494 /* Compare two location atoms for exact equality.  */
1495 
1496 static bool
loc_descr_equal_p_1(dw_loc_descr_ref a,dw_loc_descr_ref b)1497 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1498 {
1499   if (a->dw_loc_opc != b->dw_loc_opc)
1500     return false;
1501 
1502   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1503      address size, but since we always allocate cleared storage it
1504      should be zero for other types of locations.  */
1505   if (a->dtprel != b->dtprel)
1506     return false;
1507 
1508   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1509 	  && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1510 }
1511 
1512 /* Compare two complete location expressions for exact equality.  */
1513 
1514 bool
loc_descr_equal_p(dw_loc_descr_ref a,dw_loc_descr_ref b)1515 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1516 {
1517   while (1)
1518     {
1519       if (a == b)
1520 	return true;
1521       if (a == NULL || b == NULL)
1522 	return false;
1523       if (!loc_descr_equal_p_1 (a, b))
1524 	return false;
1525 
1526       a = a->dw_loc_next;
1527       b = b->dw_loc_next;
1528     }
1529 }
1530 
1531 
1532 /* Add a constant POLY_OFFSET to a location expression.  */
1533 
1534 static void
loc_descr_plus_const(dw_loc_descr_ref * list_head,poly_int64 poly_offset)1535 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1536 {
1537   dw_loc_descr_ref loc;
1538   HOST_WIDE_INT *p;
1539 
1540   gcc_assert (*list_head != NULL);
1541 
1542   if (known_eq (poly_offset, 0))
1543     return;
1544 
1545   /* Find the end of the chain.  */
1546   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1547     ;
1548 
1549   HOST_WIDE_INT offset;
1550   if (!poly_offset.is_constant (&offset))
1551     {
1552       loc->dw_loc_next = int_loc_descriptor (poly_offset);
1553       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1554       return;
1555     }
1556 
1557   p = NULL;
1558   if (loc->dw_loc_opc == DW_OP_fbreg
1559       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1560     p = &loc->dw_loc_oprnd1.v.val_int;
1561   else if (loc->dw_loc_opc == DW_OP_bregx)
1562     p = &loc->dw_loc_oprnd2.v.val_int;
1563 
1564   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1565      offset.  Don't optimize if an signed integer overflow would happen.  */
1566   if (p != NULL
1567       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1568 	  || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1569     *p += offset;
1570 
1571   else if (offset > 0)
1572     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1573 
1574   else
1575     {
1576       loc->dw_loc_next
1577 	= uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1578       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1579     }
1580 }
1581 
1582 /* Return a pointer to a newly allocated location description for
1583    REG and OFFSET.  */
1584 
1585 static inline dw_loc_descr_ref
new_reg_loc_descr(unsigned int reg,poly_int64 offset)1586 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1587 {
1588   HOST_WIDE_INT const_offset;
1589   if (offset.is_constant (&const_offset))
1590     {
1591       if (reg <= 31)
1592 	return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1593 			      const_offset, 0);
1594       else
1595 	return new_loc_descr (DW_OP_bregx, reg, const_offset);
1596     }
1597   else
1598     {
1599       dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1600       loc_descr_plus_const (&ret, offset);
1601       return ret;
1602     }
1603 }
1604 
1605 /* Add a constant OFFSET to a location list.  */
1606 
1607 static void
loc_list_plus_const(dw_loc_list_ref list_head,poly_int64 offset)1608 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1609 {
1610   dw_loc_list_ref d;
1611   for (d = list_head; d != NULL; d = d->dw_loc_next)
1612     loc_descr_plus_const (&d->expr, offset);
1613 }
1614 
1615 #define DWARF_REF_SIZE	\
1616   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1617 
1618 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1619    In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1620    DW_FORM_data16 with 128 bits.  */
1621 #define DWARF_LARGEST_DATA_FORM_BITS \
1622   (dwarf_version >= 5 ? 128 : 64)
1623 
1624 /* Utility inline function for construction of ops that were GNU extension
1625    before DWARF 5.  */
1626 static inline enum dwarf_location_atom
dwarf_OP(enum dwarf_location_atom op)1627 dwarf_OP (enum dwarf_location_atom op)
1628 {
1629   switch (op)
1630     {
1631     case DW_OP_implicit_pointer:
1632       if (dwarf_version < 5)
1633 	return DW_OP_GNU_implicit_pointer;
1634       break;
1635 
1636     case DW_OP_entry_value:
1637       if (dwarf_version < 5)
1638 	return DW_OP_GNU_entry_value;
1639       break;
1640 
1641     case DW_OP_const_type:
1642       if (dwarf_version < 5)
1643 	return DW_OP_GNU_const_type;
1644       break;
1645 
1646     case DW_OP_regval_type:
1647       if (dwarf_version < 5)
1648 	return DW_OP_GNU_regval_type;
1649       break;
1650 
1651     case DW_OP_deref_type:
1652       if (dwarf_version < 5)
1653 	return DW_OP_GNU_deref_type;
1654       break;
1655 
1656     case DW_OP_convert:
1657       if (dwarf_version < 5)
1658 	return DW_OP_GNU_convert;
1659       break;
1660 
1661     case DW_OP_reinterpret:
1662       if (dwarf_version < 5)
1663 	return DW_OP_GNU_reinterpret;
1664       break;
1665 
1666     case DW_OP_addrx:
1667       if (dwarf_version < 5)
1668 	return DW_OP_GNU_addr_index;
1669       break;
1670 
1671     case DW_OP_constx:
1672       if (dwarf_version < 5)
1673 	return DW_OP_GNU_const_index;
1674       break;
1675 
1676     default:
1677       break;
1678     }
1679   return op;
1680 }
1681 
1682 /* Similarly for attributes.  */
1683 static inline enum dwarf_attribute
dwarf_AT(enum dwarf_attribute at)1684 dwarf_AT (enum dwarf_attribute at)
1685 {
1686   switch (at)
1687     {
1688     case DW_AT_call_return_pc:
1689       if (dwarf_version < 5)
1690 	return DW_AT_low_pc;
1691       break;
1692 
1693     case DW_AT_call_tail_call:
1694       if (dwarf_version < 5)
1695 	return DW_AT_GNU_tail_call;
1696       break;
1697 
1698     case DW_AT_call_origin:
1699       if (dwarf_version < 5)
1700 	return DW_AT_abstract_origin;
1701       break;
1702 
1703     case DW_AT_call_target:
1704       if (dwarf_version < 5)
1705 	return DW_AT_GNU_call_site_target;
1706       break;
1707 
1708     case DW_AT_call_target_clobbered:
1709       if (dwarf_version < 5)
1710 	return DW_AT_GNU_call_site_target_clobbered;
1711       break;
1712 
1713     case DW_AT_call_parameter:
1714       if (dwarf_version < 5)
1715 	return DW_AT_abstract_origin;
1716       break;
1717 
1718     case DW_AT_call_value:
1719       if (dwarf_version < 5)
1720 	return DW_AT_GNU_call_site_value;
1721       break;
1722 
1723     case DW_AT_call_data_value:
1724       if (dwarf_version < 5)
1725 	return DW_AT_GNU_call_site_data_value;
1726       break;
1727 
1728     case DW_AT_call_all_calls:
1729       if (dwarf_version < 5)
1730 	return DW_AT_GNU_all_call_sites;
1731       break;
1732 
1733     case DW_AT_call_all_tail_calls:
1734       if (dwarf_version < 5)
1735 	return DW_AT_GNU_all_tail_call_sites;
1736       break;
1737 
1738     case DW_AT_dwo_name:
1739       if (dwarf_version < 5)
1740 	return DW_AT_GNU_dwo_name;
1741       break;
1742 
1743     case DW_AT_addr_base:
1744       if (dwarf_version < 5)
1745 	return DW_AT_GNU_addr_base;
1746       break;
1747 
1748     default:
1749       break;
1750     }
1751   return at;
1752 }
1753 
1754 /* And similarly for tags.  */
1755 static inline enum dwarf_tag
dwarf_TAG(enum dwarf_tag tag)1756 dwarf_TAG (enum dwarf_tag tag)
1757 {
1758   switch (tag)
1759     {
1760     case DW_TAG_call_site:
1761       if (dwarf_version < 5)
1762 	return DW_TAG_GNU_call_site;
1763       break;
1764 
1765     case DW_TAG_call_site_parameter:
1766       if (dwarf_version < 5)
1767 	return DW_TAG_GNU_call_site_parameter;
1768       break;
1769 
1770     default:
1771       break;
1772     }
1773   return tag;
1774 }
1775 
1776 /* And similarly for forms.  */
1777 static inline enum dwarf_form
dwarf_FORM(enum dwarf_form form)1778 dwarf_FORM (enum dwarf_form form)
1779 {
1780   switch (form)
1781     {
1782     case DW_FORM_addrx:
1783       if (dwarf_version < 5)
1784 	return DW_FORM_GNU_addr_index;
1785       break;
1786 
1787     case DW_FORM_strx:
1788       if (dwarf_version < 5)
1789 	return DW_FORM_GNU_str_index;
1790       break;
1791 
1792     default:
1793       break;
1794     }
1795   return form;
1796 }
1797 
1798 static unsigned long int get_base_type_offset (dw_die_ref);
1799 
1800 /* Return the size of a location descriptor.  */
1801 
1802 static unsigned long
size_of_loc_descr(dw_loc_descr_ref loc)1803 size_of_loc_descr (dw_loc_descr_ref loc)
1804 {
1805   unsigned long size = 1;
1806 
1807   switch (loc->dw_loc_opc)
1808     {
1809     case DW_OP_addr:
1810       size += DWARF2_ADDR_SIZE;
1811       break;
1812     case DW_OP_GNU_addr_index:
1813     case DW_OP_addrx:
1814     case DW_OP_GNU_const_index:
1815     case DW_OP_constx:
1816       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1817       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1818       break;
1819     case DW_OP_const1u:
1820     case DW_OP_const1s:
1821       size += 1;
1822       break;
1823     case DW_OP_const2u:
1824     case DW_OP_const2s:
1825       size += 2;
1826       break;
1827     case DW_OP_const4u:
1828     case DW_OP_const4s:
1829       size += 4;
1830       break;
1831     case DW_OP_const8u:
1832     case DW_OP_const8s:
1833       size += 8;
1834       break;
1835     case DW_OP_constu:
1836       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1837       break;
1838     case DW_OP_consts:
1839       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1840       break;
1841     case DW_OP_pick:
1842       size += 1;
1843       break;
1844     case DW_OP_plus_uconst:
1845       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1846       break;
1847     case DW_OP_skip:
1848     case DW_OP_bra:
1849       size += 2;
1850       break;
1851     case DW_OP_breg0:
1852     case DW_OP_breg1:
1853     case DW_OP_breg2:
1854     case DW_OP_breg3:
1855     case DW_OP_breg4:
1856     case DW_OP_breg5:
1857     case DW_OP_breg6:
1858     case DW_OP_breg7:
1859     case DW_OP_breg8:
1860     case DW_OP_breg9:
1861     case DW_OP_breg10:
1862     case DW_OP_breg11:
1863     case DW_OP_breg12:
1864     case DW_OP_breg13:
1865     case DW_OP_breg14:
1866     case DW_OP_breg15:
1867     case DW_OP_breg16:
1868     case DW_OP_breg17:
1869     case DW_OP_breg18:
1870     case DW_OP_breg19:
1871     case DW_OP_breg20:
1872     case DW_OP_breg21:
1873     case DW_OP_breg22:
1874     case DW_OP_breg23:
1875     case DW_OP_breg24:
1876     case DW_OP_breg25:
1877     case DW_OP_breg26:
1878     case DW_OP_breg27:
1879     case DW_OP_breg28:
1880     case DW_OP_breg29:
1881     case DW_OP_breg30:
1882     case DW_OP_breg31:
1883       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1884       break;
1885     case DW_OP_regx:
1886       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1887       break;
1888     case DW_OP_fbreg:
1889       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1890       break;
1891     case DW_OP_bregx:
1892       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1893       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1894       break;
1895     case DW_OP_piece:
1896       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1897       break;
1898     case DW_OP_bit_piece:
1899       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1900       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1901       break;
1902     case DW_OP_deref_size:
1903     case DW_OP_xderef_size:
1904       size += 1;
1905       break;
1906     case DW_OP_call2:
1907       size += 2;
1908       break;
1909     case DW_OP_call4:
1910       size += 4;
1911       break;
1912     case DW_OP_call_ref:
1913     case DW_OP_GNU_variable_value:
1914       size += DWARF_REF_SIZE;
1915       break;
1916     case DW_OP_implicit_value:
1917       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1918 	      + loc->dw_loc_oprnd1.v.val_unsigned;
1919       break;
1920     case DW_OP_implicit_pointer:
1921     case DW_OP_GNU_implicit_pointer:
1922       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1923       break;
1924     case DW_OP_entry_value:
1925     case DW_OP_GNU_entry_value:
1926       {
1927 	unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1928 	size += size_of_uleb128 (op_size) + op_size;
1929 	break;
1930       }
1931     case DW_OP_const_type:
1932     case DW_OP_GNU_const_type:
1933       {
1934 	unsigned long o
1935 	  = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1936 	size += size_of_uleb128 (o) + 1;
1937 	switch (loc->dw_loc_oprnd2.val_class)
1938 	  {
1939 	  case dw_val_class_vec:
1940 	    size += loc->dw_loc_oprnd2.v.val_vec.length
1941 		    * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1942 	    break;
1943 	  case dw_val_class_const:
1944 	    size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1945 	    break;
1946 	  case dw_val_class_const_double:
1947 	    size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1948 	    break;
1949 	  case dw_val_class_wide_int:
1950 	    size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1951 		     * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1952 	    break;
1953 	  default:
1954 	    gcc_unreachable ();
1955 	  }
1956 	break;
1957       }
1958     case DW_OP_regval_type:
1959     case DW_OP_GNU_regval_type:
1960       {
1961 	unsigned long o
1962 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1963 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1964 		+ size_of_uleb128 (o);
1965       }
1966       break;
1967     case DW_OP_deref_type:
1968     case DW_OP_GNU_deref_type:
1969       {
1970 	unsigned long o
1971 	  = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1972 	size += 1 + size_of_uleb128 (o);
1973       }
1974       break;
1975     case DW_OP_convert:
1976     case DW_OP_reinterpret:
1977     case DW_OP_GNU_convert:
1978     case DW_OP_GNU_reinterpret:
1979       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1980 	size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1981       else
1982 	{
1983 	  unsigned long o
1984 	    = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1985 	  size += size_of_uleb128 (o);
1986 	}
1987       break;
1988     case DW_OP_GNU_parameter_ref:
1989       size += 4;
1990       break;
1991     default:
1992       break;
1993     }
1994 
1995   return size;
1996 }
1997 
1998 /* Return the size of a series of location descriptors.  */
1999 
2000 unsigned long
size_of_locs(dw_loc_descr_ref loc)2001 size_of_locs (dw_loc_descr_ref loc)
2002 {
2003   dw_loc_descr_ref l;
2004   unsigned long size;
2005 
2006   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2007      field, to avoid writing to a PCH file.  */
2008   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2009     {
2010       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2011 	break;
2012       size += size_of_loc_descr (l);
2013     }
2014   if (! l)
2015     return size;
2016 
2017   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2018     {
2019       l->dw_loc_addr = size;
2020       size += size_of_loc_descr (l);
2021     }
2022 
2023   return size;
2024 }
2025 
2026 /* Return the size of the value in a DW_AT_discr_value attribute.  */
2027 
2028 static int
size_of_discr_value(dw_discr_value * discr_value)2029 size_of_discr_value (dw_discr_value *discr_value)
2030 {
2031   if (discr_value->pos)
2032     return size_of_uleb128 (discr_value->v.uval);
2033   else
2034     return size_of_sleb128 (discr_value->v.sval);
2035 }
2036 
2037 /* Return the size of the value in a DW_AT_discr_list attribute.  */
2038 
2039 static int
size_of_discr_list(dw_discr_list_ref discr_list)2040 size_of_discr_list (dw_discr_list_ref discr_list)
2041 {
2042   int size = 0;
2043 
2044   for (dw_discr_list_ref list = discr_list;
2045        list != NULL;
2046        list = list->dw_discr_next)
2047     {
2048       /* One byte for the discriminant value descriptor, and then one or two
2049 	 LEB128 numbers, depending on whether it's a single case label or a
2050 	 range label.  */
2051       size += 1;
2052       size += size_of_discr_value (&list->dw_discr_lower_bound);
2053       if (list->dw_discr_range != 0)
2054 	size += size_of_discr_value (&list->dw_discr_upper_bound);
2055     }
2056   return size;
2057 }
2058 
2059 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2060 static void get_ref_die_offset_label (char *, dw_die_ref);
2061 static unsigned long int get_ref_die_offset (dw_die_ref);
2062 
2063 /* Output location description stack opcode's operands (if any).
2064    The for_eh_or_skip parameter controls whether register numbers are
2065    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2066    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2067    info).  This should be suppressed for the cases that have not been converted
2068    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2069 
2070 static void
output_loc_operands(dw_loc_descr_ref loc,int for_eh_or_skip)2071 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2072 {
2073   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2074   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2075 
2076   switch (loc->dw_loc_opc)
2077     {
2078 #ifdef DWARF2_DEBUGGING_INFO
2079     case DW_OP_const2u:
2080     case DW_OP_const2s:
2081       dw2_asm_output_data (2, val1->v.val_int, NULL);
2082       break;
2083     case DW_OP_const4u:
2084       if (loc->dtprel)
2085 	{
2086 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2087 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2088 					       val1->v.val_addr);
2089 	  fputc ('\n', asm_out_file);
2090 	  break;
2091 	}
2092       /* FALLTHRU */
2093     case DW_OP_const4s:
2094       dw2_asm_output_data (4, val1->v.val_int, NULL);
2095       break;
2096     case DW_OP_const8u:
2097       if (loc->dtprel)
2098 	{
2099 	  gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2100 	  targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2101 					       val1->v.val_addr);
2102 	  fputc ('\n', asm_out_file);
2103 	  break;
2104 	}
2105       /* FALLTHRU */
2106     case DW_OP_const8s:
2107       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2108       dw2_asm_output_data (8, val1->v.val_int, NULL);
2109       break;
2110     case DW_OP_skip:
2111     case DW_OP_bra:
2112       {
2113 	int offset;
2114 
2115 	gcc_assert (val1->val_class == dw_val_class_loc);
2116 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2117 
2118 	dw2_asm_output_data (2, offset, NULL);
2119       }
2120       break;
2121     case DW_OP_implicit_value:
2122       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2123       switch (val2->val_class)
2124 	{
2125 	case dw_val_class_const:
2126 	  dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2127 	  break;
2128 	case dw_val_class_vec:
2129 	  {
2130 	    unsigned int elt_size = val2->v.val_vec.elt_size;
2131 	    unsigned int len = val2->v.val_vec.length;
2132 	    unsigned int i;
2133 	    unsigned char *p;
2134 
2135 	    if (elt_size > sizeof (HOST_WIDE_INT))
2136 	      {
2137 		elt_size /= 2;
2138 		len *= 2;
2139 	      }
2140 	    for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2141 		 i < len;
2142 		 i++, p += elt_size)
2143 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2144 				   "fp or vector constant word %u", i);
2145 	  }
2146 	  break;
2147 	case dw_val_class_const_double:
2148 	  {
2149 	    unsigned HOST_WIDE_INT first, second;
2150 
2151 	    if (WORDS_BIG_ENDIAN)
2152 	      {
2153 		first = val2->v.val_double.high;
2154 		second = val2->v.val_double.low;
2155 	      }
2156 	    else
2157 	      {
2158 		first = val2->v.val_double.low;
2159 		second = val2->v.val_double.high;
2160 	      }
2161 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2162 				 first, NULL);
2163 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2164 				 second, NULL);
2165 	  }
2166 	  break;
2167 	case dw_val_class_wide_int:
2168 	  {
2169 	    int i;
2170 	    int len = get_full_len (*val2->v.val_wide);
2171 	    if (WORDS_BIG_ENDIAN)
2172 	      for (i = len - 1; i >= 0; --i)
2173 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2174 				     val2->v.val_wide->elt (i), NULL);
2175 	    else
2176 	      for (i = 0; i < len; ++i)
2177 		dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2178 				     val2->v.val_wide->elt (i), NULL);
2179 	  }
2180 	  break;
2181 	case dw_val_class_addr:
2182 	  gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2183 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2184 	  break;
2185 	default:
2186 	  gcc_unreachable ();
2187 	}
2188       break;
2189 #else
2190     case DW_OP_const2u:
2191     case DW_OP_const2s:
2192     case DW_OP_const4u:
2193     case DW_OP_const4s:
2194     case DW_OP_const8u:
2195     case DW_OP_const8s:
2196     case DW_OP_skip:
2197     case DW_OP_bra:
2198     case DW_OP_implicit_value:
2199       /* We currently don't make any attempt to make sure these are
2200 	 aligned properly like we do for the main unwind info, so
2201 	 don't support emitting things larger than a byte if we're
2202 	 only doing unwinding.  */
2203       gcc_unreachable ();
2204 #endif
2205     case DW_OP_const1u:
2206     case DW_OP_const1s:
2207       dw2_asm_output_data (1, val1->v.val_int, NULL);
2208       break;
2209     case DW_OP_constu:
2210       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2211       break;
2212     case DW_OP_consts:
2213       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2214       break;
2215     case DW_OP_pick:
2216       dw2_asm_output_data (1, val1->v.val_int, NULL);
2217       break;
2218     case DW_OP_plus_uconst:
2219       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2220       break;
2221     case DW_OP_breg0:
2222     case DW_OP_breg1:
2223     case DW_OP_breg2:
2224     case DW_OP_breg3:
2225     case DW_OP_breg4:
2226     case DW_OP_breg5:
2227     case DW_OP_breg6:
2228     case DW_OP_breg7:
2229     case DW_OP_breg8:
2230     case DW_OP_breg9:
2231     case DW_OP_breg10:
2232     case DW_OP_breg11:
2233     case DW_OP_breg12:
2234     case DW_OP_breg13:
2235     case DW_OP_breg14:
2236     case DW_OP_breg15:
2237     case DW_OP_breg16:
2238     case DW_OP_breg17:
2239     case DW_OP_breg18:
2240     case DW_OP_breg19:
2241     case DW_OP_breg20:
2242     case DW_OP_breg21:
2243     case DW_OP_breg22:
2244     case DW_OP_breg23:
2245     case DW_OP_breg24:
2246     case DW_OP_breg25:
2247     case DW_OP_breg26:
2248     case DW_OP_breg27:
2249     case DW_OP_breg28:
2250     case DW_OP_breg29:
2251     case DW_OP_breg30:
2252     case DW_OP_breg31:
2253       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2254       break;
2255     case DW_OP_regx:
2256       {
2257 	unsigned r = val1->v.val_unsigned;
2258 	if (for_eh_or_skip >= 0)
2259 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2260 	gcc_assert (size_of_uleb128 (r)
2261 		    == size_of_uleb128 (val1->v.val_unsigned));
2262 	dw2_asm_output_data_uleb128 (r, NULL);
2263       }
2264       break;
2265     case DW_OP_fbreg:
2266       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2267       break;
2268     case DW_OP_bregx:
2269       {
2270 	unsigned r = val1->v.val_unsigned;
2271 	if (for_eh_or_skip >= 0)
2272 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2273 	gcc_assert (size_of_uleb128 (r)
2274 		    == size_of_uleb128 (val1->v.val_unsigned));
2275 	dw2_asm_output_data_uleb128 (r, NULL);
2276 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2277       }
2278       break;
2279     case DW_OP_piece:
2280       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2281       break;
2282     case DW_OP_bit_piece:
2283       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2284       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2285       break;
2286     case DW_OP_deref_size:
2287     case DW_OP_xderef_size:
2288       dw2_asm_output_data (1, val1->v.val_int, NULL);
2289       break;
2290 
2291     case DW_OP_addr:
2292       if (loc->dtprel)
2293 	{
2294 	  if (targetm.asm_out.output_dwarf_dtprel)
2295 	    {
2296 	      targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2297 						   DWARF2_ADDR_SIZE,
2298 						   val1->v.val_addr);
2299 	      fputc ('\n', asm_out_file);
2300 	    }
2301 	  else
2302 	    gcc_unreachable ();
2303 	}
2304       else
2305 	{
2306 #ifdef DWARF2_DEBUGGING_INFO
2307 	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2308 #else
2309 	  gcc_unreachable ();
2310 #endif
2311 	}
2312       break;
2313 
2314     case DW_OP_GNU_addr_index:
2315     case DW_OP_addrx:
2316     case DW_OP_GNU_const_index:
2317     case DW_OP_constx:
2318       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2319       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2320                                    "(index into .debug_addr)");
2321       break;
2322 
2323     case DW_OP_call2:
2324     case DW_OP_call4:
2325       {
2326 	unsigned long die_offset
2327 	  = get_ref_die_offset (val1->v.val_die_ref.die);
2328 	/* Make sure the offset has been computed and that we can encode it as
2329 	   an operand.  */
2330 	gcc_assert (die_offset > 0
2331 		    && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2332 				     ? 0xffff
2333 				     : 0xffffffff));
2334 	dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2335 			     die_offset, NULL);
2336       }
2337       break;
2338 
2339     case DW_OP_call_ref:
2340     case DW_OP_GNU_variable_value:
2341       {
2342 	char label[MAX_ARTIFICIAL_LABEL_BYTES
2343 		   + HOST_BITS_PER_WIDE_INT / 2 + 2];
2344 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2345 	get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2346 	dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2347       }
2348       break;
2349 
2350     case DW_OP_implicit_pointer:
2351     case DW_OP_GNU_implicit_pointer:
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 	dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2359       }
2360       break;
2361 
2362     case DW_OP_entry_value:
2363     case DW_OP_GNU_entry_value:
2364       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2365       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2366       break;
2367 
2368     case DW_OP_const_type:
2369     case DW_OP_GNU_const_type:
2370       {
2371 	unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2372 	gcc_assert (o);
2373 	dw2_asm_output_data_uleb128 (o, NULL);
2374 	switch (val2->val_class)
2375 	  {
2376 	  case dw_val_class_const:
2377 	    l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2378 	    dw2_asm_output_data (1, l, NULL);
2379 	    dw2_asm_output_data (l, val2->v.val_int, NULL);
2380 	    break;
2381 	  case dw_val_class_vec:
2382 	    {
2383 	      unsigned int elt_size = val2->v.val_vec.elt_size;
2384 	      unsigned int len = val2->v.val_vec.length;
2385 	      unsigned int i;
2386 	      unsigned char *p;
2387 
2388 	      l = len * elt_size;
2389 	      dw2_asm_output_data (1, l, NULL);
2390 	      if (elt_size > sizeof (HOST_WIDE_INT))
2391 		{
2392 		  elt_size /= 2;
2393 		  len *= 2;
2394 		}
2395 	      for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2396 		   i < len;
2397 		   i++, p += elt_size)
2398 		dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2399 				     "fp or vector constant word %u", i);
2400 	    }
2401 	    break;
2402 	  case dw_val_class_const_double:
2403 	    {
2404 	      unsigned HOST_WIDE_INT first, second;
2405 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2406 
2407 	      dw2_asm_output_data (1, 2 * l, NULL);
2408 	      if (WORDS_BIG_ENDIAN)
2409 		{
2410 		  first = val2->v.val_double.high;
2411 		  second = val2->v.val_double.low;
2412 		}
2413 	      else
2414 		{
2415 		  first = val2->v.val_double.low;
2416 		  second = val2->v.val_double.high;
2417 		}
2418 	      dw2_asm_output_data (l, first, NULL);
2419 	      dw2_asm_output_data (l, second, NULL);
2420 	    }
2421 	    break;
2422 	  case dw_val_class_wide_int:
2423 	    {
2424 	      int i;
2425 	      int len = get_full_len (*val2->v.val_wide);
2426 	      l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2427 
2428 	      dw2_asm_output_data (1, len * l, NULL);
2429 	      if (WORDS_BIG_ENDIAN)
2430 		for (i = len - 1; i >= 0; --i)
2431 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2432 	      else
2433 		for (i = 0; i < len; ++i)
2434 		  dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2435 	    }
2436 	    break;
2437 	  default:
2438 	    gcc_unreachable ();
2439 	  }
2440       }
2441       break;
2442     case DW_OP_regval_type:
2443     case DW_OP_GNU_regval_type:
2444       {
2445 	unsigned r = val1->v.val_unsigned;
2446 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2447 	gcc_assert (o);
2448 	if (for_eh_or_skip >= 0)
2449 	  {
2450 	    r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2451 	    gcc_assert (size_of_uleb128 (r)
2452 			== size_of_uleb128 (val1->v.val_unsigned));
2453 	  }
2454 	dw2_asm_output_data_uleb128 (r, NULL);
2455 	dw2_asm_output_data_uleb128 (o, NULL);
2456       }
2457       break;
2458     case DW_OP_deref_type:
2459     case DW_OP_GNU_deref_type:
2460       {
2461 	unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2462 	gcc_assert (o);
2463 	dw2_asm_output_data (1, val1->v.val_int, NULL);
2464 	dw2_asm_output_data_uleb128 (o, NULL);
2465       }
2466       break;
2467     case DW_OP_convert:
2468     case DW_OP_reinterpret:
2469     case DW_OP_GNU_convert:
2470     case DW_OP_GNU_reinterpret:
2471       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2472 	dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2473       else
2474 	{
2475 	  unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2476 	  gcc_assert (o);
2477 	  dw2_asm_output_data_uleb128 (o, NULL);
2478 	}
2479       break;
2480 
2481     case DW_OP_GNU_parameter_ref:
2482       {
2483 	unsigned long o;
2484 	gcc_assert (val1->val_class == dw_val_class_die_ref);
2485 	o = get_ref_die_offset (val1->v.val_die_ref.die);
2486 	dw2_asm_output_data (4, o, NULL);
2487       }
2488       break;
2489 
2490     default:
2491       /* Other codes have no operands.  */
2492       break;
2493     }
2494 }
2495 
2496 /* Output a sequence of location operations.
2497    The for_eh_or_skip parameter controls whether register numbers are
2498    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2499    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2500    info).  This should be suppressed for the cases that have not been converted
2501    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2502 
2503 void
output_loc_sequence(dw_loc_descr_ref loc,int for_eh_or_skip)2504 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2505 {
2506   for (; loc != NULL; loc = loc->dw_loc_next)
2507     {
2508       enum dwarf_location_atom opc = loc->dw_loc_opc;
2509       /* Output the opcode.  */
2510       if (for_eh_or_skip >= 0
2511           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2512 	{
2513 	  unsigned r = (opc - DW_OP_breg0);
2514 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2515 	  gcc_assert (r <= 31);
2516 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2517 	}
2518       else if (for_eh_or_skip >= 0
2519 	       && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2520 	{
2521 	  unsigned r = (opc - DW_OP_reg0);
2522 	  r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2523 	  gcc_assert (r <= 31);
2524 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2525 	}
2526 
2527       dw2_asm_output_data (1, opc,
2528 			     "%s", dwarf_stack_op_name (opc));
2529 
2530       /* Output the operand(s) (if any).  */
2531       output_loc_operands (loc, for_eh_or_skip);
2532     }
2533 }
2534 
2535 /* Output location description stack opcode's operands (if any).
2536    The output is single bytes on a line, suitable for .cfi_escape.  */
2537 
2538 static void
output_loc_operands_raw(dw_loc_descr_ref loc)2539 output_loc_operands_raw (dw_loc_descr_ref loc)
2540 {
2541   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2542   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2543 
2544   switch (loc->dw_loc_opc)
2545     {
2546     case DW_OP_addr:
2547     case DW_OP_GNU_addr_index:
2548     case DW_OP_addrx:
2549     case DW_OP_GNU_const_index:
2550     case DW_OP_constx:
2551     case DW_OP_implicit_value:
2552       /* We cannot output addresses in .cfi_escape, only bytes.  */
2553       gcc_unreachable ();
2554 
2555     case DW_OP_const1u:
2556     case DW_OP_const1s:
2557     case DW_OP_pick:
2558     case DW_OP_deref_size:
2559     case DW_OP_xderef_size:
2560       fputc (',', asm_out_file);
2561       dw2_asm_output_data_raw (1, val1->v.val_int);
2562       break;
2563 
2564     case DW_OP_const2u:
2565     case DW_OP_const2s:
2566       fputc (',', asm_out_file);
2567       dw2_asm_output_data_raw (2, val1->v.val_int);
2568       break;
2569 
2570     case DW_OP_const4u:
2571     case DW_OP_const4s:
2572       fputc (',', asm_out_file);
2573       dw2_asm_output_data_raw (4, val1->v.val_int);
2574       break;
2575 
2576     case DW_OP_const8u:
2577     case DW_OP_const8s:
2578       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2579       fputc (',', asm_out_file);
2580       dw2_asm_output_data_raw (8, val1->v.val_int);
2581       break;
2582 
2583     case DW_OP_skip:
2584     case DW_OP_bra:
2585       {
2586 	int offset;
2587 
2588 	gcc_assert (val1->val_class == dw_val_class_loc);
2589 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2590 
2591         fputc (',', asm_out_file);
2592 	dw2_asm_output_data_raw (2, offset);
2593       }
2594       break;
2595 
2596     case DW_OP_regx:
2597       {
2598 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2599 	gcc_assert (size_of_uleb128 (r)
2600 		    == size_of_uleb128 (val1->v.val_unsigned));
2601 	fputc (',', asm_out_file);
2602 	dw2_asm_output_data_uleb128_raw (r);
2603       }
2604       break;
2605 
2606     case DW_OP_constu:
2607     case DW_OP_plus_uconst:
2608     case DW_OP_piece:
2609       fputc (',', asm_out_file);
2610       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2611       break;
2612 
2613     case DW_OP_bit_piece:
2614       fputc (',', asm_out_file);
2615       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2616       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2617       break;
2618 
2619     case DW_OP_consts:
2620     case DW_OP_breg0:
2621     case DW_OP_breg1:
2622     case DW_OP_breg2:
2623     case DW_OP_breg3:
2624     case DW_OP_breg4:
2625     case DW_OP_breg5:
2626     case DW_OP_breg6:
2627     case DW_OP_breg7:
2628     case DW_OP_breg8:
2629     case DW_OP_breg9:
2630     case DW_OP_breg10:
2631     case DW_OP_breg11:
2632     case DW_OP_breg12:
2633     case DW_OP_breg13:
2634     case DW_OP_breg14:
2635     case DW_OP_breg15:
2636     case DW_OP_breg16:
2637     case DW_OP_breg17:
2638     case DW_OP_breg18:
2639     case DW_OP_breg19:
2640     case DW_OP_breg20:
2641     case DW_OP_breg21:
2642     case DW_OP_breg22:
2643     case DW_OP_breg23:
2644     case DW_OP_breg24:
2645     case DW_OP_breg25:
2646     case DW_OP_breg26:
2647     case DW_OP_breg27:
2648     case DW_OP_breg28:
2649     case DW_OP_breg29:
2650     case DW_OP_breg30:
2651     case DW_OP_breg31:
2652     case DW_OP_fbreg:
2653       fputc (',', asm_out_file);
2654       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2655       break;
2656 
2657     case DW_OP_bregx:
2658       {
2659 	unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2660 	gcc_assert (size_of_uleb128 (r)
2661 		    == size_of_uleb128 (val1->v.val_unsigned));
2662 	fputc (',', asm_out_file);
2663 	dw2_asm_output_data_uleb128_raw (r);
2664 	fputc (',', asm_out_file);
2665 	dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2666       }
2667       break;
2668 
2669     case DW_OP_implicit_pointer:
2670     case DW_OP_entry_value:
2671     case DW_OP_const_type:
2672     case DW_OP_regval_type:
2673     case DW_OP_deref_type:
2674     case DW_OP_convert:
2675     case DW_OP_reinterpret:
2676     case DW_OP_GNU_implicit_pointer:
2677     case DW_OP_GNU_entry_value:
2678     case DW_OP_GNU_const_type:
2679     case DW_OP_GNU_regval_type:
2680     case DW_OP_GNU_deref_type:
2681     case DW_OP_GNU_convert:
2682     case DW_OP_GNU_reinterpret:
2683     case DW_OP_GNU_parameter_ref:
2684       gcc_unreachable ();
2685       break;
2686 
2687     default:
2688       /* Other codes have no operands.  */
2689       break;
2690     }
2691 }
2692 
2693 void
output_loc_sequence_raw(dw_loc_descr_ref loc)2694 output_loc_sequence_raw (dw_loc_descr_ref loc)
2695 {
2696   while (1)
2697     {
2698       enum dwarf_location_atom opc = loc->dw_loc_opc;
2699       /* Output the opcode.  */
2700       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2701 	{
2702 	  unsigned r = (opc - DW_OP_breg0);
2703 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2704 	  gcc_assert (r <= 31);
2705 	  opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2706 	}
2707       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2708 	{
2709 	  unsigned r = (opc - DW_OP_reg0);
2710 	  r = DWARF2_FRAME_REG_OUT (r, 1);
2711 	  gcc_assert (r <= 31);
2712 	  opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2713 	}
2714       /* Output the opcode.  */
2715       fprintf (asm_out_file, "%#x", opc);
2716       output_loc_operands_raw (loc);
2717 
2718       if (!loc->dw_loc_next)
2719 	break;
2720       loc = loc->dw_loc_next;
2721 
2722       fputc (',', asm_out_file);
2723     }
2724 }
2725 
2726 /* This function builds a dwarf location descriptor sequence from a
2727    dw_cfa_location, adding the given OFFSET to the result of the
2728    expression.  */
2729 
2730 struct dw_loc_descr_node *
build_cfa_loc(dw_cfa_location * cfa,poly_int64 offset)2731 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2732 {
2733   struct dw_loc_descr_node *head, *tmp;
2734 
2735   offset += cfa->offset;
2736 
2737   if (cfa->indirect)
2738     {
2739       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2740       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2741       head->dw_loc_oprnd1.val_entry = NULL;
2742       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2743       add_loc_descr (&head, tmp);
2744       loc_descr_plus_const (&head, offset);
2745     }
2746   else
2747     head = new_reg_loc_descr (cfa->reg, offset);
2748 
2749   return head;
2750 }
2751 
2752 /* This function builds a dwarf location descriptor sequence for
2753    the address at OFFSET from the CFA when stack is aligned to
2754    ALIGNMENT byte.  */
2755 
2756 struct dw_loc_descr_node *
build_cfa_aligned_loc(dw_cfa_location * cfa,poly_int64 offset,HOST_WIDE_INT alignment)2757 build_cfa_aligned_loc (dw_cfa_location *cfa,
2758 		       poly_int64 offset, HOST_WIDE_INT alignment)
2759 {
2760   struct dw_loc_descr_node *head;
2761   unsigned int dwarf_fp
2762     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2763 
2764   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2765   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2766     {
2767       head = new_reg_loc_descr (dwarf_fp, 0);
2768       add_loc_descr (&head, int_loc_descriptor (alignment));
2769       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2770       loc_descr_plus_const (&head, offset);
2771     }
2772   else
2773     head = new_reg_loc_descr (dwarf_fp, offset);
2774   return head;
2775 }
2776 
2777 /* And now, the support for symbolic debugging information.  */
2778 
2779 /* .debug_str support.  */
2780 
2781 static void dwarf2out_init (const char *);
2782 static void dwarf2out_finish (const char *);
2783 static void dwarf2out_early_finish (const char *);
2784 static void dwarf2out_assembly_start (void);
2785 static void dwarf2out_define (unsigned int, const char *);
2786 static void dwarf2out_undef (unsigned int, const char *);
2787 static void dwarf2out_start_source_file (unsigned, const char *);
2788 static void dwarf2out_end_source_file (unsigned);
2789 static void dwarf2out_function_decl (tree);
2790 static void dwarf2out_begin_block (unsigned, unsigned);
2791 static void dwarf2out_end_block (unsigned, unsigned);
2792 static bool dwarf2out_ignore_block (const_tree);
2793 static void dwarf2out_early_global_decl (tree);
2794 static void dwarf2out_late_global_decl (tree);
2795 static void dwarf2out_type_decl (tree, int);
2796 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2797 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2798 						 dw_die_ref);
2799 static void dwarf2out_abstract_function (tree);
2800 static void dwarf2out_var_location (rtx_insn *);
2801 static void dwarf2out_inline_entry (tree);
2802 static void dwarf2out_size_function (tree);
2803 static void dwarf2out_begin_function (tree);
2804 static void dwarf2out_end_function (unsigned int);
2805 static void dwarf2out_register_main_translation_unit (tree unit);
2806 static void dwarf2out_set_name (tree, tree);
2807 static void dwarf2out_register_external_die (tree decl, const char *sym,
2808 					     unsigned HOST_WIDE_INT off);
2809 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2810 					unsigned HOST_WIDE_INT *off);
2811 
2812 /* The debug hooks structure.  */
2813 
2814 const struct gcc_debug_hooks dwarf2_debug_hooks =
2815 {
2816   dwarf2out_init,
2817   dwarf2out_finish,
2818   dwarf2out_early_finish,
2819   dwarf2out_assembly_start,
2820   dwarf2out_define,
2821   dwarf2out_undef,
2822   dwarf2out_start_source_file,
2823   dwarf2out_end_source_file,
2824   dwarf2out_begin_block,
2825   dwarf2out_end_block,
2826   dwarf2out_ignore_block,
2827   dwarf2out_source_line,
2828   dwarf2out_begin_prologue,
2829 #if VMS_DEBUGGING_INFO
2830   dwarf2out_vms_end_prologue,
2831   dwarf2out_vms_begin_epilogue,
2832 #else
2833   debug_nothing_int_charstar,
2834   debug_nothing_int_charstar,
2835 #endif
2836   dwarf2out_end_epilogue,
2837   dwarf2out_begin_function,
2838   dwarf2out_end_function,	/* end_function */
2839   dwarf2out_register_main_translation_unit,
2840   dwarf2out_function_decl,	/* function_decl */
2841   dwarf2out_early_global_decl,
2842   dwarf2out_late_global_decl,
2843   dwarf2out_type_decl,		/* type_decl */
2844   dwarf2out_imported_module_or_decl,
2845   dwarf2out_die_ref_for_decl,
2846   dwarf2out_register_external_die,
2847   debug_nothing_tree,		/* deferred_inline_function */
2848   /* The DWARF 2 backend tries to reduce debugging bloat by not
2849      emitting the abstract description of inline functions until
2850      something tries to reference them.  */
2851   dwarf2out_abstract_function,	/* outlining_inline_function */
2852   debug_nothing_rtx_code_label,	/* label */
2853   debug_nothing_int,		/* handle_pch */
2854   dwarf2out_var_location,
2855   dwarf2out_inline_entry,	/* inline_entry */
2856   dwarf2out_size_function,	/* size_function */
2857   dwarf2out_switch_text_section,
2858   dwarf2out_set_name,
2859   1,                            /* start_end_main_source_file */
2860   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2861 };
2862 
2863 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2864 {
2865   dwarf2out_init,
2866   debug_nothing_charstar,
2867   debug_nothing_charstar,
2868   dwarf2out_assembly_start,
2869   debug_nothing_int_charstar,
2870   debug_nothing_int_charstar,
2871   debug_nothing_int_charstar,
2872   debug_nothing_int,
2873   debug_nothing_int_int,	         /* begin_block */
2874   debug_nothing_int_int,	         /* end_block */
2875   debug_true_const_tree,	         /* ignore_block */
2876   dwarf2out_source_line,		 /* source_line */
2877   debug_nothing_int_int_charstar,	 /* begin_prologue */
2878   debug_nothing_int_charstar,	         /* end_prologue */
2879   debug_nothing_int_charstar,	         /* begin_epilogue */
2880   debug_nothing_int_charstar,	         /* end_epilogue */
2881   debug_nothing_tree,		         /* begin_function */
2882   debug_nothing_int,		         /* end_function */
2883   debug_nothing_tree,			 /* register_main_translation_unit */
2884   debug_nothing_tree,		         /* function_decl */
2885   debug_nothing_tree,		         /* early_global_decl */
2886   debug_nothing_tree,		         /* late_global_decl */
2887   debug_nothing_tree_int,		 /* type_decl */
2888   debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2889   debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2890   debug_nothing_tree_charstar_uhwi,      /* register_external_die */
2891   debug_nothing_tree,		         /* deferred_inline_function */
2892   debug_nothing_tree,		         /* outlining_inline_function */
2893   debug_nothing_rtx_code_label,	         /* label */
2894   debug_nothing_int,		         /* handle_pch */
2895   debug_nothing_rtx_insn,	         /* var_location */
2896   debug_nothing_tree,	         	 /* inline_entry */
2897   debug_nothing_tree,			 /* size_function */
2898   debug_nothing_void,                    /* switch_text_section */
2899   debug_nothing_tree_tree,		 /* set_name */
2900   0,                                     /* start_end_main_source_file */
2901   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2902 };
2903 
2904 /* NOTE: In the comments in this file, many references are made to
2905    "Debugging Information Entries".  This term is abbreviated as `DIE'
2906    throughout the remainder of this file.  */
2907 
2908 /* An internal representation of the DWARF output is built, and then
2909    walked to generate the DWARF debugging info.  The walk of the internal
2910    representation is done after the entire program has been compiled.
2911    The types below are used to describe the internal representation.  */
2912 
2913 /* Whether to put type DIEs into their own section .debug_types instead
2914    of making them part of the .debug_info section.  Only supported for
2915    Dwarf V4 or higher and the user didn't disable them through
2916    -fno-debug-types-section.  It is more efficient to put them in a
2917    separate comdat sections since the linker will then be able to
2918    remove duplicates.  But not all tools support .debug_types sections
2919    yet.  For Dwarf V5 or higher .debug_types doesn't exist any more,
2920    it is DW_UT_type unit type in .debug_info section.  For late LTO
2921    debug there should be almost no types emitted so avoid enabling
2922    -fdebug-types-section there.  */
2923 
2924 #define use_debug_types (dwarf_version >= 4 \
2925 			 && flag_debug_types_section \
2926 			 && !in_lto_p)
2927 
2928 /* Various DIE's use offsets relative to the beginning of the
2929    .debug_info section to refer to each other.  */
2930 
2931 typedef long int dw_offset;
2932 
2933 struct comdat_type_node;
2934 
2935 /* The entries in the line_info table more-or-less mirror the opcodes
2936    that are used in the real dwarf line table.  Arrays of these entries
2937    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2938    supported.  */
2939 
2940 enum dw_line_info_opcode {
2941   /* Emit DW_LNE_set_address; the operand is the label index.  */
2942   LI_set_address,
2943 
2944   /* Emit a row to the matrix with the given line.  This may be done
2945      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2946      special opcodes.  */
2947   LI_set_line,
2948 
2949   /* Emit a DW_LNS_set_file.  */
2950   LI_set_file,
2951 
2952   /* Emit a DW_LNS_set_column.  */
2953   LI_set_column,
2954 
2955   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2956   LI_negate_stmt,
2957 
2958   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2959   LI_set_prologue_end,
2960   LI_set_epilogue_begin,
2961 
2962   /* Emit a DW_LNE_set_discriminator.  */
2963   LI_set_discriminator,
2964 
2965   /* Output a Fixed Advance PC; the target PC is the label index; the
2966      base PC is the previous LI_adv_address or LI_set_address entry.
2967      We only use this when emitting debug views without assembler
2968      support, at explicit user request.  Ideally, we should only use
2969      it when the offset might be zero but we can't tell: it's the only
2970      way to maybe change the PC without resetting the view number.  */
2971   LI_adv_address
2972 };
2973 
2974 typedef struct GTY(()) dw_line_info_struct {
2975   enum dw_line_info_opcode opcode;
2976   unsigned int val;
2977 } dw_line_info_entry;
2978 
2979 
2980 struct GTY(()) dw_line_info_table {
2981   /* The label that marks the end of this section.  */
2982   const char *end_label;
2983 
2984   /* The values for the last row of the matrix, as collected in the table.
2985      These are used to minimize the changes to the next row.  */
2986   unsigned int file_num;
2987   unsigned int line_num;
2988   unsigned int column_num;
2989   int discrim_num;
2990   bool is_stmt;
2991   bool in_use;
2992 
2993   /* This denotes the NEXT view number.
2994 
2995      If it is 0, it is known that the NEXT view will be the first view
2996      at the given PC.
2997 
2998      If it is -1, we're forcing the view number to be reset, e.g. at a
2999      function entry.
3000 
3001      The meaning of other nonzero values depends on whether we're
3002      computing views internally or leaving it for the assembler to do
3003      so.  If we're emitting them internally, view denotes the view
3004      number since the last known advance of PC.  If we're leaving it
3005      for the assembler, it denotes the LVU label number that we're
3006      going to ask the assembler to assign.  */
3007   var_loc_view view;
3008 
3009   /* This counts the number of symbolic views emitted in this table
3010      since the latest view reset.  Its max value, over all tables,
3011      sets symview_upper_bound.  */
3012   var_loc_view symviews_since_reset;
3013 
3014 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3015 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3016 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3017 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3018 
3019   vec<dw_line_info_entry, va_gc> *entries;
3020 };
3021 
3022 /* This is an upper bound for view numbers that the assembler may
3023    assign to symbolic views output in this translation.  It is used to
3024    decide how big a field to use to represent view numbers in
3025    symview-classed attributes.  */
3026 
3027 static var_loc_view symview_upper_bound;
3028 
3029 /* If we're keep track of location views and their reset points, and
3030    INSN is a reset point (i.e., it necessarily advances the PC), mark
3031    the next view in TABLE as reset.  */
3032 
3033 static void
maybe_reset_location_view(rtx_insn * insn,dw_line_info_table * table)3034 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3035 {
3036   if (!debug_internal_reset_location_views)
3037     return;
3038 
3039   /* Maybe turn (part of?) this test into a default target hook.  */
3040   int reset = 0;
3041 
3042   if (targetm.reset_location_view)
3043     reset = targetm.reset_location_view (insn);
3044 
3045   if (reset)
3046     ;
3047   else if (JUMP_TABLE_DATA_P (insn))
3048     reset = 1;
3049   else if (GET_CODE (insn) == USE
3050 	   || GET_CODE (insn) == CLOBBER
3051 	   || GET_CODE (insn) == ASM_INPUT
3052 	   || asm_noperands (insn) >= 0)
3053     ;
3054   else if (get_attr_min_length (insn) > 0)
3055     reset = 1;
3056 
3057   if (reset > 0 && !RESETTING_VIEW_P (table->view))
3058     RESET_NEXT_VIEW (table->view);
3059 }
3060 
3061 /* Each DIE attribute has a field specifying the attribute kind,
3062    a link to the next attribute in the chain, and an attribute value.
3063    Attributes are typically linked below the DIE they modify.  */
3064 
3065 typedef struct GTY(()) dw_attr_struct {
3066   enum dwarf_attribute dw_attr;
3067   dw_val_node dw_attr_val;
3068 }
3069 dw_attr_node;
3070 
3071 
3072 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3073    The children of each node form a circular list linked by
3074    die_sib.  die_child points to the node *before* the "first" child node.  */
3075 
3076 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3077   union die_symbol_or_type_node
3078     {
3079       const char * GTY ((tag ("0"))) die_symbol;
3080       comdat_type_node *GTY ((tag ("1"))) die_type_node;
3081     }
3082   GTY ((desc ("%0.comdat_type_p"))) die_id;
3083   vec<dw_attr_node, va_gc> *die_attr;
3084   dw_die_ref die_parent;
3085   dw_die_ref die_child;
3086   dw_die_ref die_sib;
3087   dw_die_ref die_definition; /* ref from a specification to its definition */
3088   dw_offset die_offset;
3089   unsigned long die_abbrev;
3090   int die_mark;
3091   unsigned int decl_id;
3092   enum dwarf_tag die_tag;
3093   /* Die is used and must not be pruned as unused.  */
3094   BOOL_BITFIELD die_perennial_p : 1;
3095   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3096   /* For an external ref to die_symbol if die_offset contains an extra
3097      offset to that symbol.  */
3098   BOOL_BITFIELD with_offset : 1;
3099   /* Whether this DIE was removed from the DIE tree, for example via
3100      prune_unused_types.  We don't consider those present from the
3101      DIE lookup routines.  */
3102   BOOL_BITFIELD removed : 1;
3103   /* Lots of spare bits.  */
3104 }
3105 die_node;
3106 
3107 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
3108 static bool early_dwarf;
3109 static bool early_dwarf_finished;
3110 class set_early_dwarf {
3111 public:
3112   bool saved;
set_early_dwarf()3113   set_early_dwarf () : saved(early_dwarf)
3114     {
3115       gcc_assert (! early_dwarf_finished);
3116       early_dwarf = true;
3117     }
~set_early_dwarf()3118   ~set_early_dwarf () { early_dwarf = saved; }
3119 };
3120 
3121 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3122 #define FOR_EACH_CHILD(die, c, expr) do {	\
3123   c = die->die_child;				\
3124   if (c) do {					\
3125     c = c->die_sib;				\
3126     expr;					\
3127   } while (c != die->die_child);		\
3128 } while (0)
3129 
3130 /* The pubname structure */
3131 
3132 typedef struct GTY(()) pubname_struct {
3133   dw_die_ref die;
3134   const char *name;
3135 }
3136 pubname_entry;
3137 
3138 
3139 struct GTY(()) dw_ranges {
3140   const char *label;
3141   /* If this is positive, it's a block number, otherwise it's a
3142      bitwise-negated index into dw_ranges_by_label.  */
3143   int num;
3144   /* Index for the range list for DW_FORM_rnglistx.  */
3145   unsigned int idx : 31;
3146   /* True if this range might be possibly in a different section
3147      from previous entry.  */
3148   unsigned int maybe_new_sec : 1;
3149 };
3150 
3151 /* A structure to hold a macinfo entry.  */
3152 
3153 typedef struct GTY(()) macinfo_struct {
3154   unsigned char code;
3155   unsigned HOST_WIDE_INT lineno;
3156   const char *info;
3157 }
3158 macinfo_entry;
3159 
3160 
3161 struct GTY(()) dw_ranges_by_label {
3162   const char *begin;
3163   const char *end;
3164 };
3165 
3166 /* The comdat type node structure.  */
3167 struct GTY(()) comdat_type_node
3168 {
3169   dw_die_ref root_die;
3170   dw_die_ref type_die;
3171   dw_die_ref skeleton_die;
3172   char signature[DWARF_TYPE_SIGNATURE_SIZE];
3173   comdat_type_node *next;
3174 };
3175 
3176 /* A list of DIEs for which we can't determine ancestry (parent_die
3177    field) just yet.  Later in dwarf2out_finish we will fill in the
3178    missing bits.  */
3179 typedef struct GTY(()) limbo_die_struct {
3180   dw_die_ref die;
3181   /* The tree for which this DIE was created.  We use this to
3182      determine ancestry later.  */
3183   tree created_for;
3184   struct limbo_die_struct *next;
3185 }
3186 limbo_die_node;
3187 
3188 typedef struct skeleton_chain_struct
3189 {
3190   dw_die_ref old_die;
3191   dw_die_ref new_die;
3192   struct skeleton_chain_struct *parent;
3193 }
3194 skeleton_chain_node;
3195 
3196 /* Define a macro which returns nonzero for a TYPE_DECL which was
3197    implicitly generated for a type.
3198 
3199    Note that, unlike the C front-end (which generates a NULL named
3200    TYPE_DECL node for each complete tagged type, each array type,
3201    and each function type node created) the C++ front-end generates
3202    a _named_ TYPE_DECL node for each tagged type node created.
3203    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3204    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
3205    front-end, but for each type, tagged or not.  */
3206 
3207 #define TYPE_DECL_IS_STUB(decl)				\
3208   (DECL_NAME (decl) == NULL_TREE			\
3209    || (DECL_ARTIFICIAL (decl)				\
3210        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
3211 	   /* This is necessary for stub decls that	\
3212 	      appear in nested inline functions.  */	\
3213 	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
3214 	       && (decl_ultimate_origin (decl)		\
3215 		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3216 
3217 /* Information concerning the compilation unit's programming
3218    language, and compiler version.  */
3219 
3220 /* Fixed size portion of the DWARF compilation unit header.  */
3221 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3222   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE			\
3223    + (dwarf_version >= 5 ? 4 : 3))
3224 
3225 /* Fixed size portion of the DWARF comdat type unit header.  */
3226 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3227   (DWARF_COMPILE_UNIT_HEADER_SIZE					\
3228    + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3229 
3230 /* Fixed size portion of the DWARF skeleton compilation unit header.  */
3231 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3232   (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3233 
3234 /* Fixed size portion of public names info.  */
3235 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3236 
3237 /* Fixed size portion of the address range info.  */
3238 #define DWARF_ARANGES_HEADER_SIZE					\
3239   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
3240 		DWARF2_ADDR_SIZE * 2)					\
3241    - DWARF_INITIAL_LENGTH_SIZE)
3242 
3243 /* Size of padding portion in the address range info.  It must be
3244    aligned to twice the pointer size.  */
3245 #define DWARF_ARANGES_PAD_SIZE \
3246   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3247 		DWARF2_ADDR_SIZE * 2)				   \
3248    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3249 
3250 /* Use assembler line directives if available.  */
3251 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3252 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3253 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3254 #else
3255 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3256 #endif
3257 #endif
3258 
3259 /* Use assembler views in line directives if available.  */
3260 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3261 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3262 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3263 #else
3264 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3265 #endif
3266 #endif
3267 
3268 /* Return true if GCC configure detected assembler support for .loc.  */
3269 
3270 bool
dwarf2out_default_as_loc_support(void)3271 dwarf2out_default_as_loc_support (void)
3272 {
3273   return DWARF2_ASM_LINE_DEBUG_INFO;
3274 #if (GCC_VERSION >= 3000)
3275 # undef DWARF2_ASM_LINE_DEBUG_INFO
3276 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3277 #endif
3278 }
3279 
3280 /* Return true if GCC configure detected assembler support for views
3281    in .loc directives.  */
3282 
3283 bool
dwarf2out_default_as_locview_support(void)3284 dwarf2out_default_as_locview_support (void)
3285 {
3286   return DWARF2_ASM_VIEW_DEBUG_INFO;
3287 #if (GCC_VERSION >= 3000)
3288 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3289 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3290 #endif
3291 }
3292 
3293 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3294    view computation, and it refers to a view identifier for which we
3295    will not emit a label because it is known to map to a view number
3296    zero.  We won't allocate the bitmap if we're not using assembler
3297    support for location views, but we have to make the variable
3298    visible for GGC and for code that will be optimized out for lack of
3299    support but that's still parsed and compiled.  We could abstract it
3300    out with macros, but it's not worth it.  */
3301 static GTY(()) bitmap zero_view_p;
3302 
3303 /* Evaluate to TRUE iff N is known to identify the first location view
3304    at its PC.  When not using assembler location view computation,
3305    that must be view number zero.  Otherwise, ZERO_VIEW_P is allocated
3306    and views label numbers recorded in it are the ones known to be
3307    zero.  */
3308 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0				\
3309 			|| (N) == (var_loc_view)-1			\
3310 			|| (zero_view_p					\
3311 			    && bitmap_bit_p (zero_view_p, (N))))
3312 
3313 /* Return true iff we're to emit .loc directives for the assembler to
3314    generate line number sections.
3315 
3316    When we're not emitting views, all we need from the assembler is
3317    support for .loc directives.
3318 
3319    If we are emitting views, we can only use the assembler's .loc
3320    support if it also supports views.
3321 
3322    When the compiler is emitting the line number programs and
3323    computing view numbers itself, it resets view numbers at known PC
3324    changes and counts from that, and then it emits view numbers as
3325    literal constants in locviewlists.  There are cases in which the
3326    compiler is not sure about PC changes, e.g. when extra alignment is
3327    requested for a label.  In these cases, the compiler may not reset
3328    the view counter, and the potential PC advance in the line number
3329    program will use an opcode that does not reset the view counter
3330    even if the PC actually changes, so that compiler and debug info
3331    consumer can keep view numbers in sync.
3332 
3333    When the compiler defers view computation to the assembler, it
3334    emits symbolic view numbers in locviewlists, with the exception of
3335    views known to be zero (forced resets, or reset after
3336    compiler-visible PC changes): instead of emitting symbols for
3337    these, we emit literal zero and assert the assembler agrees with
3338    the compiler's assessment.  We could use symbolic views everywhere,
3339    instead of special-casing zero views, but then we'd be unable to
3340    optimize out locviewlists that contain only zeros.  */
3341 
3342 static bool
output_asm_line_debug_info(void)3343 output_asm_line_debug_info (void)
3344 {
3345   return (dwarf2out_as_loc_support
3346 	  && (dwarf2out_as_locview_support
3347 	      || !debug_variable_location_views));
3348 }
3349 
3350 /* Minimum line offset in a special line info. opcode.
3351    This value was chosen to give a reasonable range of values.  */
3352 #define DWARF_LINE_BASE  -10
3353 
3354 /* First special line opcode - leave room for the standard opcodes.  */
3355 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
3356 
3357 /* Range of line offsets in a special line info. opcode.  */
3358 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3359 
3360 /* Flag that indicates the initial value of the is_stmt_start flag.
3361    In the present implementation, we do not mark any lines as
3362    the beginning of a source statement, because that information
3363    is not made available by the GCC front-end.  */
3364 #define	DWARF_LINE_DEFAULT_IS_STMT_START 1
3365 
3366 /* Maximum number of operations per instruction bundle.  */
3367 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3368 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3369 #endif
3370 
3371 /* This location is used by calc_die_sizes() to keep track
3372    the offset of each DIE within the .debug_info section.  */
3373 static unsigned long next_die_offset;
3374 
3375 /* Record the root of the DIE's built for the current compilation unit.  */
3376 static GTY(()) dw_die_ref single_comp_unit_die;
3377 
3378 /* A list of type DIEs that have been separated into comdat sections.  */
3379 static GTY(()) comdat_type_node *comdat_type_list;
3380 
3381 /* A list of CU DIEs that have been separated.  */
3382 static GTY(()) limbo_die_node *cu_die_list;
3383 
3384 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3385 static GTY(()) limbo_die_node *limbo_die_list;
3386 
3387 /* A list of DIEs for which we may have to generate
3388    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
3389 static GTY(()) limbo_die_node *deferred_asm_name;
3390 
3391 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3392 {
3393   typedef const char *compare_type;
3394 
3395   static hashval_t hash (dwarf_file_data *);
3396   static bool equal (dwarf_file_data *, const char *);
3397 };
3398 
3399 /* Filenames referenced by this compilation unit.  */
3400 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3401 
3402 struct decl_die_hasher : ggc_ptr_hash<die_node>
3403 {
3404   typedef tree compare_type;
3405 
3406   static hashval_t hash (die_node *);
3407   static bool equal (die_node *, tree);
3408 };
3409 /* A hash table of references to DIE's that describe declarations.
3410    The key is a DECL_UID() which is a unique number identifying each decl.  */
3411 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3412 
3413 struct GTY ((for_user)) variable_value_struct {
3414   unsigned int decl_id;
3415   vec<dw_die_ref, va_gc> *dies;
3416 };
3417 
3418 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3419 {
3420   typedef tree compare_type;
3421 
3422   static hashval_t hash (variable_value_struct *);
3423   static bool equal (variable_value_struct *, tree);
3424 };
3425 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3426    dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3427    DECL_CONTEXT of the referenced VAR_DECLs.  */
3428 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3429 
3430 struct block_die_hasher : ggc_ptr_hash<die_struct>
3431 {
3432   static hashval_t hash (die_struct *);
3433   static bool equal (die_struct *, die_struct *);
3434 };
3435 
3436 /* A hash table of references to DIE's that describe COMMON blocks.
3437    The key is DECL_UID() ^ die_parent.  */
3438 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3439 
3440 typedef struct GTY(()) die_arg_entry_struct {
3441     dw_die_ref die;
3442     tree arg;
3443 } die_arg_entry;
3444 
3445 
3446 /* Node of the variable location list.  */
3447 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3448   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3449      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3450      in mode of the EXPR_LIST node and first EXPR_LIST operand
3451      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3452      location or NULL for padding.  For larger bitsizes,
3453      mode is 0 and first operand is a CONCAT with bitsize
3454      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3455      NULL as second operand.  */
3456   rtx GTY (()) loc;
3457   const char * GTY (()) label;
3458   struct var_loc_node * GTY (()) next;
3459   var_loc_view view;
3460 };
3461 
3462 /* Variable location list.  */
3463 struct GTY ((for_user)) var_loc_list_def {
3464   struct var_loc_node * GTY (()) first;
3465 
3466   /* Pointer to the last but one or last element of the
3467      chained list.  If the list is empty, both first and
3468      last are NULL, if the list contains just one node
3469      or the last node certainly is not redundant, it points
3470      to the last node, otherwise points to the last but one.
3471      Do not mark it for GC because it is marked through the chain.  */
3472   struct var_loc_node * GTY ((skip ("%h"))) last;
3473 
3474   /* Pointer to the last element before section switch,
3475      if NULL, either sections weren't switched or first
3476      is after section switch.  */
3477   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3478 
3479   /* DECL_UID of the variable decl.  */
3480   unsigned int decl_id;
3481 };
3482 typedef struct var_loc_list_def var_loc_list;
3483 
3484 /* Call argument location list.  */
3485 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3486   rtx GTY (()) call_arg_loc_note;
3487   const char * GTY (()) label;
3488   tree GTY (()) block;
3489   bool tail_call_p;
3490   rtx GTY (()) symbol_ref;
3491   struct call_arg_loc_node * GTY (()) next;
3492 };
3493 
3494 
3495 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3496 {
3497   typedef const_tree compare_type;
3498 
3499   static hashval_t hash (var_loc_list *);
3500   static bool equal (var_loc_list *, const_tree);
3501 };
3502 
3503 /* Table of decl location linked lists.  */
3504 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3505 
3506 /* Head and tail of call_arg_loc chain.  */
3507 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3508 static struct call_arg_loc_node *call_arg_loc_last;
3509 
3510 /* Number of call sites in the current function.  */
3511 static int call_site_count = -1;
3512 /* Number of tail call sites in the current function.  */
3513 static int tail_call_site_count = -1;
3514 
3515 /* A cached location list.  */
3516 struct GTY ((for_user)) cached_dw_loc_list_def {
3517   /* The DECL_UID of the decl that this entry describes.  */
3518   unsigned int decl_id;
3519 
3520   /* The cached location list.  */
3521   dw_loc_list_ref loc_list;
3522 };
3523 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3524 
3525 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3526 {
3527 
3528   typedef const_tree compare_type;
3529 
3530   static hashval_t hash (cached_dw_loc_list *);
3531   static bool equal (cached_dw_loc_list *, const_tree);
3532 };
3533 
3534 /* Table of cached location lists.  */
3535 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3536 
3537 /* A vector of references to DIE's that are uniquely identified by their tag,
3538    presence/absence of children DIE's, and list of attribute/value pairs.  */
3539 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3540 
3541 /* A hash map to remember the stack usage for DWARF procedures.  The value
3542    stored is the stack size difference between before the DWARF procedure
3543    invokation and after it returned.  In other words, for a DWARF procedure
3544    that consumes N stack slots and that pushes M ones, this stores M - N.  */
3545 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3546 
3547 /* A global counter for generating labels for line number data.  */
3548 static unsigned int line_info_label_num;
3549 
3550 /* The current table to which we should emit line number information
3551    for the current function.  This will be set up at the beginning of
3552    assembly for the function.  */
3553 static GTY(()) dw_line_info_table *cur_line_info_table;
3554 
3555 /* The two default tables of line number info.  */
3556 static GTY(()) dw_line_info_table *text_section_line_info;
3557 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3558 
3559 /* The set of all non-default tables of line number info.  */
3560 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3561 
3562 /* A flag to tell pubnames/types export if there is an info section to
3563    refer to.  */
3564 static bool info_section_emitted;
3565 
3566 /* A pointer to the base of a table that contains a list of publicly
3567    accessible names.  */
3568 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3569 
3570 /* A pointer to the base of a table that contains a list of publicly
3571    accessible types.  */
3572 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3573 
3574 /* A pointer to the base of a table that contains a list of macro
3575    defines/undefines (and file start/end markers).  */
3576 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3577 
3578 /* True if .debug_macinfo or .debug_macros section is going to be
3579    emitted.  */
3580 #define have_macinfo \
3581   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3582    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3583    && !macinfo_table->is_empty ())
3584 
3585 /* Vector of dies for which we should generate .debug_ranges info.  */
3586 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3587 
3588 /* Vector of pairs of labels referenced in ranges_table.  */
3589 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3590 
3591 /* Whether we have location lists that need outputting */
3592 static GTY(()) bool have_location_lists;
3593 
3594 /* Unique label counter.  */
3595 static GTY(()) unsigned int loclabel_num;
3596 
3597 /* Unique label counter for point-of-call tables.  */
3598 static GTY(()) unsigned int poc_label_num;
3599 
3600 /* The last file entry emitted by maybe_emit_file().  */
3601 static GTY(()) struct dwarf_file_data * last_emitted_file;
3602 
3603 /* Number of internal labels generated by gen_internal_sym().  */
3604 static GTY(()) int label_num;
3605 
3606 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3607 
3608 /* Instances of generic types for which we need to generate debug
3609    info that describe their generic parameters and arguments. That
3610    generation needs to happen once all types are properly laid out so
3611    we do it at the end of compilation.  */
3612 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3613 
3614 /* Offset from the "steady-state frame pointer" to the frame base,
3615    within the current function.  */
3616 static poly_int64 frame_pointer_fb_offset;
3617 static bool frame_pointer_fb_offset_valid;
3618 
3619 static vec<dw_die_ref> base_types;
3620 
3621 /* Flags to represent a set of attribute classes for attributes that represent
3622    a scalar value (bounds, pointers, ...).  */
3623 enum dw_scalar_form
3624 {
3625   dw_scalar_form_constant = 0x01,
3626   dw_scalar_form_exprloc = 0x02,
3627   dw_scalar_form_reference = 0x04
3628 };
3629 
3630 /* Forward declarations for functions defined in this file.  */
3631 
3632 static int is_pseudo_reg (const_rtx);
3633 static tree type_main_variant (tree);
3634 static int is_tagged_type (const_tree);
3635 static const char *dwarf_tag_name (unsigned);
3636 static const char *dwarf_attr_name (unsigned);
3637 static const char *dwarf_form_name (unsigned);
3638 static tree decl_ultimate_origin (const_tree);
3639 static tree decl_class_context (tree);
3640 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3641 static inline enum dw_val_class AT_class (dw_attr_node *);
3642 static inline unsigned int AT_index (dw_attr_node *);
3643 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3644 static inline unsigned AT_flag (dw_attr_node *);
3645 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3646 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3647 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3648 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3649 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3650 			   HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3651 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3652 			       unsigned int, unsigned char *);
3653 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3654 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3655 static inline const char *AT_string (dw_attr_node *);
3656 static enum dwarf_form AT_string_form (dw_attr_node *);
3657 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3658 static void add_AT_specification (dw_die_ref, dw_die_ref);
3659 static inline dw_die_ref AT_ref (dw_attr_node *);
3660 static inline int AT_ref_external (dw_attr_node *);
3661 static inline void set_AT_ref_external (dw_attr_node *, int);
3662 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3663 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3664 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3665 			     dw_loc_list_ref);
3666 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3667 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3668 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3669 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3670 static void remove_addr_table_entry (addr_table_entry *);
3671 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3672 static inline rtx AT_addr (dw_attr_node *);
3673 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3674 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3675 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3676 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3677 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3678                                unsigned long, bool);
3679 static inline const char *AT_lbl (dw_attr_node *);
3680 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3681 static const char *get_AT_low_pc (dw_die_ref);
3682 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3683 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3684 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3685 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3686 static bool is_c (void);
3687 static bool is_cxx (void);
3688 static bool is_cxx (const_tree);
3689 static bool is_fortran (void);
3690 static bool is_ada (void);
3691 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3692 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3693 static void add_child_die (dw_die_ref, dw_die_ref);
3694 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3695 static dw_die_ref lookup_type_die (tree);
3696 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3697 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3698 static void equate_type_number_to_die (tree, dw_die_ref);
3699 static dw_die_ref lookup_decl_die (tree);
3700 static var_loc_list *lookup_decl_loc (const_tree);
3701 static void equate_decl_number_to_die (tree, dw_die_ref);
3702 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3703 static void print_spaces (FILE *);
3704 static void print_die (dw_die_ref, FILE *);
3705 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3706 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3707 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3708 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3709 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3710 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3711 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3712 				   struct md5_ctx *, int *);
3713 struct checksum_attributes;
3714 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3715 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3716 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3717 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3718 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3719 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3720 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3721 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3722 static int is_type_die (dw_die_ref);
3723 static inline bool is_template_instantiation (dw_die_ref);
3724 static int is_declaration_die (dw_die_ref);
3725 static int should_move_die_to_comdat (dw_die_ref);
3726 static dw_die_ref clone_as_declaration (dw_die_ref);
3727 static dw_die_ref clone_die (dw_die_ref);
3728 static dw_die_ref clone_tree (dw_die_ref);
3729 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3730 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3731 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3732 static dw_die_ref generate_skeleton (dw_die_ref);
3733 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3734                                                          dw_die_ref,
3735                                                          dw_die_ref);
3736 static void break_out_comdat_types (dw_die_ref);
3737 static void copy_decls_for_unworthy_types (dw_die_ref);
3738 
3739 static void add_sibling_attributes (dw_die_ref);
3740 static void output_location_lists (dw_die_ref);
3741 static int constant_size (unsigned HOST_WIDE_INT);
3742 static unsigned long size_of_die (dw_die_ref);
3743 static void calc_die_sizes (dw_die_ref);
3744 static void calc_base_type_die_sizes (void);
3745 static void mark_dies (dw_die_ref);
3746 static void unmark_dies (dw_die_ref);
3747 static void unmark_all_dies (dw_die_ref);
3748 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3749 static unsigned long size_of_aranges (void);
3750 static enum dwarf_form value_format (dw_attr_node *);
3751 static void output_value_format (dw_attr_node *);
3752 static void output_abbrev_section (void);
3753 static void output_die_abbrevs (unsigned long, dw_die_ref);
3754 static void output_die (dw_die_ref);
3755 static void output_compilation_unit_header (enum dwarf_unit_type);
3756 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3757 static void output_comdat_type_unit (comdat_type_node *, bool);
3758 static const char *dwarf2_name (tree, int);
3759 static void add_pubname (tree, dw_die_ref);
3760 static void add_enumerator_pubname (const char *, dw_die_ref);
3761 static void add_pubname_string (const char *, dw_die_ref);
3762 static void add_pubtype (tree, dw_die_ref);
3763 static void output_pubnames (vec<pubname_entry, va_gc> *);
3764 static void output_aranges (void);
3765 static unsigned int add_ranges (const_tree, bool = false);
3766 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3767                                   bool *, bool);
3768 static void output_ranges (void);
3769 static dw_line_info_table *new_line_info_table (void);
3770 static void output_line_info (bool);
3771 static void output_file_names (void);
3772 static dw_die_ref base_type_die (tree, bool);
3773 static int is_base_type (tree);
3774 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3775 static int decl_quals (const_tree);
3776 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3777 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3778 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3779 static unsigned int dbx_reg_number (const_rtx);
3780 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3781 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3782 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3783 						enum var_init_status);
3784 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3785 						     enum var_init_status);
3786 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3787 					 enum var_init_status);
3788 static int is_based_loc (const_rtx);
3789 static bool resolve_one_addr (rtx *);
3790 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3791 					       enum var_init_status);
3792 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3793 					enum var_init_status);
3794 struct loc_descr_context;
3795 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3796 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3797 static dw_loc_list_ref loc_list_from_tree (tree, int,
3798 					   struct loc_descr_context *);
3799 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3800 						  struct loc_descr_context *);
3801 static tree field_type (const_tree);
3802 static unsigned int simple_type_align_in_bits (const_tree);
3803 static unsigned int simple_decl_align_in_bits (const_tree);
3804 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3805 struct vlr_context;
3806 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3807 					   HOST_WIDE_INT *);
3808 static void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
3809 					 dw_loc_list_ref);
3810 static void add_data_member_location_attribute (dw_die_ref, tree,
3811 						struct vlr_context *);
3812 static bool add_const_value_attribute (dw_die_ref, rtx);
3813 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3814 static void insert_wide_int (const wide_int &, unsigned char *, int);
3815 static unsigned insert_float (const_rtx, unsigned char *);
3816 static rtx rtl_for_decl_location (tree);
3817 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3818 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3819 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3820 static void add_name_attribute (dw_die_ref, const char *);
3821 static void add_desc_attribute (dw_die_ref, tree);
3822 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3823 static void add_comp_dir_attribute (dw_die_ref);
3824 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3825 			     struct loc_descr_context *);
3826 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3827 			    struct loc_descr_context *);
3828 static void add_subscript_info (dw_die_ref, tree, bool);
3829 static void add_byte_size_attribute (dw_die_ref, tree);
3830 static void add_alignment_attribute (dw_die_ref, tree);
3831 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3832 					     struct vlr_context *);
3833 static void add_bit_size_attribute (dw_die_ref, tree);
3834 static void add_prototyped_attribute (dw_die_ref, tree);
3835 static void add_abstract_origin_attribute (dw_die_ref, tree);
3836 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3837 static void add_src_coords_attributes (dw_die_ref, tree);
3838 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3839 static void add_discr_value (dw_die_ref, dw_discr_value *);
3840 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3841 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3842 static dw_die_ref scope_die_for (tree, dw_die_ref);
3843 static inline int local_scope_p (dw_die_ref);
3844 static inline int class_scope_p (dw_die_ref);
3845 static inline int class_or_namespace_scope_p (dw_die_ref);
3846 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3847 static void add_calling_convention_attribute (dw_die_ref, tree);
3848 static const char *type_tag (const_tree);
3849 static tree member_declared_type (const_tree);
3850 #if 0
3851 static const char *decl_start_label (tree);
3852 #endif
3853 static void gen_array_type_die (tree, dw_die_ref);
3854 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3855 #if 0
3856 static void gen_entry_point_die (tree, dw_die_ref);
3857 #endif
3858 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3859 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3860 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3861 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3862 static void gen_formal_types_die (tree, dw_die_ref);
3863 static void gen_subprogram_die (tree, dw_die_ref);
3864 static void gen_variable_die (tree, tree, dw_die_ref);
3865 static void gen_const_die (tree, dw_die_ref);
3866 static void gen_label_die (tree, dw_die_ref);
3867 static void gen_lexical_block_die (tree, dw_die_ref);
3868 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3869 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3870 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3871 static dw_die_ref gen_compile_unit_die (const char *);
3872 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3873 static void gen_member_die (tree, dw_die_ref);
3874 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3875 						enum debug_info_usage);
3876 static void gen_subroutine_type_die (tree, dw_die_ref);
3877 static void gen_typedef_die (tree, dw_die_ref);
3878 static void gen_type_die (tree, dw_die_ref);
3879 static void gen_block_die (tree, dw_die_ref);
3880 static void decls_for_scope (tree, dw_die_ref, bool = true);
3881 static bool is_naming_typedef_decl (const_tree);
3882 static inline dw_die_ref get_context_die (tree);
3883 static void gen_namespace_die (tree, dw_die_ref);
3884 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3885 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3886 static dw_die_ref force_decl_die (tree);
3887 static dw_die_ref force_type_die (tree);
3888 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3889 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3890 static struct dwarf_file_data * lookup_filename (const char *);
3891 static void retry_incomplete_types (void);
3892 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3893 static void gen_generic_params_dies (tree);
3894 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3895 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3896 static void splice_child_die (dw_die_ref, dw_die_ref);
3897 static int file_info_cmp (const void *, const void *);
3898 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3899 				     const char *, var_loc_view, const char *);
3900 static void output_loc_list (dw_loc_list_ref);
3901 static char *gen_internal_sym (const char *);
3902 static bool want_pubnames (void);
3903 
3904 static void prune_unmark_dies (dw_die_ref);
3905 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3906 static void prune_unused_types_mark (dw_die_ref, int);
3907 static void prune_unused_types_walk (dw_die_ref);
3908 static void prune_unused_types_walk_attribs (dw_die_ref);
3909 static void prune_unused_types_prune (dw_die_ref);
3910 static void prune_unused_types (void);
3911 static int maybe_emit_file (struct dwarf_file_data *fd);
3912 static inline const char *AT_vms_delta1 (dw_attr_node *);
3913 static inline const char *AT_vms_delta2 (dw_attr_node *);
3914 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3915 				     const char *, const char *);
3916 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3917 static void gen_remaining_tmpl_value_param_die_attribute (void);
3918 static bool generic_type_p (tree);
3919 static void schedule_generic_params_dies_gen (tree t);
3920 static void gen_scheduled_generic_parms_dies (void);
3921 static void resolve_variable_values (void);
3922 
3923 static const char *comp_dir_string (void);
3924 
3925 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3926 
3927 /* enum for tracking thread-local variables whose address is really an offset
3928    relative to the TLS pointer, which will need link-time relocation, but will
3929    not need relocation by the DWARF consumer.  */
3930 
3931 enum dtprel_bool
3932 {
3933   dtprel_false = 0,
3934   dtprel_true = 1
3935 };
3936 
3937 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3938    use DW_OP_const*.  For regular variables, which need both link-time
3939    relocation and consumer-level relocation (e.g., to account for shared objects
3940    loaded at a random address), we use DW_OP_addr*.  */
3941 
3942 static inline enum dwarf_location_atom
dw_addr_op(enum dtprel_bool dtprel)3943 dw_addr_op (enum dtprel_bool dtprel)
3944 {
3945   if (dtprel == dtprel_true)
3946     return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3947             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3948   else
3949     return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3950 }
3951 
3952 /* Return a pointer to a newly allocated address location description.  If
3953    dwarf_split_debug_info is true, then record the address with the appropriate
3954    relocation.  */
3955 static inline dw_loc_descr_ref
new_addr_loc_descr(rtx addr,enum dtprel_bool dtprel)3956 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3957 {
3958   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3959 
3960   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3961   ref->dw_loc_oprnd1.v.val_addr = addr;
3962   ref->dtprel = dtprel;
3963   if (dwarf_split_debug_info)
3964     ref->dw_loc_oprnd1.val_entry
3965       = add_addr_table_entry (addr,
3966 			      dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3967   else
3968     ref->dw_loc_oprnd1.val_entry = NULL;
3969 
3970   return ref;
3971 }
3972 
3973 /* Section names used to hold DWARF debugging information.  */
3974 
3975 #ifndef DEBUG_INFO_SECTION
3976 #define DEBUG_INFO_SECTION	".debug_info"
3977 #endif
3978 #ifndef DEBUG_DWO_INFO_SECTION
3979 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3980 #endif
3981 #ifndef DEBUG_LTO_INFO_SECTION
3982 #define DEBUG_LTO_INFO_SECTION	".gnu.debuglto_.debug_info"
3983 #endif
3984 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3985 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3986 #endif
3987 #ifndef DEBUG_ABBREV_SECTION
3988 #define DEBUG_ABBREV_SECTION	".debug_abbrev"
3989 #endif
3990 #ifndef DEBUG_LTO_ABBREV_SECTION
3991 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3992 #endif
3993 #ifndef DEBUG_DWO_ABBREV_SECTION
3994 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3995 #endif
3996 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3997 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3998 #endif
3999 #ifndef DEBUG_ARANGES_SECTION
4000 #define DEBUG_ARANGES_SECTION	".debug_aranges"
4001 #endif
4002 #ifndef DEBUG_ADDR_SECTION
4003 #define DEBUG_ADDR_SECTION     ".debug_addr"
4004 #endif
4005 #ifndef DEBUG_MACINFO_SECTION
4006 #define DEBUG_MACINFO_SECTION     ".debug_macinfo"
4007 #endif
4008 #ifndef DEBUG_LTO_MACINFO_SECTION
4009 #define DEBUG_LTO_MACINFO_SECTION      ".gnu.debuglto_.debug_macinfo"
4010 #endif
4011 #ifndef DEBUG_DWO_MACINFO_SECTION
4012 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
4013 #endif
4014 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4015 #define DEBUG_LTO_DWO_MACINFO_SECTION  ".gnu.debuglto_.debug_macinfo.dwo"
4016 #endif
4017 #ifndef DEBUG_MACRO_SECTION
4018 #define DEBUG_MACRO_SECTION	".debug_macro"
4019 #endif
4020 #ifndef DEBUG_LTO_MACRO_SECTION
4021 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4022 #endif
4023 #ifndef DEBUG_DWO_MACRO_SECTION
4024 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
4025 #endif
4026 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4027 #define DEBUG_LTO_DWO_MACRO_SECTION    ".gnu.debuglto_.debug_macro.dwo"
4028 #endif
4029 #ifndef DEBUG_LINE_SECTION
4030 #define DEBUG_LINE_SECTION	".debug_line"
4031 #endif
4032 #ifndef DEBUG_LTO_LINE_SECTION
4033 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4034 #endif
4035 #ifndef DEBUG_DWO_LINE_SECTION
4036 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4037 #endif
4038 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4039 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4040 #endif
4041 #ifndef DEBUG_LOC_SECTION
4042 #define DEBUG_LOC_SECTION	".debug_loc"
4043 #endif
4044 #ifndef DEBUG_DWO_LOC_SECTION
4045 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
4046 #endif
4047 #ifndef DEBUG_LOCLISTS_SECTION
4048 #define DEBUG_LOCLISTS_SECTION	".debug_loclists"
4049 #endif
4050 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4051 #define DEBUG_DWO_LOCLISTS_SECTION  ".debug_loclists.dwo"
4052 #endif
4053 #ifndef DEBUG_PUBNAMES_SECTION
4054 #define DEBUG_PUBNAMES_SECTION	\
4055   ((debug_generate_pub_sections == 2) \
4056    ? ".debug_gnu_pubnames" : ".debug_pubnames")
4057 #endif
4058 #ifndef DEBUG_PUBTYPES_SECTION
4059 #define DEBUG_PUBTYPES_SECTION	\
4060   ((debug_generate_pub_sections == 2) \
4061    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4062 #endif
4063 #ifndef DEBUG_STR_OFFSETS_SECTION
4064 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4065 #endif
4066 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4067 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4068 #endif
4069 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4070 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4071 #endif
4072 #ifndef DEBUG_STR_SECTION
4073 #define DEBUG_STR_SECTION  ".debug_str"
4074 #endif
4075 #ifndef DEBUG_LTO_STR_SECTION
4076 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4077 #endif
4078 #ifndef DEBUG_STR_DWO_SECTION
4079 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
4080 #endif
4081 #ifndef DEBUG_LTO_STR_DWO_SECTION
4082 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4083 #endif
4084 #ifndef DEBUG_RANGES_SECTION
4085 #define DEBUG_RANGES_SECTION	".debug_ranges"
4086 #endif
4087 #ifndef DEBUG_RNGLISTS_SECTION
4088 #define DEBUG_RNGLISTS_SECTION	".debug_rnglists"
4089 #endif
4090 #ifndef DEBUG_LINE_STR_SECTION
4091 #define DEBUG_LINE_STR_SECTION  ".debug_line_str"
4092 #endif
4093 #ifndef DEBUG_LTO_LINE_STR_SECTION
4094 #define DEBUG_LTO_LINE_STR_SECTION  ".gnu.debuglto_.debug_line_str"
4095 #endif
4096 
4097 /* Standard ELF section names for compiled code and data.  */
4098 #ifndef TEXT_SECTION_NAME
4099 #define TEXT_SECTION_NAME	".text"
4100 #endif
4101 
4102 /* Section flags for .debug_str section.  */
4103 #define DEBUG_STR_SECTION_FLAGS                                 \
4104   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4105    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4106    : SECTION_DEBUG)
4107 
4108 /* Section flags for .debug_str.dwo section.  */
4109 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4110 
4111 /* Attribute used to refer to the macro section.  */
4112 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4113 		   : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4114 
4115 /* Labels we insert at beginning sections we can reference instead of
4116    the section names themselves.  */
4117 
4118 #ifndef TEXT_SECTION_LABEL
4119 #define TEXT_SECTION_LABEL                 "Ltext"
4120 #endif
4121 #ifndef COLD_TEXT_SECTION_LABEL
4122 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
4123 #endif
4124 #ifndef DEBUG_LINE_SECTION_LABEL
4125 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
4126 #endif
4127 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4128 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
4129 #endif
4130 #ifndef DEBUG_INFO_SECTION_LABEL
4131 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
4132 #endif
4133 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4134 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
4135 #endif
4136 #ifndef DEBUG_ABBREV_SECTION_LABEL
4137 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
4138 #endif
4139 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4140 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4141 #endif
4142 #ifndef DEBUG_ADDR_SECTION_LABEL
4143 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
4144 #endif
4145 #ifndef DEBUG_LOC_SECTION_LABEL
4146 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
4147 #endif
4148 #ifndef DEBUG_RANGES_SECTION_LABEL
4149 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
4150 #endif
4151 #ifndef DEBUG_MACINFO_SECTION_LABEL
4152 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
4153 #endif
4154 #ifndef DEBUG_MACRO_SECTION_LABEL
4155 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
4156 #endif
4157 #define SKELETON_COMP_DIE_ABBREV 1
4158 #define SKELETON_TYPE_DIE_ABBREV 2
4159 
4160 /* Definitions of defaults for formats and names of various special
4161    (artificial) labels which may be generated within this file (when the -g
4162    options is used and DWARF2_DEBUGGING_INFO is in effect.
4163    If necessary, these may be overridden from within the tm.h file, but
4164    typically, overriding these defaults is unnecessary.  */
4165 
4166 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4167 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4169 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4170 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4171 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4172 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4173 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4174 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4175 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4176 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4177 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4178 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4179 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4180 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4181 
4182 #ifndef TEXT_END_LABEL
4183 #define TEXT_END_LABEL		"Letext"
4184 #endif
4185 #ifndef COLD_END_LABEL
4186 #define COLD_END_LABEL          "Letext_cold"
4187 #endif
4188 #ifndef BLOCK_BEGIN_LABEL
4189 #define BLOCK_BEGIN_LABEL	"LBB"
4190 #endif
4191 #ifndef BLOCK_INLINE_ENTRY_LABEL
4192 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4193 #endif
4194 #ifndef BLOCK_END_LABEL
4195 #define BLOCK_END_LABEL		"LBE"
4196 #endif
4197 #ifndef LINE_CODE_LABEL
4198 #define LINE_CODE_LABEL		"LM"
4199 #endif
4200 
4201 
4202 /* Return the root of the DIE's built for the current compilation unit.  */
4203 static dw_die_ref
comp_unit_die(void)4204 comp_unit_die (void)
4205 {
4206   if (!single_comp_unit_die)
4207     single_comp_unit_die = gen_compile_unit_die (NULL);
4208   return single_comp_unit_die;
4209 }
4210 
4211 /* We allow a language front-end to designate a function that is to be
4212    called to "demangle" any name before it is put into a DIE.  */
4213 
4214 static const char *(*demangle_name_func) (const char *);
4215 
4216 void
dwarf2out_set_demangle_name_func(const char * (* func)(const char *))4217 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4218 {
4219   demangle_name_func = func;
4220 }
4221 
4222 /* Test if rtl node points to a pseudo register.  */
4223 
4224 static inline int
is_pseudo_reg(const_rtx rtl)4225 is_pseudo_reg (const_rtx rtl)
4226 {
4227   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4228 	  || (GET_CODE (rtl) == SUBREG
4229 	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4230 }
4231 
4232 /* Return a reference to a type, with its const and volatile qualifiers
4233    removed.  */
4234 
4235 static inline tree
type_main_variant(tree type)4236 type_main_variant (tree type)
4237 {
4238   type = TYPE_MAIN_VARIANT (type);
4239 
4240   /* ??? There really should be only one main variant among any group of
4241      variants of a given type (and all of the MAIN_VARIANT values for all
4242      members of the group should point to that one type) but sometimes the C
4243      front-end messes this up for array types, so we work around that bug
4244      here.  */
4245   if (TREE_CODE (type) == ARRAY_TYPE)
4246     while (type != TYPE_MAIN_VARIANT (type))
4247       type = TYPE_MAIN_VARIANT (type);
4248 
4249   return type;
4250 }
4251 
4252 /* Return nonzero if the given type node represents a tagged type.  */
4253 
4254 static inline int
is_tagged_type(const_tree type)4255 is_tagged_type (const_tree type)
4256 {
4257   enum tree_code code = TREE_CODE (type);
4258 
4259   return (code == RECORD_TYPE || code == UNION_TYPE
4260 	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4261 }
4262 
4263 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
4264 
4265 static void
get_ref_die_offset_label(char * label,dw_die_ref ref)4266 get_ref_die_offset_label (char *label, dw_die_ref ref)
4267 {
4268   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4269 }
4270 
4271 /* Return die_offset of a DIE reference to a base type.  */
4272 
4273 static unsigned long int
get_base_type_offset(dw_die_ref ref)4274 get_base_type_offset (dw_die_ref ref)
4275 {
4276   if (ref->die_offset)
4277     return ref->die_offset;
4278   if (comp_unit_die ()->die_abbrev)
4279     {
4280       calc_base_type_die_sizes ();
4281       gcc_assert (ref->die_offset);
4282     }
4283   return ref->die_offset;
4284 }
4285 
4286 /* Return die_offset of a DIE reference other than base type.  */
4287 
4288 static unsigned long int
get_ref_die_offset(dw_die_ref ref)4289 get_ref_die_offset (dw_die_ref ref)
4290 {
4291   gcc_assert (ref->die_offset);
4292   return ref->die_offset;
4293 }
4294 
4295 /* Convert a DIE tag into its string name.  */
4296 
4297 static const char *
dwarf_tag_name(unsigned int tag)4298 dwarf_tag_name (unsigned int tag)
4299 {
4300   const char *name = get_DW_TAG_name (tag);
4301 
4302   if (name != NULL)
4303     return name;
4304 
4305   return "DW_TAG_<unknown>";
4306 }
4307 
4308 /* Convert a DWARF attribute code into its string name.  */
4309 
4310 static const char *
dwarf_attr_name(unsigned int attr)4311 dwarf_attr_name (unsigned int attr)
4312 {
4313   const char *name;
4314 
4315   switch (attr)
4316     {
4317 #if VMS_DEBUGGING_INFO
4318     case DW_AT_HP_prologue:
4319       return "DW_AT_HP_prologue";
4320 #else
4321     case DW_AT_MIPS_loop_unroll_factor:
4322       return "DW_AT_MIPS_loop_unroll_factor";
4323 #endif
4324 
4325 #if VMS_DEBUGGING_INFO
4326     case DW_AT_HP_epilogue:
4327       return "DW_AT_HP_epilogue";
4328 #else
4329     case DW_AT_MIPS_stride:
4330       return "DW_AT_MIPS_stride";
4331 #endif
4332     }
4333 
4334   name = get_DW_AT_name (attr);
4335 
4336   if (name != NULL)
4337     return name;
4338 
4339   return "DW_AT_<unknown>";
4340 }
4341 
4342 /* Convert a DWARF value form code into its string name.  */
4343 
4344 static const char *
dwarf_form_name(unsigned int form)4345 dwarf_form_name (unsigned int form)
4346 {
4347   const char *name = get_DW_FORM_name (form);
4348 
4349   if (name != NULL)
4350     return name;
4351 
4352   return "DW_FORM_<unknown>";
4353 }
4354 
4355 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4356    instance of an inlined instance of a decl which is local to an inline
4357    function, so we have to trace all of the way back through the origin chain
4358    to find out what sort of node actually served as the original seed for the
4359    given block.  */
4360 
4361 static tree
decl_ultimate_origin(const_tree decl)4362 decl_ultimate_origin (const_tree decl)
4363 {
4364   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4365     return NULL_TREE;
4366 
4367   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4368      we're trying to output the abstract instance of this function.  */
4369   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4370     return NULL_TREE;
4371 
4372   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4373      most distant ancestor, this should never happen.  */
4374   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4375 
4376   return DECL_ABSTRACT_ORIGIN (decl);
4377 }
4378 
4379 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4380    of a virtual function may refer to a base class, so we check the 'this'
4381    parameter.  */
4382 
4383 static tree
decl_class_context(tree decl)4384 decl_class_context (tree decl)
4385 {
4386   tree context = NULL_TREE;
4387 
4388   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4389     context = DECL_CONTEXT (decl);
4390   else
4391     context = TYPE_MAIN_VARIANT
4392       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4393 
4394   if (context && !TYPE_P (context))
4395     context = NULL_TREE;
4396 
4397   return context;
4398 }
4399 
4400 /* Add an attribute/value pair to a DIE.  */
4401 
4402 static inline void
add_dwarf_attr(dw_die_ref die,dw_attr_node * attr)4403 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4404 {
4405   /* Maybe this should be an assert?  */
4406   if (die == NULL)
4407     return;
4408 
4409   if (flag_checking)
4410     {
4411       /* Check we do not add duplicate attrs.  Can't use get_AT here
4412          because that recurses to the specification/abstract origin DIE.  */
4413       dw_attr_node *a;
4414       unsigned ix;
4415       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4416 	gcc_assert (a->dw_attr != attr->dw_attr);
4417     }
4418 
4419   vec_safe_reserve (die->die_attr, 1);
4420   vec_safe_push (die->die_attr, *attr);
4421 }
4422 
4423 static inline enum dw_val_class
AT_class(dw_attr_node * a)4424 AT_class (dw_attr_node *a)
4425 {
4426   return a->dw_attr_val.val_class;
4427 }
4428 
4429 /* Return the index for any attribute that will be referenced with a
4430    DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index.  String
4431    indices are stored in dw_attr_val.v.val_str for reference counting
4432    pruning.  */
4433 
4434 static inline unsigned int
AT_index(dw_attr_node * a)4435 AT_index (dw_attr_node *a)
4436 {
4437   if (AT_class (a) == dw_val_class_str)
4438     return a->dw_attr_val.v.val_str->index;
4439   else if (a->dw_attr_val.val_entry != NULL)
4440     return a->dw_attr_val.val_entry->index;
4441   return NOT_INDEXED;
4442 }
4443 
4444 /* Add a flag value attribute to a DIE.  */
4445 
4446 static inline void
add_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned int flag)4447 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4448 {
4449   dw_attr_node attr;
4450 
4451   attr.dw_attr = attr_kind;
4452   attr.dw_attr_val.val_class = dw_val_class_flag;
4453   attr.dw_attr_val.val_entry = NULL;
4454   attr.dw_attr_val.v.val_flag = flag;
4455   add_dwarf_attr (die, &attr);
4456 }
4457 
4458 static inline unsigned
AT_flag(dw_attr_node * a)4459 AT_flag (dw_attr_node *a)
4460 {
4461   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4462   return a->dw_attr_val.v.val_flag;
4463 }
4464 
4465 /* Add a signed integer attribute value to a DIE.  */
4466 
4467 static inline void
add_AT_int(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT int_val)4468 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4469 {
4470   dw_attr_node attr;
4471 
4472   attr.dw_attr = attr_kind;
4473   attr.dw_attr_val.val_class = dw_val_class_const;
4474   attr.dw_attr_val.val_entry = NULL;
4475   attr.dw_attr_val.v.val_int = int_val;
4476   add_dwarf_attr (die, &attr);
4477 }
4478 
4479 static inline HOST_WIDE_INT
AT_int(dw_attr_node * a)4480 AT_int (dw_attr_node *a)
4481 {
4482   gcc_assert (a && (AT_class (a) == dw_val_class_const
4483 		    || AT_class (a) == dw_val_class_const_implicit));
4484   return a->dw_attr_val.v.val_int;
4485 }
4486 
4487 /* Add an unsigned integer attribute value to a DIE.  */
4488 
4489 static inline void
add_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned HOST_WIDE_INT unsigned_val)4490 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4491 		 unsigned HOST_WIDE_INT unsigned_val)
4492 {
4493   dw_attr_node attr;
4494 
4495   attr.dw_attr = attr_kind;
4496   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4497   attr.dw_attr_val.val_entry = NULL;
4498   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4499   add_dwarf_attr (die, &attr);
4500 }
4501 
4502 static inline unsigned HOST_WIDE_INT
AT_unsigned(dw_attr_node * a)4503 AT_unsigned (dw_attr_node *a)
4504 {
4505   gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4506 		    || AT_class (a) == dw_val_class_unsigned_const_implicit));
4507   return a->dw_attr_val.v.val_unsigned;
4508 }
4509 
4510 /* Add an unsigned wide integer attribute value to a DIE.  */
4511 
4512 static inline void
add_AT_wide(dw_die_ref die,enum dwarf_attribute attr_kind,const wide_int & w)4513 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4514 	     const wide_int& w)
4515 {
4516   dw_attr_node attr;
4517 
4518   attr.dw_attr = attr_kind;
4519   attr.dw_attr_val.val_class = dw_val_class_wide_int;
4520   attr.dw_attr_val.val_entry = NULL;
4521   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4522   *attr.dw_attr_val.v.val_wide = w;
4523   add_dwarf_attr (die, &attr);
4524 }
4525 
4526 /* Add an unsigned double integer attribute value to a DIE.  */
4527 
4528 static inline void
add_AT_double(dw_die_ref die,enum dwarf_attribute attr_kind,HOST_WIDE_INT high,unsigned HOST_WIDE_INT low)4529 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4530 	       HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4531 {
4532   dw_attr_node attr;
4533 
4534   attr.dw_attr = attr_kind;
4535   attr.dw_attr_val.val_class = dw_val_class_const_double;
4536   attr.dw_attr_val.val_entry = NULL;
4537   attr.dw_attr_val.v.val_double.high = high;
4538   attr.dw_attr_val.v.val_double.low = low;
4539   add_dwarf_attr (die, &attr);
4540 }
4541 
4542 /* Add a floating point attribute value to a DIE and return it.  */
4543 
4544 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)4545 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4546 	    unsigned int length, unsigned int elt_size, unsigned char *array)
4547 {
4548   dw_attr_node attr;
4549 
4550   attr.dw_attr = attr_kind;
4551   attr.dw_attr_val.val_class = dw_val_class_vec;
4552   attr.dw_attr_val.val_entry = NULL;
4553   attr.dw_attr_val.v.val_vec.length = length;
4554   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4555   attr.dw_attr_val.v.val_vec.array = array;
4556   add_dwarf_attr (die, &attr);
4557 }
4558 
4559 /* Add an 8-byte data attribute value to a DIE.  */
4560 
4561 static inline void
add_AT_data8(dw_die_ref die,enum dwarf_attribute attr_kind,unsigned char data8[8])4562 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4563               unsigned char data8[8])
4564 {
4565   dw_attr_node attr;
4566 
4567   attr.dw_attr = attr_kind;
4568   attr.dw_attr_val.val_class = dw_val_class_data8;
4569   attr.dw_attr_val.val_entry = NULL;
4570   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4571   add_dwarf_attr (die, &attr);
4572 }
4573 
4574 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
4575    dwarf_split_debug_info, address attributes in dies destined for the
4576    final executable have force_direct set to avoid using indexed
4577    references.  */
4578 
4579 static inline void
add_AT_low_high_pc(dw_die_ref die,const char * lbl_low,const char * lbl_high,bool force_direct)4580 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4581                     bool force_direct)
4582 {
4583   dw_attr_node attr;
4584   char * lbl_id;
4585 
4586   lbl_id = xstrdup (lbl_low);
4587   attr.dw_attr = DW_AT_low_pc;
4588   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4589   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4590   if (dwarf_split_debug_info && !force_direct)
4591     attr.dw_attr_val.val_entry
4592       = add_addr_table_entry (lbl_id, ate_kind_label);
4593   else
4594     attr.dw_attr_val.val_entry = NULL;
4595   add_dwarf_attr (die, &attr);
4596 
4597   attr.dw_attr = DW_AT_high_pc;
4598   if (dwarf_version < 4)
4599     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4600   else
4601     attr.dw_attr_val.val_class = dw_val_class_high_pc;
4602   lbl_id = xstrdup (lbl_high);
4603   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4604   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4605       && dwarf_split_debug_info && !force_direct)
4606     attr.dw_attr_val.val_entry
4607       = add_addr_table_entry (lbl_id, ate_kind_label);
4608   else
4609     attr.dw_attr_val.val_entry = NULL;
4610   add_dwarf_attr (die, &attr);
4611 }
4612 
4613 /* Hash and equality functions for debug_str_hash.  */
4614 
4615 hashval_t
hash(indirect_string_node * x)4616 indirect_string_hasher::hash (indirect_string_node *x)
4617 {
4618   return htab_hash_string (x->str);
4619 }
4620 
4621 bool
equal(indirect_string_node * x1,const char * x2)4622 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4623 {
4624   return strcmp (x1->str, x2) == 0;
4625 }
4626 
4627 /* Add STR to the given string hash table.  */
4628 
4629 static struct indirect_string_node *
4630 find_AT_string_in_table (const char *str,
4631 			 hash_table<indirect_string_hasher> *table,
4632 			 enum insert_option insert = INSERT)
4633 {
4634   struct indirect_string_node *node;
4635 
4636   indirect_string_node **slot
4637     = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4638   if (*slot == NULL)
4639     {
4640       node = ggc_cleared_alloc<indirect_string_node> ();
4641       node->str = ggc_strdup (str);
4642       *slot = node;
4643     }
4644   else
4645     node = *slot;
4646 
4647   node->refcount++;
4648   return node;
4649 }
4650 
4651 /* Add STR to the indirect string hash table.  */
4652 
4653 static struct indirect_string_node *
4654 find_AT_string (const char *str, enum insert_option insert = INSERT)
4655 {
4656   if (! debug_str_hash)
4657     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4658 
4659   return find_AT_string_in_table (str, debug_str_hash, insert);
4660 }
4661 
4662 /* Add a string attribute value to a DIE.  */
4663 
4664 static inline void
add_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)4665 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4666 {
4667   dw_attr_node attr;
4668   struct indirect_string_node *node;
4669 
4670   node = find_AT_string (str);
4671 
4672   attr.dw_attr = attr_kind;
4673   attr.dw_attr_val.val_class = dw_val_class_str;
4674   attr.dw_attr_val.val_entry = NULL;
4675   attr.dw_attr_val.v.val_str = node;
4676   add_dwarf_attr (die, &attr);
4677 }
4678 
4679 static inline const char *
AT_string(dw_attr_node * a)4680 AT_string (dw_attr_node *a)
4681 {
4682   gcc_assert (a && AT_class (a) == dw_val_class_str);
4683   return a->dw_attr_val.v.val_str->str;
4684 }
4685 
4686 /* Call this function directly to bypass AT_string_form's logic to put
4687    the string inline in the die. */
4688 
4689 static void
set_indirect_string(struct indirect_string_node * node)4690 set_indirect_string (struct indirect_string_node *node)
4691 {
4692   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4693   /* Already indirect is a no op.  */
4694   if (node->form == DW_FORM_strp
4695       || node->form == DW_FORM_line_strp
4696       || node->form == dwarf_FORM (DW_FORM_strx))
4697     {
4698       gcc_assert (node->label);
4699       return;
4700     }
4701   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4702   ++dw2_string_counter;
4703   node->label = xstrdup (label);
4704 
4705   if (!dwarf_split_debug_info)
4706     {
4707       node->form = DW_FORM_strp;
4708       node->index = NOT_INDEXED;
4709     }
4710   else
4711     {
4712       node->form = dwarf_FORM (DW_FORM_strx);
4713       node->index = NO_INDEX_ASSIGNED;
4714     }
4715 }
4716 
4717 /* A helper function for dwarf2out_finish, called to reset indirect
4718    string decisions done for early LTO dwarf output before fat object
4719    dwarf output.  */
4720 
4721 int
reset_indirect_string(indirect_string_node ** h,void *)4722 reset_indirect_string (indirect_string_node **h, void *)
4723 {
4724   struct indirect_string_node *node = *h;
4725   if (node->form == DW_FORM_strp || node->form == dwarf_FORM (DW_FORM_strx))
4726     {
4727       free (node->label);
4728       node->label = NULL;
4729       node->form = (dwarf_form) 0;
4730       node->index = 0;
4731     }
4732   return 1;
4733 }
4734 
4735 /* Find out whether a string should be output inline in DIE
4736    or out-of-line in .debug_str section.  */
4737 
4738 static enum dwarf_form
find_string_form(struct indirect_string_node * node)4739 find_string_form (struct indirect_string_node *node)
4740 {
4741   unsigned int len;
4742 
4743   if (node->form)
4744     return node->form;
4745 
4746   len = strlen (node->str) + 1;
4747 
4748   /* If the string is shorter or equal to the size of the reference, it is
4749      always better to put it inline.  */
4750   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4751     return node->form = DW_FORM_string;
4752 
4753   /* If we cannot expect the linker to merge strings in .debug_str
4754      section, only put it into .debug_str if it is worth even in this
4755      single module.  */
4756   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4757       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4758 	  && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4759     return node->form = DW_FORM_string;
4760 
4761   set_indirect_string (node);
4762 
4763   return node->form;
4764 }
4765 
4766 /* Find out whether the string referenced from the attribute should be
4767    output inline in DIE or out-of-line in .debug_str section.  */
4768 
4769 static enum dwarf_form
AT_string_form(dw_attr_node * a)4770 AT_string_form (dw_attr_node *a)
4771 {
4772   gcc_assert (a && AT_class (a) == dw_val_class_str);
4773   return find_string_form (a->dw_attr_val.v.val_str);
4774 }
4775 
4776 /* Add a DIE reference attribute value to a DIE.  */
4777 
4778 static inline void
add_AT_die_ref(dw_die_ref die,enum dwarf_attribute attr_kind,dw_die_ref targ_die)4779 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4780 {
4781   dw_attr_node attr;
4782   gcc_checking_assert (targ_die != NULL);
4783 
4784   /* With LTO we can end up trying to reference something we didn't create
4785      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4786   if (targ_die == NULL)
4787     return;
4788 
4789   attr.dw_attr = attr_kind;
4790   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4791   attr.dw_attr_val.val_entry = NULL;
4792   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4793   attr.dw_attr_val.v.val_die_ref.external = 0;
4794   add_dwarf_attr (die, &attr);
4795 }
4796 
4797 /* Change DIE reference REF to point to NEW_DIE instead.  */
4798 
4799 static inline void
change_AT_die_ref(dw_attr_node * ref,dw_die_ref new_die)4800 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4801 {
4802   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4803   ref->dw_attr_val.v.val_die_ref.die = new_die;
4804   ref->dw_attr_val.v.val_die_ref.external = 0;
4805 }
4806 
4807 /* Add an AT_specification attribute to a DIE, and also make the back
4808    pointer from the specification to the definition.  */
4809 
4810 static inline void
add_AT_specification(dw_die_ref die,dw_die_ref targ_die)4811 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4812 {
4813   add_AT_die_ref (die, DW_AT_specification, targ_die);
4814   gcc_assert (!targ_die->die_definition);
4815   targ_die->die_definition = die;
4816 }
4817 
4818 static inline dw_die_ref
AT_ref(dw_attr_node * a)4819 AT_ref (dw_attr_node *a)
4820 {
4821   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4822   return a->dw_attr_val.v.val_die_ref.die;
4823 }
4824 
4825 static inline int
AT_ref_external(dw_attr_node * a)4826 AT_ref_external (dw_attr_node *a)
4827 {
4828   if (a && AT_class (a) == dw_val_class_die_ref)
4829     return a->dw_attr_val.v.val_die_ref.external;
4830 
4831   return 0;
4832 }
4833 
4834 static inline void
set_AT_ref_external(dw_attr_node * a,int i)4835 set_AT_ref_external (dw_attr_node *a, int i)
4836 {
4837   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4838   a->dw_attr_val.v.val_die_ref.external = i;
4839 }
4840 
4841 /* Add a location description attribute value to a DIE.  */
4842 
4843 static inline void
add_AT_loc(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_descr_ref loc)4844 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4845 {
4846   dw_attr_node attr;
4847 
4848   attr.dw_attr = attr_kind;
4849   attr.dw_attr_val.val_class = dw_val_class_loc;
4850   attr.dw_attr_val.val_entry = NULL;
4851   attr.dw_attr_val.v.val_loc = loc;
4852   add_dwarf_attr (die, &attr);
4853 }
4854 
4855 static inline dw_loc_descr_ref
AT_loc(dw_attr_node * a)4856 AT_loc (dw_attr_node *a)
4857 {
4858   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4859   return a->dw_attr_val.v.val_loc;
4860 }
4861 
4862 static inline void
add_AT_loc_list(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref loc_list)4863 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4864 {
4865   dw_attr_node attr;
4866 
4867   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4868     return;
4869 
4870   attr.dw_attr = attr_kind;
4871   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4872   attr.dw_attr_val.val_entry = NULL;
4873   attr.dw_attr_val.v.val_loc_list = loc_list;
4874   add_dwarf_attr (die, &attr);
4875   have_location_lists = true;
4876 }
4877 
4878 static inline dw_loc_list_ref
AT_loc_list(dw_attr_node * a)4879 AT_loc_list (dw_attr_node *a)
4880 {
4881   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4882   return a->dw_attr_val.v.val_loc_list;
4883 }
4884 
4885 /* Add a view list attribute to DIE.  It must have a DW_AT_location
4886    attribute, because the view list complements the location list.  */
4887 
4888 static inline void
add_AT_view_list(dw_die_ref die,enum dwarf_attribute attr_kind)4889 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4890 {
4891   dw_attr_node attr;
4892 
4893   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4894     return;
4895 
4896   attr.dw_attr = attr_kind;
4897   attr.dw_attr_val.val_class = dw_val_class_view_list;
4898   attr.dw_attr_val.val_entry = NULL;
4899   attr.dw_attr_val.v.val_view_list = die;
4900   add_dwarf_attr (die, &attr);
4901   gcc_checking_assert (get_AT (die, DW_AT_location));
4902   gcc_assert (have_location_lists);
4903 }
4904 
4905 /* Return a pointer to the location list referenced by the attribute.
4906    If the named attribute is a view list, look up the corresponding
4907    DW_AT_location attribute and return its location list.  */
4908 
4909 static inline dw_loc_list_ref *
AT_loc_list_ptr(dw_attr_node * a)4910 AT_loc_list_ptr (dw_attr_node *a)
4911 {
4912   gcc_assert (a);
4913   switch (AT_class (a))
4914     {
4915     case dw_val_class_loc_list:
4916       return &a->dw_attr_val.v.val_loc_list;
4917     case dw_val_class_view_list:
4918       {
4919 	dw_attr_node *l;
4920 	l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4921 	if (!l)
4922 	  return NULL;
4923 	gcc_checking_assert (l + 1 == a);
4924 	return AT_loc_list_ptr (l);
4925       }
4926     default:
4927       gcc_unreachable ();
4928     }
4929 }
4930 
4931 /* Return the location attribute value associated with a view list
4932    attribute value.  */
4933 
4934 static inline dw_val_node *
view_list_to_loc_list_val_node(dw_val_node * val)4935 view_list_to_loc_list_val_node (dw_val_node *val)
4936 {
4937   gcc_assert (val->val_class == dw_val_class_view_list);
4938   dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4939   if (!loc)
4940     return NULL;
4941   gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4942   gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4943   return &loc->dw_attr_val;
4944 }
4945 
4946 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4947 {
4948   static hashval_t hash (addr_table_entry *);
4949   static bool equal (addr_table_entry *, addr_table_entry *);
4950 };
4951 
4952 /* Table of entries into the .debug_addr section.  */
4953 
4954 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4955 
4956 /* Hash an address_table_entry.  */
4957 
4958 hashval_t
hash(addr_table_entry * a)4959 addr_hasher::hash (addr_table_entry *a)
4960 {
4961   inchash::hash hstate;
4962   switch (a->kind)
4963     {
4964       case ate_kind_rtx:
4965 	hstate.add_int (0);
4966 	break;
4967       case ate_kind_rtx_dtprel:
4968 	hstate.add_int (1);
4969 	break;
4970       case ate_kind_label:
4971         return htab_hash_string (a->addr.label);
4972       default:
4973         gcc_unreachable ();
4974     }
4975   inchash::add_rtx (a->addr.rtl, hstate);
4976   return hstate.end ();
4977 }
4978 
4979 /* Determine equality for two address_table_entries.  */
4980 
4981 bool
equal(addr_table_entry * a1,addr_table_entry * a2)4982 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4983 {
4984   if (a1->kind != a2->kind)
4985     return 0;
4986   switch (a1->kind)
4987     {
4988       case ate_kind_rtx:
4989       case ate_kind_rtx_dtprel:
4990         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4991       case ate_kind_label:
4992         return strcmp (a1->addr.label, a2->addr.label) == 0;
4993       default:
4994         gcc_unreachable ();
4995     }
4996 }
4997 
4998 /* Initialize an addr_table_entry.  */
4999 
5000 void
init_addr_table_entry(addr_table_entry * e,enum ate_kind kind,void * addr)5001 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5002 {
5003   e->kind = kind;
5004   switch (kind)
5005     {
5006       case ate_kind_rtx:
5007       case ate_kind_rtx_dtprel:
5008         e->addr.rtl = (rtx) addr;
5009         break;
5010       case ate_kind_label:
5011         e->addr.label = (char *) addr;
5012         break;
5013     }
5014   e->refcount = 0;
5015   e->index = NO_INDEX_ASSIGNED;
5016 }
5017 
5018 /* Add attr to the address table entry to the table.  Defer setting an
5019    index until output time.  */
5020 
5021 static addr_table_entry *
add_addr_table_entry(void * addr,enum ate_kind kind)5022 add_addr_table_entry (void *addr, enum ate_kind kind)
5023 {
5024   addr_table_entry *node;
5025   addr_table_entry finder;
5026 
5027   gcc_assert (dwarf_split_debug_info);
5028   if (! addr_index_table)
5029     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5030   init_addr_table_entry (&finder, kind, addr);
5031   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5032 
5033   if (*slot == HTAB_EMPTY_ENTRY)
5034     {
5035       node = ggc_cleared_alloc<addr_table_entry> ();
5036       init_addr_table_entry (node, kind, addr);
5037       *slot = node;
5038     }
5039   else
5040     node = *slot;
5041 
5042   node->refcount++;
5043   return node;
5044 }
5045 
5046 /* Remove an entry from the addr table by decrementing its refcount.
5047    Strictly, decrementing the refcount would be enough, but the
5048    assertion that the entry is actually in the table has found
5049    bugs.  */
5050 
5051 static void
remove_addr_table_entry(addr_table_entry * entry)5052 remove_addr_table_entry (addr_table_entry *entry)
5053 {
5054   gcc_assert (dwarf_split_debug_info && addr_index_table);
5055   /* After an index is assigned, the table is frozen.  */
5056   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5057   entry->refcount--;
5058 }
5059 
5060 /* Given a location list, remove all addresses it refers to from the
5061    address_table.  */
5062 
5063 static void
remove_loc_list_addr_table_entries(dw_loc_descr_ref descr)5064 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5065 {
5066   for (; descr; descr = descr->dw_loc_next)
5067     if (descr->dw_loc_oprnd1.val_entry != NULL)
5068       {
5069         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5070         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5071       }
5072 }
5073 
5074 /* A helper function for dwarf2out_finish called through
5075    htab_traverse.  Assign an addr_table_entry its index.  All entries
5076    must be collected into the table when this function is called,
5077    because the indexing code relies on htab_traverse to traverse nodes
5078    in the same order for each run. */
5079 
5080 int
index_addr_table_entry(addr_table_entry ** h,unsigned int * index)5081 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5082 {
5083   addr_table_entry *node = *h;
5084 
5085   /* Don't index unreferenced nodes.  */
5086   if (node->refcount == 0)
5087     return 1;
5088 
5089   gcc_assert (node->index == NO_INDEX_ASSIGNED);
5090   node->index = *index;
5091   *index += 1;
5092 
5093   return 1;
5094 }
5095 
5096 /* Add an address constant attribute value to a DIE.  When using
5097    dwarf_split_debug_info, address attributes in dies destined for the
5098    final executable should be direct references--setting the parameter
5099    force_direct ensures this behavior.  */
5100 
5101 static inline void
add_AT_addr(dw_die_ref die,enum dwarf_attribute attr_kind,rtx addr,bool force_direct)5102 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5103              bool force_direct)
5104 {
5105   dw_attr_node attr;
5106 
5107   attr.dw_attr = attr_kind;
5108   attr.dw_attr_val.val_class = dw_val_class_addr;
5109   attr.dw_attr_val.v.val_addr = addr;
5110   if (dwarf_split_debug_info && !force_direct)
5111     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5112   else
5113     attr.dw_attr_val.val_entry = NULL;
5114   add_dwarf_attr (die, &attr);
5115 }
5116 
5117 /* Get the RTX from to an address DIE attribute.  */
5118 
5119 static inline rtx
AT_addr(dw_attr_node * a)5120 AT_addr (dw_attr_node *a)
5121 {
5122   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5123   return a->dw_attr_val.v.val_addr;
5124 }
5125 
5126 /* Add a file attribute value to a DIE.  */
5127 
5128 static inline void
add_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind,struct dwarf_file_data * fd)5129 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5130 	     struct dwarf_file_data *fd)
5131 {
5132   dw_attr_node attr;
5133 
5134   attr.dw_attr = attr_kind;
5135   attr.dw_attr_val.val_class = dw_val_class_file;
5136   attr.dw_attr_val.val_entry = NULL;
5137   attr.dw_attr_val.v.val_file = fd;
5138   add_dwarf_attr (die, &attr);
5139 }
5140 
5141 /* Get the dwarf_file_data from a file DIE attribute.  */
5142 
5143 static inline struct dwarf_file_data *
AT_file(dw_attr_node * a)5144 AT_file (dw_attr_node *a)
5145 {
5146   gcc_assert (a && (AT_class (a) == dw_val_class_file
5147 		    || AT_class (a) == dw_val_class_file_implicit));
5148   return a->dw_attr_val.v.val_file;
5149 }
5150 
5151 /* Add a vms delta attribute value to a DIE.  */
5152 
5153 static inline void
add_AT_vms_delta(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl1,const char * lbl2)5154 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5155 		  const char *lbl1, const char *lbl2)
5156 {
5157   dw_attr_node attr;
5158 
5159   attr.dw_attr = attr_kind;
5160   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5161   attr.dw_attr_val.val_entry = NULL;
5162   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5163   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5164   add_dwarf_attr (die, &attr);
5165 }
5166 
5167 /* Add a symbolic view identifier attribute value to a DIE.  */
5168 
5169 static inline void
add_AT_symview(dw_die_ref die,enum dwarf_attribute attr_kind,const char * view_label)5170 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5171                const char *view_label)
5172 {
5173   dw_attr_node attr;
5174 
5175   attr.dw_attr = attr_kind;
5176   attr.dw_attr_val.val_class = dw_val_class_symview;
5177   attr.dw_attr_val.val_entry = NULL;
5178   attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5179   add_dwarf_attr (die, &attr);
5180 }
5181 
5182 /* Add a label identifier attribute value to a DIE.  */
5183 
5184 static inline void
add_AT_lbl_id(dw_die_ref die,enum dwarf_attribute attr_kind,const char * lbl_id)5185 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5186                const char *lbl_id)
5187 {
5188   dw_attr_node attr;
5189 
5190   attr.dw_attr = attr_kind;
5191   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5192   attr.dw_attr_val.val_entry = NULL;
5193   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5194   if (dwarf_split_debug_info)
5195     attr.dw_attr_val.val_entry
5196         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5197                                 ate_kind_label);
5198   add_dwarf_attr (die, &attr);
5199 }
5200 
5201 /* Add a section offset attribute value to a DIE, an offset into the
5202    debug_line section.  */
5203 
5204 static inline void
add_AT_lineptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)5205 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5206 		const char *label)
5207 {
5208   dw_attr_node attr;
5209 
5210   attr.dw_attr = attr_kind;
5211   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5212   attr.dw_attr_val.val_entry = NULL;
5213   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5214   add_dwarf_attr (die, &attr);
5215 }
5216 
5217 /* Add a section offset attribute value to a DIE, an offset into the
5218    debug_macinfo section.  */
5219 
5220 static inline void
add_AT_macptr(dw_die_ref die,enum dwarf_attribute attr_kind,const char * label)5221 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5222 	       const char *label)
5223 {
5224   dw_attr_node attr;
5225 
5226   attr.dw_attr = attr_kind;
5227   attr.dw_attr_val.val_class = dw_val_class_macptr;
5228   attr.dw_attr_val.val_entry = NULL;
5229   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5230   add_dwarf_attr (die, &attr);
5231 }
5232 
5233 /* Add a range_list attribute value to a DIE.  When using
5234    dwarf_split_debug_info, address attributes in dies destined for the
5235    final executable should be direct references--setting the parameter
5236    force_direct ensures this behavior.  */
5237 
5238 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5239 #define RELOCATED_OFFSET (NULL)
5240 
5241 static void
add_AT_range_list(dw_die_ref die,enum dwarf_attribute attr_kind,long unsigned int offset,bool force_direct)5242 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5243                    long unsigned int offset, bool force_direct)
5244 {
5245   dw_attr_node attr;
5246 
5247   attr.dw_attr = attr_kind;
5248   attr.dw_attr_val.val_class = dw_val_class_range_list;
5249   /* For the range_list attribute, use val_entry to store whether the
5250      offset should follow split-debug-info or normal semantics.  This
5251      value is read in output_range_list_offset.  */
5252   if (dwarf_split_debug_info && !force_direct)
5253     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5254   else
5255     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5256   attr.dw_attr_val.v.val_offset = offset;
5257   add_dwarf_attr (die, &attr);
5258 }
5259 
5260 /* Return the start label of a delta attribute.  */
5261 
5262 static inline const char *
AT_vms_delta1(dw_attr_node * a)5263 AT_vms_delta1 (dw_attr_node *a)
5264 {
5265   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5266   return a->dw_attr_val.v.val_vms_delta.lbl1;
5267 }
5268 
5269 /* Return the end label of a delta attribute.  */
5270 
5271 static inline const char *
AT_vms_delta2(dw_attr_node * a)5272 AT_vms_delta2 (dw_attr_node *a)
5273 {
5274   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5275   return a->dw_attr_val.v.val_vms_delta.lbl2;
5276 }
5277 
5278 static inline const char *
AT_lbl(dw_attr_node * a)5279 AT_lbl (dw_attr_node *a)
5280 {
5281   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5282 		    || AT_class (a) == dw_val_class_lineptr
5283 		    || AT_class (a) == dw_val_class_macptr
5284 		    || AT_class (a) == dw_val_class_loclistsptr
5285 		    || AT_class (a) == dw_val_class_high_pc));
5286   return a->dw_attr_val.v.val_lbl_id;
5287 }
5288 
5289 /* Get the attribute of type attr_kind.  */
5290 
5291 static dw_attr_node *
get_AT(dw_die_ref die,enum dwarf_attribute attr_kind)5292 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5293 {
5294   dw_attr_node *a;
5295   unsigned ix;
5296   dw_die_ref spec = NULL;
5297 
5298   if (! die)
5299     return NULL;
5300 
5301   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5302     if (a->dw_attr == attr_kind)
5303       return a;
5304     else if (a->dw_attr == DW_AT_specification
5305 	     || a->dw_attr == DW_AT_abstract_origin)
5306       spec = AT_ref (a);
5307 
5308   if (spec)
5309     return get_AT (spec, attr_kind);
5310 
5311   return NULL;
5312 }
5313 
5314 /* Returns the parent of the declaration of DIE.  */
5315 
5316 static dw_die_ref
get_die_parent(dw_die_ref die)5317 get_die_parent (dw_die_ref die)
5318 {
5319   dw_die_ref t;
5320 
5321   if (!die)
5322     return NULL;
5323 
5324   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5325       || (t = get_AT_ref (die, DW_AT_specification)))
5326     die = t;
5327 
5328   return die->die_parent;
5329 }
5330 
5331 /* Return the "low pc" attribute value, typically associated with a subprogram
5332    DIE.  Return null if the "low pc" attribute is either not present, or if it
5333    cannot be represented as an assembler label identifier.  */
5334 
5335 static inline const char *
get_AT_low_pc(dw_die_ref die)5336 get_AT_low_pc (dw_die_ref die)
5337 {
5338   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5339 
5340   return a ? AT_lbl (a) : NULL;
5341 }
5342 
5343 /* Return the value of the string attribute designated by ATTR_KIND, or
5344    NULL if it is not present.  */
5345 
5346 static inline const char *
get_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind)5347 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5348 {
5349   dw_attr_node *a = get_AT (die, attr_kind);
5350 
5351   return a ? AT_string (a) : NULL;
5352 }
5353 
5354 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5355    if it is not present.  */
5356 
5357 static inline int
get_AT_flag(dw_die_ref die,enum dwarf_attribute attr_kind)5358 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5359 {
5360   dw_attr_node *a = get_AT (die, attr_kind);
5361 
5362   return a ? AT_flag (a) : 0;
5363 }
5364 
5365 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5366    if it is not present.  */
5367 
5368 static inline unsigned
get_AT_unsigned(dw_die_ref die,enum dwarf_attribute attr_kind)5369 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5370 {
5371   dw_attr_node *a = get_AT (die, attr_kind);
5372 
5373   return a ? AT_unsigned (a) : 0;
5374 }
5375 
5376 static inline dw_die_ref
get_AT_ref(dw_die_ref die,enum dwarf_attribute attr_kind)5377 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5378 {
5379   dw_attr_node *a = get_AT (die, attr_kind);
5380 
5381   return a ? AT_ref (a) : NULL;
5382 }
5383 
5384 static inline struct dwarf_file_data *
get_AT_file(dw_die_ref die,enum dwarf_attribute attr_kind)5385 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5386 {
5387   dw_attr_node *a = get_AT (die, attr_kind);
5388 
5389   return a ? AT_file (a) : NULL;
5390 }
5391 
5392 /* Return TRUE if the language is C.  */
5393 
5394 static inline bool
is_c(void)5395 is_c (void)
5396 {
5397   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5398 
5399   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5400 	  || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5401 
5402 
5403 }
5404 
5405 /* Return TRUE if the language is C++.  */
5406 
5407 static inline bool
is_cxx(void)5408 is_cxx (void)
5409 {
5410   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5411 
5412   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5413 	  || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5414 }
5415 
5416 /* Return TRUE if DECL was created by the C++ frontend.  */
5417 
5418 static bool
is_cxx(const_tree decl)5419 is_cxx (const_tree decl)
5420 {
5421   if (in_lto_p)
5422     {
5423       const_tree context = get_ultimate_context (decl);
5424       if (context && TRANSLATION_UNIT_LANGUAGE (context))
5425 	return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5426     }
5427   return is_cxx ();
5428 }
5429 
5430 /* Return TRUE if the language is Fortran.  */
5431 
5432 static inline bool
is_fortran(void)5433 is_fortran (void)
5434 {
5435   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5436 
5437   return (lang == DW_LANG_Fortran77
5438 	  || lang == DW_LANG_Fortran90
5439 	  || lang == DW_LANG_Fortran95
5440 	  || lang == DW_LANG_Fortran03
5441 	  || lang == DW_LANG_Fortran08);
5442 }
5443 
5444 static inline bool
is_fortran(const_tree decl)5445 is_fortran (const_tree decl)
5446 {
5447   if (in_lto_p)
5448     {
5449       const_tree context = get_ultimate_context (decl);
5450       if (context && TRANSLATION_UNIT_LANGUAGE (context))
5451 	return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5452 			 "GNU Fortran", 11) == 0
5453 		|| strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5454 			   "GNU F77") == 0);
5455     }
5456   return is_fortran ();
5457 }
5458 
5459 /* Return TRUE if the language is Ada.  */
5460 
5461 static inline bool
is_ada(void)5462 is_ada (void)
5463 {
5464   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5465 
5466   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5467 }
5468 
5469 /* Return TRUE if the language is D.  */
5470 
5471 static inline bool
is_dlang(void)5472 is_dlang (void)
5473 {
5474   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5475 
5476   return lang == DW_LANG_D;
5477 }
5478 
5479 /* Remove the specified attribute if present.  Return TRUE if removal
5480    was successful.  */
5481 
5482 static bool
remove_AT(dw_die_ref die,enum dwarf_attribute attr_kind)5483 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5484 {
5485   dw_attr_node *a;
5486   unsigned ix;
5487 
5488   if (! die)
5489     return false;
5490 
5491   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5492     if (a->dw_attr == attr_kind)
5493       {
5494 	if (AT_class (a) == dw_val_class_str)
5495 	  if (a->dw_attr_val.v.val_str->refcount)
5496 	    a->dw_attr_val.v.val_str->refcount--;
5497 
5498 	/* vec::ordered_remove should help reduce the number of abbrevs
5499 	   that are needed.  */
5500 	die->die_attr->ordered_remove (ix);
5501 	return true;
5502       }
5503   return false;
5504 }
5505 
5506 /* Remove CHILD from its parent.  PREV must have the property that
5507    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5508 
5509 static void
remove_child_with_prev(dw_die_ref child,dw_die_ref prev)5510 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5511 {
5512   gcc_assert (child->die_parent == prev->die_parent);
5513   gcc_assert (prev->die_sib == child);
5514   if (prev == child)
5515     {
5516       gcc_assert (child->die_parent->die_child == child);
5517       prev = NULL;
5518     }
5519   else
5520     prev->die_sib = child->die_sib;
5521   if (child->die_parent->die_child == child)
5522     child->die_parent->die_child = prev;
5523   child->die_sib = NULL;
5524 }
5525 
5526 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
5527    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
5528 
5529 static void
replace_child(dw_die_ref old_child,dw_die_ref new_child,dw_die_ref prev)5530 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5531 {
5532   dw_die_ref parent = old_child->die_parent;
5533 
5534   gcc_assert (parent == prev->die_parent);
5535   gcc_assert (prev->die_sib == old_child);
5536 
5537   new_child->die_parent = parent;
5538   if (prev == old_child)
5539     {
5540       gcc_assert (parent->die_child == old_child);
5541       new_child->die_sib = new_child;
5542     }
5543   else
5544     {
5545       prev->die_sib = new_child;
5546       new_child->die_sib = old_child->die_sib;
5547     }
5548   if (old_child->die_parent->die_child == old_child)
5549     old_child->die_parent->die_child = new_child;
5550   old_child->die_sib = NULL;
5551 }
5552 
5553 /* Move all children from OLD_PARENT to NEW_PARENT.  */
5554 
5555 static void
move_all_children(dw_die_ref old_parent,dw_die_ref new_parent)5556 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5557 {
5558   dw_die_ref c;
5559   new_parent->die_child = old_parent->die_child;
5560   old_parent->die_child = NULL;
5561   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5562 }
5563 
5564 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5565    matches TAG.  */
5566 
5567 static void
remove_child_TAG(dw_die_ref die,enum dwarf_tag tag)5568 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5569 {
5570   dw_die_ref c;
5571 
5572   c = die->die_child;
5573   if (c) do {
5574     dw_die_ref prev = c;
5575     c = c->die_sib;
5576     while (c->die_tag == tag)
5577       {
5578 	remove_child_with_prev (c, prev);
5579 	c->die_parent = NULL;
5580 	/* Might have removed every child.  */
5581 	if (die->die_child == NULL)
5582 	  return;
5583 	c = prev->die_sib;
5584       }
5585   } while (c != die->die_child);
5586 }
5587 
5588 /* Add a CHILD_DIE as the last child of DIE.  */
5589 
5590 static void
add_child_die(dw_die_ref die,dw_die_ref child_die)5591 add_child_die (dw_die_ref die, dw_die_ref child_die)
5592 {
5593   /* FIXME this should probably be an assert.  */
5594   if (! die || ! child_die)
5595     return;
5596   gcc_assert (die != child_die);
5597 
5598   child_die->die_parent = die;
5599   if (die->die_child)
5600     {
5601       child_die->die_sib = die->die_child->die_sib;
5602       die->die_child->die_sib = child_die;
5603     }
5604   else
5605     child_die->die_sib = child_die;
5606   die->die_child = child_die;
5607 }
5608 
5609 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE.  */
5610 
5611 static void
add_child_die_after(dw_die_ref die,dw_die_ref child_die,dw_die_ref after_die)5612 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5613 		     dw_die_ref after_die)
5614 {
5615   gcc_assert (die
5616 	      && child_die
5617 	      && after_die
5618 	      && die->die_child
5619 	      && die != child_die);
5620 
5621   child_die->die_parent = die;
5622   child_die->die_sib = after_die->die_sib;
5623   after_die->die_sib = child_die;
5624   if (die->die_child == after_die)
5625     die->die_child = child_die;
5626 }
5627 
5628 /* Unassociate CHILD from its parent, and make its parent be
5629    NEW_PARENT.  */
5630 
5631 static void
reparent_child(dw_die_ref child,dw_die_ref new_parent)5632 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5633 {
5634   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5635     if (p->die_sib == child)
5636       {
5637 	remove_child_with_prev (child, p);
5638 	break;
5639       }
5640   add_child_die (new_parent, child);
5641 }
5642 
5643 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5644    is the specification, to the end of PARENT's list of children.
5645    This is done by removing and re-adding it.  */
5646 
5647 static void
splice_child_die(dw_die_ref parent,dw_die_ref child)5648 splice_child_die (dw_die_ref parent, dw_die_ref child)
5649 {
5650   /* We want the declaration DIE from inside the class, not the
5651      specification DIE at toplevel.  */
5652   if (child->die_parent != parent)
5653     {
5654       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5655 
5656       if (tmp)
5657 	child = tmp;
5658     }
5659 
5660   gcc_assert (child->die_parent == parent
5661 	      || (child->die_parent
5662 		  == get_AT_ref (parent, DW_AT_specification)));
5663 
5664   reparent_child (child, parent);
5665 }
5666 
5667 /* Create and return a new die with TAG_VALUE as tag.  */
5668 
5669 static inline dw_die_ref
new_die_raw(enum dwarf_tag tag_value)5670 new_die_raw (enum dwarf_tag tag_value)
5671 {
5672   dw_die_ref die = ggc_cleared_alloc<die_node> ();
5673   die->die_tag = tag_value;
5674   return die;
5675 }
5676 
5677 /* Create and return a new die with a parent of PARENT_DIE.  If
5678    PARENT_DIE is NULL, the new DIE is placed in limbo and an
5679    associated tree T must be supplied to determine parenthood
5680    later.  */
5681 
5682 static inline dw_die_ref
new_die(enum dwarf_tag tag_value,dw_die_ref parent_die,tree t)5683 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5684 {
5685   dw_die_ref die = new_die_raw (tag_value);
5686 
5687   if (parent_die != NULL)
5688     add_child_die (parent_die, die);
5689   else
5690     {
5691       limbo_die_node *limbo_node;
5692 
5693       /* No DIEs created after early dwarf should end up in limbo,
5694 	 because the limbo list should not persist past LTO
5695 	 streaming.  */
5696       if (tag_value != DW_TAG_compile_unit
5697 	  /* These are allowed because they're generated while
5698 	     breaking out COMDAT units late.  */
5699 	  && tag_value != DW_TAG_type_unit
5700 	  && tag_value != DW_TAG_skeleton_unit
5701 	  && !early_dwarf
5702 	  /* Allow nested functions to live in limbo because they will
5703 	     only temporarily live there, as decls_for_scope will fix
5704 	     them up.  */
5705 	  && (TREE_CODE (t) != FUNCTION_DECL
5706 	      || !decl_function_context (t))
5707 	  /* Same as nested functions above but for types.  Types that
5708 	     are local to a function will be fixed in
5709 	     decls_for_scope.  */
5710 	  && (!RECORD_OR_UNION_TYPE_P (t)
5711 	      || !TYPE_CONTEXT (t)
5712 	      || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5713 	  /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5714 	     especially in the ltrans stage, but once we implement LTO
5715 	     dwarf streaming, we should remove this exception.  */
5716 	  && !in_lto_p)
5717 	{
5718 	  fprintf (stderr, "symbol ended up in limbo too late:");
5719 	  debug_generic_stmt (t);
5720 	  gcc_unreachable ();
5721 	}
5722 
5723       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5724       limbo_node->die = die;
5725       limbo_node->created_for = t;
5726       limbo_node->next = limbo_die_list;
5727       limbo_die_list = limbo_node;
5728     }
5729 
5730   return die;
5731 }
5732 
5733 /* Return the DIE associated with the given type specifier.  */
5734 
5735 static inline dw_die_ref
lookup_type_die(tree type)5736 lookup_type_die (tree type)
5737 {
5738   dw_die_ref die = TYPE_SYMTAB_DIE (type);
5739   if (die && die->removed)
5740     {
5741       TYPE_SYMTAB_DIE (type) = NULL;
5742       return NULL;
5743     }
5744   return die;
5745 }
5746 
5747 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5748    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5749    anonymous type instead the one of the naming typedef.  */
5750 
5751 static inline dw_die_ref
strip_naming_typedef(tree type,dw_die_ref type_die)5752 strip_naming_typedef (tree type, dw_die_ref type_die)
5753 {
5754   if (type
5755       && TREE_CODE (type) == RECORD_TYPE
5756       && type_die
5757       && type_die->die_tag == DW_TAG_typedef
5758       && is_naming_typedef_decl (TYPE_NAME (type)))
5759     type_die = get_AT_ref (type_die, DW_AT_type);
5760   return type_die;
5761 }
5762 
5763 /* Like lookup_type_die, but if type is an anonymous type named by a
5764    typedef[1], return the DIE of the anonymous type instead the one of
5765    the naming typedef.  This is because in gen_typedef_die, we did
5766    equate the anonymous struct named by the typedef with the DIE of
5767    the naming typedef. So by default, lookup_type_die on an anonymous
5768    struct yields the DIE of the naming typedef.
5769 
5770    [1]: Read the comment of is_naming_typedef_decl to learn about what
5771    a naming typedef is.  */
5772 
5773 static inline dw_die_ref
lookup_type_die_strip_naming_typedef(tree type)5774 lookup_type_die_strip_naming_typedef (tree type)
5775 {
5776   dw_die_ref die = lookup_type_die (type);
5777   return strip_naming_typedef (type, die);
5778 }
5779 
5780 /* Equate a DIE to a given type specifier.  */
5781 
5782 static inline void
equate_type_number_to_die(tree type,dw_die_ref type_die)5783 equate_type_number_to_die (tree type, dw_die_ref type_die)
5784 {
5785   TYPE_SYMTAB_DIE (type) = type_die;
5786 }
5787 
5788 static dw_die_ref maybe_create_die_with_external_ref (tree);
5789 struct GTY(()) sym_off_pair
5790 {
5791   const char * GTY((skip)) sym;
5792   unsigned HOST_WIDE_INT off;
5793 };
5794 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5795 
5796 /* Returns a hash value for X (which really is a die_struct).  */
5797 
5798 inline hashval_t
hash(die_node * x)5799 decl_die_hasher::hash (die_node *x)
5800 {
5801   return (hashval_t) x->decl_id;
5802 }
5803 
5804 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5805 
5806 inline bool
equal(die_node * x,tree y)5807 decl_die_hasher::equal (die_node *x, tree y)
5808 {
5809   return (x->decl_id == DECL_UID (y));
5810 }
5811 
5812 /* Return the DIE associated with a given declaration.  */
5813 
5814 static inline dw_die_ref
lookup_decl_die(tree decl)5815 lookup_decl_die (tree decl)
5816 {
5817   dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5818 							 NO_INSERT);
5819   if (!die)
5820     {
5821       if (in_lto_p)
5822 	return maybe_create_die_with_external_ref (decl);
5823       return NULL;
5824     }
5825   if ((*die)->removed)
5826     {
5827       decl_die_table->clear_slot (die);
5828       return NULL;
5829     }
5830   return *die;
5831 }
5832 
5833 
5834 /* Return the DIE associated with BLOCK.  */
5835 
5836 static inline dw_die_ref
lookup_block_die(tree block)5837 lookup_block_die (tree block)
5838 {
5839   dw_die_ref die = BLOCK_DIE (block);
5840   if (!die && in_lto_p)
5841     return maybe_create_die_with_external_ref (block);
5842   return die;
5843 }
5844 
5845 /* Associate DIE with BLOCK.  */
5846 
5847 static inline void
equate_block_to_die(tree block,dw_die_ref die)5848 equate_block_to_die (tree block, dw_die_ref die)
5849 {
5850   BLOCK_DIE (block) = die;
5851 }
5852 #undef BLOCK_DIE
5853 
5854 
5855 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5856    style reference.  Return true if we found one refering to a DIE for
5857    DECL, otherwise return false.  */
5858 
5859 static bool
dwarf2out_die_ref_for_decl(tree decl,const char ** sym,unsigned HOST_WIDE_INT * off)5860 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5861 			    unsigned HOST_WIDE_INT *off)
5862 {
5863   dw_die_ref die;
5864 
5865   if (in_lto_p)
5866     {
5867       /* During WPA stage and incremental linking we use a hash-map
5868 	 to store the decl <-> label + offset map.  */
5869       if (!external_die_map)
5870 	return false;
5871       sym_off_pair *desc = external_die_map->get (decl);
5872       if (!desc)
5873 	return false;
5874       *sym = desc->sym;
5875       *off = desc->off;
5876       return true;
5877     }
5878 
5879   if (TREE_CODE (decl) == BLOCK)
5880     die = lookup_block_die (decl);
5881   else
5882     die = lookup_decl_die (decl);
5883   if (!die)
5884     return false;
5885 
5886   /* Similar to get_ref_die_offset_label, but using the "correct"
5887      label.  */
5888   *off = die->die_offset;
5889   while (die->die_parent)
5890     die = die->die_parent;
5891   /* For the containing CU DIE we compute a die_symbol in
5892      compute_comp_unit_symbol.  */
5893   gcc_assert (die->die_tag == DW_TAG_compile_unit
5894 	      && die->die_id.die_symbol != NULL);
5895   *sym = die->die_id.die_symbol;
5896   return true;
5897 }
5898 
5899 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE.  */
5900 
5901 static void
add_AT_external_die_ref(dw_die_ref die,enum dwarf_attribute attr_kind,const char * symbol,HOST_WIDE_INT offset)5902 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5903 			 const char *symbol, HOST_WIDE_INT offset)
5904 {
5905   /* Create a fake DIE that contains the reference.  Don't use
5906      new_die because we don't want to end up in the limbo list.  */
5907   /* ???  We probably want to share these, thus put a ref to the DIE
5908      we create here to the external_die_map entry.  */
5909   dw_die_ref ref = new_die_raw (die->die_tag);
5910   ref->die_id.die_symbol = symbol;
5911   ref->die_offset = offset;
5912   ref->with_offset = 1;
5913   add_AT_die_ref (die, attr_kind, ref);
5914 }
5915 
5916 /* Create a DIE for DECL if required and add a reference to a DIE
5917    at SYMBOL + OFFSET which contains attributes dumped early.  */
5918 
5919 static void
dwarf2out_register_external_die(tree decl,const char * sym,unsigned HOST_WIDE_INT off)5920 dwarf2out_register_external_die (tree decl, const char *sym,
5921 				 unsigned HOST_WIDE_INT off)
5922 {
5923   if (debug_info_level == DINFO_LEVEL_NONE)
5924     return;
5925 
5926   if (!external_die_map)
5927     external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
5928   gcc_checking_assert (!external_die_map->get (decl));
5929   sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
5930   external_die_map->put (decl, p);
5931 }
5932 
5933 /* If we have a registered external DIE for DECL return a new DIE for
5934    the concrete instance with an appropriate abstract origin.  */
5935 
5936 static dw_die_ref
maybe_create_die_with_external_ref(tree decl)5937 maybe_create_die_with_external_ref (tree decl)
5938 {
5939   if (!external_die_map)
5940     return NULL;
5941   sym_off_pair *desc = external_die_map->get (decl);
5942   if (!desc)
5943     return NULL;
5944 
5945   const char *sym = desc->sym;
5946   unsigned HOST_WIDE_INT off = desc->off;
5947 
5948   in_lto_p = false;
5949   dw_die_ref die = (TREE_CODE (decl) == BLOCK
5950 		    ? lookup_block_die (decl) : lookup_decl_die (decl));
5951   gcc_assert (!die);
5952   in_lto_p = true;
5953 
5954   tree ctx;
5955   dw_die_ref parent = NULL;
5956   /* Need to lookup a DIE for the decls context - the containing
5957      function or translation unit.  */
5958   if (TREE_CODE (decl) == BLOCK)
5959     {
5960       ctx = BLOCK_SUPERCONTEXT (decl);
5961       /* ???  We do not output DIEs for all scopes thus skip as
5962 	 many DIEs as needed.  */
5963       while (TREE_CODE (ctx) == BLOCK
5964 	     && !lookup_block_die (ctx))
5965 	ctx = BLOCK_SUPERCONTEXT (ctx);
5966     }
5967   else
5968     ctx = DECL_CONTEXT (decl);
5969   /* Peel types in the context stack.  */
5970   while (ctx && TYPE_P (ctx))
5971     ctx = TYPE_CONTEXT (ctx);
5972   /* Likewise namespaces in case we do not want to emit DIEs for them.  */
5973   if (debug_info_level <= DINFO_LEVEL_TERSE)
5974     while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5975       ctx = DECL_CONTEXT (ctx);
5976   if (ctx)
5977     {
5978       if (TREE_CODE (ctx) == BLOCK)
5979 	parent = lookup_block_die (ctx);
5980       else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5981 	       /* Keep the 1:1 association during WPA.  */
5982 	       && !flag_wpa
5983 	       && flag_incremental_link != INCREMENTAL_LINK_LTO)
5984 	/* Otherwise all late annotations go to the main CU which
5985 	   imports the original CUs.  */
5986 	parent = comp_unit_die ();
5987       else if (TREE_CODE (ctx) == FUNCTION_DECL
5988 	       && TREE_CODE (decl) != FUNCTION_DECL
5989 	       && TREE_CODE (decl) != PARM_DECL
5990 	       && TREE_CODE (decl) != RESULT_DECL
5991 	       && TREE_CODE (decl) != BLOCK)
5992 	/* Leave function local entities parent determination to when
5993 	   we process scope vars.  */
5994 	;
5995       else
5996 	parent = lookup_decl_die (ctx);
5997     }
5998   else
5999     /* In some cases the FEs fail to set DECL_CONTEXT properly.
6000        Handle this case gracefully by globalizing stuff.  */
6001     parent = comp_unit_die ();
6002   /* Create a DIE "stub".  */
6003   switch (TREE_CODE (decl))
6004     {
6005     case TRANSLATION_UNIT_DECL:
6006       {
6007 	die = comp_unit_die ();
6008 	/* We re-target all CU decls to the LTRANS CU DIE, so no need
6009 	   to create a DIE for the original CUs.  */
6010 	return die;
6011       }
6012     case NAMESPACE_DECL:
6013       if (is_fortran (decl))
6014 	die = new_die (DW_TAG_module, parent, decl);
6015       else
6016 	die = new_die (DW_TAG_namespace, parent, decl);
6017       break;
6018     case FUNCTION_DECL:
6019       die = new_die (DW_TAG_subprogram, parent, decl);
6020       break;
6021     case VAR_DECL:
6022       die = new_die (DW_TAG_variable, parent, decl);
6023       break;
6024     case RESULT_DECL:
6025       die = new_die (DW_TAG_variable, parent, decl);
6026       break;
6027     case PARM_DECL:
6028       die = new_die (DW_TAG_formal_parameter, parent, decl);
6029       break;
6030     case CONST_DECL:
6031       die = new_die (DW_TAG_constant, parent, decl);
6032       break;
6033     case LABEL_DECL:
6034       die = new_die (DW_TAG_label, parent, decl);
6035       break;
6036     case BLOCK:
6037       die = new_die (DW_TAG_lexical_block, parent, decl);
6038       break;
6039     default:
6040       gcc_unreachable ();
6041     }
6042   if (TREE_CODE (decl) == BLOCK)
6043     equate_block_to_die (decl, die);
6044   else
6045     equate_decl_number_to_die (decl, die);
6046 
6047   add_desc_attribute (die, decl);
6048 
6049   /* Add a reference to the DIE providing early debug at $sym + off.  */
6050   add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6051 
6052   return die;
6053 }
6054 
6055 /* Returns a hash value for X (which really is a var_loc_list).  */
6056 
6057 inline hashval_t
hash(var_loc_list * x)6058 decl_loc_hasher::hash (var_loc_list *x)
6059 {
6060   return (hashval_t) x->decl_id;
6061 }
6062 
6063 /* Return nonzero if decl_id of var_loc_list X is the same as
6064    UID of decl *Y.  */
6065 
6066 inline bool
equal(var_loc_list * x,const_tree y)6067 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6068 {
6069   return (x->decl_id == DECL_UID (y));
6070 }
6071 
6072 /* Return the var_loc list associated with a given declaration.  */
6073 
6074 static inline var_loc_list *
lookup_decl_loc(const_tree decl)6075 lookup_decl_loc (const_tree decl)
6076 {
6077   if (!decl_loc_table)
6078     return NULL;
6079   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6080 }
6081 
6082 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
6083 
6084 inline hashval_t
hash(cached_dw_loc_list * x)6085 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6086 {
6087   return (hashval_t) x->decl_id;
6088 }
6089 
6090 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6091    UID of decl *Y.  */
6092 
6093 inline bool
equal(cached_dw_loc_list * x,const_tree y)6094 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6095 {
6096   return (x->decl_id == DECL_UID (y));
6097 }
6098 
6099 /* Equate a DIE to a particular declaration.  */
6100 
6101 static void
equate_decl_number_to_die(tree decl,dw_die_ref decl_die)6102 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6103 {
6104   unsigned int decl_id = DECL_UID (decl);
6105 
6106   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6107   decl_die->decl_id = decl_id;
6108 }
6109 
6110 /* Return how many bits covers PIECE EXPR_LIST.  */
6111 
6112 static HOST_WIDE_INT
decl_piece_bitsize(rtx piece)6113 decl_piece_bitsize (rtx piece)
6114 {
6115   int ret = (int) GET_MODE (piece);
6116   if (ret)
6117     return ret;
6118   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6119 	      && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6120   return INTVAL (XEXP (XEXP (piece, 0), 0));
6121 }
6122 
6123 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
6124 
6125 static rtx *
decl_piece_varloc_ptr(rtx piece)6126 decl_piece_varloc_ptr (rtx piece)
6127 {
6128   if ((int) GET_MODE (piece))
6129     return &XEXP (piece, 0);
6130   else
6131     return &XEXP (XEXP (piece, 0), 1);
6132 }
6133 
6134 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6135    Next is the chain of following piece nodes.  */
6136 
6137 static rtx_expr_list *
decl_piece_node(rtx loc_note,HOST_WIDE_INT bitsize,rtx next)6138 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6139 {
6140   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6141     return alloc_EXPR_LIST (bitsize, loc_note, next);
6142   else
6143     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6144 					       GEN_INT (bitsize),
6145 					       loc_note), next);
6146 }
6147 
6148 /* Return rtx that should be stored into loc field for
6149    LOC_NOTE and BITPOS/BITSIZE.  */
6150 
6151 static rtx
construct_piece_list(rtx loc_note,HOST_WIDE_INT bitpos,HOST_WIDE_INT bitsize)6152 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6153 		      HOST_WIDE_INT bitsize)
6154 {
6155   if (bitsize != -1)
6156     {
6157       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6158       if (bitpos != 0)
6159 	loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6160     }
6161   return loc_note;
6162 }
6163 
6164 /* This function either modifies location piece list *DEST in
6165    place (if SRC and INNER is NULL), or copies location piece list
6166    *SRC to *DEST while modifying it.  Location BITPOS is modified
6167    to contain LOC_NOTE, any pieces overlapping it are removed resp.
6168    not copied and if needed some padding around it is added.
6169    When modifying in place, DEST should point to EXPR_LIST where
6170    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6171    to the start of the whole list and INNER points to the EXPR_LIST
6172    where earlier pieces cover PIECE_BITPOS bits.  */
6173 
6174 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)6175 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6176 		   HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6177 		   HOST_WIDE_INT bitsize, rtx loc_note)
6178 {
6179   HOST_WIDE_INT diff;
6180   bool copy = inner != NULL;
6181 
6182   if (copy)
6183     {
6184       /* First copy all nodes preceding the current bitpos.  */
6185       while (src != inner)
6186 	{
6187 	  *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6188 				   decl_piece_bitsize (*src), NULL_RTX);
6189 	  dest = &XEXP (*dest, 1);
6190 	  src = &XEXP (*src, 1);
6191 	}
6192     }
6193   /* Add padding if needed.  */
6194   if (bitpos != piece_bitpos)
6195     {
6196       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6197 			       copy ? NULL_RTX : *dest);
6198       dest = &XEXP (*dest, 1);
6199     }
6200   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6201     {
6202       gcc_assert (!copy);
6203       /* A piece with correct bitpos and bitsize already exist,
6204 	 just update the location for it and return.  */
6205       *decl_piece_varloc_ptr (*dest) = loc_note;
6206       return;
6207     }
6208   /* Add the piece that changed.  */
6209   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6210   dest = &XEXP (*dest, 1);
6211   /* Skip over pieces that overlap it.  */
6212   diff = bitpos - piece_bitpos + bitsize;
6213   if (!copy)
6214     src = dest;
6215   while (diff > 0 && *src)
6216     {
6217       rtx piece = *src;
6218       diff -= decl_piece_bitsize (piece);
6219       if (copy)
6220 	src = &XEXP (piece, 1);
6221       else
6222 	{
6223 	  *src = XEXP (piece, 1);
6224 	  free_EXPR_LIST_node (piece);
6225 	}
6226     }
6227   /* Add padding if needed.  */
6228   if (diff < 0 && *src)
6229     {
6230       if (!copy)
6231 	dest = src;
6232       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6233       dest = &XEXP (*dest, 1);
6234     }
6235   if (!copy)
6236     return;
6237   /* Finally copy all nodes following it.  */
6238   while (*src)
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 
6247 /* Add a variable location node to the linked list for DECL.  */
6248 
6249 static struct var_loc_node *
add_var_loc_to_decl(tree decl,rtx loc_note,const char * label,var_loc_view view)6250 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6251 {
6252   unsigned int decl_id;
6253   var_loc_list *temp;
6254   struct var_loc_node *loc = NULL;
6255   HOST_WIDE_INT bitsize = -1, bitpos = -1;
6256 
6257   if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6258     {
6259       tree realdecl = DECL_DEBUG_EXPR (decl);
6260       if (handled_component_p (realdecl)
6261 	  || (TREE_CODE (realdecl) == MEM_REF
6262 	      && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6263 	{
6264 	  bool reverse;
6265 	  tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6266 							&bitsize, &reverse);
6267 	  if (!innerdecl
6268 	      || !DECL_P (innerdecl)
6269 	      || DECL_IGNORED_P (innerdecl)
6270 	      || TREE_STATIC (innerdecl)
6271 	      || bitsize == 0
6272 	      || bitpos + bitsize > 256)
6273 	    return NULL;
6274 	  decl = innerdecl;
6275 	}
6276     }
6277 
6278   decl_id = DECL_UID (decl);
6279   var_loc_list **slot
6280     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6281   if (*slot == NULL)
6282     {
6283       temp = ggc_cleared_alloc<var_loc_list> ();
6284       temp->decl_id = decl_id;
6285       *slot = temp;
6286     }
6287   else
6288     temp = *slot;
6289 
6290   /* For PARM_DECLs try to keep around the original incoming value,
6291      even if that means we'll emit a zero-range .debug_loc entry.  */
6292   if (temp->last
6293       && temp->first == temp->last
6294       && TREE_CODE (decl) == PARM_DECL
6295       && NOTE_P (temp->first->loc)
6296       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6297       && DECL_INCOMING_RTL (decl)
6298       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6299       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6300 	 == GET_CODE (DECL_INCOMING_RTL (decl))
6301       && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6302       && (bitsize != -1
6303 	  || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6304 			   NOTE_VAR_LOCATION_LOC (loc_note))
6305 	  || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6306 	      != NOTE_VAR_LOCATION_STATUS (loc_note))))
6307     {
6308       loc = ggc_cleared_alloc<var_loc_node> ();
6309       temp->first->next = loc;
6310       temp->last = loc;
6311       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6312     }
6313   else if (temp->last)
6314     {
6315       struct var_loc_node *last = temp->last, *unused = NULL;
6316       rtx *piece_loc = NULL, last_loc_note;
6317       HOST_WIDE_INT piece_bitpos = 0;
6318       if (last->next)
6319 	{
6320 	  last = last->next;
6321 	  gcc_assert (last->next == NULL);
6322 	}
6323       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6324 	{
6325 	  piece_loc = &last->loc;
6326 	  do
6327 	    {
6328 	      HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6329 	      if (piece_bitpos + cur_bitsize > bitpos)
6330 		break;
6331 	      piece_bitpos += cur_bitsize;
6332 	      piece_loc = &XEXP (*piece_loc, 1);
6333 	    }
6334 	  while (*piece_loc);
6335 	}
6336       /* TEMP->LAST here is either pointer to the last but one or
6337 	 last element in the chained list, LAST is pointer to the
6338 	 last element.  */
6339       if (label && strcmp (last->label, label) == 0 && last->view == view)
6340 	{
6341 	  /* For SRA optimized variables if there weren't any real
6342 	     insns since last note, just modify the last node.  */
6343 	  if (piece_loc != NULL)
6344 	    {
6345 	      adjust_piece_list (piece_loc, NULL, NULL,
6346 				 bitpos, piece_bitpos, bitsize, loc_note);
6347 	      return NULL;
6348 	    }
6349 	  /* If the last note doesn't cover any instructions, remove it.  */
6350 	  if (temp->last != last)
6351 	    {
6352 	      temp->last->next = NULL;
6353 	      unused = last;
6354 	      last = temp->last;
6355 	      gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6356 	    }
6357 	  else
6358 	    {
6359 	      gcc_assert (temp->first == temp->last
6360 			  || (temp->first->next == temp->last
6361 			      && TREE_CODE (decl) == PARM_DECL));
6362 	      memset (temp->last, '\0', sizeof (*temp->last));
6363 	      temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6364 	      return temp->last;
6365 	    }
6366 	}
6367       if (bitsize == -1 && NOTE_P (last->loc))
6368 	last_loc_note = last->loc;
6369       else if (piece_loc != NULL
6370 	       && *piece_loc != NULL_RTX
6371 	       && piece_bitpos == bitpos
6372 	       && decl_piece_bitsize (*piece_loc) == bitsize)
6373 	last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6374       else
6375 	last_loc_note = NULL_RTX;
6376       /* If the current location is the same as the end of the list,
6377 	 and either both or neither of the locations is uninitialized,
6378 	 we have nothing to do.  */
6379       if (last_loc_note == NULL_RTX
6380 	  || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6381 			    NOTE_VAR_LOCATION_LOC (loc_note)))
6382 	  || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6383 	       != NOTE_VAR_LOCATION_STATUS (loc_note))
6384 	      && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6385 		   == VAR_INIT_STATUS_UNINITIALIZED)
6386 		  || (NOTE_VAR_LOCATION_STATUS (loc_note)
6387 		      == VAR_INIT_STATUS_UNINITIALIZED))))
6388 	{
6389 	  /* Add LOC to the end of list and update LAST.  If the last
6390 	     element of the list has been removed above, reuse its
6391 	     memory for the new node, otherwise allocate a new one.  */
6392 	  if (unused)
6393 	    {
6394 	      loc = unused;
6395 	      memset (loc, '\0', sizeof (*loc));
6396 	    }
6397 	  else
6398 	    loc = ggc_cleared_alloc<var_loc_node> ();
6399 	  if (bitsize == -1 || piece_loc == NULL)
6400 	    loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6401 	  else
6402 	    adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6403 			       bitpos, piece_bitpos, bitsize, loc_note);
6404 	  last->next = loc;
6405 	  /* Ensure TEMP->LAST will point either to the new last but one
6406 	     element of the chain, or to the last element in it.  */
6407 	  if (last != temp->last)
6408 	    temp->last = last;
6409 	}
6410       else if (unused)
6411 	ggc_free (unused);
6412     }
6413   else
6414     {
6415       loc = ggc_cleared_alloc<var_loc_node> ();
6416       temp->first = loc;
6417       temp->last = loc;
6418       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6419     }
6420   return loc;
6421 }
6422 
6423 /* Keep track of the number of spaces used to indent the
6424    output of the debugging routines that print the structure of
6425    the DIE internal representation.  */
6426 static int print_indent;
6427 
6428 /* Indent the line the number of spaces given by print_indent.  */
6429 
6430 static inline void
print_spaces(FILE * outfile)6431 print_spaces (FILE *outfile)
6432 {
6433   fprintf (outfile, "%*s", print_indent, "");
6434 }
6435 
6436 /* Print a type signature in hex.  */
6437 
6438 static inline void
print_signature(FILE * outfile,char * sig)6439 print_signature (FILE *outfile, char *sig)
6440 {
6441   int i;
6442 
6443   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6444     fprintf (outfile, "%02x", sig[i] & 0xff);
6445 }
6446 
6447 static inline void
print_discr_value(FILE * outfile,dw_discr_value * discr_value)6448 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6449 {
6450   if (discr_value->pos)
6451     fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6452   else
6453     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6454 }
6455 
6456 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6457 
6458 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
6459    RECURSE, output location descriptor operations.  */
6460 
6461 static void
print_dw_val(dw_val_node * val,bool recurse,FILE * outfile)6462 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6463 {
6464   switch (val->val_class)
6465     {
6466     case dw_val_class_addr:
6467       fprintf (outfile, "address");
6468       break;
6469     case dw_val_class_offset:
6470       fprintf (outfile, "offset");
6471       break;
6472     case dw_val_class_loc:
6473       fprintf (outfile, "location descriptor");
6474       if (val->v.val_loc == NULL)
6475 	fprintf (outfile, " -> <null>\n");
6476       else if (recurse)
6477 	{
6478 	  fprintf (outfile, ":\n");
6479 	  print_indent += 4;
6480 	  print_loc_descr (val->v.val_loc, outfile);
6481 	  print_indent -= 4;
6482 	}
6483       else
6484 	{
6485 	  if (flag_dump_noaddr || flag_dump_unnumbered)
6486 	    fprintf (outfile, " #\n");
6487 	  else
6488 	    fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6489 	}
6490       break;
6491     case dw_val_class_loc_list:
6492       fprintf (outfile, "location list -> label:%s",
6493 	       val->v.val_loc_list->ll_symbol);
6494       break;
6495     case dw_val_class_view_list:
6496       val = view_list_to_loc_list_val_node (val);
6497       fprintf (outfile, "location list with views -> labels:%s and %s",
6498 	       val->v.val_loc_list->ll_symbol,
6499 	       val->v.val_loc_list->vl_symbol);
6500       break;
6501     case dw_val_class_range_list:
6502       fprintf (outfile, "range list");
6503       break;
6504     case dw_val_class_const:
6505     case dw_val_class_const_implicit:
6506       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6507       break;
6508     case dw_val_class_unsigned_const:
6509     case dw_val_class_unsigned_const_implicit:
6510       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6511       break;
6512     case dw_val_class_const_double:
6513       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6514 			HOST_WIDE_INT_PRINT_UNSIGNED")",
6515 	       val->v.val_double.high,
6516 	       val->v.val_double.low);
6517       break;
6518     case dw_val_class_wide_int:
6519       {
6520 	int i = val->v.val_wide->get_len ();
6521 	fprintf (outfile, "constant (");
6522 	gcc_assert (i > 0);
6523 	if (val->v.val_wide->elt (i - 1) == 0)
6524 	  fprintf (outfile, "0x");
6525 	fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6526 		 val->v.val_wide->elt (--i));
6527 	while (--i >= 0)
6528 	  fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6529 		   val->v.val_wide->elt (i));
6530 	fprintf (outfile, ")");
6531 	break;
6532       }
6533     case dw_val_class_vec:
6534       fprintf (outfile, "floating-point or vector constant");
6535       break;
6536     case dw_val_class_flag:
6537       fprintf (outfile, "%u", val->v.val_flag);
6538       break;
6539     case dw_val_class_die_ref:
6540       if (val->v.val_die_ref.die != NULL)
6541 	{
6542 	  dw_die_ref die = val->v.val_die_ref.die;
6543 
6544 	  if (die->comdat_type_p)
6545 	    {
6546 	      fprintf (outfile, "die -> signature: ");
6547 	      print_signature (outfile,
6548 			       die->die_id.die_type_node->signature);
6549 	    }
6550 	  else if (die->die_id.die_symbol)
6551 	    {
6552 	      fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6553 	      if (die->with_offset)
6554 		fprintf (outfile, " + %ld", die->die_offset);
6555 	    }
6556 	  else
6557 	    fprintf (outfile, "die -> %ld", die->die_offset);
6558 	  if (flag_dump_noaddr || flag_dump_unnumbered)
6559 	    fprintf (outfile, " #");
6560 	  else
6561 	    fprintf (outfile, " (%p)", (void *) die);
6562 	}
6563       else
6564 	fprintf (outfile, "die -> <null>");
6565       break;
6566     case dw_val_class_vms_delta:
6567       fprintf (outfile, "delta: @slotcount(%s-%s)",
6568 	       val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6569       break;
6570     case dw_val_class_symview:
6571       fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6572       break;
6573     case dw_val_class_lbl_id:
6574     case dw_val_class_lineptr:
6575     case dw_val_class_macptr:
6576     case dw_val_class_loclistsptr:
6577     case dw_val_class_high_pc:
6578       fprintf (outfile, "label: %s", val->v.val_lbl_id);
6579       break;
6580     case dw_val_class_str:
6581       if (val->v.val_str->str != NULL)
6582 	fprintf (outfile, "\"%s\"", val->v.val_str->str);
6583       else
6584 	fprintf (outfile, "<null>");
6585       break;
6586     case dw_val_class_file:
6587     case dw_val_class_file_implicit:
6588       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6589 	       val->v.val_file->emitted_number);
6590       break;
6591     case dw_val_class_data8:
6592       {
6593 	int i;
6594 
6595 	for (i = 0; i < 8; i++)
6596 	  fprintf (outfile, "%02x", val->v.val_data8[i]);
6597 	break;
6598       }
6599     case dw_val_class_discr_value:
6600       print_discr_value (outfile, &val->v.val_discr_value);
6601       break;
6602     case dw_val_class_discr_list:
6603       for (dw_discr_list_ref node = val->v.val_discr_list;
6604 	   node != NULL;
6605 	   node = node->dw_discr_next)
6606 	{
6607 	  if (node->dw_discr_range)
6608 	    {
6609 	      fprintf (outfile, " .. ");
6610 	      print_discr_value (outfile, &node->dw_discr_lower_bound);
6611 	      print_discr_value (outfile, &node->dw_discr_upper_bound);
6612 	    }
6613 	  else
6614 	    print_discr_value (outfile, &node->dw_discr_lower_bound);
6615 
6616 	  if (node->dw_discr_next != NULL)
6617 	    fprintf (outfile, " | ");
6618 	}
6619     default:
6620       break;
6621     }
6622 }
6623 
6624 /* Likewise, for a DIE attribute.  */
6625 
6626 static void
print_attribute(dw_attr_node * a,bool recurse,FILE * outfile)6627 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6628 {
6629   print_dw_val (&a->dw_attr_val, recurse, outfile);
6630 }
6631 
6632 
6633 /* Print the list of operands in the LOC location description to OUTFILE.  This
6634    routine is a debugging aid only.  */
6635 
6636 static void
print_loc_descr(dw_loc_descr_ref loc,FILE * outfile)6637 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6638 {
6639   dw_loc_descr_ref l = loc;
6640 
6641   if (loc == NULL)
6642     {
6643       print_spaces (outfile);
6644       fprintf (outfile, "<null>\n");
6645       return;
6646     }
6647 
6648   for (l = loc; l != NULL; l = l->dw_loc_next)
6649     {
6650       print_spaces (outfile);
6651       if (flag_dump_noaddr || flag_dump_unnumbered)
6652 	fprintf (outfile, "#");
6653       else
6654 	fprintf (outfile, "(%p)", (void *) l);
6655       fprintf (outfile, " %s",
6656 	       dwarf_stack_op_name (l->dw_loc_opc));
6657       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6658 	{
6659 	  fprintf (outfile, " ");
6660 	  print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6661 	}
6662       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6663 	{
6664 	  fprintf (outfile, ", ");
6665 	  print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6666 	}
6667       fprintf (outfile, "\n");
6668     }
6669 }
6670 
6671 /* Print the information associated with a given DIE, and its children.
6672    This routine is a debugging aid only.  */
6673 
6674 static void
print_die(dw_die_ref die,FILE * outfile)6675 print_die (dw_die_ref die, FILE *outfile)
6676 {
6677   dw_attr_node *a;
6678   dw_die_ref c;
6679   unsigned ix;
6680 
6681   print_spaces (outfile);
6682   fprintf (outfile, "DIE %4ld: %s ",
6683 	   die->die_offset, dwarf_tag_name (die->die_tag));
6684   if (flag_dump_noaddr || flag_dump_unnumbered)
6685     fprintf (outfile, "#\n");
6686   else
6687     fprintf (outfile, "(%p)\n", (void*) die);
6688   print_spaces (outfile);
6689   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6690   fprintf (outfile, " offset: %ld", die->die_offset);
6691   fprintf (outfile, " mark: %d\n", die->die_mark);
6692 
6693   if (die->comdat_type_p)
6694     {
6695       print_spaces (outfile);
6696       fprintf (outfile, "  signature: ");
6697       print_signature (outfile, die->die_id.die_type_node->signature);
6698       fprintf (outfile, "\n");
6699     }
6700 
6701   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6702     {
6703       print_spaces (outfile);
6704       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6705 
6706       print_attribute (a, true, outfile);
6707       fprintf (outfile, "\n");
6708     }
6709 
6710   if (die->die_child != NULL)
6711     {
6712       print_indent += 4;
6713       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6714       print_indent -= 4;
6715     }
6716   if (print_indent == 0)
6717     fprintf (outfile, "\n");
6718 }
6719 
6720 /* Print the list of operations in the LOC location description.  */
6721 
6722 DEBUG_FUNCTION void
debug_dwarf_loc_descr(dw_loc_descr_ref loc)6723 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6724 {
6725   print_loc_descr (loc, stderr);
6726 }
6727 
6728 /* Print the information collected for a given DIE.  */
6729 
6730 DEBUG_FUNCTION void
debug_dwarf_die(dw_die_ref die)6731 debug_dwarf_die (dw_die_ref die)
6732 {
6733   print_die (die, stderr);
6734 }
6735 
6736 DEBUG_FUNCTION void
debug(die_struct & ref)6737 debug (die_struct &ref)
6738 {
6739   print_die (&ref, stderr);
6740 }
6741 
6742 DEBUG_FUNCTION void
debug(die_struct * ptr)6743 debug (die_struct *ptr)
6744 {
6745   if (ptr)
6746     debug (*ptr);
6747   else
6748     fprintf (stderr, "<nil>\n");
6749 }
6750 
6751 
6752 /* Print all DWARF information collected for the compilation unit.
6753    This routine is a debugging aid only.  */
6754 
6755 DEBUG_FUNCTION void
debug_dwarf(void)6756 debug_dwarf (void)
6757 {
6758   print_indent = 0;
6759   print_die (comp_unit_die (), stderr);
6760 }
6761 
6762 /* Verify the DIE tree structure.  */
6763 
6764 DEBUG_FUNCTION void
verify_die(dw_die_ref die)6765 verify_die (dw_die_ref die)
6766 {
6767   gcc_assert (!die->die_mark);
6768   if (die->die_parent == NULL
6769       && die->die_sib == NULL)
6770     return;
6771   /* Verify the die_sib list is cyclic.  */
6772   dw_die_ref x = die;
6773   do
6774     {
6775       x->die_mark = 1;
6776       x = x->die_sib;
6777     }
6778   while (x && !x->die_mark);
6779   gcc_assert (x == die);
6780   x = die;
6781   do
6782     {
6783       /* Verify all dies have the same parent.  */
6784       gcc_assert (x->die_parent == die->die_parent);
6785       if (x->die_child)
6786 	{
6787 	  /* Verify the child has the proper parent and recurse.  */
6788 	  gcc_assert (x->die_child->die_parent == x);
6789 	  verify_die (x->die_child);
6790 	}
6791       x->die_mark = 0;
6792       x = x->die_sib;
6793     }
6794   while (x && x->die_mark);
6795 }
6796 
6797 /* Sanity checks on DIEs.  */
6798 
6799 static void
check_die(dw_die_ref die)6800 check_die (dw_die_ref die)
6801 {
6802   unsigned ix;
6803   dw_attr_node *a;
6804   bool inline_found = false;
6805   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6806   int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6807   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6808     {
6809       switch (a->dw_attr)
6810 	{
6811 	case DW_AT_inline:
6812 	  if (a->dw_attr_val.v.val_unsigned)
6813 	    inline_found = true;
6814 	  break;
6815 	case DW_AT_location:
6816 	  ++n_location;
6817 	  break;
6818 	case DW_AT_low_pc:
6819 	  ++n_low_pc;
6820 	  break;
6821 	case DW_AT_high_pc:
6822 	  ++n_high_pc;
6823 	  break;
6824 	case DW_AT_artificial:
6825 	  ++n_artificial;
6826 	  break;
6827         case DW_AT_decl_column:
6828 	  ++n_decl_column;
6829 	  break;
6830 	case DW_AT_decl_line:
6831 	  ++n_decl_line;
6832 	  break;
6833 	case DW_AT_decl_file:
6834 	  ++n_decl_file;
6835 	  break;
6836 	default:
6837 	  break;
6838 	}
6839     }
6840   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6841       || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6842     {
6843       fprintf (stderr, "Duplicate attributes in DIE:\n");
6844       debug_dwarf_die (die);
6845       gcc_unreachable ();
6846     }
6847   if (inline_found)
6848     {
6849       /* A debugging information entry that is a member of an abstract
6850 	 instance tree [that has DW_AT_inline] should not contain any
6851 	 attributes which describe aspects of the subroutine which vary
6852 	 between distinct inlined expansions or distinct out-of-line
6853 	 expansions.  */
6854       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6855 	gcc_assert (a->dw_attr != DW_AT_low_pc
6856 		    && a->dw_attr != DW_AT_high_pc
6857 		    && a->dw_attr != DW_AT_location
6858 		    && a->dw_attr != DW_AT_frame_base
6859 		    && a->dw_attr != DW_AT_call_all_calls
6860 		    && a->dw_attr != DW_AT_GNU_all_call_sites);
6861     }
6862 }
6863 
6864 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6865 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6866 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6867 
6868 /* Calculate the checksum of a location expression.  */
6869 
6870 static inline void
loc_checksum(dw_loc_descr_ref loc,struct md5_ctx * ctx)6871 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6872 {
6873   int tem;
6874   inchash::hash hstate;
6875   hashval_t hash;
6876 
6877   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6878   CHECKSUM (tem);
6879   hash_loc_operands (loc, hstate);
6880   hash = hstate.end();
6881   CHECKSUM (hash);
6882 }
6883 
6884 /* Calculate the checksum of an attribute.  */
6885 
6886 static void
attr_checksum(dw_attr_node * at,struct md5_ctx * ctx,int * mark)6887 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6888 {
6889   dw_loc_descr_ref loc;
6890   rtx r;
6891 
6892   CHECKSUM (at->dw_attr);
6893 
6894   /* We don't care that this was compiled with a different compiler
6895      snapshot; if the output is the same, that's what matters.  */
6896   if (at->dw_attr == DW_AT_producer)
6897     return;
6898 
6899   switch (AT_class (at))
6900     {
6901     case dw_val_class_const:
6902     case dw_val_class_const_implicit:
6903       CHECKSUM (at->dw_attr_val.v.val_int);
6904       break;
6905     case dw_val_class_unsigned_const:
6906     case dw_val_class_unsigned_const_implicit:
6907       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6908       break;
6909     case dw_val_class_const_double:
6910       CHECKSUM (at->dw_attr_val.v.val_double);
6911       break;
6912     case dw_val_class_wide_int:
6913       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6914 		      get_full_len (*at->dw_attr_val.v.val_wide)
6915 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6916       break;
6917     case dw_val_class_vec:
6918       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6919 		      (at->dw_attr_val.v.val_vec.length
6920 		       * at->dw_attr_val.v.val_vec.elt_size));
6921       break;
6922     case dw_val_class_flag:
6923       CHECKSUM (at->dw_attr_val.v.val_flag);
6924       break;
6925     case dw_val_class_str:
6926       CHECKSUM_STRING (AT_string (at));
6927       break;
6928 
6929     case dw_val_class_addr:
6930       r = AT_addr (at);
6931       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6932       CHECKSUM_STRING (XSTR (r, 0));
6933       break;
6934 
6935     case dw_val_class_offset:
6936       CHECKSUM (at->dw_attr_val.v.val_offset);
6937       break;
6938 
6939     case dw_val_class_loc:
6940       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6941 	loc_checksum (loc, ctx);
6942       break;
6943 
6944     case dw_val_class_die_ref:
6945       die_checksum (AT_ref (at), ctx, mark);
6946       break;
6947 
6948     case dw_val_class_fde_ref:
6949     case dw_val_class_vms_delta:
6950     case dw_val_class_symview:
6951     case dw_val_class_lbl_id:
6952     case dw_val_class_lineptr:
6953     case dw_val_class_macptr:
6954     case dw_val_class_loclistsptr:
6955     case dw_val_class_high_pc:
6956       break;
6957 
6958     case dw_val_class_file:
6959     case dw_val_class_file_implicit:
6960       CHECKSUM_STRING (AT_file (at)->filename);
6961       break;
6962 
6963     case dw_val_class_data8:
6964       CHECKSUM (at->dw_attr_val.v.val_data8);
6965       break;
6966 
6967     default:
6968       break;
6969     }
6970 }
6971 
6972 /* Calculate the checksum of a DIE.  */
6973 
6974 static void
die_checksum(dw_die_ref die,struct md5_ctx * ctx,int * mark)6975 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6976 {
6977   dw_die_ref c;
6978   dw_attr_node *a;
6979   unsigned ix;
6980 
6981   /* To avoid infinite recursion.  */
6982   if (die->die_mark)
6983     {
6984       CHECKSUM (die->die_mark);
6985       return;
6986     }
6987   die->die_mark = ++(*mark);
6988 
6989   CHECKSUM (die->die_tag);
6990 
6991   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6992     attr_checksum (a, ctx, mark);
6993 
6994   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6995 }
6996 
6997 #undef CHECKSUM
6998 #undef CHECKSUM_BLOCK
6999 #undef CHECKSUM_STRING
7000 
7001 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
7002 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7003 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7004 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7005 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7006 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7007 #define CHECKSUM_ATTR(FOO) \
7008   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7009 
7010 /* Calculate the checksum of a number in signed LEB128 format.  */
7011 
7012 static void
checksum_sleb128(HOST_WIDE_INT value,struct md5_ctx * ctx)7013 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7014 {
7015   unsigned char byte;
7016   bool more;
7017 
7018   while (1)
7019     {
7020       byte = (value & 0x7f);
7021       value >>= 7;
7022       more = !((value == 0 && (byte & 0x40) == 0)
7023 		|| (value == -1 && (byte & 0x40) != 0));
7024       if (more)
7025 	byte |= 0x80;
7026       CHECKSUM (byte);
7027       if (!more)
7028 	break;
7029     }
7030 }
7031 
7032 /* Calculate the checksum of a number in unsigned LEB128 format.  */
7033 
7034 static void
checksum_uleb128(unsigned HOST_WIDE_INT value,struct md5_ctx * ctx)7035 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7036 {
7037   while (1)
7038     {
7039       unsigned char byte = (value & 0x7f);
7040       value >>= 7;
7041       if (value != 0)
7042 	/* More bytes to follow.  */
7043 	byte |= 0x80;
7044       CHECKSUM (byte);
7045       if (value == 0)
7046 	break;
7047     }
7048 }
7049 
7050 /* Checksum the context of the DIE.  This adds the names of any
7051    surrounding namespaces or structures to the checksum.  */
7052 
7053 static void
checksum_die_context(dw_die_ref die,struct md5_ctx * ctx)7054 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7055 {
7056   const char *name;
7057   dw_die_ref spec;
7058   int tag = die->die_tag;
7059 
7060   if (tag != DW_TAG_namespace
7061       && tag != DW_TAG_structure_type
7062       && tag != DW_TAG_class_type)
7063     return;
7064 
7065   name = get_AT_string (die, DW_AT_name);
7066 
7067   spec = get_AT_ref (die, DW_AT_specification);
7068   if (spec != NULL)
7069     die = spec;
7070 
7071   if (die->die_parent != NULL)
7072     checksum_die_context (die->die_parent, ctx);
7073 
7074   CHECKSUM_ULEB128 ('C');
7075   CHECKSUM_ULEB128 (tag);
7076   if (name != NULL)
7077     CHECKSUM_STRING (name);
7078 }
7079 
7080 /* Calculate the checksum of a location expression.  */
7081 
7082 static inline void
loc_checksum_ordered(dw_loc_descr_ref loc,struct md5_ctx * ctx)7083 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7084 {
7085   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7086      were emitted as a DW_FORM_sdata instead of a location expression.  */
7087   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7088     {
7089       CHECKSUM_ULEB128 (DW_FORM_sdata);
7090       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7091       return;
7092     }
7093 
7094   /* Otherwise, just checksum the raw location expression.  */
7095   while (loc != NULL)
7096     {
7097       inchash::hash hstate;
7098       hashval_t hash;
7099 
7100       CHECKSUM_ULEB128 (loc->dtprel);
7101       CHECKSUM_ULEB128 (loc->dw_loc_opc);
7102       hash_loc_operands (loc, hstate);
7103       hash = hstate.end ();
7104       CHECKSUM (hash);
7105       loc = loc->dw_loc_next;
7106     }
7107 }
7108 
7109 /* Calculate the checksum of an attribute.  */
7110 
7111 static void
attr_checksum_ordered(enum dwarf_tag tag,dw_attr_node * at,struct md5_ctx * ctx,int * mark)7112 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7113 		       struct md5_ctx *ctx, int *mark)
7114 {
7115   dw_loc_descr_ref loc;
7116   rtx r;
7117 
7118   if (AT_class (at) == dw_val_class_die_ref)
7119     {
7120       dw_die_ref target_die = AT_ref (at);
7121 
7122       /* For pointer and reference types, we checksum only the (qualified)
7123 	 name of the target type (if there is a name).  For friend entries,
7124 	 we checksum only the (qualified) name of the target type or function.
7125 	 This allows the checksum to remain the same whether the target type
7126 	 is complete or not.  */
7127       if ((at->dw_attr == DW_AT_type
7128 	   && (tag == DW_TAG_pointer_type
7129 	       || tag == DW_TAG_reference_type
7130 	       || tag == DW_TAG_rvalue_reference_type
7131 	       || tag == DW_TAG_ptr_to_member_type))
7132 	  || (at->dw_attr == DW_AT_friend
7133 	      && tag == DW_TAG_friend))
7134 	{
7135 	  dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7136 
7137 	  if (name_attr != NULL)
7138 	    {
7139 	      dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7140 
7141 	      if (decl == NULL)
7142 		decl = target_die;
7143 	      CHECKSUM_ULEB128 ('N');
7144 	      CHECKSUM_ULEB128 (at->dw_attr);
7145 	      if (decl->die_parent != NULL)
7146 		checksum_die_context (decl->die_parent, ctx);
7147 	      CHECKSUM_ULEB128 ('E');
7148 	      CHECKSUM_STRING (AT_string (name_attr));
7149 	      return;
7150 	    }
7151 	}
7152 
7153       /* For all other references to another DIE, we check to see if the
7154          target DIE has already been visited.  If it has, we emit a
7155          backward reference; if not, we descend recursively.  */
7156       if (target_die->die_mark > 0)
7157         {
7158 	  CHECKSUM_ULEB128 ('R');
7159 	  CHECKSUM_ULEB128 (at->dw_attr);
7160 	  CHECKSUM_ULEB128 (target_die->die_mark);
7161         }
7162       else
7163         {
7164 	  dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7165 
7166 	  if (decl == NULL)
7167 	    decl = target_die;
7168 	  target_die->die_mark = ++(*mark);
7169 	  CHECKSUM_ULEB128 ('T');
7170 	  CHECKSUM_ULEB128 (at->dw_attr);
7171 	  if (decl->die_parent != NULL)
7172 	    checksum_die_context (decl->die_parent, ctx);
7173 	  die_checksum_ordered (target_die, ctx, mark);
7174         }
7175       return;
7176     }
7177 
7178   CHECKSUM_ULEB128 ('A');
7179   CHECKSUM_ULEB128 (at->dw_attr);
7180 
7181   switch (AT_class (at))
7182     {
7183     case dw_val_class_const:
7184     case dw_val_class_const_implicit:
7185       CHECKSUM_ULEB128 (DW_FORM_sdata);
7186       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7187       break;
7188 
7189     case dw_val_class_unsigned_const:
7190     case dw_val_class_unsigned_const_implicit:
7191       CHECKSUM_ULEB128 (DW_FORM_sdata);
7192       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7193       break;
7194 
7195     case dw_val_class_const_double:
7196       CHECKSUM_ULEB128 (DW_FORM_block);
7197       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7198       CHECKSUM (at->dw_attr_val.v.val_double);
7199       break;
7200 
7201     case dw_val_class_wide_int:
7202       CHECKSUM_ULEB128 (DW_FORM_block);
7203       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7204 			* HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7205       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7206 		      get_full_len (*at->dw_attr_val.v.val_wide)
7207 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7208       break;
7209 
7210     case dw_val_class_vec:
7211       CHECKSUM_ULEB128 (DW_FORM_block);
7212       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7213 			* at->dw_attr_val.v.val_vec.elt_size);
7214       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7215 		      (at->dw_attr_val.v.val_vec.length
7216 		       * at->dw_attr_val.v.val_vec.elt_size));
7217       break;
7218 
7219     case dw_val_class_flag:
7220       CHECKSUM_ULEB128 (DW_FORM_flag);
7221       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7222       break;
7223 
7224     case dw_val_class_str:
7225       CHECKSUM_ULEB128 (DW_FORM_string);
7226       CHECKSUM_STRING (AT_string (at));
7227       break;
7228 
7229     case dw_val_class_addr:
7230       r = AT_addr (at);
7231       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7232       CHECKSUM_ULEB128 (DW_FORM_string);
7233       CHECKSUM_STRING (XSTR (r, 0));
7234       break;
7235 
7236     case dw_val_class_offset:
7237       CHECKSUM_ULEB128 (DW_FORM_sdata);
7238       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7239       break;
7240 
7241     case dw_val_class_loc:
7242       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7243 	loc_checksum_ordered (loc, ctx);
7244       break;
7245 
7246     case dw_val_class_fde_ref:
7247     case dw_val_class_symview:
7248     case dw_val_class_lbl_id:
7249     case dw_val_class_lineptr:
7250     case dw_val_class_macptr:
7251     case dw_val_class_loclistsptr:
7252     case dw_val_class_high_pc:
7253       break;
7254 
7255     case dw_val_class_file:
7256     case dw_val_class_file_implicit:
7257       CHECKSUM_ULEB128 (DW_FORM_string);
7258       CHECKSUM_STRING (AT_file (at)->filename);
7259       break;
7260 
7261     case dw_val_class_data8:
7262       CHECKSUM (at->dw_attr_val.v.val_data8);
7263       break;
7264 
7265     default:
7266       break;
7267     }
7268 }
7269 
7270 struct checksum_attributes
7271 {
7272   dw_attr_node *at_name;
7273   dw_attr_node *at_type;
7274   dw_attr_node *at_friend;
7275   dw_attr_node *at_accessibility;
7276   dw_attr_node *at_address_class;
7277   dw_attr_node *at_alignment;
7278   dw_attr_node *at_allocated;
7279   dw_attr_node *at_artificial;
7280   dw_attr_node *at_associated;
7281   dw_attr_node *at_binary_scale;
7282   dw_attr_node *at_bit_offset;
7283   dw_attr_node *at_bit_size;
7284   dw_attr_node *at_bit_stride;
7285   dw_attr_node *at_byte_size;
7286   dw_attr_node *at_byte_stride;
7287   dw_attr_node *at_const_value;
7288   dw_attr_node *at_containing_type;
7289   dw_attr_node *at_count;
7290   dw_attr_node *at_data_location;
7291   dw_attr_node *at_data_member_location;
7292   dw_attr_node *at_decimal_scale;
7293   dw_attr_node *at_decimal_sign;
7294   dw_attr_node *at_default_value;
7295   dw_attr_node *at_digit_count;
7296   dw_attr_node *at_discr;
7297   dw_attr_node *at_discr_list;
7298   dw_attr_node *at_discr_value;
7299   dw_attr_node *at_encoding;
7300   dw_attr_node *at_endianity;
7301   dw_attr_node *at_explicit;
7302   dw_attr_node *at_is_optional;
7303   dw_attr_node *at_location;
7304   dw_attr_node *at_lower_bound;
7305   dw_attr_node *at_mutable;
7306   dw_attr_node *at_ordering;
7307   dw_attr_node *at_picture_string;
7308   dw_attr_node *at_prototyped;
7309   dw_attr_node *at_small;
7310   dw_attr_node *at_segment;
7311   dw_attr_node *at_string_length;
7312   dw_attr_node *at_string_length_bit_size;
7313   dw_attr_node *at_string_length_byte_size;
7314   dw_attr_node *at_threads_scaled;
7315   dw_attr_node *at_upper_bound;
7316   dw_attr_node *at_use_location;
7317   dw_attr_node *at_use_UTF8;
7318   dw_attr_node *at_variable_parameter;
7319   dw_attr_node *at_virtuality;
7320   dw_attr_node *at_visibility;
7321   dw_attr_node *at_vtable_elem_location;
7322 };
7323 
7324 /* Collect the attributes that we will want to use for the checksum.  */
7325 
7326 static void
collect_checksum_attributes(struct checksum_attributes * attrs,dw_die_ref die)7327 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7328 {
7329   dw_attr_node *a;
7330   unsigned ix;
7331 
7332   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7333     {
7334       switch (a->dw_attr)
7335         {
7336         case DW_AT_name:
7337           attrs->at_name = a;
7338           break;
7339         case DW_AT_type:
7340           attrs->at_type = a;
7341           break;
7342         case DW_AT_friend:
7343           attrs->at_friend = a;
7344           break;
7345         case DW_AT_accessibility:
7346           attrs->at_accessibility = a;
7347           break;
7348         case DW_AT_address_class:
7349           attrs->at_address_class = a;
7350           break;
7351 	case DW_AT_alignment:
7352 	  attrs->at_alignment = a;
7353 	  break;
7354         case DW_AT_allocated:
7355           attrs->at_allocated = a;
7356           break;
7357         case DW_AT_artificial:
7358           attrs->at_artificial = a;
7359           break;
7360         case DW_AT_associated:
7361           attrs->at_associated = a;
7362           break;
7363         case DW_AT_binary_scale:
7364           attrs->at_binary_scale = a;
7365           break;
7366         case DW_AT_bit_offset:
7367           attrs->at_bit_offset = a;
7368           break;
7369         case DW_AT_bit_size:
7370           attrs->at_bit_size = a;
7371           break;
7372         case DW_AT_bit_stride:
7373           attrs->at_bit_stride = a;
7374           break;
7375         case DW_AT_byte_size:
7376           attrs->at_byte_size = a;
7377           break;
7378         case DW_AT_byte_stride:
7379           attrs->at_byte_stride = a;
7380           break;
7381         case DW_AT_const_value:
7382           attrs->at_const_value = a;
7383           break;
7384         case DW_AT_containing_type:
7385           attrs->at_containing_type = a;
7386           break;
7387         case DW_AT_count:
7388           attrs->at_count = a;
7389           break;
7390         case DW_AT_data_location:
7391           attrs->at_data_location = a;
7392           break;
7393         case DW_AT_data_member_location:
7394           attrs->at_data_member_location = a;
7395           break;
7396         case DW_AT_decimal_scale:
7397           attrs->at_decimal_scale = a;
7398           break;
7399         case DW_AT_decimal_sign:
7400           attrs->at_decimal_sign = a;
7401           break;
7402         case DW_AT_default_value:
7403           attrs->at_default_value = a;
7404           break;
7405         case DW_AT_digit_count:
7406           attrs->at_digit_count = a;
7407           break;
7408         case DW_AT_discr:
7409           attrs->at_discr = a;
7410           break;
7411         case DW_AT_discr_list:
7412           attrs->at_discr_list = a;
7413           break;
7414         case DW_AT_discr_value:
7415           attrs->at_discr_value = a;
7416           break;
7417         case DW_AT_encoding:
7418           attrs->at_encoding = a;
7419           break;
7420         case DW_AT_endianity:
7421           attrs->at_endianity = a;
7422           break;
7423         case DW_AT_explicit:
7424           attrs->at_explicit = a;
7425           break;
7426         case DW_AT_is_optional:
7427           attrs->at_is_optional = a;
7428           break;
7429         case DW_AT_location:
7430           attrs->at_location = a;
7431           break;
7432         case DW_AT_lower_bound:
7433           attrs->at_lower_bound = a;
7434           break;
7435         case DW_AT_mutable:
7436           attrs->at_mutable = a;
7437           break;
7438         case DW_AT_ordering:
7439           attrs->at_ordering = a;
7440           break;
7441         case DW_AT_picture_string:
7442           attrs->at_picture_string = a;
7443           break;
7444         case DW_AT_prototyped:
7445           attrs->at_prototyped = a;
7446           break;
7447         case DW_AT_small:
7448           attrs->at_small = a;
7449           break;
7450         case DW_AT_segment:
7451           attrs->at_segment = a;
7452           break;
7453         case DW_AT_string_length:
7454           attrs->at_string_length = a;
7455           break;
7456 	case DW_AT_string_length_bit_size:
7457 	  attrs->at_string_length_bit_size = a;
7458 	  break;
7459 	case DW_AT_string_length_byte_size:
7460 	  attrs->at_string_length_byte_size = a;
7461 	  break;
7462         case DW_AT_threads_scaled:
7463           attrs->at_threads_scaled = a;
7464           break;
7465         case DW_AT_upper_bound:
7466           attrs->at_upper_bound = a;
7467           break;
7468         case DW_AT_use_location:
7469           attrs->at_use_location = a;
7470           break;
7471         case DW_AT_use_UTF8:
7472           attrs->at_use_UTF8 = a;
7473           break;
7474         case DW_AT_variable_parameter:
7475           attrs->at_variable_parameter = a;
7476           break;
7477         case DW_AT_virtuality:
7478           attrs->at_virtuality = a;
7479           break;
7480         case DW_AT_visibility:
7481           attrs->at_visibility = a;
7482           break;
7483         case DW_AT_vtable_elem_location:
7484           attrs->at_vtable_elem_location = a;
7485           break;
7486         default:
7487           break;
7488         }
7489     }
7490 }
7491 
7492 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
7493 
7494 static void
die_checksum_ordered(dw_die_ref die,struct md5_ctx * ctx,int * mark)7495 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7496 {
7497   dw_die_ref c;
7498   dw_die_ref decl;
7499   struct checksum_attributes attrs;
7500 
7501   CHECKSUM_ULEB128 ('D');
7502   CHECKSUM_ULEB128 (die->die_tag);
7503 
7504   memset (&attrs, 0, sizeof (attrs));
7505 
7506   decl = get_AT_ref (die, DW_AT_specification);
7507   if (decl != NULL)
7508     collect_checksum_attributes (&attrs, decl);
7509   collect_checksum_attributes (&attrs, die);
7510 
7511   CHECKSUM_ATTR (attrs.at_name);
7512   CHECKSUM_ATTR (attrs.at_accessibility);
7513   CHECKSUM_ATTR (attrs.at_address_class);
7514   CHECKSUM_ATTR (attrs.at_allocated);
7515   CHECKSUM_ATTR (attrs.at_artificial);
7516   CHECKSUM_ATTR (attrs.at_associated);
7517   CHECKSUM_ATTR (attrs.at_binary_scale);
7518   CHECKSUM_ATTR (attrs.at_bit_offset);
7519   CHECKSUM_ATTR (attrs.at_bit_size);
7520   CHECKSUM_ATTR (attrs.at_bit_stride);
7521   CHECKSUM_ATTR (attrs.at_byte_size);
7522   CHECKSUM_ATTR (attrs.at_byte_stride);
7523   CHECKSUM_ATTR (attrs.at_const_value);
7524   CHECKSUM_ATTR (attrs.at_containing_type);
7525   CHECKSUM_ATTR (attrs.at_count);
7526   CHECKSUM_ATTR (attrs.at_data_location);
7527   CHECKSUM_ATTR (attrs.at_data_member_location);
7528   CHECKSUM_ATTR (attrs.at_decimal_scale);
7529   CHECKSUM_ATTR (attrs.at_decimal_sign);
7530   CHECKSUM_ATTR (attrs.at_default_value);
7531   CHECKSUM_ATTR (attrs.at_digit_count);
7532   CHECKSUM_ATTR (attrs.at_discr);
7533   CHECKSUM_ATTR (attrs.at_discr_list);
7534   CHECKSUM_ATTR (attrs.at_discr_value);
7535   CHECKSUM_ATTR (attrs.at_encoding);
7536   CHECKSUM_ATTR (attrs.at_endianity);
7537   CHECKSUM_ATTR (attrs.at_explicit);
7538   CHECKSUM_ATTR (attrs.at_is_optional);
7539   CHECKSUM_ATTR (attrs.at_location);
7540   CHECKSUM_ATTR (attrs.at_lower_bound);
7541   CHECKSUM_ATTR (attrs.at_mutable);
7542   CHECKSUM_ATTR (attrs.at_ordering);
7543   CHECKSUM_ATTR (attrs.at_picture_string);
7544   CHECKSUM_ATTR (attrs.at_prototyped);
7545   CHECKSUM_ATTR (attrs.at_small);
7546   CHECKSUM_ATTR (attrs.at_segment);
7547   CHECKSUM_ATTR (attrs.at_string_length);
7548   CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7549   CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7550   CHECKSUM_ATTR (attrs.at_threads_scaled);
7551   CHECKSUM_ATTR (attrs.at_upper_bound);
7552   CHECKSUM_ATTR (attrs.at_use_location);
7553   CHECKSUM_ATTR (attrs.at_use_UTF8);
7554   CHECKSUM_ATTR (attrs.at_variable_parameter);
7555   CHECKSUM_ATTR (attrs.at_virtuality);
7556   CHECKSUM_ATTR (attrs.at_visibility);
7557   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7558   CHECKSUM_ATTR (attrs.at_type);
7559   CHECKSUM_ATTR (attrs.at_friend);
7560   CHECKSUM_ATTR (attrs.at_alignment);
7561 
7562   /* Checksum the child DIEs.  */
7563   c = die->die_child;
7564   if (c) do {
7565     dw_attr_node *name_attr;
7566 
7567     c = c->die_sib;
7568     name_attr = get_AT (c, DW_AT_name);
7569     if (is_template_instantiation (c))
7570       {
7571 	/* Ignore instantiations of member type and function templates.  */
7572       }
7573     else if (name_attr != NULL
7574 	     && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7575       {
7576 	/* Use a shallow checksum for named nested types and member
7577 	   functions.  */
7578         CHECKSUM_ULEB128 ('S');
7579         CHECKSUM_ULEB128 (c->die_tag);
7580         CHECKSUM_STRING (AT_string (name_attr));
7581       }
7582     else
7583       {
7584 	/* Use a deep checksum for other children.  */
7585         /* Mark this DIE so it gets processed when unmarking.  */
7586         if (c->die_mark == 0)
7587           c->die_mark = -1;
7588         die_checksum_ordered (c, ctx, mark);
7589       }
7590   } while (c != die->die_child);
7591 
7592   CHECKSUM_ULEB128 (0);
7593 }
7594 
7595 /* Add a type name and tag to a hash.  */
7596 static void
die_odr_checksum(int tag,const char * name,md5_ctx * ctx)7597 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7598 {
7599   CHECKSUM_ULEB128 (tag);
7600   CHECKSUM_STRING (name);
7601 }
7602 
7603 #undef CHECKSUM
7604 #undef CHECKSUM_STRING
7605 #undef CHECKSUM_ATTR
7606 #undef CHECKSUM_LEB128
7607 #undef CHECKSUM_ULEB128
7608 
7609 /* Generate the type signature for DIE.  This is computed by generating an
7610    MD5 checksum over the DIE's tag, its relevant attributes, and its
7611    children.  Attributes that are references to other DIEs are processed
7612    by recursion, using the MARK field to prevent infinite recursion.
7613    If the DIE is nested inside a namespace or another type, we also
7614    need to include that context in the signature.  The lower 64 bits
7615    of the resulting MD5 checksum comprise the signature.  */
7616 
7617 static void
generate_type_signature(dw_die_ref die,comdat_type_node * type_node)7618 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7619 {
7620   int mark;
7621   const char *name;
7622   unsigned char checksum[16];
7623   struct md5_ctx ctx;
7624   dw_die_ref decl;
7625   dw_die_ref parent;
7626 
7627   name = get_AT_string (die, DW_AT_name);
7628   decl = get_AT_ref (die, DW_AT_specification);
7629   parent = get_die_parent (die);
7630 
7631   /* First, compute a signature for just the type name (and its surrounding
7632      context, if any.  This is stored in the type unit DIE for link-time
7633      ODR (one-definition rule) checking.  */
7634 
7635   if (is_cxx () && name != NULL)
7636     {
7637       md5_init_ctx (&ctx);
7638 
7639       /* Checksum the names of surrounding namespaces and structures.  */
7640       if (parent != NULL)
7641         checksum_die_context (parent, &ctx);
7642 
7643       /* Checksum the current DIE. */
7644       die_odr_checksum (die->die_tag, name, &ctx);
7645       md5_finish_ctx (&ctx, checksum);
7646 
7647       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7648     }
7649 
7650   /* Next, compute the complete type signature.  */
7651 
7652   md5_init_ctx (&ctx);
7653   mark = 1;
7654   die->die_mark = mark;
7655 
7656   /* Checksum the names of surrounding namespaces and structures.  */
7657   if (parent != NULL)
7658     checksum_die_context (parent, &ctx);
7659 
7660   /* Checksum the DIE and its children.  */
7661   die_checksum_ordered (die, &ctx, &mark);
7662   unmark_all_dies (die);
7663   md5_finish_ctx (&ctx, checksum);
7664 
7665   /* Store the signature in the type node and link the type DIE and the
7666      type node together.  */
7667   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7668           DWARF_TYPE_SIGNATURE_SIZE);
7669   die->comdat_type_p = true;
7670   die->die_id.die_type_node = type_node;
7671   type_node->type_die = die;
7672 
7673   /* If the DIE is a specification, link its declaration to the type node
7674      as well.  */
7675   if (decl != NULL)
7676     {
7677       decl->comdat_type_p = true;
7678       decl->die_id.die_type_node = type_node;
7679     }
7680 }
7681 
7682 /* Do the location expressions look same?  */
7683 static inline int
same_loc_p(dw_loc_descr_ref loc1,dw_loc_descr_ref loc2,int * mark)7684 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7685 {
7686   return loc1->dw_loc_opc == loc2->dw_loc_opc
7687 	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7688 	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7689 }
7690 
7691 /* Do the values look the same?  */
7692 static int
same_dw_val_p(const dw_val_node * v1,const dw_val_node * v2,int * mark)7693 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7694 {
7695   dw_loc_descr_ref loc1, loc2;
7696   rtx r1, r2;
7697 
7698   if (v1->val_class != v2->val_class)
7699     return 0;
7700 
7701   switch (v1->val_class)
7702     {
7703     case dw_val_class_const:
7704     case dw_val_class_const_implicit:
7705       return v1->v.val_int == v2->v.val_int;
7706     case dw_val_class_unsigned_const:
7707     case dw_val_class_unsigned_const_implicit:
7708       return v1->v.val_unsigned == v2->v.val_unsigned;
7709     case dw_val_class_const_double:
7710       return v1->v.val_double.high == v2->v.val_double.high
7711 	     && v1->v.val_double.low == v2->v.val_double.low;
7712     case dw_val_class_wide_int:
7713       return *v1->v.val_wide == *v2->v.val_wide;
7714     case dw_val_class_vec:
7715       if (v1->v.val_vec.length != v2->v.val_vec.length
7716 	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7717 	return 0;
7718       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7719 		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
7720 	return 0;
7721       return 1;
7722     case dw_val_class_flag:
7723       return v1->v.val_flag == v2->v.val_flag;
7724     case dw_val_class_str:
7725       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7726 
7727     case dw_val_class_addr:
7728       r1 = v1->v.val_addr;
7729       r2 = v2->v.val_addr;
7730       if (GET_CODE (r1) != GET_CODE (r2))
7731 	return 0;
7732       return !rtx_equal_p (r1, r2);
7733 
7734     case dw_val_class_offset:
7735       return v1->v.val_offset == v2->v.val_offset;
7736 
7737     case dw_val_class_loc:
7738       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7739 	   loc1 && loc2;
7740 	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7741 	if (!same_loc_p (loc1, loc2, mark))
7742 	  return 0;
7743       return !loc1 && !loc2;
7744 
7745     case dw_val_class_die_ref:
7746       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7747 
7748     case dw_val_class_symview:
7749       return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7750 
7751     case dw_val_class_fde_ref:
7752     case dw_val_class_vms_delta:
7753     case dw_val_class_lbl_id:
7754     case dw_val_class_lineptr:
7755     case dw_val_class_macptr:
7756     case dw_val_class_loclistsptr:
7757     case dw_val_class_high_pc:
7758       return 1;
7759 
7760     case dw_val_class_file:
7761     case dw_val_class_file_implicit:
7762       return v1->v.val_file == v2->v.val_file;
7763 
7764     case dw_val_class_data8:
7765       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7766 
7767     default:
7768       return 1;
7769     }
7770 }
7771 
7772 /* Do the attributes look the same?  */
7773 
7774 static int
same_attr_p(dw_attr_node * at1,dw_attr_node * at2,int * mark)7775 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7776 {
7777   if (at1->dw_attr != at2->dw_attr)
7778     return 0;
7779 
7780   /* We don't care that this was compiled with a different compiler
7781      snapshot; if the output is the same, that's what matters. */
7782   if (at1->dw_attr == DW_AT_producer)
7783     return 1;
7784 
7785   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7786 }
7787 
7788 /* Do the dies look the same?  */
7789 
7790 static int
same_die_p(dw_die_ref die1,dw_die_ref die2,int * mark)7791 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7792 {
7793   dw_die_ref c1, c2;
7794   dw_attr_node *a1;
7795   unsigned ix;
7796 
7797   /* To avoid infinite recursion.  */
7798   if (die1->die_mark)
7799     return die1->die_mark == die2->die_mark;
7800   die1->die_mark = die2->die_mark = ++(*mark);
7801 
7802   if (die1->die_tag != die2->die_tag)
7803     return 0;
7804 
7805   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7806     return 0;
7807 
7808   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7809     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7810       return 0;
7811 
7812   c1 = die1->die_child;
7813   c2 = die2->die_child;
7814   if (! c1)
7815     {
7816       if (c2)
7817 	return 0;
7818     }
7819   else
7820     for (;;)
7821       {
7822 	if (!same_die_p (c1, c2, mark))
7823 	  return 0;
7824 	c1 = c1->die_sib;
7825 	c2 = c2->die_sib;
7826 	if (c1 == die1->die_child)
7827 	  {
7828 	    if (c2 == die2->die_child)
7829 	      break;
7830 	    else
7831 	      return 0;
7832 	  }
7833     }
7834 
7835   return 1;
7836 }
7837 
7838 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7839    children, and set die_symbol.  */
7840 
7841 static void
compute_comp_unit_symbol(dw_die_ref unit_die)7842 compute_comp_unit_symbol (dw_die_ref unit_die)
7843 {
7844   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7845   const char *base = die_name ? lbasename (die_name) : "anonymous";
7846   char *name = XALLOCAVEC (char, strlen (base) + 64);
7847   char *p;
7848   int i, mark;
7849   unsigned char checksum[16];
7850   struct md5_ctx ctx;
7851 
7852   /* Compute the checksum of the DIE, then append part of it as hex digits to
7853      the name filename of the unit.  */
7854 
7855   md5_init_ctx (&ctx);
7856   mark = 0;
7857   die_checksum (unit_die, &ctx, &mark);
7858   unmark_all_dies (unit_die);
7859   md5_finish_ctx (&ctx, checksum);
7860 
7861   /* When we this for comp_unit_die () we have a DW_AT_name that might
7862      not start with a letter but with anything valid for filenames and
7863      clean_symbol_name doesn't fix that up.  Prepend 'g' if the first
7864      character is not a letter.  */
7865   sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7866   clean_symbol_name (name);
7867 
7868   p = name + strlen (name);
7869   for (i = 0; i < 4; i++)
7870     {
7871       sprintf (p, "%.2x", checksum[i]);
7872       p += 2;
7873     }
7874 
7875   unit_die->die_id.die_symbol = xstrdup (name);
7876 }
7877 
7878 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7879 
7880 static int
is_type_die(dw_die_ref die)7881 is_type_die (dw_die_ref die)
7882 {
7883   switch (die->die_tag)
7884     {
7885     case DW_TAG_array_type:
7886     case DW_TAG_class_type:
7887     case DW_TAG_interface_type:
7888     case DW_TAG_enumeration_type:
7889     case DW_TAG_pointer_type:
7890     case DW_TAG_reference_type:
7891     case DW_TAG_rvalue_reference_type:
7892     case DW_TAG_string_type:
7893     case DW_TAG_structure_type:
7894     case DW_TAG_subroutine_type:
7895     case DW_TAG_union_type:
7896     case DW_TAG_ptr_to_member_type:
7897     case DW_TAG_set_type:
7898     case DW_TAG_subrange_type:
7899     case DW_TAG_base_type:
7900     case DW_TAG_const_type:
7901     case DW_TAG_file_type:
7902     case DW_TAG_packed_type:
7903     case DW_TAG_volatile_type:
7904     case DW_TAG_typedef:
7905       return 1;
7906     default:
7907       return 0;
7908     }
7909 }
7910 
7911 /* Returns true iff C is a compile-unit DIE.  */
7912 
7913 static inline bool
is_cu_die(dw_die_ref c)7914 is_cu_die (dw_die_ref c)
7915 {
7916   return c && (c->die_tag == DW_TAG_compile_unit
7917 	       || c->die_tag == DW_TAG_skeleton_unit);
7918 }
7919 
7920 /* Returns true iff C is a unit DIE of some sort.  */
7921 
7922 static inline bool
is_unit_die(dw_die_ref c)7923 is_unit_die (dw_die_ref c)
7924 {
7925   return c && (c->die_tag == DW_TAG_compile_unit
7926 	       || c->die_tag == DW_TAG_partial_unit
7927 	       || c->die_tag == DW_TAG_type_unit
7928 	       || c->die_tag == DW_TAG_skeleton_unit);
7929 }
7930 
7931 /* Returns true iff C is a namespace DIE.  */
7932 
7933 static inline bool
is_namespace_die(dw_die_ref c)7934 is_namespace_die (dw_die_ref c)
7935 {
7936   return c && c->die_tag == DW_TAG_namespace;
7937 }
7938 
7939 /* Return non-zero if this DIE is a template parameter.  */
7940 
7941 static inline bool
is_template_parameter(dw_die_ref die)7942 is_template_parameter (dw_die_ref die)
7943 {
7944   switch (die->die_tag)
7945     {
7946     case DW_TAG_template_type_param:
7947     case DW_TAG_template_value_param:
7948     case DW_TAG_GNU_template_template_param:
7949     case DW_TAG_GNU_template_parameter_pack:
7950       return true;
7951     default:
7952       return false;
7953     }
7954 }
7955 
7956 /* Return non-zero if this DIE represents a template instantiation.  */
7957 
7958 static inline bool
is_template_instantiation(dw_die_ref die)7959 is_template_instantiation (dw_die_ref die)
7960 {
7961   dw_die_ref c;
7962 
7963   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7964     return false;
7965   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7966   return false;
7967 }
7968 
7969 static char *
gen_internal_sym(const char * prefix)7970 gen_internal_sym (const char *prefix)
7971 {
7972   char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7973 
7974   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7975   return xstrdup (buf);
7976 }
7977 
7978 /* Return non-zero if this DIE is a declaration.  */
7979 
7980 static int
is_declaration_die(dw_die_ref die)7981 is_declaration_die (dw_die_ref die)
7982 {
7983   dw_attr_node *a;
7984   unsigned ix;
7985 
7986   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7987     if (a->dw_attr == DW_AT_declaration)
7988       return 1;
7989 
7990   return 0;
7991 }
7992 
7993 /* Return non-zero if this DIE is nested inside a subprogram.  */
7994 
7995 static int
is_nested_in_subprogram(dw_die_ref die)7996 is_nested_in_subprogram (dw_die_ref die)
7997 {
7998   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7999 
8000   if (decl == NULL)
8001     decl = die;
8002   return local_scope_p (decl);
8003 }
8004 
8005 /* Return non-zero if this DIE contains a defining declaration of a
8006    subprogram.  */
8007 
8008 static int
contains_subprogram_definition(dw_die_ref die)8009 contains_subprogram_definition (dw_die_ref die)
8010 {
8011   dw_die_ref c;
8012 
8013   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8014     return 1;
8015   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8016   return 0;
8017 }
8018 
8019 /* Return non-zero if this is a type DIE that should be moved to a
8020    COMDAT .debug_types section or .debug_info section with DW_UT_*type
8021    unit type.  */
8022 
8023 static int
should_move_die_to_comdat(dw_die_ref die)8024 should_move_die_to_comdat (dw_die_ref die)
8025 {
8026   switch (die->die_tag)
8027     {
8028     case DW_TAG_class_type:
8029     case DW_TAG_structure_type:
8030     case DW_TAG_enumeration_type:
8031     case DW_TAG_union_type:
8032       /* Don't move declarations, inlined instances, types nested in a
8033 	 subprogram, or types that contain subprogram definitions.  */
8034       if (is_declaration_die (die)
8035           || get_AT (die, DW_AT_abstract_origin)
8036           || is_nested_in_subprogram (die)
8037           || contains_subprogram_definition (die))
8038         return 0;
8039       return 1;
8040     case DW_TAG_array_type:
8041     case DW_TAG_interface_type:
8042     case DW_TAG_pointer_type:
8043     case DW_TAG_reference_type:
8044     case DW_TAG_rvalue_reference_type:
8045     case DW_TAG_string_type:
8046     case DW_TAG_subroutine_type:
8047     case DW_TAG_ptr_to_member_type:
8048     case DW_TAG_set_type:
8049     case DW_TAG_subrange_type:
8050     case DW_TAG_base_type:
8051     case DW_TAG_const_type:
8052     case DW_TAG_file_type:
8053     case DW_TAG_packed_type:
8054     case DW_TAG_volatile_type:
8055     case DW_TAG_typedef:
8056     default:
8057       return 0;
8058     }
8059 }
8060 
8061 /* Make a clone of DIE.  */
8062 
8063 static dw_die_ref
clone_die(dw_die_ref die)8064 clone_die (dw_die_ref die)
8065 {
8066   dw_die_ref clone = new_die_raw (die->die_tag);
8067   dw_attr_node *a;
8068   unsigned ix;
8069 
8070   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8071     add_dwarf_attr (clone, a);
8072 
8073   return clone;
8074 }
8075 
8076 /* Make a clone of the tree rooted at DIE.  */
8077 
8078 static dw_die_ref
clone_tree(dw_die_ref die)8079 clone_tree (dw_die_ref die)
8080 {
8081   dw_die_ref c;
8082   dw_die_ref clone = clone_die (die);
8083 
8084   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8085 
8086   return clone;
8087 }
8088 
8089 /* Make a clone of DIE as a declaration.  */
8090 
8091 static dw_die_ref
clone_as_declaration(dw_die_ref die)8092 clone_as_declaration (dw_die_ref die)
8093 {
8094   dw_die_ref clone;
8095   dw_die_ref decl;
8096   dw_attr_node *a;
8097   unsigned ix;
8098 
8099   /* If the DIE is already a declaration, just clone it.  */
8100   if (is_declaration_die (die))
8101     return clone_die (die);
8102 
8103   /* If the DIE is a specification, just clone its declaration DIE.  */
8104   decl = get_AT_ref (die, DW_AT_specification);
8105   if (decl != NULL)
8106     {
8107       clone = clone_die (decl);
8108       if (die->comdat_type_p)
8109 	add_AT_die_ref (clone, DW_AT_signature, die);
8110       return clone;
8111     }
8112 
8113   clone = new_die_raw (die->die_tag);
8114 
8115   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8116     {
8117       /* We don't want to copy over all attributes.
8118          For example we don't want DW_AT_byte_size because otherwise we will no
8119          longer have a declaration and GDB will treat it as a definition.  */
8120 
8121       switch (a->dw_attr)
8122         {
8123         case DW_AT_abstract_origin:
8124         case DW_AT_artificial:
8125         case DW_AT_containing_type:
8126         case DW_AT_external:
8127         case DW_AT_name:
8128         case DW_AT_type:
8129         case DW_AT_virtuality:
8130         case DW_AT_linkage_name:
8131         case DW_AT_MIPS_linkage_name:
8132           add_dwarf_attr (clone, a);
8133           break;
8134         case DW_AT_byte_size:
8135 	case DW_AT_alignment:
8136         default:
8137           break;
8138         }
8139     }
8140 
8141   if (die->comdat_type_p)
8142     add_AT_die_ref (clone, DW_AT_signature, die);
8143 
8144   add_AT_flag (clone, DW_AT_declaration, 1);
8145   return clone;
8146 }
8147 
8148 
8149 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
8150 
8151 struct decl_table_entry
8152 {
8153   dw_die_ref orig;
8154   dw_die_ref copy;
8155 };
8156 
8157 /* Helpers to manipulate hash table of copied declarations.  */
8158 
8159 /* Hashtable helpers.  */
8160 
8161 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8162 {
8163   typedef die_struct *compare_type;
8164   static inline hashval_t hash (const decl_table_entry *);
8165   static inline bool equal (const decl_table_entry *, const die_struct *);
8166 };
8167 
8168 inline hashval_t
hash(const decl_table_entry * entry)8169 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8170 {
8171   return htab_hash_pointer (entry->orig);
8172 }
8173 
8174 inline bool
equal(const decl_table_entry * entry1,const die_struct * entry2)8175 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8176 				const die_struct *entry2)
8177 {
8178   return entry1->orig == entry2;
8179 }
8180 
8181 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8182 
8183 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8184    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
8185    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
8186    to check if the ancestor has already been copied into UNIT.  */
8187 
8188 static dw_die_ref
copy_ancestor_tree(dw_die_ref unit,dw_die_ref die,decl_hash_type * decl_table)8189 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8190 		    decl_hash_type *decl_table)
8191 {
8192   dw_die_ref parent = die->die_parent;
8193   dw_die_ref new_parent = unit;
8194   dw_die_ref copy;
8195   decl_table_entry **slot = NULL;
8196   struct decl_table_entry *entry = NULL;
8197 
8198   /* If DIE refers to a stub unfold that so we get the appropriate
8199      DIE registered as orig in decl_table.  */
8200   if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8201     die = c;
8202 
8203   if (decl_table)
8204     {
8205       /* Check if the entry has already been copied to UNIT.  */
8206       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8207 					      INSERT);
8208       if (*slot != HTAB_EMPTY_ENTRY)
8209         {
8210           entry = *slot;
8211           return entry->copy;
8212         }
8213 
8214       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
8215       entry = XCNEW (struct decl_table_entry);
8216       entry->orig = die;
8217       entry->copy = NULL;
8218       *slot = entry;
8219     }
8220 
8221   if (parent != NULL)
8222     {
8223       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8224       if (spec != NULL)
8225         parent = spec;
8226       if (!is_unit_die (parent))
8227         new_parent = copy_ancestor_tree (unit, parent, decl_table);
8228     }
8229 
8230   copy = clone_as_declaration (die);
8231   add_child_die (new_parent, copy);
8232 
8233   if (decl_table)
8234     {
8235       /* Record the pointer to the copy.  */
8236       entry->copy = copy;
8237     }
8238 
8239   return copy;
8240 }
8241 /* Copy the declaration context to the new type unit DIE.  This includes
8242    any surrounding namespace or type declarations.  If the DIE has an
8243    AT_specification attribute, it also includes attributes and children
8244    attached to the specification, and returns a pointer to the original
8245    parent of the declaration DIE.  Returns NULL otherwise.  */
8246 
8247 static dw_die_ref
copy_declaration_context(dw_die_ref unit,dw_die_ref die)8248 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8249 {
8250   dw_die_ref decl;
8251   dw_die_ref new_decl;
8252   dw_die_ref orig_parent = NULL;
8253 
8254   decl = get_AT_ref (die, DW_AT_specification);
8255   if (decl == NULL)
8256     decl = die;
8257   else
8258     {
8259       unsigned ix;
8260       dw_die_ref c;
8261       dw_attr_node *a;
8262 
8263       /* The original DIE will be changed to a declaration, and must
8264          be moved to be a child of the original declaration DIE.  */
8265       orig_parent = decl->die_parent;
8266 
8267       /* Copy the type node pointer from the new DIE to the original
8268          declaration DIE so we can forward references later.  */
8269       decl->comdat_type_p = true;
8270       decl->die_id.die_type_node = die->die_id.die_type_node;
8271 
8272       remove_AT (die, DW_AT_specification);
8273 
8274       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8275         {
8276           if (a->dw_attr != DW_AT_name
8277               && a->dw_attr != DW_AT_declaration
8278               && a->dw_attr != DW_AT_external)
8279             add_dwarf_attr (die, a);
8280         }
8281 
8282       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8283     }
8284 
8285   if (decl->die_parent != NULL
8286       && !is_unit_die (decl->die_parent))
8287     {
8288       new_decl = copy_ancestor_tree (unit, decl, NULL);
8289       if (new_decl != NULL)
8290         {
8291           remove_AT (new_decl, DW_AT_signature);
8292           add_AT_specification (die, new_decl);
8293         }
8294     }
8295 
8296   return orig_parent;
8297 }
8298 
8299 /* Generate the skeleton ancestor tree for the given NODE, then clone
8300    the DIE and add the clone into the tree.  */
8301 
8302 static void
generate_skeleton_ancestor_tree(skeleton_chain_node * node)8303 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8304 {
8305   if (node->new_die != NULL)
8306     return;
8307 
8308   node->new_die = clone_as_declaration (node->old_die);
8309 
8310   if (node->parent != NULL)
8311     {
8312       generate_skeleton_ancestor_tree (node->parent);
8313       add_child_die (node->parent->new_die, node->new_die);
8314     }
8315 }
8316 
8317 /* Generate a skeleton tree of DIEs containing any declarations that are
8318    found in the original tree.  We traverse the tree looking for declaration
8319    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
8320 
8321 static void
generate_skeleton_bottom_up(skeleton_chain_node * parent)8322 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8323 {
8324   skeleton_chain_node node;
8325   dw_die_ref c;
8326   dw_die_ref first;
8327   dw_die_ref prev = NULL;
8328   dw_die_ref next = NULL;
8329 
8330   node.parent = parent;
8331 
8332   first = c = parent->old_die->die_child;
8333   if (c)
8334     next = c->die_sib;
8335   if (c) do {
8336     if (prev == NULL || prev->die_sib == c)
8337       prev = c;
8338     c = next;
8339     next = (c == first ? NULL : c->die_sib);
8340     node.old_die = c;
8341     node.new_die = NULL;
8342     if (is_declaration_die (c))
8343       {
8344 	if (is_template_instantiation (c))
8345 	  {
8346 	    /* Instantiated templates do not need to be cloned into the
8347 	       type unit.  Just move the DIE and its children back to
8348 	       the skeleton tree (in the main CU).  */
8349 	    remove_child_with_prev (c, prev);
8350 	    add_child_die (parent->new_die, c);
8351 	    c = prev;
8352 	  }
8353 	else if (c->comdat_type_p)
8354 	  {
8355 	    /* This is the skeleton of earlier break_out_comdat_types
8356 	       type.  Clone the existing DIE, but keep the children
8357 	       under the original (which is in the main CU).  */
8358 	    dw_die_ref clone = clone_die (c);
8359 
8360 	    replace_child (c, clone, prev);
8361 	    generate_skeleton_ancestor_tree (parent);
8362 	    add_child_die (parent->new_die, c);
8363 	    c = clone;
8364 	    continue;
8365 	  }
8366 	else
8367 	  {
8368 	    /* Clone the existing DIE, move the original to the skeleton
8369 	       tree (which is in the main CU), and put the clone, with
8370 	       all the original's children, where the original came from
8371 	       (which is about to be moved to the type unit).  */
8372 	    dw_die_ref clone = clone_die (c);
8373 	    move_all_children (c, clone);
8374 
8375 	    /* If the original has a DW_AT_object_pointer attribute,
8376 	       it would now point to a child DIE just moved to the
8377 	       cloned tree, so we need to remove that attribute from
8378 	       the original.  */
8379 	    remove_AT (c, DW_AT_object_pointer);
8380 
8381 	    replace_child (c, clone, prev);
8382 	    generate_skeleton_ancestor_tree (parent);
8383 	    add_child_die (parent->new_die, c);
8384 	    node.old_die = clone;
8385 	    node.new_die = c;
8386 	    c = clone;
8387 	  }
8388       }
8389     generate_skeleton_bottom_up (&node);
8390   } while (next != NULL);
8391 }
8392 
8393 /* Wrapper function for generate_skeleton_bottom_up.  */
8394 
8395 static dw_die_ref
generate_skeleton(dw_die_ref die)8396 generate_skeleton (dw_die_ref die)
8397 {
8398   skeleton_chain_node node;
8399 
8400   node.old_die = die;
8401   node.new_die = NULL;
8402   node.parent = NULL;
8403 
8404   /* If this type definition is nested inside another type,
8405      and is not an instantiation of a template, always leave
8406      at least a declaration in its place.  */
8407   if (die->die_parent != NULL
8408       && is_type_die (die->die_parent)
8409       && !is_template_instantiation (die))
8410     node.new_die = clone_as_declaration (die);
8411 
8412   generate_skeleton_bottom_up (&node);
8413   return node.new_die;
8414 }
8415 
8416 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8417    declaration.  The original DIE is moved to a new compile unit so that
8418    existing references to it follow it to the new location.  If any of the
8419    original DIE's descendants is a declaration, we need to replace the
8420    original DIE with a skeleton tree and move the declarations back into the
8421    skeleton tree.  */
8422 
8423 static dw_die_ref
remove_child_or_replace_with_skeleton(dw_die_ref unit,dw_die_ref child,dw_die_ref prev)8424 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8425 				       dw_die_ref prev)
8426 {
8427   dw_die_ref skeleton, orig_parent;
8428 
8429   /* Copy the declaration context to the type unit DIE.  If the returned
8430      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8431      that DIE.  */
8432   orig_parent = copy_declaration_context (unit, child);
8433 
8434   skeleton = generate_skeleton (child);
8435   if (skeleton == NULL)
8436     remove_child_with_prev (child, prev);
8437   else
8438     {
8439       skeleton->comdat_type_p = true;
8440       skeleton->die_id.die_type_node = child->die_id.die_type_node;
8441 
8442       /* If the original DIE was a specification, we need to put
8443          the skeleton under the parent DIE of the declaration.
8444 	 This leaves the original declaration in the tree, but
8445 	 it will be pruned later since there are no longer any
8446 	 references to it.  */
8447       if (orig_parent != NULL)
8448 	{
8449 	  remove_child_with_prev (child, prev);
8450 	  add_child_die (orig_parent, skeleton);
8451 	}
8452       else
8453 	replace_child (child, skeleton, prev);
8454     }
8455 
8456   return skeleton;
8457 }
8458 
8459 static void
8460 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8461 			       comdat_type_node *type_node,
8462 			       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8463 
8464 /* Helper for copy_dwarf_procs_ref_in_dies.  Make a copy of the DIE DWARF
8465    procedure, put it under TYPE_NODE and return the copy.  Continue looking for
8466    DWARF procedure references in the DW_AT_location attribute.  */
8467 
8468 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)8469 copy_dwarf_procedure (dw_die_ref die,
8470 		      comdat_type_node *type_node,
8471 		      hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8472 {
8473   gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8474 
8475   /* DWARF procedures are not supposed to have children...  */
8476   gcc_assert (die->die_child == NULL);
8477 
8478   /* ... and they are supposed to have only one attribute: DW_AT_location.  */
8479   gcc_assert (vec_safe_length (die->die_attr) == 1
8480 	      && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8481 
8482   /* Do not copy more than once DWARF procedures.  */
8483   bool existed;
8484   dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8485   if (existed)
8486     return die_copy;
8487 
8488   die_copy = clone_die (die);
8489   add_child_die (type_node->root_die, die_copy);
8490   copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8491   return die_copy;
8492 }
8493 
8494 /* Helper for copy_dwarf_procs_ref_in_dies.  Look for references to DWARF
8495    procedures in DIE's attributes.  */
8496 
8497 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)8498 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8499 			       comdat_type_node *type_node,
8500 			       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8501 {
8502   dw_attr_node *a;
8503   unsigned i;
8504 
8505   FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8506     {
8507       dw_loc_descr_ref loc;
8508 
8509       if (a->dw_attr_val.val_class != dw_val_class_loc)
8510 	continue;
8511 
8512       for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8513 	{
8514 	  switch (loc->dw_loc_opc)
8515 	    {
8516 	    case DW_OP_call2:
8517 	    case DW_OP_call4:
8518 	    case DW_OP_call_ref:
8519 	      gcc_assert (loc->dw_loc_oprnd1.val_class
8520 			  == dw_val_class_die_ref);
8521 	      loc->dw_loc_oprnd1.v.val_die_ref.die
8522 	        = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8523 					type_node,
8524 					copied_dwarf_procs);
8525 
8526 	    default:
8527 	      break;
8528 	    }
8529 	}
8530     }
8531 }
8532 
8533 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8534    rewrite references to point to the copies.
8535 
8536    References are looked for in DIE's attributes and recursively in all its
8537    children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8538    mapping from old DWARF procedures to their copy. It is used not to copy
8539    twice the same DWARF procedure under TYPE_NODE.  */
8540 
8541 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)8542 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8543 			      comdat_type_node *type_node,
8544 			      hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8545 {
8546   dw_die_ref c;
8547 
8548   copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8549   FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8550 							type_node,
8551 							copied_dwarf_procs));
8552 }
8553 
8554 /* Traverse the DIE and set up additional .debug_types or .debug_info
8555    DW_UT_*type sections for each type worthy of being placed in a COMDAT
8556    section.  */
8557 
8558 static void
break_out_comdat_types(dw_die_ref die)8559 break_out_comdat_types (dw_die_ref die)
8560 {
8561   dw_die_ref c;
8562   dw_die_ref first;
8563   dw_die_ref prev = NULL;
8564   dw_die_ref next = NULL;
8565   dw_die_ref unit = NULL;
8566 
8567   first = c = die->die_child;
8568   if (c)
8569     next = c->die_sib;
8570   if (c) do {
8571     if (prev == NULL || prev->die_sib == c)
8572       prev = c;
8573     c = next;
8574     next = (c == first ? NULL : c->die_sib);
8575     if (should_move_die_to_comdat (c))
8576       {
8577         dw_die_ref replacement;
8578 	comdat_type_node *type_node;
8579 
8580         /* Break out nested types into their own type units.  */
8581         break_out_comdat_types (c);
8582 
8583         /* Create a new type unit DIE as the root for the new tree.  */
8584         unit = new_die (DW_TAG_type_unit, NULL, NULL);
8585         add_AT_unsigned (unit, DW_AT_language,
8586                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
8587 
8588 	/* Add the new unit's type DIE into the comdat type list.  */
8589         type_node = ggc_cleared_alloc<comdat_type_node> ();
8590         type_node->root_die = unit;
8591         type_node->next = comdat_type_list;
8592         comdat_type_list = type_node;
8593 
8594         /* Generate the type signature.  */
8595         generate_type_signature (c, type_node);
8596 
8597         /* Copy the declaration context, attributes, and children of the
8598            declaration into the new type unit DIE, then remove this DIE
8599 	   from the main CU (or replace it with a skeleton if necessary).  */
8600 	replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8601 	type_node->skeleton_die = replacement;
8602 
8603         /* Add the DIE to the new compunit.  */
8604 	add_child_die (unit, c);
8605 
8606 	/* Types can reference DWARF procedures for type size or data location
8607 	   expressions.  Calls in DWARF expressions cannot target procedures
8608 	   that are not in the same section.  So we must copy DWARF procedures
8609 	   along with this type and then rewrite references to them.  */
8610 	hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8611 	copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8612 
8613         if (replacement != NULL)
8614           c = replacement;
8615       }
8616     else if (c->die_tag == DW_TAG_namespace
8617              || c->die_tag == DW_TAG_class_type
8618              || c->die_tag == DW_TAG_structure_type
8619              || c->die_tag == DW_TAG_union_type)
8620       {
8621         /* Look for nested types that can be broken out.  */
8622         break_out_comdat_types (c);
8623       }
8624   } while (next != NULL);
8625 }
8626 
8627 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8628    Enter all the cloned children into the hash table decl_table.  */
8629 
8630 static dw_die_ref
clone_tree_partial(dw_die_ref die,decl_hash_type * decl_table)8631 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8632 {
8633   dw_die_ref c;
8634   dw_die_ref clone;
8635   struct decl_table_entry *entry;
8636   decl_table_entry **slot;
8637 
8638   if (die->die_tag == DW_TAG_subprogram)
8639     clone = clone_as_declaration (die);
8640   else
8641     clone = clone_die (die);
8642 
8643   slot = decl_table->find_slot_with_hash (die,
8644 					  htab_hash_pointer (die), INSERT);
8645 
8646   /* Assert that DIE isn't in the hash table yet.  If it would be there
8647      before, the ancestors would be necessarily there as well, therefore
8648      clone_tree_partial wouldn't be called.  */
8649   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8650 
8651   entry = XCNEW (struct decl_table_entry);
8652   entry->orig = die;
8653   entry->copy = clone;
8654   *slot = entry;
8655 
8656   if (die->die_tag != DW_TAG_subprogram)
8657     FOR_EACH_CHILD (die, c,
8658 		    add_child_die (clone, clone_tree_partial (c, decl_table)));
8659 
8660   return clone;
8661 }
8662 
8663 /* Walk the DIE and its children, looking for references to incomplete
8664    or trivial types that are unmarked (i.e., that are not in the current
8665    type_unit).  */
8666 
8667 static void
copy_decls_walk(dw_die_ref unit,dw_die_ref die,decl_hash_type * decl_table)8668 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8669 {
8670   dw_die_ref c;
8671   dw_attr_node *a;
8672   unsigned ix;
8673 
8674   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8675     {
8676       if (AT_class (a) == dw_val_class_die_ref)
8677         {
8678           dw_die_ref targ = AT_ref (a);
8679           decl_table_entry **slot;
8680           struct decl_table_entry *entry;
8681 
8682           if (targ->die_mark != 0 || targ->comdat_type_p)
8683             continue;
8684 
8685           slot = decl_table->find_slot_with_hash (targ,
8686 						  htab_hash_pointer (targ),
8687 						  INSERT);
8688 
8689           if (*slot != HTAB_EMPTY_ENTRY)
8690             {
8691               /* TARG has already been copied, so we just need to
8692                  modify the reference to point to the copy.  */
8693               entry = *slot;
8694               a->dw_attr_val.v.val_die_ref.die = entry->copy;
8695             }
8696           else
8697             {
8698               dw_die_ref parent = unit;
8699 	      dw_die_ref copy = clone_die (targ);
8700 
8701               /* Record in DECL_TABLE that TARG has been copied.
8702                  Need to do this now, before the recursive call,
8703                  because DECL_TABLE may be expanded and SLOT
8704                  would no longer be a valid pointer.  */
8705               entry = XCNEW (struct decl_table_entry);
8706               entry->orig = targ;
8707               entry->copy = copy;
8708               *slot = entry;
8709 
8710 	      /* If TARG is not a declaration DIE, we need to copy its
8711 	         children.  */
8712 	      if (!is_declaration_die (targ))
8713 		{
8714 		  FOR_EACH_CHILD (
8715 		      targ, c,
8716 		      add_child_die (copy,
8717 				     clone_tree_partial (c, decl_table)));
8718 		}
8719 
8720               /* Make sure the cloned tree is marked as part of the
8721                  type unit.  */
8722               mark_dies (copy);
8723 
8724               /* If TARG has surrounding context, copy its ancestor tree
8725                  into the new type unit.  */
8726               if (targ->die_parent != NULL
8727 		  && !is_unit_die (targ->die_parent))
8728                 parent = copy_ancestor_tree (unit, targ->die_parent,
8729                                              decl_table);
8730 
8731               add_child_die (parent, copy);
8732               a->dw_attr_val.v.val_die_ref.die = copy;
8733 
8734               /* Make sure the newly-copied DIE is walked.  If it was
8735                  installed in a previously-added context, it won't
8736                  get visited otherwise.  */
8737               if (parent != unit)
8738 		{
8739 		  /* Find the highest point of the newly-added tree,
8740 		     mark each node along the way, and walk from there.  */
8741 		  parent->die_mark = 1;
8742 		  while (parent->die_parent
8743 		  	 && parent->die_parent->die_mark == 0)
8744 		    {
8745 		      parent = parent->die_parent;
8746 		      parent->die_mark = 1;
8747 		    }
8748 		  copy_decls_walk (unit, parent, decl_table);
8749 		}
8750             }
8751         }
8752     }
8753 
8754   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8755 }
8756 
8757 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8758    and record them in DECL_TABLE.  */
8759 
8760 static void
collect_skeleton_dies(dw_die_ref die,decl_hash_type * decl_table)8761 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8762 {
8763   dw_die_ref c;
8764 
8765   if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8766     {
8767       dw_die_ref targ = AT_ref (a);
8768       gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8769       decl_table_entry **slot
8770         = decl_table->find_slot_with_hash (targ,
8771 					   htab_hash_pointer (targ),
8772 					   INSERT);
8773       gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8774       /* Record in DECL_TABLE that TARG has been already copied
8775 	 by remove_child_or_replace_with_skeleton.  */
8776       decl_table_entry *entry = XCNEW (struct decl_table_entry);
8777       entry->orig = targ;
8778       entry->copy = die;
8779       *slot = entry;
8780     }
8781   FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8782 }
8783 
8784 /* Copy declarations for "unworthy" types into the new comdat section.
8785    Incomplete types, modified types, and certain other types aren't broken
8786    out into comdat sections of their own, so they don't have a signature,
8787    and we need to copy the declaration into the same section so that we
8788    don't have an external reference.  */
8789 
8790 static void
copy_decls_for_unworthy_types(dw_die_ref unit)8791 copy_decls_for_unworthy_types (dw_die_ref unit)
8792 {
8793   mark_dies (unit);
8794   decl_hash_type decl_table (10);
8795   collect_skeleton_dies (unit, &decl_table);
8796   copy_decls_walk (unit, unit, &decl_table);
8797   unmark_dies (unit);
8798 }
8799 
8800 /* Traverse the DIE and add a sibling attribute if it may have the
8801    effect of speeding up access to siblings.  To save some space,
8802    avoid generating sibling attributes for DIE's without children.  */
8803 
8804 static void
add_sibling_attributes(dw_die_ref die)8805 add_sibling_attributes (dw_die_ref die)
8806 {
8807   dw_die_ref c;
8808 
8809   if (! die->die_child)
8810     return;
8811 
8812   if (die->die_parent && die != die->die_parent->die_child)
8813     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8814 
8815   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8816 }
8817 
8818 /* Output all location lists for the DIE and its children.  */
8819 
8820 static void
output_location_lists(dw_die_ref die)8821 output_location_lists (dw_die_ref die)
8822 {
8823   dw_die_ref c;
8824   dw_attr_node *a;
8825   unsigned ix;
8826 
8827   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8828     if (AT_class (a) == dw_val_class_loc_list)
8829       output_loc_list (AT_loc_list (a));
8830 
8831   FOR_EACH_CHILD (die, c, output_location_lists (c));
8832 }
8833 
8834 /* During assign_location_list_indexes and output_loclists_offset the
8835    current index, after it the number of assigned indexes (i.e. how
8836    large the .debug_loclists* offset table should be).  */
8837 static unsigned int loc_list_idx;
8838 
8839 /* Output all location list offsets for the DIE and its children.  */
8840 
8841 static void
output_loclists_offsets(dw_die_ref die)8842 output_loclists_offsets (dw_die_ref die)
8843 {
8844   dw_die_ref c;
8845   dw_attr_node *a;
8846   unsigned ix;
8847 
8848   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8849     if (AT_class (a) == dw_val_class_loc_list)
8850       {
8851 	dw_loc_list_ref l = AT_loc_list (a);
8852 	if (l->offset_emitted)
8853 	  continue;
8854 	dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8855 			      loc_section_label, NULL);
8856 	gcc_assert (l->hash == loc_list_idx);
8857 	loc_list_idx++;
8858 	l->offset_emitted = true;
8859       }
8860 
8861   FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8862 }
8863 
8864 /* Recursively set indexes of location lists.  */
8865 
8866 static void
assign_location_list_indexes(dw_die_ref die)8867 assign_location_list_indexes (dw_die_ref die)
8868 {
8869   dw_die_ref c;
8870   dw_attr_node *a;
8871   unsigned ix;
8872 
8873   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8874     if (AT_class (a) == dw_val_class_loc_list)
8875       {
8876 	dw_loc_list_ref list = AT_loc_list (a);
8877 	if (!list->num_assigned)
8878 	  {
8879 	    list->num_assigned = true;
8880 	    list->hash = loc_list_idx++;
8881 	  }
8882       }
8883 
8884   FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8885 }
8886 
8887 /* We want to limit the number of external references, because they are
8888    larger than local references: a relocation takes multiple words, and
8889    even a sig8 reference is always eight bytes, whereas a local reference
8890    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8891    So if we encounter multiple external references to the same type DIE, we
8892    make a local typedef stub for it and redirect all references there.
8893 
8894    This is the element of the hash table for keeping track of these
8895    references.  */
8896 
8897 struct external_ref
8898 {
8899   dw_die_ref type;
8900   dw_die_ref stub;
8901   unsigned n_refs;
8902 };
8903 
8904 /* Hashtable helpers.  */
8905 
8906 struct external_ref_hasher : free_ptr_hash <external_ref>
8907 {
8908   static inline hashval_t hash (const external_ref *);
8909   static inline bool equal (const external_ref *, const external_ref *);
8910 };
8911 
8912 inline hashval_t
hash(const external_ref * r)8913 external_ref_hasher::hash (const external_ref *r)
8914 {
8915   dw_die_ref die = r->type;
8916   hashval_t h = 0;
8917 
8918   /* We can't use the address of the DIE for hashing, because
8919      that will make the order of the stub DIEs non-deterministic.  */
8920   if (! die->comdat_type_p)
8921     /* We have a symbol; use it to compute a hash.  */
8922     h = htab_hash_string (die->die_id.die_symbol);
8923   else
8924     {
8925       /* We have a type signature; use a subset of the bits as the hash.
8926 	 The 8-byte signature is at least as large as hashval_t.  */
8927       comdat_type_node *type_node = die->die_id.die_type_node;
8928       memcpy (&h, type_node->signature, sizeof (h));
8929     }
8930   return h;
8931 }
8932 
8933 inline bool
equal(const external_ref * r1,const external_ref * r2)8934 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8935 {
8936   return r1->type == r2->type;
8937 }
8938 
8939 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8940 
8941 /* Return a pointer to the external_ref for references to DIE.  */
8942 
8943 static struct external_ref *
lookup_external_ref(external_ref_hash_type * map,dw_die_ref die)8944 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8945 {
8946   struct external_ref ref, *ref_p;
8947   external_ref **slot;
8948 
8949   ref.type = die;
8950   slot = map->find_slot (&ref, INSERT);
8951   if (*slot != HTAB_EMPTY_ENTRY)
8952     return *slot;
8953 
8954   ref_p = XCNEW (struct external_ref);
8955   ref_p->type = die;
8956   *slot = ref_p;
8957   return ref_p;
8958 }
8959 
8960 /* Subroutine of optimize_external_refs, below.
8961 
8962    If we see a type skeleton, record it as our stub.  If we see external
8963    references, remember how many we've seen.  */
8964 
8965 static void
optimize_external_refs_1(dw_die_ref die,external_ref_hash_type * map)8966 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8967 {
8968   dw_die_ref c;
8969   dw_attr_node *a;
8970   unsigned ix;
8971   struct external_ref *ref_p;
8972 
8973   if (is_type_die (die)
8974       && (c = get_AT_ref (die, DW_AT_signature)))
8975     {
8976       /* This is a local skeleton; use it for local references.  */
8977       ref_p = lookup_external_ref (map, c);
8978       ref_p->stub = die;
8979     }
8980 
8981   /* Scan the DIE references, and remember any that refer to DIEs from
8982      other CUs (i.e. those which are not marked).  */
8983   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8984     if (AT_class (a) == dw_val_class_die_ref
8985 	&& (c = AT_ref (a))->die_mark == 0
8986 	&& is_type_die (c))
8987       {
8988 	ref_p = lookup_external_ref (map, c);
8989 	ref_p->n_refs++;
8990       }
8991 
8992   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8993 }
8994 
8995 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
8996    points to an external_ref, DATA is the CU we're processing.  If we don't
8997    already have a local stub, and we have multiple refs, build a stub.  */
8998 
8999 int
dwarf2_build_local_stub(external_ref ** slot,dw_die_ref data)9000 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9001 {
9002   struct external_ref *ref_p = *slot;
9003 
9004   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9005     {
9006       /* We have multiple references to this type, so build a small stub.
9007 	 Both of these forms are a bit dodgy from the perspective of the
9008 	 DWARF standard, since technically they should have names.  */
9009       dw_die_ref cu = data;
9010       dw_die_ref type = ref_p->type;
9011       dw_die_ref stub = NULL;
9012 
9013       if (type->comdat_type_p)
9014 	{
9015 	  /* If we refer to this type via sig8, use AT_signature.  */
9016 	  stub = new_die (type->die_tag, cu, NULL_TREE);
9017 	  add_AT_die_ref (stub, DW_AT_signature, type);
9018 	}
9019       else
9020 	{
9021 	  /* Otherwise, use a typedef with no name.  */
9022 	  stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9023 	  add_AT_die_ref (stub, DW_AT_type, type);
9024 	}
9025 
9026       stub->die_mark++;
9027       ref_p->stub = stub;
9028     }
9029   return 1;
9030 }
9031 
9032 /* DIE is a unit; look through all the DIE references to see if there are
9033    any external references to types, and if so, create local stubs for
9034    them which will be applied in build_abbrev_table.  This is useful because
9035    references to local DIEs are smaller.  */
9036 
9037 static external_ref_hash_type *
optimize_external_refs(dw_die_ref die)9038 optimize_external_refs (dw_die_ref die)
9039 {
9040   external_ref_hash_type *map = new external_ref_hash_type (10);
9041   optimize_external_refs_1 (die, map);
9042   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9043   return map;
9044 }
9045 
9046 /* The following 3 variables are temporaries that are computed only during the
9047    build_abbrev_table call and used and released during the following
9048    optimize_abbrev_table call.  */
9049 
9050 /* First abbrev_id that can be optimized based on usage.  */
9051 static unsigned int abbrev_opt_start;
9052 
9053 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9054    abbrev_id smaller than this, because they must be already sized
9055    during build_abbrev_table).  */
9056 static unsigned int abbrev_opt_base_type_end;
9057 
9058 /* Vector of usage counts during build_abbrev_table.  Indexed by
9059    abbrev_id - abbrev_opt_start.  */
9060 static vec<unsigned int> abbrev_usage_count;
9061 
9062 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start.  */
9063 static vec<dw_die_ref> sorted_abbrev_dies;
9064 
9065 /* The format of each DIE (and its attribute value pairs) is encoded in an
9066    abbreviation table.  This routine builds the abbreviation table and assigns
9067    a unique abbreviation id for each abbreviation entry.  The children of each
9068    die are visited recursively.  */
9069 
9070 static void
build_abbrev_table(dw_die_ref die,external_ref_hash_type * extern_map)9071 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9072 {
9073   unsigned int abbrev_id = 0;
9074   dw_die_ref c;
9075   dw_attr_node *a;
9076   unsigned ix;
9077   dw_die_ref abbrev;
9078 
9079   /* Scan the DIE references, and replace any that refer to
9080      DIEs from other CUs (i.e. those which are not marked) with
9081      the local stubs we built in optimize_external_refs.  */
9082   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9083     if (AT_class (a) == dw_val_class_die_ref
9084 	&& (c = AT_ref (a))->die_mark == 0)
9085       {
9086 	struct external_ref *ref_p;
9087 	gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9088 
9089 	if (is_type_die (c)
9090 	    && (ref_p = lookup_external_ref (extern_map, c))
9091 	    && ref_p->stub && ref_p->stub != die)
9092 	  {
9093 	    gcc_assert (a->dw_attr != DW_AT_signature);
9094 	    change_AT_die_ref (a, ref_p->stub);
9095 	  }
9096 	else
9097 	  /* We aren't changing this reference, so mark it external.  */
9098 	  set_AT_ref_external (a, 1);
9099       }
9100 
9101   FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9102     {
9103       dw_attr_node *die_a, *abbrev_a;
9104       unsigned ix;
9105       bool ok = true;
9106 
9107       if (abbrev_id == 0)
9108 	continue;
9109       if (abbrev->die_tag != die->die_tag)
9110 	continue;
9111       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9112 	continue;
9113 
9114       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9115 	continue;
9116 
9117       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9118 	{
9119 	  abbrev_a = &(*abbrev->die_attr)[ix];
9120 	  if ((abbrev_a->dw_attr != die_a->dw_attr)
9121 	      || (value_format (abbrev_a) != value_format (die_a)))
9122 	    {
9123 	      ok = false;
9124 	      break;
9125 	    }
9126 	}
9127       if (ok)
9128 	break;
9129     }
9130 
9131   if (abbrev_id >= vec_safe_length (abbrev_die_table))
9132     {
9133       vec_safe_push (abbrev_die_table, die);
9134       if (abbrev_opt_start)
9135 	abbrev_usage_count.safe_push (0);
9136     }
9137   if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9138     {
9139       abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9140       sorted_abbrev_dies.safe_push (die);
9141     }
9142 
9143   die->die_abbrev = abbrev_id;
9144   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9145 }
9146 
9147 /* Callback function for sorted_abbrev_dies vector sorting.  We sort
9148    by die_abbrev's usage count, from the most commonly used
9149    abbreviation to the least.  */
9150 
9151 static int
die_abbrev_cmp(const void * p1,const void * p2)9152 die_abbrev_cmp (const void *p1, const void *p2)
9153 {
9154   dw_die_ref die1 = *(const dw_die_ref *) p1;
9155   dw_die_ref die2 = *(const dw_die_ref *) p2;
9156 
9157   gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9158   gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9159 
9160   if (die1->die_abbrev >= abbrev_opt_base_type_end
9161       && die2->die_abbrev >= abbrev_opt_base_type_end)
9162     {
9163       if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9164 	  > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9165 	return -1;
9166       if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9167 	  < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9168 	return 1;
9169     }
9170 
9171   /* Stabilize the sort.  */
9172   if (die1->die_abbrev < die2->die_abbrev)
9173     return -1;
9174   if (die1->die_abbrev > die2->die_abbrev)
9175     return 1;
9176 
9177   return 0;
9178 }
9179 
9180 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9181    of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9182    into dw_val_class_const_implicit or
9183    dw_val_class_unsigned_const_implicit.  */
9184 
9185 static void
optimize_implicit_const(unsigned int first_id,unsigned int end,vec<bool> & implicit_consts)9186 optimize_implicit_const (unsigned int first_id, unsigned int end,
9187 			 vec<bool> &implicit_consts)
9188 {
9189   /* It never makes sense if there is just one DIE using the abbreviation.  */
9190   if (end < first_id + 2)
9191     return;
9192 
9193   dw_attr_node *a;
9194   unsigned ix, i;
9195   dw_die_ref die = sorted_abbrev_dies[first_id];
9196   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9197     if (implicit_consts[ix])
9198       {
9199 	enum dw_val_class new_class = dw_val_class_none;
9200 	switch (AT_class (a))
9201 	  {
9202 	  case dw_val_class_unsigned_const:
9203 	    if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9204 	      continue;
9205 
9206 	    /* The .debug_abbrev section will grow by
9207 	       size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9208 	       in all the DIEs using that abbreviation.  */
9209 	    if (constant_size (AT_unsigned (a)) * (end - first_id)
9210 		<= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9211 	      continue;
9212 
9213 	    new_class = dw_val_class_unsigned_const_implicit;
9214 	    break;
9215 
9216 	  case dw_val_class_const:
9217 	    new_class = dw_val_class_const_implicit;
9218 	    break;
9219 
9220 	  case dw_val_class_file:
9221 	    new_class = dw_val_class_file_implicit;
9222 	    break;
9223 
9224 	  default:
9225 	    continue;
9226 	  }
9227 	for (i = first_id; i < end; i++)
9228 	  (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9229 	    = new_class;
9230       }
9231 }
9232 
9233 /* Attempt to optimize abbreviation table from abbrev_opt_start
9234    abbreviation above.  */
9235 
9236 static void
optimize_abbrev_table(void)9237 optimize_abbrev_table (void)
9238 {
9239   if (abbrev_opt_start
9240       && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9241       && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9242     {
9243       auto_vec<bool, 32> implicit_consts;
9244       sorted_abbrev_dies.qsort (die_abbrev_cmp);
9245 
9246       unsigned int abbrev_id = abbrev_opt_start - 1;
9247       unsigned int first_id = ~0U;
9248       unsigned int last_abbrev_id = 0;
9249       unsigned int i;
9250       dw_die_ref die;
9251       if (abbrev_opt_base_type_end > abbrev_opt_start)
9252 	abbrev_id = abbrev_opt_base_type_end - 1;
9253       /* Reassign abbreviation ids from abbrev_opt_start above, so that
9254 	 most commonly used abbreviations come first.  */
9255       FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9256 	{
9257 	  dw_attr_node *a;
9258 	  unsigned ix;
9259 
9260 	  /* If calc_base_type_die_sizes has been called, the CU and
9261 	     base types after it can't be optimized, because we've already
9262 	     calculated their DIE offsets.  We've sorted them first.  */
9263 	  if (die->die_abbrev < abbrev_opt_base_type_end)
9264 	    continue;
9265 	  if (die->die_abbrev != last_abbrev_id)
9266 	    {
9267 	      last_abbrev_id = die->die_abbrev;
9268 	      if (dwarf_version >= 5 && first_id != ~0U)
9269 		optimize_implicit_const (first_id, i, implicit_consts);
9270 	      abbrev_id++;
9271 	      (*abbrev_die_table)[abbrev_id] = die;
9272 	      if (dwarf_version >= 5)
9273 		{
9274 		  first_id = i;
9275 		  implicit_consts.truncate (0);
9276 
9277 		  FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9278 		    switch (AT_class (a))
9279 		      {
9280 		      case dw_val_class_const:
9281 		      case dw_val_class_unsigned_const:
9282 		      case dw_val_class_file:
9283 			implicit_consts.safe_push (true);
9284 			break;
9285 		      default:
9286 			implicit_consts.safe_push (false);
9287 			break;
9288 		      }
9289 		}
9290 	    }
9291 	  else if (dwarf_version >= 5)
9292 	    {
9293 	      FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9294 		if (!implicit_consts[ix])
9295 		  continue;
9296 		else
9297 		  {
9298 		    dw_attr_node *other_a
9299 		      = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9300 		    if (!dw_val_equal_p (&a->dw_attr_val,
9301 					 &other_a->dw_attr_val))
9302 		      implicit_consts[ix] = false;
9303 		  }
9304 	    }
9305 	  die->die_abbrev = abbrev_id;
9306 	}
9307       gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9308       if (dwarf_version >= 5 && first_id != ~0U)
9309 	optimize_implicit_const (first_id, i, implicit_consts);
9310     }
9311 
9312   abbrev_opt_start = 0;
9313   abbrev_opt_base_type_end = 0;
9314   abbrev_usage_count.release ();
9315   sorted_abbrev_dies.release ();
9316 }
9317 
9318 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9319 
9320 static int
constant_size(unsigned HOST_WIDE_INT value)9321 constant_size (unsigned HOST_WIDE_INT value)
9322 {
9323   int log;
9324 
9325   if (value == 0)
9326     log = 0;
9327   else
9328     log = floor_log2 (value);
9329 
9330   log = log / 8;
9331   log = 1 << (floor_log2 (log) + 1);
9332 
9333   return log;
9334 }
9335 
9336 /* Return the size of a DIE as it is represented in the
9337    .debug_info section.  */
9338 
9339 static unsigned long
size_of_die(dw_die_ref die)9340 size_of_die (dw_die_ref die)
9341 {
9342   unsigned long size = 0;
9343   dw_attr_node *a;
9344   unsigned ix;
9345   enum dwarf_form form;
9346 
9347   size += size_of_uleb128 (die->die_abbrev);
9348   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9349     {
9350       switch (AT_class (a))
9351 	{
9352 	case dw_val_class_addr:
9353           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9354             {
9355               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9356               size += size_of_uleb128 (AT_index (a));
9357             }
9358           else
9359             size += DWARF2_ADDR_SIZE;
9360 	  break;
9361 	case dw_val_class_offset:
9362 	  size += DWARF_OFFSET_SIZE;
9363 	  break;
9364 	case dw_val_class_loc:
9365 	  {
9366 	    unsigned long lsize = size_of_locs (AT_loc (a));
9367 
9368 	    /* Block length.  */
9369 	    if (dwarf_version >= 4)
9370 	      size += size_of_uleb128 (lsize);
9371 	    else
9372 	      size += constant_size (lsize);
9373 	    size += lsize;
9374 	  }
9375 	  break;
9376 	case dw_val_class_loc_list:
9377 	  if (dwarf_split_debug_info && dwarf_version >= 5)
9378 	    {
9379 	      gcc_assert (AT_loc_list (a)->num_assigned);
9380 	      size += size_of_uleb128 (AT_loc_list (a)->hash);
9381 	    }
9382           else
9383             size += DWARF_OFFSET_SIZE;
9384 	  break;
9385 	case dw_val_class_view_list:
9386 	  size += DWARF_OFFSET_SIZE;
9387 	  break;
9388 	case dw_val_class_range_list:
9389 	  if (value_format (a) == DW_FORM_rnglistx)
9390 	    {
9391 	      gcc_assert (rnglist_idx);
9392 	      dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9393 	      size += size_of_uleb128 (r->idx);
9394 	    }
9395 	  else
9396 	    size += DWARF_OFFSET_SIZE;
9397 	  break;
9398 	case dw_val_class_const:
9399 	  size += size_of_sleb128 (AT_int (a));
9400 	  break;
9401 	case dw_val_class_unsigned_const:
9402 	  {
9403 	    int csize = constant_size (AT_unsigned (a));
9404 	    if (dwarf_version == 3
9405 		&& a->dw_attr == DW_AT_data_member_location
9406 		&& csize >= 4)
9407 	      size += size_of_uleb128 (AT_unsigned (a));
9408 	    else
9409 	      size += csize;
9410 	  }
9411 	  break;
9412 	case dw_val_class_symview:
9413 	  if (symview_upper_bound <= 0xff)
9414 	    size += 1;
9415 	  else if (symview_upper_bound <= 0xffff)
9416 	    size += 2;
9417 	  else if (symview_upper_bound <= 0xffffffff)
9418 	    size += 4;
9419 	  else
9420 	    size += 8;
9421 	  break;
9422 	case dw_val_class_const_implicit:
9423 	case dw_val_class_unsigned_const_implicit:
9424 	case dw_val_class_file_implicit:
9425 	  /* These occupy no size in the DIE, just an extra sleb128 in
9426 	     .debug_abbrev.  */
9427 	  break;
9428 	case dw_val_class_const_double:
9429 	  size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9430 	  if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9431 	    size++; /* block */
9432 	  break;
9433 	case dw_val_class_wide_int:
9434 	  size += (get_full_len (*a->dw_attr_val.v.val_wide)
9435 		   * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9436 	  if (get_full_len (*a->dw_attr_val.v.val_wide)
9437 	      * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9438 	    size++; /* block */
9439 	  break;
9440 	case dw_val_class_vec:
9441 	  size += constant_size (a->dw_attr_val.v.val_vec.length
9442 				 * a->dw_attr_val.v.val_vec.elt_size)
9443 		  + a->dw_attr_val.v.val_vec.length
9444 		    * a->dw_attr_val.v.val_vec.elt_size; /* block */
9445 	  break;
9446 	case dw_val_class_flag:
9447 	  if (dwarf_version >= 4)
9448 	    /* Currently all add_AT_flag calls pass in 1 as last argument,
9449 	       so DW_FORM_flag_present can be used.  If that ever changes,
9450 	       we'll need to use DW_FORM_flag and have some optimization
9451 	       in build_abbrev_table that will change those to
9452 	       DW_FORM_flag_present if it is set to 1 in all DIEs using
9453 	       the same abbrev entry.  */
9454 	    gcc_assert (a->dw_attr_val.v.val_flag == 1);
9455 	  else
9456 	    size += 1;
9457 	  break;
9458 	case dw_val_class_die_ref:
9459 	  if (AT_ref_external (a))
9460 	    {
9461 	      /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9462 		 we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9463 		 is sized by target address length, whereas in DWARF3
9464 		 it's always sized as an offset.  */
9465 	      if (AT_ref (a)->comdat_type_p)
9466 		size += DWARF_TYPE_SIGNATURE_SIZE;
9467 	      else if (dwarf_version == 2)
9468 		size += DWARF2_ADDR_SIZE;
9469 	      else
9470 		size += DWARF_OFFSET_SIZE;
9471 	    }
9472 	  else
9473 	    size += DWARF_OFFSET_SIZE;
9474 	  break;
9475 	case dw_val_class_fde_ref:
9476 	  size += DWARF_OFFSET_SIZE;
9477 	  break;
9478 	case dw_val_class_lbl_id:
9479           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9480             {
9481               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9482               size += size_of_uleb128 (AT_index (a));
9483             }
9484           else
9485             size += DWARF2_ADDR_SIZE;
9486 	  break;
9487 	case dw_val_class_lineptr:
9488 	case dw_val_class_macptr:
9489 	case dw_val_class_loclistsptr:
9490 	  size += DWARF_OFFSET_SIZE;
9491 	  break;
9492 	case dw_val_class_str:
9493           form = AT_string_form (a);
9494 	  if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9495 	    size += DWARF_OFFSET_SIZE;
9496 	  else if (form == dwarf_FORM (DW_FORM_strx))
9497 	    size += size_of_uleb128 (AT_index (a));
9498 	  else
9499 	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9500 	  break;
9501 	case dw_val_class_file:
9502 	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9503 	  break;
9504 	case dw_val_class_data8:
9505 	  size += 8;
9506 	  break;
9507 	case dw_val_class_vms_delta:
9508 	  size += DWARF_OFFSET_SIZE;
9509 	  break;
9510 	case dw_val_class_high_pc:
9511 	  size += DWARF2_ADDR_SIZE;
9512 	  break;
9513 	case dw_val_class_discr_value:
9514 	  size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9515 	  break;
9516 	case dw_val_class_discr_list:
9517 	    {
9518 	      unsigned block_size = size_of_discr_list (AT_discr_list (a));
9519 
9520 	      /* This is a block, so we have the block length and then its
9521 		 data.  */
9522 	      size += constant_size (block_size) + block_size;
9523 	    }
9524 	  break;
9525 	default:
9526 	  gcc_unreachable ();
9527 	}
9528     }
9529 
9530   return size;
9531 }
9532 
9533 /* Size the debugging information associated with a given DIE.  Visits the
9534    DIE's children recursively.  Updates the global variable next_die_offset, on
9535    each time through.  Uses the current value of next_die_offset to update the
9536    die_offset field in each DIE.  */
9537 
9538 static void
calc_die_sizes(dw_die_ref die)9539 calc_die_sizes (dw_die_ref die)
9540 {
9541   dw_die_ref c;
9542 
9543   gcc_assert (die->die_offset == 0
9544 	      || (unsigned long int) die->die_offset == next_die_offset);
9545   die->die_offset = next_die_offset;
9546   next_die_offset += size_of_die (die);
9547 
9548   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9549 
9550   if (die->die_child != NULL)
9551     /* Count the null byte used to terminate sibling lists.  */
9552     next_die_offset += 1;
9553 }
9554 
9555 /* Size just the base type children at the start of the CU.
9556    This is needed because build_abbrev needs to size locs
9557    and sizing of type based stack ops needs to know die_offset
9558    values for the base types.  */
9559 
9560 static void
calc_base_type_die_sizes(void)9561 calc_base_type_die_sizes (void)
9562 {
9563   unsigned long die_offset = (dwarf_split_debug_info
9564 			      ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9565 			      : DWARF_COMPILE_UNIT_HEADER_SIZE);
9566   unsigned int i;
9567   dw_die_ref base_type;
9568 #if ENABLE_ASSERT_CHECKING
9569   dw_die_ref prev = comp_unit_die ()->die_child;
9570 #endif
9571 
9572   die_offset += size_of_die (comp_unit_die ());
9573   for (i = 0; base_types.iterate (i, &base_type); i++)
9574     {
9575 #if ENABLE_ASSERT_CHECKING
9576       gcc_assert (base_type->die_offset == 0
9577 		  && prev->die_sib == base_type
9578 		  && base_type->die_child == NULL
9579 		  && base_type->die_abbrev);
9580       prev = base_type;
9581 #endif
9582       if (abbrev_opt_start
9583 	  && base_type->die_abbrev >= abbrev_opt_base_type_end)
9584 	abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9585       base_type->die_offset = die_offset;
9586       die_offset += size_of_die (base_type);
9587     }
9588 }
9589 
9590 /* Set the marks for a die and its children.  We do this so
9591    that we know whether or not a reference needs to use FORM_ref_addr; only
9592    DIEs in the same CU will be marked.  We used to clear out the offset
9593    and use that as the flag, but ran into ordering problems.  */
9594 
9595 static void
mark_dies(dw_die_ref die)9596 mark_dies (dw_die_ref die)
9597 {
9598   dw_die_ref c;
9599 
9600   gcc_assert (!die->die_mark);
9601 
9602   die->die_mark = 1;
9603   FOR_EACH_CHILD (die, c, mark_dies (c));
9604 }
9605 
9606 /* Clear the marks for a die and its children.  */
9607 
9608 static void
unmark_dies(dw_die_ref die)9609 unmark_dies (dw_die_ref die)
9610 {
9611   dw_die_ref c;
9612 
9613   if (! use_debug_types)
9614     gcc_assert (die->die_mark);
9615 
9616   die->die_mark = 0;
9617   FOR_EACH_CHILD (die, c, unmark_dies (c));
9618 }
9619 
9620 /* Clear the marks for a die, its children and referred dies.  */
9621 
9622 static void
unmark_all_dies(dw_die_ref die)9623 unmark_all_dies (dw_die_ref die)
9624 {
9625   dw_die_ref c;
9626   dw_attr_node *a;
9627   unsigned ix;
9628 
9629   if (!die->die_mark)
9630     return;
9631   die->die_mark = 0;
9632 
9633   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9634 
9635   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9636     if (AT_class (a) == dw_val_class_die_ref)
9637       unmark_all_dies (AT_ref (a));
9638 }
9639 
9640 /* Calculate if the entry should appear in the final output file.  It may be
9641    from a pruned a type.  */
9642 
9643 static bool
include_pubname_in_output(vec<pubname_entry,va_gc> * table,pubname_entry * p)9644 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9645 {
9646   /* By limiting gnu pubnames to definitions only, gold can generate a
9647      gdb index without entries for declarations, which don't include
9648      enough information to be useful.  */
9649   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9650     return false;
9651 
9652   if (table == pubname_table)
9653     {
9654       /* Enumerator names are part of the pubname table, but the
9655          parent DW_TAG_enumeration_type die may have been pruned.
9656          Don't output them if that is the case.  */
9657       if (p->die->die_tag == DW_TAG_enumerator &&
9658           (p->die->die_parent == NULL
9659            || !p->die->die_parent->die_perennial_p))
9660         return false;
9661 
9662       /* Everything else in the pubname table is included.  */
9663       return true;
9664     }
9665 
9666   /* The pubtypes table shouldn't include types that have been
9667      pruned.  */
9668   return (p->die->die_offset != 0
9669           || !flag_eliminate_unused_debug_types);
9670 }
9671 
9672 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9673    generated for the compilation unit.  */
9674 
9675 static unsigned long
size_of_pubnames(vec<pubname_entry,va_gc> * names)9676 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9677 {
9678   unsigned long size;
9679   unsigned i;
9680   pubname_entry *p;
9681   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9682 
9683   size = DWARF_PUBNAMES_HEADER_SIZE;
9684   FOR_EACH_VEC_ELT (*names, i, p)
9685     if (include_pubname_in_output (names, p))
9686       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9687 
9688   size += DWARF_OFFSET_SIZE;
9689   return size;
9690 }
9691 
9692 /* Return the size of the information in the .debug_aranges section.  */
9693 
9694 static unsigned long
size_of_aranges(void)9695 size_of_aranges (void)
9696 {
9697   unsigned long size;
9698 
9699   size = DWARF_ARANGES_HEADER_SIZE;
9700 
9701   /* Count the address/length pair for this compilation unit.  */
9702   if (text_section_used)
9703     size += 2 * DWARF2_ADDR_SIZE;
9704   if (cold_text_section_used)
9705     size += 2 * DWARF2_ADDR_SIZE;
9706   if (have_multiple_function_sections)
9707     {
9708       unsigned fde_idx;
9709       dw_fde_ref fde;
9710 
9711       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9712 	{
9713 	  if (DECL_IGNORED_P (fde->decl))
9714 	    continue;
9715 	  if (!fde->in_std_section)
9716 	    size += 2 * DWARF2_ADDR_SIZE;
9717 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9718 	    size += 2 * DWARF2_ADDR_SIZE;
9719 	}
9720     }
9721 
9722   /* Count the two zero words used to terminated the address range table.  */
9723   size += 2 * DWARF2_ADDR_SIZE;
9724   return size;
9725 }
9726 
9727 /* Select the encoding of an attribute value.  */
9728 
9729 static enum dwarf_form
value_format(dw_attr_node * a)9730 value_format (dw_attr_node *a)
9731 {
9732   switch (AT_class (a))
9733     {
9734     case dw_val_class_addr:
9735       /* Only very few attributes allow DW_FORM_addr.  */
9736       switch (a->dw_attr)
9737 	{
9738 	case DW_AT_low_pc:
9739 	case DW_AT_high_pc:
9740 	case DW_AT_entry_pc:
9741 	case DW_AT_trampoline:
9742           return (AT_index (a) == NOT_INDEXED
9743                   ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9744 	default:
9745 	  break;
9746 	}
9747       switch (DWARF2_ADDR_SIZE)
9748 	{
9749 	case 1:
9750 	  return DW_FORM_data1;
9751 	case 2:
9752 	  return DW_FORM_data2;
9753 	case 4:
9754 	  return DW_FORM_data4;
9755 	case 8:
9756 	  return DW_FORM_data8;
9757 	default:
9758 	  gcc_unreachable ();
9759 	}
9760     case dw_val_class_loc_list:
9761       if (dwarf_split_debug_info
9762 	  && dwarf_version >= 5
9763 	  && AT_loc_list (a)->num_assigned)
9764 	return DW_FORM_loclistx;
9765       /* FALLTHRU */
9766     case dw_val_class_view_list:
9767     case dw_val_class_range_list:
9768       /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9769 	 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9770 	 care about sizes of .debug* sections in shared libraries and
9771 	 executables and don't take into account relocations that affect just
9772 	 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9773 	 table in the .debug_rnglists section.  */
9774       if (dwarf_split_debug_info
9775 	  && dwarf_version >= 5
9776 	  && AT_class (a) == dw_val_class_range_list
9777 	  && rnglist_idx
9778 	  && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9779 	return DW_FORM_rnglistx;
9780       if (dwarf_version >= 4)
9781 	return DW_FORM_sec_offset;
9782       /* FALLTHRU */
9783     case dw_val_class_vms_delta:
9784     case dw_val_class_offset:
9785       switch (DWARF_OFFSET_SIZE)
9786 	{
9787 	case 4:
9788 	  return DW_FORM_data4;
9789 	case 8:
9790 	  return DW_FORM_data8;
9791 	default:
9792 	  gcc_unreachable ();
9793 	}
9794     case dw_val_class_loc:
9795       if (dwarf_version >= 4)
9796 	return DW_FORM_exprloc;
9797       switch (constant_size (size_of_locs (AT_loc (a))))
9798 	{
9799 	case 1:
9800 	  return DW_FORM_block1;
9801 	case 2:
9802 	  return DW_FORM_block2;
9803 	case 4:
9804 	  return DW_FORM_block4;
9805 	default:
9806 	  gcc_unreachable ();
9807 	}
9808     case dw_val_class_const:
9809       return DW_FORM_sdata;
9810     case dw_val_class_unsigned_const:
9811       switch (constant_size (AT_unsigned (a)))
9812 	{
9813 	case 1:
9814 	  return DW_FORM_data1;
9815 	case 2:
9816 	  return DW_FORM_data2;
9817 	case 4:
9818 	  /* In DWARF3 DW_AT_data_member_location with
9819 	     DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9820 	     constant, so we need to use DW_FORM_udata if we need
9821 	     a large constant.  */
9822 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9823 	    return DW_FORM_udata;
9824 	  return DW_FORM_data4;
9825 	case 8:
9826 	  if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9827 	    return DW_FORM_udata;
9828 	  return DW_FORM_data8;
9829 	default:
9830 	  gcc_unreachable ();
9831 	}
9832     case dw_val_class_const_implicit:
9833     case dw_val_class_unsigned_const_implicit:
9834     case dw_val_class_file_implicit:
9835       return DW_FORM_implicit_const;
9836     case dw_val_class_const_double:
9837       switch (HOST_BITS_PER_WIDE_INT)
9838 	{
9839 	case 8:
9840 	  return DW_FORM_data2;
9841 	case 16:
9842 	  return DW_FORM_data4;
9843 	case 32:
9844 	  return DW_FORM_data8;
9845 	case 64:
9846 	  if (dwarf_version >= 5)
9847 	    return DW_FORM_data16;
9848 	  /* FALLTHRU */
9849 	default:
9850 	  return DW_FORM_block1;
9851 	}
9852     case dw_val_class_wide_int:
9853       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9854 	{
9855 	case 8:
9856 	  return DW_FORM_data1;
9857 	case 16:
9858 	  return DW_FORM_data2;
9859 	case 32:
9860 	  return DW_FORM_data4;
9861 	case 64:
9862 	  return DW_FORM_data8;
9863 	case 128:
9864 	  if (dwarf_version >= 5)
9865 	    return DW_FORM_data16;
9866 	  /* FALLTHRU */
9867 	default:
9868 	  return DW_FORM_block1;
9869 	}
9870     case dw_val_class_symview:
9871       /* ??? We might use uleb128, but then we'd have to compute
9872 	 .debug_info offsets in the assembler.  */
9873       if (symview_upper_bound <= 0xff)
9874 	return DW_FORM_data1;
9875       else if (symview_upper_bound <= 0xffff)
9876 	return DW_FORM_data2;
9877       else if (symview_upper_bound <= 0xffffffff)
9878 	return DW_FORM_data4;
9879       else
9880 	return DW_FORM_data8;
9881     case dw_val_class_vec:
9882       switch (constant_size (a->dw_attr_val.v.val_vec.length
9883 			     * a->dw_attr_val.v.val_vec.elt_size))
9884 	{
9885 	case 1:
9886 	  return DW_FORM_block1;
9887 	case 2:
9888 	  return DW_FORM_block2;
9889 	case 4:
9890 	  return DW_FORM_block4;
9891 	default:
9892 	  gcc_unreachable ();
9893 	}
9894     case dw_val_class_flag:
9895       if (dwarf_version >= 4)
9896 	{
9897 	  /* Currently all add_AT_flag calls pass in 1 as last argument,
9898 	     so DW_FORM_flag_present can be used.  If that ever changes,
9899 	     we'll need to use DW_FORM_flag and have some optimization
9900 	     in build_abbrev_table that will change those to
9901 	     DW_FORM_flag_present if it is set to 1 in all DIEs using
9902 	     the same abbrev entry.  */
9903 	  gcc_assert (a->dw_attr_val.v.val_flag == 1);
9904 	  return DW_FORM_flag_present;
9905 	}
9906       return DW_FORM_flag;
9907     case dw_val_class_die_ref:
9908       if (AT_ref_external (a))
9909 	{
9910 	  if (AT_ref (a)->comdat_type_p)
9911 	    return DW_FORM_ref_sig8;
9912 	  else
9913 	    return DW_FORM_ref_addr;
9914 	}
9915       else
9916 	return DW_FORM_ref;
9917     case dw_val_class_fde_ref:
9918       return DW_FORM_data;
9919     case dw_val_class_lbl_id:
9920       return (AT_index (a) == NOT_INDEXED
9921               ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9922     case dw_val_class_lineptr:
9923     case dw_val_class_macptr:
9924     case dw_val_class_loclistsptr:
9925       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9926     case dw_val_class_str:
9927       return AT_string_form (a);
9928     case dw_val_class_file:
9929       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9930 	{
9931 	case 1:
9932 	  return DW_FORM_data1;
9933 	case 2:
9934 	  return DW_FORM_data2;
9935 	case 4:
9936 	  return DW_FORM_data4;
9937 	default:
9938 	  gcc_unreachable ();
9939 	}
9940 
9941     case dw_val_class_data8:
9942       return DW_FORM_data8;
9943 
9944     case dw_val_class_high_pc:
9945       switch (DWARF2_ADDR_SIZE)
9946 	{
9947 	case 1:
9948 	  return DW_FORM_data1;
9949 	case 2:
9950 	  return DW_FORM_data2;
9951 	case 4:
9952 	  return DW_FORM_data4;
9953 	case 8:
9954 	  return DW_FORM_data8;
9955 	default:
9956 	  gcc_unreachable ();
9957 	}
9958 
9959     case dw_val_class_discr_value:
9960       return (a->dw_attr_val.v.val_discr_value.pos
9961 	      ? DW_FORM_udata
9962 	      : DW_FORM_sdata);
9963     case dw_val_class_discr_list:
9964       switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9965 	{
9966 	case 1:
9967 	  return DW_FORM_block1;
9968 	case 2:
9969 	  return DW_FORM_block2;
9970 	case 4:
9971 	  return DW_FORM_block4;
9972 	default:
9973 	  gcc_unreachable ();
9974 	}
9975 
9976     default:
9977       gcc_unreachable ();
9978     }
9979 }
9980 
9981 /* Output the encoding of an attribute value.  */
9982 
9983 static void
output_value_format(dw_attr_node * a)9984 output_value_format (dw_attr_node *a)
9985 {
9986   enum dwarf_form form = value_format (a);
9987 
9988   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9989 }
9990 
9991 /* Given a die and id, produce the appropriate abbreviations.  */
9992 
9993 static void
output_die_abbrevs(unsigned long abbrev_id,dw_die_ref abbrev)9994 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9995 {
9996   unsigned ix;
9997   dw_attr_node *a_attr;
9998 
9999   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10000   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10001                                dwarf_tag_name (abbrev->die_tag));
10002 
10003   if (abbrev->die_child != NULL)
10004     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10005   else
10006     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10007 
10008   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10009     {
10010       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10011                                    dwarf_attr_name (a_attr->dw_attr));
10012       output_value_format (a_attr);
10013       if (value_format (a_attr) == DW_FORM_implicit_const)
10014 	{
10015 	  if (AT_class (a_attr) == dw_val_class_file_implicit)
10016 	    {
10017 	      int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10018 	      const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10019 	      dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10020 	    }
10021 	  else
10022 	    dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10023 	}
10024     }
10025 
10026   dw2_asm_output_data (1, 0, NULL);
10027   dw2_asm_output_data (1, 0, NULL);
10028 }
10029 
10030 
10031 /* Output the .debug_abbrev section which defines the DIE abbreviation
10032    table.  */
10033 
10034 static void
output_abbrev_section(void)10035 output_abbrev_section (void)
10036 {
10037   unsigned int abbrev_id;
10038   dw_die_ref abbrev;
10039 
10040   FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10041     if (abbrev_id != 0)
10042       output_die_abbrevs (abbrev_id, abbrev);
10043 
10044   /* Terminate the table.  */
10045   dw2_asm_output_data (1, 0, NULL);
10046 }
10047 
10048 /* Return a new location list, given the begin and end range, and the
10049    expression.  */
10050 
10051 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)10052 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10053 	      const char *end, var_loc_view vend,
10054 	      const char *section)
10055 {
10056   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10057 
10058   retlist->begin = begin;
10059   retlist->begin_entry = NULL;
10060   retlist->end = end;
10061   retlist->expr = expr;
10062   retlist->section = section;
10063   retlist->vbegin = vbegin;
10064   retlist->vend = vend;
10065 
10066   return retlist;
10067 }
10068 
10069 /* Return true iff there's any nonzero view number in the loc list.
10070 
10071    ??? When views are not enabled, we'll often extend a single range
10072    to the entire function, so that we emit a single location
10073    expression rather than a location list.  With views, even with a
10074    single range, we'll output a list if start or end have a nonzero
10075    view.  If we change this, we may want to stop splitting a single
10076    range in dw_loc_list just because of a nonzero view, even if it
10077    straddles across hot/cold partitions.  */
10078 
10079 static bool
loc_list_has_views(dw_loc_list_ref list)10080 loc_list_has_views (dw_loc_list_ref list)
10081 {
10082   if (!debug_variable_location_views)
10083     return false;
10084 
10085   for (dw_loc_list_ref loc = list;
10086        loc != NULL; loc = loc->dw_loc_next)
10087     if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10088       return true;
10089 
10090   return false;
10091 }
10092 
10093 /* Generate a new internal symbol for this location list node, if it
10094    hasn't got one yet.  */
10095 
10096 static inline void
gen_llsym(dw_loc_list_ref list)10097 gen_llsym (dw_loc_list_ref list)
10098 {
10099   gcc_assert (!list->ll_symbol);
10100   list->ll_symbol = gen_internal_sym ("LLST");
10101 
10102   if (!loc_list_has_views (list))
10103     return;
10104 
10105   if (dwarf2out_locviews_in_attribute ())
10106     {
10107       /* Use the same label_num for the view list.  */
10108       label_num--;
10109       list->vl_symbol = gen_internal_sym ("LVUS");
10110     }
10111   else
10112     list->vl_symbol = list->ll_symbol;
10113 }
10114 
10115 /* Generate a symbol for the list, but only if we really want to emit
10116    it as a list.  */
10117 
10118 static inline void
maybe_gen_llsym(dw_loc_list_ref list)10119 maybe_gen_llsym (dw_loc_list_ref list)
10120 {
10121   if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10122     return;
10123 
10124   gen_llsym (list);
10125 }
10126 
10127 /* Determine whether or not to skip loc_list entry CURR.  If SIZEP is
10128    NULL, don't consider size of the location expression.  If we're not
10129    to skip it, and SIZEP is non-null, store the size of CURR->expr's
10130    representation in *SIZEP.  */
10131 
10132 static bool
10133 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10134 {
10135   /* Don't output an entry that starts and ends at the same address.  */
10136   if (strcmp (curr->begin, curr->end) == 0
10137       && curr->vbegin == curr->vend && !curr->force)
10138     return true;
10139 
10140   if (!sizep)
10141     return false;
10142 
10143   unsigned long size = size_of_locs (curr->expr);
10144 
10145   /* If the expression is too large, drop it on the floor.  We could
10146      perhaps put it into DW_TAG_dwarf_procedure and refer to that
10147      in the expression, but >= 64KB expressions for a single value
10148      in a single range are unlikely very useful.  */
10149   if (dwarf_version < 5 && size > 0xffff)
10150     return true;
10151 
10152   *sizep = size;
10153 
10154   return false;
10155 }
10156 
10157 /* Output a view pair loclist entry for CURR, if it requires one.  */
10158 
10159 static void
dwarf2out_maybe_output_loclist_view_pair(dw_loc_list_ref curr)10160 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10161 {
10162   if (!dwarf2out_locviews_in_loclist ())
10163     return;
10164 
10165   if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10166     return;
10167 
10168 #ifdef DW_LLE_view_pair
10169   dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10170 
10171   if (dwarf2out_as_locview_support)
10172     {
10173       if (ZERO_VIEW_P (curr->vbegin))
10174 	dw2_asm_output_data_uleb128 (0, "Location view begin");
10175       else
10176 	{
10177 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
10178 	  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10179 	  dw2_asm_output_symname_uleb128 (label, "Location view begin");
10180 	}
10181 
10182       if (ZERO_VIEW_P (curr->vend))
10183 	dw2_asm_output_data_uleb128 (0, "Location view end");
10184       else
10185 	{
10186 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
10187 	  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10188 	  dw2_asm_output_symname_uleb128 (label, "Location view end");
10189 	}
10190     }
10191   else
10192     {
10193       dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10194       dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10195     }
10196 #endif /* DW_LLE_view_pair */
10197 
10198   return;
10199 }
10200 
10201 /* Output the location list given to us.  */
10202 
10203 static void
output_loc_list(dw_loc_list_ref list_head)10204 output_loc_list (dw_loc_list_ref list_head)
10205 {
10206   int vcount = 0, lcount = 0;
10207 
10208   if (list_head->emitted)
10209     return;
10210   list_head->emitted = true;
10211 
10212   if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10213     {
10214       ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10215 
10216       for (dw_loc_list_ref curr = list_head; curr != NULL;
10217 	   curr = curr->dw_loc_next)
10218 	{
10219 	  unsigned long size;
10220 
10221 	  if (skip_loc_list_entry (curr, &size))
10222 	    continue;
10223 
10224 	  vcount++;
10225 
10226 	  /* ?? dwarf_split_debug_info?  */
10227 	  if (dwarf2out_as_locview_support)
10228 	    {
10229 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
10230 
10231 	      if (!ZERO_VIEW_P (curr->vbegin))
10232 		{
10233 		  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10234 		  dw2_asm_output_symname_uleb128 (label,
10235 						  "View list begin (%s)",
10236 						  list_head->vl_symbol);
10237 		}
10238 	      else
10239 		dw2_asm_output_data_uleb128 (0,
10240 					     "View list begin (%s)",
10241 					     list_head->vl_symbol);
10242 
10243 	      if (!ZERO_VIEW_P (curr->vend))
10244 		{
10245 		  ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10246 		  dw2_asm_output_symname_uleb128 (label,
10247 						  "View list end (%s)",
10248 						  list_head->vl_symbol);
10249 		}
10250 	      else
10251 		dw2_asm_output_data_uleb128 (0,
10252 					     "View list end (%s)",
10253 					     list_head->vl_symbol);
10254 	    }
10255 	  else
10256 	    {
10257 	      dw2_asm_output_data_uleb128 (curr->vbegin,
10258 					   "View list begin (%s)",
10259 					   list_head->vl_symbol);
10260 	      dw2_asm_output_data_uleb128 (curr->vend,
10261 					   "View list end (%s)",
10262 					   list_head->vl_symbol);
10263 	    }
10264 	}
10265     }
10266 
10267   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10268 
10269   const char *last_section = NULL;
10270   const char *base_label = NULL;
10271 
10272   /* Walk the location list, and output each range + expression.  */
10273   for (dw_loc_list_ref curr = list_head; curr != NULL;
10274        curr = curr->dw_loc_next)
10275     {
10276       unsigned long size;
10277 
10278       /* Skip this entry?  If we skip it here, we must skip it in the
10279 	 view list above as well. */
10280       if (skip_loc_list_entry (curr, &size))
10281 	continue;
10282 
10283       lcount++;
10284 
10285       if (dwarf_version >= 5)
10286 	{
10287 	  if (dwarf_split_debug_info)
10288 	    {
10289 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10290 	      /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10291 		 uleb128 index into .debug_addr and uleb128 length.  */
10292 	      dw2_asm_output_data (1, DW_LLE_startx_length,
10293 				   "DW_LLE_startx_length (%s)",
10294 				   list_head->ll_symbol);
10295 	      dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10296 					   "Location list range start index "
10297 					   "(%s)", curr->begin);
10298 	      /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10299 		 For that case we probably need to emit DW_LLE_startx_endx,
10300 		 but we'd need 2 .debug_addr entries rather than just one.  */
10301 	      dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10302 					    "Location list length (%s)",
10303 					    list_head->ll_symbol);
10304 	    }
10305 	  else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10306 	    {
10307 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10308 	      /* If all code is in .text section, the base address is
10309 		 already provided by the CU attributes.  Use
10310 		 DW_LLE_offset_pair where both addresses are uleb128 encoded
10311 		 offsets against that base.  */
10312 	      dw2_asm_output_data (1, DW_LLE_offset_pair,
10313 				   "DW_LLE_offset_pair (%s)",
10314 				   list_head->ll_symbol);
10315 	      dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10316 					    "Location list begin address (%s)",
10317 					    list_head->ll_symbol);
10318 	      dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10319 					    "Location list end address (%s)",
10320 					    list_head->ll_symbol);
10321 	    }
10322 	  else if (HAVE_AS_LEB128)
10323 	    {
10324 	      /* Otherwise, find out how many consecutive entries could share
10325 		 the same base entry.  If just one, emit DW_LLE_start_length,
10326 		 otherwise emit DW_LLE_base_address for the base address
10327 		 followed by a series of DW_LLE_offset_pair.  */
10328 	      if (last_section == NULL || curr->section != last_section)
10329 		{
10330 		  dw_loc_list_ref curr2;
10331 		  for (curr2 = curr->dw_loc_next; curr2 != NULL;
10332 		       curr2 = curr2->dw_loc_next)
10333 		    {
10334 		      if (strcmp (curr2->begin, curr2->end) == 0
10335 			  && !curr2->force)
10336 			continue;
10337 		      break;
10338 		    }
10339 		  if (curr2 == NULL || curr->section != curr2->section)
10340 		    last_section = NULL;
10341 		  else
10342 		    {
10343 		      last_section = curr->section;
10344 		      base_label = curr->begin;
10345 		      dw2_asm_output_data (1, DW_LLE_base_address,
10346 					   "DW_LLE_base_address (%s)",
10347 					   list_head->ll_symbol);
10348 		      dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10349 					   "Base address (%s)",
10350 					   list_head->ll_symbol);
10351 		    }
10352 		}
10353 	      /* Only one entry with the same base address.  Use
10354 		 DW_LLE_start_length with absolute address and uleb128
10355 		 length.  */
10356 	      if (last_section == NULL)
10357 		{
10358 		  dwarf2out_maybe_output_loclist_view_pair (curr);
10359 		  dw2_asm_output_data (1, DW_LLE_start_length,
10360 				       "DW_LLE_start_length (%s)",
10361 				       list_head->ll_symbol);
10362 		  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10363 				       "Location list begin address (%s)",
10364 				       list_head->ll_symbol);
10365 		  dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10366 						"Location list length "
10367 						"(%s)", list_head->ll_symbol);
10368 		}
10369 	      /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10370 		 DW_LLE_base_address.  */
10371 	      else
10372 		{
10373 		  dwarf2out_maybe_output_loclist_view_pair (curr);
10374 		  dw2_asm_output_data (1, DW_LLE_offset_pair,
10375 				       "DW_LLE_offset_pair (%s)",
10376 				       list_head->ll_symbol);
10377 		  dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10378 						"Location list begin address "
10379 						"(%s)", list_head->ll_symbol);
10380 		  dw2_asm_output_delta_uleb128 (curr->end, base_label,
10381 						"Location list end address "
10382 						"(%s)", list_head->ll_symbol);
10383 		}
10384 	    }
10385 	  /* The assembler does not support .uleb128 directive.  Emit
10386 	     DW_LLE_start_end with a pair of absolute addresses.  */
10387 	  else
10388 	    {
10389 	      dwarf2out_maybe_output_loclist_view_pair (curr);
10390 	      dw2_asm_output_data (1, DW_LLE_start_end,
10391 				   "DW_LLE_start_end (%s)",
10392 				   list_head->ll_symbol);
10393 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10394 				   "Location list begin address (%s)",
10395 				   list_head->ll_symbol);
10396 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10397 				   "Location list end address (%s)",
10398 				   list_head->ll_symbol);
10399 	    }
10400 	}
10401       else if (dwarf_split_debug_info)
10402 	{
10403 	  /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10404 	     and 4 byte length.  */
10405 	  dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10406 			       "Location list start/length entry (%s)",
10407 			       list_head->ll_symbol);
10408 	  dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10409 				       "Location list range start index (%s)",
10410 				       curr->begin);
10411 	  /* The length field is 4 bytes.  If we ever need to support
10412 	     an 8-byte length, we can add a new DW_LLE code or fall back
10413 	     to DW_LLE_GNU_start_end_entry.  */
10414 	  dw2_asm_output_delta (4, curr->end, curr->begin,
10415 				"Location list range length (%s)",
10416 				list_head->ll_symbol);
10417 	}
10418       else if (!have_multiple_function_sections)
10419 	{
10420 	  /* Pair of relative addresses against start of text section.  */
10421 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10422 				"Location list begin address (%s)",
10423 				list_head->ll_symbol);
10424 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10425 				"Location list end address (%s)",
10426 				list_head->ll_symbol);
10427 	}
10428       else
10429 	{
10430 	  /* Pair of absolute addresses.  */
10431 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10432 			       "Location list begin address (%s)",
10433 			       list_head->ll_symbol);
10434 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10435 			       "Location list end address (%s)",
10436 			       list_head->ll_symbol);
10437 	}
10438 
10439       /* Output the block length for this list of location operations.  */
10440       if (dwarf_version >= 5)
10441 	dw2_asm_output_data_uleb128 (size, "Location expression size");
10442       else
10443 	{
10444 	  gcc_assert (size <= 0xffff);
10445 	  dw2_asm_output_data (2, size, "Location expression size");
10446 	}
10447 
10448       output_loc_sequence (curr->expr, -1);
10449     }
10450 
10451   /* And finally list termination.  */
10452   if (dwarf_version >= 5)
10453     dw2_asm_output_data (1, DW_LLE_end_of_list,
10454 			 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10455   else if (dwarf_split_debug_info)
10456     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10457 			 "Location list terminator (%s)",
10458 			 list_head->ll_symbol);
10459   else
10460     {
10461       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10462 			   "Location list terminator begin (%s)",
10463 			   list_head->ll_symbol);
10464       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10465 			   "Location list terminator end (%s)",
10466 			   list_head->ll_symbol);
10467     }
10468 
10469   gcc_assert (!list_head->vl_symbol
10470 	      || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10471 }
10472 
10473 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10474    section.  Emit a relocated reference if val_entry is NULL, otherwise,
10475    emit an indirect reference.  */
10476 
10477 static void
output_range_list_offset(dw_attr_node * a)10478 output_range_list_offset (dw_attr_node *a)
10479 {
10480   const char *name = dwarf_attr_name (a->dw_attr);
10481 
10482   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10483     {
10484       if (dwarf_version >= 5)
10485 	{
10486 	  dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10487 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10488 				 debug_ranges_section, "%s", name);
10489 	}
10490       else
10491 	{
10492 	  char *p = strchr (ranges_section_label, '\0');
10493 	  sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10494 		   a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10495 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10496 				 debug_ranges_section, "%s", name);
10497 	  *p = '\0';
10498 	}
10499     }
10500   else if (dwarf_version >= 5)
10501     {
10502       dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10503       gcc_assert (rnglist_idx);
10504       dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10505     }
10506   else
10507     dw2_asm_output_data (DWARF_OFFSET_SIZE,
10508 			 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10509                          "%s (offset from %s)", name, ranges_section_label);
10510 }
10511 
10512 /* Output the offset into the debug_loc section.  */
10513 
10514 static void
output_loc_list_offset(dw_attr_node * a)10515 output_loc_list_offset (dw_attr_node *a)
10516 {
10517   char *sym = AT_loc_list (a)->ll_symbol;
10518 
10519   gcc_assert (sym);
10520   if (!dwarf_split_debug_info)
10521     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10522                            "%s", dwarf_attr_name (a->dw_attr));
10523   else if (dwarf_version >= 5)
10524     {
10525       gcc_assert (AT_loc_list (a)->num_assigned);
10526       dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10527 				   dwarf_attr_name (a->dw_attr),
10528 				   sym);
10529     }
10530   else
10531     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10532 			  "%s", dwarf_attr_name (a->dw_attr));
10533 }
10534 
10535 /* Output the offset into the debug_loc section.  */
10536 
10537 static void
output_view_list_offset(dw_attr_node * a)10538 output_view_list_offset (dw_attr_node *a)
10539 {
10540   char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10541 
10542   gcc_assert (sym);
10543   if (dwarf_split_debug_info)
10544     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10545                           "%s", dwarf_attr_name (a->dw_attr));
10546   else
10547     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10548                            "%s", dwarf_attr_name (a->dw_attr));
10549 }
10550 
10551 /* Output an attribute's index or value appropriately.  */
10552 
10553 static void
output_attr_index_or_value(dw_attr_node * a)10554 output_attr_index_or_value (dw_attr_node *a)
10555 {
10556   const char *name = dwarf_attr_name (a->dw_attr);
10557 
10558   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10559     {
10560       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10561       return;
10562     }
10563   switch (AT_class (a))
10564     {
10565     case dw_val_class_addr:
10566       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10567       break;
10568     case dw_val_class_high_pc:
10569     case dw_val_class_lbl_id:
10570       dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10571       break;
10572     default:
10573       gcc_unreachable ();
10574     }
10575 }
10576 
10577 /* Output a type signature.  */
10578 
10579 static inline void
output_signature(const char * sig,const char * name)10580 output_signature (const char *sig, const char *name)
10581 {
10582   int i;
10583 
10584   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10585     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10586 }
10587 
10588 /* Output a discriminant value.  */
10589 
10590 static inline void
output_discr_value(dw_discr_value * discr_value,const char * name)10591 output_discr_value (dw_discr_value *discr_value, const char *name)
10592 {
10593   if (discr_value->pos)
10594     dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10595   else
10596     dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10597 }
10598 
10599 /* Output the DIE and its attributes.  Called recursively to generate
10600    the definitions of each child DIE.  */
10601 
10602 static void
output_die(dw_die_ref die)10603 output_die (dw_die_ref die)
10604 {
10605   dw_attr_node *a;
10606   dw_die_ref c;
10607   unsigned long size;
10608   unsigned ix;
10609 
10610   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10611 			       (unsigned long)die->die_offset,
10612 			       dwarf_tag_name (die->die_tag));
10613 
10614   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10615     {
10616       const char *name = dwarf_attr_name (a->dw_attr);
10617 
10618       switch (AT_class (a))
10619 	{
10620 	case dw_val_class_addr:
10621           output_attr_index_or_value (a);
10622 	  break;
10623 
10624 	case dw_val_class_offset:
10625 	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10626 			       "%s", name);
10627 	  break;
10628 
10629 	case dw_val_class_range_list:
10630           output_range_list_offset (a);
10631 	  break;
10632 
10633 	case dw_val_class_loc:
10634 	  size = size_of_locs (AT_loc (a));
10635 
10636 	  /* Output the block length for this list of location operations.  */
10637 	  if (dwarf_version >= 4)
10638 	    dw2_asm_output_data_uleb128 (size, "%s", name);
10639 	  else
10640 	    dw2_asm_output_data (constant_size (size), size, "%s", name);
10641 
10642 	  output_loc_sequence (AT_loc (a), -1);
10643 	  break;
10644 
10645 	case dw_val_class_const:
10646 	  /* ??? It would be slightly more efficient to use a scheme like is
10647 	     used for unsigned constants below, but gdb 4.x does not sign
10648 	     extend.  Gdb 5.x does sign extend.  */
10649 	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10650 	  break;
10651 
10652 	case dw_val_class_unsigned_const:
10653 	  {
10654 	    int csize = constant_size (AT_unsigned (a));
10655 	    if (dwarf_version == 3
10656 		&& a->dw_attr == DW_AT_data_member_location
10657 		&& csize >= 4)
10658 	      dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10659 	    else
10660 	      dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10661 	  }
10662 	  break;
10663 
10664 	case dw_val_class_symview:
10665 	  {
10666 	    int vsize;
10667 	    if (symview_upper_bound <= 0xff)
10668 	      vsize = 1;
10669 	    else if (symview_upper_bound <= 0xffff)
10670 	      vsize = 2;
10671 	    else if (symview_upper_bound <= 0xffffffff)
10672 	      vsize = 4;
10673 	    else
10674 	      vsize = 8;
10675 	    dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10676 				 "%s", name);
10677 	  }
10678 	  break;
10679 
10680 	case dw_val_class_const_implicit:
10681 	  if (flag_debug_asm)
10682 	    fprintf (asm_out_file, "\t\t\t%s %s ("
10683 				   HOST_WIDE_INT_PRINT_DEC ")\n",
10684 		     ASM_COMMENT_START, name, AT_int (a));
10685 	  break;
10686 
10687 	case dw_val_class_unsigned_const_implicit:
10688 	  if (flag_debug_asm)
10689 	    fprintf (asm_out_file, "\t\t\t%s %s ("
10690 				   HOST_WIDE_INT_PRINT_HEX ")\n",
10691 		     ASM_COMMENT_START, name, AT_unsigned (a));
10692 	  break;
10693 
10694 	case dw_val_class_const_double:
10695 	  {
10696 	    unsigned HOST_WIDE_INT first, second;
10697 
10698 	    if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10699 	      dw2_asm_output_data (1,
10700 				   HOST_BITS_PER_DOUBLE_INT
10701 				   / HOST_BITS_PER_CHAR,
10702 				   NULL);
10703 
10704 	    if (WORDS_BIG_ENDIAN)
10705 	      {
10706 		first = a->dw_attr_val.v.val_double.high;
10707 		second = a->dw_attr_val.v.val_double.low;
10708 	      }
10709 	    else
10710 	      {
10711 		first = a->dw_attr_val.v.val_double.low;
10712 		second = a->dw_attr_val.v.val_double.high;
10713 	      }
10714 
10715 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10716                                  first, "%s", name);
10717 	    dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10718 				 second, NULL);
10719 	  }
10720 	  break;
10721 
10722 	case dw_val_class_wide_int:
10723 	  {
10724 	    int i;
10725 	    int len = get_full_len (*a->dw_attr_val.v.val_wide);
10726 	    int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10727 	    if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10728 	      dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10729 				      * l, NULL);
10730 
10731 	    if (WORDS_BIG_ENDIAN)
10732 	      for (i = len - 1; i >= 0; --i)
10733 		{
10734 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10735 				       "%s", name);
10736 		  name = "";
10737 		}
10738 	    else
10739 	      for (i = 0; i < len; ++i)
10740 		{
10741 		  dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10742 				       "%s", name);
10743 		  name = "";
10744 		}
10745 	  }
10746 	  break;
10747 
10748 	case dw_val_class_vec:
10749 	  {
10750 	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10751 	    unsigned int len = a->dw_attr_val.v.val_vec.length;
10752 	    unsigned int i;
10753 	    unsigned char *p;
10754 
10755 	    dw2_asm_output_data (constant_size (len * elt_size),
10756 				 len * elt_size, "%s", name);
10757 	    if (elt_size > sizeof (HOST_WIDE_INT))
10758 	      {
10759 		elt_size /= 2;
10760 		len *= 2;
10761 	      }
10762 	    for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10763 		 i < len;
10764 		 i++, p += elt_size)
10765 	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10766 				   "fp or vector constant word %u", i);
10767 	    break;
10768 	  }
10769 
10770 	case dw_val_class_flag:
10771 	  if (dwarf_version >= 4)
10772 	    {
10773 	      /* Currently all add_AT_flag calls pass in 1 as last argument,
10774 		 so DW_FORM_flag_present can be used.  If that ever changes,
10775 		 we'll need to use DW_FORM_flag and have some optimization
10776 		 in build_abbrev_table that will change those to
10777 		 DW_FORM_flag_present if it is set to 1 in all DIEs using
10778 		 the same abbrev entry.  */
10779 	      gcc_assert (AT_flag (a) == 1);
10780 	      if (flag_debug_asm)
10781 		fprintf (asm_out_file, "\t\t\t%s %s\n",
10782 			 ASM_COMMENT_START, name);
10783 	      break;
10784 	    }
10785 	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
10786 	  break;
10787 
10788 	case dw_val_class_loc_list:
10789 	  output_loc_list_offset (a);
10790 	  break;
10791 
10792 	case dw_val_class_view_list:
10793 	  output_view_list_offset (a);
10794 	  break;
10795 
10796 	case dw_val_class_die_ref:
10797 	  if (AT_ref_external (a))
10798 	    {
10799 	      if (AT_ref (a)->comdat_type_p)
10800 	        {
10801 		  comdat_type_node *type_node
10802 		    = AT_ref (a)->die_id.die_type_node;
10803 
10804 	          gcc_assert (type_node);
10805 	          output_signature (type_node->signature, name);
10806 	        }
10807 	      else
10808 	        {
10809 		  const char *sym = AT_ref (a)->die_id.die_symbol;
10810 		  int size;
10811 
10812 		  gcc_assert (sym);
10813 		  /* In DWARF2, DW_FORM_ref_addr is sized by target address
10814 		     length, whereas in DWARF3 it's always sized as an
10815 		     offset.  */
10816 		  if (dwarf_version == 2)
10817 		    size = DWARF2_ADDR_SIZE;
10818 		  else
10819 		    size = DWARF_OFFSET_SIZE;
10820 		  /* ???  We cannot unconditionally output die_offset if
10821 		     non-zero - others might create references to those
10822 		     DIEs via symbols.
10823 		     And we do not clear its DIE offset after outputting it
10824 		     (and the label refers to the actual DIEs, not the
10825 		     DWARF CU unit header which is when using label + offset
10826 		     would be the correct thing to do).
10827 		     ???  This is the reason for the with_offset flag.  */
10828 		  if (AT_ref (a)->with_offset)
10829 		    dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10830 					   debug_info_section, "%s", name);
10831 		  else
10832 		    dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10833 					   name);
10834 		}
10835 	    }
10836 	  else
10837 	    {
10838 	      gcc_assert (AT_ref (a)->die_offset);
10839 	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10840 				   "%s", name);
10841 	    }
10842 	  break;
10843 
10844 	case dw_val_class_fde_ref:
10845 	  {
10846 	    char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10847 
10848 	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10849 					 a->dw_attr_val.v.val_fde_index * 2);
10850 	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10851 				   "%s", name);
10852 	  }
10853 	  break;
10854 
10855 	case dw_val_class_vms_delta:
10856 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10857 	  dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10858 				    AT_vms_delta2 (a), AT_vms_delta1 (a),
10859 				    "%s", name);
10860 #else
10861 	  dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10862 				AT_vms_delta2 (a), AT_vms_delta1 (a),
10863 				"%s", name);
10864 #endif
10865 	  break;
10866 
10867 	case dw_val_class_lbl_id:
10868 	  output_attr_index_or_value (a);
10869 	  break;
10870 
10871 	case dw_val_class_lineptr:
10872 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10873 				 debug_line_section, "%s", name);
10874 	  break;
10875 
10876 	case dw_val_class_macptr:
10877 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10878 				 debug_macinfo_section, "%s", name);
10879 	  break;
10880 
10881 	case dw_val_class_loclistsptr:
10882 	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10883 				 debug_loc_section, "%s", name);
10884 	  break;
10885 
10886 	case dw_val_class_str:
10887           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10888             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10889                                    a->dw_attr_val.v.val_str->label,
10890                                    debug_str_section,
10891                                    "%s: \"%s\"", name, AT_string (a));
10892 	  else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10893 	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10894 				   a->dw_attr_val.v.val_str->label,
10895 				   debug_line_str_section,
10896 				   "%s: \"%s\"", name, AT_string (a));
10897           else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10898             dw2_asm_output_data_uleb128 (AT_index (a),
10899                                          "%s: \"%s\"", name, AT_string (a));
10900           else
10901 	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10902 	  break;
10903 
10904 	case dw_val_class_file:
10905 	  {
10906 	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10907 
10908 	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10909 				 a->dw_attr_val.v.val_file->filename);
10910 	    break;
10911 	  }
10912 
10913 	case dw_val_class_file_implicit:
10914 	  if (flag_debug_asm)
10915 	    fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10916 		     ASM_COMMENT_START, name,
10917 		     maybe_emit_file (a->dw_attr_val.v.val_file),
10918 		     a->dw_attr_val.v.val_file->filename);
10919 	  break;
10920 
10921 	case dw_val_class_data8:
10922 	  {
10923 	    int i;
10924 
10925 	    for (i = 0; i < 8; i++)
10926 	      dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10927 				   i == 0 ? "%s" : NULL, name);
10928 	    break;
10929 	  }
10930 
10931 	case dw_val_class_high_pc:
10932 	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10933 				get_AT_low_pc (die), "DW_AT_high_pc");
10934 	  break;
10935 
10936 	case dw_val_class_discr_value:
10937 	  output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10938 	  break;
10939 
10940 	case dw_val_class_discr_list:
10941 	  {
10942 	    dw_discr_list_ref list = AT_discr_list (a);
10943 	    const int size = size_of_discr_list (list);
10944 
10945 	    /* This is a block, so output its length first.  */
10946 	    dw2_asm_output_data (constant_size (size), size,
10947 				 "%s: block size", name);
10948 
10949 	    for (; list != NULL; list = list->dw_discr_next)
10950 	      {
10951 		/* One byte for the discriminant value descriptor, and then as
10952 		   many LEB128 numbers as required.  */
10953 		if (list->dw_discr_range)
10954 		  dw2_asm_output_data (1, DW_DSC_range,
10955 				       "%s: DW_DSC_range", name);
10956 		else
10957 		  dw2_asm_output_data (1, DW_DSC_label,
10958 				       "%s: DW_DSC_label", name);
10959 
10960 		output_discr_value (&list->dw_discr_lower_bound, name);
10961 		if (list->dw_discr_range)
10962 		  output_discr_value (&list->dw_discr_upper_bound, name);
10963 	      }
10964 	    break;
10965 	  }
10966 
10967 	default:
10968 	  gcc_unreachable ();
10969 	}
10970     }
10971 
10972   FOR_EACH_CHILD (die, c, output_die (c));
10973 
10974   /* Add null byte to terminate sibling list.  */
10975   if (die->die_child != NULL)
10976     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10977 			 (unsigned long) die->die_offset);
10978 }
10979 
10980 /* Output the dwarf version number.  */
10981 
10982 static void
output_dwarf_version()10983 output_dwarf_version ()
10984 {
10985   /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10986      views in loclist.  That will change eventually.  */
10987   if (dwarf_version == 6)
10988     {
10989       static bool once;
10990       if (!once)
10991 	{
10992 	  warning (0, "%<-gdwarf-6%> is output as version 5 with "
10993 		   "incompatibilities");
10994 	  once = true;
10995 	}
10996       dw2_asm_output_data (2, 5, "DWARF version number");
10997     }
10998   else
10999     dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11000 }
11001 
11002 /* Output the compilation unit that appears at the beginning of the
11003    .debug_info section, and precedes the DIE descriptions.  */
11004 
11005 static void
output_compilation_unit_header(enum dwarf_unit_type ut)11006 output_compilation_unit_header (enum dwarf_unit_type ut)
11007 {
11008   if (!XCOFF_DEBUGGING_INFO)
11009     {
11010       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11011 	dw2_asm_output_data (4, 0xffffffff,
11012 	  "Initial length escape value indicating 64-bit DWARF extension");
11013       dw2_asm_output_data (DWARF_OFFSET_SIZE,
11014 			   next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11015 			   "Length of Compilation Unit Info");
11016     }
11017 
11018   output_dwarf_version ();
11019   if (dwarf_version >= 5)
11020     {
11021       const char *name;
11022       switch (ut)
11023 	{
11024 	case DW_UT_compile: name = "DW_UT_compile"; break;
11025 	case DW_UT_type: name = "DW_UT_type"; break;
11026 	case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11027 	case DW_UT_split_type: name = "DW_UT_split_type"; break;
11028 	default: gcc_unreachable ();
11029 	}
11030       dw2_asm_output_data (1, ut, "%s", name);
11031       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11032     }
11033   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11034 			 debug_abbrev_section,
11035 			 "Offset Into Abbrev. Section");
11036   if (dwarf_version < 5)
11037     dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11038 }
11039 
11040 /* Output the compilation unit DIE and its children.  */
11041 
11042 static void
output_comp_unit(dw_die_ref die,int output_if_empty,const unsigned char * dwo_id)11043 output_comp_unit (dw_die_ref die, int output_if_empty,
11044 		  const unsigned char *dwo_id)
11045 {
11046   const char *secname, *oldsym;
11047   char *tmp;
11048 
11049   /* Unless we are outputting main CU, we may throw away empty ones.  */
11050   if (!output_if_empty && die->die_child == NULL)
11051     return;
11052 
11053   /* Even if there are no children of this DIE, we must output the information
11054      about the compilation unit.  Otherwise, on an empty translation unit, we
11055      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11056      will then complain when examining the file.  First mark all the DIEs in
11057      this CU so we know which get local refs.  */
11058   mark_dies (die);
11059 
11060   external_ref_hash_type *extern_map = optimize_external_refs (die);
11061 
11062   /* For now, optimize only the main CU, in order to optimize the rest
11063      we'd need to see all of them earlier.  Leave the rest for post-linking
11064      tools like DWZ.  */
11065   if (die == comp_unit_die ())
11066     abbrev_opt_start = vec_safe_length (abbrev_die_table);
11067 
11068   build_abbrev_table (die, extern_map);
11069 
11070   optimize_abbrev_table ();
11071 
11072   delete extern_map;
11073 
11074   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11075   next_die_offset = (dwo_id
11076 		     ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11077 		     : DWARF_COMPILE_UNIT_HEADER_SIZE);
11078   calc_die_sizes (die);
11079 
11080   oldsym = die->die_id.die_symbol;
11081   if (oldsym && die->comdat_type_p)
11082     {
11083       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11084 
11085       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11086       secname = tmp;
11087       die->die_id.die_symbol = NULL;
11088       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11089     }
11090   else
11091     {
11092       switch_to_section (debug_info_section);
11093       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11094       info_section_emitted = true;
11095     }
11096 
11097   /* For LTO cross unit DIE refs we want a symbol on the start of the
11098      debuginfo section, not on the CU DIE.  */
11099   if ((flag_generate_lto || flag_generate_offload) && oldsym)
11100     {
11101       /* ???  No way to get visibility assembled without a decl.  */
11102       tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11103 			      get_identifier (oldsym), char_type_node);
11104       TREE_PUBLIC (decl) = true;
11105       TREE_STATIC (decl) = true;
11106       DECL_ARTIFICIAL (decl) = true;
11107       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11108       DECL_VISIBILITY_SPECIFIED (decl) = true;
11109       targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11110 #ifdef ASM_WEAKEN_LABEL
11111       /* We prefer a .weak because that handles duplicates from duplicate
11112          archive members in a graceful way.  */
11113       ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11114 #else
11115       targetm.asm_out.globalize_label (asm_out_file, oldsym);
11116 #endif
11117       ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11118     }
11119 
11120   /* Output debugging information.  */
11121   output_compilation_unit_header (dwo_id
11122 				  ? DW_UT_split_compile : DW_UT_compile);
11123   if (dwarf_version >= 5)
11124     {
11125       if (dwo_id != NULL)
11126 	for (int i = 0; i < 8; i++)
11127 	  dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11128     }
11129   output_die (die);
11130 
11131   /* Leave the marks on the main CU, so we can check them in
11132      output_pubnames.  */
11133   if (oldsym)
11134     {
11135       unmark_dies (die);
11136       die->die_id.die_symbol = oldsym;
11137     }
11138 }
11139 
11140 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11141    and .debug_pubtypes.  This is configured per-target, but can be
11142    overridden by the -gpubnames or -gno-pubnames options.  */
11143 
11144 static inline bool
want_pubnames(void)11145 want_pubnames (void)
11146 {
11147   if (debug_info_level <= DINFO_LEVEL_TERSE
11148       /* Names and types go to the early debug part only.  */
11149       || in_lto_p)
11150     return false;
11151   if (debug_generate_pub_sections != -1)
11152     return debug_generate_pub_sections;
11153   return targetm.want_debug_pub_sections;
11154 }
11155 
11156 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
11157 
11158 static void
add_AT_pubnames(dw_die_ref die)11159 add_AT_pubnames (dw_die_ref die)
11160 {
11161   if (want_pubnames ())
11162     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11163 }
11164 
11165 /* Add a string attribute value to a skeleton DIE.  */
11166 
11167 static inline void
add_skeleton_AT_string(dw_die_ref die,enum dwarf_attribute attr_kind,const char * str)11168 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11169                         const char *str)
11170 {
11171   dw_attr_node attr;
11172   struct indirect_string_node *node;
11173 
11174   if (! skeleton_debug_str_hash)
11175     skeleton_debug_str_hash
11176       = hash_table<indirect_string_hasher>::create_ggc (10);
11177 
11178   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11179   find_string_form (node);
11180   if (node->form == dwarf_FORM (DW_FORM_strx))
11181     node->form = DW_FORM_strp;
11182 
11183   attr.dw_attr = attr_kind;
11184   attr.dw_attr_val.val_class = dw_val_class_str;
11185   attr.dw_attr_val.val_entry = NULL;
11186   attr.dw_attr_val.v.val_str = node;
11187   add_dwarf_attr (die, &attr);
11188 }
11189 
11190 /* Helper function to generate top-level dies for skeleton debug_info and
11191    debug_types.  */
11192 
11193 static void
add_top_level_skeleton_die_attrs(dw_die_ref die)11194 add_top_level_skeleton_die_attrs (dw_die_ref die)
11195 {
11196   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11197   const char *comp_dir = comp_dir_string ();
11198 
11199   add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11200   if (comp_dir != NULL)
11201     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11202   add_AT_pubnames (die);
11203   if (addr_index_table != NULL && addr_index_table->size () > 0)
11204     add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11205 }
11206 
11207 /* Output skeleton debug sections that point to the dwo file.  */
11208 
11209 static void
output_skeleton_debug_sections(dw_die_ref comp_unit,const unsigned char * dwo_id)11210 output_skeleton_debug_sections (dw_die_ref comp_unit,
11211 				const unsigned char *dwo_id)
11212 {
11213   /* These attributes will be found in the full debug_info section.  */
11214   remove_AT (comp_unit, DW_AT_producer);
11215   remove_AT (comp_unit, DW_AT_language);
11216 
11217   switch_to_section (debug_skeleton_info_section);
11218   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11219 
11220   /* Produce the skeleton compilation-unit header.  This one differs enough from
11221      a normal CU header that it's better not to call output_compilation_unit
11222      header.  */
11223   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11224     dw2_asm_output_data (4, 0xffffffff,
11225 			 "Initial length escape value indicating 64-bit "
11226 			 "DWARF extension");
11227 
11228   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11229 		       DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11230                        - DWARF_INITIAL_LENGTH_SIZE
11231                        + size_of_die (comp_unit),
11232                       "Length of Compilation Unit Info");
11233   output_dwarf_version ();
11234   if (dwarf_version >= 5)
11235     {
11236       dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11237       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11238     }
11239   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11240 			 debug_skeleton_abbrev_section,
11241                          "Offset Into Abbrev. Section");
11242   if (dwarf_version < 5)
11243     dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11244   else
11245     for (int i = 0; i < 8; i++)
11246       dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11247 
11248   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11249   output_die (comp_unit);
11250 
11251   /* Build the skeleton debug_abbrev section.  */
11252   switch_to_section (debug_skeleton_abbrev_section);
11253   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11254 
11255   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11256 
11257   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11258 }
11259 
11260 /* Output a comdat type unit DIE and its children.  */
11261 
11262 static void
output_comdat_type_unit(comdat_type_node * node,bool early_lto_debug ATTRIBUTE_UNUSED)11263 output_comdat_type_unit (comdat_type_node *node,
11264 			 bool early_lto_debug ATTRIBUTE_UNUSED)
11265 {
11266   const char *secname;
11267   char *tmp;
11268   int i;
11269 #if defined (OBJECT_FORMAT_ELF)
11270   tree comdat_key;
11271 #endif
11272 
11273   /* First mark all the DIEs in this CU so we know which get local refs.  */
11274   mark_dies (node->root_die);
11275 
11276   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11277 
11278   build_abbrev_table (node->root_die, extern_map);
11279 
11280   delete extern_map;
11281   extern_map = NULL;
11282 
11283   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11284   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11285   calc_die_sizes (node->root_die);
11286 
11287 #if defined (OBJECT_FORMAT_ELF)
11288   if (dwarf_version >= 5)
11289     {
11290       if (!dwarf_split_debug_info)
11291 	secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11292       else
11293 	secname = (early_lto_debug
11294 		   ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11295     }
11296   else if (!dwarf_split_debug_info)
11297     secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11298   else
11299     secname = (early_lto_debug
11300 	       ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11301 
11302   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11303   sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11304   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11305     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11306   comdat_key = get_identifier (tmp);
11307   targetm.asm_out.named_section (secname,
11308                                  SECTION_DEBUG | SECTION_LINKONCE,
11309                                  comdat_key);
11310 #else
11311   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11312   sprintf (tmp, (dwarf_version >= 5
11313 		 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11314   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11315     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11316   secname = tmp;
11317   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11318 #endif
11319 
11320   /* Output debugging information.  */
11321   output_compilation_unit_header (dwarf_split_debug_info
11322 				  ? DW_UT_split_type : DW_UT_type);
11323   output_signature (node->signature, "Type Signature");
11324   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11325 		       "Offset to Type DIE");
11326   output_die (node->root_die);
11327 
11328   unmark_dies (node->root_die);
11329 }
11330 
11331 /* Return the DWARF2/3 pubname associated with a decl.  */
11332 
11333 static const char *
dwarf2_name(tree decl,int scope)11334 dwarf2_name (tree decl, int scope)
11335 {
11336   if (DECL_NAMELESS (decl))
11337     return NULL;
11338   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11339 }
11340 
11341 /* Add a new entry to .debug_pubnames if appropriate.  */
11342 
11343 static void
add_pubname_string(const char * str,dw_die_ref die)11344 add_pubname_string (const char *str, dw_die_ref die)
11345 {
11346   pubname_entry e;
11347 
11348   e.die = die;
11349   e.name = xstrdup (str);
11350   vec_safe_push (pubname_table, e);
11351 }
11352 
11353 static void
add_pubname(tree decl,dw_die_ref die)11354 add_pubname (tree decl, dw_die_ref die)
11355 {
11356   if (!want_pubnames ())
11357     return;
11358 
11359   /* Don't add items to the table when we expect that the consumer will have
11360      just read the enclosing die.  For example, if the consumer is looking at a
11361      class_member, it will either be inside the class already, or will have just
11362      looked up the class to find the member.  Either way, searching the class is
11363      faster than searching the index.  */
11364   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11365       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11366     {
11367       const char *name = dwarf2_name (decl, 1);
11368 
11369       if (name)
11370 	add_pubname_string (name, die);
11371     }
11372 }
11373 
11374 /* Add an enumerator to the pubnames section.  */
11375 
11376 static void
add_enumerator_pubname(const char * scope_name,dw_die_ref die)11377 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11378 {
11379   pubname_entry e;
11380 
11381   gcc_assert (scope_name);
11382   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11383   e.die = die;
11384   vec_safe_push (pubname_table, e);
11385 }
11386 
11387 /* Add a new entry to .debug_pubtypes if appropriate.  */
11388 
11389 static void
add_pubtype(tree decl,dw_die_ref die)11390 add_pubtype (tree decl, dw_die_ref die)
11391 {
11392   pubname_entry e;
11393 
11394   if (!want_pubnames ())
11395     return;
11396 
11397   if ((TREE_PUBLIC (decl)
11398        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11399       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11400     {
11401       tree scope = NULL;
11402       const char *scope_name = "";
11403       const char *sep = is_cxx () ? "::" : ".";
11404       const char *name;
11405 
11406       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11407       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11408         {
11409           scope_name = lang_hooks.dwarf_name (scope, 1);
11410           if (scope_name != NULL && scope_name[0] != '\0')
11411             scope_name = concat (scope_name, sep, NULL);
11412           else
11413             scope_name = "";
11414 	}
11415 
11416       if (TYPE_P (decl))
11417         name = type_tag (decl);
11418       else
11419         name = lang_hooks.dwarf_name (decl, 1);
11420 
11421       /* If we don't have a name for the type, there's no point in adding
11422 	 it to the table.  */
11423       if (name != NULL && name[0] != '\0')
11424         {
11425           e.die = die;
11426           e.name = concat (scope_name, name, NULL);
11427           vec_safe_push (pubtype_table, e);
11428         }
11429 
11430       /* Although it might be more consistent to add the pubinfo for the
11431          enumerators as their dies are created, they should only be added if the
11432          enum type meets the criteria above.  So rather than re-check the parent
11433          enum type whenever an enumerator die is created, just output them all
11434          here.  This isn't protected by the name conditional because anonymous
11435          enums don't have names.  */
11436       if (die->die_tag == DW_TAG_enumeration_type)
11437         {
11438           dw_die_ref c;
11439 
11440           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11441         }
11442     }
11443 }
11444 
11445 /* Output a single entry in the pubnames table.  */
11446 
11447 static void
output_pubname(dw_offset die_offset,pubname_entry * entry)11448 output_pubname (dw_offset die_offset, pubname_entry *entry)
11449 {
11450   dw_die_ref die = entry->die;
11451   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11452 
11453   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11454 
11455   if (debug_generate_pub_sections == 2)
11456     {
11457       /* This logic follows gdb's method for determining the value of the flag
11458          byte.  */
11459       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11460       switch (die->die_tag)
11461       {
11462         case DW_TAG_typedef:
11463         case DW_TAG_base_type:
11464         case DW_TAG_subrange_type:
11465           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11466           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11467           break;
11468         case DW_TAG_enumerator:
11469           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11470                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11471 	  if (!is_cxx ())
11472 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11473           break;
11474         case DW_TAG_subprogram:
11475           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11476                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
11477           if (!is_ada ())
11478             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11479           break;
11480         case DW_TAG_constant:
11481           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11482                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11483           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11484           break;
11485         case DW_TAG_variable:
11486           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11487                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
11488           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11489           break;
11490         case DW_TAG_namespace:
11491         case DW_TAG_imported_declaration:
11492           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11493           break;
11494         case DW_TAG_class_type:
11495         case DW_TAG_interface_type:
11496         case DW_TAG_structure_type:
11497         case DW_TAG_union_type:
11498         case DW_TAG_enumeration_type:
11499           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11500 	  if (!is_cxx ())
11501 	    GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11502           break;
11503         default:
11504           /* An unusual tag.  Leave the flag-byte empty.  */
11505           break;
11506       }
11507       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11508                            "GDB-index flags");
11509     }
11510 
11511   dw2_asm_output_nstring (entry->name, -1, "external name");
11512 }
11513 
11514 
11515 /* Output the public names table used to speed up access to externally
11516    visible names; or the public types table used to find type definitions.  */
11517 
11518 static void
output_pubnames(vec<pubname_entry,va_gc> * names)11519 output_pubnames (vec<pubname_entry, va_gc> *names)
11520 {
11521   unsigned i;
11522   unsigned long pubnames_length = size_of_pubnames (names);
11523   pubname_entry *pub;
11524 
11525   if (!XCOFF_DEBUGGING_INFO)
11526     {
11527       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11528 	dw2_asm_output_data (4, 0xffffffff,
11529 	  "Initial length escape value indicating 64-bit DWARF extension");
11530       dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11531 			   "Pub Info Length");
11532     }
11533 
11534   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
11535   dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11536 
11537   if (dwarf_split_debug_info)
11538     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11539                            debug_skeleton_info_section,
11540                            "Offset of Compilation Unit Info");
11541   else
11542     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11543                            debug_info_section,
11544                            "Offset of Compilation Unit Info");
11545   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11546 		       "Compilation Unit Length");
11547 
11548   FOR_EACH_VEC_ELT (*names, i, pub)
11549     {
11550       if (include_pubname_in_output (names, pub))
11551 	{
11552 	  dw_offset die_offset = pub->die->die_offset;
11553 
11554           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11555           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11556             gcc_assert (pub->die->die_mark);
11557 
11558 	  /* If we're putting types in their own .debug_types sections,
11559 	     the .debug_pubtypes table will still point to the compile
11560 	     unit (not the type unit), so we want to use the offset of
11561 	     the skeleton DIE (if there is one).  */
11562 	  if (pub->die->comdat_type_p && names == pubtype_table)
11563 	    {
11564 	      comdat_type_node *type_node = pub->die->die_id.die_type_node;
11565 
11566 	      if (type_node != NULL)
11567 	        die_offset = (type_node->skeleton_die != NULL
11568 			      ? type_node->skeleton_die->die_offset
11569 			      : comp_unit_die ()->die_offset);
11570 	    }
11571 
11572           output_pubname (die_offset, pub);
11573 	}
11574     }
11575 
11576   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11577 }
11578 
11579 /* Output public names and types tables if necessary.  */
11580 
11581 static void
output_pubtables(void)11582 output_pubtables (void)
11583 {
11584   if (!want_pubnames () || !info_section_emitted)
11585     return;
11586 
11587   switch_to_section (debug_pubnames_section);
11588   output_pubnames (pubname_table);
11589   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11590      It shouldn't hurt to emit it always, since pure DWARF2 consumers
11591      simply won't look for the section.  */
11592   switch_to_section (debug_pubtypes_section);
11593   output_pubnames (pubtype_table);
11594 }
11595 
11596 
11597 /* Output the information that goes into the .debug_aranges table.
11598    Namely, define the beginning and ending address range of the
11599    text section generated for this compilation unit.  */
11600 
11601 static void
output_aranges(void)11602 output_aranges (void)
11603 {
11604   unsigned i;
11605   unsigned long aranges_length = size_of_aranges ();
11606 
11607   if (!XCOFF_DEBUGGING_INFO)
11608     {
11609       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11610 	dw2_asm_output_data (4, 0xffffffff,
11611 	  "Initial length escape value indicating 64-bit DWARF extension");
11612       dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11613 			   "Length of Address Ranges Info");
11614     }
11615 
11616   /* Version number for aranges is still 2, even up to DWARF5.  */
11617   dw2_asm_output_data (2, 2, "DWARF aranges version");
11618   if (dwarf_split_debug_info)
11619     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11620                            debug_skeleton_info_section,
11621                            "Offset of Compilation Unit Info");
11622   else
11623     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11624                            debug_info_section,
11625                            "Offset of Compilation Unit Info");
11626   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11627   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11628 
11629   /* We need to align to twice the pointer size here.  */
11630   if (DWARF_ARANGES_PAD_SIZE)
11631     {
11632       /* Pad using a 2 byte words so that padding is correct for any
11633 	 pointer size.  */
11634       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11635 			   2 * DWARF2_ADDR_SIZE);
11636       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11637 	dw2_asm_output_data (2, 0, NULL);
11638     }
11639 
11640   /* It is necessary not to output these entries if the sections were
11641      not used; if the sections were not used, the length will be 0 and
11642      the address may end up as 0 if the section is discarded by ld
11643      --gc-sections, leaving an invalid (0, 0) entry that can be
11644      confused with the terminator.  */
11645   if (text_section_used)
11646     {
11647       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11648       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11649 			    text_section_label, "Length");
11650     }
11651   if (cold_text_section_used)
11652     {
11653       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11654 			   "Address");
11655       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11656 			    cold_text_section_label, "Length");
11657     }
11658 
11659   if (have_multiple_function_sections)
11660     {
11661       unsigned fde_idx;
11662       dw_fde_ref fde;
11663 
11664       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11665 	{
11666 	  if (DECL_IGNORED_P (fde->decl))
11667 	    continue;
11668 	  if (!fde->in_std_section)
11669 	    {
11670 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11671 				   "Address");
11672 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11673 				    fde->dw_fde_begin, "Length");
11674 	    }
11675 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11676 	    {
11677 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11678 				   "Address");
11679 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11680 				    fde->dw_fde_second_begin, "Length");
11681 	    }
11682 	}
11683     }
11684 
11685   /* Output the terminator words.  */
11686   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11687   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11688 }
11689 
11690 /* Add a new entry to .debug_ranges.  Return its index into
11691    ranges_table vector.  */
11692 
11693 static unsigned int
add_ranges_num(int num,bool maybe_new_sec)11694 add_ranges_num (int num, bool maybe_new_sec)
11695 {
11696   dw_ranges r = { NULL, num, 0, maybe_new_sec };
11697   vec_safe_push (ranges_table, r);
11698   return vec_safe_length (ranges_table) - 1;
11699 }
11700 
11701 /* Add a new entry to .debug_ranges corresponding to a block, or a
11702    range terminator if BLOCK is NULL.  MAYBE_NEW_SEC is true if
11703    this entry might be in a different section from previous range.  */
11704 
11705 static unsigned int
add_ranges(const_tree block,bool maybe_new_sec)11706 add_ranges (const_tree block, bool maybe_new_sec)
11707 {
11708   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11709 }
11710 
11711 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11712    chain, or middle entry of a chain that will be directly referred to.  */
11713 
11714 static void
note_rnglist_head(unsigned int offset)11715 note_rnglist_head (unsigned int offset)
11716 {
11717   if (dwarf_version < 5 || (*ranges_table)[offset].label)
11718     return;
11719   (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11720 }
11721 
11722 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11723    When using dwarf_split_debug_info, address attributes in dies destined
11724    for the final executable should be direct references--setting the
11725    parameter force_direct ensures this behavior.  */
11726 
11727 static void
add_ranges_by_labels(dw_die_ref die,const char * begin,const char * end,bool * added,bool force_direct)11728 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11729                       bool *added, bool force_direct)
11730 {
11731   unsigned int in_use = vec_safe_length (ranges_by_label);
11732   unsigned int offset;
11733   dw_ranges_by_label rbl = { begin, end };
11734   vec_safe_push (ranges_by_label, rbl);
11735   offset = add_ranges_num (-(int)in_use - 1, true);
11736   if (!*added)
11737     {
11738       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11739       *added = true;
11740       note_rnglist_head (offset);
11741     }
11742 }
11743 
11744 /* Emit .debug_ranges section.  */
11745 
11746 static void
output_ranges(void)11747 output_ranges (void)
11748 {
11749   unsigned i;
11750   static const char *const start_fmt = "Offset %#x";
11751   const char *fmt = start_fmt;
11752   dw_ranges *r;
11753 
11754   switch_to_section (debug_ranges_section);
11755   ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11756   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11757     {
11758       int block_num = r->num;
11759 
11760       if (block_num > 0)
11761 	{
11762 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11763 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11764 
11765 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11766 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11767 
11768 	  /* If all code is in the text section, then the compilation
11769 	     unit base address defaults to DW_AT_low_pc, which is the
11770 	     base of the text section.  */
11771 	  if (!have_multiple_function_sections)
11772 	    {
11773 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11774 				    text_section_label,
11775 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
11776 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11777 				    text_section_label, NULL);
11778 	    }
11779 
11780 	  /* Otherwise, the compilation unit base address is zero,
11781 	     which allows us to use absolute addresses, and not worry
11782 	     about whether the target supports cross-section
11783 	     arithmetic.  */
11784 	  else
11785 	    {
11786 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11787 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
11788 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11789 	    }
11790 
11791 	  fmt = NULL;
11792 	}
11793 
11794       /* Negative block_num stands for an index into ranges_by_label.  */
11795       else if (block_num < 0)
11796 	{
11797 	  int lab_idx = - block_num - 1;
11798 
11799 	  if (!have_multiple_function_sections)
11800 	    {
11801 	      gcc_unreachable ();
11802 #if 0
11803 	      /* If we ever use add_ranges_by_labels () for a single
11804 		 function section, all we have to do is to take out
11805 		 the #if 0 above.  */
11806 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11807 				    (*ranges_by_label)[lab_idx].begin,
11808 				    text_section_label,
11809 				    fmt, i * 2 * DWARF2_ADDR_SIZE);
11810 	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11811 				    (*ranges_by_label)[lab_idx].end,
11812 				    text_section_label, NULL);
11813 #endif
11814 	    }
11815 	  else
11816 	    {
11817 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11818 				   (*ranges_by_label)[lab_idx].begin,
11819 				   fmt, i * 2 * DWARF2_ADDR_SIZE);
11820 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11821 				   (*ranges_by_label)[lab_idx].end,
11822 				   NULL);
11823 	    }
11824 	}
11825       else
11826 	{
11827 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11828 	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11829 	  fmt = start_fmt;
11830 	}
11831     }
11832 }
11833 
11834 /* Non-zero if .debug_line_str should be used for .debug_line section
11835    strings or strings that are likely shareable with those.  */
11836 #define DWARF5_USE_DEBUG_LINE_STR \
11837   (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET		\
11838    && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0		\
11839    /* FIXME: there is no .debug_line_str.dwo section,		\
11840       for -gsplit-dwarf we should use DW_FORM_strx instead.  */	\
11841    && !dwarf_split_debug_info)
11842 
11843 /* Assign .debug_rnglists indexes.  */
11844 
11845 static void
index_rnglists(void)11846 index_rnglists (void)
11847 {
11848   unsigned i;
11849   dw_ranges *r;
11850 
11851   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11852     if (r->label)
11853       r->idx = rnglist_idx++;
11854 }
11855 
11856 /* Emit .debug_rnglists section.  */
11857 
11858 static void
output_rnglists(unsigned generation)11859 output_rnglists (unsigned generation)
11860 {
11861   unsigned i;
11862   dw_ranges *r;
11863   char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11864   char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11865   char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11866 
11867   switch_to_section (debug_ranges_section);
11868   ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11869   /* There are up to 4 unique ranges labels per generation.
11870      See also init_sections_and_labels.  */
11871   ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11872 			       2 + generation * 4);
11873   ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11874 			       3 + generation * 4);
11875   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11876     dw2_asm_output_data (4, 0xffffffff,
11877 			 "Initial length escape value indicating "
11878 			 "64-bit DWARF extension");
11879   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11880 			"Length of Range Lists");
11881   ASM_OUTPUT_LABEL (asm_out_file, l1);
11882   output_dwarf_version ();
11883   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11884   dw2_asm_output_data (1, 0, "Segment Size");
11885   /* Emit the offset table only for -gsplit-dwarf.  If we don't care
11886      about relocation sizes and primarily care about the size of .debug*
11887      sections in linked shared libraries and executables, then
11888      the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11889      into it are usually larger than just DW_FORM_sec_offset offsets
11890      into the .debug_rnglists section.  */
11891   dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11892 		       "Offset Entry Count");
11893   if (dwarf_split_debug_info)
11894     {
11895       ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11896       FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11897 	if (r->label)
11898 	  dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11899 				ranges_base_label, NULL);
11900     }
11901 
11902   const char *lab = "";
11903   unsigned int len = vec_safe_length (ranges_table);
11904   const char *base = NULL;
11905   FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11906     {
11907       int block_num = r->num;
11908 
11909       if (r->label)
11910 	{
11911 	  ASM_OUTPUT_LABEL (asm_out_file, r->label);
11912 	  lab = r->label;
11913 	}
11914       if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11915 	base = NULL;
11916       if (block_num > 0)
11917 	{
11918 	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11919 	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11920 
11921 	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11922 	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11923 
11924 	  if (HAVE_AS_LEB128)
11925 	    {
11926 	      /* If all code is in the text section, then the compilation
11927 		 unit base address defaults to DW_AT_low_pc, which is the
11928 		 base of the text section.  */
11929 	      if (!have_multiple_function_sections)
11930 		{
11931 		  dw2_asm_output_data (1, DW_RLE_offset_pair,
11932 				       "DW_RLE_offset_pair (%s)", lab);
11933 		  dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11934 						"Range begin address (%s)", lab);
11935 		  dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11936 						"Range end address (%s)", lab);
11937 		  continue;
11938 		}
11939 	      if (base == NULL)
11940 		{
11941 		  dw_ranges *r2 = NULL;
11942 		  if (i < len - 1)
11943 		    r2 = &(*ranges_table)[i + 1];
11944 		  if (r2
11945 		      && r2->num != 0
11946 		      && r2->label == NULL
11947 		      && !r2->maybe_new_sec)
11948 		    {
11949 		      dw2_asm_output_data (1, DW_RLE_base_address,
11950 					   "DW_RLE_base_address (%s)", lab);
11951 		      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11952 					   "Base address (%s)", lab);
11953 		      strcpy (basebuf, blabel);
11954 		      base = basebuf;
11955 		    }
11956 		}
11957 	      if (base)
11958 		{
11959 		  dw2_asm_output_data (1, DW_RLE_offset_pair,
11960 				       "DW_RLE_offset_pair (%s)", lab);
11961 		  dw2_asm_output_delta_uleb128 (blabel, base,
11962 						"Range begin address (%s)", lab);
11963 		  dw2_asm_output_delta_uleb128 (elabel, base,
11964 						"Range end address (%s)", lab);
11965 		  continue;
11966 		}
11967 	      dw2_asm_output_data (1, DW_RLE_start_length,
11968 				   "DW_RLE_start_length (%s)", lab);
11969 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11970 				   "Range begin address (%s)", lab);
11971 	      dw2_asm_output_delta_uleb128 (elabel, blabel,
11972 					    "Range length (%s)", lab);
11973 	    }
11974 	  else
11975 	    {
11976 	      dw2_asm_output_data (1, DW_RLE_start_end,
11977 				   "DW_RLE_start_end (%s)", lab);
11978 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11979 				   "Range begin address (%s)", lab);
11980 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11981 				   "Range end address (%s)", lab);
11982 	    }
11983 	}
11984 
11985       /* Negative block_num stands for an index into ranges_by_label.  */
11986       else if (block_num < 0)
11987 	{
11988 	  int lab_idx = - block_num - 1;
11989 	  const char *blabel = (*ranges_by_label)[lab_idx].begin;
11990 	  const char *elabel = (*ranges_by_label)[lab_idx].end;
11991 
11992 	  if (!have_multiple_function_sections)
11993 	    gcc_unreachable ();
11994 	  if (HAVE_AS_LEB128)
11995 	    {
11996 	      dw2_asm_output_data (1, DW_RLE_start_length,
11997 				   "DW_RLE_start_length (%s)", lab);
11998 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11999 				   "Range begin address (%s)", lab);
12000 	      dw2_asm_output_delta_uleb128 (elabel, blabel,
12001 					    "Range length (%s)", lab);
12002 	    }
12003 	  else
12004 	    {
12005 	      dw2_asm_output_data (1, DW_RLE_start_end,
12006 				   "DW_RLE_start_end (%s)", lab);
12007 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12008 				   "Range begin address (%s)", lab);
12009 	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12010 				   "Range end address (%s)", lab);
12011 	    }
12012 	}
12013       else
12014 	dw2_asm_output_data (1, DW_RLE_end_of_list,
12015 			     "DW_RLE_end_of_list (%s)", lab);
12016     }
12017   ASM_OUTPUT_LABEL (asm_out_file, l2);
12018 }
12019 
12020 /* Data structure containing information about input files.  */
12021 struct file_info
12022 {
12023   const char *path;	/* Complete file name.  */
12024   const char *fname;	/* File name part.  */
12025   int length;		/* Length of entire string.  */
12026   struct dwarf_file_data * file_idx;	/* Index in input file table.  */
12027   int dir_idx;		/* Index in directory table.  */
12028 };
12029 
12030 /* Data structure containing information about directories with source
12031    files.  */
12032 struct dir_info
12033 {
12034   const char *path;	/* Path including directory name.  */
12035   int length;		/* Path length.  */
12036   int prefix;		/* Index of directory entry which is a prefix.  */
12037   int count;		/* Number of files in this directory.  */
12038   int dir_idx;		/* Index of directory used as base.  */
12039 };
12040 
12041 /* Callback function for file_info comparison.  We sort by looking at
12042    the directories in the path.  */
12043 
12044 static int
file_info_cmp(const void * p1,const void * p2)12045 file_info_cmp (const void *p1, const void *p2)
12046 {
12047   const struct file_info *const s1 = (const struct file_info *) p1;
12048   const struct file_info *const s2 = (const struct file_info *) p2;
12049   const unsigned char *cp1;
12050   const unsigned char *cp2;
12051 
12052   /* Take care of file names without directories.  We need to make sure that
12053      we return consistent values to qsort since some will get confused if
12054      we return the same value when identical operands are passed in opposite
12055      orders.  So if neither has a directory, return 0 and otherwise return
12056      1 or -1 depending on which one has the directory.  We want the one with
12057      the directory to sort after the one without, so all no directory files
12058      are at the start (normally only the compilation unit file).  */
12059   if ((s1->path == s1->fname || s2->path == s2->fname))
12060     return (s2->path == s2->fname) - (s1->path == s1->fname);
12061 
12062   cp1 = (const unsigned char *) s1->path;
12063   cp2 = (const unsigned char *) s2->path;
12064 
12065   while (1)
12066     {
12067       ++cp1;
12068       ++cp2;
12069       /* Reached the end of the first path?  If so, handle like above,
12070 	 but now we want longer directory prefixes before shorter ones.  */
12071       if ((cp1 == (const unsigned char *) s1->fname)
12072 	  || (cp2 == (const unsigned char *) s2->fname))
12073 	return ((cp1 == (const unsigned char *) s1->fname)
12074 		- (cp2 == (const unsigned char *) s2->fname));
12075 
12076       /* Character of current path component the same?  */
12077       else if (*cp1 != *cp2)
12078 	return *cp1 - *cp2;
12079     }
12080 }
12081 
12082 struct file_name_acquire_data
12083 {
12084   struct file_info *files;
12085   int used_files;
12086   int max_files;
12087 };
12088 
12089 /* Traversal function for the hash table.  */
12090 
12091 int
file_name_acquire(dwarf_file_data ** slot,file_name_acquire_data * fnad)12092 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12093 {
12094   struct dwarf_file_data *d = *slot;
12095   struct file_info *fi;
12096   const char *f;
12097 
12098   gcc_assert (fnad->max_files >= d->emitted_number);
12099 
12100   if (! d->emitted_number)
12101     return 1;
12102 
12103   gcc_assert (fnad->max_files != fnad->used_files);
12104 
12105   fi = fnad->files + fnad->used_files++;
12106 
12107   f = remap_debug_filename (d->filename);
12108 
12109   /* Skip all leading "./".  */
12110   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12111     f += 2;
12112 
12113   /* Create a new array entry.  */
12114   fi->path = f;
12115   fi->length = strlen (f);
12116   fi->file_idx = d;
12117 
12118   /* Search for the file name part.  */
12119   f = strrchr (f, DIR_SEPARATOR);
12120 #if defined (DIR_SEPARATOR_2)
12121   {
12122     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12123 
12124     if (g != NULL)
12125       {
12126 	if (f == NULL || f < g)
12127 	  f = g;
12128       }
12129   }
12130 #endif
12131 
12132   fi->fname = f == NULL ? fi->path : f + 1;
12133   return 1;
12134 }
12135 
12136 /* Helper function for output_file_names.  Emit a FORM encoded
12137    string STR, with assembly comment start ENTRY_KIND and
12138    index IDX */
12139 
12140 static void
output_line_string(enum dwarf_form form,const char * str,const char * entry_kind,unsigned int idx)12141 output_line_string (enum dwarf_form form, const char *str,
12142 		    const char *entry_kind, unsigned int idx)
12143 {
12144   switch (form)
12145     {
12146     case DW_FORM_string:
12147       dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12148       break;
12149     case DW_FORM_line_strp:
12150       if (!debug_line_str_hash)
12151 	debug_line_str_hash
12152 	  = hash_table<indirect_string_hasher>::create_ggc (10);
12153 
12154       struct indirect_string_node *node;
12155       node = find_AT_string_in_table (str, debug_line_str_hash);
12156       set_indirect_string (node);
12157       node->form = form;
12158       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12159 			     debug_line_str_section, "%s: %#x: \"%s\"",
12160 			     entry_kind, 0, node->str);
12161       break;
12162     default:
12163       gcc_unreachable ();
12164     }
12165 }
12166 
12167 /* Output the directory table and the file name table.  We try to minimize
12168    the total amount of memory needed.  A heuristic is used to avoid large
12169    slowdowns with many input files.  */
12170 
12171 static void
output_file_names(void)12172 output_file_names (void)
12173 {
12174   struct file_name_acquire_data fnad;
12175   int numfiles;
12176   struct file_info *files;
12177   struct dir_info *dirs;
12178   int *saved;
12179   int *savehere;
12180   int *backmap;
12181   int ndirs;
12182   int idx_offset;
12183   int i;
12184 
12185   if (!last_emitted_file)
12186     {
12187       if (dwarf_version >= 5)
12188 	{
12189 	  dw2_asm_output_data (1, 0, "Directory entry format count");
12190 	  dw2_asm_output_data_uleb128 (0, "Directories count");
12191 	  dw2_asm_output_data (1, 0, "File name entry format count");
12192 	  dw2_asm_output_data_uleb128 (0, "File names count");
12193 	}
12194       else
12195 	{
12196 	  dw2_asm_output_data (1, 0, "End directory table");
12197 	  dw2_asm_output_data (1, 0, "End file name table");
12198 	}
12199       return;
12200     }
12201 
12202   numfiles = last_emitted_file->emitted_number;
12203 
12204   /* Allocate the various arrays we need.  */
12205   files = XALLOCAVEC (struct file_info, numfiles);
12206   dirs = XALLOCAVEC (struct dir_info, numfiles);
12207 
12208   fnad.files = files;
12209   fnad.used_files = 0;
12210   fnad.max_files = numfiles;
12211   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12212   gcc_assert (fnad.used_files == fnad.max_files);
12213 
12214   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12215 
12216   /* Find all the different directories used.  */
12217   dirs[0].path = files[0].path;
12218   dirs[0].length = files[0].fname - files[0].path;
12219   dirs[0].prefix = -1;
12220   dirs[0].count = 1;
12221   dirs[0].dir_idx = 0;
12222   files[0].dir_idx = 0;
12223   ndirs = 1;
12224 
12225   for (i = 1; i < numfiles; i++)
12226     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12227 	&& memcmp (dirs[ndirs - 1].path, files[i].path,
12228 		   dirs[ndirs - 1].length) == 0)
12229       {
12230 	/* Same directory as last entry.  */
12231 	files[i].dir_idx = ndirs - 1;
12232 	++dirs[ndirs - 1].count;
12233       }
12234     else
12235       {
12236 	int j;
12237 
12238 	/* This is a new directory.  */
12239 	dirs[ndirs].path = files[i].path;
12240 	dirs[ndirs].length = files[i].fname - files[i].path;
12241 	dirs[ndirs].count = 1;
12242 	dirs[ndirs].dir_idx = ndirs;
12243 	files[i].dir_idx = ndirs;
12244 
12245 	/* Search for a prefix.  */
12246 	dirs[ndirs].prefix = -1;
12247 	for (j = 0; j < ndirs; j++)
12248 	  if (dirs[j].length < dirs[ndirs].length
12249 	      && dirs[j].length > 1
12250 	      && (dirs[ndirs].prefix == -1
12251 		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12252 	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12253 	    dirs[ndirs].prefix = j;
12254 
12255 	++ndirs;
12256       }
12257 
12258   /* Now to the actual work.  We have to find a subset of the directories which
12259      allow expressing the file name using references to the directory table
12260      with the least amount of characters.  We do not do an exhaustive search
12261      where we would have to check out every combination of every single
12262      possible prefix.  Instead we use a heuristic which provides nearly optimal
12263      results in most cases and never is much off.  */
12264   saved = XALLOCAVEC (int, ndirs);
12265   savehere = XALLOCAVEC (int, ndirs);
12266 
12267   memset (saved, '\0', ndirs * sizeof (saved[0]));
12268   for (i = 0; i < ndirs; i++)
12269     {
12270       int j;
12271       int total;
12272 
12273       /* We can always save some space for the current directory.  But this
12274 	 does not mean it will be enough to justify adding the directory.  */
12275       savehere[i] = dirs[i].length;
12276       total = (savehere[i] - saved[i]) * dirs[i].count;
12277 
12278       for (j = i + 1; j < ndirs; j++)
12279 	{
12280 	  savehere[j] = 0;
12281 	  if (saved[j] < dirs[i].length)
12282 	    {
12283 	      /* Determine whether the dirs[i] path is a prefix of the
12284 		 dirs[j] path.  */
12285 	      int k;
12286 
12287 	      k = dirs[j].prefix;
12288 	      while (k != -1 && k != (int) i)
12289 		k = dirs[k].prefix;
12290 
12291 	      if (k == (int) i)
12292 		{
12293 		  /* Yes it is.  We can possibly save some memory by
12294 		     writing the filenames in dirs[j] relative to
12295 		     dirs[i].  */
12296 		  savehere[j] = dirs[i].length;
12297 		  total += (savehere[j] - saved[j]) * dirs[j].count;
12298 		}
12299 	    }
12300 	}
12301 
12302       /* Check whether we can save enough to justify adding the dirs[i]
12303 	 directory.  */
12304       if (total > dirs[i].length + 1)
12305 	{
12306 	  /* It's worthwhile adding.  */
12307 	  for (j = i; j < ndirs; j++)
12308 	    if (savehere[j] > 0)
12309 	      {
12310 		/* Remember how much we saved for this directory so far.  */
12311 		saved[j] = savehere[j];
12312 
12313 		/* Remember the prefix directory.  */
12314 		dirs[j].dir_idx = i;
12315 	      }
12316 	}
12317     }
12318 
12319   /* Emit the directory name table.  */
12320   idx_offset = dirs[0].length > 0 ? 1 : 0;
12321   enum dwarf_form str_form = DW_FORM_string;
12322   enum dwarf_form idx_form = DW_FORM_udata;
12323   if (dwarf_version >= 5)
12324     {
12325       const char *comp_dir = comp_dir_string ();
12326       if (comp_dir == NULL)
12327 	comp_dir = "";
12328       dw2_asm_output_data (1, 1, "Directory entry format count");
12329       if (DWARF5_USE_DEBUG_LINE_STR)
12330 	str_form = DW_FORM_line_strp;
12331       dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12332       dw2_asm_output_data_uleb128 (str_form, "%s",
12333 				   get_DW_FORM_name (str_form));
12334       dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12335       if (str_form == DW_FORM_string)
12336 	{
12337 	  dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12338 	  for (i = 1 - idx_offset; i < ndirs; i++)
12339 	    dw2_asm_output_nstring (dirs[i].path,
12340 				    dirs[i].length
12341 				    - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12342 				    "Directory Entry: %#x", i + idx_offset);
12343 	}
12344       else
12345 	{
12346 	  output_line_string (str_form, comp_dir, "Directory Entry", 0);
12347 	  for (i = 1 - idx_offset; i < ndirs; i++)
12348 	    {
12349 	      const char *str
12350 		= ggc_alloc_string (dirs[i].path,
12351 				    dirs[i].length
12352 				    - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12353 	      output_line_string (str_form, str, "Directory Entry",
12354 				  (unsigned) i + idx_offset);
12355 	    }
12356 	}
12357     }
12358   else
12359     {
12360       for (i = 1 - idx_offset; i < ndirs; i++)
12361 	dw2_asm_output_nstring (dirs[i].path,
12362 				dirs[i].length
12363 				- !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12364 				"Directory Entry: %#x", i + idx_offset);
12365 
12366       dw2_asm_output_data (1, 0, "End directory table");
12367     }
12368 
12369   /* We have to emit them in the order of emitted_number since that's
12370      used in the debug info generation.  To do this efficiently we
12371      generate a back-mapping of the indices first.  */
12372   backmap = XALLOCAVEC (int, numfiles);
12373   for (i = 0; i < numfiles; i++)
12374     backmap[files[i].file_idx->emitted_number - 1] = i;
12375 
12376   if (dwarf_version >= 5)
12377     {
12378       const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12379       if (filename0 == NULL)
12380 	filename0 = "";
12381       /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12382 	 DW_FORM_data2.  Choose one based on the number of directories
12383 	 and how much space would they occupy in each encoding.
12384 	 If we have at most 256 directories, all indexes fit into
12385 	 a single byte, so DW_FORM_data1 is most compact (if there
12386 	 are at most 128 directories, DW_FORM_udata would be as
12387 	 compact as that, but not shorter and slower to decode).  */
12388       if (ndirs + idx_offset <= 256)
12389 	idx_form = DW_FORM_data1;
12390       /* If there are more than 65536 directories, we have to use
12391 	 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12392 	 Otherwise, compute what space would occupy if all the indexes
12393 	 used DW_FORM_udata - sum - and compare that to how large would
12394 	 be DW_FORM_data2 encoding, and pick the more efficient one.  */
12395       else if (ndirs + idx_offset <= 65536)
12396 	{
12397 	  unsigned HOST_WIDE_INT sum = 1;
12398 	  for (i = 0; i < numfiles; i++)
12399 	    {
12400 	      int file_idx = backmap[i];
12401 	      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12402 	      sum += size_of_uleb128 (dir_idx);
12403 	    }
12404 	  if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12405 	    idx_form = DW_FORM_data2;
12406 	}
12407 #ifdef VMS_DEBUGGING_INFO
12408       dw2_asm_output_data (1, 4, "File name entry format count");
12409 #else
12410       dw2_asm_output_data (1, 2, "File name entry format count");
12411 #endif
12412       dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12413       dw2_asm_output_data_uleb128 (str_form, "%s",
12414 				   get_DW_FORM_name (str_form));
12415       dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12416 				   "DW_LNCT_directory_index");
12417       dw2_asm_output_data_uleb128 (idx_form, "%s",
12418 				   get_DW_FORM_name (idx_form));
12419 #ifdef VMS_DEBUGGING_INFO
12420       dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12421       dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12422       dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12423       dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12424 #endif
12425       dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12426 
12427       output_line_string (str_form, filename0, "File Entry", 0);
12428 
12429       /* Include directory index.  */
12430       if (idx_form != DW_FORM_udata)
12431 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12432 			     0, NULL);
12433       else
12434 	dw2_asm_output_data_uleb128 (0, NULL);
12435 
12436 #ifdef VMS_DEBUGGING_INFO
12437       dw2_asm_output_data_uleb128 (0, NULL);
12438       dw2_asm_output_data_uleb128 (0, NULL);
12439 #endif
12440     }
12441 
12442   /* Now write all the file names.  */
12443   for (i = 0; i < numfiles; i++)
12444     {
12445       int file_idx = backmap[i];
12446       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12447 
12448 #ifdef VMS_DEBUGGING_INFO
12449 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12450 
12451       /* Setting these fields can lead to debugger miscomparisons,
12452          but VMS Debug requires them to be set correctly.  */
12453 
12454       int ver;
12455       long long cdt;
12456       long siz;
12457       int maxfilelen = (strlen (files[file_idx].path)
12458 			+ dirs[dir_idx].length
12459 			+ MAX_VMS_VERSION_LEN + 1);
12460       char *filebuf = XALLOCAVEC (char, maxfilelen);
12461 
12462       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12463       snprintf (filebuf, maxfilelen, "%s;%d",
12464 	        files[file_idx].path + dirs[dir_idx].length, ver);
12465 
12466       output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12467 
12468       /* Include directory index.  */
12469       if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12470 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12471 			     dir_idx + idx_offset, NULL);
12472       else
12473 	dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12474 
12475       /* Modification time.  */
12476       dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12477 							 &cdt, 0, 0, 0) == 0)
12478 				   ? cdt : 0, NULL);
12479 
12480       /* File length in bytes.  */
12481       dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12482 							 0, &siz, 0, 0) == 0)
12483 				   ? siz : 0, NULL);
12484 #else
12485       output_line_string (str_form,
12486 			  files[file_idx].path + dirs[dir_idx].length,
12487 			  "File Entry", (unsigned) i + 1);
12488 
12489       /* Include directory index.  */
12490       if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12491 	dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12492 			     dir_idx + idx_offset, NULL);
12493       else
12494 	dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12495 
12496       if (dwarf_version >= 5)
12497 	continue;
12498 
12499       /* Modification time.  */
12500       dw2_asm_output_data_uleb128 (0, NULL);
12501 
12502       /* File length in bytes.  */
12503       dw2_asm_output_data_uleb128 (0, NULL);
12504 #endif /* VMS_DEBUGGING_INFO */
12505     }
12506 
12507   if (dwarf_version < 5)
12508     dw2_asm_output_data (1, 0, "End file name table");
12509 }
12510 
12511 
12512 /* Output one line number table into the .debug_line section.  */
12513 
12514 static void
output_one_line_info_table(dw_line_info_table * table)12515 output_one_line_info_table (dw_line_info_table *table)
12516 {
12517   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12518   unsigned int current_line = 1;
12519   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12520   dw_line_info_entry *ent, *prev_addr;
12521   size_t i;
12522   unsigned int view;
12523 
12524   view = 0;
12525 
12526   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12527     {
12528       switch (ent->opcode)
12529 	{
12530 	case LI_set_address:
12531 	  /* ??? Unfortunately, we have little choice here currently, and
12532 	     must always use the most general form.  GCC does not know the
12533 	     address delta itself, so we can't use DW_LNS_advance_pc.  Many
12534 	     ports do have length attributes which will give an upper bound
12535 	     on the address range.  We could perhaps use length attributes
12536 	     to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12537 	  ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12538 
12539 	  view = 0;
12540 
12541 	  /* This can handle any delta.  This takes
12542 	     4+DWARF2_ADDR_SIZE bytes.  */
12543 	  dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12544 			       debug_variable_location_views
12545 			       ? ", reset view to 0" : "");
12546 	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12547 	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12548 	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12549 
12550 	  prev_addr = ent;
12551 	  break;
12552 
12553 	case LI_adv_address:
12554 	  {
12555 	    ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12556 	    char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12557 	    ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12558 
12559 	    view++;
12560 
12561 	    dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12562 	    dw2_asm_output_delta (2, line_label, prev_label,
12563 				  "from %s to %s", prev_label, line_label);
12564 
12565 	    prev_addr = ent;
12566 	    break;
12567 	  }
12568 
12569 	case LI_set_line:
12570 	  if (ent->val == current_line)
12571 	    {
12572 	      /* We still need to start a new row, so output a copy insn.  */
12573 	      dw2_asm_output_data (1, DW_LNS_copy,
12574 				   "copy line %u", current_line);
12575 	    }
12576 	  else
12577 	    {
12578 	      int line_offset = ent->val - current_line;
12579 	      int line_delta = line_offset - DWARF_LINE_BASE;
12580 
12581 	      current_line = ent->val;
12582 	      if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12583 		{
12584 		  /* This can handle deltas from -10 to 234, using the current
12585 		     definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12586 		     This takes 1 byte.  */
12587 		  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12588 				       "line %u", current_line);
12589 		}
12590 	      else
12591 		{
12592 		  /* This can handle any delta.  This takes at least 4 bytes,
12593 		     depending on the value being encoded.  */
12594 		  dw2_asm_output_data (1, DW_LNS_advance_line,
12595 				       "advance to line %u", current_line);
12596 		  dw2_asm_output_data_sleb128 (line_offset, NULL);
12597 		  dw2_asm_output_data (1, DW_LNS_copy, NULL);
12598 		}
12599 	    }
12600 	  break;
12601 
12602 	case LI_set_file:
12603 	  dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12604 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12605 	  break;
12606 
12607 	case LI_set_column:
12608 	  dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12609 	  dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12610 	  break;
12611 
12612 	case LI_negate_stmt:
12613 	  current_is_stmt = !current_is_stmt;
12614 	  dw2_asm_output_data (1, DW_LNS_negate_stmt,
12615 			       "is_stmt %d", current_is_stmt);
12616 	  break;
12617 
12618 	case LI_set_prologue_end:
12619 	  dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12620 			       "set prologue end");
12621 	  break;
12622 
12623 	case LI_set_epilogue_begin:
12624 	  dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12625 			       "set epilogue begin");
12626 	  break;
12627 
12628 	case LI_set_discriminator:
12629 	  dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12630 	  dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12631 	  dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12632 	  dw2_asm_output_data_uleb128 (ent->val, NULL);
12633 	  break;
12634 	}
12635     }
12636 
12637   /* Emit debug info for the address of the end of the table.  */
12638   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12639   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12640   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12641   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12642 
12643   dw2_asm_output_data (1, 0, "end sequence");
12644   dw2_asm_output_data_uleb128 (1, NULL);
12645   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12646 }
12647 
12648 /* Output the source line number correspondence information.  This
12649    information goes into the .debug_line section.  */
12650 
12651 static void
output_line_info(bool prologue_only)12652 output_line_info (bool prologue_only)
12653 {
12654   static unsigned int generation;
12655   char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12656   char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12657   bool saw_one = false;
12658   int opc;
12659 
12660   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12661   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12662   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12663   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12664 
12665   if (!XCOFF_DEBUGGING_INFO)
12666     {
12667       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12668 	dw2_asm_output_data (4, 0xffffffff,
12669 	  "Initial length escape value indicating 64-bit DWARF extension");
12670       dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12671 			    "Length of Source Line Info");
12672     }
12673 
12674   ASM_OUTPUT_LABEL (asm_out_file, l1);
12675 
12676   output_dwarf_version ();
12677   if (dwarf_version >= 5)
12678     {
12679       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12680       dw2_asm_output_data (1, 0, "Segment Size");
12681     }
12682   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12683   ASM_OUTPUT_LABEL (asm_out_file, p1);
12684 
12685   /* Define the architecture-dependent minimum instruction length (in bytes).
12686      In this implementation of DWARF, this field is used for information
12687      purposes only.  Since GCC generates assembly language, we have no
12688      a priori knowledge of how many instruction bytes are generated for each
12689      source line, and therefore can use only the DW_LNE_set_address and
12690      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12691      this as '1', which is "correct enough" for all architectures,
12692      and don't let the target override.  */
12693   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12694 
12695   if (dwarf_version >= 4)
12696     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12697 			 "Maximum Operations Per Instruction");
12698   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12699 		       "Default is_stmt_start flag");
12700   dw2_asm_output_data (1, DWARF_LINE_BASE,
12701 		       "Line Base Value (Special Opcodes)");
12702   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12703 		       "Line Range Value (Special Opcodes)");
12704   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12705 		       "Special Opcode Base");
12706 
12707   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12708     {
12709       int n_op_args;
12710       switch (opc)
12711 	{
12712 	case DW_LNS_advance_pc:
12713 	case DW_LNS_advance_line:
12714 	case DW_LNS_set_file:
12715 	case DW_LNS_set_column:
12716 	case DW_LNS_fixed_advance_pc:
12717 	case DW_LNS_set_isa:
12718 	  n_op_args = 1;
12719 	  break;
12720 	default:
12721 	  n_op_args = 0;
12722 	  break;
12723 	}
12724 
12725       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12726 			   opc, n_op_args);
12727     }
12728 
12729   /* Write out the information about the files we use.  */
12730   output_file_names ();
12731   ASM_OUTPUT_LABEL (asm_out_file, p2);
12732   if (prologue_only)
12733     {
12734       /* Output the marker for the end of the line number info.  */
12735       ASM_OUTPUT_LABEL (asm_out_file, l2);
12736       return;
12737     }
12738 
12739   if (separate_line_info)
12740     {
12741       dw_line_info_table *table;
12742       size_t i;
12743 
12744       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12745 	if (table->in_use)
12746 	  {
12747 	    output_one_line_info_table (table);
12748 	    saw_one = true;
12749 	  }
12750     }
12751   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12752     {
12753       output_one_line_info_table (cold_text_section_line_info);
12754       saw_one = true;
12755     }
12756 
12757   /* ??? Some Darwin linkers crash on a .debug_line section with no
12758      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12759      sufficient -- the address column must also be initialized.
12760      Make sure to output at least one set_address/end_sequence pair,
12761      choosing .text since that section is always present.  */
12762   if (text_section_line_info->in_use || !saw_one)
12763     output_one_line_info_table (text_section_line_info);
12764 
12765   /* Output the marker for the end of the line number info.  */
12766   ASM_OUTPUT_LABEL (asm_out_file, l2);
12767 }
12768 
12769 /* Return true if DW_AT_endianity should be emitted according to REVERSE.  */
12770 
12771 static inline bool
need_endianity_attribute_p(bool reverse)12772 need_endianity_attribute_p (bool reverse)
12773 {
12774   return reverse && (dwarf_version >= 3 || !dwarf_strict);
12775 }
12776 
12777 /* Given a pointer to a tree node for some base type, return a pointer to
12778    a DIE that describes the given type.  REVERSE is true if the type is
12779    to be interpreted in the reverse storage order wrt the target order.
12780 
12781    This routine must only be called for GCC type nodes that correspond to
12782    Dwarf base (fundamental) types.  */
12783 
12784 static dw_die_ref
base_type_die(tree type,bool reverse)12785 base_type_die (tree type, bool reverse)
12786 {
12787   dw_die_ref base_type_result;
12788   enum dwarf_type encoding;
12789   bool fpt_used = false;
12790   struct fixed_point_type_info fpt_info;
12791   tree type_bias = NULL_TREE;
12792 
12793   /* If this is a subtype that should not be emitted as a subrange type,
12794      use the base type.  See subrange_type_for_debug_p.  */
12795   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12796     type = TREE_TYPE (type);
12797 
12798   switch (TREE_CODE (type))
12799     {
12800     case INTEGER_TYPE:
12801       if ((dwarf_version >= 4 || !dwarf_strict)
12802 	  && TYPE_NAME (type)
12803 	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12804 	  && DECL_IS_BUILTIN (TYPE_NAME (type))
12805 	  && DECL_NAME (TYPE_NAME (type)))
12806 	{
12807 	  const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12808 	  if (strcmp (name, "char16_t") == 0
12809 	      || strcmp (name, "char32_t") == 0)
12810 	    {
12811 	      encoding = DW_ATE_UTF;
12812 	      break;
12813 	    }
12814 	}
12815       if ((dwarf_version >= 3 || !dwarf_strict)
12816 	  && lang_hooks.types.get_fixed_point_type_info)
12817 	{
12818 	  memset (&fpt_info, 0, sizeof (fpt_info));
12819 	  if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12820 	    {
12821 	      fpt_used = true;
12822 	      encoding = ((TYPE_UNSIGNED (type))
12823 			  ? DW_ATE_unsigned_fixed
12824 			  : DW_ATE_signed_fixed);
12825 	      break;
12826 	    }
12827 	}
12828       if (TYPE_STRING_FLAG (type))
12829 	{
12830 	  if (TYPE_UNSIGNED (type))
12831 	    encoding = DW_ATE_unsigned_char;
12832 	  else
12833 	    encoding = DW_ATE_signed_char;
12834 	}
12835       else if (TYPE_UNSIGNED (type))
12836 	encoding = DW_ATE_unsigned;
12837       else
12838 	encoding = DW_ATE_signed;
12839 
12840       if (!dwarf_strict
12841 	  && lang_hooks.types.get_type_bias)
12842 	type_bias = lang_hooks.types.get_type_bias (type);
12843       break;
12844 
12845     case REAL_TYPE:
12846       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12847 	{
12848 	  if (dwarf_version >= 3 || !dwarf_strict)
12849 	    encoding = DW_ATE_decimal_float;
12850 	  else
12851 	    encoding = DW_ATE_lo_user;
12852 	}
12853       else
12854 	encoding = DW_ATE_float;
12855       break;
12856 
12857     case FIXED_POINT_TYPE:
12858       if (!(dwarf_version >= 3 || !dwarf_strict))
12859 	encoding = DW_ATE_lo_user;
12860       else if (TYPE_UNSIGNED (type))
12861 	encoding = DW_ATE_unsigned_fixed;
12862       else
12863 	encoding = DW_ATE_signed_fixed;
12864       break;
12865 
12866       /* Dwarf2 doesn't know anything about complex ints, so use
12867 	 a user defined type for it.  */
12868     case COMPLEX_TYPE:
12869       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12870 	encoding = DW_ATE_complex_float;
12871       else
12872 	encoding = DW_ATE_lo_user;
12873       break;
12874 
12875     case BOOLEAN_TYPE:
12876       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12877       encoding = DW_ATE_boolean;
12878       break;
12879 
12880     default:
12881       /* No other TREE_CODEs are Dwarf fundamental types.  */
12882       gcc_unreachable ();
12883     }
12884 
12885   base_type_result = new_die_raw (DW_TAG_base_type);
12886 
12887   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12888 		   int_size_in_bytes (type));
12889   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12890 
12891   if (need_endianity_attribute_p (reverse))
12892     add_AT_unsigned (base_type_result, DW_AT_endianity,
12893 		     BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12894 
12895   add_alignment_attribute (base_type_result, type);
12896 
12897   if (fpt_used)
12898     {
12899       switch (fpt_info.scale_factor_kind)
12900 	{
12901 	case fixed_point_scale_factor_binary:
12902 	  add_AT_int (base_type_result, DW_AT_binary_scale,
12903 		      fpt_info.scale_factor.binary);
12904 	  break;
12905 
12906 	case fixed_point_scale_factor_decimal:
12907 	  add_AT_int (base_type_result, DW_AT_decimal_scale,
12908 		      fpt_info.scale_factor.decimal);
12909 	  break;
12910 
12911 	case fixed_point_scale_factor_arbitrary:
12912 	  /* Arbitrary scale factors cannot be described in standard DWARF,
12913 	     yet.  */
12914 	  if (!dwarf_strict)
12915 	    {
12916 	      /* Describe the scale factor as a rational constant.  */
12917 	      const dw_die_ref scale_factor
12918 		= new_die (DW_TAG_constant, comp_unit_die (), type);
12919 
12920 	      add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12921 			       fpt_info.scale_factor.arbitrary.numerator);
12922 	      add_AT_int (scale_factor, DW_AT_GNU_denominator,
12923 			  fpt_info.scale_factor.arbitrary.denominator);
12924 
12925 	      add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12926 	    }
12927 	  break;
12928 
12929 	default:
12930 	  gcc_unreachable ();
12931 	}
12932     }
12933 
12934   if (type_bias)
12935     add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12936 		     dw_scalar_form_constant
12937 		     | dw_scalar_form_exprloc
12938 		     | dw_scalar_form_reference,
12939 		     NULL);
12940 
12941   return base_type_result;
12942 }
12943 
12944 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12945    named 'auto' in its type: return true for it, false otherwise.  */
12946 
12947 static inline bool
is_cxx_auto(tree type)12948 is_cxx_auto (tree type)
12949 {
12950   if (is_cxx ())
12951     {
12952       tree name = TYPE_IDENTIFIER (type);
12953       if (name == get_identifier ("auto")
12954 	  || name == get_identifier ("decltype(auto)"))
12955 	return true;
12956     }
12957   return false;
12958 }
12959 
12960 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12961    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12962 
12963 static inline int
is_base_type(tree type)12964 is_base_type (tree type)
12965 {
12966   switch (TREE_CODE (type))
12967     {
12968     case INTEGER_TYPE:
12969     case REAL_TYPE:
12970     case FIXED_POINT_TYPE:
12971     case COMPLEX_TYPE:
12972     case BOOLEAN_TYPE:
12973       return 1;
12974 
12975     case VOID_TYPE:
12976     case ARRAY_TYPE:
12977     case RECORD_TYPE:
12978     case UNION_TYPE:
12979     case QUAL_UNION_TYPE:
12980     case ENUMERAL_TYPE:
12981     case FUNCTION_TYPE:
12982     case METHOD_TYPE:
12983     case POINTER_TYPE:
12984     case REFERENCE_TYPE:
12985     case NULLPTR_TYPE:
12986     case OFFSET_TYPE:
12987     case LANG_TYPE:
12988     case VECTOR_TYPE:
12989       return 0;
12990 
12991     default:
12992       if (is_cxx_auto (type))
12993 	return 0;
12994       gcc_unreachable ();
12995     }
12996 
12997   return 0;
12998 }
12999 
13000 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13001    node, return the size in bits for the type if it is a constant, or else
13002    return the alignment for the type if the type's size is not constant, or
13003    else return BITS_PER_WORD if the type actually turns out to be an
13004    ERROR_MARK node.  */
13005 
13006 static inline unsigned HOST_WIDE_INT
simple_type_size_in_bits(const_tree type)13007 simple_type_size_in_bits (const_tree type)
13008 {
13009   if (TREE_CODE (type) == ERROR_MARK)
13010     return BITS_PER_WORD;
13011   else if (TYPE_SIZE (type) == NULL_TREE)
13012     return 0;
13013   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13014     return tree_to_uhwi (TYPE_SIZE (type));
13015   else
13016     return TYPE_ALIGN (type);
13017 }
13018 
13019 /* Similarly, but return an offset_int instead of UHWI.  */
13020 
13021 static inline offset_int
offset_int_type_size_in_bits(const_tree type)13022 offset_int_type_size_in_bits (const_tree type)
13023 {
13024   if (TREE_CODE (type) == ERROR_MARK)
13025     return BITS_PER_WORD;
13026   else if (TYPE_SIZE (type) == NULL_TREE)
13027     return 0;
13028   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13029     return wi::to_offset (TYPE_SIZE (type));
13030   else
13031     return TYPE_ALIGN (type);
13032 }
13033 
13034 /*  Given a pointer to a tree node for a subrange type, return a pointer
13035     to a DIE that describes the given type.  */
13036 
13037 static dw_die_ref
subrange_type_die(tree type,tree low,tree high,tree bias,dw_die_ref context_die)13038 subrange_type_die (tree type, tree low, tree high, tree bias,
13039 		   dw_die_ref context_die)
13040 {
13041   dw_die_ref subrange_die;
13042   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13043 
13044   if (context_die == NULL)
13045     context_die = comp_unit_die ();
13046 
13047   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13048 
13049   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13050     {
13051       /* The size of the subrange type and its base type do not match,
13052 	 so we need to generate a size attribute for the subrange type.  */
13053       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13054     }
13055 
13056   add_alignment_attribute (subrange_die, type);
13057 
13058   if (low)
13059     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13060   if (high)
13061     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13062   if (bias && !dwarf_strict)
13063     add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13064 		     dw_scalar_form_constant
13065 		     | dw_scalar_form_exprloc
13066 		     | dw_scalar_form_reference,
13067 		     NULL);
13068 
13069   return subrange_die;
13070 }
13071 
13072 /* Returns the (const and/or volatile) cv_qualifiers associated with
13073    the decl node.  This will normally be augmented with the
13074    cv_qualifiers of the underlying type in add_type_attribute.  */
13075 
13076 static int
decl_quals(const_tree decl)13077 decl_quals (const_tree decl)
13078 {
13079   return ((TREE_READONLY (decl)
13080 	   /* The C++ front-end correctly marks reference-typed
13081 	      variables as readonly, but from a language (and debug
13082 	      info) standpoint they are not const-qualified.  */
13083 	   && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13084 	   ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13085 	  | (TREE_THIS_VOLATILE (decl)
13086 	     ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13087 }
13088 
13089 /* Determine the TYPE whose qualifiers match the largest strict subset
13090    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
13091    qualifiers outside QUAL_MASK.  */
13092 
13093 static int
get_nearest_type_subqualifiers(tree type,int type_quals,int qual_mask)13094 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13095 {
13096   tree t;
13097   int best_rank = 0, best_qual = 0, max_rank;
13098 
13099   type_quals &= qual_mask;
13100   max_rank = popcount_hwi (type_quals) - 1;
13101 
13102   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13103        t = TYPE_NEXT_VARIANT (t))
13104     {
13105       int q = TYPE_QUALS (t) & qual_mask;
13106 
13107       if ((q & type_quals) == q && q != type_quals
13108 	  && check_base_type (t, type))
13109 	{
13110 	  int rank = popcount_hwi (q);
13111 
13112 	  if (rank > best_rank)
13113 	    {
13114 	      best_rank = rank;
13115 	      best_qual = q;
13116 	    }
13117 	}
13118     }
13119 
13120   return best_qual;
13121 }
13122 
13123 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13124 static const dwarf_qual_info_t dwarf_qual_info[] =
13125 {
13126   { TYPE_QUAL_CONST, DW_TAG_const_type },
13127   { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13128   { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13129   { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13130 };
13131 static const unsigned int dwarf_qual_info_size
13132   = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13133 
13134 /* If DIE is a qualified DIE of some base DIE with the same parent,
13135    return the base DIE, otherwise return NULL.  Set MASK to the
13136    qualifiers added compared to the returned DIE.  */
13137 
13138 static dw_die_ref
qualified_die_p(dw_die_ref die,int * mask,unsigned int depth)13139 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13140 {
13141   unsigned int i;
13142   for (i = 0; i < dwarf_qual_info_size; i++)
13143     if (die->die_tag == dwarf_qual_info[i].t)
13144       break;
13145   if (i == dwarf_qual_info_size)
13146     return NULL;
13147   if (vec_safe_length (die->die_attr) != 1)
13148     return NULL;
13149   dw_die_ref type = get_AT_ref (die, DW_AT_type);
13150   if (type == NULL || type->die_parent != die->die_parent)
13151     return NULL;
13152   *mask |= dwarf_qual_info[i].q;
13153   if (depth)
13154     {
13155       dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13156       if (ret)
13157 	return ret;
13158     }
13159   return type;
13160 }
13161 
13162 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13163    entry that chains the modifiers specified by CV_QUALS in front of the
13164    given type.  REVERSE is true if the type is to be interpreted in the
13165    reverse storage order wrt the target order.  */
13166 
13167 static dw_die_ref
modified_type_die(tree type,int cv_quals,bool reverse,dw_die_ref context_die)13168 modified_type_die (tree type, int cv_quals, bool reverse,
13169 		   dw_die_ref context_die)
13170 {
13171   enum tree_code code = TREE_CODE (type);
13172   dw_die_ref mod_type_die;
13173   dw_die_ref sub_die = NULL;
13174   tree item_type = NULL;
13175   tree qualified_type;
13176   tree name, low, high;
13177   dw_die_ref mod_scope;
13178   /* Only these cv-qualifiers are currently handled.  */
13179   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13180 			    | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13181 			    ENCODE_QUAL_ADDR_SPACE(~0U));
13182   const bool reverse_base_type
13183     = need_endianity_attribute_p (reverse) && is_base_type (type);
13184 
13185   if (code == ERROR_MARK)
13186     return NULL;
13187 
13188   if (lang_hooks.types.get_debug_type)
13189     {
13190       tree debug_type = lang_hooks.types.get_debug_type (type);
13191 
13192       if (debug_type != NULL_TREE && debug_type != type)
13193 	return modified_type_die (debug_type, cv_quals, reverse, context_die);
13194     }
13195 
13196   cv_quals &= cv_qual_mask;
13197 
13198   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13199      tag modifier (and not an attribute) old consumers won't be able
13200      to handle it.  */
13201   if (dwarf_version < 3)
13202     cv_quals &= ~TYPE_QUAL_RESTRICT;
13203 
13204   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
13205   if (dwarf_version < 5)
13206     cv_quals &= ~TYPE_QUAL_ATOMIC;
13207 
13208   /* See if we already have the appropriately qualified variant of
13209      this type.  */
13210   qualified_type = get_qualified_type (type, cv_quals);
13211 
13212   if (qualified_type == sizetype)
13213     {
13214       /* Try not to expose the internal sizetype type's name.  */
13215       if (TYPE_NAME (qualified_type)
13216 	  && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13217 	{
13218 	  tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13219 
13220 	  gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13221 			       && (TYPE_PRECISION (t)
13222 				   == TYPE_PRECISION (qualified_type))
13223 			       && (TYPE_UNSIGNED (t)
13224 				   == TYPE_UNSIGNED (qualified_type)));
13225 	  qualified_type = t;
13226 	}
13227       else if (qualified_type == sizetype
13228 	       && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13229 	       && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13230 	       && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13231 	qualified_type = size_type_node;
13232       if (type == sizetype)
13233 	type = qualified_type;
13234     }
13235 
13236   /* If we do, then we can just use its DIE, if it exists.  */
13237   if (qualified_type)
13238     {
13239       mod_type_die = lookup_type_die (qualified_type);
13240 
13241       /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13242 	 dealt with specially: the DIE with the attribute, if it exists, is
13243 	 placed immediately after the regular DIE for the same base type.  */
13244       if (mod_type_die
13245 	  && (!reverse_base_type
13246 	      || ((mod_type_die = mod_type_die->die_sib) != NULL
13247 		  && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13248 	return mod_type_die;
13249     }
13250 
13251   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13252 
13253   /* Handle C typedef types.  */
13254   if (name
13255       && TREE_CODE (name) == TYPE_DECL
13256       && DECL_ORIGINAL_TYPE (name)
13257       && !DECL_ARTIFICIAL (name))
13258     {
13259       tree dtype = TREE_TYPE (name);
13260 
13261       /* Skip the typedef for base types with DW_AT_endianity, no big deal.  */
13262       if (qualified_type == dtype && !reverse_base_type)
13263 	{
13264 	  tree origin = decl_ultimate_origin (name);
13265 
13266 	  /* Typedef variants that have an abstract origin don't get their own
13267 	     type DIE (see gen_typedef_die), so fall back on the ultimate
13268 	     abstract origin instead.  */
13269 	  if (origin != NULL && origin != name)
13270 	    return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13271 				      context_die);
13272 
13273 	  /* For a named type, use the typedef.  */
13274 	  gen_type_die (qualified_type, context_die);
13275 	  return lookup_type_die (qualified_type);
13276 	}
13277       else
13278 	{
13279 	  int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13280 	  dquals &= cv_qual_mask;
13281 	  if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13282 	      || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13283 	    /* cv-unqualified version of named type.  Just use
13284 	       the unnamed type to which it refers.  */
13285 	    return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13286 				      reverse, context_die);
13287 	  /* Else cv-qualified version of named type; fall through.  */
13288 	}
13289     }
13290 
13291   mod_scope = scope_die_for (type, context_die);
13292 
13293   if (cv_quals)
13294     {
13295       int sub_quals = 0, first_quals = 0;
13296       unsigned i;
13297       dw_die_ref first = NULL, last = NULL;
13298 
13299       /* Determine a lesser qualified type that most closely matches
13300 	 this one.  Then generate DW_TAG_* entries for the remaining
13301 	 qualifiers.  */
13302       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13303 						  cv_qual_mask);
13304       if (sub_quals && use_debug_types)
13305 	{
13306 	  bool needed = false;
13307 	  /* If emitting type units, make sure the order of qualifiers
13308 	     is canonical.  Thus, start from unqualified type if
13309 	     an earlier qualifier is missing in sub_quals, but some later
13310 	     one is present there.  */
13311 	  for (i = 0; i < dwarf_qual_info_size; i++)
13312 	    if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13313 	      needed = true;
13314 	    else if (needed && (dwarf_qual_info[i].q & cv_quals))
13315 	      {
13316 		sub_quals = 0;
13317 		break;
13318 	      }
13319 	}
13320       mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13321       if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13322 	{
13323 	  /* As not all intermediate qualified DIEs have corresponding
13324 	     tree types, ensure that qualified DIEs in the same scope
13325 	     as their DW_AT_type are emitted after their DW_AT_type,
13326 	     only with other qualified DIEs for the same type possibly
13327 	     in between them.  Determine the range of such qualified
13328 	     DIEs now (first being the base type, last being corresponding
13329 	     last qualified DIE for it).  */
13330 	  unsigned int count = 0;
13331 	  first = qualified_die_p (mod_type_die, &first_quals,
13332 				   dwarf_qual_info_size);
13333 	  if (first == NULL)
13334 	    first = mod_type_die;
13335 	  gcc_assert ((first_quals & ~sub_quals) == 0);
13336 	  for (count = 0, last = first;
13337 	       count < (1U << dwarf_qual_info_size);
13338 	       count++, last = last->die_sib)
13339 	    {
13340 	      int quals = 0;
13341 	      if (last == mod_scope->die_child)
13342 		break;
13343 	      if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13344 		  != first)
13345 		break;
13346 	    }
13347 	}
13348 
13349       for (i = 0; i < dwarf_qual_info_size; i++)
13350 	if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13351 	  {
13352 	    dw_die_ref d;
13353 	    if (first && first != last)
13354 	      {
13355 		for (d = first->die_sib; ; d = d->die_sib)
13356 		  {
13357 		    int quals = 0;
13358 		    qualified_die_p (d, &quals, dwarf_qual_info_size);
13359 		    if (quals == (first_quals | dwarf_qual_info[i].q))
13360 		      break;
13361 		    if (d == last)
13362 		      {
13363 			d = NULL;
13364 			break;
13365 		      }
13366 		  }
13367 		if (d)
13368 		  {
13369 		    mod_type_die = d;
13370 		    continue;
13371 		  }
13372 	      }
13373 	    if (first)
13374 	      {
13375 		d = new_die_raw (dwarf_qual_info[i].t);
13376 		add_child_die_after (mod_scope, d, last);
13377 		last = d;
13378 	      }
13379 	    else
13380 	      d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13381 	    if (mod_type_die)
13382 	      add_AT_die_ref (d, DW_AT_type, mod_type_die);
13383 	    mod_type_die = d;
13384 	    first_quals |= dwarf_qual_info[i].q;
13385 	  }
13386     }
13387   else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13388     {
13389       dwarf_tag tag = DW_TAG_pointer_type;
13390       if (code == REFERENCE_TYPE)
13391 	{
13392 	  if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13393 	    tag = DW_TAG_rvalue_reference_type;
13394 	  else
13395 	    tag = DW_TAG_reference_type;
13396 	}
13397       mod_type_die = new_die (tag, mod_scope, type);
13398 
13399       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13400 		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
13401       add_alignment_attribute (mod_type_die, type);
13402       item_type = TREE_TYPE (type);
13403 
13404       addr_space_t as = TYPE_ADDR_SPACE (item_type);
13405       if (!ADDR_SPACE_GENERIC_P (as))
13406 	{
13407 	  int action = targetm.addr_space.debug (as);
13408 	  if (action >= 0)
13409 	    {
13410 	      /* Positive values indicate an address_class.  */
13411 	      add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13412 	    }
13413 	  else
13414 	    {
13415 	      /* Negative values indicate an (inverted) segment base reg.  */
13416 	      dw_loc_descr_ref d
13417 		= one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13418 	      add_AT_loc (mod_type_die, DW_AT_segment, d);
13419 	    }
13420 	}
13421     }
13422   else if (code == INTEGER_TYPE
13423 	   && TREE_TYPE (type) != NULL_TREE
13424 	   && subrange_type_for_debug_p (type, &low, &high))
13425     {
13426       tree bias = NULL_TREE;
13427       if (lang_hooks.types.get_type_bias)
13428 	bias = lang_hooks.types.get_type_bias (type);
13429       mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13430       item_type = TREE_TYPE (type);
13431     }
13432   else if (is_base_type (type))
13433     {
13434       mod_type_die = base_type_die (type, reverse);
13435 
13436       /* The DIE with DW_AT_endianity is placed right after the naked DIE.  */
13437       if (reverse_base_type)
13438 	{
13439 	  dw_die_ref after_die
13440 	    = modified_type_die (type, cv_quals, false, context_die);
13441 	  add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13442 	}
13443       else
13444 	add_child_die (comp_unit_die (), mod_type_die);
13445 
13446       add_pubtype (type, mod_type_die);
13447     }
13448   else
13449     {
13450       gen_type_die (type, context_die);
13451 
13452       /* We have to get the type_main_variant here (and pass that to the
13453 	 `lookup_type_die' routine) because the ..._TYPE node we have
13454 	 might simply be a *copy* of some original type node (where the
13455 	 copy was created to help us keep track of typedef names) and
13456 	 that copy might have a different TYPE_UID from the original
13457 	 ..._TYPE node.  */
13458       if (TREE_CODE (type) == FUNCTION_TYPE
13459 	  || TREE_CODE (type) == METHOD_TYPE)
13460 	{
13461 	  /* For function/method types, can't just use type_main_variant here,
13462 	     because that can have different ref-qualifiers for C++,
13463 	     but try to canonicalize.  */
13464 	  tree main = TYPE_MAIN_VARIANT (type);
13465 	  for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13466 	    if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13467 		&& check_base_type (t, main)
13468 		&& check_lang_type (t, type))
13469 	      return lookup_type_die (t);
13470 	  return lookup_type_die (type);
13471 	}
13472       else if (TREE_CODE (type) != VECTOR_TYPE
13473 	       && TREE_CODE (type) != ARRAY_TYPE)
13474 	return lookup_type_die (type_main_variant (type));
13475       else
13476 	/* Vectors have the debugging information in the type,
13477 	   not the main variant.  */
13478 	return lookup_type_die (type);
13479     }
13480 
13481   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13482      don't output a DW_TAG_typedef, since there isn't one in the
13483      user's program; just attach a DW_AT_name to the type.
13484      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13485      if the base type already has the same name.  */
13486   if (name
13487       && ((TREE_CODE (name) != TYPE_DECL
13488 	   && (qualified_type == TYPE_MAIN_VARIANT (type)
13489 	       || (cv_quals == TYPE_UNQUALIFIED)))
13490 	  || (TREE_CODE (name) == TYPE_DECL
13491 	      && TREE_TYPE (name) == qualified_type
13492 	      && DECL_NAME (name))))
13493     {
13494       if (TREE_CODE (name) == TYPE_DECL)
13495 	/* Could just call add_name_and_src_coords_attributes here,
13496 	   but since this is a builtin type it doesn't have any
13497 	   useful source coordinates anyway.  */
13498 	name = DECL_NAME (name);
13499       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13500     }
13501   /* This probably indicates a bug.  */
13502   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13503     {
13504       name = TYPE_IDENTIFIER (type);
13505       add_name_attribute (mod_type_die,
13506 			  name ? IDENTIFIER_POINTER (name) : "__unknown__");
13507     }
13508 
13509   if (qualified_type && !reverse_base_type)
13510     equate_type_number_to_die (qualified_type, mod_type_die);
13511 
13512   if (item_type)
13513     /* We must do this after the equate_type_number_to_die call, in case
13514        this is a recursive type.  This ensures that the modified_type_die
13515        recursion will terminate even if the type is recursive.  Recursive
13516        types are possible in Ada.  */
13517     sub_die = modified_type_die (item_type,
13518 				 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13519 				 reverse,
13520 				 context_die);
13521 
13522   if (sub_die != NULL)
13523     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13524 
13525   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13526   if (TYPE_ARTIFICIAL (type))
13527     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13528 
13529   return mod_type_die;
13530 }
13531 
13532 /* Generate DIEs for the generic parameters of T.
13533    T must be either a generic type or a generic function.
13534    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13535 
13536 static void
gen_generic_params_dies(tree t)13537 gen_generic_params_dies (tree t)
13538 {
13539   tree parms, args;
13540   int parms_num, i;
13541   dw_die_ref die = NULL;
13542   int non_default;
13543 
13544   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13545     return;
13546 
13547   if (TYPE_P (t))
13548     die = lookup_type_die (t);
13549   else if (DECL_P (t))
13550     die = lookup_decl_die (t);
13551 
13552   gcc_assert (die);
13553 
13554   parms = lang_hooks.get_innermost_generic_parms (t);
13555   if (!parms)
13556     /* T has no generic parameter. It means T is neither a generic type
13557        or function. End of story.  */
13558     return;
13559 
13560   parms_num = TREE_VEC_LENGTH (parms);
13561   args = lang_hooks.get_innermost_generic_args (t);
13562   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13563     non_default = int_cst_value (TREE_CHAIN (args));
13564   else
13565     non_default = TREE_VEC_LENGTH (args);
13566   for (i = 0; i < parms_num; i++)
13567     {
13568       tree parm, arg, arg_pack_elems;
13569       dw_die_ref parm_die;
13570 
13571       parm = TREE_VEC_ELT (parms, i);
13572       arg = TREE_VEC_ELT (args, i);
13573       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13574       gcc_assert (parm && TREE_VALUE (parm) && arg);
13575 
13576       if (parm && TREE_VALUE (parm) && arg)
13577 	{
13578 	  /* If PARM represents a template parameter pack,
13579 	     emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13580 	     by DW_TAG_template_*_parameter DIEs for the argument
13581 	     pack elements of ARG. Note that ARG would then be
13582 	     an argument pack.  */
13583 	  if (arg_pack_elems)
13584 	    parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13585 						    arg_pack_elems,
13586 						    die);
13587 	  else
13588 	    parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13589 					      true /* emit name */, die);
13590 	  if (i >= non_default)
13591 	    add_AT_flag (parm_die, DW_AT_default_value, 1);
13592 	}
13593     }
13594 }
13595 
13596 /* Create and return a DIE for PARM which should be
13597    the representation of a generic type parameter.
13598    For instance, in the C++ front end, PARM would be a template parameter.
13599    ARG is the argument to PARM.
13600    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13601    name of the PARM.
13602    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13603    as a child node.  */
13604 
13605 static dw_die_ref
generic_parameter_die(tree parm,tree arg,bool emit_name_p,dw_die_ref parent_die)13606 generic_parameter_die (tree parm, tree arg,
13607 		       bool emit_name_p,
13608 		       dw_die_ref parent_die)
13609 {
13610   dw_die_ref tmpl_die = NULL;
13611   const char *name = NULL;
13612 
13613   /* C++2a accepts class literals as template parameters, and var
13614      decls with initializers represent them.  The VAR_DECLs would be
13615      rejected, but we can take the DECL_INITIAL constructor and
13616      attempt to expand it.  */
13617   if (arg && VAR_P (arg))
13618     arg = DECL_INITIAL (arg);
13619 
13620   if (!parm || !DECL_NAME (parm) || !arg)
13621     return NULL;
13622 
13623   /* We support non-type generic parameters and arguments,
13624      type generic parameters and arguments, as well as
13625      generic generic parameters (a.k.a. template template parameters in C++)
13626      and arguments.  */
13627   if (TREE_CODE (parm) == PARM_DECL)
13628     /* PARM is a nontype generic parameter  */
13629     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13630   else if (TREE_CODE (parm) == TYPE_DECL)
13631     /* PARM is a type generic parameter.  */
13632     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13633   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13634     /* PARM is a generic generic parameter.
13635        Its DIE is a GNU extension. It shall have a
13636        DW_AT_name attribute to represent the name of the template template
13637        parameter, and a DW_AT_GNU_template_name attribute to represent the
13638        name of the template template argument.  */
13639     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13640 			parent_die, parm);
13641   else
13642     gcc_unreachable ();
13643 
13644   if (tmpl_die)
13645     {
13646       tree tmpl_type;
13647 
13648       /* If PARM is a generic parameter pack, it means we are
13649          emitting debug info for a template argument pack element.
13650 	 In other terms, ARG is a template argument pack element.
13651 	 In that case, we don't emit any DW_AT_name attribute for
13652 	 the die.  */
13653       if (emit_name_p)
13654 	{
13655 	  name = IDENTIFIER_POINTER (DECL_NAME (parm));
13656 	  gcc_assert (name);
13657 	  add_AT_string (tmpl_die, DW_AT_name, name);
13658 	}
13659 
13660       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13661 	{
13662 	  /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13663 	     TMPL_DIE should have a child DW_AT_type attribute that is set
13664 	     to the type of the argument to PARM, which is ARG.
13665 	     If PARM is a type generic parameter, TMPL_DIE should have a
13666 	     child DW_AT_type that is set to ARG.  */
13667 	  tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13668 	  add_type_attribute (tmpl_die, tmpl_type,
13669 			      (TREE_THIS_VOLATILE (tmpl_type)
13670 			       ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13671 			      false, parent_die);
13672 	}
13673       else
13674 	{
13675 	  /* So TMPL_DIE is a DIE representing a
13676 	     a generic generic template parameter, a.k.a template template
13677 	     parameter in C++ and arg is a template.  */
13678 
13679 	  /* The DW_AT_GNU_template_name attribute of the DIE must be set
13680 	     to the name of the argument.  */
13681 	  name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13682 	  if (name)
13683 	    add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13684 	}
13685 
13686       if (TREE_CODE (parm) == PARM_DECL)
13687 	/* So PARM is a non-type generic parameter.
13688 	   DWARF3 5.6.8 says we must set a DW_AT_const_value child
13689 	   attribute of TMPL_DIE which value represents the value
13690 	   of ARG.
13691 	   We must be careful here:
13692 	   The value of ARG might reference some function decls.
13693 	   We might currently be emitting debug info for a generic
13694 	   type and types are emitted before function decls, we don't
13695 	   know if the function decls referenced by ARG will actually be
13696 	   emitted after cgraph computations.
13697 	   So must defer the generation of the DW_AT_const_value to
13698 	   after cgraph is ready.  */
13699 	append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13700     }
13701 
13702   return tmpl_die;
13703 }
13704 
13705 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13706    PARM_PACK must be a template parameter pack. The returned DIE
13707    will be child DIE of PARENT_DIE.  */
13708 
13709 static dw_die_ref
template_parameter_pack_die(tree parm_pack,tree parm_pack_args,dw_die_ref parent_die)13710 template_parameter_pack_die (tree parm_pack,
13711 			     tree parm_pack_args,
13712 			     dw_die_ref parent_die)
13713 {
13714   dw_die_ref die;
13715   int j;
13716 
13717   gcc_assert (parent_die && parm_pack);
13718 
13719   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13720   add_name_and_src_coords_attributes (die, parm_pack);
13721   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13722     generic_parameter_die (parm_pack,
13723 			   TREE_VEC_ELT (parm_pack_args, j),
13724 			   false /* Don't emit DW_AT_name */,
13725 			   die);
13726   return die;
13727 }
13728 
13729 /* Return the DBX register number described by a given RTL node.  */
13730 
13731 static unsigned int
dbx_reg_number(const_rtx rtl)13732 dbx_reg_number (const_rtx rtl)
13733 {
13734   unsigned regno = REGNO (rtl);
13735 
13736   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13737 
13738 #ifdef LEAF_REG_REMAP
13739   if (crtl->uses_only_leaf_regs)
13740     {
13741       int leaf_reg = LEAF_REG_REMAP (regno);
13742       if (leaf_reg != -1)
13743 	regno = (unsigned) leaf_reg;
13744     }
13745 #endif
13746 
13747   regno = DBX_REGISTER_NUMBER (regno);
13748   gcc_assert (regno != INVALID_REGNUM);
13749   return regno;
13750 }
13751 
13752 /* Optionally add a DW_OP_piece term to a location description expression.
13753    DW_OP_piece is only added if the location description expression already
13754    doesn't end with DW_OP_piece.  */
13755 
13756 static void
add_loc_descr_op_piece(dw_loc_descr_ref * list_head,int size)13757 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13758 {
13759   dw_loc_descr_ref loc;
13760 
13761   if (*list_head != NULL)
13762     {
13763       /* Find the end of the chain.  */
13764       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13765 	;
13766 
13767       if (loc->dw_loc_opc != DW_OP_piece)
13768 	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13769     }
13770 }
13771 
13772 /* Return a location descriptor that designates a machine register or
13773    zero if there is none.  */
13774 
13775 static dw_loc_descr_ref
reg_loc_descriptor(rtx rtl,enum var_init_status initialized)13776 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13777 {
13778   rtx regs;
13779 
13780   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13781     return 0;
13782 
13783   /* We only use "frame base" when we're sure we're talking about the
13784      post-prologue local stack frame.  We do this by *not* running
13785      register elimination until this point, and recognizing the special
13786      argument pointer and soft frame pointer rtx's.
13787      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13788   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13789       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13790     {
13791       dw_loc_descr_ref result = NULL;
13792 
13793       if (dwarf_version >= 4 || !dwarf_strict)
13794 	{
13795 	  result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13796 				       initialized);
13797 	  if (result)
13798 	    add_loc_descr (&result,
13799 			   new_loc_descr (DW_OP_stack_value, 0, 0));
13800 	}
13801       return result;
13802     }
13803 
13804   regs = targetm.dwarf_register_span (rtl);
13805 
13806   if (REG_NREGS (rtl) > 1 || regs)
13807     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13808   else
13809     {
13810       unsigned int dbx_regnum = dbx_reg_number (rtl);
13811       if (dbx_regnum == IGNORED_DWARF_REGNUM)
13812 	return 0;
13813       return one_reg_loc_descriptor (dbx_regnum, initialized);
13814     }
13815 }
13816 
13817 /* Return a location descriptor that designates a machine register for
13818    a given hard register number.  */
13819 
13820 static dw_loc_descr_ref
one_reg_loc_descriptor(unsigned int regno,enum var_init_status initialized)13821 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13822 {
13823   dw_loc_descr_ref reg_loc_descr;
13824 
13825   if (regno <= 31)
13826     reg_loc_descr
13827       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13828   else
13829     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13830 
13831   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13832     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13833 
13834   return reg_loc_descr;
13835 }
13836 
13837 /* Given an RTL of a register, return a location descriptor that
13838    designates a value that spans more than one register.  */
13839 
13840 static dw_loc_descr_ref
multiple_reg_loc_descriptor(rtx rtl,rtx regs,enum var_init_status initialized)13841 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13842 			     enum var_init_status initialized)
13843 {
13844   int size, i;
13845   dw_loc_descr_ref loc_result = NULL;
13846 
13847   /* Simple, contiguous registers.  */
13848   if (regs == NULL_RTX)
13849     {
13850       unsigned reg = REGNO (rtl);
13851       int nregs;
13852 
13853 #ifdef LEAF_REG_REMAP
13854       if (crtl->uses_only_leaf_regs)
13855 	{
13856 	  int leaf_reg = LEAF_REG_REMAP (reg);
13857 	  if (leaf_reg != -1)
13858 	    reg = (unsigned) leaf_reg;
13859 	}
13860 #endif
13861 
13862       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13863       nregs = REG_NREGS (rtl);
13864 
13865       /* At present we only track constant-sized pieces.  */
13866       if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13867 	return NULL;
13868       size /= nregs;
13869 
13870       loc_result = NULL;
13871       while (nregs--)
13872 	{
13873 	  dw_loc_descr_ref t;
13874 
13875 	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13876 				      VAR_INIT_STATUS_INITIALIZED);
13877 	  add_loc_descr (&loc_result, t);
13878 	  add_loc_descr_op_piece (&loc_result, size);
13879 	  ++reg;
13880 	}
13881       return loc_result;
13882     }
13883 
13884   /* Now onto stupid register sets in non contiguous locations.  */
13885 
13886   gcc_assert (GET_CODE (regs) == PARALLEL);
13887 
13888   /* At present we only track constant-sized pieces.  */
13889   if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13890     return NULL;
13891   loc_result = NULL;
13892 
13893   for (i = 0; i < XVECLEN (regs, 0); ++i)
13894     {
13895       dw_loc_descr_ref t;
13896 
13897       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13898 				  VAR_INIT_STATUS_INITIALIZED);
13899       add_loc_descr (&loc_result, t);
13900       add_loc_descr_op_piece (&loc_result, size);
13901     }
13902 
13903   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13904     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13905   return loc_result;
13906 }
13907 
13908 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13909 
13910 /* Return a location descriptor that designates a constant i,
13911    as a compound operation from constant (i >> shift), constant shift
13912    and DW_OP_shl.  */
13913 
13914 static dw_loc_descr_ref
int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)13915 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13916 {
13917   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13918   add_loc_descr (&ret, int_loc_descriptor (shift));
13919   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13920   return ret;
13921 }
13922 
13923 /* Return a location descriptor that designates constant POLY_I.  */
13924 
13925 static dw_loc_descr_ref
int_loc_descriptor(poly_int64 poly_i)13926 int_loc_descriptor (poly_int64 poly_i)
13927 {
13928   enum dwarf_location_atom op;
13929 
13930   HOST_WIDE_INT i;
13931   if (!poly_i.is_constant (&i))
13932     {
13933       /* Create location descriptions for the non-constant part and
13934 	 add any constant offset at the end.  */
13935       dw_loc_descr_ref ret = NULL;
13936       HOST_WIDE_INT constant = poly_i.coeffs[0];
13937       for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13938 	{
13939 	  HOST_WIDE_INT coeff = poly_i.coeffs[j];
13940 	  if (coeff != 0)
13941 	    {
13942 	      dw_loc_descr_ref start = ret;
13943 	      unsigned int factor;
13944 	      int bias;
13945 	      unsigned int regno = targetm.dwarf_poly_indeterminate_value
13946 		(j, &factor, &bias);
13947 
13948 	      /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13949 		 add COEFF * (REGNO / FACTOR) now and subtract
13950 		 COEFF * BIAS from the final constant part.  */
13951 	      constant -= coeff * bias;
13952 	      add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13953 	      if (coeff % factor == 0)
13954 		coeff /= factor;
13955 	      else
13956 		{
13957 		  int amount = exact_log2 (factor);
13958 		  gcc_assert (amount >= 0);
13959 		  add_loc_descr (&ret, int_loc_descriptor (amount));
13960 		  add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13961 		}
13962 	      if (coeff != 1)
13963 		{
13964 		  add_loc_descr (&ret, int_loc_descriptor (coeff));
13965 		  add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13966 		}
13967 	      if (start)
13968 		add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13969 	    }
13970 	}
13971       loc_descr_plus_const (&ret, constant);
13972       return ret;
13973     }
13974 
13975   /* Pick the smallest representation of a constant, rather than just
13976      defaulting to the LEB encoding.  */
13977   if (i >= 0)
13978     {
13979       int clz = clz_hwi (i);
13980       int ctz = ctz_hwi (i);
13981       if (i <= 31)
13982 	op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13983       else if (i <= 0xff)
13984 	op = DW_OP_const1u;
13985       else if (i <= 0xffff)
13986 	op = DW_OP_const2u;
13987       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13988 	       && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13989 	/* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13990 	   DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13991 	   while DW_OP_const4u is 5 bytes.  */
13992 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13993       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13994 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13995 	/* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13996 	   while DW_OP_const4u is 5 bytes.  */
13997 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13998 
13999       else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14000 	       && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14001 		  <= 4)
14002 	{
14003 	  /* As i >= 2**31, the double cast above will yield a negative number.
14004 	     Since wrapping is defined in DWARF expressions we can output big
14005 	     positive integers as small negative ones, regardless of the size
14006 	     of host wide ints.
14007 
14008 	     Here, since the evaluator will handle 32-bit values and since i >=
14009 	     2**31, we know it's going to be interpreted as a negative literal:
14010 	     store it this way if we can do better than 5 bytes this way.  */
14011 	  return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14012 	}
14013       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14014 	op = DW_OP_const4u;
14015 
14016       /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14017 	 least 6 bytes: see if we can do better before falling back to it.  */
14018       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14019 	       && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14020 	/* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes.  */
14021 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14022       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14023 	       && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14024 		  >= HOST_BITS_PER_WIDE_INT)
14025 	/* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14026 	   DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes.  */
14027 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14028       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14029 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14030 	       && size_of_uleb128 (i) > 6)
14031 	/* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
14032 	return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14033       else
14034 	op = DW_OP_constu;
14035     }
14036   else
14037     {
14038       if (i >= -0x80)
14039 	op = DW_OP_const1s;
14040       else if (i >= -0x8000)
14041 	op = DW_OP_const2s;
14042       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14043 	{
14044 	  if (size_of_int_loc_descriptor (i) < 5)
14045 	    {
14046 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
14047 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14048 	      return ret;
14049 	    }
14050 	  op = DW_OP_const4s;
14051 	}
14052       else
14053 	{
14054 	  if (size_of_int_loc_descriptor (i)
14055 	      < (unsigned long) 1 + size_of_sleb128 (i))
14056 	    {
14057 	      dw_loc_descr_ref ret = int_loc_descriptor (-i);
14058 	      add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14059 	      return ret;
14060 	    }
14061 	  op = DW_OP_consts;
14062 	}
14063     }
14064 
14065   return new_loc_descr (op, i, 0);
14066 }
14067 
14068 /* Likewise, for unsigned constants.  */
14069 
14070 static dw_loc_descr_ref
uint_loc_descriptor(unsigned HOST_WIDE_INT i)14071 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14072 {
14073   const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14074   const unsigned HOST_WIDE_INT max_uint
14075     = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14076 
14077   /* If possible, use the clever signed constants handling.  */
14078   if (i <= max_int)
14079     return int_loc_descriptor ((HOST_WIDE_INT) i);
14080 
14081   /* Here, we are left with positive numbers that cannot be represented as
14082      HOST_WIDE_INT, i.e.:
14083          max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14084 
14085      Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14086      whereas may be better to output a negative integer: thanks to integer
14087      wrapping, we know that:
14088          x = x - 2 ** DWARF2_ADDR_SIZE
14089 	   = x - 2 * (max (HOST_WIDE_INT) + 1)
14090      So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14091      small negative integers.  Let's try that in cases it will clearly improve
14092      the encoding: there is no gain turning DW_OP_const4u into
14093      DW_OP_const4s.  */
14094   if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14095       && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14096 	  || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14097     {
14098       const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14099 
14100       /* Now, -1 <  first_shift <= max (HOST_WIDE_INT)
14101 	 i.e.  0 <= first_shift <= max (HOST_WIDE_INT).  */
14102       const HOST_WIDE_INT second_shift
14103         = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14104 
14105       /* So we finally have:
14106 	      -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14107 	 i.e.  min (HOST_WIDE_INT)     <= second_shift <  0.  */
14108       return int_loc_descriptor (second_shift);
14109     }
14110 
14111   /* Last chance: fallback to a simple constant operation.  */
14112   return new_loc_descr
14113      ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14114       ? DW_OP_const4u
14115       : DW_OP_const8u,
14116       i, 0);
14117 }
14118 
14119 /* Generate and return a location description that computes the unsigned
14120    comparison of the two stack top entries (a OP b where b is the top-most
14121    entry and a is the second one).  The KIND of comparison can be LT_EXPR,
14122    LE_EXPR, GT_EXPR or GE_EXPR.  */
14123 
14124 static dw_loc_descr_ref
uint_comparison_loc_list(enum tree_code kind)14125 uint_comparison_loc_list (enum tree_code kind)
14126 {
14127   enum dwarf_location_atom op, flip_op;
14128   dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14129 
14130   switch (kind)
14131     {
14132     case LT_EXPR:
14133       op = DW_OP_lt;
14134       break;
14135     case LE_EXPR:
14136       op = DW_OP_le;
14137       break;
14138     case GT_EXPR:
14139       op = DW_OP_gt;
14140       break;
14141     case GE_EXPR:
14142       op = DW_OP_ge;
14143       break;
14144     default:
14145       gcc_unreachable ();
14146     }
14147 
14148   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14149   jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14150 
14151   /* Until DWARFv4, operations all work on signed integers.  It is nevertheless
14152      possible to perform unsigned comparisons: we just have to distinguish
14153      three cases:
14154 
14155        1. when a and b have the same sign (as signed integers); then we should
14156 	  return: a OP(signed) b;
14157 
14158        2. when a is a negative signed integer while b is a positive one, then a
14159 	  is a greater unsigned integer than b; likewise when a and b's roles
14160 	  are flipped.
14161 
14162      So first, compare the sign of the two operands.  */
14163   ret = new_loc_descr (DW_OP_over, 0, 0);
14164   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14165   add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14166   /* If they have different signs (i.e. they have different sign bits), then
14167      the stack top value has now the sign bit set and thus it's smaller than
14168      zero.  */
14169   add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14170   add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14171   add_loc_descr (&ret, bra_node);
14172 
14173   /* We are in case 1.  At this point, we know both operands have the same
14174      sign, to it's safe to use the built-in signed comparison.  */
14175   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14176   add_loc_descr (&ret, jmp_node);
14177 
14178   /* We are in case 2.  Here, we know both operands do not have the same sign,
14179      so we have to flip the signed comparison.  */
14180   flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14181   tmp = new_loc_descr (flip_op, 0, 0);
14182   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14183   bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14184   add_loc_descr (&ret, tmp);
14185 
14186   /* This dummy operation is necessary to make the two branches join.  */
14187   tmp = new_loc_descr (DW_OP_nop, 0, 0);
14188   jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14189   jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14190   add_loc_descr (&ret, tmp);
14191 
14192   return ret;
14193 }
14194 
14195 /* Likewise, but takes the location description lists (might be destructive on
14196    them).  Return NULL if either is NULL or if concatenation fails.  */
14197 
14198 static dw_loc_list_ref
loc_list_from_uint_comparison(dw_loc_list_ref left,dw_loc_list_ref right,enum tree_code kind)14199 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14200 			       enum tree_code kind)
14201 {
14202   if (left == NULL || right == NULL)
14203     return NULL;
14204 
14205   add_loc_list (&left, right);
14206   if (left == NULL)
14207     return NULL;
14208 
14209   add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14210   return left;
14211 }
14212 
14213 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14214    without actually allocating it.  */
14215 
14216 static unsigned long
size_of_int_shift_loc_descriptor(HOST_WIDE_INT i,int shift)14217 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14218 {
14219   return size_of_int_loc_descriptor (i >> shift)
14220 	 + size_of_int_loc_descriptor (shift)
14221 	 + 1;
14222 }
14223 
14224 /* Return size_of_locs (int_loc_descriptor (i)) without
14225    actually allocating it.  */
14226 
14227 static unsigned long
size_of_int_loc_descriptor(HOST_WIDE_INT i)14228 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14229 {
14230   unsigned long s;
14231 
14232   if (i >= 0)
14233     {
14234       int clz, ctz;
14235       if (i <= 31)
14236 	return 1;
14237       else if (i <= 0xff)
14238 	return 2;
14239       else if (i <= 0xffff)
14240 	return 3;
14241       clz = clz_hwi (i);
14242       ctz = ctz_hwi (i);
14243       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14244 	  && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14245 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14246 						    - clz - 5);
14247       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14248 	       && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14249 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14250 						    - clz - 8);
14251       else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14252 	       && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14253 		  <= 4)
14254 	return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14255       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14256 	return 5;
14257       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14258       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14259 	  && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14260 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14261 						    - clz - 8);
14262       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14263 	       && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14264 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14265 						    - clz - 16);
14266       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14267 	       && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14268 	       && s > 6)
14269 	return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14270 						    - clz - 32);
14271       else
14272 	return 1 + s;
14273     }
14274   else
14275     {
14276       if (i >= -0x80)
14277 	return 2;
14278       else if (i >= -0x8000)
14279 	return 3;
14280       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14281 	{
14282 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14283 	    {
14284 	      s = size_of_int_loc_descriptor (-i) + 1;
14285 	      if (s < 5)
14286 		return s;
14287 	    }
14288 	  return 5;
14289 	}
14290       else
14291 	{
14292 	  unsigned long r = 1 + size_of_sleb128 (i);
14293 	  if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14294 	    {
14295 	      s = size_of_int_loc_descriptor (-i) + 1;
14296 	      if (s < r)
14297 		return s;
14298 	    }
14299 	  return r;
14300 	}
14301     }
14302 }
14303 
14304 /* Return loc description representing "address" of integer value.
14305    This can appear only as toplevel expression.  */
14306 
14307 static dw_loc_descr_ref
address_of_int_loc_descriptor(int size,HOST_WIDE_INT i)14308 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14309 {
14310   int litsize;
14311   dw_loc_descr_ref loc_result = NULL;
14312 
14313   if (!(dwarf_version >= 4 || !dwarf_strict))
14314     return NULL;
14315 
14316   litsize = size_of_int_loc_descriptor (i);
14317   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14318      is more compact.  For DW_OP_stack_value we need:
14319      litsize + 1 (DW_OP_stack_value)
14320      and for DW_OP_implicit_value:
14321      1 (DW_OP_implicit_value) + 1 (length) + size.  */
14322   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14323     {
14324       loc_result = int_loc_descriptor (i);
14325       add_loc_descr (&loc_result,
14326 		     new_loc_descr (DW_OP_stack_value, 0, 0));
14327       return loc_result;
14328     }
14329 
14330   loc_result = new_loc_descr (DW_OP_implicit_value,
14331 			      size, 0);
14332   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14333   loc_result->dw_loc_oprnd2.v.val_int = i;
14334   return loc_result;
14335 }
14336 
14337 /* Return a location descriptor that designates a base+offset location.  */
14338 
14339 static dw_loc_descr_ref
based_loc_descr(rtx reg,poly_int64 offset,enum var_init_status initialized)14340 based_loc_descr (rtx reg, poly_int64 offset,
14341 		 enum var_init_status initialized)
14342 {
14343   unsigned int regno;
14344   dw_loc_descr_ref result;
14345   dw_fde_ref fde = cfun->fde;
14346 
14347   /* We only use "frame base" when we're sure we're talking about the
14348      post-prologue local stack frame.  We do this by *not* running
14349      register elimination until this point, and recognizing the special
14350      argument pointer and soft frame pointer rtx's.  */
14351   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14352     {
14353       rtx elim = (ira_use_lra_p
14354 		  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14355 		  : eliminate_regs (reg, VOIDmode, NULL_RTX));
14356 
14357       if (elim != reg)
14358 	{
14359 	  /* Allow hard frame pointer here even if frame pointer
14360 	    isn't used since hard frame pointer is encoded with
14361 	    DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14362 	    not hard frame pointer directly.  */
14363 	  elim = strip_offset_and_add (elim, &offset);
14364 	  gcc_assert (elim == hard_frame_pointer_rtx
14365 		      || elim == stack_pointer_rtx);
14366 
14367 	  /* If drap register is used to align stack, use frame
14368 	     pointer + offset to access stack variables.  If stack
14369 	     is aligned without drap, use stack pointer + offset to
14370 	     access stack variables.  */
14371 	  if (crtl->stack_realign_tried
14372 	      && reg == frame_pointer_rtx)
14373 	    {
14374 	      int base_reg
14375 		= DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14376 				      ? HARD_FRAME_POINTER_REGNUM
14377 				      : REGNO (elim));
14378 	      return new_reg_loc_descr (base_reg, offset);
14379 	    }
14380 
14381 	  gcc_assert (frame_pointer_fb_offset_valid);
14382 	  offset += frame_pointer_fb_offset;
14383 	  HOST_WIDE_INT const_offset;
14384 	  if (offset.is_constant (&const_offset))
14385 	    return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14386 	  else
14387 	    {
14388 	      dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14389 	      loc_descr_plus_const (&ret, offset);
14390 	      return ret;
14391 	    }
14392 	}
14393     }
14394 
14395   regno = REGNO (reg);
14396 #ifdef LEAF_REG_REMAP
14397   if (crtl->uses_only_leaf_regs)
14398     {
14399       int leaf_reg = LEAF_REG_REMAP (regno);
14400       if (leaf_reg != -1)
14401 	regno = (unsigned) leaf_reg;
14402     }
14403 #endif
14404   regno = DWARF_FRAME_REGNUM (regno);
14405 
14406   HOST_WIDE_INT const_offset;
14407   if (!optimize && fde
14408       && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14409       && offset.is_constant (&const_offset))
14410     {
14411       /* Use cfa+offset to represent the location of arguments passed
14412 	 on the stack when drap is used to align stack.
14413 	 Only do this when not optimizing, for optimized code var-tracking
14414 	 is supposed to track where the arguments live and the register
14415 	 used as vdrap or drap in some spot might be used for something
14416 	 else in other part of the routine.  */
14417       return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14418     }
14419 
14420   result = new_reg_loc_descr (regno, offset);
14421 
14422   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14423     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14424 
14425   return result;
14426 }
14427 
14428 /* Return true if this RTL expression describes a base+offset calculation.  */
14429 
14430 static inline int
is_based_loc(const_rtx rtl)14431 is_based_loc (const_rtx rtl)
14432 {
14433   return (GET_CODE (rtl) == PLUS
14434 	  && ((REG_P (XEXP (rtl, 0))
14435 	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14436 	       && CONST_INT_P (XEXP (rtl, 1)))));
14437 }
14438 
14439 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14440    failed.  */
14441 
14442 static dw_loc_descr_ref
tls_mem_loc_descriptor(rtx mem)14443 tls_mem_loc_descriptor (rtx mem)
14444 {
14445   tree base;
14446   dw_loc_descr_ref loc_result;
14447 
14448   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14449     return NULL;
14450 
14451   base = get_base_address (MEM_EXPR (mem));
14452   if (base == NULL
14453       || !VAR_P (base)
14454       || !DECL_THREAD_LOCAL_P (base))
14455     return NULL;
14456 
14457   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14458   if (loc_result == NULL)
14459     return NULL;
14460 
14461   if (maybe_ne (MEM_OFFSET (mem), 0))
14462     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14463 
14464   return loc_result;
14465 }
14466 
14467 /* Output debug info about reason why we failed to expand expression as dwarf
14468    expression.  */
14469 
14470 static void
expansion_failed(tree expr,rtx rtl,char const * reason)14471 expansion_failed (tree expr, rtx rtl, char const *reason)
14472 {
14473   if (dump_file && (dump_flags & TDF_DETAILS))
14474     {
14475       fprintf (dump_file, "Failed to expand as dwarf: ");
14476       if (expr)
14477 	print_generic_expr (dump_file, expr, dump_flags);
14478       if (rtl)
14479 	{
14480 	  fprintf (dump_file, "\n");
14481 	  print_rtl (dump_file, rtl);
14482 	}
14483       fprintf (dump_file, "\nReason: %s\n", reason);
14484     }
14485 }
14486 
14487 /* Helper function for const_ok_for_output.  */
14488 
14489 static bool
const_ok_for_output_1(rtx rtl)14490 const_ok_for_output_1 (rtx rtl)
14491 {
14492   if (targetm.const_not_ok_for_debug_p (rtl))
14493     {
14494       if (GET_CODE (rtl) != UNSPEC)
14495 	{
14496 	  expansion_failed (NULL_TREE, rtl,
14497 			    "Expression rejected for debug by the backend.\n");
14498 	  return false;
14499 	}
14500 
14501       /* If delegitimize_address couldn't do anything with the UNSPEC, and
14502 	 the target hook doesn't explicitly allow it in debug info, assume
14503 	 we can't express it in the debug info.  */
14504       /* Don't complain about TLS UNSPECs, those are just too hard to
14505 	 delegitimize.  Note this could be a non-decl SYMBOL_REF such as
14506 	 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14507 	 rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
14508       if (flag_checking
14509 	  && (XVECLEN (rtl, 0) == 0
14510 	      || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14511 	      || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14512 	inform (current_function_decl
14513 		? DECL_SOURCE_LOCATION (current_function_decl)
14514 		: UNKNOWN_LOCATION,
14515 #if NUM_UNSPEC_VALUES > 0
14516 		"non-delegitimized UNSPEC %s (%d) found in variable location",
14517 		((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14518 		 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14519 #else
14520 		"non-delegitimized UNSPEC %d found in variable location",
14521 #endif
14522 		XINT (rtl, 1));
14523       expansion_failed (NULL_TREE, rtl,
14524 			"UNSPEC hasn't been delegitimized.\n");
14525       return false;
14526     }
14527 
14528   if (CONST_POLY_INT_P (rtl))
14529     return false;
14530 
14531   /* FIXME: Refer to PR60655. It is possible for simplification
14532      of rtl expressions in var tracking to produce such expressions.
14533      We should really identify / validate expressions
14534      enclosed in CONST that can be handled by assemblers on various
14535      targets and only handle legitimate cases here.  */
14536   switch (GET_CODE (rtl))
14537     {
14538     case SYMBOL_REF:
14539       break;
14540     case NOT:
14541     case NEG:
14542       return false;
14543     case PLUS:
14544       {
14545 	/* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14546 	   operands.  */
14547 	subrtx_var_iterator::array_type array;
14548 	bool first = false;
14549 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14550 	  if (SYMBOL_REF_P (*iter)
14551 	      || LABEL_P (*iter)
14552 	      || GET_CODE (*iter) == UNSPEC)
14553 	    {
14554 	      first = true;
14555 	      break;
14556 	    }
14557 	if (!first)
14558 	  return true;
14559 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14560 	  if (SYMBOL_REF_P (*iter)
14561 	      || LABEL_P (*iter)
14562 	      || GET_CODE (*iter) == UNSPEC)
14563 	    return false;
14564 	return true;
14565       }
14566     case MINUS:
14567       {
14568 	/* Disallow negation of SYMBOL_REFs or UNSPECs when they
14569 	   appear in the second operand of MINUS.  */
14570 	subrtx_var_iterator::array_type array;
14571 	FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14572 	  if (SYMBOL_REF_P (*iter)
14573 	      || LABEL_P (*iter)
14574 	      || GET_CODE (*iter) == UNSPEC)
14575 	    return false;
14576 	return true;
14577       }
14578     default:
14579       return true;
14580     }
14581 
14582   if (CONSTANT_POOL_ADDRESS_P (rtl))
14583     {
14584       bool marked;
14585       get_pool_constant_mark (rtl, &marked);
14586       /* If all references to this pool constant were optimized away,
14587 	 it was not output and thus we can't represent it.  */
14588       if (!marked)
14589 	{
14590 	  expansion_failed (NULL_TREE, rtl,
14591 			    "Constant was removed from constant pool.\n");
14592 	  return false;
14593 	}
14594     }
14595 
14596   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14597     return false;
14598 
14599   /* Avoid references to external symbols in debug info, on several targets
14600      the linker might even refuse to link when linking a shared library,
14601      and in many other cases the relocations for .debug_info/.debug_loc are
14602      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
14603      to be defined within the same shared library or executable are fine.  */
14604   if (SYMBOL_REF_EXTERNAL_P (rtl))
14605     {
14606       tree decl = SYMBOL_REF_DECL (rtl);
14607 
14608       if (decl == NULL || !targetm.binds_local_p (decl))
14609 	{
14610 	  expansion_failed (NULL_TREE, rtl,
14611 			    "Symbol not defined in current TU.\n");
14612 	  return false;
14613 	}
14614     }
14615 
14616   return true;
14617 }
14618 
14619 /* Return true if constant RTL can be emitted in DW_OP_addr or
14620    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
14621    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
14622 
14623 static bool
const_ok_for_output(rtx rtl)14624 const_ok_for_output (rtx rtl)
14625 {
14626   if (GET_CODE (rtl) == SYMBOL_REF)
14627     return const_ok_for_output_1 (rtl);
14628 
14629   if (GET_CODE (rtl) == CONST)
14630     {
14631       subrtx_var_iterator::array_type array;
14632       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14633 	if (!const_ok_for_output_1 (*iter))
14634 	  return false;
14635       return true;
14636     }
14637 
14638   return true;
14639 }
14640 
14641 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14642    if possible, NULL otherwise.  */
14643 
14644 static dw_die_ref
base_type_for_mode(machine_mode mode,bool unsignedp)14645 base_type_for_mode (machine_mode mode, bool unsignedp)
14646 {
14647   dw_die_ref type_die;
14648   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14649 
14650   if (type == NULL)
14651     return NULL;
14652   switch (TREE_CODE (type))
14653     {
14654     case INTEGER_TYPE:
14655     case REAL_TYPE:
14656       break;
14657     default:
14658       return NULL;
14659     }
14660   type_die = lookup_type_die (type);
14661   if (!type_die)
14662     type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14663 				  comp_unit_die ());
14664   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14665     return NULL;
14666   return type_die;
14667 }
14668 
14669 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14670    type matching MODE, or, if MODE is narrower than or as wide as
14671    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
14672    possible.  */
14673 
14674 static dw_loc_descr_ref
convert_descriptor_to_mode(scalar_int_mode mode,dw_loc_descr_ref op)14675 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14676 {
14677   machine_mode outer_mode = mode;
14678   dw_die_ref type_die;
14679   dw_loc_descr_ref cvt;
14680 
14681   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14682     {
14683       add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14684       return op;
14685     }
14686   type_die = base_type_for_mode (outer_mode, 1);
14687   if (type_die == NULL)
14688     return NULL;
14689   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14690   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14691   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14692   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14693   add_loc_descr (&op, cvt);
14694   return op;
14695 }
14696 
14697 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
14698 
14699 static dw_loc_descr_ref
compare_loc_descriptor(enum dwarf_location_atom op,dw_loc_descr_ref op0,dw_loc_descr_ref op1)14700 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14701 			dw_loc_descr_ref op1)
14702 {
14703   dw_loc_descr_ref ret = op0;
14704   add_loc_descr (&ret, op1);
14705   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14706   if (STORE_FLAG_VALUE != 1)
14707     {
14708       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14709       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14710     }
14711   return ret;
14712 }
14713 
14714 /* Subroutine of scompare_loc_descriptor for the case in which we're
14715    comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14716    and in which OP_MODE is bigger than DWARF2_ADDR_SIZE.  */
14717 
14718 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)14719 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14720 			      scalar_int_mode op_mode,
14721 			      dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14722 {
14723   dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14724   dw_loc_descr_ref cvt;
14725 
14726   if (type_die == NULL)
14727     return NULL;
14728   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14729   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14730   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14731   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14732   add_loc_descr (&op0, cvt);
14733   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14734   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14735   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14736   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14737   add_loc_descr (&op1, cvt);
14738   return compare_loc_descriptor (op, op0, op1);
14739 }
14740 
14741 /* Subroutine of scompare_loc_descriptor for the case in which we're
14742    comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14743    and in which OP_MODE is smaller than DWARF2_ADDR_SIZE.  */
14744 
14745 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)14746 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14747 				scalar_int_mode op_mode,
14748 				dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14749 {
14750   int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14751   /* For eq/ne, if the operands are known to be zero-extended,
14752      there is no need to do the fancy shifting up.  */
14753   if (op == DW_OP_eq || op == DW_OP_ne)
14754     {
14755       dw_loc_descr_ref last0, last1;
14756       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14757 	;
14758       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14759 	;
14760       /* deref_size zero extends, and for constants we can check
14761 	 whether they are zero extended or not.  */
14762       if (((last0->dw_loc_opc == DW_OP_deref_size
14763 	    && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14764 	   || (CONST_INT_P (XEXP (rtl, 0))
14765 	       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14766 		  == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14767 	  && ((last1->dw_loc_opc == DW_OP_deref_size
14768 	       && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14769 	      || (CONST_INT_P (XEXP (rtl, 1))
14770 		  && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14771 		     == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14772 	return compare_loc_descriptor (op, op0, op1);
14773 
14774       /* EQ/NE comparison against constant in narrower type than
14775 	 DWARF2_ADDR_SIZE can be performed either as
14776 	 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14777 	 DW_OP_{eq,ne}
14778 	 or
14779 	 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14780 	 DW_OP_{eq,ne}.  Pick whatever is shorter.  */
14781       if (CONST_INT_P (XEXP (rtl, 1))
14782 	  && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14783 	  && (size_of_int_loc_descriptor (shift) + 1
14784 	      + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14785 	      >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14786 		 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14787 					       & GET_MODE_MASK (op_mode))))
14788 	{
14789 	  add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14790 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14791 	  op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14792 				    & GET_MODE_MASK (op_mode));
14793 	  return compare_loc_descriptor (op, op0, op1);
14794 	}
14795     }
14796   add_loc_descr (&op0, int_loc_descriptor (shift));
14797   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14798   if (CONST_INT_P (XEXP (rtl, 1)))
14799     op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14800   else
14801     {
14802       add_loc_descr (&op1, int_loc_descriptor (shift));
14803       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14804     }
14805   return compare_loc_descriptor (op, op0, op1);
14806 }
14807 
14808 /* Return location descriptor for unsigned comparison OP RTL.  */
14809 
14810 static dw_loc_descr_ref
scompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,machine_mode mem_mode)14811 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14812 			 machine_mode mem_mode)
14813 {
14814   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14815   dw_loc_descr_ref op0, op1;
14816 
14817   if (op_mode == VOIDmode)
14818     op_mode = GET_MODE (XEXP (rtl, 1));
14819   if (op_mode == VOIDmode)
14820     return NULL;
14821 
14822   scalar_int_mode int_op_mode;
14823   if (dwarf_strict
14824       && dwarf_version < 5
14825       && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14826 	  || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14827     return NULL;
14828 
14829   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14830 			    VAR_INIT_STATUS_INITIALIZED);
14831   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14832 			    VAR_INIT_STATUS_INITIALIZED);
14833 
14834   if (op0 == NULL || op1 == NULL)
14835     return NULL;
14836 
14837   if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14838     {
14839       if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14840 	return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14841 
14842       if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14843 	return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14844     }
14845   return compare_loc_descriptor (op, op0, op1);
14846 }
14847 
14848 /* Return location descriptor for unsigned comparison OP RTL.  */
14849 
14850 static dw_loc_descr_ref
ucompare_loc_descriptor(enum dwarf_location_atom op,rtx rtl,machine_mode mem_mode)14851 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14852 			 machine_mode mem_mode)
14853 {
14854   dw_loc_descr_ref op0, op1;
14855 
14856   machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14857   if (test_op_mode == VOIDmode)
14858     test_op_mode = GET_MODE (XEXP (rtl, 1));
14859 
14860   scalar_int_mode op_mode;
14861   if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14862     return NULL;
14863 
14864   if (dwarf_strict
14865       && dwarf_version < 5
14866       && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14867     return NULL;
14868 
14869   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14870 			    VAR_INIT_STATUS_INITIALIZED);
14871   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14872 			    VAR_INIT_STATUS_INITIALIZED);
14873 
14874   if (op0 == NULL || op1 == NULL)
14875     return NULL;
14876 
14877   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14878     {
14879       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14880       dw_loc_descr_ref last0, last1;
14881       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14882 	;
14883       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14884 	;
14885       if (CONST_INT_P (XEXP (rtl, 0)))
14886 	op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14887       /* deref_size zero extends, so no need to mask it again.  */
14888       else if (last0->dw_loc_opc != DW_OP_deref_size
14889 	       || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14890 	{
14891 	  add_loc_descr (&op0, int_loc_descriptor (mask));
14892 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14893 	}
14894       if (CONST_INT_P (XEXP (rtl, 1)))
14895 	op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14896       /* deref_size zero extends, so no need to mask it again.  */
14897       else if (last1->dw_loc_opc != DW_OP_deref_size
14898 	       || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14899 	{
14900 	  add_loc_descr (&op1, int_loc_descriptor (mask));
14901 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14902 	}
14903     }
14904   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14905     {
14906       HOST_WIDE_INT bias = 1;
14907       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14908       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14909       if (CONST_INT_P (XEXP (rtl, 1)))
14910 	op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14911 				  + INTVAL (XEXP (rtl, 1)));
14912       else
14913 	add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14914 					    bias, 0));
14915     }
14916   return compare_loc_descriptor (op, op0, op1);
14917 }
14918 
14919 /* Return location descriptor for {U,S}{MIN,MAX}.  */
14920 
14921 static dw_loc_descr_ref
minmax_loc_descriptor(rtx rtl,machine_mode mode,machine_mode mem_mode)14922 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14923 		       machine_mode mem_mode)
14924 {
14925   enum dwarf_location_atom op;
14926   dw_loc_descr_ref op0, op1, ret;
14927   dw_loc_descr_ref bra_node, drop_node;
14928 
14929   scalar_int_mode int_mode;
14930   if (dwarf_strict
14931       && dwarf_version < 5
14932       && (!is_a <scalar_int_mode> (mode, &int_mode)
14933 	  || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14934     return NULL;
14935 
14936   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14937 			    VAR_INIT_STATUS_INITIALIZED);
14938   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14939 			    VAR_INIT_STATUS_INITIALIZED);
14940 
14941   if (op0 == NULL || op1 == NULL)
14942     return NULL;
14943 
14944   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14945   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14946   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14947   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14948     {
14949       /* Checked by the caller.  */
14950       int_mode = as_a <scalar_int_mode> (mode);
14951       if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14952 	{
14953 	  HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14954 	  add_loc_descr (&op0, int_loc_descriptor (mask));
14955 	  add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14956 	  add_loc_descr (&op1, int_loc_descriptor (mask));
14957 	  add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14958 	}
14959       else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14960 	{
14961 	  HOST_WIDE_INT bias = 1;
14962 	  bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14963 	  add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14964 	  add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14965 	}
14966     }
14967   else if (is_a <scalar_int_mode> (mode, &int_mode)
14968 	   && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14969     {
14970       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14971       add_loc_descr (&op0, int_loc_descriptor (shift));
14972       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14973       add_loc_descr (&op1, int_loc_descriptor (shift));
14974       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14975     }
14976   else if (is_a <scalar_int_mode> (mode, &int_mode)
14977 	   && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14978     {
14979       dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14980       dw_loc_descr_ref cvt;
14981       if (type_die == NULL)
14982 	return NULL;
14983       cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14984       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14985       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14986       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14987       add_loc_descr (&op0, cvt);
14988       cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14989       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14990       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14991       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14992       add_loc_descr (&op1, cvt);
14993     }
14994 
14995   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14996     op = DW_OP_lt;
14997   else
14998     op = DW_OP_gt;
14999   ret = op0;
15000   add_loc_descr (&ret, op1);
15001   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15002   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15003   add_loc_descr (&ret, bra_node);
15004   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15005   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15006   add_loc_descr (&ret, drop_node);
15007   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15008   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15009   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15010       && is_a <scalar_int_mode> (mode, &int_mode)
15011       && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15012     ret = convert_descriptor_to_mode (int_mode, ret);
15013   return ret;
15014 }
15015 
15016 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
15017    but after converting arguments to type_die, afterwards
15018    convert back to unsigned.  */
15019 
15020 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)15021 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15022 	     scalar_int_mode mode, machine_mode mem_mode)
15023 {
15024   dw_loc_descr_ref cvt, op0, op1;
15025 
15026   if (type_die == NULL)
15027     return NULL;
15028   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15029 			    VAR_INIT_STATUS_INITIALIZED);
15030   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15031 			    VAR_INIT_STATUS_INITIALIZED);
15032   if (op0 == NULL || op1 == NULL)
15033     return NULL;
15034   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15035   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15036   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15037   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15038   add_loc_descr (&op0, cvt);
15039   cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15040   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15041   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15042   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15043   add_loc_descr (&op1, cvt);
15044   add_loc_descr (&op0, op1);
15045   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15046   return convert_descriptor_to_mode (mode, op0);
15047 }
15048 
15049 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15050    const0 is DW_OP_lit0 or corresponding typed constant,
15051    const1 is DW_OP_lit1 or corresponding typed constant
15052    and constMSB is constant with just the MSB bit set
15053    for the mode):
15054        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15055    L1: const0 DW_OP_swap
15056    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15057        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15058    L3: DW_OP_drop
15059    L4: DW_OP_nop
15060 
15061    CTZ is similar:
15062        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15063    L1: const0 DW_OP_swap
15064    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15065        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15066    L3: DW_OP_drop
15067    L4: DW_OP_nop
15068 
15069    FFS is similar:
15070        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15071    L1: const1 DW_OP_swap
15072    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15073        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15074    L3: DW_OP_drop
15075    L4: DW_OP_nop  */
15076 
15077 static dw_loc_descr_ref
clz_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15078 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15079 		    machine_mode mem_mode)
15080 {
15081   dw_loc_descr_ref op0, ret, tmp;
15082   HOST_WIDE_INT valv;
15083   dw_loc_descr_ref l1jump, l1label;
15084   dw_loc_descr_ref l2jump, l2label;
15085   dw_loc_descr_ref l3jump, l3label;
15086   dw_loc_descr_ref l4jump, l4label;
15087   rtx msb;
15088 
15089   if (GET_MODE (XEXP (rtl, 0)) != mode)
15090     return NULL;
15091 
15092   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15093 			    VAR_INIT_STATUS_INITIALIZED);
15094   if (op0 == NULL)
15095     return NULL;
15096   ret = op0;
15097   if (GET_CODE (rtl) == CLZ)
15098     {
15099       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15100 	valv = GET_MODE_BITSIZE (mode);
15101     }
15102   else if (GET_CODE (rtl) == FFS)
15103     valv = 0;
15104   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15105     valv = GET_MODE_BITSIZE (mode);
15106   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15107   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15108   add_loc_descr (&ret, l1jump);
15109   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15110   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15111 			    VAR_INIT_STATUS_INITIALIZED);
15112   if (tmp == NULL)
15113     return NULL;
15114   add_loc_descr (&ret, tmp);
15115   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15116   add_loc_descr (&ret, l4jump);
15117   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15118 				? const1_rtx : const0_rtx,
15119 				mode, mem_mode,
15120 				VAR_INIT_STATUS_INITIALIZED);
15121   if (l1label == NULL)
15122     return NULL;
15123   add_loc_descr (&ret, l1label);
15124   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15125   l2label = new_loc_descr (DW_OP_dup, 0, 0);
15126   add_loc_descr (&ret, l2label);
15127   if (GET_CODE (rtl) != CLZ)
15128     msb = const1_rtx;
15129   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15130     msb = GEN_INT (HOST_WIDE_INT_1U
15131 		   << (GET_MODE_BITSIZE (mode) - 1));
15132   else
15133     msb = immed_wide_int_const
15134       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15135 			    GET_MODE_PRECISION (mode)), mode);
15136   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15137     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15138 			 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15139 			 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15140   else
15141     tmp = mem_loc_descriptor (msb, mode, mem_mode,
15142 			      VAR_INIT_STATUS_INITIALIZED);
15143   if (tmp == NULL)
15144     return NULL;
15145   add_loc_descr (&ret, tmp);
15146   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15147   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15148   add_loc_descr (&ret, l3jump);
15149   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15150 			    VAR_INIT_STATUS_INITIALIZED);
15151   if (tmp == NULL)
15152     return NULL;
15153   add_loc_descr (&ret, tmp);
15154   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15155 				      ? DW_OP_shl : DW_OP_shr, 0, 0));
15156   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15157   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15158   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15159   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15160   add_loc_descr (&ret, l2jump);
15161   l3label = new_loc_descr (DW_OP_drop, 0, 0);
15162   add_loc_descr (&ret, l3label);
15163   l4label = new_loc_descr (DW_OP_nop, 0, 0);
15164   add_loc_descr (&ret, l4label);
15165   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15166   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15167   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15168   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15169   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15170   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15171   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15172   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15173   return ret;
15174 }
15175 
15176 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15177    const1 is DW_OP_lit1 or corresponding typed constant):
15178        const0 DW_OP_swap
15179    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15180        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15181    L2: DW_OP_drop
15182 
15183    PARITY is similar:
15184    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15185        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15186    L2: DW_OP_drop  */
15187 
15188 static dw_loc_descr_ref
popcount_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15189 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15190 			 machine_mode mem_mode)
15191 {
15192   dw_loc_descr_ref op0, ret, tmp;
15193   dw_loc_descr_ref l1jump, l1label;
15194   dw_loc_descr_ref l2jump, l2label;
15195 
15196   if (GET_MODE (XEXP (rtl, 0)) != mode)
15197     return NULL;
15198 
15199   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15200 			    VAR_INIT_STATUS_INITIALIZED);
15201   if (op0 == NULL)
15202     return NULL;
15203   ret = op0;
15204   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15205 			    VAR_INIT_STATUS_INITIALIZED);
15206   if (tmp == NULL)
15207     return NULL;
15208   add_loc_descr (&ret, tmp);
15209   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15210   l1label = new_loc_descr (DW_OP_dup, 0, 0);
15211   add_loc_descr (&ret, l1label);
15212   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15213   add_loc_descr (&ret, l2jump);
15214   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15215   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15216   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15217 			    VAR_INIT_STATUS_INITIALIZED);
15218   if (tmp == NULL)
15219     return NULL;
15220   add_loc_descr (&ret, tmp);
15221   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15222   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15223 				      ? DW_OP_plus : DW_OP_xor, 0, 0));
15224   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15225   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15226 			    VAR_INIT_STATUS_INITIALIZED);
15227   add_loc_descr (&ret, tmp);
15228   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15229   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15230   add_loc_descr (&ret, l1jump);
15231   l2label = new_loc_descr (DW_OP_drop, 0, 0);
15232   add_loc_descr (&ret, l2label);
15233   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15234   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15235   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15236   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15237   return ret;
15238 }
15239 
15240 /* BSWAP (constS is initial shift count, either 56 or 24):
15241        constS const0
15242    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15243        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15244        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15245        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15246    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
15247 
15248 static dw_loc_descr_ref
bswap_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15249 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15250 		      machine_mode mem_mode)
15251 {
15252   dw_loc_descr_ref op0, ret, tmp;
15253   dw_loc_descr_ref l1jump, l1label;
15254   dw_loc_descr_ref l2jump, l2label;
15255 
15256   if (BITS_PER_UNIT != 8
15257       || (GET_MODE_BITSIZE (mode) != 32
15258 	  && GET_MODE_BITSIZE (mode) != 64))
15259     return NULL;
15260 
15261   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15262 			    VAR_INIT_STATUS_INITIALIZED);
15263   if (op0 == NULL)
15264     return NULL;
15265 
15266   ret = op0;
15267   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15268 			    mode, mem_mode,
15269 			    VAR_INIT_STATUS_INITIALIZED);
15270   if (tmp == NULL)
15271     return NULL;
15272   add_loc_descr (&ret, tmp);
15273   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15274 			    VAR_INIT_STATUS_INITIALIZED);
15275   if (tmp == NULL)
15276     return NULL;
15277   add_loc_descr (&ret, tmp);
15278   l1label = new_loc_descr (DW_OP_pick, 2, 0);
15279   add_loc_descr (&ret, l1label);
15280   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15281 			    mode, mem_mode,
15282 			    VAR_INIT_STATUS_INITIALIZED);
15283   add_loc_descr (&ret, tmp);
15284   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15285   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15286   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15287   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15288 			    VAR_INIT_STATUS_INITIALIZED);
15289   if (tmp == NULL)
15290     return NULL;
15291   add_loc_descr (&ret, tmp);
15292   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15293   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15294   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15295   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15296   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15297   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15298   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15299 			    VAR_INIT_STATUS_INITIALIZED);
15300   add_loc_descr (&ret, tmp);
15301   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15302   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15303   add_loc_descr (&ret, l2jump);
15304   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15305 			    VAR_INIT_STATUS_INITIALIZED);
15306   add_loc_descr (&ret, tmp);
15307   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15308   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15309   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15310   add_loc_descr (&ret, l1jump);
15311   l2label = new_loc_descr (DW_OP_drop, 0, 0);
15312   add_loc_descr (&ret, l2label);
15313   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15314   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15315   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15316   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15317   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15318   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15319   return ret;
15320 }
15321 
15322 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15323    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15324    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15325    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15326 
15327    ROTATERT is similar:
15328    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15329    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15330    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
15331 
15332 static dw_loc_descr_ref
rotate_loc_descriptor(rtx rtl,scalar_int_mode mode,machine_mode mem_mode)15333 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15334 		       machine_mode mem_mode)
15335 {
15336   rtx rtlop1 = XEXP (rtl, 1);
15337   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15338   int i;
15339 
15340   if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15341     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15342   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15343 			    VAR_INIT_STATUS_INITIALIZED);
15344   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15345 			    VAR_INIT_STATUS_INITIALIZED);
15346   if (op0 == NULL || op1 == NULL)
15347     return NULL;
15348   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15349     for (i = 0; i < 2; i++)
15350       {
15351 	if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15352 	  mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15353 					mode, mem_mode,
15354 					VAR_INIT_STATUS_INITIALIZED);
15355 	else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15356 	  mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15357 				   ? DW_OP_const4u
15358 				   : HOST_BITS_PER_WIDE_INT == 64
15359 				   ? DW_OP_const8u : DW_OP_constu,
15360 				   GET_MODE_MASK (mode), 0);
15361 	else
15362 	  mask[i] = NULL;
15363 	if (mask[i] == NULL)
15364 	  return NULL;
15365 	add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15366       }
15367   ret = op0;
15368   add_loc_descr (&ret, op1);
15369   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15370   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15371   if (GET_CODE (rtl) == ROTATERT)
15372     {
15373       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15374       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15375 					  GET_MODE_BITSIZE (mode), 0));
15376     }
15377   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15378   if (mask[0] != NULL)
15379     add_loc_descr (&ret, mask[0]);
15380   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15381   if (mask[1] != NULL)
15382     {
15383       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15384       add_loc_descr (&ret, mask[1]);
15385       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15386     }
15387   if (GET_CODE (rtl) == ROTATE)
15388     {
15389       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15390       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15391 					  GET_MODE_BITSIZE (mode), 0));
15392     }
15393   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15394   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15395   return ret;
15396 }
15397 
15398 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
15399    for DEBUG_PARAMETER_REF RTL.  */
15400 
15401 static dw_loc_descr_ref
parameter_ref_descriptor(rtx rtl)15402 parameter_ref_descriptor (rtx rtl)
15403 {
15404   dw_loc_descr_ref ret;
15405   dw_die_ref ref;
15406 
15407   if (dwarf_strict)
15408     return NULL;
15409   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15410   /* With LTO during LTRANS we get the late DIE that refers to the early
15411      DIE, thus we add another indirection here.  This seems to confuse
15412      gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO.  */
15413   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15414   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15415   if (ref)
15416     {
15417       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15418       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15419       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15420     }
15421   else
15422     {
15423       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15424       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15425     }
15426   return ret;
15427 }
15428 
15429 /* The following routine converts the RTL for a variable or parameter
15430    (resident in memory) into an equivalent Dwarf representation of a
15431    mechanism for getting the address of that same variable onto the top of a
15432    hypothetical "address evaluation" stack.
15433 
15434    When creating memory location descriptors, we are effectively transforming
15435    the RTL for a memory-resident object into its Dwarf postfix expression
15436    equivalent.  This routine recursively descends an RTL tree, turning
15437    it into Dwarf postfix code as it goes.
15438 
15439    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15440 
15441    MEM_MODE is the mode of the memory reference, needed to handle some
15442    autoincrement addressing modes.
15443 
15444    Return 0 if we can't represent the location.  */
15445 
15446 dw_loc_descr_ref
mem_loc_descriptor(rtx rtl,machine_mode mode,machine_mode mem_mode,enum var_init_status initialized)15447 mem_loc_descriptor (rtx rtl, machine_mode mode,
15448 		    machine_mode mem_mode,
15449 		    enum var_init_status initialized)
15450 {
15451   dw_loc_descr_ref mem_loc_result = NULL;
15452   enum dwarf_location_atom op;
15453   dw_loc_descr_ref op0, op1;
15454   rtx inner = NULL_RTX;
15455   poly_int64 offset;
15456 
15457   if (mode == VOIDmode)
15458     mode = GET_MODE (rtl);
15459 
15460   /* Note that for a dynamically sized array, the location we will generate a
15461      description of here will be the lowest numbered location which is
15462      actually within the array.  That's *not* necessarily the same as the
15463      zeroth element of the array.  */
15464 
15465   rtl = targetm.delegitimize_address (rtl);
15466 
15467   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15468     return NULL;
15469 
15470   scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15471   switch (GET_CODE (rtl))
15472     {
15473     case POST_INC:
15474     case POST_DEC:
15475     case POST_MODIFY:
15476       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15477 
15478     case SUBREG:
15479       /* The case of a subreg may arise when we have a local (register)
15480 	 variable or a formal (register) parameter which doesn't quite fill
15481 	 up an entire register.  For now, just assume that it is
15482 	 legitimate to make the Dwarf info refer to the whole register which
15483 	 contains the given subreg.  */
15484       if (!subreg_lowpart_p (rtl))
15485 	break;
15486       inner = SUBREG_REG (rtl);
15487       /* FALLTHRU */
15488     case TRUNCATE:
15489       if (inner == NULL_RTX)
15490         inner = XEXP (rtl, 0);
15491       if (is_a <scalar_int_mode> (mode, &int_mode)
15492 	  && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15493 	  && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15494 #ifdef POINTERS_EXTEND_UNSIGNED
15495 	      || (int_mode == Pmode && mem_mode != VOIDmode)
15496 #endif
15497 	     )
15498 	  && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15499 	{
15500 	  mem_loc_result = mem_loc_descriptor (inner,
15501 					       inner_mode,
15502 					       mem_mode, initialized);
15503 	  break;
15504 	}
15505       if (dwarf_strict && dwarf_version < 5)
15506 	break;
15507       if (is_a <scalar_int_mode> (mode, &int_mode)
15508 	  && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15509 	  ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15510 	  : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15511 	{
15512 	  dw_die_ref type_die;
15513 	  dw_loc_descr_ref cvt;
15514 
15515 	  mem_loc_result = mem_loc_descriptor (inner,
15516 					       GET_MODE (inner),
15517 					       mem_mode, initialized);
15518 	  if (mem_loc_result == NULL)
15519 	    break;
15520 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15521 	  if (type_die == NULL)
15522 	    {
15523 	      mem_loc_result = NULL;
15524 	      break;
15525 	    }
15526 	  if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15527 	    cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15528 	  else
15529 	    cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15530 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15531 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15532 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15533 	  add_loc_descr (&mem_loc_result, cvt);
15534 	  if (is_a <scalar_int_mode> (mode, &int_mode)
15535 	      && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15536 	    {
15537 	      /* Convert it to untyped afterwards.  */
15538 	      cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15539 	      add_loc_descr (&mem_loc_result, cvt);
15540 	    }
15541 	}
15542       break;
15543 
15544     case REG:
15545       if (!is_a <scalar_int_mode> (mode, &int_mode)
15546 	  || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15547 	      && rtl != arg_pointer_rtx
15548 	      && rtl != frame_pointer_rtx
15549 #ifdef POINTERS_EXTEND_UNSIGNED
15550 	      && (int_mode != Pmode || mem_mode == VOIDmode)
15551 #endif
15552 	      ))
15553 	{
15554 	  dw_die_ref type_die;
15555 	  unsigned int dbx_regnum;
15556 
15557 	  if (dwarf_strict && dwarf_version < 5)
15558 	    break;
15559 	  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15560 	    break;
15561 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15562 	  if (type_die == NULL)
15563 	    break;
15564 
15565 	  dbx_regnum = dbx_reg_number (rtl);
15566 	  if (dbx_regnum == IGNORED_DWARF_REGNUM)
15567 	    break;
15568 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15569 					  dbx_regnum, 0);
15570 	  mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15571 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15572 	  mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15573 	  break;
15574 	}
15575       /* Whenever a register number forms a part of the description of the
15576 	 method for calculating the (dynamic) address of a memory resident
15577 	 object, DWARF rules require the register number be referred to as
15578 	 a "base register".  This distinction is not based in any way upon
15579 	 what category of register the hardware believes the given register
15580 	 belongs to.  This is strictly DWARF terminology we're dealing with
15581 	 here. Note that in cases where the location of a memory-resident
15582 	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15583 	 OP_CONST (0)) the actual DWARF location descriptor that we generate
15584 	 may just be OP_BASEREG (basereg).  This may look deceptively like
15585 	 the object in question was allocated to a register (rather than in
15586 	 memory) so DWARF consumers need to be aware of the subtle
15587 	 distinction between OP_REG and OP_BASEREG.  */
15588       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15589 	mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15590       else if (stack_realign_drap
15591 	       && crtl->drap_reg
15592 	       && crtl->args.internal_arg_pointer == rtl
15593 	       && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15594 	{
15595 	  /* If RTL is internal_arg_pointer, which has been optimized
15596 	     out, use DRAP instead.  */
15597 	  mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15598 					    VAR_INIT_STATUS_INITIALIZED);
15599 	}
15600       break;
15601 
15602     case SIGN_EXTEND:
15603     case ZERO_EXTEND:
15604       if (!is_a <scalar_int_mode> (mode, &int_mode)
15605 	  || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15606 	break;
15607       op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15608 				mem_mode, VAR_INIT_STATUS_INITIALIZED);
15609       if (op0 == 0)
15610 	break;
15611       else if (GET_CODE (rtl) == ZERO_EXTEND
15612 	       && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15613 	       && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15614 	       /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15615 		  to expand zero extend as two shifts instead of
15616 		  masking.  */
15617 	       && GET_MODE_SIZE (inner_mode) <= 4)
15618 	{
15619 	  mem_loc_result = op0;
15620 	  add_loc_descr (&mem_loc_result,
15621 			 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15622 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15623 	}
15624       else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15625 	{
15626 	  int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15627 	  shift *= BITS_PER_UNIT;
15628 	  if (GET_CODE (rtl) == SIGN_EXTEND)
15629 	    op = DW_OP_shra;
15630 	  else
15631 	    op = DW_OP_shr;
15632 	  mem_loc_result = op0;
15633 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15634 	  add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15635 	  add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15636 	  add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15637 	}
15638       else if (!dwarf_strict || dwarf_version >= 5)
15639 	{
15640 	  dw_die_ref type_die1, type_die2;
15641 	  dw_loc_descr_ref cvt;
15642 
15643 	  type_die1 = base_type_for_mode (inner_mode,
15644 					  GET_CODE (rtl) == ZERO_EXTEND);
15645 	  if (type_die1 == NULL)
15646 	    break;
15647 	  type_die2 = base_type_for_mode (int_mode, 1);
15648 	  if (type_die2 == NULL)
15649 	    break;
15650 	  mem_loc_result = op0;
15651 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15652 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15653 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15654 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15655 	  add_loc_descr (&mem_loc_result, cvt);
15656 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15657 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15658 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15659 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15660 	  add_loc_descr (&mem_loc_result, cvt);
15661 	}
15662       break;
15663 
15664     case MEM:
15665       {
15666 	rtx new_rtl = avoid_constant_pool_reference (rtl);
15667 	if (new_rtl != rtl)
15668 	  {
15669 	    mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15670 						 initialized);
15671 	    if (mem_loc_result != NULL)
15672 	      return mem_loc_result;
15673 	  }
15674       }
15675       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15676 					   get_address_mode (rtl), mode,
15677 					   VAR_INIT_STATUS_INITIALIZED);
15678       if (mem_loc_result == NULL)
15679 	mem_loc_result = tls_mem_loc_descriptor (rtl);
15680       if (mem_loc_result != NULL)
15681 	{
15682 	  if (!is_a <scalar_int_mode> (mode, &int_mode)
15683 	      || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15684 	    {
15685 	      dw_die_ref type_die;
15686 	      dw_loc_descr_ref deref;
15687 	      HOST_WIDE_INT size;
15688 
15689 	      if (dwarf_strict && dwarf_version < 5)
15690 		return NULL;
15691 	      if (!GET_MODE_SIZE (mode).is_constant (&size))
15692 		return NULL;
15693 	      type_die
15694 		= base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15695 	      if (type_die == NULL)
15696 		return NULL;
15697 	      deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15698 	      deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15699 	      deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15700 	      deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15701 	      add_loc_descr (&mem_loc_result, deref);
15702 	    }
15703 	  else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15704 	    add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15705 	  else
15706 	    add_loc_descr (&mem_loc_result,
15707 			   new_loc_descr (DW_OP_deref_size,
15708 					  GET_MODE_SIZE (int_mode), 0));
15709 	}
15710       break;
15711 
15712     case LO_SUM:
15713       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15714 
15715     case LABEL_REF:
15716       /* Some ports can transform a symbol ref into a label ref, because
15717 	 the symbol ref is too far away and has to be dumped into a constant
15718 	 pool.  */
15719     case CONST:
15720     case SYMBOL_REF:
15721     case UNSPEC:
15722       if (!is_a <scalar_int_mode> (mode, &int_mode)
15723 	  || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15724 #ifdef POINTERS_EXTEND_UNSIGNED
15725 	      && (int_mode != Pmode || mem_mode == VOIDmode)
15726 #endif
15727 	      ))
15728 	break;
15729 
15730       if (GET_CODE (rtl) == UNSPEC)
15731 	{
15732 	  /* If delegitimize_address couldn't do anything with the UNSPEC, we
15733 	     can't express it in the debug info.  This can happen e.g. with some
15734 	     TLS UNSPECs.  Allow UNSPECs formerly from CONST that the backend
15735 	     approves.  */
15736 	  bool not_ok = false;
15737 	  subrtx_var_iterator::array_type array;
15738 	  FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15739 	    if (*iter != rtl && !CONSTANT_P (*iter))
15740 	      {
15741 		not_ok = true;
15742 		break;
15743 	      }
15744 
15745 	  if (not_ok)
15746 	    break;
15747 
15748 	  FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
15749 	    if (!const_ok_for_output_1 (*iter))
15750 	      {
15751 		not_ok = true;
15752 		break;
15753 	      }
15754 
15755 	  if (not_ok)
15756 	    break;
15757 
15758 	  rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
15759 	  goto symref;
15760 	}
15761 
15762       if (GET_CODE (rtl) == SYMBOL_REF
15763 	  && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15764 	{
15765 	  dw_loc_descr_ref temp;
15766 
15767 	  /* If this is not defined, we have no way to emit the data.  */
15768 	  if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15769 	    break;
15770 
15771           temp = new_addr_loc_descr (rtl, dtprel_true);
15772 
15773 	  /* We check for DWARF 5 here because gdb did not implement
15774 	     DW_OP_form_tls_address until after 7.12.  */
15775 	  mem_loc_result = new_loc_descr ((dwarf_version >= 5
15776 					   ? DW_OP_form_tls_address
15777 					   : DW_OP_GNU_push_tls_address),
15778 					  0, 0);
15779 	  add_loc_descr (&mem_loc_result, temp);
15780 
15781 	  break;
15782 	}
15783 
15784       if (!const_ok_for_output (rtl))
15785 	{
15786 	  if (GET_CODE (rtl) == CONST)
15787 	    switch (GET_CODE (XEXP (rtl, 0)))
15788 	      {
15789 	      case NOT:
15790 		op = DW_OP_not;
15791 		goto try_const_unop;
15792 	      case NEG:
15793 		op = DW_OP_neg;
15794 		goto try_const_unop;
15795 	      try_const_unop:
15796 		rtx arg;
15797 		arg = XEXP (XEXP (rtl, 0), 0);
15798 		if (!CONSTANT_P (arg))
15799 		  arg = gen_rtx_CONST (int_mode, arg);
15800 		op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15801 					  initialized);
15802 		if (op0)
15803 		  {
15804 		    mem_loc_result = op0;
15805 		    add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15806 		  }
15807 		break;
15808 	      default:
15809 		mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15810 						     mem_mode, initialized);
15811 		break;
15812 	      }
15813 	  break;
15814 	}
15815 
15816     symref:
15817       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15818       vec_safe_push (used_rtx_array, rtl);
15819       break;
15820 
15821     case CONCAT:
15822     case CONCATN:
15823     case VAR_LOCATION:
15824     case DEBUG_IMPLICIT_PTR:
15825       expansion_failed (NULL_TREE, rtl,
15826 			"CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15827       return 0;
15828 
15829     case ENTRY_VALUE:
15830       if (dwarf_strict && dwarf_version < 5)
15831 	return NULL;
15832       if (REG_P (ENTRY_VALUE_EXP (rtl)))
15833 	{
15834 	  if (!is_a <scalar_int_mode> (mode, &int_mode)
15835 	      || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15836 	    op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15837 				      VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15838 	  else
15839 	    {
15840               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15841 	      if (dbx_regnum == IGNORED_DWARF_REGNUM)
15842 		return NULL;
15843 	      op0 = one_reg_loc_descriptor (dbx_regnum,
15844 					    VAR_INIT_STATUS_INITIALIZED);
15845 	    }
15846 	}
15847       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15848 	       && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15849 	{
15850 	  op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15851 				    VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15852 	  if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15853 	    return NULL;
15854 	}
15855       else
15856 	gcc_unreachable ();
15857       if (op0 == NULL)
15858 	return NULL;
15859       mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15860       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15861       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15862       break;
15863 
15864     case DEBUG_PARAMETER_REF:
15865       mem_loc_result = parameter_ref_descriptor (rtl);
15866       break;
15867 
15868     case PRE_MODIFY:
15869       /* Extract the PLUS expression nested inside and fall into
15870 	 PLUS code below.  */
15871       rtl = XEXP (rtl, 1);
15872       goto plus;
15873 
15874     case PRE_INC:
15875     case PRE_DEC:
15876       /* Turn these into a PLUS expression and fall into the PLUS code
15877 	 below.  */
15878       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15879 			  gen_int_mode (GET_CODE (rtl) == PRE_INC
15880 					? GET_MODE_UNIT_SIZE (mem_mode)
15881 					: -GET_MODE_UNIT_SIZE (mem_mode),
15882 					mode));
15883 
15884       /* fall through */
15885 
15886     case PLUS:
15887     plus:
15888       if (is_based_loc (rtl)
15889 	  && is_a <scalar_int_mode> (mode, &int_mode)
15890 	  && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15891 	      || XEXP (rtl, 0) == arg_pointer_rtx
15892 	      || XEXP (rtl, 0) == frame_pointer_rtx))
15893 	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15894 					  INTVAL (XEXP (rtl, 1)),
15895 					  VAR_INIT_STATUS_INITIALIZED);
15896       else
15897 	{
15898 	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15899 					       VAR_INIT_STATUS_INITIALIZED);
15900 	  if (mem_loc_result == 0)
15901 	    break;
15902 
15903 	  if (CONST_INT_P (XEXP (rtl, 1))
15904 	      && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15905 		  <= DWARF2_ADDR_SIZE))
15906 	    loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15907 	  else
15908 	    {
15909 	      op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15910 					VAR_INIT_STATUS_INITIALIZED);
15911 	      if (op1 == 0)
15912 		return NULL;
15913 	      add_loc_descr (&mem_loc_result, op1);
15914 	      add_loc_descr (&mem_loc_result,
15915 			     new_loc_descr (DW_OP_plus, 0, 0));
15916 	    }
15917 	}
15918       break;
15919 
15920     /* If a pseudo-reg is optimized away, it is possible for it to
15921        be replaced with a MEM containing a multiply or shift.  */
15922     case MINUS:
15923       op = DW_OP_minus;
15924       goto do_binop;
15925 
15926     case MULT:
15927       op = DW_OP_mul;
15928       goto do_binop;
15929 
15930     case DIV:
15931       if ((!dwarf_strict || dwarf_version >= 5)
15932 	  && is_a <scalar_int_mode> (mode, &int_mode)
15933 	  && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15934 	{
15935 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
15936 					base_type_for_mode (mode, 0),
15937 					int_mode, mem_mode);
15938 	  break;
15939 	}
15940       op = DW_OP_div;
15941       goto do_binop;
15942 
15943     case UMOD:
15944       op = DW_OP_mod;
15945       goto do_binop;
15946 
15947     case ASHIFT:
15948       op = DW_OP_shl;
15949       goto do_shift;
15950 
15951     case ASHIFTRT:
15952       op = DW_OP_shra;
15953       goto do_shift;
15954 
15955     case LSHIFTRT:
15956       op = DW_OP_shr;
15957       goto do_shift;
15958 
15959     do_shift:
15960       if (!is_a <scalar_int_mode> (mode, &int_mode))
15961 	break;
15962       op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15963 				VAR_INIT_STATUS_INITIALIZED);
15964       {
15965 	rtx rtlop1 = XEXP (rtl, 1);
15966 	if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15967 	    && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15968 	  rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15969 	op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15970 				  VAR_INIT_STATUS_INITIALIZED);
15971       }
15972 
15973       if (op0 == 0 || op1 == 0)
15974 	break;
15975 
15976       mem_loc_result = op0;
15977       add_loc_descr (&mem_loc_result, op1);
15978       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15979       break;
15980 
15981     case AND:
15982       op = DW_OP_and;
15983       goto do_binop;
15984 
15985     case IOR:
15986       op = DW_OP_or;
15987       goto do_binop;
15988 
15989     case XOR:
15990       op = DW_OP_xor;
15991       goto do_binop;
15992 
15993     do_binop:
15994       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15995 				VAR_INIT_STATUS_INITIALIZED);
15996       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15997 				VAR_INIT_STATUS_INITIALIZED);
15998 
15999       if (op0 == 0 || op1 == 0)
16000 	break;
16001 
16002       mem_loc_result = op0;
16003       add_loc_descr (&mem_loc_result, op1);
16004       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16005       break;
16006 
16007     case MOD:
16008       if ((!dwarf_strict || dwarf_version >= 5)
16009 	  && is_a <scalar_int_mode> (mode, &int_mode)
16010 	  && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16011 	{
16012 	  mem_loc_result = typed_binop (DW_OP_mod, rtl,
16013 					base_type_for_mode (mode, 0),
16014 					int_mode, mem_mode);
16015 	  break;
16016 	}
16017 
16018       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16019 				VAR_INIT_STATUS_INITIALIZED);
16020       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16021 				VAR_INIT_STATUS_INITIALIZED);
16022 
16023       if (op0 == 0 || op1 == 0)
16024 	break;
16025 
16026       mem_loc_result = op0;
16027       add_loc_descr (&mem_loc_result, op1);
16028       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16029       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16030       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16031       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16032       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16033       break;
16034 
16035     case UDIV:
16036       if ((!dwarf_strict || dwarf_version >= 5)
16037 	  && is_a <scalar_int_mode> (mode, &int_mode))
16038 	{
16039 	  if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16040 	    {
16041 	      op = DW_OP_div;
16042 	      goto do_binop;
16043 	    }
16044 	  mem_loc_result = typed_binop (DW_OP_div, rtl,
16045 					base_type_for_mode (int_mode, 1),
16046 					int_mode, mem_mode);
16047 	}
16048       break;
16049 
16050     case NOT:
16051       op = DW_OP_not;
16052       goto do_unop;
16053 
16054     case ABS:
16055       op = DW_OP_abs;
16056       goto do_unop;
16057 
16058     case NEG:
16059       op = DW_OP_neg;
16060       goto do_unop;
16061 
16062     do_unop:
16063       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16064 				VAR_INIT_STATUS_INITIALIZED);
16065 
16066       if (op0 == 0)
16067 	break;
16068 
16069       mem_loc_result = op0;
16070       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16071       break;
16072 
16073     case CONST_INT:
16074       if (!is_a <scalar_int_mode> (mode, &int_mode)
16075 	  || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16076 #ifdef POINTERS_EXTEND_UNSIGNED
16077 	  || (int_mode == Pmode
16078 	      && mem_mode != VOIDmode
16079 	      && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16080 #endif
16081 	  )
16082 	{
16083 	  mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16084 	  break;
16085 	}
16086       if ((!dwarf_strict || dwarf_version >= 5)
16087 	  && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16088 	      || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16089 	{
16090 	  dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16091 	  scalar_int_mode amode;
16092 	  if (type_die == NULL)
16093 	    return NULL;
16094 	  if (INTVAL (rtl) >= 0
16095 	      && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16096 		  .exists (&amode))
16097 	      && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16098 	      /* const DW_OP_convert <XXX> vs.
16099 		 DW_OP_const_type <XXX, 1, const>.  */
16100 	      && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16101 		 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16102 	    {
16103 	      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16104 	      op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16105 	      op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16106 	      op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16107 	      op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16108 	      add_loc_descr (&mem_loc_result, op0);
16109 	      return mem_loc_result;
16110 	    }
16111 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16112 					  INTVAL (rtl));
16113 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16114 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16115 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16116 	  if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16117 	    mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16118 	  else
16119 	    {
16120 	      mem_loc_result->dw_loc_oprnd2.val_class
16121 		= dw_val_class_const_double;
16122 	      mem_loc_result->dw_loc_oprnd2.v.val_double
16123 		= double_int::from_shwi (INTVAL (rtl));
16124 	    }
16125 	}
16126       break;
16127 
16128     case CONST_DOUBLE:
16129       if (!dwarf_strict || dwarf_version >= 5)
16130 	{
16131 	  dw_die_ref type_die;
16132 
16133 	  /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16134 	     CONST_DOUBLE rtx could represent either a large integer
16135 	     or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
16136 	     the value is always a floating point constant.
16137 
16138 	     When it is an integer, a CONST_DOUBLE is used whenever
16139 	     the constant requires 2 HWIs to be adequately represented.
16140 	     We output CONST_DOUBLEs as blocks.  */
16141 	  if (mode == VOIDmode
16142 	      || (GET_MODE (rtl) == VOIDmode
16143 		  && maybe_ne (GET_MODE_BITSIZE (mode),
16144 			       HOST_BITS_PER_DOUBLE_INT)))
16145 	    break;
16146 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16147 	  if (type_die == NULL)
16148 	    return NULL;
16149 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16150 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16151 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16152 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16153 #if TARGET_SUPPORTS_WIDE_INT == 0
16154 	  if (!SCALAR_FLOAT_MODE_P (mode))
16155 	    {
16156 	      mem_loc_result->dw_loc_oprnd2.val_class
16157 		= dw_val_class_const_double;
16158 	      mem_loc_result->dw_loc_oprnd2.v.val_double
16159 		= rtx_to_double_int (rtl);
16160 	    }
16161 	  else
16162 #endif
16163 	    {
16164 	      scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16165 	      unsigned int length = GET_MODE_SIZE (float_mode);
16166 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16167 	      unsigned int elt_size = insert_float (rtl, array);
16168 
16169 	      mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16170 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16171 		= length / elt_size;
16172 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16173 	      mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16174 	    }
16175 	}
16176       break;
16177 
16178     case CONST_WIDE_INT:
16179       if (!dwarf_strict || dwarf_version >= 5)
16180 	{
16181 	  dw_die_ref type_die;
16182 
16183 	  type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16184 	  if (type_die == NULL)
16185 	    return NULL;
16186 	  mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16187 	  mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16188 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16189 	  mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16190 	  mem_loc_result->dw_loc_oprnd2.val_class
16191 	    = dw_val_class_wide_int;
16192 	  mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16193 	  *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16194 	}
16195       break;
16196 
16197     case CONST_POLY_INT:
16198       mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16199       break;
16200 
16201     case EQ:
16202       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16203       break;
16204 
16205     case GE:
16206       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16207       break;
16208 
16209     case GT:
16210       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16211       break;
16212 
16213     case LE:
16214       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16215       break;
16216 
16217     case LT:
16218       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16219       break;
16220 
16221     case NE:
16222       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16223       break;
16224 
16225     case GEU:
16226       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16227       break;
16228 
16229     case GTU:
16230       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16231       break;
16232 
16233     case LEU:
16234       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16235       break;
16236 
16237     case LTU:
16238       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16239       break;
16240 
16241     case UMIN:
16242     case UMAX:
16243       if (!SCALAR_INT_MODE_P (mode))
16244 	break;
16245       /* FALLTHRU */
16246     case SMIN:
16247     case SMAX:
16248       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16249       break;
16250 
16251     case ZERO_EXTRACT:
16252     case SIGN_EXTRACT:
16253       if (CONST_INT_P (XEXP (rtl, 1))
16254 	  && CONST_INT_P (XEXP (rtl, 2))
16255 	  && is_a <scalar_int_mode> (mode, &int_mode)
16256 	  && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16257 	  && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16258 	  && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16259 	  && ((unsigned) INTVAL (XEXP (rtl, 1))
16260 	      + (unsigned) INTVAL (XEXP (rtl, 2))
16261 	      <= GET_MODE_BITSIZE (int_mode)))
16262 	{
16263 	  int shift, size;
16264 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16265 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
16266 	  if (op0 == 0)
16267 	    break;
16268 	  if (GET_CODE (rtl) == SIGN_EXTRACT)
16269 	    op = DW_OP_shra;
16270 	  else
16271 	    op = DW_OP_shr;
16272 	  mem_loc_result = op0;
16273 	  size = INTVAL (XEXP (rtl, 1));
16274 	  shift = INTVAL (XEXP (rtl, 2));
16275 	  if (BITS_BIG_ENDIAN)
16276 	    shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16277 	  if (shift + size != (int) DWARF2_ADDR_SIZE)
16278 	    {
16279 	      add_loc_descr (&mem_loc_result,
16280 			     int_loc_descriptor (DWARF2_ADDR_SIZE
16281 						 - shift - size));
16282 	      add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16283 	    }
16284 	  if (size != (int) DWARF2_ADDR_SIZE)
16285 	    {
16286 	      add_loc_descr (&mem_loc_result,
16287 			     int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16288 	      add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16289 	    }
16290 	}
16291       break;
16292 
16293     case IF_THEN_ELSE:
16294       {
16295 	dw_loc_descr_ref op2, bra_node, drop_node;
16296 	op0 = mem_loc_descriptor (XEXP (rtl, 0),
16297 				  GET_MODE (XEXP (rtl, 0)) == VOIDmode
16298 				  ? word_mode : GET_MODE (XEXP (rtl, 0)),
16299 				  mem_mode, VAR_INIT_STATUS_INITIALIZED);
16300 	op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16301 				  VAR_INIT_STATUS_INITIALIZED);
16302 	op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16303 				  VAR_INIT_STATUS_INITIALIZED);
16304 	if (op0 == NULL || op1 == NULL || op2 == NULL)
16305 	  break;
16306 
16307 	mem_loc_result = op1;
16308 	add_loc_descr (&mem_loc_result, op2);
16309 	add_loc_descr (&mem_loc_result, op0);
16310 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16311 	add_loc_descr (&mem_loc_result, bra_node);
16312 	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16313 	drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16314 	add_loc_descr (&mem_loc_result, drop_node);
16315 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16316 	bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16317       }
16318       break;
16319 
16320     case FLOAT_EXTEND:
16321     case FLOAT_TRUNCATE:
16322     case FLOAT:
16323     case UNSIGNED_FLOAT:
16324     case FIX:
16325     case UNSIGNED_FIX:
16326       if (!dwarf_strict || dwarf_version >= 5)
16327 	{
16328 	  dw_die_ref type_die;
16329 	  dw_loc_descr_ref cvt;
16330 
16331 	  op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16332 				    mem_mode, VAR_INIT_STATUS_INITIALIZED);
16333 	  if (op0 == NULL)
16334 	    break;
16335 	  if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16336 	      && (GET_CODE (rtl) == FLOAT
16337 		  || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16338 	    {
16339 	      type_die = base_type_for_mode (int_mode,
16340 					     GET_CODE (rtl) == UNSIGNED_FLOAT);
16341 	      if (type_die == NULL)
16342 		break;
16343 	      cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16344 	      cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16345 	      cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16346 	      cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16347 	      add_loc_descr (&op0, cvt);
16348 	    }
16349 	  type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16350 	  if (type_die == NULL)
16351 	    break;
16352 	  cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16353 	  cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16354 	  cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16355 	  cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16356 	  add_loc_descr (&op0, cvt);
16357 	  if (is_a <scalar_int_mode> (mode, &int_mode)
16358 	      && (GET_CODE (rtl) == FIX
16359 		  || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16360 	    {
16361 	      op0 = convert_descriptor_to_mode (int_mode, op0);
16362 	      if (op0 == NULL)
16363 		break;
16364 	    }
16365 	  mem_loc_result = op0;
16366 	}
16367       break;
16368 
16369     case CLZ:
16370     case CTZ:
16371     case FFS:
16372       if (is_a <scalar_int_mode> (mode, &int_mode))
16373 	mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16374       break;
16375 
16376     case POPCOUNT:
16377     case PARITY:
16378       if (is_a <scalar_int_mode> (mode, &int_mode))
16379 	mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16380       break;
16381 
16382     case BSWAP:
16383       if (is_a <scalar_int_mode> (mode, &int_mode))
16384 	mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16385       break;
16386 
16387     case ROTATE:
16388     case ROTATERT:
16389       if (is_a <scalar_int_mode> (mode, &int_mode))
16390 	mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16391       break;
16392 
16393     case COMPARE:
16394       /* In theory, we could implement the above.  */
16395       /* DWARF cannot represent the unsigned compare operations
16396 	 natively.  */
16397     case SS_MULT:
16398     case US_MULT:
16399     case SS_DIV:
16400     case US_DIV:
16401     case SS_PLUS:
16402     case US_PLUS:
16403     case SS_MINUS:
16404     case US_MINUS:
16405     case SS_NEG:
16406     case US_NEG:
16407     case SS_ABS:
16408     case SS_ASHIFT:
16409     case US_ASHIFT:
16410     case SS_TRUNCATE:
16411     case US_TRUNCATE:
16412     case UNORDERED:
16413     case ORDERED:
16414     case UNEQ:
16415     case UNGE:
16416     case UNGT:
16417     case UNLE:
16418     case UNLT:
16419     case LTGT:
16420     case FRACT_CONVERT:
16421     case UNSIGNED_FRACT_CONVERT:
16422     case SAT_FRACT:
16423     case UNSIGNED_SAT_FRACT:
16424     case SQRT:
16425     case ASM_OPERANDS:
16426     case VEC_MERGE:
16427     case VEC_SELECT:
16428     case VEC_CONCAT:
16429     case VEC_DUPLICATE:
16430     case VEC_SERIES:
16431     case HIGH:
16432     case FMA:
16433     case STRICT_LOW_PART:
16434     case CONST_VECTOR:
16435     case CONST_FIXED:
16436     case CLRSB:
16437     case CLOBBER:
16438       break;
16439 
16440     case CONST_STRING:
16441       resolve_one_addr (&rtl);
16442       goto symref;
16443 
16444     /* RTL sequences inside PARALLEL record a series of DWARF operations for
16445        the expression.  An UNSPEC rtx represents a raw DWARF operation,
16446        new_loc_descr is called for it to build the operation directly.
16447        Otherwise mem_loc_descriptor is called recursively.  */
16448     case PARALLEL:
16449       {
16450 	int index = 0;
16451 	dw_loc_descr_ref exp_result = NULL;
16452 
16453 	for (; index < XVECLEN (rtl, 0); index++)
16454 	  {
16455 	    rtx elem = XVECEXP (rtl, 0, index);
16456 	    if (GET_CODE (elem) == UNSPEC)
16457 	      {
16458 		/* Each DWARF operation UNSPEC contain two operands, if
16459 		   one operand is not used for the operation, const0_rtx is
16460 		   passed.  */
16461 		gcc_assert (XVECLEN (elem, 0) == 2);
16462 
16463 		HOST_WIDE_INT dw_op = XINT (elem, 1);
16464 		HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16465 		HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16466 		exp_result
16467 		  = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16468 				   oprnd2);
16469 	      }
16470 	    else
16471 	      exp_result
16472 		= mem_loc_descriptor (elem, mode, mem_mode,
16473 				      VAR_INIT_STATUS_INITIALIZED);
16474 
16475 	    if (!mem_loc_result)
16476 	      mem_loc_result = exp_result;
16477 	    else
16478 	      add_loc_descr (&mem_loc_result, exp_result);
16479 	  }
16480 
16481 	break;
16482       }
16483 
16484     default:
16485       if (flag_checking)
16486 	{
16487 	  print_rtl (stderr, rtl);
16488 	  gcc_unreachable ();
16489 	}
16490       break;
16491     }
16492 
16493   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16494     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16495 
16496   return mem_loc_result;
16497 }
16498 
16499 /* Return a descriptor that describes the concatenation of two locations.
16500    This is typically a complex variable.  */
16501 
16502 static dw_loc_descr_ref
concat_loc_descriptor(rtx x0,rtx x1,enum var_init_status initialized)16503 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16504 {
16505   /* At present we only track constant-sized pieces.  */
16506   unsigned int size0, size1;
16507   if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16508       || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16509     return 0;
16510 
16511   dw_loc_descr_ref cc_loc_result = NULL;
16512   dw_loc_descr_ref x0_ref
16513     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16514   dw_loc_descr_ref x1_ref
16515     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16516 
16517   if (x0_ref == 0 || x1_ref == 0)
16518     return 0;
16519 
16520   cc_loc_result = x0_ref;
16521   add_loc_descr_op_piece (&cc_loc_result, size0);
16522 
16523   add_loc_descr (&cc_loc_result, x1_ref);
16524   add_loc_descr_op_piece (&cc_loc_result, size1);
16525 
16526   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16527     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16528 
16529   return cc_loc_result;
16530 }
16531 
16532 /* Return a descriptor that describes the concatenation of N
16533    locations.  */
16534 
16535 static dw_loc_descr_ref
concatn_loc_descriptor(rtx concatn,enum var_init_status initialized)16536 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16537 {
16538   unsigned int i;
16539   dw_loc_descr_ref cc_loc_result = NULL;
16540   unsigned int n = XVECLEN (concatn, 0);
16541   unsigned int size;
16542 
16543   for (i = 0; i < n; ++i)
16544     {
16545       dw_loc_descr_ref ref;
16546       rtx x = XVECEXP (concatn, 0, i);
16547 
16548       /* At present we only track constant-sized pieces.  */
16549       if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16550 	return NULL;
16551 
16552       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16553       if (ref == NULL)
16554 	return NULL;
16555 
16556       add_loc_descr (&cc_loc_result, ref);
16557       add_loc_descr_op_piece (&cc_loc_result, size);
16558     }
16559 
16560   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16561     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16562 
16563   return cc_loc_result;
16564 }
16565 
16566 /* Helper function for loc_descriptor.  Return DW_OP_implicit_pointer
16567    for DEBUG_IMPLICIT_PTR RTL.  */
16568 
16569 static dw_loc_descr_ref
implicit_ptr_descriptor(rtx rtl,HOST_WIDE_INT offset)16570 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16571 {
16572   dw_loc_descr_ref ret;
16573   dw_die_ref ref;
16574 
16575   if (dwarf_strict && dwarf_version < 5)
16576     return NULL;
16577   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16578 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16579 	      || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16580   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16581   ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16582   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16583   if (ref)
16584     {
16585       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16586       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16587       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16588     }
16589   else
16590     {
16591       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16592       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16593     }
16594   return ret;
16595 }
16596 
16597 /* Output a proper Dwarf location descriptor for a variable or parameter
16598    which is either allocated in a register or in a memory location.  For a
16599    register, we just generate an OP_REG and the register number.  For a
16600    memory location we provide a Dwarf postfix expression describing how to
16601    generate the (dynamic) address of the object onto the address stack.
16602 
16603    MODE is mode of the decl if this loc_descriptor is going to be used in
16604    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16605    allowed, VOIDmode otherwise.
16606 
16607    If we don't know how to describe it, return 0.  */
16608 
16609 static dw_loc_descr_ref
loc_descriptor(rtx rtl,machine_mode mode,enum var_init_status initialized)16610 loc_descriptor (rtx rtl, machine_mode mode,
16611 		enum var_init_status initialized)
16612 {
16613   dw_loc_descr_ref loc_result = NULL;
16614   scalar_int_mode int_mode;
16615 
16616   switch (GET_CODE (rtl))
16617     {
16618     case SUBREG:
16619       /* The case of a subreg may arise when we have a local (register)
16620 	 variable or a formal (register) parameter which doesn't quite fill
16621 	 up an entire register.  For now, just assume that it is
16622 	 legitimate to make the Dwarf info refer to the whole register which
16623 	 contains the given subreg.  */
16624       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16625 	loc_result = loc_descriptor (SUBREG_REG (rtl),
16626 				     GET_MODE (SUBREG_REG (rtl)), initialized);
16627       else
16628 	goto do_default;
16629       break;
16630 
16631     case REG:
16632       loc_result = reg_loc_descriptor (rtl, initialized);
16633       break;
16634 
16635     case MEM:
16636       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16637 				       GET_MODE (rtl), initialized);
16638       if (loc_result == NULL)
16639 	loc_result = tls_mem_loc_descriptor (rtl);
16640       if (loc_result == NULL)
16641 	{
16642 	  rtx new_rtl = avoid_constant_pool_reference (rtl);
16643 	  if (new_rtl != rtl)
16644 	    loc_result = loc_descriptor (new_rtl, mode, initialized);
16645 	}
16646       break;
16647 
16648     case CONCAT:
16649       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16650 					  initialized);
16651       break;
16652 
16653     case CONCATN:
16654       loc_result = concatn_loc_descriptor (rtl, initialized);
16655       break;
16656 
16657     case VAR_LOCATION:
16658       /* Single part.  */
16659       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16660 	{
16661 	  rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16662 	  if (GET_CODE (loc) == EXPR_LIST)
16663 	    loc = XEXP (loc, 0);
16664 	  loc_result = loc_descriptor (loc, mode, initialized);
16665 	  break;
16666 	}
16667 
16668       rtl = XEXP (rtl, 1);
16669       /* FALLTHRU */
16670 
16671     case PARALLEL:
16672       {
16673 	rtvec par_elems = XVEC (rtl, 0);
16674 	int num_elem = GET_NUM_ELEM (par_elems);
16675 	machine_mode mode;
16676 	int i, size;
16677 
16678 	/* Create the first one, so we have something to add to.  */
16679 	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16680 				     VOIDmode, initialized);
16681 	if (loc_result == NULL)
16682 	  return NULL;
16683 	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16684 	/* At present we only track constant-sized pieces.  */
16685 	if (!GET_MODE_SIZE (mode).is_constant (&size))
16686 	  return NULL;
16687 	add_loc_descr_op_piece (&loc_result, size);
16688 	for (i = 1; i < num_elem; i++)
16689 	  {
16690 	    dw_loc_descr_ref temp;
16691 
16692 	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16693 				   VOIDmode, initialized);
16694 	    if (temp == NULL)
16695 	      return NULL;
16696 	    add_loc_descr (&loc_result, temp);
16697 	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16698 	    /* At present we only track constant-sized pieces.  */
16699 	    if (!GET_MODE_SIZE (mode).is_constant (&size))
16700 	      return NULL;
16701 	    add_loc_descr_op_piece (&loc_result, size);
16702 	  }
16703       }
16704       break;
16705 
16706     case CONST_INT:
16707       if (mode != VOIDmode && mode != BLKmode)
16708 	{
16709 	  int_mode = as_a <scalar_int_mode> (mode);
16710 	  loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16711 						      INTVAL (rtl));
16712 	}
16713       break;
16714 
16715     case CONST_DOUBLE:
16716       if (mode == VOIDmode)
16717 	mode = GET_MODE (rtl);
16718 
16719       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16720 	{
16721 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16722 
16723 	  /* Note that a CONST_DOUBLE rtx could represent either an integer
16724 	     or a floating-point constant.  A CONST_DOUBLE is used whenever
16725 	     the constant requires more than one word in order to be
16726 	     adequately represented.  We output CONST_DOUBLEs as blocks.  */
16727 	  scalar_mode smode = as_a <scalar_mode> (mode);
16728 	  loc_result = new_loc_descr (DW_OP_implicit_value,
16729 				      GET_MODE_SIZE (smode), 0);
16730 #if TARGET_SUPPORTS_WIDE_INT == 0
16731 	  if (!SCALAR_FLOAT_MODE_P (smode))
16732 	    {
16733 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16734 	      loc_result->dw_loc_oprnd2.v.val_double
16735 	        = rtx_to_double_int (rtl);
16736 	    }
16737 	  else
16738 #endif
16739 	    {
16740 	      unsigned int length = GET_MODE_SIZE (smode);
16741 	      unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16742 	      unsigned int elt_size = insert_float (rtl, array);
16743 
16744 	      loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16745 	      loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
16746 	      loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16747 	      loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16748 	    }
16749 	}
16750       break;
16751 
16752     case CONST_WIDE_INT:
16753       if (mode == VOIDmode)
16754 	mode = GET_MODE (rtl);
16755 
16756       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16757 	{
16758 	  int_mode = as_a <scalar_int_mode> (mode);
16759 	  loc_result = new_loc_descr (DW_OP_implicit_value,
16760 				      GET_MODE_SIZE (int_mode), 0);
16761 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16762 	  loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16763 	  *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16764 	}
16765       break;
16766 
16767     case CONST_VECTOR:
16768       if (mode == VOIDmode)
16769 	mode = GET_MODE (rtl);
16770 
16771       if (mode != VOIDmode
16772 	  /* The combination of a length and byte elt_size doesn't extend
16773 	     naturally to boolean vectors, where several elements are packed
16774 	     into the same byte.  */
16775 	  && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
16776 	  && (dwarf_version >= 4 || !dwarf_strict))
16777 	{
16778 	  unsigned int length;
16779 	  if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16780 	    return NULL;
16781 
16782 	  unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16783 	  unsigned char *array
16784 	    = ggc_vec_alloc<unsigned char> (length * elt_size);
16785 	  unsigned int i;
16786 	  unsigned char *p;
16787 	  machine_mode imode = GET_MODE_INNER (mode);
16788 
16789 	  gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16790 	  switch (GET_MODE_CLASS (mode))
16791 	    {
16792 	    case MODE_VECTOR_INT:
16793 	      for (i = 0, p = array; i < length; i++, p += elt_size)
16794 		{
16795 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
16796 		  insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16797 		}
16798 	      break;
16799 
16800 	    case MODE_VECTOR_FLOAT:
16801 	      for (i = 0, p = array; i < length; i++, p += elt_size)
16802 		{
16803 		  rtx elt = CONST_VECTOR_ELT (rtl, i);
16804 		  insert_float (elt, p);
16805 		}
16806 	      break;
16807 
16808 	    default:
16809 	      gcc_unreachable ();
16810 	    }
16811 
16812 	  loc_result = new_loc_descr (DW_OP_implicit_value,
16813 				      length * elt_size, 0);
16814 	  loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16815 	  loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16816 	  loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16817 	  loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16818 	}
16819       break;
16820 
16821     case CONST:
16822       if (mode == VOIDmode
16823 	  || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16824 	  || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16825 	  || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16826 	{
16827 	  loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16828 	  break;
16829 	}
16830       /* FALLTHROUGH */
16831     case SYMBOL_REF:
16832       if (!const_ok_for_output (rtl))
16833 	break;
16834       /* FALLTHROUGH */
16835     case LABEL_REF:
16836       if (is_a <scalar_int_mode> (mode, &int_mode)
16837 	  && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16838 	  && (dwarf_version >= 4 || !dwarf_strict))
16839 	{
16840          loc_result = new_addr_loc_descr (rtl, dtprel_false);
16841 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16842 	  vec_safe_push (used_rtx_array, rtl);
16843 	}
16844       break;
16845 
16846     case DEBUG_IMPLICIT_PTR:
16847       loc_result = implicit_ptr_descriptor (rtl, 0);
16848       break;
16849 
16850     case PLUS:
16851       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16852 	  && CONST_INT_P (XEXP (rtl, 1)))
16853 	{
16854 	  loc_result
16855 	    = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16856 	  break;
16857 	}
16858       /* FALLTHRU */
16859     do_default:
16860     default:
16861       if ((is_a <scalar_int_mode> (mode, &int_mode)
16862 	   && GET_MODE (rtl) == int_mode
16863 	   && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16864 	   && dwarf_version >= 4)
16865 	  || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16866 	{
16867 	  /* Value expression.  */
16868 	  loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16869 	  if (loc_result)
16870 	    add_loc_descr (&loc_result,
16871 			   new_loc_descr (DW_OP_stack_value, 0, 0));
16872 	}
16873       break;
16874     }
16875 
16876   return loc_result;
16877 }
16878 
16879 /* We need to figure out what section we should use as the base for the
16880    address ranges where a given location is valid.
16881    1. If this particular DECL has a section associated with it, use that.
16882    2. If this function has a section associated with it, use that.
16883    3. Otherwise, use the text section.
16884    XXX: If you split a variable across multiple sections, we won't notice.  */
16885 
16886 static const char *
secname_for_decl(const_tree decl)16887 secname_for_decl (const_tree decl)
16888 {
16889   const char *secname;
16890 
16891   if (VAR_OR_FUNCTION_DECL_P (decl)
16892       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16893       && DECL_SECTION_NAME (decl))
16894     secname = DECL_SECTION_NAME (decl);
16895   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16896     {
16897       if (in_cold_section_p)
16898 	{
16899 	  section *sec = current_function_section ();
16900 	  if (sec->common.flags & SECTION_NAMED)
16901 	    return sec->named.name;
16902 	}
16903       secname = DECL_SECTION_NAME (current_function_decl);
16904     }
16905   else if (cfun && in_cold_section_p)
16906     secname = crtl->subsections.cold_section_label;
16907   else
16908     secname = text_section_label;
16909 
16910   return secname;
16911 }
16912 
16913 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
16914 
16915 static bool
decl_by_reference_p(tree decl)16916 decl_by_reference_p (tree decl)
16917 {
16918   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16919   	   || VAR_P (decl))
16920 	  && DECL_BY_REFERENCE (decl));
16921 }
16922 
16923 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
16924    for VARLOC.  */
16925 
16926 static dw_loc_descr_ref
dw_loc_list_1(tree loc,rtx varloc,int want_address,enum var_init_status initialized)16927 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16928 	       enum var_init_status initialized)
16929 {
16930   int have_address = 0;
16931   dw_loc_descr_ref descr;
16932   machine_mode mode;
16933 
16934   if (want_address != 2)
16935     {
16936       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16937       /* Single part.  */
16938       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16939 	{
16940 	  varloc = PAT_VAR_LOCATION_LOC (varloc);
16941 	  if (GET_CODE (varloc) == EXPR_LIST)
16942 	    varloc = XEXP (varloc, 0);
16943 	  mode = GET_MODE (varloc);
16944 	  if (MEM_P (varloc))
16945 	    {
16946 	      rtx addr = XEXP (varloc, 0);
16947 	      descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16948 					  mode, initialized);
16949 	      if (descr)
16950 		have_address = 1;
16951 	      else
16952 		{
16953 		  rtx x = avoid_constant_pool_reference (varloc);
16954 		  if (x != varloc)
16955 		    descr = mem_loc_descriptor (x, mode, VOIDmode,
16956 						initialized);
16957 		}
16958 	    }
16959 	  else
16960 	    descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16961 	}
16962       else
16963 	return 0;
16964     }
16965   else
16966     {
16967       if (GET_CODE (varloc) == VAR_LOCATION)
16968 	mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16969       else
16970 	mode = DECL_MODE (loc);
16971       descr = loc_descriptor (varloc, mode, initialized);
16972       have_address = 1;
16973     }
16974 
16975   if (!descr)
16976     return 0;
16977 
16978   if (want_address == 2 && !have_address
16979       && (dwarf_version >= 4 || !dwarf_strict))
16980     {
16981       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16982 	{
16983 	  expansion_failed (loc, NULL_RTX,
16984 			    "DWARF address size mismatch");
16985 	  return 0;
16986 	}
16987       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16988       have_address = 1;
16989     }
16990   /* Show if we can't fill the request for an address.  */
16991   if (want_address && !have_address)
16992     {
16993       expansion_failed (loc, NULL_RTX,
16994 			"Want address and only have value");
16995       return 0;
16996     }
16997 
16998   /* If we've got an address and don't want one, dereference.  */
16999   if (!want_address && have_address)
17000     {
17001       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17002       enum dwarf_location_atom op;
17003 
17004       if (size > DWARF2_ADDR_SIZE || size == -1)
17005 	{
17006 	  expansion_failed (loc, NULL_RTX,
17007 			    "DWARF address size mismatch");
17008 	  return 0;
17009 	}
17010       else if (size == DWARF2_ADDR_SIZE)
17011 	op = DW_OP_deref;
17012       else
17013 	op = DW_OP_deref_size;
17014 
17015       add_loc_descr (&descr, new_loc_descr (op, size, 0));
17016     }
17017 
17018   return descr;
17019 }
17020 
17021 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17022    if it is not possible.  */
17023 
17024 static dw_loc_descr_ref
new_loc_descr_op_bit_piece(HOST_WIDE_INT bitsize,HOST_WIDE_INT offset)17025 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17026 {
17027   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17028     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17029   else if (dwarf_version >= 3 || !dwarf_strict)
17030     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17031   else
17032     return NULL;
17033 }
17034 
17035 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
17036    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
17037 
17038 static dw_loc_descr_ref
dw_sra_loc_expr(tree decl,rtx loc)17039 dw_sra_loc_expr (tree decl, rtx loc)
17040 {
17041   rtx p;
17042   unsigned HOST_WIDE_INT padsize = 0;
17043   dw_loc_descr_ref descr, *descr_tail;
17044   unsigned HOST_WIDE_INT decl_size;
17045   rtx varloc;
17046   enum var_init_status initialized;
17047 
17048   if (DECL_SIZE (decl) == NULL
17049       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17050     return NULL;
17051 
17052   decl_size = tree_to_uhwi (DECL_SIZE (decl));
17053   descr = NULL;
17054   descr_tail = &descr;
17055 
17056   for (p = loc; p; p = XEXP (p, 1))
17057     {
17058       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17059       rtx loc_note = *decl_piece_varloc_ptr (p);
17060       dw_loc_descr_ref cur_descr;
17061       dw_loc_descr_ref *tail, last = NULL;
17062       unsigned HOST_WIDE_INT opsize = 0;
17063 
17064       if (loc_note == NULL_RTX
17065 	  || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17066 	{
17067 	  padsize += bitsize;
17068 	  continue;
17069 	}
17070       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17071       varloc = NOTE_VAR_LOCATION (loc_note);
17072       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17073       if (cur_descr == NULL)
17074 	{
17075 	  padsize += bitsize;
17076 	  continue;
17077 	}
17078 
17079       /* Check that cur_descr either doesn't use
17080 	 DW_OP_*piece operations, or their sum is equal
17081 	 to bitsize.  Otherwise we can't embed it.  */
17082       for (tail = &cur_descr; *tail != NULL;
17083 	   tail = &(*tail)->dw_loc_next)
17084 	if ((*tail)->dw_loc_opc == DW_OP_piece)
17085 	  {
17086 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17087 		      * BITS_PER_UNIT;
17088 	    last = *tail;
17089 	  }
17090 	else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17091 	  {
17092 	    opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17093 	    last = *tail;
17094 	  }
17095 
17096       if (last != NULL && opsize != bitsize)
17097 	{
17098 	  padsize += bitsize;
17099 	  /* Discard the current piece of the descriptor and release any
17100 	     addr_table entries it uses.  */
17101 	  remove_loc_list_addr_table_entries (cur_descr);
17102 	  continue;
17103 	}
17104 
17105       /* If there is a hole, add DW_OP_*piece after empty DWARF
17106 	 expression, which means that those bits are optimized out.  */
17107       if (padsize)
17108 	{
17109 	  if (padsize > decl_size)
17110 	    {
17111 	      remove_loc_list_addr_table_entries (cur_descr);
17112 	      goto discard_descr;
17113 	    }
17114 	  decl_size -= padsize;
17115 	  *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17116 	  if (*descr_tail == NULL)
17117 	    {
17118 	      remove_loc_list_addr_table_entries (cur_descr);
17119 	      goto discard_descr;
17120 	    }
17121 	  descr_tail = &(*descr_tail)->dw_loc_next;
17122 	  padsize = 0;
17123 	}
17124       *descr_tail = cur_descr;
17125       descr_tail = tail;
17126       if (bitsize > decl_size)
17127 	goto discard_descr;
17128       decl_size -= bitsize;
17129       if (last == NULL)
17130 	{
17131 	  HOST_WIDE_INT offset = 0;
17132 	  if (GET_CODE (varloc) == VAR_LOCATION
17133 	      && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17134 	    {
17135 	      varloc = PAT_VAR_LOCATION_LOC (varloc);
17136 	      if (GET_CODE (varloc) == EXPR_LIST)
17137 		varloc = XEXP (varloc, 0);
17138 	    }
17139 	  do
17140 	    {
17141 	      if (GET_CODE (varloc) == CONST
17142 		  || GET_CODE (varloc) == SIGN_EXTEND
17143 		  || GET_CODE (varloc) == ZERO_EXTEND)
17144 		varloc = XEXP (varloc, 0);
17145 	      else if (GET_CODE (varloc) == SUBREG)
17146 		varloc = SUBREG_REG (varloc);
17147 	      else
17148 		break;
17149 	    }
17150 	  while (1);
17151 	  /* DW_OP_bit_size offset should be zero for register
17152 	     or implicit location descriptions and empty location
17153 	     descriptions, but for memory addresses needs big endian
17154 	     adjustment.  */
17155 	  if (MEM_P (varloc))
17156 	    {
17157 	      unsigned HOST_WIDE_INT memsize;
17158 	      if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17159 		goto discard_descr;
17160 	      memsize *= BITS_PER_UNIT;
17161 	      if (memsize != bitsize)
17162 		{
17163 		  if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17164 		      && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17165 		    goto discard_descr;
17166 		  if (memsize < bitsize)
17167 		    goto discard_descr;
17168 		  if (BITS_BIG_ENDIAN)
17169 		    offset = memsize - bitsize;
17170 		}
17171 	    }
17172 
17173 	  *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17174 	  if (*descr_tail == NULL)
17175 	    goto discard_descr;
17176 	  descr_tail = &(*descr_tail)->dw_loc_next;
17177 	}
17178     }
17179 
17180   /* If there were any non-empty expressions, add padding till the end of
17181      the decl.  */
17182   if (descr != NULL && decl_size != 0)
17183     {
17184       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17185       if (*descr_tail == NULL)
17186 	goto discard_descr;
17187     }
17188   return descr;
17189 
17190 discard_descr:
17191   /* Discard the descriptor and release any addr_table entries it uses.  */
17192   remove_loc_list_addr_table_entries (descr);
17193   return NULL;
17194 }
17195 
17196 /* Return the dwarf representation of the location list LOC_LIST of
17197    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
17198    function.  */
17199 
17200 static dw_loc_list_ref
dw_loc_list(var_loc_list * loc_list,tree decl,int want_address)17201 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17202 {
17203   const char *endname, *secname;
17204   var_loc_view endview;
17205   rtx varloc;
17206   enum var_init_status initialized;
17207   struct var_loc_node *node;
17208   dw_loc_descr_ref descr;
17209   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17210   dw_loc_list_ref list = NULL;
17211   dw_loc_list_ref *listp = &list;
17212 
17213   /* Now that we know what section we are using for a base,
17214      actually construct the list of locations.
17215      The first location information is what is passed to the
17216      function that creates the location list, and the remaining
17217      locations just get added on to that list.
17218      Note that we only know the start address for a location
17219      (IE location changes), so to build the range, we use
17220      the range [current location start, next location start].
17221      This means we have to special case the last node, and generate
17222      a range of [last location start, end of function label].  */
17223 
17224   if (cfun && crtl->has_bb_partition)
17225     {
17226       bool save_in_cold_section_p = in_cold_section_p;
17227       in_cold_section_p = first_function_block_is_cold;
17228       if (loc_list->last_before_switch == NULL)
17229 	in_cold_section_p = !in_cold_section_p;
17230       secname = secname_for_decl (decl);
17231       in_cold_section_p = save_in_cold_section_p;
17232     }
17233   else
17234     secname = secname_for_decl (decl);
17235 
17236   for (node = loc_list->first; node; node = node->next)
17237     {
17238       bool range_across_switch = false;
17239       if (GET_CODE (node->loc) == EXPR_LIST
17240 	  || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17241 	{
17242 	  if (GET_CODE (node->loc) == EXPR_LIST)
17243 	    {
17244 	      descr = NULL;
17245 	      /* This requires DW_OP_{,bit_}piece, which is not usable
17246 		 inside DWARF expressions.  */
17247 	      if (want_address == 2)
17248 		descr = dw_sra_loc_expr (decl, node->loc);
17249 	    }
17250 	  else
17251 	    {
17252 	      initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17253 	      varloc = NOTE_VAR_LOCATION (node->loc);
17254 	      descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17255 	    }
17256 	  if (descr)
17257 	    {
17258 	      /* If section switch happens in between node->label
17259 		 and node->next->label (or end of function) and
17260 		 we can't emit it as a single entry list,
17261 		 emit two ranges, first one ending at the end
17262 		 of first partition and second one starting at the
17263 		 beginning of second partition.  */
17264 	      if (node == loc_list->last_before_switch
17265 		  && (node != loc_list->first || loc_list->first->next
17266 		      /* If we are to emit a view number, we will emit
17267 			 a loclist rather than a single location
17268 			 expression for the entire function (see
17269 			 loc_list_has_views), so we have to split the
17270 			 range that straddles across partitions.  */
17271 		      || !ZERO_VIEW_P (node->view))
17272 		  && current_function_decl)
17273 		{
17274 		  endname = cfun->fde->dw_fde_end;
17275 		  endview = 0;
17276 		  range_across_switch = true;
17277 		}
17278 	      /* The variable has a location between NODE->LABEL and
17279 		 NODE->NEXT->LABEL.  */
17280 	      else if (node->next)
17281 		endname = node->next->label, endview = node->next->view;
17282 	      /* If the variable has a location at the last label
17283 		 it keeps its location until the end of function.  */
17284 	      else if (!current_function_decl)
17285 		endname = text_end_label, endview = 0;
17286 	      else
17287 		{
17288 		  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17289 					       current_function_funcdef_no);
17290 		  endname = ggc_strdup (label_id);
17291 		  endview = 0;
17292 		}
17293 
17294 	      *listp = new_loc_list (descr, node->label, node->view,
17295 				     endname, endview, secname);
17296 	      if (TREE_CODE (decl) == PARM_DECL
17297 		  && node == loc_list->first
17298 		  && NOTE_P (node->loc)
17299 		  && strcmp (node->label, endname) == 0)
17300 		(*listp)->force = true;
17301 	      listp = &(*listp)->dw_loc_next;
17302 	    }
17303 	}
17304 
17305       if (cfun
17306 	  && crtl->has_bb_partition
17307 	  && node == loc_list->last_before_switch)
17308 	{
17309 	  bool save_in_cold_section_p = in_cold_section_p;
17310 	  in_cold_section_p = !first_function_block_is_cold;
17311 	  secname = secname_for_decl (decl);
17312 	  in_cold_section_p = save_in_cold_section_p;
17313 	}
17314 
17315       if (range_across_switch)
17316 	{
17317 	  if (GET_CODE (node->loc) == EXPR_LIST)
17318 	    descr = dw_sra_loc_expr (decl, node->loc);
17319 	  else
17320 	    {
17321 	      initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17322 	      varloc = NOTE_VAR_LOCATION (node->loc);
17323 	      descr = dw_loc_list_1 (decl, varloc, want_address,
17324 				     initialized);
17325 	    }
17326 	  gcc_assert (descr);
17327 	  /* The variable has a location between NODE->LABEL and
17328 	     NODE->NEXT->LABEL.  */
17329 	  if (node->next)
17330 	    endname = node->next->label, endview = node->next->view;
17331 	  else
17332 	    endname = cfun->fde->dw_fde_second_end, endview = 0;
17333 	  *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17334 				 endname, endview, secname);
17335 	  listp = &(*listp)->dw_loc_next;
17336 	}
17337     }
17338 
17339   /* Try to avoid the overhead of a location list emitting a location
17340      expression instead, but only if we didn't have more than one
17341      location entry in the first place.  If some entries were not
17342      representable, we don't want to pretend a single entry that was
17343      applies to the entire scope in which the variable is
17344      available.  */
17345   if (list && loc_list->first->next)
17346     gen_llsym (list);
17347   else
17348     maybe_gen_llsym (list);
17349 
17350   return list;
17351 }
17352 
17353 /* Return if the loc_list has only single element and thus can be represented
17354    as location description.   */
17355 
17356 static bool
single_element_loc_list_p(dw_loc_list_ref list)17357 single_element_loc_list_p (dw_loc_list_ref list)
17358 {
17359   gcc_assert (!list->dw_loc_next || list->ll_symbol);
17360   return !list->ll_symbol;
17361 }
17362 
17363 /* Duplicate a single element of location list.  */
17364 
17365 static inline dw_loc_descr_ref
copy_loc_descr(dw_loc_descr_ref ref)17366 copy_loc_descr (dw_loc_descr_ref ref)
17367 {
17368   dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17369   memcpy (copy, ref, sizeof (dw_loc_descr_node));
17370   return copy;
17371 }
17372 
17373 /* To each location in list LIST append loc descr REF.  */
17374 
17375 static void
add_loc_descr_to_each(dw_loc_list_ref list,dw_loc_descr_ref ref)17376 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17377 {
17378   dw_loc_descr_ref copy;
17379   add_loc_descr (&list->expr, ref);
17380   list = list->dw_loc_next;
17381   while (list)
17382     {
17383       copy = copy_loc_descr (ref);
17384       add_loc_descr (&list->expr, copy);
17385       while (copy->dw_loc_next)
17386 	copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17387       list = list->dw_loc_next;
17388     }
17389 }
17390 
17391 /* To each location in list LIST prepend loc descr REF.  */
17392 
17393 static void
prepend_loc_descr_to_each(dw_loc_list_ref list,dw_loc_descr_ref ref)17394 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17395 {
17396   dw_loc_descr_ref copy;
17397   dw_loc_descr_ref ref_end = list->expr;
17398   add_loc_descr (&ref, list->expr);
17399   list->expr = ref;
17400   list = list->dw_loc_next;
17401   while (list)
17402     {
17403       dw_loc_descr_ref end = list->expr;
17404       list->expr = copy = copy_loc_descr (ref);
17405       while (copy->dw_loc_next != ref_end)
17406 	copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17407       copy->dw_loc_next = end;
17408       list = list->dw_loc_next;
17409     }
17410 }
17411 
17412 /* Given two lists RET and LIST
17413    produce location list that is result of adding expression in LIST
17414    to expression in RET on each position in program.
17415    Might be destructive on both RET and LIST.
17416 
17417    TODO: We handle only simple cases of RET or LIST having at most one
17418    element.  General case would involve sorting the lists in program order
17419    and merging them that will need some additional work.
17420    Adding that will improve quality of debug info especially for SRA-ed
17421    structures.  */
17422 
17423 static void
add_loc_list(dw_loc_list_ref * ret,dw_loc_list_ref list)17424 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17425 {
17426   if (!list)
17427     return;
17428   if (!*ret)
17429     {
17430       *ret = list;
17431       return;
17432     }
17433   if (!list->dw_loc_next)
17434     {
17435       add_loc_descr_to_each (*ret, list->expr);
17436       return;
17437     }
17438   if (!(*ret)->dw_loc_next)
17439     {
17440       prepend_loc_descr_to_each (list, (*ret)->expr);
17441       *ret = list;
17442       return;
17443     }
17444   expansion_failed (NULL_TREE, NULL_RTX,
17445 		    "Don't know how to merge two non-trivial"
17446 		    " location lists.\n");
17447   *ret = NULL;
17448   return;
17449 }
17450 
17451 /* LOC is constant expression.  Try a luck, look it up in constant
17452    pool and return its loc_descr of its address.  */
17453 
17454 static dw_loc_descr_ref
cst_pool_loc_descr(tree loc)17455 cst_pool_loc_descr (tree loc)
17456 {
17457   /* Get an RTL for this, if something has been emitted.  */
17458   rtx rtl = lookup_constant_def (loc);
17459 
17460   if (!rtl || !MEM_P (rtl))
17461     {
17462       gcc_assert (!rtl);
17463       return 0;
17464     }
17465   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17466 
17467   /* TODO: We might get more coverage if we was actually delaying expansion
17468      of all expressions till end of compilation when constant pools are fully
17469      populated.  */
17470   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17471     {
17472       expansion_failed (loc, NULL_RTX,
17473 			"CST value in contant pool but not marked.");
17474       return 0;
17475     }
17476   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17477 			     GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17478 }
17479 
17480 /* Return dw_loc_list representing address of addr_expr LOC
17481    by looking for inner INDIRECT_REF expression and turning
17482    it into simple arithmetics.
17483 
17484    See loc_list_from_tree for the meaning of CONTEXT.  */
17485 
17486 static dw_loc_list_ref
loc_list_for_address_of_addr_expr_of_indirect_ref(tree loc,bool toplev,loc_descr_context * context)17487 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17488 						   loc_descr_context *context)
17489 {
17490   tree obj, offset;
17491   poly_int64 bitsize, bitpos, bytepos;
17492   machine_mode mode;
17493   int unsignedp, reversep, volatilep = 0;
17494   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17495 
17496   obj = get_inner_reference (TREE_OPERAND (loc, 0),
17497 			     &bitsize, &bitpos, &offset, &mode,
17498 			     &unsignedp, &reversep, &volatilep);
17499   STRIP_NOPS (obj);
17500   if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17501     {
17502       expansion_failed (loc, NULL_RTX, "bitfield access");
17503       return 0;
17504     }
17505   if (!INDIRECT_REF_P (obj))
17506     {
17507       expansion_failed (obj,
17508 			NULL_RTX, "no indirect ref in inner refrence");
17509       return 0;
17510     }
17511   if (!offset && known_eq (bitpos, 0))
17512     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17513 				   context);
17514   else if (toplev
17515 	   && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17516 	   && (dwarf_version >= 4 || !dwarf_strict))
17517     {
17518       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17519       if (!list_ret)
17520 	return 0;
17521       if (offset)
17522 	{
17523 	  /* Variable offset.  */
17524 	  list_ret1 = loc_list_from_tree (offset, 0, context);
17525 	  if (list_ret1 == 0)
17526 	    return 0;
17527 	  add_loc_list (&list_ret, list_ret1);
17528 	  if (!list_ret)
17529 	    return 0;
17530 	  add_loc_descr_to_each (list_ret,
17531 				 new_loc_descr (DW_OP_plus, 0, 0));
17532 	}
17533       HOST_WIDE_INT value;
17534       if (bytepos.is_constant (&value) && value > 0)
17535 	add_loc_descr_to_each (list_ret,
17536 			       new_loc_descr (DW_OP_plus_uconst, value, 0));
17537       else if (maybe_ne (bytepos, 0))
17538 	loc_list_plus_const (list_ret, bytepos);
17539       add_loc_descr_to_each (list_ret,
17540 			     new_loc_descr (DW_OP_stack_value, 0, 0));
17541     }
17542   return list_ret;
17543 }
17544 
17545 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17546    all operations from LOC are nops, move to the last one.  Insert in NOPS all
17547    operations that are skipped.  */
17548 
17549 static void
loc_descr_to_next_no_nop(dw_loc_descr_ref & loc,hash_set<dw_loc_descr_ref> & nops)17550 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17551 			  hash_set<dw_loc_descr_ref> &nops)
17552 {
17553   while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17554     {
17555       nops.add (loc);
17556       loc = loc->dw_loc_next;
17557     }
17558 }
17559 
17560 /* Helper for loc_descr_without_nops: free the location description operation
17561    P.  */
17562 
17563 bool
free_loc_descr(const dw_loc_descr_ref & loc,void * data ATTRIBUTE_UNUSED)17564 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17565 {
17566   ggc_free (loc);
17567   return true;
17568 }
17569 
17570 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17571    finishes LOC.  */
17572 
17573 static void
loc_descr_without_nops(dw_loc_descr_ref & loc)17574 loc_descr_without_nops (dw_loc_descr_ref &loc)
17575 {
17576   if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17577     return;
17578 
17579   /* Set of all DW_OP_nop operations we remove.  */
17580   hash_set<dw_loc_descr_ref> nops;
17581 
17582   /* First, strip all prefix NOP operations in order to keep the head of the
17583      operations list.  */
17584   loc_descr_to_next_no_nop (loc, nops);
17585 
17586   for (dw_loc_descr_ref cur = loc; cur != NULL;)
17587     {
17588       /* For control flow operations: strip "prefix" nops in destination
17589 	 labels.  */
17590       if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17591 	loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17592       if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17593 	loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17594 
17595       /* Do the same for the operations that follow, then move to the next
17596 	 iteration.  */
17597       if (cur->dw_loc_next != NULL)
17598 	loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17599       cur = cur->dw_loc_next;
17600     }
17601 
17602   nops.traverse<void *, free_loc_descr> (NULL);
17603 }
17604 
17605 
17606 struct dwarf_procedure_info;
17607 
17608 /* Helper structure for location descriptions generation.  */
17609 struct loc_descr_context
17610 {
17611   /* The type that is implicitly referenced by DW_OP_push_object_address, or
17612      NULL_TREE if DW_OP_push_object_address in invalid for this location
17613      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
17614   tree context_type;
17615   /* The ..._DECL node that should be translated as a
17616      DW_OP_push_object_address operation.  */
17617   tree base_decl;
17618   /* Information about the DWARF procedure we are currently generating. NULL if
17619      we are not generating a DWARF procedure.  */
17620   struct dwarf_procedure_info *dpi;
17621   /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17622      by consumer.  Used for DW_TAG_generic_subrange attributes.  */
17623   bool placeholder_arg;
17624   /* True if PLACEHOLDER_EXPR has been seen.  */
17625   bool placeholder_seen;
17626 };
17627 
17628 /* DWARF procedures generation
17629 
17630    DWARF expressions (aka. location descriptions) are used to encode variable
17631    things such as sizes or offsets.  Such computations can have redundant parts
17632    that can be factorized in order to reduce the size of the output debug
17633    information.  This is the whole point of DWARF procedures.
17634 
17635    Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17636    already factorized into functions ("size functions") in order to handle very
17637    big and complex types.  Such functions are quite simple: they have integral
17638    arguments, they return an integral result and their body contains only a
17639    return statement with arithmetic expressions.  This is the only kind of
17640    function we are interested in translating into DWARF procedures, here.
17641 
17642    DWARF expressions and DWARF procedure are executed using a stack, so we have
17643    to define some calling convention for them to interact.  Let's say that:
17644 
17645    - Before calling a DWARF procedure, DWARF expressions must push on the stack
17646      all arguments in reverse order (right-to-left) so that when the DWARF
17647      procedure execution starts, the first argument is the top of the stack.
17648 
17649    - Then, when returning, the DWARF procedure must have consumed all arguments
17650      on the stack, must have pushed the result and touched nothing else.
17651 
17652    - Each integral argument and the result are integral types can be hold in a
17653      single stack slot.
17654 
17655    - We call "frame offset" the number of stack slots that are "under DWARF
17656      procedure control": it includes the arguments slots, the temporaries and
17657      the result slot. Thus, it is equal to the number of arguments when the
17658      procedure execution starts and must be equal to one (the result) when it
17659      returns.  */
17660 
17661 /* Helper structure used when generating operations for a DWARF procedure.  */
17662 struct dwarf_procedure_info
17663 {
17664   /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17665      currently translated.  */
17666   tree fndecl;
17667   /* The number of arguments FNDECL takes.  */
17668   unsigned args_count;
17669 };
17670 
17671 /* Return a pointer to a newly created DIE node for a DWARF procedure.  Add
17672    LOCATION as its DW_AT_location attribute.  If FNDECL is not NULL_TREE,
17673    equate it to this DIE.  */
17674 
17675 static dw_die_ref
new_dwarf_proc_die(dw_loc_descr_ref location,tree fndecl,dw_die_ref parent_die)17676 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17677 		    dw_die_ref parent_die)
17678 {
17679   dw_die_ref dwarf_proc_die;
17680 
17681   if ((dwarf_version < 3 && dwarf_strict)
17682       || location == NULL)
17683     return NULL;
17684 
17685   dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17686   if (fndecl)
17687     equate_decl_number_to_die (fndecl, dwarf_proc_die);
17688   add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17689   return dwarf_proc_die;
17690 }
17691 
17692 /* Return whether TYPE is a supported type as a DWARF procedure argument
17693    type or return type (we handle only scalar types and pointer types that
17694    aren't wider than the DWARF expression evaluation stack.  */
17695 
17696 static bool
is_handled_procedure_type(tree type)17697 is_handled_procedure_type (tree type)
17698 {
17699   return ((INTEGRAL_TYPE_P (type)
17700 	   || TREE_CODE (type) == OFFSET_TYPE
17701 	   || TREE_CODE (type) == POINTER_TYPE)
17702 	  && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17703 }
17704 
17705 /* Helper for resolve_args_picking: do the same but stop when coming across
17706    visited nodes.  For each node we visit, register in FRAME_OFFSETS the frame
17707    offset *before* evaluating the corresponding operation.  */
17708 
17709 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)17710 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17711 			struct dwarf_procedure_info *dpi,
17712 			hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17713 {
17714   /* The "frame_offset" identifier is already used to name a macro... */
17715   unsigned frame_offset_ = initial_frame_offset;
17716   dw_loc_descr_ref l;
17717 
17718   for (l = loc; l != NULL;)
17719     {
17720       bool existed;
17721       unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17722 
17723       /* If we already met this node, there is nothing to compute anymore.  */
17724       if (existed)
17725 	{
17726 	  /* Make sure that the stack size is consistent wherever the execution
17727 	     flow comes from.  */
17728 	  gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17729 	  break;
17730 	}
17731       l_frame_offset = frame_offset_;
17732 
17733       /* If needed, relocate the picking offset with respect to the frame
17734 	 offset. */
17735       if (l->frame_offset_rel)
17736 	{
17737 	  unsigned HOST_WIDE_INT off;
17738 	  switch (l->dw_loc_opc)
17739 	    {
17740 	    case DW_OP_pick:
17741 	      off = l->dw_loc_oprnd1.v.val_unsigned;
17742 	      break;
17743 	    case DW_OP_dup:
17744 	      off = 0;
17745 	      break;
17746 	    case DW_OP_over:
17747 	      off = 1;
17748 	      break;
17749 	    default:
17750 	      gcc_unreachable ();
17751 	    }
17752 	  /* frame_offset_ is the size of the current stack frame, including
17753 	     incoming arguments. Besides, the arguments are pushed
17754 	     right-to-left.  Thus, in order to access the Nth argument from
17755 	     this operation node, the picking has to skip temporaries *plus*
17756 	     one stack slot per argument (0 for the first one, 1 for the second
17757 	     one, etc.).
17758 
17759 	     The targetted argument number (N) is already set as the operand,
17760 	     and the number of temporaries can be computed with:
17761 	       frame_offsets_ - dpi->args_count */
17762 	  off += frame_offset_ - dpi->args_count;
17763 
17764 	  /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)...  */
17765 	  if (off > 255)
17766 	    return false;
17767 
17768 	  if (off == 0)
17769 	    {
17770 	      l->dw_loc_opc = DW_OP_dup;
17771 	      l->dw_loc_oprnd1.v.val_unsigned = 0;
17772 	    }
17773 	  else if (off == 1)
17774 	    {
17775 	      l->dw_loc_opc = DW_OP_over;
17776 	      l->dw_loc_oprnd1.v.val_unsigned = 0;
17777 	    }
17778 	  else
17779 	    {
17780 	      l->dw_loc_opc = DW_OP_pick;
17781 	      l->dw_loc_oprnd1.v.val_unsigned = off;
17782 	    }
17783 	}
17784 
17785       /* Update frame_offset according to the effect the current operation has
17786 	 on the stack.  */
17787       switch (l->dw_loc_opc)
17788 	{
17789 	case DW_OP_deref:
17790 	case DW_OP_swap:
17791 	case DW_OP_rot:
17792 	case DW_OP_abs:
17793 	case DW_OP_neg:
17794 	case DW_OP_not:
17795 	case DW_OP_plus_uconst:
17796 	case DW_OP_skip:
17797 	case DW_OP_reg0:
17798 	case DW_OP_reg1:
17799 	case DW_OP_reg2:
17800 	case DW_OP_reg3:
17801 	case DW_OP_reg4:
17802 	case DW_OP_reg5:
17803 	case DW_OP_reg6:
17804 	case DW_OP_reg7:
17805 	case DW_OP_reg8:
17806 	case DW_OP_reg9:
17807 	case DW_OP_reg10:
17808 	case DW_OP_reg11:
17809 	case DW_OP_reg12:
17810 	case DW_OP_reg13:
17811 	case DW_OP_reg14:
17812 	case DW_OP_reg15:
17813 	case DW_OP_reg16:
17814 	case DW_OP_reg17:
17815 	case DW_OP_reg18:
17816 	case DW_OP_reg19:
17817 	case DW_OP_reg20:
17818 	case DW_OP_reg21:
17819 	case DW_OP_reg22:
17820 	case DW_OP_reg23:
17821 	case DW_OP_reg24:
17822 	case DW_OP_reg25:
17823 	case DW_OP_reg26:
17824 	case DW_OP_reg27:
17825 	case DW_OP_reg28:
17826 	case DW_OP_reg29:
17827 	case DW_OP_reg30:
17828 	case DW_OP_reg31:
17829 	case DW_OP_bregx:
17830 	case DW_OP_piece:
17831 	case DW_OP_deref_size:
17832 	case DW_OP_nop:
17833 	case DW_OP_bit_piece:
17834 	case DW_OP_implicit_value:
17835 	case DW_OP_stack_value:
17836 	  break;
17837 
17838 	case DW_OP_addr:
17839 	case DW_OP_const1u:
17840 	case DW_OP_const1s:
17841 	case DW_OP_const2u:
17842 	case DW_OP_const2s:
17843 	case DW_OP_const4u:
17844 	case DW_OP_const4s:
17845 	case DW_OP_const8u:
17846 	case DW_OP_const8s:
17847 	case DW_OP_constu:
17848 	case DW_OP_consts:
17849 	case DW_OP_dup:
17850 	case DW_OP_over:
17851 	case DW_OP_pick:
17852 	case DW_OP_lit0:
17853 	case DW_OP_lit1:
17854 	case DW_OP_lit2:
17855 	case DW_OP_lit3:
17856 	case DW_OP_lit4:
17857 	case DW_OP_lit5:
17858 	case DW_OP_lit6:
17859 	case DW_OP_lit7:
17860 	case DW_OP_lit8:
17861 	case DW_OP_lit9:
17862 	case DW_OP_lit10:
17863 	case DW_OP_lit11:
17864 	case DW_OP_lit12:
17865 	case DW_OP_lit13:
17866 	case DW_OP_lit14:
17867 	case DW_OP_lit15:
17868 	case DW_OP_lit16:
17869 	case DW_OP_lit17:
17870 	case DW_OP_lit18:
17871 	case DW_OP_lit19:
17872 	case DW_OP_lit20:
17873 	case DW_OP_lit21:
17874 	case DW_OP_lit22:
17875 	case DW_OP_lit23:
17876 	case DW_OP_lit24:
17877 	case DW_OP_lit25:
17878 	case DW_OP_lit26:
17879 	case DW_OP_lit27:
17880 	case DW_OP_lit28:
17881 	case DW_OP_lit29:
17882 	case DW_OP_lit30:
17883 	case DW_OP_lit31:
17884 	case DW_OP_breg0:
17885 	case DW_OP_breg1:
17886 	case DW_OP_breg2:
17887 	case DW_OP_breg3:
17888 	case DW_OP_breg4:
17889 	case DW_OP_breg5:
17890 	case DW_OP_breg6:
17891 	case DW_OP_breg7:
17892 	case DW_OP_breg8:
17893 	case DW_OP_breg9:
17894 	case DW_OP_breg10:
17895 	case DW_OP_breg11:
17896 	case DW_OP_breg12:
17897 	case DW_OP_breg13:
17898 	case DW_OP_breg14:
17899 	case DW_OP_breg15:
17900 	case DW_OP_breg16:
17901 	case DW_OP_breg17:
17902 	case DW_OP_breg18:
17903 	case DW_OP_breg19:
17904 	case DW_OP_breg20:
17905 	case DW_OP_breg21:
17906 	case DW_OP_breg22:
17907 	case DW_OP_breg23:
17908 	case DW_OP_breg24:
17909 	case DW_OP_breg25:
17910 	case DW_OP_breg26:
17911 	case DW_OP_breg27:
17912 	case DW_OP_breg28:
17913 	case DW_OP_breg29:
17914 	case DW_OP_breg30:
17915 	case DW_OP_breg31:
17916 	case DW_OP_fbreg:
17917 	case DW_OP_push_object_address:
17918 	case DW_OP_call_frame_cfa:
17919 	case DW_OP_GNU_variable_value:
17920 	case DW_OP_GNU_addr_index:
17921 	case DW_OP_GNU_const_index:
17922 	  ++frame_offset_;
17923 	  break;
17924 
17925 	case DW_OP_drop:
17926 	case DW_OP_xderef:
17927 	case DW_OP_and:
17928 	case DW_OP_div:
17929 	case DW_OP_minus:
17930 	case DW_OP_mod:
17931 	case DW_OP_mul:
17932 	case DW_OP_or:
17933 	case DW_OP_plus:
17934 	case DW_OP_shl:
17935 	case DW_OP_shr:
17936 	case DW_OP_shra:
17937 	case DW_OP_xor:
17938 	case DW_OP_bra:
17939 	case DW_OP_eq:
17940 	case DW_OP_ge:
17941 	case DW_OP_gt:
17942 	case DW_OP_le:
17943 	case DW_OP_lt:
17944 	case DW_OP_ne:
17945 	case DW_OP_regx:
17946 	case DW_OP_xderef_size:
17947 	  --frame_offset_;
17948 	  break;
17949 
17950 	case DW_OP_call2:
17951 	case DW_OP_call4:
17952 	case DW_OP_call_ref:
17953 	  {
17954 	    dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17955 	    int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17956 
17957 	    if (stack_usage == NULL)
17958 	      return false;
17959 	    frame_offset_ += *stack_usage;
17960 	    break;
17961 	  }
17962 
17963 	case DW_OP_implicit_pointer:
17964 	case DW_OP_entry_value:
17965 	case DW_OP_const_type:
17966 	case DW_OP_regval_type:
17967 	case DW_OP_deref_type:
17968 	case DW_OP_convert:
17969 	case DW_OP_reinterpret:
17970 	case DW_OP_form_tls_address:
17971 	case DW_OP_GNU_push_tls_address:
17972 	case DW_OP_GNU_uninit:
17973 	case DW_OP_GNU_encoded_addr:
17974 	case DW_OP_GNU_implicit_pointer:
17975 	case DW_OP_GNU_entry_value:
17976 	case DW_OP_GNU_const_type:
17977 	case DW_OP_GNU_regval_type:
17978 	case DW_OP_GNU_deref_type:
17979 	case DW_OP_GNU_convert:
17980 	case DW_OP_GNU_reinterpret:
17981 	case DW_OP_GNU_parameter_ref:
17982 	  /* loc_list_from_tree will probably not output these operations for
17983 	     size functions, so assume they will not appear here.  */
17984 	  /* Fall through...  */
17985 
17986 	default:
17987 	  gcc_unreachable ();
17988 	}
17989 
17990       /* Now, follow the control flow (except subroutine calls).  */
17991       switch (l->dw_loc_opc)
17992 	{
17993 	case DW_OP_bra:
17994 	  if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17995 				       frame_offsets))
17996 	    return false;
17997 	  /* Fall through. */
17998 
17999 	case DW_OP_skip:
18000 	  l = l->dw_loc_oprnd1.v.val_loc;
18001 	  break;
18002 
18003 	case DW_OP_stack_value:
18004 	  return true;
18005 
18006 	default:
18007 	  l = l->dw_loc_next;
18008 	  break;
18009 	}
18010     }
18011 
18012   return true;
18013 }
18014 
18015 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18016    operations) in order to resolve the operand of DW_OP_pick operations that
18017    target DWARF procedure arguments (DPI).  INITIAL_FRAME_OFFSET is the frame
18018    offset *before* LOC is executed.  Return if all relocations were
18019    successful.  */
18020 
18021 static bool
resolve_args_picking(dw_loc_descr_ref loc,unsigned initial_frame_offset,struct dwarf_procedure_info * dpi)18022 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18023 		      struct dwarf_procedure_info *dpi)
18024 {
18025   /* Associate to all visited operations the frame offset *before* evaluating
18026      this operation.  */
18027   hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18028 
18029   return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
18030 				 frame_offsets);
18031 }
18032 
18033 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18034    Return NULL if it is not possible.  */
18035 
18036 static dw_die_ref
function_to_dwarf_procedure(tree fndecl)18037 function_to_dwarf_procedure (tree fndecl)
18038 {
18039   struct loc_descr_context ctx;
18040   struct dwarf_procedure_info dpi;
18041   dw_die_ref dwarf_proc_die;
18042   tree tree_body = DECL_SAVED_TREE (fndecl);
18043   dw_loc_descr_ref loc_body, epilogue;
18044 
18045   tree cursor;
18046   unsigned i;
18047 
18048   /* Do not generate multiple DWARF procedures for the same function
18049      declaration.  */
18050   dwarf_proc_die = lookup_decl_die (fndecl);
18051   if (dwarf_proc_die != NULL)
18052     return dwarf_proc_die;
18053 
18054   /* DWARF procedures are available starting with the DWARFv3 standard.  */
18055   if (dwarf_version < 3 && dwarf_strict)
18056     return NULL;
18057 
18058   /* We handle only functions for which we still have a body, that return a
18059      supported type and that takes arguments with supported types.  Note that
18060      there is no point translating functions that return nothing.  */
18061   if (tree_body == NULL_TREE
18062       || DECL_RESULT (fndecl) == NULL_TREE
18063       || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18064     return NULL;
18065 
18066   for (cursor = DECL_ARGUMENTS (fndecl);
18067        cursor != NULL_TREE;
18068        cursor = TREE_CHAIN (cursor))
18069     if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18070       return NULL;
18071 
18072   /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)).  */
18073   if (TREE_CODE (tree_body) != RETURN_EXPR)
18074     return NULL;
18075   tree_body = TREE_OPERAND (tree_body, 0);
18076   if (TREE_CODE (tree_body) != MODIFY_EXPR
18077       || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18078     return NULL;
18079   tree_body = TREE_OPERAND (tree_body, 1);
18080 
18081   /* Try to translate the body expression itself.  Note that this will probably
18082      cause an infinite recursion if its call graph has a cycle.  This is very
18083      unlikely for size functions, however, so don't bother with such things at
18084      the moment.  */
18085   ctx.context_type = NULL_TREE;
18086   ctx.base_decl = NULL_TREE;
18087   ctx.dpi = &dpi;
18088   ctx.placeholder_arg = false;
18089   ctx.placeholder_seen = false;
18090   dpi.fndecl = fndecl;
18091   dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18092   loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18093   if (!loc_body)
18094     return NULL;
18095 
18096   /* After evaluating all operands in "loc_body", we should still have on the
18097      stack all arguments plus the desired function result (top of the stack).
18098      Generate code in order to keep only the result in our stack frame.  */
18099   epilogue = NULL;
18100   for (i = 0; i < dpi.args_count; ++i)
18101     {
18102       dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18103       op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18104       op_couple->dw_loc_next->dw_loc_next = epilogue;
18105       epilogue = op_couple;
18106     }
18107   add_loc_descr (&loc_body, epilogue);
18108   if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18109     return NULL;
18110 
18111   /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18112      because they are considered useful.  Now there is an epilogue, they are
18113      not anymore, so give it another try.   */
18114   loc_descr_without_nops (loc_body);
18115 
18116   /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18117      a DW_TAG_dwarf_procedure, so we may have a conflict, here.  It's unlikely,
18118      though, given that size functions do not come from source, so they should
18119      not have a dedicated DW_TAG_subprogram DIE.  */
18120   dwarf_proc_die
18121     = new_dwarf_proc_die (loc_body, fndecl,
18122 			  get_context_die (DECL_CONTEXT (fndecl)));
18123 
18124   /* The called DWARF procedure consumes one stack slot per argument and
18125      returns one stack slot.  */
18126   dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18127 
18128   return dwarf_proc_die;
18129 }
18130 
18131 
18132 /* Generate Dwarf location list representing LOC.
18133    If WANT_ADDRESS is false, expression computing LOC will be computed
18134    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18135    if WANT_ADDRESS is 2, expression computing address useable in location
18136      will be returned (i.e. DW_OP_reg can be used
18137      to refer to register values).
18138 
18139    CONTEXT provides information to customize the location descriptions
18140    generation.  Its context_type field specifies what type is implicitly
18141    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
18142    will not be generated.
18143 
18144    Its DPI field determines whether we are generating a DWARF expression for a
18145    DWARF procedure, so PARM_DECL references are processed specifically.
18146 
18147    If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18148    and dpi fields were null.  */
18149 
18150 static dw_loc_list_ref
loc_list_from_tree_1(tree loc,int want_address,struct loc_descr_context * context)18151 loc_list_from_tree_1 (tree loc, int want_address,
18152 		      struct loc_descr_context *context)
18153 {
18154   dw_loc_descr_ref ret = NULL, ret1 = NULL;
18155   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18156   int have_address = 0;
18157   enum dwarf_location_atom op;
18158 
18159   /* ??? Most of the time we do not take proper care for sign/zero
18160      extending the values properly.  Hopefully this won't be a real
18161      problem...  */
18162 
18163   if (context != NULL
18164       && context->base_decl == loc
18165       && want_address == 0)
18166     {
18167       if (dwarf_version >= 3 || !dwarf_strict)
18168 	return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18169 			     NULL, 0, NULL, 0, NULL);
18170       else
18171 	return NULL;
18172     }
18173 
18174   switch (TREE_CODE (loc))
18175     {
18176     case ERROR_MARK:
18177       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18178       return 0;
18179 
18180     case PLACEHOLDER_EXPR:
18181       /* This case involves extracting fields from an object to determine the
18182 	 position of other fields. It is supposed to appear only as the first
18183          operand of COMPONENT_REF nodes and to reference precisely the type
18184          that the context allows.  */
18185       if (context != NULL
18186           && TREE_TYPE (loc) == context->context_type
18187 	  && want_address >= 1)
18188 	{
18189 	  if (dwarf_version >= 3 || !dwarf_strict)
18190 	    {
18191 	      ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18192 	      have_address = 1;
18193 	      break;
18194 	    }
18195 	  else
18196 	    return NULL;
18197 	}
18198       /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18199 	 the single argument passed by consumer.  */
18200       else if (context != NULL
18201 	       && context->placeholder_arg
18202 	       && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18203 	       && want_address == 0)
18204 	{
18205 	  ret = new_loc_descr (DW_OP_pick, 0, 0);
18206 	  ret->frame_offset_rel = 1;
18207 	  context->placeholder_seen = true;
18208 	  break;
18209 	}
18210       else
18211 	expansion_failed (loc, NULL_RTX,
18212 			  "PLACEHOLDER_EXPR for an unexpected type");
18213       break;
18214 
18215     case CALL_EXPR:
18216 	{
18217 	  const int nargs = call_expr_nargs (loc);
18218 	  tree callee = get_callee_fndecl (loc);
18219 	  int i;
18220 	  dw_die_ref dwarf_proc;
18221 
18222 	  if (callee == NULL_TREE)
18223 	    goto call_expansion_failed;
18224 
18225 	  /* We handle only functions that return an integer.  */
18226 	  if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18227 	    goto call_expansion_failed;
18228 
18229 	  dwarf_proc = function_to_dwarf_procedure (callee);
18230 	  if (dwarf_proc == NULL)
18231 	    goto call_expansion_failed;
18232 
18233 	  /* Evaluate arguments right-to-left so that the first argument will
18234 	     be the top-most one on the stack.  */
18235 	  for (i = nargs - 1; i >= 0; --i)
18236 	    {
18237 	      dw_loc_descr_ref loc_descr
18238 	        = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18239 					    context);
18240 
18241 	      if (loc_descr == NULL)
18242 		goto call_expansion_failed;
18243 
18244 	      add_loc_descr (&ret, loc_descr);
18245 	    }
18246 
18247 	  ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18248 	  ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18249 	  ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18250 	  ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18251 	  add_loc_descr (&ret, ret1);
18252 	  break;
18253 
18254 	call_expansion_failed:
18255 	  expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18256 	  /* There are no opcodes for these operations.  */
18257 	  return 0;
18258 	}
18259 
18260     case PREINCREMENT_EXPR:
18261     case PREDECREMENT_EXPR:
18262     case POSTINCREMENT_EXPR:
18263     case POSTDECREMENT_EXPR:
18264       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18265       /* There are no opcodes for these operations.  */
18266       return 0;
18267 
18268     case ADDR_EXPR:
18269       /* If we already want an address, see if there is INDIRECT_REF inside
18270          e.g. for &this->field.  */
18271       if (want_address)
18272 	{
18273 	  list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18274 		       (loc, want_address == 2, context);
18275 	  if (list_ret)
18276 	    have_address = 1;
18277 	  else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18278 	  	   && (ret = cst_pool_loc_descr (loc)))
18279 	    have_address = 1;
18280 	}
18281         /* Otherwise, process the argument and look for the address.  */
18282       if (!list_ret && !ret)
18283         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18284       else
18285 	{
18286 	  if (want_address)
18287 	    expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18288 	  return NULL;
18289 	}
18290       break;
18291 
18292     case VAR_DECL:
18293       if (DECL_THREAD_LOCAL_P (loc))
18294 	{
18295 	  rtx rtl;
18296          enum dwarf_location_atom tls_op;
18297          enum dtprel_bool dtprel = dtprel_false;
18298 
18299 	  if (targetm.have_tls)
18300 	    {
18301 	      /* If this is not defined, we have no way to emit the
18302 		 data.  */
18303 	      if (!targetm.asm_out.output_dwarf_dtprel)
18304 		return 0;
18305 
18306 	       /* The way DW_OP_GNU_push_tls_address is specified, we
18307 	     	  can only look up addresses of objects in the current
18308 	     	  module.  We used DW_OP_addr as first op, but that's
18309 		  wrong, because DW_OP_addr is relocated by the debug
18310 		  info consumer, while DW_OP_GNU_push_tls_address
18311 		  operand shouldn't be.  */
18312 	      if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18313 		return 0;
18314 	      dtprel = dtprel_true;
18315 	      /* We check for DWARF 5 here because gdb did not implement
18316 		 DW_OP_form_tls_address until after 7.12.  */
18317 	      tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18318 			: DW_OP_GNU_push_tls_address);
18319 	    }
18320 	  else
18321 	    {
18322 	      if (!targetm.emutls.debug_form_tls_address
18323 		  || !(dwarf_version >= 3 || !dwarf_strict))
18324 		return 0;
18325 	      /* We stuffed the control variable into the DECL_VALUE_EXPR
18326 		 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18327 		 no longer appear in gimple code.  We used the control
18328 		 variable in specific so that we could pick it up here.  */
18329 	      loc = DECL_VALUE_EXPR (loc);
18330               tls_op = DW_OP_form_tls_address;
18331 	    }
18332 
18333 	  rtl = rtl_for_decl_location (loc);
18334 	  if (rtl == NULL_RTX)
18335 	    return 0;
18336 
18337 	  if (!MEM_P (rtl))
18338 	    return 0;
18339 	  rtl = XEXP (rtl, 0);
18340 	  if (! CONSTANT_P (rtl))
18341 	    return 0;
18342 
18343           ret = new_addr_loc_descr (rtl, dtprel);
18344           ret1 = new_loc_descr (tls_op, 0, 0);
18345 	  add_loc_descr (&ret, ret1);
18346 
18347 	  have_address = 1;
18348 	  break;
18349 	}
18350       /* FALLTHRU */
18351 
18352     case PARM_DECL:
18353       if (context != NULL && context->dpi != NULL
18354 	  && DECL_CONTEXT (loc) == context->dpi->fndecl)
18355 	{
18356 	  /* We are generating code for a DWARF procedure and we want to access
18357 	     one of its arguments: find the appropriate argument offset and let
18358 	     the resolve_args_picking pass compute the offset that complies
18359 	     with the stack frame size.  */
18360 	  unsigned i = 0;
18361 	  tree cursor;
18362 
18363 	  for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18364 	       cursor != NULL_TREE && cursor != loc;
18365 	       cursor = TREE_CHAIN (cursor), ++i)
18366 	    ;
18367 	  /* If we are translating a DWARF procedure, all referenced parameters
18368 	     must belong to the current function.  */
18369 	  gcc_assert (cursor != NULL_TREE);
18370 
18371 	  ret = new_loc_descr (DW_OP_pick, i, 0);
18372 	  ret->frame_offset_rel = 1;
18373 	  break;
18374 	}
18375       /* FALLTHRU */
18376 
18377     case RESULT_DECL:
18378       if (DECL_HAS_VALUE_EXPR_P (loc))
18379 	return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18380 				     want_address, context);
18381       /* FALLTHRU */
18382 
18383     case FUNCTION_DECL:
18384       {
18385 	rtx rtl;
18386 	var_loc_list *loc_list = lookup_decl_loc (loc);
18387 
18388 	if (loc_list && loc_list->first)
18389 	  {
18390 	    list_ret = dw_loc_list (loc_list, loc, want_address);
18391 	    have_address = want_address != 0;
18392 	    break;
18393 	  }
18394 	rtl = rtl_for_decl_location (loc);
18395 	if (rtl == NULL_RTX)
18396 	  {
18397 	    if (TREE_CODE (loc) != FUNCTION_DECL
18398 		&& early_dwarf
18399 		&& current_function_decl
18400 		&& want_address != 1
18401 		&& ! DECL_IGNORED_P (loc)
18402 		&& (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18403 		    || POINTER_TYPE_P (TREE_TYPE (loc)))
18404 		&& DECL_CONTEXT (loc) == current_function_decl
18405 		&& (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18406 		    <= DWARF2_ADDR_SIZE))
18407 	      {
18408 		dw_die_ref ref = lookup_decl_die (loc);
18409 		ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18410 		if (ref)
18411 		  {
18412 		    ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18413 		    ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18414 		    ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18415 		  }
18416 		else
18417 		  {
18418 		    ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18419 		    ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18420 		  }
18421 		break;
18422 	      }
18423 	    expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18424 	    return 0;
18425 	  }
18426 	else if (CONST_INT_P (rtl))
18427 	  {
18428 	    HOST_WIDE_INT val = INTVAL (rtl);
18429 	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18430 	      val &= GET_MODE_MASK (DECL_MODE (loc));
18431 	    ret = int_loc_descriptor (val);
18432 	  }
18433 	else if (GET_CODE (rtl) == CONST_STRING)
18434 	  {
18435 	    expansion_failed (loc, NULL_RTX, "CONST_STRING");
18436 	    return 0;
18437 	  }
18438 	else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18439           ret = new_addr_loc_descr (rtl, dtprel_false);
18440 	else
18441 	  {
18442 	    machine_mode mode, mem_mode;
18443 
18444 	    /* Certain constructs can only be represented at top-level.  */
18445 	    if (want_address == 2)
18446 	      {
18447 		ret = loc_descriptor (rtl, VOIDmode,
18448 				      VAR_INIT_STATUS_INITIALIZED);
18449 		have_address = 1;
18450 	      }
18451 	    else
18452 	      {
18453 		mode = GET_MODE (rtl);
18454 		mem_mode = VOIDmode;
18455 		if (MEM_P (rtl))
18456 		  {
18457 		    mem_mode = mode;
18458 		    mode = get_address_mode (rtl);
18459 		    rtl = XEXP (rtl, 0);
18460 		    have_address = 1;
18461 		  }
18462 		ret = mem_loc_descriptor (rtl, mode, mem_mode,
18463 					  VAR_INIT_STATUS_INITIALIZED);
18464 	      }
18465 	    if (!ret)
18466 	      expansion_failed (loc, rtl,
18467 				"failed to produce loc descriptor for rtl");
18468 	  }
18469       }
18470       break;
18471 
18472     case MEM_REF:
18473       if (!integer_zerop (TREE_OPERAND (loc, 1)))
18474 	{
18475 	  have_address = 1;
18476 	  goto do_plus;
18477 	}
18478       /* Fallthru.  */
18479     case INDIRECT_REF:
18480       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18481       have_address = 1;
18482       break;
18483 
18484     case TARGET_MEM_REF:
18485     case SSA_NAME:
18486     case DEBUG_EXPR_DECL:
18487       return NULL;
18488 
18489     case COMPOUND_EXPR:
18490       return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18491 				   context);
18492 
18493     CASE_CONVERT:
18494     case VIEW_CONVERT_EXPR:
18495     case SAVE_EXPR:
18496     case MODIFY_EXPR:
18497     case NON_LVALUE_EXPR:
18498       return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18499 				   context);
18500 
18501     case COMPONENT_REF:
18502     case BIT_FIELD_REF:
18503     case ARRAY_REF:
18504     case ARRAY_RANGE_REF:
18505     case REALPART_EXPR:
18506     case IMAGPART_EXPR:
18507       {
18508 	tree obj, offset;
18509 	poly_int64 bitsize, bitpos, bytepos;
18510 	machine_mode mode;
18511 	int unsignedp, reversep, volatilep = 0;
18512 
18513 	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18514 				   &unsignedp, &reversep, &volatilep);
18515 
18516 	gcc_assert (obj != loc);
18517 
18518 	list_ret = loc_list_from_tree_1 (obj,
18519 					 want_address == 2
18520 					 && known_eq (bitpos, 0)
18521 					 && !offset ? 2 : 1,
18522 					 context);
18523 	/* TODO: We can extract value of the small expression via shifting even
18524 	   for nonzero bitpos.  */
18525 	if (list_ret == 0)
18526 	  return 0;
18527 	if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18528 	    || !multiple_p (bitsize, BITS_PER_UNIT))
18529 	  {
18530 	    expansion_failed (loc, NULL_RTX,
18531 			      "bitfield access");
18532 	    return 0;
18533 	  }
18534 
18535 	if (offset != NULL_TREE)
18536 	  {
18537 	    /* Variable offset.  */
18538 	    list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18539 	    if (list_ret1 == 0)
18540 	      return 0;
18541 	    add_loc_list (&list_ret, list_ret1);
18542 	    if (!list_ret)
18543 	      return 0;
18544 	    add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18545 	  }
18546 
18547 	HOST_WIDE_INT value;
18548 	if (bytepos.is_constant (&value) && value > 0)
18549 	  add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18550 							  value, 0));
18551 	else if (maybe_ne (bytepos, 0))
18552 	  loc_list_plus_const (list_ret, bytepos);
18553 
18554 	have_address = 1;
18555 	break;
18556       }
18557 
18558     case INTEGER_CST:
18559       if ((want_address || !tree_fits_shwi_p (loc))
18560 	  && (ret = cst_pool_loc_descr (loc)))
18561 	have_address = 1;
18562       else if (want_address == 2
18563 	       && tree_fits_shwi_p (loc)
18564 	       && (ret = address_of_int_loc_descriptor
18565 	       		   (int_size_in_bytes (TREE_TYPE (loc)),
18566 	       		    tree_to_shwi (loc))))
18567 	have_address = 1;
18568       else if (tree_fits_shwi_p (loc))
18569 	ret = int_loc_descriptor (tree_to_shwi (loc));
18570       else if (tree_fits_uhwi_p (loc))
18571 	ret = uint_loc_descriptor (tree_to_uhwi (loc));
18572       else
18573 	{
18574 	  expansion_failed (loc, NULL_RTX,
18575 			    "Integer operand is not host integer");
18576 	  return 0;
18577 	}
18578       break;
18579 
18580     case POLY_INT_CST:
18581       {
18582 	if (want_address)
18583 	  {
18584 	    expansion_failed (loc, NULL_RTX,
18585 			      "constant address with a runtime component");
18586 	    return 0;
18587 	  }
18588 	poly_int64 value;
18589 	if (!poly_int_tree_p (loc, &value))
18590 	  {
18591 	    expansion_failed (loc, NULL_RTX, "constant too big");
18592 	    return 0;
18593 	  }
18594 	ret = int_loc_descriptor (value);
18595       }
18596       break;
18597 
18598     case CONSTRUCTOR:
18599     case REAL_CST:
18600     case STRING_CST:
18601     case COMPLEX_CST:
18602       if ((ret = cst_pool_loc_descr (loc)))
18603 	have_address = 1;
18604       else if (TREE_CODE (loc) == CONSTRUCTOR)
18605 	{
18606 	  tree type = TREE_TYPE (loc);
18607 	  unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18608 	  unsigned HOST_WIDE_INT offset = 0;
18609 	  unsigned HOST_WIDE_INT cnt;
18610 	  constructor_elt *ce;
18611 
18612 	  if (TREE_CODE (type) == RECORD_TYPE)
18613 	    {
18614 	      /* This is very limited, but it's enough to output
18615 		 pointers to member functions, as long as the
18616 		 referenced function is defined in the current
18617 		 translation unit.  */
18618 	      FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18619 		{
18620 		  tree val = ce->value;
18621 
18622 		  tree field = ce->index;
18623 
18624 		  if (val)
18625 		    STRIP_NOPS (val);
18626 
18627 		  if (!field || DECL_BIT_FIELD (field))
18628 		    {
18629 		      expansion_failed (loc, NULL_RTX,
18630 					"bitfield in record type constructor");
18631 		      size = offset = (unsigned HOST_WIDE_INT)-1;
18632 		      ret = NULL;
18633 		      break;
18634 		    }
18635 
18636 		  HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18637 		  unsigned HOST_WIDE_INT pos = int_byte_position (field);
18638 		  gcc_assert (pos + fieldsize <= size);
18639 		  if (pos < offset)
18640 		    {
18641 		      expansion_failed (loc, NULL_RTX,
18642 					"out-of-order fields in record constructor");
18643 		      size = offset = (unsigned HOST_WIDE_INT)-1;
18644 		      ret = NULL;
18645 		      break;
18646 		    }
18647 		  if (pos > offset)
18648 		    {
18649 		      ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18650 		      add_loc_descr (&ret, ret1);
18651 		      offset = pos;
18652 		    }
18653 		  if (val && fieldsize != 0)
18654 		    {
18655 		      ret1 = loc_descriptor_from_tree (val, want_address, context);
18656 		      if (!ret1)
18657 			{
18658 			  expansion_failed (loc, NULL_RTX,
18659 					    "unsupported expression in field");
18660 			  size = offset = (unsigned HOST_WIDE_INT)-1;
18661 			  ret = NULL;
18662 			  break;
18663 			}
18664 		      add_loc_descr (&ret, ret1);
18665 		    }
18666 		  if (fieldsize)
18667 		    {
18668 		      ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18669 		      add_loc_descr (&ret, ret1);
18670 		      offset = pos + fieldsize;
18671 		    }
18672 		}
18673 
18674 	      if (offset != size)
18675 		{
18676 		  ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18677 		  add_loc_descr (&ret, ret1);
18678 		  offset = size;
18679 		}
18680 
18681 	      have_address = !!want_address;
18682 	    }
18683 	  else
18684 	    expansion_failed (loc, NULL_RTX,
18685 			      "constructor of non-record type");
18686 	}
18687       else
18688       /* We can construct small constants here using int_loc_descriptor.  */
18689 	expansion_failed (loc, NULL_RTX,
18690 			  "constructor or constant not in constant pool");
18691       break;
18692 
18693     case TRUTH_AND_EXPR:
18694     case TRUTH_ANDIF_EXPR:
18695     case BIT_AND_EXPR:
18696       op = DW_OP_and;
18697       goto do_binop;
18698 
18699     case TRUTH_XOR_EXPR:
18700     case BIT_XOR_EXPR:
18701       op = DW_OP_xor;
18702       goto do_binop;
18703 
18704     case TRUTH_OR_EXPR:
18705     case TRUTH_ORIF_EXPR:
18706     case BIT_IOR_EXPR:
18707       op = DW_OP_or;
18708       goto do_binop;
18709 
18710     case FLOOR_DIV_EXPR:
18711     case CEIL_DIV_EXPR:
18712     case ROUND_DIV_EXPR:
18713     case TRUNC_DIV_EXPR:
18714     case EXACT_DIV_EXPR:
18715       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18716 	return 0;
18717       op = DW_OP_div;
18718       goto do_binop;
18719 
18720     case MINUS_EXPR:
18721       op = DW_OP_minus;
18722       goto do_binop;
18723 
18724     case FLOOR_MOD_EXPR:
18725     case CEIL_MOD_EXPR:
18726     case ROUND_MOD_EXPR:
18727     case TRUNC_MOD_EXPR:
18728       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18729 	{
18730 	  op = DW_OP_mod;
18731 	  goto do_binop;
18732 	}
18733       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18734       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18735       if (list_ret == 0 || list_ret1 == 0)
18736 	return 0;
18737 
18738       add_loc_list (&list_ret, list_ret1);
18739       if (list_ret == 0)
18740 	return 0;
18741       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18742       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18743       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18744       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18745       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18746       break;
18747 
18748     case MULT_EXPR:
18749       op = DW_OP_mul;
18750       goto do_binop;
18751 
18752     case LSHIFT_EXPR:
18753       op = DW_OP_shl;
18754       goto do_binop;
18755 
18756     case RSHIFT_EXPR:
18757       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18758       goto do_binop;
18759 
18760     case POINTER_PLUS_EXPR:
18761     case PLUS_EXPR:
18762     do_plus:
18763       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18764 	{
18765 	  /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18766 	     smarter to encode their opposite.  The DW_OP_plus_uconst operation
18767 	     takes 1 + X bytes, X being the size of the ULEB128 addend.  On the
18768 	     other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18769 	     bytes, Y being the size of the operation that pushes the opposite
18770 	     of the addend.  So let's choose the smallest representation.  */
18771 	  const tree tree_addend = TREE_OPERAND (loc, 1);
18772 	  offset_int wi_addend;
18773 	  HOST_WIDE_INT shwi_addend;
18774 	  dw_loc_descr_ref loc_naddend;
18775 
18776 	  list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18777 	  if (list_ret == 0)
18778 	    return 0;
18779 
18780 	  /* Try to get the literal to push.  It is the opposite of the addend,
18781 	     so as we rely on wrapping during DWARF evaluation, first decode
18782 	     the literal as a "DWARF-sized" signed number.  */
18783 	  wi_addend = wi::to_offset (tree_addend);
18784 	  wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18785 	  shwi_addend = wi_addend.to_shwi ();
18786 	  loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18787 			? int_loc_descriptor (-shwi_addend)
18788 			: NULL;
18789 
18790 	  if (loc_naddend != NULL
18791 	      && ((unsigned) size_of_uleb128 (shwi_addend)
18792 	          > size_of_loc_descr (loc_naddend)))
18793 	    {
18794 	      add_loc_descr_to_each (list_ret, loc_naddend);
18795 	      add_loc_descr_to_each (list_ret,
18796 				     new_loc_descr (DW_OP_minus, 0, 0));
18797 	    }
18798 	  else
18799 	    {
18800 	      for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18801 		{
18802 		  loc_naddend = loc_cur;
18803 		  loc_cur = loc_cur->dw_loc_next;
18804 		  ggc_free (loc_naddend);
18805 		}
18806 	      loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18807 	    }
18808 	  break;
18809 	}
18810 
18811       op = DW_OP_plus;
18812       goto do_binop;
18813 
18814     case LE_EXPR:
18815       op = DW_OP_le;
18816       goto do_comp_binop;
18817 
18818     case GE_EXPR:
18819       op = DW_OP_ge;
18820       goto do_comp_binop;
18821 
18822     case LT_EXPR:
18823       op = DW_OP_lt;
18824       goto do_comp_binop;
18825 
18826     case GT_EXPR:
18827       op = DW_OP_gt;
18828       goto do_comp_binop;
18829 
18830     do_comp_binop:
18831       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18832 	{
18833 	  list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18834 	  list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18835 	  list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18836 						    TREE_CODE (loc));
18837 	  break;
18838 	}
18839       else
18840 	goto do_binop;
18841 
18842     case EQ_EXPR:
18843       op = DW_OP_eq;
18844       goto do_binop;
18845 
18846     case NE_EXPR:
18847       op = DW_OP_ne;
18848       goto do_binop;
18849 
18850     do_binop:
18851       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18852       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18853       if (list_ret == 0 || list_ret1 == 0)
18854 	return 0;
18855 
18856       add_loc_list (&list_ret, list_ret1);
18857       if (list_ret == 0)
18858 	return 0;
18859       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18860       break;
18861 
18862     case TRUTH_NOT_EXPR:
18863     case BIT_NOT_EXPR:
18864       op = DW_OP_not;
18865       goto do_unop;
18866 
18867     case ABS_EXPR:
18868       op = DW_OP_abs;
18869       goto do_unop;
18870 
18871     case NEGATE_EXPR:
18872       op = DW_OP_neg;
18873       goto do_unop;
18874 
18875     do_unop:
18876       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18877       if (list_ret == 0)
18878 	return 0;
18879 
18880       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18881       break;
18882 
18883     case MIN_EXPR:
18884     case MAX_EXPR:
18885       {
18886 	const enum tree_code code =
18887 	  TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18888 
18889 	loc = build3 (COND_EXPR, TREE_TYPE (loc),
18890 		      build2 (code, integer_type_node,
18891 			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18892 		      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18893       }
18894 
18895       /* fall through */
18896 
18897     case COND_EXPR:
18898       {
18899 	dw_loc_descr_ref lhs
18900 	  = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18901 	dw_loc_list_ref rhs
18902 	  = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18903 	dw_loc_descr_ref bra_node, jump_node, tmp;
18904 
18905 	list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18906 	if (list_ret == 0 || lhs == 0 || rhs == 0)
18907 	  return 0;
18908 
18909 	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18910 	add_loc_descr_to_each (list_ret, bra_node);
18911 
18912 	add_loc_list (&list_ret, rhs);
18913 	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18914 	add_loc_descr_to_each (list_ret, jump_node);
18915 
18916 	add_loc_descr_to_each (list_ret, lhs);
18917 	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18918 	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18919 
18920 	/* ??? Need a node to point the skip at.  Use a nop.  */
18921 	tmp = new_loc_descr (DW_OP_nop, 0, 0);
18922 	add_loc_descr_to_each (list_ret, tmp);
18923 	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18924 	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18925       }
18926       break;
18927 
18928     case FIX_TRUNC_EXPR:
18929       return 0;
18930 
18931     default:
18932       /* Leave front-end specific codes as simply unknown.  This comes
18933 	 up, for instance, with the C STMT_EXPR.  */
18934       if ((unsigned int) TREE_CODE (loc)
18935 	  >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18936 	{
18937 	  expansion_failed (loc, NULL_RTX,
18938 			    "language specific tree node");
18939 	  return 0;
18940 	}
18941 
18942       /* Otherwise this is a generic code; we should just lists all of
18943 	 these explicitly.  We forgot one.  */
18944       if (flag_checking)
18945 	gcc_unreachable ();
18946 
18947       /* In a release build, we want to degrade gracefully: better to
18948 	 generate incomplete debugging information than to crash.  */
18949       return NULL;
18950     }
18951 
18952   if (!ret && !list_ret)
18953     return 0;
18954 
18955   if (want_address == 2 && !have_address
18956       && (dwarf_version >= 4 || !dwarf_strict))
18957     {
18958       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18959 	{
18960 	  expansion_failed (loc, NULL_RTX,
18961 			    "DWARF address size mismatch");
18962 	  return 0;
18963 	}
18964       if (ret)
18965 	add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18966       else
18967 	add_loc_descr_to_each (list_ret,
18968 			       new_loc_descr (DW_OP_stack_value, 0, 0));
18969       have_address = 1;
18970     }
18971   /* Show if we can't fill the request for an address.  */
18972   if (want_address && !have_address)
18973     {
18974       expansion_failed (loc, NULL_RTX,
18975 			"Want address and only have value");
18976       return 0;
18977     }
18978 
18979   gcc_assert (!ret || !list_ret);
18980 
18981   /* If we've got an address and don't want one, dereference.  */
18982   if (!want_address && have_address)
18983     {
18984       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18985 
18986       if (size > DWARF2_ADDR_SIZE || size == -1)
18987 	{
18988 	  expansion_failed (loc, NULL_RTX,
18989 			    "DWARF address size mismatch");
18990 	  return 0;
18991 	}
18992       else if (size == DWARF2_ADDR_SIZE)
18993 	op = DW_OP_deref;
18994       else
18995 	op = DW_OP_deref_size;
18996 
18997       if (ret)
18998 	add_loc_descr (&ret, new_loc_descr (op, size, 0));
18999       else
19000 	add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
19001     }
19002   if (ret)
19003     list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19004 
19005   return list_ret;
19006 }
19007 
19008 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19009    expressions.  */
19010 
19011 static dw_loc_list_ref
loc_list_from_tree(tree loc,int want_address,struct loc_descr_context * context)19012 loc_list_from_tree (tree loc, int want_address,
19013 		    struct loc_descr_context *context)
19014 {
19015   dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19016 
19017   for (dw_loc_list_ref loc_cur = result;
19018        loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19019     loc_descr_without_nops (loc_cur->expr);
19020   return result;
19021 }
19022 
19023 /* Same as above but return only single location expression.  */
19024 static dw_loc_descr_ref
loc_descriptor_from_tree(tree loc,int want_address,struct loc_descr_context * context)19025 loc_descriptor_from_tree (tree loc, int want_address,
19026 			  struct loc_descr_context *context)
19027 {
19028   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19029   if (!ret)
19030     return NULL;
19031   if (ret->dw_loc_next)
19032     {
19033       expansion_failed (loc, NULL_RTX,
19034 			"Location list where only loc descriptor needed");
19035       return NULL;
19036     }
19037   return ret->expr;
19038 }
19039 
19040 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19041    pointer to the declared type for the relevant field variable, or return
19042    `integer_type_node' if the given node turns out to be an
19043    ERROR_MARK node.  */
19044 
19045 static inline tree
field_type(const_tree decl)19046 field_type (const_tree decl)
19047 {
19048   tree type;
19049 
19050   if (TREE_CODE (decl) == ERROR_MARK)
19051     return integer_type_node;
19052 
19053   type = DECL_BIT_FIELD_TYPE (decl);
19054   if (type == NULL_TREE)
19055     type = TREE_TYPE (decl);
19056 
19057   return type;
19058 }
19059 
19060 /* Given a pointer to a tree node, return the alignment in bits for
19061    it, or else return BITS_PER_WORD if the node actually turns out to
19062    be an ERROR_MARK node.  */
19063 
19064 static inline unsigned
simple_type_align_in_bits(const_tree type)19065 simple_type_align_in_bits (const_tree type)
19066 {
19067   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19068 }
19069 
19070 static inline unsigned
simple_decl_align_in_bits(const_tree decl)19071 simple_decl_align_in_bits (const_tree decl)
19072 {
19073   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19074 }
19075 
19076 /* Return the result of rounding T up to ALIGN.  */
19077 
19078 static inline offset_int
round_up_to_align(const offset_int & t,unsigned int align)19079 round_up_to_align (const offset_int &t, unsigned int align)
19080 {
19081   return wi::udiv_trunc (t + align - 1, align) * align;
19082 }
19083 
19084 /* Compute the size of TYPE in bytes.  If possible, return NULL and store the
19085    size as an integer constant in CST_SIZE.  Otherwise, if possible, return a
19086    DWARF expression that computes the size.  Return NULL and set CST_SIZE to -1
19087    if we fail to return the size in one of these two forms.  */
19088 
19089 static dw_loc_descr_ref
type_byte_size(const_tree type,HOST_WIDE_INT * cst_size)19090 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
19091 {
19092   tree tree_size;
19093   struct loc_descr_context ctx;
19094 
19095   /* Return a constant integer in priority, if possible.  */
19096   *cst_size = int_size_in_bytes (type);
19097   if (*cst_size != -1)
19098     return NULL;
19099 
19100   ctx.context_type = const_cast<tree> (type);
19101   ctx.base_decl = NULL_TREE;
19102   ctx.dpi = NULL;
19103   ctx.placeholder_arg = false;
19104   ctx.placeholder_seen = false;
19105 
19106   type = TYPE_MAIN_VARIANT (type);
19107   tree_size = TYPE_SIZE_UNIT (type);
19108   return ((tree_size != NULL_TREE)
19109 	  ? loc_descriptor_from_tree (tree_size, 0, &ctx)
19110 	  : NULL);
19111 }
19112 
19113 /* Helper structure for RECORD_TYPE processing.  */
19114 struct vlr_context
19115 {
19116   /* Root RECORD_TYPE.  It is needed to generate data member location
19117      descriptions in variable-length records (VLR), but also to cope with
19118      variants, which are composed of nested structures multiplexed with
19119      QUAL_UNION_TYPE nodes.  Each time such a structure is passed to a
19120      function processing a FIELD_DECL, it is required to be non null.  */
19121   tree struct_type;
19122   /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19123      QUAL_UNION_TYPE), this holds an expression that computes the offset for
19124      this variant part as part of the root record (in storage units).  For
19125      regular records, it must be NULL_TREE.  */
19126   tree variant_part_offset;
19127 };
19128 
19129 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19130    addressed byte of the "containing object" for the given FIELD_DECL. If
19131    possible, return a native constant through CST_OFFSET (in which case NULL is
19132    returned); otherwise return a DWARF expression that computes the offset.
19133 
19134    Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19135    that offset is, either because the argument turns out to be a pointer to an
19136    ERROR_MARK node, or because the offset expression is too complex for us.
19137 
19138    CTX is required: see the comment for VLR_CONTEXT.  */
19139 
19140 static dw_loc_descr_ref
field_byte_offset(const_tree decl,struct vlr_context * ctx,HOST_WIDE_INT * cst_offset)19141 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19142 		   HOST_WIDE_INT *cst_offset)
19143 {
19144   tree tree_result;
19145   dw_loc_list_ref loc_result;
19146 
19147   *cst_offset = 0;
19148 
19149   if (TREE_CODE (decl) == ERROR_MARK)
19150     return NULL;
19151   else
19152     gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19153 
19154   /* We cannot handle variable bit offsets at the moment, so abort if it's the
19155      case.  */
19156   if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19157     return NULL;
19158 
19159   /* We used to handle only constant offsets in all cases.  Now, we handle
19160      properly dynamic byte offsets only when PCC bitfield type doesn't
19161      matter.  */
19162   if (PCC_BITFIELD_TYPE_MATTERS
19163       && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19164     {
19165       offset_int object_offset_in_bits;
19166       offset_int object_offset_in_bytes;
19167       offset_int bitpos_int;
19168       tree type;
19169       tree field_size_tree;
19170       offset_int deepest_bitpos;
19171       offset_int field_size_in_bits;
19172       unsigned int type_align_in_bits;
19173       unsigned int decl_align_in_bits;
19174       offset_int type_size_in_bits;
19175 
19176       bitpos_int = wi::to_offset (bit_position (decl));
19177       type = field_type (decl);
19178       type_size_in_bits = offset_int_type_size_in_bits (type);
19179       type_align_in_bits = simple_type_align_in_bits (type);
19180 
19181       field_size_tree = DECL_SIZE (decl);
19182 
19183       /* The size could be unspecified if there was an error, or for
19184 	 a flexible array member.  */
19185       if (!field_size_tree)
19186 	field_size_tree = bitsize_zero_node;
19187 
19188       /* If the size of the field is not constant, use the type size.  */
19189       if (TREE_CODE (field_size_tree) == INTEGER_CST)
19190 	field_size_in_bits = wi::to_offset (field_size_tree);
19191       else
19192 	field_size_in_bits = type_size_in_bits;
19193 
19194       decl_align_in_bits = simple_decl_align_in_bits (decl);
19195 
19196       /* The GCC front-end doesn't make any attempt to keep track of the
19197 	 starting bit offset (relative to the start of the containing
19198 	 structure type) of the hypothetical "containing object" for a
19199 	 bit-field.  Thus, when computing the byte offset value for the
19200 	 start of the "containing object" of a bit-field, we must deduce
19201 	 this information on our own. This can be rather tricky to do in
19202 	 some cases.  For example, handling the following structure type
19203 	 definition when compiling for an i386/i486 target (which only
19204 	 aligns long long's to 32-bit boundaries) can be very tricky:
19205 
19206 	 struct S { int field1; long long field2:31; };
19207 
19208 	 Fortunately, there is a simple rule-of-thumb which can be used
19209 	 in such cases.  When compiling for an i386/i486, GCC will
19210 	 allocate 8 bytes for the structure shown above.  It decides to
19211 	 do this based upon one simple rule for bit-field allocation.
19212 	 GCC allocates each "containing object" for each bit-field at
19213 	 the first (i.e. lowest addressed) legitimate alignment boundary
19214 	 (based upon the required minimum alignment for the declared
19215 	 type of the field) which it can possibly use, subject to the
19216 	 condition that there is still enough available space remaining
19217 	 in the containing object (when allocated at the selected point)
19218 	 to fully accommodate all of the bits of the bit-field itself.
19219 
19220 	 This simple rule makes it obvious why GCC allocates 8 bytes for
19221 	 each object of the structure type shown above.  When looking
19222 	 for a place to allocate the "containing object" for `field2',
19223 	 the compiler simply tries to allocate a 64-bit "containing
19224 	 object" at each successive 32-bit boundary (starting at zero)
19225 	 until it finds a place to allocate that 64- bit field such that
19226 	 at least 31 contiguous (and previously unallocated) bits remain
19227 	 within that selected 64 bit field.  (As it turns out, for the
19228 	 example above, the compiler finds it is OK to allocate the
19229 	 "containing object" 64-bit field at bit-offset zero within the
19230 	 structure type.)
19231 
19232 	 Here we attempt to work backwards from the limited set of facts
19233 	 we're given, and we try to deduce from those facts, where GCC
19234 	 must have believed that the containing object started (within
19235 	 the structure type). The value we deduce is then used (by the
19236 	 callers of this routine) to generate DW_AT_location and
19237 	 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19238 	 the case of DW_AT_location, regular fields as well).  */
19239 
19240       /* Figure out the bit-distance from the start of the structure to
19241 	 the "deepest" bit of the bit-field.  */
19242       deepest_bitpos = bitpos_int + field_size_in_bits;
19243 
19244       /* This is the tricky part.  Use some fancy footwork to deduce
19245 	 where the lowest addressed bit of the containing object must
19246 	 be.  */
19247       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19248 
19249       /* Round up to type_align by default.  This works best for
19250 	 bitfields.  */
19251       object_offset_in_bits
19252 	= round_up_to_align (object_offset_in_bits, type_align_in_bits);
19253 
19254       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19255 	{
19256 	  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19257 
19258 	  /* Round up to decl_align instead.  */
19259 	  object_offset_in_bits
19260 	    = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19261 	}
19262 
19263       object_offset_in_bytes
19264 	= wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19265       if (ctx->variant_part_offset == NULL_TREE)
19266 	{
19267 	  *cst_offset = object_offset_in_bytes.to_shwi ();
19268 	  return NULL;
19269 	}
19270       tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19271     }
19272   else
19273     tree_result = byte_position (decl);
19274 
19275   if (ctx->variant_part_offset != NULL_TREE)
19276     tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19277 			       ctx->variant_part_offset, tree_result);
19278 
19279   /* If the byte offset is a constant, it's simplier to handle a native
19280      constant rather than a DWARF expression.  */
19281   if (TREE_CODE (tree_result) == INTEGER_CST)
19282     {
19283       *cst_offset = wi::to_offset (tree_result).to_shwi ();
19284       return NULL;
19285     }
19286   struct loc_descr_context loc_ctx = {
19287     ctx->struct_type, /* context_type */
19288     NULL_TREE,	      /* base_decl */
19289     NULL,	      /* dpi */
19290     false,	      /* placeholder_arg */
19291     false	      /* placeholder_seen */
19292   };
19293   loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19294 
19295   /* We want a DWARF expression: abort if we only have a location list with
19296      multiple elements.  */
19297   if (!loc_result || !single_element_loc_list_p (loc_result))
19298     return NULL;
19299   else
19300     return loc_result->expr;
19301 }
19302 
19303 /* The following routines define various Dwarf attributes and any data
19304    associated with them.  */
19305 
19306 /* Add a location description attribute value to a DIE.
19307 
19308    This emits location attributes suitable for whole variables and
19309    whole parameters.  Note that the location attributes for struct fields are
19310    generated by the routine `data_member_location_attribute' below.  */
19311 
19312 static inline void
add_AT_location_description(dw_die_ref die,enum dwarf_attribute attr_kind,dw_loc_list_ref descr)19313 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19314 			     dw_loc_list_ref descr)
19315 {
19316   bool check_no_locviews = true;
19317   if (descr == 0)
19318     return;
19319   if (single_element_loc_list_p (descr))
19320     add_AT_loc (die, attr_kind, descr->expr);
19321   else
19322     {
19323       add_AT_loc_list (die, attr_kind, descr);
19324       gcc_assert (descr->ll_symbol);
19325       if (attr_kind == DW_AT_location && descr->vl_symbol
19326 	  && dwarf2out_locviews_in_attribute ())
19327 	{
19328 	  add_AT_view_list (die, DW_AT_GNU_locviews);
19329 	  check_no_locviews = false;
19330 	}
19331     }
19332 
19333   if (check_no_locviews)
19334     gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19335 }
19336 
19337 /* Add DW_AT_accessibility attribute to DIE if needed.  */
19338 
19339 static void
add_accessibility_attribute(dw_die_ref die,tree decl)19340 add_accessibility_attribute (dw_die_ref die, tree decl)
19341 {
19342   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19343      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19344      the default has always been DW_ACCESS_public.  */
19345   if (TREE_PROTECTED (decl))
19346     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19347   else if (TREE_PRIVATE (decl))
19348     {
19349       if (dwarf_version == 2
19350 	  || die->die_parent == NULL
19351 	  || die->die_parent->die_tag != DW_TAG_class_type)
19352 	add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19353     }
19354   else if (dwarf_version > 2
19355 	   && die->die_parent
19356 	   && die->die_parent->die_tag == DW_TAG_class_type)
19357     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19358 }
19359 
19360 /* Attach the specialized form of location attribute used for data members of
19361    struct and union types.  In the special case of a FIELD_DECL node which
19362    represents a bit-field, the "offset" part of this special location
19363    descriptor must indicate the distance in bytes from the lowest-addressed
19364    byte of the containing struct or union type to the lowest-addressed byte of
19365    the "containing object" for the bit-field.  (See the `field_byte_offset'
19366    function above).
19367 
19368    For any given bit-field, the "containing object" is a hypothetical object
19369    (of some integral or enum type) within which the given bit-field lives.  The
19370    type of this hypothetical "containing object" is always the same as the
19371    declared type of the individual bit-field itself (for GCC anyway... the
19372    DWARF spec doesn't actually mandate this).  Note that it is the size (in
19373    bytes) of the hypothetical "containing object" which will be given in the
19374    DW_AT_byte_size attribute for this bit-field.  (See the
19375    `byte_size_attribute' function below.)  It is also used when calculating the
19376    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
19377    function below.)
19378 
19379    CTX is required: see the comment for VLR_CONTEXT.  */
19380 
19381 static void
add_data_member_location_attribute(dw_die_ref die,tree decl,struct vlr_context * ctx)19382 add_data_member_location_attribute (dw_die_ref die,
19383 				    tree decl,
19384 				    struct vlr_context *ctx)
19385 {
19386   HOST_WIDE_INT offset;
19387   dw_loc_descr_ref loc_descr = 0;
19388 
19389   if (TREE_CODE (decl) == TREE_BINFO)
19390     {
19391       /* We're working on the TAG_inheritance for a base class.  */
19392       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19393 	{
19394 	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19395 	     aren't at a fixed offset from all (sub)objects of the same
19396 	     type.  We need to extract the appropriate offset from our
19397 	     vtable.  The following dwarf expression means
19398 
19399 	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
19400 
19401 	     This is specific to the V3 ABI, of course.  */
19402 
19403 	  dw_loc_descr_ref tmp;
19404 
19405 	  /* Make a copy of the object address.  */
19406 	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
19407 	  add_loc_descr (&loc_descr, tmp);
19408 
19409 	  /* Extract the vtable address.  */
19410 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
19411 	  add_loc_descr (&loc_descr, tmp);
19412 
19413 	  /* Calculate the address of the offset.  */
19414 	  offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19415 	  gcc_assert (offset < 0);
19416 
19417 	  tmp = int_loc_descriptor (-offset);
19418 	  add_loc_descr (&loc_descr, tmp);
19419 	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
19420 	  add_loc_descr (&loc_descr, tmp);
19421 
19422 	  /* Extract the offset.  */
19423 	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
19424 	  add_loc_descr (&loc_descr, tmp);
19425 
19426 	  /* Add it to the object address.  */
19427 	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
19428 	  add_loc_descr (&loc_descr, tmp);
19429 	}
19430       else
19431 	offset = tree_to_shwi (BINFO_OFFSET (decl));
19432     }
19433   else
19434     {
19435       loc_descr = field_byte_offset (decl, ctx, &offset);
19436 
19437       /* If loc_descr is available then we know the field offset is dynamic.
19438 	 However, GDB does not handle dynamic field offsets very well at the
19439 	 moment.  */
19440       if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19441 	{
19442 	  loc_descr = NULL;
19443 	  offset = 0;
19444 	}
19445 
19446       /* Data member location evalutation starts with the base address on the
19447 	 stack.  Compute the field offset and add it to this base address.  */
19448       else if (loc_descr != NULL)
19449 	add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19450     }
19451 
19452   if (! loc_descr)
19453     {
19454       /* While DW_AT_data_bit_offset has been added already in DWARF4,
19455 	 e.g. GDB only added support to it in November 2016.  For DWARF5
19456 	 we need newer debug info consumers anyway.  We might change this
19457 	 to dwarf_version >= 4 once most consumers catched up.  */
19458       if (dwarf_version >= 5
19459 	  && TREE_CODE (decl) == FIELD_DECL
19460 	  && DECL_BIT_FIELD_TYPE (decl))
19461 	{
19462 	  tree off = bit_position (decl);
19463 	  if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19464 	    {
19465 	      remove_AT (die, DW_AT_byte_size);
19466 	      remove_AT (die, DW_AT_bit_offset);
19467 	      add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19468 	      return;
19469 	    }
19470 	}
19471       if (dwarf_version > 2)
19472 	{
19473 	  /* Don't need to output a location expression, just the constant. */
19474 	  if (offset < 0)
19475 	    add_AT_int (die, DW_AT_data_member_location, offset);
19476 	  else
19477 	    add_AT_unsigned (die, DW_AT_data_member_location, offset);
19478 	  return;
19479 	}
19480       else
19481 	{
19482 	  enum dwarf_location_atom op;
19483 
19484 	  /* The DWARF2 standard says that we should assume that the structure
19485 	     address is already on the stack, so we can specify a structure
19486 	     field address by using DW_OP_plus_uconst.  */
19487 	  op = DW_OP_plus_uconst;
19488 	  loc_descr = new_loc_descr (op, offset, 0);
19489 	}
19490     }
19491 
19492   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19493 }
19494 
19495 /* Writes integer values to dw_vec_const array.  */
19496 
19497 static void
insert_int(HOST_WIDE_INT val,unsigned int size,unsigned char * dest)19498 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19499 {
19500   while (size != 0)
19501     {
19502       *dest++ = val & 0xff;
19503       val >>= 8;
19504       --size;
19505     }
19506 }
19507 
19508 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
19509 
19510 static HOST_WIDE_INT
extract_int(const unsigned char * src,unsigned int size)19511 extract_int (const unsigned char *src, unsigned int size)
19512 {
19513   HOST_WIDE_INT val = 0;
19514 
19515   src += size;
19516   while (size != 0)
19517     {
19518       val <<= 8;
19519       val |= *--src & 0xff;
19520       --size;
19521     }
19522   return val;
19523 }
19524 
19525 /* Writes wide_int values to dw_vec_const array.  */
19526 
19527 static void
insert_wide_int(const wide_int & val,unsigned char * dest,int elt_size)19528 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19529 {
19530   int i;
19531 
19532   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19533     {
19534       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19535       return;
19536     }
19537 
19538   /* We'd have to extend this code to support odd sizes.  */
19539   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19540 
19541   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19542 
19543   if (WORDS_BIG_ENDIAN)
19544     for (i = n - 1; i >= 0; i--)
19545       {
19546 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19547 	dest += sizeof (HOST_WIDE_INT);
19548       }
19549   else
19550     for (i = 0; i < n; i++)
19551       {
19552 	insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19553 	dest += sizeof (HOST_WIDE_INT);
19554       }
19555 }
19556 
19557 /* Writes floating point values to dw_vec_const array.  */
19558 
19559 static unsigned
insert_float(const_rtx rtl,unsigned char * array)19560 insert_float (const_rtx rtl, unsigned char *array)
19561 {
19562   long val[4];
19563   int i;
19564   scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19565 
19566   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19567 
19568   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
19569   if (GET_MODE_SIZE (mode) < 4)
19570     {
19571       gcc_assert (GET_MODE_SIZE (mode) == 2);
19572       insert_int (val[0], 2, array);
19573       return 2;
19574     }
19575 
19576   for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19577     {
19578       insert_int (val[i], 4, array);
19579       array += 4;
19580     }
19581   return 4;
19582 }
19583 
19584 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19585    does not have a "location" either in memory or in a register.  These
19586    things can arise in GNU C when a constant is passed as an actual parameter
19587    to an inlined function.  They can also arise in C++ where declared
19588    constants do not necessarily get memory "homes".  */
19589 
19590 static bool
add_const_value_attribute(dw_die_ref die,rtx rtl)19591 add_const_value_attribute (dw_die_ref die, rtx rtl)
19592 {
19593   switch (GET_CODE (rtl))
19594     {
19595     case CONST_INT:
19596       {
19597 	HOST_WIDE_INT val = INTVAL (rtl);
19598 
19599 	if (val < 0)
19600 	  add_AT_int (die, DW_AT_const_value, val);
19601 	else
19602 	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19603       }
19604       return true;
19605 
19606     case CONST_WIDE_INT:
19607       {
19608 	wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19609 	unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19610 				 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19611 	wide_int w = wi::zext (w1, prec);
19612 	add_AT_wide (die, DW_AT_const_value, w);
19613       }
19614       return true;
19615 
19616     case CONST_DOUBLE:
19617       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19618 	 floating-point constant.  A CONST_DOUBLE is used whenever the
19619 	 constant requires more than one word in order to be adequately
19620 	 represented.  */
19621       if (TARGET_SUPPORTS_WIDE_INT == 0
19622 	  && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19623 	add_AT_double (die, DW_AT_const_value,
19624 		       CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19625       else
19626 	{
19627 	  scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19628 	  unsigned int length = GET_MODE_SIZE (mode);
19629 	  unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19630 	  unsigned int elt_size = insert_float (rtl, array);
19631 
19632 	  add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
19633 		      array);
19634 	}
19635       return true;
19636 
19637     case CONST_VECTOR:
19638       {
19639 	unsigned int length;
19640 	if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19641 	  return false;
19642 
19643 	machine_mode mode = GET_MODE (rtl);
19644 	/* The combination of a length and byte elt_size doesn't extend
19645 	   naturally to boolean vectors, where several elements are packed
19646 	   into the same byte.  */
19647 	if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
19648 	  return false;
19649 
19650 	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19651 	unsigned char *array
19652 	  = ggc_vec_alloc<unsigned char> (length * elt_size);
19653 	unsigned int i;
19654 	unsigned char *p;
19655 	machine_mode imode = GET_MODE_INNER (mode);
19656 
19657 	switch (GET_MODE_CLASS (mode))
19658 	  {
19659 	  case MODE_VECTOR_INT:
19660 	    for (i = 0, p = array; i < length; i++, p += elt_size)
19661 	      {
19662 		rtx elt = CONST_VECTOR_ELT (rtl, i);
19663 		insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19664 	      }
19665 	    break;
19666 
19667 	  case MODE_VECTOR_FLOAT:
19668 	    for (i = 0, p = array; i < length; i++, p += elt_size)
19669 	      {
19670 		rtx elt = CONST_VECTOR_ELT (rtl, i);
19671 		insert_float (elt, p);
19672 	      }
19673 	    break;
19674 
19675 	  default:
19676 	    gcc_unreachable ();
19677 	  }
19678 
19679 	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19680       }
19681       return true;
19682 
19683     case CONST_STRING:
19684       if (dwarf_version >= 4 || !dwarf_strict)
19685 	{
19686 	  dw_loc_descr_ref loc_result;
19687 	  resolve_one_addr (&rtl);
19688 	rtl_addr:
19689           loc_result = new_addr_loc_descr (rtl, dtprel_false);
19690 	  add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19691 	  add_AT_loc (die, DW_AT_location, loc_result);
19692 	  vec_safe_push (used_rtx_array, rtl);
19693 	  return true;
19694 	}
19695       return false;
19696 
19697     case CONST:
19698       if (CONSTANT_P (XEXP (rtl, 0)))
19699 	return add_const_value_attribute (die, XEXP (rtl, 0));
19700       /* FALLTHROUGH */
19701     case SYMBOL_REF:
19702       if (!const_ok_for_output (rtl))
19703 	return false;
19704       /* FALLTHROUGH */
19705     case LABEL_REF:
19706       if (dwarf_version >= 4 || !dwarf_strict)
19707 	goto rtl_addr;
19708       return false;
19709 
19710     case PLUS:
19711       /* In cases where an inlined instance of an inline function is passed
19712 	 the address of an `auto' variable (which is local to the caller) we
19713 	 can get a situation where the DECL_RTL of the artificial local
19714 	 variable (for the inlining) which acts as a stand-in for the
19715 	 corresponding formal parameter (of the inline function) will look
19716 	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
19717 	 exactly a compile-time constant expression, but it isn't the address
19718 	 of the (artificial) local variable either.  Rather, it represents the
19719 	 *value* which the artificial local variable always has during its
19720 	 lifetime.  We currently have no way to represent such quasi-constant
19721 	 values in Dwarf, so for now we just punt and generate nothing.  */
19722       return false;
19723 
19724     case HIGH:
19725     case CONST_FIXED:
19726     case MINUS:
19727     case SIGN_EXTEND:
19728     case ZERO_EXTEND:
19729     case CONST_POLY_INT:
19730       return false;
19731 
19732     case MEM:
19733       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19734 	  && MEM_READONLY_P (rtl)
19735 	  && GET_MODE (rtl) == BLKmode)
19736 	{
19737 	  add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19738 	  return true;
19739 	}
19740       return false;
19741 
19742     default:
19743       /* No other kinds of rtx should be possible here.  */
19744       gcc_unreachable ();
19745     }
19746   return false;
19747 }
19748 
19749 /* Determine whether the evaluation of EXPR references any variables
19750    or functions which aren't otherwise used (and therefore may not be
19751    output).  */
19752 static tree
reference_to_unused(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)19753 reference_to_unused (tree * tp, int * walk_subtrees,
19754 		     void * data ATTRIBUTE_UNUSED)
19755 {
19756   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19757     *walk_subtrees = 0;
19758 
19759   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19760       && ! TREE_ASM_WRITTEN (*tp))
19761     return *tp;
19762   /* ???  The C++ FE emits debug information for using decls, so
19763      putting gcc_unreachable here falls over.  See PR31899.  For now
19764      be conservative.  */
19765   else if (!symtab->global_info_ready && VAR_P (*tp))
19766     return *tp;
19767   else if (VAR_P (*tp))
19768     {
19769       varpool_node *node = varpool_node::get (*tp);
19770       if (!node || !node->definition)
19771 	return *tp;
19772     }
19773   else if (TREE_CODE (*tp) == FUNCTION_DECL
19774 	   && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19775     {
19776       /* The call graph machinery must have finished analyzing,
19777          optimizing and gimplifying the CU by now.
19778 	 So if *TP has no call graph node associated
19779 	 to it, it means *TP will not be emitted.  */
19780       if (!symtab->global_info_ready || !cgraph_node::get (*tp))
19781 	return *tp;
19782     }
19783   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19784     return *tp;
19785 
19786   return NULL_TREE;
19787 }
19788 
19789 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19790    for use in a later add_const_value_attribute call.  */
19791 
19792 static rtx
rtl_for_decl_init(tree init,tree type)19793 rtl_for_decl_init (tree init, tree type)
19794 {
19795   rtx rtl = NULL_RTX;
19796 
19797   STRIP_NOPS (init);
19798 
19799   /* If a variable is initialized with a string constant without embedded
19800      zeros, build CONST_STRING.  */
19801   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19802     {
19803       tree enttype = TREE_TYPE (type);
19804       tree domain = TYPE_DOMAIN (type);
19805       scalar_int_mode mode;
19806 
19807       if (is_int_mode (TYPE_MODE (enttype), &mode)
19808 	  && GET_MODE_SIZE (mode) == 1
19809 	  && domain
19810 	  && TYPE_MAX_VALUE (domain)
19811 	  && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19812 	  && integer_zerop (TYPE_MIN_VALUE (domain))
19813 	  && compare_tree_int (TYPE_MAX_VALUE (domain),
19814 			       TREE_STRING_LENGTH (init) - 1) == 0
19815 	  && ((size_t) TREE_STRING_LENGTH (init)
19816 	      == strlen (TREE_STRING_POINTER (init)) + 1))
19817 	{
19818 	  rtl = gen_rtx_CONST_STRING (VOIDmode,
19819 				      ggc_strdup (TREE_STRING_POINTER (init)));
19820 	  rtl = gen_rtx_MEM (BLKmode, rtl);
19821 	  MEM_READONLY_P (rtl) = 1;
19822 	}
19823     }
19824   /* Other aggregates, and complex values, could be represented using
19825      CONCAT: FIXME!  */
19826   else if (AGGREGATE_TYPE_P (type)
19827 	   || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19828 	       && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19829 	   || TREE_CODE (type) == COMPLEX_TYPE)
19830     ;
19831   /* Vectors only work if their mode is supported by the target.
19832      FIXME: generic vectors ought to work too.  */
19833   else if (TREE_CODE (type) == VECTOR_TYPE
19834 	   && !VECTOR_MODE_P (TYPE_MODE (type)))
19835     ;
19836   /* If the initializer is something that we know will expand into an
19837      immediate RTL constant, expand it now.  We must be careful not to
19838      reference variables which won't be output.  */
19839   else if (initializer_constant_valid_p (init, type)
19840 	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19841     {
19842       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19843 	 possible.  */
19844       if (TREE_CODE (type) == VECTOR_TYPE)
19845 	switch (TREE_CODE (init))
19846 	  {
19847 	  case VECTOR_CST:
19848 	    break;
19849 	  case CONSTRUCTOR:
19850 	    if (TREE_CONSTANT (init))
19851 	      {
19852 		vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19853 		bool constant_p = true;
19854 		tree value;
19855 		unsigned HOST_WIDE_INT ix;
19856 
19857 		/* Even when ctor is constant, it might contain non-*_CST
19858 		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19859 		   belong into VECTOR_CST nodes.  */
19860 		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19861 		  if (!CONSTANT_CLASS_P (value))
19862 		    {
19863 		      constant_p = false;
19864 		      break;
19865 		    }
19866 
19867 		if (constant_p)
19868 		  {
19869 		    init = build_vector_from_ctor (type, elts);
19870 		    break;
19871 		  }
19872 	      }
19873 	    /* FALLTHRU */
19874 
19875 	  default:
19876 	    return NULL;
19877 	  }
19878 
19879       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19880 
19881       /* If expand_expr returns a MEM, it wasn't immediate.  */
19882       gcc_assert (!rtl || !MEM_P (rtl));
19883     }
19884 
19885   return rtl;
19886 }
19887 
19888 /* Generate RTL for the variable DECL to represent its location.  */
19889 
19890 static rtx
rtl_for_decl_location(tree decl)19891 rtl_for_decl_location (tree decl)
19892 {
19893   rtx rtl;
19894 
19895   /* Here we have to decide where we are going to say the parameter "lives"
19896      (as far as the debugger is concerned).  We only have a couple of
19897      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19898 
19899      DECL_RTL normally indicates where the parameter lives during most of the
19900      activation of the function.  If optimization is enabled however, this
19901      could be either NULL or else a pseudo-reg.  Both of those cases indicate
19902      that the parameter doesn't really live anywhere (as far as the code
19903      generation parts of GCC are concerned) during most of the function's
19904      activation.  That will happen (for example) if the parameter is never
19905      referenced within the function.
19906 
19907      We could just generate a location descriptor here for all non-NULL
19908      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19909      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19910      where DECL_RTL is NULL or is a pseudo-reg.
19911 
19912      Note however that we can only get away with using DECL_INCOMING_RTL as
19913      a backup substitute for DECL_RTL in certain limited cases.  In cases
19914      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19915      we can be sure that the parameter was passed using the same type as it is
19916      declared to have within the function, and that its DECL_INCOMING_RTL
19917      points us to a place where a value of that type is passed.
19918 
19919      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19920      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19921      because in these cases DECL_INCOMING_RTL points us to a value of some
19922      type which is *different* from the type of the parameter itself.  Thus,
19923      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19924      such cases, the debugger would end up (for example) trying to fetch a
19925      `float' from a place which actually contains the first part of a
19926      `double'.  That would lead to really incorrect and confusing
19927      output at debug-time.
19928 
19929      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19930      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
19931      are a couple of exceptions however.  On little-endian machines we can
19932      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19933      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19934      an integral type that is smaller than TREE_TYPE (decl). These cases arise
19935      when (on a little-endian machine) a non-prototyped function has a
19936      parameter declared to be of type `short' or `char'.  In such cases,
19937      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19938      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19939      passed `int' value.  If the debugger then uses that address to fetch
19940      a `short' or a `char' (on a little-endian machine) the result will be
19941      the correct data, so we allow for such exceptional cases below.
19942 
19943      Note that our goal here is to describe the place where the given formal
19944      parameter lives during most of the function's activation (i.e. between the
19945      end of the prologue and the start of the epilogue).  We'll do that as best
19946      as we can. Note however that if the given formal parameter is modified
19947      sometime during the execution of the function, then a stack backtrace (at
19948      debug-time) will show the function as having been called with the *new*
19949      value rather than the value which was originally passed in.  This happens
19950      rarely enough that it is not a major problem, but it *is* a problem, and
19951      I'd like to fix it.
19952 
19953      A future version of dwarf2out.c may generate two additional attributes for
19954      any given DW_TAG_formal_parameter DIE which will describe the "passed
19955      type" and the "passed location" for the given formal parameter in addition
19956      to the attributes we now generate to indicate the "declared type" and the
19957      "active location" for each parameter.  This additional set of attributes
19958      could be used by debuggers for stack backtraces. Separately, note that
19959      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19960      This happens (for example) for inlined-instances of inline function formal
19961      parameters which are never referenced.  This really shouldn't be
19962      happening.  All PARM_DECL nodes should get valid non-NULL
19963      DECL_INCOMING_RTL values.  FIXME.  */
19964 
19965   /* Use DECL_RTL as the "location" unless we find something better.  */
19966   rtl = DECL_RTL_IF_SET (decl);
19967 
19968   /* When generating abstract instances, ignore everything except
19969      constants, symbols living in memory, and symbols living in
19970      fixed registers.  */
19971   if (! reload_completed)
19972     {
19973       if (rtl
19974 	  && (CONSTANT_P (rtl)
19975 	      || (MEM_P (rtl)
19976 	          && CONSTANT_P (XEXP (rtl, 0)))
19977 	      || (REG_P (rtl)
19978 	          && VAR_P (decl)
19979 		  && TREE_STATIC (decl))))
19980 	{
19981 	  rtl = targetm.delegitimize_address (rtl);
19982 	  return rtl;
19983 	}
19984       rtl = NULL_RTX;
19985     }
19986   else if (TREE_CODE (decl) == PARM_DECL)
19987     {
19988       if (rtl == NULL_RTX
19989 	  || is_pseudo_reg (rtl)
19990 	  || (MEM_P (rtl)
19991 	      && is_pseudo_reg (XEXP (rtl, 0))
19992 	      && DECL_INCOMING_RTL (decl)
19993 	      && MEM_P (DECL_INCOMING_RTL (decl))
19994 	      && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19995 	{
19996 	  tree declared_type = TREE_TYPE (decl);
19997 	  tree passed_type = DECL_ARG_TYPE (decl);
19998 	  machine_mode dmode = TYPE_MODE (declared_type);
19999 	  machine_mode pmode = TYPE_MODE (passed_type);
20000 
20001 	  /* This decl represents a formal parameter which was optimized out.
20002 	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20003 	     all cases where (rtl == NULL_RTX) just below.  */
20004 	  if (dmode == pmode)
20005 	    rtl = DECL_INCOMING_RTL (decl);
20006 	  else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20007 		   && SCALAR_INT_MODE_P (dmode)
20008 		   && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20009 		   && DECL_INCOMING_RTL (decl))
20010 	    {
20011 	      rtx inc = DECL_INCOMING_RTL (decl);
20012 	      if (REG_P (inc))
20013 		rtl = inc;
20014 	      else if (MEM_P (inc))
20015 		{
20016 		  if (BYTES_BIG_ENDIAN)
20017 		    rtl = adjust_address_nv (inc, dmode,
20018 					     GET_MODE_SIZE (pmode)
20019 					     - GET_MODE_SIZE (dmode));
20020 		  else
20021 		    rtl = inc;
20022 		}
20023 	    }
20024 	}
20025 
20026       /* If the parm was passed in registers, but lives on the stack, then
20027 	 make a big endian correction if the mode of the type of the
20028 	 parameter is not the same as the mode of the rtl.  */
20029       /* ??? This is the same series of checks that are made in dbxout.c before
20030 	 we reach the big endian correction code there.  It isn't clear if all
20031 	 of these checks are necessary here, but keeping them all is the safe
20032 	 thing to do.  */
20033       else if (MEM_P (rtl)
20034 	       && XEXP (rtl, 0) != const0_rtx
20035 	       && ! CONSTANT_P (XEXP (rtl, 0))
20036 	       /* Not passed in memory.  */
20037 	       && !MEM_P (DECL_INCOMING_RTL (decl))
20038 	       /* Not passed by invisible reference.  */
20039 	       && (!REG_P (XEXP (rtl, 0))
20040 		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20041 		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20042 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20043 		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20044 #endif
20045 		     )
20046 	       /* Big endian correction check.  */
20047 	       && BYTES_BIG_ENDIAN
20048 	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20049 	       && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20050 			    UNITS_PER_WORD))
20051 	{
20052 	  machine_mode addr_mode = get_address_mode (rtl);
20053 	  poly_int64 offset = (UNITS_PER_WORD
20054 			       - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20055 
20056 	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20057 			     plus_constant (addr_mode, XEXP (rtl, 0), offset));
20058 	}
20059     }
20060   else if (VAR_P (decl)
20061 	   && rtl
20062 	   && MEM_P (rtl)
20063 	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20064     {
20065       machine_mode addr_mode = get_address_mode (rtl);
20066       poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20067 					       GET_MODE (rtl));
20068 
20069       /* If a variable is declared "register" yet is smaller than
20070 	 a register, then if we store the variable to memory, it
20071 	 looks like we're storing a register-sized value, when in
20072 	 fact we are not.  We need to adjust the offset of the
20073 	 storage location to reflect the actual value's bytes,
20074 	 else gdb will not be able to display it.  */
20075       if (maybe_ne (offset, 0))
20076 	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20077 			   plus_constant (addr_mode, XEXP (rtl, 0), offset));
20078     }
20079 
20080   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20081      and will have been substituted directly into all expressions that use it.
20082      C does not have such a concept, but C++ and other languages do.  */
20083   if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20084     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20085 
20086   if (rtl)
20087     rtl = targetm.delegitimize_address (rtl);
20088 
20089   /* If we don't look past the constant pool, we risk emitting a
20090      reference to a constant pool entry that isn't referenced from
20091      code, and thus is not emitted.  */
20092   if (rtl)
20093     rtl = avoid_constant_pool_reference (rtl);
20094 
20095   /* Try harder to get a rtl.  If this symbol ends up not being emitted
20096      in the current CU, resolve_addr will remove the expression referencing
20097      it.  */
20098   if (rtl == NULL_RTX
20099       && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20100       && VAR_P (decl)
20101       && !DECL_EXTERNAL (decl)
20102       && TREE_STATIC (decl)
20103       && DECL_NAME (decl)
20104       && !DECL_HARD_REGISTER (decl)
20105       && DECL_MODE (decl) != VOIDmode)
20106     {
20107       rtl = make_decl_rtl_for_debug (decl);
20108       if (!MEM_P (rtl)
20109 	  || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20110 	  || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20111 	rtl = NULL_RTX;
20112     }
20113 
20114   return rtl;
20115 }
20116 
20117 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
20118    returned.  If so, the decl for the COMMON block is returned, and the
20119    value is the offset into the common block for the symbol.  */
20120 
20121 static tree
fortran_common(tree decl,HOST_WIDE_INT * value)20122 fortran_common (tree decl, HOST_WIDE_INT *value)
20123 {
20124   tree val_expr, cvar;
20125   machine_mode mode;
20126   poly_int64 bitsize, bitpos;
20127   tree offset;
20128   HOST_WIDE_INT cbitpos;
20129   int unsignedp, reversep, volatilep = 0;
20130 
20131   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20132      it does not have a value (the offset into the common area), or if it
20133      is thread local (as opposed to global) then it isn't common, and shouldn't
20134      be handled as such.  */
20135   if (!VAR_P (decl)
20136       || !TREE_STATIC (decl)
20137       || !DECL_HAS_VALUE_EXPR_P (decl)
20138       || !is_fortran ())
20139     return NULL_TREE;
20140 
20141   val_expr = DECL_VALUE_EXPR (decl);
20142   if (TREE_CODE (val_expr) != COMPONENT_REF)
20143     return NULL_TREE;
20144 
20145   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20146 			      &unsignedp, &reversep, &volatilep);
20147 
20148   if (cvar == NULL_TREE
20149       || !VAR_P (cvar)
20150       || DECL_ARTIFICIAL (cvar)
20151       || !TREE_PUBLIC (cvar)
20152       /* We don't expect to have to cope with variable offsets,
20153 	 since at present all static data must have a constant size.  */
20154       || !bitpos.is_constant (&cbitpos))
20155     return NULL_TREE;
20156 
20157   *value = 0;
20158   if (offset != NULL)
20159     {
20160       if (!tree_fits_shwi_p (offset))
20161 	return NULL_TREE;
20162       *value = tree_to_shwi (offset);
20163     }
20164   if (cbitpos != 0)
20165     *value += cbitpos / BITS_PER_UNIT;
20166 
20167   return cvar;
20168 }
20169 
20170 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20171    data attribute for a variable or a parameter.  We generate the
20172    DW_AT_const_value attribute only in those cases where the given variable
20173    or parameter does not have a true "location" either in memory or in a
20174    register.  This can happen (for example) when a constant is passed as an
20175    actual argument in a call to an inline function.  (It's possible that
20176    these things can crop up in other ways also.)  Note that one type of
20177    constant value which can be passed into an inlined function is a constant
20178    pointer.  This can happen for example if an actual argument in an inlined
20179    function call evaluates to a compile-time constant address.
20180 
20181    CACHE_P is true if it is worth caching the location list for DECL,
20182    so that future calls can reuse it rather than regenerate it from scratch.
20183    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20184    since we will need to refer to them each time the function is inlined.  */
20185 
20186 static bool
add_location_or_const_value_attribute(dw_die_ref die,tree decl,bool cache_p)20187 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20188 {
20189   rtx rtl;
20190   dw_loc_list_ref list;
20191   var_loc_list *loc_list;
20192   cached_dw_loc_list *cache;
20193 
20194   if (early_dwarf)
20195     return false;
20196 
20197   if (TREE_CODE (decl) == ERROR_MARK)
20198     return false;
20199 
20200   if (get_AT (die, DW_AT_location)
20201       || get_AT (die, DW_AT_const_value))
20202     return true;
20203 
20204   gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20205 	      || TREE_CODE (decl) == RESULT_DECL);
20206 
20207   /* Try to get some constant RTL for this decl, and use that as the value of
20208      the location.  */
20209 
20210   rtl = rtl_for_decl_location (decl);
20211   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20212       && add_const_value_attribute (die, rtl))
20213     return true;
20214 
20215   /* See if we have single element location list that is equivalent to
20216      a constant value.  That way we are better to use add_const_value_attribute
20217      rather than expanding constant value equivalent.  */
20218   loc_list = lookup_decl_loc (decl);
20219   if (loc_list
20220       && loc_list->first
20221       && loc_list->first->next == NULL
20222       && NOTE_P (loc_list->first->loc)
20223       && NOTE_VAR_LOCATION (loc_list->first->loc)
20224       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20225     {
20226       struct var_loc_node *node;
20227 
20228       node = loc_list->first;
20229       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20230       if (GET_CODE (rtl) == EXPR_LIST)
20231 	rtl = XEXP (rtl, 0);
20232       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20233 	  && add_const_value_attribute (die, rtl))
20234 	 return true;
20235     }
20236   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20237      list several times.  See if we've already cached the contents.  */
20238   list = NULL;
20239   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20240     cache_p = false;
20241   if (cache_p)
20242     {
20243       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20244       if (cache)
20245 	list = cache->loc_list;
20246     }
20247   if (list == NULL)
20248     {
20249       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20250 				 NULL);
20251       /* It is usually worth caching this result if the decl is from
20252 	 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
20253       if (cache_p && list && list->dw_loc_next)
20254 	{
20255 	  cached_dw_loc_list **slot
20256 	    = cached_dw_loc_list_table->find_slot_with_hash (decl,
20257 							     DECL_UID (decl),
20258 							     INSERT);
20259 	  cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20260 	  cache->decl_id = DECL_UID (decl);
20261 	  cache->loc_list = list;
20262 	  *slot = cache;
20263 	}
20264     }
20265   if (list)
20266     {
20267       add_AT_location_description (die, DW_AT_location, list);
20268       return true;
20269     }
20270   /* None of that worked, so it must not really have a location;
20271      try adding a constant value attribute from the DECL_INITIAL.  */
20272   return tree_add_const_value_attribute_for_decl (die, decl);
20273 }
20274 
20275 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20276    attribute is the const value T.  */
20277 
20278 static bool
tree_add_const_value_attribute(dw_die_ref die,tree t)20279 tree_add_const_value_attribute (dw_die_ref die, tree t)
20280 {
20281   tree init;
20282   tree type = TREE_TYPE (t);
20283   rtx rtl;
20284 
20285   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20286     return false;
20287 
20288   init = t;
20289   gcc_assert (!DECL_P (init));
20290 
20291   if (TREE_CODE (init) == INTEGER_CST)
20292     {
20293       if (tree_fits_uhwi_p (init))
20294 	{
20295 	  add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20296 	  return true;
20297 	}
20298       if (tree_fits_shwi_p (init))
20299 	{
20300 	  add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20301 	  return true;
20302 	}
20303     }
20304   /* Generate the RTL even if early_dwarf to force mangling of all refered to
20305      symbols.  */
20306   rtl = rtl_for_decl_init (init, type);
20307   if (rtl && !early_dwarf)
20308     return add_const_value_attribute (die, rtl);
20309   /* If the host and target are sane, try harder.  */
20310   if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20311       && initializer_constant_valid_p (init, type))
20312     {
20313       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20314       if (size > 0 && (int) size == size)
20315 	{
20316 	  unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20317 
20318 	  if (native_encode_initializer (init, array, size) == size)
20319 	    {
20320 	      add_AT_vec (die, DW_AT_const_value, size, 1, array);
20321 	      return true;
20322 	    }
20323 	  ggc_free (array);
20324 	}
20325     }
20326   return false;
20327 }
20328 
20329 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20330    attribute is the const value of T, where T is an integral constant
20331    variable with static storage duration
20332    (so it can't be a PARM_DECL or a RESULT_DECL).  */
20333 
20334 static bool
tree_add_const_value_attribute_for_decl(dw_die_ref var_die,tree decl)20335 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20336 {
20337 
20338   if (!decl
20339       || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20340       || (VAR_P (decl) && !TREE_STATIC (decl)))
20341     return false;
20342 
20343   if (TREE_READONLY (decl)
20344       && ! TREE_THIS_VOLATILE (decl)
20345       && DECL_INITIAL (decl))
20346     /* OK */;
20347   else
20348     return false;
20349 
20350   /* Don't add DW_AT_const_value if abstract origin already has one.  */
20351   if (get_AT (var_die, DW_AT_const_value))
20352     return false;
20353 
20354   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20355 }
20356 
20357 /* Convert the CFI instructions for the current function into a
20358    location list.  This is used for DW_AT_frame_base when we targeting
20359    a dwarf2 consumer that does not support the dwarf3
20360    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
20361    expressions.  */
20362 
20363 static dw_loc_list_ref
convert_cfa_to_fb_loc_list(HOST_WIDE_INT offset)20364 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20365 {
20366   int ix;
20367   dw_fde_ref fde;
20368   dw_loc_list_ref list, *list_tail;
20369   dw_cfi_ref cfi;
20370   dw_cfa_location last_cfa, next_cfa;
20371   const char *start_label, *last_label, *section;
20372   dw_cfa_location remember;
20373 
20374   fde = cfun->fde;
20375   gcc_assert (fde != NULL);
20376 
20377   section = secname_for_decl (current_function_decl);
20378   list_tail = &list;
20379   list = NULL;
20380 
20381   memset (&next_cfa, 0, sizeof (next_cfa));
20382   next_cfa.reg = INVALID_REGNUM;
20383   remember = next_cfa;
20384 
20385   start_label = fde->dw_fde_begin;
20386 
20387   /* ??? Bald assumption that the CIE opcode list does not contain
20388      advance opcodes.  */
20389   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20390     lookup_cfa_1 (cfi, &next_cfa, &remember);
20391 
20392   last_cfa = next_cfa;
20393   last_label = start_label;
20394 
20395   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20396     {
20397       /* If the first partition contained no CFI adjustments, the
20398 	 CIE opcodes apply to the whole first partition.  */
20399       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20400 				 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20401       list_tail =&(*list_tail)->dw_loc_next;
20402       start_label = last_label = fde->dw_fde_second_begin;
20403     }
20404 
20405   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20406     {
20407       switch (cfi->dw_cfi_opc)
20408 	{
20409 	case DW_CFA_set_loc:
20410 	case DW_CFA_advance_loc1:
20411 	case DW_CFA_advance_loc2:
20412 	case DW_CFA_advance_loc4:
20413 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
20414 	    {
20415 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20416 					 start_label, 0, last_label, 0, section);
20417 
20418 	      list_tail = &(*list_tail)->dw_loc_next;
20419 	      last_cfa = next_cfa;
20420 	      start_label = last_label;
20421 	    }
20422 	  last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20423 	  break;
20424 
20425 	case DW_CFA_advance_loc:
20426 	  /* The encoding is complex enough that we should never emit this.  */
20427 	  gcc_unreachable ();
20428 
20429 	default:
20430 	  lookup_cfa_1 (cfi, &next_cfa, &remember);
20431 	  break;
20432 	}
20433       if (ix + 1 == fde->dw_fde_switch_cfi_index)
20434 	{
20435 	  if (!cfa_equal_p (&last_cfa, &next_cfa))
20436 	    {
20437 	      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20438 					 start_label, 0, last_label, 0, section);
20439 
20440 	      list_tail = &(*list_tail)->dw_loc_next;
20441 	      last_cfa = next_cfa;
20442 	      start_label = last_label;
20443 	    }
20444 	  *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20445 				     start_label, 0, fde->dw_fde_end, 0, section);
20446 	  list_tail = &(*list_tail)->dw_loc_next;
20447 	  start_label = last_label = fde->dw_fde_second_begin;
20448 	}
20449     }
20450 
20451   if (!cfa_equal_p (&last_cfa, &next_cfa))
20452     {
20453       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20454 				 start_label, 0, last_label, 0, section);
20455       list_tail = &(*list_tail)->dw_loc_next;
20456       start_label = last_label;
20457     }
20458 
20459   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20460 			     start_label, 0,
20461 			     fde->dw_fde_second_begin
20462 			     ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20463 			     section);
20464 
20465   maybe_gen_llsym (list);
20466 
20467   return list;
20468 }
20469 
20470 /* Compute a displacement from the "steady-state frame pointer" to the
20471    frame base (often the same as the CFA), and store it in
20472    frame_pointer_fb_offset.  OFFSET is added to the displacement
20473    before the latter is negated.  */
20474 
20475 static void
compute_frame_pointer_to_fb_displacement(poly_int64 offset)20476 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20477 {
20478   rtx reg, elim;
20479 
20480 #ifdef FRAME_POINTER_CFA_OFFSET
20481   reg = frame_pointer_rtx;
20482   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20483 #else
20484   reg = arg_pointer_rtx;
20485   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20486 #endif
20487 
20488   elim = (ira_use_lra_p
20489 	  ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20490 	  : eliminate_regs (reg, VOIDmode, NULL_RTX));
20491   elim = strip_offset_and_add (elim, &offset);
20492 
20493   frame_pointer_fb_offset = -offset;
20494 
20495   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20496      in which to eliminate.  This is because it's stack pointer isn't
20497      directly accessible as a register within the ISA.  To work around
20498      this, assume that while we cannot provide a proper value for
20499      frame_pointer_fb_offset, we won't need one either.  We can use
20500      hard frame pointer in debug info even if frame pointer isn't used
20501      since hard frame pointer in debug info is encoded with DW_OP_fbreg
20502      which uses the DW_AT_frame_base attribute, not hard frame pointer
20503      directly.  */
20504   frame_pointer_fb_offset_valid
20505     = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20506 }
20507 
20508 /* Generate a DW_AT_name attribute given some string value to be included as
20509    the value of the attribute.  */
20510 
20511 static void
add_name_attribute(dw_die_ref die,const char * name_string)20512 add_name_attribute (dw_die_ref die, const char *name_string)
20513 {
20514   if (name_string != NULL && *name_string != 0)
20515     {
20516       if (demangle_name_func)
20517 	name_string = (*demangle_name_func) (name_string);
20518 
20519       add_AT_string (die, DW_AT_name, name_string);
20520     }
20521 }
20522 
20523 /* Generate a DW_AT_description attribute given some string value to be included
20524    as the value of the attribute.  */
20525 
20526 static void
add_desc_attribute(dw_die_ref die,const char * name_string)20527 add_desc_attribute (dw_die_ref die, const char *name_string)
20528 {
20529   if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20530     return;
20531 
20532   if (name_string == NULL || *name_string == 0)
20533     return;
20534 
20535   if (demangle_name_func)
20536     name_string = (*demangle_name_func) (name_string);
20537 
20538   add_AT_string (die, DW_AT_description, name_string);
20539 }
20540 
20541 /* Generate a DW_AT_description attribute given some decl to be included
20542    as the value of the attribute.  */
20543 
20544 static void
add_desc_attribute(dw_die_ref die,tree decl)20545 add_desc_attribute (dw_die_ref die, tree decl)
20546 {
20547   tree decl_name;
20548 
20549   if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20550     return;
20551 
20552   if (decl == NULL_TREE || !DECL_P (decl))
20553     return;
20554   decl_name = DECL_NAME (decl);
20555 
20556   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20557     {
20558       const char *name = dwarf2_name (decl, 0);
20559       add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20560     }
20561   else
20562     {
20563       char *desc = print_generic_expr_to_str (decl);
20564       add_desc_attribute (die, desc);
20565       free (desc);
20566     }
20567 }
20568 
20569 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20570    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20571    of TYPE accordingly.
20572 
20573    ??? This is a temporary measure until after we're able to generate
20574    regular DWARF for the complex Ada type system.  */
20575 
20576 static void
add_gnat_descriptive_type_attribute(dw_die_ref die,tree type,dw_die_ref context_die)20577 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20578 				     dw_die_ref context_die)
20579 {
20580   tree dtype;
20581   dw_die_ref dtype_die;
20582 
20583   if (!lang_hooks.types.descriptive_type)
20584     return;
20585 
20586   dtype = lang_hooks.types.descriptive_type (type);
20587   if (!dtype)
20588     return;
20589 
20590   dtype_die = lookup_type_die (dtype);
20591   if (!dtype_die)
20592     {
20593       gen_type_die (dtype, context_die);
20594       dtype_die = lookup_type_die (dtype);
20595       gcc_assert (dtype_die);
20596     }
20597 
20598   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20599 }
20600 
20601 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
20602 
20603 static const char *
comp_dir_string(void)20604 comp_dir_string (void)
20605 {
20606   const char *wd;
20607   char *wd_plus_sep = NULL;
20608   static const char *cached_wd = NULL;
20609 
20610   if (cached_wd != NULL)
20611     return cached_wd;
20612 
20613   wd = get_src_pwd ();
20614   if (wd == NULL)
20615     return NULL;
20616 
20617   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20618     {
20619       size_t wdlen = strlen (wd);
20620       wd_plus_sep = XNEWVEC (char, wdlen + 2);
20621       strcpy (wd_plus_sep, wd);
20622       wd_plus_sep [wdlen] = DIR_SEPARATOR;
20623       wd_plus_sep [wdlen + 1] = 0;
20624       wd = wd_plus_sep;
20625     }
20626 
20627   cached_wd = remap_debug_filename (wd);
20628 
20629   /* remap_debug_filename can just pass through wd or return a new gc string.
20630      These two types can't be both stored in a GTY(())-tagged string, but since
20631      the cached value lives forever just copy it if needed.  */
20632   if (cached_wd != wd)
20633     {
20634       cached_wd = xstrdup (cached_wd);
20635       if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
20636         free (wd_plus_sep);
20637     }
20638 
20639   return cached_wd;
20640 }
20641 
20642 /* Generate a DW_AT_comp_dir attribute for DIE.  */
20643 
20644 static void
add_comp_dir_attribute(dw_die_ref die)20645 add_comp_dir_attribute (dw_die_ref die)
20646 {
20647   const char * wd = comp_dir_string ();
20648   if (wd != NULL)
20649     add_AT_string (die, DW_AT_comp_dir, wd);
20650 }
20651 
20652 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20653    pointer computation, ...), output a representation for that bound according
20654    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
20655    loc_list_from_tree for the meaning of CONTEXT.  */
20656 
20657 static void
add_scalar_info(dw_die_ref die,enum dwarf_attribute attr,tree value,int forms,struct loc_descr_context * context)20658 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20659 		 int forms, struct loc_descr_context *context)
20660 {
20661   dw_die_ref context_die, decl_die = NULL;
20662   dw_loc_list_ref list;
20663   bool strip_conversions = true;
20664   bool placeholder_seen = false;
20665 
20666   while (strip_conversions)
20667     switch (TREE_CODE (value))
20668       {
20669       case ERROR_MARK:
20670       case SAVE_EXPR:
20671 	return;
20672 
20673       CASE_CONVERT:
20674       case VIEW_CONVERT_EXPR:
20675 	value = TREE_OPERAND (value, 0);
20676 	break;
20677 
20678       default:
20679 	strip_conversions = false;
20680 	break;
20681       }
20682 
20683   /* If possible and permitted, output the attribute as a constant.  */
20684   if ((forms & dw_scalar_form_constant) != 0
20685       && TREE_CODE (value) == INTEGER_CST)
20686     {
20687       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20688 
20689       /* If HOST_WIDE_INT is big enough then represent the bound as
20690 	 a constant value.  We need to choose a form based on
20691 	 whether the type is signed or unsigned.  We cannot just
20692 	 call add_AT_unsigned if the value itself is positive
20693 	 (add_AT_unsigned might add the unsigned value encoded as
20694 	 DW_FORM_data[1248]).  Some DWARF consumers will lookup the
20695 	 bounds type and then sign extend any unsigned values found
20696 	 for signed types.  This is needed only for
20697 	 DW_AT_{lower,upper}_bound, since for most other attributes,
20698 	 consumers will treat DW_FORM_data[1248] as unsigned values,
20699 	 regardless of the underlying type.  */
20700       if (prec <= HOST_BITS_PER_WIDE_INT
20701 	  || tree_fits_uhwi_p (value))
20702 	{
20703 	  if (TYPE_UNSIGNED (TREE_TYPE (value)))
20704 	    add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20705 	  else
20706 	    add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20707 	}
20708       else if (dwarf_version >= 5
20709 	       && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
20710 	/* Otherwise represent the bound as an unsigned value with
20711 	   the precision of its type.  The precision and signedness
20712 	   of the type will be necessary to re-interpret it
20713 	   unambiguously.  */
20714 	add_AT_wide (die, attr, wi::to_wide (value));
20715       else
20716 	{
20717 	  rtx v = immed_wide_int_const (wi::to_wide (value),
20718 					TYPE_MODE (TREE_TYPE (value)));
20719 	  dw_loc_descr_ref loc
20720 	    = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
20721 			      VAR_INIT_STATUS_INITIALIZED);
20722 	  if (loc)
20723 	    add_AT_loc (die, attr, loc);
20724 	}
20725       return;
20726     }
20727 
20728   /* Otherwise, if it's possible and permitted too, output a reference to
20729      another DIE.  */
20730   if ((forms & dw_scalar_form_reference) != 0)
20731     {
20732       tree decl = NULL_TREE;
20733 
20734       /* Some type attributes reference an outer type.  For instance, the upper
20735 	 bound of an array may reference an embedding record (this happens in
20736 	 Ada).  */
20737       if (TREE_CODE (value) == COMPONENT_REF
20738 	  && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20739 	  && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20740 	decl = TREE_OPERAND (value, 1);
20741 
20742       else if (VAR_P (value)
20743 	       || TREE_CODE (value) == PARM_DECL
20744 	       || TREE_CODE (value) == RESULT_DECL)
20745 	decl = value;
20746 
20747       if (decl != NULL_TREE)
20748 	{
20749 	  decl_die = lookup_decl_die (decl);
20750 
20751 	  /* ??? Can this happen, or should the variable have been bound
20752 	     first?  Probably it can, since I imagine that we try to create
20753 	     the types of parameters in the order in which they exist in
20754 	     the list, and won't have created a forward reference to a
20755 	     later parameter.  */
20756 	  if (decl_die != NULL)
20757 	    {
20758 	      if (get_AT (decl_die, DW_AT_location)
20759 		  || get_AT (decl_die, DW_AT_data_member_location)
20760 		  || get_AT (decl_die, DW_AT_const_value))
20761 		{
20762 		  add_AT_die_ref (die, attr, decl_die);
20763 		  return;
20764 		}
20765 	    }
20766 	}
20767     }
20768 
20769   /* Last chance: try to create a stack operation procedure to evaluate the
20770      value.  Do nothing if even that is not possible or permitted.  */
20771   if ((forms & dw_scalar_form_exprloc) == 0)
20772     return;
20773 
20774   list = loc_list_from_tree (value, 2, context);
20775   if (context && context->placeholder_arg)
20776     {
20777       placeholder_seen = context->placeholder_seen;
20778       context->placeholder_seen = false;
20779     }
20780   if (list == NULL || single_element_loc_list_p (list))
20781     {
20782       /* If this attribute is not a reference nor constant, it is
20783 	 a DWARF expression rather than location description.  For that
20784 	 loc_list_from_tree (value, 0, &context) is needed.  */
20785       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20786       if (list2 && single_element_loc_list_p (list2))
20787 	{
20788 	  if (placeholder_seen)
20789 	    {
20790 	      struct dwarf_procedure_info dpi;
20791 	      dpi.fndecl = NULL_TREE;
20792 	      dpi.args_count = 1;
20793 	      if (!resolve_args_picking (list2->expr, 1, &dpi))
20794 		return;
20795 	    }
20796 	  add_AT_loc (die, attr, list2->expr);
20797 	  return;
20798 	}
20799     }
20800 
20801   /* If that failed to give a single element location list, fall back to
20802      outputting this as a reference... still if permitted.  */
20803   if (list == NULL
20804       || (forms & dw_scalar_form_reference) == 0
20805       || placeholder_seen)
20806     return;
20807 
20808   if (!decl_die)
20809     {
20810       if (current_function_decl == 0)
20811 	context_die = comp_unit_die ();
20812       else
20813 	context_die = lookup_decl_die (current_function_decl);
20814 
20815       decl_die = new_die (DW_TAG_variable, context_die, value);
20816       add_AT_flag (decl_die, DW_AT_artificial, 1);
20817       add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20818 			  context_die);
20819     }
20820 
20821   add_AT_location_description (decl_die, DW_AT_location, list);
20822   add_AT_die_ref (die, attr, decl_die);
20823 }
20824 
20825 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20826    default.  */
20827 
20828 static int
lower_bound_default(void)20829 lower_bound_default (void)
20830 {
20831   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20832     {
20833     case DW_LANG_C:
20834     case DW_LANG_C89:
20835     case DW_LANG_C99:
20836     case DW_LANG_C11:
20837     case DW_LANG_C_plus_plus:
20838     case DW_LANG_C_plus_plus_11:
20839     case DW_LANG_C_plus_plus_14:
20840     case DW_LANG_ObjC:
20841     case DW_LANG_ObjC_plus_plus:
20842       return 0;
20843     case DW_LANG_Fortran77:
20844     case DW_LANG_Fortran90:
20845     case DW_LANG_Fortran95:
20846     case DW_LANG_Fortran03:
20847     case DW_LANG_Fortran08:
20848       return 1;
20849     case DW_LANG_UPC:
20850     case DW_LANG_D:
20851     case DW_LANG_Python:
20852       return dwarf_version >= 4 ? 0 : -1;
20853     case DW_LANG_Ada95:
20854     case DW_LANG_Ada83:
20855     case DW_LANG_Cobol74:
20856     case DW_LANG_Cobol85:
20857     case DW_LANG_Modula2:
20858     case DW_LANG_PLI:
20859       return dwarf_version >= 4 ? 1 : -1;
20860     default:
20861       return -1;
20862     }
20863 }
20864 
20865 /* Given a tree node describing an array bound (either lower or upper) output
20866    a representation for that bound.  */
20867 
20868 static void
add_bound_info(dw_die_ref subrange_die,enum dwarf_attribute bound_attr,tree bound,struct loc_descr_context * context)20869 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20870 		tree bound, struct loc_descr_context *context)
20871 {
20872   int dflt;
20873 
20874   while (1)
20875     switch (TREE_CODE (bound))
20876       {
20877       /* Strip all conversions.  */
20878       CASE_CONVERT:
20879       case VIEW_CONVERT_EXPR:
20880 	bound = TREE_OPERAND (bound, 0);
20881 	break;
20882 
20883       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
20884 	 are even omitted when they are the default.  */
20885       case INTEGER_CST:
20886 	/* If the value for this bound is the default one, we can even omit the
20887 	   attribute.  */
20888 	if (bound_attr == DW_AT_lower_bound
20889 	    && tree_fits_shwi_p (bound)
20890 	    && (dflt = lower_bound_default ()) != -1
20891 	    && tree_to_shwi (bound) == dflt)
20892 	  return;
20893 
20894 	/* FALLTHRU */
20895 
20896       default:
20897 	/* Because of the complex interaction there can be with other GNAT
20898 	   encodings, GDB isn't ready yet to handle proper DWARF description
20899 	   for self-referencial subrange bounds: let GNAT encodings do the
20900 	   magic in such a case.  */
20901 	if (is_ada ()
20902 	    && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20903 	    && contains_placeholder_p (bound))
20904 	  return;
20905 
20906 	add_scalar_info (subrange_die, bound_attr, bound,
20907 			 dw_scalar_form_constant
20908 			 | dw_scalar_form_exprloc
20909 			 | dw_scalar_form_reference,
20910 			 context);
20911 	return;
20912       }
20913 }
20914 
20915 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20916    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20917    Note that the block of subscript information for an array type also
20918    includes information about the element type of the given array type.
20919 
20920    This function reuses previously set type and bound information if
20921    available.  */
20922 
20923 static void
add_subscript_info(dw_die_ref type_die,tree type,bool collapse_p)20924 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20925 {
20926   unsigned dimension_number;
20927   tree lower, upper;
20928   dw_die_ref child = type_die->die_child;
20929 
20930   for (dimension_number = 0;
20931        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20932        type = TREE_TYPE (type), dimension_number++)
20933     {
20934       tree domain = TYPE_DOMAIN (type);
20935 
20936       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20937 	break;
20938 
20939       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20940 	 and (in GNU C only) variable bounds.  Handle all three forms
20941 	 here.  */
20942 
20943       /* Find and reuse a previously generated DW_TAG_subrange_type if
20944 	 available.
20945 
20946          For multi-dimensional arrays, as we iterate through the
20947          various dimensions in the enclosing for loop above, we also
20948          iterate through the DIE children and pick at each
20949          DW_TAG_subrange_type previously generated (if available).
20950          Each child DW_TAG_subrange_type DIE describes the range of
20951          the current dimension.  At this point we should have as many
20952          DW_TAG_subrange_type's as we have dimensions in the
20953          array.  */
20954       dw_die_ref subrange_die = NULL;
20955       if (child)
20956 	while (1)
20957 	  {
20958 	    child = child->die_sib;
20959 	    if (child->die_tag == DW_TAG_subrange_type)
20960 	      subrange_die = child;
20961 	    if (child == type_die->die_child)
20962 	      {
20963 		/* If we wrapped around, stop looking next time.  */
20964 		child = NULL;
20965 		break;
20966 	      }
20967 	    if (child->die_tag == DW_TAG_subrange_type)
20968 	      break;
20969 	  }
20970       if (!subrange_die)
20971 	subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20972 
20973       if (domain)
20974 	{
20975 	  /* We have an array type with specified bounds.  */
20976 	  lower = TYPE_MIN_VALUE (domain);
20977 	  upper = TYPE_MAX_VALUE (domain);
20978 
20979 	  /* Define the index type.  */
20980 	  if (TREE_TYPE (domain)
20981 	      && !get_AT (subrange_die, DW_AT_type))
20982 	    {
20983 	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
20984 		 TREE_TYPE field.  We can't emit debug info for this
20985 		 because it is an unnamed integral type.  */
20986 	      if (TREE_CODE (domain) == INTEGER_TYPE
20987 		  && TYPE_NAME (domain) == NULL_TREE
20988 		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20989 		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20990 		;
20991 	      else
20992 		add_type_attribute (subrange_die, TREE_TYPE (domain),
20993 				    TYPE_UNQUALIFIED, false, type_die);
20994 	    }
20995 
20996 	  /* ??? If upper is NULL, the array has unspecified length,
20997 	     but it does have a lower bound.  This happens with Fortran
20998 	       dimension arr(N:*)
20999 	     Since the debugger is definitely going to need to know N
21000 	     to produce useful results, go ahead and output the lower
21001 	     bound solo, and hope the debugger can cope.  */
21002 
21003 	  if (!get_AT (subrange_die, DW_AT_lower_bound))
21004 	    add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21005 	  if (!get_AT (subrange_die, DW_AT_upper_bound)
21006 	      && !get_AT (subrange_die, DW_AT_count))
21007 	    {
21008 	      if (upper)
21009 		add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21010 	      else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21011 		/* Zero-length array.  */
21012 		add_bound_info (subrange_die, DW_AT_count,
21013 				build_int_cst (TREE_TYPE (lower), 0), NULL);
21014 	    }
21015 	}
21016 
21017       /* Otherwise we have an array type with an unspecified length.  The
21018 	 DWARF-2 spec does not say how to handle this; let's just leave out the
21019 	 bounds.  */
21020     }
21021 }
21022 
21023 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
21024 
21025 static void
add_byte_size_attribute(dw_die_ref die,tree tree_node)21026 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21027 {
21028   dw_die_ref decl_die;
21029   HOST_WIDE_INT size;
21030   dw_loc_descr_ref size_expr = NULL;
21031 
21032   switch (TREE_CODE (tree_node))
21033     {
21034     case ERROR_MARK:
21035       size = 0;
21036       break;
21037     case ENUMERAL_TYPE:
21038     case RECORD_TYPE:
21039     case UNION_TYPE:
21040     case QUAL_UNION_TYPE:
21041       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21042 	  && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21043 	{
21044 	  add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21045 	  return;
21046 	}
21047       size_expr = type_byte_size (tree_node, &size);
21048       break;
21049     case FIELD_DECL:
21050       /* For a data member of a struct or union, the DW_AT_byte_size is
21051 	 generally given as the number of bytes normally allocated for an
21052 	 object of the *declared* type of the member itself.  This is true
21053 	 even for bit-fields.  */
21054       size = int_size_in_bytes (field_type (tree_node));
21055       break;
21056     default:
21057       gcc_unreachable ();
21058     }
21059 
21060   /* Support for dynamically-sized objects was introduced by DWARFv3.
21061      At the moment, GDB does not handle variable byte sizes very well,
21062      though.  */
21063   if ((dwarf_version >= 3 || !dwarf_strict)
21064       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
21065       && size_expr != NULL)
21066     add_AT_loc (die, DW_AT_byte_size, size_expr);
21067 
21068   /* Note that `size' might be -1 when we get to this point.  If it is, that
21069      indicates that the byte size of the entity in question is variable and
21070      that we could not generate a DWARF expression that computes it.  */
21071   if (size >= 0)
21072     add_AT_unsigned (die, DW_AT_byte_size, size);
21073 }
21074 
21075 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21076    alignment.  */
21077 
21078 static void
add_alignment_attribute(dw_die_ref die,tree tree_node)21079 add_alignment_attribute (dw_die_ref die, tree tree_node)
21080 {
21081   if (dwarf_version < 5 && dwarf_strict)
21082     return;
21083 
21084   unsigned align;
21085 
21086   if (DECL_P (tree_node))
21087     {
21088       if (!DECL_USER_ALIGN (tree_node))
21089 	return;
21090 
21091       align = DECL_ALIGN_UNIT (tree_node);
21092     }
21093   else if (TYPE_P (tree_node))
21094     {
21095       if (!TYPE_USER_ALIGN (tree_node))
21096 	return;
21097 
21098       align = TYPE_ALIGN_UNIT (tree_node);
21099     }
21100   else
21101     gcc_unreachable ();
21102 
21103   add_AT_unsigned (die, DW_AT_alignment, align);
21104 }
21105 
21106 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21107    which specifies the distance in bits from the highest order bit of the
21108    "containing object" for the bit-field to the highest order bit of the
21109    bit-field itself.
21110 
21111    For any given bit-field, the "containing object" is a hypothetical object
21112    (of some integral or enum type) within which the given bit-field lives.  The
21113    type of this hypothetical "containing object" is always the same as the
21114    declared type of the individual bit-field itself.  The determination of the
21115    exact location of the "containing object" for a bit-field is rather
21116    complicated.  It's handled by the `field_byte_offset' function (above).
21117 
21118    CTX is required: see the comment for VLR_CONTEXT.
21119 
21120    Note that it is the size (in bytes) of the hypothetical "containing object"
21121    which will be given in the DW_AT_byte_size attribute for this bit-field.
21122    (See `byte_size_attribute' above).  */
21123 
21124 static inline void
add_bit_offset_attribute(dw_die_ref die,tree decl,struct vlr_context * ctx)21125 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
21126 {
21127   HOST_WIDE_INT object_offset_in_bytes;
21128   tree original_type = DECL_BIT_FIELD_TYPE (decl);
21129   HOST_WIDE_INT bitpos_int;
21130   HOST_WIDE_INT highest_order_object_bit_offset;
21131   HOST_WIDE_INT highest_order_field_bit_offset;
21132   HOST_WIDE_INT bit_offset;
21133 
21134   field_byte_offset (decl, ctx, &object_offset_in_bytes);
21135 
21136   /* Must be a field and a bit field.  */
21137   gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21138 
21139   /* We can't yet handle bit-fields whose offsets are variable, so if we
21140      encounter such things, just return without generating any attribute
21141      whatsoever.  Likewise for variable or too large size.  */
21142   if (! tree_fits_shwi_p (bit_position (decl))
21143       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21144     return;
21145 
21146   bitpos_int = int_bit_position (decl);
21147 
21148   /* Note that the bit offset is always the distance (in bits) from the
21149      highest-order bit of the "containing object" to the highest-order bit of
21150      the bit-field itself.  Since the "high-order end" of any object or field
21151      is different on big-endian and little-endian machines, the computation
21152      below must take account of these differences.  */
21153   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21154   highest_order_field_bit_offset = bitpos_int;
21155 
21156   if (! BYTES_BIG_ENDIAN)
21157     {
21158       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21159       highest_order_object_bit_offset +=
21160         simple_type_size_in_bits (original_type);
21161     }
21162 
21163   bit_offset
21164     = (! BYTES_BIG_ENDIAN
21165        ? highest_order_object_bit_offset - highest_order_field_bit_offset
21166        : highest_order_field_bit_offset - highest_order_object_bit_offset);
21167 
21168   if (bit_offset < 0)
21169     add_AT_int (die, DW_AT_bit_offset, bit_offset);
21170   else
21171     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21172 }
21173 
21174 /* For a FIELD_DECL node which represents a bit field, output an attribute
21175    which specifies the length in bits of the given field.  */
21176 
21177 static inline void
add_bit_size_attribute(dw_die_ref die,tree decl)21178 add_bit_size_attribute (dw_die_ref die, tree decl)
21179 {
21180   /* Must be a field and a bit field.  */
21181   gcc_assert (TREE_CODE (decl) == FIELD_DECL
21182 	      && DECL_BIT_FIELD_TYPE (decl));
21183 
21184   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21185     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21186 }
21187 
21188 /* If the compiled language is ANSI C, then add a 'prototyped'
21189    attribute, if arg types are given for the parameters of a function.  */
21190 
21191 static inline void
add_prototyped_attribute(dw_die_ref die,tree func_type)21192 add_prototyped_attribute (dw_die_ref die, tree func_type)
21193 {
21194   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21195     {
21196     case DW_LANG_C:
21197     case DW_LANG_C89:
21198     case DW_LANG_C99:
21199     case DW_LANG_C11:
21200     case DW_LANG_ObjC:
21201       if (prototype_p (func_type))
21202 	add_AT_flag (die, DW_AT_prototyped, 1);
21203       break;
21204     default:
21205       break;
21206     }
21207 }
21208 
21209 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
21210    by looking in the type declaration, the object declaration equate table or
21211    the block mapping.  */
21212 
21213 static inline void
add_abstract_origin_attribute(dw_die_ref die,tree origin)21214 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21215 {
21216   dw_die_ref origin_die = NULL;
21217 
21218   /* For late LTO debug output we want to refer directly to the abstract
21219      DIE in the early debug rather to the possibly existing concrete
21220      instance and avoid creating that just for this purpose.  */
21221   sym_off_pair *desc;
21222   if (in_lto_p
21223       && external_die_map
21224       && (desc = external_die_map->get (origin)))
21225     {
21226       add_AT_external_die_ref (die, DW_AT_abstract_origin,
21227 			       desc->sym, desc->off);
21228       return;
21229     }
21230 
21231   if (DECL_P (origin))
21232     origin_die = lookup_decl_die (origin);
21233   else if (TYPE_P (origin))
21234     origin_die = lookup_type_die (origin);
21235   else if (TREE_CODE (origin) == BLOCK)
21236     origin_die = lookup_block_die (origin);
21237 
21238   /* XXX: Functions that are never lowered don't always have correct block
21239      trees (in the case of java, they simply have no block tree, in some other
21240      languages).  For these functions, there is nothing we can really do to
21241      output correct debug info for inlined functions in all cases.  Rather
21242      than die, we'll just produce deficient debug info now, in that we will
21243      have variables without a proper abstract origin.  In the future, when all
21244      functions are lowered, we should re-add a gcc_assert (origin_die)
21245      here.  */
21246 
21247   if (origin_die)
21248     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21249 }
21250 
21251 /* We do not currently support the pure_virtual attribute.  */
21252 
21253 static inline void
add_pure_or_virtual_attribute(dw_die_ref die,tree func_decl)21254 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21255 {
21256   if (DECL_VINDEX (func_decl))
21257     {
21258       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21259 
21260       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21261 	add_AT_loc (die, DW_AT_vtable_elem_location,
21262 		    new_loc_descr (DW_OP_constu,
21263 				   tree_to_shwi (DECL_VINDEX (func_decl)),
21264 				   0));
21265 
21266       /* GNU extension: Record what type this method came from originally.  */
21267       if (debug_info_level > DINFO_LEVEL_TERSE
21268 	  && DECL_CONTEXT (func_decl))
21269 	add_AT_die_ref (die, DW_AT_containing_type,
21270 			lookup_type_die (DECL_CONTEXT (func_decl)));
21271     }
21272 }
21273 
21274 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21275    given decl.  This used to be a vendor extension until after DWARF 4
21276    standardized it.  */
21277 
21278 static void
add_linkage_attr(dw_die_ref die,tree decl)21279 add_linkage_attr (dw_die_ref die, tree decl)
21280 {
21281   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21282 
21283   /* Mimic what assemble_name_raw does with a leading '*'.  */
21284   if (name[0] == '*')
21285     name = &name[1];
21286 
21287   if (dwarf_version >= 4)
21288     add_AT_string (die, DW_AT_linkage_name, name);
21289   else
21290     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21291 }
21292 
21293 /* Add source coordinate attributes for the given decl.  */
21294 
21295 static void
add_src_coords_attributes(dw_die_ref die,tree decl)21296 add_src_coords_attributes (dw_die_ref die, tree decl)
21297 {
21298   expanded_location s;
21299 
21300   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21301     return;
21302   s = expand_location (DECL_SOURCE_LOCATION (decl));
21303   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21304   add_AT_unsigned (die, DW_AT_decl_line, s.line);
21305   if (debug_column_info && s.column)
21306     add_AT_unsigned (die, DW_AT_decl_column, s.column);
21307 }
21308 
21309 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
21310 
21311 static void
add_linkage_name_raw(dw_die_ref die,tree decl)21312 add_linkage_name_raw (dw_die_ref die, tree decl)
21313 {
21314   /* Defer until we have an assembler name set.  */
21315   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21316     {
21317       limbo_die_node *asm_name;
21318 
21319       asm_name = ggc_cleared_alloc<limbo_die_node> ();
21320       asm_name->die = die;
21321       asm_name->created_for = decl;
21322       asm_name->next = deferred_asm_name;
21323       deferred_asm_name = asm_name;
21324     }
21325   else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21326     add_linkage_attr (die, decl);
21327 }
21328 
21329 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired.  */
21330 
21331 static void
add_linkage_name(dw_die_ref die,tree decl)21332 add_linkage_name (dw_die_ref die, tree decl)
21333 {
21334   if (debug_info_level > DINFO_LEVEL_NONE
21335       && VAR_OR_FUNCTION_DECL_P (decl)
21336       && TREE_PUBLIC (decl)
21337       && !(VAR_P (decl) && DECL_REGISTER (decl))
21338       && die->die_tag != DW_TAG_member)
21339     add_linkage_name_raw (die, decl);
21340 }
21341 
21342 /* Add a DW_AT_name attribute and source coordinate attribute for the
21343    given decl, but only if it actually has a name.  */
21344 
21345 static void
add_name_and_src_coords_attributes(dw_die_ref die,tree decl,bool no_linkage_name)21346 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21347 				    bool no_linkage_name)
21348 {
21349   tree decl_name;
21350 
21351   decl_name = DECL_NAME (decl);
21352   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21353     {
21354       const char *name = dwarf2_name (decl, 0);
21355       if (name)
21356 	add_name_attribute (die, name);
21357       else
21358 	add_desc_attribute (die, decl);
21359 
21360       if (! DECL_ARTIFICIAL (decl))
21361 	add_src_coords_attributes (die, decl);
21362 
21363       if (!no_linkage_name)
21364 	add_linkage_name (die, decl);
21365     }
21366   else
21367     add_desc_attribute (die, decl);
21368 
21369 #ifdef VMS_DEBUGGING_INFO
21370   /* Get the function's name, as described by its RTL.  This may be different
21371      from the DECL_NAME name used in the source file.  */
21372   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21373     {
21374       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21375                   XEXP (DECL_RTL (decl), 0), false);
21376       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21377     }
21378 #endif /* VMS_DEBUGGING_INFO */
21379 }
21380 
21381 /* Add VALUE as a DW_AT_discr_value attribute to DIE.  */
21382 
21383 static void
add_discr_value(dw_die_ref die,dw_discr_value * value)21384 add_discr_value (dw_die_ref die, dw_discr_value *value)
21385 {
21386   dw_attr_node attr;
21387 
21388   attr.dw_attr = DW_AT_discr_value;
21389   attr.dw_attr_val.val_class = dw_val_class_discr_value;
21390   attr.dw_attr_val.val_entry = NULL;
21391   attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21392   if (value->pos)
21393     attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21394   else
21395     attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21396   add_dwarf_attr (die, &attr);
21397 }
21398 
21399 /* Add DISCR_LIST as a DW_AT_discr_list to DIE.  */
21400 
21401 static void
add_discr_list(dw_die_ref die,dw_discr_list_ref discr_list)21402 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21403 {
21404   dw_attr_node attr;
21405 
21406   attr.dw_attr = DW_AT_discr_list;
21407   attr.dw_attr_val.val_class = dw_val_class_discr_list;
21408   attr.dw_attr_val.val_entry = NULL;
21409   attr.dw_attr_val.v.val_discr_list = discr_list;
21410   add_dwarf_attr (die, &attr);
21411 }
21412 
21413 static inline dw_discr_list_ref
AT_discr_list(dw_attr_node * attr)21414 AT_discr_list (dw_attr_node *attr)
21415 {
21416   return attr->dw_attr_val.v.val_discr_list;
21417 }
21418 
21419 #ifdef VMS_DEBUGGING_INFO
21420 /* Output the debug main pointer die for VMS */
21421 
21422 void
dwarf2out_vms_debug_main_pointer(void)21423 dwarf2out_vms_debug_main_pointer (void)
21424 {
21425   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21426   dw_die_ref die;
21427 
21428   /* Allocate the VMS debug main subprogram die.  */
21429   die = new_die_raw (DW_TAG_subprogram);
21430   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21431   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21432 			       current_function_funcdef_no);
21433   add_AT_lbl_id (die, DW_AT_entry_pc, label);
21434 
21435   /* Make it the first child of comp_unit_die ().  */
21436   die->die_parent = comp_unit_die ();
21437   if (comp_unit_die ()->die_child)
21438     {
21439       die->die_sib = comp_unit_die ()->die_child->die_sib;
21440       comp_unit_die ()->die_child->die_sib = die;
21441     }
21442   else
21443     {
21444       die->die_sib = die;
21445       comp_unit_die ()->die_child = die;
21446     }
21447 }
21448 #endif /* VMS_DEBUGGING_INFO */
21449 
21450 /* walk_tree helper function for uses_local_type, below.  */
21451 
21452 static tree
uses_local_type_r(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)21453 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21454 {
21455   if (!TYPE_P (*tp))
21456     *walk_subtrees = 0;
21457   else
21458     {
21459       tree name = TYPE_NAME (*tp);
21460       if (name && DECL_P (name) && decl_function_context (name))
21461 	return *tp;
21462     }
21463   return NULL_TREE;
21464 }
21465 
21466 /* If TYPE involves a function-local type (including a local typedef to a
21467    non-local type), returns that type; otherwise returns NULL_TREE.  */
21468 
21469 static tree
uses_local_type(tree type)21470 uses_local_type (tree type)
21471 {
21472   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21473   return used;
21474 }
21475 
21476 /* Return the DIE for the scope that immediately contains this type.
21477    Non-named types that do not involve a function-local type get global
21478    scope.  Named types nested in namespaces or other types get their
21479    containing scope.  All other types (i.e. function-local named types) get
21480    the current active scope.  */
21481 
21482 static dw_die_ref
scope_die_for(tree t,dw_die_ref context_die)21483 scope_die_for (tree t, dw_die_ref context_die)
21484 {
21485   dw_die_ref scope_die = NULL;
21486   tree containing_scope;
21487 
21488   /* Non-types always go in the current scope.  */
21489   gcc_assert (TYPE_P (t));
21490 
21491   /* Use the scope of the typedef, rather than the scope of the type
21492      it refers to.  */
21493   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21494     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21495   else
21496     containing_scope = TYPE_CONTEXT (t);
21497 
21498   /* Use the containing namespace if there is one.  */
21499   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21500     {
21501       if (context_die == lookup_decl_die (containing_scope))
21502 	/* OK */;
21503       else if (debug_info_level > DINFO_LEVEL_TERSE)
21504 	context_die = get_context_die (containing_scope);
21505       else
21506 	containing_scope = NULL_TREE;
21507     }
21508 
21509   /* Ignore function type "scopes" from the C frontend.  They mean that
21510      a tagged type is local to a parmlist of a function declarator, but
21511      that isn't useful to DWARF.  */
21512   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21513     containing_scope = NULL_TREE;
21514 
21515   if (SCOPE_FILE_SCOPE_P (containing_scope))
21516     {
21517       /* If T uses a local type keep it local as well, to avoid references
21518 	 to function-local DIEs from outside the function.  */
21519       if (current_function_decl && uses_local_type (t))
21520 	scope_die = context_die;
21521       else
21522 	scope_die = comp_unit_die ();
21523     }
21524   else if (TYPE_P (containing_scope))
21525     {
21526       /* For types, we can just look up the appropriate DIE.  */
21527       if (debug_info_level > DINFO_LEVEL_TERSE)
21528 	scope_die = get_context_die (containing_scope);
21529       else
21530 	{
21531 	  scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21532 	  if (scope_die == NULL)
21533 	    scope_die = comp_unit_die ();
21534 	}
21535     }
21536   else
21537     scope_die = context_die;
21538 
21539   return scope_die;
21540 }
21541 
21542 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
21543 
21544 static inline int
local_scope_p(dw_die_ref context_die)21545 local_scope_p (dw_die_ref context_die)
21546 {
21547   for (; context_die; context_die = context_die->die_parent)
21548     if (context_die->die_tag == DW_TAG_inlined_subroutine
21549 	|| context_die->die_tag == DW_TAG_subprogram)
21550       return 1;
21551 
21552   return 0;
21553 }
21554 
21555 /* Returns nonzero if CONTEXT_DIE is a class.  */
21556 
21557 static inline int
class_scope_p(dw_die_ref context_die)21558 class_scope_p (dw_die_ref context_die)
21559 {
21560   return (context_die
21561 	  && (context_die->die_tag == DW_TAG_structure_type
21562 	      || context_die->die_tag == DW_TAG_class_type
21563 	      || context_die->die_tag == DW_TAG_interface_type
21564 	      || context_die->die_tag == DW_TAG_union_type));
21565 }
21566 
21567 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21568    whether or not to treat a DIE in this context as a declaration.  */
21569 
21570 static inline int
class_or_namespace_scope_p(dw_die_ref context_die)21571 class_or_namespace_scope_p (dw_die_ref context_die)
21572 {
21573   return (class_scope_p (context_die)
21574 	  || (context_die && context_die->die_tag == DW_TAG_namespace));
21575 }
21576 
21577 /* Many forms of DIEs require a "type description" attribute.  This
21578    routine locates the proper "type descriptor" die for the type given
21579    by 'type' plus any additional qualifiers given by 'cv_quals', and
21580    adds a DW_AT_type attribute below the given die.  */
21581 
21582 static void
add_type_attribute(dw_die_ref object_die,tree type,int cv_quals,bool reverse,dw_die_ref context_die)21583 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21584 		    bool reverse, dw_die_ref context_die)
21585 {
21586   enum tree_code code  = TREE_CODE (type);
21587   dw_die_ref type_die  = NULL;
21588 
21589   if (debug_info_level <= DINFO_LEVEL_TERSE)
21590     return;
21591 
21592   /* ??? If this type is an unnamed subrange type of an integral, floating-point
21593      or fixed-point type, use the inner type.  This is because we have no
21594      support for unnamed types in base_type_die.  This can happen if this is
21595      an Ada subrange type.  Correct solution is emit a subrange type die.  */
21596   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21597       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21598     type = TREE_TYPE (type), code = TREE_CODE (type);
21599 
21600   if (code == ERROR_MARK
21601       /* Handle a special case.  For functions whose return type is void, we
21602 	 generate *no* type attribute.  (Note that no object may have type
21603 	 `void', so this only applies to function return types).  */
21604       || code == VOID_TYPE)
21605     return;
21606 
21607   type_die = modified_type_die (type,
21608 				cv_quals | TYPE_QUALS (type),
21609 				reverse,
21610 				context_die);
21611 
21612   if (type_die != NULL)
21613     add_AT_die_ref (object_die, DW_AT_type, type_die);
21614 }
21615 
21616 /* Given an object die, add the calling convention attribute for the
21617    function call type.  */
21618 static void
add_calling_convention_attribute(dw_die_ref subr_die,tree decl)21619 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21620 {
21621   enum dwarf_calling_convention value = DW_CC_normal;
21622 
21623   value = ((enum dwarf_calling_convention)
21624 	   targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21625 
21626   if (is_fortran ()
21627       && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21628     {
21629       /* DWARF 2 doesn't provide a way to identify a program's source-level
21630 	entry point.  DW_AT_calling_convention attributes are only meant
21631 	to describe functions' calling conventions.  However, lacking a
21632 	better way to signal the Fortran main program, we used this for
21633 	a long time, following existing custom.  Now, DWARF 4 has
21634 	DW_AT_main_subprogram, which we add below, but some tools still
21635 	rely on the old way, which we thus keep.  */
21636       value = DW_CC_program;
21637 
21638       if (dwarf_version >= 4 || !dwarf_strict)
21639 	add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21640     }
21641 
21642   /* Only add the attribute if the backend requests it, and
21643      is not DW_CC_normal.  */
21644   if (value && (value != DW_CC_normal))
21645     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21646 }
21647 
21648 /* Given a tree pointer to a struct, class, union, or enum type node, return
21649    a pointer to the (string) tag name for the given type, or zero if the type
21650    was declared without a tag.  */
21651 
21652 static const char *
type_tag(const_tree type)21653 type_tag (const_tree type)
21654 {
21655   const char *name = 0;
21656 
21657   if (TYPE_NAME (type) != 0)
21658     {
21659       tree t = 0;
21660 
21661       /* Find the IDENTIFIER_NODE for the type name.  */
21662       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21663 	  && !TYPE_NAMELESS (type))
21664 	t = TYPE_NAME (type);
21665 
21666       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21667 	 a TYPE_DECL node, regardless of whether or not a `typedef' was
21668 	 involved.  */
21669       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21670 	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
21671 	{
21672 	  /* We want to be extra verbose.  Don't call dwarf_name if
21673 	     DECL_NAME isn't set.  The default hook for decl_printable_name
21674 	     doesn't like that, and in this context it's correct to return
21675 	     0, instead of "<anonymous>" or the like.  */
21676 	  if (DECL_NAME (TYPE_NAME (type))
21677 	      && !DECL_NAMELESS (TYPE_NAME (type)))
21678 	    name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21679 	}
21680 
21681       /* Now get the name as a string, or invent one.  */
21682       if (!name && t != 0)
21683 	name = IDENTIFIER_POINTER (t);
21684     }
21685 
21686   return (name == 0 || *name == '\0') ? 0 : name;
21687 }
21688 
21689 /* Return the type associated with a data member, make a special check
21690    for bit field types.  */
21691 
21692 static inline tree
member_declared_type(const_tree member)21693 member_declared_type (const_tree member)
21694 {
21695   return (DECL_BIT_FIELD_TYPE (member)
21696 	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21697 }
21698 
21699 /* Get the decl's label, as described by its RTL. This may be different
21700    from the DECL_NAME name used in the source file.  */
21701 
21702 #if 0
21703 static const char *
21704 decl_start_label (tree decl)
21705 {
21706   rtx x;
21707   const char *fnname;
21708 
21709   x = DECL_RTL (decl);
21710   gcc_assert (MEM_P (x));
21711 
21712   x = XEXP (x, 0);
21713   gcc_assert (GET_CODE (x) == SYMBOL_REF);
21714 
21715   fnname = XSTR (x, 0);
21716   return fnname;
21717 }
21718 #endif
21719 
21720 /* For variable-length arrays that have been previously generated, but
21721    may be incomplete due to missing subscript info, fill the subscript
21722    info.  Return TRUE if this is one of those cases.  */
21723 static bool
fill_variable_array_bounds(tree type)21724 fill_variable_array_bounds (tree type)
21725 {
21726   if (TREE_ASM_WRITTEN (type)
21727       && TREE_CODE (type) == ARRAY_TYPE
21728       && variably_modified_type_p (type, NULL))
21729     {
21730       dw_die_ref array_die = lookup_type_die (type);
21731       if (!array_die)
21732 	return false;
21733       add_subscript_info (array_die, type, !is_ada ());
21734       return true;
21735     }
21736   return false;
21737 }
21738 
21739 /* These routines generate the internal representation of the DIE's for
21740    the compilation unit.  Debugging information is collected by walking
21741    the declaration trees passed in from dwarf2out_decl().  */
21742 
21743 static void
gen_array_type_die(tree type,dw_die_ref context_die)21744 gen_array_type_die (tree type, dw_die_ref context_die)
21745 {
21746   dw_die_ref array_die;
21747 
21748   /* GNU compilers represent multidimensional array types as sequences of one
21749      dimensional array types whose element types are themselves array types.
21750      We sometimes squish that down to a single array_type DIE with multiple
21751      subscripts in the Dwarf debugging info.  The draft Dwarf specification
21752      say that we are allowed to do this kind of compression in C, because
21753      there is no difference between an array of arrays and a multidimensional
21754      array.  We don't do this for Ada to remain as close as possible to the
21755      actual representation, which is especially important against the language
21756      flexibilty wrt arrays of variable size.  */
21757 
21758   bool collapse_nested_arrays = !is_ada ();
21759 
21760   if (fill_variable_array_bounds (type))
21761     return;
21762 
21763   dw_die_ref scope_die = scope_die_for (type, context_die);
21764   tree element_type;
21765 
21766   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21767      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
21768   if (TREE_CODE (type) == ARRAY_TYPE
21769       && TYPE_STRING_FLAG (type)
21770       && is_fortran ()
21771       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21772     {
21773       HOST_WIDE_INT size;
21774 
21775       array_die = new_die (DW_TAG_string_type, scope_die, type);
21776       add_name_attribute (array_die, type_tag (type));
21777       equate_type_number_to_die (type, array_die);
21778       size = int_size_in_bytes (type);
21779       if (size >= 0)
21780 	add_AT_unsigned (array_die, DW_AT_byte_size, size);
21781       /* ???  We can't annotate types late, but for LTO we may not
21782 	 generate a location early either (gfortran.dg/save_6.f90).  */
21783       else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21784 	       && TYPE_DOMAIN (type) != NULL_TREE
21785 	       && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21786 	{
21787 	  tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21788 	  tree rszdecl = szdecl;
21789 
21790 	  size = int_size_in_bytes (TREE_TYPE (szdecl));
21791 	  if (!DECL_P (szdecl))
21792 	    {
21793 	      if (TREE_CODE (szdecl) == INDIRECT_REF
21794 		  && DECL_P (TREE_OPERAND (szdecl, 0)))
21795 		{
21796 		  rszdecl = TREE_OPERAND (szdecl, 0);
21797 		  if (int_size_in_bytes (TREE_TYPE (rszdecl))
21798 		      != DWARF2_ADDR_SIZE)
21799 		    size = 0;
21800 		}
21801 	      else
21802 		size = 0;
21803 	    }
21804 	  if (size > 0)
21805 	    {
21806 	      dw_loc_list_ref loc
21807 		= loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21808 				      NULL);
21809 	      if (loc)
21810 		{
21811 		  add_AT_location_description (array_die, DW_AT_string_length,
21812 					       loc);
21813 		  if (size != DWARF2_ADDR_SIZE)
21814 		    add_AT_unsigned (array_die, dwarf_version >= 5
21815 						? DW_AT_string_length_byte_size
21816 						: DW_AT_byte_size, size);
21817 		}
21818 	    }
21819 	}
21820       return;
21821     }
21822 
21823   array_die = new_die (DW_TAG_array_type, scope_die, type);
21824   add_name_attribute (array_die, type_tag (type));
21825   equate_type_number_to_die (type, array_die);
21826 
21827   if (TREE_CODE (type) == VECTOR_TYPE)
21828     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21829 
21830   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
21831   if (is_fortran ()
21832       && TREE_CODE (type) == ARRAY_TYPE
21833       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21834       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21835     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21836 
21837 #if 0
21838   /* We default the array ordering.  Debuggers will probably do the right
21839      things even if DW_AT_ordering is not present.  It's not even an issue
21840      until we start to get into multidimensional arrays anyway.  If a debugger
21841      is ever caught doing the Wrong Thing for multi-dimensional arrays,
21842      then we'll have to put the DW_AT_ordering attribute back in.  (But if
21843      and when we find out that we need to put these in, we will only do so
21844      for multidimensional arrays.  */
21845   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21846 #endif
21847 
21848   if (TREE_CODE (type) == VECTOR_TYPE)
21849     {
21850       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
21851       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21852       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21853       add_bound_info (subrange_die, DW_AT_upper_bound,
21854 		      size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21855     }
21856   else
21857     add_subscript_info (array_die, type, collapse_nested_arrays);
21858 
21859   /* Add representation of the type of the elements of this array type and
21860      emit the corresponding DIE if we haven't done it already.  */
21861   element_type = TREE_TYPE (type);
21862   if (collapse_nested_arrays)
21863     while (TREE_CODE (element_type) == ARRAY_TYPE)
21864       {
21865 	if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21866 	  break;
21867 	element_type = TREE_TYPE (element_type);
21868       }
21869 
21870   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21871 		      TREE_CODE (type) == ARRAY_TYPE
21872 		      && TYPE_REVERSE_STORAGE_ORDER (type),
21873 		      context_die);
21874 
21875   add_gnat_descriptive_type_attribute (array_die, type, context_die);
21876   if (TYPE_ARTIFICIAL (type))
21877     add_AT_flag (array_die, DW_AT_artificial, 1);
21878 
21879   if (get_AT (array_die, DW_AT_name))
21880     add_pubtype (type, array_die);
21881 
21882   add_alignment_attribute (array_die, type);
21883 }
21884 
21885 /* This routine generates DIE for array with hidden descriptor, details
21886    are filled into *info by a langhook.  */
21887 
21888 static void
gen_descr_array_type_die(tree type,struct array_descr_info * info,dw_die_ref context_die)21889 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21890 			  dw_die_ref context_die)
21891 {
21892   const dw_die_ref scope_die = scope_die_for (type, context_die);
21893   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21894   struct loc_descr_context context = { type, info->base_decl, NULL,
21895 				       false, false };
21896   enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21897   int dim;
21898 
21899   add_name_attribute (array_die, type_tag (type));
21900   equate_type_number_to_die (type, array_die);
21901 
21902   if (info->ndimensions > 1)
21903     switch (info->ordering)
21904       {
21905       case array_descr_ordering_row_major:
21906 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21907 	break;
21908       case array_descr_ordering_column_major:
21909 	add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21910 	break;
21911       default:
21912 	break;
21913       }
21914 
21915   if (dwarf_version >= 3 || !dwarf_strict)
21916     {
21917       if (info->data_location)
21918 	add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21919 			 dw_scalar_form_exprloc, &context);
21920       if (info->associated)
21921 	add_scalar_info (array_die, DW_AT_associated, info->associated,
21922 			 dw_scalar_form_constant
21923 			 | dw_scalar_form_exprloc
21924 			 | dw_scalar_form_reference, &context);
21925       if (info->allocated)
21926 	add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21927 			 dw_scalar_form_constant
21928 			 | dw_scalar_form_exprloc
21929 			 | dw_scalar_form_reference, &context);
21930       if (info->stride)
21931 	{
21932 	  const enum dwarf_attribute attr
21933 	    = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21934 	  const int forms
21935 	    = (info->stride_in_bits)
21936 	      ? dw_scalar_form_constant
21937 	      : (dw_scalar_form_constant
21938 		 | dw_scalar_form_exprloc
21939 		 | dw_scalar_form_reference);
21940 
21941 	  add_scalar_info (array_die, attr, info->stride, forms, &context);
21942 	}
21943     }
21944   if (dwarf_version >= 5)
21945     {
21946       if (info->rank)
21947 	{
21948 	  add_scalar_info (array_die, DW_AT_rank, info->rank,
21949 			   dw_scalar_form_constant
21950 			   | dw_scalar_form_exprloc, &context);
21951 	  subrange_tag = DW_TAG_generic_subrange;
21952 	  context.placeholder_arg = true;
21953 	}
21954     }
21955 
21956   add_gnat_descriptive_type_attribute (array_die, type, context_die);
21957 
21958   for (dim = 0; dim < info->ndimensions; dim++)
21959     {
21960       dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21961 
21962       if (info->dimen[dim].bounds_type)
21963 	add_type_attribute (subrange_die,
21964 			    info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21965 			    false, context_die);
21966       if (info->dimen[dim].lower_bound)
21967 	add_bound_info (subrange_die, DW_AT_lower_bound,
21968 			info->dimen[dim].lower_bound, &context);
21969       if (info->dimen[dim].upper_bound)
21970 	add_bound_info (subrange_die, DW_AT_upper_bound,
21971 			info->dimen[dim].upper_bound, &context);
21972       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21973 	add_scalar_info (subrange_die, DW_AT_byte_stride,
21974 			 info->dimen[dim].stride,
21975 			 dw_scalar_form_constant
21976 			 | dw_scalar_form_exprloc
21977 			 | dw_scalar_form_reference,
21978 			 &context);
21979     }
21980 
21981   gen_type_die (info->element_type, context_die);
21982   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21983 		      TREE_CODE (type) == ARRAY_TYPE
21984 		      && TYPE_REVERSE_STORAGE_ORDER (type),
21985 		      context_die);
21986 
21987   if (get_AT (array_die, DW_AT_name))
21988     add_pubtype (type, array_die);
21989 
21990   add_alignment_attribute (array_die, type);
21991 }
21992 
21993 #if 0
21994 static void
21995 gen_entry_point_die (tree decl, dw_die_ref context_die)
21996 {
21997   tree origin = decl_ultimate_origin (decl);
21998   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21999 
22000   if (origin != NULL)
22001     add_abstract_origin_attribute (decl_die, origin);
22002   else
22003     {
22004       add_name_and_src_coords_attributes (decl_die, decl);
22005       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22006 			  TYPE_UNQUALIFIED, false, context_die);
22007     }
22008 
22009   if (DECL_ABSTRACT_P (decl))
22010     equate_decl_number_to_die (decl, decl_die);
22011   else
22012     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22013 }
22014 #endif
22015 
22016 /* Walk through the list of incomplete types again, trying once more to
22017    emit full debugging info for them.  */
22018 
22019 static void
retry_incomplete_types(void)22020 retry_incomplete_types (void)
22021 {
22022   set_early_dwarf s;
22023   int i;
22024 
22025   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22026     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22027       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22028   vec_safe_truncate (incomplete_types, 0);
22029 }
22030 
22031 /* Determine what tag to use for a record type.  */
22032 
22033 static enum dwarf_tag
record_type_tag(tree type)22034 record_type_tag (tree type)
22035 {
22036   if (! lang_hooks.types.classify_record)
22037     return DW_TAG_structure_type;
22038 
22039   switch (lang_hooks.types.classify_record (type))
22040     {
22041     case RECORD_IS_STRUCT:
22042       return DW_TAG_structure_type;
22043 
22044     case RECORD_IS_CLASS:
22045       return DW_TAG_class_type;
22046 
22047     case RECORD_IS_INTERFACE:
22048       if (dwarf_version >= 3 || !dwarf_strict)
22049 	return DW_TAG_interface_type;
22050       return DW_TAG_structure_type;
22051 
22052     default:
22053       gcc_unreachable ();
22054     }
22055 }
22056 
22057 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
22058    include all of the information about the enumeration values also. Each
22059    enumerated type name/value is listed as a child of the enumerated type
22060    DIE.  */
22061 
22062 static dw_die_ref
gen_enumeration_type_die(tree type,dw_die_ref context_die)22063 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22064 {
22065   dw_die_ref type_die = lookup_type_die (type);
22066   dw_die_ref orig_type_die = type_die;
22067 
22068   if (type_die == NULL)
22069     {
22070       type_die = new_die (DW_TAG_enumeration_type,
22071 			  scope_die_for (type, context_die), type);
22072       equate_type_number_to_die (type, type_die);
22073       add_name_attribute (type_die, type_tag (type));
22074       if ((dwarf_version >= 4 || !dwarf_strict)
22075 	  && ENUM_IS_SCOPED (type))
22076 	add_AT_flag (type_die, DW_AT_enum_class, 1);
22077       if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22078 	add_AT_flag (type_die, DW_AT_declaration, 1);
22079       if (!dwarf_strict)
22080 	add_AT_unsigned (type_die, DW_AT_encoding,
22081 			 TYPE_UNSIGNED (type)
22082 			 ? DW_ATE_unsigned
22083 			 : DW_ATE_signed);
22084     }
22085   else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22086     return type_die;
22087   else
22088     remove_AT (type_die, DW_AT_declaration);
22089 
22090   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
22091      given enum type is incomplete, do not generate the DW_AT_byte_size
22092      attribute or the DW_AT_element_list attribute.  */
22093   if (TYPE_SIZE (type))
22094     {
22095       tree link;
22096 
22097       if (!ENUM_IS_OPAQUE (type))
22098 	TREE_ASM_WRITTEN (type) = 1;
22099       if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22100 	add_byte_size_attribute (type_die, type);
22101       if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22102 	add_alignment_attribute (type_die, type);
22103       if ((dwarf_version >= 3 || !dwarf_strict)
22104 	  && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22105 	{
22106 	  tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22107 	  add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22108 			      context_die);
22109 	}
22110       if (TYPE_STUB_DECL (type) != NULL_TREE)
22111 	{
22112 	  if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22113 	    add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22114 	  if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22115 	    add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22116 	}
22117 
22118       /* If the first reference to this type was as the return type of an
22119 	 inline function, then it may not have a parent.  Fix this now.  */
22120       if (type_die->die_parent == NULL)
22121 	add_child_die (scope_die_for (type, context_die), type_die);
22122 
22123       for (link = TYPE_VALUES (type);
22124 	   link != NULL; link = TREE_CHAIN (link))
22125 	{
22126 	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22127 	  tree value = TREE_VALUE (link);
22128 
22129 	  gcc_assert (!ENUM_IS_OPAQUE (type));
22130 	  add_name_attribute (enum_die,
22131 			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22132 
22133 	  if (TREE_CODE (value) == CONST_DECL)
22134 	    value = DECL_INITIAL (value);
22135 
22136 	  if (simple_type_size_in_bits (TREE_TYPE (value))
22137 	      <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22138 	    {
22139 	      /* For constant forms created by add_AT_unsigned DWARF
22140 		 consumers (GDB, elfutils, etc.) always zero extend
22141 		 the value.  Only when the actual value is negative
22142 		 do we need to use add_AT_int to generate a constant
22143 		 form that can represent negative values.  */
22144 	      HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22145 	      if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22146 		add_AT_unsigned (enum_die, DW_AT_const_value,
22147 				 (unsigned HOST_WIDE_INT) val);
22148 	      else
22149 		add_AT_int (enum_die, DW_AT_const_value, val);
22150 	    }
22151 	  else
22152 	    /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
22153 	       that here.  TODO: This should be re-worked to use correct
22154 	       signed/unsigned double tags for all cases.  */
22155 	    add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22156 	}
22157 
22158       add_gnat_descriptive_type_attribute (type_die, type, context_die);
22159       if (TYPE_ARTIFICIAL (type)
22160 	  && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22161 	add_AT_flag (type_die, DW_AT_artificial, 1);
22162     }
22163   else
22164     add_AT_flag (type_die, DW_AT_declaration, 1);
22165 
22166   add_pubtype (type, type_die);
22167 
22168   return type_die;
22169 }
22170 
22171 /* Generate a DIE to represent either a real live formal parameter decl or to
22172    represent just the type of some formal parameter position in some function
22173    type.
22174 
22175    Note that this routine is a bit unusual because its argument may be a
22176    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22177    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22178    node.  If it's the former then this function is being called to output a
22179    DIE to represent a formal parameter object (or some inlining thereof).  If
22180    it's the latter, then this function is only being called to output a
22181    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22182    argument type of some subprogram type.
22183    If EMIT_NAME_P is true, name and source coordinate attributes
22184    are emitted.  */
22185 
22186 static dw_die_ref
gen_formal_parameter_die(tree node,tree origin,bool emit_name_p,dw_die_ref context_die)22187 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22188 			  dw_die_ref context_die)
22189 {
22190   tree node_or_origin = node ? node : origin;
22191   tree ultimate_origin;
22192   dw_die_ref parm_die = NULL;
22193 
22194   if (DECL_P (node_or_origin))
22195     {
22196       parm_die = lookup_decl_die (node);
22197 
22198       /* If the contexts differ, we may not be talking about the same
22199 	 thing.
22200 	 ???  When in LTO the DIE parent is the "abstract" copy and the
22201 	 context_die is the specification "copy".  */
22202       if (parm_die
22203 	  && parm_die->die_parent != context_die
22204 	  && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22205 	      || parm_die->die_parent->die_parent != context_die)
22206 	  && !in_lto_p)
22207 	{
22208 	  gcc_assert (!DECL_ABSTRACT_P (node));
22209 	  /* This can happen when creating a concrete instance, in
22210 	     which case we need to create a new DIE that will get
22211 	     annotated with DW_AT_abstract_origin.  */
22212 	  parm_die = NULL;
22213 	}
22214 
22215       if (parm_die && parm_die->die_parent == NULL)
22216 	{
22217 	  /* Check that parm_die already has the right attributes that
22218 	     we would have added below.  If any attributes are
22219 	     missing, fall through to add them.  */
22220 	  if (! DECL_ABSTRACT_P (node_or_origin)
22221 	      && !get_AT (parm_die, DW_AT_location)
22222 	      && !get_AT (parm_die, DW_AT_const_value))
22223 	    /* We are missing  location info, and are about to add it.  */
22224 	    ;
22225 	  else
22226 	    {
22227 	      add_child_die (context_die, parm_die);
22228 	      return parm_die;
22229 	    }
22230 	}
22231     }
22232 
22233   /* If we have a previously generated DIE, use it, unless this is an
22234      concrete instance (origin != NULL), in which case we need a new
22235      DIE with a corresponding DW_AT_abstract_origin.  */
22236   bool reusing_die;
22237   if (parm_die && origin == NULL)
22238     reusing_die = true;
22239   else
22240     {
22241       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22242       reusing_die = false;
22243     }
22244 
22245   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22246     {
22247     case tcc_declaration:
22248       ultimate_origin = decl_ultimate_origin (node_or_origin);
22249       if (node || ultimate_origin)
22250 	origin = ultimate_origin;
22251 
22252       if (reusing_die)
22253 	goto add_location;
22254 
22255       if (origin != NULL)
22256 	add_abstract_origin_attribute (parm_die, origin);
22257       else if (emit_name_p)
22258 	add_name_and_src_coords_attributes (parm_die, node);
22259       if (origin == NULL
22260 	  || (! DECL_ABSTRACT_P (node_or_origin)
22261 	      && variably_modified_type_p (TREE_TYPE (node_or_origin),
22262 					   decl_function_context
22263 							    (node_or_origin))))
22264 	{
22265 	  tree type = TREE_TYPE (node_or_origin);
22266 	  if (decl_by_reference_p (node_or_origin))
22267 	    add_type_attribute (parm_die, TREE_TYPE (type),
22268 				TYPE_UNQUALIFIED,
22269 				false, context_die);
22270 	  else
22271 	    add_type_attribute (parm_die, type,
22272 				decl_quals (node_or_origin),
22273 				false, context_die);
22274 	}
22275       if (origin == NULL && DECL_ARTIFICIAL (node))
22276 	add_AT_flag (parm_die, DW_AT_artificial, 1);
22277     add_location:
22278       if (node && node != origin)
22279         equate_decl_number_to_die (node, parm_die);
22280       if (! DECL_ABSTRACT_P (node_or_origin))
22281 	add_location_or_const_value_attribute (parm_die, node_or_origin,
22282 					       node == NULL);
22283 
22284       break;
22285 
22286     case tcc_type:
22287       /* We were called with some kind of a ..._TYPE node.  */
22288       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22289 			  context_die);
22290       break;
22291 
22292     default:
22293       gcc_unreachable ();
22294     }
22295 
22296   return parm_die;
22297 }
22298 
22299 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22300    children DW_TAG_formal_parameter DIEs representing the arguments of the
22301    parameter pack.
22302 
22303    PARM_PACK must be a function parameter pack.
22304    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22305    must point to the subsequent arguments of the function PACK_ARG belongs to.
22306    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22307    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22308    following the last one for which a DIE was generated.  */
22309 
22310 static dw_die_ref
gen_formal_parameter_pack_die(tree parm_pack,tree pack_arg,dw_die_ref subr_die,tree * next_arg)22311 gen_formal_parameter_pack_die  (tree parm_pack,
22312 				tree pack_arg,
22313 				dw_die_ref subr_die,
22314 				tree *next_arg)
22315 {
22316   tree arg;
22317   dw_die_ref parm_pack_die;
22318 
22319   gcc_assert (parm_pack
22320 	      && lang_hooks.function_parameter_pack_p (parm_pack)
22321 	      && subr_die);
22322 
22323   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22324   add_src_coords_attributes (parm_pack_die, parm_pack);
22325 
22326   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22327     {
22328       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22329 								 parm_pack))
22330 	break;
22331       gen_formal_parameter_die (arg, NULL,
22332 				false /* Don't emit name attribute.  */,
22333 				parm_pack_die);
22334     }
22335   if (next_arg)
22336     *next_arg = arg;
22337   return parm_pack_die;
22338 }
22339 
22340 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22341    at the end of an (ANSI prototyped) formal parameters list.  */
22342 
22343 static void
gen_unspecified_parameters_die(tree decl_or_type,dw_die_ref context_die)22344 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22345 {
22346   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22347 }
22348 
22349 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22350    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22351    parameters as specified in some function type specification (except for
22352    those which appear as part of a function *definition*).  */
22353 
22354 static void
gen_formal_types_die(tree function_or_method_type,dw_die_ref context_die)22355 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22356 {
22357   tree link;
22358   tree formal_type = NULL;
22359   tree first_parm_type;
22360   tree arg;
22361 
22362   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22363     {
22364       arg = DECL_ARGUMENTS (function_or_method_type);
22365       function_or_method_type = TREE_TYPE (function_or_method_type);
22366     }
22367   else
22368     arg = NULL_TREE;
22369 
22370   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22371 
22372   /* Make our first pass over the list of formal parameter types and output a
22373      DW_TAG_formal_parameter DIE for each one.  */
22374   for (link = first_parm_type; link; )
22375     {
22376       dw_die_ref parm_die;
22377 
22378       formal_type = TREE_VALUE (link);
22379       if (formal_type == void_type_node)
22380 	break;
22381 
22382       /* Output a (nameless) DIE to represent the formal parameter itself.  */
22383       parm_die = gen_formal_parameter_die (formal_type, NULL,
22384 					   true /* Emit name attribute.  */,
22385 					   context_die);
22386       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22387 	  && link == first_parm_type)
22388 	{
22389 	  add_AT_flag (parm_die, DW_AT_artificial, 1);
22390 	  if (dwarf_version >= 3 || !dwarf_strict)
22391 	    add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22392 	}
22393       else if (arg && DECL_ARTIFICIAL (arg))
22394 	add_AT_flag (parm_die, DW_AT_artificial, 1);
22395 
22396       link = TREE_CHAIN (link);
22397       if (arg)
22398 	arg = DECL_CHAIN (arg);
22399     }
22400 
22401   /* If this function type has an ellipsis, add a
22402      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
22403   if (formal_type != void_type_node)
22404     gen_unspecified_parameters_die (function_or_method_type, context_die);
22405 
22406   /* Make our second (and final) pass over the list of formal parameter types
22407      and output DIEs to represent those types (as necessary).  */
22408   for (link = TYPE_ARG_TYPES (function_or_method_type);
22409        link && TREE_VALUE (link);
22410        link = TREE_CHAIN (link))
22411     gen_type_die (TREE_VALUE (link), context_die);
22412 }
22413 
22414 /* We want to generate the DIE for TYPE so that we can generate the
22415    die for MEMBER, which has been defined; we will need to refer back
22416    to the member declaration nested within TYPE.  If we're trying to
22417    generate minimal debug info for TYPE, processing TYPE won't do the
22418    trick; we need to attach the member declaration by hand.  */
22419 
22420 static void
gen_type_die_for_member(tree type,tree member,dw_die_ref context_die)22421 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22422 {
22423   gen_type_die (type, context_die);
22424 
22425   /* If we're trying to avoid duplicate debug info, we may not have
22426      emitted the member decl for this function.  Emit it now.  */
22427   if (TYPE_STUB_DECL (type)
22428       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22429       && ! lookup_decl_die (member))
22430     {
22431       dw_die_ref type_die;
22432       gcc_assert (!decl_ultimate_origin (member));
22433 
22434       type_die = lookup_type_die_strip_naming_typedef (type);
22435       if (TREE_CODE (member) == FUNCTION_DECL)
22436 	gen_subprogram_die (member, type_die);
22437       else if (TREE_CODE (member) == FIELD_DECL)
22438 	{
22439 	  /* Ignore the nameless fields that are used to skip bits but handle
22440 	     C++ anonymous unions and structs.  */
22441 	  if (DECL_NAME (member) != NULL_TREE
22442 	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22443 	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22444 	    {
22445 	      struct vlr_context vlr_ctx = {
22446 		DECL_CONTEXT (member), /* struct_type */
22447 		NULL_TREE /* variant_part_offset */
22448 	      };
22449 	      gen_type_die (member_declared_type (member), type_die);
22450 	      gen_field_die (member, &vlr_ctx, type_die);
22451 	    }
22452 	}
22453       else
22454 	gen_variable_die (member, NULL_TREE, type_die);
22455     }
22456 }
22457 
22458 /* Forward declare these functions, because they are mutually recursive
22459   with their set_block_* pairing functions.  */
22460 static void set_decl_origin_self (tree);
22461 
22462 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22463    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22464    that it points to the node itself, thus indicating that the node is its
22465    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22466    the given node is NULL, recursively descend the decl/block tree which
22467    it is the root of, and for each other ..._DECL or BLOCK node contained
22468    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22469    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22470    values to point to themselves.  */
22471 
22472 static void
set_block_origin_self(tree stmt)22473 set_block_origin_self (tree stmt)
22474 {
22475   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22476     {
22477       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22478 
22479       {
22480 	tree local_decl;
22481 
22482 	for (local_decl = BLOCK_VARS (stmt);
22483 	     local_decl != NULL_TREE;
22484 	     local_decl = DECL_CHAIN (local_decl))
22485 	  /* Do not recurse on nested functions since the inlining status
22486 	     of parent and child can be different as per the DWARF spec.  */
22487 	  if (TREE_CODE (local_decl) != FUNCTION_DECL
22488 	      && !DECL_EXTERNAL (local_decl))
22489 	    set_decl_origin_self (local_decl);
22490       }
22491 
22492       {
22493 	tree subblock;
22494 
22495 	for (subblock = BLOCK_SUBBLOCKS (stmt);
22496 	     subblock != NULL_TREE;
22497 	     subblock = BLOCK_CHAIN (subblock))
22498 	  set_block_origin_self (subblock);	/* Recurse.  */
22499       }
22500     }
22501 }
22502 
22503 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22504    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22505    node to so that it points to the node itself, thus indicating that the
22506    node represents its own (abstract) origin.  Additionally, if the
22507    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22508    the decl/block tree of which the given node is the root of, and for
22509    each other ..._DECL or BLOCK node contained therein whose
22510    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22511    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22512    point to themselves.  */
22513 
22514 static void
set_decl_origin_self(tree decl)22515 set_decl_origin_self (tree decl)
22516 {
22517   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22518     {
22519       DECL_ABSTRACT_ORIGIN (decl) = decl;
22520       if (TREE_CODE (decl) == FUNCTION_DECL)
22521 	{
22522 	  tree arg;
22523 
22524 	  for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22525 	    DECL_ABSTRACT_ORIGIN (arg) = arg;
22526 	  if (DECL_INITIAL (decl) != NULL_TREE
22527 	      && DECL_INITIAL (decl) != error_mark_node)
22528 	    set_block_origin_self (DECL_INITIAL (decl));
22529 	}
22530     }
22531 }
22532 
22533 /* Mark the early DIE for DECL as the abstract instance.  */
22534 
22535 static void
dwarf2out_abstract_function(tree decl)22536 dwarf2out_abstract_function (tree decl)
22537 {
22538   dw_die_ref old_die;
22539 
22540   /* Make sure we have the actual abstract inline, not a clone.  */
22541   decl = DECL_ORIGIN (decl);
22542 
22543   if (DECL_IGNORED_P (decl))
22544     return;
22545 
22546   /* In LTO we're all set.  We already created abstract instances
22547      early and we want to avoid creating a concrete instance of that
22548      if we don't output it.  */
22549   if (in_lto_p)
22550     return;
22551 
22552   old_die = lookup_decl_die (decl);
22553   gcc_assert (old_die != NULL);
22554   if (get_AT (old_die, DW_AT_inline))
22555     /* We've already generated the abstract instance.  */
22556     return;
22557 
22558   /* Go ahead and put DW_AT_inline on the DIE.  */
22559   if (DECL_DECLARED_INLINE_P (decl))
22560     {
22561       if (cgraph_function_possibly_inlined_p (decl))
22562 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22563       else
22564 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22565     }
22566   else
22567     {
22568       if (cgraph_function_possibly_inlined_p (decl))
22569 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22570       else
22571 	add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22572     }
22573 
22574   if (DECL_DECLARED_INLINE_P (decl)
22575       && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22576     add_AT_flag (old_die, DW_AT_artificial, 1);
22577 
22578   set_decl_origin_self (decl);
22579 }
22580 
22581 /* Helper function of premark_used_types() which gets called through
22582    htab_traverse.
22583 
22584    Marks the DIE of a given type in *SLOT as perennial, so it never gets
22585    marked as unused by prune_unused_types.  */
22586 
22587 bool
premark_used_types_helper(tree const & type,void *)22588 premark_used_types_helper (tree const &type, void *)
22589 {
22590   dw_die_ref die;
22591 
22592   die = lookup_type_die (type);
22593   if (die != NULL)
22594     die->die_perennial_p = 1;
22595   return true;
22596 }
22597 
22598 /* Helper function of premark_types_used_by_global_vars which gets called
22599    through htab_traverse.
22600 
22601    Marks the DIE of a given type in *SLOT as perennial, so it never gets
22602    marked as unused by prune_unused_types. The DIE of the type is marked
22603    only if the global variable using the type will actually be emitted.  */
22604 
22605 int
premark_types_used_by_global_vars_helper(types_used_by_vars_entry ** slot,void *)22606 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22607 					  void *)
22608 {
22609   struct types_used_by_vars_entry *entry;
22610   dw_die_ref die;
22611 
22612   entry = (struct types_used_by_vars_entry *) *slot;
22613   gcc_assert (entry->type != NULL
22614 	      && entry->var_decl != NULL);
22615   die = lookup_type_die (entry->type);
22616   if (die)
22617     {
22618       /* Ask cgraph if the global variable really is to be emitted.
22619          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
22620       varpool_node *node = varpool_node::get (entry->var_decl);
22621       if (node && node->definition)
22622 	{
22623 	  die->die_perennial_p = 1;
22624 	  /* Keep the parent DIEs as well.  */
22625 	  while ((die = die->die_parent) && die->die_perennial_p == 0)
22626 	    die->die_perennial_p = 1;
22627 	}
22628     }
22629   return 1;
22630 }
22631 
22632 /* Mark all members of used_types_hash as perennial.  */
22633 
22634 static void
premark_used_types(struct function * fun)22635 premark_used_types (struct function *fun)
22636 {
22637   if (fun && fun->used_types_hash)
22638     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22639 }
22640 
22641 /* Mark all members of types_used_by_vars_entry as perennial.  */
22642 
22643 static void
premark_types_used_by_global_vars(void)22644 premark_types_used_by_global_vars (void)
22645 {
22646   if (types_used_by_vars_hash)
22647     types_used_by_vars_hash
22648       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22649 }
22650 
22651 /* Mark all variables used by the symtab as perennial.  */
22652 
22653 static void
premark_used_variables(void)22654 premark_used_variables (void)
22655 {
22656   /* Mark DIEs in the symtab as used.  */
22657   varpool_node *var;
22658   FOR_EACH_VARIABLE (var)
22659     {
22660       dw_die_ref die = lookup_decl_die (var->decl);
22661       if (die)
22662 	die->die_perennial_p = 1;
22663     }
22664 }
22665 
22666 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22667    for CA_LOC call arg loc node.  */
22668 
22669 static dw_die_ref
gen_call_site_die(tree decl,dw_die_ref subr_die,struct call_arg_loc_node * ca_loc)22670 gen_call_site_die (tree decl, dw_die_ref subr_die,
22671 		   struct call_arg_loc_node *ca_loc)
22672 {
22673   dw_die_ref stmt_die = NULL, die;
22674   tree block = ca_loc->block;
22675 
22676   while (block
22677 	 && block != DECL_INITIAL (decl)
22678 	 && TREE_CODE (block) == BLOCK)
22679     {
22680       stmt_die = lookup_block_die (block);
22681       if (stmt_die)
22682 	break;
22683       block = BLOCK_SUPERCONTEXT (block);
22684     }
22685   if (stmt_die == NULL)
22686     stmt_die = subr_die;
22687   die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22688   add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22689   if (ca_loc->tail_call_p)
22690     add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22691   if (ca_loc->symbol_ref)
22692     {
22693       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22694       if (tdie)
22695 	add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22696       else
22697 	add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22698 		     false);
22699     }
22700   return die;
22701 }
22702 
22703 /* Generate a DIE to represent a declared function (either file-scope or
22704    block-local).  */
22705 
22706 static void
gen_subprogram_die(tree decl,dw_die_ref context_die)22707 gen_subprogram_die (tree decl, dw_die_ref context_die)
22708 {
22709   tree origin = decl_ultimate_origin (decl);
22710   dw_die_ref subr_die;
22711   dw_die_ref old_die = lookup_decl_die (decl);
22712   bool old_die_had_no_children = false;
22713 
22714   /* This function gets called multiple times for different stages of
22715      the debug process.  For example, for func() in this code:
22716 
22717 	namespace S
22718 	{
22719 	  void func() { ... }
22720 	}
22721 
22722      ...we get called 4 times.  Twice in early debug and twice in
22723      late debug:
22724 
22725      Early debug
22726      -----------
22727 
22728        1. Once while generating func() within the namespace.  This is
22729           the declaration.  The declaration bit below is set, as the
22730           context is the namespace.
22731 
22732 	  A new DIE will be generated with DW_AT_declaration set.
22733 
22734        2. Once for func() itself.  This is the specification.  The
22735           declaration bit below is clear as the context is the CU.
22736 
22737 	  We will use the cached DIE from (1) to create a new DIE with
22738 	  DW_AT_specification pointing to the declaration in (1).
22739 
22740      Late debug via rest_of_handle_final()
22741      -------------------------------------
22742 
22743        3. Once generating func() within the namespace.  This is also the
22744           declaration, as in (1), but this time we will early exit below
22745           as we have a cached DIE and a declaration needs no additional
22746           annotations (no locations), as the source declaration line
22747           info is enough.
22748 
22749        4. Once for func() itself.  As in (2), this is the specification,
22750           but this time we will re-use the cached DIE, and just annotate
22751           it with the location information that should now be available.
22752 
22753      For something without namespaces, but with abstract instances, we
22754      are also called a multiple times:
22755 
22756         class Base
22757 	{
22758 	public:
22759 	  Base ();	  // constructor declaration (1)
22760 	};
22761 
22762 	Base::Base () { } // constructor specification (2)
22763 
22764     Early debug
22765     -----------
22766 
22767        1. Once for the Base() constructor by virtue of it being a
22768           member of the Base class.  This is done via
22769           rest_of_type_compilation.
22770 
22771 	  This is a declaration, so a new DIE will be created with
22772 	  DW_AT_declaration.
22773 
22774        2. Once for the Base() constructor definition, but this time
22775           while generating the abstract instance of the base
22776           constructor (__base_ctor) which is being generated via early
22777           debug of reachable functions.
22778 
22779 	  Even though we have a cached version of the declaration (1),
22780 	  we will create a DW_AT_specification of the declaration DIE
22781 	  in (1).
22782 
22783        3. Once for the __base_ctor itself, but this time, we generate
22784           an DW_AT_abstract_origin version of the DW_AT_specification in
22785 	  (2).
22786 
22787     Late debug via rest_of_handle_final
22788     -----------------------------------
22789 
22790        4. One final time for the __base_ctor (which will have a cached
22791           DIE with DW_AT_abstract_origin created in (3).  This time,
22792           we will just annotate the location information now
22793           available.
22794   */
22795   int declaration = (current_function_decl != decl
22796 		     || (!DECL_INITIAL (decl) && !origin)
22797 		     || class_or_namespace_scope_p (context_die));
22798 
22799   /* A declaration that has been previously dumped needs no
22800      additional information.  */
22801   if (old_die && declaration)
22802     return;
22803 
22804   if (in_lto_p && old_die && old_die->die_child == NULL)
22805     old_die_had_no_children = true;
22806 
22807   /* Now that the C++ front end lazily declares artificial member fns, we
22808      might need to retrofit the declaration into its class.  */
22809   if (!declaration && !origin && !old_die
22810       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22811       && !class_or_namespace_scope_p (context_die)
22812       && debug_info_level > DINFO_LEVEL_TERSE)
22813     old_die = force_decl_die (decl);
22814 
22815   /* A concrete instance, tag a new DIE with DW_AT_abstract_origin.  */
22816   if (origin != NULL)
22817     {
22818       gcc_assert (!declaration || local_scope_p (context_die));
22819 
22820       /* Fixup die_parent for the abstract instance of a nested
22821 	 inline function.  */
22822       if (old_die && old_die->die_parent == NULL)
22823 	add_child_die (context_die, old_die);
22824 
22825       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22826 	{
22827 	  /* If we have a DW_AT_abstract_origin we have a working
22828 	     cached version.  */
22829 	  subr_die = old_die;
22830 	}
22831       else
22832 	{
22833 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22834 	  add_abstract_origin_attribute (subr_die, origin);
22835 	  /*  This is where the actual code for a cloned function is.
22836 	      Let's emit linkage name attribute for it.  This helps
22837 	      debuggers to e.g, set breakpoints into
22838 	      constructors/destructors when the user asks "break
22839 	      K::K".  */
22840 	  add_linkage_name (subr_die, decl);
22841 	}
22842     }
22843   /* A cached copy, possibly from early dwarf generation.  Reuse as
22844      much as possible.  */
22845   else if (old_die)
22846     {
22847       if (!get_AT_flag (old_die, DW_AT_declaration)
22848 	  /* We can have a normal definition following an inline one in the
22849 	     case of redefinition of GNU C extern inlines.
22850 	     It seems reasonable to use AT_specification in this case.  */
22851 	  && !get_AT (old_die, DW_AT_inline))
22852 	{
22853 	  /* Detect and ignore this case, where we are trying to output
22854 	     something we have already output.  */
22855 	  if (get_AT (old_die, DW_AT_low_pc)
22856 	      || get_AT (old_die, DW_AT_ranges))
22857 	    return;
22858 
22859 	  /* If we have no location information, this must be a
22860 	     partially generated DIE from early dwarf generation.
22861 	     Fall through and generate it.  */
22862 	}
22863 
22864       /* If the definition comes from the same place as the declaration,
22865 	 maybe use the old DIE.  We always want the DIE for this function
22866 	 that has the *_pc attributes to be under comp_unit_die so the
22867 	 debugger can find it.  We also need to do this for abstract
22868 	 instances of inlines, since the spec requires the out-of-line copy
22869 	 to have the same parent.  For local class methods, this doesn't
22870 	 apply; we just use the old DIE.  */
22871       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22872       struct dwarf_file_data * file_index = lookup_filename (s.file);
22873       if (((is_unit_die (old_die->die_parent)
22874 	    /* This condition fixes the inconsistency/ICE with the
22875 	       following Fortran test (or some derivative thereof) while
22876 	       building libgfortran:
22877 
22878 		  module some_m
22879 		  contains
22880 		     logical function funky (FLAG)
22881 		       funky = .true.
22882 		    end function
22883 		  end module
22884 	     */
22885 	    || (old_die->die_parent
22886 		&& old_die->die_parent->die_tag == DW_TAG_module)
22887 	    || local_scope_p (old_die->die_parent)
22888 	    || context_die == NULL)
22889 	   && (DECL_ARTIFICIAL (decl)
22890 	       || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22891 		   && (get_AT_unsigned (old_die, DW_AT_decl_line)
22892 		       == (unsigned) s.line)
22893 		   && (!debug_column_info
22894 		       || s.column == 0
22895 		       || (get_AT_unsigned (old_die, DW_AT_decl_column)
22896 			   == (unsigned) s.column)))))
22897 	  /* With LTO if there's an abstract instance for
22898 	     the old DIE, this is a concrete instance and
22899 	     thus re-use the DIE.  */
22900 	  || get_AT (old_die, DW_AT_abstract_origin))
22901 	{
22902 	  subr_die = old_die;
22903 
22904 	  /* Clear out the declaration attribute, but leave the
22905 	     parameters so they can be augmented with location
22906 	     information later.  Unless this was a declaration, in
22907 	     which case, wipe out the nameless parameters and recreate
22908 	     them further down.  */
22909 	  if (remove_AT (subr_die, DW_AT_declaration))
22910 	    {
22911 
22912 	      remove_AT (subr_die, DW_AT_object_pointer);
22913 	      remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22914 	    }
22915 	}
22916       /* Make a specification pointing to the previously built
22917 	 declaration.  */
22918       else
22919 	{
22920 	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22921 	  add_AT_specification (subr_die, old_die);
22922           add_pubname (decl, subr_die);
22923 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22924 	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
22925 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22926 	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22927 	  if (debug_column_info
22928 	      && s.column
22929 	      && (get_AT_unsigned (old_die, DW_AT_decl_column)
22930 		  != (unsigned) s.column))
22931 	    add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22932 
22933 	  /* If the prototype had an 'auto' or 'decltype(auto)' in
22934 	     the return type, emit the real type on the definition die.  */
22935 	  if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22936 	    {
22937 	      dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22938 	      while (die
22939 		     && (die->die_tag == DW_TAG_reference_type
22940 			 || die->die_tag == DW_TAG_rvalue_reference_type
22941 			 || die->die_tag == DW_TAG_pointer_type
22942 			 || die->die_tag == DW_TAG_const_type
22943 			 || die->die_tag == DW_TAG_volatile_type
22944 			 || die->die_tag == DW_TAG_restrict_type
22945 			 || die->die_tag == DW_TAG_array_type
22946 			 || die->die_tag == DW_TAG_ptr_to_member_type
22947 			 || die->die_tag == DW_TAG_subroutine_type))
22948 		die = get_AT_ref (die, DW_AT_type);
22949 	      if (die == auto_die || die == decltype_auto_die)
22950 		add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22951 				    TYPE_UNQUALIFIED, false, context_die);
22952 	    }
22953 
22954 	  /* When we process the method declaration, we haven't seen
22955 	     the out-of-class defaulted definition yet, so we have to
22956 	     recheck now.  */
22957 	  if ((dwarf_version >= 5 || ! dwarf_strict)
22958 	      && !get_AT (subr_die, DW_AT_defaulted))
22959 	    {
22960 	      int defaulted
22961 		= lang_hooks.decls.decl_dwarf_attribute (decl,
22962 							 DW_AT_defaulted);
22963 	      if (defaulted != -1)
22964 		{
22965 		  /* Other values must have been handled before.  */
22966 		  gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22967 		  add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22968 		}
22969 	    }
22970 	}
22971     }
22972   /* Create a fresh DIE for anything else.  */
22973   else
22974     {
22975       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22976 
22977       if (TREE_PUBLIC (decl))
22978 	add_AT_flag (subr_die, DW_AT_external, 1);
22979 
22980       add_name_and_src_coords_attributes (subr_die, decl);
22981       add_pubname (decl, subr_die);
22982       if (debug_info_level > DINFO_LEVEL_TERSE)
22983 	{
22984 	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22985 	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22986 			      TYPE_UNQUALIFIED, false, context_die);
22987 	}
22988 
22989       add_pure_or_virtual_attribute (subr_die, decl);
22990       if (DECL_ARTIFICIAL (decl))
22991 	add_AT_flag (subr_die, DW_AT_artificial, 1);
22992 
22993       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22994 	add_AT_flag (subr_die, DW_AT_noreturn, 1);
22995 
22996       add_alignment_attribute (subr_die, decl);
22997 
22998       add_accessibility_attribute (subr_die, decl);
22999     }
23000 
23001   /* Unless we have an existing non-declaration DIE, equate the new
23002      DIE.  */
23003   if (!old_die || is_declaration_die (old_die))
23004     equate_decl_number_to_die (decl, subr_die);
23005 
23006   if (declaration)
23007     {
23008       if (!old_die || !get_AT (old_die, DW_AT_inline))
23009 	{
23010 	  add_AT_flag (subr_die, DW_AT_declaration, 1);
23011 
23012 	  /* If this is an explicit function declaration then generate
23013 	     a DW_AT_explicit attribute.  */
23014 	  if ((dwarf_version >= 3 || !dwarf_strict)
23015 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23016 							DW_AT_explicit) == 1)
23017 	    add_AT_flag (subr_die, DW_AT_explicit, 1);
23018 
23019 	  /* If this is a C++11 deleted special function member then generate
23020 	     a DW_AT_deleted attribute.  */
23021 	  if ((dwarf_version >= 5 || !dwarf_strict)
23022 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23023 							DW_AT_deleted) == 1)
23024 	    add_AT_flag (subr_die, DW_AT_deleted, 1);
23025 
23026 	  /* If this is a C++11 defaulted special function member then
23027 	     generate a DW_AT_defaulted attribute.  */
23028 	  if (dwarf_version >= 5 || !dwarf_strict)
23029 	    {
23030 	      int defaulted
23031 		= lang_hooks.decls.decl_dwarf_attribute (decl,
23032 							 DW_AT_defaulted);
23033 	      if (defaulted != -1)
23034 		add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23035 	    }
23036 
23037 	  /* If this is a C++11 non-static member function with & ref-qualifier
23038 	     then generate a DW_AT_reference attribute.  */
23039 	  if ((dwarf_version >= 5 || !dwarf_strict)
23040 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23041 							DW_AT_reference) == 1)
23042 	    add_AT_flag (subr_die, DW_AT_reference, 1);
23043 
23044 	  /* If this is a C++11 non-static member function with &&
23045 	     ref-qualifier then generate a DW_AT_reference attribute.  */
23046 	  if ((dwarf_version >= 5 || !dwarf_strict)
23047 	      && lang_hooks.decls.decl_dwarf_attribute (decl,
23048 							DW_AT_rvalue_reference)
23049 		 == 1)
23050 	    add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23051 	}
23052     }
23053   /* For non DECL_EXTERNALs, if range information is available, fill
23054      the DIE with it.  */
23055   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23056     {
23057       HOST_WIDE_INT cfa_fb_offset;
23058 
23059       struct function *fun = DECL_STRUCT_FUNCTION (decl);
23060 
23061       if (!crtl->has_bb_partition)
23062 	{
23063 	  dw_fde_ref fde = fun->fde;
23064 	  if (fde->dw_fde_begin)
23065 	    {
23066 	      /* We have already generated the labels.  */
23067              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23068                                  fde->dw_fde_end, false);
23069 	    }
23070 	  else
23071 	    {
23072 	      /* Create start/end labels and add the range.  */
23073 	      char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23074 	      char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23075 	      ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23076 					   current_function_funcdef_no);
23077 	      ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23078 					   current_function_funcdef_no);
23079              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23080                                  false);
23081 	    }
23082 
23083 #if VMS_DEBUGGING_INFO
23084       /* HP OpenVMS Industry Standard 64: DWARF Extensions
23085 	 Section 2.3 Prologue and Epilogue Attributes:
23086 	 When a breakpoint is set on entry to a function, it is generally
23087 	 desirable for execution to be suspended, not on the very first
23088 	 instruction of the function, but rather at a point after the
23089 	 function's frame has been set up, after any language defined local
23090 	 declaration processing has been completed, and before execution of
23091 	 the first statement of the function begins. Debuggers generally
23092 	 cannot properly determine where this point is.  Similarly for a
23093 	 breakpoint set on exit from a function. The prologue and epilogue
23094 	 attributes allow a compiler to communicate the location(s) to use.  */
23095 
23096       {
23097         if (fde->dw_fde_vms_end_prologue)
23098           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23099 	    fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23100 
23101         if (fde->dw_fde_vms_begin_epilogue)
23102           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23103 	    fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23104       }
23105 #endif
23106 
23107 	}
23108       else
23109 	{
23110 	  /* Generate pubnames entries for the split function code ranges.  */
23111 	  dw_fde_ref fde = fun->fde;
23112 
23113 	  if (fde->dw_fde_second_begin)
23114 	    {
23115 	      if (dwarf_version >= 3 || !dwarf_strict)
23116 		{
23117 		  /* We should use ranges for non-contiguous code section
23118 		     addresses.  Use the actual code range for the initial
23119 		     section, since the HOT/COLD labels might precede an
23120 		     alignment offset.  */
23121 		  bool range_list_added = false;
23122 		  add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23123 					fde->dw_fde_end, &range_list_added,
23124 					false);
23125 		  add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23126 					fde->dw_fde_second_end,
23127 					&range_list_added, false);
23128 		  if (range_list_added)
23129 		    add_ranges (NULL);
23130 		}
23131 	      else
23132 		{
23133 		  /* There is no real support in DW2 for this .. so we make
23134 		     a work-around.  First, emit the pub name for the segment
23135 		     containing the function label.  Then make and emit a
23136 		     simplified subprogram DIE for the second segment with the
23137 		     name pre-fixed by __hot/cold_sect_of_.  We use the same
23138 		     linkage name for the second die so that gdb will find both
23139 		     sections when given "b foo".  */
23140 		  const char *name = NULL;
23141 		  tree decl_name = DECL_NAME (decl);
23142 		  dw_die_ref seg_die;
23143 
23144 		  /* Do the 'primary' section.   */
23145 		  add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23146                                       fde->dw_fde_end, false);
23147 
23148 		  /* Build a minimal DIE for the secondary section.  */
23149 		  seg_die = new_die (DW_TAG_subprogram,
23150 				     subr_die->die_parent, decl);
23151 
23152 		  if (TREE_PUBLIC (decl))
23153 		    add_AT_flag (seg_die, DW_AT_external, 1);
23154 
23155 		  if (decl_name != NULL
23156 		      && IDENTIFIER_POINTER (decl_name) != NULL)
23157 		    {
23158 		      name = dwarf2_name (decl, 1);
23159 		      if (! DECL_ARTIFICIAL (decl))
23160 			add_src_coords_attributes (seg_die, decl);
23161 
23162 		      add_linkage_name (seg_die, decl);
23163 		    }
23164 		  gcc_assert (name != NULL);
23165 		  add_pure_or_virtual_attribute (seg_die, decl);
23166 		  if (DECL_ARTIFICIAL (decl))
23167 		    add_AT_flag (seg_die, DW_AT_artificial, 1);
23168 
23169 		  name = concat ("__second_sect_of_", name, NULL);
23170 		  add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23171                                       fde->dw_fde_second_end, false);
23172 		  add_name_attribute (seg_die, name);
23173 		  if (want_pubnames ())
23174 		    add_pubname_string (name, seg_die);
23175 		}
23176 	    }
23177 	  else
23178            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23179                                false);
23180 	}
23181 
23182       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23183 
23184       /* We define the "frame base" as the function's CFA.  This is more
23185 	 convenient for several reasons: (1) It's stable across the prologue
23186 	 and epilogue, which makes it better than just a frame pointer,
23187 	 (2) With dwarf3, there exists a one-byte encoding that allows us
23188 	 to reference the .debug_frame data by proxy, but failing that,
23189 	 (3) We can at least reuse the code inspection and interpretation
23190 	 code that determines the CFA position at various points in the
23191 	 function.  */
23192       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23193 	{
23194 	  dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23195 	  add_AT_loc (subr_die, DW_AT_frame_base, op);
23196 	}
23197       else
23198 	{
23199 	  dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23200 	  if (list->dw_loc_next)
23201 	    add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23202 	  else
23203 	    add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23204 	}
23205 
23206       /* Compute a displacement from the "steady-state frame pointer" to
23207 	 the CFA.  The former is what all stack slots and argument slots
23208 	 will reference in the rtl; the latter is what we've told the
23209 	 debugger about.  We'll need to adjust all frame_base references
23210 	 by this displacement.  */
23211       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23212 
23213       if (fun->static_chain_decl)
23214 	{
23215 	  /* DWARF requires here a location expression that computes the
23216 	     address of the enclosing subprogram's frame base.  The machinery
23217 	     in tree-nested.c is supposed to store this specific address in the
23218 	     last field of the FRAME record.  */
23219 	  const tree frame_type
23220 	    = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23221 	  const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23222 
23223 	  tree fb_expr
23224 	    = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23225 	  fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23226 			    fb_expr, fb_decl, NULL_TREE);
23227 
23228 	  add_AT_location_description (subr_die, DW_AT_static_link,
23229 				       loc_list_from_tree (fb_expr, 0, NULL));
23230 	}
23231 
23232       resolve_variable_values ();
23233     }
23234 
23235   /* Generate child dies for template paramaters.  */
23236   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23237     gen_generic_params_dies (decl);
23238 
23239   /* Now output descriptions of the arguments for this function. This gets
23240      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23241      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23242      `...' at the end of the formal parameter list.  In order to find out if
23243      there was a trailing ellipsis or not, we must instead look at the type
23244      associated with the FUNCTION_DECL.  This will be a node of type
23245      FUNCTION_TYPE. If the chain of type nodes hanging off of this
23246      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23247      an ellipsis at the end.  */
23248 
23249   /* In the case where we are describing a mere function declaration, all we
23250      need to do here (and all we *can* do here) is to describe the *types* of
23251      its formal parameters.  */
23252   if (debug_info_level <= DINFO_LEVEL_TERSE)
23253     ;
23254   else if (declaration)
23255     gen_formal_types_die (decl, subr_die);
23256   else
23257     {
23258       /* Generate DIEs to represent all known formal parameters.  */
23259       tree parm = DECL_ARGUMENTS (decl);
23260       tree generic_decl = early_dwarf
23261 	? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23262       tree generic_decl_parm = generic_decl
23263 				? DECL_ARGUMENTS (generic_decl)
23264 				: NULL;
23265 
23266       /* Now we want to walk the list of parameters of the function and
23267 	 emit their relevant DIEs.
23268 
23269 	 We consider the case of DECL being an instance of a generic function
23270 	 as well as it being a normal function.
23271 
23272 	 If DECL is an instance of a generic function we walk the
23273 	 parameters of the generic function declaration _and_ the parameters of
23274 	 DECL itself. This is useful because we want to emit specific DIEs for
23275 	 function parameter packs and those are declared as part of the
23276 	 generic function declaration. In that particular case,
23277 	 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23278 	 That DIE has children DIEs representing the set of arguments
23279 	 of the pack. Note that the set of pack arguments can be empty.
23280 	 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23281 	 children DIE.
23282 
23283 	 Otherwise, we just consider the parameters of DECL.  */
23284       while (generic_decl_parm || parm)
23285 	{
23286 	  if (generic_decl_parm
23287 	      && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23288 	    gen_formal_parameter_pack_die (generic_decl_parm,
23289 					   parm, subr_die,
23290 					   &parm);
23291 	  else if (parm)
23292 	    {
23293 	      dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23294 
23295 	      if (early_dwarf
23296 		  && parm == DECL_ARGUMENTS (decl)
23297 		  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23298 		  && parm_die
23299 		  && (dwarf_version >= 3 || !dwarf_strict))
23300 		add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23301 
23302 	      parm = DECL_CHAIN (parm);
23303 	    }
23304 
23305 	  if (generic_decl_parm)
23306 	    generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23307 	}
23308 
23309       /* Decide whether we need an unspecified_parameters DIE at the end.
23310 	 There are 2 more cases to do this for: 1) the ansi ... declaration -
23311 	 this is detectable when the end of the arg list is not a
23312 	 void_type_node 2) an unprototyped function declaration (not a
23313 	 definition).  This just means that we have no info about the
23314 	 parameters at all.  */
23315       if (early_dwarf)
23316 	{
23317 	  if (prototype_p (TREE_TYPE (decl)))
23318 	    {
23319 	      /* This is the prototyped case, check for....  */
23320 	      if (stdarg_p (TREE_TYPE (decl)))
23321 		gen_unspecified_parameters_die (decl, subr_die);
23322 	    }
23323 	  else if (DECL_INITIAL (decl) == NULL_TREE)
23324 	    gen_unspecified_parameters_die (decl, subr_die);
23325 	}
23326       else if ((subr_die != old_die || old_die_had_no_children)
23327 	       && prototype_p (TREE_TYPE (decl))
23328 	       && stdarg_p (TREE_TYPE (decl)))
23329 	gen_unspecified_parameters_die (decl, subr_die);
23330     }
23331 
23332   if (subr_die != old_die)
23333     /* Add the calling convention attribute if requested.  */
23334     add_calling_convention_attribute (subr_die, decl);
23335 
23336   /* Output Dwarf info for all of the stuff within the body of the function
23337      (if it has one - it may be just a declaration).
23338 
23339      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23340      a function.  This BLOCK actually represents the outermost binding contour
23341      for the function, i.e. the contour in which the function's formal
23342      parameters and labels get declared. Curiously, it appears that the front
23343      end doesn't actually put the PARM_DECL nodes for the current function onto
23344      the BLOCK_VARS list for this outer scope, but are strung off of the
23345      DECL_ARGUMENTS list for the function instead.
23346 
23347      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23348      the LABEL_DECL nodes for the function however, and we output DWARF info
23349      for those in decls_for_scope.  Just within the `outer_scope' there will be
23350      a BLOCK node representing the function's outermost pair of curly braces,
23351      and any blocks used for the base and member initializers of a C++
23352      constructor function.  */
23353   tree outer_scope = DECL_INITIAL (decl);
23354   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23355     {
23356       int call_site_note_count = 0;
23357       int tail_call_site_note_count = 0;
23358 
23359       /* Emit a DW_TAG_variable DIE for a named return value.  */
23360       if (DECL_NAME (DECL_RESULT (decl)))
23361 	gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23362 
23363       /* The first time through decls_for_scope we will generate the
23364 	 DIEs for the locals.  The second time, we fill in the
23365 	 location info.  */
23366       decls_for_scope (outer_scope, subr_die);
23367 
23368       if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23369 	{
23370 	  struct call_arg_loc_node *ca_loc;
23371 	  for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23372 	    {
23373 	      dw_die_ref die = NULL;
23374 	      rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23375 	      rtx arg, next_arg;
23376 	      tree arg_decl = NULL_TREE;
23377 
23378 	      for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23379 			  ? XEXP (ca_loc->call_arg_loc_note, 0)
23380 			  : NULL_RTX);
23381 		   arg; arg = next_arg)
23382 		{
23383 		  dw_loc_descr_ref reg, val;
23384 		  machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23385 		  dw_die_ref cdie, tdie = NULL;
23386 
23387 		  next_arg = XEXP (arg, 1);
23388 		  if (REG_P (XEXP (XEXP (arg, 0), 0))
23389 		      && next_arg
23390 		      && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23391 		      && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23392 		      && REGNO (XEXP (XEXP (arg, 0), 0))
23393 			 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23394 		    next_arg = XEXP (next_arg, 1);
23395 		  if (mode == VOIDmode)
23396 		    {
23397 		      mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23398 		      if (mode == VOIDmode)
23399 			mode = GET_MODE (XEXP (arg, 0));
23400 		    }
23401 		  if (mode == VOIDmode || mode == BLKmode)
23402 		    continue;
23403 		  /* Get dynamic information about call target only if we
23404 		     have no static information: we cannot generate both
23405 		     DW_AT_call_origin and DW_AT_call_target
23406 		     attributes.  */
23407 		  if (ca_loc->symbol_ref == NULL_RTX)
23408 		    {
23409 		      if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23410 			{
23411 			  tloc = XEXP (XEXP (arg, 0), 1);
23412 			  continue;
23413 			}
23414 		      else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23415 			       && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23416 			{
23417 			  tlocc = XEXP (XEXP (arg, 0), 1);
23418 			  continue;
23419 			}
23420 		    }
23421 		  reg = NULL;
23422 		  if (REG_P (XEXP (XEXP (arg, 0), 0)))
23423 		    reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23424 					      VAR_INIT_STATUS_INITIALIZED);
23425 		  else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23426 		    {
23427 		      rtx mem = XEXP (XEXP (arg, 0), 0);
23428 		      reg = mem_loc_descriptor (XEXP (mem, 0),
23429 						get_address_mode (mem),
23430 						GET_MODE (mem),
23431 						VAR_INIT_STATUS_INITIALIZED);
23432 		    }
23433 		  else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23434 			   == DEBUG_PARAMETER_REF)
23435 		    {
23436 		      tree tdecl
23437 			= DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23438 		      tdie = lookup_decl_die (tdecl);
23439 		      if (tdie == NULL)
23440 			continue;
23441 		      arg_decl = tdecl;
23442 		    }
23443 		  else
23444 		    continue;
23445 		  if (reg == NULL
23446 		      && GET_CODE (XEXP (XEXP (arg, 0), 0))
23447 			 != DEBUG_PARAMETER_REF)
23448 		    continue;
23449 		  val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23450 					    VOIDmode,
23451 					    VAR_INIT_STATUS_INITIALIZED);
23452 		  if (val == NULL)
23453 		    continue;
23454 		  if (die == NULL)
23455 		    die = gen_call_site_die (decl, subr_die, ca_loc);
23456 		  cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23457 				  NULL_TREE);
23458 		  add_desc_attribute (cdie, arg_decl);
23459 		  if (reg != NULL)
23460 		    add_AT_loc (cdie, DW_AT_location, reg);
23461 		  else if (tdie != NULL)
23462 		    add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23463 				    tdie);
23464 		  add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23465 		  if (next_arg != XEXP (arg, 1))
23466 		    {
23467 		      mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23468 		      if (mode == VOIDmode)
23469 			mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23470 		      val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23471 							    0), 1),
23472 						mode, VOIDmode,
23473 						VAR_INIT_STATUS_INITIALIZED);
23474 		      if (val != NULL)
23475 			add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23476 				    val);
23477 		    }
23478 		}
23479 	      if (die == NULL
23480 		  && (ca_loc->symbol_ref || tloc))
23481 		die = gen_call_site_die (decl, subr_die, ca_loc);
23482 	      if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23483 		{
23484 		  dw_loc_descr_ref tval = NULL;
23485 
23486 		  if (tloc != NULL_RTX)
23487 		    tval = mem_loc_descriptor (tloc,
23488 					       GET_MODE (tloc) == VOIDmode
23489 					       ? Pmode : GET_MODE (tloc),
23490 					       VOIDmode,
23491 					       VAR_INIT_STATUS_INITIALIZED);
23492 		  if (tval)
23493 		    add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23494 		  else if (tlocc != NULL_RTX)
23495 		    {
23496 		      tval = mem_loc_descriptor (tlocc,
23497 						 GET_MODE (tlocc) == VOIDmode
23498 						 ? Pmode : GET_MODE (tlocc),
23499 						 VOIDmode,
23500 						 VAR_INIT_STATUS_INITIALIZED);
23501 		      if (tval)
23502 			add_AT_loc (die,
23503 				    dwarf_AT (DW_AT_call_target_clobbered),
23504 				    tval);
23505 		    }
23506 		}
23507 	      if (die != NULL)
23508 		{
23509 		  call_site_note_count++;
23510 		  if (ca_loc->tail_call_p)
23511 		    tail_call_site_note_count++;
23512 		}
23513 	    }
23514 	}
23515       call_arg_locations = NULL;
23516       call_arg_loc_last = NULL;
23517       if (tail_call_site_count >= 0
23518 	  && tail_call_site_count == tail_call_site_note_count
23519 	  && (!dwarf_strict || dwarf_version >= 5))
23520 	{
23521 	  if (call_site_count >= 0
23522 	      && call_site_count == call_site_note_count)
23523 	    add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23524 	  else
23525 	    add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23526 	}
23527       call_site_count = -1;
23528       tail_call_site_count = -1;
23529     }
23530 
23531   /* Mark used types after we have created DIEs for the functions scopes.  */
23532   premark_used_types (DECL_STRUCT_FUNCTION (decl));
23533 }
23534 
23535 /* Returns a hash value for X (which really is a die_struct).  */
23536 
23537 hashval_t
hash(die_struct * d)23538 block_die_hasher::hash (die_struct *d)
23539 {
23540   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23541 }
23542 
23543 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23544    as decl_id and die_parent of die_struct Y.  */
23545 
23546 bool
equal(die_struct * x,die_struct * y)23547 block_die_hasher::equal (die_struct *x, die_struct *y)
23548 {
23549   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23550 }
23551 
23552 /* Hold information about markers for inlined entry points.  */
23553 struct GTY ((for_user)) inline_entry_data
23554 {
23555   /* The block that's the inlined_function_outer_scope for an inlined
23556      function.  */
23557   tree block;
23558 
23559   /* The label at the inlined entry point.  */
23560   const char *label_pfx;
23561   unsigned int label_num;
23562 
23563   /* The view number to be used as the inlined entry point.  */
23564   var_loc_view view;
23565 };
23566 
23567 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23568 {
23569   typedef tree compare_type;
23570   static inline hashval_t hash (const inline_entry_data *);
23571   static inline bool equal (const inline_entry_data *, const_tree);
23572 };
23573 
23574 /* Hash table routines for inline_entry_data.  */
23575 
23576 inline hashval_t
hash(const inline_entry_data * data)23577 inline_entry_data_hasher::hash (const inline_entry_data *data)
23578 {
23579   return htab_hash_pointer (data->block);
23580 }
23581 
23582 inline bool
equal(const inline_entry_data * data,const_tree block)23583 inline_entry_data_hasher::equal (const inline_entry_data *data,
23584 				 const_tree block)
23585 {
23586   return data->block == block;
23587 }
23588 
23589 /* Inlined entry points pending DIE creation in this compilation unit.  */
23590 
23591 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23592 
23593 
23594 /* Return TRUE if DECL, which may have been previously generated as
23595    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
23596    true if decl (or its origin) is either an extern declaration or a
23597    class/namespace scoped declaration.
23598 
23599    The declare_in_namespace support causes us to get two DIEs for one
23600    variable, both of which are declarations.  We want to avoid
23601    considering one to be a specification, so we must test for
23602    DECLARATION and DW_AT_declaration.  */
23603 static inline bool
decl_will_get_specification_p(dw_die_ref old_die,tree decl,bool declaration)23604 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23605 {
23606   return (old_die && TREE_STATIC (decl) && !declaration
23607 	  && get_AT_flag (old_die, DW_AT_declaration) == 1);
23608 }
23609 
23610 /* Return true if DECL is a local static.  */
23611 
23612 static inline bool
local_function_static(tree decl)23613 local_function_static (tree decl)
23614 {
23615   gcc_assert (VAR_P (decl));
23616   return TREE_STATIC (decl)
23617     && DECL_CONTEXT (decl)
23618     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23619 }
23620 
23621 /* Return true iff DECL overrides (presumably completes) the type of
23622    OLD_DIE within CONTEXT_DIE.  */
23623 
23624 static bool
override_type_for_decl_p(tree decl,dw_die_ref old_die,dw_die_ref context_die)23625 override_type_for_decl_p (tree decl, dw_die_ref old_die,
23626 			  dw_die_ref context_die)
23627 {
23628   tree type = TREE_TYPE (decl);
23629   int cv_quals;
23630 
23631   if (decl_by_reference_p (decl))
23632     {
23633       type = TREE_TYPE (type);
23634       cv_quals = TYPE_UNQUALIFIED;
23635     }
23636   else
23637     cv_quals = decl_quals (decl);
23638 
23639   dw_die_ref type_die = modified_type_die (type,
23640 					   cv_quals | TYPE_QUALS (type),
23641 					   false,
23642 					   context_die);
23643 
23644   dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
23645 
23646   return type_die != old_type_die;
23647 }
23648 
23649 /* Generate a DIE to represent a declared data object.
23650    Either DECL or ORIGIN must be non-null.  */
23651 
23652 static void
gen_variable_die(tree decl,tree origin,dw_die_ref context_die)23653 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23654 {
23655   HOST_WIDE_INT off = 0;
23656   tree com_decl;
23657   tree decl_or_origin = decl ? decl : origin;
23658   tree ultimate_origin;
23659   dw_die_ref var_die;
23660   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23661   bool declaration = (DECL_EXTERNAL (decl_or_origin)
23662 		      || class_or_namespace_scope_p (context_die));
23663   bool specialization_p = false;
23664   bool no_linkage_name = false;
23665 
23666   /* While C++ inline static data members have definitions inside of the
23667      class, force the first DIE to be a declaration, then let gen_member_die
23668      reparent it to the class context and call gen_variable_die again
23669      to create the outside of the class DIE for the definition.  */
23670   if (!declaration
23671       && old_die == NULL
23672       && decl
23673       && DECL_CONTEXT (decl)
23674       && TYPE_P (DECL_CONTEXT (decl))
23675       && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23676     {
23677       declaration = true;
23678       if (dwarf_version < 5)
23679 	no_linkage_name = true;
23680     }
23681 
23682   ultimate_origin = decl_ultimate_origin (decl_or_origin);
23683   if (decl || ultimate_origin)
23684     origin = ultimate_origin;
23685   com_decl = fortran_common (decl_or_origin, &off);
23686 
23687   /* Symbol in common gets emitted as a child of the common block, in the form
23688      of a data member.  */
23689   if (com_decl)
23690     {
23691       dw_die_ref com_die;
23692       dw_loc_list_ref loc = NULL;
23693       die_node com_die_arg;
23694 
23695       var_die = lookup_decl_die (decl_or_origin);
23696       if (var_die)
23697 	{
23698 	  if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23699 	    {
23700 	      loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23701 	      if (loc)
23702 		{
23703 		  if (off)
23704 		    {
23705 		      /* Optimize the common case.  */
23706 		      if (single_element_loc_list_p (loc)
23707 			  && loc->expr->dw_loc_opc == DW_OP_addr
23708 			  && loc->expr->dw_loc_next == NULL
23709 			  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23710 			     == SYMBOL_REF)
23711 			{
23712 			  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23713 			  loc->expr->dw_loc_oprnd1.v.val_addr
23714 			    = plus_constant (GET_MODE (x), x , off);
23715 			}
23716 		      else
23717 			loc_list_plus_const (loc, off);
23718 		    }
23719 		  add_AT_location_description (var_die, DW_AT_location, loc);
23720 		  remove_AT (var_die, DW_AT_declaration);
23721 		}
23722 	    }
23723 	  return;
23724 	}
23725 
23726       if (common_block_die_table == NULL)
23727 	common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23728 
23729       com_die_arg.decl_id = DECL_UID (com_decl);
23730       com_die_arg.die_parent = context_die;
23731       com_die = common_block_die_table->find (&com_die_arg);
23732       if (! early_dwarf)
23733 	loc = loc_list_from_tree (com_decl, 2, NULL);
23734       if (com_die == NULL)
23735 	{
23736 	  const char *cnam
23737 	    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23738 	  die_node **slot;
23739 
23740 	  com_die = new_die (DW_TAG_common_block, context_die, decl);
23741 	  add_name_and_src_coords_attributes (com_die, com_decl);
23742 	  if (loc)
23743 	    {
23744 	      add_AT_location_description (com_die, DW_AT_location, loc);
23745 	      /* Avoid sharing the same loc descriptor between
23746 		 DW_TAG_common_block and DW_TAG_variable.  */
23747 	      loc = loc_list_from_tree (com_decl, 2, NULL);
23748 	    }
23749 	  else if (DECL_EXTERNAL (decl_or_origin))
23750 	    add_AT_flag (com_die, DW_AT_declaration, 1);
23751 	  if (want_pubnames ())
23752 	    add_pubname_string (cnam, com_die); /* ??? needed? */
23753 	  com_die->decl_id = DECL_UID (com_decl);
23754 	  slot = common_block_die_table->find_slot (com_die, INSERT);
23755 	  *slot = com_die;
23756 	}
23757       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23758 	{
23759 	  add_AT_location_description (com_die, DW_AT_location, loc);
23760 	  loc = loc_list_from_tree (com_decl, 2, NULL);
23761 	  remove_AT (com_die, DW_AT_declaration);
23762 	}
23763       var_die = new_die (DW_TAG_variable, com_die, decl);
23764       add_name_and_src_coords_attributes (var_die, decl_or_origin);
23765       add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23766 			  decl_quals (decl_or_origin), false,
23767 			  context_die);
23768       add_alignment_attribute (var_die, decl);
23769       add_AT_flag (var_die, DW_AT_external, 1);
23770       if (loc)
23771 	{
23772 	  if (off)
23773 	    {
23774 	      /* Optimize the common case.  */
23775 	      if (single_element_loc_list_p (loc)
23776                   && loc->expr->dw_loc_opc == DW_OP_addr
23777 		  && loc->expr->dw_loc_next == NULL
23778 		  && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23779 		{
23780 		  rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23781 		  loc->expr->dw_loc_oprnd1.v.val_addr
23782 		    = plus_constant (GET_MODE (x), x, off);
23783 		}
23784 	      else
23785 		loc_list_plus_const (loc, off);
23786 	    }
23787 	  add_AT_location_description (var_die, DW_AT_location, loc);
23788 	}
23789       else if (DECL_EXTERNAL (decl_or_origin))
23790 	add_AT_flag (var_die, DW_AT_declaration, 1);
23791       if (decl)
23792 	equate_decl_number_to_die (decl, var_die);
23793       return;
23794     }
23795 
23796   if (old_die)
23797     {
23798       if (declaration)
23799 	{
23800 	  /* A declaration that has been previously dumped, needs no
23801 	     further annotations, since it doesn't need location on
23802 	     the second pass.  */
23803 	  return;
23804 	}
23805       else if (decl_will_get_specification_p (old_die, decl, declaration)
23806 	       && !get_AT (old_die, DW_AT_specification))
23807 	{
23808 	  /* Fall-thru so we can make a new variable die along with a
23809 	     DW_AT_specification.  */
23810 	}
23811       else if (origin && old_die->die_parent != context_die)
23812 	{
23813 	  /* If we will be creating an inlined instance, we need a
23814 	     new DIE that will get annotated with
23815 	     DW_AT_abstract_origin.  */
23816 	  gcc_assert (!DECL_ABSTRACT_P (decl));
23817 	}
23818       else
23819 	{
23820 	  /* If a DIE was dumped early, it still needs location info.
23821 	     Skip to where we fill the location bits.  */
23822 	  var_die = old_die;
23823 
23824 	  /* ???  In LTRANS we cannot annotate early created variably
23825 	     modified type DIEs without copying them and adjusting all
23826 	     references to them.  Thus we dumped them again.  Also add a
23827 	     reference to them but beware of -g0 compile and -g link
23828 	     in which case the reference will be already present.  */
23829 	  tree type = TREE_TYPE (decl_or_origin);
23830 	  if (in_lto_p
23831 	      && ! get_AT (var_die, DW_AT_type)
23832 	      && variably_modified_type_p
23833 		   (type, decl_function_context (decl_or_origin)))
23834 	    {
23835 	      if (decl_by_reference_p (decl_or_origin))
23836 		add_type_attribute (var_die, TREE_TYPE (type),
23837 				    TYPE_UNQUALIFIED, false, context_die);
23838 	      else
23839 		add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23840 				    false, context_die);
23841 	    }
23842 
23843 	  goto gen_variable_die_location;
23844 	}
23845     }
23846 
23847   /* For static data members, the declaration in the class is supposed
23848      to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23849      also in DWARF2; the specification should still be DW_TAG_variable
23850      referencing the DW_TAG_member DIE.  */
23851   if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23852     var_die = new_die (DW_TAG_member, context_die, decl);
23853   else
23854     var_die = new_die (DW_TAG_variable, context_die, decl);
23855 
23856   if (origin != NULL)
23857     add_abstract_origin_attribute (var_die, origin);
23858 
23859   /* Loop unrolling can create multiple blocks that refer to the same
23860      static variable, so we must test for the DW_AT_declaration flag.
23861 
23862      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23863      copy decls and set the DECL_ABSTRACT_P flag on them instead of
23864      sharing them.
23865 
23866      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
23867   else if (decl_will_get_specification_p (old_die, decl, declaration))
23868     {
23869       /* This is a definition of a C++ class level static.  */
23870       add_AT_specification (var_die, old_die);
23871       specialization_p = true;
23872       if (DECL_NAME (decl))
23873 	{
23874 	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23875 	  struct dwarf_file_data * file_index = lookup_filename (s.file);
23876 
23877 	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23878 	    add_AT_file (var_die, DW_AT_decl_file, file_index);
23879 
23880 	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23881 	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23882 
23883 	  if (debug_column_info
23884 	      && s.column
23885 	      && (get_AT_unsigned (old_die, DW_AT_decl_column)
23886 		  != (unsigned) s.column))
23887 	    add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23888 
23889 	  if (old_die->die_tag == DW_TAG_member)
23890 	    add_linkage_name (var_die, decl);
23891 	}
23892     }
23893   else
23894     add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23895 
23896   if ((origin == NULL && !specialization_p)
23897       || (origin != NULL
23898 	  && !DECL_ABSTRACT_P (decl_or_origin)
23899 	  && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23900 				       decl_function_context
23901 				       (decl_or_origin)))
23902       || (old_die && specialization_p
23903 	  && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
23904     {
23905       tree type = TREE_TYPE (decl_or_origin);
23906 
23907       if (decl_by_reference_p (decl_or_origin))
23908 	add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23909 			    context_die);
23910       else
23911 	add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23912 			    context_die);
23913     }
23914 
23915   if (origin == NULL && !specialization_p)
23916     {
23917       if (TREE_PUBLIC (decl))
23918 	add_AT_flag (var_die, DW_AT_external, 1);
23919 
23920       if (DECL_ARTIFICIAL (decl))
23921 	add_AT_flag (var_die, DW_AT_artificial, 1);
23922 
23923       add_alignment_attribute (var_die, decl);
23924 
23925       add_accessibility_attribute (var_die, decl);
23926     }
23927 
23928   if (declaration)
23929     add_AT_flag (var_die, DW_AT_declaration, 1);
23930 
23931   if (decl && (DECL_ABSTRACT_P (decl)
23932 	       || !old_die || is_declaration_die (old_die)))
23933     equate_decl_number_to_die (decl, var_die);
23934 
23935  gen_variable_die_location:
23936   if (! declaration
23937       && (! DECL_ABSTRACT_P (decl_or_origin)
23938 	  /* Local static vars are shared between all clones/inlines,
23939 	     so emit DW_AT_location on the abstract DIE if DECL_RTL is
23940 	     already set.  */
23941 	  || (VAR_P (decl_or_origin)
23942 	      && TREE_STATIC (decl_or_origin)
23943 	      && DECL_RTL_SET_P (decl_or_origin))))
23944     {
23945       if (early_dwarf)
23946 	add_pubname (decl_or_origin, var_die);
23947       else
23948 	add_location_or_const_value_attribute (var_die, decl_or_origin,
23949 					       decl == NULL);
23950     }
23951   else
23952     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23953 
23954   if ((dwarf_version >= 4 || !dwarf_strict)
23955       && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23956 						DW_AT_const_expr) == 1
23957       && !get_AT (var_die, DW_AT_const_expr)
23958       && !specialization_p)
23959     add_AT_flag (var_die, DW_AT_const_expr, 1);
23960 
23961   if (!dwarf_strict)
23962     {
23963       int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23964 						       DW_AT_inline);
23965       if (inl != -1
23966 	  && !get_AT (var_die, DW_AT_inline)
23967 	  && !specialization_p)
23968 	add_AT_unsigned (var_die, DW_AT_inline, inl);
23969     }
23970 }
23971 
23972 /* Generate a DIE to represent a named constant.  */
23973 
23974 static void
gen_const_die(tree decl,dw_die_ref context_die)23975 gen_const_die (tree decl, dw_die_ref context_die)
23976 {
23977   dw_die_ref const_die;
23978   tree type = TREE_TYPE (decl);
23979 
23980   const_die = lookup_decl_die (decl);
23981   if (const_die)
23982     return;
23983 
23984   const_die = new_die (DW_TAG_constant, context_die, decl);
23985   equate_decl_number_to_die (decl, const_die);
23986   add_name_and_src_coords_attributes (const_die, decl);
23987   add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23988   if (TREE_PUBLIC (decl))
23989     add_AT_flag (const_die, DW_AT_external, 1);
23990   if (DECL_ARTIFICIAL (decl))
23991     add_AT_flag (const_die, DW_AT_artificial, 1);
23992   tree_add_const_value_attribute_for_decl (const_die, decl);
23993 }
23994 
23995 /* Generate a DIE to represent a label identifier.  */
23996 
23997 static void
gen_label_die(tree decl,dw_die_ref context_die)23998 gen_label_die (tree decl, dw_die_ref context_die)
23999 {
24000   tree origin = decl_ultimate_origin (decl);
24001   dw_die_ref lbl_die = lookup_decl_die (decl);
24002   rtx insn;
24003   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24004 
24005   if (!lbl_die)
24006     {
24007       lbl_die = new_die (DW_TAG_label, context_die, decl);
24008       equate_decl_number_to_die (decl, lbl_die);
24009 
24010       if (origin != NULL)
24011 	add_abstract_origin_attribute (lbl_die, origin);
24012       else
24013 	add_name_and_src_coords_attributes (lbl_die, decl);
24014     }
24015 
24016   if (DECL_ABSTRACT_P (decl))
24017     equate_decl_number_to_die (decl, lbl_die);
24018   else if (! early_dwarf)
24019     {
24020       insn = DECL_RTL_IF_SET (decl);
24021 
24022       /* Deleted labels are programmer specified labels which have been
24023 	 eliminated because of various optimizations.  We still emit them
24024 	 here so that it is possible to put breakpoints on them.  */
24025       if (insn
24026 	  && (LABEL_P (insn)
24027 	      || ((NOTE_P (insn)
24028 	           && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24029 	{
24030 	  /* When optimization is enabled (via -O) some parts of the compiler
24031 	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24032 	     represent source-level labels which were explicitly declared by
24033 	     the user.  This really shouldn't be happening though, so catch
24034 	     it if it ever does happen.  */
24035 	  gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24036 
24037 	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24038           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24039 	}
24040       else if (insn
24041 	       && NOTE_P (insn)
24042 	       && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24043 	       && CODE_LABEL_NUMBER (insn) != -1)
24044 	{
24045 	  ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24046           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24047 	}
24048     }
24049 }
24050 
24051 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
24052    attributes to the DIE for a block STMT, to describe where the inlined
24053    function was called from.  This is similar to add_src_coords_attributes.  */
24054 
24055 static inline void
add_call_src_coords_attributes(tree stmt,dw_die_ref die)24056 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24057 {
24058   /* We can end up with BUILTINS_LOCATION here.  */
24059   if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24060     return;
24061 
24062   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24063 
24064   if (dwarf_version >= 3 || !dwarf_strict)
24065     {
24066       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24067       add_AT_unsigned (die, DW_AT_call_line, s.line);
24068       if (debug_column_info && s.column)
24069 	add_AT_unsigned (die, DW_AT_call_column, s.column);
24070     }
24071 }
24072 
24073 
24074 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24075    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
24076 
24077 static inline void
add_high_low_attributes(tree stmt,dw_die_ref die)24078 add_high_low_attributes (tree stmt, dw_die_ref die)
24079 {
24080   char label[MAX_ARTIFICIAL_LABEL_BYTES];
24081 
24082   if (inline_entry_data **iedp
24083       = !inline_entry_data_table ? NULL
24084       : inline_entry_data_table->find_slot_with_hash (stmt,
24085 						      htab_hash_pointer (stmt),
24086 						      NO_INSERT))
24087     {
24088       inline_entry_data *ied = *iedp;
24089       gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24090       gcc_assert (debug_inline_points);
24091       gcc_assert (inlined_function_outer_scope_p (stmt));
24092 
24093       ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24094       add_AT_lbl_id (die, DW_AT_entry_pc, label);
24095 
24096       if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24097 	  && !dwarf_strict)
24098 	{
24099 	  if (!output_asm_line_debug_info ())
24100 	    add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24101 	  else
24102 	    {
24103 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24104 	      /* FIXME: this will resolve to a small number.  Could we
24105 		 possibly emit smaller data?  Ideally we'd emit a
24106 		 uleb128, but that would make the size of DIEs
24107 		 impossible for the compiler to compute, since it's
24108 		 the assembler that computes the value of the view
24109 		 label in this case.  Ideally, we'd have a single form
24110 		 encompassing both the address and the view, and
24111 		 indirecting them through a table might make things
24112 		 easier, but even that would be more wasteful,
24113 		 space-wise, than what we have now.  */
24114 	      add_AT_symview (die, DW_AT_GNU_entry_view, label);
24115 	    }
24116 	}
24117 
24118       inline_entry_data_table->clear_slot (iedp);
24119     }
24120 
24121   if (BLOCK_FRAGMENT_CHAIN (stmt)
24122       && (dwarf_version >= 3 || !dwarf_strict))
24123     {
24124       tree chain, superblock = NULL_TREE;
24125       dw_die_ref pdie;
24126       dw_attr_node *attr = NULL;
24127 
24128       if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24129 	{
24130 	  ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24131 				       BLOCK_NUMBER (stmt));
24132           add_AT_lbl_id (die, DW_AT_entry_pc, label);
24133 	}
24134 
24135       /* Optimize duplicate .debug_ranges lists or even tails of
24136 	 lists.  If this BLOCK has same ranges as its supercontext,
24137 	 lookup DW_AT_ranges attribute in the supercontext (and
24138 	 recursively so), verify that the ranges_table contains the
24139 	 right values and use it instead of adding a new .debug_range.  */
24140       for (chain = stmt, pdie = die;
24141 	   BLOCK_SAME_RANGE (chain);
24142 	   chain = BLOCK_SUPERCONTEXT (chain))
24143 	{
24144 	  dw_attr_node *new_attr;
24145 
24146 	  pdie = pdie->die_parent;
24147 	  if (pdie == NULL)
24148 	    break;
24149 	  if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24150 	    break;
24151 	  new_attr = get_AT (pdie, DW_AT_ranges);
24152 	  if (new_attr == NULL
24153 	      || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24154 	    break;
24155 	  attr = new_attr;
24156 	  superblock = BLOCK_SUPERCONTEXT (chain);
24157 	}
24158       if (attr != NULL
24159 	  && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24160 	      == (int)BLOCK_NUMBER (superblock))
24161 	  && BLOCK_FRAGMENT_CHAIN (superblock))
24162 	{
24163 	  unsigned long off = attr->dw_attr_val.v.val_offset;
24164 	  unsigned long supercnt = 0, thiscnt = 0;
24165 	  for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24166 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24167 	    {
24168 	      ++supercnt;
24169 	      gcc_checking_assert ((*ranges_table)[off + supercnt].num
24170 				   == (int)BLOCK_NUMBER (chain));
24171 	    }
24172 	  gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24173 	  for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24174 	       chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24175 	    ++thiscnt;
24176 	  gcc_assert (supercnt >= thiscnt);
24177 	  add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24178 			     false);
24179 	  note_rnglist_head (off + supercnt - thiscnt);
24180 	  return;
24181 	}
24182 
24183       unsigned int offset = add_ranges (stmt, true);
24184       add_AT_range_list (die, DW_AT_ranges, offset, false);
24185       note_rnglist_head (offset);
24186 
24187       bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24188       chain = BLOCK_FRAGMENT_CHAIN (stmt);
24189       do
24190 	{
24191 	  add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24192 	  prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24193 	  chain = BLOCK_FRAGMENT_CHAIN (chain);
24194 	}
24195       while (chain);
24196       add_ranges (NULL);
24197     }
24198   else
24199     {
24200       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24201       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24202 				   BLOCK_NUMBER (stmt));
24203       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24204 				   BLOCK_NUMBER (stmt));
24205       add_AT_low_high_pc (die, label, label_high, false);
24206     }
24207 }
24208 
24209 /* Generate a DIE for a lexical block.  */
24210 
24211 static void
gen_lexical_block_die(tree stmt,dw_die_ref context_die)24212 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24213 {
24214   dw_die_ref old_die = lookup_block_die (stmt);
24215   dw_die_ref stmt_die = NULL;
24216   if (!old_die)
24217     {
24218       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24219       equate_block_to_die (stmt, stmt_die);
24220     }
24221 
24222   if (BLOCK_ABSTRACT_ORIGIN (stmt))
24223     {
24224       /* If this is an inlined or conrecte instance, create a new lexical
24225 	 die for anything below to attach DW_AT_abstract_origin to.  */
24226       if (old_die)
24227 	stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24228 
24229       tree origin = block_ultimate_origin (stmt);
24230       if (origin != NULL_TREE && (origin != stmt || old_die))
24231 	add_abstract_origin_attribute (stmt_die, origin);
24232 
24233       old_die = NULL;
24234     }
24235 
24236   if (old_die)
24237     stmt_die = old_die;
24238 
24239   /* A non abstract block whose blocks have already been reordered
24240      should have the instruction range for this block.  If so, set the
24241      high/low attributes.  */
24242   if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24243     {
24244       gcc_assert (stmt_die);
24245       add_high_low_attributes (stmt, stmt_die);
24246     }
24247 
24248   decls_for_scope (stmt, stmt_die);
24249 }
24250 
24251 /* Generate a DIE for an inlined subprogram.  */
24252 
24253 static void
gen_inlined_subroutine_die(tree stmt,dw_die_ref context_die)24254 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24255 {
24256   tree decl = block_ultimate_origin (stmt);
24257 
24258   /* Make sure any inlined functions are known to be inlineable.  */
24259   gcc_checking_assert (DECL_ABSTRACT_P (decl)
24260 		       || cgraph_function_possibly_inlined_p (decl));
24261 
24262   dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24263 
24264   if (call_arg_locations || debug_inline_points)
24265     equate_block_to_die (stmt, subr_die);
24266   add_abstract_origin_attribute (subr_die, decl);
24267   if (TREE_ASM_WRITTEN (stmt))
24268     add_high_low_attributes (stmt, subr_die);
24269   add_call_src_coords_attributes (stmt, subr_die);
24270 
24271   /* The inliner creates an extra BLOCK for the parameter setup,
24272      we want to merge that with the actual outermost BLOCK of the
24273      inlined function to avoid duplicate locals in consumers.
24274      Do that by doing the recursion to subblocks on the single subblock
24275      of STMT.  */
24276   bool unwrap_one = false;
24277   if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24278     {
24279       tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24280       if (origin
24281 	  && TREE_CODE (origin) == BLOCK
24282 	  && BLOCK_SUPERCONTEXT (origin) == decl)
24283 	unwrap_one = true;
24284     }
24285   decls_for_scope (stmt, subr_die, !unwrap_one);
24286   if (unwrap_one)
24287     decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24288 }
24289 
24290 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
24291    the comment for VLR_CONTEXT.  */
24292 
24293 static void
gen_field_die(tree decl,struct vlr_context * ctx,dw_die_ref context_die)24294 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24295 {
24296   dw_die_ref decl_die;
24297 
24298   if (TREE_TYPE (decl) == error_mark_node)
24299     return;
24300 
24301   decl_die = new_die (DW_TAG_member, context_die, decl);
24302   add_name_and_src_coords_attributes (decl_die, decl);
24303   add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24304 		      TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24305 		      context_die);
24306 
24307   if (DECL_BIT_FIELD_TYPE (decl))
24308     {
24309       add_byte_size_attribute (decl_die, decl);
24310       add_bit_size_attribute (decl_die, decl);
24311       add_bit_offset_attribute (decl_die, decl, ctx);
24312     }
24313 
24314   add_alignment_attribute (decl_die, decl);
24315 
24316   /* If we have a variant part offset, then we are supposed to process a member
24317      of a QUAL_UNION_TYPE, which is how we represent variant parts in
24318      trees.  */
24319   gcc_assert (ctx->variant_part_offset == NULL_TREE
24320 	      || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24321   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24322     add_data_member_location_attribute (decl_die, decl, ctx);
24323 
24324   if (DECL_ARTIFICIAL (decl))
24325     add_AT_flag (decl_die, DW_AT_artificial, 1);
24326 
24327   add_accessibility_attribute (decl_die, decl);
24328 
24329   /* Equate decl number to die, so that we can look up this decl later on.  */
24330   equate_decl_number_to_die (decl, decl_die);
24331 }
24332 
24333 /* Generate a DIE for a pointer to a member type.  TYPE can be an
24334    OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24335    pointer to member function.  */
24336 
24337 static void
gen_ptr_to_mbr_type_die(tree type,dw_die_ref context_die)24338 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24339 {
24340   if (lookup_type_die (type))
24341     return;
24342 
24343   dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24344 				scope_die_for (type, context_die), type);
24345 
24346   equate_type_number_to_die (type, ptr_die);
24347   add_AT_die_ref (ptr_die, DW_AT_containing_type,
24348 		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24349   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24350 		      context_die);
24351   add_alignment_attribute (ptr_die, type);
24352 
24353   if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24354       && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24355     {
24356       dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24357       add_AT_loc (ptr_die, DW_AT_use_location, op);
24358     }
24359 }
24360 
24361 static char *producer_string;
24362 
24363 /* Return a heap allocated producer string including command line options
24364    if -grecord-gcc-switches.  */
24365 
24366 static char *
gen_producer_string(void)24367 gen_producer_string (void)
24368 {
24369   size_t j;
24370   auto_vec<const char *> switches;
24371   const char *language_string = lang_hooks.name;
24372   char *producer, *tail;
24373   const char *p;
24374   size_t len = dwarf_record_gcc_switches ? 0 : 3;
24375   size_t plen = strlen (language_string) + 1 + strlen (version_string);
24376 
24377   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24378     switch (save_decoded_options[j].opt_index)
24379       {
24380       case OPT_o:
24381       case OPT_d:
24382       case OPT_dumpbase:
24383       case OPT_dumpdir:
24384       case OPT_auxbase:
24385       case OPT_auxbase_strip:
24386       case OPT_quiet:
24387       case OPT_version:
24388       case OPT_v:
24389       case OPT_w:
24390       case OPT_L:
24391       case OPT_D:
24392       case OPT_I:
24393       case OPT_U:
24394       case OPT_SPECIAL_unknown:
24395       case OPT_SPECIAL_ignore:
24396       case OPT_SPECIAL_warn_removed:
24397       case OPT_SPECIAL_program_name:
24398       case OPT_SPECIAL_input_file:
24399       case OPT_grecord_gcc_switches:
24400       case OPT__output_pch_:
24401       case OPT_fdiagnostics_show_location_:
24402       case OPT_fdiagnostics_show_option:
24403       case OPT_fdiagnostics_show_caret:
24404       case OPT_fdiagnostics_show_labels:
24405       case OPT_fdiagnostics_show_line_numbers:
24406       case OPT_fdiagnostics_color_:
24407       case OPT_fdiagnostics_format_:
24408       case OPT_fverbose_asm:
24409       case OPT____:
24410       case OPT__sysroot_:
24411       case OPT_nostdinc:
24412       case OPT_nostdinc__:
24413       case OPT_fpreprocessed:
24414       case OPT_fltrans_output_list_:
24415       case OPT_fresolution_:
24416       case OPT_fdebug_prefix_map_:
24417       case OPT_fmacro_prefix_map_:
24418       case OPT_ffile_prefix_map_:
24419       case OPT_fcompare_debug:
24420       case OPT_fchecking:
24421       case OPT_fchecking_:
24422 	/* Ignore these.  */
24423 	continue;
24424       case OPT_flto_:
24425 	{
24426 	  const char *lto_canonical = "-flto";
24427 	  switches.safe_push (lto_canonical);
24428 	  len += strlen (lto_canonical) + 1;
24429 	  break;
24430 	}
24431       default:
24432         if (cl_options[save_decoded_options[j].opt_index].flags
24433 	    & CL_NO_DWARF_RECORD)
24434 	  continue;
24435         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24436 			     == '-');
24437         switch (save_decoded_options[j].canonical_option[0][1])
24438 	  {
24439 	  case 'M':
24440 	  case 'i':
24441 	  case 'W':
24442 	    continue;
24443 	  case 'f':
24444 	    if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24445 			 "dump", 4) == 0)
24446 	      continue;
24447 	    break;
24448 	  default:
24449 	    break;
24450 	  }
24451 	switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24452 	len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24453 	break;
24454       }
24455 
24456   producer = XNEWVEC (char, plen + 1 + len + 1);
24457   tail = producer;
24458   sprintf (tail, "%s %s", language_string, version_string);
24459   tail += plen;
24460 
24461   FOR_EACH_VEC_ELT (switches, j, p)
24462     {
24463       len = strlen (p);
24464       *tail = ' ';
24465       memcpy (tail + 1, p, len);
24466       tail += len + 1;
24467     }
24468 
24469   *tail = '\0';
24470   return producer;
24471 }
24472 
24473 /* Given a C and/or C++ language/version string return the "highest".
24474    C++ is assumed to be "higher" than C in this case.  Used for merging
24475    LTO translation unit languages.  */
24476 static const char *
highest_c_language(const char * lang1,const char * lang2)24477 highest_c_language (const char *lang1, const char *lang2)
24478 {
24479   if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24480     return "GNU C++17";
24481   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24482     return "GNU C++14";
24483   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24484     return "GNU C++11";
24485   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24486     return "GNU C++98";
24487 
24488   if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24489     return "GNU C2X";
24490   if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24491     return "GNU C17";
24492   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24493     return "GNU C11";
24494   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24495     return "GNU C99";
24496   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24497     return "GNU C89";
24498 
24499   gcc_unreachable ();
24500 }
24501 
24502 
24503 /* Generate the DIE for the compilation unit.  */
24504 
24505 static dw_die_ref
gen_compile_unit_die(const char * filename)24506 gen_compile_unit_die (const char *filename)
24507 {
24508   dw_die_ref die;
24509   const char *language_string = lang_hooks.name;
24510   int language;
24511 
24512   die = new_die (DW_TAG_compile_unit, NULL, NULL);
24513 
24514   if (filename)
24515     {
24516       add_name_attribute (die, filename);
24517       /* Don't add cwd for <built-in>.  */
24518       if (filename[0] != '<')
24519 	add_comp_dir_attribute (die);
24520     }
24521 
24522   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24523 
24524   /* If our producer is LTO try to figure out a common language to use
24525      from the global list of translation units.  */
24526   if (strcmp (language_string, "GNU GIMPLE") == 0)
24527     {
24528       unsigned i;
24529       tree t;
24530       const char *common_lang = NULL;
24531 
24532       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24533 	{
24534 	  if (!TRANSLATION_UNIT_LANGUAGE (t))
24535 	    continue;
24536 	  if (!common_lang)
24537 	    common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24538 	  else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24539 	    ;
24540 	  else if (strncmp (common_lang, "GNU C", 5) == 0
24541 		    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24542 	    /* Mixing C and C++ is ok, use C++ in that case.  */
24543 	    common_lang = highest_c_language (common_lang,
24544 					      TRANSLATION_UNIT_LANGUAGE (t));
24545 	  else
24546 	    {
24547 	      /* Fall back to C.  */
24548 	      common_lang = NULL;
24549 	      break;
24550 	    }
24551 	}
24552 
24553       if (common_lang)
24554 	language_string = common_lang;
24555     }
24556 
24557   language = DW_LANG_C;
24558   if (strncmp (language_string, "GNU C", 5) == 0
24559       && ISDIGIT (language_string[5]))
24560     {
24561       language = DW_LANG_C89;
24562       if (dwarf_version >= 3 || !dwarf_strict)
24563 	{
24564 	  if (strcmp (language_string, "GNU C89") != 0)
24565 	    language = DW_LANG_C99;
24566 
24567 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
24568 	    if (strcmp (language_string, "GNU C11") == 0
24569 		|| strcmp (language_string, "GNU C17") == 0
24570 		|| strcmp (language_string, "GNU C2X"))
24571 	      language = DW_LANG_C11;
24572 	}
24573     }
24574   else if (strncmp (language_string, "GNU C++", 7) == 0)
24575     {
24576       language = DW_LANG_C_plus_plus;
24577       if (dwarf_version >= 5 /* || !dwarf_strict */)
24578 	{
24579 	  if (strcmp (language_string, "GNU C++11") == 0)
24580 	    language = DW_LANG_C_plus_plus_11;
24581 	  else if (strcmp (language_string, "GNU C++14") == 0)
24582 	    language = DW_LANG_C_plus_plus_14;
24583 	  else if (strcmp (language_string, "GNU C++17") == 0)
24584 	    /* For now.  */
24585 	    language = DW_LANG_C_plus_plus_14;
24586 	}
24587     }
24588   else if (strcmp (language_string, "GNU F77") == 0)
24589     language = DW_LANG_Fortran77;
24590   else if (dwarf_version >= 3 || !dwarf_strict)
24591     {
24592       if (strcmp (language_string, "GNU Ada") == 0)
24593 	language = DW_LANG_Ada95;
24594       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24595 	{
24596 	  language = DW_LANG_Fortran95;
24597 	  if (dwarf_version >= 5 /* || !dwarf_strict */)
24598 	    {
24599 	      if (strcmp (language_string, "GNU Fortran2003") == 0)
24600 		language = DW_LANG_Fortran03;
24601 	      else if (strcmp (language_string, "GNU Fortran2008") == 0)
24602 		language = DW_LANG_Fortran08;
24603 	    }
24604 	}
24605       else if (strcmp (language_string, "GNU Objective-C") == 0)
24606 	language = DW_LANG_ObjC;
24607       else if (strcmp (language_string, "GNU Objective-C++") == 0)
24608 	language = DW_LANG_ObjC_plus_plus;
24609       else if (strcmp (language_string, "GNU D") == 0)
24610 	language = DW_LANG_D;
24611       else if (dwarf_version >= 5 || !dwarf_strict)
24612 	{
24613 	  if (strcmp (language_string, "GNU Go") == 0)
24614 	    language = DW_LANG_Go;
24615 	}
24616     }
24617   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
24618   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24619     language = DW_LANG_Fortran90;
24620   /* Likewise for Ada.  */
24621   else if (strcmp (language_string, "GNU Ada") == 0)
24622     language = DW_LANG_Ada83;
24623 
24624   add_AT_unsigned (die, DW_AT_language, language);
24625 
24626   switch (language)
24627     {
24628     case DW_LANG_Fortran77:
24629     case DW_LANG_Fortran90:
24630     case DW_LANG_Fortran95:
24631     case DW_LANG_Fortran03:
24632     case DW_LANG_Fortran08:
24633       /* Fortran has case insensitive identifiers and the front-end
24634 	 lowercases everything.  */
24635       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24636       break;
24637     default:
24638       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
24639       break;
24640     }
24641   return die;
24642 }
24643 
24644 /* Generate the DIE for a base class.  */
24645 
24646 static void
gen_inheritance_die(tree binfo,tree access,tree type,dw_die_ref context_die)24647 gen_inheritance_die (tree binfo, tree access, tree type,
24648 		     dw_die_ref context_die)
24649 {
24650   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24651   struct vlr_context ctx = { type, NULL };
24652 
24653   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24654 		      context_die);
24655   add_data_member_location_attribute (die, binfo, &ctx);
24656 
24657   if (BINFO_VIRTUAL_P (binfo))
24658     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24659 
24660   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24661      children, otherwise the default is DW_ACCESS_public.  In DWARF2
24662      the default has always been DW_ACCESS_private.  */
24663   if (access == access_public_node)
24664     {
24665       if (dwarf_version == 2
24666 	  || context_die->die_tag == DW_TAG_class_type)
24667       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24668     }
24669   else if (access == access_protected_node)
24670     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24671   else if (dwarf_version > 2
24672 	   && context_die->die_tag != DW_TAG_class_type)
24673     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24674 }
24675 
24676 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24677    structure.  */
24678 
24679 static bool
is_variant_part(tree decl)24680 is_variant_part (tree decl)
24681 {
24682   return (TREE_CODE (decl) == FIELD_DECL
24683 	  && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24684 }
24685 
24686 /* Check that OPERAND is a reference to a field in STRUCT_TYPE.  If it is,
24687    return the FIELD_DECL.  Return NULL_TREE otherwise.  */
24688 
24689 static tree
analyze_discr_in_predicate(tree operand,tree struct_type)24690 analyze_discr_in_predicate (tree operand, tree struct_type)
24691 {
24692   while (CONVERT_EXPR_P (operand))
24693     operand = TREE_OPERAND (operand, 0);
24694 
24695   /* Match field access to members of struct_type only.  */
24696   if (TREE_CODE (operand) == COMPONENT_REF
24697       && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24698       && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24699       && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24700     return TREE_OPERAND (operand, 1);
24701   else
24702     return NULL_TREE;
24703 }
24704 
24705 /* Check that SRC is a constant integer that can be represented as a native
24706    integer constant (either signed or unsigned).  If so, store it into DEST and
24707    return true.  Return false otherwise. */
24708 
24709 static bool
get_discr_value(tree src,dw_discr_value * dest)24710 get_discr_value (tree src, dw_discr_value *dest)
24711 {
24712   tree discr_type = TREE_TYPE (src);
24713 
24714   if (lang_hooks.types.get_debug_type)
24715     {
24716       tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24717       if (debug_type != NULL)
24718 	discr_type = debug_type;
24719     }
24720 
24721   if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24722     return false;
24723 
24724   /* Signedness can vary between the original type and the debug type. This
24725      can happen for character types in Ada for instance: the character type
24726      used for code generation can be signed, to be compatible with the C one,
24727      but from a debugger point of view, it must be unsigned.  */
24728   bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24729   bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24730 
24731   if (is_orig_unsigned != is_debug_unsigned)
24732     src = fold_convert (discr_type, src);
24733 
24734   if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24735     return false;
24736 
24737   dest->pos = is_debug_unsigned;
24738   if (is_debug_unsigned)
24739     dest->v.uval = tree_to_uhwi (src);
24740   else
24741     dest->v.sval = tree_to_shwi (src);
24742 
24743   return true;
24744 }
24745 
24746 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24747    FIELD_DECL in STRUCT_TYPE that represents a variant part.  If unsuccessful,
24748    store NULL_TREE in DISCR_DECL.  Otherwise:
24749 
24750      - store the discriminant field in STRUCT_TYPE that controls the variant
24751        part to *DISCR_DECL
24752 
24753      - put in *DISCR_LISTS_P an array where for each variant, the item
24754        represents the corresponding matching list of discriminant values.
24755 
24756      - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24757        the above array.
24758 
24759    Note that when the array is allocated (i.e. when the analysis is
24760    successful), it is up to the caller to free the array.  */
24761 
24762 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)24763 analyze_variants_discr (tree variant_part_decl,
24764 			tree struct_type,
24765 			tree *discr_decl,
24766 			dw_discr_list_ref **discr_lists_p,
24767 			unsigned *discr_lists_length)
24768 {
24769   tree variant_part_type = TREE_TYPE (variant_part_decl);
24770   tree variant;
24771   dw_discr_list_ref *discr_lists;
24772   unsigned i;
24773 
24774   /* Compute how many variants there are in this variant part.  */
24775   *discr_lists_length = 0;
24776   for (variant = TYPE_FIELDS (variant_part_type);
24777        variant != NULL_TREE;
24778        variant = DECL_CHAIN (variant))
24779     ++*discr_lists_length;
24780 
24781   *discr_decl = NULL_TREE;
24782   *discr_lists_p
24783     = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24784 				     sizeof (**discr_lists_p));
24785   discr_lists = *discr_lists_p;
24786 
24787   /* And then analyze all variants to extract discriminant information for all
24788      of them.  This analysis is conservative: as soon as we detect something we
24789      do not support, abort everything and pretend we found nothing.  */
24790   for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24791        variant != NULL_TREE;
24792        variant = DECL_CHAIN (variant), ++i)
24793     {
24794       tree match_expr = DECL_QUALIFIER (variant);
24795 
24796       /* Now, try to analyze the predicate and deduce a discriminant for
24797 	 it.  */
24798       if (match_expr == boolean_true_node)
24799 	/* Typically happens for the default variant: it matches all cases that
24800 	   previous variants rejected.  Don't output any matching value for
24801 	   this one.  */
24802 	continue;
24803 
24804       /* The following loop tries to iterate over each discriminant
24805 	 possibility: single values or ranges.  */
24806       while (match_expr != NULL_TREE)
24807 	{
24808 	  tree next_round_match_expr;
24809 	  tree candidate_discr = NULL_TREE;
24810 	  dw_discr_list_ref new_node = NULL;
24811 
24812 	  /* Possibilities are matched one after the other by nested
24813 	     TRUTH_ORIF_EXPR expressions.  Process the current possibility and
24814 	     continue with the rest at next iteration.  */
24815 	  if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24816 	    {
24817 	      next_round_match_expr = TREE_OPERAND (match_expr, 0);
24818 	      match_expr = TREE_OPERAND (match_expr, 1);
24819 	    }
24820 	  else
24821 	    next_round_match_expr = NULL_TREE;
24822 
24823 	  if (match_expr == boolean_false_node)
24824 	    /* This sub-expression matches nothing: just wait for the next
24825 	       one.  */
24826 	    ;
24827 
24828 	  else if (TREE_CODE (match_expr) == EQ_EXPR)
24829 	    {
24830 	      /* We are matching:  <discr_field> == <integer_cst>
24831 		 This sub-expression matches a single value.  */
24832 	      tree integer_cst = TREE_OPERAND (match_expr, 1);
24833 
24834 	      candidate_discr
24835 	       = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24836 					     struct_type);
24837 
24838 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24839 	      if (!get_discr_value (integer_cst,
24840 				    &new_node->dw_discr_lower_bound))
24841 		goto abort;
24842 	      new_node->dw_discr_range = false;
24843 	    }
24844 
24845 	  else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24846 	    {
24847 	      /* We are matching:
24848 		   <discr_field> > <integer_cst>
24849 		   && <discr_field> < <integer_cst>.
24850 		 This sub-expression matches the range of values between the
24851 		 two matched integer constants.  Note that comparisons can be
24852 		 inclusive or exclusive.  */
24853 	      tree candidate_discr_1, candidate_discr_2;
24854 	      tree lower_cst, upper_cst;
24855 	      bool lower_cst_included, upper_cst_included;
24856 	      tree lower_op = TREE_OPERAND (match_expr, 0);
24857 	      tree upper_op = TREE_OPERAND (match_expr, 1);
24858 
24859 	      /* When the comparison is exclusive, the integer constant is not
24860 		 the discriminant range bound we are looking for: we will have
24861 		 to increment or decrement it.  */
24862 	      if (TREE_CODE (lower_op) == GE_EXPR)
24863 		lower_cst_included = true;
24864 	      else if (TREE_CODE (lower_op) == GT_EXPR)
24865 		lower_cst_included = false;
24866 	      else
24867 		goto abort;
24868 
24869 	      if (TREE_CODE (upper_op) == LE_EXPR)
24870 		upper_cst_included = true;
24871 	      else if (TREE_CODE (upper_op) == LT_EXPR)
24872 		upper_cst_included = false;
24873 	      else
24874 		goto abort;
24875 
24876 	      /* Extract the discriminant from the first operand and check it
24877 		 is consistant with the same analysis in the second
24878 		 operand.  */
24879 	      candidate_discr_1
24880 	        = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24881 					      struct_type);
24882 	      candidate_discr_2
24883 	        = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24884 					      struct_type);
24885 	      if (candidate_discr_1 == candidate_discr_2)
24886 		candidate_discr = candidate_discr_1;
24887 	      else
24888 		goto abort;
24889 
24890 	      /* Extract bounds from both.  */
24891 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24892 	      lower_cst = TREE_OPERAND (lower_op, 1);
24893 	      upper_cst = TREE_OPERAND (upper_op, 1);
24894 
24895 	      if (!lower_cst_included)
24896 		lower_cst
24897 		  = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24898 				 build_int_cst (TREE_TYPE (lower_cst), 1));
24899 	      if (!upper_cst_included)
24900 		upper_cst
24901 		  = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24902 				 build_int_cst (TREE_TYPE (upper_cst), 1));
24903 
24904 	      if (!get_discr_value (lower_cst,
24905 				    &new_node->dw_discr_lower_bound)
24906 		  || !get_discr_value (upper_cst,
24907 				       &new_node->dw_discr_upper_bound))
24908 		goto abort;
24909 
24910 	      new_node->dw_discr_range = true;
24911 	    }
24912 
24913 	  else if ((candidate_discr
24914 		      = analyze_discr_in_predicate (match_expr, struct_type))
24915 		   && TREE_TYPE (candidate_discr) == boolean_type_node)
24916 	    {
24917 	      /* We are matching:  <discr_field> for a boolean discriminant.
24918 		 This sub-expression matches boolean_true_node.  */
24919 	      new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24920 	      if (!get_discr_value (boolean_true_node,
24921 				    &new_node->dw_discr_lower_bound))
24922 		goto abort;
24923 	      new_node->dw_discr_range = false;
24924 	    }
24925 
24926 	  else
24927 	    /* Unsupported sub-expression: we cannot determine the set of
24928 	       matching discriminant values.  Abort everything.  */
24929 	    goto abort;
24930 
24931 	  /* If the discriminant info is not consistant with what we saw so
24932 	     far, consider the analysis failed and abort everything.  */
24933 	  if (candidate_discr == NULL_TREE
24934 	      || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24935 	    goto abort;
24936 	  else
24937 	    *discr_decl = candidate_discr;
24938 
24939 	  if (new_node != NULL)
24940 	    {
24941 	      new_node->dw_discr_next = discr_lists[i];
24942 	      discr_lists[i] = new_node;
24943 	    }
24944 	  match_expr = next_round_match_expr;
24945 	}
24946     }
24947 
24948   /* If we reach this point, we could match everything we were interested
24949      in.  */
24950   return;
24951 
24952 abort:
24953   /* Clean all data structure and return no result.  */
24954   free (*discr_lists_p);
24955   *discr_lists_p = NULL;
24956   *discr_decl = NULL_TREE;
24957 }
24958 
24959 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24960    of STRUCT_TYPE, a record type.  This new DIE is emitted as the next child
24961    under CONTEXT_DIE.
24962 
24963    Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24964    QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter.  The members for
24965    this type, which are record types, represent the available variants and each
24966    has a DECL_QUALIFIER attribute.  The discriminant and the discriminant
24967    values are inferred from these attributes.
24968 
24969    In trees, the offsets for the fields inside these sub-records are relative
24970    to the variant part itself, whereas the corresponding DIEs should have
24971    offset attributes that are relative to the embedding record base address.
24972    This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24973    must be an expression that computes the offset of the variant part to
24974    describe in DWARF.  */
24975 
24976 static void
gen_variant_part(tree variant_part_decl,struct vlr_context * vlr_ctx,dw_die_ref context_die)24977 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24978 		  dw_die_ref context_die)
24979 {
24980   const tree variant_part_type = TREE_TYPE (variant_part_decl);
24981   tree variant_part_offset = vlr_ctx->variant_part_offset;
24982   struct loc_descr_context ctx = {
24983     vlr_ctx->struct_type, /* context_type */
24984     NULL_TREE,		  /* base_decl */
24985     NULL,		  /* dpi */
24986     false,		  /* placeholder_arg */
24987     false		  /* placeholder_seen */
24988   };
24989 
24990   /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24991      NULL_TREE if there is no such field.  */
24992   tree discr_decl = NULL_TREE;
24993   dw_discr_list_ref *discr_lists;
24994   unsigned discr_lists_length = 0;
24995   unsigned i;
24996 
24997   dw_die_ref dwarf_proc_die = NULL;
24998   dw_die_ref variant_part_die
24999     = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25000 
25001   equate_decl_number_to_die (variant_part_decl, variant_part_die);
25002 
25003   analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25004 			  &discr_decl, &discr_lists, &discr_lists_length);
25005 
25006   if (discr_decl != NULL_TREE)
25007     {
25008       dw_die_ref discr_die = lookup_decl_die (discr_decl);
25009 
25010       if (discr_die)
25011 	add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25012       else
25013 	/* We have no DIE for the discriminant, so just discard all
25014 	   discrimimant information in the output.  */
25015 	discr_decl = NULL_TREE;
25016     }
25017 
25018   /* If the offset for this variant part is more complex than a constant,
25019      create a DWARF procedure for it so that we will not have to generate DWARF
25020      expressions for it for each member.  */
25021   if (TREE_CODE (variant_part_offset) != INTEGER_CST
25022       && (dwarf_version >= 3 || !dwarf_strict))
25023     {
25024       const tree dwarf_proc_fndecl
25025         = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25026 		      build_function_type (TREE_TYPE (variant_part_offset),
25027 					   NULL_TREE));
25028       const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25029       const dw_loc_descr_ref dwarf_proc_body
25030         = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25031 
25032       dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25033 					   dwarf_proc_fndecl, context_die);
25034       if (dwarf_proc_die != NULL)
25035 	variant_part_offset = dwarf_proc_call;
25036     }
25037 
25038   /* Output DIEs for all variants.  */
25039   i = 0;
25040   for (tree variant = TYPE_FIELDS (variant_part_type);
25041        variant != NULL_TREE;
25042        variant = DECL_CHAIN (variant), ++i)
25043     {
25044       tree variant_type = TREE_TYPE (variant);
25045       dw_die_ref variant_die;
25046 
25047       /* All variants (i.e. members of a variant part) are supposed to be
25048 	 encoded as structures.  Sub-variant parts are QUAL_UNION_TYPE fields
25049 	 under these records.  */
25050       gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25051 
25052       variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25053       equate_decl_number_to_die (variant, variant_die);
25054 
25055       /* Output discriminant values this variant matches, if any.  */
25056       if (discr_decl == NULL || discr_lists[i] == NULL)
25057 	/* In the case we have discriminant information at all, this is
25058 	   probably the default variant: as the standard says, don't
25059 	   output any discriminant value/list attribute.  */
25060 	;
25061       else if (discr_lists[i]->dw_discr_next == NULL
25062 	       && !discr_lists[i]->dw_discr_range)
25063 	/* If there is only one accepted value, don't bother outputting a
25064 	   list.  */
25065 	add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25066       else
25067 	add_discr_list (variant_die, discr_lists[i]);
25068 
25069       for (tree member = TYPE_FIELDS (variant_type);
25070 	   member != NULL_TREE;
25071 	   member = DECL_CHAIN (member))
25072 	{
25073 	  struct vlr_context vlr_sub_ctx = {
25074 	    vlr_ctx->struct_type, /* struct_type */
25075 	    NULL		  /* variant_part_offset */
25076 	  };
25077 	  if (is_variant_part (member))
25078 	    {
25079 	      /* All offsets for fields inside variant parts are relative to
25080 		 the top-level embedding RECORD_TYPE's base address.  On the
25081 		 other hand, offsets in GCC's types are relative to the
25082 		 nested-most variant part.  So we have to sum offsets each time
25083 		 we recurse.  */
25084 
25085 	      vlr_sub_ctx.variant_part_offset
25086 		= fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25087 			       variant_part_offset, byte_position (member));
25088 	      gen_variant_part (member, &vlr_sub_ctx, variant_die);
25089 	    }
25090 	  else
25091 	    {
25092 	      vlr_sub_ctx.variant_part_offset = variant_part_offset;
25093 	      gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25094 	    }
25095 	}
25096     }
25097 
25098   free (discr_lists);
25099 }
25100 
25101 /* Generate a DIE for a class member.  */
25102 
25103 static void
gen_member_die(tree type,dw_die_ref context_die)25104 gen_member_die (tree type, dw_die_ref context_die)
25105 {
25106   tree member;
25107   tree binfo = TYPE_BINFO (type);
25108 
25109   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25110 
25111   /* If this is not an incomplete type, output descriptions of each of its
25112      members. Note that as we output the DIEs necessary to represent the
25113      members of this record or union type, we will also be trying to output
25114      DIEs to represent the *types* of those members. However the `type'
25115      function (above) will specifically avoid generating type DIEs for member
25116      types *within* the list of member DIEs for this (containing) type except
25117      for those types (of members) which are explicitly marked as also being
25118      members of this (containing) type themselves.  The g++ front- end can
25119      force any given type to be treated as a member of some other (containing)
25120      type by setting the TYPE_CONTEXT of the given (member) type to point to
25121      the TREE node representing the appropriate (containing) type.  */
25122 
25123   /* First output info about the base classes.  */
25124   if (binfo && early_dwarf)
25125     {
25126       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25127       int i;
25128       tree base;
25129 
25130       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25131 	gen_inheritance_die (base,
25132 			     (accesses ? (*accesses)[i] : access_public_node),
25133 			     type,
25134 			     context_die);
25135     }
25136 
25137   /* Now output info about the members. */
25138   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25139     {
25140       /* Ignore clones.  */
25141       if (DECL_ABSTRACT_ORIGIN (member))
25142 	continue;
25143 
25144       struct vlr_context vlr_ctx = { type, NULL_TREE };
25145       bool static_inline_p
25146 	= (VAR_P (member)
25147 	   && TREE_STATIC (member)
25148 	   && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25149 	       != -1));
25150 
25151       /* If we thought we were generating minimal debug info for TYPE
25152 	 and then changed our minds, some of the member declarations
25153 	 may have already been defined.  Don't define them again, but
25154 	 do put them in the right order.  */
25155 
25156       if (dw_die_ref child = lookup_decl_die (member))
25157 	{
25158 	  /* Handle inline static data members, which only have in-class
25159 	     declarations.  */
25160 	  bool splice = true;
25161 
25162 	  dw_die_ref ref = NULL;
25163 	  if (child->die_tag == DW_TAG_variable
25164 	      && child->die_parent == comp_unit_die ())
25165 	    {
25166 	      ref = get_AT_ref (child, DW_AT_specification);
25167 
25168 	      /* For C++17 inline static data members followed by redundant
25169 		 out of class redeclaration, we might get here with
25170 		 child being the DIE created for the out of class
25171 		 redeclaration and with its DW_AT_specification being
25172 		 the DIE created for in-class definition.  We want to
25173 		 reparent the latter, and don't want to create another
25174 		 DIE with DW_AT_specification in that case, because
25175 		 we already have one.  */
25176 	      if (ref
25177 		  && static_inline_p
25178 		  && ref->die_tag == DW_TAG_variable
25179 		  && ref->die_parent == comp_unit_die ()
25180 		  && get_AT (ref, DW_AT_specification) == NULL)
25181 		{
25182 		  child = ref;
25183 		  ref = NULL;
25184 		  static_inline_p = false;
25185 		}
25186 
25187 	      if (!ref)
25188 		{
25189 		  reparent_child (child, context_die);
25190 		  if (dwarf_version < 5)
25191 		    child->die_tag = DW_TAG_member;
25192 		  splice = false;
25193 		}
25194 	    }
25195 
25196 	  if (splice)
25197 	    splice_child_die (context_die, child);
25198 	}
25199 
25200       /* Do not generate standard DWARF for variant parts if we are generating
25201 	 the corresponding GNAT encodings: DIEs generated for both would
25202 	 conflict in our mappings.  */
25203       else if (is_variant_part (member)
25204 	       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25205 	{
25206 	  vlr_ctx.variant_part_offset = byte_position (member);
25207 	  gen_variant_part (member, &vlr_ctx, context_die);
25208 	}
25209       else
25210 	{
25211 	  vlr_ctx.variant_part_offset = NULL_TREE;
25212 	  gen_decl_die (member, NULL, &vlr_ctx, context_die);
25213 	}
25214 
25215       /* For C++ inline static data members emit immediately a DW_TAG_variable
25216 	 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25217 	 DW_AT_specification.  */
25218       if (static_inline_p)
25219 	{
25220 	  int old_extern = DECL_EXTERNAL (member);
25221 	  DECL_EXTERNAL (member) = 0;
25222 	  gen_decl_die (member, NULL, NULL, comp_unit_die ());
25223 	  DECL_EXTERNAL (member) = old_extern;
25224 	}
25225     }
25226 }
25227 
25228 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
25229    is set, we pretend that the type was never defined, so we only get the
25230    member DIEs needed by later specification DIEs.  */
25231 
25232 static void
gen_struct_or_union_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)25233 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25234 				enum debug_info_usage usage)
25235 {
25236   if (TREE_ASM_WRITTEN (type))
25237     {
25238       /* Fill in the bound of variable-length fields in late dwarf if
25239 	 still incomplete.  */
25240       if (!early_dwarf && variably_modified_type_p (type, NULL))
25241 	for (tree member = TYPE_FIELDS (type);
25242 	     member;
25243 	     member = DECL_CHAIN (member))
25244 	  fill_variable_array_bounds (TREE_TYPE (member));
25245       return;
25246     }
25247 
25248   dw_die_ref type_die = lookup_type_die (type);
25249   dw_die_ref scope_die = 0;
25250   int nested = 0;
25251   int complete = (TYPE_SIZE (type)
25252 		  && (! TYPE_STUB_DECL (type)
25253 		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25254   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25255   complete = complete && should_emit_struct_debug (type, usage);
25256 
25257   if (type_die && ! complete)
25258     return;
25259 
25260   if (TYPE_CONTEXT (type) != NULL_TREE
25261       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25262 	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25263     nested = 1;
25264 
25265   scope_die = scope_die_for (type, context_die);
25266 
25267   /* Generate child dies for template paramaters.  */
25268   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25269     schedule_generic_params_dies_gen (type);
25270 
25271   if (! type_die || (nested && is_cu_die (scope_die)))
25272     /* First occurrence of type or toplevel definition of nested class.  */
25273     {
25274       dw_die_ref old_die = type_die;
25275 
25276       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25277 			  ? record_type_tag (type) : DW_TAG_union_type,
25278 			  scope_die, type);
25279       equate_type_number_to_die (type, type_die);
25280       if (old_die)
25281 	add_AT_specification (type_die, old_die);
25282       else
25283 	add_name_attribute (type_die, type_tag (type));
25284     }
25285   else
25286     remove_AT (type_die, DW_AT_declaration);
25287 
25288   /* If this type has been completed, then give it a byte_size attribute and
25289      then give a list of members.  */
25290   if (complete && !ns_decl)
25291     {
25292       /* Prevent infinite recursion in cases where the type of some member of
25293 	 this type is expressed in terms of this type itself.  */
25294       TREE_ASM_WRITTEN (type) = 1;
25295       add_byte_size_attribute (type_die, type);
25296       add_alignment_attribute (type_die, type);
25297       if (TYPE_STUB_DECL (type) != NULL_TREE)
25298 	{
25299 	  add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25300 	  add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25301 	}
25302 
25303       /* If the first reference to this type was as the return type of an
25304 	 inline function, then it may not have a parent.  Fix this now.  */
25305       if (type_die->die_parent == NULL)
25306 	add_child_die (scope_die, type_die);
25307 
25308       gen_member_die (type, type_die);
25309 
25310       add_gnat_descriptive_type_attribute (type_die, type, context_die);
25311       if (TYPE_ARTIFICIAL (type))
25312 	add_AT_flag (type_die, DW_AT_artificial, 1);
25313 
25314       /* GNU extension: Record what type our vtable lives in.  */
25315       if (TYPE_VFIELD (type))
25316 	{
25317 	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25318 
25319 	  gen_type_die (vtype, context_die);
25320 	  add_AT_die_ref (type_die, DW_AT_containing_type,
25321 			  lookup_type_die (vtype));
25322 	}
25323     }
25324   else
25325     {
25326       add_AT_flag (type_die, DW_AT_declaration, 1);
25327 
25328       /* We don't need to do this for function-local types.  */
25329       if (TYPE_STUB_DECL (type)
25330 	  && ! decl_function_context (TYPE_STUB_DECL (type)))
25331 	vec_safe_push (incomplete_types, type);
25332     }
25333 
25334   if (get_AT (type_die, DW_AT_name))
25335     add_pubtype (type, type_die);
25336 }
25337 
25338 /* Generate a DIE for a subroutine _type_.  */
25339 
25340 static void
gen_subroutine_type_die(tree type,dw_die_ref context_die)25341 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25342 {
25343   tree return_type = TREE_TYPE (type);
25344   dw_die_ref subr_die
25345     = new_die (DW_TAG_subroutine_type,
25346 	       scope_die_for (type, context_die), type);
25347 
25348   equate_type_number_to_die (type, subr_die);
25349   add_prototyped_attribute (subr_die, type);
25350   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25351 		      context_die);
25352   add_alignment_attribute (subr_die, type);
25353   gen_formal_types_die (type, subr_die);
25354 
25355   if (get_AT (subr_die, DW_AT_name))
25356     add_pubtype (type, subr_die);
25357   if ((dwarf_version >= 5 || !dwarf_strict)
25358       && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25359     add_AT_flag (subr_die, DW_AT_reference, 1);
25360   if ((dwarf_version >= 5 || !dwarf_strict)
25361       && lang_hooks.types.type_dwarf_attribute (type,
25362 						DW_AT_rvalue_reference) != -1)
25363     add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25364 }
25365 
25366 /* Generate a DIE for a type definition.  */
25367 
25368 static void
gen_typedef_die(tree decl,dw_die_ref context_die)25369 gen_typedef_die (tree decl, dw_die_ref context_die)
25370 {
25371   dw_die_ref type_die;
25372   tree type;
25373 
25374   if (TREE_ASM_WRITTEN (decl))
25375     {
25376       if (DECL_ORIGINAL_TYPE (decl))
25377 	fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25378       return;
25379     }
25380 
25381   /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25382      checks in process_scope_var and modified_type_die), this should be called
25383      only for original types.  */
25384   gcc_assert (decl_ultimate_origin (decl) == NULL
25385 	      || decl_ultimate_origin (decl) == decl);
25386 
25387   TREE_ASM_WRITTEN (decl) = 1;
25388   type_die = new_die (DW_TAG_typedef, context_die, decl);
25389 
25390   add_name_and_src_coords_attributes (type_die, decl);
25391   if (DECL_ORIGINAL_TYPE (decl))
25392     {
25393       type = DECL_ORIGINAL_TYPE (decl);
25394       if (type == error_mark_node)
25395 	return;
25396 
25397       gcc_assert (type != TREE_TYPE (decl));
25398       equate_type_number_to_die (TREE_TYPE (decl), type_die);
25399     }
25400   else
25401     {
25402       type = TREE_TYPE (decl);
25403       if (type == error_mark_node)
25404 	return;
25405 
25406       if (is_naming_typedef_decl (TYPE_NAME (type)))
25407 	{
25408 	  /* Here, we are in the case of decl being a typedef naming
25409 	     an anonymous type, e.g:
25410 		 typedef struct {...} foo;
25411 	     In that case TREE_TYPE (decl) is not a typedef variant
25412 	     type and TYPE_NAME of the anonymous type is set to the
25413 	     TYPE_DECL of the typedef. This construct is emitted by
25414 	     the C++ FE.
25415 
25416 	     TYPE is the anonymous struct named by the typedef
25417 	     DECL. As we need the DW_AT_type attribute of the
25418 	     DW_TAG_typedef to point to the DIE of TYPE, let's
25419 	     generate that DIE right away. add_type_attribute
25420 	     called below will then pick (via lookup_type_die) that
25421 	     anonymous struct DIE.  */
25422 	  if (!TREE_ASM_WRITTEN (type))
25423 	    gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25424 
25425 	  /* This is a GNU Extension.  We are adding a
25426 	     DW_AT_linkage_name attribute to the DIE of the
25427 	     anonymous struct TYPE.  The value of that attribute
25428 	     is the name of the typedef decl naming the anonymous
25429 	     struct.  This greatly eases the work of consumers of
25430 	     this debug info.  */
25431 	  add_linkage_name_raw (lookup_type_die (type), decl);
25432 	}
25433     }
25434 
25435   add_type_attribute (type_die, type, decl_quals (decl), false,
25436 		      context_die);
25437 
25438   if (is_naming_typedef_decl (decl))
25439     /* We want that all subsequent calls to lookup_type_die with
25440        TYPE in argument yield the DW_TAG_typedef we have just
25441        created.  */
25442     equate_type_number_to_die (type, type_die);
25443 
25444   add_alignment_attribute (type_die, TREE_TYPE (decl));
25445 
25446   add_accessibility_attribute (type_die, decl);
25447 
25448   if (DECL_ABSTRACT_P (decl))
25449     equate_decl_number_to_die (decl, type_die);
25450 
25451   if (get_AT (type_die, DW_AT_name))
25452     add_pubtype (decl, type_die);
25453 }
25454 
25455 /* Generate a DIE for a struct, class, enum or union type.  */
25456 
25457 static void
gen_tagged_type_die(tree type,dw_die_ref context_die,enum debug_info_usage usage)25458 gen_tagged_type_die (tree type,
25459 		     dw_die_ref context_die,
25460 		     enum debug_info_usage usage)
25461 {
25462   if (type == NULL_TREE
25463       || !is_tagged_type (type))
25464     return;
25465 
25466   if (TREE_ASM_WRITTEN (type))
25467     ;
25468   /* If this is a nested type whose containing class hasn't been written
25469      out yet, writing it out will cover this one, too.  This does not apply
25470      to instantiations of member class templates; they need to be added to
25471      the containing class as they are generated.  FIXME: This hurts the
25472      idea of combining type decls from multiple TUs, since we can't predict
25473      what set of template instantiations we'll get.  */
25474   else if (TYPE_CONTEXT (type)
25475       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25476       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25477     {
25478       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25479 
25480       if (TREE_ASM_WRITTEN (type))
25481 	return;
25482 
25483       /* If that failed, attach ourselves to the stub.  */
25484       context_die = lookup_type_die (TYPE_CONTEXT (type));
25485     }
25486   else if (TYPE_CONTEXT (type) != NULL_TREE
25487 	   && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25488     {
25489       /* If this type is local to a function that hasn't been written
25490 	 out yet, use a NULL context for now; it will be fixed up in
25491 	 decls_for_scope.  */
25492       context_die = lookup_decl_die (TYPE_CONTEXT (type));
25493       /* A declaration DIE doesn't count; nested types need to go in the
25494 	 specification.  */
25495       if (context_die && is_declaration_die (context_die))
25496 	context_die = NULL;
25497     }
25498   else
25499     context_die = declare_in_namespace (type, context_die);
25500 
25501   if (TREE_CODE (type) == ENUMERAL_TYPE)
25502     {
25503       /* This might have been written out by the call to
25504 	 declare_in_namespace.  */
25505       if (!TREE_ASM_WRITTEN (type))
25506 	gen_enumeration_type_die (type, context_die);
25507     }
25508   else
25509     gen_struct_or_union_type_die (type, context_die, usage);
25510 
25511   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25512      it up if it is ever completed.  gen_*_type_die will set it for us
25513      when appropriate.  */
25514 }
25515 
25516 /* Generate a type description DIE.  */
25517 
25518 static void
gen_type_die_with_usage(tree type,dw_die_ref context_die,enum debug_info_usage usage)25519 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25520 			 enum debug_info_usage usage)
25521 {
25522   struct array_descr_info info;
25523 
25524   if (type == NULL_TREE || type == error_mark_node)
25525     return;
25526 
25527   if (flag_checking && type)
25528      verify_type (type);
25529 
25530   if (TYPE_NAME (type) != NULL_TREE
25531       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25532       && is_redundant_typedef (TYPE_NAME (type))
25533       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25534     /* The DECL of this type is a typedef we don't want to emit debug
25535        info for but we want debug info for its underlying typedef.
25536        This can happen for e.g, the injected-class-name of a C++
25537        type.  */
25538     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25539 
25540   /* If TYPE is a typedef type variant, let's generate debug info
25541      for the parent typedef which TYPE is a type of.  */
25542   if (typedef_variant_p (type))
25543     {
25544       if (TREE_ASM_WRITTEN (type))
25545 	return;
25546 
25547       tree name = TYPE_NAME (type);
25548       tree origin = decl_ultimate_origin (name);
25549       if (origin != NULL && origin != name)
25550 	{
25551 	  gen_decl_die (origin, NULL, NULL, context_die);
25552 	  return;
25553 	}
25554 
25555       /* Prevent broken recursion; we can't hand off to the same type.  */
25556       gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25557 
25558       /* Give typedefs the right scope.  */
25559       context_die = scope_die_for (type, context_die);
25560 
25561       TREE_ASM_WRITTEN (type) = 1;
25562 
25563       gen_decl_die (name, NULL, NULL, context_die);
25564       return;
25565     }
25566 
25567   /* If type is an anonymous tagged type named by a typedef, let's
25568      generate debug info for the typedef.  */
25569   if (is_naming_typedef_decl (TYPE_NAME (type)))
25570     {
25571       /* Give typedefs the right scope.  */
25572       context_die = scope_die_for (type, context_die);
25573 
25574       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25575       return;
25576     }
25577 
25578   if (lang_hooks.types.get_debug_type)
25579     {
25580       tree debug_type = lang_hooks.types.get_debug_type (type);
25581 
25582       if (debug_type != NULL_TREE && debug_type != type)
25583 	{
25584 	  gen_type_die_with_usage (debug_type, context_die, usage);
25585 	  return;
25586 	}
25587     }
25588 
25589   /* We are going to output a DIE to represent the unqualified version
25590      of this type (i.e. without any const or volatile qualifiers) so
25591      get the main variant (i.e. the unqualified version) of this type
25592      now.  (Vectors and arrays are special because the debugging info is in the
25593      cloned type itself.  Similarly function/method types can contain extra
25594      ref-qualification).  */
25595   if (TREE_CODE (type) == FUNCTION_TYPE
25596       || TREE_CODE (type) == METHOD_TYPE)
25597     {
25598       /* For function/method types, can't use type_main_variant here,
25599 	 because that can have different ref-qualifiers for C++,
25600 	 but try to canonicalize.  */
25601       tree main = TYPE_MAIN_VARIANT (type);
25602       for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25603 	if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25604 	    && check_base_type (t, main)
25605 	    && check_lang_type (t, type))
25606 	  {
25607 	    type = t;
25608 	    break;
25609 	  }
25610     }
25611   else if (TREE_CODE (type) != VECTOR_TYPE
25612 	   && TREE_CODE (type) != ARRAY_TYPE)
25613     type = type_main_variant (type);
25614 
25615   /* If this is an array type with hidden descriptor, handle it first.  */
25616   if (!TREE_ASM_WRITTEN (type)
25617       && lang_hooks.types.get_array_descr_info)
25618     {
25619       memset (&info, 0, sizeof (info));
25620       if (lang_hooks.types.get_array_descr_info (type, &info))
25621 	{
25622 	  /* Fortran sometimes emits array types with no dimension.  */
25623 	  gcc_assert (info.ndimensions >= 0
25624 		      && (info.ndimensions
25625 			  <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25626 	  gen_descr_array_type_die (type, &info, context_die);
25627 	  TREE_ASM_WRITTEN (type) = 1;
25628 	  return;
25629 	}
25630     }
25631 
25632   if (TREE_ASM_WRITTEN (type))
25633     {
25634       /* Variable-length types may be incomplete even if
25635 	 TREE_ASM_WRITTEN.  For such types, fall through to
25636 	 gen_array_type_die() and possibly fill in
25637 	 DW_AT_{upper,lower}_bound attributes.  */
25638       if ((TREE_CODE (type) != ARRAY_TYPE
25639 	   && TREE_CODE (type) != RECORD_TYPE
25640 	   && TREE_CODE (type) != UNION_TYPE
25641 	   && TREE_CODE (type) != QUAL_UNION_TYPE)
25642 	  || !variably_modified_type_p (type, NULL))
25643 	return;
25644     }
25645 
25646   switch (TREE_CODE (type))
25647     {
25648     case ERROR_MARK:
25649       break;
25650 
25651     case POINTER_TYPE:
25652     case REFERENCE_TYPE:
25653       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
25654 	 ensures that the gen_type_die recursion will terminate even if the
25655 	 type is recursive.  Recursive types are possible in Ada.  */
25656       /* ??? We could perhaps do this for all types before the switch
25657 	 statement.  */
25658       TREE_ASM_WRITTEN (type) = 1;
25659 
25660       /* For these types, all that is required is that we output a DIE (or a
25661 	 set of DIEs) to represent the "basis" type.  */
25662       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25663 			       DINFO_USAGE_IND_USE);
25664       break;
25665 
25666     case OFFSET_TYPE:
25667       /* This code is used for C++ pointer-to-data-member types.
25668 	 Output a description of the relevant class type.  */
25669       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25670 			       DINFO_USAGE_IND_USE);
25671 
25672       /* Output a description of the type of the object pointed to.  */
25673       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25674 			       DINFO_USAGE_IND_USE);
25675 
25676       /* Now output a DIE to represent this pointer-to-data-member type
25677 	 itself.  */
25678       gen_ptr_to_mbr_type_die (type, context_die);
25679       break;
25680 
25681     case FUNCTION_TYPE:
25682       /* Force out return type (in case it wasn't forced out already).  */
25683       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25684 			       DINFO_USAGE_DIR_USE);
25685       gen_subroutine_type_die (type, context_die);
25686       break;
25687 
25688     case METHOD_TYPE:
25689       /* Force out return type (in case it wasn't forced out already).  */
25690       gen_type_die_with_usage (TREE_TYPE (type), context_die,
25691 			       DINFO_USAGE_DIR_USE);
25692       gen_subroutine_type_die (type, context_die);
25693       break;
25694 
25695     case ARRAY_TYPE:
25696     case VECTOR_TYPE:
25697       gen_array_type_die (type, context_die);
25698       break;
25699 
25700     case ENUMERAL_TYPE:
25701     case RECORD_TYPE:
25702     case UNION_TYPE:
25703     case QUAL_UNION_TYPE:
25704       gen_tagged_type_die (type, context_die, usage);
25705       return;
25706 
25707     case VOID_TYPE:
25708     case INTEGER_TYPE:
25709     case REAL_TYPE:
25710     case FIXED_POINT_TYPE:
25711     case COMPLEX_TYPE:
25712     case BOOLEAN_TYPE:
25713       /* No DIEs needed for fundamental types.  */
25714       break;
25715 
25716     case NULLPTR_TYPE:
25717     case LANG_TYPE:
25718       /* Just use DW_TAG_unspecified_type.  */
25719       {
25720         dw_die_ref type_die = lookup_type_die (type);
25721         if (type_die == NULL)
25722           {
25723 	    tree name = TYPE_IDENTIFIER (type);
25724             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25725 				type);
25726             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25727             equate_type_number_to_die (type, type_die);
25728           }
25729       }
25730       break;
25731 
25732     default:
25733       if (is_cxx_auto (type))
25734 	{
25735 	  tree name = TYPE_IDENTIFIER (type);
25736 	  dw_die_ref *die = (name == get_identifier ("auto")
25737 			     ? &auto_die : &decltype_auto_die);
25738 	  if (!*die)
25739 	    {
25740 	      *die = new_die (DW_TAG_unspecified_type,
25741 			      comp_unit_die (), NULL_TREE);
25742 	      add_name_attribute (*die, IDENTIFIER_POINTER (name));
25743 	    }
25744 	  equate_type_number_to_die (type, *die);
25745 	  break;
25746 	}
25747       gcc_unreachable ();
25748     }
25749 
25750   TREE_ASM_WRITTEN (type) = 1;
25751 }
25752 
25753 static void
gen_type_die(tree type,dw_die_ref context_die)25754 gen_type_die (tree type, dw_die_ref context_die)
25755 {
25756   if (type != error_mark_node)
25757     {
25758       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25759       if (flag_checking)
25760 	{
25761 	  dw_die_ref die = lookup_type_die (type);
25762 	  if (die)
25763 	    check_die (die);
25764 	}
25765     }
25766 }
25767 
25768 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25769    things which are local to the given block.  */
25770 
25771 static void
gen_block_die(tree stmt,dw_die_ref context_die)25772 gen_block_die (tree stmt, dw_die_ref context_die)
25773 {
25774   int must_output_die = 0;
25775   bool inlined_func;
25776 
25777   /* Ignore blocks that are NULL.  */
25778   if (stmt == NULL_TREE)
25779     return;
25780 
25781   inlined_func = inlined_function_outer_scope_p (stmt);
25782 
25783   /* If the block is one fragment of a non-contiguous block, do not
25784      process the variables, since they will have been done by the
25785      origin block.  Do process subblocks.  */
25786   if (BLOCK_FRAGMENT_ORIGIN (stmt))
25787     {
25788       tree sub;
25789 
25790       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25791 	gen_block_die (sub, context_die);
25792 
25793       return;
25794     }
25795 
25796   /* Determine if we need to output any Dwarf DIEs at all to represent this
25797      block.  */
25798   if (inlined_func)
25799     /* The outer scopes for inlinings *must* always be represented.  We
25800        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
25801     must_output_die = 1;
25802   else if (lookup_block_die (stmt))
25803     /* If we already have a DIE then it was filled early.  Meanwhile
25804        we might have pruned all BLOCK_VARS as optimized out but we
25805        still want to generate high/low PC attributes so output it.  */
25806     must_output_die = 1;
25807   else if (TREE_USED (stmt)
25808 	   || TREE_ASM_WRITTEN (stmt))
25809     {
25810       /* Determine if this block directly contains any "significant"
25811 	 local declarations which we will need to output DIEs for.  */
25812       if (debug_info_level > DINFO_LEVEL_TERSE)
25813 	{
25814 	  /* We are not in terse mode so any local declaration that
25815 	     is not ignored for debug purposes counts as being a
25816 	     "significant" one.  */
25817 	  if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25818 	    must_output_die = 1;
25819 	  else
25820 	    for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25821 	      if (!DECL_IGNORED_P (var))
25822 		{
25823 		  must_output_die = 1;
25824 		  break;
25825 		}
25826 	}
25827       else if (!dwarf2out_ignore_block (stmt))
25828 	must_output_die = 1;
25829     }
25830 
25831   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25832      DIE for any block which contains no significant local declarations at
25833      all.  Rather, in such cases we just call `decls_for_scope' so that any
25834      needed Dwarf info for any sub-blocks will get properly generated. Note
25835      that in terse mode, our definition of what constitutes a "significant"
25836      local declaration gets restricted to include only inlined function
25837      instances and local (nested) function definitions.  */
25838   if (must_output_die)
25839     {
25840       if (inlined_func)
25841 	gen_inlined_subroutine_die (stmt, context_die);
25842       else
25843 	gen_lexical_block_die (stmt, context_die);
25844     }
25845   else
25846     decls_for_scope (stmt, context_die);
25847 }
25848 
25849 /* Process variable DECL (or variable with origin ORIGIN) within
25850    block STMT and add it to CONTEXT_DIE.  */
25851 static void
process_scope_var(tree stmt,tree decl,tree origin,dw_die_ref context_die)25852 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25853 {
25854   dw_die_ref die;
25855   tree decl_or_origin = decl ? decl : origin;
25856 
25857   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25858     die = lookup_decl_die (decl_or_origin);
25859   else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25860     {
25861       if (TYPE_DECL_IS_STUB (decl_or_origin))
25862 	die = lookup_type_die (TREE_TYPE (decl_or_origin));
25863       else
25864 	die = lookup_decl_die (decl_or_origin);
25865       /* Avoid re-creating the DIE late if it was optimized as unused early.  */
25866       if (! die && ! early_dwarf)
25867 	return;
25868     }
25869   else
25870     die = NULL;
25871 
25872   /* Avoid creating DIEs for local typedefs and concrete static variables that
25873      will only be pruned later.  */
25874   if ((origin || decl_ultimate_origin (decl))
25875       && (TREE_CODE (decl_or_origin) == TYPE_DECL
25876 	  || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25877     {
25878       origin = decl_ultimate_origin (decl_or_origin);
25879       if (decl && VAR_P (decl) && die != NULL)
25880 	{
25881 	  die = lookup_decl_die (origin);
25882 	  if (die != NULL)
25883 	    equate_decl_number_to_die (decl, die);
25884 	}
25885       return;
25886     }
25887 
25888   if (die != NULL && die->die_parent == NULL)
25889     add_child_die (context_die, die);
25890   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25891     {
25892       if (early_dwarf)
25893 	dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25894 					     stmt, context_die);
25895     }
25896   else
25897     {
25898       if (decl && DECL_P (decl))
25899 	{
25900 	  die = lookup_decl_die (decl);
25901 
25902 	  /* Early created DIEs do not have a parent as the decls refer
25903 	     to the function as DECL_CONTEXT rather than the BLOCK.  */
25904 	  if (die && die->die_parent == NULL)
25905 	    {
25906 	      gcc_assert (in_lto_p);
25907 	      add_child_die (context_die, die);
25908 	    }
25909 	}
25910 
25911       gen_decl_die (decl, origin, NULL, context_die);
25912     }
25913 }
25914 
25915 /* Generate all of the decls declared within a given scope and (recursively)
25916    all of its sub-blocks.  */
25917 
25918 static void
decls_for_scope(tree stmt,dw_die_ref context_die,bool recurse)25919 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
25920 {
25921   tree decl;
25922   unsigned int i;
25923   tree subblocks;
25924 
25925   /* Ignore NULL blocks.  */
25926   if (stmt == NULL_TREE)
25927     return;
25928 
25929   /* Output the DIEs to represent all of the data objects and typedefs
25930      declared directly within this block but not within any nested
25931      sub-blocks.  Also, nested function and tag DIEs have been
25932      generated with a parent of NULL; fix that up now.  We don't
25933      have to do this if we're at -g1.  */
25934   if (debug_info_level > DINFO_LEVEL_TERSE)
25935     {
25936       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25937 	process_scope_var (stmt, decl, NULL_TREE, context_die);
25938       /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25939 	 origin - avoid doing this twice as we have no good way to see
25940 	 if we've done it once already.  */
25941       if (! early_dwarf)
25942 	for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25943 	  {
25944 	    decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25945 	    if (decl == current_function_decl)
25946 	      /* Ignore declarations of the current function, while they
25947 		 are declarations, gen_subprogram_die would treat them
25948 		 as definitions again, because they are equal to
25949 		 current_function_decl and endlessly recurse.  */;
25950 	    else if (TREE_CODE (decl) == FUNCTION_DECL)
25951 	      process_scope_var (stmt, decl, NULL_TREE, context_die);
25952 	    else
25953 	      process_scope_var (stmt, NULL_TREE, decl, context_die);
25954 	  }
25955     }
25956 
25957   /* Even if we're at -g1, we need to process the subblocks in order to get
25958      inlined call information.  */
25959 
25960   /* Output the DIEs to represent all sub-blocks (and the items declared
25961      therein) of this block.  */
25962   if (recurse)
25963     for (subblocks = BLOCK_SUBBLOCKS (stmt);
25964 	 subblocks != NULL;
25965 	 subblocks = BLOCK_CHAIN (subblocks))
25966       gen_block_die (subblocks, context_die);
25967 }
25968 
25969 /* Is this a typedef we can avoid emitting?  */
25970 
25971 static bool
is_redundant_typedef(const_tree decl)25972 is_redundant_typedef (const_tree decl)
25973 {
25974   if (TYPE_DECL_IS_STUB (decl))
25975     return true;
25976 
25977   if (DECL_ARTIFICIAL (decl)
25978       && DECL_CONTEXT (decl)
25979       && is_tagged_type (DECL_CONTEXT (decl))
25980       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25981       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25982     /* Also ignore the artificial member typedef for the class name.  */
25983     return true;
25984 
25985   return false;
25986 }
25987 
25988 /* Return TRUE if TYPE is a typedef that names a type for linkage
25989    purposes. This kind of typedefs is produced by the C++ FE for
25990    constructs like:
25991 
25992    typedef struct {...} foo;
25993 
25994    In that case, there is no typedef variant type produced for foo.
25995    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25996    struct type.  */
25997 
25998 static bool
is_naming_typedef_decl(const_tree decl)25999 is_naming_typedef_decl (const_tree decl)
26000 {
26001   if (decl == NULL_TREE
26002       || TREE_CODE (decl) != TYPE_DECL
26003       || DECL_NAMELESS (decl)
26004       || !is_tagged_type (TREE_TYPE (decl))
26005       || DECL_IS_BUILTIN (decl)
26006       || is_redundant_typedef (decl)
26007       /* It looks like Ada produces TYPE_DECLs that are very similar
26008          to C++ naming typedefs but that have different
26009          semantics. Let's be specific to c++ for now.  */
26010       || !is_cxx (decl))
26011     return FALSE;
26012 
26013   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26014 	  && TYPE_NAME (TREE_TYPE (decl)) == decl
26015 	  && (TYPE_STUB_DECL (TREE_TYPE (decl))
26016 	      != TYPE_NAME (TREE_TYPE (decl))));
26017 }
26018 
26019 /* Looks up the DIE for a context.  */
26020 
26021 static inline dw_die_ref
lookup_context_die(tree context)26022 lookup_context_die (tree context)
26023 {
26024   if (context)
26025     {
26026       /* Find die that represents this context.  */
26027       if (TYPE_P (context))
26028 	{
26029 	  context = TYPE_MAIN_VARIANT (context);
26030 	  dw_die_ref ctx = lookup_type_die (context);
26031 	  if (!ctx)
26032 	    return NULL;
26033 	  return strip_naming_typedef (context, ctx);
26034 	}
26035       else
26036 	return lookup_decl_die (context);
26037     }
26038   return comp_unit_die ();
26039 }
26040 
26041 /* Returns the DIE for a context.  */
26042 
26043 static inline dw_die_ref
get_context_die(tree context)26044 get_context_die (tree context)
26045 {
26046   if (context)
26047     {
26048       /* Find die that represents this context.  */
26049       if (TYPE_P (context))
26050 	{
26051 	  context = TYPE_MAIN_VARIANT (context);
26052 	  return strip_naming_typedef (context, force_type_die (context));
26053 	}
26054       else
26055 	return force_decl_die (context);
26056     }
26057   return comp_unit_die ();
26058 }
26059 
26060 /* Returns the DIE for decl.  A DIE will always be returned.  */
26061 
26062 static dw_die_ref
force_decl_die(tree decl)26063 force_decl_die (tree decl)
26064 {
26065   dw_die_ref decl_die;
26066   unsigned saved_external_flag;
26067   tree save_fn = NULL_TREE;
26068   decl_die = lookup_decl_die (decl);
26069   if (!decl_die)
26070     {
26071       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26072 
26073       decl_die = lookup_decl_die (decl);
26074       if (decl_die)
26075 	return decl_die;
26076 
26077       switch (TREE_CODE (decl))
26078 	{
26079 	case FUNCTION_DECL:
26080 	  /* Clear current_function_decl, so that gen_subprogram_die thinks
26081 	     that this is a declaration. At this point, we just want to force
26082 	     declaration die.  */
26083 	  save_fn = current_function_decl;
26084 	  current_function_decl = NULL_TREE;
26085 	  gen_subprogram_die (decl, context_die);
26086 	  current_function_decl = save_fn;
26087 	  break;
26088 
26089 	case VAR_DECL:
26090 	  /* Set external flag to force declaration die. Restore it after
26091 	   gen_decl_die() call.  */
26092 	  saved_external_flag = DECL_EXTERNAL (decl);
26093 	  DECL_EXTERNAL (decl) = 1;
26094 	  gen_decl_die (decl, NULL, NULL, context_die);
26095 	  DECL_EXTERNAL (decl) = saved_external_flag;
26096 	  break;
26097 
26098 	case NAMESPACE_DECL:
26099 	  if (dwarf_version >= 3 || !dwarf_strict)
26100 	    dwarf2out_decl (decl);
26101 	  else
26102 	    /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
26103 	    decl_die = comp_unit_die ();
26104 	  break;
26105 
26106 	case TRANSLATION_UNIT_DECL:
26107 	  decl_die = comp_unit_die ();
26108 	  break;
26109 
26110 	default:
26111 	  gcc_unreachable ();
26112 	}
26113 
26114       /* We should be able to find the DIE now.  */
26115       if (!decl_die)
26116 	decl_die = lookup_decl_die (decl);
26117       gcc_assert (decl_die);
26118     }
26119 
26120   return decl_die;
26121 }
26122 
26123 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
26124    always returned.  */
26125 
26126 static dw_die_ref
force_type_die(tree type)26127 force_type_die (tree type)
26128 {
26129   dw_die_ref type_die;
26130 
26131   type_die = lookup_type_die (type);
26132   if (!type_die)
26133     {
26134       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26135 
26136       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26137 				    false, context_die);
26138       gcc_assert (type_die);
26139     }
26140   return type_die;
26141 }
26142 
26143 /* Force out any required namespaces to be able to output DECL,
26144    and return the new context_die for it, if it's changed.  */
26145 
26146 static dw_die_ref
setup_namespace_context(tree thing,dw_die_ref context_die)26147 setup_namespace_context (tree thing, dw_die_ref context_die)
26148 {
26149   tree context = (DECL_P (thing)
26150 		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26151   if (context && TREE_CODE (context) == NAMESPACE_DECL)
26152     /* Force out the namespace.  */
26153     context_die = force_decl_die (context);
26154 
26155   return context_die;
26156 }
26157 
26158 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26159    type) within its namespace, if appropriate.
26160 
26161    For compatibility with older debuggers, namespace DIEs only contain
26162    declarations; all definitions are emitted at CU scope, with
26163    DW_AT_specification pointing to the declaration (like with class
26164    members).  */
26165 
26166 static dw_die_ref
declare_in_namespace(tree thing,dw_die_ref context_die)26167 declare_in_namespace (tree thing, dw_die_ref context_die)
26168 {
26169   dw_die_ref ns_context;
26170 
26171   if (debug_info_level <= DINFO_LEVEL_TERSE)
26172     return context_die;
26173 
26174   /* External declarations in the local scope only need to be emitted
26175      once, not once in the namespace and once in the scope.
26176 
26177      This avoids declaring the `extern' below in the
26178      namespace DIE as well as in the innermost scope:
26179 
26180           namespace S
26181 	  {
26182             int i=5;
26183             int foo()
26184 	    {
26185               int i=8;
26186               extern int i;
26187      	      return i;
26188 	    }
26189           }
26190   */
26191   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26192     return context_die;
26193 
26194   /* If this decl is from an inlined function, then don't try to emit it in its
26195      namespace, as we will get confused.  It would have already been emitted
26196      when the abstract instance of the inline function was emitted anyways.  */
26197   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26198     return context_die;
26199 
26200   ns_context = setup_namespace_context (thing, context_die);
26201 
26202   if (ns_context != context_die)
26203     {
26204       if (is_fortran () || is_dlang ())
26205 	return ns_context;
26206       if (DECL_P (thing))
26207 	gen_decl_die (thing, NULL, NULL, ns_context);
26208       else
26209 	gen_type_die (thing, ns_context);
26210     }
26211   return context_die;
26212 }
26213 
26214 /* Generate a DIE for a namespace or namespace alias.  */
26215 
26216 static void
gen_namespace_die(tree decl,dw_die_ref context_die)26217 gen_namespace_die (tree decl, dw_die_ref context_die)
26218 {
26219   dw_die_ref namespace_die;
26220 
26221   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26222      they are an alias of.  */
26223   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26224     {
26225       /* Output a real namespace or module.  */
26226       context_die = setup_namespace_context (decl, comp_unit_die ());
26227       namespace_die = new_die (is_fortran () || is_dlang ()
26228 			       ? DW_TAG_module : DW_TAG_namespace,
26229 			       context_die, decl);
26230       /* For Fortran modules defined in different CU don't add src coords.  */
26231       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26232 	{
26233 	  const char *name = dwarf2_name (decl, 0);
26234 	  if (name)
26235 	    add_name_attribute (namespace_die, name);
26236 	}
26237       else
26238 	add_name_and_src_coords_attributes (namespace_die, decl);
26239       if (DECL_EXTERNAL (decl))
26240 	add_AT_flag (namespace_die, DW_AT_declaration, 1);
26241       equate_decl_number_to_die (decl, namespace_die);
26242     }
26243   else
26244     {
26245       /* Output a namespace alias.  */
26246 
26247       /* Force out the namespace we are an alias of, if necessary.  */
26248       dw_die_ref origin_die
26249 	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26250 
26251       if (DECL_FILE_SCOPE_P (decl)
26252 	  || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26253 	context_die = setup_namespace_context (decl, comp_unit_die ());
26254       /* Now create the namespace alias DIE.  */
26255       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26256       add_name_and_src_coords_attributes (namespace_die, decl);
26257       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26258       equate_decl_number_to_die (decl, namespace_die);
26259     }
26260   if ((dwarf_version >= 5 || !dwarf_strict)
26261       && lang_hooks.decls.decl_dwarf_attribute (decl,
26262 						DW_AT_export_symbols) == 1)
26263     add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26264 
26265   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
26266   if (want_pubnames ())
26267     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26268 }
26269 
26270 /* Generate Dwarf debug information for a decl described by DECL.
26271    The return value is currently only meaningful for PARM_DECLs,
26272    for all other decls it returns NULL.
26273 
26274    If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26275    It can be NULL otherwise.  */
26276 
26277 static dw_die_ref
gen_decl_die(tree decl,tree origin,struct vlr_context * ctx,dw_die_ref context_die)26278 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26279 	      dw_die_ref context_die)
26280 {
26281   tree decl_or_origin = decl ? decl : origin;
26282   tree class_origin = NULL, ultimate_origin;
26283 
26284   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26285     return NULL;
26286 
26287   switch (TREE_CODE (decl_or_origin))
26288     {
26289     case ERROR_MARK:
26290       break;
26291 
26292     case CONST_DECL:
26293       if (!is_fortran () && !is_ada () && !is_dlang ())
26294 	{
26295 	  /* The individual enumerators of an enum type get output when we output
26296 	     the Dwarf representation of the relevant enum type itself.  */
26297 	  break;
26298 	}
26299 
26300       /* Emit its type.  */
26301       gen_type_die (TREE_TYPE (decl), context_die);
26302 
26303       /* And its containing namespace.  */
26304       context_die = declare_in_namespace (decl, context_die);
26305 
26306       gen_const_die (decl, context_die);
26307       break;
26308 
26309     case FUNCTION_DECL:
26310 #if 0
26311       /* FIXME */
26312       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26313 	 on local redeclarations of global functions.  That seems broken.  */
26314       if (current_function_decl != decl)
26315 	/* This is only a declaration.  */;
26316 #endif
26317 
26318       /* We should have abstract copies already and should not generate
26319 	 stray type DIEs in late LTO dumping.  */
26320       if (! early_dwarf)
26321 	;
26322 
26323       /* If we're emitting a clone, emit info for the abstract instance.  */
26324       else if (origin || DECL_ORIGIN (decl) != decl)
26325 	dwarf2out_abstract_function (origin
26326 				     ? DECL_ORIGIN (origin)
26327 				     : DECL_ABSTRACT_ORIGIN (decl));
26328 
26329       /* If we're emitting a possibly inlined function emit it as
26330          abstract instance.  */
26331       else if (cgraph_function_possibly_inlined_p (decl)
26332 	       && ! DECL_ABSTRACT_P (decl)
26333 	       && ! class_or_namespace_scope_p (context_die)
26334 	       /* dwarf2out_abstract_function won't emit a die if this is just
26335 		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
26336 		  that case, because that works only if we have a die.  */
26337 	       && DECL_INITIAL (decl) != NULL_TREE)
26338 	dwarf2out_abstract_function (decl);
26339 
26340       /* Otherwise we're emitting the primary DIE for this decl.  */
26341       else if (debug_info_level > DINFO_LEVEL_TERSE)
26342 	{
26343 	  /* Before we describe the FUNCTION_DECL itself, make sure that we
26344 	     have its containing type.  */
26345 	  if (!origin)
26346 	    origin = decl_class_context (decl);
26347 	  if (origin != NULL_TREE)
26348 	    gen_type_die (origin, context_die);
26349 
26350 	  /* And its return type.  */
26351 	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26352 
26353 	  /* And its virtual context.  */
26354 	  if (DECL_VINDEX (decl) != NULL_TREE)
26355 	    gen_type_die (DECL_CONTEXT (decl), context_die);
26356 
26357 	  /* Make sure we have a member DIE for decl.  */
26358 	  if (origin != NULL_TREE)
26359 	    gen_type_die_for_member (origin, decl, context_die);
26360 
26361 	  /* And its containing namespace.  */
26362 	  context_die = declare_in_namespace (decl, context_die);
26363 	}
26364 
26365       /* Now output a DIE to represent the function itself.  */
26366       if (decl)
26367         gen_subprogram_die (decl, context_die);
26368       break;
26369 
26370     case TYPE_DECL:
26371       /* If we are in terse mode, don't generate any DIEs to represent any
26372 	 actual typedefs.  */
26373       if (debug_info_level <= DINFO_LEVEL_TERSE)
26374 	break;
26375 
26376       /* In the special case of a TYPE_DECL node representing the declaration
26377 	 of some type tag, if the given TYPE_DECL is marked as having been
26378 	 instantiated from some other (original) TYPE_DECL node (e.g. one which
26379 	 was generated within the original definition of an inline function) we
26380 	 used to generate a special (abbreviated) DW_TAG_structure_type,
26381 	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
26382 	 should be actually referencing those DIEs, as variable DIEs with that
26383 	 type would be emitted already in the abstract origin, so it was always
26384 	 removed during unused type prunning.  Don't add anything in this
26385 	 case.  */
26386       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26387 	break;
26388 
26389       if (is_redundant_typedef (decl))
26390 	gen_type_die (TREE_TYPE (decl), context_die);
26391       else
26392 	/* Output a DIE to represent the typedef itself.  */
26393 	gen_typedef_die (decl, context_die);
26394       break;
26395 
26396     case LABEL_DECL:
26397       if (debug_info_level >= DINFO_LEVEL_NORMAL)
26398 	gen_label_die (decl, context_die);
26399       break;
26400 
26401     case VAR_DECL:
26402     case RESULT_DECL:
26403       /* If we are in terse mode, don't generate any DIEs to represent any
26404 	 variable declarations or definitions unless it is external.  */
26405       if (debug_info_level < DINFO_LEVEL_TERSE
26406 	  || (debug_info_level == DINFO_LEVEL_TERSE
26407 	      && !TREE_PUBLIC (decl_or_origin)))
26408 	break;
26409 
26410       if (debug_info_level > DINFO_LEVEL_TERSE)
26411 	{
26412 	  /* Avoid generating stray type DIEs during late dwarf dumping.
26413 	     All types have been dumped early.  */
26414 	  if (early_dwarf
26415 	      /* ???  But in LTRANS we cannot annotate early created variably
26416 		 modified type DIEs without copying them and adjusting all
26417 		 references to them.  Dump them again as happens for inlining
26418 		 which copies both the decl and the types.  */
26419 	      /* ???  And even non-LTO needs to re-visit type DIEs to fill
26420 		 in VLA bound information for example.  */
26421 	      || (decl && variably_modified_type_p (TREE_TYPE (decl),
26422 						    current_function_decl)))
26423 	    {
26424 	      /* Output any DIEs that are needed to specify the type of this data
26425 		 object.  */
26426 	      if (decl_by_reference_p (decl_or_origin))
26427 		gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26428 	      else
26429 		gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26430 	    }
26431 
26432 	  if (early_dwarf)
26433 	    {
26434 	      /* And its containing type.  */
26435 	      class_origin = decl_class_context (decl_or_origin);
26436 	      if (class_origin != NULL_TREE)
26437 		gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26438 
26439 	      /* And its containing namespace.  */
26440 	      context_die = declare_in_namespace (decl_or_origin, context_die);
26441 	    }
26442 	}
26443 
26444       /* Now output the DIE to represent the data object itself.  This gets
26445 	 complicated because of the possibility that the VAR_DECL really
26446 	 represents an inlined instance of a formal parameter for an inline
26447 	 function.  */
26448       ultimate_origin = decl_ultimate_origin (decl_or_origin);
26449       if (ultimate_origin != NULL_TREE
26450 	  && TREE_CODE (ultimate_origin) == PARM_DECL)
26451 	gen_formal_parameter_die (decl, origin,
26452 				  true /* Emit name attribute.  */,
26453 				  context_die);
26454       else
26455 	gen_variable_die (decl, origin, context_die);
26456       break;
26457 
26458     case FIELD_DECL:
26459       gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26460       /* Ignore the nameless fields that are used to skip bits but handle C++
26461 	 anonymous unions and structs.  */
26462       if (DECL_NAME (decl) != NULL_TREE
26463 	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26464 	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26465 	{
26466 	  gen_type_die (member_declared_type (decl), context_die);
26467 	  gen_field_die (decl, ctx, context_die);
26468 	}
26469       break;
26470 
26471     case PARM_DECL:
26472       /* Avoid generating stray type DIEs during late dwarf dumping.
26473          All types have been dumped early.  */
26474       if (early_dwarf
26475 	  /* ???  But in LTRANS we cannot annotate early created variably
26476 	     modified type DIEs without copying them and adjusting all
26477 	     references to them.  Dump them again as happens for inlining
26478 	     which copies both the decl and the types.  */
26479 	  /* ???  And even non-LTO needs to re-visit type DIEs to fill
26480 	     in VLA bound information for example.  */
26481 	  || (decl && variably_modified_type_p (TREE_TYPE (decl),
26482 						current_function_decl)))
26483 	{
26484 	  if (DECL_BY_REFERENCE (decl_or_origin))
26485 	    gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26486 	  else
26487 	    gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26488 	}
26489       return gen_formal_parameter_die (decl, origin,
26490 				       true /* Emit name attribute.  */,
26491 				       context_die);
26492 
26493     case NAMESPACE_DECL:
26494       if (dwarf_version >= 3 || !dwarf_strict)
26495 	gen_namespace_die (decl, context_die);
26496       break;
26497 
26498     case IMPORTED_DECL:
26499       dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26500 					   DECL_CONTEXT (decl), context_die);
26501       break;
26502 
26503     case NAMELIST_DECL:
26504       gen_namelist_decl (DECL_NAME (decl), context_die,
26505 			 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26506       break;
26507 
26508     default:
26509       /* Probably some frontend-internal decl.  Assume we don't care.  */
26510       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26511       break;
26512     }
26513 
26514   return NULL;
26515 }
26516 
26517 /* Output initial debug information for global DECL.  Called at the
26518    end of the parsing process.
26519 
26520    This is the initial debug generation process.  As such, the DIEs
26521    generated may be incomplete.  A later debug generation pass
26522    (dwarf2out_late_global_decl) will augment the information generated
26523    in this pass (e.g., with complete location info).  */
26524 
26525 static void
dwarf2out_early_global_decl(tree decl)26526 dwarf2out_early_global_decl (tree decl)
26527 {
26528   set_early_dwarf s;
26529 
26530   /* gen_decl_die() will set DECL_ABSTRACT because
26531      cgraph_function_possibly_inlined_p() returns true.  This is in
26532      turn will cause DW_AT_inline attributes to be set.
26533 
26534      This happens because at early dwarf generation, there is no
26535      cgraph information, causing cgraph_function_possibly_inlined_p()
26536      to return true.  Trick cgraph_function_possibly_inlined_p()
26537      while we generate dwarf early.  */
26538   bool save = symtab->global_info_ready;
26539   symtab->global_info_ready = true;
26540 
26541   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
26542      other DECLs and they can point to template types or other things
26543      that dwarf2out can't handle when done via dwarf2out_decl.  */
26544   if (TREE_CODE (decl) != TYPE_DECL
26545       && TREE_CODE (decl) != PARM_DECL)
26546     {
26547       if (TREE_CODE (decl) == FUNCTION_DECL)
26548 	{
26549 	  tree save_fndecl = current_function_decl;
26550 
26551 	  /* For nested functions, make sure we have DIEs for the parents first
26552 	     so that all nested DIEs are generated at the proper scope in the
26553 	     first shot.  */
26554 	  tree context = decl_function_context (decl);
26555 	  if (context != NULL)
26556 	    {
26557 	      dw_die_ref context_die = lookup_decl_die (context);
26558 	      current_function_decl = context;
26559 
26560 	      /* Avoid emitting DIEs multiple times, but still process CONTEXT
26561 		 enough so that it lands in its own context.  This avoids type
26562 		 pruning issues later on.  */
26563 	      if (context_die == NULL || is_declaration_die (context_die))
26564 		dwarf2out_early_global_decl (context);
26565 	    }
26566 
26567 	  /* Emit an abstract origin of a function first.  This happens
26568 	     with C++ constructor clones for example and makes
26569 	     dwarf2out_abstract_function happy which requires the early
26570 	     DIE of the abstract instance to be present.  */
26571 	  tree origin = DECL_ABSTRACT_ORIGIN (decl);
26572 	  dw_die_ref origin_die;
26573 	  if (origin != NULL
26574 	      /* Do not emit the DIE multiple times but make sure to
26575 	         process it fully here in case we just saw a declaration.  */
26576 	      && ((origin_die = lookup_decl_die (origin)) == NULL
26577 		  || is_declaration_die (origin_die)))
26578 	    {
26579 	      current_function_decl = origin;
26580 	      dwarf2out_decl (origin);
26581 	    }
26582 
26583 	  /* Emit the DIE for decl but avoid doing that multiple times.  */
26584 	  dw_die_ref old_die;
26585 	  if ((old_die = lookup_decl_die (decl)) == NULL
26586 	      || is_declaration_die (old_die))
26587 	    {
26588 	      current_function_decl = decl;
26589 	      dwarf2out_decl (decl);
26590 	    }
26591 
26592 	  current_function_decl = save_fndecl;
26593 	}
26594       else
26595 	dwarf2out_decl (decl);
26596     }
26597   symtab->global_info_ready = save;
26598 }
26599 
26600 /* Return whether EXPR is an expression with the following pattern:
26601    INDIRECT_REF (NOP_EXPR (INTEGER_CST)).  */
26602 
26603 static bool
is_trivial_indirect_ref(tree expr)26604 is_trivial_indirect_ref (tree expr)
26605 {
26606   if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26607     return false;
26608 
26609   tree nop = TREE_OPERAND (expr, 0);
26610   if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26611     return false;
26612 
26613   tree int_cst = TREE_OPERAND (nop, 0);
26614   return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26615 }
26616 
26617 /* Output debug information for global decl DECL.  Called from
26618    toplev.c after compilation proper has finished.  */
26619 
26620 static void
dwarf2out_late_global_decl(tree decl)26621 dwarf2out_late_global_decl (tree decl)
26622 {
26623   /* Fill-in any location information we were unable to determine
26624      on the first pass.  */
26625   if (VAR_P (decl))
26626     {
26627       dw_die_ref die = lookup_decl_die (decl);
26628 
26629       /* We may have to generate full debug late for LTO in case debug
26630          was not enabled at compile-time or the target doesn't support
26631 	 the LTO early debug scheme.  */
26632       if (! die && in_lto_p)
26633 	dwarf2out_decl (decl);
26634       else if (die)
26635 	{
26636 	  /* We get called via the symtab code invoking late_global_decl
26637 	     for symbols that are optimized out.
26638 
26639 	     Do not add locations for those, except if they have a
26640 	     DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26641 	     Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26642 	     INDIRECT_REF expression, as this could generate relocations to
26643 	     text symbols in LTO object files, which is invalid.  */
26644 	  varpool_node *node = varpool_node::get (decl);
26645 	  if ((! node || ! node->definition)
26646 	      && ! (DECL_HAS_VALUE_EXPR_P (decl)
26647 		    && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26648 	    tree_add_const_value_attribute_for_decl (die, decl);
26649 	  else
26650 	    add_location_or_const_value_attribute (die, decl, false);
26651 	}
26652     }
26653 }
26654 
26655 /* Output debug information for type decl DECL.  Called from toplev.c
26656    and from language front ends (to record built-in types).  */
26657 static void
dwarf2out_type_decl(tree decl,int local)26658 dwarf2out_type_decl (tree decl, int local)
26659 {
26660   if (!local)
26661     {
26662       set_early_dwarf s;
26663       dwarf2out_decl (decl);
26664     }
26665 }
26666 
26667 /* Output debug information for imported module or decl DECL.
26668    NAME is non-NULL name in the lexical block if the decl has been renamed.
26669    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26670    that DECL belongs to.
26671    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
26672 static void
dwarf2out_imported_module_or_decl_1(tree decl,tree name,tree lexical_block,dw_die_ref lexical_block_die)26673 dwarf2out_imported_module_or_decl_1 (tree decl,
26674 				     tree name,
26675 				     tree lexical_block,
26676 				     dw_die_ref lexical_block_die)
26677 {
26678   expanded_location xloc;
26679   dw_die_ref imported_die = NULL;
26680   dw_die_ref at_import_die;
26681 
26682   if (TREE_CODE (decl) == IMPORTED_DECL)
26683     {
26684       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26685       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26686       gcc_assert (decl);
26687     }
26688   else
26689     xloc = expand_location (input_location);
26690 
26691   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26692     {
26693       at_import_die = force_type_die (TREE_TYPE (decl));
26694       /* For namespace N { typedef void T; } using N::T; base_type_die
26695 	 returns NULL, but DW_TAG_imported_declaration requires
26696 	 the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
26697       if (!at_import_die)
26698 	{
26699 	  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26700 	  gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26701 	  at_import_die = lookup_type_die (TREE_TYPE (decl));
26702 	  gcc_assert (at_import_die);
26703 	}
26704     }
26705   else
26706     {
26707       at_import_die = lookup_decl_die (decl);
26708       if (!at_import_die)
26709 	{
26710 	  /* If we're trying to avoid duplicate debug info, we may not have
26711 	     emitted the member decl for this field.  Emit it now.  */
26712 	  if (TREE_CODE (decl) == FIELD_DECL)
26713 	    {
26714 	      tree type = DECL_CONTEXT (decl);
26715 
26716 	      if (TYPE_CONTEXT (type)
26717 		  && TYPE_P (TYPE_CONTEXT (type))
26718 		  && !should_emit_struct_debug (TYPE_CONTEXT (type),
26719 						DINFO_USAGE_DIR_USE))
26720 		return;
26721 	      gen_type_die_for_member (type, decl,
26722 				       get_context_die (TYPE_CONTEXT (type)));
26723 	    }
26724 	  if (TREE_CODE (decl) == NAMELIST_DECL)
26725 	    at_import_die = gen_namelist_decl (DECL_NAME (decl),
26726 					 get_context_die (DECL_CONTEXT (decl)),
26727 					 NULL_TREE);
26728 	  else
26729 	    at_import_die = force_decl_die (decl);
26730 	}
26731     }
26732 
26733   if (TREE_CODE (decl) == NAMESPACE_DECL)
26734     {
26735       if (dwarf_version >= 3 || !dwarf_strict)
26736 	imported_die = new_die (DW_TAG_imported_module,
26737 				lexical_block_die,
26738 				lexical_block);
26739       else
26740 	return;
26741     }
26742   else
26743     imported_die = new_die (DW_TAG_imported_declaration,
26744 			    lexical_block_die,
26745 			    lexical_block);
26746 
26747   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26748   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26749   if (debug_column_info && xloc.column)
26750     add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26751   if (name)
26752     add_AT_string (imported_die, DW_AT_name,
26753 		   IDENTIFIER_POINTER (name));
26754   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26755 }
26756 
26757 /* Output debug information for imported module or decl DECL.
26758    NAME is non-NULL name in context if the decl has been renamed.
26759    CHILD is true if decl is one of the renamed decls as part of
26760    importing whole module.
26761    IMPLICIT is set if this hook is called for an implicit import
26762    such as inline namespace.  */
26763 
26764 static void
dwarf2out_imported_module_or_decl(tree decl,tree name,tree context,bool child,bool implicit)26765 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26766 				   bool child, bool implicit)
26767 {
26768   /* dw_die_ref at_import_die;  */
26769   dw_die_ref scope_die;
26770 
26771   if (debug_info_level <= DINFO_LEVEL_TERSE)
26772     return;
26773 
26774   gcc_assert (decl);
26775 
26776   /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26777      should be enough, for DWARF4 and older even if we emit as extension
26778      DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26779      for the benefit of consumers unaware of DW_AT_export_symbols.  */
26780   if (implicit
26781       && dwarf_version >= 5
26782       && lang_hooks.decls.decl_dwarf_attribute (decl,
26783 						DW_AT_export_symbols) == 1)
26784     return;
26785 
26786   set_early_dwarf s;
26787 
26788   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26789      We need decl DIE for reference and scope die. First, get DIE for the decl
26790      itself.  */
26791 
26792   /* Get the scope die for decl context. Use comp_unit_die for global module
26793      or decl. If die is not found for non globals, force new die.  */
26794   if (context
26795       && TYPE_P (context)
26796       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26797     return;
26798 
26799   scope_die = get_context_die (context);
26800 
26801   if (child)
26802     {
26803       /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26804 	 there is nothing we can do, here.  */
26805       if (dwarf_version < 3 && dwarf_strict)
26806 	return;
26807 
26808       gcc_assert (scope_die->die_child);
26809       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26810       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26811       scope_die = scope_die->die_child;
26812     }
26813 
26814   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
26815   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26816 }
26817 
26818 /* Output debug information for namelists.   */
26819 
26820 static dw_die_ref
gen_namelist_decl(tree name,dw_die_ref scope_die,tree item_decls)26821 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26822 {
26823   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26824   tree value;
26825   unsigned i;
26826 
26827   if (debug_info_level <= DINFO_LEVEL_TERSE)
26828     return NULL;
26829 
26830   gcc_assert (scope_die != NULL);
26831   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26832   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26833 
26834   /* If there are no item_decls, we have a nondefining namelist, e.g.
26835      with USE association; hence, set DW_AT_declaration.  */
26836   if (item_decls == NULL_TREE)
26837     {
26838       add_AT_flag (nml_die, DW_AT_declaration, 1);
26839       return nml_die;
26840     }
26841 
26842   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26843     {
26844       nml_item_ref_die = lookup_decl_die (value);
26845       if (!nml_item_ref_die)
26846 	nml_item_ref_die = force_decl_die (value);
26847 
26848       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26849       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26850     }
26851   return nml_die;
26852 }
26853 
26854 
26855 /* Write the debugging output for DECL and return the DIE.  */
26856 
26857 static void
dwarf2out_decl(tree decl)26858 dwarf2out_decl (tree decl)
26859 {
26860   dw_die_ref context_die = comp_unit_die ();
26861 
26862   switch (TREE_CODE (decl))
26863     {
26864     case ERROR_MARK:
26865       return;
26866 
26867     case FUNCTION_DECL:
26868       /* If we're a nested function, initially use a parent of NULL; if we're
26869 	 a plain function, this will be fixed up in decls_for_scope.  If
26870 	 we're a method, it will be ignored, since we already have a DIE.
26871 	 Avoid doing this late though since clones of class methods may
26872 	 otherwise end up in limbo and create type DIEs late.  */
26873       if (early_dwarf
26874 	  && decl_function_context (decl)
26875 	  /* But if we're in terse mode, we don't care about scope.  */
26876 	  && debug_info_level > DINFO_LEVEL_TERSE)
26877 	context_die = NULL;
26878       break;
26879 
26880     case VAR_DECL:
26881       /* For local statics lookup proper context die.  */
26882       if (local_function_static (decl))
26883 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
26884 
26885       /* If we are in terse mode, don't generate any DIEs to represent any
26886 	 variable declarations or definitions unless it is external.  */
26887       if (debug_info_level < DINFO_LEVEL_TERSE
26888 	  || (debug_info_level == DINFO_LEVEL_TERSE
26889 	      && !TREE_PUBLIC (decl)))
26890 	return;
26891       break;
26892 
26893     case CONST_DECL:
26894       if (debug_info_level <= DINFO_LEVEL_TERSE)
26895 	return;
26896       if (!is_fortran () && !is_ada () && !is_dlang ())
26897 	return;
26898       if (TREE_STATIC (decl) && decl_function_context (decl))
26899 	context_die = lookup_decl_die (DECL_CONTEXT (decl));
26900       break;
26901 
26902     case NAMESPACE_DECL:
26903     case IMPORTED_DECL:
26904       if (debug_info_level <= DINFO_LEVEL_TERSE)
26905 	return;
26906       if (lookup_decl_die (decl) != NULL)
26907 	return;
26908       break;
26909 
26910     case TYPE_DECL:
26911       /* Don't emit stubs for types unless they are needed by other DIEs.  */
26912       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26913 	return;
26914 
26915       /* Don't bother trying to generate any DIEs to represent any of the
26916 	 normal built-in types for the language we are compiling.  */
26917       if (DECL_IS_BUILTIN (decl))
26918 	return;
26919 
26920       /* If we are in terse mode, don't generate any DIEs for types.  */
26921       if (debug_info_level <= DINFO_LEVEL_TERSE)
26922 	return;
26923 
26924       /* If we're a function-scope tag, initially use a parent of NULL;
26925 	 this will be fixed up in decls_for_scope.  */
26926       if (decl_function_context (decl))
26927 	context_die = NULL;
26928 
26929       break;
26930 
26931     case NAMELIST_DECL:
26932       break;
26933 
26934     default:
26935       return;
26936     }
26937 
26938   gen_decl_die (decl, NULL, NULL, context_die);
26939 
26940   if (flag_checking)
26941     {
26942       dw_die_ref die = lookup_decl_die (decl);
26943       if (die)
26944 	check_die (die);
26945     }
26946 }
26947 
26948 /* Write the debugging output for DECL.  */
26949 
26950 static void
dwarf2out_function_decl(tree decl)26951 dwarf2out_function_decl (tree decl)
26952 {
26953   dwarf2out_decl (decl);
26954   call_arg_locations = NULL;
26955   call_arg_loc_last = NULL;
26956   call_site_count = -1;
26957   tail_call_site_count = -1;
26958   decl_loc_table->empty ();
26959   cached_dw_loc_list_table->empty ();
26960 }
26961 
26962 /* Output a marker (i.e. a label) for the beginning of the generated code for
26963    a lexical block.  */
26964 
26965 static void
dwarf2out_begin_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)26966 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26967 		       unsigned int blocknum)
26968 {
26969   switch_to_section (current_function_section ());
26970   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26971 }
26972 
26973 /* Output a marker (i.e. a label) for the end of the generated code for a
26974    lexical block.  */
26975 
26976 static void
dwarf2out_end_block(unsigned int line ATTRIBUTE_UNUSED,unsigned int blocknum)26977 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26978 {
26979   switch_to_section (current_function_section ());
26980   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26981 }
26982 
26983 /* Returns nonzero if it is appropriate not to emit any debugging
26984    information for BLOCK, because it doesn't contain any instructions.
26985 
26986    Don't allow this for blocks with nested functions or local classes
26987    as we would end up with orphans, and in the presence of scheduling
26988    we may end up calling them anyway.  */
26989 
26990 static bool
dwarf2out_ignore_block(const_tree block)26991 dwarf2out_ignore_block (const_tree block)
26992 {
26993   tree decl;
26994   unsigned int i;
26995 
26996   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26997     if (TREE_CODE (decl) == FUNCTION_DECL
26998 	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26999       return 0;
27000   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27001     {
27002       decl = BLOCK_NONLOCALIZED_VAR (block, i);
27003       if (TREE_CODE (decl) == FUNCTION_DECL
27004 	  || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27005       return 0;
27006     }
27007 
27008   return 1;
27009 }
27010 
27011 /* Hash table routines for file_hash.  */
27012 
27013 bool
equal(dwarf_file_data * p1,const char * p2)27014 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27015 {
27016   return filename_cmp (p1->filename, p2) == 0;
27017 }
27018 
27019 hashval_t
hash(dwarf_file_data * p)27020 dwarf_file_hasher::hash (dwarf_file_data *p)
27021 {
27022   return htab_hash_string (p->filename);
27023 }
27024 
27025 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27026    dwarf2out.c) and return its "index".  The index of each (known) filename is
27027    just a unique number which is associated with only that one filename.  We
27028    need such numbers for the sake of generating labels (in the .debug_sfnames
27029    section) and references to those files numbers (in the .debug_srcinfo
27030    and .debug_macinfo sections).  If the filename given as an argument is not
27031    found in our current list, add it to the list and assign it the next
27032    available unique index number.  */
27033 
27034 static struct dwarf_file_data *
lookup_filename(const char * file_name)27035 lookup_filename (const char *file_name)
27036 {
27037   struct dwarf_file_data * created;
27038 
27039   if (!file_name)
27040     return NULL;
27041 
27042   if (!file_name[0])
27043     file_name = "<stdin>";
27044 
27045   dwarf_file_data **slot
27046     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27047 				       INSERT);
27048   if (*slot)
27049     return *slot;
27050 
27051   created = ggc_alloc<dwarf_file_data> ();
27052   created->filename = file_name;
27053   created->emitted_number = 0;
27054   *slot = created;
27055   return created;
27056 }
27057 
27058 /* If the assembler will construct the file table, then translate the compiler
27059    internal file table number into the assembler file table number, and emit
27060    a .file directive if we haven't already emitted one yet.  The file table
27061    numbers are different because we prune debug info for unused variables and
27062    types, which may include filenames.  */
27063 
27064 static int
maybe_emit_file(struct dwarf_file_data * fd)27065 maybe_emit_file (struct dwarf_file_data * fd)
27066 {
27067   if (! fd->emitted_number)
27068     {
27069       if (last_emitted_file)
27070 	fd->emitted_number = last_emitted_file->emitted_number + 1;
27071       else
27072 	fd->emitted_number = 1;
27073       last_emitted_file = fd;
27074 
27075       if (output_asm_line_debug_info ())
27076 	{
27077 	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27078 	  output_quoted_string (asm_out_file,
27079 				remap_debug_filename (fd->filename));
27080 	  fputc ('\n', asm_out_file);
27081 	}
27082     }
27083 
27084   return fd->emitted_number;
27085 }
27086 
27087 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27088    That generation should happen after function debug info has been
27089    generated. The value of the attribute is the constant value of ARG.  */
27090 
27091 static void
append_entry_to_tmpl_value_parm_die_table(dw_die_ref die,tree arg)27092 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27093 {
27094   die_arg_entry entry;
27095 
27096   if (!die || !arg)
27097     return;
27098 
27099   gcc_assert (early_dwarf);
27100 
27101   if (!tmpl_value_parm_die_table)
27102     vec_alloc (tmpl_value_parm_die_table, 32);
27103 
27104   entry.die = die;
27105   entry.arg = arg;
27106   vec_safe_push (tmpl_value_parm_die_table, entry);
27107 }
27108 
27109 /* Return TRUE if T is an instance of generic type, FALSE
27110    otherwise.  */
27111 
27112 static bool
generic_type_p(tree t)27113 generic_type_p (tree t)
27114 {
27115   if (t == NULL_TREE || !TYPE_P (t))
27116     return false;
27117   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27118 }
27119 
27120 /* Schedule the generation of the generic parameter dies for the
27121   instance of generic type T. The proper generation itself is later
27122   done by gen_scheduled_generic_parms_dies. */
27123 
27124 static void
schedule_generic_params_dies_gen(tree t)27125 schedule_generic_params_dies_gen (tree t)
27126 {
27127   if (!generic_type_p (t))
27128     return;
27129 
27130   gcc_assert (early_dwarf);
27131 
27132   if (!generic_type_instances)
27133     vec_alloc (generic_type_instances, 256);
27134 
27135   vec_safe_push (generic_type_instances, t);
27136 }
27137 
27138 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27139    by append_entry_to_tmpl_value_parm_die_table. This function must
27140    be called after function DIEs have been generated.  */
27141 
27142 static void
gen_remaining_tmpl_value_param_die_attribute(void)27143 gen_remaining_tmpl_value_param_die_attribute (void)
27144 {
27145   if (tmpl_value_parm_die_table)
27146     {
27147       unsigned i, j;
27148       die_arg_entry *e;
27149 
27150       /* We do this in two phases - first get the cases we can
27151 	 handle during early-finish, preserving those we cannot
27152 	 (containing symbolic constants where we don't yet know
27153 	 whether we are going to output the referenced symbols).
27154 	 For those we try again at late-finish.  */
27155       j = 0;
27156       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27157 	{
27158 	  if (!e->die->removed
27159 	      && !tree_add_const_value_attribute (e->die, e->arg))
27160 	    {
27161 	      dw_loc_descr_ref loc = NULL;
27162 	      if (! early_dwarf
27163 		  && (dwarf_version >= 5 || !dwarf_strict))
27164 		loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27165 	      if (loc)
27166 		add_AT_loc (e->die, DW_AT_location, loc);
27167 	      else
27168 		(*tmpl_value_parm_die_table)[j++] = *e;
27169 	    }
27170 	}
27171       tmpl_value_parm_die_table->truncate (j);
27172     }
27173 }
27174 
27175 /* Generate generic parameters DIEs for instances of generic types
27176    that have been previously scheduled by
27177    schedule_generic_params_dies_gen. This function must be called
27178    after all the types of the CU have been laid out.  */
27179 
27180 static void
gen_scheduled_generic_parms_dies(void)27181 gen_scheduled_generic_parms_dies (void)
27182 {
27183   unsigned i;
27184   tree t;
27185 
27186   if (!generic_type_instances)
27187     return;
27188 
27189   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27190     if (COMPLETE_TYPE_P (t))
27191       gen_generic_params_dies (t);
27192 
27193   generic_type_instances = NULL;
27194 }
27195 
27196 
27197 /* Replace DW_AT_name for the decl with name.  */
27198 
27199 static void
dwarf2out_set_name(tree decl,tree name)27200 dwarf2out_set_name (tree decl, tree name)
27201 {
27202   dw_die_ref die;
27203   dw_attr_node *attr;
27204   const char *dname;
27205 
27206   die = TYPE_SYMTAB_DIE (decl);
27207   if (!die)
27208     return;
27209 
27210   dname = dwarf2_name (name, 0);
27211   if (!dname)
27212     return;
27213 
27214   attr = get_AT (die, DW_AT_name);
27215   if (attr)
27216     {
27217       struct indirect_string_node *node;
27218 
27219       node = find_AT_string (dname);
27220       /* replace the string.  */
27221       attr->dw_attr_val.v.val_str = node;
27222     }
27223 
27224   else
27225     add_name_attribute (die, dname);
27226 }
27227 
27228 /* True if before or during processing of the first function being emitted.  */
27229 static bool in_first_function_p = true;
27230 /* True if loc_note during dwarf2out_var_location call might still be
27231    before first real instruction at address equal to .Ltext0.  */
27232 static bool maybe_at_text_label_p = true;
27233 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
27234 static unsigned int first_loclabel_num_not_at_text_label;
27235 
27236 /* Look ahead for a real insn.  */
27237 
27238 static rtx_insn *
dwarf2out_next_real_insn(rtx_insn * loc_note)27239 dwarf2out_next_real_insn (rtx_insn *loc_note)
27240 {
27241   rtx_insn *next_real = NEXT_INSN (loc_note);
27242 
27243   while (next_real)
27244     if (INSN_P (next_real))
27245       break;
27246     else
27247       next_real = NEXT_INSN (next_real);
27248 
27249   return next_real;
27250 }
27251 
27252 /* Called by the final INSN scan whenever we see a var location.  We
27253    use it to drop labels in the right places, and throw the location in
27254    our lookup table.  */
27255 
27256 static void
dwarf2out_var_location(rtx_insn * loc_note)27257 dwarf2out_var_location (rtx_insn *loc_note)
27258 {
27259   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27260   struct var_loc_node *newloc;
27261   rtx_insn *next_real;
27262   rtx_insn *call_insn = NULL;
27263   static const char *last_label;
27264   static const char *last_postcall_label;
27265   static bool last_in_cold_section_p;
27266   static rtx_insn *expected_next_loc_note;
27267   tree decl;
27268   bool var_loc_p;
27269   var_loc_view view = 0;
27270 
27271   if (!NOTE_P (loc_note))
27272     {
27273       if (CALL_P (loc_note))
27274 	{
27275 	  maybe_reset_location_view (loc_note, cur_line_info_table);
27276 	  call_site_count++;
27277 	  if (SIBLING_CALL_P (loc_note))
27278 	    tail_call_site_count++;
27279 	  if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27280 	    {
27281 	      call_insn = loc_note;
27282 	      loc_note = NULL;
27283 	      var_loc_p = false;
27284 
27285 	      next_real = dwarf2out_next_real_insn (call_insn);
27286 	      cached_next_real_insn = NULL;
27287 	      goto create_label;
27288 	    }
27289 	  if (optimize == 0 && !flag_var_tracking)
27290 	    {
27291 	      /* When the var-tracking pass is not running, there is no note
27292 		 for indirect calls whose target is compile-time known. In this
27293 		 case, process such calls specifically so that we generate call
27294 		 sites for them anyway.  */
27295 	      rtx x = PATTERN (loc_note);
27296 	      if (GET_CODE (x) == PARALLEL)
27297 		x = XVECEXP (x, 0, 0);
27298 	      if (GET_CODE (x) == SET)
27299 		x = SET_SRC (x);
27300 	      if (GET_CODE (x) == CALL)
27301 		x = XEXP (x, 0);
27302 	      if (!MEM_P (x)
27303 		  || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27304 		  || !SYMBOL_REF_DECL (XEXP (x, 0))
27305 		  || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27306 		      != FUNCTION_DECL))
27307 		{
27308 		  call_insn = loc_note;
27309 		  loc_note = NULL;
27310 		  var_loc_p = false;
27311 
27312 		  next_real = dwarf2out_next_real_insn (call_insn);
27313 		  cached_next_real_insn = NULL;
27314 		  goto create_label;
27315 		}
27316 	    }
27317 	}
27318       else if (!debug_variable_location_views)
27319 	gcc_unreachable ();
27320       else
27321 	maybe_reset_location_view (loc_note, cur_line_info_table);
27322 
27323       return;
27324     }
27325 
27326   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27327   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27328     return;
27329 
27330   /* Optimize processing a large consecutive sequence of location
27331      notes so we don't spend too much time in next_real_insn.  If the
27332      next insn is another location note, remember the next_real_insn
27333      calculation for next time.  */
27334   next_real = cached_next_real_insn;
27335   if (next_real)
27336     {
27337       if (expected_next_loc_note != loc_note)
27338 	next_real = NULL;
27339     }
27340 
27341   if (! next_real)
27342     next_real = dwarf2out_next_real_insn (loc_note);
27343 
27344   if (next_real)
27345     {
27346       rtx_insn *next_note = NEXT_INSN (loc_note);
27347       while (next_note != next_real)
27348 	{
27349 	  if (! next_note->deleted ()
27350 	      && NOTE_P (next_note)
27351 	      && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27352 	    break;
27353 	  next_note = NEXT_INSN (next_note);
27354 	}
27355 
27356       if (next_note == next_real)
27357 	cached_next_real_insn = NULL;
27358       else
27359 	{
27360 	  expected_next_loc_note = next_note;
27361 	  cached_next_real_insn = next_real;
27362 	}
27363     }
27364   else
27365     cached_next_real_insn = NULL;
27366 
27367   /* If there are no instructions which would be affected by this note,
27368      don't do anything.  */
27369   if (var_loc_p
27370       && next_real == NULL_RTX
27371       && !NOTE_DURING_CALL_P (loc_note))
27372     return;
27373 
27374 create_label:
27375 
27376   if (next_real == NULL_RTX)
27377     next_real = get_last_insn ();
27378 
27379   /* If there were any real insns between note we processed last time
27380      and this note (or if it is the first note), clear
27381      last_{,postcall_}label so that they are not reused this time.  */
27382   if (last_var_location_insn == NULL_RTX
27383       || last_var_location_insn != next_real
27384       || last_in_cold_section_p != in_cold_section_p)
27385     {
27386       last_label = NULL;
27387       last_postcall_label = NULL;
27388     }
27389 
27390   if (var_loc_p)
27391     {
27392       const char *label
27393 	= NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27394       view = cur_line_info_table->view;
27395       decl = NOTE_VAR_LOCATION_DECL (loc_note);
27396       newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27397       if (newloc == NULL)
27398 	return;
27399     }
27400   else
27401     {
27402       decl = NULL_TREE;
27403       newloc = NULL;
27404     }
27405 
27406   /* If there were no real insns between note we processed last time
27407      and this note, use the label we emitted last time.  Otherwise
27408      create a new label and emit it.  */
27409   if (last_label == NULL)
27410     {
27411       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27412       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27413       loclabel_num++;
27414       last_label = ggc_strdup (loclabel);
27415       /* See if loclabel might be equal to .Ltext0.  If yes,
27416 	 bump first_loclabel_num_not_at_text_label.  */
27417       if (!have_multiple_function_sections
27418 	  && in_first_function_p
27419 	  && maybe_at_text_label_p)
27420 	{
27421 	  static rtx_insn *last_start;
27422 	  rtx_insn *insn;
27423 	  for (insn = loc_note; insn; insn = previous_insn (insn))
27424 	    if (insn == last_start)
27425 	      break;
27426 	    else if (!NONDEBUG_INSN_P (insn))
27427 	      continue;
27428 	    else
27429 	      {
27430 		rtx body = PATTERN (insn);
27431 		if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27432 		  continue;
27433 		/* Inline asm could occupy zero bytes.  */
27434 		else if (GET_CODE (body) == ASM_INPUT
27435 			 || asm_noperands (body) >= 0)
27436 		  continue;
27437 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h.  */
27438 		else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27439 		  continue;
27440 #endif
27441 		else
27442 		  {
27443 		    /* Assume insn has non-zero length.  */
27444 		    maybe_at_text_label_p = false;
27445 		    break;
27446 		  }
27447 	      }
27448 	  if (maybe_at_text_label_p)
27449 	    {
27450 	      last_start = loc_note;
27451 	      first_loclabel_num_not_at_text_label = loclabel_num;
27452 	    }
27453 	}
27454     }
27455 
27456   gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27457 	      || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27458 
27459   if (!var_loc_p)
27460     {
27461       struct call_arg_loc_node *ca_loc
27462 	= ggc_cleared_alloc<call_arg_loc_node> ();
27463       rtx_insn *prev = call_insn;
27464 
27465       ca_loc->call_arg_loc_note
27466 	= find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27467       ca_loc->next = NULL;
27468       ca_loc->label = last_label;
27469       gcc_assert (prev
27470 		  && (CALL_P (prev)
27471 		      || (NONJUMP_INSN_P (prev)
27472 			  && GET_CODE (PATTERN (prev)) == SEQUENCE
27473 			  && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27474       if (!CALL_P (prev))
27475 	prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27476       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27477 
27478       /* Look for a SYMBOL_REF in the "prev" instruction.  */
27479       rtx x = get_call_rtx_from (prev);
27480       if (x)
27481 	{
27482 	  /* Try to get the call symbol, if any.  */
27483 	  if (MEM_P (XEXP (x, 0)))
27484 	    x = XEXP (x, 0);
27485 	  /* First, look for a memory access to a symbol_ref.  */
27486 	  if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27487 	      && SYMBOL_REF_DECL (XEXP (x, 0))
27488 	      && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27489 	    ca_loc->symbol_ref = XEXP (x, 0);
27490 	  /* Otherwise, look at a compile-time known user-level function
27491 	     declaration.  */
27492 	  else if (MEM_P (x)
27493 		   && MEM_EXPR (x)
27494 		   && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27495 	    ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27496 	}
27497 
27498       ca_loc->block = insn_scope (prev);
27499       if (call_arg_locations)
27500 	call_arg_loc_last->next = ca_loc;
27501       else
27502 	call_arg_locations = ca_loc;
27503       call_arg_loc_last = ca_loc;
27504     }
27505   else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27506     {
27507       newloc->label = last_label;
27508       newloc->view = view;
27509     }
27510   else
27511     {
27512       if (!last_postcall_label)
27513 	{
27514 	  sprintf (loclabel, "%s-1", last_label);
27515 	  last_postcall_label = ggc_strdup (loclabel);
27516 	}
27517       newloc->label = last_postcall_label;
27518       /* ??? This view is at last_label, not last_label-1, but we
27519 	 could only assume view at last_label-1 is zero if we could
27520 	 assume calls always have length greater than one.  This is
27521 	 probably true in general, though there might be a rare
27522 	 exception to this rule, e.g. if a call insn is optimized out
27523 	 by target magic.  Then, even the -1 in the label will be
27524 	 wrong, which might invalidate the range.  Anyway, using view,
27525 	 though technically possibly incorrect, will work as far as
27526 	 ranges go: since L-1 is in the middle of the call insn,
27527 	 (L-1).0 and (L-1).V shouldn't make any difference, and having
27528 	 the loclist entry refer to the .loc entry might be useful, so
27529 	 leave it like this.  */
27530       newloc->view = view;
27531     }
27532 
27533   if (var_loc_p && flag_debug_asm)
27534     {
27535       const char *name, *sep, *patstr;
27536       if (decl && DECL_NAME (decl))
27537 	name = IDENTIFIER_POINTER (DECL_NAME (decl));
27538       else
27539 	name = "";
27540       if (NOTE_VAR_LOCATION_LOC (loc_note))
27541 	{
27542 	  sep = " => ";
27543 	  patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27544 	}
27545       else
27546 	{
27547 	  sep = " ";
27548 	  patstr = "RESET";
27549 	}
27550       fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27551 	       name, sep, patstr);
27552     }
27553 
27554   last_var_location_insn = next_real;
27555   last_in_cold_section_p = in_cold_section_p;
27556 }
27557 
27558 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27559    OUTER itself.  If BOTHWAYS, check not only that BLOCK can reach
27560    OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27561    path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27562    BLOCK_FRAGMENT_ORIGIN links.  */
27563 static bool
block_within_block_p(tree block,tree outer,bool bothways)27564 block_within_block_p (tree block, tree outer, bool bothways)
27565 {
27566   if (block == outer)
27567     return true;
27568 
27569   /* Quickly check that OUTER is up BLOCK's supercontext chain.  */
27570   for (tree context = BLOCK_SUPERCONTEXT (block);
27571        context != outer;
27572        context = BLOCK_SUPERCONTEXT (context))
27573     if (!context || TREE_CODE (context) != BLOCK)
27574       return false;
27575 
27576   if (!bothways)
27577     return true;
27578 
27579   /* Now check that each block is actually referenced by its
27580      parent.  */
27581   for (tree context = BLOCK_SUPERCONTEXT (block); ;
27582        context = BLOCK_SUPERCONTEXT (context))
27583     {
27584       if (BLOCK_FRAGMENT_ORIGIN (context))
27585 	{
27586 	  gcc_assert (!BLOCK_SUBBLOCKS (context));
27587 	  context = BLOCK_FRAGMENT_ORIGIN (context);
27588 	}
27589       for (tree sub = BLOCK_SUBBLOCKS (context);
27590 	   sub != block;
27591 	   sub = BLOCK_CHAIN (sub))
27592 	if (!sub)
27593 	  return false;
27594       if (context == outer)
27595 	return true;
27596       else
27597 	block = context;
27598     }
27599 }
27600 
27601 /* Called during final while assembling the marker of the entry point
27602    for an inlined function.  */
27603 
27604 static void
dwarf2out_inline_entry(tree block)27605 dwarf2out_inline_entry (tree block)
27606 {
27607   gcc_assert (debug_inline_points);
27608 
27609   /* If we can't represent it, don't bother.  */
27610   if (!(dwarf_version >= 3 || !dwarf_strict))
27611     return;
27612 
27613   gcc_assert (DECL_P (block_ultimate_origin (block)));
27614 
27615   /* Sanity check the block tree.  This would catch a case in which
27616      BLOCK got removed from the tree reachable from the outermost
27617      lexical block, but got retained in markers.  It would still link
27618      back to its parents, but some ancestor would be missing a link
27619      down the path to the sub BLOCK.  If the block got removed, its
27620      BLOCK_NUMBER will not be a usable value.  */
27621   if (flag_checking)
27622     gcc_assert (block_within_block_p (block,
27623 				      DECL_INITIAL (current_function_decl),
27624 				      true));
27625 
27626   gcc_assert (inlined_function_outer_scope_p (block));
27627   gcc_assert (!lookup_block_die (block));
27628 
27629   if (BLOCK_FRAGMENT_ORIGIN (block))
27630     block = BLOCK_FRAGMENT_ORIGIN (block);
27631   /* Can the entry point ever not be at the beginning of an
27632      unfragmented lexical block?  */
27633   else if (!(BLOCK_FRAGMENT_CHAIN (block)
27634 	     || (cur_line_info_table
27635 		 && !ZERO_VIEW_P (cur_line_info_table->view))))
27636     return;
27637 
27638   if (!inline_entry_data_table)
27639     inline_entry_data_table
27640       = hash_table<inline_entry_data_hasher>::create_ggc (10);
27641 
27642 
27643   inline_entry_data **iedp
27644     = inline_entry_data_table->find_slot_with_hash (block,
27645 						    htab_hash_pointer (block),
27646 						    INSERT);
27647   if (*iedp)
27648     /* ??? Ideally, we'd record all entry points for the same inlined
27649        function (some may have been duplicated by e.g. unrolling), but
27650        we have no way to represent that ATM.  */
27651     return;
27652 
27653   inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27654   ied->block = block;
27655   ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27656   ied->label_num = BLOCK_NUMBER (block);
27657   if (cur_line_info_table)
27658     ied->view = cur_line_info_table->view;
27659 
27660   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
27661 			  BLOCK_NUMBER (block));
27662 }
27663 
27664 /* Called from finalize_size_functions for size functions so that their body
27665    can be encoded in the debug info to describe the layout of variable-length
27666    structures.  */
27667 
27668 static void
dwarf2out_size_function(tree decl)27669 dwarf2out_size_function (tree decl)
27670 {
27671   set_early_dwarf s;
27672   function_to_dwarf_procedure (decl);
27673 }
27674 
27675 /* Note in one location list that text section has changed.  */
27676 
27677 int
var_location_switch_text_section_1(var_loc_list ** slot,void *)27678 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27679 {
27680   var_loc_list *list = *slot;
27681   if (list->first)
27682     list->last_before_switch
27683       = list->last->next ? list->last->next : list->last;
27684   return 1;
27685 }
27686 
27687 /* Note in all location lists that text section has changed.  */
27688 
27689 static void
var_location_switch_text_section(void)27690 var_location_switch_text_section (void)
27691 {
27692   if (decl_loc_table == NULL)
27693     return;
27694 
27695   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27696 }
27697 
27698 /* Create a new line number table.  */
27699 
27700 static dw_line_info_table *
new_line_info_table(void)27701 new_line_info_table (void)
27702 {
27703   dw_line_info_table *table;
27704 
27705   table = ggc_cleared_alloc<dw_line_info_table> ();
27706   table->file_num = 1;
27707   table->line_num = 1;
27708   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27709   FORCE_RESET_NEXT_VIEW (table->view);
27710   table->symviews_since_reset = 0;
27711 
27712   return table;
27713 }
27714 
27715 /* Lookup the "current" table into which we emit line info, so
27716    that we don't have to do it for every source line.  */
27717 
27718 static void
set_cur_line_info_table(section * sec)27719 set_cur_line_info_table (section *sec)
27720 {
27721   dw_line_info_table *table;
27722 
27723   if (sec == text_section)
27724     table = text_section_line_info;
27725   else if (sec == cold_text_section)
27726     {
27727       table = cold_text_section_line_info;
27728       if (!table)
27729 	{
27730 	  cold_text_section_line_info = table = new_line_info_table ();
27731 	  table->end_label = cold_end_label;
27732 	}
27733     }
27734   else
27735     {
27736       const char *end_label;
27737 
27738       if (crtl->has_bb_partition)
27739 	{
27740 	  if (in_cold_section_p)
27741 	    end_label = crtl->subsections.cold_section_end_label;
27742 	  else
27743 	    end_label = crtl->subsections.hot_section_end_label;
27744 	}
27745       else
27746 	{
27747 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
27748 	  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27749 				       current_function_funcdef_no);
27750 	  end_label = ggc_strdup (label);
27751 	}
27752 
27753       table = new_line_info_table ();
27754       table->end_label = end_label;
27755 
27756       vec_safe_push (separate_line_info, table);
27757     }
27758 
27759   if (output_asm_line_debug_info ())
27760     table->is_stmt = (cur_line_info_table
27761 		      ? cur_line_info_table->is_stmt
27762 		      : DWARF_LINE_DEFAULT_IS_STMT_START);
27763   cur_line_info_table = table;
27764 }
27765 
27766 
27767 /* We need to reset the locations at the beginning of each
27768    function. We can't do this in the end_function hook, because the
27769    declarations that use the locations won't have been output when
27770    that hook is called.  Also compute have_multiple_function_sections here.  */
27771 
27772 static void
dwarf2out_begin_function(tree fun)27773 dwarf2out_begin_function (tree fun)
27774 {
27775   section *sec = function_section (fun);
27776 
27777   if (sec != text_section)
27778     have_multiple_function_sections = true;
27779 
27780   if (crtl->has_bb_partition && !cold_text_section)
27781     {
27782       gcc_assert (current_function_decl == fun);
27783       cold_text_section = unlikely_text_section ();
27784       switch_to_section (cold_text_section);
27785       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27786       switch_to_section (sec);
27787     }
27788 
27789   dwarf2out_note_section_used ();
27790   call_site_count = 0;
27791   tail_call_site_count = 0;
27792 
27793   set_cur_line_info_table (sec);
27794   FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27795 }
27796 
27797 /* Helper function of dwarf2out_end_function, called only after emitting
27798    the very first function into assembly.  Check if some .debug_loc range
27799    might end with a .LVL* label that could be equal to .Ltext0.
27800    In that case we must force using absolute addresses in .debug_loc ranges,
27801    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27802    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27803    list terminator.
27804    Set have_multiple_function_sections to true in that case and
27805    terminate htab traversal.  */
27806 
27807 int
find_empty_loc_ranges_at_text_label(var_loc_list ** slot,int)27808 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27809 {
27810   var_loc_list *entry = *slot;
27811   struct var_loc_node *node;
27812 
27813   node = entry->first;
27814   if (node && node->next && node->next->label)
27815     {
27816       unsigned int i;
27817       const char *label = node->next->label;
27818       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27819 
27820       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27821 	{
27822 	  ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27823 	  if (strcmp (label, loclabel) == 0)
27824 	    {
27825 	      have_multiple_function_sections = true;
27826 	      return 0;
27827 	    }
27828 	}
27829     }
27830   return 1;
27831 }
27832 
27833 /* Hook called after emitting a function into assembly.
27834    This does something only for the very first function emitted.  */
27835 
27836 static void
dwarf2out_end_function(unsigned int)27837 dwarf2out_end_function (unsigned int)
27838 {
27839   if (in_first_function_p
27840       && !have_multiple_function_sections
27841       && first_loclabel_num_not_at_text_label
27842       && decl_loc_table)
27843     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27844   in_first_function_p = false;
27845   maybe_at_text_label_p = false;
27846 }
27847 
27848 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
27849    front-ends register a translation unit even before dwarf2out_init is
27850    called.  */
27851 static tree main_translation_unit = NULL_TREE;
27852 
27853 /* Hook called by front-ends after they built their main translation unit.
27854    Associate comp_unit_die to UNIT.  */
27855 
27856 static void
dwarf2out_register_main_translation_unit(tree unit)27857 dwarf2out_register_main_translation_unit (tree unit)
27858 {
27859   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27860 	      && main_translation_unit == NULL_TREE);
27861   main_translation_unit = unit;
27862   /* If dwarf2out_init has not been called yet, it will perform the association
27863      itself looking at main_translation_unit.  */
27864   if (decl_die_table != NULL)
27865     equate_decl_number_to_die (unit, comp_unit_die ());
27866 }
27867 
27868 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
27869 
27870 static void
push_dw_line_info_entry(dw_line_info_table * table,enum dw_line_info_opcode opcode,unsigned int val)27871 push_dw_line_info_entry (dw_line_info_table *table,
27872 			 enum dw_line_info_opcode opcode, unsigned int val)
27873 {
27874   dw_line_info_entry e;
27875   e.opcode = opcode;
27876   e.val = val;
27877   vec_safe_push (table->entries, e);
27878 }
27879 
27880 /* Output a label to mark the beginning of a source code line entry
27881    and record information relating to this source line, in
27882    'line_info_table' for later output of the .debug_line section.  */
27883 /* ??? The discriminator parameter ought to be unsigned.  */
27884 
27885 static void
dwarf2out_source_line(unsigned int line,unsigned int column,const char * filename,int discriminator,bool is_stmt)27886 dwarf2out_source_line (unsigned int line, unsigned int column,
27887 		       const char *filename,
27888                        int discriminator, bool is_stmt)
27889 {
27890   unsigned int file_num;
27891   dw_line_info_table *table;
27892   static var_loc_view lvugid;
27893 
27894   if (debug_info_level < DINFO_LEVEL_TERSE)
27895     return;
27896 
27897   table = cur_line_info_table;
27898 
27899   if (line == 0)
27900     {
27901       if (debug_variable_location_views
27902 	  && output_asm_line_debug_info ()
27903 	  && table && !RESETTING_VIEW_P (table->view))
27904 	{
27905 	  /* If we're using the assembler to compute view numbers, we
27906 	     can't issue a .loc directive for line zero, so we can't
27907 	     get a view number at this point.  We might attempt to
27908 	     compute it from the previous view, or equate it to a
27909 	     subsequent view (though it might not be there!), but
27910 	     since we're omitting the line number entry, we might as
27911 	     well omit the view number as well.  That means pretending
27912 	     it's a view number zero, which might very well turn out
27913 	     to be correct.  ??? Extend the assembler so that the
27914 	     compiler could emit e.g. ".locview .LVU#", to output a
27915 	     view without changing line number information.  We'd then
27916 	     have to count it in symviews_since_reset; when it's omitted,
27917 	     it doesn't count.  */
27918 	  if (!zero_view_p)
27919 	    zero_view_p = BITMAP_GGC_ALLOC ();
27920 	  bitmap_set_bit (zero_view_p, table->view);
27921 	  if (flag_debug_asm)
27922 	    {
27923 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
27924 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27925 	      fprintf (asm_out_file, "\t%s line 0, omitted view ",
27926 		       ASM_COMMENT_START);
27927 	      assemble_name (asm_out_file, label);
27928 	      putc ('\n', asm_out_file);
27929 	    }
27930 	  table->view = ++lvugid;
27931 	}
27932       return;
27933     }
27934 
27935   /* The discriminator column was added in dwarf4.  Simplify the below
27936      by simply removing it if we're not supposed to output it.  */
27937   if (dwarf_version < 4 && dwarf_strict)
27938     discriminator = 0;
27939 
27940   if (!debug_column_info)
27941     column = 0;
27942 
27943   file_num = maybe_emit_file (lookup_filename (filename));
27944 
27945   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
27946      the debugger has used the second (possibly duplicate) line number
27947      at the beginning of the function to mark the end of the prologue.
27948      We could eliminate any other duplicates within the function.  For
27949      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27950      that second line number entry.  */
27951   /* Recall that this end-of-prologue indication is *not* the same thing
27952      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
27953      to which the hook corresponds, follows the last insn that was
27954      emitted by gen_prologue.  What we need is to precede the first insn
27955      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27956      insn that corresponds to something the user wrote.  These may be
27957      very different locations once scheduling is enabled.  */
27958 
27959   if (0 && file_num == table->file_num
27960       && line == table->line_num
27961       && column == table->column_num
27962       && discriminator == table->discrim_num
27963       && is_stmt == table->is_stmt)
27964     return;
27965 
27966   switch_to_section (current_function_section ());
27967 
27968   /* If requested, emit something human-readable.  */
27969   if (flag_debug_asm)
27970     {
27971       if (debug_column_info)
27972 	fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27973 		 filename, line, column);
27974       else
27975 	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27976 		 filename, line);
27977     }
27978 
27979   if (output_asm_line_debug_info ())
27980     {
27981       /* Emit the .loc directive understood by GNU as.  */
27982       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27983 	 file_num, line, is_stmt, discriminator */
27984       fputs ("\t.loc ", asm_out_file);
27985       fprint_ul (asm_out_file, file_num);
27986       putc (' ', asm_out_file);
27987       fprint_ul (asm_out_file, line);
27988       putc (' ', asm_out_file);
27989       fprint_ul (asm_out_file, column);
27990 
27991       if (is_stmt != table->is_stmt)
27992 	{
27993 #if HAVE_GAS_LOC_STMT
27994 	  fputs (" is_stmt ", asm_out_file);
27995 	  putc (is_stmt ? '1' : '0', asm_out_file);
27996 #endif
27997 	}
27998       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27999 	{
28000 	  gcc_assert (discriminator > 0);
28001 	  fputs (" discriminator ", asm_out_file);
28002 	  fprint_ul (asm_out_file, (unsigned long) discriminator);
28003 	}
28004       if (debug_variable_location_views)
28005 	{
28006 	  if (!RESETTING_VIEW_P (table->view))
28007 	    {
28008 	      table->symviews_since_reset++;
28009 	      if (table->symviews_since_reset > symview_upper_bound)
28010 		symview_upper_bound = table->symviews_since_reset;
28011 	      /* When we're using the assembler to compute view
28012 		 numbers, we output symbolic labels after "view" in
28013 		 .loc directives, and the assembler will set them for
28014 		 us, so that we can refer to the view numbers in
28015 		 location lists.  The only exceptions are when we know
28016 		 a view will be zero: "-0" is a forced reset, used
28017 		 e.g. in the beginning of functions, whereas "0" tells
28018 		 the assembler to check that there was a PC change
28019 		 since the previous view, in a way that implicitly
28020 		 resets the next view.  */
28021 	      fputs (" view ", asm_out_file);
28022 	      char label[MAX_ARTIFICIAL_LABEL_BYTES];
28023 	      ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28024 	      assemble_name (asm_out_file, label);
28025 	      table->view = ++lvugid;
28026 	    }
28027 	  else
28028 	    {
28029 	      table->symviews_since_reset = 0;
28030 	      if (FORCE_RESETTING_VIEW_P (table->view))
28031 		fputs (" view -0", asm_out_file);
28032 	      else
28033 		fputs (" view 0", asm_out_file);
28034 	      /* Mark the present view as a zero view.  Earlier debug
28035 		 binds may have already added its id to loclists to be
28036 		 emitted later, so we can't reuse the id for something
28037 		 else.  However, it's good to know whether a view is
28038 		 known to be zero, because then we may be able to
28039 		 optimize out locviews that are all zeros, so take
28040 		 note of it in zero_view_p.  */
28041 	      if (!zero_view_p)
28042 		zero_view_p = BITMAP_GGC_ALLOC ();
28043 	      bitmap_set_bit (zero_view_p, lvugid);
28044 	      table->view = ++lvugid;
28045 	    }
28046 	}
28047       putc ('\n', asm_out_file);
28048     }
28049   else
28050     {
28051       unsigned int label_num = ++line_info_label_num;
28052 
28053       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28054 
28055       if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28056 	push_dw_line_info_entry (table, LI_adv_address, label_num);
28057       else
28058 	push_dw_line_info_entry (table, LI_set_address, label_num);
28059       if (debug_variable_location_views)
28060 	{
28061 	  bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28062 	  if (resetting)
28063 	    table->view = 0;
28064 
28065 	  if (flag_debug_asm)
28066 	    fprintf (asm_out_file, "\t%s view %s%d\n",
28067 		     ASM_COMMENT_START,
28068 		     resetting ? "-" : "",
28069 		     table->view);
28070 
28071 	  table->view++;
28072 	}
28073       if (file_num != table->file_num)
28074 	push_dw_line_info_entry (table, LI_set_file, file_num);
28075       if (discriminator != table->discrim_num)
28076 	push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28077       if (is_stmt != table->is_stmt)
28078 	push_dw_line_info_entry (table, LI_negate_stmt, 0);
28079       push_dw_line_info_entry (table, LI_set_line, line);
28080       if (debug_column_info)
28081 	push_dw_line_info_entry (table, LI_set_column, column);
28082     }
28083 
28084   table->file_num = file_num;
28085   table->line_num = line;
28086   table->column_num = column;
28087   table->discrim_num = discriminator;
28088   table->is_stmt = is_stmt;
28089   table->in_use = true;
28090 }
28091 
28092 /* Record the beginning of a new source file.  */
28093 
28094 static void
dwarf2out_start_source_file(unsigned int lineno,const char * filename)28095 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28096 {
28097   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28098     {
28099       macinfo_entry e;
28100       e.code = DW_MACINFO_start_file;
28101       e.lineno = lineno;
28102       e.info = ggc_strdup (filename);
28103       vec_safe_push (macinfo_table, e);
28104     }
28105 }
28106 
28107 /* Record the end of a source file.  */
28108 
28109 static void
dwarf2out_end_source_file(unsigned int lineno ATTRIBUTE_UNUSED)28110 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28111 {
28112   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28113     {
28114       macinfo_entry e;
28115       e.code = DW_MACINFO_end_file;
28116       e.lineno = lineno;
28117       e.info = NULL;
28118       vec_safe_push (macinfo_table, e);
28119     }
28120 }
28121 
28122 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
28123    the tail part of the directive line, i.e. the part which is past the
28124    initial whitespace, #, whitespace, directive-name, whitespace part.  */
28125 
28126 static void
dwarf2out_define(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)28127 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28128 		  const char *buffer ATTRIBUTE_UNUSED)
28129 {
28130   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28131     {
28132       macinfo_entry e;
28133       /* Insert a dummy first entry to be able to optimize the whole
28134 	 predefined macro block using DW_MACRO_import.  */
28135       if (macinfo_table->is_empty () && lineno <= 1)
28136 	{
28137 	  e.code = 0;
28138 	  e.lineno = 0;
28139 	  e.info = NULL;
28140 	  vec_safe_push (macinfo_table, e);
28141 	}
28142       e.code = DW_MACINFO_define;
28143       e.lineno = lineno;
28144       e.info = ggc_strdup (buffer);
28145       vec_safe_push (macinfo_table, e);
28146     }
28147 }
28148 
28149 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
28150    the tail part of the directive line, i.e. the part which is past the
28151    initial whitespace, #, whitespace, directive-name, whitespace part.  */
28152 
28153 static void
dwarf2out_undef(unsigned int lineno ATTRIBUTE_UNUSED,const char * buffer ATTRIBUTE_UNUSED)28154 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28155 		 const char *buffer ATTRIBUTE_UNUSED)
28156 {
28157   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28158     {
28159       macinfo_entry e;
28160       /* Insert a dummy first entry to be able to optimize the whole
28161 	 predefined macro block using DW_MACRO_import.  */
28162       if (macinfo_table->is_empty () && lineno <= 1)
28163 	{
28164 	  e.code = 0;
28165 	  e.lineno = 0;
28166 	  e.info = NULL;
28167 	  vec_safe_push (macinfo_table, e);
28168 	}
28169       e.code = DW_MACINFO_undef;
28170       e.lineno = lineno;
28171       e.info = ggc_strdup (buffer);
28172       vec_safe_push (macinfo_table, e);
28173     }
28174 }
28175 
28176 /* Helpers to manipulate hash table of CUs.  */
28177 
28178 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28179 {
28180   static inline hashval_t hash (const macinfo_entry *);
28181   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28182 };
28183 
28184 inline hashval_t
hash(const macinfo_entry * entry)28185 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28186 {
28187   return htab_hash_string (entry->info);
28188 }
28189 
28190 inline bool
equal(const macinfo_entry * entry1,const macinfo_entry * entry2)28191 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28192 			     const macinfo_entry *entry2)
28193 {
28194   return !strcmp (entry1->info, entry2->info);
28195 }
28196 
28197 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28198 
28199 /* Output a single .debug_macinfo entry.  */
28200 
28201 static void
output_macinfo_op(macinfo_entry * ref)28202 output_macinfo_op (macinfo_entry *ref)
28203 {
28204   int file_num;
28205   size_t len;
28206   struct indirect_string_node *node;
28207   char label[MAX_ARTIFICIAL_LABEL_BYTES];
28208   struct dwarf_file_data *fd;
28209 
28210   switch (ref->code)
28211     {
28212     case DW_MACINFO_start_file:
28213       fd = lookup_filename (ref->info);
28214       file_num = maybe_emit_file (fd);
28215       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28216       dw2_asm_output_data_uleb128 (ref->lineno,
28217 				   "Included from line number %lu",
28218 				   (unsigned long) ref->lineno);
28219       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28220       break;
28221     case DW_MACINFO_end_file:
28222       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28223       break;
28224     case DW_MACINFO_define:
28225     case DW_MACINFO_undef:
28226       len = strlen (ref->info) + 1;
28227       if (!dwarf_strict
28228 	  && len > DWARF_OFFSET_SIZE
28229 	  && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28230 	  && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28231 	{
28232 	  ref->code = ref->code == DW_MACINFO_define
28233 		      ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28234 	  output_macinfo_op (ref);
28235 	  return;
28236 	}
28237       dw2_asm_output_data (1, ref->code,
28238 			   ref->code == DW_MACINFO_define
28239 			   ? "Define macro" : "Undefine macro");
28240       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28241 				   (unsigned long) ref->lineno);
28242       dw2_asm_output_nstring (ref->info, -1, "The macro");
28243       break;
28244     case DW_MACRO_define_strp:
28245     case DW_MACRO_undef_strp:
28246       /* NB: dwarf2out_finish performs:
28247 	   1. save_macinfo_strings
28248 	   2. hash table traverse of index_string
28249 	   3. output_macinfo -> output_macinfo_op
28250 	   4. output_indirect_strings
28251 		-> hash table traverse of output_index_string
28252 
28253 	 When output_macinfo_op is called, all index strings have been
28254 	 added to hash table by save_macinfo_strings and we can't pass
28255 	 INSERT to find_slot_with_hash which may expand hash table, even
28256 	 if no insertion is needed, and change hash table traverse order
28257 	 between index_string and output_index_string.  */
28258       node = find_AT_string (ref->info, NO_INSERT);
28259       gcc_assert (node
28260 		  && (node->form == DW_FORM_strp
28261 		      || node->form == dwarf_FORM (DW_FORM_strx)));
28262       dw2_asm_output_data (1, ref->code,
28263 			   ref->code == DW_MACRO_define_strp
28264 			   ? "Define macro strp"
28265 			   : "Undefine macro strp");
28266       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28267 				   (unsigned long) ref->lineno);
28268       if (node->form == DW_FORM_strp)
28269         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28270                                debug_str_section, "The macro: \"%s\"",
28271                                ref->info);
28272       else
28273         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28274                                      ref->info);
28275       break;
28276     case DW_MACRO_import:
28277       dw2_asm_output_data (1, ref->code, "Import");
28278       ASM_GENERATE_INTERNAL_LABEL (label,
28279 				   DEBUG_MACRO_SECTION_LABEL,
28280 				   ref->lineno + macinfo_label_base);
28281       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28282       break;
28283     default:
28284       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28285 	       ASM_COMMENT_START, (unsigned long) ref->code);
28286       break;
28287     }
28288 }
28289 
28290 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28291    other compilation unit .debug_macinfo sections.  IDX is the first
28292    index of a define/undef, return the number of ops that should be
28293    emitted in a comdat .debug_macinfo section and emit
28294    a DW_MACRO_import entry referencing it.
28295    If the define/undef entry should be emitted normally, return 0.  */
28296 
28297 static unsigned
optimize_macinfo_range(unsigned int idx,vec<macinfo_entry,va_gc> * files,macinfo_hash_type ** macinfo_htab)28298 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28299 			macinfo_hash_type **macinfo_htab)
28300 {
28301   macinfo_entry *first, *second, *cur, *inc;
28302   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28303   unsigned char checksum[16];
28304   struct md5_ctx ctx;
28305   char *grp_name, *tail;
28306   const char *base;
28307   unsigned int i, count, encoded_filename_len, linebuf_len;
28308   macinfo_entry **slot;
28309 
28310   first = &(*macinfo_table)[idx];
28311   second = &(*macinfo_table)[idx + 1];
28312 
28313   /* Optimize only if there are at least two consecutive define/undef ops,
28314      and either all of them are before first DW_MACINFO_start_file
28315      with lineno {0,1} (i.e. predefined macro block), or all of them are
28316      in some included header file.  */
28317   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28318     return 0;
28319   if (vec_safe_is_empty (files))
28320     {
28321       if (first->lineno > 1 || second->lineno > 1)
28322 	return 0;
28323     }
28324   else if (first->lineno == 0)
28325     return 0;
28326 
28327   /* Find the last define/undef entry that can be grouped together
28328      with first and at the same time compute md5 checksum of their
28329      codes, linenumbers and strings.  */
28330   md5_init_ctx (&ctx);
28331   for (i = idx; macinfo_table->iterate (i, &cur); i++)
28332     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28333       break;
28334     else if (vec_safe_is_empty (files) && cur->lineno > 1)
28335       break;
28336     else
28337       {
28338 	unsigned char code = cur->code;
28339 	md5_process_bytes (&code, 1, &ctx);
28340 	checksum_uleb128 (cur->lineno, &ctx);
28341 	md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28342       }
28343   md5_finish_ctx (&ctx, checksum);
28344   count = i - idx;
28345 
28346   /* From the containing include filename (if any) pick up just
28347      usable characters from its basename.  */
28348   if (vec_safe_is_empty (files))
28349     base = "";
28350   else
28351     base = lbasename (files->last ().info);
28352   for (encoded_filename_len = 0, i = 0; base[i]; i++)
28353     if (ISIDNUM (base[i]) || base[i] == '.')
28354       encoded_filename_len++;
28355   /* Count . at the end.  */
28356   if (encoded_filename_len)
28357     encoded_filename_len++;
28358 
28359   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28360   linebuf_len = strlen (linebuf);
28361 
28362   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
28363   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28364 			 + 16 * 2 + 1);
28365   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28366   tail = grp_name + 4;
28367   if (encoded_filename_len)
28368     {
28369       for (i = 0; base[i]; i++)
28370 	if (ISIDNUM (base[i]) || base[i] == '.')
28371 	  *tail++ = base[i];
28372       *tail++ = '.';
28373     }
28374   memcpy (tail, linebuf, linebuf_len);
28375   tail += linebuf_len;
28376   *tail++ = '.';
28377   for (i = 0; i < 16; i++)
28378     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28379 
28380   /* Construct a macinfo_entry for DW_MACRO_import
28381      in the empty vector entry before the first define/undef.  */
28382   inc = &(*macinfo_table)[idx - 1];
28383   inc->code = DW_MACRO_import;
28384   inc->lineno = 0;
28385   inc->info = ggc_strdup (grp_name);
28386   if (!*macinfo_htab)
28387     *macinfo_htab = new macinfo_hash_type (10);
28388   /* Avoid emitting duplicates.  */
28389   slot = (*macinfo_htab)->find_slot (inc, INSERT);
28390   if (*slot != NULL)
28391     {
28392       inc->code = 0;
28393       inc->info = NULL;
28394       /* If such an entry has been used before, just emit
28395 	 a DW_MACRO_import op.  */
28396       inc = *slot;
28397       output_macinfo_op (inc);
28398       /* And clear all macinfo_entry in the range to avoid emitting them
28399 	 in the second pass.  */
28400       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28401 	{
28402 	  cur->code = 0;
28403 	  cur->info = NULL;
28404 	}
28405     }
28406   else
28407     {
28408       *slot = inc;
28409       inc->lineno = (*macinfo_htab)->elements ();
28410       output_macinfo_op (inc);
28411     }
28412   return count;
28413 }
28414 
28415 /* Save any strings needed by the macinfo table in the debug str
28416    table.  All strings must be collected into the table by the time
28417    index_string is called.  */
28418 
28419 static void
save_macinfo_strings(void)28420 save_macinfo_strings (void)
28421 {
28422   unsigned len;
28423   unsigned i;
28424   macinfo_entry *ref;
28425 
28426   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28427     {
28428       switch (ref->code)
28429         {
28430           /* Match the logic in output_macinfo_op to decide on
28431              indirect strings.  */
28432           case DW_MACINFO_define:
28433           case DW_MACINFO_undef:
28434             len = strlen (ref->info) + 1;
28435             if (!dwarf_strict
28436                 && len > DWARF_OFFSET_SIZE
28437                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28438                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28439               set_indirect_string (find_AT_string (ref->info));
28440             break;
28441 	  case DW_MACINFO_start_file:
28442 	    /* -gsplit-dwarf -g3 will also output filename as indirect
28443 	       string.  */
28444 	    if (!dwarf_split_debug_info)
28445 	      break;
28446 	    /* Fall through. */
28447 	  case DW_MACRO_define_strp:
28448 	  case DW_MACRO_undef_strp:
28449             set_indirect_string (find_AT_string (ref->info));
28450             break;
28451           default:
28452             break;
28453         }
28454     }
28455 }
28456 
28457 /* Output macinfo section(s).  */
28458 
28459 static void
output_macinfo(const char * debug_line_label,bool early_lto_debug)28460 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28461 {
28462   unsigned i;
28463   unsigned long length = vec_safe_length (macinfo_table);
28464   macinfo_entry *ref;
28465   vec<macinfo_entry, va_gc> *files = NULL;
28466   macinfo_hash_type *macinfo_htab = NULL;
28467   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28468 
28469   if (! length)
28470     return;
28471 
28472   /* output_macinfo* uses these interchangeably.  */
28473   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28474 	      && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28475 	      && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28476 	      && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28477 
28478   /* AIX Assembler inserts the length, so adjust the reference to match the
28479      offset expected by debuggers.  */
28480   strcpy (dl_section_ref, debug_line_label);
28481   if (XCOFF_DEBUGGING_INFO)
28482     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28483 
28484   /* For .debug_macro emit the section header.  */
28485   if (!dwarf_strict || dwarf_version >= 5)
28486     {
28487       dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28488 			   "DWARF macro version number");
28489       if (DWARF_OFFSET_SIZE == 8)
28490 	dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28491       else
28492 	dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28493       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28494                              debug_line_section, NULL);
28495     }
28496 
28497   /* In the first loop, it emits the primary .debug_macinfo section
28498      and after each emitted op the macinfo_entry is cleared.
28499      If a longer range of define/undef ops can be optimized using
28500      DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28501      the vector before the first define/undef in the range and the
28502      whole range of define/undef ops is not emitted and kept.  */
28503   for (i = 0; macinfo_table->iterate (i, &ref); i++)
28504     {
28505       switch (ref->code)
28506 	{
28507 	case DW_MACINFO_start_file:
28508 	  vec_safe_push (files, *ref);
28509 	  break;
28510 	case DW_MACINFO_end_file:
28511 	  if (!vec_safe_is_empty (files))
28512 	    files->pop ();
28513 	  break;
28514 	case DW_MACINFO_define:
28515 	case DW_MACINFO_undef:
28516 	  if ((!dwarf_strict || dwarf_version >= 5)
28517 	      && HAVE_COMDAT_GROUP
28518 	      && vec_safe_length (files) != 1
28519 	      && i > 0
28520 	      && i + 1 < length
28521 	      && (*macinfo_table)[i - 1].code == 0)
28522 	    {
28523 	      unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28524 	      if (count)
28525 		{
28526 		  i += count - 1;
28527 		  continue;
28528 		}
28529 	    }
28530 	  break;
28531 	case 0:
28532 	  /* A dummy entry may be inserted at the beginning to be able
28533 	     to optimize the whole block of predefined macros.  */
28534 	  if (i == 0)
28535 	    continue;
28536 	default:
28537 	  break;
28538 	}
28539       output_macinfo_op (ref);
28540       ref->info = NULL;
28541       ref->code = 0;
28542     }
28543 
28544   if (!macinfo_htab)
28545     return;
28546 
28547   /* Save the number of transparent includes so we can adjust the
28548      label number for the fat LTO object DWARF.  */
28549   unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28550 
28551   delete macinfo_htab;
28552   macinfo_htab = NULL;
28553 
28554   /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28555      terminate the current chain and switch to a new comdat .debug_macinfo
28556      section and emit the define/undef entries within it.  */
28557   for (i = 0; macinfo_table->iterate (i, &ref); i++)
28558     switch (ref->code)
28559       {
28560       case 0:
28561 	continue;
28562       case DW_MACRO_import:
28563 	{
28564 	  char label[MAX_ARTIFICIAL_LABEL_BYTES];
28565 	  tree comdat_key = get_identifier (ref->info);
28566 	  /* Terminate the previous .debug_macinfo section.  */
28567 	  dw2_asm_output_data (1, 0, "End compilation unit");
28568 	  targetm.asm_out.named_section (debug_macinfo_section_name,
28569 					 SECTION_DEBUG
28570 					 | SECTION_LINKONCE
28571 					 | (early_lto_debug
28572 					    ? SECTION_EXCLUDE : 0),
28573 					 comdat_key);
28574 	  ASM_GENERATE_INTERNAL_LABEL (label,
28575 				       DEBUG_MACRO_SECTION_LABEL,
28576 				       ref->lineno + macinfo_label_base);
28577 	  ASM_OUTPUT_LABEL (asm_out_file, label);
28578 	  ref->code = 0;
28579 	  ref->info = NULL;
28580 	  dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28581 			       "DWARF macro version number");
28582 	  if (DWARF_OFFSET_SIZE == 8)
28583 	    dw2_asm_output_data (1, 1, "Flags: 64-bit");
28584 	  else
28585 	    dw2_asm_output_data (1, 0, "Flags: 32-bit");
28586 	}
28587 	break;
28588       case DW_MACINFO_define:
28589       case DW_MACINFO_undef:
28590 	output_macinfo_op (ref);
28591 	ref->code = 0;
28592 	ref->info = NULL;
28593 	break;
28594       default:
28595 	gcc_unreachable ();
28596       }
28597 
28598   macinfo_label_base += macinfo_label_base_adj;
28599 }
28600 
28601 /* Initialize the various sections and labels for dwarf output and prefix
28602    them with PREFIX if non-NULL.  Returns the generation (zero based
28603    number of times function was called).  */
28604 
28605 static unsigned
init_sections_and_labels(bool early_lto_debug)28606 init_sections_and_labels (bool early_lto_debug)
28607 {
28608   /* As we may get called multiple times have a generation count for
28609      labels.  */
28610   static unsigned generation = 0;
28611 
28612   if (early_lto_debug)
28613     {
28614       if (!dwarf_split_debug_info)
28615 	{
28616 	  debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28617 					    SECTION_DEBUG | SECTION_EXCLUDE,
28618 					    NULL);
28619 	  debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28620 					      SECTION_DEBUG | SECTION_EXCLUDE,
28621 					      NULL);
28622 	  debug_macinfo_section_name
28623 	    = ((dwarf_strict && dwarf_version < 5)
28624 	       ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28625 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28626 					       SECTION_DEBUG
28627 					       | SECTION_EXCLUDE, NULL);
28628 	}
28629       else
28630 	{
28631 	  /* ???  Which of the following do we need early?  */
28632 	  debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28633 					    SECTION_DEBUG | SECTION_EXCLUDE,
28634 					    NULL);
28635 	  debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28636 					      SECTION_DEBUG | SECTION_EXCLUDE,
28637 					      NULL);
28638 	  debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28639 						     SECTION_DEBUG
28640 						     | SECTION_EXCLUDE, NULL);
28641 	  debug_skeleton_abbrev_section
28642 	    = get_section (DEBUG_LTO_ABBREV_SECTION,
28643 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28644 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28645 				       DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28646 				       generation);
28647 
28648 	  /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28649 	     stay in the main .o, but the skeleton_line goes into the split
28650 	     off dwo.  */
28651 	  debug_skeleton_line_section
28652 	    = get_section (DEBUG_LTO_LINE_SECTION,
28653 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28654 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28655 				       DEBUG_SKELETON_LINE_SECTION_LABEL,
28656 				       generation);
28657 	  debug_str_offsets_section
28658 	    = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28659 			   SECTION_DEBUG | SECTION_EXCLUDE,
28660 			   NULL);
28661 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28662 				       DEBUG_SKELETON_INFO_SECTION_LABEL,
28663 				       generation);
28664 	  debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28665 					       DEBUG_STR_DWO_SECTION_FLAGS,
28666 					       NULL);
28667 	  debug_macinfo_section_name
28668 	    = ((dwarf_strict && dwarf_version < 5)
28669 	       ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28670 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28671 					       SECTION_DEBUG | SECTION_EXCLUDE,
28672 					       NULL);
28673 	}
28674       /* For macro info and the file table we have to refer to a
28675 	 debug_line section.  */
28676       debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28677 					SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28678       ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28679 				   DEBUG_LINE_SECTION_LABEL, generation);
28680 
28681       debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28682 				       DEBUG_STR_SECTION_FLAGS
28683 				       | SECTION_EXCLUDE, NULL);
28684       if (!dwarf_split_debug_info)
28685 	debug_line_str_section
28686 	  = get_section (DEBUG_LTO_LINE_STR_SECTION,
28687 			 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28688     }
28689   else
28690     {
28691       if (!dwarf_split_debug_info)
28692 	{
28693 	  debug_info_section = get_section (DEBUG_INFO_SECTION,
28694 					    SECTION_DEBUG, NULL);
28695 	  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28696 					      SECTION_DEBUG, NULL);
28697 	  debug_loc_section = get_section (dwarf_version >= 5
28698 					   ? DEBUG_LOCLISTS_SECTION
28699 					   : DEBUG_LOC_SECTION,
28700 					   SECTION_DEBUG, NULL);
28701 	  debug_macinfo_section_name
28702 	    = ((dwarf_strict && dwarf_version < 5)
28703 	       ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28704 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28705 					       SECTION_DEBUG, NULL);
28706 	}
28707       else
28708 	{
28709 	  debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28710 					    SECTION_DEBUG | SECTION_EXCLUDE,
28711 					    NULL);
28712 	  debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28713 					      SECTION_DEBUG | SECTION_EXCLUDE,
28714 					      NULL);
28715 	  debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28716 					    SECTION_DEBUG, NULL);
28717 	  debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28718 						     SECTION_DEBUG, NULL);
28719 	  debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28720 						       SECTION_DEBUG, NULL);
28721 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28722 				       DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28723 				       generation);
28724 
28725 	  /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28726 	     stay in the main .o, but the skeleton_line goes into the
28727 	     split off dwo.  */
28728 	  debug_skeleton_line_section
28729 	      = get_section (DEBUG_DWO_LINE_SECTION,
28730 			     SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28731 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28732 				       DEBUG_SKELETON_LINE_SECTION_LABEL,
28733 				       generation);
28734 	  debug_str_offsets_section
28735 	    = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28736 			   SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28737 	  ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28738 				       DEBUG_SKELETON_INFO_SECTION_LABEL,
28739 				       generation);
28740 	  debug_loc_section = get_section (dwarf_version >= 5
28741 					   ? DEBUG_DWO_LOCLISTS_SECTION
28742 					   : DEBUG_DWO_LOC_SECTION,
28743 					   SECTION_DEBUG | SECTION_EXCLUDE,
28744 					   NULL);
28745 	  debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28746 					       DEBUG_STR_DWO_SECTION_FLAGS,
28747 					       NULL);
28748 	  debug_macinfo_section_name
28749 	    = ((dwarf_strict && dwarf_version < 5)
28750 	       ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28751 	  debug_macinfo_section = get_section (debug_macinfo_section_name,
28752 					       SECTION_DEBUG | SECTION_EXCLUDE,
28753 					       NULL);
28754 	}
28755       debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28756 					   SECTION_DEBUG, NULL);
28757       debug_line_section = get_section (DEBUG_LINE_SECTION,
28758 					SECTION_DEBUG, NULL);
28759       debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28760 					    SECTION_DEBUG, NULL);
28761       debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28762 					    SECTION_DEBUG, NULL);
28763       debug_str_section = get_section (DEBUG_STR_SECTION,
28764 				       DEBUG_STR_SECTION_FLAGS, NULL);
28765       if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28766 	debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28767 					      DEBUG_STR_SECTION_FLAGS, NULL);
28768 
28769       debug_ranges_section = get_section (dwarf_version >= 5
28770 					  ? DEBUG_RNGLISTS_SECTION
28771 					  : DEBUG_RANGES_SECTION,
28772 					  SECTION_DEBUG, NULL);
28773       debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28774 					 SECTION_DEBUG, NULL);
28775     }
28776 
28777   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28778 			       DEBUG_ABBREV_SECTION_LABEL, generation);
28779   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28780 			       DEBUG_INFO_SECTION_LABEL, generation);
28781   info_section_emitted = false;
28782   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28783 			       DEBUG_LINE_SECTION_LABEL, generation);
28784   /* There are up to 4 unique ranges labels per generation.
28785      See also output_rnglists.  */
28786   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28787 			       DEBUG_RANGES_SECTION_LABEL, generation * 4);
28788   if (dwarf_version >= 5 && dwarf_split_debug_info)
28789     ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28790 				 DEBUG_RANGES_SECTION_LABEL,
28791 				 1 + generation * 4);
28792   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28793 			       DEBUG_ADDR_SECTION_LABEL, generation);
28794   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28795 			       (dwarf_strict && dwarf_version < 5)
28796 			       ? DEBUG_MACINFO_SECTION_LABEL
28797 			       : DEBUG_MACRO_SECTION_LABEL, generation);
28798   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28799 			       generation);
28800 
28801   ++generation;
28802   return generation - 1;
28803 }
28804 
28805 /* Set up for Dwarf output at the start of compilation.  */
28806 
28807 static void
dwarf2out_init(const char * filename ATTRIBUTE_UNUSED)28808 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28809 {
28810   /* Allocate the file_table.  */
28811   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28812 
28813 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28814   /* Allocate the decl_die_table.  */
28815   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28816 
28817   /* Allocate the decl_loc_table.  */
28818   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28819 
28820   /* Allocate the cached_dw_loc_list_table.  */
28821   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28822 
28823   /* Allocate the initial hunk of the abbrev_die_table.  */
28824   vec_alloc (abbrev_die_table, 256);
28825   /* Zero-th entry is allocated, but unused.  */
28826   abbrev_die_table->quick_push (NULL);
28827 
28828   /* Allocate the dwarf_proc_stack_usage_map.  */
28829   dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28830 
28831   /* Allocate the pubtypes and pubnames vectors.  */
28832   vec_alloc (pubname_table, 32);
28833   vec_alloc (pubtype_table, 32);
28834 
28835   vec_alloc (incomplete_types, 64);
28836 
28837   vec_alloc (used_rtx_array, 32);
28838 
28839   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28840     vec_alloc (macinfo_table, 64);
28841 #endif
28842 
28843   /* If front-ends already registered a main translation unit but we were not
28844      ready to perform the association, do this now.  */
28845   if (main_translation_unit != NULL_TREE)
28846     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28847 }
28848 
28849 /* Called before compile () starts outputtting functions, variables
28850    and toplevel asms into assembly.  */
28851 
28852 static void
dwarf2out_assembly_start(void)28853 dwarf2out_assembly_start (void)
28854 {
28855   if (text_section_line_info)
28856     return;
28857 
28858 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28859   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28860   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28861   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28862 			       COLD_TEXT_SECTION_LABEL, 0);
28863   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28864 
28865   switch_to_section (text_section);
28866   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28867 #endif
28868 
28869   /* Make sure the line number table for .text always exists.  */
28870   text_section_line_info = new_line_info_table ();
28871   text_section_line_info->end_label = text_end_label;
28872 
28873 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28874   cur_line_info_table = text_section_line_info;
28875 #endif
28876 
28877   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28878       && dwarf2out_do_cfi_asm ()
28879       && !dwarf2out_do_eh_frame ())
28880     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28881 }
28882 
28883 /* A helper function for dwarf2out_finish called through
28884    htab_traverse.  Assign a string its index.  All strings must be
28885    collected into the table by the time index_string is called,
28886    because the indexing code relies on htab_traverse to traverse nodes
28887    in the same order for each run. */
28888 
28889 int
index_string(indirect_string_node ** h,unsigned int * index)28890 index_string (indirect_string_node **h, unsigned int *index)
28891 {
28892   indirect_string_node *node = *h;
28893 
28894   find_string_form (node);
28895   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28896     {
28897       gcc_assert (node->index == NO_INDEX_ASSIGNED);
28898       node->index = *index;
28899       *index += 1;
28900     }
28901   return 1;
28902 }
28903 
28904 /* A helper function for output_indirect_strings called through
28905    htab_traverse.  Output the offset to a string and update the
28906    current offset.  */
28907 
28908 int
output_index_string_offset(indirect_string_node ** h,unsigned int * offset)28909 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28910 {
28911   indirect_string_node *node = *h;
28912 
28913   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28914     {
28915       /* Assert that this node has been assigned an index.  */
28916       gcc_assert (node->index != NO_INDEX_ASSIGNED
28917                   && node->index != NOT_INDEXED);
28918       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28919                            "indexed string 0x%x: %s", node->index, node->str);
28920       *offset += strlen (node->str) + 1;
28921     }
28922   return 1;
28923 }
28924 
28925 /* A helper function for dwarf2out_finish called through
28926    htab_traverse.  Output the indexed string.  */
28927 
28928 int
output_index_string(indirect_string_node ** h,unsigned int * cur_idx)28929 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28930 {
28931   struct indirect_string_node *node = *h;
28932 
28933   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28934     {
28935       /* Assert that the strings are output in the same order as their
28936          indexes were assigned.  */
28937       gcc_assert (*cur_idx == node->index);
28938       assemble_string (node->str, strlen (node->str) + 1);
28939       *cur_idx += 1;
28940     }
28941   return 1;
28942 }
28943 
28944 /* A helper function for output_indirect_strings.  Counts the number
28945    of index strings offsets.  Must match the logic of the functions
28946    output_index_string[_offsets] above.  */
28947 int
count_index_strings(indirect_string_node ** h,unsigned int * last_idx)28948 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28949 {
28950   struct indirect_string_node *node = *h;
28951 
28952   if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28953     *last_idx += 1;
28954   return 1;
28955 }
28956 
28957 /* A helper function for dwarf2out_finish called through
28958    htab_traverse.  Emit one queued .debug_str string.  */
28959 
28960 int
output_indirect_string(indirect_string_node ** h,enum dwarf_form form)28961 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28962 {
28963   struct indirect_string_node *node = *h;
28964 
28965   node->form = find_string_form (node);
28966   if (node->form == form && node->refcount > 0)
28967     {
28968       ASM_OUTPUT_LABEL (asm_out_file, node->label);
28969       assemble_string (node->str, strlen (node->str) + 1);
28970     }
28971 
28972   return 1;
28973 }
28974 
28975 /* Output the indexed string table.  */
28976 
28977 static void
output_indirect_strings(void)28978 output_indirect_strings (void)
28979 {
28980   switch_to_section (debug_str_section);
28981   if (!dwarf_split_debug_info)
28982     debug_str_hash->traverse<enum dwarf_form,
28983 			     output_indirect_string> (DW_FORM_strp);
28984   else
28985     {
28986       unsigned int offset = 0;
28987       unsigned int cur_idx = 0;
28988 
28989       if (skeleton_debug_str_hash)
28990         skeleton_debug_str_hash->traverse<enum dwarf_form,
28991 					  output_indirect_string> (DW_FORM_strp);
28992 
28993       switch_to_section (debug_str_offsets_section);
28994       /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
28995 	 header.  Note that we don't need to generate a label to the
28996 	 actual index table following the header here, because this is
28997 	 for the split dwarf case only.  In an .dwo file there is only
28998 	 one string offsets table (and one debug info section).  But
28999 	 if we would start using string offset tables for the main (or
29000 	 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29001 	 pointing to the actual index after the header.  Split dwarf
29002 	 units will never have a string offsets base attribute.  When
29003 	 a split unit is moved into a .dwp file the string offsets can
29004 	 be found through the .debug_cu_index section table.  */
29005       if (dwarf_version >= 5)
29006 	{
29007 	  unsigned int last_idx = 0;
29008 	  unsigned long str_offsets_length;
29009 
29010 	  debug_str_hash->traverse_noresize
29011 	    <unsigned int *, count_index_strings> (&last_idx);
29012 	  str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
29013 	  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29014 	    dw2_asm_output_data (4, 0xffffffff,
29015 				 "Escape value for 64-bit DWARF extension");
29016 	  dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
29017 			       "Length of string offsets unit");
29018 	  dw2_asm_output_data (2, 5, "DWARF string offsets version");
29019 	  dw2_asm_output_data (2, 0, "Header zero padding");
29020 	}
29021       debug_str_hash->traverse_noresize
29022 	<unsigned int *, output_index_string_offset> (&offset);
29023       switch_to_section (debug_str_dwo_section);
29024       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29025 	(&cur_idx);
29026     }
29027 }
29028 
29029 /* Callback for htab_traverse to assign an index to an entry in the
29030    table, and to write that entry to the .debug_addr section.  */
29031 
29032 int
output_addr_table_entry(addr_table_entry ** slot,unsigned int * cur_index)29033 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29034 {
29035   addr_table_entry *entry = *slot;
29036 
29037   if (entry->refcount == 0)
29038     {
29039       gcc_assert (entry->index == NO_INDEX_ASSIGNED
29040                   || entry->index == NOT_INDEXED);
29041       return 1;
29042     }
29043 
29044   gcc_assert (entry->index == *cur_index);
29045   (*cur_index)++;
29046 
29047   switch (entry->kind)
29048     {
29049       case ate_kind_rtx:
29050         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29051                                  "0x%x", entry->index);
29052         break;
29053       case ate_kind_rtx_dtprel:
29054         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29055         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29056                                              DWARF2_ADDR_SIZE,
29057                                              entry->addr.rtl);
29058         fputc ('\n', asm_out_file);
29059         break;
29060       case ate_kind_label:
29061         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29062                                  "0x%x", entry->index);
29063         break;
29064       default:
29065         gcc_unreachable ();
29066     }
29067   return 1;
29068 }
29069 
29070 /* A helper function for dwarf2out_finish.  Counts the number
29071    of indexed addresses.  Must match the logic of the functions
29072    output_addr_table_entry above.  */
29073 int
count_index_addrs(addr_table_entry ** slot,unsigned int * last_idx)29074 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29075 {
29076   addr_table_entry *entry = *slot;
29077 
29078   if (entry->refcount > 0)
29079     *last_idx += 1;
29080   return 1;
29081 }
29082 
29083 /* Produce the .debug_addr section.  */
29084 
29085 static void
output_addr_table(void)29086 output_addr_table (void)
29087 {
29088   unsigned int index = 0;
29089   if (addr_index_table == NULL || addr_index_table->size () == 0)
29090     return;
29091 
29092   switch_to_section (debug_addr_section);
29093   /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29094      which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29095      before DWARF5, didn't have a header for .debug_addr units.
29096      DWARF5 specifies a small header when address tables are used.  */
29097   if (dwarf_version >= 5)
29098     {
29099       unsigned int last_idx = 0;
29100       unsigned long addrs_length;
29101 
29102       addr_index_table->traverse_noresize
29103 	<unsigned int *, count_index_addrs> (&last_idx);
29104       addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29105 
29106       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
29107 	dw2_asm_output_data (4, 0xffffffff,
29108 			     "Escape value for 64-bit DWARF extension");
29109       dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
29110 			   "Length of Address Unit");
29111       dw2_asm_output_data (2, 5, "DWARF addr version");
29112       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29113       dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29114     }
29115   ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29116 
29117   addr_index_table
29118     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29119 }
29120 
29121 #if ENABLE_ASSERT_CHECKING
29122 /* Verify that all marks are clear.  */
29123 
29124 static void
verify_marks_clear(dw_die_ref die)29125 verify_marks_clear (dw_die_ref die)
29126 {
29127   dw_die_ref c;
29128 
29129   gcc_assert (! die->die_mark);
29130   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29131 }
29132 #endif /* ENABLE_ASSERT_CHECKING */
29133 
29134 /* Clear the marks for a die and its children.
29135    Be cool if the mark isn't set.  */
29136 
29137 static void
prune_unmark_dies(dw_die_ref die)29138 prune_unmark_dies (dw_die_ref die)
29139 {
29140   dw_die_ref c;
29141 
29142   if (die->die_mark)
29143     die->die_mark = 0;
29144   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29145 }
29146 
29147 /* Given LOC that is referenced by a DIE we're marking as used, find all
29148    referenced DWARF procedures it references and mark them as used.  */
29149 
29150 static void
prune_unused_types_walk_loc_descr(dw_loc_descr_ref loc)29151 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29152 {
29153   for (; loc != NULL; loc = loc->dw_loc_next)
29154     switch (loc->dw_loc_opc)
29155       {
29156       case DW_OP_implicit_pointer:
29157       case DW_OP_convert:
29158       case DW_OP_reinterpret:
29159       case DW_OP_GNU_implicit_pointer:
29160       case DW_OP_GNU_convert:
29161       case DW_OP_GNU_reinterpret:
29162 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29163 	  prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29164 	break;
29165       case DW_OP_GNU_variable_value:
29166 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29167 	  {
29168 	    dw_die_ref ref
29169 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29170 	    if (ref == NULL)
29171 	      break;
29172 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29173 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29174 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29175 	  }
29176 	/* FALLTHRU */
29177       case DW_OP_call2:
29178       case DW_OP_call4:
29179       case DW_OP_call_ref:
29180       case DW_OP_const_type:
29181       case DW_OP_GNU_const_type:
29182       case DW_OP_GNU_parameter_ref:
29183 	gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29184 	prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29185 	break;
29186       case DW_OP_regval_type:
29187       case DW_OP_deref_type:
29188       case DW_OP_GNU_regval_type:
29189       case DW_OP_GNU_deref_type:
29190 	gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29191 	prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29192 	break;
29193       case DW_OP_entry_value:
29194       case DW_OP_GNU_entry_value:
29195 	gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29196 	prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29197 	break;
29198       default:
29199 	break;
29200       }
29201 }
29202 
29203 /* Given DIE that we're marking as used, find any other dies
29204    it references as attributes and mark them as used.  */
29205 
29206 static void
prune_unused_types_walk_attribs(dw_die_ref die)29207 prune_unused_types_walk_attribs (dw_die_ref die)
29208 {
29209   dw_attr_node *a;
29210   unsigned ix;
29211 
29212   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29213     {
29214       switch (AT_class (a))
29215 	{
29216 	/* Make sure DWARF procedures referenced by location descriptions will
29217 	   get emitted.  */
29218 	case dw_val_class_loc:
29219 	  prune_unused_types_walk_loc_descr (AT_loc (a));
29220 	  break;
29221 	case dw_val_class_loc_list:
29222 	  for (dw_loc_list_ref list = AT_loc_list (a);
29223 	       list != NULL;
29224 	       list = list->dw_loc_next)
29225 	    prune_unused_types_walk_loc_descr (list->expr);
29226 	  break;
29227 
29228 	case dw_val_class_view_list:
29229 	  /* This points to a loc_list in another attribute, so it's
29230 	     already covered.  */
29231 	  break;
29232 
29233 	case dw_val_class_die_ref:
29234 	  /* A reference to another DIE.
29235 	     Make sure that it will get emitted.
29236 	     If it was broken out into a comdat group, don't follow it.  */
29237           if (! AT_ref (a)->comdat_type_p
29238               || a->dw_attr == DW_AT_specification)
29239 	    prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29240 	  break;
29241 
29242 	case dw_val_class_str:
29243 	  /* Set the string's refcount to 0 so that prune_unused_types_mark
29244 	     accounts properly for it.  */
29245 	  a->dw_attr_val.v.val_str->refcount = 0;
29246 	  break;
29247 
29248 	default:
29249 	  break;
29250 	}
29251     }
29252 }
29253 
29254 /* Mark the generic parameters and arguments children DIEs of DIE.  */
29255 
29256 static void
prune_unused_types_mark_generic_parms_dies(dw_die_ref die)29257 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29258 {
29259   dw_die_ref c;
29260 
29261   if (die == NULL || die->die_child == NULL)
29262     return;
29263   c = die->die_child;
29264   do
29265     {
29266       if (is_template_parameter (c))
29267 	prune_unused_types_mark (c, 1);
29268       c = c->die_sib;
29269     } while (c && c != die->die_child);
29270 }
29271 
29272 /* Mark DIE as being used.  If DOKIDS is true, then walk down
29273    to DIE's children.  */
29274 
29275 static void
prune_unused_types_mark(dw_die_ref die,int dokids)29276 prune_unused_types_mark (dw_die_ref die, int dokids)
29277 {
29278   dw_die_ref c;
29279 
29280   if (die->die_mark == 0)
29281     {
29282       /* We haven't done this node yet.  Mark it as used.  */
29283       die->die_mark = 1;
29284       /* If this is the DIE of a generic type instantiation,
29285 	 mark the children DIEs that describe its generic parms and
29286 	 args.  */
29287       prune_unused_types_mark_generic_parms_dies (die);
29288 
29289       /* We also have to mark its parents as used.
29290 	 (But we don't want to mark our parent's kids due to this,
29291 	 unless it is a class.)  */
29292       if (die->die_parent)
29293 	prune_unused_types_mark (die->die_parent,
29294 				 class_scope_p (die->die_parent));
29295 
29296       /* Mark any referenced nodes.  */
29297       prune_unused_types_walk_attribs (die);
29298 
29299       /* If this node is a specification,
29300 	 also mark the definition, if it exists.  */
29301       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29302 	prune_unused_types_mark (die->die_definition, 1);
29303     }
29304 
29305   if (dokids && die->die_mark != 2)
29306     {
29307       /* We need to walk the children, but haven't done so yet.
29308 	 Remember that we've walked the kids.  */
29309       die->die_mark = 2;
29310 
29311       /* If this is an array type, we need to make sure our
29312 	 kids get marked, even if they're types.  If we're
29313 	 breaking out types into comdat sections, do this
29314 	 for all type definitions.  */
29315       if (die->die_tag == DW_TAG_array_type
29316           || (use_debug_types
29317               && is_type_die (die) && ! is_declaration_die (die)))
29318 	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29319       else
29320 	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29321     }
29322 }
29323 
29324 /* For local classes, look if any static member functions were emitted
29325    and if so, mark them.  */
29326 
29327 static void
prune_unused_types_walk_local_classes(dw_die_ref die)29328 prune_unused_types_walk_local_classes (dw_die_ref die)
29329 {
29330   dw_die_ref c;
29331 
29332   if (die->die_mark == 2)
29333     return;
29334 
29335   switch (die->die_tag)
29336     {
29337     case DW_TAG_structure_type:
29338     case DW_TAG_union_type:
29339     case DW_TAG_class_type:
29340     case DW_TAG_interface_type:
29341       break;
29342 
29343     case DW_TAG_subprogram:
29344       if (!get_AT_flag (die, DW_AT_declaration)
29345 	  || die->die_definition != NULL)
29346 	prune_unused_types_mark (die, 1);
29347       return;
29348 
29349     default:
29350       return;
29351     }
29352 
29353   /* Mark children.  */
29354   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29355 }
29356 
29357 /* Walk the tree DIE and mark types that we actually use.  */
29358 
29359 static void
prune_unused_types_walk(dw_die_ref die)29360 prune_unused_types_walk (dw_die_ref die)
29361 {
29362   dw_die_ref c;
29363 
29364   /* Don't do anything if this node is already marked and
29365      children have been marked as well.  */
29366   if (die->die_mark == 2)
29367     return;
29368 
29369   switch (die->die_tag)
29370     {
29371     case DW_TAG_structure_type:
29372     case DW_TAG_union_type:
29373     case DW_TAG_class_type:
29374     case DW_TAG_interface_type:
29375       if (die->die_perennial_p)
29376 	break;
29377 
29378       for (c = die->die_parent; c; c = c->die_parent)
29379 	if (c->die_tag == DW_TAG_subprogram)
29380 	  break;
29381 
29382       /* Finding used static member functions inside of classes
29383 	 is needed just for local classes, because for other classes
29384 	 static member function DIEs with DW_AT_specification
29385 	 are emitted outside of the DW_TAG_*_type.  If we ever change
29386 	 it, we'd need to call this even for non-local classes.  */
29387       if (c)
29388 	prune_unused_types_walk_local_classes (die);
29389 
29390       /* It's a type node --- don't mark it.  */
29391       return;
29392 
29393     case DW_TAG_const_type:
29394     case DW_TAG_packed_type:
29395     case DW_TAG_pointer_type:
29396     case DW_TAG_reference_type:
29397     case DW_TAG_rvalue_reference_type:
29398     case DW_TAG_volatile_type:
29399     case DW_TAG_typedef:
29400     case DW_TAG_array_type:
29401     case DW_TAG_friend:
29402     case DW_TAG_enumeration_type:
29403     case DW_TAG_subroutine_type:
29404     case DW_TAG_string_type:
29405     case DW_TAG_set_type:
29406     case DW_TAG_subrange_type:
29407     case DW_TAG_ptr_to_member_type:
29408     case DW_TAG_file_type:
29409       /* Type nodes are useful only when other DIEs reference them --- don't
29410 	 mark them.  */
29411       /* FALLTHROUGH */
29412 
29413     case DW_TAG_dwarf_procedure:
29414       /* Likewise for DWARF procedures.  */
29415 
29416       if (die->die_perennial_p)
29417 	break;
29418 
29419       return;
29420 
29421     case DW_TAG_variable:
29422       if (flag_debug_only_used_symbols)
29423 	{
29424 	  if (die->die_perennial_p)
29425 	    break;
29426 
29427 	  /* premark_used_variables marks external variables --- don't mark
29428 	     them here.  But function-local externals are always considered
29429 	     used.  */
29430 	  if (get_AT (die, DW_AT_external))
29431 	    {
29432 	      for (c = die->die_parent; c; c = c->die_parent)
29433 		if (c->die_tag == DW_TAG_subprogram)
29434 		  break;
29435 	      if (!c)
29436 		return;
29437 	    }
29438 	}
29439       /* FALLTHROUGH */
29440 
29441     default:
29442       /* Mark everything else.  */
29443       break;
29444   }
29445 
29446   if (die->die_mark == 0)
29447     {
29448       die->die_mark = 1;
29449 
29450       /* Now, mark any dies referenced from here.  */
29451       prune_unused_types_walk_attribs (die);
29452     }
29453 
29454   die->die_mark = 2;
29455 
29456   /* Mark children.  */
29457   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29458 }
29459 
29460 /* Increment the string counts on strings referred to from DIE's
29461    attributes.  */
29462 
29463 static void
prune_unused_types_update_strings(dw_die_ref die)29464 prune_unused_types_update_strings (dw_die_ref die)
29465 {
29466   dw_attr_node *a;
29467   unsigned ix;
29468 
29469   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29470     if (AT_class (a) == dw_val_class_str)
29471       {
29472 	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29473 	s->refcount++;
29474 	/* Avoid unnecessarily putting strings that are used less than
29475 	   twice in the hash table.  */
29476 	if (s->refcount
29477 	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29478 	  {
29479 	    indirect_string_node **slot
29480 	      = debug_str_hash->find_slot_with_hash (s->str,
29481 						     htab_hash_string (s->str),
29482 						     INSERT);
29483 	    gcc_assert (*slot == NULL);
29484 	    *slot = s;
29485 	  }
29486       }
29487 }
29488 
29489 /* Mark DIE and its children as removed.  */
29490 
29491 static void
mark_removed(dw_die_ref die)29492 mark_removed (dw_die_ref die)
29493 {
29494   dw_die_ref c;
29495   die->removed = true;
29496   FOR_EACH_CHILD (die, c, mark_removed (c));
29497 }
29498 
29499 /* Remove from the tree DIE any dies that aren't marked.  */
29500 
29501 static void
prune_unused_types_prune(dw_die_ref die)29502 prune_unused_types_prune (dw_die_ref die)
29503 {
29504   dw_die_ref c;
29505 
29506   gcc_assert (die->die_mark);
29507   prune_unused_types_update_strings (die);
29508 
29509   if (! die->die_child)
29510     return;
29511 
29512   c = die->die_child;
29513   do {
29514     dw_die_ref prev = c, next;
29515     for (c = c->die_sib; ! c->die_mark; c = next)
29516       if (c == die->die_child)
29517 	{
29518 	  /* No marked children between 'prev' and the end of the list.  */
29519 	  if (prev == c)
29520 	    /* No marked children at all.  */
29521 	    die->die_child = NULL;
29522 	  else
29523 	    {
29524 	      prev->die_sib = c->die_sib;
29525 	      die->die_child = prev;
29526 	    }
29527 	  c->die_sib = NULL;
29528 	  mark_removed (c);
29529 	  return;
29530 	}
29531       else
29532 	{
29533 	  next = c->die_sib;
29534 	  c->die_sib = NULL;
29535 	  mark_removed (c);
29536 	}
29537 
29538     if (c != prev->die_sib)
29539       prev->die_sib = c;
29540     prune_unused_types_prune (c);
29541   } while (c != die->die_child);
29542 }
29543 
29544 /* Remove dies representing declarations that we never use.  */
29545 
29546 static void
prune_unused_types(void)29547 prune_unused_types (void)
29548 {
29549   unsigned int i;
29550   limbo_die_node *node;
29551   comdat_type_node *ctnode;
29552   pubname_entry *pub;
29553   dw_die_ref base_type;
29554 
29555 #if ENABLE_ASSERT_CHECKING
29556   /* All the marks should already be clear.  */
29557   verify_marks_clear (comp_unit_die ());
29558   for (node = limbo_die_list; node; node = node->next)
29559     verify_marks_clear (node->die);
29560   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29561     verify_marks_clear (ctnode->root_die);
29562 #endif /* ENABLE_ASSERT_CHECKING */
29563 
29564   /* Mark types that are used in global variables.  */
29565   premark_types_used_by_global_vars ();
29566 
29567   /* Mark variables used in the symtab.  */
29568   if (flag_debug_only_used_symbols)
29569     premark_used_variables ();
29570 
29571   /* Set the mark on nodes that are actually used.  */
29572   prune_unused_types_walk (comp_unit_die ());
29573   for (node = limbo_die_list; node; node = node->next)
29574     prune_unused_types_walk (node->die);
29575   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29576     {
29577       prune_unused_types_walk (ctnode->root_die);
29578       prune_unused_types_mark (ctnode->type_die, 1);
29579     }
29580 
29581   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
29582      are unusual in that they are pubnames that are the children of pubtypes.
29583      They should only be marked via their parent DW_TAG_enumeration_type die,
29584      not as roots in themselves.  */
29585   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29586     if (pub->die->die_tag != DW_TAG_enumerator)
29587       prune_unused_types_mark (pub->die, 1);
29588   for (i = 0; base_types.iterate (i, &base_type); i++)
29589     prune_unused_types_mark (base_type, 1);
29590 
29591   /* Also set the mark on nodes that could be referenced by
29592      DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
29593      by DW_TAG_inlined_subroutine origins.  */
29594   cgraph_node *cnode;
29595   FOR_EACH_FUNCTION (cnode)
29596     if (cnode->referred_to_p (false))
29597       {
29598 	dw_die_ref die = lookup_decl_die (cnode->decl);
29599 	if (die == NULL || die->die_mark)
29600 	  continue;
29601 	for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29602 	  if (e->caller != cnode)
29603 	    {
29604 	      prune_unused_types_mark (die, 1);
29605 	      break;
29606 	    }
29607       }
29608 
29609   if (debug_str_hash)
29610     debug_str_hash->empty ();
29611   if (skeleton_debug_str_hash)
29612     skeleton_debug_str_hash->empty ();
29613   prune_unused_types_prune (comp_unit_die ());
29614   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29615     {
29616       node = *pnode;
29617       if (!node->die->die_mark)
29618 	*pnode = node->next;
29619       else
29620 	{
29621 	  prune_unused_types_prune (node->die);
29622 	  pnode = &node->next;
29623 	}
29624     }
29625   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29626     prune_unused_types_prune (ctnode->root_die);
29627 
29628   /* Leave the marks clear.  */
29629   prune_unmark_dies (comp_unit_die ());
29630   for (node = limbo_die_list; node; node = node->next)
29631     prune_unmark_dies (node->die);
29632   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29633     prune_unmark_dies (ctnode->root_die);
29634 }
29635 
29636 /* Helpers to manipulate hash table of comdat type units.  */
29637 
29638 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29639 {
29640   static inline hashval_t hash (const comdat_type_node *);
29641   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29642 };
29643 
29644 inline hashval_t
hash(const comdat_type_node * type_node)29645 comdat_type_hasher::hash (const comdat_type_node *type_node)
29646 {
29647   hashval_t h;
29648   memcpy (&h, type_node->signature, sizeof (h));
29649   return h;
29650 }
29651 
29652 inline bool
equal(const comdat_type_node * type_node_1,const comdat_type_node * type_node_2)29653 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29654 			   const comdat_type_node *type_node_2)
29655 {
29656   return (! memcmp (type_node_1->signature, type_node_2->signature,
29657                     DWARF_TYPE_SIGNATURE_SIZE));
29658 }
29659 
29660 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29661    to the location it would have been added, should we know its
29662    DECL_ASSEMBLER_NAME when we added other attributes.  This will
29663    probably improve compactness of debug info, removing equivalent
29664    abbrevs, and hide any differences caused by deferring the
29665    computation of the assembler name, triggered by e.g. PCH.  */
29666 
29667 static inline void
move_linkage_attr(dw_die_ref die)29668 move_linkage_attr (dw_die_ref die)
29669 {
29670   unsigned ix = vec_safe_length (die->die_attr);
29671   dw_attr_node linkage = (*die->die_attr)[ix - 1];
29672 
29673   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29674 	      || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29675 
29676   while (--ix > 0)
29677     {
29678       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29679 
29680       if (prev->dw_attr == DW_AT_decl_line
29681 	  || prev->dw_attr == DW_AT_decl_column
29682 	  || prev->dw_attr == DW_AT_name)
29683 	break;
29684     }
29685 
29686   if (ix != vec_safe_length (die->die_attr) - 1)
29687     {
29688       die->die_attr->pop ();
29689       die->die_attr->quick_insert (ix, linkage);
29690     }
29691 }
29692 
29693 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29694    referenced from typed stack ops and count how often they are used.  */
29695 
29696 static void
mark_base_types(dw_loc_descr_ref loc)29697 mark_base_types (dw_loc_descr_ref loc)
29698 {
29699   dw_die_ref base_type = NULL;
29700 
29701   for (; loc; loc = loc->dw_loc_next)
29702     {
29703       switch (loc->dw_loc_opc)
29704 	{
29705 	case DW_OP_regval_type:
29706 	case DW_OP_deref_type:
29707 	case DW_OP_GNU_regval_type:
29708 	case DW_OP_GNU_deref_type:
29709 	  base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29710 	  break;
29711 	case DW_OP_convert:
29712 	case DW_OP_reinterpret:
29713 	case DW_OP_GNU_convert:
29714 	case DW_OP_GNU_reinterpret:
29715 	  if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29716 	    continue;
29717 	  /* FALLTHRU */
29718 	case DW_OP_const_type:
29719 	case DW_OP_GNU_const_type:
29720 	  base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29721 	  break;
29722 	case DW_OP_entry_value:
29723 	case DW_OP_GNU_entry_value:
29724 	  mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29725 	  continue;
29726 	default:
29727 	  continue;
29728 	}
29729       gcc_assert (base_type->die_parent == comp_unit_die ());
29730       if (base_type->die_mark)
29731 	base_type->die_mark++;
29732       else
29733 	{
29734 	  base_types.safe_push (base_type);
29735 	  base_type->die_mark = 1;
29736 	}
29737     }
29738 }
29739 
29740 /* Comparison function for sorting marked base types.  */
29741 
29742 static int
base_type_cmp(const void * x,const void * y)29743 base_type_cmp (const void *x, const void *y)
29744 {
29745   dw_die_ref dx = *(const dw_die_ref *) x;
29746   dw_die_ref dy = *(const dw_die_ref *) y;
29747   unsigned int byte_size1, byte_size2;
29748   unsigned int encoding1, encoding2;
29749   unsigned int align1, align2;
29750   if (dx->die_mark > dy->die_mark)
29751     return -1;
29752   if (dx->die_mark < dy->die_mark)
29753     return 1;
29754   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29755   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29756   if (byte_size1 < byte_size2)
29757     return 1;
29758   if (byte_size1 > byte_size2)
29759     return -1;
29760   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29761   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29762   if (encoding1 < encoding2)
29763     return 1;
29764   if (encoding1 > encoding2)
29765     return -1;
29766   align1 = get_AT_unsigned (dx, DW_AT_alignment);
29767   align2 = get_AT_unsigned (dy, DW_AT_alignment);
29768   if (align1 < align2)
29769     return 1;
29770   if (align1 > align2)
29771     return -1;
29772   return 0;
29773 }
29774 
29775 /* Move base types marked by mark_base_types as early as possible
29776    in the CU, sorted by decreasing usage count both to make the
29777    uleb128 references as small as possible and to make sure they
29778    will have die_offset already computed by calc_die_sizes when
29779    sizes of typed stack loc ops is computed.  */
29780 
29781 static void
move_marked_base_types(void)29782 move_marked_base_types (void)
29783 {
29784   unsigned int i;
29785   dw_die_ref base_type, die, c;
29786 
29787   if (base_types.is_empty ())
29788     return;
29789 
29790   /* Sort by decreasing usage count, they will be added again in that
29791      order later on.  */
29792   base_types.qsort (base_type_cmp);
29793   die = comp_unit_die ();
29794   c = die->die_child;
29795   do
29796     {
29797       dw_die_ref prev = c;
29798       c = c->die_sib;
29799       while (c->die_mark)
29800 	{
29801 	  remove_child_with_prev (c, prev);
29802 	  /* As base types got marked, there must be at least
29803 	     one node other than DW_TAG_base_type.  */
29804 	  gcc_assert (die->die_child != NULL);
29805 	  c = prev->die_sib;
29806 	}
29807     }
29808   while (c != die->die_child);
29809   gcc_assert (die->die_child);
29810   c = die->die_child;
29811   for (i = 0; base_types.iterate (i, &base_type); i++)
29812     {
29813       base_type->die_mark = 0;
29814       base_type->die_sib = c->die_sib;
29815       c->die_sib = base_type;
29816       c = base_type;
29817     }
29818 }
29819 
29820 /* Helper function for resolve_addr, attempt to resolve
29821    one CONST_STRING, return true if successful.  Similarly verify that
29822    SYMBOL_REFs refer to variables emitted in the current CU.  */
29823 
29824 static bool
resolve_one_addr(rtx * addr)29825 resolve_one_addr (rtx *addr)
29826 {
29827   rtx rtl = *addr;
29828 
29829   if (GET_CODE (rtl) == CONST_STRING)
29830     {
29831       size_t len = strlen (XSTR (rtl, 0)) + 1;
29832       tree t = build_string (len, XSTR (rtl, 0));
29833       tree tlen = size_int (len - 1);
29834       TREE_TYPE (t)
29835 	= build_array_type (char_type_node, build_index_type (tlen));
29836       rtl = lookup_constant_def (t);
29837       if (!rtl || !MEM_P (rtl))
29838 	return false;
29839       rtl = XEXP (rtl, 0);
29840       if (GET_CODE (rtl) == SYMBOL_REF
29841 	  && SYMBOL_REF_DECL (rtl)
29842 	  && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29843 	return false;
29844       vec_safe_push (used_rtx_array, rtl);
29845       *addr = rtl;
29846       return true;
29847     }
29848 
29849   if (GET_CODE (rtl) == SYMBOL_REF
29850       && SYMBOL_REF_DECL (rtl))
29851     {
29852       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29853 	{
29854 	  if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29855 	    return false;
29856 	}
29857       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29858 	return false;
29859     }
29860 
29861   if (GET_CODE (rtl) == CONST)
29862     {
29863       subrtx_ptr_iterator::array_type array;
29864       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29865 	if (!resolve_one_addr (*iter))
29866 	  return false;
29867     }
29868 
29869   return true;
29870 }
29871 
29872 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29873    if possible, and create DW_TAG_dwarf_procedure that can be referenced
29874    from DW_OP_implicit_pointer if the string hasn't been seen yet.  */
29875 
29876 static rtx
string_cst_pool_decl(tree t)29877 string_cst_pool_decl (tree t)
29878 {
29879   rtx rtl = output_constant_def (t, 1);
29880   unsigned char *array;
29881   dw_loc_descr_ref l;
29882   tree decl;
29883   size_t len;
29884   dw_die_ref ref;
29885 
29886   if (!rtl || !MEM_P (rtl))
29887     return NULL_RTX;
29888   rtl = XEXP (rtl, 0);
29889   if (GET_CODE (rtl) != SYMBOL_REF
29890       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29891     return NULL_RTX;
29892 
29893   decl = SYMBOL_REF_DECL (rtl);
29894   if (!lookup_decl_die (decl))
29895     {
29896       len = TREE_STRING_LENGTH (t);
29897       vec_safe_push (used_rtx_array, rtl);
29898       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29899       array = ggc_vec_alloc<unsigned char> (len);
29900       memcpy (array, TREE_STRING_POINTER (t), len);
29901       l = new_loc_descr (DW_OP_implicit_value, len, 0);
29902       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29903       l->dw_loc_oprnd2.v.val_vec.length = len;
29904       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29905       l->dw_loc_oprnd2.v.val_vec.array = array;
29906       add_AT_loc (ref, DW_AT_location, l);
29907       equate_decl_number_to_die (decl, ref);
29908     }
29909   return rtl;
29910 }
29911 
29912 /* Helper function of resolve_addr_in_expr.  LOC is
29913    a DW_OP_addr followed by DW_OP_stack_value, either at the start
29914    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29915    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
29916    with DW_OP_implicit_pointer if possible
29917    and return true, if unsuccessful, return false.  */
29918 
29919 static bool
optimize_one_addr_into_implicit_ptr(dw_loc_descr_ref loc)29920 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29921 {
29922   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29923   HOST_WIDE_INT offset = 0;
29924   dw_die_ref ref = NULL;
29925   tree decl;
29926 
29927   if (GET_CODE (rtl) == CONST
29928       && GET_CODE (XEXP (rtl, 0)) == PLUS
29929       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29930     {
29931       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29932       rtl = XEXP (XEXP (rtl, 0), 0);
29933     }
29934   if (GET_CODE (rtl) == CONST_STRING)
29935     {
29936       size_t len = strlen (XSTR (rtl, 0)) + 1;
29937       tree t = build_string (len, XSTR (rtl, 0));
29938       tree tlen = size_int (len - 1);
29939 
29940       TREE_TYPE (t)
29941 	= build_array_type (char_type_node, build_index_type (tlen));
29942       rtl = string_cst_pool_decl (t);
29943       if (!rtl)
29944 	return false;
29945     }
29946   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29947     {
29948       decl = SYMBOL_REF_DECL (rtl);
29949       if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29950 	{
29951 	  ref = lookup_decl_die (decl);
29952 	  if (ref && (get_AT (ref, DW_AT_location)
29953 		      || get_AT (ref, DW_AT_const_value)))
29954 	    {
29955 	      loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29956 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29957 	      loc->dw_loc_oprnd1.val_entry = NULL;
29958 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29959 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29960 	      loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29961 	      loc->dw_loc_oprnd2.v.val_int = offset;
29962 	      return true;
29963 	    }
29964 	}
29965     }
29966   return false;
29967 }
29968 
29969 /* Helper function for resolve_addr, handle one location
29970    expression, return false if at least one CONST_STRING or SYMBOL_REF in
29971    the location list couldn't be resolved.  */
29972 
29973 static bool
resolve_addr_in_expr(dw_attr_node * a,dw_loc_descr_ref loc)29974 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29975 {
29976   dw_loc_descr_ref keep = NULL;
29977   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29978     switch (loc->dw_loc_opc)
29979       {
29980       case DW_OP_addr:
29981 	if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29982 	  {
29983 	    if ((prev == NULL
29984 		 || prev->dw_loc_opc == DW_OP_piece
29985 		 || prev->dw_loc_opc == DW_OP_bit_piece)
29986 		&& loc->dw_loc_next
29987 		&& loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29988 		&& (!dwarf_strict || dwarf_version >= 5)
29989 		&& optimize_one_addr_into_implicit_ptr (loc))
29990 	      break;
29991 	    return false;
29992 	  }
29993 	break;
29994       case DW_OP_GNU_addr_index:
29995       case DW_OP_addrx:
29996       case DW_OP_GNU_const_index:
29997       case DW_OP_constx:
29998 	if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
29999 	     || loc->dw_loc_opc == DW_OP_addrx)
30000 	    || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30001 		 || loc->dw_loc_opc == DW_OP_constx)
30002 		&& loc->dtprel))
30003           {
30004             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30005             if (!resolve_one_addr (&rtl))
30006               return false;
30007             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30008 	    loc->dw_loc_oprnd1.val_entry
30009 	      = add_addr_table_entry (rtl, ate_kind_rtx);
30010           }
30011 	break;
30012       case DW_OP_const4u:
30013       case DW_OP_const8u:
30014 	if (loc->dtprel
30015 	    && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30016 	  return false;
30017 	break;
30018       case DW_OP_plus_uconst:
30019 	if (size_of_loc_descr (loc)
30020 	    > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30021 	      + 1
30022 	    && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30023 	  {
30024 	    dw_loc_descr_ref repl
30025 	      = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30026 	    add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30027 	    add_loc_descr (&repl, loc->dw_loc_next);
30028 	    *loc = *repl;
30029 	  }
30030 	break;
30031       case DW_OP_implicit_value:
30032 	if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30033 	    && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30034 	  return false;
30035 	break;
30036       case DW_OP_implicit_pointer:
30037       case DW_OP_GNU_implicit_pointer:
30038       case DW_OP_GNU_parameter_ref:
30039       case DW_OP_GNU_variable_value:
30040 	if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30041 	  {
30042 	    dw_die_ref ref
30043 	      = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30044 	    if (ref == NULL)
30045 	      return false;
30046 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30047 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30048 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30049 	  }
30050 	if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30051 	  {
30052 	    if (prev == NULL
30053 		&& loc->dw_loc_next == NULL
30054 		&& AT_class (a) == dw_val_class_loc)
30055 	      switch (a->dw_attr)
30056 		{
30057 		  /* Following attributes allow both exprloc and reference,
30058 		     so if the whole expression is DW_OP_GNU_variable_value
30059 		     alone we could transform it into reference.  */
30060 		case DW_AT_byte_size:
30061 		case DW_AT_bit_size:
30062 		case DW_AT_lower_bound:
30063 		case DW_AT_upper_bound:
30064 		case DW_AT_bit_stride:
30065 		case DW_AT_count:
30066 		case DW_AT_allocated:
30067 		case DW_AT_associated:
30068 		case DW_AT_byte_stride:
30069 		  a->dw_attr_val.val_class = dw_val_class_die_ref;
30070 		  a->dw_attr_val.val_entry = NULL;
30071 		  a->dw_attr_val.v.val_die_ref.die
30072 		    = loc->dw_loc_oprnd1.v.val_die_ref.die;
30073 		  a->dw_attr_val.v.val_die_ref.external = 0;
30074 		  return true;
30075 		default:
30076 		  break;
30077 		}
30078 	    if (dwarf_strict)
30079 	      return false;
30080 	  }
30081 	break;
30082       case DW_OP_const_type:
30083       case DW_OP_regval_type:
30084       case DW_OP_deref_type:
30085       case DW_OP_convert:
30086       case DW_OP_reinterpret:
30087       case DW_OP_GNU_const_type:
30088       case DW_OP_GNU_regval_type:
30089       case DW_OP_GNU_deref_type:
30090       case DW_OP_GNU_convert:
30091       case DW_OP_GNU_reinterpret:
30092 	while (loc->dw_loc_next
30093 	       && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30094 		   || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30095 	  {
30096 	    dw_die_ref base1, base2;
30097 	    unsigned enc1, enc2, size1, size2;
30098 	    if (loc->dw_loc_opc == DW_OP_regval_type
30099 		|| loc->dw_loc_opc == DW_OP_deref_type
30100 		|| loc->dw_loc_opc == DW_OP_GNU_regval_type
30101 		|| loc->dw_loc_opc == DW_OP_GNU_deref_type)
30102 	      base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30103 	    else if (loc->dw_loc_oprnd1.val_class
30104 		     == dw_val_class_unsigned_const)
30105 	      break;
30106 	    else
30107 	      base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30108 	    if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30109 		== dw_val_class_unsigned_const)
30110 	      break;
30111 	    base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30112 	    gcc_assert (base1->die_tag == DW_TAG_base_type
30113 			&& base2->die_tag == DW_TAG_base_type);
30114 	    enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30115 	    enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30116 	    size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30117 	    size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30118 	    if (size1 == size2
30119 		&& (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30120 		     && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30121 		     && loc != keep)
30122 		    || enc1 == enc2))
30123 	      {
30124 		/* Optimize away next DW_OP_convert after
30125 		   adjusting LOC's base type die reference.  */
30126 		if (loc->dw_loc_opc == DW_OP_regval_type
30127 		    || loc->dw_loc_opc == DW_OP_deref_type
30128 		    || loc->dw_loc_opc == DW_OP_GNU_regval_type
30129 		    || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30130 		  loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30131 		else
30132 		  loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30133 		loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30134 		continue;
30135 	      }
30136 	    /* Don't change integer DW_OP_convert after e.g. floating
30137 	       point typed stack entry.  */
30138 	    else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30139 	      keep = loc->dw_loc_next;
30140 	    break;
30141 	  }
30142 	break;
30143       default:
30144 	break;
30145       }
30146   return true;
30147 }
30148 
30149 /* Helper function of resolve_addr.  DIE had DW_AT_location of
30150    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30151    and DW_OP_addr couldn't be resolved.  resolve_addr has already
30152    removed the DW_AT_location attribute.  This function attempts to
30153    add a new DW_AT_location attribute with DW_OP_implicit_pointer
30154    to it or DW_AT_const_value attribute, if possible.  */
30155 
30156 static void
optimize_location_into_implicit_ptr(dw_die_ref die,tree decl)30157 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30158 {
30159   if (!VAR_P (decl)
30160       || lookup_decl_die (decl) != die
30161       || DECL_EXTERNAL (decl)
30162       || !TREE_STATIC (decl)
30163       || DECL_INITIAL (decl) == NULL_TREE
30164       || DECL_P (DECL_INITIAL (decl))
30165       || get_AT (die, DW_AT_const_value))
30166     return;
30167 
30168   tree init = DECL_INITIAL (decl);
30169   HOST_WIDE_INT offset = 0;
30170   /* For variables that have been optimized away and thus
30171      don't have a memory location, see if we can emit
30172      DW_AT_const_value instead.  */
30173   if (tree_add_const_value_attribute (die, init))
30174     return;
30175   if (dwarf_strict && dwarf_version < 5)
30176     return;
30177   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30178      and ADDR_EXPR refers to a decl that has DW_AT_location or
30179      DW_AT_const_value (but isn't addressable, otherwise
30180      resolving the original DW_OP_addr wouldn't fail), see if
30181      we can add DW_OP_implicit_pointer.  */
30182   STRIP_NOPS (init);
30183   if (TREE_CODE (init) == POINTER_PLUS_EXPR
30184       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30185     {
30186       offset = tree_to_shwi (TREE_OPERAND (init, 1));
30187       init = TREE_OPERAND (init, 0);
30188       STRIP_NOPS (init);
30189     }
30190   if (TREE_CODE (init) != ADDR_EXPR)
30191     return;
30192   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30193        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30194       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30195 	  && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30196 	  && TREE_OPERAND (init, 0) != decl))
30197     {
30198       dw_die_ref ref;
30199       dw_loc_descr_ref l;
30200 
30201       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30202 	{
30203 	  rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30204 	  if (!rtl)
30205 	    return;
30206 	  decl = SYMBOL_REF_DECL (rtl);
30207 	}
30208       else
30209 	decl = TREE_OPERAND (init, 0);
30210       ref = lookup_decl_die (decl);
30211       if (ref == NULL
30212 	  || (!get_AT (ref, DW_AT_location)
30213 	      && !get_AT (ref, DW_AT_const_value)))
30214 	return;
30215       l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30216       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30217       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30218       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30219       add_AT_loc (die, DW_AT_location, l);
30220     }
30221 }
30222 
30223 /* Return NULL if l is a DWARF expression, or first op that is not
30224    valid DWARF expression.  */
30225 
30226 static dw_loc_descr_ref
non_dwarf_expression(dw_loc_descr_ref l)30227 non_dwarf_expression (dw_loc_descr_ref l)
30228 {
30229   while (l)
30230     {
30231       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30232 	return l;
30233       switch (l->dw_loc_opc)
30234 	{
30235 	case DW_OP_regx:
30236 	case DW_OP_implicit_value:
30237 	case DW_OP_stack_value:
30238 	case DW_OP_implicit_pointer:
30239 	case DW_OP_GNU_implicit_pointer:
30240 	case DW_OP_GNU_parameter_ref:
30241 	case DW_OP_piece:
30242 	case DW_OP_bit_piece:
30243 	  return l;
30244 	default:
30245 	  break;
30246 	}
30247       l = l->dw_loc_next;
30248     }
30249   return NULL;
30250 }
30251 
30252 /* Return adjusted copy of EXPR:
30253    If it is empty DWARF expression, return it.
30254    If it is valid non-empty DWARF expression,
30255    return copy of EXPR with DW_OP_deref appended to it.
30256    If it is DWARF expression followed by DW_OP_reg{N,x}, return
30257    copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30258    If it is DWARF expression followed by DW_OP_stack_value, return
30259    copy of the DWARF expression without anything appended.
30260    Otherwise, return NULL.  */
30261 
30262 static dw_loc_descr_ref
copy_deref_exprloc(dw_loc_descr_ref expr)30263 copy_deref_exprloc (dw_loc_descr_ref expr)
30264 {
30265   dw_loc_descr_ref tail = NULL;
30266 
30267   if (expr == NULL)
30268     return NULL;
30269 
30270   dw_loc_descr_ref l = non_dwarf_expression (expr);
30271   if (l && l->dw_loc_next)
30272     return NULL;
30273 
30274   if (l)
30275     {
30276       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30277 	tail = new_loc_descr ((enum dwarf_location_atom)
30278 			      (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30279 			      0, 0);
30280       else
30281 	switch (l->dw_loc_opc)
30282 	  {
30283 	  case DW_OP_regx:
30284 	    tail = new_loc_descr (DW_OP_bregx,
30285 				  l->dw_loc_oprnd1.v.val_unsigned, 0);
30286 	    break;
30287 	  case DW_OP_stack_value:
30288 	    break;
30289 	  default:
30290 	    return NULL;
30291 	  }
30292     }
30293   else
30294     tail = new_loc_descr (DW_OP_deref, 0, 0);
30295 
30296   dw_loc_descr_ref ret = NULL, *p = &ret;
30297   while (expr != l)
30298     {
30299       *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30300       (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30301       (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30302       p = &(*p)->dw_loc_next;
30303       expr = expr->dw_loc_next;
30304     }
30305   *p = tail;
30306   return ret;
30307 }
30308 
30309 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30310    reference to a variable or argument, adjust it if needed and return:
30311    -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30312       attribute if present should be removed
30313    0 keep the attribute perhaps with minor modifications, no need to rescan
30314    1 if the attribute has been successfully adjusted.  */
30315 
30316 static int
optimize_string_length(dw_attr_node * a)30317 optimize_string_length (dw_attr_node *a)
30318 {
30319   dw_loc_descr_ref l = AT_loc (a), lv;
30320   dw_die_ref die;
30321   if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30322     {
30323       tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30324       die = lookup_decl_die (decl);
30325       if (die)
30326 	{
30327 	  l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30328 	  l->dw_loc_oprnd1.v.val_die_ref.die = die;
30329 	  l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30330 	}
30331       else
30332 	return -1;
30333     }
30334   else
30335     die = l->dw_loc_oprnd1.v.val_die_ref.die;
30336 
30337   /* DWARF5 allows reference class, so we can then reference the DIE.
30338      Only do this for DW_OP_GNU_variable_value DW_OP_stack_value.  */
30339   if (l->dw_loc_next != NULL && dwarf_version >= 5)
30340     {
30341       a->dw_attr_val.val_class = dw_val_class_die_ref;
30342       a->dw_attr_val.val_entry = NULL;
30343       a->dw_attr_val.v.val_die_ref.die = die;
30344       a->dw_attr_val.v.val_die_ref.external = 0;
30345       return 0;
30346     }
30347 
30348   dw_attr_node *av = get_AT (die, DW_AT_location);
30349   dw_loc_list_ref d;
30350   bool non_dwarf_expr = false;
30351 
30352   if (av == NULL)
30353     return dwarf_strict ? -1 : 0;
30354   switch (AT_class (av))
30355     {
30356     case dw_val_class_loc_list:
30357       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30358 	if (d->expr && non_dwarf_expression (d->expr))
30359 	  non_dwarf_expr = true;
30360       break;
30361     case dw_val_class_view_list:
30362       gcc_unreachable ();
30363     case dw_val_class_loc:
30364       lv = AT_loc (av);
30365       if (lv == NULL)
30366 	return dwarf_strict ? -1 : 0;
30367       if (non_dwarf_expression (lv))
30368 	non_dwarf_expr = true;
30369       break;
30370     default:
30371       return dwarf_strict ? -1 : 0;
30372     }
30373 
30374   /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30375      into DW_OP_call4  or DW_OP_GNU_variable_value into
30376      DW_OP_call4 DW_OP_deref, do so.  */
30377   if (!non_dwarf_expr
30378       && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30379     {
30380       l->dw_loc_opc = DW_OP_call4;
30381       if (l->dw_loc_next)
30382 	l->dw_loc_next = NULL;
30383       else
30384 	l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30385       return 0;
30386     }
30387 
30388   /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30389      copy over the DW_AT_location attribute from die to a.  */
30390   if (l->dw_loc_next != NULL)
30391     {
30392       a->dw_attr_val = av->dw_attr_val;
30393       return 1;
30394     }
30395 
30396   dw_loc_list_ref list, *p;
30397   switch (AT_class (av))
30398     {
30399     case dw_val_class_loc_list:
30400       p = &list;
30401       list = NULL;
30402       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30403 	{
30404 	  lv = copy_deref_exprloc (d->expr);
30405 	  if (lv)
30406 	    {
30407 	      *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30408 	      p = &(*p)->dw_loc_next;
30409 	    }
30410 	  else if (!dwarf_strict && d->expr)
30411 	    return 0;
30412 	}
30413       if (list == NULL)
30414 	return dwarf_strict ? -1 : 0;
30415       a->dw_attr_val.val_class = dw_val_class_loc_list;
30416       gen_llsym (list);
30417       *AT_loc_list_ptr (a) = list;
30418       return 1;
30419     case dw_val_class_loc:
30420       lv = copy_deref_exprloc (AT_loc (av));
30421       if (lv == NULL)
30422 	return dwarf_strict ? -1 : 0;
30423       a->dw_attr_val.v.val_loc = lv;
30424       return 1;
30425     default:
30426       gcc_unreachable ();
30427     }
30428 }
30429 
30430 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30431    an address in .rodata section if the string literal is emitted there,
30432    or remove the containing location list or replace DW_AT_const_value
30433    with DW_AT_location and empty location expression, if it isn't found
30434    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
30435    to something that has been emitted in the current CU.  */
30436 
30437 static void
resolve_addr(dw_die_ref die)30438 resolve_addr (dw_die_ref die)
30439 {
30440   dw_die_ref c;
30441   dw_attr_node *a;
30442   dw_loc_list_ref *curr, *start, loc;
30443   unsigned ix;
30444   bool remove_AT_byte_size = false;
30445 
30446   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30447     switch (AT_class (a))
30448       {
30449       case dw_val_class_loc_list:
30450 	start = curr = AT_loc_list_ptr (a);
30451 	loc = *curr;
30452 	gcc_assert (loc);
30453 	/* The same list can be referenced more than once.  See if we have
30454 	   already recorded the result from a previous pass.  */
30455 	if (loc->replaced)
30456 	  *curr = loc->dw_loc_next;
30457 	else if (!loc->resolved_addr)
30458 	  {
30459 	    /* As things stand, we do not expect or allow one die to
30460 	       reference a suffix of another die's location list chain.
30461 	       References must be identical or completely separate.
30462 	       There is therefore no need to cache the result of this
30463 	       pass on any list other than the first; doing so
30464 	       would lead to unnecessary writes.  */
30465 	    while (*curr)
30466 	      {
30467 		gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30468 		if (!resolve_addr_in_expr (a, (*curr)->expr))
30469 		  {
30470 		    dw_loc_list_ref next = (*curr)->dw_loc_next;
30471                     dw_loc_descr_ref l = (*curr)->expr;
30472 
30473 		    if (next && (*curr)->ll_symbol)
30474 		      {
30475 			gcc_assert (!next->ll_symbol);
30476 			next->ll_symbol = (*curr)->ll_symbol;
30477 			next->vl_symbol = (*curr)->vl_symbol;
30478 		      }
30479                     if (dwarf_split_debug_info)
30480                       remove_loc_list_addr_table_entries (l);
30481 		    *curr = next;
30482 		  }
30483 		else
30484 		  {
30485 		    mark_base_types ((*curr)->expr);
30486 		    curr = &(*curr)->dw_loc_next;
30487 		  }
30488 	      }
30489 	    if (loc == *start)
30490 	      loc->resolved_addr = 1;
30491 	    else
30492 	      {
30493 		loc->replaced = 1;
30494 		loc->dw_loc_next = *start;
30495 	      }
30496 	  }
30497 	if (!*start)
30498 	  {
30499 	    remove_AT (die, a->dw_attr);
30500 	    ix--;
30501 	  }
30502 	break;
30503       case dw_val_class_view_list:
30504 	{
30505 	  gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30506 	  gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30507 	  dw_val_node *llnode
30508 	    = view_list_to_loc_list_val_node (&a->dw_attr_val);
30509 	  /* If we no longer have a loclist, or it no longer needs
30510 	     views, drop this attribute.  */
30511 	  if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30512 	    {
30513 	      remove_AT (die, a->dw_attr);
30514 	      ix--;
30515 	    }
30516 	  break;
30517 	}
30518       case dw_val_class_loc:
30519 	{
30520 	  dw_loc_descr_ref l = AT_loc (a);
30521 	  /* DW_OP_GNU_variable_value DW_OP_stack_value or
30522 	     DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30523 	     into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30524 	     DWARF4 unlike DW_OP_GNU_variable_value.  Or for DWARF5
30525 	     DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30526 	     with DW_FORM_ref referencing the same DIE as
30527 	     DW_OP_GNU_variable_value used to reference.  */
30528 	  if (a->dw_attr == DW_AT_string_length
30529 	      && l
30530 	      && l->dw_loc_opc == DW_OP_GNU_variable_value
30531 	      && (l->dw_loc_next == NULL
30532 		  || (l->dw_loc_next->dw_loc_next == NULL
30533 		      && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30534 	    {
30535 	      switch (optimize_string_length (a))
30536 		{
30537 		case -1:
30538 		  remove_AT (die, a->dw_attr);
30539 		  ix--;
30540 		  /* If we drop DW_AT_string_length, we need to drop also
30541 		     DW_AT_{string_length_,}byte_size.  */
30542 		  remove_AT_byte_size = true;
30543 		  continue;
30544 		default:
30545 		  break;
30546 		case 1:
30547 		  /* Even if we keep the optimized DW_AT_string_length,
30548 		     it might have changed AT_class, so process it again.  */
30549 		  ix--;
30550 		  continue;
30551 		}
30552 	    }
30553 	  /* For -gdwarf-2 don't attempt to optimize
30554 	     DW_AT_data_member_location containing
30555 	     DW_OP_plus_uconst - older consumers might
30556 	     rely on it being that op instead of a more complex,
30557 	     but shorter, location description.  */
30558 	  if ((dwarf_version > 2
30559 	       || a->dw_attr != DW_AT_data_member_location
30560 	       || l == NULL
30561 	       || l->dw_loc_opc != DW_OP_plus_uconst
30562 	       || l->dw_loc_next != NULL)
30563 	      && !resolve_addr_in_expr (a, l))
30564 	    {
30565 	      if (dwarf_split_debug_info)
30566 		remove_loc_list_addr_table_entries (l);
30567 	      if (l != NULL
30568 		  && l->dw_loc_next == NULL
30569 		  && l->dw_loc_opc == DW_OP_addr
30570 		  && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30571 		  && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30572 		  && a->dw_attr == DW_AT_location)
30573 		{
30574 		  tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30575 		  remove_AT (die, a->dw_attr);
30576 		  ix--;
30577 		  optimize_location_into_implicit_ptr (die, decl);
30578 		  break;
30579 		}
30580 	      if (a->dw_attr == DW_AT_string_length)
30581 		/* If we drop DW_AT_string_length, we need to drop also
30582 		   DW_AT_{string_length_,}byte_size.  */
30583 		remove_AT_byte_size = true;
30584 	      remove_AT (die, a->dw_attr);
30585 	      ix--;
30586 	    }
30587 	  else
30588 	    mark_base_types (l);
30589 	}
30590 	break;
30591       case dw_val_class_addr:
30592 	if (a->dw_attr == DW_AT_const_value
30593 	    && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30594 	  {
30595             if (AT_index (a) != NOT_INDEXED)
30596               remove_addr_table_entry (a->dw_attr_val.val_entry);
30597 	    remove_AT (die, a->dw_attr);
30598 	    ix--;
30599 	  }
30600 	if ((die->die_tag == DW_TAG_call_site
30601 	     && a->dw_attr == DW_AT_call_origin)
30602 	    || (die->die_tag == DW_TAG_GNU_call_site
30603 		&& a->dw_attr == DW_AT_abstract_origin))
30604 	  {
30605 	    tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30606 	    dw_die_ref tdie = lookup_decl_die (tdecl);
30607 	    dw_die_ref cdie;
30608 	    if (tdie == NULL
30609 		&& DECL_EXTERNAL (tdecl)
30610 		&& DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30611 		&& (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30612 	      {
30613 		dw_die_ref pdie = cdie;
30614 		/* Make sure we don't add these DIEs into type units.
30615 		   We could emit skeleton DIEs for context (namespaces,
30616 		   outer structs/classes) and a skeleton DIE for the
30617 		   innermost context with DW_AT_signature pointing to the
30618 		   type unit.  See PR78835.  */
30619 		while (pdie && pdie->die_tag != DW_TAG_type_unit)
30620 		  pdie = pdie->die_parent;
30621 		if (pdie == NULL)
30622 		  {
30623 		    /* Creating a full DIE for tdecl is overly expensive and
30624 		       at this point even wrong when in the LTO phase
30625 		       as it can end up generating new type DIEs we didn't
30626 		       output and thus optimize_external_refs will crash.  */
30627 		    tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30628 		    add_AT_flag (tdie, DW_AT_external, 1);
30629 		    add_AT_flag (tdie, DW_AT_declaration, 1);
30630 		    add_linkage_attr (tdie, tdecl);
30631 		    add_name_and_src_coords_attributes (tdie, tdecl, true);
30632 		    equate_decl_number_to_die (tdecl, tdie);
30633 		  }
30634 	      }
30635 	    if (tdie)
30636 	      {
30637 		a->dw_attr_val.val_class = dw_val_class_die_ref;
30638 		a->dw_attr_val.v.val_die_ref.die = tdie;
30639 		a->dw_attr_val.v.val_die_ref.external = 0;
30640 	      }
30641 	    else
30642 	      {
30643                 if (AT_index (a) != NOT_INDEXED)
30644                   remove_addr_table_entry (a->dw_attr_val.val_entry);
30645 		remove_AT (die, a->dw_attr);
30646 		ix--;
30647 	      }
30648 	  }
30649 	break;
30650       default:
30651 	break;
30652       }
30653 
30654   if (remove_AT_byte_size)
30655     remove_AT (die, dwarf_version >= 5
30656 		    ? DW_AT_string_length_byte_size
30657 		    : DW_AT_byte_size);
30658 
30659   FOR_EACH_CHILD (die, c, resolve_addr (c));
30660 }
30661 
30662 /* Helper routines for optimize_location_lists.
30663    This pass tries to share identical local lists in .debug_loc
30664    section.  */
30665 
30666 /* Iteratively hash operands of LOC opcode into HSTATE.  */
30667 
30668 static void
hash_loc_operands(dw_loc_descr_ref loc,inchash::hash & hstate)30669 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30670 {
30671   dw_val_ref val1 = &loc->dw_loc_oprnd1;
30672   dw_val_ref val2 = &loc->dw_loc_oprnd2;
30673 
30674   switch (loc->dw_loc_opc)
30675     {
30676     case DW_OP_const4u:
30677     case DW_OP_const8u:
30678       if (loc->dtprel)
30679 	goto hash_addr;
30680       /* FALLTHRU */
30681     case DW_OP_const1u:
30682     case DW_OP_const1s:
30683     case DW_OP_const2u:
30684     case DW_OP_const2s:
30685     case DW_OP_const4s:
30686     case DW_OP_const8s:
30687     case DW_OP_constu:
30688     case DW_OP_consts:
30689     case DW_OP_pick:
30690     case DW_OP_plus_uconst:
30691     case DW_OP_breg0:
30692     case DW_OP_breg1:
30693     case DW_OP_breg2:
30694     case DW_OP_breg3:
30695     case DW_OP_breg4:
30696     case DW_OP_breg5:
30697     case DW_OP_breg6:
30698     case DW_OP_breg7:
30699     case DW_OP_breg8:
30700     case DW_OP_breg9:
30701     case DW_OP_breg10:
30702     case DW_OP_breg11:
30703     case DW_OP_breg12:
30704     case DW_OP_breg13:
30705     case DW_OP_breg14:
30706     case DW_OP_breg15:
30707     case DW_OP_breg16:
30708     case DW_OP_breg17:
30709     case DW_OP_breg18:
30710     case DW_OP_breg19:
30711     case DW_OP_breg20:
30712     case DW_OP_breg21:
30713     case DW_OP_breg22:
30714     case DW_OP_breg23:
30715     case DW_OP_breg24:
30716     case DW_OP_breg25:
30717     case DW_OP_breg26:
30718     case DW_OP_breg27:
30719     case DW_OP_breg28:
30720     case DW_OP_breg29:
30721     case DW_OP_breg30:
30722     case DW_OP_breg31:
30723     case DW_OP_regx:
30724     case DW_OP_fbreg:
30725     case DW_OP_piece:
30726     case DW_OP_deref_size:
30727     case DW_OP_xderef_size:
30728       hstate.add_object (val1->v.val_int);
30729       break;
30730     case DW_OP_skip:
30731     case DW_OP_bra:
30732       {
30733 	int offset;
30734 
30735 	gcc_assert (val1->val_class == dw_val_class_loc);
30736 	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30737 	hstate.add_object (offset);
30738       }
30739       break;
30740     case DW_OP_implicit_value:
30741       hstate.add_object (val1->v.val_unsigned);
30742       switch (val2->val_class)
30743 	{
30744 	case dw_val_class_const:
30745 	  hstate.add_object (val2->v.val_int);
30746 	  break;
30747 	case dw_val_class_vec:
30748 	  {
30749 	    unsigned int elt_size = val2->v.val_vec.elt_size;
30750 	    unsigned int len = val2->v.val_vec.length;
30751 
30752 	    hstate.add_int (elt_size);
30753 	    hstate.add_int (len);
30754 	    hstate.add (val2->v.val_vec.array, len * elt_size);
30755 	  }
30756 	  break;
30757 	case dw_val_class_const_double:
30758 	  hstate.add_object (val2->v.val_double.low);
30759 	  hstate.add_object (val2->v.val_double.high);
30760 	  break;
30761 	case dw_val_class_wide_int:
30762 	  hstate.add (val2->v.val_wide->get_val (),
30763 		      get_full_len (*val2->v.val_wide)
30764 		      * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30765 	  break;
30766 	case dw_val_class_addr:
30767 	  inchash::add_rtx (val2->v.val_addr, hstate);
30768 	  break;
30769 	default:
30770 	  gcc_unreachable ();
30771 	}
30772       break;
30773     case DW_OP_bregx:
30774     case DW_OP_bit_piece:
30775       hstate.add_object (val1->v.val_int);
30776       hstate.add_object (val2->v.val_int);
30777       break;
30778     case DW_OP_addr:
30779     hash_addr:
30780       if (loc->dtprel)
30781 	{
30782 	  unsigned char dtprel = 0xd1;
30783 	  hstate.add_object (dtprel);
30784 	}
30785       inchash::add_rtx (val1->v.val_addr, hstate);
30786       break;
30787     case DW_OP_GNU_addr_index:
30788     case DW_OP_addrx:
30789     case DW_OP_GNU_const_index:
30790     case DW_OP_constx:
30791       {
30792         if (loc->dtprel)
30793           {
30794             unsigned char dtprel = 0xd1;
30795 	    hstate.add_object (dtprel);
30796           }
30797         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30798       }
30799       break;
30800     case DW_OP_implicit_pointer:
30801     case DW_OP_GNU_implicit_pointer:
30802       hstate.add_int (val2->v.val_int);
30803       break;
30804     case DW_OP_entry_value:
30805     case DW_OP_GNU_entry_value:
30806       hstate.add_object (val1->v.val_loc);
30807       break;
30808     case DW_OP_regval_type:
30809     case DW_OP_deref_type:
30810     case DW_OP_GNU_regval_type:
30811     case DW_OP_GNU_deref_type:
30812       {
30813 	unsigned int byte_size
30814 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30815 	unsigned int encoding
30816 	  = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30817 	hstate.add_object (val1->v.val_int);
30818 	hstate.add_object (byte_size);
30819 	hstate.add_object (encoding);
30820       }
30821       break;
30822     case DW_OP_convert:
30823     case DW_OP_reinterpret:
30824     case DW_OP_GNU_convert:
30825     case DW_OP_GNU_reinterpret:
30826       if (val1->val_class == dw_val_class_unsigned_const)
30827 	{
30828 	  hstate.add_object (val1->v.val_unsigned);
30829 	  break;
30830 	}
30831       /* FALLTHRU */
30832     case DW_OP_const_type:
30833     case DW_OP_GNU_const_type:
30834       {
30835 	unsigned int byte_size
30836 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30837 	unsigned int encoding
30838 	  = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30839 	hstate.add_object (byte_size);
30840 	hstate.add_object (encoding);
30841 	if (loc->dw_loc_opc != DW_OP_const_type
30842 	    && loc->dw_loc_opc != DW_OP_GNU_const_type)
30843 	  break;
30844 	hstate.add_object (val2->val_class);
30845 	switch (val2->val_class)
30846 	  {
30847 	  case dw_val_class_const:
30848 	    hstate.add_object (val2->v.val_int);
30849 	    break;
30850 	  case dw_val_class_vec:
30851 	    {
30852 	      unsigned int elt_size = val2->v.val_vec.elt_size;
30853 	      unsigned int len = val2->v.val_vec.length;
30854 
30855 	      hstate.add_object (elt_size);
30856 	      hstate.add_object (len);
30857 	      hstate.add (val2->v.val_vec.array, len * elt_size);
30858 	    }
30859 	    break;
30860 	  case dw_val_class_const_double:
30861 	    hstate.add_object (val2->v.val_double.low);
30862 	    hstate.add_object (val2->v.val_double.high);
30863 	    break;
30864 	  case dw_val_class_wide_int:
30865 	    hstate.add (val2->v.val_wide->get_val (),
30866 			get_full_len (*val2->v.val_wide)
30867 			* HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30868 	    break;
30869 	  default:
30870 	    gcc_unreachable ();
30871 	  }
30872       }
30873       break;
30874 
30875     default:
30876       /* Other codes have no operands.  */
30877       break;
30878     }
30879 }
30880 
30881 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
30882 
30883 static inline void
hash_locs(dw_loc_descr_ref loc,inchash::hash & hstate)30884 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30885 {
30886   dw_loc_descr_ref l;
30887   bool sizes_computed = false;
30888   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
30889   size_of_locs (loc);
30890 
30891   for (l = loc; l != NULL; l = l->dw_loc_next)
30892     {
30893       enum dwarf_location_atom opc = l->dw_loc_opc;
30894       hstate.add_object (opc);
30895       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30896 	{
30897 	  size_of_locs (loc);
30898 	  sizes_computed = true;
30899 	}
30900       hash_loc_operands (l, hstate);
30901     }
30902 }
30903 
30904 /* Compute hash of the whole location list LIST_HEAD.  */
30905 
30906 static inline void
hash_loc_list(dw_loc_list_ref list_head)30907 hash_loc_list (dw_loc_list_ref list_head)
30908 {
30909   dw_loc_list_ref curr = list_head;
30910   inchash::hash hstate;
30911 
30912   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30913     {
30914       hstate.add (curr->begin, strlen (curr->begin) + 1);
30915       hstate.add (curr->end, strlen (curr->end) + 1);
30916       hstate.add_object (curr->vbegin);
30917       hstate.add_object (curr->vend);
30918       if (curr->section)
30919 	hstate.add (curr->section, strlen (curr->section) + 1);
30920       hash_locs (curr->expr, hstate);
30921     }
30922   list_head->hash = hstate.end ();
30923 }
30924 
30925 /* Return true if X and Y opcodes have the same operands.  */
30926 
30927 static inline bool
compare_loc_operands(dw_loc_descr_ref x,dw_loc_descr_ref y)30928 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30929 {
30930   dw_val_ref valx1 = &x->dw_loc_oprnd1;
30931   dw_val_ref valx2 = &x->dw_loc_oprnd2;
30932   dw_val_ref valy1 = &y->dw_loc_oprnd1;
30933   dw_val_ref valy2 = &y->dw_loc_oprnd2;
30934 
30935   switch (x->dw_loc_opc)
30936     {
30937     case DW_OP_const4u:
30938     case DW_OP_const8u:
30939       if (x->dtprel)
30940 	goto hash_addr;
30941       /* FALLTHRU */
30942     case DW_OP_const1u:
30943     case DW_OP_const1s:
30944     case DW_OP_const2u:
30945     case DW_OP_const2s:
30946     case DW_OP_const4s:
30947     case DW_OP_const8s:
30948     case DW_OP_constu:
30949     case DW_OP_consts:
30950     case DW_OP_pick:
30951     case DW_OP_plus_uconst:
30952     case DW_OP_breg0:
30953     case DW_OP_breg1:
30954     case DW_OP_breg2:
30955     case DW_OP_breg3:
30956     case DW_OP_breg4:
30957     case DW_OP_breg5:
30958     case DW_OP_breg6:
30959     case DW_OP_breg7:
30960     case DW_OP_breg8:
30961     case DW_OP_breg9:
30962     case DW_OP_breg10:
30963     case DW_OP_breg11:
30964     case DW_OP_breg12:
30965     case DW_OP_breg13:
30966     case DW_OP_breg14:
30967     case DW_OP_breg15:
30968     case DW_OP_breg16:
30969     case DW_OP_breg17:
30970     case DW_OP_breg18:
30971     case DW_OP_breg19:
30972     case DW_OP_breg20:
30973     case DW_OP_breg21:
30974     case DW_OP_breg22:
30975     case DW_OP_breg23:
30976     case DW_OP_breg24:
30977     case DW_OP_breg25:
30978     case DW_OP_breg26:
30979     case DW_OP_breg27:
30980     case DW_OP_breg28:
30981     case DW_OP_breg29:
30982     case DW_OP_breg30:
30983     case DW_OP_breg31:
30984     case DW_OP_regx:
30985     case DW_OP_fbreg:
30986     case DW_OP_piece:
30987     case DW_OP_deref_size:
30988     case DW_OP_xderef_size:
30989       return valx1->v.val_int == valy1->v.val_int;
30990     case DW_OP_skip:
30991     case DW_OP_bra:
30992       /* If splitting debug info, the use of DW_OP_GNU_addr_index
30993         can cause irrelevant differences in dw_loc_addr.  */
30994       gcc_assert (valx1->val_class == dw_val_class_loc
30995 		  && valy1->val_class == dw_val_class_loc
30996                   && (dwarf_split_debug_info
30997                       || x->dw_loc_addr == y->dw_loc_addr));
30998       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30999     case DW_OP_implicit_value:
31000       if (valx1->v.val_unsigned != valy1->v.val_unsigned
31001 	  || valx2->val_class != valy2->val_class)
31002 	return false;
31003       switch (valx2->val_class)
31004 	{
31005 	case dw_val_class_const:
31006 	  return valx2->v.val_int == valy2->v.val_int;
31007 	case dw_val_class_vec:
31008 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31009 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
31010 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31011 			    valx2->v.val_vec.elt_size
31012 			    * valx2->v.val_vec.length) == 0;
31013 	case dw_val_class_const_double:
31014 	  return valx2->v.val_double.low == valy2->v.val_double.low
31015 		 && valx2->v.val_double.high == valy2->v.val_double.high;
31016 	case dw_val_class_wide_int:
31017 	  return *valx2->v.val_wide == *valy2->v.val_wide;
31018 	case dw_val_class_addr:
31019 	  return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31020 	default:
31021 	  gcc_unreachable ();
31022 	}
31023     case DW_OP_bregx:
31024     case DW_OP_bit_piece:
31025       return valx1->v.val_int == valy1->v.val_int
31026 	     && valx2->v.val_int == valy2->v.val_int;
31027     case DW_OP_addr:
31028     hash_addr:
31029       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31030     case DW_OP_GNU_addr_index:
31031     case DW_OP_addrx:
31032     case DW_OP_GNU_const_index:
31033     case DW_OP_constx:
31034       {
31035         rtx ax1 = valx1->val_entry->addr.rtl;
31036         rtx ay1 = valy1->val_entry->addr.rtl;
31037         return rtx_equal_p (ax1, ay1);
31038       }
31039     case DW_OP_implicit_pointer:
31040     case DW_OP_GNU_implicit_pointer:
31041       return valx1->val_class == dw_val_class_die_ref
31042 	     && valx1->val_class == valy1->val_class
31043 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31044 	     && valx2->v.val_int == valy2->v.val_int;
31045     case DW_OP_entry_value:
31046     case DW_OP_GNU_entry_value:
31047       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31048     case DW_OP_const_type:
31049     case DW_OP_GNU_const_type:
31050       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31051 	  || valx2->val_class != valy2->val_class)
31052 	return false;
31053       switch (valx2->val_class)
31054 	{
31055 	case dw_val_class_const:
31056 	  return valx2->v.val_int == valy2->v.val_int;
31057 	case dw_val_class_vec:
31058 	  return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31059 		 && valx2->v.val_vec.length == valy2->v.val_vec.length
31060 		 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31061 			    valx2->v.val_vec.elt_size
31062 			    * valx2->v.val_vec.length) == 0;
31063 	case dw_val_class_const_double:
31064 	  return valx2->v.val_double.low == valy2->v.val_double.low
31065 		 && valx2->v.val_double.high == valy2->v.val_double.high;
31066 	case dw_val_class_wide_int:
31067 	  return *valx2->v.val_wide == *valy2->v.val_wide;
31068 	default:
31069 	  gcc_unreachable ();
31070 	}
31071     case DW_OP_regval_type:
31072     case DW_OP_deref_type:
31073     case DW_OP_GNU_regval_type:
31074     case DW_OP_GNU_deref_type:
31075       return valx1->v.val_int == valy1->v.val_int
31076 	     && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31077     case DW_OP_convert:
31078     case DW_OP_reinterpret:
31079     case DW_OP_GNU_convert:
31080     case DW_OP_GNU_reinterpret:
31081       if (valx1->val_class != valy1->val_class)
31082 	return false;
31083       if (valx1->val_class == dw_val_class_unsigned_const)
31084 	return valx1->v.val_unsigned == valy1->v.val_unsigned;
31085       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31086     case DW_OP_GNU_parameter_ref:
31087       return valx1->val_class == dw_val_class_die_ref
31088 	     && valx1->val_class == valy1->val_class
31089 	     && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31090     default:
31091       /* Other codes have no operands.  */
31092       return true;
31093     }
31094 }
31095 
31096 /* Return true if DWARF location expressions X and Y are the same.  */
31097 
31098 static inline bool
compare_locs(dw_loc_descr_ref x,dw_loc_descr_ref y)31099 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31100 {
31101   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31102     if (x->dw_loc_opc != y->dw_loc_opc
31103 	|| x->dtprel != y->dtprel
31104 	|| !compare_loc_operands (x, y))
31105       break;
31106   return x == NULL && y == NULL;
31107 }
31108 
31109 /* Hashtable helpers.  */
31110 
31111 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31112 {
31113   static inline hashval_t hash (const dw_loc_list_struct *);
31114   static inline bool equal (const dw_loc_list_struct *,
31115 			    const dw_loc_list_struct *);
31116 };
31117 
31118 /* Return precomputed hash of location list X.  */
31119 
31120 inline hashval_t
hash(const dw_loc_list_struct * x)31121 loc_list_hasher::hash (const dw_loc_list_struct *x)
31122 {
31123   return x->hash;
31124 }
31125 
31126 /* Return true if location lists A and B are the same.  */
31127 
31128 inline bool
equal(const dw_loc_list_struct * a,const dw_loc_list_struct * b)31129 loc_list_hasher::equal (const dw_loc_list_struct *a,
31130 			const dw_loc_list_struct *b)
31131 {
31132   if (a == b)
31133     return 1;
31134   if (a->hash != b->hash)
31135     return 0;
31136   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31137     if (strcmp (a->begin, b->begin) != 0
31138 	|| strcmp (a->end, b->end) != 0
31139 	|| (a->section == NULL) != (b->section == NULL)
31140 	|| (a->section && strcmp (a->section, b->section) != 0)
31141 	|| a->vbegin != b->vbegin || a->vend != b->vend
31142 	|| !compare_locs (a->expr, b->expr))
31143       break;
31144   return a == NULL && b == NULL;
31145 }
31146 
31147 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31148 
31149 
31150 /* Recursively optimize location lists referenced from DIE
31151    children and share them whenever possible.  */
31152 
31153 static void
optimize_location_lists_1(dw_die_ref die,loc_list_hash_type * htab)31154 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31155 {
31156   dw_die_ref c;
31157   dw_attr_node *a;
31158   unsigned ix;
31159   dw_loc_list_struct **slot;
31160   bool drop_locviews = false;
31161   bool has_locviews = false;
31162 
31163   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31164     if (AT_class (a) == dw_val_class_loc_list)
31165       {
31166 	dw_loc_list_ref list = AT_loc_list (a);
31167 	/* TODO: perform some optimizations here, before hashing
31168 	   it and storing into the hash table.  */
31169 	hash_loc_list (list);
31170 	slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31171 	if (*slot == NULL)
31172 	  {
31173 	    *slot = list;
31174 	    if (loc_list_has_views (list))
31175 	      gcc_assert (list->vl_symbol);
31176 	    else if (list->vl_symbol)
31177 	      {
31178 		drop_locviews = true;
31179 		list->vl_symbol = NULL;
31180 	      }
31181 	  }
31182 	else
31183 	  {
31184 	    if (list->vl_symbol && !(*slot)->vl_symbol)
31185 	      drop_locviews = true;
31186 	    a->dw_attr_val.v.val_loc_list = *slot;
31187 	  }
31188       }
31189     else if (AT_class (a) == dw_val_class_view_list)
31190       {
31191 	gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31192 	has_locviews = true;
31193       }
31194 
31195 
31196   if (drop_locviews && has_locviews)
31197     remove_AT (die, DW_AT_GNU_locviews);
31198 
31199   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31200 }
31201 
31202 
31203 /* Recursively assign each location list a unique index into the debug_addr
31204    section.  */
31205 
31206 static void
index_location_lists(dw_die_ref die)31207 index_location_lists (dw_die_ref die)
31208 {
31209   dw_die_ref c;
31210   dw_attr_node *a;
31211   unsigned ix;
31212 
31213   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31214     if (AT_class (a) == dw_val_class_loc_list)
31215       {
31216         dw_loc_list_ref list = AT_loc_list (a);
31217         dw_loc_list_ref curr;
31218         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31219           {
31220             /* Don't index an entry that has already been indexed
31221 	       or won't be output.  Make sure skip_loc_list_entry doesn't
31222 	       call size_of_locs, because that might cause circular dependency,
31223 	       index_location_lists requiring address table indexes to be
31224 	       computed, but adding new indexes through add_addr_table_entry
31225 	       and address table index computation requiring no new additions
31226 	       to the hash table.  In the rare case of DWARF[234] >= 64KB
31227 	       location expression, we'll just waste unused address table entry
31228 	       for it.  */
31229             if (curr->begin_entry != NULL
31230                 || skip_loc_list_entry (curr))
31231               continue;
31232 
31233             curr->begin_entry
31234 	      = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31235           }
31236       }
31237 
31238   FOR_EACH_CHILD (die, c, index_location_lists (c));
31239 }
31240 
31241 /* Optimize location lists referenced from DIE
31242    children and share them whenever possible.  */
31243 
31244 static void
optimize_location_lists(dw_die_ref die)31245 optimize_location_lists (dw_die_ref die)
31246 {
31247   loc_list_hash_type htab (500);
31248   optimize_location_lists_1 (die, &htab);
31249 }
31250 
31251 /* Traverse the limbo die list, and add parent/child links.  The only
31252    dies without parents that should be here are concrete instances of
31253    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
31254    For concrete instances, we can get the parent die from the abstract
31255    instance.  */
31256 
31257 static void
flush_limbo_die_list(void)31258 flush_limbo_die_list (void)
31259 {
31260   limbo_die_node *node;
31261 
31262   /* get_context_die calls force_decl_die, which can put new DIEs on the
31263      limbo list in LTO mode when nested functions are put in a different
31264      partition than that of their parent function.  */
31265   while ((node = limbo_die_list))
31266     {
31267       dw_die_ref die = node->die;
31268       limbo_die_list = node->next;
31269 
31270       if (die->die_parent == NULL)
31271 	{
31272 	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31273 
31274 	  if (origin && origin->die_parent)
31275 	    add_child_die (origin->die_parent, die);
31276 	  else if (is_cu_die (die))
31277 	    ;
31278 	  else if (seen_error ())
31279 	    /* It's OK to be confused by errors in the input.  */
31280 	    add_child_die (comp_unit_die (), die);
31281 	  else
31282 	    {
31283 	      /* In certain situations, the lexical block containing a
31284 		 nested function can be optimized away, which results
31285 		 in the nested function die being orphaned.  Likewise
31286 		 with the return type of that nested function.  Force
31287 		 this to be a child of the containing function.
31288 
31289 		 It may happen that even the containing function got fully
31290 		 inlined and optimized out.  In that case we are lost and
31291 		 assign the empty child.  This should not be big issue as
31292 		 the function is likely unreachable too.  */
31293 	      gcc_assert (node->created_for);
31294 
31295 	      if (DECL_P (node->created_for))
31296 		origin = get_context_die (DECL_CONTEXT (node->created_for));
31297 	      else if (TYPE_P (node->created_for))
31298 		origin = scope_die_for (node->created_for, comp_unit_die ());
31299 	      else
31300 		origin = comp_unit_die ();
31301 
31302 	      add_child_die (origin, die);
31303 	    }
31304 	}
31305     }
31306 }
31307 
31308 /* Reset DIEs so we can output them again.  */
31309 
31310 static void
reset_dies(dw_die_ref die)31311 reset_dies (dw_die_ref die)
31312 {
31313   dw_die_ref c;
31314 
31315   /* Remove stuff we re-generate.  */
31316   die->die_mark = 0;
31317   die->die_offset = 0;
31318   die->die_abbrev = 0;
31319   remove_AT (die, DW_AT_sibling);
31320 
31321   FOR_EACH_CHILD (die, c, reset_dies (c));
31322 }
31323 
31324 /* Output stuff that dwarf requires at the end of every file,
31325    and generate the DWARF-2 debugging info.  */
31326 
31327 static void
dwarf2out_finish(const char * filename)31328 dwarf2out_finish (const char *filename)
31329 {
31330   comdat_type_node *ctnode;
31331   dw_die_ref main_comp_unit_die;
31332   unsigned char checksum[16];
31333   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31334 
31335   /* Flush out any latecomers to the limbo party.  */
31336   flush_limbo_die_list ();
31337 
31338   if (inline_entry_data_table)
31339     gcc_assert (inline_entry_data_table->is_empty ());
31340 
31341   if (flag_checking)
31342     {
31343       verify_die (comp_unit_die ());
31344       for (limbo_die_node *node = cu_die_list; node; node = node->next)
31345 	verify_die (node->die);
31346     }
31347 
31348   /* We shouldn't have any symbols with delayed asm names for
31349      DIEs generated after early finish.  */
31350   gcc_assert (deferred_asm_name == NULL);
31351 
31352   gen_remaining_tmpl_value_param_die_attribute ();
31353 
31354   if (flag_generate_lto || flag_generate_offload)
31355     {
31356       gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31357 
31358       /* Prune stuff so that dwarf2out_finish runs successfully
31359 	 for the fat part of the object.  */
31360       reset_dies (comp_unit_die ());
31361       for (limbo_die_node *node = cu_die_list; node; node = node->next)
31362 	reset_dies (node->die);
31363 
31364       hash_table<comdat_type_hasher> comdat_type_table (100);
31365       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31366 	{
31367 	  comdat_type_node **slot
31368 	      = comdat_type_table.find_slot (ctnode, INSERT);
31369 
31370 	  /* Don't reset types twice.  */
31371 	  if (*slot != HTAB_EMPTY_ENTRY)
31372 	    continue;
31373 
31374 	  /* Remove the pointer to the line table.  */
31375 	  remove_AT (ctnode->root_die, DW_AT_stmt_list);
31376 
31377 	  if (debug_info_level >= DINFO_LEVEL_TERSE)
31378 	    reset_dies (ctnode->root_die);
31379 
31380 	  *slot = ctnode;
31381 	}
31382 
31383       /* Reset die CU symbol so we don't output it twice.  */
31384       comp_unit_die ()->die_id.die_symbol = NULL;
31385 
31386       /* Remove DW_AT_macro and DW_AT_stmt_list from the early output.  */
31387       remove_AT (comp_unit_die (), DW_AT_stmt_list);
31388       if (have_macinfo)
31389 	remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31390 
31391       /* Remove indirect string decisions.  */
31392       debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31393       if (debug_line_str_hash)
31394 	{
31395 	  debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31396 	  debug_line_str_hash = NULL;
31397 	}
31398     }
31399 
31400 #if ENABLE_ASSERT_CHECKING
31401   {
31402     dw_die_ref die = comp_unit_die (), c;
31403     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31404   }
31405 #endif
31406   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31407     resolve_addr (ctnode->root_die);
31408   resolve_addr (comp_unit_die ());
31409   move_marked_base_types ();
31410 
31411   if (dump_file)
31412     {
31413       fprintf (dump_file, "DWARF for %s\n", filename);
31414       print_die (comp_unit_die (), dump_file);
31415     }
31416 
31417   /* Initialize sections and labels used for actual assembler output.  */
31418   unsigned generation = init_sections_and_labels (false);
31419 
31420   /* Traverse the DIE's and add sibling attributes to those DIE's that
31421      have children.  */
31422   add_sibling_attributes (comp_unit_die ());
31423   limbo_die_node *node;
31424   for (node = cu_die_list; node; node = node->next)
31425     add_sibling_attributes (node->die);
31426   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31427     add_sibling_attributes (ctnode->root_die);
31428 
31429   /* When splitting DWARF info, we put some attributes in the
31430      skeleton compile_unit DIE that remains in the .o, while
31431      most attributes go in the DWO compile_unit_die.  */
31432   if (dwarf_split_debug_info)
31433     {
31434       limbo_die_node *cu;
31435       main_comp_unit_die = gen_compile_unit_die (NULL);
31436       if (dwarf_version >= 5)
31437 	main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31438       cu = limbo_die_list;
31439       gcc_assert (cu->die == main_comp_unit_die);
31440       limbo_die_list = limbo_die_list->next;
31441       cu->next = cu_die_list;
31442       cu_die_list = cu;
31443     }
31444   else
31445     main_comp_unit_die = comp_unit_die ();
31446 
31447   /* Output a terminator label for the .text section.  */
31448   switch_to_section (text_section);
31449   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31450   if (cold_text_section)
31451     {
31452       switch_to_section (cold_text_section);
31453       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31454     }
31455 
31456   /* We can only use the low/high_pc attributes if all of the code was
31457      in .text.  */
31458   if (!have_multiple_function_sections
31459       || (dwarf_version < 3 && dwarf_strict))
31460     {
31461       /* Don't add if the CU has no associated code.  */
31462       if (text_section_used)
31463         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31464                             text_end_label, true);
31465     }
31466   else
31467     {
31468       unsigned fde_idx;
31469       dw_fde_ref fde;
31470       bool range_list_added = false;
31471 
31472       if (text_section_used)
31473         add_ranges_by_labels (main_comp_unit_die, text_section_label,
31474                               text_end_label, &range_list_added, true);
31475       if (cold_text_section_used)
31476         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31477                               cold_end_label, &range_list_added, true);
31478 
31479       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31480 	{
31481 	  if (DECL_IGNORED_P (fde->decl))
31482 	    continue;
31483 	  if (!fde->in_std_section)
31484             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31485                                   fde->dw_fde_end, &range_list_added,
31486                                   true);
31487 	  if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31488             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31489                                   fde->dw_fde_second_end, &range_list_added,
31490                                   true);
31491 	}
31492 
31493       if (range_list_added)
31494 	{
31495 	  /* We need to give .debug_loc and .debug_ranges an appropriate
31496 	     "base address".  Use zero so that these addresses become
31497 	     absolute.  Historically, we've emitted the unexpected
31498 	     DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31499 	     Emit both to give time for other tools to adapt.  */
31500           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31501 	  if (! dwarf_strict && dwarf_version < 4)
31502             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31503 
31504 	  add_ranges (NULL);
31505 	}
31506     }
31507 
31508   /* AIX Assembler inserts the length, so adjust the reference to match the
31509      offset expected by debuggers.  */
31510   strcpy (dl_section_ref, debug_line_section_label);
31511   if (XCOFF_DEBUGGING_INFO)
31512     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31513 
31514   if (debug_info_level >= DINFO_LEVEL_TERSE)
31515     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31516 		    dl_section_ref);
31517 
31518   if (have_macinfo)
31519     add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31520 		   macinfo_section_label);
31521 
31522   if (dwarf_split_debug_info)
31523     {
31524       if (have_location_lists)
31525 	{
31526 	  /* Since we generate the loclists in the split DWARF .dwo
31527 	     file itself, we don't need to generate a loclists_base
31528 	     attribute for the split compile unit DIE.  That attribute
31529 	     (and using relocatable sec_offset FORMs) isn't allowed
31530 	     for a split compile unit.  Only if the .debug_loclists
31531 	     section was in the main file, would we need to generate a
31532 	     loclists_base attribute here (for the full or skeleton
31533 	     unit DIE).  */
31534 
31535 	  /* optimize_location_lists calculates the size of the lists,
31536 	     so index them first, and assign indices to the entries.
31537 	     Although optimize_location_lists will remove entries from
31538 	     the table, it only does so for duplicates, and therefore
31539 	     only reduces ref_counts to 1.  */
31540 	  index_location_lists (comp_unit_die ());
31541 	}
31542 
31543       if (addr_index_table != NULL)
31544         {
31545           unsigned int index = 0;
31546           addr_index_table
31547 	    ->traverse_noresize<unsigned int *, index_addr_table_entry>
31548 	    (&index);
31549         }
31550     }
31551 
31552   loc_list_idx = 0;
31553   if (have_location_lists)
31554     {
31555       optimize_location_lists (comp_unit_die ());
31556       /* And finally assign indexes to the entries for -gsplit-dwarf.  */
31557       if (dwarf_version >= 5 && dwarf_split_debug_info)
31558 	assign_location_list_indexes (comp_unit_die ());
31559     }
31560 
31561   save_macinfo_strings ();
31562 
31563   if (dwarf_split_debug_info)
31564     {
31565       unsigned int index = 0;
31566 
31567       /* Add attributes common to skeleton compile_units and
31568          type_units.  Because these attributes include strings, it
31569          must be done before freezing the string table.  Top-level
31570          skeleton die attrs are added when the skeleton type unit is
31571          created, so ensure it is created by this point.  */
31572       add_top_level_skeleton_die_attrs (main_comp_unit_die);
31573       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31574     }
31575 
31576   /* Output all of the compilation units.  We put the main one last so that
31577      the offsets are available to output_pubnames.  */
31578   for (node = cu_die_list; node; node = node->next)
31579     output_comp_unit (node->die, 0, NULL);
31580 
31581   hash_table<comdat_type_hasher> comdat_type_table (100);
31582   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31583     {
31584       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31585 
31586       /* Don't output duplicate types.  */
31587       if (*slot != HTAB_EMPTY_ENTRY)
31588         continue;
31589 
31590       /* Add a pointer to the line table for the main compilation unit
31591          so that the debugger can make sense of DW_AT_decl_file
31592          attributes.  */
31593       if (debug_info_level >= DINFO_LEVEL_TERSE)
31594         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31595                         (!dwarf_split_debug_info
31596                          ? dl_section_ref
31597                          : debug_skeleton_line_section_label));
31598 
31599       output_comdat_type_unit (ctnode, false);
31600       *slot = ctnode;
31601     }
31602 
31603   if (dwarf_split_debug_info)
31604     {
31605       int mark;
31606       struct md5_ctx ctx;
31607 
31608       if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31609 	index_rnglists ();
31610 
31611       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
31612       md5_init_ctx (&ctx);
31613       mark = 0;
31614       die_checksum (comp_unit_die (), &ctx, &mark);
31615       unmark_all_dies (comp_unit_die ());
31616       md5_finish_ctx (&ctx, checksum);
31617 
31618       if (dwarf_version < 5)
31619 	{
31620 	  /* Use the first 8 bytes of the checksum as the dwo_id,
31621 	     and add it to both comp-unit DIEs.  */
31622 	  add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31623 	  add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31624 	}
31625 
31626       /* Add the base offset of the ranges table to the skeleton
31627         comp-unit DIE.  */
31628       if (!vec_safe_is_empty (ranges_table))
31629 	{
31630 	  if (dwarf_version >= 5)
31631 	    add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31632 			    ranges_base_label);
31633 	  else
31634 	    add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31635 			    ranges_section_label);
31636 	}
31637 
31638       output_addr_table ();
31639     }
31640 
31641   /* Output the main compilation unit if non-empty or if .debug_macinfo
31642      or .debug_macro will be emitted.  */
31643   output_comp_unit (comp_unit_die (), have_macinfo,
31644 		    dwarf_split_debug_info ? checksum : NULL);
31645 
31646   if (dwarf_split_debug_info && info_section_emitted)
31647     output_skeleton_debug_sections (main_comp_unit_die, checksum);
31648 
31649   /* Output the abbreviation table.  */
31650   if (vec_safe_length (abbrev_die_table) != 1)
31651     {
31652       switch_to_section (debug_abbrev_section);
31653       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31654       output_abbrev_section ();
31655     }
31656 
31657   /* Output location list section if necessary.  */
31658   if (have_location_lists)
31659     {
31660       char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31661       char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31662       /* Output the location lists info.  */
31663       switch_to_section (debug_loc_section);
31664       if (dwarf_version >= 5)
31665 	{
31666 	  ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31667 	  ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31668 	  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31669 	    dw2_asm_output_data (4, 0xffffffff,
31670 				 "Initial length escape value indicating "
31671 				 "64-bit DWARF extension");
31672 	  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31673 			    "Length of Location Lists");
31674 	  ASM_OUTPUT_LABEL (asm_out_file, l1);
31675 	  output_dwarf_version ();
31676 	  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31677 	  dw2_asm_output_data (1, 0, "Segment Size");
31678 	  dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31679 			       "Offset Entry Count");
31680 	}
31681       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31682       if (dwarf_version >= 5 && dwarf_split_debug_info)
31683 	{
31684 	  unsigned int save_loc_list_idx = loc_list_idx;
31685 	  loc_list_idx = 0;
31686 	  output_loclists_offsets (comp_unit_die ());
31687 	  gcc_assert (save_loc_list_idx == loc_list_idx);
31688 	}
31689       output_location_lists (comp_unit_die ());
31690       if (dwarf_version >= 5)
31691 	ASM_OUTPUT_LABEL (asm_out_file, l2);
31692     }
31693 
31694   output_pubtables ();
31695 
31696   /* Output the address range information if a CU (.debug_info section)
31697      was emitted.  We output an empty table even if we had no functions
31698      to put in it.  This because the consumer has no way to tell the
31699      difference between an empty table that we omitted and failure to
31700      generate a table that would have contained data.  */
31701   if (info_section_emitted)
31702     {
31703       switch_to_section (debug_aranges_section);
31704       output_aranges ();
31705     }
31706 
31707   /* Output ranges section if necessary.  */
31708   if (!vec_safe_is_empty (ranges_table))
31709     {
31710       if (dwarf_version >= 5)
31711 	output_rnglists (generation);
31712       else
31713 	output_ranges ();
31714     }
31715 
31716   /* Have to end the macro section.  */
31717   if (have_macinfo)
31718     {
31719       switch_to_section (debug_macinfo_section);
31720       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31721       output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31722 		      : debug_skeleton_line_section_label, false);
31723       dw2_asm_output_data (1, 0, "End compilation unit");
31724     }
31725 
31726   /* Output the source line correspondence table.  We must do this
31727      even if there is no line information.  Otherwise, on an empty
31728      translation unit, we will generate a present, but empty,
31729      .debug_info section.  IRIX 6.5 `nm' will then complain when
31730      examining the file.  This is done late so that any filenames
31731      used by the debug_info section are marked as 'used'.  */
31732   switch_to_section (debug_line_section);
31733   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31734   if (! output_asm_line_debug_info ())
31735     output_line_info (false);
31736 
31737   if (dwarf_split_debug_info && info_section_emitted)
31738     {
31739       switch_to_section (debug_skeleton_line_section);
31740       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31741       output_line_info (true);
31742     }
31743 
31744   /* If we emitted any indirect strings, output the string table too.  */
31745   if (debug_str_hash || skeleton_debug_str_hash)
31746     output_indirect_strings ();
31747   if (debug_line_str_hash)
31748     {
31749       switch_to_section (debug_line_str_section);
31750       const enum dwarf_form form = DW_FORM_line_strp;
31751       debug_line_str_hash->traverse<enum dwarf_form,
31752 				    output_indirect_string> (form);
31753     }
31754 
31755   /* ??? Move lvugid out of dwarf2out_source_line and reset it too?  */
31756   symview_upper_bound = 0;
31757   if (zero_view_p)
31758     bitmap_clear (zero_view_p);
31759 }
31760 
31761 /* Returns a hash value for X (which really is a variable_value_struct).  */
31762 
31763 inline hashval_t
hash(variable_value_struct * x)31764 variable_value_hasher::hash (variable_value_struct *x)
31765 {
31766   return (hashval_t) x->decl_id;
31767 }
31768 
31769 /* Return nonzero if decl_id of variable_value_struct X is the same as
31770    UID of decl Y.  */
31771 
31772 inline bool
equal(variable_value_struct * x,tree y)31773 variable_value_hasher::equal (variable_value_struct *x, tree y)
31774 {
31775   return x->decl_id == DECL_UID (y);
31776 }
31777 
31778 /* Helper function for resolve_variable_value, handle
31779    DW_OP_GNU_variable_value in one location expression.
31780    Return true if exprloc has been changed into loclist.  */
31781 
31782 static bool
resolve_variable_value_in_expr(dw_attr_node * a,dw_loc_descr_ref loc)31783 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31784 {
31785   dw_loc_descr_ref next;
31786   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31787     {
31788       next = loc->dw_loc_next;
31789       if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31790 	  || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31791 	continue;
31792 
31793       tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31794       if (DECL_CONTEXT (decl) != current_function_decl)
31795 	continue;
31796 
31797       dw_die_ref ref = lookup_decl_die (decl);
31798       if (ref)
31799 	{
31800 	  loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31801 	  loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31802 	  loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31803 	  continue;
31804 	}
31805       dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31806       if (l == NULL)
31807 	continue;
31808       if (l->dw_loc_next)
31809 	{
31810 	  if (AT_class (a) != dw_val_class_loc)
31811 	    continue;
31812 	  switch (a->dw_attr)
31813 	    {
31814 	    /* Following attributes allow both exprloc and loclist
31815 	       classes, so we can change them into a loclist.  */
31816 	    case DW_AT_location:
31817 	    case DW_AT_string_length:
31818 	    case DW_AT_return_addr:
31819 	    case DW_AT_data_member_location:
31820 	    case DW_AT_frame_base:
31821 	    case DW_AT_segment:
31822 	    case DW_AT_static_link:
31823 	    case DW_AT_use_location:
31824 	    case DW_AT_vtable_elem_location:
31825 	      if (prev)
31826 		{
31827 		  prev->dw_loc_next = NULL;
31828 		  prepend_loc_descr_to_each (l, AT_loc (a));
31829 		}
31830 	      if (next)
31831 		add_loc_descr_to_each (l, next);
31832 	      a->dw_attr_val.val_class = dw_val_class_loc_list;
31833 	      a->dw_attr_val.val_entry = NULL;
31834 	      a->dw_attr_val.v.val_loc_list = l;
31835 	      have_location_lists = true;
31836 	      return true;
31837 	    /* Following attributes allow both exprloc and reference,
31838 	       so if the whole expression is DW_OP_GNU_variable_value alone
31839 	       we could transform it into reference.  */
31840 	    case DW_AT_byte_size:
31841 	    case DW_AT_bit_size:
31842 	    case DW_AT_lower_bound:
31843 	    case DW_AT_upper_bound:
31844 	    case DW_AT_bit_stride:
31845 	    case DW_AT_count:
31846 	    case DW_AT_allocated:
31847 	    case DW_AT_associated:
31848 	    case DW_AT_byte_stride:
31849 	      if (prev == NULL && next == NULL)
31850 		break;
31851 	      /* FALLTHRU */
31852 	    default:
31853 	      if (dwarf_strict)
31854 		continue;
31855 	      break;
31856 	    }
31857 	  /* Create DW_TAG_variable that we can refer to.  */
31858 	  gen_decl_die (decl, NULL_TREE, NULL,
31859 			lookup_decl_die (current_function_decl));
31860 	  ref = lookup_decl_die (decl);
31861 	  if (ref)
31862 	    {
31863 	      loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31864 	      loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31865 	      loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31866 	    }
31867 	  continue;
31868 	}
31869       if (prev)
31870 	{
31871 	  prev->dw_loc_next = l->expr;
31872 	  add_loc_descr (&prev->dw_loc_next, next);
31873 	  free_loc_descr (loc, NULL);
31874 	  next = prev->dw_loc_next;
31875 	}
31876       else
31877 	{
31878 	  memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31879 	  add_loc_descr (&loc, next);
31880 	  next = loc;
31881 	}
31882       loc = prev;
31883     }
31884   return false;
31885 }
31886 
31887 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree.  */
31888 
31889 static void
resolve_variable_value(dw_die_ref die)31890 resolve_variable_value (dw_die_ref die)
31891 {
31892   dw_attr_node *a;
31893   dw_loc_list_ref loc;
31894   unsigned ix;
31895 
31896   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31897     switch (AT_class (a))
31898       {
31899       case dw_val_class_loc:
31900 	if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31901 	  break;
31902 	/* FALLTHRU */
31903       case dw_val_class_loc_list:
31904 	loc = AT_loc_list (a);
31905 	gcc_assert (loc);
31906 	for (; loc; loc = loc->dw_loc_next)
31907 	  resolve_variable_value_in_expr (a, loc->expr);
31908 	break;
31909       default:
31910 	break;
31911       }
31912 }
31913 
31914 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31915    temporaries in the current function.  */
31916 
31917 static void
resolve_variable_values(void)31918 resolve_variable_values (void)
31919 {
31920   if (!variable_value_hash || !current_function_decl)
31921     return;
31922 
31923   struct variable_value_struct *node
31924     = variable_value_hash->find_with_hash (current_function_decl,
31925 					   DECL_UID (current_function_decl));
31926 
31927   if (node == NULL)
31928     return;
31929 
31930   unsigned int i;
31931   dw_die_ref die;
31932   FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31933     resolve_variable_value (die);
31934 }
31935 
31936 /* Helper function for note_variable_value, handle one location
31937    expression.  */
31938 
31939 static void
note_variable_value_in_expr(dw_die_ref die,dw_loc_descr_ref loc)31940 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31941 {
31942   for (; loc; loc = loc->dw_loc_next)
31943     if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31944 	&& loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31945       {
31946 	tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31947 	dw_die_ref ref = lookup_decl_die (decl);
31948 	if (! ref && (flag_generate_lto || flag_generate_offload))
31949 	  {
31950 	    /* ???  This is somewhat a hack because we do not create DIEs
31951 	       for variables not in BLOCK trees early but when generating
31952 	       early LTO output we need the dw_val_class_decl_ref to be
31953 	       fully resolved.  For fat LTO objects we'd also like to
31954 	       undo this after LTO dwarf output.  */
31955 	    gcc_assert (DECL_CONTEXT (decl));
31956 	    dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31957 	    gcc_assert (ctx != NULL);
31958 	    gen_decl_die (decl, NULL_TREE, NULL, ctx);
31959 	    ref = lookup_decl_die (decl);
31960 	    gcc_assert (ref != NULL);
31961 	  }
31962 	if (ref)
31963 	  {
31964 	    loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31965 	    loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31966 	    loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31967 	    continue;
31968 	  }
31969 	if (VAR_P (decl)
31970 	    && DECL_CONTEXT (decl)
31971 	    && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31972 	    && lookup_decl_die (DECL_CONTEXT (decl)))
31973 	  {
31974 	    if (!variable_value_hash)
31975 	      variable_value_hash
31976 		= hash_table<variable_value_hasher>::create_ggc (10);
31977 
31978 	    tree fndecl = DECL_CONTEXT (decl);
31979 	    struct variable_value_struct *node;
31980 	    struct variable_value_struct **slot
31981 	      = variable_value_hash->find_slot_with_hash (fndecl,
31982 							  DECL_UID (fndecl),
31983 							  INSERT);
31984 	    if (*slot == NULL)
31985 	      {
31986 		node = ggc_cleared_alloc<variable_value_struct> ();
31987 		node->decl_id = DECL_UID (fndecl);
31988 		*slot = node;
31989 	      }
31990 	    else
31991 	      node = *slot;
31992 
31993 	    vec_safe_push (node->dies, die);
31994 	  }
31995       }
31996 }
31997 
31998 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31999    with dw_val_class_decl_ref operand.  */
32000 
32001 static void
note_variable_value(dw_die_ref die)32002 note_variable_value (dw_die_ref die)
32003 {
32004   dw_die_ref c;
32005   dw_attr_node *a;
32006   dw_loc_list_ref loc;
32007   unsigned ix;
32008 
32009   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32010     switch (AT_class (a))
32011       {
32012       case dw_val_class_loc_list:
32013 	loc = AT_loc_list (a);
32014 	gcc_assert (loc);
32015 	if (!loc->noted_variable_value)
32016 	  {
32017 	    loc->noted_variable_value = 1;
32018 	    for (; loc; loc = loc->dw_loc_next)
32019 	      note_variable_value_in_expr (die, loc->expr);
32020 	  }
32021 	break;
32022       case dw_val_class_loc:
32023 	note_variable_value_in_expr (die, AT_loc (a));
32024 	break;
32025       default:
32026 	break;
32027       }
32028 
32029   /* Mark children.  */
32030   FOR_EACH_CHILD (die, c, note_variable_value (c));
32031 }
32032 
32033 /* Perform any cleanups needed after the early debug generation pass
32034    has run.  */
32035 
32036 static void
dwarf2out_early_finish(const char * filename)32037 dwarf2out_early_finish (const char *filename)
32038 {
32039   set_early_dwarf s;
32040   char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32041 
32042   /* PCH might result in DW_AT_producer string being restored from the
32043      header compilation, so always fill it with empty string initially
32044      and overwrite only here.  */
32045   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32046   producer_string = gen_producer_string ();
32047   producer->dw_attr_val.v.val_str->refcount--;
32048   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32049 
32050   /* Add the name for the main input file now.  We delayed this from
32051      dwarf2out_init to avoid complications with PCH.  */
32052   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
32053   add_comp_dir_attribute (comp_unit_die ());
32054 
32055   /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
32056      DW_AT_comp_dir into .debug_line_str section.  */
32057   if (!output_asm_line_debug_info ()
32058       && dwarf_version >= 5
32059       && DWARF5_USE_DEBUG_LINE_STR)
32060     {
32061       for (int i = 0; i < 2; i++)
32062 	{
32063 	  dw_attr_node *a = get_AT (comp_unit_die (),
32064 				    i ? DW_AT_comp_dir : DW_AT_name);
32065 	  if (a == NULL
32066 	      || AT_class (a) != dw_val_class_str
32067 	      || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
32068 	    continue;
32069 
32070 	  if (! debug_line_str_hash)
32071 	    debug_line_str_hash
32072 	      = hash_table<indirect_string_hasher>::create_ggc (10);
32073 
32074 	  struct indirect_string_node *node
32075 	    = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
32076 	  set_indirect_string (node);
32077 	  node->form = DW_FORM_line_strp;
32078 	  a->dw_attr_val.v.val_str->refcount--;
32079 	  a->dw_attr_val.v.val_str = node;
32080 	}
32081     }
32082 
32083   /* With LTO early dwarf was really finished at compile-time, so make
32084      sure to adjust the phase after annotating the LTRANS CU DIE.  */
32085   if (in_lto_p)
32086     {
32087       early_dwarf_finished = true;
32088       if (dump_file)
32089 	{
32090 	  fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32091 	  print_die (comp_unit_die (), dump_file);
32092 	}
32093       return;
32094     }
32095 
32096   /* Walk through the list of incomplete types again, trying once more to
32097      emit full debugging info for them.  */
32098   retry_incomplete_types ();
32099 
32100   gen_scheduled_generic_parms_dies ();
32101   gen_remaining_tmpl_value_param_die_attribute ();
32102 
32103   /* The point here is to flush out the limbo list so that it is empty
32104      and we don't need to stream it for LTO.  */
32105   flush_limbo_die_list ();
32106 
32107   /* Add DW_AT_linkage_name for all deferred DIEs.  */
32108   for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32109     {
32110       tree decl = node->created_for;
32111       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32112 	  /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32113 	     ended up in deferred_asm_name before we knew it was
32114 	     constant and never written to disk.  */
32115 	  && DECL_ASSEMBLER_NAME (decl))
32116 	{
32117 	  add_linkage_attr (node->die, decl);
32118 	  move_linkage_attr (node->die);
32119 	}
32120     }
32121   deferred_asm_name = NULL;
32122 
32123   if (flag_eliminate_unused_debug_types)
32124     prune_unused_types ();
32125 
32126   /* Generate separate COMDAT sections for type DIEs. */
32127   if (use_debug_types)
32128     {
32129       break_out_comdat_types (comp_unit_die ());
32130 
32131       /* Each new type_unit DIE was added to the limbo die list when created.
32132          Since these have all been added to comdat_type_list, clear the
32133          limbo die list.  */
32134       limbo_die_list = NULL;
32135 
32136       /* For each new comdat type unit, copy declarations for incomplete
32137          types to make the new unit self-contained (i.e., no direct
32138          references to the main compile unit).  */
32139       for (comdat_type_node *ctnode = comdat_type_list;
32140 	   ctnode != NULL; ctnode = ctnode->next)
32141         copy_decls_for_unworthy_types (ctnode->root_die);
32142       copy_decls_for_unworthy_types (comp_unit_die ());
32143 
32144       /* In the process of copying declarations from one unit to another,
32145          we may have left some declarations behind that are no longer
32146          referenced.  Prune them.  */
32147       prune_unused_types ();
32148     }
32149 
32150   /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32151      with dw_val_class_decl_ref operand.  */
32152   note_variable_value (comp_unit_die ());
32153   for (limbo_die_node *node = cu_die_list; node; node = node->next)
32154     note_variable_value (node->die);
32155   for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32156        ctnode = ctnode->next)
32157     note_variable_value (ctnode->root_die);
32158   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32159     note_variable_value (node->die);
32160 
32161   /* The AT_pubnames attribute needs to go in all skeleton dies, including
32162      both the main_cu and all skeleton TUs.  Making this call unconditional
32163      would end up either adding a second copy of the AT_pubnames attribute, or
32164      requiring a special case in add_top_level_skeleton_die_attrs.  */
32165   if (!dwarf_split_debug_info)
32166     add_AT_pubnames (comp_unit_die ());
32167 
32168   /* The early debug phase is now finished.  */
32169   early_dwarf_finished = true;
32170   if (dump_file)
32171     {
32172       fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32173       print_die (comp_unit_die (), dump_file);
32174     }
32175 
32176   /* Do not generate DWARF assembler now when not producing LTO bytecode.  */
32177   if ((!flag_generate_lto && !flag_generate_offload)
32178       /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32179 	 copy_lto_debug_sections operation of the simple object support in
32180 	 libiberty is not implemented for them yet.  */
32181       || TARGET_PECOFF || TARGET_COFF)
32182     return;
32183 
32184   /* Now as we are going to output for LTO initialize sections and labels
32185      to the LTO variants.  We don't need a random-seed postfix as other
32186      LTO sections as linking the LTO debug sections into one in a partial
32187      link is fine.  */
32188   init_sections_and_labels (true);
32189 
32190   /* The output below is modeled after dwarf2out_finish with all
32191      location related output removed and some LTO specific changes.
32192      Some refactoring might make both smaller and easier to match up.  */
32193 
32194   /* Traverse the DIE's and add sibling attributes to those DIE's
32195      that have children.  */
32196   add_sibling_attributes (comp_unit_die ());
32197   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32198     add_sibling_attributes (node->die);
32199   for (comdat_type_node *ctnode = comdat_type_list;
32200        ctnode != NULL; ctnode = ctnode->next)
32201     add_sibling_attributes (ctnode->root_die);
32202 
32203   /* AIX Assembler inserts the length, so adjust the reference to match the
32204      offset expected by debuggers.  */
32205   strcpy (dl_section_ref, debug_line_section_label);
32206   if (XCOFF_DEBUGGING_INFO)
32207     strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32208 
32209   if (debug_info_level >= DINFO_LEVEL_TERSE)
32210     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32211 
32212   if (have_macinfo)
32213     add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32214 		   macinfo_section_label);
32215 
32216   save_macinfo_strings ();
32217 
32218   if (dwarf_split_debug_info)
32219     {
32220       unsigned int index = 0;
32221       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32222     }
32223 
32224   /* Output all of the compilation units.  We put the main one last so that
32225      the offsets are available to output_pubnames.  */
32226   for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32227     output_comp_unit (node->die, 0, NULL);
32228 
32229   hash_table<comdat_type_hasher> comdat_type_table (100);
32230   for (comdat_type_node *ctnode = comdat_type_list;
32231        ctnode != NULL; ctnode = ctnode->next)
32232     {
32233       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32234 
32235       /* Don't output duplicate types.  */
32236       if (*slot != HTAB_EMPTY_ENTRY)
32237         continue;
32238 
32239       /* Add a pointer to the line table for the main compilation unit
32240          so that the debugger can make sense of DW_AT_decl_file
32241          attributes.  */
32242       if (debug_info_level >= DINFO_LEVEL_TERSE)
32243         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32244                         (!dwarf_split_debug_info
32245                          ? debug_line_section_label
32246                          : debug_skeleton_line_section_label));
32247 
32248       output_comdat_type_unit (ctnode, true);
32249       *slot = ctnode;
32250     }
32251 
32252   /* Stick a unique symbol to the main debuginfo section.  */
32253   compute_comp_unit_symbol (comp_unit_die ());
32254 
32255   /* Output the main compilation unit.  We always need it if only for
32256      the CU symbol.  */
32257   output_comp_unit (comp_unit_die (), true, NULL);
32258 
32259   /* Output the abbreviation table.  */
32260   if (vec_safe_length (abbrev_die_table) != 1)
32261     {
32262       switch_to_section (debug_abbrev_section);
32263       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32264       output_abbrev_section ();
32265     }
32266 
32267   /* Have to end the macro section.  */
32268   if (have_macinfo)
32269     {
32270       /* We have to save macinfo state if we need to output it again
32271 	 for the FAT part of the object.  */
32272       vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32273       if (flag_fat_lto_objects)
32274 	macinfo_table = macinfo_table->copy ();
32275 
32276       switch_to_section (debug_macinfo_section);
32277       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32278       output_macinfo (debug_line_section_label, true);
32279       dw2_asm_output_data (1, 0, "End compilation unit");
32280 
32281       if (flag_fat_lto_objects)
32282 	{
32283 	  vec_free (macinfo_table);
32284 	  macinfo_table = saved_macinfo_table;
32285 	}
32286     }
32287 
32288   /* Emit a skeleton debug_line section.  */
32289   switch_to_section (debug_line_section);
32290   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32291   output_line_info (true);
32292 
32293   /* If we emitted any indirect strings, output the string table too.  */
32294   if (debug_str_hash || skeleton_debug_str_hash)
32295     output_indirect_strings ();
32296   if (debug_line_str_hash)
32297     {
32298       switch_to_section (debug_line_str_section);
32299       const enum dwarf_form form = DW_FORM_line_strp;
32300       debug_line_str_hash->traverse<enum dwarf_form,
32301 				    output_indirect_string> (form);
32302     }
32303 
32304   /* Switch back to the text section.  */
32305   switch_to_section (text_section);
32306 }
32307 
32308 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32309    within the same process.  For use by toplev::finalize.  */
32310 
32311 void
dwarf2out_c_finalize(void)32312 dwarf2out_c_finalize (void)
32313 {
32314   last_var_location_insn = NULL;
32315   cached_next_real_insn = NULL;
32316   used_rtx_array = NULL;
32317   incomplete_types = NULL;
32318   debug_info_section = NULL;
32319   debug_skeleton_info_section = NULL;
32320   debug_abbrev_section = NULL;
32321   debug_skeleton_abbrev_section = NULL;
32322   debug_aranges_section = NULL;
32323   debug_addr_section = NULL;
32324   debug_macinfo_section = NULL;
32325   debug_line_section = NULL;
32326   debug_skeleton_line_section = NULL;
32327   debug_loc_section = NULL;
32328   debug_pubnames_section = NULL;
32329   debug_pubtypes_section = NULL;
32330   debug_str_section = NULL;
32331   debug_line_str_section = NULL;
32332   debug_str_dwo_section = NULL;
32333   debug_str_offsets_section = NULL;
32334   debug_ranges_section = NULL;
32335   debug_frame_section = NULL;
32336   fde_vec = NULL;
32337   debug_str_hash = NULL;
32338   debug_line_str_hash = NULL;
32339   skeleton_debug_str_hash = NULL;
32340   dw2_string_counter = 0;
32341   have_multiple_function_sections = false;
32342   text_section_used = false;
32343   cold_text_section_used = false;
32344   cold_text_section = NULL;
32345   current_unit_personality = NULL;
32346 
32347   early_dwarf = false;
32348   early_dwarf_finished = false;
32349 
32350   next_die_offset = 0;
32351   single_comp_unit_die = NULL;
32352   comdat_type_list = NULL;
32353   limbo_die_list = NULL;
32354   file_table = NULL;
32355   decl_die_table = NULL;
32356   common_block_die_table = NULL;
32357   decl_loc_table = NULL;
32358   call_arg_locations = NULL;
32359   call_arg_loc_last = NULL;
32360   call_site_count = -1;
32361   tail_call_site_count = -1;
32362   cached_dw_loc_list_table = NULL;
32363   abbrev_die_table = NULL;
32364   delete dwarf_proc_stack_usage_map;
32365   dwarf_proc_stack_usage_map = NULL;
32366   line_info_label_num = 0;
32367   cur_line_info_table = NULL;
32368   text_section_line_info = NULL;
32369   cold_text_section_line_info = NULL;
32370   separate_line_info = NULL;
32371   info_section_emitted = false;
32372   pubname_table = NULL;
32373   pubtype_table = NULL;
32374   macinfo_table = NULL;
32375   ranges_table = NULL;
32376   ranges_by_label = NULL;
32377   rnglist_idx = 0;
32378   have_location_lists = false;
32379   loclabel_num = 0;
32380   poc_label_num = 0;
32381   last_emitted_file = NULL;
32382   label_num = 0;
32383   tmpl_value_parm_die_table = NULL;
32384   generic_type_instances = NULL;
32385   frame_pointer_fb_offset = 0;
32386   frame_pointer_fb_offset_valid = false;
32387   base_types.release ();
32388   XDELETEVEC (producer_string);
32389   producer_string = NULL;
32390 }
32391 
32392 #include "gt-dwarf2out.h"
32393