1/* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2025 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
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along 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 "dwarf2ctf.h"
83#include "dwarf2codeview.h"
84#include "dwarf2asm.h"
85#include "toplev.h"
86#include "md5.h"
87#include "tree-pretty-print.h"
88#include "print-rtl.h"
89#include "debug.h"
90#include "common/common-target.h"
91#include "langhooks.h"
92#include "lra.h"
93#include "dumpfile.h"
94#include "opts.h"
95#include "tree-dfa.h"
96#include "gdb/gdb-index.h"
97#include "rtl-iter.h"
98#include "stringpool.h"
99#include "attribs.h"
100#include "file-prefix-map.h" /* remap_debug_filename() */
101
102static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
103 int, bool);
104static rtx_insn *last_var_location_insn;
105static rtx_insn *cached_next_real_insn;
106static void dwarf2out_decl (tree);
107static bool is_redundant_typedef (const_tree);
108
109#ifndef XCOFF_DEBUGGING_INFO
110#define XCOFF_DEBUGGING_INFO 0
111#endif
112
113#ifndef HAVE_XCOFF_DWARF_EXTRAS
114#define HAVE_XCOFF_DWARF_EXTRAS 0
115#endif
116
117#ifdef VMS_DEBUGGING_INFO
118int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119
120/* Define this macro to be a nonzero value if the directory specifications
121 which are output in the debug info should end with a separator. */
122#define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123/* Define this macro to evaluate to a nonzero value if GCC should refrain
124 from generating indirect strings in DWARF2 debug information, for instance
125 if your target is stuck with an old version of GDB that is unable to
126 process them properly or uses VMS Debug. */
127#define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128#else
129#define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130#define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131#endif
132
133/* ??? Poison these here until it can be done generically. They've been
134 totally replaced in this file; make sure it stays that way. */
135#undef DWARF2_UNWIND_INFO
136#undef DWARF2_FRAME_INFO
137#if (GCC_VERSION >= 3000)
138 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139#endif
140
141/* The size of the target's pointer type. */
142#ifndef PTR_SIZE
143#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144#endif
145
146/* Array of RTXes referenced by the debugging information, which therefore
147 must be kept around forever. */
148static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149
150/* A pointer to the base of a list of incomplete types which might be
151 completed at some later time. incomplete_types_list needs to be a
152 vec<tree, va_gc> *because we want to tell the garbage collector about
153 it. */
154static GTY(()) vec<tree, va_gc> *incomplete_types;
155
156/* Pointers to various DWARF2 sections. */
157static GTY(()) section *debug_info_section;
158static GTY(()) section *debug_skeleton_info_section;
159static GTY(()) section *debug_abbrev_section;
160static GTY(()) section *debug_skeleton_abbrev_section;
161static GTY(()) section *debug_aranges_section;
162static GTY(()) section *debug_addr_section;
163static GTY(()) section *debug_macinfo_section;
164static const char *debug_macinfo_section_name;
165static unsigned macinfo_label_base = 1;
166static GTY(()) section *debug_line_section;
167static GTY(()) section *debug_skeleton_line_section;
168static GTY(()) section *debug_loc_section;
169static GTY(()) section *debug_pubnames_section;
170static GTY(()) section *debug_pubtypes_section;
171static GTY(()) section *debug_str_section;
172static GTY(()) section *debug_line_str_section;
173static GTY(()) section *debug_str_dwo_section;
174static GTY(()) section *debug_str_offsets_section;
175static GTY(()) section *debug_ranges_section;
176static GTY(()) section *debug_ranges_dwo_section;
177static GTY(()) section *debug_frame_section;
178
179/* Maximum size (in bytes) of an artificially generated label. */
180#define MAX_ARTIFICIAL_LABEL_BYTES 40
181
182/* According to the (draft) DWARF 3 specification, the initial length
183 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
184 bytes are 0xffffffff, followed by the length stored in the next 8
185 bytes.
186
187 However, the SGI/MIPS ABI uses an initial length which is equal to
188 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189
190#ifndef DWARF_INITIAL_LENGTH_SIZE
191#define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
192#endif
193
194#ifndef DWARF_INITIAL_LENGTH_SIZE_STR
195#define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
196#endif
197
198/* Round SIZE up to the nearest BOUNDARY. */
199#define DWARF_ROUND(SIZE,BOUNDARY) \
200 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201
202/* CIE identifier. */
203#if HOST_BITS_PER_WIDE_INT >= 64
204#define DWARF_CIE_ID \
205 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
206#else
207#define DWARF_CIE_ID DW_CIE_ID
208#endif
209
210
211/* A vector for a table that contains frame description
212 information for each routine. */
213#define NOT_INDEXED (-1U)
214#define NO_INDEX_ASSIGNED (-2U)
215
216static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217
218struct GTY((for_user)) indirect_string_node {
219 const char *str;
220 unsigned int refcount;
221 enum dwarf_form form;
222 char *label;
223 unsigned int index;
224};
225
226struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227{
228 typedef const char *compare_type;
229
230 static hashval_t hash (indirect_string_node *);
231 static bool equal (indirect_string_node *, const char *);
232};
233
234static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235
236static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237
238/* With split_debug_info, both the comp_dir and dwo_name go in the
239 main object file, rather than the dwo, similar to the force_direct
240 parameter elsewhere but with additional complications:
241
242 1) The string is needed in both the main object file and the dwo.
243 That is, the comp_dir and dwo_name will appear in both places.
244
245 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
246 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247
248 3) GCC chooses the form to use late, depending on the size and
249 reference count.
250
251 Rather than forcing the all debug string handling functions and
252 callers to deal with these complications, simply use a separate,
253 special-cased string table for any attribute that should go in the
254 main object file. This limits the complexity to just the places
255 that need it. */
256
257static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258
259static GTY(()) int dw2_string_counter;
260
261/* True if the compilation unit places functions in more than one section. */
262static GTY(()) bool have_multiple_function_sections = false;
263
264/* The default cold text section. */
265static GTY(()) section *cold_text_section;
266
267/* True if currently in text section. */
268static GTY(()) bool in_text_section_p = false;
269
270/* Last debug-on location in corresponding section. */
271static GTY(()) const char *last_text_label;
272static GTY(()) const char *last_cold_label;
273
274/* Mark debug-on/off locations per section.
275 NULL means the section is not used at all. */
276static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
277static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278
279/* The DIE for C++14 'auto' in a function return type. */
280static GTY(()) dw_die_ref auto_die;
281
282/* The DIE for C++14 'decltype(auto)' in a function return type. */
283static GTY(()) dw_die_ref decltype_auto_die;
284
285/* Forward declarations for functions defined in this file. */
286
287static void output_call_frame_info (int);
288
289/* Personality decl of current unit. Used only when assembler does not support
290 personality CFI. */
291static GTY(()) rtx current_unit_personality;
292
293/* Whether an eh_frame section is required. */
294static GTY(()) bool do_eh_frame = false;
295
296/* .debug_rnglists next index. */
297static unsigned int rnglist_idx;
298
299/* Data and reference forms for relocatable data. */
300#define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
301#define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302
303#ifndef DEBUG_FRAME_SECTION
304#define DEBUG_FRAME_SECTION ".debug_frame"
305#endif
306
307#ifndef FUNC_BEGIN_LABEL
308#define FUNC_BEGIN_LABEL "LFB"
309#endif
310
311#ifndef FUNC_SECOND_SECT_LABEL
312#define FUNC_SECOND_SECT_LABEL "LFSB"
313#endif
314
315#ifndef FUNC_END_LABEL
316#define FUNC_END_LABEL "LFE"
317#endif
318
319#ifndef PROLOGUE_END_LABEL
320#define PROLOGUE_END_LABEL "LPE"
321#endif
322
323#ifndef EPILOGUE_BEGIN_LABEL
324#define EPILOGUE_BEGIN_LABEL "LEB"
325#endif
326
327#ifndef FRAME_BEGIN_LABEL
328#define FRAME_BEGIN_LABEL "Lframe"
329#endif
330#define CIE_AFTER_SIZE_LABEL "LSCIE"
331#define CIE_END_LABEL "LECIE"
332#define FDE_LABEL "LSFDE"
333#define FDE_AFTER_SIZE_LABEL "LASFDE"
334#define FDE_END_LABEL "LEFDE"
335#define LINE_NUMBER_BEGIN_LABEL "LSLT"
336#define LINE_NUMBER_END_LABEL "LELT"
337#define LN_PROLOG_AS_LABEL "LASLTP"
338#define LN_PROLOG_END_LABEL "LELTP"
339#define DIE_LABEL_PREFIX "DW"
340
341/* Match the base name of a file to the base name of a compilation unit. */
342
343static bool
344matches_main_base (const char *path)
345{
346 /* Cache the last query. */
347 static const char *last_path = NULL;
348 static bool last_match = false;
349 if (path != last_path)
350 {
351 const char *base;
352 int length = base_of_path (path, base_out: &base);
353 last_path = path;
354 last_match = (length == main_input_baselength
355 && memcmp (s1: base, main_input_basename, n: length) == 0);
356 }
357 return last_match;
358}
359
360#ifdef DEBUG_DEBUG_STRUCT
361
362static bool
363dump_struct_debug (tree type, enum debug_info_usage usage,
364 enum debug_struct_file criterion, int generic,
365 bool matches, bool result)
366{
367 /* Find the type name. */
368 tree type_decl = TYPE_STUB_DECL (type);
369 tree t = type_decl;
370 const char *name = 0;
371 if (TREE_CODE (t) == TYPE_DECL)
372 t = DECL_NAME (t);
373 if (t)
374 name = IDENTIFIER_POINTER (t);
375
376 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
377 criterion,
378 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
379 matches ? "bas" : "hdr",
380 generic ? "gen" : "ord",
381 usage == DINFO_USAGE_DFN ? ";" :
382 usage == DINFO_USAGE_DIR_USE ? "." : "*",
383 result,
384 (void*) type_decl, name);
385 return result;
386}
387#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
388 dump_struct_debug (type, usage, criterion, generic, matches, result)
389
390#else
391
392#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
393 (result)
394
395#endif
396
397/* Get the number of HOST_WIDE_INTs needed to represent the precision
398 of the number. */
399
400static unsigned int
401get_full_len (const dw_wide_int &op)
402{
403 return CEIL (op.get_precision (), HOST_BITS_PER_WIDE_INT);
404}
405
406static bool
407should_emit_struct_debug (tree type, enum debug_info_usage usage)
408{
409 if (debug_info_level <= DINFO_LEVEL_TERSE)
410 return false;
411
412 enum debug_struct_file criterion;
413 tree type_decl;
414 bool generic = lang_hooks.types.generic_p (type);
415
416 if (generic)
417 criterion = debug_struct_generic[usage];
418 else
419 criterion = debug_struct_ordinary[usage];
420
421 if (criterion == DINFO_STRUCT_FILE_NONE)
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
423 if (criterion == DINFO_STRUCT_FILE_ANY)
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425
426 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
427
428 if (type_decl != NULL)
429 {
430 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
431 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
432
433 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
434 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
435 }
436
437 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
438}
439
440/* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
441 switch to the data section instead, and write out a synthetic start label
442 for collect2 the first time around. */
443
444static void
445switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
446{
447 if (eh_frame_section == 0)
448 {
449 int flags;
450
451 if (EH_TABLES_CAN_BE_READ_ONLY)
452 {
453 int fde_encoding;
454 int per_encoding;
455 int lsda_encoding;
456
457 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
458 /*global=*/0);
459 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
460 /*global=*/1);
461 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
462 /*global=*/0);
463 flags = ((! flag_pic
464 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
465 && (fde_encoding & 0x70) != DW_EH_PE_aligned
466 && (per_encoding & 0x70) != DW_EH_PE_absptr
467 && (per_encoding & 0x70) != DW_EH_PE_aligned
468 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
469 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
470 ? 0 : SECTION_WRITE);
471 }
472 else
473 flags = SECTION_WRITE;
474
475#ifdef EH_FRAME_SECTION_NAME
476 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477#else
478 eh_frame_section = ((flags == SECTION_WRITE)
479 ? data_section : readonly_data_section);
480#endif /* EH_FRAME_SECTION_NAME */
481 }
482
483 switch_to_section (eh_frame_section);
484
485#ifdef EH_FRAME_THROUGH_COLLECT2
486 /* We have no special eh_frame section. Emit special labels to guide
487 collect2. */
488 if (!back)
489 {
490 tree label = get_file_function_name ("F");
491 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
492 targetm.asm_out.globalize_label (asm_out_file,
493 IDENTIFIER_POINTER (label));
494 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
495 }
496#endif
497}
498
499/* Switch [BACK] to the eh or debug frame table section, depending on
500 FOR_EH. */
501
502static void
503switch_to_frame_table_section (int for_eh, bool back)
504{
505 if (for_eh)
506 switch_to_eh_frame_section (back);
507 else
508 {
509 if (!debug_frame_section)
510 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
511 SECTION_DEBUG, NULL);
512 switch_to_section (debug_frame_section);
513 }
514}
515
516/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
517
518enum dw_cfi_oprnd_type
519dw_cfi_oprnd1_desc (dwarf_call_frame_info cfi)
520{
521 switch (cfi)
522 {
523 case DW_CFA_nop:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
527
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
534
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 case DW_CFA_val_expression:
548 return dw_cfi_oprnd_reg_num;
549
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
554
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
557
558 default:
559 {
560 dw_cfi_oprnd_type oprnd_type;
561 if (targetm.dw_cfi_oprnd1_desc (cfi, oprnd_type))
562 return oprnd_type;
563 else
564 gcc_unreachable ();
565 }
566 }
567}
568
569/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
570
571enum dw_cfi_oprnd_type
572dw_cfi_oprnd2_desc (dwarf_call_frame_info cfi)
573{
574 switch (cfi)
575 {
576 case DW_CFA_def_cfa:
577 case DW_CFA_def_cfa_sf:
578 case DW_CFA_offset:
579 case DW_CFA_offset_extended_sf:
580 case DW_CFA_offset_extended:
581 return dw_cfi_oprnd_offset;
582
583 case DW_CFA_register:
584 return dw_cfi_oprnd_reg_num;
585
586 case DW_CFA_expression:
587 case DW_CFA_val_expression:
588 return dw_cfi_oprnd_loc;
589
590 case DW_CFA_def_cfa_expression:
591 return dw_cfi_oprnd_cfa_loc;
592
593 default:
594 return dw_cfi_oprnd_unused;
595 }
596}
597
598/* Output one FDE. */
599
600static void
601output_fde (dw_fde_ref fde, bool for_eh, bool second,
602 char *section_start_label, int fde_encoding, char *augmentation,
603 bool any_lsda_needed, int lsda_encoding)
604{
605 const char *begin, *end;
606 static unsigned int j;
607 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
608
609 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
610 /* empty */ 0);
611 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
612 for_eh + j);
613 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
614 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
615 if (!XCOFF_DEBUGGING_INFO || for_eh)
616 {
617 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
618 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
619 " indicating 64-bit DWARF extension");
620 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
621 "FDE Length");
622 }
623 ASM_OUTPUT_LABEL (asm_out_file, l1);
624
625 if (for_eh)
626 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
627 else
628 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
629 debug_frame_section, "FDE CIE offset");
630
631 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
632 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
633
634 if (for_eh)
635 {
636 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
637 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
638 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
639 "FDE initial location");
640 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
641 end, begin, "FDE address range");
642 }
643 else
644 {
645 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
647 }
648
649 if (augmentation[0])
650 {
651 if (any_lsda_needed)
652 {
653 int size = size_of_encoded_value (lsda_encoding);
654
655 if (lsda_encoding == DW_EH_PE_aligned)
656 {
657 int offset = ( 4 /* Length */
658 + 4 /* CIE offset */
659 + 2 * size_of_encoded_value (fde_encoding)
660 + 1 /* Augmentation size */ );
661 int pad = -offset & (PTR_SIZE - 1);
662
663 size += pad;
664 gcc_assert (size_of_uleb128 (size) == 1);
665 }
666
667 dw2_asm_output_data_uleb128 (size, "Augmentation size");
668
669 if (fde->uses_eh_lsda)
670 {
671 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
672 fde->funcdef_number);
673 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
674 gen_rtx_SYMBOL_REF (Pmode, l1),
675 false,
676 "Language Specific Data Area");
677 }
678 else
679 {
680 if (lsda_encoding == DW_EH_PE_aligned)
681 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
682 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
683 "Language Specific Data Area (none)");
684 }
685 }
686 else
687 dw2_asm_output_data_uleb128 (0, "Augmentation size");
688 }
689
690 /* Loop through the Call Frame Instructions associated with this FDE. */
691 fde->dw_fde_current_label = begin;
692 {
693 size_t from, until, i;
694
695 from = 0;
696 until = vec_safe_length (v: fde->dw_fde_cfi);
697
698 if (fde->dw_fde_second_begin == NULL)
699 ;
700 else if (!second)
701 until = fde->dw_fde_switch_cfi_index;
702 else
703 from = fde->dw_fde_switch_cfi_index;
704
705 for (i = from; i < until; i++)
706 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
707 }
708
709 /* If we are to emit a ref/link from function bodies to their frame tables,
710 do it now. This is typically performed to make sure that tables
711 associated with functions are dragged with them and not discarded in
712 garbage collecting links. We need to do this on a per function basis to
713 cope with -ffunction-sections. */
714
715#ifdef ASM_OUTPUT_DWARF_TABLE_REF
716 /* Switch to the function section, emit the ref to the tables, and
717 switch *back* into the table section. */
718 switch_to_section (function_section (fde->decl));
719 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
720 switch_to_frame_table_section (for_eh, true);
721#endif
722
723 /* Pad the FDE out to an address sized boundary. */
724 ASM_OUTPUT_ALIGN (asm_out_file,
725 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
726 ASM_OUTPUT_LABEL (asm_out_file, l2);
727
728 j += 2;
729}
730
731/* Return true if frame description entry FDE is needed for EH. */
732
733static bool
734fde_needed_for_eh_p (dw_fde_ref fde)
735{
736 if (flag_asynchronous_unwind_tables)
737 return true;
738
739 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
740 return true;
741
742 if (fde->uses_eh_lsda)
743 return true;
744
745 /* If exceptions are enabled, we have collected nothrow info. */
746 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
747 return false;
748
749 return true;
750}
751
752/* Output the call frame information used to record information
753 that relates to calculating the frame pointer, and records the
754 location of saved registers. */
755
756static void
757output_call_frame_info (int for_eh)
758{
759 unsigned int i;
760 dw_fde_ref fde;
761 dw_cfi_ref cfi;
762 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
763 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
764 bool any_lsda_needed = false;
765 char augmentation[6];
766 int augmentation_size;
767 int fde_encoding = DW_EH_PE_absptr;
768 int per_encoding = DW_EH_PE_absptr;
769 int lsda_encoding = DW_EH_PE_absptr;
770 int return_reg;
771 rtx personality = NULL;
772 int dw_cie_version;
773
774 /* Don't emit a CIE if there won't be any FDEs. */
775 if (!fde_vec)
776 return;
777
778 /* Nothing to do if the assembler's doing it all. */
779 if (dwarf2out_do_cfi_asm ())
780 return;
781
782 /* If we don't have any functions we'll want to unwind out of, don't emit
783 any EH unwind information. If we make FDEs linkonce, we may have to
784 emit an empty label for an FDE that wouldn't otherwise be emitted. We
785 want to avoid having an FDE kept around when the function it refers to
786 is discarded. Example where this matters: a primary function template
787 in C++ requires EH information, an explicit specialization doesn't. */
788 if (for_eh)
789 {
790 bool any_eh_needed = false;
791
792 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
793 {
794 if (fde->uses_eh_lsda)
795 any_eh_needed = any_lsda_needed = true;
796 else if (fde_needed_for_eh_p (fde))
797 any_eh_needed = true;
798 else if (TARGET_USES_WEAK_UNWIND_INFO)
799 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
800 }
801
802 if (!any_eh_needed)
803 return;
804 }
805
806 /* We're going to be generating comments, so turn on app. */
807 if (flag_debug_asm)
808 app_enable ();
809
810 /* Switch to the proper frame section, first time. */
811 switch_to_frame_table_section (for_eh, back: false);
812
813 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
814 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
815
816 /* Output the CIE. */
817 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
818 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
819 if (!XCOFF_DEBUGGING_INFO || for_eh)
820 {
821 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
822 dw2_asm_output_data (4, 0xffffffff,
823 "Initial length escape value indicating 64-bit DWARF extension");
824 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
825 "Length of Common Information Entry");
826 }
827 ASM_OUTPUT_LABEL (asm_out_file, l1);
828
829 /* Now that the CIE pointer is PC-relative for EH,
830 use 0 to identify the CIE. */
831 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
832 (for_eh ? 0 : DWARF_CIE_ID),
833 "CIE Identifier Tag");
834
835 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
836 use CIE version 1, unless that would produce incorrect results
837 due to overflowing the return register column. */
838 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
839 dw_cie_version = 1;
840 if (return_reg >= 256 || dwarf_version > 2)
841 dw_cie_version = 3;
842 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
843
844 augmentation[0] = 0;
845 augmentation_size = 0;
846
847 personality = current_unit_personality;
848 if (for_eh)
849 {
850 char *p;
851
852 /* Augmentation:
853 z Indicates that a uleb128 is present to size the
854 augmentation section.
855 L Indicates the encoding (and thus presence) of
856 an LSDA pointer in the FDE augmentation.
857 R Indicates a non-default pointer encoding for
858 FDE code pointers.
859 P Indicates the presence of an encoding + language
860 personality routine in the CIE augmentation. */
861
862 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
863 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
864 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
865
866 p = augmentation + 1;
867 if (personality)
868 {
869 *p++ = 'P';
870 augmentation_size += 1 + size_of_encoded_value (per_encoding);
871 assemble_external_libcall (personality);
872 }
873 if (any_lsda_needed)
874 {
875 *p++ = 'L';
876 augmentation_size += 1;
877 }
878 if (fde_encoding != DW_EH_PE_absptr)
879 {
880 *p++ = 'R';
881 augmentation_size += 1;
882 }
883 if (p > augmentation + 1)
884 {
885 augmentation[0] = 'z';
886 *p = '\0';
887 }
888
889 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
890 if (personality && per_encoding == DW_EH_PE_aligned)
891 {
892 int offset = ( 4 /* Length */
893 + 4 /* CIE Id */
894 + 1 /* CIE version */
895 + strlen (s: augmentation) + 1 /* Augmentation */
896 + size_of_uleb128 (1) /* Code alignment */
897 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
898 + 1 /* RA column */
899 + 1 /* Augmentation size */
900 + 1 /* Personality encoding */ );
901 int pad = -offset & (PTR_SIZE - 1);
902
903 augmentation_size += pad;
904
905 /* Augmentations should be small, so there's scarce need to
906 iterate for a solution. Die if we exceed one uleb128 byte. */
907 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
908 }
909 }
910
911 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
912 if (dw_cie_version >= 4)
913 {
914 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
915 dw2_asm_output_data (1, 0, "CIE Segment Size");
916 }
917 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
918 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
919 "CIE Data Alignment Factor");
920
921 if (dw_cie_version == 1)
922 dw2_asm_output_data (1, return_reg, "CIE RA Column");
923 else
924 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
925
926 if (augmentation[0])
927 {
928 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
929 if (personality)
930 {
931 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
932 eh_data_format_name (per_encoding));
933 dw2_asm_output_encoded_addr_rtx (per_encoding,
934 personality,
935 true, NULL);
936 }
937
938 if (any_lsda_needed)
939 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
940 eh_data_format_name (lsda_encoding));
941
942 if (fde_encoding != DW_EH_PE_absptr)
943 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
944 eh_data_format_name (fde_encoding));
945 }
946
947 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
948 output_cfi (cfi, NULL, for_eh);
949
950 /* Pad the CIE out to an address sized boundary. */
951 ASM_OUTPUT_ALIGN (asm_out_file,
952 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
953 ASM_OUTPUT_LABEL (asm_out_file, l2);
954
955 /* Loop through all of the FDE's. */
956 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
957 {
958 unsigned int k;
959
960 /* Don't emit EH unwind info for leaf functions that don't need it. */
961 if (for_eh && !fde_needed_for_eh_p (fde))
962 continue;
963
964 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
965 output_fde (fde, for_eh, second: k, section_start_label, fde_encoding,
966 augmentation, any_lsda_needed, lsda_encoding);
967 }
968
969 if (for_eh && targetm.terminate_dw2_eh_frame_info)
970 dw2_asm_output_data (4, 0, "End of Table");
971
972 /* Turn off app to make assembly quicker. */
973 if (flag_debug_asm)
974 app_disable ();
975}
976
977/* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
978
979static void
980dwarf2out_do_cfi_startproc (bool second)
981{
982 int enc;
983 rtx ref;
984
985 fprintf (stream: asm_out_file, format: "\t.cfi_startproc\n");
986
987 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
988
989 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
990 eh unwinders. */
991 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
992 return;
993
994 rtx personality = get_personality_function (current_function_decl);
995
996 if (personality)
997 {
998 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
999 ref = personality;
1000
1001 /* ??? The GAS support isn't entirely consistent. We have to
1002 handle indirect support ourselves, but PC-relative is done
1003 in the assembler. Further, the assembler can't handle any
1004 of the weirder relocation types. */
1005 if (enc & DW_EH_PE_indirect)
1006 {
1007 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1008 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1009 else
1010 ref = dw2_force_const_mem (ref, true);
1011 }
1012
1013 fprintf (stream: asm_out_file, format: "\t.cfi_personality %#x,", enc);
1014 output_addr_const (asm_out_file, ref);
1015 fputc (c: '\n', stream: asm_out_file);
1016 }
1017
1018 if (crtl->uses_eh_lsda)
1019 {
1020 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1021
1022 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1023 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1024 current_function_funcdef_no);
1025 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1026 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1027
1028 if (enc & DW_EH_PE_indirect)
1029 {
1030 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1031 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1032 else
1033 ref = dw2_force_const_mem (ref, true);
1034 }
1035
1036 fprintf (stream: asm_out_file, format: "\t.cfi_lsda %#x,", enc);
1037 output_addr_const (asm_out_file, ref);
1038 fputc (c: '\n', stream: asm_out_file);
1039 }
1040}
1041
1042/* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1043 this allocation may be done before pass_final. */
1044
1045dw_fde_ref
1046dwarf2out_alloc_current_fde (void)
1047{
1048 dw_fde_ref fde;
1049
1050 fde = ggc_cleared_alloc<dw_fde_node> ();
1051 fde->decl = current_function_decl;
1052 fde->funcdef_number = current_function_funcdef_no;
1053 fde->fde_index = vec_safe_length (v: fde_vec);
1054 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1055 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1056 fde->nothrow = crtl->nothrow;
1057 fde->drap_reg = INVALID_REGNUM;
1058 fde->vdrap_reg = INVALID_REGNUM;
1059
1060 /* Record the FDE associated with this function. */
1061 cfun->fde = fde;
1062 vec_safe_push (v&: fde_vec, obj: fde);
1063
1064 return fde;
1065}
1066
1067/* Output a marker (i.e. a label) for the beginning of a function, before
1068 the prologue. */
1069
1070void
1071dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1072 unsigned int column ATTRIBUTE_UNUSED,
1073 const char *file ATTRIBUTE_UNUSED)
1074{
1075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1076 char * dup_label;
1077 dw_fde_ref fde;
1078 section *fnsec;
1079 bool do_frame;
1080
1081 current_function_func_begin_label = NULL;
1082
1083 do_frame = dwarf2out_do_frame ();
1084
1085 /* ??? current_function_func_begin_label is also used by except.cc for
1086 call-site information. We must emit this label if it might be used. */
1087 if (!do_frame
1088 && (!flag_exceptions
1089 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1090 return;
1091
1092 fnsec = function_section (current_function_decl);
1093 switch_to_section (fnsec);
1094 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1095 current_function_funcdef_no);
1096 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1097 current_function_funcdef_no);
1098 dup_label = xstrdup (label);
1099 current_function_func_begin_label = dup_label;
1100
1101 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1102 if (!do_frame)
1103 return;
1104
1105 /* Unlike the debug version, the EH version of frame unwind info is a per-
1106 function setting so we need to record whether we need it for the unit. */
1107 do_eh_frame |= dwarf2out_do_eh_frame ();
1108
1109 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1110 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1111 would include pass_dwarf2_frame. If we've not created the FDE yet,
1112 do so now. */
1113 fde = cfun->fde;
1114 if (fde == NULL)
1115 fde = dwarf2out_alloc_current_fde ();
1116
1117 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1118 fde->dw_fde_begin = dup_label;
1119 fde->dw_fde_current_label = dup_label;
1120 fde->in_std_section = (fnsec == text_section
1121 || (cold_text_section && fnsec == cold_text_section));
1122 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1123 in_text_section_p = fnsec == text_section;
1124
1125 /* We only want to output line number information for the genuine dwarf2
1126 prologue case, not the eh frame case. */
1127#ifdef DWARF2_DEBUGGING_INFO
1128 if (file)
1129 dwarf2out_source_line (line, column, file, 0, true);
1130#endif
1131
1132 if (dwarf2out_do_cfi_asm ())
1133 dwarf2out_do_cfi_startproc (second: false);
1134 else
1135 {
1136 rtx personality = get_personality_function (current_function_decl);
1137 if (!current_unit_personality)
1138 current_unit_personality = personality;
1139
1140 /* We cannot keep a current personality per function as without CFI
1141 asm, at the point where we emit the CFI data, there is no current
1142 function anymore. */
1143 if (personality && current_unit_personality != personality)
1144 sorry ("multiple EH personalities are supported only with assemblers "
1145 "supporting %<.cfi_personality%> directive");
1146 }
1147}
1148
1149/* Output a marker (i.e. a label) for the end of the generated code
1150 for a function prologue. This gets called *after* the prologue code has
1151 been generated. */
1152
1153void
1154dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1155 const char *file ATTRIBUTE_UNUSED)
1156{
1157 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158
1159 /* Output a label to mark the endpoint of the code generated for this
1160 function. */
1161 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1162 current_function_funcdef_no);
1163 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1164 current_function_funcdef_no);
1165 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1166}
1167
1168/* Output a marker (i.e. a label) for the beginning of the generated code
1169 for a function epilogue. This gets called *before* the prologue code has
1170 been generated. */
1171
1172void
1173dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1174 const char *file ATTRIBUTE_UNUSED)
1175{
1176 dw_fde_ref fde = cfun->fde;
1177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1178
1179 if (fde->dw_fde_vms_begin_epilogue)
1180 return;
1181
1182 /* Output a label to mark the endpoint of the code generated for this
1183 function. */
1184 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1185 current_function_funcdef_no);
1186 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1187 current_function_funcdef_no);
1188 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1189}
1190
1191/* Mark the ranges of non-debug subsections in the std text sections. */
1192
1193static void
1194mark_ignored_debug_section (dw_fde_ref fde, bool second)
1195{
1196 bool std_section;
1197 const char *begin_label, *end_label;
1198 const char **last_end_label;
1199 vec<const char *, va_gc> **switch_ranges;
1200
1201 if (second)
1202 {
1203 std_section = fde->second_in_std_section;
1204 begin_label = fde->dw_fde_second_begin;
1205 end_label = fde->dw_fde_second_end;
1206 }
1207 else
1208 {
1209 std_section = fde->in_std_section;
1210 begin_label = fde->dw_fde_begin;
1211 end_label = fde->dw_fde_end;
1212 }
1213
1214 if (!std_section)
1215 return;
1216
1217 if (in_text_section_p)
1218 {
1219 last_end_label = &last_text_label;
1220 switch_ranges = &switch_text_ranges;
1221 }
1222 else
1223 {
1224 last_end_label = &last_cold_label;
1225 switch_ranges = &switch_cold_ranges;
1226 }
1227
1228 if (fde->ignored_debug)
1229 {
1230 if (*switch_ranges && !(vec_safe_length (v: *switch_ranges) & 1))
1231 vec_safe_push (v&: *switch_ranges, obj: *last_end_label);
1232 }
1233 else
1234 {
1235 *last_end_label = end_label;
1236
1237 if (!*switch_ranges)
1238 vec_alloc (v&: *switch_ranges, nelems: 16);
1239 else if (vec_safe_length (v: *switch_ranges) & 1)
1240 vec_safe_push (v&: *switch_ranges, obj: begin_label);
1241 }
1242}
1243
1244/* Output a marker (i.e. a label) for the absolute end of the generated code
1245 for a function definition. This gets called *after* the epilogue code has
1246 been generated. */
1247
1248void
1249dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1250 const char *file ATTRIBUTE_UNUSED)
1251{
1252 dw_fde_ref fde;
1253 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1254
1255 last_var_location_insn = NULL;
1256 cached_next_real_insn = NULL;
1257
1258 if (dwarf2out_do_cfi_asm ())
1259 fprintf (stream: asm_out_file, format: "\t.cfi_endproc\n");
1260
1261#ifdef CODEVIEW_DEBUGGING_INFO
1262 if (codeview_debuginfo_p ())
1263 codeview_end_epilogue ();
1264#endif
1265
1266 /* Output a label to mark the endpoint of the code generated for this
1267 function. */
1268 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1269 current_function_funcdef_no);
1270 ASM_OUTPUT_LABEL (asm_out_file, label);
1271 fde = cfun->fde;
1272 gcc_assert (fde != NULL);
1273 if (fde->dw_fde_second_begin == NULL)
1274 fde->dw_fde_end = xstrdup (label);
1275
1276 mark_ignored_debug_section (fde, second: fde->dw_fde_second_begin != NULL);
1277}
1278
1279void
1280dwarf2out_frame_finish (void)
1281{
1282 /* Output call frame information. */
1283 if (targetm.debug_unwind_info () == UI_DWARF2)
1284 output_call_frame_info (for_eh: 0);
1285
1286 /* Output another copy for the unwinder. */
1287 if (do_eh_frame)
1288 output_call_frame_info (for_eh: 1);
1289}
1290
1291static void var_location_switch_text_section (void);
1292static void set_cur_line_info_table (section *);
1293
1294void
1295dwarf2out_switch_text_section (void)
1296{
1297 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1298 section *sect;
1299 dw_fde_ref fde = cfun->fde;
1300
1301 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1302
1303 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1304 current_function_funcdef_no);
1305
1306 fde->dw_fde_second_begin = ggc_strdup (label);
1307 if (!in_cold_section_p)
1308 {
1309 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1310 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1311 }
1312 else
1313 {
1314 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1315 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1316 }
1317 have_multiple_function_sections = true;
1318
1319#ifdef CODEVIEW_DEBUGGING_INFO
1320 if (codeview_debuginfo_p ())
1321 codeview_switch_text_section ();
1322#endif
1323
1324 if (dwarf2out_do_cfi_asm ())
1325 fprintf (stream: asm_out_file, format: "\t.cfi_endproc\n");
1326
1327 mark_ignored_debug_section (fde, second: false);
1328
1329 /* Now do the real section switch. */
1330 sect = current_function_section ();
1331 switch_to_section (sect);
1332
1333 fde->second_in_std_section
1334 = (sect == text_section
1335 || (cold_text_section && sect == cold_text_section));
1336 in_text_section_p = sect == text_section;
1337
1338 if (dwarf2out_do_cfi_asm ())
1339 dwarf2out_do_cfi_startproc (second: true);
1340
1341 var_location_switch_text_section ();
1342
1343 if (cold_text_section != NULL)
1344 set_cur_line_info_table (sect);
1345}
1346
1347/* And now, the subset of the debugging information support code necessary
1348 for emitting location expressions. */
1349
1350/* Describe an entry into the .debug_addr section. */
1351
1352enum ate_kind {
1353 ate_kind_rtx,
1354 ate_kind_rtx_dtprel,
1355 ate_kind_label
1356};
1357
1358struct GTY((for_user)) addr_table_entry {
1359 enum ate_kind kind;
1360 unsigned int refcount;
1361 unsigned int index;
1362 union addr_table_entry_struct_union
1363 {
1364 rtx GTY ((tag ("0"))) rtl;
1365 char * GTY ((tag ("1"))) label;
1366 }
1367 GTY ((desc ("%1.kind"))) addr;
1368};
1369
1370static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1371static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1372
1373/* Convert a DWARF stack opcode into its string name. */
1374
1375static const char *
1376dwarf_stack_op_name (unsigned int op)
1377{
1378 const char *name = get_DW_OP_name (op);
1379
1380 if (name != NULL)
1381 return name;
1382
1383 return "OP_<unknown>";
1384}
1385
1386/* Return TRUE iff we're to output location view lists as a separate
1387 attribute next to the location lists, as an extension compatible
1388 with DWARF 2 and above. */
1389
1390static inline bool
1391dwarf2out_locviews_in_attribute ()
1392{
1393 return debug_variable_location_views == 1;
1394}
1395
1396/* Return TRUE iff we're to output location view lists as part of the
1397 location lists, as proposed for standardization after DWARF 5. */
1398
1399static inline bool
1400dwarf2out_locviews_in_loclist ()
1401{
1402#ifndef DW_LLE_view_pair
1403 return false;
1404#else
1405 return debug_variable_location_views == -1;
1406#endif
1407}
1408
1409/* Return a pointer to a newly allocated location description. Location
1410 descriptions are simple expression terms that can be strung
1411 together to form more complicated location (address) descriptions. */
1412
1413static inline dw_loc_descr_ref
1414new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1415 unsigned HOST_WIDE_INT oprnd2)
1416{
1417 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1418
1419 descr->dw_loc_opc = op;
1420 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1421 descr->dw_loc_oprnd1.val_entry = NULL;
1422 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1423 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1424 descr->dw_loc_oprnd2.val_entry = NULL;
1425 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1426
1427 return descr;
1428}
1429
1430/* Add a location description term to a location description expression. */
1431
1432static inline void
1433add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1434{
1435 dw_loc_descr_ref *d;
1436
1437 /* Find the end of the chain. */
1438 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1439 ;
1440
1441 *d = descr;
1442}
1443
1444/* Compare two location operands for exact equality. */
1445
1446static bool
1447dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1448{
1449 if (a->val_class != b->val_class)
1450 return false;
1451 switch (a->val_class)
1452 {
1453 case dw_val_class_none:
1454 return true;
1455 case dw_val_class_addr:
1456 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1457
1458 case dw_val_class_offset:
1459 case dw_val_class_unsigned_const:
1460 case dw_val_class_const:
1461 case dw_val_class_unsigned_const_implicit:
1462 case dw_val_class_const_implicit:
1463 case dw_val_class_range_list:
1464 /* These are all HOST_WIDE_INT, signed or unsigned. */
1465 return a->v.val_unsigned == b->v.val_unsigned;
1466
1467 case dw_val_class_loc:
1468 return a->v.val_loc == b->v.val_loc;
1469 case dw_val_class_loc_list:
1470 return a->v.val_loc_list == b->v.val_loc_list;
1471 case dw_val_class_view_list:
1472 return a->v.val_view_list == b->v.val_view_list;
1473 case dw_val_class_die_ref:
1474 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1475 case dw_val_class_fde_ref:
1476 return a->v.val_fde_index == b->v.val_fde_index;
1477 case dw_val_class_symview:
1478 return strcmp (s1: a->v.val_symbolic_view, s2: b->v.val_symbolic_view) == 0;
1479 case dw_val_class_lbl_id:
1480 case dw_val_class_lineptr:
1481 case dw_val_class_macptr:
1482 case dw_val_class_loclistsptr:
1483 case dw_val_class_high_pc:
1484 return strcmp (s1: a->v.val_lbl_id, s2: b->v.val_lbl_id) == 0;
1485 case dw_val_class_str:
1486 return a->v.val_str == b->v.val_str;
1487 case dw_val_class_flag:
1488 return a->v.val_flag == b->v.val_flag;
1489 case dw_val_class_file:
1490 case dw_val_class_file_implicit:
1491 return a->v.val_file == b->v.val_file;
1492 case dw_val_class_decl_ref:
1493 return a->v.val_decl_ref == b->v.val_decl_ref;
1494
1495 case dw_val_class_const_double:
1496 return (a->v.val_double.high == b->v.val_double.high
1497 && a->v.val_double.low == b->v.val_double.low);
1498
1499 case dw_val_class_wide_int:
1500 return *a->v.val_wide == *b->v.val_wide;
1501
1502 case dw_val_class_vec:
1503 {
1504 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1505 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1506
1507 return (a_len == b_len
1508 && !memcmp (s1: a->v.val_vec.array, s2: b->v.val_vec.array, n: a_len));
1509 }
1510
1511 case dw_val_class_data8:
1512 return memcmp (s1: a->v.val_data8, s2: b->v.val_data8, n: 8) == 0;
1513
1514 case dw_val_class_vms_delta:
1515 return (!strcmp (s1: a->v.val_vms_delta.lbl1, s2: b->v.val_vms_delta.lbl1)
1516 && !strcmp (s1: a->v.val_vms_delta.lbl2, s2: b->v.val_vms_delta.lbl2));
1517
1518 case dw_val_class_discr_value:
1519 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1520 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1521 case dw_val_class_discr_list:
1522 /* It makes no sense comparing two discriminant value lists. */
1523 return false;
1524 }
1525 gcc_unreachable ();
1526}
1527
1528/* Compare two location atoms for exact equality. */
1529
1530static bool
1531loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1532{
1533 if (a->dw_loc_opc != b->dw_loc_opc)
1534 return false;
1535
1536 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1537 address size, but since we always allocate cleared storage it
1538 should be zero for other types of locations. */
1539 if (a->dw_loc_dtprel != b->dw_loc_dtprel)
1540 return false;
1541
1542 return (dw_val_equal_p (a: &a->dw_loc_oprnd1, b: &b->dw_loc_oprnd1)
1543 && dw_val_equal_p (a: &a->dw_loc_oprnd2, b: &b->dw_loc_oprnd2));
1544}
1545
1546/* Compare two complete location expressions for exact equality. */
1547
1548bool
1549loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1550{
1551 while (1)
1552 {
1553 if (a == b)
1554 return true;
1555 if (a == NULL || b == NULL)
1556 return false;
1557 if (!loc_descr_equal_p_1 (a, b))
1558 return false;
1559
1560 a = a->dw_loc_next;
1561 b = b->dw_loc_next;
1562 }
1563}
1564
1565
1566/* Add a constant POLY_OFFSET to a location expression. */
1567
1568static void
1569loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1570{
1571 dw_loc_descr_ref loc;
1572 HOST_WIDE_INT *p;
1573
1574 gcc_assert (*list_head != NULL);
1575
1576 if (known_eq (poly_offset, 0))
1577 return;
1578
1579 /* Find the end of the chain. */
1580 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1581 ;
1582
1583 HOST_WIDE_INT offset;
1584 if (!poly_offset.is_constant (const_value: &offset))
1585 {
1586 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1587 add_loc_descr (list_head: &loc->dw_loc_next, descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
1588 return;
1589 }
1590
1591 p = NULL;
1592 if (loc->dw_loc_opc == DW_OP_fbreg
1593 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1594 p = &loc->dw_loc_oprnd1.v.val_int;
1595 else if (loc->dw_loc_opc == DW_OP_bregx)
1596 p = &loc->dw_loc_oprnd2.v.val_int;
1597
1598 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1599 offset. Don't optimize if an signed integer overflow would happen. */
1600 if (p != NULL
1601 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1602 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1603 *p += offset;
1604
1605 else if (offset > 0)
1606 loc->dw_loc_next = new_loc_descr (op: DW_OP_plus_uconst, oprnd1: offset, oprnd2: 0);
1607
1608 else
1609 {
1610 loc->dw_loc_next
1611 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1612 add_loc_descr (list_head: &loc->dw_loc_next, descr: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
1613 }
1614}
1615
1616/* Return a pointer to a newly allocated location description for
1617 REG and OFFSET. */
1618
1619static inline dw_loc_descr_ref
1620new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1621{
1622 HOST_WIDE_INT const_offset;
1623 if (offset.is_constant (const_value: &const_offset))
1624 {
1625 if (reg <= 31)
1626 return new_loc_descr (op: (enum dwarf_location_atom) (DW_OP_breg0 + reg),
1627 oprnd1: const_offset, oprnd2: 0);
1628 else
1629 return new_loc_descr (op: DW_OP_bregx, oprnd1: reg, oprnd2: const_offset);
1630 }
1631 else
1632 {
1633 dw_loc_descr_ref ret = new_reg_loc_descr (reg, offset: 0);
1634 loc_descr_plus_const (list_head: &ret, poly_offset: offset);
1635 return ret;
1636 }
1637}
1638
1639/* Add a constant OFFSET to a location list. */
1640
1641static void
1642loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1643{
1644 dw_loc_list_ref d;
1645 for (d = list_head; d != NULL; d = d->dw_loc_next)
1646 loc_descr_plus_const (list_head: &d->expr, poly_offset: offset);
1647}
1648
1649#define DWARF_REF_SIZE \
1650 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1651
1652/* The number of bits that can be encoded by largest DW_FORM_dataN.
1653 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1654 DW_FORM_data16 with 128 bits. */
1655#define DWARF_LARGEST_DATA_FORM_BITS \
1656 (dwarf_version >= 5 ? 128 : 64)
1657
1658/* Utility inline function for construction of ops that were GNU extension
1659 before DWARF 5. */
1660static inline enum dwarf_location_atom
1661dwarf_OP (enum dwarf_location_atom op)
1662{
1663 switch (op)
1664 {
1665 case DW_OP_implicit_pointer:
1666 if (dwarf_version < 5)
1667 return DW_OP_GNU_implicit_pointer;
1668 break;
1669
1670 case DW_OP_entry_value:
1671 if (dwarf_version < 5)
1672 return DW_OP_GNU_entry_value;
1673 break;
1674
1675 case DW_OP_const_type:
1676 if (dwarf_version < 5)
1677 return DW_OP_GNU_const_type;
1678 break;
1679
1680 case DW_OP_regval_type:
1681 if (dwarf_version < 5)
1682 return DW_OP_GNU_regval_type;
1683 break;
1684
1685 case DW_OP_deref_type:
1686 if (dwarf_version < 5)
1687 return DW_OP_GNU_deref_type;
1688 break;
1689
1690 case DW_OP_convert:
1691 if (dwarf_version < 5)
1692 return DW_OP_GNU_convert;
1693 break;
1694
1695 case DW_OP_reinterpret:
1696 if (dwarf_version < 5)
1697 return DW_OP_GNU_reinterpret;
1698 break;
1699
1700 case DW_OP_addrx:
1701 if (dwarf_version < 5)
1702 return DW_OP_GNU_addr_index;
1703 break;
1704
1705 case DW_OP_constx:
1706 if (dwarf_version < 5)
1707 return DW_OP_GNU_const_index;
1708 break;
1709
1710 default:
1711 break;
1712 }
1713 return op;
1714}
1715
1716/* Similarly for attributes. */
1717static inline enum dwarf_attribute
1718dwarf_AT (enum dwarf_attribute at)
1719{
1720 switch (at)
1721 {
1722 case DW_AT_call_return_pc:
1723 if (dwarf_version < 5)
1724 return DW_AT_low_pc;
1725 break;
1726
1727 case DW_AT_call_tail_call:
1728 if (dwarf_version < 5)
1729 return DW_AT_GNU_tail_call;
1730 break;
1731
1732 case DW_AT_call_origin:
1733 if (dwarf_version < 5)
1734 return DW_AT_abstract_origin;
1735 break;
1736
1737 case DW_AT_call_target:
1738 if (dwarf_version < 5)
1739 return DW_AT_GNU_call_site_target;
1740 break;
1741
1742 case DW_AT_call_target_clobbered:
1743 if (dwarf_version < 5)
1744 return DW_AT_GNU_call_site_target_clobbered;
1745 break;
1746
1747 case DW_AT_call_parameter:
1748 if (dwarf_version < 5)
1749 return DW_AT_abstract_origin;
1750 break;
1751
1752 case DW_AT_call_value:
1753 if (dwarf_version < 5)
1754 return DW_AT_GNU_call_site_value;
1755 break;
1756
1757 case DW_AT_call_data_value:
1758 if (dwarf_version < 5)
1759 return DW_AT_GNU_call_site_data_value;
1760 break;
1761
1762 case DW_AT_call_all_calls:
1763 if (dwarf_version < 5)
1764 return DW_AT_GNU_all_call_sites;
1765 break;
1766
1767 case DW_AT_call_all_tail_calls:
1768 if (dwarf_version < 5)
1769 return DW_AT_GNU_all_tail_call_sites;
1770 break;
1771
1772 case DW_AT_dwo_name:
1773 if (dwarf_version < 5)
1774 return DW_AT_GNU_dwo_name;
1775 break;
1776
1777 case DW_AT_addr_base:
1778 if (dwarf_version < 5)
1779 return DW_AT_GNU_addr_base;
1780 break;
1781
1782 default:
1783 break;
1784 }
1785 return at;
1786}
1787
1788/* And similarly for tags. */
1789static inline enum dwarf_tag
1790dwarf_TAG (enum dwarf_tag tag)
1791{
1792 switch (tag)
1793 {
1794 case DW_TAG_call_site:
1795 if (dwarf_version < 5)
1796 return DW_TAG_GNU_call_site;
1797 break;
1798
1799 case DW_TAG_call_site_parameter:
1800 if (dwarf_version < 5)
1801 return DW_TAG_GNU_call_site_parameter;
1802 break;
1803
1804 default:
1805 break;
1806 }
1807 return tag;
1808}
1809
1810/* And similarly for forms. */
1811static inline enum dwarf_form
1812dwarf_FORM (enum dwarf_form form)
1813{
1814 switch (form)
1815 {
1816 case DW_FORM_addrx:
1817 if (dwarf_version < 5)
1818 return DW_FORM_GNU_addr_index;
1819 break;
1820
1821 case DW_FORM_strx:
1822 if (dwarf_version < 5)
1823 return DW_FORM_GNU_str_index;
1824 break;
1825
1826 default:
1827 break;
1828 }
1829 return form;
1830}
1831
1832static unsigned long int get_base_type_offset (dw_die_ref);
1833
1834/* Return the size of a location descriptor. */
1835
1836static unsigned long
1837size_of_loc_descr (dw_loc_descr_ref loc)
1838{
1839 unsigned long size = 1;
1840
1841 switch (loc->dw_loc_opc)
1842 {
1843 case DW_OP_addr:
1844 size += DWARF2_ADDR_SIZE;
1845 break;
1846 case DW_OP_GNU_addr_index:
1847 case DW_OP_addrx:
1848 case DW_OP_GNU_const_index:
1849 case DW_OP_constx:
1850 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1851 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1852 break;
1853 case DW_OP_const1u:
1854 case DW_OP_const1s:
1855 size += 1;
1856 break;
1857 case DW_OP_const2u:
1858 case DW_OP_const2s:
1859 size += 2;
1860 break;
1861 case DW_OP_const4u:
1862 case DW_OP_const4s:
1863 size += 4;
1864 break;
1865 case DW_OP_const8u:
1866 case DW_OP_const8s:
1867 size += 8;
1868 break;
1869 case DW_OP_constu:
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 break;
1872 case DW_OP_consts:
1873 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1874 break;
1875 case DW_OP_pick:
1876 size += 1;
1877 break;
1878 case DW_OP_plus_uconst:
1879 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1880 break;
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 size += 2;
1884 break;
1885 case DW_OP_breg0:
1886 case DW_OP_breg1:
1887 case DW_OP_breg2:
1888 case DW_OP_breg3:
1889 case DW_OP_breg4:
1890 case DW_OP_breg5:
1891 case DW_OP_breg6:
1892 case DW_OP_breg7:
1893 case DW_OP_breg8:
1894 case DW_OP_breg9:
1895 case DW_OP_breg10:
1896 case DW_OP_breg11:
1897 case DW_OP_breg12:
1898 case DW_OP_breg13:
1899 case DW_OP_breg14:
1900 case DW_OP_breg15:
1901 case DW_OP_breg16:
1902 case DW_OP_breg17:
1903 case DW_OP_breg18:
1904 case DW_OP_breg19:
1905 case DW_OP_breg20:
1906 case DW_OP_breg21:
1907 case DW_OP_breg22:
1908 case DW_OP_breg23:
1909 case DW_OP_breg24:
1910 case DW_OP_breg25:
1911 case DW_OP_breg26:
1912 case DW_OP_breg27:
1913 case DW_OP_breg28:
1914 case DW_OP_breg29:
1915 case DW_OP_breg30:
1916 case DW_OP_breg31:
1917 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1918 break;
1919 case DW_OP_regx:
1920 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1921 break;
1922 case DW_OP_fbreg:
1923 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1924 break;
1925 case DW_OP_bregx:
1926 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1927 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1928 break;
1929 case DW_OP_piece:
1930 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1931 break;
1932 case DW_OP_bit_piece:
1933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1934 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1935 break;
1936 case DW_OP_deref_size:
1937 case DW_OP_xderef_size:
1938 size += 1;
1939 break;
1940 case DW_OP_call2:
1941 size += 2;
1942 break;
1943 case DW_OP_call4:
1944 size += 4;
1945 break;
1946 case DW_OP_call_ref:
1947 case DW_OP_GNU_variable_value:
1948 size += DWARF_REF_SIZE;
1949 break;
1950 case DW_OP_implicit_value:
1951 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1952 + loc->dw_loc_oprnd1.v.val_unsigned;
1953 break;
1954 case DW_OP_implicit_pointer:
1955 case DW_OP_GNU_implicit_pointer:
1956 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1957 break;
1958 case DW_OP_entry_value:
1959 case DW_OP_GNU_entry_value:
1960 {
1961 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1962 size += size_of_uleb128 (op_size) + op_size;
1963 break;
1964 }
1965 case DW_OP_const_type:
1966 case DW_OP_GNU_const_type:
1967 {
1968 unsigned long o
1969 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1970 size += size_of_uleb128 (o) + 1;
1971 switch (loc->dw_loc_oprnd2.val_class)
1972 {
1973 case dw_val_class_vec:
1974 size += loc->dw_loc_oprnd2.v.val_vec.length
1975 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1976 break;
1977 case dw_val_class_const:
1978 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1979 break;
1980 case dw_val_class_const_double:
1981 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1982 break;
1983 case dw_val_class_wide_int:
1984 size += (get_full_len (op: *loc->dw_loc_oprnd2.v.val_wide)
1985 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1986 break;
1987 default:
1988 gcc_unreachable ();
1989 }
1990 break;
1991 }
1992 case DW_OP_regval_type:
1993 case DW_OP_GNU_regval_type:
1994 {
1995 unsigned long o
1996 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1997 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1998 + size_of_uleb128 (o);
1999 }
2000 break;
2001 case DW_OP_deref_type:
2002 case DW_OP_GNU_deref_type:
2003 {
2004 unsigned long o
2005 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2006 size += 1 + size_of_uleb128 (o);
2007 }
2008 break;
2009 case DW_OP_convert:
2010 case DW_OP_reinterpret:
2011 case DW_OP_GNU_convert:
2012 case DW_OP_GNU_reinterpret:
2013 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2014 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2015 else
2016 {
2017 unsigned long o
2018 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2019 size += size_of_uleb128 (o);
2020 }
2021 break;
2022 case DW_OP_GNU_parameter_ref:
2023 size += 4;
2024 break;
2025 default:
2026 break;
2027 }
2028
2029 return size;
2030}
2031
2032/* Return the size of a series of location descriptors. */
2033
2034unsigned long
2035size_of_locs (dw_loc_descr_ref loc)
2036{
2037 dw_loc_descr_ref l;
2038 unsigned long size;
2039
2040 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2041 field, to avoid writing to a PCH file. */
2042 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2043 {
2044 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2045 break;
2046 size += size_of_loc_descr (loc: l);
2047 }
2048 if (! l)
2049 return size;
2050
2051 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2052 {
2053 l->dw_loc_addr = size;
2054 size += size_of_loc_descr (loc: l);
2055 }
2056
2057 return size;
2058}
2059
2060/* Return the size of the value in a DW_AT_discr_value attribute. */
2061
2062static int
2063size_of_discr_value (dw_discr_value *discr_value)
2064{
2065 if (discr_value->pos)
2066 return size_of_uleb128 (discr_value->v.uval);
2067 else
2068 return size_of_sleb128 (discr_value->v.sval);
2069}
2070
2071/* Return the size of the value in a DW_AT_discr_list attribute. */
2072
2073static int
2074size_of_discr_list (dw_discr_list_ref discr_list)
2075{
2076 int size = 0;
2077
2078 for (dw_discr_list_ref list = discr_list;
2079 list != NULL;
2080 list = list->dw_discr_next)
2081 {
2082 /* One byte for the discriminant value descriptor, and then one or two
2083 LEB128 numbers, depending on whether it's a single case label or a
2084 range label. */
2085 size += 1;
2086 size += size_of_discr_value (discr_value: &list->dw_discr_lower_bound);
2087 if (list->dw_discr_range != 0)
2088 size += size_of_discr_value (discr_value: &list->dw_discr_upper_bound);
2089 }
2090 return size;
2091}
2092
2093static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2094static void get_ref_die_offset_label (char *, dw_die_ref);
2095static unsigned long int get_ref_die_offset (dw_die_ref);
2096
2097/* Output location description stack opcode's operands (if any).
2098 The for_eh_or_skip parameter controls whether register numbers are
2099 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2100 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2101 info). This should be suppressed for the cases that have not been converted
2102 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2103
2104static void
2105output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2106{
2107 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2108 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2109
2110 switch (loc->dw_loc_opc)
2111 {
2112#ifdef DWARF2_DEBUGGING_INFO
2113 case DW_OP_const2u:
2114 case DW_OP_const2s:
2115 dw2_asm_output_data (2, val1->v.val_int, NULL);
2116 break;
2117 case DW_OP_const4u:
2118 if (loc->dw_loc_dtprel)
2119 {
2120 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2121 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2122 val1->v.val_addr);
2123 fputc (c: '\n', stream: asm_out_file);
2124 break;
2125 }
2126 /* FALLTHRU */
2127 case DW_OP_const4s:
2128 dw2_asm_output_data (4, val1->v.val_int, NULL);
2129 break;
2130 case DW_OP_const8u:
2131 if (loc->dw_loc_dtprel)
2132 {
2133 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2134 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2135 val1->v.val_addr);
2136 fputc (c: '\n', stream: asm_out_file);
2137 break;
2138 }
2139 /* FALLTHRU */
2140 case DW_OP_const8s:
2141 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2142 dw2_asm_output_data (8, val1->v.val_int, NULL);
2143 break;
2144 case DW_OP_skip:
2145 case DW_OP_bra:
2146 {
2147 int offset;
2148
2149 gcc_assert (val1->val_class == dw_val_class_loc);
2150 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2151
2152 dw2_asm_output_data (2, offset, NULL);
2153 }
2154 break;
2155 case DW_OP_implicit_value:
2156 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2157 switch (val2->val_class)
2158 {
2159 case dw_val_class_const:
2160 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2161 break;
2162 case dw_val_class_vec:
2163 {
2164 unsigned int elt_size = val2->v.val_vec.elt_size;
2165 unsigned int len = val2->v.val_vec.length;
2166 unsigned int i;
2167 unsigned char *p;
2168
2169 if (elt_size > sizeof (HOST_WIDE_INT))
2170 {
2171 elt_size /= 2;
2172 len *= 2;
2173 }
2174 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2175 i < len;
2176 i++, p += elt_size)
2177 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2178 "fp or vector constant word %u", i);
2179 }
2180 break;
2181 case dw_val_class_const_double:
2182 {
2183 unsigned HOST_WIDE_INT first, second;
2184
2185 if (WORDS_BIG_ENDIAN)
2186 {
2187 first = val2->v.val_double.high;
2188 second = val2->v.val_double.low;
2189 }
2190 else
2191 {
2192 first = val2->v.val_double.low;
2193 second = val2->v.val_double.high;
2194 }
2195 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2196 first, NULL);
2197 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2198 second, NULL);
2199 }
2200 break;
2201 case dw_val_class_wide_int:
2202 {
2203 int i;
2204 int len = get_full_len (op: *val2->v.val_wide);
2205 if (WORDS_BIG_ENDIAN)
2206 for (i = len - 1; i >= 0; --i)
2207 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2208 val2->v.val_wide->elt (i), NULL);
2209 else
2210 for (i = 0; i < len; ++i)
2211 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2212 val2->v.val_wide->elt (i), NULL);
2213 }
2214 break;
2215 case dw_val_class_addr:
2216 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2217 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2218 break;
2219 default:
2220 gcc_unreachable ();
2221 }
2222 break;
2223#else
2224 case DW_OP_const2u:
2225 case DW_OP_const2s:
2226 case DW_OP_const4u:
2227 case DW_OP_const4s:
2228 case DW_OP_const8u:
2229 case DW_OP_const8s:
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2232 case DW_OP_implicit_value:
2233 /* We currently don't make any attempt to make sure these are
2234 aligned properly like we do for the main unwind info, so
2235 don't support emitting things larger than a byte if we're
2236 only doing unwinding. */
2237 gcc_unreachable ();
2238#endif
2239 case DW_OP_const1u:
2240 case DW_OP_const1s:
2241 dw2_asm_output_data (1, val1->v.val_int, NULL);
2242 break;
2243 case DW_OP_constu:
2244 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2245 break;
2246 case DW_OP_consts:
2247 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2248 break;
2249 case DW_OP_pick:
2250 dw2_asm_output_data (1, val1->v.val_int, NULL);
2251 break;
2252 case DW_OP_plus_uconst:
2253 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2254 break;
2255 case DW_OP_breg0:
2256 case DW_OP_breg1:
2257 case DW_OP_breg2:
2258 case DW_OP_breg3:
2259 case DW_OP_breg4:
2260 case DW_OP_breg5:
2261 case DW_OP_breg6:
2262 case DW_OP_breg7:
2263 case DW_OP_breg8:
2264 case DW_OP_breg9:
2265 case DW_OP_breg10:
2266 case DW_OP_breg11:
2267 case DW_OP_breg12:
2268 case DW_OP_breg13:
2269 case DW_OP_breg14:
2270 case DW_OP_breg15:
2271 case DW_OP_breg16:
2272 case DW_OP_breg17:
2273 case DW_OP_breg18:
2274 case DW_OP_breg19:
2275 case DW_OP_breg20:
2276 case DW_OP_breg21:
2277 case DW_OP_breg22:
2278 case DW_OP_breg23:
2279 case DW_OP_breg24:
2280 case DW_OP_breg25:
2281 case DW_OP_breg26:
2282 case DW_OP_breg27:
2283 case DW_OP_breg28:
2284 case DW_OP_breg29:
2285 case DW_OP_breg30:
2286 case DW_OP_breg31:
2287 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2288 break;
2289 case DW_OP_regx:
2290 {
2291 unsigned r = val1->v.val_unsigned;
2292 if (for_eh_or_skip >= 0)
2293 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2294 gcc_assert (size_of_uleb128 (r)
2295 == size_of_uleb128 (val1->v.val_unsigned));
2296 dw2_asm_output_data_uleb128 (r, NULL);
2297 }
2298 break;
2299 case DW_OP_fbreg:
2300 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2301 break;
2302 case DW_OP_bregx:
2303 {
2304 unsigned r = val1->v.val_unsigned;
2305 if (for_eh_or_skip >= 0)
2306 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 dw2_asm_output_data_uleb128 (r, NULL);
2310 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2311 }
2312 break;
2313 case DW_OP_piece:
2314 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2315 break;
2316 case DW_OP_bit_piece:
2317 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2318 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2319 break;
2320 case DW_OP_deref_size:
2321 case DW_OP_xderef_size:
2322 dw2_asm_output_data (1, val1->v.val_int, NULL);
2323 break;
2324
2325 case DW_OP_addr:
2326 if (loc->dw_loc_dtprel)
2327 {
2328 if (targetm.asm_out.output_dwarf_dtprel)
2329 {
2330 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2331 DWARF2_ADDR_SIZE,
2332 val1->v.val_addr);
2333 fputc (c: '\n', stream: asm_out_file);
2334 }
2335 else
2336 gcc_unreachable ();
2337 }
2338 else
2339 {
2340#ifdef DWARF2_DEBUGGING_INFO
2341 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2342#else
2343 gcc_unreachable ();
2344#endif
2345 }
2346 break;
2347
2348 case DW_OP_GNU_addr_index:
2349 case DW_OP_addrx:
2350 case DW_OP_GNU_const_index:
2351 case DW_OP_constx:
2352 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2353 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2354 "(index into .debug_addr)");
2355 break;
2356
2357 case DW_OP_call2:
2358 case DW_OP_call4:
2359 {
2360 unsigned long die_offset
2361 = get_ref_die_offset (val1->v.val_die_ref.die);
2362 /* Make sure the offset has been computed and that we can encode it as
2363 an operand. */
2364 gcc_assert (die_offset > 0
2365 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2366 ? 0xffff
2367 : 0xffffffff));
2368 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2369 die_offset, NULL);
2370 }
2371 break;
2372
2373 case DW_OP_call_ref:
2374 case DW_OP_GNU_variable_value:
2375 {
2376 char label[MAX_ARTIFICIAL_LABEL_BYTES
2377 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2378 gcc_assert (val1->val_class == dw_val_class_die_ref);
2379 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2380 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2381 }
2382 break;
2383
2384 case DW_OP_implicit_pointer:
2385 case DW_OP_GNU_implicit_pointer:
2386 {
2387 char label[MAX_ARTIFICIAL_LABEL_BYTES
2388 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2389 gcc_assert (val1->val_class == dw_val_class_die_ref);
2390 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2391 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2392 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2393 }
2394 break;
2395
2396 case DW_OP_entry_value:
2397 case DW_OP_GNU_entry_value:
2398 dw2_asm_output_data_uleb128 (size_of_locs (loc: val1->v.val_loc), NULL);
2399 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2400 break;
2401
2402 case DW_OP_const_type:
2403 case DW_OP_GNU_const_type:
2404 {
2405 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2406 gcc_assert (o);
2407 dw2_asm_output_data_uleb128 (o, NULL);
2408 switch (val2->val_class)
2409 {
2410 case dw_val_class_const:
2411 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2412 dw2_asm_output_data (1, l, NULL);
2413 dw2_asm_output_data (l, val2->v.val_int, NULL);
2414 break;
2415 case dw_val_class_vec:
2416 {
2417 unsigned int elt_size = val2->v.val_vec.elt_size;
2418 unsigned int len = val2->v.val_vec.length;
2419 unsigned int i;
2420 unsigned char *p;
2421
2422 l = len * elt_size;
2423 dw2_asm_output_data (1, l, NULL);
2424 if (elt_size > sizeof (HOST_WIDE_INT))
2425 {
2426 elt_size /= 2;
2427 len *= 2;
2428 }
2429 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2430 i < len;
2431 i++, p += elt_size)
2432 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2433 "fp or vector constant word %u", i);
2434 }
2435 break;
2436 case dw_val_class_const_double:
2437 {
2438 unsigned HOST_WIDE_INT first, second;
2439 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2440
2441 dw2_asm_output_data (1, 2 * l, NULL);
2442 if (WORDS_BIG_ENDIAN)
2443 {
2444 first = val2->v.val_double.high;
2445 second = val2->v.val_double.low;
2446 }
2447 else
2448 {
2449 first = val2->v.val_double.low;
2450 second = val2->v.val_double.high;
2451 }
2452 dw2_asm_output_data (l, first, NULL);
2453 dw2_asm_output_data (l, second, NULL);
2454 }
2455 break;
2456 case dw_val_class_wide_int:
2457 {
2458 int i;
2459 int len = get_full_len (op: *val2->v.val_wide);
2460 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2461
2462 dw2_asm_output_data (1, len * l, NULL);
2463 if (WORDS_BIG_ENDIAN)
2464 for (i = len - 1; i >= 0; --i)
2465 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2466 else
2467 for (i = 0; i < len; ++i)
2468 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2469 }
2470 break;
2471 default:
2472 gcc_unreachable ();
2473 }
2474 }
2475 break;
2476 case DW_OP_regval_type:
2477 case DW_OP_GNU_regval_type:
2478 {
2479 unsigned r = val1->v.val_unsigned;
2480 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2481 gcc_assert (o);
2482 if (for_eh_or_skip >= 0)
2483 {
2484 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 }
2488 dw2_asm_output_data_uleb128 (r, NULL);
2489 dw2_asm_output_data_uleb128 (o, NULL);
2490 }
2491 break;
2492 case DW_OP_deref_type:
2493 case DW_OP_GNU_deref_type:
2494 {
2495 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2496 gcc_assert (o);
2497 dw2_asm_output_data (1, val1->v.val_int, NULL);
2498 dw2_asm_output_data_uleb128 (o, NULL);
2499 }
2500 break;
2501 case DW_OP_convert:
2502 case DW_OP_reinterpret:
2503 case DW_OP_GNU_convert:
2504 case DW_OP_GNU_reinterpret:
2505 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2506 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2507 else
2508 {
2509 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2510 gcc_assert (o);
2511 dw2_asm_output_data_uleb128 (o, NULL);
2512 }
2513 break;
2514
2515 case DW_OP_GNU_parameter_ref:
2516 {
2517 unsigned long o;
2518 gcc_assert (val1->val_class == dw_val_class_die_ref);
2519 o = get_ref_die_offset (val1->v.val_die_ref.die);
2520 dw2_asm_output_data (4, o, NULL);
2521 }
2522 break;
2523
2524 default:
2525 /* Other codes have no operands. */
2526 break;
2527 }
2528}
2529
2530/* Output a sequence of location operations.
2531 The for_eh_or_skip parameter controls whether register numbers are
2532 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2533 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2534 info). This should be suppressed for the cases that have not been converted
2535 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2536
2537void
2538output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2539{
2540 for (; loc != NULL; loc = loc->dw_loc_next)
2541 {
2542 enum dwarf_location_atom opc = loc->dw_loc_opc;
2543 /* Output the opcode. */
2544 if (for_eh_or_skip >= 0
2545 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2546 {
2547 unsigned r = (opc - DW_OP_breg0);
2548 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2549 gcc_assert (r <= 31);
2550 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2551 }
2552 else if (for_eh_or_skip >= 0
2553 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2554 {
2555 unsigned r = (opc - DW_OP_reg0);
2556 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2557 gcc_assert (r <= 31);
2558 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2559 }
2560
2561 dw2_asm_output_data (1, opc,
2562 "%s", dwarf_stack_op_name (op: opc));
2563
2564 /* Output the operand(s) (if any). */
2565 output_loc_operands (loc, for_eh_or_skip);
2566 }
2567}
2568
2569/* Output location description stack opcode's operands (if any).
2570 The output is single bytes on a line, suitable for .cfi_escape. */
2571
2572static void
2573output_loc_operands_raw (dw_loc_descr_ref loc)
2574{
2575 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2576 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2577
2578 switch (loc->dw_loc_opc)
2579 {
2580 case DW_OP_addr:
2581 case DW_OP_GNU_addr_index:
2582 case DW_OP_addrx:
2583 case DW_OP_GNU_const_index:
2584 case DW_OP_constx:
2585 case DW_OP_implicit_value:
2586 /* We cannot output addresses in .cfi_escape, only bytes. */
2587 gcc_unreachable ();
2588
2589 case DW_OP_const1u:
2590 case DW_OP_const1s:
2591 case DW_OP_pick:
2592 case DW_OP_deref_size:
2593 case DW_OP_xderef_size:
2594 fputc (c: ',', stream: asm_out_file);
2595 dw2_asm_output_data_raw (1, val1->v.val_int);
2596 break;
2597
2598 case DW_OP_const2u:
2599 case DW_OP_const2s:
2600 fputc (c: ',', stream: asm_out_file);
2601 dw2_asm_output_data_raw (2, val1->v.val_int);
2602 break;
2603
2604 case DW_OP_const4u:
2605 case DW_OP_const4s:
2606 fputc (c: ',', stream: asm_out_file);
2607 dw2_asm_output_data_raw (4, val1->v.val_int);
2608 break;
2609
2610 case DW_OP_const8u:
2611 case DW_OP_const8s:
2612 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2613 fputc (c: ',', stream: asm_out_file);
2614 dw2_asm_output_data_raw (8, val1->v.val_int);
2615 break;
2616
2617 case DW_OP_skip:
2618 case DW_OP_bra:
2619 {
2620 int offset;
2621
2622 gcc_assert (val1->val_class == dw_val_class_loc);
2623 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2624
2625 fputc (c: ',', stream: asm_out_file);
2626 dw2_asm_output_data_raw (2, offset);
2627 }
2628 break;
2629
2630 case DW_OP_regx:
2631 {
2632 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2633 gcc_assert (size_of_uleb128 (r)
2634 == size_of_uleb128 (val1->v.val_unsigned));
2635 fputc (c: ',', stream: asm_out_file);
2636 dw2_asm_output_data_uleb128_raw (r);
2637 }
2638 break;
2639
2640 case DW_OP_constu:
2641 case DW_OP_plus_uconst:
2642 case DW_OP_piece:
2643 fputc (c: ',', stream: asm_out_file);
2644 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2645 break;
2646
2647 case DW_OP_bit_piece:
2648 fputc (c: ',', stream: asm_out_file);
2649 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2650 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2651 break;
2652
2653 case DW_OP_consts:
2654 case DW_OP_breg0:
2655 case DW_OP_breg1:
2656 case DW_OP_breg2:
2657 case DW_OP_breg3:
2658 case DW_OP_breg4:
2659 case DW_OP_breg5:
2660 case DW_OP_breg6:
2661 case DW_OP_breg7:
2662 case DW_OP_breg8:
2663 case DW_OP_breg9:
2664 case DW_OP_breg10:
2665 case DW_OP_breg11:
2666 case DW_OP_breg12:
2667 case DW_OP_breg13:
2668 case DW_OP_breg14:
2669 case DW_OP_breg15:
2670 case DW_OP_breg16:
2671 case DW_OP_breg17:
2672 case DW_OP_breg18:
2673 case DW_OP_breg19:
2674 case DW_OP_breg20:
2675 case DW_OP_breg21:
2676 case DW_OP_breg22:
2677 case DW_OP_breg23:
2678 case DW_OP_breg24:
2679 case DW_OP_breg25:
2680 case DW_OP_breg26:
2681 case DW_OP_breg27:
2682 case DW_OP_breg28:
2683 case DW_OP_breg29:
2684 case DW_OP_breg30:
2685 case DW_OP_breg31:
2686 case DW_OP_fbreg:
2687 fputc (c: ',', stream: asm_out_file);
2688 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2689 break;
2690
2691 case DW_OP_bregx:
2692 {
2693 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2694 gcc_assert (size_of_uleb128 (r)
2695 == size_of_uleb128 (val1->v.val_unsigned));
2696 fputc (c: ',', stream: asm_out_file);
2697 dw2_asm_output_data_uleb128_raw (r);
2698 fputc (c: ',', stream: asm_out_file);
2699 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2700 }
2701 break;
2702
2703 case DW_OP_implicit_pointer:
2704 case DW_OP_entry_value:
2705 case DW_OP_const_type:
2706 case DW_OP_regval_type:
2707 case DW_OP_deref_type:
2708 case DW_OP_convert:
2709 case DW_OP_reinterpret:
2710 case DW_OP_GNU_implicit_pointer:
2711 case DW_OP_GNU_entry_value:
2712 case DW_OP_GNU_const_type:
2713 case DW_OP_GNU_regval_type:
2714 case DW_OP_GNU_deref_type:
2715 case DW_OP_GNU_convert:
2716 case DW_OP_GNU_reinterpret:
2717 case DW_OP_GNU_parameter_ref:
2718 gcc_unreachable ();
2719 break;
2720
2721 default:
2722 /* Other codes have no operands. */
2723 break;
2724 }
2725}
2726
2727void
2728output_loc_sequence_raw (dw_loc_descr_ref loc)
2729{
2730 while (1)
2731 {
2732 enum dwarf_location_atom opc = loc->dw_loc_opc;
2733 /* Output the opcode. */
2734 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2735 {
2736 unsigned r = (opc - DW_OP_breg0);
2737 r = DWARF2_FRAME_REG_OUT (r, 1);
2738 gcc_assert (r <= 31);
2739 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2740 }
2741 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2742 {
2743 unsigned r = (opc - DW_OP_reg0);
2744 r = DWARF2_FRAME_REG_OUT (r, 1);
2745 gcc_assert (r <= 31);
2746 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2747 }
2748 /* Output the opcode. */
2749 fprintf (stream: asm_out_file, format: "%#x", opc);
2750 output_loc_operands_raw (loc);
2751
2752 if (!loc->dw_loc_next)
2753 break;
2754 loc = loc->dw_loc_next;
2755
2756 fputc (c: ',', stream: asm_out_file);
2757 }
2758}
2759
2760static void
2761build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2762{
2763 if (regno <= 31)
2764 add_loc_descr (list_head: head, descr: new_loc_descr (op: (enum dwarf_location_atom)
2765 (DW_OP_breg0 + regno), oprnd1: 0, oprnd2: 0));
2766 else
2767 add_loc_descr (list_head: head, descr: new_loc_descr (op: DW_OP_bregx, oprnd1: regno, oprnd2: 0));
2768}
2769
2770/* Build a dwarf location for a cfa_reg spanning multiple
2771 consecutive registers. */
2772
2773struct dw_loc_descr_node *
2774build_span_loc (struct cfa_reg reg)
2775{
2776 struct dw_loc_descr_node *head = NULL;
2777
2778 gcc_assert (reg.span_width > 0);
2779 gcc_assert (reg.span > 1);
2780
2781 /* Start from the highest number register as it goes in the upper bits. */
2782 unsigned int regno = reg.reg + reg.span - 1;
2783 build_breg_loc (head: &head, regno);
2784
2785 /* Deal with the remaining registers in the span. */
2786 for (int i = reg.span - 2; i >= 0; i--)
2787 {
2788 add_loc_descr (list_head: &head, descr: int_loc_descriptor (reg.span_width * 8));
2789 add_loc_descr (list_head: &head, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
2790 regno--;
2791 build_breg_loc (head: &head, regno);
2792 add_loc_descr (list_head: &head, descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
2793 }
2794 return head;
2795}
2796
2797/* This function builds a dwarf location descriptor sequence from a
2798 dw_cfa_location, adding the given OFFSET to the result of the
2799 expression. */
2800
2801struct dw_loc_descr_node *
2802build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2803{
2804 struct dw_loc_descr_node *head, *tmp;
2805
2806 offset += cfa->offset;
2807
2808 if (cfa->reg.span > 1)
2809 {
2810 head = build_span_loc (reg: cfa->reg);
2811
2812 if (maybe_ne (a: offset, b: 0))
2813 loc_descr_plus_const (list_head: &head, poly_offset: offset);
2814 }
2815 else if (cfa->indirect)
2816 {
2817 head = new_reg_loc_descr (reg: cfa->reg.reg, offset: cfa->base_offset);
2818 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2819 head->dw_loc_oprnd1.val_entry = NULL;
2820 tmp = new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0);
2821 add_loc_descr (list_head: &head, descr: tmp);
2822 loc_descr_plus_const (list_head: &head, poly_offset: offset);
2823 }
2824 else
2825 head = new_reg_loc_descr (reg: cfa->reg.reg, offset);
2826
2827 return head;
2828}
2829
2830/* This function builds a dwarf location descriptor sequence for
2831 the address at OFFSET from the CFA when stack is aligned to
2832 ALIGNMENT byte. */
2833
2834struct dw_loc_descr_node *
2835build_cfa_aligned_loc (dw_cfa_location *cfa,
2836 poly_int64 offset, HOST_WIDE_INT alignment)
2837{
2838 struct dw_loc_descr_node *head;
2839 unsigned int dwarf_fp
2840 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2841
2842 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2843 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2844 {
2845 head = new_reg_loc_descr (reg: dwarf_fp, offset: 0);
2846 add_loc_descr (list_head: &head, descr: int_loc_descriptor (alignment));
2847 add_loc_descr (list_head: &head, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
2848 loc_descr_plus_const (list_head: &head, poly_offset: offset);
2849 }
2850 else
2851 head = new_reg_loc_descr (reg: dwarf_fp, offset);
2852 return head;
2853}
2854
2855/* And now, the support for symbolic debugging information. */
2856
2857/* .debug_str support. */
2858
2859static void dwarf2out_init (const char *);
2860static void dwarf2out_finish (const char *);
2861static void dwarf2out_early_finish (const char *);
2862static void dwarf2out_assembly_start (void);
2863static void dwarf2out_define (unsigned int, const char *);
2864static void dwarf2out_undef (unsigned int, const char *);
2865static void dwarf2out_start_source_file (unsigned, const char *);
2866static void dwarf2out_end_source_file (unsigned);
2867static void dwarf2out_function_decl (tree);
2868static void dwarf2out_begin_block (unsigned, unsigned, tree);
2869static void dwarf2out_end_block (unsigned, unsigned);
2870static bool dwarf2out_ignore_block (const_tree);
2871static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2872static void dwarf2out_early_global_decl (tree);
2873static void dwarf2out_late_global_decl (tree);
2874static void dwarf2out_type_decl (tree, int);
2875static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2876static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2877 dw_die_ref);
2878static void dwarf2out_abstract_function (tree);
2879static void dwarf2out_var_location (rtx_insn *);
2880static void dwarf2out_inline_entry (tree);
2881static void dwarf2out_size_function (tree);
2882static void dwarf2out_begin_function (tree);
2883static void dwarf2out_end_function (unsigned int);
2884static void dwarf2out_register_main_translation_unit (tree unit);
2885static void dwarf2out_set_name (tree, tree);
2886static void dwarf2out_register_external_die (tree decl, const char *sym,
2887 unsigned HOST_WIDE_INT off);
2888static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2889 unsigned HOST_WIDE_INT *off);
2890
2891/* The debug hooks structure. */
2892
2893const struct gcc_debug_hooks dwarf2_debug_hooks =
2894{
2895 .init: dwarf2out_init,
2896 .finish: dwarf2out_finish,
2897 .early_finish: dwarf2out_early_finish,
2898 .assembly_start: dwarf2out_assembly_start,
2899 .define: dwarf2out_define,
2900 .undef: dwarf2out_undef,
2901 .start_source_file: dwarf2out_start_source_file,
2902 .end_source_file: dwarf2out_end_source_file,
2903 .begin_block: dwarf2out_begin_block,
2904 .end_block: dwarf2out_end_block,
2905 .ignore_block: dwarf2out_ignore_block,
2906 .source_line: dwarf2out_source_line,
2907 .set_ignored_loc: dwarf2out_set_ignored_loc,
2908 .begin_prologue: dwarf2out_begin_prologue,
2909#if VMS_DEBUGGING_INFO
2910 dwarf2out_vms_end_prologue,
2911 dwarf2out_vms_begin_epilogue,
2912#else
2913 .end_prologue: debug_nothing_int_charstar,
2914 .begin_epilogue: debug_nothing_int_charstar,
2915#endif
2916 .end_epilogue: dwarf2out_end_epilogue,
2917 .begin_function: dwarf2out_begin_function,
2918 .end_function: dwarf2out_end_function, /* end_function */
2919 .register_main_translation_unit: dwarf2out_register_main_translation_unit,
2920 .function_decl: dwarf2out_function_decl, /* function_decl */
2921 .early_global_decl: dwarf2out_early_global_decl,
2922 .late_global_decl: dwarf2out_late_global_decl,
2923 .type_decl: dwarf2out_type_decl, /* type_decl */
2924 .imported_module_or_decl: dwarf2out_imported_module_or_decl,
2925 .die_ref_for_decl: dwarf2out_die_ref_for_decl,
2926 .register_external_die: dwarf2out_register_external_die,
2927 .deferred_inline_function: debug_nothing_tree, /* deferred_inline_function */
2928 /* The DWARF 2 backend tries to reduce debugging bloat by not
2929 emitting the abstract description of inline functions until
2930 something tries to reference them. */
2931 .outlining_inline_function: dwarf2out_abstract_function, /* outlining_inline_function */
2932 .label: debug_nothing_rtx_code_label, /* label */
2933 .handle_pch: debug_nothing_int, /* handle_pch */
2934 .var_location: dwarf2out_var_location,
2935 .inline_entry: dwarf2out_inline_entry, /* inline_entry */
2936 .size_function: dwarf2out_size_function, /* size_function */
2937 .switch_text_section: dwarf2out_switch_text_section,
2938 .set_name: dwarf2out_set_name,
2939 .start_end_main_source_file: 1, /* start_end_main_source_file */
2940 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2941};
2942
2943const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2944{
2945 .init: dwarf2out_init,
2946 .finish: debug_nothing_charstar,
2947 .early_finish: debug_nothing_charstar,
2948 .assembly_start: dwarf2out_assembly_start,
2949 .define: debug_nothing_int_charstar,
2950 .undef: debug_nothing_int_charstar,
2951 .start_source_file: debug_nothing_int_charstar,
2952 .end_source_file: debug_nothing_int,
2953 .begin_block: debug_nothing_int_int_tree, /* begin_block */
2954 .end_block: debug_nothing_int_int, /* end_block */
2955 .ignore_block: debug_true_const_tree, /* ignore_block */
2956 .source_line: dwarf2out_source_line, /* source_line */
2957 .set_ignored_loc: debug_nothing_int_int_charstar, /* set_ignored_loc */
2958 .begin_prologue: debug_nothing_int_int_charstar, /* begin_prologue */
2959 .end_prologue: debug_nothing_int_charstar, /* end_prologue */
2960 .begin_epilogue: debug_nothing_int_charstar, /* begin_epilogue */
2961 .end_epilogue: debug_nothing_int_charstar, /* end_epilogue */
2962 .begin_function: debug_nothing_tree, /* begin_function */
2963 .end_function: debug_nothing_int, /* end_function */
2964 .register_main_translation_unit: debug_nothing_tree, /* register_main_translation_unit */
2965 .function_decl: debug_nothing_tree, /* function_decl */
2966 .early_global_decl: debug_nothing_tree, /* early_global_decl */
2967 .late_global_decl: debug_nothing_tree, /* late_global_decl */
2968 .type_decl: debug_nothing_tree_int, /* type_decl */
2969 .imported_module_or_decl: debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2970 .die_ref_for_decl: debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2971 .register_external_die: debug_nothing_tree_charstar_uhwi, /* register_external_die */
2972 .deferred_inline_function: debug_nothing_tree, /* deferred_inline_function */
2973 .outlining_inline_function: debug_nothing_tree, /* outlining_inline_function */
2974 .label: debug_nothing_rtx_code_label, /* label */
2975 .handle_pch: debug_nothing_int, /* handle_pch */
2976 .var_location: debug_nothing_rtx_insn, /* var_location */
2977 .inline_entry: debug_nothing_tree, /* inline_entry */
2978 .size_function: debug_nothing_tree, /* size_function */
2979 .switch_text_section: debug_nothing_void, /* switch_text_section */
2980 .set_name: debug_nothing_tree_tree, /* set_name */
2981 .start_end_main_source_file: 0, /* start_end_main_source_file */
2982 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2983};
2984
2985/* NOTE: In the comments in this file, many references are made to
2986 "Debugging Information Entries". This term is abbreviated as `DIE'
2987 throughout the remainder of this file. */
2988
2989/* An internal representation of the DWARF output is built, and then
2990 walked to generate the DWARF debugging info. The walk of the internal
2991 representation is done after the entire program has been compiled.
2992 The types below are used to describe the internal representation. */
2993
2994/* Whether to put type DIEs into their own section .debug_types instead
2995 of making them part of the .debug_info section. Only supported for
2996 Dwarf V4 or higher and the user didn't disable them through
2997 -fno-debug-types-section. It is more efficient to put them in a
2998 separate comdat sections since the linker will then be able to
2999 remove duplicates. But not all tools support .debug_types sections
3000 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3001 it is DW_UT_type unit type in .debug_info section. For late LTO
3002 debug there should be almost no types emitted so avoid enabling
3003 -fdebug-types-section there. */
3004
3005#define use_debug_types (dwarf_version >= 4 \
3006 && flag_debug_types_section \
3007 && !in_lto_p)
3008
3009/* Various DIE's use offsets relative to the beginning of the
3010 .debug_info section to refer to each other. */
3011
3012typedef long int dw_offset;
3013
3014struct comdat_type_node;
3015
3016/* The entries in the line_info table more-or-less mirror the opcodes
3017 that are used in the real dwarf line table. Arrays of these entries
3018 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3019 supported. */
3020
3021enum dw_line_info_opcode {
3022 /* Emit DW_LNE_set_address; the operand is the label index. */
3023 LI_set_address,
3024
3025 /* Emit a row to the matrix with the given line. This may be done
3026 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3027 special opcodes. */
3028 LI_set_line,
3029
3030 /* Emit a DW_LNS_set_file. */
3031 LI_set_file,
3032
3033 /* Emit a DW_LNS_set_column. */
3034 LI_set_column,
3035
3036 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3037 LI_negate_stmt,
3038
3039 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3040 LI_set_prologue_end,
3041 LI_set_epilogue_begin,
3042
3043 /* Emit a DW_LNE_set_discriminator. */
3044 LI_set_discriminator,
3045
3046 /* Output a Fixed Advance PC; the target PC is the label index; the
3047 base PC is the previous LI_adv_address or LI_set_address entry.
3048 We only use this when emitting debug views without assembler
3049 support, at explicit user request. Ideally, we should only use
3050 it when the offset might be zero but we can't tell: it's the only
3051 way to maybe change the PC without resetting the view number. */
3052 LI_adv_address
3053};
3054
3055typedef struct GTY(()) dw_line_info_struct {
3056 enum dw_line_info_opcode opcode;
3057 unsigned int val;
3058} dw_line_info_entry;
3059
3060
3061struct GTY(()) dw_line_info_table {
3062 /* The label that marks the end of this section. */
3063 const char *end_label;
3064
3065 /* The values for the last row of the matrix, as collected in the table.
3066 These are used to minimize the changes to the next row. */
3067 unsigned int file_num;
3068 unsigned int line_num;
3069 unsigned int column_num;
3070 int discrim_num;
3071 bool is_stmt;
3072 bool in_use;
3073
3074 /* This denotes the NEXT view number.
3075
3076 If it is 0, it is known that the NEXT view will be the first view
3077 at the given PC.
3078
3079 If it is -1, we're forcing the view number to be reset, e.g. at a
3080 function entry.
3081
3082 The meaning of other nonzero values depends on whether we're
3083 computing views internally or leaving it for the assembler to do
3084 so. If we're emitting them internally, view denotes the view
3085 number since the last known advance of PC. If we're leaving it
3086 for the assembler, it denotes the LVU label number that we're
3087 going to ask the assembler to assign. */
3088 var_loc_view view;
3089
3090 /* This counts the number of symbolic views emitted in this table
3091 since the latest view reset. Its max value, over all tables,
3092 sets symview_upper_bound. */
3093 var_loc_view symviews_since_reset;
3094
3095#define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3096#define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3097#define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3098#define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3099
3100 vec<dw_line_info_entry, va_gc> *entries;
3101};
3102
3103/* This is an upper bound for view numbers that the assembler may
3104 assign to symbolic views output in this translation. It is used to
3105 decide how big a field to use to represent view numbers in
3106 symview-classed attributes. */
3107
3108static var_loc_view symview_upper_bound;
3109
3110/* If we're keep track of location views and their reset points, and
3111 INSN is a reset point (i.e., it necessarily advances the PC), mark
3112 the next view in TABLE as reset. */
3113
3114static void
3115maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3116{
3117 if (!debug_internal_reset_location_views)
3118 return;
3119
3120 /* Maybe turn (part of?) this test into a default target hook. */
3121 int reset = 0;
3122
3123 if (targetm.reset_location_view)
3124 reset = targetm.reset_location_view (insn);
3125
3126 if (reset)
3127 ;
3128 else if (JUMP_TABLE_DATA_P (insn))
3129 reset = 1;
3130 else if (GET_CODE (insn) == USE
3131 || GET_CODE (insn) == CLOBBER
3132 || GET_CODE (insn) == ASM_INPUT
3133 || asm_noperands (insn) >= 0)
3134 ;
3135 else if (get_attr_min_length (insn) > 0)
3136 reset = 1;
3137
3138 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3139 RESET_NEXT_VIEW (table->view);
3140}
3141
3142/* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3143 The children of each node form a circular list linked by
3144 die_sib. die_child points to the node *before* the "first" child node. */
3145
3146typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3147 union die_symbol_or_type_node
3148 {
3149 const char * GTY ((tag ("0"))) die_symbol;
3150 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3151 }
3152 GTY ((desc ("%0.comdat_type_p"))) die_id;
3153 vec<dw_attr_node, va_gc> *die_attr;
3154 dw_die_ref die_parent;
3155 dw_die_ref die_child;
3156 dw_die_ref die_sib;
3157 dw_die_ref die_definition; /* ref from a specification to its definition */
3158 dw_offset die_offset;
3159 unsigned long die_abbrev;
3160 int die_mark;
3161 unsigned int decl_id;
3162 enum dwarf_tag die_tag;
3163 /* Die is used and must not be pruned as unused. */
3164 BOOL_BITFIELD die_perennial_p : 1;
3165 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3166 /* For an external ref to die_symbol if die_offset contains an extra
3167 offset to that symbol. */
3168 BOOL_BITFIELD with_offset : 1;
3169 /* Whether this DIE was removed from the DIE tree, for example via
3170 prune_unused_types. We don't consider those present from the
3171 DIE lookup routines. */
3172 BOOL_BITFIELD removed : 1;
3173 /* Lots of spare bits. */
3174}
3175die_node;
3176
3177/* Set to TRUE while dwarf2out_early_global_decl is running. */
3178static bool early_dwarf;
3179static bool early_dwarf_finished;
3180class set_early_dwarf {
3181public:
3182 bool saved;
3183 set_early_dwarf () : saved(early_dwarf)
3184 {
3185 gcc_assert (! early_dwarf_finished);
3186 early_dwarf = true;
3187 }
3188 ~set_early_dwarf () { early_dwarf = saved; }
3189};
3190
3191/* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3192#define FOR_EACH_CHILD(die, c, expr) do { \
3193 c = die->die_child; \
3194 if (c) do { \
3195 c = c->die_sib; \
3196 expr; \
3197 } while (c != die->die_child); \
3198} while (0)
3199
3200/* The pubname structure */
3201
3202typedef struct GTY(()) pubname_struct {
3203 dw_die_ref die;
3204 const char *name;
3205}
3206pubname_entry;
3207
3208
3209struct GTY(()) dw_ranges {
3210 const char *label;
3211 /* If this is positive, it's a block number, otherwise it's a
3212 bitwise-negated index into dw_ranges_by_label. */
3213 int num;
3214 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3215 into .debug_rnglists section rather than .debug_rnglists.dwo
3216 for -gsplit-dwarf and DWARF >= 5. */
3217#define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3218 /* Index for the range list for DW_FORM_rnglistx. */
3219 unsigned int idx : 31;
3220 /* True if this range might be possibly in a different section
3221 from previous entry. */
3222 unsigned int maybe_new_sec : 1;
3223 addr_table_entry *begin_entry;
3224 addr_table_entry *end_entry;
3225};
3226
3227/* A structure to hold a macinfo entry. */
3228
3229typedef struct GTY(()) macinfo_struct {
3230 unsigned char code;
3231 unsigned HOST_WIDE_INT lineno;
3232 const char *info;
3233}
3234macinfo_entry;
3235
3236
3237struct GTY(()) dw_ranges_by_label {
3238 const char *begin;
3239 const char *end;
3240};
3241
3242/* The comdat type node structure. */
3243struct GTY(()) comdat_type_node
3244{
3245 dw_die_ref root_die;
3246 dw_die_ref type_die;
3247 dw_die_ref skeleton_die;
3248 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3249 comdat_type_node *next;
3250};
3251
3252/* A list of DIEs for which we can't determine ancestry (parent_die
3253 field) just yet. Later in dwarf2out_finish we will fill in the
3254 missing bits. */
3255typedef struct GTY(()) limbo_die_struct {
3256 dw_die_ref die;
3257 /* The tree for which this DIE was created. We use this to
3258 determine ancestry later. */
3259 tree created_for;
3260 struct limbo_die_struct *next;
3261}
3262limbo_die_node;
3263
3264typedef struct skeleton_chain_struct
3265{
3266 dw_die_ref old_die;
3267 dw_die_ref new_die;
3268 struct skeleton_chain_struct *parent;
3269}
3270skeleton_chain_node;
3271
3272/* Define a macro which returns nonzero for a TYPE_DECL which was
3273 implicitly generated for a type.
3274
3275 Note that, unlike the C front-end (which generates a NULL named
3276 TYPE_DECL node for each complete tagged type, each array type,
3277 and each function type node created) the C++ front-end generates
3278 a _named_ TYPE_DECL node for each tagged type node created.
3279 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3280 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3281 front-end, but for each type, tagged or not. */
3282
3283#define TYPE_DECL_IS_STUB(decl) \
3284 (DECL_NAME (decl) == NULL_TREE \
3285 || (DECL_ARTIFICIAL (decl) \
3286 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3287 /* This is necessary for stub decls that \
3288 appear in nested inline functions. */ \
3289 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3290 && (decl_ultimate_origin (decl) \
3291 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3292
3293/* Information concerning the compilation unit's programming
3294 language, and compiler version. */
3295
3296/* Fixed size portion of the DWARF compilation unit header. */
3297#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3298 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3299 + (dwarf_version >= 5 ? 4 : 3))
3300
3301/* Fixed size portion of the DWARF comdat type unit header. */
3302#define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3303 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3304 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3305
3306/* Fixed size portion of the DWARF skeleton compilation unit header. */
3307#define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3308 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3309
3310/* Fixed size portion of public names info. */
3311#define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3312
3313/* Fixed size portion of the address range info. */
3314#define DWARF_ARANGES_HEADER_SIZE \
3315 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3316 DWARF2_ADDR_SIZE * 2) \
3317 - DWARF_INITIAL_LENGTH_SIZE)
3318
3319/* Size of padding portion in the address range info. It must be
3320 aligned to twice the pointer size. */
3321#define DWARF_ARANGES_PAD_SIZE \
3322 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3323 DWARF2_ADDR_SIZE * 2) \
3324 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3325
3326/* Use assembler line directives if available. */
3327#ifndef DWARF2_ASM_LINE_DEBUG_INFO
3328#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3329#define DWARF2_ASM_LINE_DEBUG_INFO 1
3330#else
3331#define DWARF2_ASM_LINE_DEBUG_INFO 0
3332#endif
3333#endif
3334
3335/* Use assembler views in line directives if available. */
3336#ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3337#ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3338#define DWARF2_ASM_VIEW_DEBUG_INFO 1
3339#else
3340#define DWARF2_ASM_VIEW_DEBUG_INFO 0
3341#endif
3342#endif
3343
3344/* Return true if GCC configure detected assembler support for .loc. */
3345
3346bool
3347dwarf2out_default_as_loc_support (void)
3348{
3349 return DWARF2_ASM_LINE_DEBUG_INFO;
3350#if (GCC_VERSION >= 3000)
3351# undef DWARF2_ASM_LINE_DEBUG_INFO
3352# pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3353#endif
3354}
3355
3356/* Return true if GCC configure detected assembler support for views
3357 in .loc directives. */
3358
3359bool
3360dwarf2out_default_as_locview_support (void)
3361{
3362 return DWARF2_ASM_VIEW_DEBUG_INFO;
3363#if (GCC_VERSION >= 3000)
3364# undef DWARF2_ASM_VIEW_DEBUG_INFO
3365# pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3366#endif
3367}
3368
3369/* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3370 view computation, and it refers to a view identifier for which we
3371 will not emit a label because it is known to map to a view number
3372 zero. We won't allocate the bitmap if we're not using assembler
3373 support for location views, but we have to make the variable
3374 visible for GGC and for code that will be optimized out for lack of
3375 support but that's still parsed and compiled. We could abstract it
3376 out with macros, but it's not worth it. */
3377static GTY(()) bitmap zero_view_p;
3378
3379/* Evaluate to TRUE iff N is known to identify the first location view
3380 at its PC. When not using assembler location view computation,
3381 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3382 and views label numbers recorded in it are the ones known to be
3383 zero. */
3384#define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3385 || (N) == (var_loc_view)-1 \
3386 || (zero_view_p \
3387 && bitmap_bit_p (zero_view_p, (N))))
3388
3389/* Return true iff we're to emit .loc directives for the assembler to
3390 generate line number sections.
3391
3392 When we're not emitting views, all we need from the assembler is
3393 support for .loc directives.
3394
3395 If we are emitting views, we can only use the assembler's .loc
3396 support if it also supports views.
3397
3398 When the compiler is emitting the line number programs and
3399 computing view numbers itself, it resets view numbers at known PC
3400 changes and counts from that, and then it emits view numbers as
3401 literal constants in locviewlists. There are cases in which the
3402 compiler is not sure about PC changes, e.g. when extra alignment is
3403 requested for a label. In these cases, the compiler may not reset
3404 the view counter, and the potential PC advance in the line number
3405 program will use an opcode that does not reset the view counter
3406 even if the PC actually changes, so that compiler and debug info
3407 consumer can keep view numbers in sync.
3408
3409 When the compiler defers view computation to the assembler, it
3410 emits symbolic view numbers in locviewlists, with the exception of
3411 views known to be zero (forced resets, or reset after
3412 compiler-visible PC changes): instead of emitting symbols for
3413 these, we emit literal zero and assert the assembler agrees with
3414 the compiler's assessment. We could use symbolic views everywhere,
3415 instead of special-casing zero views, but then we'd be unable to
3416 optimize out locviewlists that contain only zeros. */
3417
3418static bool
3419output_asm_line_debug_info (void)
3420{
3421 return (dwarf2out_as_loc_support
3422 && (dwarf2out_as_locview_support
3423 || !debug_variable_location_views));
3424}
3425
3426static bool asm_outputs_debug_line_str (void);
3427
3428/* Minimum line offset in a special line info. opcode.
3429 This value was chosen to give a reasonable range of values. */
3430#define DWARF_LINE_BASE -10
3431
3432/* First special line opcode - leave room for the standard opcodes. */
3433#define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3434
3435/* Range of line offsets in a special line info. opcode. */
3436#define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3437
3438/* Flag that indicates the initial value of the is_stmt_start flag.
3439 In the present implementation, we do not mark any lines as
3440 the beginning of a source statement, because that information
3441 is not made available by the GCC front-end. */
3442#define DWARF_LINE_DEFAULT_IS_STMT_START 1
3443
3444/* Maximum number of operations per instruction bundle. */
3445#ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3446#define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3447#endif
3448
3449/* This location is used by calc_die_sizes() to keep track
3450 the offset of each DIE within the .debug_info section. */
3451static unsigned long next_die_offset;
3452
3453/* Record the root of the DIE's built for the current compilation unit. */
3454static GTY(()) dw_die_ref single_comp_unit_die;
3455
3456/* A list of type DIEs that have been separated into comdat sections. */
3457static GTY(()) comdat_type_node *comdat_type_list;
3458
3459/* A list of CU DIEs that have been separated. */
3460static GTY(()) limbo_die_node *cu_die_list;
3461
3462/* A list of DIEs with a NULL parent waiting to be relocated. */
3463static GTY(()) limbo_die_node *limbo_die_list;
3464
3465/* A list of DIEs for which we may have to generate
3466 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3467static GTY(()) limbo_die_node *deferred_asm_name;
3468
3469struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3470{
3471 typedef const char *compare_type;
3472
3473 static hashval_t hash (dwarf_file_data *);
3474 static bool equal (dwarf_file_data *, const char *);
3475};
3476
3477/* Filenames referenced by this compilation unit. */
3478static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3479
3480struct decl_die_hasher : ggc_ptr_hash<die_node>
3481{
3482 typedef tree compare_type;
3483
3484 static hashval_t hash (die_node *);
3485 static bool equal (die_node *, tree);
3486};
3487/* A hash table of references to DIE's that describe declarations.
3488 The key is a DECL_UID() which is a unique number identifying each decl. */
3489static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3490
3491struct GTY ((for_user)) variable_value_struct {
3492 unsigned int decl_id;
3493 vec<dw_die_ref, va_gc> *dies;
3494};
3495
3496struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3497{
3498 typedef tree compare_type;
3499
3500 static hashval_t hash (variable_value_struct *);
3501 static bool equal (variable_value_struct *, tree);
3502};
3503/* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3504 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3505 DECL_CONTEXT of the referenced VAR_DECLs. */
3506static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3507
3508struct block_die_hasher : ggc_ptr_hash<die_struct>
3509{
3510 static hashval_t hash (die_struct *);
3511 static bool equal (die_struct *, die_struct *);
3512};
3513
3514/* A hash table of references to DIE's that describe COMMON blocks.
3515 The key is DECL_UID() ^ die_parent. */
3516static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3517
3518typedef struct GTY(()) die_arg_entry_struct {
3519 dw_die_ref die;
3520 tree arg;
3521} die_arg_entry;
3522
3523
3524/* Node of the variable location list. */
3525struct GTY ((chain_next ("%h.next"))) var_loc_node {
3526 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3527 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3528 in mode of the EXPR_LIST node and first EXPR_LIST operand
3529 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3530 location or NULL for padding. For larger bitsizes,
3531 mode is 0 and first operand is a CONCAT with bitsize
3532 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3533 NULL as second operand. */
3534 rtx GTY (()) loc;
3535 const char * GTY (()) label;
3536 struct var_loc_node * GTY (()) next;
3537 var_loc_view view;
3538};
3539
3540/* Variable location list. */
3541struct GTY ((for_user)) var_loc_list_def {
3542 struct var_loc_node * GTY (()) first;
3543
3544 /* Pointer to the last but one or last element of the
3545 chained list. If the list is empty, both first and
3546 last are NULL, if the list contains just one node
3547 or the last node certainly is not redundant, it points
3548 to the last node, otherwise points to the last but one.
3549 Do not mark it for GC because it is marked through the chain. */
3550 struct var_loc_node * GTY ((skip ("%h"))) last;
3551
3552 /* Pointer to the last element before section switch,
3553 if NULL, either sections weren't switched or first
3554 is after section switch. */
3555 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3556
3557 /* DECL_UID of the variable decl. */
3558 unsigned int decl_id;
3559};
3560typedef struct var_loc_list_def var_loc_list;
3561
3562/* Call argument location list. */
3563struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3564 rtx_insn * GTY (()) call_insn;
3565 const char * GTY (()) label;
3566 tree GTY (()) block;
3567 bool tail_call_p;
3568 rtx GTY (()) symbol_ref;
3569 struct call_arg_loc_node * GTY (()) next;
3570};
3571
3572
3573struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3574{
3575 typedef const_tree compare_type;
3576
3577 static hashval_t hash (var_loc_list *);
3578 static bool equal (var_loc_list *, const_tree);
3579};
3580
3581/* Table of decl location linked lists. */
3582static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3583
3584/* Head and tail of call_arg_loc chain. */
3585static GTY (()) struct call_arg_loc_node *call_arg_locations;
3586static struct call_arg_loc_node *call_arg_loc_last;
3587
3588/* Number of call sites in the current function. */
3589static int call_site_count = -1;
3590/* Number of tail call sites in the current function. */
3591static int tail_call_site_count = -1;
3592
3593/* A cached location list. */
3594struct GTY ((for_user)) cached_dw_loc_list_def {
3595 /* The DECL_UID of the decl that this entry describes. */
3596 unsigned int decl_id;
3597
3598 /* The cached location list. */
3599 dw_loc_list_ref loc_list;
3600};
3601typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3602
3603struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3604{
3605
3606 typedef const_tree compare_type;
3607
3608 static hashval_t hash (cached_dw_loc_list *);
3609 static bool equal (cached_dw_loc_list *, const_tree);
3610};
3611
3612/* Table of cached location lists. */
3613static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3614
3615/* A vector of references to DIE's that are uniquely identified by their tag,
3616 presence/absence of children DIE's, and list of attribute/value pairs. */
3617static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3618
3619/* A hash map to remember the stack usage for DWARF procedures. The value
3620 stored is the stack size difference between before the DWARF procedure
3621 invokation and after it returned. In other words, for a DWARF procedure
3622 that consumes N stack slots and that pushes M ones, this stores M - N. */
3623static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3624
3625/* A global counter for generating labels for line number data. */
3626static unsigned int line_info_label_num;
3627
3628/* The current table to which we should emit line number information
3629 for the current function. This will be set up at the beginning of
3630 assembly for the function. */
3631static GTY(()) dw_line_info_table *cur_line_info_table;
3632
3633/* The two default tables of line number info. */
3634static GTY(()) dw_line_info_table *text_section_line_info;
3635static GTY(()) dw_line_info_table *cold_text_section_line_info;
3636
3637/* The set of all non-default tables of line number info. */
3638static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3639
3640/* A flag to tell pubnames/types export if there is an info section to
3641 refer to. */
3642static bool info_section_emitted;
3643
3644/* A pointer to the base of a table that contains a list of publicly
3645 accessible names. */
3646static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3647
3648/* A pointer to the base of a table that contains a list of publicly
3649 accessible types. */
3650static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3651
3652/* A pointer to the base of a table that contains a list of macro
3653 defines/undefines (and file start/end markers). */
3654static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3655
3656/* True if .debug_macinfo or .debug_macros section is going to be
3657 emitted. */
3658#define have_macinfo \
3659 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3660 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3661 && !macinfo_table->is_empty ())
3662
3663/* Vector of dies for which we should generate .debug_ranges info. */
3664static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3665
3666/* Vector of pairs of labels referenced in ranges_table. */
3667static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3668
3669/* Whether we have location lists that need outputting */
3670static GTY(()) bool have_location_lists;
3671
3672/* Unique label counter. */
3673static GTY(()) unsigned int loclabel_num;
3674
3675/* Unique label counter for point-of-call tables. */
3676static GTY(()) unsigned int poc_label_num;
3677
3678/* The last file entry emitted by maybe_emit_file(). */
3679static GTY(()) struct dwarf_file_data * last_emitted_file;
3680
3681/* Number of internal labels generated by gen_internal_sym(). */
3682static GTY(()) int label_num;
3683
3684static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3685
3686/* Instances of generic types for which we need to generate debug
3687 info that describe their generic parameters and arguments. That
3688 generation needs to happen once all types are properly laid out so
3689 we do it at the end of compilation. */
3690static GTY(()) vec<tree, va_gc> *generic_type_instances;
3691
3692/* Offset from the "steady-state frame pointer" to the frame base,
3693 within the current function. */
3694static poly_int64 frame_pointer_fb_offset;
3695static bool frame_pointer_fb_offset_valid;
3696
3697static vec<dw_die_ref> base_types;
3698
3699/* Flags to represent a set of attribute classes for attributes that represent
3700 a scalar value (bounds, pointers, ...). */
3701enum dw_scalar_form
3702{
3703 dw_scalar_form_constant = 0x01,
3704 dw_scalar_form_exprloc = 0x02,
3705 dw_scalar_form_reference = 0x04
3706};
3707
3708/* Forward declarations for functions defined in this file. */
3709
3710static bool is_pseudo_reg (const_rtx);
3711static tree type_main_variant (tree);
3712static bool is_tagged_type (const_tree);
3713static const char *dwarf_tag_name (unsigned);
3714static const char *dwarf_attr_name (unsigned);
3715static const char *dwarf_form_name (unsigned);
3716static tree decl_ultimate_origin (const_tree);
3717static tree decl_class_context (tree);
3718static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3719static inline unsigned int AT_index (dw_attr_node *);
3720static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3721static inline unsigned AT_flag (dw_attr_node *);
3722static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3723static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3724static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3725 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3726static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3727 unsigned int, unsigned char *);
3728static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3729static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3730static inline const char *AT_string (dw_attr_node *);
3731static enum dwarf_form AT_string_form (dw_attr_node *);
3732static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3733static void add_AT_specification (dw_die_ref, dw_die_ref);
3734static inline dw_die_ref AT_ref (dw_attr_node *);
3735static inline int AT_ref_external (dw_attr_node *);
3736static inline void set_AT_ref_external (dw_attr_node *, int);
3737static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3738static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3739 dw_loc_list_ref);
3740static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3741static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3742static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3743static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3744static void remove_addr_table_entry (addr_table_entry *);
3745static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3746static inline rtx AT_addr (dw_attr_node *);
3747static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3748static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *,
3749 int = 0);
3750static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3751static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3752static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3753 unsigned long, bool);
3754static inline const char *AT_lbl (dw_attr_node *);
3755static const char *get_AT_low_pc (dw_die_ref);
3756static bool is_c (void);
3757static bool is_cxx (void);
3758static bool is_cxx (const_tree);
3759static bool is_fortran (void);
3760static bool is_ada (void);
3761static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3762static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3763static void add_child_die (dw_die_ref, dw_die_ref);
3764static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3765static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3766static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3767static void equate_type_number_to_die (tree, dw_die_ref);
3768static var_loc_list *lookup_decl_loc (const_tree);
3769static void equate_decl_number_to_die (tree, dw_die_ref);
3770static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3771static void print_spaces (FILE *);
3772static void print_die (dw_die_ref, FILE *);
3773static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3774static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3775static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3776static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3777static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3778static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3779static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3780 struct md5_ctx *, int *);
3781struct checksum_attributes;
3782static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3783static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3784static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3785static void generate_type_signature (dw_die_ref, comdat_type_node *);
3786static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3787static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3788static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3789static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3790static bool is_type_die (dw_die_ref);
3791static inline bool is_template_instantiation (dw_die_ref);
3792static bool is_declaration_die (dw_die_ref);
3793static bool should_move_die_to_comdat (dw_die_ref);
3794static dw_die_ref clone_as_declaration (dw_die_ref);
3795static dw_die_ref clone_die (dw_die_ref);
3796static dw_die_ref clone_tree (dw_die_ref);
3797static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3798static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3799static void generate_skeleton_bottom_up (skeleton_chain_node *);
3800static dw_die_ref generate_skeleton (dw_die_ref);
3801static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3802 dw_die_ref,
3803 dw_die_ref);
3804static void break_out_comdat_types (dw_die_ref);
3805static void copy_decls_for_unworthy_types (dw_die_ref);
3806
3807static void add_sibling_attributes (dw_die_ref);
3808static void output_location_lists (dw_die_ref);
3809static int constant_size (unsigned HOST_WIDE_INT);
3810static unsigned long size_of_die (dw_die_ref);
3811static void calc_die_sizes (dw_die_ref);
3812static void calc_base_type_die_sizes (void);
3813static void mark_dies (dw_die_ref);
3814static void unmark_dies (dw_die_ref);
3815static void unmark_all_dies (dw_die_ref);
3816static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3817static unsigned long size_of_aranges (void);
3818static enum dwarf_form value_format (dw_attr_node *);
3819static void output_value_format (dw_attr_node *);
3820static void output_abbrev_section (void);
3821static void output_die_abbrevs (unsigned long, dw_die_ref);
3822static void output_die (dw_die_ref);
3823static void output_compilation_unit_header (enum dwarf_unit_type);
3824static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3825static void output_comdat_type_unit (comdat_type_node *, bool);
3826static const char *dwarf2_name (tree, int);
3827static void add_pubname (tree, dw_die_ref);
3828static void add_enumerator_pubname (const char *, dw_die_ref);
3829static void add_pubname_string (const char *, dw_die_ref);
3830static void add_pubtype (tree, dw_die_ref);
3831static void output_pubnames (vec<pubname_entry, va_gc> *);
3832static void output_aranges (void);
3833static unsigned int add_ranges (const_tree, bool = false);
3834static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3835 bool *, bool);
3836static void output_ranges (void);
3837static dw_line_info_table *new_line_info_table (void);
3838static void output_line_info (bool);
3839static void output_file_names (void);
3840static bool is_base_type (tree);
3841static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3842static int decl_quals (const_tree);
3843static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3844static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3845static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3846static unsigned int debugger_reg_number (const_rtx);
3847static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3848static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3849static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3850 enum var_init_status);
3851static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3852 enum var_init_status);
3853static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3854 enum var_init_status);
3855static bool is_based_loc (const_rtx);
3856static bool resolve_one_addr (rtx *);
3857static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3858 enum var_init_status);
3859static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3860 enum var_init_status);
3861struct loc_descr_context;
3862static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3863static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3864static dw_loc_list_ref loc_list_from_tree (tree, int,
3865 struct loc_descr_context *);
3866static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3867 struct loc_descr_context *);
3868static tree field_type (const_tree);
3869static unsigned int simple_type_align_in_bits (const_tree);
3870static unsigned int simple_decl_align_in_bits (const_tree);
3871static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3872struct vlr_context;
3873static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3874 HOST_WIDE_INT *);
3875static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3876 dw_loc_list_ref);
3877static void add_data_member_location_attribute (dw_die_ref, tree,
3878 struct vlr_context *);
3879static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3880static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3881static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3882static unsigned insert_float (const_rtx, unsigned char *);
3883static rtx rtl_for_decl_location (tree);
3884static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3885static bool tree_add_const_value_attribute (dw_die_ref, tree);
3886static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3887static void add_desc_attribute (dw_die_ref, tree);
3888static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3889static void add_comp_dir_attribute (dw_die_ref);
3890static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3891 struct loc_descr_context *);
3892static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3893 struct loc_descr_context *);
3894static void add_subscript_info (dw_die_ref, tree, bool);
3895static void add_byte_size_attribute (dw_die_ref, tree);
3896static void add_alignment_attribute (dw_die_ref, tree);
3897static void add_bit_offset_attribute (dw_die_ref, tree);
3898static void add_bit_size_attribute (dw_die_ref, tree);
3899static void add_prototyped_attribute (dw_die_ref, tree);
3900static void add_abstract_origin_attribute (dw_die_ref, tree);
3901static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3902static void add_src_coords_attributes (dw_die_ref, tree);
3903static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3904static void add_discr_value (dw_die_ref, dw_discr_value *);
3905static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3906static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3907static dw_die_ref scope_die_for (tree, dw_die_ref);
3908static inline bool local_scope_p (dw_die_ref);
3909static inline bool class_scope_p (dw_die_ref);
3910static inline bool class_or_namespace_scope_p (dw_die_ref);
3911static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3912static void add_calling_convention_attribute (dw_die_ref, tree);
3913static const char *type_tag (const_tree);
3914static tree member_declared_type (const_tree);
3915#if 0
3916static const char *decl_start_label (tree);
3917#endif
3918static void gen_array_type_die (tree, dw_die_ref);
3919static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3920#if 0
3921static void gen_entry_point_die (tree, dw_die_ref);
3922#endif
3923static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3924static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3925static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3926static void gen_unspecified_parameters_die (tree, dw_die_ref);
3927static void gen_formal_types_die (tree, dw_die_ref);
3928static void gen_subprogram_die (tree, dw_die_ref);
3929static void gen_variable_die (tree, tree, dw_die_ref);
3930static void gen_const_die (tree, dw_die_ref);
3931static void gen_label_die (tree, dw_die_ref);
3932static void gen_lexical_block_die (tree, dw_die_ref);
3933static void gen_inlined_subroutine_die (tree, dw_die_ref);
3934static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3935static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3936static dw_die_ref gen_compile_unit_die (const char *);
3937static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3938static void gen_member_die (tree, dw_die_ref);
3939static void gen_struct_or_union_type_die (tree, dw_die_ref,
3940 enum debug_info_usage);
3941static void gen_subroutine_type_die (tree, dw_die_ref);
3942static void gen_typedef_die (tree, dw_die_ref);
3943static void gen_type_die (tree, dw_die_ref, bool = false);
3944static void gen_block_die (tree, dw_die_ref);
3945static void decls_for_scope (tree, dw_die_ref, bool = true);
3946static bool is_naming_typedef_decl (const_tree);
3947static inline dw_die_ref get_context_die (tree);
3948static void gen_namespace_die (tree, dw_die_ref);
3949static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3950static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3951static dw_die_ref force_decl_die (tree);
3952static dw_die_ref force_type_die (tree);
3953static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3954static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3955static struct dwarf_file_data * lookup_filename (const char *);
3956static void retry_incomplete_types (void);
3957static void gen_type_die_for_member (tree, tree, dw_die_ref);
3958static void gen_generic_params_dies (tree);
3959static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3960 bool = false);
3961static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3962 bool = false);
3963static void splice_child_die (dw_die_ref, dw_die_ref);
3964static int file_info_cmp (const void *, const void *);
3965static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3966 const char *, var_loc_view, const char *);
3967static void output_loc_list (dw_loc_list_ref);
3968static char *gen_internal_sym (const char *);
3969static bool want_pubnames (void);
3970
3971static void prune_unmark_dies (dw_die_ref);
3972static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3973static void prune_unused_types_mark (dw_die_ref, int);
3974static void prune_unused_types_walk (dw_die_ref);
3975static void prune_unused_types_walk_attribs (dw_die_ref);
3976static void prune_unused_types_prune (dw_die_ref);
3977static void prune_unused_types (void);
3978static int maybe_emit_file (struct dwarf_file_data *fd);
3979static inline const char *AT_vms_delta1 (dw_attr_node *);
3980static inline const char *AT_vms_delta2 (dw_attr_node *);
3981#if VMS_DEBUGGING_INFO
3982static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3983 const char *, const char *);
3984#endif
3985static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3986static void gen_remaining_tmpl_value_param_die_attribute (void);
3987static bool generic_type_p (tree);
3988static void schedule_generic_params_dies_gen (tree t);
3989static void gen_scheduled_generic_parms_dies (void);
3990static void resolve_variable_values (void);
3991
3992static const char *comp_dir_string (void);
3993
3994static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3995
3996/* enum for tracking thread-local variables whose address is really an offset
3997 relative to the TLS pointer, which will need link-time relocation, but will
3998 not need relocation by the DWARF consumer. */
3999
4000enum dtprel_bool
4001{
4002 dtprel_false = 0,
4003 dtprel_true = 1
4004};
4005
4006/* Return the operator to use for an address of a variable. For dtprel_true, we
4007 use DW_OP_const*. For regular variables, which need both link-time
4008 relocation and consumer-level relocation (e.g., to account for shared objects
4009 loaded at a random address), we use DW_OP_addr*. */
4010
4011static inline enum dwarf_location_atom
4012dw_addr_op (enum dtprel_bool dtprel)
4013{
4014 if (dtprel == dtprel_true)
4015 return (dwarf_split_debug_info ? dwarf_OP (op: DW_OP_constx)
4016 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4017 else
4018 return dwarf_split_debug_info ? dwarf_OP (op: DW_OP_addrx) : DW_OP_addr;
4019}
4020
4021/* Return a pointer to a newly allocated address location description. If
4022 dwarf_split_debug_info is true, then record the address with the appropriate
4023 relocation. */
4024static inline dw_loc_descr_ref
4025new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4026{
4027 dw_loc_descr_ref ref = new_loc_descr (op: dw_addr_op (dtprel), oprnd1: 0, oprnd2: 0);
4028
4029 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4030 ref->dw_loc_oprnd1.v.val_addr = addr;
4031 ref->dw_loc_dtprel = dtprel;
4032 if (dwarf_split_debug_info)
4033 ref->dw_loc_oprnd1.val_entry
4034 = add_addr_table_entry (addr,
4035 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4036 else
4037 ref->dw_loc_oprnd1.val_entry = NULL;
4038
4039 return ref;
4040}
4041
4042/* Section names used to hold DWARF debugging information. */
4043
4044#ifndef DEBUG_INFO_SECTION
4045#define DEBUG_INFO_SECTION ".debug_info"
4046#endif
4047#ifndef DEBUG_DWO_INFO_SECTION
4048#define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4049#endif
4050#ifndef DEBUG_LTO_INFO_SECTION
4051#define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4052#endif
4053#ifndef DEBUG_LTO_DWO_INFO_SECTION
4054#define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4055#endif
4056#ifndef DEBUG_ABBREV_SECTION
4057#define DEBUG_ABBREV_SECTION ".debug_abbrev"
4058#endif
4059#ifndef DEBUG_LTO_ABBREV_SECTION
4060#define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4061#endif
4062#ifndef DEBUG_DWO_ABBREV_SECTION
4063#define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4064#endif
4065#ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4066#define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4067#endif
4068#ifndef DEBUG_ARANGES_SECTION
4069#define DEBUG_ARANGES_SECTION ".debug_aranges"
4070#endif
4071#ifndef DEBUG_ADDR_SECTION
4072#define DEBUG_ADDR_SECTION ".debug_addr"
4073#endif
4074#ifndef DEBUG_MACINFO_SECTION
4075#define DEBUG_MACINFO_SECTION ".debug_macinfo"
4076#endif
4077#ifndef DEBUG_LTO_MACINFO_SECTION
4078#define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4079#endif
4080#ifndef DEBUG_DWO_MACINFO_SECTION
4081#define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4082#endif
4083#ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4084#define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4085#endif
4086#ifndef DEBUG_MACRO_SECTION
4087#define DEBUG_MACRO_SECTION ".debug_macro"
4088#endif
4089#ifndef DEBUG_LTO_MACRO_SECTION
4090#define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4091#endif
4092#ifndef DEBUG_DWO_MACRO_SECTION
4093#define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4094#endif
4095#ifndef DEBUG_LTO_DWO_MACRO_SECTION
4096#define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4097#endif
4098#ifndef DEBUG_LINE_SECTION
4099#define DEBUG_LINE_SECTION ".debug_line"
4100#endif
4101#ifndef DEBUG_LTO_LINE_SECTION
4102#define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4103#endif
4104#ifndef DEBUG_DWO_LINE_SECTION
4105#define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4106#endif
4107#ifndef DEBUG_LTO_DWO_LINE_SECTION
4108#define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4109#endif
4110#ifndef DEBUG_LOC_SECTION
4111#define DEBUG_LOC_SECTION ".debug_loc"
4112#endif
4113#ifndef DEBUG_DWO_LOC_SECTION
4114#define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4115#endif
4116#ifndef DEBUG_LOCLISTS_SECTION
4117#define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4118#endif
4119#ifndef DEBUG_DWO_LOCLISTS_SECTION
4120#define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4121#endif
4122#ifndef DEBUG_PUBNAMES_SECTION
4123#define DEBUG_PUBNAMES_SECTION \
4124 ((debug_generate_pub_sections == 2) \
4125 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4126#endif
4127#ifndef DEBUG_PUBTYPES_SECTION
4128#define DEBUG_PUBTYPES_SECTION \
4129 ((debug_generate_pub_sections == 2) \
4130 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4131#endif
4132#ifndef DEBUG_STR_OFFSETS_SECTION
4133#define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4134#endif
4135#ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4136#define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4137#endif
4138#ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4139#define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4140#endif
4141#ifndef DEBUG_STR_SECTION
4142#define DEBUG_STR_SECTION ".debug_str"
4143#endif
4144#ifndef DEBUG_LTO_STR_SECTION
4145#define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4146#endif
4147#ifndef DEBUG_STR_DWO_SECTION
4148#define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4149#endif
4150#ifndef DEBUG_LTO_STR_DWO_SECTION
4151#define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4152#endif
4153#ifndef DEBUG_RANGES_SECTION
4154#define DEBUG_RANGES_SECTION ".debug_ranges"
4155#endif
4156#ifndef DEBUG_RNGLISTS_SECTION
4157#define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4158#endif
4159#ifndef DEBUG_DWO_RNGLISTS_SECTION
4160#define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4161#endif
4162#ifndef DEBUG_LINE_STR_SECTION
4163#define DEBUG_LINE_STR_SECTION ".debug_line_str"
4164#endif
4165#ifndef DEBUG_LTO_LINE_STR_SECTION
4166#define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4167#endif
4168
4169/* Section flags for .debug_str section. */
4170#define DEBUG_STR_SECTION_FLAGS \
4171 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4172 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4173 : SECTION_DEBUG)
4174
4175/* Section flags for .debug_str.dwo section. */
4176#define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4177
4178/* Attribute used to refer to the macro section. */
4179#define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4180 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4181
4182/* Labels we insert at beginning sections we can reference instead of
4183 the section names themselves. */
4184
4185#ifndef TEXT_SECTION_LABEL
4186#define TEXT_SECTION_LABEL "Ltext"
4187#endif
4188#ifndef COLD_TEXT_SECTION_LABEL
4189#define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4190#endif
4191#ifndef DEBUG_LINE_SECTION_LABEL
4192#define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4193#endif
4194#ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4195#define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4196#endif
4197#ifndef DEBUG_INFO_SECTION_LABEL
4198#define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4199#endif
4200#ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4201#define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4202#endif
4203#ifndef DEBUG_ABBREV_SECTION_LABEL
4204#define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4205#endif
4206#ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4207#define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4208#endif
4209#ifndef DEBUG_ADDR_SECTION_LABEL
4210#define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4211#endif
4212#ifndef DEBUG_LOC_SECTION_LABEL
4213#define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4214#endif
4215#ifndef DEBUG_RANGES_SECTION_LABEL
4216#define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4217#endif
4218#ifndef DEBUG_MACINFO_SECTION_LABEL
4219#define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4220#endif
4221#ifndef DEBUG_MACRO_SECTION_LABEL
4222#define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4223#endif
4224#define SKELETON_COMP_DIE_ABBREV 1
4225#define SKELETON_TYPE_DIE_ABBREV 2
4226
4227/* Definitions of defaults for formats and names of various special
4228 (artificial) labels which may be generated within this file (when the -g
4229 options is used and DWARF2_DEBUGGING_INFO is in effect.
4230 If necessary, these may be overridden from within the tm.h file, but
4231 typically, overriding these defaults is unnecessary. */
4232
4233static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4234static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4235static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4236static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4237static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4238static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4239static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4240static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4241static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4242static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4243static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4244static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4245static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4246static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4247static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4248
4249#ifndef TEXT_END_LABEL
4250#define TEXT_END_LABEL "Letext"
4251#endif
4252#ifndef COLD_END_LABEL
4253#define COLD_END_LABEL "Letext_cold"
4254#endif
4255#ifndef BLOCK_BEGIN_LABEL
4256#define BLOCK_BEGIN_LABEL "LBB"
4257#endif
4258#ifndef BLOCK_INLINE_ENTRY_LABEL
4259#define BLOCK_INLINE_ENTRY_LABEL "LBI"
4260#endif
4261#ifndef BLOCK_END_LABEL
4262#define BLOCK_END_LABEL "LBE"
4263#endif
4264#ifndef LINE_CODE_LABEL
4265#define LINE_CODE_LABEL "LM"
4266#endif
4267
4268
4269/* Return the root of the DIE's built for the current compilation unit. */
4270static dw_die_ref
4271comp_unit_die (void)
4272{
4273 if (!single_comp_unit_die)
4274 single_comp_unit_die = gen_compile_unit_die (NULL);
4275 return single_comp_unit_die;
4276}
4277
4278/* We allow a language front-end to designate a function that is to be
4279 called to "demangle" any name before it is put into a DIE. */
4280
4281static const char *(*demangle_name_func) (const char *);
4282
4283void
4284dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4285{
4286 demangle_name_func = func;
4287}
4288
4289/* Test if rtl node points to a pseudo register. */
4290
4291static inline bool
4292is_pseudo_reg (const_rtx rtl)
4293{
4294 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4295 || (GET_CODE (rtl) == SUBREG
4296 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4297}
4298
4299/* Return a reference to a type, with its const and volatile qualifiers
4300 removed. */
4301
4302static inline tree
4303type_main_variant (tree type)
4304{
4305 type = TYPE_MAIN_VARIANT (type);
4306
4307 /* ??? There really should be only one main variant among any group of
4308 variants of a given type (and all of the MAIN_VARIANT values for all
4309 members of the group should point to that one type) but sometimes the C
4310 front-end messes this up for array types, so we work around that bug
4311 here. */
4312 if (TREE_CODE (type) == ARRAY_TYPE)
4313 while (type != TYPE_MAIN_VARIANT (type))
4314 type = TYPE_MAIN_VARIANT (type);
4315
4316 return type;
4317}
4318
4319/* Return true if the given type node represents a tagged type. */
4320
4321static inline bool
4322is_tagged_type (const_tree type)
4323{
4324 enum tree_code code = TREE_CODE (type);
4325
4326 return (code == RECORD_TYPE || code == UNION_TYPE
4327 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4328}
4329
4330/* Set label to debug_info_section_label + die_offset of a DIE reference. */
4331
4332static void
4333get_ref_die_offset_label (char *label, dw_die_ref ref)
4334{
4335 sprintf (s: label, format: "%s+%ld", debug_info_section_label, ref->die_offset);
4336}
4337
4338/* Return die_offset of a DIE reference to a base type. */
4339
4340static unsigned long int
4341get_base_type_offset (dw_die_ref ref)
4342{
4343 if (ref->die_offset)
4344 return ref->die_offset;
4345 if (comp_unit_die ()->die_abbrev)
4346 {
4347 calc_base_type_die_sizes ();
4348 gcc_assert (ref->die_offset);
4349 }
4350 return ref->die_offset;
4351}
4352
4353/* Return die_offset of a DIE reference other than base type. */
4354
4355static unsigned long int
4356get_ref_die_offset (dw_die_ref ref)
4357{
4358 gcc_assert (ref->die_offset);
4359 return ref->die_offset;
4360}
4361
4362/* Convert a DIE tag into its string name. */
4363
4364static const char *
4365dwarf_tag_name (unsigned int tag)
4366{
4367 const char *name = get_DW_TAG_name (tag);
4368
4369 if (name != NULL)
4370 return name;
4371
4372 return "DW_TAG_<unknown>";
4373}
4374
4375/* Convert a DWARF attribute code into its string name. */
4376
4377static const char *
4378dwarf_attr_name (unsigned int attr)
4379{
4380 const char *name;
4381
4382 switch (attr)
4383 {
4384#if VMS_DEBUGGING_INFO
4385 case DW_AT_HP_prologue:
4386 return "DW_AT_HP_prologue";
4387#else
4388 case DW_AT_MIPS_loop_unroll_factor:
4389 return "DW_AT_MIPS_loop_unroll_factor";
4390#endif
4391
4392#if VMS_DEBUGGING_INFO
4393 case DW_AT_HP_epilogue:
4394 return "DW_AT_HP_epilogue";
4395#else
4396 case DW_AT_MIPS_stride:
4397 return "DW_AT_MIPS_stride";
4398#endif
4399 }
4400
4401 name = get_DW_AT_name (attr);
4402
4403 if (name != NULL)
4404 return name;
4405
4406 return "DW_AT_<unknown>";
4407}
4408
4409/* Convert a DWARF value form code into its string name. */
4410
4411static const char *
4412dwarf_form_name (unsigned int form)
4413{
4414 const char *name = get_DW_FORM_name (form);
4415
4416 if (name != NULL)
4417 return name;
4418
4419 return "DW_FORM_<unknown>";
4420}
4421
4422/* Determine the "ultimate origin" of a decl. The decl may be an inlined
4423 instance of an inlined instance of a decl which is local to an inline
4424 function, so we have to trace all of the way back through the origin chain
4425 to find out what sort of node actually served as the original seed for the
4426 given block. */
4427
4428static tree
4429decl_ultimate_origin (const_tree decl)
4430{
4431 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4432 return NULL_TREE;
4433
4434 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4435 we're trying to output the abstract instance of this function. */
4436 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4437 return NULL_TREE;
4438
4439 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4440 most distant ancestor, this should never happen. */
4441 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4442
4443 return DECL_ABSTRACT_ORIGIN (decl);
4444}
4445
4446/* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4447 of a virtual function may refer to a base class, so we check the 'this'
4448 parameter. */
4449
4450static tree
4451decl_class_context (tree decl)
4452{
4453 tree context = NULL_TREE;
4454
4455 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4456 context = DECL_CONTEXT (decl);
4457 else
4458 context = TYPE_MAIN_VARIANT
4459 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4460
4461 if (context && !TYPE_P (context))
4462 context = NULL_TREE;
4463
4464 return context;
4465}
4466
4467/* Add an attribute/value pair to a DIE. */
4468
4469static inline void
4470add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4471{
4472 /* Maybe this should be an assert? */
4473 if (die == NULL)
4474 return;
4475
4476 if (flag_checking)
4477 {
4478 /* Check we do not add duplicate attrs. Can't use get_AT here
4479 because that recurses to the specification/abstract origin DIE. */
4480 dw_attr_node *a;
4481 unsigned ix;
4482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4483 gcc_assert (a->dw_attr != attr->dw_attr);
4484 }
4485
4486 vec_safe_reserve (v&: die->die_attr, nelems: 1);
4487 vec_safe_push (v&: die->die_attr, obj: *attr);
4488}
4489
4490enum dw_val_class
4491AT_class (dw_attr_node *a)
4492{
4493 return a->dw_attr_val.val_class;
4494}
4495
4496/* Return the index for any attribute that will be referenced with a
4497 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4498 indices are stored in dw_attr_val.v.val_str for reference counting
4499 pruning. */
4500
4501static inline unsigned int
4502AT_index (dw_attr_node *a)
4503{
4504 if (AT_class (a) == dw_val_class_str)
4505 return a->dw_attr_val.v.val_str->index;
4506 else if (a->dw_attr_val.val_entry != NULL)
4507 return a->dw_attr_val.val_entry->index;
4508 return NOT_INDEXED;
4509}
4510
4511/* Add a flag value attribute to a DIE. */
4512
4513static inline void
4514add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4515{
4516 dw_attr_node attr;
4517
4518 attr.dw_attr = attr_kind;
4519 attr.dw_attr_val.val_class = dw_val_class_flag;
4520 attr.dw_attr_val.val_entry = NULL;
4521 attr.dw_attr_val.v.val_flag = flag;
4522 add_dwarf_attr (die, attr: &attr);
4523}
4524
4525static inline unsigned
4526AT_flag (dw_attr_node *a)
4527{
4528 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4529 return a->dw_attr_val.v.val_flag;
4530}
4531
4532/* Add a signed integer attribute value to a DIE. */
4533
4534static inline void
4535add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4536{
4537 dw_attr_node attr;
4538
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_const;
4541 attr.dw_attr_val.val_entry = NULL;
4542 attr.dw_attr_val.v.val_int = int_val;
4543 add_dwarf_attr (die, attr: &attr);
4544}
4545
4546HOST_WIDE_INT
4547AT_int (dw_attr_node *a)
4548{
4549 gcc_assert (a && (AT_class (a) == dw_val_class_const
4550 || AT_class (a) == dw_val_class_const_implicit));
4551 return a->dw_attr_val.v.val_int;
4552}
4553
4554/* Add an unsigned integer attribute value to a DIE. */
4555
4556static inline void
4557add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4558 unsigned HOST_WIDE_INT unsigned_val)
4559{
4560 dw_attr_node attr;
4561
4562 attr.dw_attr = attr_kind;
4563 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4564 attr.dw_attr_val.val_entry = NULL;
4565 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4566 add_dwarf_attr (die, attr: &attr);
4567}
4568
4569unsigned HOST_WIDE_INT
4570AT_unsigned (dw_attr_node *a)
4571{
4572 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4573 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4574 return a->dw_attr_val.v.val_unsigned;
4575}
4576
4577dw_wide_int *
4578alloc_dw_wide_int (const wide_int_ref &w)
4579{
4580 dw_wide_int *p
4581 = (dw_wide_int *) ggc_internal_alloc (s: sizeof (dw_wide_int)
4582 + ((w.get_len () - 1)
4583 * sizeof (HOST_WIDE_INT)));
4584 p->precision = w.get_precision ();
4585 p->len = w.get_len ();
4586 memcpy (dest: p->val, src: w.get_val (), n: p->len * sizeof (HOST_WIDE_INT));
4587 return p;
4588}
4589
4590/* Add an unsigned wide integer attribute value to a DIE. */
4591
4592static inline void
4593add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4594 const wide_int_ref &w)
4595{
4596 dw_attr_node attr;
4597
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4602 add_dwarf_attr (die, attr: &attr);
4603}
4604
4605/* Add an unsigned double integer attribute value to a DIE. */
4606
4607static inline void
4608add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4609 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4610{
4611 dw_attr_node attr;
4612
4613 attr.dw_attr = attr_kind;
4614 attr.dw_attr_val.val_class = dw_val_class_const_double;
4615 attr.dw_attr_val.val_entry = NULL;
4616 attr.dw_attr_val.v.val_double.high = high;
4617 attr.dw_attr_val.v.val_double.low = low;
4618 add_dwarf_attr (die, attr: &attr);
4619}
4620
4621/* Add a floating point attribute value to a DIE and return it. */
4622
4623static inline void
4624add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4625 unsigned int length, unsigned int elt_size, unsigned char *array)
4626{
4627 dw_attr_node attr;
4628
4629 attr.dw_attr = attr_kind;
4630 attr.dw_attr_val.val_class = dw_val_class_vec;
4631 attr.dw_attr_val.val_entry = NULL;
4632 attr.dw_attr_val.v.val_vec.length = length;
4633 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4634 attr.dw_attr_val.v.val_vec.array = array;
4635 add_dwarf_attr (die, attr: &attr);
4636}
4637
4638/* Add an 8-byte data attribute value to a DIE. */
4639
4640static inline void
4641add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4642 unsigned char data8[8])
4643{
4644 dw_attr_node attr;
4645
4646 attr.dw_attr = attr_kind;
4647 attr.dw_attr_val.val_class = dw_val_class_data8;
4648 attr.dw_attr_val.val_entry = NULL;
4649 memcpy (dest: attr.dw_attr_val.v.val_data8, src: data8, n: 8);
4650 add_dwarf_attr (die, attr: &attr);
4651}
4652
4653/* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4654 dwarf_split_debug_info, address attributes in dies destined for the
4655 final executable have force_direct set to avoid using indexed
4656 references. */
4657
4658static inline void
4659add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4660 bool force_direct)
4661{
4662 dw_attr_node attr;
4663 char * lbl_id;
4664
4665 lbl_id = xstrdup (lbl_low);
4666 attr.dw_attr = DW_AT_low_pc;
4667 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4668 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4669 if (dwarf_split_debug_info && !force_direct)
4670 attr.dw_attr_val.val_entry
4671 = add_addr_table_entry (lbl_id, ate_kind_label);
4672 else
4673 attr.dw_attr_val.val_entry = NULL;
4674 add_dwarf_attr (die, attr: &attr);
4675
4676 attr.dw_attr = DW_AT_high_pc;
4677 if (dwarf_version < 4)
4678 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4679 else
4680 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4681 lbl_id = xstrdup (lbl_high);
4682 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4683 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4684 && dwarf_split_debug_info && !force_direct)
4685 attr.dw_attr_val.val_entry
4686 = add_addr_table_entry (lbl_id, ate_kind_label);
4687 else
4688 attr.dw_attr_val.val_entry = NULL;
4689 add_dwarf_attr (die, attr: &attr);
4690}
4691
4692/* Hash and equality functions for debug_str_hash. */
4693
4694hashval_t
4695indirect_string_hasher::hash (indirect_string_node *x)
4696{
4697 return htab_hash_string (x->str);
4698}
4699
4700bool
4701indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4702{
4703 return strcmp (s1: x1->str, s2: x2) == 0;
4704}
4705
4706/* Add STR to the given string hash table. */
4707
4708static struct indirect_string_node *
4709find_AT_string_in_table (const char *str,
4710 hash_table<indirect_string_hasher> *table,
4711 enum insert_option insert = INSERT)
4712{
4713 struct indirect_string_node *node;
4714
4715 indirect_string_node **slot
4716 = table->find_slot_with_hash (comparable: str, hash: htab_hash_string (str), insert);
4717 if (*slot == NULL)
4718 {
4719 node = ggc_cleared_alloc<indirect_string_node> ();
4720 node->str = ggc_strdup (str);
4721 *slot = node;
4722 }
4723 else
4724 node = *slot;
4725
4726 node->refcount++;
4727 return node;
4728}
4729
4730/* Add STR to the indirect string hash table. */
4731
4732static struct indirect_string_node *
4733find_AT_string (const char *str, enum insert_option insert = INSERT)
4734{
4735 if (! debug_str_hash)
4736 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (n: 10);
4737
4738 return find_AT_string_in_table (str, table: debug_str_hash, insert);
4739}
4740
4741/* Add a string attribute value to a DIE. */
4742
4743static inline void
4744add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4745{
4746 dw_attr_node attr;
4747 struct indirect_string_node *node;
4748
4749 node = find_AT_string (str);
4750
4751 attr.dw_attr = attr_kind;
4752 attr.dw_attr_val.val_class = dw_val_class_str;
4753 attr.dw_attr_val.val_entry = NULL;
4754 attr.dw_attr_val.v.val_str = node;
4755 add_dwarf_attr (die, attr: &attr);
4756}
4757
4758static inline const char *
4759AT_string (dw_attr_node *a)
4760{
4761 gcc_assert (a && AT_class (a) == dw_val_class_str);
4762 return a->dw_attr_val.v.val_str->str;
4763}
4764
4765/* Call this function directly to bypass AT_string_form's logic to put
4766 the string inline in the die. */
4767
4768static void
4769set_indirect_string (struct indirect_string_node *node)
4770{
4771 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4772 /* Already indirect is a no op. */
4773 if (node->form == DW_FORM_strp
4774 || node->form == DW_FORM_line_strp
4775 || node->form == dwarf_FORM (form: DW_FORM_strx))
4776 {
4777 gcc_assert (node->label);
4778 return;
4779 }
4780 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4781 ++dw2_string_counter;
4782 node->label = xstrdup (label);
4783
4784 if (!dwarf_split_debug_info)
4785 {
4786 node->form = DW_FORM_strp;
4787 node->index = NOT_INDEXED;
4788 }
4789 else
4790 {
4791 node->form = dwarf_FORM (form: DW_FORM_strx);
4792 node->index = NO_INDEX_ASSIGNED;
4793 }
4794}
4795
4796/* A helper function for dwarf2out_finish, called to reset indirect
4797 string decisions done for early LTO dwarf output before fat object
4798 dwarf output. */
4799
4800int
4801reset_indirect_string (indirect_string_node **h, void *)
4802{
4803 struct indirect_string_node *node = *h;
4804 if (node->form == DW_FORM_strp
4805 || node->form == DW_FORM_line_strp
4806 || node->form == dwarf_FORM (form: DW_FORM_strx))
4807 {
4808 free (ptr: node->label);
4809 node->label = NULL;
4810 node->form = (dwarf_form) 0;
4811 node->index = 0;
4812 }
4813 return 1;
4814}
4815
4816/* Add a string representing a file or filepath attribute value to a DIE. */
4817
4818static inline void
4819add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 const char *str)
4821{
4822 if (! asm_outputs_debug_line_str ())
4823 add_AT_string (die, attr_kind, str);
4824 else
4825 {
4826 dw_attr_node attr;
4827 struct indirect_string_node *node;
4828
4829 if (!debug_line_str_hash)
4830 debug_line_str_hash
4831 = hash_table<indirect_string_hasher>::create_ggc (n: 10);
4832
4833 node = find_AT_string_in_table (str, table: debug_line_str_hash);
4834 set_indirect_string (node);
4835 node->form = DW_FORM_line_strp;
4836
4837 attr.dw_attr = attr_kind;
4838 attr.dw_attr_val.val_class = dw_val_class_str;
4839 attr.dw_attr_val.val_entry = NULL;
4840 attr.dw_attr_val.v.val_str = node;
4841 add_dwarf_attr (die, attr: &attr);
4842 }
4843}
4844
4845/* Find out whether a string should be output inline in DIE
4846 or out-of-line in .debug_str section. */
4847
4848static enum dwarf_form
4849find_string_form (struct indirect_string_node *node)
4850{
4851 unsigned int len;
4852
4853 if (node->form)
4854 return node->form;
4855
4856 len = strlen (s: node->str) + 1;
4857
4858 /* If the string is shorter or equal to the size of the reference, it is
4859 always better to put it inline. */
4860 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4861 return node->form = DW_FORM_string;
4862
4863 /* If we cannot expect the linker to merge strings in .debug_str
4864 section, only put it into .debug_str if it is worth even in this
4865 single module. */
4866 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4867 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4868 && (len - dwarf_offset_size) * node->refcount <= len))
4869 return node->form = DW_FORM_string;
4870
4871 set_indirect_string (node);
4872
4873 return node->form;
4874}
4875
4876/* Find out whether the string referenced from the attribute should be
4877 output inline in DIE or out-of-line in .debug_str section. */
4878
4879static enum dwarf_form
4880AT_string_form (dw_attr_node *a)
4881{
4882 gcc_assert (a && AT_class (a) == dw_val_class_str);
4883 return find_string_form (node: a->dw_attr_val.v.val_str);
4884}
4885
4886/* Add a DIE reference attribute value to a DIE. */
4887
4888static inline void
4889add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4890{
4891 dw_attr_node attr;
4892 gcc_checking_assert (targ_die != NULL);
4893 gcc_assert (targ_die != die
4894 || (attr_kind != DW_AT_abstract_origin
4895 && attr_kind != DW_AT_specification));
4896
4897 /* With LTO we can end up trying to reference something we didn't create
4898 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4899 if (targ_die == NULL)
4900 return;
4901
4902 attr.dw_attr = attr_kind;
4903 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4904 attr.dw_attr_val.val_entry = NULL;
4905 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4906 attr.dw_attr_val.v.val_die_ref.external = 0;
4907 add_dwarf_attr (die, attr: &attr);
4908}
4909
4910/* Change DIE reference REF to point to NEW_DIE instead. */
4911
4912static inline void
4913change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4914{
4915 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4916 ref->dw_attr_val.v.val_die_ref.die = new_die;
4917 ref->dw_attr_val.v.val_die_ref.external = 0;
4918}
4919
4920/* Add an AT_specification attribute to a DIE, and also make the back
4921 pointer from the specification to the definition. */
4922
4923static inline void
4924add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4925{
4926 add_AT_die_ref (die, attr_kind: DW_AT_specification, targ_die);
4927 gcc_assert (!targ_die->die_definition);
4928 targ_die->die_definition = die;
4929}
4930
4931static inline dw_die_ref
4932AT_ref (dw_attr_node *a)
4933{
4934 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4935 return a->dw_attr_val.v.val_die_ref.die;
4936}
4937
4938static inline int
4939AT_ref_external (dw_attr_node *a)
4940{
4941 if (a && AT_class (a) == dw_val_class_die_ref)
4942 return a->dw_attr_val.v.val_die_ref.external;
4943
4944 return 0;
4945}
4946
4947static inline void
4948set_AT_ref_external (dw_attr_node *a, int i)
4949{
4950 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4951 a->dw_attr_val.v.val_die_ref.external = i;
4952}
4953
4954/* Add a location description attribute value to a DIE. */
4955
4956static inline void
4957add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4958{
4959 dw_attr_node attr;
4960
4961 attr.dw_attr = attr_kind;
4962 attr.dw_attr_val.val_class = dw_val_class_loc;
4963 attr.dw_attr_val.val_entry = NULL;
4964 attr.dw_attr_val.v.val_loc = loc;
4965 add_dwarf_attr (die, attr: &attr);
4966}
4967
4968dw_loc_descr_ref
4969AT_loc (dw_attr_node *a)
4970{
4971 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4972 return a->dw_attr_val.v.val_loc;
4973}
4974
4975static inline void
4976add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4977{
4978 dw_attr_node attr;
4979
4980 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4981 return;
4982
4983 attr.dw_attr = attr_kind;
4984 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4985 attr.dw_attr_val.val_entry = NULL;
4986 attr.dw_attr_val.v.val_loc_list = loc_list;
4987 add_dwarf_attr (die, attr: &attr);
4988 have_location_lists = true;
4989}
4990
4991static inline dw_loc_list_ref
4992AT_loc_list (dw_attr_node *a)
4993{
4994 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4995 return a->dw_attr_val.v.val_loc_list;
4996}
4997
4998/* Add a view list attribute to DIE. It must have a DW_AT_location
4999 attribute, because the view list complements the location list. */
5000
5001static inline void
5002add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5003{
5004 dw_attr_node attr;
5005
5006 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5007 return;
5008
5009 attr.dw_attr = attr_kind;
5010 attr.dw_attr_val.val_class = dw_val_class_view_list;
5011 attr.dw_attr_val.val_entry = NULL;
5012 attr.dw_attr_val.v.val_view_list = die;
5013 add_dwarf_attr (die, attr: &attr);
5014 gcc_checking_assert (get_AT (die, DW_AT_location));
5015 gcc_assert (have_location_lists);
5016}
5017
5018/* Return a pointer to the location list referenced by the attribute.
5019 If the named attribute is a view list, look up the corresponding
5020 DW_AT_location attribute and return its location list. */
5021
5022static inline dw_loc_list_ref *
5023AT_loc_list_ptr (dw_attr_node *a)
5024{
5025 gcc_assert (a);
5026 switch (AT_class (a))
5027 {
5028 case dw_val_class_loc_list:
5029 return &a->dw_attr_val.v.val_loc_list;
5030 case dw_val_class_view_list:
5031 {
5032 dw_attr_node *l;
5033 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5034 if (!l)
5035 return NULL;
5036 gcc_checking_assert (l + 1 == a);
5037 return AT_loc_list_ptr (a: l);
5038 }
5039 default:
5040 gcc_unreachable ();
5041 }
5042}
5043
5044/* Return the location attribute value associated with a view list
5045 attribute value. */
5046
5047static inline dw_val_node *
5048view_list_to_loc_list_val_node (dw_val_node *val)
5049{
5050 gcc_assert (val->val_class == dw_val_class_view_list);
5051 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5052 if (!loc)
5053 return NULL;
5054 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5055 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5056 return &loc->dw_attr_val;
5057}
5058
5059struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5060{
5061 static hashval_t hash (addr_table_entry *);
5062 static bool equal (addr_table_entry *, addr_table_entry *);
5063};
5064
5065/* Table of entries into the .debug_addr section. */
5066
5067static GTY (()) hash_table<addr_hasher> *addr_index_table;
5068
5069/* Hash an address_table_entry. */
5070
5071hashval_t
5072addr_hasher::hash (addr_table_entry *a)
5073{
5074 inchash::hash hstate;
5075 switch (a->kind)
5076 {
5077 case ate_kind_rtx:
5078 hstate.add_int (v: 0);
5079 break;
5080 case ate_kind_rtx_dtprel:
5081 hstate.add_int (v: 1);
5082 break;
5083 case ate_kind_label:
5084 return htab_hash_string (a->addr.label);
5085 default:
5086 gcc_unreachable ();
5087 }
5088 inchash::add_rtx (a->addr.rtl, hstate);
5089 return hstate.end ();
5090}
5091
5092/* Determine equality for two address_table_entries. */
5093
5094bool
5095addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5096{
5097 if (a1->kind != a2->kind)
5098 return false;
5099 switch (a1->kind)
5100 {
5101 case ate_kind_rtx:
5102 case ate_kind_rtx_dtprel:
5103 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5104 case ate_kind_label:
5105 return strcmp (s1: a1->addr.label, s2: a2->addr.label) == 0;
5106 default:
5107 gcc_unreachable ();
5108 }
5109}
5110
5111/* Initialize an addr_table_entry. */
5112
5113void
5114init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5115{
5116 e->kind = kind;
5117 switch (kind)
5118 {
5119 case ate_kind_rtx:
5120 case ate_kind_rtx_dtprel:
5121 e->addr.rtl = (rtx) addr;
5122 break;
5123 case ate_kind_label:
5124 e->addr.label = (char *) addr;
5125 break;
5126 }
5127 e->refcount = 0;
5128 e->index = NO_INDEX_ASSIGNED;
5129}
5130
5131/* Add attr to the address table entry to the table. Defer setting an
5132 index until output time. */
5133
5134static addr_table_entry *
5135add_addr_table_entry (void *addr, enum ate_kind kind)
5136{
5137 addr_table_entry *node;
5138 addr_table_entry finder;
5139
5140 gcc_assert (dwarf_split_debug_info);
5141 if (! addr_index_table)
5142 addr_index_table = hash_table<addr_hasher>::create_ggc (n: 10);
5143 init_addr_table_entry (e: &finder, kind, addr);
5144 addr_table_entry **slot = addr_index_table->find_slot (value: &finder, insert: INSERT);
5145
5146 if (*slot == HTAB_EMPTY_ENTRY)
5147 {
5148 node = ggc_cleared_alloc<addr_table_entry> ();
5149 init_addr_table_entry (e: node, kind, addr);
5150 *slot = node;
5151 }
5152 else
5153 node = *slot;
5154
5155 node->refcount++;
5156 return node;
5157}
5158
5159/* Remove an entry from the addr table by decrementing its refcount.
5160 Strictly, decrementing the refcount would be enough, but the
5161 assertion that the entry is actually in the table has found
5162 bugs. */
5163
5164static void
5165remove_addr_table_entry (addr_table_entry *entry)
5166{
5167 gcc_assert (dwarf_split_debug_info && addr_index_table);
5168 /* After an index is assigned, the table is frozen. */
5169 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5170 entry->refcount--;
5171}
5172
5173/* Given a location list, remove all addresses it refers to from the
5174 address_table. */
5175
5176static void
5177remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5178{
5179 for (; descr; descr = descr->dw_loc_next)
5180 if (descr->dw_loc_oprnd1.val_entry != NULL)
5181 {
5182 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5183 remove_addr_table_entry (entry: descr->dw_loc_oprnd1.val_entry);
5184 }
5185}
5186
5187/* A helper function for dwarf2out_finish called through
5188 htab_traverse. Assign an addr_table_entry its index. All entries
5189 must be collected into the table when this function is called,
5190 because the indexing code relies on htab_traverse to traverse nodes
5191 in the same order for each run. */
5192
5193int
5194index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5195{
5196 addr_table_entry *node = *h;
5197
5198 /* Don't index unreferenced nodes. */
5199 if (node->refcount == 0)
5200 return 1;
5201
5202 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5203 node->index = *index;
5204 *index += 1;
5205
5206 return 1;
5207}
5208
5209/* Return the tag of a given DIE. */
5210
5211enum dwarf_tag
5212dw_get_die_tag (dw_die_ref die)
5213{
5214 return die->die_tag;
5215}
5216
5217/* Return a reference to the children list of a given DIE. */
5218
5219dw_die_ref
5220dw_get_die_child (dw_die_ref die)
5221{
5222 return die->die_child;
5223}
5224
5225/* Return a reference to the sibling of a given DIE. */
5226
5227dw_die_ref
5228dw_get_die_sib (dw_die_ref die)
5229{
5230 return die->die_sib;
5231}
5232
5233/* Add an address constant 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
5238static inline void
5239add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5240 bool force_direct)
5241{
5242 dw_attr_node attr;
5243
5244 attr.dw_attr = attr_kind;
5245 attr.dw_attr_val.val_class = dw_val_class_addr;
5246 attr.dw_attr_val.v.val_addr = addr;
5247 if (dwarf_split_debug_info && !force_direct)
5248 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, kind: ate_kind_rtx);
5249 else
5250 attr.dw_attr_val.val_entry = NULL;
5251 add_dwarf_attr (die, attr: &attr);
5252}
5253
5254/* Get the RTX from to an address DIE attribute. */
5255
5256static inline rtx
5257AT_addr (dw_attr_node *a)
5258{
5259 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5260 return a->dw_attr_val.v.val_addr;
5261}
5262
5263/* Add a file attribute value to a DIE. */
5264
5265static inline void
5266add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5267 struct dwarf_file_data *fd)
5268{
5269 dw_attr_node attr;
5270
5271 attr.dw_attr = attr_kind;
5272 attr.dw_attr_val.val_class = dw_val_class_file;
5273 attr.dw_attr_val.val_entry = NULL;
5274 attr.dw_attr_val.v.val_file = fd;
5275 add_dwarf_attr (die, attr: &attr);
5276}
5277
5278/* Get the dwarf_file_data from a file DIE attribute. */
5279
5280static inline struct dwarf_file_data *
5281AT_file (dw_attr_node *a)
5282{
5283 gcc_assert (a && (AT_class (a) == dw_val_class_file
5284 || AT_class (a) == dw_val_class_file_implicit));
5285 return a->dw_attr_val.v.val_file;
5286}
5287
5288#if VMS_DEBUGGING_INFO
5289/* Add a vms delta attribute value to a DIE. */
5290
5291static inline void
5292add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5293 const char *lbl1, const char *lbl2)
5294{
5295 dw_attr_node attr;
5296
5297 attr.dw_attr = attr_kind;
5298 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5299 attr.dw_attr_val.val_entry = NULL;
5300 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5301 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5302 add_dwarf_attr (die, &attr);
5303}
5304#endif
5305
5306/* Add a symbolic view identifier attribute value to a DIE. */
5307
5308static inline void
5309add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5310 const char *view_label)
5311{
5312 dw_attr_node attr;
5313
5314 attr.dw_attr = attr_kind;
5315 attr.dw_attr_val.val_class = dw_val_class_symview;
5316 attr.dw_attr_val.val_entry = NULL;
5317 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5318 add_dwarf_attr (die, attr: &attr);
5319}
5320
5321/* Add a label identifier attribute value to a DIE. */
5322
5323static inline void
5324add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5325 const char *lbl_id, int offset)
5326{
5327 dw_attr_node attr;
5328
5329 attr.dw_attr = attr_kind;
5330 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5331 attr.dw_attr_val.val_entry = NULL;
5332 if (!offset)
5333 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5334 else
5335 attr.dw_attr_val.v.val_lbl_id = xasprintf ("%s%+i", lbl_id, offset);
5336 if (dwarf_split_debug_info)
5337 attr.dw_attr_val.val_entry
5338 = add_addr_table_entry (addr: attr.dw_attr_val.v.val_lbl_id,
5339 kind: ate_kind_label);
5340 add_dwarf_attr (die, attr: &attr);
5341}
5342
5343/* Add a section offset attribute value to a DIE, an offset into the
5344 debug_line section. */
5345
5346static inline void
5347add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5348 const char *label)
5349{
5350 dw_attr_node attr;
5351
5352 attr.dw_attr = attr_kind;
5353 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5354 attr.dw_attr_val.val_entry = NULL;
5355 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5356 add_dwarf_attr (die, attr: &attr);
5357}
5358
5359/* Add a section offset attribute value to a DIE, an offset into the
5360 debug_macinfo section. */
5361
5362static inline void
5363add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5364 const char *label)
5365{
5366 dw_attr_node attr;
5367
5368 attr.dw_attr = attr_kind;
5369 attr.dw_attr_val.val_class = dw_val_class_macptr;
5370 attr.dw_attr_val.val_entry = NULL;
5371 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5372 add_dwarf_attr (die, attr: &attr);
5373}
5374
5375/* Add a range_list attribute value to a DIE. When using
5376 dwarf_split_debug_info, address attributes in dies destined for the
5377 final executable should be direct references--setting the parameter
5378 force_direct ensures this behavior. */
5379
5380#define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5381#define RELOCATED_OFFSET (NULL)
5382
5383static void
5384add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5385 long unsigned int offset, bool force_direct)
5386{
5387 dw_attr_node attr;
5388
5389 attr.dw_attr = attr_kind;
5390 attr.dw_attr_val.val_class = dw_val_class_range_list;
5391 /* For the range_list attribute, use val_entry to store whether the
5392 offset should follow split-debug-info or normal semantics. This
5393 value is read in output_range_list_offset. */
5394 if (dwarf_split_debug_info && !force_direct)
5395 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5396 else
5397 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5398 attr.dw_attr_val.v.val_offset = offset;
5399 add_dwarf_attr (die, attr: &attr);
5400}
5401
5402/* Return the start label of a delta attribute. */
5403
5404static inline const char *
5405AT_vms_delta1 (dw_attr_node *a)
5406{
5407 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5408 return a->dw_attr_val.v.val_vms_delta.lbl1;
5409}
5410
5411/* Return the end label of a delta attribute. */
5412
5413static inline const char *
5414AT_vms_delta2 (dw_attr_node *a)
5415{
5416 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5417 return a->dw_attr_val.v.val_vms_delta.lbl2;
5418}
5419
5420static inline const char *
5421AT_lbl (dw_attr_node *a)
5422{
5423 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5424 || AT_class (a) == dw_val_class_lineptr
5425 || AT_class (a) == dw_val_class_macptr
5426 || AT_class (a) == dw_val_class_loclistsptr
5427 || AT_class (a) == dw_val_class_high_pc));
5428 return a->dw_attr_val.v.val_lbl_id;
5429}
5430
5431/* Get the attribute of type attr_kind. */
5432
5433dw_attr_node *
5434get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5435{
5436 dw_attr_node *a;
5437 unsigned ix;
5438 dw_die_ref spec = NULL;
5439
5440 if (! die)
5441 return NULL;
5442
5443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5444 if (a->dw_attr == attr_kind)
5445 return a;
5446 else if (a->dw_attr == DW_AT_specification
5447 || a->dw_attr == DW_AT_abstract_origin)
5448 spec = AT_ref (a);
5449
5450 if (spec)
5451 return get_AT (die: spec, attr_kind);
5452
5453 return NULL;
5454}
5455
5456/* Returns the parent of the declaration of DIE. */
5457
5458dw_die_ref
5459dw_get_die_parent (dw_die_ref die)
5460{
5461 dw_die_ref t;
5462
5463 if (!die)
5464 return NULL;
5465
5466 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5467 || (t = get_AT_ref (die, DW_AT_specification)))
5468 die = t;
5469
5470 return die->die_parent;
5471}
5472
5473/* Return the "low pc" attribute value, typically associated with a subprogram
5474 DIE. Return null if the "low pc" attribute is either not present, or if it
5475 cannot be represented as an assembler label identifier. */
5476
5477static inline const char *
5478get_AT_low_pc (dw_die_ref die)
5479{
5480 dw_attr_node *a = get_AT (die, attr_kind: DW_AT_low_pc);
5481
5482 return a ? AT_lbl (a) : NULL;
5483}
5484
5485/* Return the value of the string attribute designated by ATTR_KIND, or
5486 NULL if it is not present. */
5487
5488const char *
5489get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5490{
5491 dw_attr_node *a = get_AT (die, attr_kind);
5492
5493 return a ? AT_string (a) : NULL;
5494}
5495
5496/* Return the value of the flag attribute designated by ATTR_KIND, or -1
5497 if it is not present. */
5498
5499int
5500get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5501{
5502 dw_attr_node *a = get_AT (die, attr_kind);
5503
5504 return a ? AT_flag (a) : 0;
5505}
5506
5507/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5508 if it is not present. */
5509
5510unsigned
5511get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5512{
5513 dw_attr_node *a = get_AT (die, attr_kind);
5514
5515 return a ? AT_unsigned (a) : 0;
5516}
5517
5518dw_die_ref
5519get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5520{
5521 dw_attr_node *a = get_AT (die, attr_kind);
5522
5523 return a ? AT_ref (a) : NULL;
5524}
5525
5526struct dwarf_file_data *
5527get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5528{
5529 dw_attr_node *a = get_AT (die, attr_kind);
5530
5531 return a ? AT_file (a) : NULL;
5532}
5533
5534/* Return TRUE if the language is C. */
5535
5536static inline bool
5537is_c (void)
5538{
5539 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5540
5541 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5542 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5543
5544
5545}
5546
5547/* Return TRUE if the language is C++. */
5548
5549static inline bool
5550is_cxx (void)
5551{
5552 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5553
5554 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5555 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5556}
5557
5558/* Return TRUE if DECL was created by the C++ frontend. */
5559
5560static bool
5561is_cxx (const_tree decl)
5562{
5563 if (in_lto_p)
5564 {
5565 const_tree context = get_ultimate_context (decl);
5566 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5567 return startswith (TRANSLATION_UNIT_LANGUAGE (context), prefix: "GNU C++");
5568 }
5569 return is_cxx ();
5570}
5571
5572/* Return TRUE if the language is Fortran. */
5573
5574static inline bool
5575is_fortran (void)
5576{
5577 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5578
5579 return (lang == DW_LANG_Fortran77
5580 || lang == DW_LANG_Fortran90
5581 || lang == DW_LANG_Fortran95
5582 || lang == DW_LANG_Fortran03
5583 || lang == DW_LANG_Fortran08);
5584}
5585
5586static inline bool
5587is_fortran (const_tree decl)
5588{
5589 if (in_lto_p)
5590 {
5591 const_tree context = get_ultimate_context (decl);
5592 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5593 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5594 s2: "GNU Fortran", n: 11) == 0
5595 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 s2: "GNU F77") == 0);
5597 }
5598 return is_fortran ();
5599}
5600
5601/* Return TRUE if the language is Rust.
5602 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5603
5604static inline bool
5605is_rust (void)
5606{
5607 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5608
5609 return lang == DW_LANG_Rust;
5610}
5611
5612/* Return TRUE if the language is Ada. */
5613
5614static inline bool
5615is_ada (void)
5616{
5617 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5618
5619 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5620}
5621
5622/* Return TRUE if the language is D. */
5623
5624static inline bool
5625is_dlang (void)
5626{
5627 unsigned int lang = get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language);
5628
5629 return lang == DW_LANG_D;
5630}
5631
5632/* Remove the specified attribute if present. Return TRUE if removal
5633 was successful. */
5634
5635static bool
5636remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5637{
5638 dw_attr_node *a;
5639 unsigned ix;
5640
5641 if (! die)
5642 return false;
5643
5644 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5645 if (a->dw_attr == attr_kind)
5646 {
5647 if (AT_class (a) == dw_val_class_str)
5648 if (a->dw_attr_val.v.val_str->refcount)
5649 a->dw_attr_val.v.val_str->refcount--;
5650
5651 /* vec::ordered_remove should help reduce the number of abbrevs
5652 that are needed. */
5653 die->die_attr->ordered_remove (ix);
5654 return true;
5655 }
5656 return false;
5657}
5658
5659/* Remove CHILD from its parent. PREV must have the property that
5660 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5661
5662static void
5663remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5664{
5665 gcc_assert (child->die_parent == prev->die_parent);
5666 gcc_assert (prev->die_sib == child);
5667 if (prev == child)
5668 {
5669 gcc_assert (child->die_parent->die_child == child);
5670 prev = NULL;
5671 }
5672 else
5673 prev->die_sib = child->die_sib;
5674 if (child->die_parent->die_child == child)
5675 child->die_parent->die_child = prev;
5676 child->die_sib = NULL;
5677}
5678
5679/* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5680 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5681
5682static void
5683replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5684{
5685 dw_die_ref parent = old_child->die_parent;
5686
5687 gcc_assert (parent == prev->die_parent);
5688 gcc_assert (prev->die_sib == old_child);
5689
5690 new_child->die_parent = parent;
5691 if (prev == old_child)
5692 {
5693 gcc_assert (parent->die_child == old_child);
5694 new_child->die_sib = new_child;
5695 }
5696 else
5697 {
5698 prev->die_sib = new_child;
5699 new_child->die_sib = old_child->die_sib;
5700 }
5701 if (old_child->die_parent->die_child == old_child)
5702 old_child->die_parent->die_child = new_child;
5703 old_child->die_sib = NULL;
5704}
5705
5706/* Move all children from OLD_PARENT to NEW_PARENT. */
5707
5708static void
5709move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5710{
5711 dw_die_ref c;
5712 new_parent->die_child = old_parent->die_child;
5713 old_parent->die_child = NULL;
5714 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5715}
5716
5717/* Remove child DIE whose die_tag is TAG. Do nothing if no child
5718 matches TAG. */
5719
5720static void
5721remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5722{
5723 dw_die_ref c;
5724
5725 c = die->die_child;
5726 if (c) do {
5727 dw_die_ref prev = c;
5728 c = c->die_sib;
5729 while (c->die_tag == tag)
5730 {
5731 remove_child_with_prev (child: c, prev);
5732 c->die_parent = NULL;
5733 /* Might have removed every child. */
5734 if (die->die_child == NULL)
5735 return;
5736 c = prev->die_sib;
5737 }
5738 } while (c != die->die_child);
5739}
5740
5741/* Add a CHILD_DIE as the last child of DIE. */
5742
5743static void
5744add_child_die (dw_die_ref die, dw_die_ref child_die)
5745{
5746 /* FIXME this should probably be an assert. */
5747 if (! die || ! child_die)
5748 return;
5749 gcc_assert (die != child_die);
5750
5751 child_die->die_parent = die;
5752 if (die->die_child)
5753 {
5754 child_die->die_sib = die->die_child->die_sib;
5755 die->die_child->die_sib = child_die;
5756 }
5757 else
5758 child_die->die_sib = child_die;
5759 die->die_child = child_die;
5760}
5761
5762/* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5763
5764static void
5765add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5766 dw_die_ref after_die)
5767{
5768 gcc_assert (die
5769 && child_die
5770 && after_die
5771 && die->die_child
5772 && die != child_die);
5773
5774 child_die->die_parent = die;
5775 child_die->die_sib = after_die->die_sib;
5776 after_die->die_sib = child_die;
5777 if (die->die_child == after_die)
5778 die->die_child = child_die;
5779}
5780
5781/* Unassociate CHILD from its parent, and make its parent be
5782 NEW_PARENT. */
5783
5784static void
5785reparent_child (dw_die_ref child, dw_die_ref new_parent)
5786{
5787 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5788 if (p->die_sib == child)
5789 {
5790 remove_child_with_prev (child, prev: p);
5791 break;
5792 }
5793 add_child_die (die: new_parent, child_die: child);
5794}
5795
5796/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5797 is the specification, to the end of PARENT's list of children.
5798 This is done by removing and re-adding it. */
5799
5800static void
5801splice_child_die (dw_die_ref parent, dw_die_ref child)
5802{
5803 /* We want the declaration DIE from inside the class, not the
5804 specification DIE at toplevel. */
5805 if (child->die_parent != parent)
5806 {
5807 dw_die_ref tmp = get_AT_ref (die: child, attr_kind: DW_AT_specification);
5808
5809 if (tmp)
5810 child = tmp;
5811 }
5812
5813 gcc_assert (child->die_parent == parent
5814 || (child->die_parent
5815 == get_AT_ref (parent, DW_AT_specification)));
5816
5817 reparent_child (child, new_parent: parent);
5818}
5819
5820/* Create and return a new die with TAG_VALUE as tag. */
5821
5822dw_die_ref
5823new_die_raw (enum dwarf_tag tag_value)
5824{
5825 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5826 die->die_tag = tag_value;
5827 return die;
5828}
5829
5830/* Create and return a new die with a parent of PARENT_DIE. If
5831 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5832 associated tree T must be supplied to determine parenthood
5833 later. */
5834
5835static inline dw_die_ref
5836new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5837{
5838 dw_die_ref die = new_die_raw (tag_value);
5839
5840 if (parent_die != NULL)
5841 add_child_die (die: parent_die, child_die: die);
5842 else
5843 {
5844 limbo_die_node *limbo_node;
5845
5846 /* No DIEs created after early dwarf should end up in limbo,
5847 because the limbo list should not persist past LTO
5848 streaming. */
5849 if (tag_value != DW_TAG_compile_unit
5850 /* These are allowed because they're generated while
5851 breaking out COMDAT units late. */
5852 && tag_value != DW_TAG_type_unit
5853 && tag_value != DW_TAG_skeleton_unit
5854 && !early_dwarf
5855 /* Allow nested functions to live in limbo because they will
5856 only temporarily live there, as decls_for_scope will fix
5857 them up. */
5858 && (TREE_CODE (t) != FUNCTION_DECL
5859 || !decl_function_context (t))
5860 /* Same as nested functions above but for types. Types that
5861 are local to a function will be fixed in
5862 decls_for_scope. */
5863 && (!RECORD_OR_UNION_TYPE_P (t)
5864 || !TYPE_CONTEXT (t)
5865 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5866 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5867 especially in the ltrans stage, but once we implement LTO
5868 dwarf streaming, we should remove this exception. */
5869 && !in_lto_p)
5870 {
5871 fprintf (stderr, format: "symbol ended up in limbo too late:");
5872 debug_generic_stmt (t);
5873 gcc_unreachable ();
5874 }
5875
5876 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5877 limbo_node->die = die;
5878 limbo_node->created_for = t;
5879 limbo_node->next = limbo_die_list;
5880 limbo_die_list = limbo_node;
5881 }
5882
5883 return die;
5884}
5885
5886/* Return the DIE associated with the given type specifier. */
5887
5888dw_die_ref
5889lookup_type_die (tree type)
5890{
5891 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5892 if (die && die->removed)
5893 {
5894 TYPE_SYMTAB_DIE (type) = NULL;
5895 TREE_ASM_WRITTEN (type) = 0;
5896 return NULL;
5897 }
5898 return die;
5899}
5900
5901/* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5902 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5903 anonymous type instead the one of the naming typedef. */
5904
5905static inline dw_die_ref
5906strip_naming_typedef (tree type, dw_die_ref type_die)
5907{
5908 if (type
5909 && TREE_CODE (type) == RECORD_TYPE
5910 && type_die
5911 && type_die->die_tag == DW_TAG_typedef
5912 && is_naming_typedef_decl (TYPE_NAME (type)))
5913 type_die = get_AT_ref (die: type_die, attr_kind: DW_AT_type);
5914 return type_die;
5915}
5916
5917/* Like lookup_type_die, but if type is an anonymous type named by a
5918 typedef[1], return the DIE of the anonymous type instead the one of
5919 the naming typedef. This is because in gen_typedef_die, we did
5920 equate the anonymous struct named by the typedef with the DIE of
5921 the naming typedef. So by default, lookup_type_die on an anonymous
5922 struct yields the DIE of the naming typedef.
5923
5924 [1]: Read the comment of is_naming_typedef_decl to learn about what
5925 a naming typedef is. */
5926
5927static inline dw_die_ref
5928lookup_type_die_strip_naming_typedef (tree type)
5929{
5930 dw_die_ref die = lookup_type_die (type);
5931 return strip_naming_typedef (type, type_die: die);
5932}
5933
5934/* Equate a DIE to a given type specifier. */
5935
5936static inline void
5937equate_type_number_to_die (tree type, dw_die_ref type_die)
5938{
5939 TYPE_SYMTAB_DIE (type) = type_die;
5940}
5941
5942static dw_die_ref maybe_create_die_with_external_ref (tree);
5943struct GTY(()) sym_off_pair
5944{
5945 const char * GTY((skip)) sym;
5946 unsigned HOST_WIDE_INT off;
5947};
5948static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5949
5950/* Returns a hash value for X (which really is a die_struct). */
5951
5952inline hashval_t
5953decl_die_hasher::hash (die_node *x)
5954{
5955 return (hashval_t) x->decl_id;
5956}
5957
5958/* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5959
5960inline bool
5961decl_die_hasher::equal (die_node *x, tree y)
5962{
5963 return (x->decl_id == DECL_UID (y));
5964}
5965
5966/* Return the DIE associated with a given declaration. */
5967
5968dw_die_ref
5969lookup_decl_die (tree decl)
5970{
5971 dw_die_ref *die = decl_die_table->find_slot_with_hash (comparable: decl, DECL_UID (decl),
5972 insert: NO_INSERT);
5973 if (!die)
5974 {
5975 if (in_lto_p)
5976 return maybe_create_die_with_external_ref (decl);
5977 return NULL;
5978 }
5979 if ((*die)->removed)
5980 {
5981 decl_die_table->clear_slot (slot: die);
5982 return NULL;
5983 }
5984 return *die;
5985}
5986
5987
5988/* Return the DIE associated with BLOCK. */
5989
5990static inline dw_die_ref
5991lookup_block_die (tree block)
5992{
5993 dw_die_ref die = BLOCK_DIE (block);
5994 if (!die && in_lto_p)
5995 return maybe_create_die_with_external_ref (block);
5996 return die;
5997}
5998
5999/* Associate DIE with BLOCK. */
6000
6001static inline void
6002equate_block_to_die (tree block, dw_die_ref die)
6003{
6004 BLOCK_DIE (block) = die;
6005}
6006#undef BLOCK_DIE
6007
6008
6009/* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6010 style reference. Return true if we found one refering to a DIE for
6011 DECL, otherwise return false. */
6012
6013static bool
6014dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6015 unsigned HOST_WIDE_INT *off)
6016{
6017 dw_die_ref die;
6018
6019 if (in_lto_p)
6020 {
6021 /* During WPA stage and incremental linking we use a hash-map
6022 to store the decl <-> label + offset map. */
6023 if (!external_die_map)
6024 return false;
6025 sym_off_pair *desc = external_die_map->get (k: decl);
6026 if (!desc)
6027 return false;
6028 *sym = desc->sym;
6029 *off = desc->off;
6030 return true;
6031 }
6032
6033 if (TREE_CODE (decl) == BLOCK)
6034 die = lookup_block_die (block: decl);
6035 else
6036 die = lookup_decl_die (decl);
6037 if (!die)
6038 return false;
6039
6040 /* Similar to get_ref_die_offset_label, but using the "correct"
6041 label. */
6042 *off = die->die_offset;
6043 while (die->die_parent)
6044 die = die->die_parent;
6045 /* For the containing CU DIE we compute a die_symbol in
6046 compute_comp_unit_symbol. */
6047 if (die->die_tag == DW_TAG_compile_unit)
6048 {
6049 gcc_assert (die->die_id.die_symbol != NULL);
6050 *sym = die->die_id.die_symbol;
6051 return true;
6052 }
6053 /* While we can gracefully handle running into say a type unit
6054 we don't really want and consider this a bug. */
6055 if (flag_checking)
6056 gcc_unreachable ();
6057 return false;
6058}
6059
6060/* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6061
6062static void
6063add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6064 const char *symbol, HOST_WIDE_INT offset)
6065{
6066 /* Create a fake DIE that contains the reference. Don't use
6067 new_die because we don't want to end up in the limbo list. */
6068 /* ??? We probably want to share these, thus put a ref to the DIE
6069 we create here to the external_die_map entry. */
6070 dw_die_ref ref = new_die_raw (tag_value: die->die_tag);
6071 ref->die_id.die_symbol = symbol;
6072 ref->die_offset = offset;
6073 ref->with_offset = 1;
6074 add_AT_die_ref (die, attr_kind, targ_die: ref);
6075}
6076
6077/* Create a DIE for DECL if required and add a reference to a DIE
6078 at SYMBOL + OFFSET which contains attributes dumped early. */
6079
6080static void
6081dwarf2out_register_external_die (tree decl, const char *sym,
6082 unsigned HOST_WIDE_INT off)
6083{
6084 if (debug_info_level == DINFO_LEVEL_NONE)
6085 return;
6086
6087 if (!external_die_map)
6088 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (size: 1000);
6089 gcc_checking_assert (!external_die_map->get (decl));
6090 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), .off: off };
6091 external_die_map->put (k: decl, v: p);
6092}
6093
6094/* If we have a registered external DIE for DECL return a new DIE for
6095 the concrete instance with an appropriate abstract origin. */
6096
6097static dw_die_ref
6098maybe_create_die_with_external_ref (tree decl)
6099{
6100 if (!external_die_map)
6101 return NULL;
6102 sym_off_pair *desc = external_die_map->get (k: decl);
6103 if (!desc)
6104 return NULL;
6105
6106 const char *sym = desc->sym;
6107 unsigned HOST_WIDE_INT off = desc->off;
6108 external_die_map->remove (k: decl);
6109
6110 in_lto_p = false;
6111 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6112 ? lookup_block_die (block: decl) : lookup_decl_die (decl));
6113 gcc_assert (!die);
6114 in_lto_p = true;
6115
6116 tree ctx;
6117 dw_die_ref parent = NULL;
6118 /* Need to lookup a DIE for the decls context - the containing
6119 function or translation unit. */
6120 if (TREE_CODE (decl) == BLOCK)
6121 {
6122 ctx = BLOCK_SUPERCONTEXT (decl);
6123 /* ??? We do not output DIEs for all scopes thus skip as
6124 many DIEs as needed. */
6125 while (TREE_CODE (ctx) == BLOCK
6126 && !lookup_block_die (block: ctx))
6127 ctx = BLOCK_SUPERCONTEXT (ctx);
6128 }
6129 else
6130 ctx = DECL_CONTEXT (decl);
6131 /* Peel types in the context stack. */
6132 while (ctx && TYPE_P (ctx))
6133 ctx = TYPE_CONTEXT (ctx);
6134 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6135 if (debug_info_level <= DINFO_LEVEL_TERSE)
6136 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6137 ctx = DECL_CONTEXT (ctx);
6138 if (ctx)
6139 {
6140 if (TREE_CODE (ctx) == BLOCK)
6141 parent = lookup_block_die (block: ctx);
6142 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6143 /* Keep the 1:1 association during WPA. */
6144 && !flag_wpa
6145 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6146 /* Otherwise all late annotations go to the main CU which
6147 imports the original CUs. */
6148 parent = comp_unit_die ();
6149 else if (TREE_CODE (ctx) == FUNCTION_DECL
6150 && TREE_CODE (decl) != FUNCTION_DECL
6151 && TREE_CODE (decl) != PARM_DECL
6152 && TREE_CODE (decl) != RESULT_DECL
6153 && TREE_CODE (decl) != BLOCK)
6154 /* Leave function local entities parent determination to when
6155 we process scope vars. */
6156 ;
6157 else
6158 parent = lookup_decl_die (decl: ctx);
6159 }
6160 else
6161 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6162 Handle this case gracefully by globalizing stuff. */
6163 parent = comp_unit_die ();
6164 /* Create a DIE "stub". */
6165 switch (TREE_CODE (decl))
6166 {
6167 case TRANSLATION_UNIT_DECL:
6168 {
6169 die = comp_unit_die ();
6170 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6171 to create a DIE for the original CUs. */
6172 return die;
6173 }
6174 case NAMESPACE_DECL:
6175 if (is_fortran (decl))
6176 die = new_die (tag_value: DW_TAG_module, parent_die: parent, t: decl);
6177 else
6178 die = new_die (tag_value: DW_TAG_namespace, parent_die: parent, t: decl);
6179 break;
6180 case FUNCTION_DECL:
6181 die = new_die (tag_value: DW_TAG_subprogram, parent_die: parent, t: decl);
6182 break;
6183 case VAR_DECL:
6184 die = new_die (tag_value: DW_TAG_variable, parent_die: parent, t: decl);
6185 break;
6186 case RESULT_DECL:
6187 die = new_die (tag_value: DW_TAG_variable, parent_die: parent, t: decl);
6188 break;
6189 case PARM_DECL:
6190 die = new_die (tag_value: DW_TAG_formal_parameter, parent_die: parent, t: decl);
6191 break;
6192 case CONST_DECL:
6193 die = new_die (tag_value: DW_TAG_constant, parent_die: parent, t: decl);
6194 break;
6195 case LABEL_DECL:
6196 die = new_die (tag_value: DW_TAG_label, parent_die: parent, t: decl);
6197 break;
6198 case BLOCK:
6199 die = new_die (tag_value: DW_TAG_lexical_block, parent_die: parent, t: decl);
6200 break;
6201 default:
6202 gcc_unreachable ();
6203 }
6204 if (TREE_CODE (decl) == BLOCK)
6205 equate_block_to_die (block: decl, die);
6206 else
6207 equate_decl_number_to_die (decl, die);
6208
6209 add_desc_attribute (die, decl);
6210
6211 /* Add a reference to the DIE providing early debug at $sym + off. */
6212 add_AT_external_die_ref (die, attr_kind: DW_AT_abstract_origin, symbol: sym, offset: off);
6213
6214 return die;
6215}
6216
6217/* Returns a hash value for X (which really is a var_loc_list). */
6218
6219inline hashval_t
6220decl_loc_hasher::hash (var_loc_list *x)
6221{
6222 return (hashval_t) x->decl_id;
6223}
6224
6225/* Return true if decl_id of var_loc_list X is the same as
6226 UID of decl *Y. */
6227
6228inline bool
6229decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6230{
6231 return (x->decl_id == DECL_UID (y));
6232}
6233
6234/* Return the var_loc list associated with a given declaration. */
6235
6236static inline var_loc_list *
6237lookup_decl_loc (const_tree decl)
6238{
6239 if (!decl_loc_table)
6240 return NULL;
6241 return decl_loc_table->find_with_hash (comparable: decl, DECL_UID (decl));
6242}
6243
6244/* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6245
6246inline hashval_t
6247dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6248{
6249 return (hashval_t) x->decl_id;
6250}
6251
6252/* Return true if decl_id of cached_dw_loc_list X is the same as
6253 UID of decl *Y. */
6254
6255inline bool
6256dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6257{
6258 return (x->decl_id == DECL_UID (y));
6259}
6260
6261/* Equate a DIE to a particular declaration. */
6262
6263static void
6264equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6265{
6266 unsigned int decl_id = DECL_UID (decl);
6267
6268 *decl_die_table->find_slot_with_hash (comparable: decl, hash: decl_id, insert: INSERT) = decl_die;
6269 decl_die->decl_id = decl_id;
6270}
6271
6272/* Return how many bits covers PIECE EXPR_LIST. */
6273
6274static HOST_WIDE_INT
6275decl_piece_bitsize (rtx piece)
6276{
6277 int ret = (int) GET_MODE (piece);
6278 if (ret)
6279 return ret;
6280 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6281 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6282 return INTVAL (XEXP (XEXP (piece, 0), 0));
6283}
6284
6285/* Return pointer to the location of location note in PIECE EXPR_LIST. */
6286
6287static rtx *
6288decl_piece_varloc_ptr (rtx piece)
6289{
6290 if ((int) GET_MODE (piece))
6291 return &XEXP (piece, 0);
6292 else
6293 return &XEXP (XEXP (piece, 0), 1);
6294}
6295
6296/* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6297 Next is the chain of following piece nodes. */
6298
6299static rtx_expr_list *
6300decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6301{
6302 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6303 return alloc_EXPR_LIST (bitsize, loc_note, next);
6304 else
6305 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6306 GEN_INT (bitsize),
6307 loc_note), next);
6308}
6309
6310/* Return rtx that should be stored into loc field for
6311 LOC_NOTE and BITPOS/BITSIZE. */
6312
6313static rtx
6314construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6315 HOST_WIDE_INT bitsize)
6316{
6317 if (bitsize != -1)
6318 {
6319 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6320 if (bitpos != 0)
6321 loc_note = decl_piece_node (NULL_RTX, bitsize: bitpos, next: loc_note);
6322 }
6323 return loc_note;
6324}
6325
6326/* This function either modifies location piece list *DEST in
6327 place (if SRC and INNER is NULL), or copies location piece list
6328 *SRC to *DEST while modifying it. Location BITPOS is modified
6329 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6330 not copied and if needed some padding around it is added.
6331 When modifying in place, DEST should point to EXPR_LIST where
6332 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6333 to the start of the whole list and INNER points to the EXPR_LIST
6334 where earlier pieces cover PIECE_BITPOS bits. */
6335
6336static void
6337adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6338 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6339 HOST_WIDE_INT bitsize, rtx loc_note)
6340{
6341 HOST_WIDE_INT diff;
6342 bool copy = inner != NULL;
6343
6344 if (copy)
6345 {
6346 /* First copy all nodes preceding the current bitpos. */
6347 while (src != inner)
6348 {
6349 *dest = decl_piece_node (loc_note: *decl_piece_varloc_ptr (piece: *src),
6350 bitsize: decl_piece_bitsize (piece: *src), NULL_RTX);
6351 dest = &XEXP (*dest, 1);
6352 src = &XEXP (*src, 1);
6353 }
6354 }
6355 /* Add padding if needed. */
6356 if (bitpos != piece_bitpos)
6357 {
6358 *dest = decl_piece_node (NULL_RTX, bitsize: bitpos - piece_bitpos,
6359 next: copy ? NULL_RTX : *dest);
6360 dest = &XEXP (*dest, 1);
6361 }
6362 else if (*dest && decl_piece_bitsize (piece: *dest) == bitsize)
6363 {
6364 gcc_assert (!copy);
6365 /* A piece with correct bitpos and bitsize already exist,
6366 just update the location for it and return. */
6367 *decl_piece_varloc_ptr (piece: *dest) = loc_note;
6368 return;
6369 }
6370 /* Add the piece that changed. */
6371 *dest = decl_piece_node (loc_note, bitsize, next: copy ? NULL_RTX : *dest);
6372 dest = &XEXP (*dest, 1);
6373 /* Skip over pieces that overlap it. */
6374 diff = bitpos - piece_bitpos + bitsize;
6375 if (!copy)
6376 src = dest;
6377 while (diff > 0 && *src)
6378 {
6379 rtx piece = *src;
6380 diff -= decl_piece_bitsize (piece);
6381 if (copy)
6382 src = &XEXP (piece, 1);
6383 else
6384 {
6385 *src = XEXP (piece, 1);
6386 free_EXPR_LIST_node (piece);
6387 }
6388 }
6389 /* Add padding if needed. */
6390 if (diff < 0 && *src)
6391 {
6392 if (!copy)
6393 dest = src;
6394 *dest = decl_piece_node (NULL_RTX, bitsize: -diff, next: copy ? NULL_RTX : *dest);
6395 dest = &XEXP (*dest, 1);
6396 }
6397 if (!copy)
6398 return;
6399 /* Finally copy all nodes following it. */
6400 while (*src)
6401 {
6402 *dest = decl_piece_node (loc_note: *decl_piece_varloc_ptr (piece: *src),
6403 bitsize: decl_piece_bitsize (piece: *src), NULL_RTX);
6404 dest = &XEXP (*dest, 1);
6405 src = &XEXP (*src, 1);
6406 }
6407}
6408
6409/* Add a variable location node to the linked list for DECL. */
6410
6411static struct var_loc_node *
6412add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6413{
6414 unsigned int decl_id;
6415 var_loc_list *temp;
6416 struct var_loc_node *loc = NULL;
6417 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6418
6419 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6420 {
6421 tree realdecl = DECL_DEBUG_EXPR (decl);
6422 if (handled_component_p (t: realdecl)
6423 || (TREE_CODE (realdecl) == MEM_REF
6424 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6425 {
6426 bool reverse;
6427 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6428 &bitsize, &reverse);
6429 if (!innerdecl
6430 || !DECL_P (innerdecl)
6431 || DECL_IGNORED_P (innerdecl)
6432 || TREE_STATIC (innerdecl)
6433 || bitsize == 0
6434 || bitpos + bitsize > 256)
6435 return NULL;
6436 decl = innerdecl;
6437 }
6438 }
6439
6440 decl_id = DECL_UID (decl);
6441 var_loc_list **slot
6442 = decl_loc_table->find_slot_with_hash (comparable: decl, hash: decl_id, insert: INSERT);
6443 if (*slot == NULL)
6444 {
6445 temp = ggc_cleared_alloc<var_loc_list> ();
6446 temp->decl_id = decl_id;
6447 *slot = temp;
6448 }
6449 else
6450 temp = *slot;
6451
6452 /* For PARM_DECLs try to keep around the original incoming value,
6453 even if that means we'll emit a zero-range .debug_loc entry. */
6454 if (temp->last
6455 && temp->first == temp->last
6456 && TREE_CODE (decl) == PARM_DECL
6457 && NOTE_P (temp->first->loc)
6458 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6459 && DECL_INCOMING_RTL (decl)
6460 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6461 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6462 == GET_CODE (DECL_INCOMING_RTL (decl))
6463 && prev_real_insn (as_a<rtx_insn *> (p: temp->first->loc)) == NULL_RTX
6464 && (bitsize != -1
6465 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6466 NOTE_VAR_LOCATION_LOC (loc_note))
6467 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6468 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6469 {
6470 loc = ggc_cleared_alloc<var_loc_node> ();
6471 temp->first->next = loc;
6472 temp->last = loc;
6473 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6474 }
6475 else if (temp->last)
6476 {
6477 struct var_loc_node *last = temp->last, *unused = NULL;
6478 rtx *piece_loc = NULL, last_loc_note;
6479 HOST_WIDE_INT piece_bitpos = 0;
6480 if (last->next)
6481 {
6482 last = last->next;
6483 gcc_assert (last->next == NULL);
6484 }
6485 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6486 {
6487 piece_loc = &last->loc;
6488 do
6489 {
6490 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (piece: *piece_loc);
6491 if (piece_bitpos + cur_bitsize > bitpos)
6492 break;
6493 piece_bitpos += cur_bitsize;
6494 piece_loc = &XEXP (*piece_loc, 1);
6495 }
6496 while (*piece_loc);
6497 }
6498 /* TEMP->LAST here is either pointer to the last but one or
6499 last element in the chained list, LAST is pointer to the
6500 last element. */
6501 if (label && strcmp (s1: last->label, s2: label) == 0 && last->view == view)
6502 {
6503 /* For SRA optimized variables if there weren't any real
6504 insns since last note, just modify the last node. */
6505 if (piece_loc != NULL)
6506 {
6507 adjust_piece_list (dest: piece_loc, NULL, NULL,
6508 bitpos, piece_bitpos, bitsize, loc_note);
6509 return NULL;
6510 }
6511 /* If the last note doesn't cover any instructions, remove it. */
6512 if (temp->last != last)
6513 {
6514 temp->last->next = NULL;
6515 unused = last;
6516 last = temp->last;
6517 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6518 }
6519 else
6520 {
6521 gcc_assert (temp->first == temp->last
6522 || (temp->first->next == temp->last
6523 && TREE_CODE (decl) == PARM_DECL));
6524 memset (s: temp->last, c: '\0', n: sizeof (*temp->last));
6525 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6526 return temp->last;
6527 }
6528 }
6529 if (bitsize == -1 && NOTE_P (last->loc))
6530 last_loc_note = last->loc;
6531 else if (piece_loc != NULL
6532 && *piece_loc != NULL_RTX
6533 && piece_bitpos == bitpos
6534 && decl_piece_bitsize (piece: *piece_loc) == bitsize)
6535 last_loc_note = *decl_piece_varloc_ptr (piece: *piece_loc);
6536 else
6537 last_loc_note = NULL_RTX;
6538 /* If the current location is the same as the end of the list,
6539 and either both or neither of the locations is uninitialized,
6540 we have nothing to do. */
6541 if (last_loc_note == NULL_RTX
6542 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6543 NOTE_VAR_LOCATION_LOC (loc_note)))
6544 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6545 != NOTE_VAR_LOCATION_STATUS (loc_note))
6546 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6547 == VAR_INIT_STATUS_UNINITIALIZED)
6548 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6549 == VAR_INIT_STATUS_UNINITIALIZED))))
6550 {
6551 /* Add LOC to the end of list and update LAST. If the last
6552 element of the list has been removed above, reuse its
6553 memory for the new node, otherwise allocate a new one. */
6554 if (unused)
6555 {
6556 loc = unused;
6557 memset (s: loc, c: '\0', n: sizeof (*loc));
6558 }
6559 else
6560 loc = ggc_cleared_alloc<var_loc_node> ();
6561 if (bitsize == -1 || piece_loc == NULL)
6562 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6563 else
6564 adjust_piece_list (dest: &loc->loc, src: &last->loc, inner: piece_loc,
6565 bitpos, piece_bitpos, bitsize, loc_note);
6566 last->next = loc;
6567 /* Ensure TEMP->LAST will point either to the new last but one
6568 element of the chain, or to the last element in it. */
6569 if (last != temp->last)
6570 temp->last = last;
6571 }
6572 else if (unused)
6573 ggc_free (unused);
6574 }
6575 else
6576 {
6577 loc = ggc_cleared_alloc<var_loc_node> ();
6578 temp->first = loc;
6579 temp->last = loc;
6580 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6581 }
6582 return loc;
6583}
6584
6585/* Keep track of the number of spaces used to indent the
6586 output of the debugging routines that print the structure of
6587 the DIE internal representation. */
6588static int print_indent;
6589
6590/* Indent the line the number of spaces given by print_indent. */
6591
6592static inline void
6593print_spaces (FILE *outfile)
6594{
6595 fprintf (stream: outfile, format: "%*s", print_indent, "");
6596}
6597
6598/* Print a type signature in hex. */
6599
6600static inline void
6601print_signature (FILE *outfile, char *sig)
6602{
6603 int i;
6604
6605 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6606 fprintf (stream: outfile, format: "%02x", sig[i] & 0xff);
6607}
6608
6609static inline void
6610print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6611{
6612 if (discr_value->pos)
6613 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6614 else
6615 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6616}
6617
6618static void print_loc_descr (dw_loc_descr_ref, FILE *);
6619
6620/* Print the value associated to the VAL DWARF value node to OUTFILE. If
6621 RECURSE, output location descriptor operations. */
6622
6623static void
6624print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6625{
6626 switch (val->val_class)
6627 {
6628 case dw_val_class_addr:
6629 fprintf (stream: outfile, format: "address");
6630 break;
6631 case dw_val_class_offset:
6632 fprintf (stream: outfile, format: "offset");
6633 break;
6634 case dw_val_class_loc:
6635 fprintf (stream: outfile, format: "location descriptor");
6636 if (val->v.val_loc == NULL)
6637 fprintf (stream: outfile, format: " -> <null>");
6638 else if (recurse)
6639 {
6640 fprintf (stream: outfile, format: ":\n");
6641 print_indent += 4;
6642 print_loc_descr (val->v.val_loc, outfile);
6643 print_indent -= 4;
6644 }
6645 else
6646 {
6647 if (flag_dump_noaddr || flag_dump_unnumbered)
6648 fprintf (stream: outfile, format: " #");
6649 else
6650 fprintf (stream: outfile, format: " (%p)", (void *) val->v.val_loc);
6651 }
6652 break;
6653 case dw_val_class_loc_list:
6654 fprintf (stream: outfile, format: "location list -> label:%s",
6655 val->v.val_loc_list->ll_symbol);
6656 break;
6657 case dw_val_class_view_list:
6658 val = view_list_to_loc_list_val_node (val);
6659 fprintf (stream: outfile, format: "location list with views -> labels:%s and %s",
6660 val->v.val_loc_list->ll_symbol,
6661 val->v.val_loc_list->vl_symbol);
6662 break;
6663 case dw_val_class_range_list:
6664 fprintf (stream: outfile, format: "range list");
6665 break;
6666 case dw_val_class_const:
6667 case dw_val_class_const_implicit:
6668 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6669 break;
6670 case dw_val_class_unsigned_const:
6671 case dw_val_class_unsigned_const_implicit:
6672 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6673 break;
6674 case dw_val_class_const_double:
6675 fprintf (stream: outfile, format: "constant (" HOST_WIDE_INT_PRINT_DEC","\
6676 HOST_WIDE_INT_PRINT_UNSIGNED")",
6677 val->v.val_double.high,
6678 val->v.val_double.low);
6679 break;
6680 case dw_val_class_wide_int:
6681 {
6682 int i = val->v.val_wide->get_len ();
6683 fprintf (stream: outfile, format: "constant (");
6684 gcc_assert (i > 0);
6685 if (val->v.val_wide->elt (i: i - 1) == 0)
6686 fprintf (stream: outfile, format: "0x");
6687 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_HEX,
6688 val->v.val_wide->elt (i: --i));
6689 while (--i >= 0)
6690 fprintf (stream: outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6691 val->v.val_wide->elt (i));
6692 fprintf (stream: outfile, format: ")");
6693 break;
6694 }
6695 case dw_val_class_vec:
6696 fprintf (stream: outfile, format: "floating-point or vector constant");
6697 break;
6698 case dw_val_class_flag:
6699 fprintf (stream: outfile, format: "%u", val->v.val_flag);
6700 break;
6701 case dw_val_class_die_ref:
6702 if (val->v.val_die_ref.die != NULL)
6703 {
6704 dw_die_ref die = val->v.val_die_ref.die;
6705
6706 if (die->comdat_type_p)
6707 {
6708 fprintf (stream: outfile, format: "die -> signature: ");
6709 print_signature (outfile,
6710 sig: die->die_id.die_type_node->signature);
6711 }
6712 else if (die->die_id.die_symbol)
6713 {
6714 fprintf (stream: outfile, format: "die -> label: %s", die->die_id.die_symbol);
6715 if (die->with_offset)
6716 fprintf (stream: outfile, format: " + %ld", die->die_offset);
6717 }
6718 else
6719 fprintf (stream: outfile, format: "die -> %ld", die->die_offset);
6720 if (flag_dump_noaddr || flag_dump_unnumbered)
6721 fprintf (stream: outfile, format: " #");
6722 else
6723 fprintf (stream: outfile, format: " (%p)", (void *) die);
6724 }
6725 else
6726 fprintf (stream: outfile, format: "die -> <null>");
6727 break;
6728 case dw_val_class_vms_delta:
6729 fprintf (stream: outfile, format: "delta: @slotcount(%s-%s)",
6730 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6731 break;
6732 case dw_val_class_symview:
6733 fprintf (stream: outfile, format: "view: %s", val->v.val_symbolic_view);
6734 break;
6735 case dw_val_class_lbl_id:
6736 case dw_val_class_lineptr:
6737 case dw_val_class_macptr:
6738 case dw_val_class_loclistsptr:
6739 case dw_val_class_high_pc:
6740 fprintf (stream: outfile, format: "label: %s", val->v.val_lbl_id);
6741 break;
6742 case dw_val_class_str:
6743 if (val->v.val_str->str != NULL)
6744 fprintf (stream: outfile, format: "\"%s\"", val->v.val_str->str);
6745 else
6746 fprintf (stream: outfile, format: "<null>");
6747 break;
6748 case dw_val_class_file:
6749 case dw_val_class_file_implicit:
6750 fprintf (stream: outfile, format: "\"%s\" (%d)", val->v.val_file->filename,
6751 val->v.val_file->emitted_number);
6752 break;
6753 case dw_val_class_data8:
6754 {
6755 int i;
6756
6757 for (i = 0; i < 8; i++)
6758 fprintf (stream: outfile, format: "%02x", val->v.val_data8[i]);
6759 break;
6760 }
6761 case dw_val_class_discr_value:
6762 print_discr_value (outfile, discr_value: &val->v.val_discr_value);
6763 break;
6764 case dw_val_class_discr_list:
6765 for (dw_discr_list_ref node = val->v.val_discr_list;
6766 node != NULL;
6767 node = node->dw_discr_next)
6768 {
6769 if (node->dw_discr_range)
6770 {
6771 fprintf (stream: outfile, format: " .. ");
6772 print_discr_value (outfile, discr_value: &node->dw_discr_lower_bound);
6773 print_discr_value (outfile, discr_value: &node->dw_discr_upper_bound);
6774 }
6775 else
6776 print_discr_value (outfile, discr_value: &node->dw_discr_lower_bound);
6777
6778 if (node->dw_discr_next != NULL)
6779 fprintf (stream: outfile, format: " | ");
6780 }
6781 default:
6782 break;
6783 }
6784}
6785
6786/* Likewise, for a DIE attribute. */
6787
6788static void
6789print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6790{
6791 print_dw_val (val: &a->dw_attr_val, recurse, outfile);
6792}
6793
6794
6795/* Print the list of operands in the LOC location description to OUTFILE. This
6796 routine is a debugging aid only. */
6797
6798static void
6799print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6800{
6801 dw_loc_descr_ref l = loc;
6802
6803 if (loc == NULL)
6804 {
6805 print_spaces (outfile);
6806 fprintf (stream: outfile, format: "<null>\n");
6807 return;
6808 }
6809
6810 for (l = loc; l != NULL; l = l->dw_loc_next)
6811 {
6812 print_spaces (outfile);
6813 if (flag_dump_noaddr || flag_dump_unnumbered)
6814 fprintf (stream: outfile, format: "#");
6815 else
6816 fprintf (stream: outfile, format: "(%p)", (void *) l);
6817 fprintf (stream: outfile, format: " %s",
6818 dwarf_stack_op_name (op: l->dw_loc_opc));
6819 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6820 {
6821 fprintf (stream: outfile, format: " ");
6822 print_dw_val (val: &l->dw_loc_oprnd1, recurse: false, outfile);
6823 }
6824 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6825 {
6826 fprintf (stream: outfile, format: ", ");
6827 print_dw_val (val: &l->dw_loc_oprnd2, recurse: false, outfile);
6828 }
6829 fprintf (stream: outfile, format: "\n");
6830 }
6831}
6832
6833/* Print the information associated with a given DIE, and its children.
6834 This routine is a debugging aid only. */
6835
6836static void
6837print_die (dw_die_ref die, FILE *outfile)
6838{
6839 dw_attr_node *a;
6840 dw_die_ref c;
6841 unsigned ix;
6842
6843 print_spaces (outfile);
6844 fprintf (stream: outfile, format: "DIE %4ld: %s ",
6845 die->die_offset, dwarf_tag_name (tag: die->die_tag));
6846 if (flag_dump_noaddr || flag_dump_unnumbered)
6847 fprintf (stream: outfile, format: "#\n");
6848 else
6849 fprintf (stream: outfile, format: "(%p)\n", (void*) die);
6850 print_spaces (outfile);
6851 fprintf (stream: outfile, format: " abbrev id: %lu", die->die_abbrev);
6852 fprintf (stream: outfile, format: " offset: %ld", die->die_offset);
6853 fprintf (stream: outfile, format: " mark: %d\n", die->die_mark);
6854
6855 if (die->comdat_type_p)
6856 {
6857 print_spaces (outfile);
6858 fprintf (stream: outfile, format: " signature: ");
6859 print_signature (outfile, sig: die->die_id.die_type_node->signature);
6860 fprintf (stream: outfile, format: "\n");
6861 }
6862
6863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6864 {
6865 print_spaces (outfile);
6866 fprintf (stream: outfile, format: " %s: ", dwarf_attr_name (attr: a->dw_attr));
6867
6868 print_attribute (a, recurse: true, outfile);
6869 fprintf (stream: outfile, format: "\n");
6870 }
6871
6872 if (die->die_child != NULL)
6873 {
6874 print_indent += 4;
6875 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6876 print_indent -= 4;
6877 }
6878 if (print_indent == 0)
6879 fprintf (stream: outfile, format: "\n");
6880}
6881
6882/* Print the list of operations in the LOC location description. */
6883
6884DEBUG_FUNCTION void
6885debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6886{
6887 print_loc_descr (loc, stderr);
6888}
6889
6890/* Print the information collected for a given DIE. */
6891
6892DEBUG_FUNCTION void
6893debug_dwarf_die (dw_die_ref die)
6894{
6895 print_die (die, stderr);
6896}
6897
6898DEBUG_FUNCTION void
6899debug (die_struct &ref)
6900{
6901 print_die (die: &ref, stderr);
6902}
6903
6904DEBUG_FUNCTION void
6905debug (die_struct *ptr)
6906{
6907 if (ptr)
6908 debug (ref&: *ptr);
6909 else
6910 fprintf (stderr, format: "<nil>\n");
6911}
6912
6913
6914/* Print all DWARF information collected for the compilation unit.
6915 This routine is a debugging aid only. */
6916
6917DEBUG_FUNCTION void
6918debug_dwarf (void)
6919{
6920 print_indent = 0;
6921 print_die (die: comp_unit_die (), stderr);
6922}
6923
6924/* Verify the DIE tree structure. */
6925
6926DEBUG_FUNCTION void
6927verify_die (dw_die_ref die)
6928{
6929 gcc_assert (!die->die_mark);
6930 if (die->die_parent == NULL
6931 && die->die_sib == NULL)
6932 return;
6933 /* Verify the die_sib list is cyclic. */
6934 dw_die_ref x = die;
6935 do
6936 {
6937 x->die_mark = 1;
6938 x = x->die_sib;
6939 }
6940 while (x && !x->die_mark);
6941 gcc_assert (x == die);
6942 x = die;
6943 do
6944 {
6945 /* Verify all dies have the same parent. */
6946 gcc_assert (x->die_parent == die->die_parent);
6947 if (x->die_child)
6948 {
6949 /* Verify the child has the proper parent and recurse. */
6950 gcc_assert (x->die_child->die_parent == x);
6951 verify_die (die: x->die_child);
6952 }
6953 x->die_mark = 0;
6954 x = x->die_sib;
6955 }
6956 while (x && x->die_mark);
6957}
6958
6959/* Sanity checks on DIEs. */
6960
6961static void
6962check_die (dw_die_ref die)
6963{
6964 unsigned ix;
6965 dw_attr_node *a;
6966 bool inline_found = false;
6967 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6968 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6969 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6970 {
6971 switch (a->dw_attr)
6972 {
6973 case DW_AT_inline:
6974 if (a->dw_attr_val.v.val_unsigned)
6975 inline_found = true;
6976 break;
6977 case DW_AT_location:
6978 ++n_location;
6979 break;
6980 case DW_AT_low_pc:
6981 ++n_low_pc;
6982 break;
6983 case DW_AT_high_pc:
6984 ++n_high_pc;
6985 break;
6986 case DW_AT_artificial:
6987 ++n_artificial;
6988 break;
6989 case DW_AT_decl_column:
6990 ++n_decl_column;
6991 break;
6992 case DW_AT_decl_line:
6993 ++n_decl_line;
6994 break;
6995 case DW_AT_decl_file:
6996 ++n_decl_file;
6997 break;
6998 default:
6999 break;
7000 }
7001 }
7002 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7003 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7004 {
7005 fprintf (stderr, format: "Duplicate attributes in DIE:\n");
7006 debug_dwarf_die (die);
7007 gcc_unreachable ();
7008 }
7009 if (inline_found)
7010 {
7011 /* A debugging information entry that is a member of an abstract
7012 instance tree [that has DW_AT_inline] should not contain any
7013 attributes which describe aspects of the subroutine which vary
7014 between distinct inlined expansions or distinct out-of-line
7015 expansions. */
7016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7017 gcc_assert (a->dw_attr != DW_AT_low_pc
7018 && a->dw_attr != DW_AT_high_pc
7019 && a->dw_attr != DW_AT_location
7020 && a->dw_attr != DW_AT_frame_base
7021 && a->dw_attr != DW_AT_call_all_calls
7022 && a->dw_attr != DW_AT_GNU_all_call_sites);
7023 }
7024}
7025
7026#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7027#define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7028#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7029
7030/* Calculate the checksum of a location expression. */
7031
7032static inline void
7033loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7034{
7035 int tem;
7036 inchash::hash hstate;
7037 hashval_t hash;
7038
7039 tem = (loc->dw_loc_dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7040 CHECKSUM (tem);
7041 hash_loc_operands (loc, hstate);
7042 hash = hstate.end();
7043 CHECKSUM (hash);
7044}
7045
7046/* Calculate the checksum of an attribute. */
7047
7048static void
7049attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7050{
7051 dw_loc_descr_ref loc;
7052 rtx r;
7053
7054 CHECKSUM (at->dw_attr);
7055
7056 /* We don't care that this was compiled with a different compiler
7057 snapshot; if the output is the same, that's what matters. */
7058 if (at->dw_attr == DW_AT_producer)
7059 return;
7060
7061 switch (AT_class (a: at))
7062 {
7063 case dw_val_class_const:
7064 case dw_val_class_const_implicit:
7065 CHECKSUM (at->dw_attr_val.v.val_int);
7066 break;
7067 case dw_val_class_unsigned_const:
7068 case dw_val_class_unsigned_const_implicit:
7069 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7070 break;
7071 case dw_val_class_const_double:
7072 CHECKSUM (at->dw_attr_val.v.val_double);
7073 break;
7074 case dw_val_class_wide_int:
7075 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7076 get_full_len (*at->dw_attr_val.v.val_wide)
7077 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7078 break;
7079 case dw_val_class_vec:
7080 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7081 (at->dw_attr_val.v.val_vec.length
7082 * at->dw_attr_val.v.val_vec.elt_size));
7083 break;
7084 case dw_val_class_flag:
7085 CHECKSUM (at->dw_attr_val.v.val_flag);
7086 break;
7087 case dw_val_class_str:
7088 CHECKSUM_STRING (AT_string (at));
7089 break;
7090
7091 case dw_val_class_addr:
7092 r = AT_addr (a: at);
7093 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7094 CHECKSUM_STRING (XSTR (r, 0));
7095 break;
7096
7097 case dw_val_class_offset:
7098 CHECKSUM (at->dw_attr_val.v.val_offset);
7099 break;
7100
7101 case dw_val_class_loc:
7102 for (loc = AT_loc (a: at); loc; loc = loc->dw_loc_next)
7103 loc_checksum (loc, ctx);
7104 break;
7105
7106 case dw_val_class_die_ref:
7107 die_checksum (AT_ref (a: at), ctx, mark);
7108 break;
7109
7110 case dw_val_class_fde_ref:
7111 case dw_val_class_vms_delta:
7112 case dw_val_class_symview:
7113 case dw_val_class_lbl_id:
7114 case dw_val_class_lineptr:
7115 case dw_val_class_macptr:
7116 case dw_val_class_loclistsptr:
7117 case dw_val_class_high_pc:
7118 break;
7119
7120 case dw_val_class_file:
7121 case dw_val_class_file_implicit:
7122 CHECKSUM_STRING (AT_file (at)->filename);
7123 break;
7124
7125 case dw_val_class_data8:
7126 CHECKSUM (at->dw_attr_val.v.val_data8);
7127 break;
7128
7129 default:
7130 break;
7131 }
7132}
7133
7134/* Calculate the checksum of a DIE. */
7135
7136static void
7137die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7138{
7139 dw_die_ref c;
7140 dw_attr_node *a;
7141 unsigned ix;
7142
7143 /* To avoid infinite recursion. */
7144 if (die->die_mark)
7145 {
7146 CHECKSUM (die->die_mark);
7147 return;
7148 }
7149 die->die_mark = ++(*mark);
7150
7151 CHECKSUM (die->die_tag);
7152
7153 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7154 attr_checksum (at: a, ctx, mark);
7155
7156 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7157}
7158
7159#undef CHECKSUM
7160#undef CHECKSUM_BLOCK
7161#undef CHECKSUM_STRING
7162
7163/* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7164#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7165#define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7166#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7167#define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7168#define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7169#define CHECKSUM_ATTR(FOO) \
7170 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7171
7172/* Calculate the checksum of a number in signed LEB128 format. */
7173
7174static void
7175checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7176{
7177 unsigned char byte;
7178 bool more;
7179
7180 while (1)
7181 {
7182 byte = (value & 0x7f);
7183 value >>= 7;
7184 more = !((value == 0 && (byte & 0x40) == 0)
7185 || (value == -1 && (byte & 0x40) != 0));
7186 if (more)
7187 byte |= 0x80;
7188 CHECKSUM (byte);
7189 if (!more)
7190 break;
7191 }
7192}
7193
7194/* Calculate the checksum of a number in unsigned LEB128 format. */
7195
7196static void
7197checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7198{
7199 while (1)
7200 {
7201 unsigned char byte = (value & 0x7f);
7202 value >>= 7;
7203 if (value != 0)
7204 /* More bytes to follow. */
7205 byte |= 0x80;
7206 CHECKSUM (byte);
7207 if (value == 0)
7208 break;
7209 }
7210}
7211
7212/* Checksum the context of the DIE. This adds the names of any
7213 surrounding namespaces or structures to the checksum. */
7214
7215static void
7216checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7217{
7218 const char *name;
7219 dw_die_ref spec;
7220 int tag = die->die_tag;
7221
7222 if (tag != DW_TAG_namespace
7223 && tag != DW_TAG_structure_type
7224 && tag != DW_TAG_class_type)
7225 return;
7226
7227 name = get_AT_string (die, attr_kind: DW_AT_name);
7228
7229 spec = get_AT_ref (die, attr_kind: DW_AT_specification);
7230 if (spec != NULL)
7231 die = spec;
7232
7233 if (die->die_parent != NULL)
7234 checksum_die_context (die: die->die_parent, ctx);
7235
7236 CHECKSUM_ULEB128 ('C');
7237 CHECKSUM_ULEB128 (tag);
7238 if (name != NULL)
7239 CHECKSUM_STRING (name);
7240}
7241
7242/* Calculate the checksum of a location expression. */
7243
7244static inline void
7245loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7246{
7247 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7248 were emitted as a DW_FORM_sdata instead of a location expression. */
7249 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7250 {
7251 CHECKSUM_ULEB128 (DW_FORM_sdata);
7252 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7253 return;
7254 }
7255
7256 /* Otherwise, just checksum the raw location expression. */
7257 while (loc != NULL)
7258 {
7259 inchash::hash hstate;
7260 hashval_t hash;
7261
7262 CHECKSUM_ULEB128 (loc->dw_loc_dtprel);
7263 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7264 hash_loc_operands (loc, hstate);
7265 hash = hstate.end ();
7266 CHECKSUM (hash);
7267 loc = loc->dw_loc_next;
7268 }
7269}
7270
7271/* Calculate the checksum of an attribute. */
7272
7273static void
7274attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7275 struct md5_ctx *ctx, int *mark)
7276{
7277 dw_loc_descr_ref loc;
7278 rtx r;
7279
7280 if (AT_class (a: at) == dw_val_class_die_ref)
7281 {
7282 dw_die_ref target_die = AT_ref (a: at);
7283
7284 /* For pointer and reference types, we checksum only the (qualified)
7285 name of the target type (if there is a name). For friend entries,
7286 we checksum only the (qualified) name of the target type or function.
7287 This allows the checksum to remain the same whether the target type
7288 is complete or not. */
7289 if ((at->dw_attr == DW_AT_type
7290 && (tag == DW_TAG_pointer_type
7291 || tag == DW_TAG_reference_type
7292 || tag == DW_TAG_rvalue_reference_type
7293 || tag == DW_TAG_ptr_to_member_type))
7294 || (at->dw_attr == DW_AT_friend
7295 && tag == DW_TAG_friend))
7296 {
7297 dw_attr_node *name_attr = get_AT (die: target_die, attr_kind: DW_AT_name);
7298
7299 if (name_attr != NULL)
7300 {
7301 dw_die_ref decl = get_AT_ref (die: target_die, attr_kind: DW_AT_specification);
7302
7303 if (decl == NULL)
7304 decl = target_die;
7305 CHECKSUM_ULEB128 ('N');
7306 CHECKSUM_ULEB128 (at->dw_attr);
7307 if (decl->die_parent != NULL)
7308 checksum_die_context (die: decl->die_parent, ctx);
7309 CHECKSUM_ULEB128 ('E');
7310 CHECKSUM_STRING (AT_string (name_attr));
7311 return;
7312 }
7313 }
7314
7315 /* For all other references to another DIE, we check to see if the
7316 target DIE has already been visited. If it has, we emit a
7317 backward reference; if not, we descend recursively. */
7318 if (target_die->die_mark > 0)
7319 {
7320 CHECKSUM_ULEB128 ('R');
7321 CHECKSUM_ULEB128 (at->dw_attr);
7322 CHECKSUM_ULEB128 (target_die->die_mark);
7323 }
7324 else
7325 {
7326 dw_die_ref decl = get_AT_ref (die: target_die, attr_kind: DW_AT_specification);
7327
7328 if (decl == NULL)
7329 decl = target_die;
7330 target_die->die_mark = ++(*mark);
7331 CHECKSUM_ULEB128 ('T');
7332 CHECKSUM_ULEB128 (at->dw_attr);
7333 if (decl->die_parent != NULL)
7334 checksum_die_context (die: decl->die_parent, ctx);
7335 die_checksum_ordered (target_die, ctx, mark);
7336 }
7337 return;
7338 }
7339
7340 CHECKSUM_ULEB128 ('A');
7341 CHECKSUM_ULEB128 (at->dw_attr);
7342
7343 switch (AT_class (a: at))
7344 {
7345 case dw_val_class_const:
7346 case dw_val_class_const_implicit:
7347 CHECKSUM_ULEB128 (DW_FORM_sdata);
7348 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7349 break;
7350
7351 case dw_val_class_unsigned_const:
7352 case dw_val_class_unsigned_const_implicit:
7353 CHECKSUM_ULEB128 (DW_FORM_sdata);
7354 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7355 break;
7356
7357 case dw_val_class_const_double:
7358 CHECKSUM_ULEB128 (DW_FORM_block);
7359 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7360 CHECKSUM (at->dw_attr_val.v.val_double);
7361 break;
7362
7363 case dw_val_class_wide_int:
7364 CHECKSUM_ULEB128 (DW_FORM_block);
7365 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7366 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7367 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7368 get_full_len (*at->dw_attr_val.v.val_wide)
7369 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7370 break;
7371
7372 case dw_val_class_vec:
7373 CHECKSUM_ULEB128 (DW_FORM_block);
7374 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7375 * at->dw_attr_val.v.val_vec.elt_size);
7376 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7377 (at->dw_attr_val.v.val_vec.length
7378 * at->dw_attr_val.v.val_vec.elt_size));
7379 break;
7380
7381 case dw_val_class_flag:
7382 CHECKSUM_ULEB128 (DW_FORM_flag);
7383 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7384 break;
7385
7386 case dw_val_class_str:
7387 CHECKSUM_ULEB128 (DW_FORM_string);
7388 CHECKSUM_STRING (AT_string (at));
7389 break;
7390
7391 case dw_val_class_addr:
7392 r = AT_addr (a: at);
7393 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7394 CHECKSUM_ULEB128 (DW_FORM_string);
7395 CHECKSUM_STRING (XSTR (r, 0));
7396 break;
7397
7398 case dw_val_class_offset:
7399 CHECKSUM_ULEB128 (DW_FORM_sdata);
7400 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7401 break;
7402
7403 case dw_val_class_loc:
7404 for (loc = AT_loc (a: at); loc; loc = loc->dw_loc_next)
7405 loc_checksum_ordered (loc, ctx);
7406 break;
7407
7408 case dw_val_class_fde_ref:
7409 case dw_val_class_symview:
7410 case dw_val_class_lbl_id:
7411 case dw_val_class_lineptr:
7412 case dw_val_class_macptr:
7413 case dw_val_class_loclistsptr:
7414 case dw_val_class_high_pc:
7415 break;
7416
7417 case dw_val_class_file:
7418 case dw_val_class_file_implicit:
7419 CHECKSUM_ULEB128 (DW_FORM_string);
7420 CHECKSUM_STRING (AT_file (at)->filename);
7421 break;
7422
7423 case dw_val_class_data8:
7424 CHECKSUM (at->dw_attr_val.v.val_data8);
7425 break;
7426
7427 default:
7428 break;
7429 }
7430}
7431
7432struct checksum_attributes
7433{
7434 dw_attr_node *at_name;
7435 dw_attr_node *at_type;
7436 dw_attr_node *at_friend;
7437 dw_attr_node *at_accessibility;
7438 dw_attr_node *at_address_class;
7439 dw_attr_node *at_alignment;
7440 dw_attr_node *at_allocated;
7441 dw_attr_node *at_artificial;
7442 dw_attr_node *at_associated;
7443 dw_attr_node *at_binary_scale;
7444 dw_attr_node *at_bit_offset;
7445 dw_attr_node *at_bit_size;
7446 dw_attr_node *at_bit_stride;
7447 dw_attr_node *at_byte_size;
7448 dw_attr_node *at_byte_stride;
7449 dw_attr_node *at_const_value;
7450 dw_attr_node *at_containing_type;
7451 dw_attr_node *at_count;
7452 dw_attr_node *at_data_location;
7453 dw_attr_node *at_data_member_location;
7454 dw_attr_node *at_decimal_scale;
7455 dw_attr_node *at_decimal_sign;
7456 dw_attr_node *at_default_value;
7457 dw_attr_node *at_digit_count;
7458 dw_attr_node *at_discr;
7459 dw_attr_node *at_discr_list;
7460 dw_attr_node *at_discr_value;
7461 dw_attr_node *at_encoding;
7462 dw_attr_node *at_endianity;
7463 dw_attr_node *at_explicit;
7464 dw_attr_node *at_is_optional;
7465 dw_attr_node *at_location;
7466 dw_attr_node *at_lower_bound;
7467 dw_attr_node *at_mutable;
7468 dw_attr_node *at_ordering;
7469 dw_attr_node *at_picture_string;
7470 dw_attr_node *at_prototyped;
7471 dw_attr_node *at_small;
7472 dw_attr_node *at_segment;
7473 dw_attr_node *at_string_length;
7474 dw_attr_node *at_string_length_bit_size;
7475 dw_attr_node *at_string_length_byte_size;
7476 dw_attr_node *at_threads_scaled;
7477 dw_attr_node *at_upper_bound;
7478 dw_attr_node *at_use_location;
7479 dw_attr_node *at_use_UTF8;
7480 dw_attr_node *at_variable_parameter;
7481 dw_attr_node *at_virtuality;
7482 dw_attr_node *at_visibility;
7483 dw_attr_node *at_vtable_elem_location;
7484};
7485
7486/* Collect the attributes that we will want to use for the checksum. */
7487
7488static void
7489collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7490{
7491 dw_attr_node *a;
7492 unsigned ix;
7493
7494 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7495 {
7496 switch (a->dw_attr)
7497 {
7498 case DW_AT_name:
7499 attrs->at_name = a;
7500 break;
7501 case DW_AT_type:
7502 attrs->at_type = a;
7503 break;
7504 case DW_AT_friend:
7505 attrs->at_friend = a;
7506 break;
7507 case DW_AT_accessibility:
7508 attrs->at_accessibility = a;
7509 break;
7510 case DW_AT_address_class:
7511 attrs->at_address_class = a;
7512 break;
7513 case DW_AT_alignment:
7514 attrs->at_alignment = a;
7515 break;
7516 case DW_AT_allocated:
7517 attrs->at_allocated = a;
7518 break;
7519 case DW_AT_artificial:
7520 attrs->at_artificial = a;
7521 break;
7522 case DW_AT_associated:
7523 attrs->at_associated = a;
7524 break;
7525 case DW_AT_binary_scale:
7526 attrs->at_binary_scale = a;
7527 break;
7528 case DW_AT_bit_offset:
7529 attrs->at_bit_offset = a;
7530 break;
7531 case DW_AT_bit_size:
7532 attrs->at_bit_size = a;
7533 break;
7534 case DW_AT_bit_stride:
7535 attrs->at_bit_stride = a;
7536 break;
7537 case DW_AT_byte_size:
7538 attrs->at_byte_size = a;
7539 break;
7540 case DW_AT_byte_stride:
7541 attrs->at_byte_stride = a;
7542 break;
7543 case DW_AT_const_value:
7544 attrs->at_const_value = a;
7545 break;
7546 case DW_AT_containing_type:
7547 attrs->at_containing_type = a;
7548 break;
7549 case DW_AT_count:
7550 attrs->at_count = a;
7551 break;
7552 case DW_AT_data_location:
7553 attrs->at_data_location = a;
7554 break;
7555 case DW_AT_data_member_location:
7556 attrs->at_data_member_location = a;
7557 break;
7558 case DW_AT_decimal_scale:
7559 attrs->at_decimal_scale = a;
7560 break;
7561 case DW_AT_decimal_sign:
7562 attrs->at_decimal_sign = a;
7563 break;
7564 case DW_AT_default_value:
7565 attrs->at_default_value = a;
7566 break;
7567 case DW_AT_digit_count:
7568 attrs->at_digit_count = a;
7569 break;
7570 case DW_AT_discr:
7571 attrs->at_discr = a;
7572 break;
7573 case DW_AT_discr_list:
7574 attrs->at_discr_list = a;
7575 break;
7576 case DW_AT_discr_value:
7577 attrs->at_discr_value = a;
7578 break;
7579 case DW_AT_encoding:
7580 attrs->at_encoding = a;
7581 break;
7582 case DW_AT_endianity:
7583 attrs->at_endianity = a;
7584 break;
7585 case DW_AT_explicit:
7586 attrs->at_explicit = a;
7587 break;
7588 case DW_AT_is_optional:
7589 attrs->at_is_optional = a;
7590 break;
7591 case DW_AT_location:
7592 attrs->at_location = a;
7593 break;
7594 case DW_AT_lower_bound:
7595 attrs->at_lower_bound = a;
7596 break;
7597 case DW_AT_mutable:
7598 attrs->at_mutable = a;
7599 break;
7600 case DW_AT_ordering:
7601 attrs->at_ordering = a;
7602 break;
7603 case DW_AT_picture_string:
7604 attrs->at_picture_string = a;
7605 break;
7606 case DW_AT_prototyped:
7607 attrs->at_prototyped = a;
7608 break;
7609 case DW_AT_small:
7610 attrs->at_small = a;
7611 break;
7612 case DW_AT_segment:
7613 attrs->at_segment = a;
7614 break;
7615 case DW_AT_string_length:
7616 attrs->at_string_length = a;
7617 break;
7618 case DW_AT_string_length_bit_size:
7619 attrs->at_string_length_bit_size = a;
7620 break;
7621 case DW_AT_string_length_byte_size:
7622 attrs->at_string_length_byte_size = a;
7623 break;
7624 case DW_AT_threads_scaled:
7625 attrs->at_threads_scaled = a;
7626 break;
7627 case DW_AT_upper_bound:
7628 attrs->at_upper_bound = a;
7629 break;
7630 case DW_AT_use_location:
7631 attrs->at_use_location = a;
7632 break;
7633 case DW_AT_use_UTF8:
7634 attrs->at_use_UTF8 = a;
7635 break;
7636 case DW_AT_variable_parameter:
7637 attrs->at_variable_parameter = a;
7638 break;
7639 case DW_AT_virtuality:
7640 attrs->at_virtuality = a;
7641 break;
7642 case DW_AT_visibility:
7643 attrs->at_visibility = a;
7644 break;
7645 case DW_AT_vtable_elem_location:
7646 attrs->at_vtable_elem_location = a;
7647 break;
7648 default:
7649 break;
7650 }
7651 }
7652}
7653
7654/* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7655
7656static void
7657die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7658{
7659 dw_die_ref c;
7660 dw_die_ref decl;
7661 struct checksum_attributes attrs;
7662
7663 CHECKSUM_ULEB128 ('D');
7664 CHECKSUM_ULEB128 (die->die_tag);
7665
7666 memset (s: &attrs, c: 0, n: sizeof (attrs));
7667
7668 decl = get_AT_ref (die, attr_kind: DW_AT_specification);
7669 if (decl != NULL)
7670 collect_checksum_attributes (attrs: &attrs, die: decl);
7671 collect_checksum_attributes (attrs: &attrs, die);
7672
7673 CHECKSUM_ATTR (attrs.at_name);
7674 CHECKSUM_ATTR (attrs.at_accessibility);
7675 CHECKSUM_ATTR (attrs.at_address_class);
7676 CHECKSUM_ATTR (attrs.at_allocated);
7677 CHECKSUM_ATTR (attrs.at_artificial);
7678 CHECKSUM_ATTR (attrs.at_associated);
7679 CHECKSUM_ATTR (attrs.at_binary_scale);
7680 CHECKSUM_ATTR (attrs.at_bit_offset);
7681 CHECKSUM_ATTR (attrs.at_bit_size);
7682 CHECKSUM_ATTR (attrs.at_bit_stride);
7683 CHECKSUM_ATTR (attrs.at_byte_size);
7684 CHECKSUM_ATTR (attrs.at_byte_stride);
7685 CHECKSUM_ATTR (attrs.at_const_value);
7686 CHECKSUM_ATTR (attrs.at_containing_type);
7687 CHECKSUM_ATTR (attrs.at_count);
7688 CHECKSUM_ATTR (attrs.at_data_location);
7689 CHECKSUM_ATTR (attrs.at_data_member_location);
7690 CHECKSUM_ATTR (attrs.at_decimal_scale);
7691 CHECKSUM_ATTR (attrs.at_decimal_sign);
7692 CHECKSUM_ATTR (attrs.at_default_value);
7693 CHECKSUM_ATTR (attrs.at_digit_count);
7694 CHECKSUM_ATTR (attrs.at_discr);
7695 CHECKSUM_ATTR (attrs.at_discr_list);
7696 CHECKSUM_ATTR (attrs.at_discr_value);
7697 CHECKSUM_ATTR (attrs.at_encoding);
7698 CHECKSUM_ATTR (attrs.at_endianity);
7699 CHECKSUM_ATTR (attrs.at_explicit);
7700 CHECKSUM_ATTR (attrs.at_is_optional);
7701 CHECKSUM_ATTR (attrs.at_location);
7702 CHECKSUM_ATTR (attrs.at_lower_bound);
7703 CHECKSUM_ATTR (attrs.at_mutable);
7704 CHECKSUM_ATTR (attrs.at_ordering);
7705 CHECKSUM_ATTR (attrs.at_picture_string);
7706 CHECKSUM_ATTR (attrs.at_prototyped);
7707 CHECKSUM_ATTR (attrs.at_small);
7708 CHECKSUM_ATTR (attrs.at_segment);
7709 CHECKSUM_ATTR (attrs.at_string_length);
7710 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7711 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7712 CHECKSUM_ATTR (attrs.at_threads_scaled);
7713 CHECKSUM_ATTR (attrs.at_upper_bound);
7714 CHECKSUM_ATTR (attrs.at_use_location);
7715 CHECKSUM_ATTR (attrs.at_use_UTF8);
7716 CHECKSUM_ATTR (attrs.at_variable_parameter);
7717 CHECKSUM_ATTR (attrs.at_virtuality);
7718 CHECKSUM_ATTR (attrs.at_visibility);
7719 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7720 CHECKSUM_ATTR (attrs.at_type);
7721 CHECKSUM_ATTR (attrs.at_friend);
7722 CHECKSUM_ATTR (attrs.at_alignment);
7723
7724 /* Checksum the child DIEs. */
7725 c = die->die_child;
7726 if (c) do {
7727 dw_attr_node *name_attr;
7728
7729 c = c->die_sib;
7730 name_attr = get_AT (die: c, attr_kind: DW_AT_name);
7731 if (is_template_instantiation (c))
7732 {
7733 /* Ignore instantiations of member type and function templates. */
7734 }
7735 else if (name_attr != NULL
7736 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7737 {
7738 /* Use a shallow checksum for named nested types and member
7739 functions. */
7740 CHECKSUM_ULEB128 ('S');
7741 CHECKSUM_ULEB128 (c->die_tag);
7742 CHECKSUM_STRING (AT_string (name_attr));
7743 }
7744 else
7745 {
7746 /* Use a deep checksum for other children. */
7747 /* Mark this DIE so it gets processed when unmarking. */
7748 if (c->die_mark == 0)
7749 c->die_mark = -1;
7750 die_checksum_ordered (die: c, ctx, mark);
7751 }
7752 } while (c != die->die_child);
7753
7754 CHECKSUM_ULEB128 (0);
7755}
7756
7757/* Add a type name and tag to a hash. */
7758static void
7759die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7760{
7761 CHECKSUM_ULEB128 (tag);
7762 CHECKSUM_STRING (name);
7763}
7764
7765#undef CHECKSUM
7766#undef CHECKSUM_STRING
7767#undef CHECKSUM_ATTR
7768#undef CHECKSUM_LEB128
7769#undef CHECKSUM_ULEB128
7770
7771/* Generate the type signature for DIE. This is computed by generating an
7772 MD5 checksum over the DIE's tag, its relevant attributes, and its
7773 children. Attributes that are references to other DIEs are processed
7774 by recursion, using the MARK field to prevent infinite recursion.
7775 If the DIE is nested inside a namespace or another type, we also
7776 need to include that context in the signature. The lower 64 bits
7777 of the resulting MD5 checksum comprise the signature. */
7778
7779static void
7780generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7781{
7782 int mark;
7783 const char *name;
7784 unsigned char checksum[16];
7785 struct md5_ctx ctx;
7786 dw_die_ref decl;
7787 dw_die_ref parent;
7788
7789 name = get_AT_string (die, attr_kind: DW_AT_name);
7790 decl = get_AT_ref (die, attr_kind: DW_AT_specification);
7791 parent = dw_get_die_parent (die);
7792
7793 /* First, compute a signature for just the type name (and its surrounding
7794 context, if any. This is stored in the type unit DIE for link-time
7795 ODR (one-definition rule) checking. */
7796
7797 if (is_cxx () && name != NULL)
7798 {
7799 md5_init_ctx (ctx: &ctx);
7800
7801 /* Checksum the names of surrounding namespaces and structures. */
7802 if (parent != NULL)
7803 checksum_die_context (die: parent, ctx: &ctx);
7804
7805 /* Checksum the current DIE. */
7806 die_odr_checksum (tag: die->die_tag, name, ctx: &ctx);
7807 md5_finish_ctx (ctx: &ctx, resbuf: checksum);
7808
7809 add_AT_data8 (die: type_node->root_die, attr_kind: DW_AT_GNU_odr_signature, data8: &checksum[8]);
7810 }
7811
7812 /* Next, compute the complete type signature. */
7813
7814 md5_init_ctx (ctx: &ctx);
7815 mark = 1;
7816 die->die_mark = mark;
7817
7818 /* Checksum the names of surrounding namespaces and structures. */
7819 if (parent != NULL)
7820 checksum_die_context (die: parent, ctx: &ctx);
7821
7822 /* Checksum the DIE and its children. */
7823 die_checksum_ordered (die, ctx: &ctx, mark: &mark);
7824 unmark_all_dies (die);
7825 md5_finish_ctx (ctx: &ctx, resbuf: checksum);
7826
7827 /* Store the signature in the type node and link the type DIE and the
7828 type node together. */
7829 memcpy (dest: type_node->signature, src: &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7830 DWARF_TYPE_SIGNATURE_SIZE);
7831 die->comdat_type_p = true;
7832 die->die_id.die_type_node = type_node;
7833 type_node->type_die = die;
7834
7835 /* If the DIE is a specification, link its declaration to the type node
7836 as well. */
7837 if (decl != NULL)
7838 {
7839 decl->comdat_type_p = true;
7840 decl->die_id.die_type_node = type_node;
7841 }
7842}
7843
7844/* Do the location expressions look same? */
7845static inline bool
7846same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7847{
7848 return loc1->dw_loc_opc == loc2->dw_loc_opc
7849 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7850 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7851}
7852
7853/* Do the values look the same? */
7854static bool
7855same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7856{
7857 dw_loc_descr_ref loc1, loc2;
7858 rtx r1, r2;
7859
7860 if (v1->val_class != v2->val_class)
7861 return false;
7862
7863 switch (v1->val_class)
7864 {
7865 case dw_val_class_const:
7866 case dw_val_class_const_implicit:
7867 return v1->v.val_int == v2->v.val_int;
7868 case dw_val_class_unsigned_const:
7869 case dw_val_class_unsigned_const_implicit:
7870 return v1->v.val_unsigned == v2->v.val_unsigned;
7871 case dw_val_class_const_double:
7872 return v1->v.val_double.high == v2->v.val_double.high
7873 && v1->v.val_double.low == v2->v.val_double.low;
7874 case dw_val_class_wide_int:
7875 return *v1->v.val_wide == *v2->v.val_wide;
7876 case dw_val_class_vec:
7877 if (v1->v.val_vec.length != v2->v.val_vec.length
7878 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7879 return false;
7880 if (memcmp (s1: v1->v.val_vec.array, s2: v2->v.val_vec.array,
7881 n: v1->v.val_vec.length * v1->v.val_vec.elt_size))
7882 return false;
7883 return true;
7884 case dw_val_class_flag:
7885 return v1->v.val_flag == v2->v.val_flag;
7886 case dw_val_class_str:
7887 return !strcmp (s1: v1->v.val_str->str, s2: v2->v.val_str->str);
7888
7889 case dw_val_class_addr:
7890 r1 = v1->v.val_addr;
7891 r2 = v2->v.val_addr;
7892 if (GET_CODE (r1) != GET_CODE (r2))
7893 return false;
7894 return !rtx_equal_p (r1, r2);
7895
7896 case dw_val_class_offset:
7897 return v1->v.val_offset == v2->v.val_offset;
7898
7899 case dw_val_class_loc:
7900 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7901 loc1 && loc2;
7902 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7903 if (!same_loc_p (loc1, loc2, mark))
7904 return false;
7905 return !loc1 && !loc2;
7906
7907 case dw_val_class_die_ref:
7908 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7909
7910 case dw_val_class_symview:
7911 return strcmp (s1: v1->v.val_symbolic_view, s2: v2->v.val_symbolic_view) == 0;
7912
7913 case dw_val_class_fde_ref:
7914 case dw_val_class_vms_delta:
7915 case dw_val_class_lbl_id:
7916 case dw_val_class_lineptr:
7917 case dw_val_class_macptr:
7918 case dw_val_class_loclistsptr:
7919 case dw_val_class_high_pc:
7920 return true;
7921
7922 case dw_val_class_file:
7923 case dw_val_class_file_implicit:
7924 return v1->v.val_file == v2->v.val_file;
7925
7926 case dw_val_class_data8:
7927 return !memcmp (s1: v1->v.val_data8, s2: v2->v.val_data8, n: 8);
7928
7929 default:
7930 return true;
7931 }
7932}
7933
7934/* Do the attributes look the same? */
7935
7936static bool
7937same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7938{
7939 if (at1->dw_attr != at2->dw_attr)
7940 return false;
7941
7942 /* We don't care that this was compiled with a different compiler
7943 snapshot; if the output is the same, that's what matters. */
7944 if (at1->dw_attr == DW_AT_producer)
7945 return true;
7946
7947 return same_dw_val_p (v1: &at1->dw_attr_val, v2: &at2->dw_attr_val, mark);
7948}
7949
7950/* Do the dies look the same? */
7951
7952static bool
7953same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7954{
7955 dw_die_ref c1, c2;
7956 dw_attr_node *a1;
7957 unsigned ix;
7958
7959 /* To avoid infinite recursion. */
7960 if (die1->die_mark)
7961 return die1->die_mark == die2->die_mark;
7962 die1->die_mark = die2->die_mark = ++(*mark);
7963
7964 if (die1->die_tag != die2->die_tag)
7965 return false;
7966
7967 if (vec_safe_length (v: die1->die_attr) != vec_safe_length (v: die2->die_attr))
7968 return false;
7969
7970 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7971 if (!same_attr_p (at1: a1, at2: &(*die2->die_attr)[ix], mark))
7972 return false;
7973
7974 c1 = die1->die_child;
7975 c2 = die2->die_child;
7976 if (! c1)
7977 {
7978 if (c2)
7979 return false;
7980 }
7981 else
7982 for (;;)
7983 {
7984 if (!same_die_p (die1: c1, die2: c2, mark))
7985 return false;
7986 c1 = c1->die_sib;
7987 c2 = c2->die_sib;
7988 if (c1 == die1->die_child)
7989 {
7990 if (c2 == die2->die_child)
7991 break;
7992 else
7993 return false;
7994 }
7995 }
7996
7997 return true;
7998}
7999
8000/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8001 children, and set die_symbol. */
8002
8003static void
8004compute_comp_unit_symbol (dw_die_ref unit_die)
8005{
8006 const char *die_name = get_AT_string (die: unit_die, attr_kind: DW_AT_name);
8007 const char *base = die_name ? lbasename (die_name) : "anonymous";
8008 char *name = XALLOCAVEC (char, strlen (base) + 64);
8009 char *p;
8010 int i, mark;
8011 unsigned char checksum[16];
8012 struct md5_ctx ctx;
8013
8014 /* Compute the checksum of the DIE, then append part of it as hex digits to
8015 the name filename of the unit. */
8016
8017 md5_init_ctx (ctx: &ctx);
8018 mark = 0;
8019 die_checksum (die: unit_die, ctx: &ctx, mark: &mark);
8020 unmark_all_dies (unit_die);
8021 md5_finish_ctx (ctx: &ctx, resbuf: checksum);
8022
8023 /* When we this for comp_unit_die () we have a DW_AT_name that might
8024 not start with a letter but with anything valid for filenames and
8025 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8026 character is not a letter. */
8027 sprintf (s: name, format: "%s%s.", ISALPHA (*base) ? "" : "g", base);
8028 clean_symbol_name (name);
8029
8030 p = name + strlen (s: name);
8031 for (i = 0; i < 4; i++)
8032 {
8033 sprintf (s: p, format: "%.2x", checksum[i]);
8034 p += 2;
8035 }
8036
8037 unit_die->die_id.die_symbol = xstrdup (name);
8038}
8039
8040/* Returns true if DIE represents a type, in the sense of TYPE_P. */
8041
8042static bool
8043is_type_die (dw_die_ref die)
8044{
8045 switch (die->die_tag)
8046 {
8047 case DW_TAG_array_type:
8048 case DW_TAG_class_type:
8049 case DW_TAG_interface_type:
8050 case DW_TAG_enumeration_type:
8051 case DW_TAG_pointer_type:
8052 case DW_TAG_reference_type:
8053 case DW_TAG_rvalue_reference_type:
8054 case DW_TAG_string_type:
8055 case DW_TAG_structure_type:
8056 case DW_TAG_subroutine_type:
8057 case DW_TAG_union_type:
8058 case DW_TAG_ptr_to_member_type:
8059 case DW_TAG_set_type:
8060 case DW_TAG_subrange_type:
8061 case DW_TAG_base_type:
8062 case DW_TAG_const_type:
8063 case DW_TAG_file_type:
8064 case DW_TAG_packed_type:
8065 case DW_TAG_volatile_type:
8066 case DW_TAG_typedef:
8067 return true;
8068 default:
8069 return false;
8070 }
8071}
8072
8073/* Returns true iff C is a compile-unit DIE. */
8074
8075static inline bool
8076is_cu_die (dw_die_ref c)
8077{
8078 return c && (c->die_tag == DW_TAG_compile_unit
8079 || c->die_tag == DW_TAG_skeleton_unit);
8080}
8081
8082/* Returns true iff C is a unit DIE of some sort. */
8083
8084static inline bool
8085is_unit_die (dw_die_ref c)
8086{
8087 return c && (c->die_tag == DW_TAG_compile_unit
8088 || c->die_tag == DW_TAG_partial_unit
8089 || c->die_tag == DW_TAG_type_unit
8090 || c->die_tag == DW_TAG_skeleton_unit);
8091}
8092
8093/* Returns true iff C is a namespace DIE. */
8094
8095static inline bool
8096is_namespace_die (dw_die_ref c)
8097{
8098 return c && c->die_tag == DW_TAG_namespace;
8099}
8100
8101/* Return true if this DIE is a template parameter. */
8102
8103static inline bool
8104is_template_parameter (dw_die_ref die)
8105{
8106 switch (die->die_tag)
8107 {
8108 case DW_TAG_template_type_param:
8109 case DW_TAG_template_value_param:
8110 case DW_TAG_GNU_template_template_param:
8111 case DW_TAG_GNU_template_parameter_pack:
8112 return true;
8113 default:
8114 return false;
8115 }
8116}
8117
8118/* Return true if this DIE represents a template instantiation. */
8119
8120static inline bool
8121is_template_instantiation (dw_die_ref die)
8122{
8123 dw_die_ref c;
8124
8125 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8126 return false;
8127 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8128 return false;
8129}
8130
8131static char *
8132gen_internal_sym (const char *prefix)
8133{
8134 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8135
8136 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8137 return xstrdup (buf);
8138}
8139
8140/* Return true if this DIE is a declaration. */
8141
8142static bool
8143is_declaration_die (dw_die_ref die)
8144{
8145 dw_attr_node *a;
8146 unsigned ix;
8147
8148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8149 if (a->dw_attr == DW_AT_declaration)
8150 return true;
8151
8152 return false;
8153}
8154
8155/* Return true if this DIE is nested inside a subprogram. */
8156
8157static bool
8158is_nested_in_subprogram (dw_die_ref die)
8159{
8160 dw_die_ref decl = get_AT_ref (die, attr_kind: DW_AT_specification);
8161
8162 if (decl == NULL)
8163 decl = die;
8164 return local_scope_p (decl);
8165}
8166
8167/* Return true if this DIE contains a defining declaration of a
8168 subprogram. */
8169
8170static bool
8171contains_subprogram_definition (dw_die_ref die)
8172{
8173 dw_die_ref c;
8174
8175 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8176 return true;
8177 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8178 return false;
8179}
8180
8181/* Return true if this is a type DIE that should be moved to a
8182 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8183 unit type. */
8184
8185static bool
8186should_move_die_to_comdat (dw_die_ref die)
8187{
8188 switch (die->die_tag)
8189 {
8190 case DW_TAG_class_type:
8191 case DW_TAG_structure_type:
8192 case DW_TAG_enumeration_type:
8193 case DW_TAG_union_type:
8194 /* Don't move declarations, inlined instances, types nested in a
8195 subprogram, or types that contain subprogram definitions. */
8196 if (is_declaration_die (die)
8197 || get_AT (die, attr_kind: DW_AT_abstract_origin)
8198 || is_nested_in_subprogram (die)
8199 || contains_subprogram_definition (die))
8200 return false;
8201 if (die->die_tag != DW_TAG_enumeration_type)
8202 {
8203 /* Don't move non-constant size aggregates. */
8204 dw_attr_node *sz = get_AT (die, attr_kind: DW_AT_byte_size);
8205 if (sz == NULL
8206 || (AT_class (a: sz) != dw_val_class_unsigned_const
8207 && AT_class (a: sz) != dw_val_class_unsigned_const_implicit))
8208 return false;
8209 }
8210 return true;
8211 case DW_TAG_array_type:
8212 case DW_TAG_interface_type:
8213 case DW_TAG_pointer_type:
8214 case DW_TAG_reference_type:
8215 case DW_TAG_rvalue_reference_type:
8216 case DW_TAG_string_type:
8217 case DW_TAG_subroutine_type:
8218 case DW_TAG_ptr_to_member_type:
8219 case DW_TAG_set_type:
8220 case DW_TAG_subrange_type:
8221 case DW_TAG_base_type:
8222 case DW_TAG_const_type:
8223 case DW_TAG_file_type:
8224 case DW_TAG_packed_type:
8225 case DW_TAG_volatile_type:
8226 case DW_TAG_typedef:
8227 default:
8228 return false;
8229 }
8230}
8231
8232/* Make a clone of DIE. */
8233
8234static dw_die_ref
8235clone_die (dw_die_ref die)
8236{
8237 dw_die_ref clone = new_die_raw (tag_value: die->die_tag);
8238 dw_attr_node *a;
8239 unsigned ix;
8240
8241 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8242 add_dwarf_attr (die: clone, attr: a);
8243
8244 return clone;
8245}
8246
8247/* Make a clone of the tree rooted at DIE. */
8248
8249static dw_die_ref
8250clone_tree (dw_die_ref die)
8251{
8252 dw_die_ref c;
8253 dw_die_ref clone = clone_die (die);
8254
8255 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8256
8257 return clone;
8258}
8259
8260/* Make a clone of DIE as a declaration. */
8261
8262static dw_die_ref
8263clone_as_declaration (dw_die_ref die)
8264{
8265 dw_die_ref clone;
8266 dw_die_ref decl;
8267 dw_attr_node *a;
8268 unsigned ix;
8269
8270 /* If the DIE is already a declaration, just clone it. */
8271 if (is_declaration_die (die))
8272 return clone_die (die);
8273
8274 /* If the DIE is a specification, just clone its declaration DIE. */
8275 decl = get_AT_ref (die, attr_kind: DW_AT_specification);
8276 if (decl != NULL)
8277 {
8278 clone = clone_die (die: decl);
8279 if (die->comdat_type_p)
8280 add_AT_die_ref (die: clone, attr_kind: DW_AT_signature, targ_die: die);
8281 return clone;
8282 }
8283
8284 clone = new_die_raw (tag_value: die->die_tag);
8285
8286 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8287 {
8288 /* We don't want to copy over all attributes.
8289 For example we don't want DW_AT_byte_size because otherwise we will no
8290 longer have a declaration and GDB will treat it as a definition. */
8291
8292 switch (a->dw_attr)
8293 {
8294 case DW_AT_abstract_origin:
8295 case DW_AT_artificial:
8296 case DW_AT_containing_type:
8297 case DW_AT_external:
8298 case DW_AT_name:
8299 case DW_AT_type:
8300 case DW_AT_virtuality:
8301 case DW_AT_linkage_name:
8302 case DW_AT_MIPS_linkage_name:
8303 add_dwarf_attr (die: clone, attr: a);
8304 break;
8305 case DW_AT_byte_size:
8306 case DW_AT_alignment:
8307 default:
8308 break;
8309 }
8310 }
8311
8312 if (die->comdat_type_p)
8313 add_AT_die_ref (die: clone, attr_kind: DW_AT_signature, targ_die: die);
8314
8315 add_AT_flag (die: clone, attr_kind: DW_AT_declaration, flag: 1);
8316 return clone;
8317}
8318
8319
8320/* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8321
8322struct decl_table_entry
8323{
8324 dw_die_ref orig;
8325 dw_die_ref copy;
8326};
8327
8328/* Helpers to manipulate hash table of copied declarations. */
8329
8330/* Hashtable helpers. */
8331
8332struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8333{
8334 typedef die_struct *compare_type;
8335 static inline hashval_t hash (const decl_table_entry *);
8336 static inline bool equal (const decl_table_entry *, const die_struct *);
8337};
8338
8339inline hashval_t
8340decl_table_entry_hasher::hash (const decl_table_entry *entry)
8341{
8342 return htab_hash_pointer (entry->orig);
8343}
8344
8345inline bool
8346decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8347 const die_struct *entry2)
8348{
8349 return entry1->orig == entry2;
8350}
8351
8352typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8353
8354/* Copy DIE and its ancestors, up to, but not including, the compile unit
8355 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8356 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8357 to check if the ancestor has already been copied into UNIT. */
8358
8359static dw_die_ref
8360copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8361 decl_hash_type *decl_table)
8362{
8363 dw_die_ref parent = die->die_parent;
8364 dw_die_ref new_parent = unit;
8365 dw_die_ref copy;
8366 decl_table_entry **slot = NULL;
8367 struct decl_table_entry *entry = NULL;
8368
8369 /* If DIE refers to a stub unfold that so we get the appropriate
8370 DIE registered as orig in decl_table. */
8371 if (dw_die_ref c = get_AT_ref (die, attr_kind: DW_AT_signature))
8372 die = c;
8373
8374 if (decl_table)
8375 {
8376 /* Check if the entry has already been copied to UNIT. */
8377 slot = decl_table->find_slot_with_hash (comparable: die, hash: htab_hash_pointer (die),
8378 insert: INSERT);
8379 if (*slot != HTAB_EMPTY_ENTRY)
8380 {
8381 entry = *slot;
8382 return entry->copy;
8383 }
8384
8385 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8386 entry = XCNEW (struct decl_table_entry);
8387 entry->orig = die;
8388 entry->copy = NULL;
8389 *slot = entry;
8390 }
8391
8392 if (parent != NULL)
8393 {
8394 dw_die_ref spec = get_AT_ref (die: parent, attr_kind: DW_AT_specification);
8395 if (spec != NULL)
8396 parent = spec;
8397 if (!is_unit_die (c: parent))
8398 new_parent = copy_ancestor_tree (unit, die: parent, decl_table);
8399 }
8400
8401 copy = clone_as_declaration (die);
8402 add_child_die (die: new_parent, child_die: copy);
8403
8404 if (decl_table)
8405 {
8406 /* Record the pointer to the copy. */
8407 entry->copy = copy;
8408 }
8409
8410 return copy;
8411}
8412/* Copy the declaration context to the new type unit DIE. This includes
8413 any surrounding namespace or type declarations. If the DIE has an
8414 AT_specification attribute, it also includes attributes and children
8415 attached to the specification, and returns a pointer to the original
8416 parent of the declaration DIE. Returns NULL otherwise. */
8417
8418static dw_die_ref
8419copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8420{
8421 dw_die_ref decl;
8422 dw_die_ref new_decl;
8423 dw_die_ref orig_parent = NULL;
8424
8425 decl = get_AT_ref (die, attr_kind: DW_AT_specification);
8426 if (decl == NULL)
8427 decl = die;
8428 else
8429 {
8430 unsigned ix;
8431 dw_die_ref c;
8432 dw_attr_node *a;
8433
8434 /* The original DIE will be changed to a declaration, and must
8435 be moved to be a child of the original declaration DIE. */
8436 orig_parent = decl->die_parent;
8437
8438 /* Copy the type node pointer from the new DIE to the original
8439 declaration DIE so we can forward references later. */
8440 decl->comdat_type_p = true;
8441 decl->die_id.die_type_node = die->die_id.die_type_node;
8442
8443 remove_AT (die, attr_kind: DW_AT_specification);
8444
8445 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8446 {
8447 if (a->dw_attr != DW_AT_name
8448 && a->dw_attr != DW_AT_declaration
8449 && a->dw_attr != DW_AT_external)
8450 add_dwarf_attr (die, attr: a);
8451 }
8452
8453 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8454 }
8455
8456 if (decl->die_parent != NULL
8457 && !is_unit_die (c: decl->die_parent))
8458 {
8459 new_decl = copy_ancestor_tree (unit, die: decl, NULL);
8460 if (new_decl != NULL)
8461 {
8462 remove_AT (die: new_decl, attr_kind: DW_AT_signature);
8463 add_AT_specification (die, targ_die: new_decl);
8464 }
8465 }
8466
8467 return orig_parent;
8468}
8469
8470/* Generate the skeleton ancestor tree for the given NODE, then clone
8471 the DIE and add the clone into the tree. */
8472
8473static void
8474generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8475{
8476 if (node->new_die != NULL)
8477 return;
8478
8479 node->new_die = clone_as_declaration (die: node->old_die);
8480
8481 if (node->parent != NULL)
8482 {
8483 generate_skeleton_ancestor_tree (node: node->parent);
8484 add_child_die (die: node->parent->new_die, child_die: node->new_die);
8485 }
8486}
8487
8488/* Generate a skeleton tree of DIEs containing any declarations that are
8489 found in the original tree. We traverse the tree looking for declaration
8490 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8491
8492static void
8493generate_skeleton_bottom_up (skeleton_chain_node *parent)
8494{
8495 skeleton_chain_node node;
8496 dw_die_ref c;
8497 dw_die_ref first;
8498 dw_die_ref prev = NULL;
8499 dw_die_ref next = NULL;
8500
8501 node.parent = parent;
8502
8503 first = c = parent->old_die->die_child;
8504 if (c)
8505 next = c->die_sib;
8506 if (c) do {
8507 if (prev == NULL || prev->die_sib == c)
8508 prev = c;
8509 c = next;
8510 next = (c == first ? NULL : c->die_sib);
8511 node.old_die = c;
8512 node.new_die = NULL;
8513 if (is_declaration_die (die: c))
8514 {
8515 if (is_template_instantiation (die: c))
8516 {
8517 /* Instantiated templates do not need to be cloned into the
8518 type unit. Just move the DIE and its children back to
8519 the skeleton tree (in the main CU). */
8520 remove_child_with_prev (child: c, prev);
8521 add_child_die (die: parent->new_die, child_die: c);
8522 c = prev;
8523 }
8524 else if (c->comdat_type_p)
8525 {
8526 /* This is the skeleton of earlier break_out_comdat_types
8527 type. Clone the existing DIE, but keep the children
8528 under the original (which is in the main CU). */
8529 dw_die_ref clone = clone_die (die: c);
8530
8531 replace_child (old_child: c, new_child: clone, prev);
8532 generate_skeleton_ancestor_tree (node: parent);
8533 add_child_die (die: parent->new_die, child_die: c);
8534 c = clone;
8535 continue;
8536 }
8537 else
8538 {
8539 /* Clone the existing DIE, move the original to the skeleton
8540 tree (which is in the main CU), and put the clone, with
8541 all the original's children, where the original came from
8542 (which is about to be moved to the type unit). */
8543 dw_die_ref clone = clone_die (die: c);
8544 move_all_children (old_parent: c, new_parent: clone);
8545
8546 /* If the original has a DW_AT_object_pointer attribute,
8547 it would now point to a child DIE just moved to the
8548 cloned tree, so we need to remove that attribute from
8549 the original. */
8550 remove_AT (die: c, attr_kind: DW_AT_object_pointer);
8551
8552 replace_child (old_child: c, new_child: clone, prev);
8553 generate_skeleton_ancestor_tree (node: parent);
8554 add_child_die (die: parent->new_die, child_die: c);
8555 node.old_die = clone;
8556 node.new_die = c;
8557 c = clone;
8558 }
8559 }
8560 generate_skeleton_bottom_up (parent: &node);
8561 } while (next != NULL);
8562}
8563
8564/* Wrapper function for generate_skeleton_bottom_up. */
8565
8566static dw_die_ref
8567generate_skeleton (dw_die_ref die)
8568{
8569 skeleton_chain_node node;
8570
8571 node.old_die = die;
8572 node.new_die = NULL;
8573 node.parent = NULL;
8574
8575 /* If this type definition is nested inside another type,
8576 and is not an instantiation of a template, always leave
8577 at least a declaration in its place. */
8578 if (die->die_parent != NULL
8579 && is_type_die (die: die->die_parent)
8580 && !is_template_instantiation (die))
8581 node.new_die = clone_as_declaration (die);
8582
8583 generate_skeleton_bottom_up (parent: &node);
8584 return node.new_die;
8585}
8586
8587/* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8588 declaration. The original DIE is moved to a new compile unit so that
8589 existing references to it follow it to the new location. If any of the
8590 original DIE's descendants is a declaration, we need to replace the
8591 original DIE with a skeleton tree and move the declarations back into the
8592 skeleton tree. */
8593
8594static dw_die_ref
8595remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8596 dw_die_ref prev)
8597{
8598 dw_die_ref skeleton, orig_parent;
8599
8600 /* Copy the declaration context to the type unit DIE. If the returned
8601 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8602 that DIE. */
8603 orig_parent = copy_declaration_context (unit, die: child);
8604
8605 skeleton = generate_skeleton (die: child);
8606 if (skeleton == NULL)
8607 remove_child_with_prev (child, prev);
8608 else
8609 {
8610 skeleton->comdat_type_p = true;
8611 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8612
8613 /* If the original DIE was a specification, we need to put
8614 the skeleton under the parent DIE of the declaration.
8615 This leaves the original declaration in the tree, but
8616 it will be pruned later since there are no longer any
8617 references to it. */
8618 if (orig_parent != NULL)
8619 {
8620 remove_child_with_prev (child, prev);
8621 add_child_die (die: orig_parent, child_die: skeleton);
8622 }
8623 else
8624 replace_child (old_child: child, new_child: skeleton, prev);
8625 }
8626
8627 return skeleton;
8628}
8629
8630static void
8631copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8632 comdat_type_node *type_node,
8633 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8634
8635/* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8636 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8637 DWARF procedure references in the DW_AT_location attribute. */
8638
8639static dw_die_ref
8640copy_dwarf_procedure (dw_die_ref die,
8641 comdat_type_node *type_node,
8642 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8643{
8644 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8645
8646 /* DWARF procedures are not supposed to have children... */
8647 gcc_assert (die->die_child == NULL);
8648
8649 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8650 gcc_assert (vec_safe_length (die->die_attr) == 1
8651 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8652
8653 /* Do not copy more than once DWARF procedures. */
8654 bool existed;
8655 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (k: die, existed: &existed);
8656 if (existed)
8657 return die_copy;
8658
8659 die_copy = clone_die (die);
8660 add_child_die (die: type_node->root_die, child_die: die_copy);
8661 copy_dwarf_procs_ref_in_attrs (die: die_copy, type_node, copied_dwarf_procs);
8662 return die_copy;
8663}
8664
8665/* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8666 procedures in DIE's attributes. */
8667
8668static void
8669copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8670 comdat_type_node *type_node,
8671 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8672{
8673 dw_attr_node *a;
8674 unsigned i;
8675
8676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8677 {
8678 dw_loc_descr_ref loc;
8679
8680 if (a->dw_attr_val.val_class != dw_val_class_loc)
8681 continue;
8682
8683 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8684 {
8685 switch (loc->dw_loc_opc)
8686 {
8687 case DW_OP_call2:
8688 case DW_OP_call4:
8689 case DW_OP_call_ref:
8690 gcc_assert (loc->dw_loc_oprnd1.val_class
8691 == dw_val_class_die_ref);
8692 loc->dw_loc_oprnd1.v.val_die_ref.die
8693 = copy_dwarf_procedure (die: loc->dw_loc_oprnd1.v.val_die_ref.die,
8694 type_node,
8695 copied_dwarf_procs);
8696
8697 default:
8698 break;
8699 }
8700 }
8701 }
8702}
8703
8704/* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8705 rewrite references to point to the copies.
8706
8707 References are looked for in DIE's attributes and recursively in all its
8708 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8709 mapping from old DWARF procedures to their copy. It is used not to copy
8710 twice the same DWARF procedure under TYPE_NODE. */
8711
8712static void
8713copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8714 comdat_type_node *type_node,
8715 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8716{
8717 dw_die_ref c;
8718
8719 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8720 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8721 type_node,
8722 copied_dwarf_procs));
8723}
8724
8725/* Traverse the DIE and set up additional .debug_types or .debug_info
8726 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8727 section. */
8728
8729static void
8730break_out_comdat_types (dw_die_ref die)
8731{
8732 dw_die_ref c;
8733 dw_die_ref first;
8734 dw_die_ref prev = NULL;
8735 dw_die_ref next = NULL;
8736 dw_die_ref unit = NULL;
8737
8738 first = c = die->die_child;
8739 if (c)
8740 next = c->die_sib;
8741 if (c) do {
8742 if (prev == NULL || prev->die_sib == c)
8743 prev = c;
8744 c = next;
8745 next = (c == first ? NULL : c->die_sib);
8746 if (should_move_die_to_comdat (die: c))
8747 {
8748 dw_die_ref replacement;
8749 comdat_type_node *type_node;
8750
8751 /* Break out nested types into their own type units. */
8752 break_out_comdat_types (die: c);
8753
8754 /* Create a new type unit DIE as the root for the new tree. */
8755 unit = new_die (tag_value: DW_TAG_type_unit, NULL, NULL);
8756 add_AT_unsigned (die: unit, attr_kind: DW_AT_language,
8757 unsigned_val: get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language));
8758 if (unsigned lname = get_AT_unsigned (die: comp_unit_die (),
8759 attr_kind: DW_AT_language_name))
8760 {
8761 add_AT_unsigned (die: unit, attr_kind: DW_AT_language_name, unsigned_val: lname);
8762 add_AT_unsigned (die: unit, attr_kind: DW_AT_language_version,
8763 unsigned_val: get_AT_unsigned (die: comp_unit_die (),
8764 attr_kind: DW_AT_language_version));
8765 }
8766
8767 /* Add the new unit's type DIE into the comdat type list. */
8768 type_node = ggc_cleared_alloc<comdat_type_node> ();
8769 type_node->root_die = unit;
8770 type_node->next = comdat_type_list;
8771 comdat_type_list = type_node;
8772
8773 /* Generate the type signature. */
8774 generate_type_signature (die: c, type_node);
8775
8776 /* Copy the declaration context, attributes, and children of the
8777 declaration into the new type unit DIE, then remove this DIE
8778 from the main CU (or replace it with a skeleton if necessary). */
8779 replacement = remove_child_or_replace_with_skeleton (unit, child: c, prev);
8780 type_node->skeleton_die = replacement;
8781
8782 /* Add the DIE to the new compunit. */
8783 add_child_die (die: unit, child_die: c);
8784
8785 /* Types can reference DWARF procedures for type size or data location
8786 expressions. Calls in DWARF expressions cannot target procedures
8787 that are not in the same section. So we must copy DWARF procedures
8788 along with this type and then rewrite references to them. */
8789 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8790 copy_dwarf_procs_ref_in_dies (die: c, type_node, copied_dwarf_procs);
8791
8792 if (replacement != NULL)
8793 c = replacement;
8794 }
8795 else if (c->die_tag == DW_TAG_namespace
8796 || c->die_tag == DW_TAG_class_type
8797 || c->die_tag == DW_TAG_structure_type
8798 || c->die_tag == DW_TAG_union_type)
8799 {
8800 /* Look for nested types that can be broken out. */
8801 break_out_comdat_types (die: c);
8802 }
8803 } while (next != NULL);
8804}
8805
8806/* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8807 Enter all the cloned children into the hash table decl_table. */
8808
8809static dw_die_ref
8810clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8811{
8812 dw_die_ref c;
8813 dw_die_ref clone;
8814 struct decl_table_entry *entry;
8815 decl_table_entry **slot;
8816
8817 if (die->die_tag == DW_TAG_subprogram)
8818 clone = clone_as_declaration (die);
8819 else
8820 clone = clone_die (die);
8821
8822 slot = decl_table->find_slot_with_hash (comparable: die,
8823 hash: htab_hash_pointer (die), insert: INSERT);
8824
8825 /* Assert that DIE isn't in the hash table yet. If it would be there
8826 before, the ancestors would be necessarily there as well, therefore
8827 clone_tree_partial wouldn't be called. */
8828 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8829
8830 entry = XCNEW (struct decl_table_entry);
8831 entry->orig = die;
8832 entry->copy = clone;
8833 *slot = entry;
8834
8835 if (die->die_tag != DW_TAG_subprogram)
8836 FOR_EACH_CHILD (die, c,
8837 add_child_die (clone, clone_tree_partial (c, decl_table)));
8838
8839 return clone;
8840}
8841
8842/* Walk the DIE and its children, looking for references to incomplete
8843 or trivial types that are unmarked (i.e., that are not in the current
8844 type_unit). */
8845
8846static void
8847copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8848{
8849 dw_die_ref c;
8850 dw_attr_node *a;
8851 unsigned ix;
8852
8853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8854 {
8855 if (AT_class (a) == dw_val_class_die_ref)
8856 {
8857 dw_die_ref targ = AT_ref (a);
8858 decl_table_entry **slot;
8859 struct decl_table_entry *entry;
8860
8861 if (targ->die_mark != 0 || targ->comdat_type_p)
8862 continue;
8863
8864 slot = decl_table->find_slot_with_hash (comparable: targ,
8865 hash: htab_hash_pointer (targ),
8866 insert: INSERT);
8867
8868 if (*slot != HTAB_EMPTY_ENTRY)
8869 {
8870 /* TARG has already been copied, so we just need to
8871 modify the reference to point to the copy. */
8872 entry = *slot;
8873 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8874 }
8875 else
8876 {
8877 dw_die_ref parent = unit;
8878 dw_die_ref copy = clone_die (die: targ);
8879
8880 /* Record in DECL_TABLE that TARG has been copied.
8881 Need to do this now, before the recursive call,
8882 because DECL_TABLE may be expanded and SLOT
8883 would no longer be a valid pointer. */
8884 entry = XCNEW (struct decl_table_entry);
8885 entry->orig = targ;
8886 entry->copy = copy;
8887 *slot = entry;
8888
8889 /* If TARG is not a declaration DIE, we need to copy its
8890 children. */
8891 if (!is_declaration_die (die: targ))
8892 {
8893 FOR_EACH_CHILD (
8894 targ, c,
8895 add_child_die (copy,
8896 clone_tree_partial (c, decl_table)));
8897 }
8898
8899 /* Make sure the cloned tree is marked as part of the
8900 type unit. */
8901 mark_dies (copy);
8902
8903 /* If TARG has surrounding context, copy its ancestor tree
8904 into the new type unit. */
8905 if (targ->die_parent != NULL
8906 && !is_unit_die (c: targ->die_parent))
8907 parent = copy_ancestor_tree (unit, die: targ->die_parent,
8908 decl_table);
8909
8910 add_child_die (die: parent, child_die: copy);
8911 a->dw_attr_val.v.val_die_ref.die = copy;
8912
8913 /* Make sure the newly-copied DIE is walked. If it was
8914 installed in a previously-added context, it won't
8915 get visited otherwise. */
8916 if (parent != unit)
8917 {
8918 /* Find the highest point of the newly-added tree,
8919 mark each node along the way, and walk from there. */
8920 parent->die_mark = 1;
8921 while (parent->die_parent
8922 && parent->die_parent->die_mark == 0)
8923 {
8924 parent = parent->die_parent;
8925 parent->die_mark = 1;
8926 }
8927 copy_decls_walk (unit, die: parent, decl_table);
8928 }
8929 }
8930 }
8931 }
8932
8933 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8934}
8935
8936/* Collect skeleton dies in DIE created by break_out_comdat_types already
8937 and record them in DECL_TABLE. */
8938
8939static void
8940collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8941{
8942 dw_die_ref c;
8943
8944 if (dw_attr_node *a = get_AT (die, attr_kind: DW_AT_signature))
8945 {
8946 dw_die_ref targ = AT_ref (a);
8947 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8948 decl_table_entry **slot
8949 = decl_table->find_slot_with_hash (comparable: targ,
8950 hash: htab_hash_pointer (targ),
8951 insert: INSERT);
8952 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8953 /* Record in DECL_TABLE that TARG has been already copied
8954 by remove_child_or_replace_with_skeleton. */
8955 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8956 entry->orig = targ;
8957 entry->copy = die;
8958 *slot = entry;
8959 }
8960 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8961}
8962
8963/* Copy declarations for "unworthy" types into the new comdat section.
8964 Incomplete types, modified types, and certain other types aren't broken
8965 out into comdat sections of their own, so they don't have a signature,
8966 and we need to copy the declaration into the same section so that we
8967 don't have an external reference. */
8968
8969static void
8970copy_decls_for_unworthy_types (dw_die_ref unit)
8971{
8972 mark_dies (unit);
8973 decl_hash_type decl_table (10);
8974 collect_skeleton_dies (die: unit, decl_table: &decl_table);
8975 copy_decls_walk (unit, die: unit, decl_table: &decl_table);
8976 unmark_dies (unit);
8977}
8978
8979/* Traverse the DIE and add a sibling attribute if it may have the
8980 effect of speeding up access to siblings. To save some space,
8981 avoid generating sibling attributes for DIE's without children. */
8982
8983static void
8984add_sibling_attributes (dw_die_ref die)
8985{
8986 dw_die_ref c;
8987
8988 if (! die->die_child)
8989 return;
8990
8991 if (die->die_parent && die != die->die_parent->die_child)
8992 add_AT_die_ref (die, attr_kind: DW_AT_sibling, targ_die: die->die_sib);
8993
8994 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8995}
8996
8997/* Output all location lists for the DIE and its children. */
8998
8999static void
9000output_location_lists (dw_die_ref die)
9001{
9002 dw_die_ref c;
9003 dw_attr_node *a;
9004 unsigned ix;
9005
9006 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9007 if (AT_class (a) == dw_val_class_loc_list)
9008 output_loc_list (AT_loc_list (a));
9009
9010 FOR_EACH_CHILD (die, c, output_location_lists (c));
9011}
9012
9013/* During assign_location_list_indexes and output_loclists_offset the
9014 current index, after it the number of assigned indexes (i.e. how
9015 large the .debug_loclists* offset table should be). */
9016static unsigned int loc_list_idx;
9017
9018/* Output all location list offsets for the DIE and its children. */
9019
9020static void
9021output_loclists_offsets (dw_die_ref die)
9022{
9023 dw_die_ref c;
9024 dw_attr_node *a;
9025 unsigned ix;
9026
9027 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9028 if (AT_class (a) == dw_val_class_loc_list)
9029 {
9030 dw_loc_list_ref l = AT_loc_list (a);
9031 if (l->offset_emitted)
9032 continue;
9033 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9034 loc_section_label, NULL);
9035 gcc_assert (l->hash == loc_list_idx);
9036 loc_list_idx++;
9037 l->offset_emitted = true;
9038 }
9039
9040 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9041}
9042
9043/* Recursively set indexes of location lists. */
9044
9045static void
9046assign_location_list_indexes (dw_die_ref die)
9047{
9048 dw_die_ref c;
9049 dw_attr_node *a;
9050 unsigned ix;
9051
9052 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9053 if (AT_class (a) == dw_val_class_loc_list)
9054 {
9055 dw_loc_list_ref list = AT_loc_list (a);
9056 if (!list->num_assigned)
9057 {
9058 list->num_assigned = true;
9059 list->hash = loc_list_idx++;
9060 }
9061 }
9062
9063 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9064}
9065
9066/* We want to limit the number of external references, because they are
9067 larger than local references: a relocation takes multiple words, and
9068 even a sig8 reference is always eight bytes, whereas a local reference
9069 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9070 So if we encounter multiple external references to the same type DIE, we
9071 make a local typedef stub for it and redirect all references there.
9072
9073 This is the element of the hash table for keeping track of these
9074 references. */
9075
9076struct external_ref
9077{
9078 dw_die_ref type;
9079 dw_die_ref stub;
9080 unsigned n_refs;
9081};
9082
9083/* Hashtable helpers. */
9084
9085struct external_ref_hasher : free_ptr_hash <external_ref>
9086{
9087 static inline hashval_t hash (const external_ref *);
9088 static inline bool equal (const external_ref *, const external_ref *);
9089};
9090
9091inline hashval_t
9092external_ref_hasher::hash (const external_ref *r)
9093{
9094 dw_die_ref die = r->type;
9095 hashval_t h = 0;
9096
9097 /* We can't use the address of the DIE for hashing, because
9098 that will make the order of the stub DIEs non-deterministic. */
9099 if (! die->comdat_type_p)
9100 /* We have a symbol; use it to compute a hash. */
9101 h = htab_hash_string (die->die_id.die_symbol);
9102 else
9103 {
9104 /* We have a type signature; use a subset of the bits as the hash.
9105 The 8-byte signature is at least as large as hashval_t. */
9106 comdat_type_node *type_node = die->die_id.die_type_node;
9107 memcpy (dest: &h, src: type_node->signature, n: sizeof (h));
9108 }
9109 return h;
9110}
9111
9112inline bool
9113external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9114{
9115 return r1->type == r2->type;
9116}
9117
9118typedef hash_table<external_ref_hasher> external_ref_hash_type;
9119
9120/* Return a pointer to the external_ref for references to DIE. */
9121
9122static struct external_ref *
9123lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9124{
9125 struct external_ref ref, *ref_p;
9126 external_ref **slot;
9127
9128 ref.type = die;
9129 slot = map->find_slot (value: &ref, insert: INSERT);
9130 if (*slot != HTAB_EMPTY_ENTRY)
9131 return *slot;
9132
9133 ref_p = XCNEW (struct external_ref);
9134 ref_p->type = die;
9135 *slot = ref_p;
9136 return ref_p;
9137}
9138
9139/* Subroutine of optimize_external_refs, below.
9140
9141 If we see a type skeleton, record it as our stub. If we see external
9142 references, remember how many we've seen. */
9143
9144static void
9145optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9146{
9147 dw_die_ref c;
9148 dw_attr_node *a;
9149 unsigned ix;
9150 struct external_ref *ref_p;
9151
9152 if (is_type_die (die)
9153 && (c = get_AT_ref (die, attr_kind: DW_AT_signature)))
9154 {
9155 /* This is a local skeleton; use it for local references. */
9156 ref_p = lookup_external_ref (map, die: c);
9157 ref_p->stub = die;
9158 }
9159
9160 /* Scan the DIE references, and remember any that refer to DIEs from
9161 other CUs (i.e. those which are not marked). */
9162 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9163 if (AT_class (a) == dw_val_class_die_ref
9164 && (c = AT_ref (a))->die_mark == 0
9165 && is_type_die (die: c))
9166 {
9167 ref_p = lookup_external_ref (map, die: c);
9168 ref_p->n_refs++;
9169 }
9170
9171 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9172}
9173
9174/* htab_traverse callback function for optimize_external_refs, below. SLOT
9175 points to an external_ref, DATA is the CU we're processing. If we don't
9176 already have a local stub, and we have multiple refs, build a stub. */
9177
9178int
9179dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9180{
9181 struct external_ref *ref_p = *slot;
9182
9183 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9184 {
9185 /* We have multiple references to this type, so build a small stub.
9186 Both of these forms are a bit dodgy from the perspective of the
9187 DWARF standard, since technically they should have names. */
9188 dw_die_ref cu = data;
9189 dw_die_ref type = ref_p->type;
9190 dw_die_ref stub = NULL;
9191
9192 if (type->comdat_type_p)
9193 {
9194 /* If we refer to this type via sig8, use AT_signature. */
9195 stub = new_die (tag_value: type->die_tag, parent_die: cu, NULL_TREE);
9196 add_AT_die_ref (die: stub, attr_kind: DW_AT_signature, targ_die: type);
9197 }
9198 else
9199 {
9200 /* Otherwise, use a typedef with no name. */
9201 stub = new_die (tag_value: DW_TAG_typedef, parent_die: cu, NULL_TREE);
9202 add_AT_die_ref (die: stub, attr_kind: DW_AT_type, targ_die: type);
9203 }
9204
9205 stub->die_mark++;
9206 ref_p->stub = stub;
9207 }
9208 return 1;
9209}
9210
9211/* DIE is a unit; look through all the DIE references to see if there are
9212 any external references to types, and if so, create local stubs for
9213 them which will be applied in build_abbrev_table. This is useful because
9214 references to local DIEs are smaller. */
9215
9216static external_ref_hash_type *
9217optimize_external_refs (dw_die_ref die)
9218{
9219 external_ref_hash_type *map = new external_ref_hash_type (10);
9220 optimize_external_refs_1 (die, map);
9221 map->traverse <dw_die_ref, dwarf2_build_local_stub> (argument: die);
9222 return map;
9223}
9224
9225/* The following 3 variables are temporaries that are computed only during the
9226 build_abbrev_table call and used and released during the following
9227 optimize_abbrev_table call. */
9228
9229/* First abbrev_id that can be optimized based on usage. */
9230static unsigned int abbrev_opt_start;
9231
9232/* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9233 abbrev_id smaller than this, because they must be already sized
9234 during build_abbrev_table). */
9235static unsigned int abbrev_opt_base_type_end;
9236
9237/* Vector of usage counts during build_abbrev_table. Indexed by
9238 abbrev_id - abbrev_opt_start. */
9239static vec<unsigned int> abbrev_usage_count;
9240
9241/* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9242static vec<dw_die_ref> sorted_abbrev_dies;
9243
9244/* The format of each DIE (and its attribute value pairs) is encoded in an
9245 abbreviation table. This routine builds the abbreviation table and assigns
9246 a unique abbreviation id for each abbreviation entry. The children of each
9247 die are visited recursively. */
9248
9249static void
9250build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9251{
9252 unsigned int abbrev_id = 0;
9253 dw_die_ref c;
9254 dw_attr_node *a;
9255 unsigned ix;
9256 dw_die_ref abbrev;
9257
9258 /* Scan the DIE references, and replace any that refer to
9259 DIEs from other CUs (i.e. those which are not marked) with
9260 the local stubs we built in optimize_external_refs. */
9261 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9262 if (AT_class (a) == dw_val_class_die_ref
9263 && (c = AT_ref (a))->die_mark == 0)
9264 {
9265 struct external_ref *ref_p;
9266 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9267
9268 if (is_type_die (die: c)
9269 && (ref_p = lookup_external_ref (map: extern_map, die: c))
9270 && ref_p->stub && ref_p->stub != die)
9271 {
9272 gcc_assert (a->dw_attr != DW_AT_signature);
9273 change_AT_die_ref (ref: a, new_die: ref_p->stub);
9274 }
9275 else
9276 /* We aren't changing this reference, so mark it external. */
9277 set_AT_ref_external (a, i: 1);
9278 }
9279
9280 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9281 {
9282 dw_attr_node *die_a, *abbrev_a;
9283 unsigned ix;
9284 bool ok = true;
9285
9286 if (abbrev_id == 0)
9287 continue;
9288 if (abbrev->die_tag != die->die_tag)
9289 continue;
9290 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9291 continue;
9292
9293 if (vec_safe_length (v: abbrev->die_attr) != vec_safe_length (v: die->die_attr))
9294 continue;
9295
9296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9297 {
9298 abbrev_a = &(*abbrev->die_attr)[ix];
9299 if ((abbrev_a->dw_attr != die_a->dw_attr)
9300 || (value_format (abbrev_a) != value_format (die_a)))
9301 {
9302 ok = false;
9303 break;
9304 }
9305 }
9306 if (ok)
9307 break;
9308 }
9309
9310 if (abbrev_id >= vec_safe_length (v: abbrev_die_table))
9311 {
9312 vec_safe_push (v&: abbrev_die_table, obj: die);
9313 if (abbrev_opt_start)
9314 abbrev_usage_count.safe_push (obj: 0);
9315 }
9316 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9317 {
9318 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9319 sorted_abbrev_dies.safe_push (obj: die);
9320 }
9321
9322 die->die_abbrev = abbrev_id;
9323 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9324}
9325
9326/* Callback function for sorted_abbrev_dies vector sorting. We sort
9327 by die_abbrev's usage count, from the most commonly used
9328 abbreviation to the least. */
9329
9330static int
9331die_abbrev_cmp (const void *p1, const void *p2)
9332{
9333 dw_die_ref die1 = *(const dw_die_ref *) p1;
9334 dw_die_ref die2 = *(const dw_die_ref *) p2;
9335
9336 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9337 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9338
9339 if (die1->die_abbrev >= abbrev_opt_base_type_end
9340 && die2->die_abbrev >= abbrev_opt_base_type_end)
9341 {
9342 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9343 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9344 return -1;
9345 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9346 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9347 return 1;
9348 }
9349
9350 /* Stabilize the sort. */
9351 if (die1->die_abbrev < die2->die_abbrev)
9352 return -1;
9353 if (die1->die_abbrev > die2->die_abbrev)
9354 return 1;
9355
9356 return 0;
9357}
9358
9359/* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9360 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9361 into dw_val_class_const_implicit or
9362 dw_val_class_unsigned_const_implicit. */
9363
9364static void
9365optimize_implicit_const (unsigned int first_id, unsigned int end,
9366 vec<bool> &implicit_consts)
9367{
9368 /* It never makes sense if there is just one DIE using the abbreviation. */
9369 if (end < first_id + 2)
9370 return;
9371
9372 dw_attr_node *a;
9373 unsigned ix, i;
9374 dw_die_ref die = sorted_abbrev_dies[first_id];
9375 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9376 if (implicit_consts[ix])
9377 {
9378 enum dw_val_class new_class = dw_val_class_none;
9379 switch (AT_class (a))
9380 {
9381 case dw_val_class_unsigned_const:
9382 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9383 continue;
9384
9385 /* The .debug_abbrev section will grow by
9386 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9387 in all the DIEs using that abbreviation. */
9388 if (constant_size (AT_unsigned (a)) * (end - first_id)
9389 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9390 continue;
9391
9392 new_class = dw_val_class_unsigned_const_implicit;
9393 break;
9394
9395 case dw_val_class_const:
9396 new_class = dw_val_class_const_implicit;
9397 break;
9398
9399 case dw_val_class_file:
9400 new_class = dw_val_class_file_implicit;
9401 break;
9402
9403 default:
9404 continue;
9405 }
9406 for (i = first_id; i < end; i++)
9407 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9408 = new_class;
9409 }
9410}
9411
9412/* Attempt to optimize abbreviation table from abbrev_opt_start
9413 abbreviation above. */
9414
9415static void
9416optimize_abbrev_table (void)
9417{
9418 if (abbrev_opt_start
9419 && vec_safe_length (v: abbrev_die_table) > abbrev_opt_start
9420 && (dwarf_version >= 5 || vec_safe_length (v: abbrev_die_table) > 127))
9421 {
9422 auto_vec<bool, 32> implicit_consts;
9423 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9424
9425 unsigned int abbrev_id = abbrev_opt_start - 1;
9426 unsigned int first_id = ~0U;
9427 unsigned int last_abbrev_id = 0;
9428 unsigned int i;
9429 dw_die_ref die;
9430 if (abbrev_opt_base_type_end > abbrev_opt_start)
9431 abbrev_id = abbrev_opt_base_type_end - 1;
9432 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9433 most commonly used abbreviations come first. */
9434 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9435 {
9436 dw_attr_node *a;
9437 unsigned ix;
9438
9439 /* If calc_base_type_die_sizes has been called, the CU and
9440 base types after it can't be optimized, because we've already
9441 calculated their DIE offsets. We've sorted them first. */
9442 if (die->die_abbrev < abbrev_opt_base_type_end)
9443 continue;
9444 if (die->die_abbrev != last_abbrev_id)
9445 {
9446 last_abbrev_id = die->die_abbrev;
9447 if (dwarf_version >= 5 && first_id != ~0U)
9448 optimize_implicit_const (first_id, end: i, implicit_consts);
9449 abbrev_id++;
9450 (*abbrev_die_table)[abbrev_id] = die;
9451 if (dwarf_version >= 5)
9452 {
9453 first_id = i;
9454 implicit_consts.truncate (size: 0);
9455
9456 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9457 switch (AT_class (a))
9458 {
9459 case dw_val_class_const:
9460 case dw_val_class_unsigned_const:
9461 case dw_val_class_file:
9462 implicit_consts.safe_push (obj: true);
9463 break;
9464 default:
9465 implicit_consts.safe_push (obj: false);
9466 break;
9467 }
9468 }
9469 }
9470 else if (dwarf_version >= 5)
9471 {
9472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9473 if (!implicit_consts[ix])
9474 continue;
9475 else
9476 {
9477 dw_attr_node *other_a
9478 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9479 if (!dw_val_equal_p (a: &a->dw_attr_val,
9480 b: &other_a->dw_attr_val))
9481 implicit_consts[ix] = false;
9482 }
9483 }
9484 die->die_abbrev = abbrev_id;
9485 }
9486 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9487 if (dwarf_version >= 5 && first_id != ~0U)
9488 optimize_implicit_const (first_id, end: i, implicit_consts);
9489 }
9490
9491 abbrev_opt_start = 0;
9492 abbrev_opt_base_type_end = 0;
9493 abbrev_usage_count.release ();
9494 sorted_abbrev_dies.release ();
9495}
9496
9497/* Return the power-of-two number of bytes necessary to represent VALUE. */
9498
9499static int
9500constant_size (unsigned HOST_WIDE_INT value)
9501{
9502 int log;
9503
9504 if (value == 0)
9505 log = 0;
9506 else
9507 log = floor_log2 (x: value);
9508
9509 log = log / 8;
9510 log = 1 << (floor_log2 (x: log) + 1);
9511
9512 return log;
9513}
9514
9515/* Return the size of a DIE as it is represented in the
9516 .debug_info section. */
9517
9518static unsigned long
9519size_of_die (dw_die_ref die)
9520{
9521 unsigned long size = 0;
9522 dw_attr_node *a;
9523 unsigned ix;
9524 enum dwarf_form form;
9525
9526 size += size_of_uleb128 (die->die_abbrev);
9527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9528 {
9529 switch (AT_class (a))
9530 {
9531 case dw_val_class_addr:
9532 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9533 {
9534 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9535 size += size_of_uleb128 (AT_index (a));
9536 }
9537 else
9538 size += DWARF2_ADDR_SIZE;
9539 break;
9540 case dw_val_class_offset:
9541 size += dwarf_offset_size;
9542 break;
9543 case dw_val_class_loc:
9544 {
9545 unsigned long lsize = size_of_locs (loc: AT_loc (a));
9546
9547 /* Block length. */
9548 if (dwarf_version >= 4)
9549 size += size_of_uleb128 (lsize);
9550 else
9551 size += constant_size (value: lsize);
9552 size += lsize;
9553 }
9554 break;
9555 case dw_val_class_loc_list:
9556 if (dwarf_split_debug_info && dwarf_version >= 5)
9557 {
9558 gcc_assert (AT_loc_list (a)->num_assigned);
9559 size += size_of_uleb128 (AT_loc_list (a)->hash);
9560 }
9561 else
9562 size += dwarf_offset_size;
9563 break;
9564 case dw_val_class_view_list:
9565 size += dwarf_offset_size;
9566 break;
9567 case dw_val_class_range_list:
9568 if (value_format (a) == DW_FORM_rnglistx)
9569 {
9570 gcc_assert (rnglist_idx);
9571 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9572 size += size_of_uleb128 (r->idx);
9573 }
9574 else
9575 size += dwarf_offset_size;
9576 break;
9577 case dw_val_class_const:
9578 size += size_of_sleb128 (AT_int (a));
9579 break;
9580 case dw_val_class_unsigned_const:
9581 {
9582 int csize = constant_size (value: AT_unsigned (a));
9583 if (dwarf_version == 3
9584 && a->dw_attr == DW_AT_data_member_location
9585 && csize >= 4)
9586 size += size_of_uleb128 (AT_unsigned (a));
9587 else
9588 size += csize;
9589 }
9590 break;
9591 case dw_val_class_symview:
9592 if (symview_upper_bound <= 0xff)
9593 size += 1;
9594 else if (symview_upper_bound <= 0xffff)
9595 size += 2;
9596 else if (symview_upper_bound <= 0xffffffff)
9597 size += 4;
9598 else
9599 size += 8;
9600 break;
9601 case dw_val_class_const_implicit:
9602 case dw_val_class_unsigned_const_implicit:
9603 case dw_val_class_file_implicit:
9604 /* These occupy no size in the DIE, just an extra sleb128 in
9605 .debug_abbrev. */
9606 break;
9607 case dw_val_class_const_double:
9608 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9609 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9610 size++; /* block */
9611 break;
9612 case dw_val_class_wide_int:
9613 size += (get_full_len (op: *a->dw_attr_val.v.val_wide)
9614 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9615 if (get_full_len (op: *a->dw_attr_val.v.val_wide)
9616 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9617 size++; /* block */
9618 break;
9619 case dw_val_class_vec:
9620 size += constant_size (value: a->dw_attr_val.v.val_vec.length
9621 * a->dw_attr_val.v.val_vec.elt_size)
9622 + a->dw_attr_val.v.val_vec.length
9623 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9624 break;
9625 case dw_val_class_flag:
9626 if (dwarf_version >= 4)
9627 /* Currently all add_AT_flag calls pass in 1 as last argument,
9628 so DW_FORM_flag_present can be used. If that ever changes,
9629 we'll need to use DW_FORM_flag and have some optimization
9630 in build_abbrev_table that will change those to
9631 DW_FORM_flag_present if it is set to 1 in all DIEs using
9632 the same abbrev entry. */
9633 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9634 else
9635 size += 1;
9636 break;
9637 case dw_val_class_die_ref:
9638 if (AT_ref_external (a))
9639 {
9640 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9641 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9642 is sized by target address length, whereas in DWARF3
9643 it's always sized as an offset. */
9644 if (AT_ref (a)->comdat_type_p)
9645 size += DWARF_TYPE_SIGNATURE_SIZE;
9646 else if (dwarf_version == 2)
9647 size += DWARF2_ADDR_SIZE;
9648 else
9649 size += dwarf_offset_size;
9650 }
9651 else
9652 size += dwarf_offset_size;
9653 break;
9654 case dw_val_class_fde_ref:
9655 size += dwarf_offset_size;
9656 break;
9657 case dw_val_class_lbl_id:
9658 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9659 {
9660 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9661 size += size_of_uleb128 (AT_index (a));
9662 }
9663 else
9664 size += DWARF2_ADDR_SIZE;
9665 break;
9666 case dw_val_class_lineptr:
9667 case dw_val_class_macptr:
9668 case dw_val_class_loclistsptr:
9669 size += dwarf_offset_size;
9670 break;
9671 case dw_val_class_str:
9672 form = AT_string_form (a);
9673 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9674 size += dwarf_offset_size;
9675 else if (form == dwarf_FORM (form: DW_FORM_strx))
9676 size += size_of_uleb128 (AT_index (a));
9677 else
9678 size += strlen (s: a->dw_attr_val.v.val_str->str) + 1;
9679 break;
9680 case dw_val_class_file:
9681 size += constant_size (value: maybe_emit_file (fd: a->dw_attr_val.v.val_file));
9682 break;
9683 case dw_val_class_data8:
9684 size += 8;
9685 break;
9686 case dw_val_class_vms_delta:
9687 size += dwarf_offset_size;
9688 break;
9689 case dw_val_class_high_pc:
9690 size += DWARF2_ADDR_SIZE;
9691 break;
9692 case dw_val_class_discr_value:
9693 size += size_of_discr_value (discr_value: &a->dw_attr_val.v.val_discr_value);
9694 break;
9695 case dw_val_class_discr_list:
9696 {
9697 unsigned block_size = size_of_discr_list (discr_list: AT_discr_list (a));
9698
9699 /* This is a block, so we have the block length and then its
9700 data. */
9701 size += constant_size (value: block_size) + block_size;
9702 }
9703 break;
9704 default:
9705 gcc_unreachable ();
9706 }
9707 }
9708
9709 return size;
9710}
9711
9712/* Size the debugging information associated with a given DIE. Visits the
9713 DIE's children recursively. Updates the global variable next_die_offset, on
9714 each time through. Uses the current value of next_die_offset to update the
9715 die_offset field in each DIE. */
9716
9717static void
9718calc_die_sizes (dw_die_ref die)
9719{
9720 dw_die_ref c;
9721
9722 gcc_assert (die->die_offset == 0
9723 || (unsigned long int) die->die_offset == next_die_offset);
9724 die->die_offset = next_die_offset;
9725 next_die_offset += size_of_die (die);
9726
9727 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9728
9729 if (die->die_child != NULL)
9730 /* Count the null byte used to terminate sibling lists. */
9731 next_die_offset += 1;
9732}
9733
9734/* Size just the base type children at the start of the CU.
9735 This is needed because build_abbrev needs to size locs
9736 and sizing of type based stack ops needs to know die_offset
9737 values for the base types. */
9738
9739static void
9740calc_base_type_die_sizes (void)
9741{
9742 unsigned long die_offset = (dwarf_split_debug_info
9743 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9744 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9745 unsigned int i;
9746 dw_die_ref base_type;
9747#if ENABLE_ASSERT_CHECKING
9748 dw_die_ref prev = comp_unit_die ()->die_child;
9749#endif
9750
9751 die_offset += size_of_die (die: comp_unit_die ());
9752 for (i = 0; base_types.iterate (ix: i, ptr: &base_type); i++)
9753 {
9754#if ENABLE_ASSERT_CHECKING
9755 gcc_assert (base_type->die_offset == 0
9756 && prev->die_sib == base_type
9757 && base_type->die_child == NULL
9758 && base_type->die_abbrev);
9759 prev = base_type;
9760#endif
9761 if (abbrev_opt_start
9762 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9763 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9764 base_type->die_offset = die_offset;
9765 die_offset += size_of_die (die: base_type);
9766 }
9767}
9768
9769/* Set the marks for a die and its children. We do this so
9770 that we know whether or not a reference needs to use FORM_ref_addr; only
9771 DIEs in the same CU will be marked. We used to clear out the offset
9772 and use that as the flag, but ran into ordering problems. */
9773
9774static void
9775mark_dies (dw_die_ref die)
9776{
9777 dw_die_ref c;
9778
9779 gcc_assert (!die->die_mark);
9780
9781 die->die_mark = 1;
9782 FOR_EACH_CHILD (die, c, mark_dies (c));
9783}
9784
9785/* Clear the marks for a die and its children. */
9786
9787static void
9788unmark_dies (dw_die_ref die)
9789{
9790 dw_die_ref c;
9791
9792 if (! use_debug_types)
9793 gcc_assert (die->die_mark);
9794
9795 die->die_mark = 0;
9796 FOR_EACH_CHILD (die, c, unmark_dies (c));
9797}
9798
9799/* Clear the marks for a die, its children and referred dies. */
9800
9801static void
9802unmark_all_dies (dw_die_ref die)
9803{
9804 dw_die_ref c;
9805 dw_attr_node *a;
9806 unsigned ix;
9807
9808 if (!die->die_mark)
9809 return;
9810 die->die_mark = 0;
9811
9812 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9813
9814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9815 if (AT_class (a) == dw_val_class_die_ref)
9816 unmark_all_dies (die: AT_ref (a));
9817}
9818
9819/* Calculate if the entry should appear in the final output file. It may be
9820 from a pruned a type. */
9821
9822static bool
9823include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9824{
9825 /* By limiting gnu pubnames to definitions only, gold can generate a
9826 gdb index without entries for declarations, which don't include
9827 enough information to be useful. */
9828 if (debug_generate_pub_sections == 2 && is_declaration_die (die: p->die))
9829 return false;
9830
9831 if (table == pubname_table)
9832 {
9833 /* Enumerator names are part of the pubname table, but the
9834 parent DW_TAG_enumeration_type die may have been pruned.
9835 Don't output them if that is the case. */
9836 if (p->die->die_tag == DW_TAG_enumerator &&
9837 (p->die->die_parent == NULL
9838 || !p->die->die_parent->die_perennial_p))
9839 return false;
9840
9841 /* Everything else in the pubname table is included. */
9842 return true;
9843 }
9844
9845 /* The pubtypes table shouldn't include types that have been
9846 pruned. */
9847 return (p->die->die_offset != 0
9848 || !flag_eliminate_unused_debug_types);
9849}
9850
9851/* Return the size of the .debug_pubnames or .debug_pubtypes table
9852 generated for the compilation unit. */
9853
9854static unsigned long
9855size_of_pubnames (vec<pubname_entry, va_gc> *names)
9856{
9857 unsigned long size;
9858 unsigned i;
9859 pubname_entry *p;
9860 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9861
9862 size = DWARF_PUBNAMES_HEADER_SIZE;
9863 FOR_EACH_VEC_ELT (*names, i, p)
9864 if (include_pubname_in_output (table: names, p))
9865 size += strlen (s: p->name) + dwarf_offset_size + 1 + space_for_flags;
9866
9867 size += dwarf_offset_size;
9868 return size;
9869}
9870
9871/* Return the size of the information in the .debug_aranges section. */
9872
9873static unsigned long
9874size_of_aranges (void)
9875{
9876 unsigned long size;
9877
9878 size = DWARF_ARANGES_HEADER_SIZE;
9879
9880 /* Count the address/length pair for this compilation unit. */
9881 if (switch_text_ranges)
9882 size += 2 * DWARF2_ADDR_SIZE
9883 * (vec_safe_length (v: switch_text_ranges) / 2 + 1);
9884 if (switch_cold_ranges)
9885 size += 2 * DWARF2_ADDR_SIZE
9886 * (vec_safe_length (v: switch_cold_ranges) / 2 + 1);
9887 if (have_multiple_function_sections)
9888 {
9889 unsigned fde_idx;
9890 dw_fde_ref fde;
9891
9892 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9893 {
9894 if (fde->ignored_debug)
9895 continue;
9896 if (!fde->in_std_section)
9897 size += 2 * DWARF2_ADDR_SIZE;
9898 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9899 size += 2 * DWARF2_ADDR_SIZE;
9900 }
9901 }
9902
9903 /* Count the two zero words used to terminated the address range table. */
9904 size += 2 * DWARF2_ADDR_SIZE;
9905 return size;
9906}
9907
9908/* Select the encoding of an attribute value. */
9909
9910static enum dwarf_form
9911value_format (dw_attr_node *a)
9912{
9913 switch (AT_class (a))
9914 {
9915 case dw_val_class_addr:
9916 /* Only very few attributes allow DW_FORM_addr. */
9917 switch (a->dw_attr)
9918 {
9919 case DW_AT_low_pc:
9920 case DW_AT_high_pc:
9921 case DW_AT_entry_pc:
9922 case DW_AT_trampoline:
9923 return (AT_index (a) == NOT_INDEXED
9924 ? DW_FORM_addr : dwarf_FORM (form: DW_FORM_addrx));
9925 default:
9926 break;
9927 }
9928 switch (DWARF2_ADDR_SIZE)
9929 {
9930 case 1:
9931 return DW_FORM_data1;
9932 case 2:
9933 return DW_FORM_data2;
9934 case 4:
9935 return DW_FORM_data4;
9936 case 8:
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9940 }
9941 case dw_val_class_loc_list:
9942 if (dwarf_split_debug_info
9943 && dwarf_version >= 5
9944 && AT_loc_list (a)->num_assigned)
9945 return DW_FORM_loclistx;
9946 /* FALLTHRU */
9947 case dw_val_class_view_list:
9948 case dw_val_class_range_list:
9949 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9950 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9951 care about sizes of .debug* sections in shared libraries and
9952 executables and don't take into account relocations that affect just
9953 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9954 table in the .debug_rnglists section. */
9955 if (dwarf_split_debug_info
9956 && dwarf_version >= 5
9957 && AT_class (a) == dw_val_class_range_list
9958 && rnglist_idx
9959 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9960 return DW_FORM_rnglistx;
9961 if (dwarf_version >= 4)
9962 return DW_FORM_sec_offset;
9963 /* FALLTHRU */
9964 case dw_val_class_vms_delta:
9965 case dw_val_class_offset:
9966 switch (dwarf_offset_size)
9967 {
9968 case 4:
9969 return DW_FORM_data4;
9970 case 8:
9971 return DW_FORM_data8;
9972 default:
9973 gcc_unreachable ();
9974 }
9975 case dw_val_class_loc:
9976 if (dwarf_version >= 4)
9977 return DW_FORM_exprloc;
9978 switch (constant_size (value: size_of_locs (loc: AT_loc (a))))
9979 {
9980 case 1:
9981 return DW_FORM_block1;
9982 case 2:
9983 return DW_FORM_block2;
9984 case 4:
9985 return DW_FORM_block4;
9986 default:
9987 gcc_unreachable ();
9988 }
9989 case dw_val_class_const:
9990 return DW_FORM_sdata;
9991 case dw_val_class_unsigned_const:
9992 switch (constant_size (value: AT_unsigned (a)))
9993 {
9994 case 1:
9995 return DW_FORM_data1;
9996 case 2:
9997 return DW_FORM_data2;
9998 case 4:
9999 /* In DWARF3 DW_AT_data_member_location with
10000 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10001 constant, so we need to use DW_FORM_udata if we need
10002 a large constant. */
10003 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10004 return DW_FORM_udata;
10005 return DW_FORM_data4;
10006 case 8:
10007 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10008 return DW_FORM_udata;
10009 return DW_FORM_data8;
10010 default:
10011 gcc_unreachable ();
10012 }
10013 case dw_val_class_const_implicit:
10014 case dw_val_class_unsigned_const_implicit:
10015 case dw_val_class_file_implicit:
10016 return DW_FORM_implicit_const;
10017 case dw_val_class_const_double:
10018 switch (HOST_BITS_PER_WIDE_INT)
10019 {
10020 case 8:
10021 return DW_FORM_data2;
10022 case 16:
10023 return DW_FORM_data4;
10024 case 32:
10025 return DW_FORM_data8;
10026 case 64:
10027 if (dwarf_version >= 5)
10028 return DW_FORM_data16;
10029 /* FALLTHRU */
10030 default:
10031 return DW_FORM_block1;
10032 }
10033 case dw_val_class_wide_int:
10034 switch (get_full_len (op: *a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10035 {
10036 case 8:
10037 return DW_FORM_data1;
10038 case 16:
10039 return DW_FORM_data2;
10040 case 32:
10041 return DW_FORM_data4;
10042 case 64:
10043 return DW_FORM_data8;
10044 case 128:
10045 if (dwarf_version >= 5)
10046 return DW_FORM_data16;
10047 /* FALLTHRU */
10048 default:
10049 return DW_FORM_block1;
10050 }
10051 case dw_val_class_symview:
10052 /* ??? We might use uleb128, but then we'd have to compute
10053 .debug_info offsets in the assembler. */
10054 if (symview_upper_bound <= 0xff)
10055 return DW_FORM_data1;
10056 else if (symview_upper_bound <= 0xffff)
10057 return DW_FORM_data2;
10058 else if (symview_upper_bound <= 0xffffffff)
10059 return DW_FORM_data4;
10060 else
10061 return DW_FORM_data8;
10062 case dw_val_class_vec:
10063 switch (constant_size (value: a->dw_attr_val.v.val_vec.length
10064 * a->dw_attr_val.v.val_vec.elt_size))
10065 {
10066 case 1:
10067 return DW_FORM_block1;
10068 case 2:
10069 return DW_FORM_block2;
10070 case 4:
10071 return DW_FORM_block4;
10072 default:
10073 gcc_unreachable ();
10074 }
10075 case dw_val_class_flag:
10076 if (dwarf_version >= 4)
10077 {
10078 /* Currently all add_AT_flag calls pass in 1 as last argument,
10079 so DW_FORM_flag_present can be used. If that ever changes,
10080 we'll need to use DW_FORM_flag and have some optimization
10081 in build_abbrev_table that will change those to
10082 DW_FORM_flag_present if it is set to 1 in all DIEs using
10083 the same abbrev entry. */
10084 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10085 return DW_FORM_flag_present;
10086 }
10087 return DW_FORM_flag;
10088 case dw_val_class_die_ref:
10089 if (AT_ref_external (a))
10090 {
10091 if (AT_ref (a)->comdat_type_p)
10092 return DW_FORM_ref_sig8;
10093 else
10094 return DW_FORM_ref_addr;
10095 }
10096 else
10097 return DW_FORM_ref;
10098 case dw_val_class_fde_ref:
10099 return DW_FORM_data;
10100 case dw_val_class_lbl_id:
10101 return (AT_index (a) == NOT_INDEXED
10102 ? DW_FORM_addr : dwarf_FORM (form: DW_FORM_addrx));
10103 case dw_val_class_lineptr:
10104 case dw_val_class_macptr:
10105 case dw_val_class_loclistsptr:
10106 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10107 case dw_val_class_str:
10108 return AT_string_form (a);
10109 case dw_val_class_file:
10110 switch (constant_size (value: maybe_emit_file (fd: a->dw_attr_val.v.val_file)))
10111 {
10112 case 1:
10113 return DW_FORM_data1;
10114 case 2:
10115 return DW_FORM_data2;
10116 case 4:
10117 return DW_FORM_data4;
10118 default:
10119 gcc_unreachable ();
10120 }
10121
10122 case dw_val_class_data8:
10123 return DW_FORM_data8;
10124
10125 case dw_val_class_high_pc:
10126 switch (DWARF2_ADDR_SIZE)
10127 {
10128 case 1:
10129 return DW_FORM_data1;
10130 case 2:
10131 return DW_FORM_data2;
10132 case 4:
10133 return DW_FORM_data4;
10134 case 8:
10135 return DW_FORM_data8;
10136 default:
10137 gcc_unreachable ();
10138 }
10139
10140 case dw_val_class_discr_value:
10141 return (a->dw_attr_val.v.val_discr_value.pos
10142 ? DW_FORM_udata
10143 : DW_FORM_sdata);
10144 case dw_val_class_discr_list:
10145 switch (constant_size (value: size_of_discr_list (discr_list: AT_discr_list (a))))
10146 {
10147 case 1:
10148 return DW_FORM_block1;
10149 case 2:
10150 return DW_FORM_block2;
10151 case 4:
10152 return DW_FORM_block4;
10153 default:
10154 gcc_unreachable ();
10155 }
10156
10157 default:
10158 gcc_unreachable ();
10159 }
10160}
10161
10162/* Output the encoding of an attribute value. */
10163
10164static void
10165output_value_format (dw_attr_node *a)
10166{
10167 enum dwarf_form form = value_format (a);
10168
10169 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10170}
10171
10172/* Given a die and id, produce the appropriate abbreviations. */
10173
10174static void
10175output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10176{
10177 unsigned ix;
10178 dw_attr_node *a_attr;
10179
10180 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10181 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10182 dwarf_tag_name (tag: abbrev->die_tag));
10183
10184 if (abbrev->die_child != NULL)
10185 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10186 else
10187 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10188
10189 for (ix = 0; vec_safe_iterate (v: abbrev->die_attr, ix, ptr: &a_attr); ix++)
10190 {
10191 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10192 dwarf_attr_name (attr: a_attr->dw_attr));
10193 output_value_format (a: a_attr);
10194 if (value_format (a: a_attr) == DW_FORM_implicit_const)
10195 {
10196 if (AT_class (a: a_attr) == dw_val_class_file_implicit)
10197 {
10198 int f = maybe_emit_file (fd: a_attr->dw_attr_val.v.val_file);
10199 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10200 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10201 }
10202 else
10203 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10204 }
10205 }
10206
10207 dw2_asm_output_data (1, 0, NULL);
10208 dw2_asm_output_data (1, 0, NULL);
10209}
10210
10211
10212/* Output the .debug_abbrev section which defines the DIE abbreviation
10213 table. */
10214
10215static void
10216output_abbrev_section (void)
10217{
10218 unsigned int abbrev_id;
10219 dw_die_ref abbrev;
10220
10221 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10222 if (abbrev_id != 0)
10223 output_die_abbrevs (abbrev_id, abbrev);
10224
10225 /* Terminate the table. */
10226 dw2_asm_output_data (1, 0, NULL);
10227}
10228
10229/* Return a new location list, given the begin and end range, and the
10230 expression. */
10231
10232static inline dw_loc_list_ref
10233new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10234 const char *end, var_loc_view vend,
10235 const char *section)
10236{
10237 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10238
10239 retlist->begin = begin;
10240 retlist->begin_entry = NULL;
10241 retlist->end = end;
10242 retlist->end_entry = NULL;
10243 retlist->expr = expr;
10244 retlist->section = section;
10245 retlist->vbegin = vbegin;
10246 retlist->vend = vend;
10247
10248 return retlist;
10249}
10250
10251/* Return true iff there's any nonzero view number in the loc list.
10252
10253 ??? When views are not enabled, we'll often extend a single range
10254 to the entire function, so that we emit a single location
10255 expression rather than a location list. With views, even with a
10256 single range, we'll output a list if start or end have a nonzero
10257 view. If we change this, we may want to stop splitting a single
10258 range in dw_loc_list just because of a nonzero view, even if it
10259 straddles across hot/cold partitions. */
10260
10261static bool
10262loc_list_has_views (dw_loc_list_ref list)
10263{
10264 if (!debug_variable_location_views)
10265 return false;
10266
10267 for (dw_loc_list_ref loc = list;
10268 loc != NULL; loc = loc->dw_loc_next)
10269 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10270 return true;
10271
10272 return false;
10273}
10274
10275/* Generate a new internal symbol for this location list node, if it
10276 hasn't got one yet. */
10277
10278static inline void
10279gen_llsym (dw_loc_list_ref list)
10280{
10281 gcc_assert (!list->ll_symbol);
10282 list->ll_symbol = gen_internal_sym (prefix: "LLST");
10283
10284 if (!loc_list_has_views (list))
10285 return;
10286
10287 if (dwarf2out_locviews_in_attribute ())
10288 {
10289 /* Use the same label_num for the view list. */
10290 label_num--;
10291 list->vl_symbol = gen_internal_sym (prefix: "LVUS");
10292 }
10293 else
10294 list->vl_symbol = list->ll_symbol;
10295}
10296
10297/* Generate a symbol for the list, but only if we really want to emit
10298 it as a list. */
10299
10300static inline void
10301maybe_gen_llsym (dw_loc_list_ref list)
10302{
10303 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10304 return;
10305
10306 gen_llsym (list);
10307}
10308
10309/* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10310 NULL, don't consider size of the location expression. If we're not
10311 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10312 representation in *SIZEP. */
10313
10314static bool
10315skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10316{
10317 /* Don't output an entry that starts and ends at the same address. */
10318 if (strcmp (s1: curr->begin, s2: curr->end) == 0
10319 && curr->vbegin == curr->vend && !curr->force)
10320 return true;
10321
10322 if (!sizep)
10323 return false;
10324
10325 unsigned long size = size_of_locs (loc: curr->expr);
10326
10327 /* If the expression is too large, drop it on the floor. We could
10328 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10329 in the expression, but >= 64KB expressions for a single value
10330 in a single range are unlikely very useful. */
10331 if (dwarf_version < 5 && size > 0xffff)
10332 return true;
10333
10334 *sizep = size;
10335
10336 return false;
10337}
10338
10339/* Output a view pair loclist entry for CURR, if it requires one. */
10340
10341static void
10342dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10343{
10344 if (!dwarf2out_locviews_in_loclist ())
10345 return;
10346
10347 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10348 return;
10349
10350#ifdef DW_LLE_view_pair
10351 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10352
10353 if (dwarf2out_as_locview_support)
10354 {
10355 if (ZERO_VIEW_P (curr->vbegin))
10356 dw2_asm_output_data_uleb128 (0, "Location view begin");
10357 else
10358 {
10359 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10360 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10361 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10362 }
10363
10364 if (ZERO_VIEW_P (curr->vend))
10365 dw2_asm_output_data_uleb128 (0, "Location view end");
10366 else
10367 {
10368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10369 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10370 dw2_asm_output_symname_uleb128 (label, "Location view end");
10371 }
10372 }
10373 else
10374 {
10375 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10376 ? 0 : curr->vbegin, "Location view begin");
10377 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10378 ? 0 : curr->vend, "Location view end");
10379 }
10380#endif /* DW_LLE_view_pair */
10381
10382 return;
10383}
10384
10385/* Output the location list given to us. */
10386
10387static void
10388output_loc_list (dw_loc_list_ref list_head)
10389{
10390 int vcount = 0, lcount = 0;
10391
10392 if (list_head->emitted)
10393 return;
10394 list_head->emitted = true;
10395
10396 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10397 {
10398 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10399
10400 for (dw_loc_list_ref curr = list_head; curr != NULL;
10401 curr = curr->dw_loc_next)
10402 {
10403 unsigned long size;
10404
10405 if (skip_loc_list_entry (curr, sizep: &size))
10406 continue;
10407
10408 vcount++;
10409
10410 /* ?? dwarf_split_debug_info? */
10411 if (dwarf2out_as_locview_support)
10412 {
10413 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10414
10415 if (!ZERO_VIEW_P (curr->vbegin))
10416 {
10417 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10418 dw2_asm_output_symname_uleb128 (label,
10419 "View list begin (%s)",
10420 list_head->vl_symbol);
10421 }
10422 else
10423 dw2_asm_output_data_uleb128 (0,
10424 "View list begin (%s)",
10425 list_head->vl_symbol);
10426
10427 if (!ZERO_VIEW_P (curr->vend))
10428 {
10429 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10430 dw2_asm_output_symname_uleb128 (label,
10431 "View list end (%s)",
10432 list_head->vl_symbol);
10433 }
10434 else
10435 dw2_asm_output_data_uleb128 (0,
10436 "View list end (%s)",
10437 list_head->vl_symbol);
10438 }
10439 else
10440 {
10441 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vbegin)
10442 ? 0 : curr->vbegin,
10443 "View list begin (%s)",
10444 list_head->vl_symbol);
10445 dw2_asm_output_data_uleb128 (ZERO_VIEW_P (curr->vend)
10446 ? 0 : curr->vend,
10447 "View list end (%s)",
10448 list_head->vl_symbol);
10449 }
10450 }
10451 }
10452
10453 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10454
10455 const char *last_section = NULL;
10456 const char *base_label = NULL;
10457
10458 /* Walk the location list, and output each range + expression. */
10459 for (dw_loc_list_ref curr = list_head; curr != NULL;
10460 curr = curr->dw_loc_next)
10461 {
10462 unsigned long size;
10463
10464 /* Skip this entry? If we skip it here, we must skip it in the
10465 view list above as well. */
10466 if (skip_loc_list_entry (curr, sizep: &size))
10467 continue;
10468
10469 lcount++;
10470
10471 if (dwarf_version >= 5)
10472 {
10473 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10474 {
10475 dwarf2out_maybe_output_loclist_view_pair (curr);
10476 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10477 uleb128 index into .debug_addr and uleb128 length. */
10478 dw2_asm_output_data (1, DW_LLE_startx_length,
10479 "DW_LLE_startx_length (%s)",
10480 list_head->ll_symbol);
10481 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10482 "Location list range start index "
10483 "(%s)", curr->begin);
10484 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10485 "Location list length (%s)",
10486 list_head->ll_symbol);
10487 }
10488 else if (dwarf_split_debug_info)
10489 {
10490 dwarf2out_maybe_output_loclist_view_pair (curr);
10491 /* For -gsplit-dwarf without usable .uleb128 support, emit
10492 DW_LLE_startx_endx, which has two uleb128 indexes into
10493 .debug_addr. */
10494 dw2_asm_output_data (1, DW_LLE_startx_endx,
10495 "DW_LLE_startx_endx (%s)",
10496 list_head->ll_symbol);
10497 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10498 "Location list range start index "
10499 "(%s)", curr->begin);
10500 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10501 "Location list range end index "
10502 "(%s)", curr->end);
10503 }
10504 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10505 {
10506 dwarf2out_maybe_output_loclist_view_pair (curr);
10507 /* If all code is in .text section, the base address is
10508 already provided by the CU attributes. Use
10509 DW_LLE_offset_pair where both addresses are uleb128 encoded
10510 offsets against that base. */
10511 dw2_asm_output_data (1, DW_LLE_offset_pair,
10512 "DW_LLE_offset_pair (%s)",
10513 list_head->ll_symbol);
10514 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10515 "Location list begin address (%s)",
10516 list_head->ll_symbol);
10517 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10518 "Location list end address (%s)",
10519 list_head->ll_symbol);
10520 }
10521 else if (HAVE_AS_LEB128)
10522 {
10523 /* Otherwise, find out how many consecutive entries could share
10524 the same base entry. If just one, emit DW_LLE_start_length,
10525 otherwise emit DW_LLE_base_address for the base address
10526 followed by a series of DW_LLE_offset_pair. */
10527 if (last_section == NULL || curr->section != last_section)
10528 {
10529 dw_loc_list_ref curr2;
10530 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10531 curr2 = curr2->dw_loc_next)
10532 {
10533 if (strcmp (s1: curr2->begin, s2: curr2->end) == 0
10534 && !curr2->force)
10535 continue;
10536 break;
10537 }
10538 if (curr2 == NULL || curr->section != curr2->section)
10539 last_section = NULL;
10540 else
10541 {
10542 last_section = curr->section;
10543 base_label = curr->begin;
10544 dw2_asm_output_data (1, DW_LLE_base_address,
10545 "DW_LLE_base_address (%s)",
10546 list_head->ll_symbol);
10547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10548 "Base address (%s)",
10549 list_head->ll_symbol);
10550 }
10551 }
10552 /* Only one entry with the same base address. Use
10553 DW_LLE_start_length with absolute address and uleb128
10554 length. */
10555 if (last_section == NULL)
10556 {
10557 dwarf2out_maybe_output_loclist_view_pair (curr);
10558 dw2_asm_output_data (1, DW_LLE_start_length,
10559 "DW_LLE_start_length (%s)",
10560 list_head->ll_symbol);
10561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10562 "Location list begin address (%s)",
10563 list_head->ll_symbol);
10564 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10565 "Location list length "
10566 "(%s)", list_head->ll_symbol);
10567 }
10568 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10569 DW_LLE_base_address. */
10570 else
10571 {
10572 dwarf2out_maybe_output_loclist_view_pair (curr);
10573 dw2_asm_output_data (1, DW_LLE_offset_pair,
10574 "DW_LLE_offset_pair (%s)",
10575 list_head->ll_symbol);
10576 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10577 "Location list begin address "
10578 "(%s)", list_head->ll_symbol);
10579 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10580 "Location list end address "
10581 "(%s)", list_head->ll_symbol);
10582 }
10583 }
10584 /* The assembler does not support .uleb128 directive. Emit
10585 DW_LLE_start_end with a pair of absolute addresses. */
10586 else
10587 {
10588 dwarf2out_maybe_output_loclist_view_pair (curr);
10589 dw2_asm_output_data (1, DW_LLE_start_end,
10590 "DW_LLE_start_end (%s)",
10591 list_head->ll_symbol);
10592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10593 "Location list begin address (%s)",
10594 list_head->ll_symbol);
10595 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10596 "Location list end address (%s)",
10597 list_head->ll_symbol);
10598 }
10599 }
10600 else if (dwarf_split_debug_info)
10601 {
10602 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10603 and 4 byte length. */
10604 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10605 "Location list start/length entry (%s)",
10606 list_head->ll_symbol);
10607 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10608 "Location list range start index (%s)",
10609 curr->begin);
10610 /* The length field is 4 bytes. If we ever need to support
10611 an 8-byte length, we can add a new DW_LLE code or fall back
10612 to DW_LLE_GNU_start_end_entry. */
10613 dw2_asm_output_delta (4, curr->end, curr->begin,
10614 "Location list range length (%s)",
10615 list_head->ll_symbol);
10616 }
10617 else if (!have_multiple_function_sections)
10618 {
10619 /* Pair of relative addresses against start of text section. */
10620 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10621 "Location list begin address (%s)",
10622 list_head->ll_symbol);
10623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10624 "Location list end address (%s)",
10625 list_head->ll_symbol);
10626 }
10627 else
10628 {
10629 /* Pair of absolute addresses. */
10630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10631 "Location list begin address (%s)",
10632 list_head->ll_symbol);
10633 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10634 "Location list end address (%s)",
10635 list_head->ll_symbol);
10636 }
10637
10638 /* Output the block length for this list of location operations. */
10639 if (dwarf_version >= 5)
10640 dw2_asm_output_data_uleb128 (size, "Location expression size");
10641 else
10642 {
10643 gcc_assert (size <= 0xffff);
10644 dw2_asm_output_data (2, size, "Location expression size");
10645 }
10646
10647 output_loc_sequence (loc: curr->expr, for_eh_or_skip: -1);
10648 }
10649
10650 /* And finally list termination. */
10651 if (dwarf_version >= 5)
10652 dw2_asm_output_data (1, DW_LLE_end_of_list,
10653 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10654 else if (dwarf_split_debug_info)
10655 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10656 "Location list terminator (%s)",
10657 list_head->ll_symbol);
10658 else
10659 {
10660 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10661 "Location list terminator begin (%s)",
10662 list_head->ll_symbol);
10663 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10664 "Location list terminator end (%s)",
10665 list_head->ll_symbol);
10666 }
10667
10668 gcc_assert (!list_head->vl_symbol
10669 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10670}
10671
10672/* Output a range_list offset into the .debug_ranges or .debug_rnglists
10673 section. Emit a relocated reference if val_entry is NULL, otherwise,
10674 emit an indirect reference. */
10675
10676static void
10677output_range_list_offset (dw_attr_node *a)
10678{
10679 const char *name = dwarf_attr_name (attr: a->dw_attr);
10680
10681 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10682 {
10683 if (dwarf_version >= 5)
10684 {
10685 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10686 dw2_asm_output_offset (dwarf_offset_size, r->label,
10687 debug_ranges_section, "%s", name);
10688 }
10689 else
10690 {
10691 char *p = strchr (s: ranges_section_label, c: '\0');
10692 sprintf (s: p, format: "+" HOST_WIDE_INT_PRINT_HEX,
10693 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10694 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10695 debug_ranges_section, "%s", name);
10696 *p = '\0';
10697 }
10698 }
10699 else if (dwarf_version >= 5)
10700 {
10701 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10702 gcc_assert (rnglist_idx);
10703 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10704 }
10705 else
10706 dw2_asm_output_data (dwarf_offset_size,
10707 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10708 "%s (offset from %s)", name, ranges_section_label);
10709}
10710
10711/* Output the offset into the debug_loc section. */
10712
10713static void
10714output_loc_list_offset (dw_attr_node *a)
10715{
10716 char *sym = AT_loc_list (a)->ll_symbol;
10717
10718 gcc_assert (sym);
10719 if (!dwarf_split_debug_info)
10720 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10721 "%s", dwarf_attr_name (attr: a->dw_attr));
10722 else if (dwarf_version >= 5)
10723 {
10724 gcc_assert (AT_loc_list (a)->num_assigned);
10725 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10726 dwarf_attr_name (attr: a->dw_attr),
10727 sym);
10728 }
10729 else
10730 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10731 "%s", dwarf_attr_name (attr: a->dw_attr));
10732}
10733
10734/* Output the offset into the debug_loc section. */
10735
10736static void
10737output_view_list_offset (dw_attr_node *a)
10738{
10739 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10740
10741 gcc_assert (sym);
10742 if (dwarf_split_debug_info)
10743 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10744 "%s", dwarf_attr_name (attr: a->dw_attr));
10745 else
10746 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10747 "%s", dwarf_attr_name (attr: a->dw_attr));
10748}
10749
10750/* Output an attribute's index or value appropriately. */
10751
10752static void
10753output_attr_index_or_value (dw_attr_node *a)
10754{
10755 const char *name = dwarf_attr_name (attr: a->dw_attr);
10756
10757 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10758 {
10759 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10760 return;
10761 }
10762 switch (AT_class (a))
10763 {
10764 case dw_val_class_addr:
10765 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10766 break;
10767 case dw_val_class_high_pc:
10768 case dw_val_class_lbl_id:
10769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10770 break;
10771 default:
10772 gcc_unreachable ();
10773 }
10774}
10775
10776/* Output a type signature. */
10777
10778static inline void
10779output_signature (const char *sig, const char *name)
10780{
10781 int i;
10782
10783 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10784 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10785}
10786
10787/* Output a discriminant value. */
10788
10789static inline void
10790output_discr_value (dw_discr_value *discr_value, const char *name)
10791{
10792 if (discr_value->pos)
10793 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10794 else
10795 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10796}
10797
10798/* Output the DIE and its attributes. Called recursively to generate
10799 the definitions of each child DIE. */
10800
10801static void
10802output_die (dw_die_ref die)
10803{
10804 dw_attr_node *a;
10805 dw_die_ref c;
10806 unsigned long size;
10807 unsigned ix;
10808
10809 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10810 (unsigned long)die->die_offset,
10811 dwarf_tag_name (tag: die->die_tag));
10812
10813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10814 {
10815 const char *name = dwarf_attr_name (attr: a->dw_attr);
10816
10817 switch (AT_class (a))
10818 {
10819 case dw_val_class_addr:
10820 output_attr_index_or_value (a);
10821 break;
10822
10823 case dw_val_class_offset:
10824 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10825 "%s", name);
10826 break;
10827
10828 case dw_val_class_range_list:
10829 output_range_list_offset (a);
10830 break;
10831
10832 case dw_val_class_loc:
10833 size = size_of_locs (loc: AT_loc (a));
10834
10835 /* Output the block length for this list of location operations. */
10836 if (dwarf_version >= 4)
10837 dw2_asm_output_data_uleb128 (size, "%s", name);
10838 else
10839 dw2_asm_output_data (constant_size (value: size), size, "%s", name);
10840
10841 output_loc_sequence (loc: AT_loc (a), for_eh_or_skip: -1);
10842 break;
10843
10844 case dw_val_class_const:
10845 /* ??? It would be slightly more efficient to use a scheme like is
10846 used for unsigned constants below, but gdb 4.x does not sign
10847 extend. Gdb 5.x does sign extend. */
10848 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10849 break;
10850
10851 case dw_val_class_unsigned_const:
10852 {
10853 int csize = constant_size (value: AT_unsigned (a));
10854 if (dwarf_version == 3
10855 && a->dw_attr == DW_AT_data_member_location
10856 && csize >= 4)
10857 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10858 else
10859 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10860 }
10861 break;
10862
10863 case dw_val_class_symview:
10864 {
10865 int vsize;
10866 if (symview_upper_bound <= 0xff)
10867 vsize = 1;
10868 else if (symview_upper_bound <= 0xffff)
10869 vsize = 2;
10870 else if (symview_upper_bound <= 0xffffffff)
10871 vsize = 4;
10872 else
10873 vsize = 8;
10874 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10875 "%s", name);
10876 }
10877 break;
10878
10879 case dw_val_class_const_implicit:
10880 if (flag_debug_asm)
10881 fprintf (stream: asm_out_file, format: "\t\t\t%s %s ("
10882 HOST_WIDE_INT_PRINT_DEC ")\n",
10883 ASM_COMMENT_START, name, AT_int (a));
10884 break;
10885
10886 case dw_val_class_unsigned_const_implicit:
10887 if (flag_debug_asm)
10888 fprintf (stream: asm_out_file, format: "\t\t\t%s %s ("
10889 HOST_WIDE_INT_PRINT_HEX ")\n",
10890 ASM_COMMENT_START, name, AT_unsigned (a));
10891 break;
10892
10893 case dw_val_class_const_double:
10894 {
10895 unsigned HOST_WIDE_INT first, second;
10896
10897 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10898 dw2_asm_output_data (1,
10899 HOST_BITS_PER_DOUBLE_INT
10900 / HOST_BITS_PER_CHAR,
10901 NULL);
10902
10903 if (WORDS_BIG_ENDIAN)
10904 {
10905 first = a->dw_attr_val.v.val_double.high;
10906 second = a->dw_attr_val.v.val_double.low;
10907 }
10908 else
10909 {
10910 first = a->dw_attr_val.v.val_double.low;
10911 second = a->dw_attr_val.v.val_double.high;
10912 }
10913
10914 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10915 first, "%s", name);
10916 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10917 second, NULL);
10918 }
10919 break;
10920
10921 case dw_val_class_wide_int:
10922 {
10923 int i;
10924 int len = get_full_len (op: *a->dw_attr_val.v.val_wide);
10925 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10926 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10927 dw2_asm_output_data (1, get_full_len (op: *a->dw_attr_val.v.val_wide)
10928 * l, NULL);
10929
10930 if (WORDS_BIG_ENDIAN)
10931 for (i = len - 1; i >= 0; --i)
10932 {
10933 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10934 "%s", name);
10935 name = "";
10936 }
10937 else
10938 for (i = 0; i < len; ++i)
10939 {
10940 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10941 "%s", name);
10942 name = "";
10943 }
10944 }
10945 break;
10946
10947 case dw_val_class_vec:
10948 {
10949 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10950 unsigned int len = a->dw_attr_val.v.val_vec.length;
10951 unsigned int i;
10952 unsigned char *p;
10953
10954 dw2_asm_output_data (constant_size (value: len * elt_size),
10955 len * elt_size, "%s", name);
10956 if (elt_size > sizeof (HOST_WIDE_INT))
10957 {
10958 elt_size /= 2;
10959 len *= 2;
10960 }
10961 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10962 i < len;
10963 i++, p += elt_size)
10964 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10965 "fp or vector constant word %u", i);
10966 break;
10967 }
10968
10969 case dw_val_class_flag:
10970 if (dwarf_version >= 4)
10971 {
10972 /* Currently all add_AT_flag calls pass in 1 as last argument,
10973 so DW_FORM_flag_present can be used. If that ever changes,
10974 we'll need to use DW_FORM_flag and have some optimization
10975 in build_abbrev_table that will change those to
10976 DW_FORM_flag_present if it is set to 1 in all DIEs using
10977 the same abbrev entry. */
10978 gcc_assert (AT_flag (a) == 1);
10979 if (flag_debug_asm)
10980 fprintf (stream: asm_out_file, format: "\t\t\t%s %s\n",
10981 ASM_COMMENT_START, name);
10982 break;
10983 }
10984 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10985 break;
10986
10987 case dw_val_class_loc_list:
10988 output_loc_list_offset (a);
10989 break;
10990
10991 case dw_val_class_view_list:
10992 output_view_list_offset (a);
10993 break;
10994
10995 case dw_val_class_die_ref:
10996 if (AT_ref_external (a))
10997 {
10998 if (AT_ref (a)->comdat_type_p)
10999 {
11000 comdat_type_node *type_node
11001 = AT_ref (a)->die_id.die_type_node;
11002
11003 gcc_assert (type_node);
11004 output_signature (sig: type_node->signature, name);
11005 }
11006 else
11007 {
11008 const char *sym = AT_ref (a)->die_id.die_symbol;
11009 int size;
11010
11011 gcc_assert (sym);
11012 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11013 length, whereas in DWARF3 it's always sized as an
11014 offset. */
11015 if (dwarf_version == 2)
11016 size = DWARF2_ADDR_SIZE;
11017 else
11018 size = dwarf_offset_size;
11019 /* ??? We cannot unconditionally output die_offset if
11020 non-zero - others might create references to those
11021 DIEs via symbols.
11022 And we do not clear its DIE offset after outputting it
11023 (and the label refers to the actual DIEs, not the
11024 DWARF CU unit header which is when using label + offset
11025 would be the correct thing to do).
11026 ??? This is the reason for the with_offset flag. */
11027 if (AT_ref (a)->with_offset)
11028 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11029 debug_info_section, "%s", name);
11030 else
11031 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11032 name);
11033 }
11034 }
11035 else
11036 {
11037 gcc_assert (AT_ref (a)->die_offset);
11038 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11039 "%s", name);
11040 }
11041 break;
11042
11043 case dw_val_class_fde_ref:
11044 {
11045 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11046
11047 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11048 a->dw_attr_val.v.val_fde_index * 2);
11049 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11050 "%s", name);
11051 }
11052 break;
11053
11054 case dw_val_class_vms_delta:
11055#ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11056 dw2_asm_output_vms_delta (dwarf_offset_size,
11057 AT_vms_delta2 (a), AT_vms_delta1 (a),
11058 "%s", name);
11059#else
11060 dw2_asm_output_delta (dwarf_offset_size,
11061 AT_vms_delta2 (a), AT_vms_delta1 (a),
11062 "%s", name);
11063#endif
11064 break;
11065
11066 case dw_val_class_lbl_id:
11067 output_attr_index_or_value (a);
11068 break;
11069
11070 case dw_val_class_lineptr:
11071 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11072 debug_line_section, "%s", name);
11073 break;
11074
11075 case dw_val_class_macptr:
11076 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11077 debug_macinfo_section, "%s", name);
11078 break;
11079
11080 case dw_val_class_loclistsptr:
11081 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11082 debug_loc_section, "%s", name);
11083 break;
11084
11085 case dw_val_class_str:
11086 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11087 dw2_asm_output_offset (dwarf_offset_size,
11088 a->dw_attr_val.v.val_str->label,
11089 debug_str_section,
11090 "%s: \"%s\"", name, AT_string (a));
11091 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11092 dw2_asm_output_offset (dwarf_offset_size,
11093 a->dw_attr_val.v.val_str->label,
11094 debug_line_str_section,
11095 "%s: \"%s\"", name, AT_string (a));
11096 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (form: DW_FORM_strx))
11097 dw2_asm_output_data_uleb128 (AT_index (a),
11098 "%s: \"%s\"", name, AT_string (a));
11099 else
11100 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11101 break;
11102
11103 case dw_val_class_file:
11104 {
11105 int f = maybe_emit_file (fd: a->dw_attr_val.v.val_file);
11106
11107 dw2_asm_output_data (constant_size (value: f), f, "%s (%s)", name,
11108 a->dw_attr_val.v.val_file->filename);
11109 break;
11110 }
11111
11112 case dw_val_class_file_implicit:
11113 if (flag_debug_asm)
11114 fprintf (stream: asm_out_file, format: "\t\t\t%s %s (%d, %s)\n",
11115 ASM_COMMENT_START, name,
11116 maybe_emit_file (fd: a->dw_attr_val.v.val_file),
11117 a->dw_attr_val.v.val_file->filename);
11118 break;
11119
11120 case dw_val_class_data8:
11121 {
11122 int i;
11123
11124 for (i = 0; i < 8; i++)
11125 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11126 i == 0 ? "%s" : NULL, name);
11127 break;
11128 }
11129
11130 case dw_val_class_high_pc:
11131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11132 get_AT_low_pc (die), "DW_AT_high_pc");
11133 break;
11134
11135 case dw_val_class_discr_value:
11136 output_discr_value (discr_value: &a->dw_attr_val.v.val_discr_value, name);
11137 break;
11138
11139 case dw_val_class_discr_list:
11140 {
11141 dw_discr_list_ref list = AT_discr_list (a);
11142 const int size = size_of_discr_list (discr_list: list);
11143
11144 /* This is a block, so output its length first. */
11145 dw2_asm_output_data (constant_size (value: size), size,
11146 "%s: block size", name);
11147
11148 for (; list != NULL; list = list->dw_discr_next)
11149 {
11150 /* One byte for the discriminant value descriptor, and then as
11151 many LEB128 numbers as required. */
11152 if (list->dw_discr_range)
11153 dw2_asm_output_data (1, DW_DSC_range,
11154 "%s: DW_DSC_range", name);
11155 else
11156 dw2_asm_output_data (1, DW_DSC_label,
11157 "%s: DW_DSC_label", name);
11158
11159 output_discr_value (discr_value: &list->dw_discr_lower_bound, name);
11160 if (list->dw_discr_range)
11161 output_discr_value (discr_value: &list->dw_discr_upper_bound, name);
11162 }
11163 break;
11164 }
11165
11166 default:
11167 gcc_unreachable ();
11168 }
11169 }
11170
11171 FOR_EACH_CHILD (die, c, output_die (c));
11172
11173 /* Add null byte to terminate sibling list. */
11174 if (die->die_child != NULL)
11175 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11176 (unsigned long) die->die_offset);
11177}
11178
11179/* Output the dwarf version number. */
11180
11181static void
11182output_dwarf_version ()
11183{
11184 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11185 views in loclist. That will change eventually. */
11186 if (dwarf_version == 6)
11187 {
11188 static bool once;
11189 if (!once)
11190 {
11191 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11192 "incompatibilities");
11193 once = true;
11194 }
11195 dw2_asm_output_data (2, 5, "DWARF version number");
11196 }
11197 else
11198 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11199}
11200
11201/* Output the compilation unit that appears at the beginning of the
11202 .debug_info section, and precedes the DIE descriptions. */
11203
11204static void
11205output_compilation_unit_header (enum dwarf_unit_type ut)
11206{
11207 if (!XCOFF_DEBUGGING_INFO)
11208 {
11209 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11210 dw2_asm_output_data (4, 0xffffffff,
11211 "Initial length escape value indicating 64-bit DWARF extension");
11212 dw2_asm_output_data (dwarf_offset_size,
11213 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11214 "Length of Compilation Unit Info");
11215 }
11216
11217 output_dwarf_version ();
11218 if (dwarf_version >= 5)
11219 {
11220 const char *name;
11221 switch (ut)
11222 {
11223 case DW_UT_compile: name = "DW_UT_compile"; break;
11224 case DW_UT_type: name = "DW_UT_type"; break;
11225 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11226 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11227 default: gcc_unreachable ();
11228 }
11229 dw2_asm_output_data (1, ut, "%s", name);
11230 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11231 }
11232 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11233 debug_abbrev_section,
11234 "Offset Into Abbrev. Section");
11235 if (dwarf_version < 5)
11236 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11237}
11238
11239/* Output the compilation unit DIE and its children. */
11240
11241static void
11242output_comp_unit (dw_die_ref die, int output_if_empty,
11243 const unsigned char *dwo_id)
11244{
11245 const char *secname, *oldsym;
11246 char *tmp;
11247
11248 /* Unless we are outputting main CU, we may throw away empty ones. */
11249 if (!output_if_empty && die->die_child == NULL)
11250 return;
11251
11252 /* Even if there are no children of this DIE, we must output the information
11253 about the compilation unit. Otherwise, on an empty translation unit, we
11254 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11255 will then complain when examining the file. First mark all the DIEs in
11256 this CU so we know which get local refs. */
11257 mark_dies (die);
11258
11259 external_ref_hash_type *extern_map = optimize_external_refs (die);
11260
11261 /* For now, optimize only the main CU, in order to optimize the rest
11262 we'd need to see all of them earlier. Leave the rest for post-linking
11263 tools like DWZ. */
11264 if (die == comp_unit_die ())
11265 abbrev_opt_start = vec_safe_length (v: abbrev_die_table);
11266
11267 build_abbrev_table (die, extern_map);
11268
11269 optimize_abbrev_table ();
11270
11271 delete extern_map;
11272
11273 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11274 next_die_offset = (dwo_id
11275 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11276 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11277 calc_die_sizes (die);
11278
11279 oldsym = die->die_id.die_symbol;
11280 if (oldsym && die->comdat_type_p)
11281 {
11282 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11283
11284 sprintf (s: tmp, format: ".gnu.linkonce.wi.%s", oldsym);
11285 secname = tmp;
11286 die->die_id.die_symbol = NULL;
11287 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11288 }
11289 else
11290 {
11291 switch_to_section (debug_info_section);
11292 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11293 info_section_emitted = true;
11294 }
11295
11296 /* For LTO cross unit DIE refs we want a symbol on the start of the
11297 debuginfo section, not on the CU DIE. */
11298 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11299 {
11300 /* ??? No way to get visibility assembled without a decl. */
11301 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11302 get_identifier (oldsym), char_type_node);
11303 TREE_PUBLIC (decl) = true;
11304 TREE_STATIC (decl) = true;
11305 DECL_ARTIFICIAL (decl) = true;
11306 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11307 DECL_VISIBILITY_SPECIFIED (decl) = true;
11308 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11309#ifdef ASM_WEAKEN_LABEL
11310 /* We prefer a .weak because that handles duplicates from duplicate
11311 archive members in a graceful way. */
11312 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11313#else
11314 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11315#endif
11316 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11317 }
11318
11319 /* Output debugging information. */
11320 output_compilation_unit_header (ut: dwo_id
11321 ? DW_UT_split_compile : DW_UT_compile);
11322 if (dwarf_version >= 5)
11323 {
11324 if (dwo_id != NULL)
11325 for (int i = 0; i < 8; i++)
11326 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11327 }
11328 output_die (die);
11329
11330 /* Leave the marks on the main CU, so we can check them in
11331 output_pubnames. */
11332 if (oldsym)
11333 {
11334 unmark_dies (die);
11335 die->die_id.die_symbol = oldsym;
11336 }
11337}
11338
11339/* Whether to generate the DWARF accelerator tables in .debug_pubnames
11340 and .debug_pubtypes. This is configured per-target, but can be
11341 overridden by the -gpubnames or -gno-pubnames options. */
11342
11343static inline bool
11344want_pubnames (void)
11345{
11346 if (debug_info_level <= DINFO_LEVEL_TERSE
11347 /* Names and types go to the early debug part only. */
11348 || in_lto_p)
11349 return false;
11350 if (debug_generate_pub_sections != -1)
11351 return debug_generate_pub_sections;
11352 return targetm.want_debug_pub_sections;
11353}
11354
11355/* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11356
11357static void
11358add_AT_pubnames (dw_die_ref die)
11359{
11360 if (want_pubnames ())
11361 add_AT_flag (die, attr_kind: DW_AT_GNU_pubnames, flag: 1);
11362}
11363
11364/* Add a string attribute value to a skeleton DIE. */
11365
11366static inline void
11367add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11368 const char *str)
11369{
11370 dw_attr_node attr;
11371 struct indirect_string_node *node;
11372
11373 if (! skeleton_debug_str_hash)
11374 skeleton_debug_str_hash
11375 = hash_table<indirect_string_hasher>::create_ggc (n: 10);
11376
11377 node = find_AT_string_in_table (str, table: skeleton_debug_str_hash);
11378 find_string_form (node);
11379 if (node->form == dwarf_FORM (form: DW_FORM_strx))
11380 node->form = DW_FORM_strp;
11381
11382 attr.dw_attr = attr_kind;
11383 attr.dw_attr_val.val_class = dw_val_class_str;
11384 attr.dw_attr_val.val_entry = NULL;
11385 attr.dw_attr_val.v.val_str = node;
11386 add_dwarf_attr (die, attr: &attr);
11387}
11388
11389/* Helper function to generate top-level dies for skeleton debug_info and
11390 debug_types. */
11391
11392static void
11393add_top_level_skeleton_die_attrs (dw_die_ref die)
11394{
11395 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11396 const char *comp_dir = comp_dir_string ();
11397
11398 add_skeleton_AT_string (die, attr_kind: dwarf_AT (at: DW_AT_dwo_name), str: dwo_file_name);
11399 if (comp_dir != NULL)
11400 add_skeleton_AT_string (die, attr_kind: DW_AT_comp_dir, str: comp_dir);
11401 add_AT_pubnames (die);
11402 if (addr_index_table != NULL && addr_index_table->size () > 0)
11403 add_AT_lineptr (die, attr_kind: dwarf_AT (at: DW_AT_addr_base), label: debug_addr_section_label);
11404}
11405
11406/* Output skeleton debug sections that point to the dwo file. */
11407
11408static void
11409output_skeleton_debug_sections (dw_die_ref comp_unit,
11410 const unsigned char *dwo_id)
11411{
11412 /* These attributes will be found in the full debug_info section. */
11413 remove_AT (die: comp_unit, attr_kind: DW_AT_producer);
11414 remove_AT (die: comp_unit, attr_kind: DW_AT_language);
11415 remove_AT (die: comp_unit, attr_kind: DW_AT_language_name);
11416 remove_AT (die: comp_unit, attr_kind: DW_AT_language_version);
11417
11418 switch_to_section (debug_skeleton_info_section);
11419 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11420
11421 /* Produce the skeleton compilation-unit header. This one differs enough from
11422 a normal CU header that it's better not to call output_compilation_unit
11423 header. */
11424 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11425 dw2_asm_output_data (4, 0xffffffff,
11426 "Initial length escape value indicating 64-bit "
11427 "DWARF extension");
11428
11429 dw2_asm_output_data (dwarf_offset_size,
11430 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11431 - DWARF_INITIAL_LENGTH_SIZE
11432 + size_of_die (die: comp_unit),
11433 "Length of Compilation Unit Info");
11434 output_dwarf_version ();
11435 if (dwarf_version >= 5)
11436 {
11437 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11438 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11439 }
11440 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11441 debug_skeleton_abbrev_section,
11442 "Offset Into Abbrev. Section");
11443 if (dwarf_version < 5)
11444 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11445 else
11446 for (int i = 0; i < 8; i++)
11447 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11448
11449 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11450 output_die (die: comp_unit);
11451
11452 /* Build the skeleton debug_abbrev section. */
11453 switch_to_section (debug_skeleton_abbrev_section);
11454 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11455
11456 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, abbrev: comp_unit);
11457
11458 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11459}
11460
11461/* Output a comdat type unit DIE and its children. */
11462
11463static void
11464output_comdat_type_unit (comdat_type_node *node,
11465 bool early_lto_debug ATTRIBUTE_UNUSED)
11466{
11467 const char *secname;
11468 char *tmp;
11469 int i;
11470#if defined (OBJECT_FORMAT_ELF)
11471 tree comdat_key;
11472#endif
11473
11474 /* First mark all the DIEs in this CU so we know which get local refs. */
11475 mark_dies (die: node->root_die);
11476
11477 external_ref_hash_type *extern_map = optimize_external_refs (die: node->root_die);
11478
11479 build_abbrev_table (die: node->root_die, extern_map);
11480
11481 delete extern_map;
11482 extern_map = NULL;
11483
11484 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11485 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11486 calc_die_sizes (die: node->root_die);
11487
11488#if defined (OBJECT_FORMAT_ELF)
11489 if (dwarf_version >= 5)
11490 {
11491 if (!dwarf_split_debug_info)
11492 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11493 else
11494 secname = (early_lto_debug
11495 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11496 }
11497 else if (!dwarf_split_debug_info)
11498 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11499 else
11500 secname = (early_lto_debug
11501 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11502
11503 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11504 sprintf (s: tmp, dwarf_version >= 5 ? "wi." : "wt.");
11505 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11506 sprintf (s: tmp + 3 + i * 2, format: "%02x", node->signature[i] & 0xff);
11507 comdat_key = get_identifier (tmp);
11508 targetm.asm_out.named_section (secname,
11509 SECTION_DEBUG | SECTION_LINKONCE,
11510 comdat_key);
11511#else
11512 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11513 sprintf (tmp, (dwarf_version >= 5
11514 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11515 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11516 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11517 secname = tmp;
11518 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11519#endif
11520
11521 /* Output debugging information. */
11522 output_compilation_unit_header (dwarf_split_debug_info
11523 ? DW_UT_split_type : DW_UT_type);
11524 output_signature (sig: node->signature, name: "Type Signature");
11525 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11526 "Offset to Type DIE");
11527 output_die (die: node->root_die);
11528
11529 unmark_dies (die: node->root_die);
11530}
11531
11532/* Return the DWARF2/3 pubname associated with a decl. */
11533
11534static const char *
11535dwarf2_name (tree decl, int scope)
11536{
11537 if (DECL_NAMELESS (decl))
11538 return NULL;
11539 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11540}
11541
11542/* Add a new entry to .debug_pubnames if appropriate. */
11543
11544static void
11545add_pubname_string (const char *str, dw_die_ref die)
11546{
11547 pubname_entry e;
11548
11549 e.die = die;
11550 e.name = xstrdup (str);
11551 vec_safe_push (v&: pubname_table, obj: e);
11552}
11553
11554static void
11555add_pubname (tree decl, dw_die_ref die)
11556{
11557 if (!want_pubnames ())
11558 return;
11559
11560 /* Don't add items to the table when we expect that the consumer will have
11561 just read the enclosing die. For example, if the consumer is looking at a
11562 class_member, it will either be inside the class already, or will have just
11563 looked up the class to find the member. Either way, searching the class is
11564 faster than searching the index. */
11565 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11566 || is_cu_die (c: die->die_parent) || is_namespace_die (c: die->die_parent))
11567 {
11568 const char *name = dwarf2_name (decl, scope: 1);
11569
11570 if (name)
11571 add_pubname_string (str: name, die);
11572 }
11573}
11574
11575/* Add an enumerator to the pubnames section. */
11576
11577static void
11578add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11579{
11580 pubname_entry e;
11581
11582 gcc_assert (scope_name);
11583 e.name = concat (scope_name, get_AT_string (die, attr_kind: DW_AT_name), NULL);
11584 e.die = die;
11585 vec_safe_push (v&: pubname_table, obj: e);
11586}
11587
11588/* Add a new entry to .debug_pubtypes if appropriate. */
11589
11590static void
11591add_pubtype (tree decl, dw_die_ref die)
11592{
11593 pubname_entry e;
11594
11595 if (!want_pubnames ())
11596 return;
11597
11598 if ((TREE_PUBLIC (decl)
11599 || is_cu_die (c: die->die_parent) || is_namespace_die (c: die->die_parent))
11600 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11601 {
11602 tree scope = NULL;
11603 const char *scope_name = "";
11604 const char *sep = is_cxx () ? "::" : ".";
11605 const char *name;
11606
11607 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11608 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11609 {
11610 scope_name = lang_hooks.dwarf_name (scope, 1);
11611 if (scope_name != NULL && scope_name[0] != '\0')
11612 scope_name = concat (scope_name, sep, NULL);
11613 else
11614 scope_name = "";
11615 }
11616
11617 if (TYPE_P (decl))
11618 name = type_tag (decl);
11619 else
11620 name = lang_hooks.dwarf_name (decl, 1);
11621
11622 /* If we don't have a name for the type, there's no point in adding
11623 it to the table. */
11624 if (name != NULL && name[0] != '\0')
11625 {
11626 e.die = die;
11627 e.name = concat (scope_name, name, NULL);
11628 vec_safe_push (v&: pubtype_table, obj: e);
11629 }
11630
11631 /* Although it might be more consistent to add the pubinfo for the
11632 enumerators as their dies are created, they should only be added if the
11633 enum type meets the criteria above. So rather than re-check the parent
11634 enum type whenever an enumerator die is created, just output them all
11635 here. This isn't protected by the name conditional because anonymous
11636 enums don't have names. */
11637 if (die->die_tag == DW_TAG_enumeration_type)
11638 {
11639 dw_die_ref c;
11640
11641 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11642 }
11643 }
11644}
11645
11646/* Output a single entry in the pubnames table. */
11647
11648static void
11649output_pubname (dw_offset die_offset, pubname_entry *entry)
11650{
11651 dw_die_ref die = entry->die;
11652 int is_static = get_AT_flag (die, attr_kind: DW_AT_external) ? 0 : 1;
11653
11654 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11655
11656 if (debug_generate_pub_sections == 2)
11657 {
11658 /* This logic follows gdb's method for determining the value of the flag
11659 byte. */
11660 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11661 switch (die->die_tag)
11662 {
11663 case DW_TAG_typedef:
11664 case DW_TAG_base_type:
11665 case DW_TAG_subrange_type:
11666 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11667 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11668 break;
11669 case DW_TAG_enumerator:
11670 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11671 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11672 if (!is_cxx ())
11673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11674 break;
11675 case DW_TAG_subprogram:
11676 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11677 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11678 if (!is_ada ())
11679 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11680 break;
11681 case DW_TAG_constant:
11682 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11683 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11684 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11685 break;
11686 case DW_TAG_variable:
11687 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11688 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11689 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11690 break;
11691 case DW_TAG_namespace:
11692 case DW_TAG_imported_declaration:
11693 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11694 break;
11695 case DW_TAG_class_type:
11696 case DW_TAG_interface_type:
11697 case DW_TAG_structure_type:
11698 case DW_TAG_union_type:
11699 case DW_TAG_enumeration_type:
11700 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11701 if (!is_cxx ())
11702 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11703 break;
11704 default:
11705 /* An unusual tag. Leave the flag-byte empty. */
11706 break;
11707 }
11708 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11709 "GDB-index flags");
11710 }
11711
11712 dw2_asm_output_nstring (entry->name, -1, "external name");
11713}
11714
11715
11716/* Output the public names table used to speed up access to externally
11717 visible names; or the public types table used to find type definitions. */
11718
11719static void
11720output_pubnames (vec<pubname_entry, va_gc> *names)
11721{
11722 unsigned i;
11723 unsigned long pubnames_length = size_of_pubnames (names);
11724 pubname_entry *pub;
11725
11726 if (!XCOFF_DEBUGGING_INFO)
11727 {
11728 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11729 dw2_asm_output_data (4, 0xffffffff,
11730 "Initial length escape value indicating 64-bit DWARF extension");
11731 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11732 "Pub Info Length");
11733 }
11734
11735 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11736 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11737
11738 if (dwarf_split_debug_info)
11739 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11740 debug_skeleton_info_section,
11741 "Offset of Compilation Unit Info");
11742 else
11743 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11744 debug_info_section,
11745 "Offset of Compilation Unit Info");
11746 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11747 "Compilation Unit Length");
11748
11749 FOR_EACH_VEC_ELT (*names, i, pub)
11750 {
11751 if (include_pubname_in_output (table: names, p: pub))
11752 {
11753 dw_offset die_offset = pub->die->die_offset;
11754
11755 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11756 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11757 gcc_assert (pub->die->die_mark);
11758
11759 /* If we're putting types in their own .debug_types sections,
11760 the .debug_pubtypes table will still point to the compile
11761 unit (not the type unit), so we want to use the offset of
11762 the skeleton DIE (if there is one). */
11763 if (pub->die->comdat_type_p && names == pubtype_table)
11764 {
11765 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11766
11767 if (type_node != NULL)
11768 die_offset = (type_node->skeleton_die != NULL
11769 ? type_node->skeleton_die->die_offset
11770 : comp_unit_die ()->die_offset);
11771 }
11772
11773 output_pubname (die_offset, entry: pub);
11774 }
11775 }
11776
11777 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11778}
11779
11780/* Output public names and types tables if necessary. */
11781
11782static void
11783output_pubtables (void)
11784{
11785 if (!want_pubnames () || !info_section_emitted)
11786 return;
11787
11788 switch_to_section (debug_pubnames_section);
11789 output_pubnames (names: pubname_table);
11790 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11791 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11792 simply won't look for the section. */
11793 switch_to_section (debug_pubtypes_section);
11794 output_pubnames (names: pubtype_table);
11795}
11796
11797
11798/* Output the information that goes into the .debug_aranges table.
11799 Namely, define the beginning and ending address range of the
11800 text section generated for this compilation unit. */
11801
11802static void
11803output_aranges (void)
11804{
11805 unsigned i;
11806 unsigned long aranges_length = size_of_aranges ();
11807
11808 if (!XCOFF_DEBUGGING_INFO)
11809 {
11810 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11811 dw2_asm_output_data (4, 0xffffffff,
11812 "Initial length escape value indicating 64-bit DWARF extension");
11813 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11814 "Length of Address Ranges Info");
11815 }
11816
11817 /* Version number for aranges is still 2, even up to DWARF5. */
11818 dw2_asm_output_data (2, 2, "DWARF aranges version");
11819 if (dwarf_split_debug_info)
11820 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11821 debug_skeleton_info_section,
11822 "Offset of Compilation Unit Info");
11823 else
11824 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11825 debug_info_section,
11826 "Offset of Compilation Unit Info");
11827 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11828 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11829
11830 /* We need to align to twice the pointer size here. */
11831 if (DWARF_ARANGES_PAD_SIZE)
11832 {
11833 /* Pad using a 2 byte words so that padding is correct for any
11834 pointer size. */
11835 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11836 2 * DWARF2_ADDR_SIZE);
11837 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11838 dw2_asm_output_data (2, 0, NULL);
11839 }
11840
11841 /* It is necessary not to output these entries if the sections were
11842 not used; if the sections were not used, the length will be 0 and
11843 the address may end up as 0 if the section is discarded by ld
11844 --gc-sections, leaving an invalid (0, 0) entry that can be
11845 confused with the terminator. */
11846 if (switch_text_ranges)
11847 {
11848 const char *prev_loc = text_section_label;
11849 const char *loc;
11850 unsigned idx;
11851
11852 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11853 if (prev_loc)
11854 {
11855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11856 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11857 prev_loc = NULL;
11858 }
11859 else
11860 prev_loc = loc;
11861
11862 if (prev_loc)
11863 {
11864 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11865 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11866 prev_loc, "Length");
11867 }
11868 }
11869
11870 if (switch_cold_ranges)
11871 {
11872 const char *prev_loc = cold_text_section_label;
11873 const char *loc;
11874 unsigned idx;
11875
11876 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11877 if (prev_loc)
11878 {
11879 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11880 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11881 prev_loc = NULL;
11882 }
11883 else
11884 prev_loc = loc;
11885
11886 if (prev_loc)
11887 {
11888 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11889 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11890 prev_loc, "Length");
11891 }
11892 }
11893
11894 if (have_multiple_function_sections)
11895 {
11896 unsigned fde_idx;
11897 dw_fde_ref fde;
11898
11899 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11900 {
11901 if (fde->ignored_debug)
11902 continue;
11903 if (!fde->in_std_section)
11904 {
11905 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11906 "Address");
11907 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11908 fde->dw_fde_begin, "Length");
11909 }
11910 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11911 {
11912 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11913 "Address");
11914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11915 fde->dw_fde_second_begin, "Length");
11916 }
11917 }
11918 }
11919
11920 /* Output the terminator words. */
11921 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11922 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11923}
11924
11925/* Add a new entry to .debug_ranges. Return its index into
11926 ranges_table vector. */
11927
11928static unsigned int
11929add_ranges_num (int num, bool maybe_new_sec)
11930{
11931 dw_ranges r = { NULL, .num: num, .idx: 0, .maybe_new_sec: maybe_new_sec, NULL, NULL };
11932 vec_safe_push (v&: ranges_table, obj: r);
11933 return vec_safe_length (v: ranges_table) - 1;
11934}
11935
11936/* Add a new entry to .debug_ranges corresponding to a block, or a
11937 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11938 this entry might be in a different section from previous range. */
11939
11940static unsigned int
11941add_ranges (const_tree block, bool maybe_new_sec)
11942{
11943 return add_ranges_num (num: block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11944}
11945
11946/* Note that (*rnglist_table)[offset] is either a head of a rnglist
11947 chain, or middle entry of a chain that will be directly referred to. */
11948
11949static void
11950note_rnglist_head (unsigned int offset)
11951{
11952 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11953 return;
11954 (*ranges_table)[offset].label = gen_internal_sym (prefix: "LLRL");
11955}
11956
11957/* Add a new entry to .debug_ranges corresponding to a pair of labels.
11958 When using dwarf_split_debug_info, address attributes in dies destined
11959 for the final executable should be direct references--setting the
11960 parameter force_direct ensures this behavior. */
11961
11962static void
11963add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11964 bool *added, bool force_direct)
11965{
11966 unsigned int in_use = vec_safe_length (v: ranges_by_label);
11967 unsigned int offset;
11968 dw_ranges_by_label rbl = { .begin: begin, .end: end };
11969 vec_safe_push (v&: ranges_by_label, obj: rbl);
11970 offset = add_ranges_num (num: -(int)in_use - 1, maybe_new_sec: true);
11971 if (!*added)
11972 {
11973 add_AT_range_list (die, attr_kind: DW_AT_ranges, offset, force_direct);
11974 *added = true;
11975 note_rnglist_head (offset);
11976 if (dwarf_split_debug_info && force_direct)
11977 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11978 }
11979}
11980
11981/* Emit .debug_ranges section. */
11982
11983static void
11984output_ranges (void)
11985{
11986 unsigned i;
11987 static const char *const start_fmt = "Offset %#x";
11988 const char *fmt = start_fmt;
11989 dw_ranges *r;
11990
11991 switch_to_section (debug_ranges_section);
11992 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11993 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11994 {
11995 int block_num = r->num;
11996
11997 if (block_num > 0)
11998 {
11999 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12000 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12001
12002 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12003 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12004
12005 /* If all code is in the text section, then the compilation
12006 unit base address defaults to DW_AT_low_pc, which is the
12007 base of the text section. */
12008 if (!have_multiple_function_sections)
12009 {
12010 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12011 text_section_label,
12012 fmt, i * 2 * DWARF2_ADDR_SIZE);
12013 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12014 text_section_label, NULL);
12015 }
12016
12017 /* Otherwise, the compilation unit base address is zero,
12018 which allows us to use absolute addresses, and not worry
12019 about whether the target supports cross-section
12020 arithmetic. */
12021 else
12022 {
12023 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12024 fmt, i * 2 * DWARF2_ADDR_SIZE);
12025 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12026 }
12027
12028 fmt = NULL;
12029 }
12030
12031 /* Negative block_num stands for an index into ranges_by_label. */
12032 else if (block_num < 0)
12033 {
12034 int lab_idx = - block_num - 1;
12035
12036 if (!have_multiple_function_sections)
12037 {
12038 gcc_unreachable ();
12039#if 0
12040 /* If we ever use add_ranges_by_labels () for a single
12041 function section, all we have to do is to take out
12042 the #if 0 above. */
12043 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12044 (*ranges_by_label)[lab_idx].begin,
12045 text_section_label,
12046 fmt, i * 2 * DWARF2_ADDR_SIZE);
12047 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12048 (*ranges_by_label)[lab_idx].end,
12049 text_section_label, NULL);
12050#endif
12051 }
12052 else
12053 {
12054 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12055 (*ranges_by_label)[lab_idx].begin,
12056 fmt, i * 2 * DWARF2_ADDR_SIZE);
12057 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12058 (*ranges_by_label)[lab_idx].end,
12059 NULL);
12060 }
12061 }
12062 else
12063 {
12064 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12065 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12066 fmt = start_fmt;
12067 }
12068 }
12069}
12070
12071/* Non-zero if .debug_line_str should be used for .debug_line section
12072 strings or strings that are likely shareable with those. */
12073#define DWARF5_USE_DEBUG_LINE_STR \
12074 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12075 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12076 /* FIXME: there is no .debug_line_str.dwo section, \
12077 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12078 && !dwarf_split_debug_info)
12079
12080
12081/* Returns TRUE if we are outputting DWARF5 and the assembler supports
12082 DWARF5 .debug_line tables using .debug_line_str or we generate
12083 it ourselves, except for split-dwarf which doesn't have a
12084 .debug_line_str. */
12085static bool
12086asm_outputs_debug_line_str (void)
12087{
12088 if (dwarf_version >= 5
12089 && ! output_asm_line_debug_info ()
12090 && DWARF5_USE_DEBUG_LINE_STR)
12091 return true;
12092 else
12093 {
12094#if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12095 return !dwarf_split_debug_info && dwarf_version >= 5;
12096#else
12097 return false;
12098#endif
12099 }
12100}
12101
12102/* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12103 I is index of the following range. */
12104
12105static bool
12106use_distinct_base_address_for_range (unsigned int i)
12107{
12108 if (i >= vec_safe_length (v: ranges_table))
12109 return false;
12110
12111 dw_ranges *r2 = &(*ranges_table)[i];
12112 /* Use DW_RLE_base_address{,x} if there is a next range in the
12113 range list and is guaranteed to be in the same section. */
12114 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12115}
12116
12117/* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12118 section when needed. */
12119
12120static void
12121index_rnglists (void)
12122{
12123 unsigned i;
12124 dw_ranges *r;
12125 bool base = false;
12126
12127 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12128 {
12129 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12130 r->idx = rnglist_idx++;
12131
12132 int block_num = r->num;
12133 if ((HAVE_AS_LEB128 || block_num < 0)
12134 && !have_multiple_function_sections)
12135 continue;
12136 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12137 base = false;
12138 if (block_num > 0)
12139 {
12140 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12141 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12142
12143 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12144 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12145
12146 if (HAVE_AS_LEB128)
12147 {
12148 if (!base && use_distinct_base_address_for_range (i: i + 1))
12149 {
12150 r->begin_entry = add_addr_table_entry (addr: xstrdup (blabel),
12151 kind: ate_kind_label);
12152 base = true;
12153 }
12154 if (base)
12155 /* If we have a base, no need for further
12156 begin_entry/end_entry, as DW_RLE_offset_pair will be
12157 used. */
12158 continue;
12159 r->begin_entry
12160 = add_addr_table_entry (addr: xstrdup (blabel), kind: ate_kind_label);
12161 /* No need for end_entry, DW_RLE_start{,x}_length will use
12162 length as opposed to a pair of addresses. */
12163 }
12164 else
12165 {
12166 r->begin_entry
12167 = add_addr_table_entry (addr: xstrdup (blabel), kind: ate_kind_label);
12168 r->end_entry
12169 = add_addr_table_entry (addr: xstrdup (elabel), kind: ate_kind_label);
12170 }
12171 }
12172
12173 /* Negative block_num stands for an index into ranges_by_label. */
12174 else if (block_num < 0)
12175 {
12176 int lab_idx = - block_num - 1;
12177 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12178 const char *elabel = (*ranges_by_label)[lab_idx].end;
12179
12180 r->begin_entry
12181 = add_addr_table_entry (addr: xstrdup (blabel), kind: ate_kind_label);
12182 if (!HAVE_AS_LEB128)
12183 r->end_entry
12184 = add_addr_table_entry (addr: xstrdup (elabel), kind: ate_kind_label);
12185 }
12186 }
12187}
12188
12189/* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12190
12191static bool
12192output_rnglists (unsigned generation, bool dwo)
12193{
12194 unsigned i;
12195 dw_ranges *r;
12196 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12197 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12198 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12199
12200 if (dwo)
12201 switch_to_section (debug_ranges_dwo_section);
12202 else
12203 {
12204 switch_to_section (debug_ranges_section);
12205 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12206 }
12207 /* There are up to 4 unique ranges labels per generation.
12208 See also init_sections_and_labels. */
12209 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12210 2 + 2 * dwo + generation * 6);
12211 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12212 3 + 2 * dwo + generation * 6);
12213 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12214 dw2_asm_output_data (4, 0xffffffff,
12215 "Initial length escape value indicating "
12216 "64-bit DWARF extension");
12217 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12218 "Length of Range Lists");
12219 ASM_OUTPUT_LABEL (asm_out_file, l1);
12220 output_dwarf_version ();
12221 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12222 dw2_asm_output_data (1, 0, "Segment Size");
12223 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12224 about relocation sizes and primarily care about the size of .debug*
12225 sections in linked shared libraries and executables, then
12226 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12227 into it are usually larger than just DW_FORM_sec_offset offsets
12228 into the .debug_rnglists section. */
12229 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12230 "Offset Entry Count");
12231 if (dwo)
12232 {
12233 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12234 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12235 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12236 dw2_asm_output_delta (dwarf_offset_size, r->label,
12237 ranges_base_label, NULL);
12238 }
12239
12240 const char *lab = "";
12241 const char *base = NULL;
12242 bool skipping = false;
12243 bool ret = false;
12244 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12245 {
12246 int block_num = r->num;
12247
12248 if (r->label)
12249 {
12250 if (dwarf_split_debug_info
12251 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12252 {
12253 ret = true;
12254 skipping = true;
12255 continue;
12256 }
12257 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12258 lab = r->label;
12259 }
12260 if (skipping)
12261 {
12262 if (block_num == 0)
12263 skipping = false;
12264 continue;
12265 }
12266 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12267 base = NULL;
12268 if (block_num > 0)
12269 {
12270 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12271 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12272
12273 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12274 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12275
12276 if (HAVE_AS_LEB128)
12277 {
12278 /* If all code is in the text section, then the compilation
12279 unit base address defaults to DW_AT_low_pc, which is the
12280 base of the text section. */
12281 if (!have_multiple_function_sections)
12282 {
12283 dw2_asm_output_data (1, DW_RLE_offset_pair,
12284 "DW_RLE_offset_pair (%s)", lab);
12285 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12286 "Range begin address (%s)", lab);
12287 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12288 "Range end address (%s)", lab);
12289 continue;
12290 }
12291 if (base == NULL && use_distinct_base_address_for_range (i: i + 1))
12292 {
12293 if (dwarf_split_debug_info)
12294 {
12295 dw2_asm_output_data (1, DW_RLE_base_addressx,
12296 "DW_RLE_base_addressx (%s)", lab);
12297 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12298 "Base address index (%s)",
12299 blabel);
12300 }
12301 else
12302 {
12303 dw2_asm_output_data (1, DW_RLE_base_address,
12304 "DW_RLE_base_address (%s)", lab);
12305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12306 "Base address (%s)", lab);
12307 }
12308 strcpy (dest: basebuf, src: blabel);
12309 base = basebuf;
12310 }
12311 if (base)
12312 {
12313 dw2_asm_output_data (1, DW_RLE_offset_pair,
12314 "DW_RLE_offset_pair (%s)", lab);
12315 dw2_asm_output_delta_uleb128 (blabel, base,
12316 "Range begin address (%s)", lab);
12317 dw2_asm_output_delta_uleb128 (elabel, base,
12318 "Range end address (%s)", lab);
12319 continue;
12320 }
12321 if (dwarf_split_debug_info)
12322 {
12323 dw2_asm_output_data (1, DW_RLE_startx_length,
12324 "DW_RLE_startx_length (%s)", lab);
12325 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12326 "Range begin address index "
12327 "(%s)", blabel);
12328 }
12329 else
12330 {
12331 dw2_asm_output_data (1, DW_RLE_start_length,
12332 "DW_RLE_start_length (%s)", lab);
12333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12334 "Range begin address (%s)", lab);
12335 }
12336 dw2_asm_output_delta_uleb128 (elabel, blabel,
12337 "Range length (%s)", lab);
12338 }
12339 else if (dwarf_split_debug_info)
12340 {
12341 dw2_asm_output_data (1, DW_RLE_startx_endx,
12342 "DW_RLE_startx_endx (%s)", lab);
12343 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12344 "Range begin address index "
12345 "(%s)", blabel);
12346 dw2_asm_output_data_uleb128 (r->end_entry->index,
12347 "Range end address index "
12348 "(%s)", elabel);
12349 }
12350 else
12351 {
12352 dw2_asm_output_data (1, DW_RLE_start_end,
12353 "DW_RLE_start_end (%s)", lab);
12354 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12355 "Range begin address (%s)", lab);
12356 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12357 "Range end address (%s)", lab);
12358 }
12359 }
12360
12361 /* Negative block_num stands for an index into ranges_by_label. */
12362 else if (block_num < 0)
12363 {
12364 int lab_idx = - block_num - 1;
12365 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12366 const char *elabel = (*ranges_by_label)[lab_idx].end;
12367
12368 if (!have_multiple_function_sections)
12369 gcc_unreachable ();
12370 if (HAVE_AS_LEB128)
12371 {
12372 if (dwarf_split_debug_info)
12373 {
12374 dw2_asm_output_data (1, DW_RLE_startx_length,
12375 "DW_RLE_startx_length (%s)", lab);
12376 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12377 "Range begin address index "
12378 "(%s)", blabel);
12379 }
12380 else
12381 {
12382 dw2_asm_output_data (1, DW_RLE_start_length,
12383 "DW_RLE_start_length (%s)", lab);
12384 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12385 "Range begin address (%s)", lab);
12386 }
12387 dw2_asm_output_delta_uleb128 (elabel, blabel,
12388 "Range length (%s)", lab);
12389 }
12390 else if (dwarf_split_debug_info)
12391 {
12392 dw2_asm_output_data (1, DW_RLE_startx_endx,
12393 "DW_RLE_startx_endx (%s)", lab);
12394 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12395 "Range begin address index "
12396 "(%s)", blabel);
12397 dw2_asm_output_data_uleb128 (r->end_entry->index,
12398 "Range end address index "
12399 "(%s)", elabel);
12400 }
12401 else
12402 {
12403 dw2_asm_output_data (1, DW_RLE_start_end,
12404 "DW_RLE_start_end (%s)", lab);
12405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12406 "Range begin address (%s)", lab);
12407 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12408 "Range end address (%s)", lab);
12409 }
12410 }
12411 else
12412 dw2_asm_output_data (1, DW_RLE_end_of_list,
12413 "DW_RLE_end_of_list (%s)", lab);
12414 }
12415 ASM_OUTPUT_LABEL (asm_out_file, l2);
12416 return ret;
12417}
12418
12419/* Data structure containing information about input files. */
12420struct file_info
12421{
12422 const char *path; /* Complete file name. */
12423 const char *fname; /* File name part. */
12424 int length; /* Length of entire string. */
12425 struct dwarf_file_data * file_idx; /* Index in input file table. */
12426 int dir_idx; /* Index in directory table. */
12427};
12428
12429/* Data structure containing information about directories with source
12430 files. */
12431struct dir_info
12432{
12433 const char *path; /* Path including directory name. */
12434 int length; /* Path length. */
12435 int prefix; /* Index of directory entry which is a prefix. */
12436 int count; /* Number of files in this directory. */
12437 int dir_idx; /* Index of directory used as base. */
12438};
12439
12440/* Callback function for file_info comparison. We sort by looking at
12441 the directories in the path. */
12442
12443static int
12444file_info_cmp (const void *p1, const void *p2)
12445{
12446 const struct file_info *const s1 = (const struct file_info *) p1;
12447 const struct file_info *const s2 = (const struct file_info *) p2;
12448 const unsigned char *cp1;
12449 const unsigned char *cp2;
12450
12451 /* Take care of file names without directories. We need to make sure that
12452 we return consistent values to qsort since some will get confused if
12453 we return the same value when identical operands are passed in opposite
12454 orders. So if neither has a directory, return 0 and otherwise return
12455 1 or -1 depending on which one has the directory. We want the one with
12456 the directory to sort after the one without, so all no directory files
12457 are at the start (normally only the compilation unit file). */
12458 if ((s1->path == s1->fname || s2->path == s2->fname))
12459 return (s2->path == s2->fname) - (s1->path == s1->fname);
12460
12461 cp1 = (const unsigned char *) s1->path;
12462 cp2 = (const unsigned char *) s2->path;
12463
12464 while (1)
12465 {
12466 ++cp1;
12467 ++cp2;
12468 /* Reached the end of the first path? If so, handle like above,
12469 but now we want longer directory prefixes before shorter ones. */
12470 if ((cp1 == (const unsigned char *) s1->fname)
12471 || (cp2 == (const unsigned char *) s2->fname))
12472 return ((cp1 == (const unsigned char *) s1->fname)
12473 - (cp2 == (const unsigned char *) s2->fname));
12474
12475 /* Character of current path component the same? */
12476 else if (*cp1 != *cp2)
12477 return *cp1 - *cp2;
12478 }
12479}
12480
12481struct file_name_acquire_data
12482{
12483 struct file_info *files;
12484 int used_files;
12485 int max_files;
12486};
12487
12488/* Traversal function for the hash table. */
12489
12490int
12491file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12492{
12493 struct dwarf_file_data *d = *slot;
12494 struct file_info *fi;
12495 const char *f;
12496
12497 gcc_assert (fnad->max_files >= d->emitted_number);
12498
12499 if (! d->emitted_number)
12500 return 1;
12501
12502 gcc_assert (fnad->max_files != fnad->used_files);
12503
12504 fi = fnad->files + fnad->used_files++;
12505
12506 f = d->filename;
12507
12508 /* Skip all leading "./". */
12509 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12510 f += 2;
12511
12512 /* Create a new array entry. */
12513 fi->path = f;
12514 fi->length = strlen (s: f);
12515 fi->file_idx = d;
12516
12517 /* Search for the file name part. */
12518 f = strrchr (s: f, DIR_SEPARATOR);
12519#if defined (DIR_SEPARATOR_2)
12520 {
12521 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12522
12523 if (g != NULL)
12524 {
12525 if (f == NULL || f < g)
12526 f = g;
12527 }
12528 }
12529#endif
12530
12531 fi->fname = f == NULL ? fi->path : f + 1;
12532 return 1;
12533}
12534
12535/* Helper function for output_file_names. Emit a FORM encoded
12536 string STR, with assembly comment start ENTRY_KIND and
12537 index IDX */
12538
12539static void
12540output_line_string (enum dwarf_form form, const char *str,
12541 const char *entry_kind, unsigned int idx)
12542{
12543 switch (form)
12544 {
12545 case DW_FORM_string:
12546 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12547 break;
12548 case DW_FORM_line_strp:
12549 if (!debug_line_str_hash)
12550 debug_line_str_hash
12551 = hash_table<indirect_string_hasher>::create_ggc (n: 10);
12552
12553 struct indirect_string_node *node;
12554 node = find_AT_string_in_table (str, table: debug_line_str_hash);
12555 set_indirect_string (node);
12556 node->form = form;
12557 dw2_asm_output_offset (dwarf_offset_size, node->label,
12558 debug_line_str_section, "%s: %#x: \"%s\"",
12559 entry_kind, 0, node->str);
12560 break;
12561 default:
12562 gcc_unreachable ();
12563 }
12564}
12565
12566/* Output the directory table and the file name table. We try to minimize
12567 the total amount of memory needed. A heuristic is used to avoid large
12568 slowdowns with many input files. */
12569
12570static void
12571output_file_names (void)
12572{
12573 struct file_name_acquire_data fnad;
12574 int numfiles;
12575 struct file_info *files;
12576 struct dir_info *dirs;
12577 int *saved;
12578 int *savehere;
12579 int *backmap;
12580 int ndirs;
12581 int idx_offset;
12582 int i;
12583
12584 if (!last_emitted_file)
12585 {
12586 if (dwarf_version >= 5)
12587 {
12588 const char *comp_dir = comp_dir_string ();
12589 if (comp_dir == NULL)
12590 comp_dir = "";
12591 dw2_asm_output_data (1, 1, "Directory entry format count");
12592 enum dwarf_form str_form = DW_FORM_string;
12593 if (DWARF5_USE_DEBUG_LINE_STR)
12594 str_form = DW_FORM_line_strp;
12595 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12596 dw2_asm_output_data_uleb128 (str_form, "%s",
12597 get_DW_FORM_name (form: str_form));
12598 dw2_asm_output_data_uleb128 (1, "Directories count");
12599 if (str_form == DW_FORM_string)
12600 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12601 else
12602 output_line_string (form: str_form, str: comp_dir, entry_kind: "Directory Entry", idx: 0);
12603 const char *filename0 = get_AT_string (die: comp_unit_die (), attr_kind: DW_AT_name);
12604 if (filename0 == NULL)
12605 filename0 = "";
12606#ifdef VMS_DEBUGGING_INFO
12607 dw2_asm_output_data (1, 4, "File name entry format count");
12608#else
12609 dw2_asm_output_data (1, 2, "File name entry format count");
12610#endif
12611 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12612 dw2_asm_output_data_uleb128 (str_form, "%s",
12613 get_DW_FORM_name (form: str_form));
12614 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12615 "DW_LNCT_directory_index");
12616 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12617 get_DW_FORM_name (form: DW_FORM_data1));
12618#ifdef VMS_DEBUGGING_INFO
12619 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12620 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12621 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12622 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12623#endif
12624 dw2_asm_output_data_uleb128 (1, "File names count");
12625
12626 output_line_string (form: str_form, str: filename0, entry_kind: "File Entry", idx: 0);
12627 dw2_asm_output_data (1, 0, NULL);
12628#ifdef VMS_DEBUGGING_INFO
12629 dw2_asm_output_data_uleb128 (0, NULL);
12630 dw2_asm_output_data_uleb128 (0, NULL);
12631#endif
12632 }
12633 else
12634 {
12635 dw2_asm_output_data (1, 0, "End directory table");
12636 dw2_asm_output_data (1, 0, "End file name table");
12637 }
12638 return;
12639 }
12640
12641 numfiles = last_emitted_file->emitted_number;
12642
12643 /* Allocate the various arrays we need. */
12644 files = XALLOCAVEC (struct file_info, numfiles);
12645 dirs = XALLOCAVEC (struct dir_info, numfiles);
12646
12647 fnad.files = files;
12648 fnad.used_files = 0;
12649 fnad.max_files = numfiles;
12650 file_table->traverse<file_name_acquire_data *, file_name_acquire> (argument: &fnad);
12651 gcc_assert (fnad.used_files == fnad.max_files);
12652
12653 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12654
12655 /* Find all the different directories used. */
12656 dirs[0].path = files[0].path;
12657 dirs[0].length = files[0].fname - files[0].path;
12658 dirs[0].prefix = -1;
12659 dirs[0].count = 1;
12660 dirs[0].dir_idx = 0;
12661 files[0].dir_idx = 0;
12662 ndirs = 1;
12663
12664 for (i = 1; i < numfiles; i++)
12665 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12666 && memcmp (s1: dirs[ndirs - 1].path, s2: files[i].path,
12667 n: dirs[ndirs - 1].length) == 0)
12668 {
12669 /* Same directory as last entry. */
12670 files[i].dir_idx = ndirs - 1;
12671 ++dirs[ndirs - 1].count;
12672 }
12673 else
12674 {
12675 int j;
12676
12677 /* This is a new directory. */
12678 dirs[ndirs].path = files[i].path;
12679 dirs[ndirs].length = files[i].fname - files[i].path;
12680 dirs[ndirs].count = 1;
12681 dirs[ndirs].dir_idx = ndirs;
12682 files[i].dir_idx = ndirs;
12683
12684 /* Search for a prefix. */
12685 dirs[ndirs].prefix = -1;
12686 for (j = 0; j < ndirs; j++)
12687 if (dirs[j].length < dirs[ndirs].length
12688 && dirs[j].length > 1
12689 && (dirs[ndirs].prefix == -1
12690 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12691 && memcmp (s1: dirs[j].path, s2: dirs[ndirs].path, n: dirs[j].length) == 0)
12692 dirs[ndirs].prefix = j;
12693
12694 ++ndirs;
12695 }
12696
12697 /* Now to the actual work. We have to find a subset of the directories which
12698 allow expressing the file name using references to the directory table
12699 with the least amount of characters. We do not do an exhaustive search
12700 where we would have to check out every combination of every single
12701 possible prefix. Instead we use a heuristic which provides nearly optimal
12702 results in most cases and never is much off. */
12703 saved = XALLOCAVEC (int, ndirs);
12704 savehere = XALLOCAVEC (int, ndirs);
12705
12706 memset (s: saved, c: '\0', n: ndirs * sizeof (saved[0]));
12707 for (i = 0; i < ndirs; i++)
12708 {
12709 int j;
12710 int total;
12711
12712 /* We can always save some space for the current directory. But this
12713 does not mean it will be enough to justify adding the directory. */
12714 savehere[i] = dirs[i].length;
12715 total = (savehere[i] - saved[i]) * dirs[i].count;
12716
12717 for (j = i + 1; j < ndirs; j++)
12718 {
12719 savehere[j] = 0;
12720 if (saved[j] < dirs[i].length)
12721 {
12722 /* Determine whether the dirs[i] path is a prefix of the
12723 dirs[j] path. */
12724 int k;
12725
12726 k = dirs[j].prefix;
12727 while (k != -1 && k != (int) i)
12728 k = dirs[k].prefix;
12729
12730 if (k == (int) i)
12731 {
12732 /* Yes it is. We can possibly save some memory by
12733 writing the filenames in dirs[j] relative to
12734 dirs[i]. */
12735 savehere[j] = dirs[i].length;
12736 total += (savehere[j] - saved[j]) * dirs[j].count;
12737 }
12738 }
12739 }
12740
12741 /* Check whether we can save enough to justify adding the dirs[i]
12742 directory. */
12743 if (total > dirs[i].length + 1)
12744 {
12745 /* It's worthwhile adding. */
12746 for (j = i; j < ndirs; j++)
12747 if (savehere[j] > 0)
12748 {
12749 /* Remember how much we saved for this directory so far. */
12750 saved[j] = savehere[j];
12751
12752 /* Remember the prefix directory. */
12753 dirs[j].dir_idx = i;
12754 }
12755 }
12756 }
12757
12758 /* Emit the directory name table. */
12759 idx_offset = dirs[0].length > 0 ? 1 : 0;
12760 enum dwarf_form str_form = DW_FORM_string;
12761 enum dwarf_form idx_form = DW_FORM_udata;
12762 if (dwarf_version >= 5)
12763 {
12764 const char *comp_dir = comp_dir_string ();
12765 if (comp_dir == NULL)
12766 comp_dir = "";
12767 dw2_asm_output_data (1, 1, "Directory entry format count");
12768 if (DWARF5_USE_DEBUG_LINE_STR)
12769 str_form = DW_FORM_line_strp;
12770 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12771 dw2_asm_output_data_uleb128 (str_form, "%s",
12772 get_DW_FORM_name (form: str_form));
12773 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12774 if (str_form == DW_FORM_string)
12775 {
12776 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12777 for (i = 1 - idx_offset; i < ndirs; i++)
12778 dw2_asm_output_nstring (dirs[i].path,
12779 dirs[i].length
12780 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12781 "Directory Entry: %#x", i + idx_offset);
12782 }
12783 else
12784 {
12785 output_line_string (form: str_form, str: comp_dir, entry_kind: "Directory Entry", idx: 0);
12786 for (i = 1 - idx_offset; i < ndirs; i++)
12787 {
12788 const char *str
12789 = ggc_alloc_string (contents: dirs[i].path,
12790 length: dirs[i].length
12791 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12792 output_line_string (form: str_form, str, entry_kind: "Directory Entry",
12793 idx: (unsigned) i + idx_offset);
12794 }
12795 }
12796 }
12797 else
12798 {
12799 for (i = 1 - idx_offset; i < ndirs; i++)
12800 dw2_asm_output_nstring (dirs[i].path,
12801 dirs[i].length
12802 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12803 "Directory Entry: %#x", i + idx_offset);
12804
12805 dw2_asm_output_data (1, 0, "End directory table");
12806 }
12807
12808 /* We have to emit them in the order of emitted_number since that's
12809 used in the debug info generation. To do this efficiently we
12810 generate a back-mapping of the indices first. */
12811 backmap = XALLOCAVEC (int, numfiles);
12812 for (i = 0; i < numfiles; i++)
12813 backmap[files[i].file_idx->emitted_number - 1] = i;
12814
12815 if (dwarf_version >= 5)
12816 {
12817 const char *filename0 = get_AT_string (die: comp_unit_die (), attr_kind: DW_AT_name);
12818 if (filename0 == NULL)
12819 filename0 = "";
12820 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12821 DW_FORM_data2. Choose one based on the number of directories
12822 and how much space would they occupy in each encoding.
12823 If we have at most 256 directories, all indexes fit into
12824 a single byte, so DW_FORM_data1 is most compact (if there
12825 are at most 128 directories, DW_FORM_udata would be as
12826 compact as that, but not shorter and slower to decode). */
12827 if (ndirs + idx_offset <= 256)
12828 idx_form = DW_FORM_data1;
12829 /* If there are more than 65536 directories, we have to use
12830 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12831 Otherwise, compute what space would occupy if all the indexes
12832 used DW_FORM_udata - sum - and compare that to how large would
12833 be DW_FORM_data2 encoding, and pick the more efficient one. */
12834 else if (ndirs + idx_offset <= 65536)
12835 {
12836 unsigned HOST_WIDE_INT sum = 1;
12837 for (i = 0; i < numfiles; i++)
12838 {
12839 int file_idx = backmap[i];
12840 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12841 sum += size_of_uleb128 (dir_idx);
12842 }
12843 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12844 idx_form = DW_FORM_data2;
12845 }
12846#ifdef VMS_DEBUGGING_INFO
12847 dw2_asm_output_data (1, 4, "File name entry format count");
12848#else
12849 dw2_asm_output_data (1, 2, "File name entry format count");
12850#endif
12851 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12852 dw2_asm_output_data_uleb128 (str_form, "%s",
12853 get_DW_FORM_name (form: str_form));
12854 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12855 "DW_LNCT_directory_index");
12856 dw2_asm_output_data_uleb128 (idx_form, "%s",
12857 get_DW_FORM_name (form: idx_form));
12858#ifdef VMS_DEBUGGING_INFO
12859 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12860 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12861 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12862 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12863#endif
12864 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12865
12866 output_line_string (form: str_form, str: filename0, entry_kind: "File Entry", idx: 0);
12867
12868 /* Include directory index. */
12869 if (idx_form != DW_FORM_udata)
12870 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12871 0, NULL);
12872 else
12873 dw2_asm_output_data_uleb128 (0, NULL);
12874
12875#ifdef VMS_DEBUGGING_INFO
12876 dw2_asm_output_data_uleb128 (0, NULL);
12877 dw2_asm_output_data_uleb128 (0, NULL);
12878#endif
12879 }
12880
12881 /* Now write all the file names. */
12882 for (i = 0; i < numfiles; i++)
12883 {
12884 int file_idx = backmap[i];
12885 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12886
12887#ifdef VMS_DEBUGGING_INFO
12888#define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12889
12890 /* Setting these fields can lead to debugger miscomparisons,
12891 but VMS Debug requires them to be set correctly. */
12892
12893 int ver;
12894 long long cdt;
12895 long siz;
12896 int maxfilelen = (strlen (files[file_idx].path)
12897 + dirs[dir_idx].length
12898 + MAX_VMS_VERSION_LEN + 1);
12899 char *filebuf = XALLOCAVEC (char, maxfilelen);
12900
12901 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12902 snprintf (filebuf, maxfilelen, "%s;%d",
12903 files[file_idx].path + dirs[dir_idx].length, ver);
12904
12905 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12906
12907 /* Include directory index. */
12908 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12909 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12910 dir_idx + idx_offset, NULL);
12911 else
12912 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12913
12914 /* Modification time. */
12915 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12916 &cdt, 0, 0, 0) == 0)
12917 ? cdt : 0, NULL);
12918
12919 /* File length in bytes. */
12920 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12921 0, &siz, 0, 0) == 0)
12922 ? siz : 0, NULL);
12923#else
12924 output_line_string (form: str_form,
12925 str: files[file_idx].path + dirs[dir_idx].length,
12926 entry_kind: "File Entry", idx: (unsigned) i + 1);
12927
12928 /* Include directory index. */
12929 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12930 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12931 dir_idx + idx_offset, NULL);
12932 else
12933 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12934
12935 if (dwarf_version >= 5)
12936 continue;
12937
12938 /* Modification time. */
12939 dw2_asm_output_data_uleb128 (0, NULL);
12940
12941 /* File length in bytes. */
12942 dw2_asm_output_data_uleb128 (0, NULL);
12943#endif /* VMS_DEBUGGING_INFO */
12944 }
12945
12946 if (dwarf_version < 5)
12947 dw2_asm_output_data (1, 0, "End file name table");
12948}
12949
12950
12951/* Output one line number table into the .debug_line section. */
12952
12953static void
12954output_one_line_info_table (dw_line_info_table *table)
12955{
12956 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12957 unsigned int current_line = 1;
12958 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12959 dw_line_info_entry *ent, *prev_addr = NULL;
12960 size_t i;
12961 unsigned int view;
12962
12963 view = 0;
12964
12965 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12966 {
12967 switch (ent->opcode)
12968 {
12969 case LI_set_address:
12970 /* ??? Unfortunately, we have little choice here currently, and
12971 must always use the most general form. GCC does not know the
12972 address delta itself, so we can't use DW_LNS_advance_pc. Many
12973 ports do have length attributes which will give an upper bound
12974 on the address range. We could perhaps use length attributes
12975 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12976 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12977
12978 view = 0;
12979
12980 /* This can handle any delta. This takes
12981 4+DWARF2_ADDR_SIZE bytes. */
12982 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12983 debug_variable_location_views
12984 ? ", reset view to 0" : "");
12985 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12986 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12987 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12988
12989 prev_addr = ent;
12990 break;
12991
12992 case LI_adv_address:
12993 {
12994 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12995 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12996 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12997
12998 view++;
12999
13000 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
13001 dw2_asm_output_delta (2, line_label, prev_label,
13002 "from %s to %s", prev_label, line_label);
13003
13004 prev_addr = ent;
13005 break;
13006 }
13007
13008 case LI_set_line:
13009 if (ent->val == current_line)
13010 {
13011 /* We still need to start a new row, so output a copy insn. */
13012 dw2_asm_output_data (1, DW_LNS_copy,
13013 "copy line %u", current_line);
13014 }
13015 else
13016 {
13017 int line_offset = ent->val - current_line;
13018 int line_delta = line_offset - DWARF_LINE_BASE;
13019
13020 current_line = ent->val;
13021 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13022 {
13023 /* This can handle deltas from -10 to 234, using the current
13024 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13025 This takes 1 byte. */
13026 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13027 "line %u", current_line);
13028 }
13029 else
13030 {
13031 /* This can handle any delta. This takes at least 4 bytes,
13032 depending on the value being encoded. */
13033 dw2_asm_output_data (1, DW_LNS_advance_line,
13034 "advance to line %u", current_line);
13035 dw2_asm_output_data_sleb128 (line_offset, NULL);
13036 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13037 }
13038 }
13039 break;
13040
13041 case LI_set_file:
13042 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13043 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13044 break;
13045
13046 case LI_set_column:
13047 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13048 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13049 break;
13050
13051 case LI_negate_stmt:
13052 current_is_stmt = !current_is_stmt;
13053 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13054 "is_stmt %d", current_is_stmt);
13055 break;
13056
13057 case LI_set_prologue_end:
13058 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13059 "set prologue end");
13060 break;
13061
13062 case LI_set_epilogue_begin:
13063 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13064 "set epilogue begin");
13065 break;
13066
13067 case LI_set_discriminator:
13068 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13069 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13070 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13071 dw2_asm_output_data_uleb128 (ent->val, NULL);
13072 break;
13073 }
13074 }
13075
13076 /* Emit debug info for the address of the end of the table. */
13077 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13078 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13079 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13080 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13081
13082 dw2_asm_output_data (1, 0, "end sequence");
13083 dw2_asm_output_data_uleb128 (1, NULL);
13084 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13085}
13086
13087static unsigned int output_line_info_generation;
13088
13089/* Output the source line number correspondence information. This
13090 information goes into the .debug_line section. */
13091
13092static void
13093output_line_info (bool prologue_only)
13094{
13095 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13096 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13097 bool saw_one = false;
13098 int opc;
13099
13100 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13101 output_line_info_generation);
13102 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13103 output_line_info_generation);
13104 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13105 output_line_info_generation);
13106 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13107 output_line_info_generation++);
13108
13109 if (!XCOFF_DEBUGGING_INFO)
13110 {
13111 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13112 dw2_asm_output_data (4, 0xffffffff,
13113 "Initial length escape value indicating 64-bit DWARF extension");
13114 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13115 "Length of Source Line Info");
13116 }
13117
13118 ASM_OUTPUT_LABEL (asm_out_file, l1);
13119
13120 output_dwarf_version ();
13121 if (dwarf_version >= 5)
13122 {
13123 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13124 dw2_asm_output_data (1, 0, "Segment Size");
13125 }
13126 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13127 ASM_OUTPUT_LABEL (asm_out_file, p1);
13128
13129 /* Define the architecture-dependent minimum instruction length (in bytes).
13130 In this implementation of DWARF, this field is used for information
13131 purposes only. Since GCC generates assembly language, we have no
13132 a priori knowledge of how many instruction bytes are generated for each
13133 source line, and therefore can use only the DW_LNE_set_address and
13134 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13135 this as '1', which is "correct enough" for all architectures,
13136 and don't let the target override. */
13137 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13138
13139 if (dwarf_version >= 4)
13140 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13141 "Maximum Operations Per Instruction");
13142 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13143 "Default is_stmt_start flag");
13144 dw2_asm_output_data (1, DWARF_LINE_BASE,
13145 "Line Base Value (Special Opcodes)");
13146 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13147 "Line Range Value (Special Opcodes)");
13148 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13149 "Special Opcode Base");
13150
13151 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13152 {
13153 int n_op_args;
13154 switch (opc)
13155 {
13156 case DW_LNS_advance_pc:
13157 case DW_LNS_advance_line:
13158 case DW_LNS_set_file:
13159 case DW_LNS_set_column:
13160 case DW_LNS_fixed_advance_pc:
13161 case DW_LNS_set_isa:
13162 n_op_args = 1;
13163 break;
13164 default:
13165 n_op_args = 0;
13166 break;
13167 }
13168
13169 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13170 opc, n_op_args);
13171 }
13172
13173 /* Write out the information about the files we use. */
13174 output_file_names ();
13175 ASM_OUTPUT_LABEL (asm_out_file, p2);
13176 if (prologue_only)
13177 {
13178 /* Output the marker for the end of the line number info. */
13179 ASM_OUTPUT_LABEL (asm_out_file, l2);
13180 return;
13181 }
13182
13183 if (separate_line_info)
13184 {
13185 dw_line_info_table *table;
13186 size_t i;
13187
13188 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13189 if (table->in_use)
13190 {
13191 output_one_line_info_table (table);
13192 saw_one = true;
13193 }
13194 }
13195 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13196 {
13197 output_one_line_info_table (table: cold_text_section_line_info);
13198 saw_one = true;
13199 }
13200
13201 /* ??? Some Darwin linkers crash on a .debug_line section with no
13202 sequences. Further, merely a DW_LNE_end_sequence entry is not
13203 sufficient -- the address column must also be initialized.
13204 Make sure to output at least one set_address/end_sequence pair,
13205 choosing .text since that section is always present. */
13206 if (text_section_line_info->in_use || !saw_one)
13207 output_one_line_info_table (table: text_section_line_info);
13208
13209 /* Output the marker for the end of the line number info. */
13210 ASM_OUTPUT_LABEL (asm_out_file, l2);
13211}
13212
13213/* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13214
13215static inline bool
13216need_endianity_attribute_p (bool reverse)
13217{
13218 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13219}
13220
13221/* Given a pointer to a tree node for some base type, return a pointer to
13222 a DIE that describes the given type. REVERSE is true if the type is
13223 to be interpreted in the reverse storage order wrt the target order.
13224
13225 This routine must only be called for GCC type nodes that correspond to
13226 Dwarf base (fundamental) types. */
13227
13228dw_die_ref
13229base_type_die (tree type, bool reverse)
13230{
13231 dw_die_ref base_type_result;
13232 enum dwarf_type encoding;
13233 bool fpt_used = false;
13234 struct fixed_point_type_info fpt_info;
13235 tree type_bias = NULL_TREE;
13236
13237 /* If this is a subtype that should not be emitted as a subrange type,
13238 use the base type. See subrange_type_for_debug_p. */
13239 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13240 type = TREE_TYPE (type);
13241
13242 switch (TREE_CODE (type))
13243 {
13244 case INTEGER_TYPE:
13245 if ((dwarf_version >= 4 || !dwarf_strict)
13246 && TYPE_NAME (type)
13247 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13248 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13249 && DECL_NAME (TYPE_NAME (type)))
13250 {
13251 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13252 if (strcmp (s1: name, s2: "char16_t") == 0
13253 || strcmp (s1: name, s2: "char8_t") == 0
13254 || strcmp (s1: name, s2: "char32_t") == 0)
13255 {
13256 encoding = DW_ATE_UTF;
13257 break;
13258 }
13259 }
13260 if ((dwarf_version >= 3 || !dwarf_strict)
13261 && lang_hooks.types.get_fixed_point_type_info)
13262 {
13263 memset (s: &fpt_info, c: 0, n: sizeof (fpt_info));
13264 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13265 {
13266 fpt_used = true;
13267 encoding = ((TYPE_UNSIGNED (type))
13268 ? DW_ATE_unsigned_fixed
13269 : DW_ATE_signed_fixed);
13270 break;
13271 }
13272 }
13273 if (TYPE_STRING_FLAG (type))
13274 {
13275 if ((dwarf_version >= 4 || !dwarf_strict)
13276 && is_rust ()
13277 && int_size_in_bytes (type) == 4)
13278 encoding = DW_ATE_UTF;
13279 else if (TYPE_UNSIGNED (type))
13280 encoding = DW_ATE_unsigned_char;
13281 else
13282 encoding = DW_ATE_signed_char;
13283 }
13284 else if (TYPE_UNSIGNED (type))
13285 encoding = DW_ATE_unsigned;
13286 else
13287 encoding = DW_ATE_signed;
13288
13289 if (!dwarf_strict
13290 && lang_hooks.types.get_type_bias)
13291 type_bias = lang_hooks.types.get_type_bias (type);
13292 break;
13293
13294 case REAL_TYPE:
13295 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13296 {
13297 if (dwarf_version >= 3 || !dwarf_strict)
13298 encoding = DW_ATE_decimal_float;
13299 else
13300 encoding = DW_ATE_lo_user;
13301 }
13302 else
13303 encoding = DW_ATE_float;
13304 break;
13305
13306 case FIXED_POINT_TYPE:
13307 if (!(dwarf_version >= 3 || !dwarf_strict))
13308 encoding = DW_ATE_lo_user;
13309 else if (TYPE_UNSIGNED (type))
13310 encoding = DW_ATE_unsigned_fixed;
13311 else
13312 encoding = DW_ATE_signed_fixed;
13313 break;
13314
13315 /* Dwarf2 doesn't know anything about complex ints, so use
13316 a user defined type for it. */
13317 case COMPLEX_TYPE:
13318 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13319 encoding = DW_ATE_complex_float;
13320 else
13321 encoding = DW_ATE_lo_user;
13322 break;
13323
13324 case BOOLEAN_TYPE:
13325 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13326 encoding = DW_ATE_boolean;
13327 break;
13328
13329 case BITINT_TYPE:
13330 /* C23 _BitInt(N). */
13331 if (TYPE_UNSIGNED (type))
13332 encoding = DW_ATE_unsigned;
13333 else
13334 encoding = DW_ATE_signed;
13335 break;
13336
13337 default:
13338 /* No other TREE_CODEs are Dwarf fundamental types. */
13339 gcc_unreachable ();
13340 }
13341
13342 base_type_result = new_die_raw (tag_value: DW_TAG_base_type);
13343
13344 add_AT_unsigned (die: base_type_result, attr_kind: DW_AT_byte_size,
13345 unsigned_val: int_size_in_bytes (type));
13346 add_AT_unsigned (die: base_type_result, attr_kind: DW_AT_encoding, unsigned_val: encoding);
13347 if (TREE_CODE (type) == BITINT_TYPE)
13348 add_AT_unsigned (die: base_type_result, attr_kind: DW_AT_bit_size, TYPE_PRECISION (type));
13349
13350 if (need_endianity_attribute_p (reverse))
13351 add_AT_unsigned (die: base_type_result, attr_kind: DW_AT_endianity,
13352 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13353
13354 add_alignment_attribute (base_type_result, type);
13355
13356 if (fpt_used)
13357 {
13358 switch (fpt_info.scale_factor_kind)
13359 {
13360 case fixed_point_scale_factor_binary:
13361 add_AT_int (die: base_type_result, attr_kind: DW_AT_binary_scale,
13362 int_val: fpt_info.scale_factor.binary);
13363 break;
13364
13365 case fixed_point_scale_factor_decimal:
13366 add_AT_int (die: base_type_result, attr_kind: DW_AT_decimal_scale,
13367 int_val: fpt_info.scale_factor.decimal);
13368 break;
13369
13370 case fixed_point_scale_factor_arbitrary:
13371 /* Arbitrary scale factors cannot be described in standard DWARF. */
13372 if (!dwarf_strict)
13373 {
13374 /* Describe the scale factor as a rational constant. */
13375 const dw_die_ref scale_factor
13376 = new_die (tag_value: DW_TAG_constant, parent_die: comp_unit_die (), t: type);
13377
13378 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13379 fpt_info.scale_factor.arbitrary.numerator,
13380 dw_scalar_form_constant, NULL);
13381 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13382 fpt_info.scale_factor.arbitrary.denominator,
13383 dw_scalar_form_constant, NULL);
13384
13385 add_AT_die_ref (die: base_type_result, attr_kind: DW_AT_small, targ_die: scale_factor);
13386 }
13387 break;
13388
13389 default:
13390 gcc_unreachable ();
13391 }
13392 }
13393
13394 if (type_bias)
13395 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13396 dw_scalar_form_constant
13397 | dw_scalar_form_exprloc
13398 | dw_scalar_form_reference,
13399 NULL);
13400
13401 return base_type_result;
13402}
13403
13404/* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13405 named 'auto' in its type: return true for it, false otherwise. */
13406
13407static inline bool
13408is_cxx_auto (tree type)
13409{
13410 if (is_cxx ())
13411 {
13412 tree name = TYPE_IDENTIFIER (type);
13413 if (name == get_identifier ("auto")
13414 || name == get_identifier ("decltype(auto)"))
13415 return true;
13416 }
13417 return false;
13418}
13419
13420/* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13421 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13422
13423static inline bool
13424is_base_type (tree type)
13425{
13426 switch (TREE_CODE (type))
13427 {
13428 case INTEGER_TYPE:
13429 case REAL_TYPE:
13430 case FIXED_POINT_TYPE:
13431 case COMPLEX_TYPE:
13432 case BOOLEAN_TYPE:
13433 case BITINT_TYPE:
13434 return true;
13435
13436 case VOID_TYPE:
13437 case OPAQUE_TYPE:
13438 case ARRAY_TYPE:
13439 case RECORD_TYPE:
13440 case UNION_TYPE:
13441 case QUAL_UNION_TYPE:
13442 case ENUMERAL_TYPE:
13443 case FUNCTION_TYPE:
13444 case METHOD_TYPE:
13445 case POINTER_TYPE:
13446 case REFERENCE_TYPE:
13447 case NULLPTR_TYPE:
13448 case OFFSET_TYPE:
13449 case LANG_TYPE:
13450 case VECTOR_TYPE:
13451 return false;
13452
13453 default:
13454 if (is_cxx_auto (type))
13455 return false;
13456 gcc_unreachable ();
13457 }
13458}
13459
13460/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13461 node, return the size in bits for the type if it is a constant, or else
13462 return the alignment for the type if the type's size is not constant, or
13463 else return BITS_PER_WORD if the type actually turns out to be an
13464 ERROR_MARK node. */
13465
13466static inline unsigned HOST_WIDE_INT
13467simple_type_size_in_bits (const_tree type)
13468{
13469 if (TREE_CODE (type) == ERROR_MARK)
13470 return BITS_PER_WORD;
13471 else if (TYPE_SIZE (type) == NULL_TREE)
13472 return 0;
13473 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13474 return tree_to_uhwi (TYPE_SIZE (type));
13475 else
13476 return TYPE_ALIGN (type);
13477}
13478
13479/* Similarly, but return an offset_int instead of UHWI. */
13480
13481static inline offset_int
13482offset_int_type_size_in_bits (const_tree type)
13483{
13484 if (TREE_CODE (type) == ERROR_MARK)
13485 return BITS_PER_WORD;
13486 else if (TYPE_SIZE (type) == NULL_TREE)
13487 return 0;
13488 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13489 return wi::to_offset (TYPE_SIZE (type));
13490 else
13491 return TYPE_ALIGN (type);
13492}
13493
13494/* Given a pointer to a tree node for a subrange type, return a pointer
13495 to a DIE that describes the given type. */
13496
13497static dw_die_ref
13498subrange_type_die (tree type, tree low, tree high, tree bias,
13499 dw_die_ref context_die)
13500{
13501 dw_die_ref subrange_die;
13502 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13503
13504 if (context_die == NULL)
13505 context_die = comp_unit_die ();
13506
13507 subrange_die = new_die (tag_value: DW_TAG_subrange_type, parent_die: context_die, t: type);
13508
13509 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13510 {
13511 /* The size of the subrange type and its base type do not match,
13512 so we need to generate a size attribute for the subrange type. */
13513 add_AT_unsigned (die: subrange_die, attr_kind: DW_AT_byte_size, unsigned_val: size_in_bytes);
13514 }
13515
13516 add_alignment_attribute (subrange_die, type);
13517
13518 if (low)
13519 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13520 if (high)
13521 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13522 if (bias && !dwarf_strict)
13523 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13524 dw_scalar_form_constant
13525 | dw_scalar_form_exprloc
13526 | dw_scalar_form_reference,
13527 NULL);
13528
13529 return subrange_die;
13530}
13531
13532/* Returns the (const and/or volatile) cv_qualifiers associated with
13533 the decl node. This will normally be augmented with the
13534 cv_qualifiers of the underlying type in add_type_attribute. */
13535
13536static int
13537decl_quals (const_tree decl)
13538{
13539 return ((TREE_READONLY (decl)
13540 /* The C++ front-end correctly marks reference-typed
13541 variables as readonly, but from a language (and debug
13542 info) standpoint they are not const-qualified. */
13543 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13544 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13545 | (TREE_THIS_VOLATILE (decl)
13546 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13547}
13548
13549/* Determine the TYPE whose qualifiers match the largest strict subset
13550 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13551 qualifiers outside QUAL_MASK. */
13552
13553static int
13554get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13555{
13556 tree t;
13557 int best_rank = 0, best_qual = 0, max_rank;
13558
13559 type_quals &= qual_mask;
13560 max_rank = popcount_hwi (x: type_quals) - 1;
13561
13562 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13563 t = TYPE_NEXT_VARIANT (t))
13564 {
13565 int q = TYPE_QUALS (t) & qual_mask;
13566
13567 if ((q & type_quals) == q && q != type_quals
13568 && check_base_type (cand: t, base: type))
13569 {
13570 int rank = popcount_hwi (x: q);
13571
13572 if (rank > best_rank)
13573 {
13574 best_rank = rank;
13575 best_qual = q;
13576 }
13577 }
13578 }
13579
13580 return best_qual;
13581}
13582
13583struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13584static const dwarf_qual_info_t dwarf_qual_info[] =
13585{
13586 { .q: TYPE_QUAL_CONST, .t: DW_TAG_const_type },
13587 { .q: TYPE_QUAL_VOLATILE, .t: DW_TAG_volatile_type },
13588 { .q: TYPE_QUAL_RESTRICT, .t: DW_TAG_restrict_type },
13589 { .q: TYPE_QUAL_ATOMIC, .t: DW_TAG_atomic_type }
13590};
13591static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13592
13593/* If DIE is a qualified DIE of some base DIE with the same parent,
13594 return the base DIE, otherwise return NULL. Set MASK to the
13595 qualifiers added compared to the returned DIE. */
13596
13597static dw_die_ref
13598qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13599{
13600 unsigned int i;
13601 for (i = 0; i < dwarf_qual_info_size; i++)
13602 if (die->die_tag == dwarf_qual_info[i].t)
13603 break;
13604 if (i == dwarf_qual_info_size)
13605 return NULL;
13606 if (vec_safe_length (v: die->die_attr) != 1)
13607 return NULL;
13608 dw_die_ref type = get_AT_ref (die, attr_kind: DW_AT_type);
13609 if (type == NULL || type->die_parent != die->die_parent)
13610 return NULL;
13611 *mask |= dwarf_qual_info[i].q;
13612 if (depth)
13613 {
13614 dw_die_ref ret = qualified_die_p (die: type, mask, depth: depth - 1);
13615 if (ret)
13616 return ret;
13617 }
13618 return type;
13619}
13620
13621/* If TYPE is long double or complex long double that
13622 should be emitted as artificial typedef to _Float128 or
13623 complex _Float128, return the type it should be emitted as.
13624 This is done in case the target already supports 16-byte
13625 composite floating point type (ibm_extended_format). */
13626
13627static tree
13628long_double_as_float128 (tree type)
13629{
13630 if (type != long_double_type_node
13631 && type != complex_long_double_type_node)
13632 return NULL_TREE;
13633
13634 machine_mode mode, fmode;
13635 if (TREE_CODE (type) == COMPLEX_TYPE)
13636 mode = TYPE_MODE (TREE_TYPE (type));
13637 else
13638 mode = TYPE_MODE (type);
13639 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13640 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13641 if (known_eq (GET_MODE_SIZE (fmode), 16)
13642 && MODE_COMPOSITE_P (fmode))
13643 {
13644 if (type == long_double_type_node)
13645 {
13646 if (float128_type_node
13647 && (TYPE_MODE (float128_type_node)
13648 == TYPE_MODE (type)))
13649 return float128_type_node;
13650 return NULL_TREE;
13651 }
13652 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13653 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13654 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13655 == TYPE_MODE (type)))
13656 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13657 }
13658
13659 return NULL_TREE;
13660}
13661
13662/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13663 entry that chains the modifiers specified by CV_QUALS in front of the
13664 given type. REVERSE is true if the type is to be interpreted in the
13665 reverse storage order wrt the target order. */
13666
13667static dw_die_ref
13668modified_type_die (tree type, int cv_quals, bool reverse,
13669 dw_die_ref context_die)
13670{
13671 enum tree_code code = TREE_CODE (type);
13672 dw_die_ref mod_type_die;
13673 dw_die_ref sub_die = NULL;
13674 tree item_type = NULL;
13675 tree qualified_type;
13676 tree name, low, high;
13677 dw_die_ref mod_scope;
13678 struct array_descr_info info;
13679 /* Only these cv-qualifiers are currently handled. */
13680 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13681 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
13682 /* DW_AT_endianity is specified only for base types in the standard. */
13683 const bool reverse_type
13684 = need_endianity_attribute_p (reverse)
13685 && (is_base_type (type)
13686 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13687
13688 if (code == ERROR_MARK)
13689 return NULL;
13690
13691 if (lang_hooks.types.get_debug_type)
13692 {
13693 tree debug_type = lang_hooks.types.get_debug_type (type);
13694
13695 if (debug_type != NULL_TREE && debug_type != type)
13696 return modified_type_die (type: debug_type, cv_quals, reverse, context_die);
13697 }
13698
13699 cv_quals &= cv_qual_mask;
13700
13701 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13702 tag modifier (and not an attribute) old consumers won't be able
13703 to handle it. */
13704 if (dwarf_version < 3)
13705 cv_quals &= ~TYPE_QUAL_RESTRICT;
13706
13707 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13708 if (dwarf_version < 5)
13709 cv_quals &= ~TYPE_QUAL_ATOMIC;
13710
13711 /* See if we already have the appropriately qualified variant of
13712 this type. */
13713 qualified_type = get_qualified_type (type, cv_quals);
13714
13715 if (qualified_type == sizetype)
13716 {
13717 /* Try not to expose the internal sizetype type's name. */
13718 if (TYPE_NAME (qualified_type)
13719 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13720 {
13721 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13722
13723 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13724 && (TYPE_PRECISION (t)
13725 == TYPE_PRECISION (qualified_type))
13726 && (TYPE_UNSIGNED (t)
13727 == TYPE_UNSIGNED (qualified_type)));
13728 qualified_type = t;
13729 }
13730 else if (qualified_type == sizetype
13731 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13732 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13733 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13734 qualified_type = size_type_node;
13735 if (type == sizetype)
13736 type = qualified_type;
13737 }
13738
13739 /* If we do, then we can just use its DIE, if it exists. */
13740 if (qualified_type)
13741 {
13742 mod_type_die = lookup_type_die (type: qualified_type);
13743
13744 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13745 dealt with specially: the DIE with the attribute, if it exists, is
13746 placed immediately after the regular DIE for the same type. */
13747 if (mod_type_die
13748 && (!reverse_type
13749 || ((mod_type_die = mod_type_die->die_sib) != NULL
13750 && get_AT_unsigned (die: mod_type_die, attr_kind: DW_AT_endianity))))
13751 return mod_type_die;
13752 }
13753
13754 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13755
13756 /* Handle C typedef types. */
13757 if (name
13758 && TREE_CODE (name) == TYPE_DECL
13759 && DECL_ORIGINAL_TYPE (name)
13760 && !DECL_ARTIFICIAL (name))
13761 {
13762 tree dtype = TREE_TYPE (name);
13763
13764 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13765 if (qualified_type == dtype && !reverse_type)
13766 {
13767 tree origin = decl_ultimate_origin (decl: name);
13768
13769 /* Typedef variants that have an abstract origin don't get their own
13770 type DIE (see gen_typedef_die), so fall back on the ultimate
13771 abstract origin instead. */
13772 if (origin != NULL && origin != name)
13773 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13774 context_die);
13775
13776 /* For a named type, use the typedef. */
13777 gen_type_die (qualified_type, context_die);
13778 return lookup_type_die (type: qualified_type);
13779 }
13780 else
13781 {
13782 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13783 dquals &= cv_qual_mask;
13784 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13785 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13786 /* cv-unqualified version of named type. Just use
13787 the unnamed type to which it refers. */
13788 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13789 reverse, context_die);
13790 /* Else cv-qualified version of named type; fall through. */
13791 }
13792 }
13793
13794 mod_scope = scope_die_for (type, context_die);
13795
13796 if (cv_quals)
13797 {
13798 int sub_quals = 0, first_quals = 0;
13799 unsigned i;
13800 dw_die_ref first = NULL, last = NULL;
13801
13802 /* Determine a lesser qualified type that most closely matches
13803 this one. Then generate DW_TAG_* entries for the remaining
13804 qualifiers. */
13805 sub_quals = get_nearest_type_subqualifiers (type, type_quals: cv_quals,
13806 qual_mask: cv_qual_mask);
13807 if (sub_quals && use_debug_types)
13808 {
13809 bool needed = false;
13810 /* If emitting type units, make sure the order of qualifiers
13811 is canonical. Thus, start from unqualified type if
13812 an earlier qualifier is missing in sub_quals, but some later
13813 one is present there. */
13814 for (i = 0; i < dwarf_qual_info_size; i++)
13815 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13816 needed = true;
13817 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13818 {
13819 sub_quals = 0;
13820 break;
13821 }
13822 }
13823 mod_type_die = modified_type_die (type, cv_quals: sub_quals, reverse, context_die);
13824 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13825 {
13826 /* As not all intermediate qualified DIEs have corresponding
13827 tree types, ensure that qualified DIEs in the same scope
13828 as their DW_AT_type are emitted after their DW_AT_type,
13829 only with other qualified DIEs for the same type possibly
13830 in between them. Determine the range of such qualified
13831 DIEs now (first being the base type, last being corresponding
13832 last qualified DIE for it). */
13833 unsigned int count = 0;
13834 first = qualified_die_p (die: mod_type_die, mask: &first_quals,
13835 depth: dwarf_qual_info_size);
13836 if (first == NULL)
13837 first = mod_type_die;
13838 gcc_assert ((first_quals & ~sub_quals) == 0);
13839 for (count = 0, last = first;
13840 count < (1U << dwarf_qual_info_size);
13841 count++, last = last->die_sib)
13842 {
13843 int quals = 0;
13844 if (last == mod_scope->die_child)
13845 break;
13846 if (qualified_die_p (die: last->die_sib, mask: &quals, depth: dwarf_qual_info_size)
13847 != first)
13848 break;
13849 }
13850 }
13851
13852 for (i = 0; i < dwarf_qual_info_size; i++)
13853 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13854 {
13855 dw_die_ref d;
13856 if (first && first != last)
13857 {
13858 for (d = first->die_sib; ; d = d->die_sib)
13859 {
13860 int quals = 0;
13861 qualified_die_p (die: d, mask: &quals, depth: dwarf_qual_info_size);
13862 if (quals == (first_quals | dwarf_qual_info[i].q))
13863 break;
13864 if (d == last)
13865 {
13866 d = NULL;
13867 break;
13868 }
13869 }
13870 if (d)
13871 {
13872 mod_type_die = d;
13873 continue;
13874 }
13875 }
13876 if (first)
13877 {
13878 d = new_die_raw (tag_value: dwarf_qual_info[i].t);
13879 add_child_die_after (die: mod_scope, child_die: d, after_die: last);
13880 last = d;
13881 }
13882 else
13883 d = new_die (tag_value: dwarf_qual_info[i].t, parent_die: mod_scope, t: type);
13884 if (mod_type_die)
13885 add_AT_die_ref (die: d, attr_kind: DW_AT_type, targ_die: mod_type_die);
13886 mod_type_die = d;
13887 first_quals |= dwarf_qual_info[i].q;
13888 }
13889 }
13890 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13891 {
13892 dwarf_tag tag = DW_TAG_pointer_type;
13893 if (code == REFERENCE_TYPE)
13894 {
13895 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13896 tag = DW_TAG_rvalue_reference_type;
13897 else
13898 tag = DW_TAG_reference_type;
13899 }
13900 mod_type_die = new_die (tag_value: tag, parent_die: mod_scope, t: type);
13901
13902 add_AT_unsigned (die: mod_type_die, attr_kind: DW_AT_byte_size,
13903 unsigned_val: simple_type_size_in_bits (type) / BITS_PER_UNIT);
13904 add_alignment_attribute (mod_type_die, type);
13905 item_type = TREE_TYPE (type);
13906
13907 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13908 if (!ADDR_SPACE_GENERIC_P (as))
13909 {
13910 int action = targetm.addr_space.debug (as);
13911 if (action >= 0)
13912 {
13913 /* Positive values indicate an address_class. */
13914 add_AT_unsigned (die: mod_type_die, attr_kind: DW_AT_address_class, unsigned_val: action);
13915 }
13916 else
13917 {
13918 /* Negative values indicate an (inverted) segment base reg. */
13919 dw_loc_descr_ref d
13920 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13921 add_AT_loc (die: mod_type_die, attr_kind: DW_AT_segment, loc: d);
13922 }
13923 }
13924 }
13925 else if (code == ARRAY_TYPE
13926 || (lang_hooks.types.get_array_descr_info
13927 && lang_hooks.types.get_array_descr_info (type, &info)))
13928 {
13929 gen_type_die (type, mod_scope);
13930 return lookup_type_die (type);
13931 }
13932 else if (code == INTEGER_TYPE
13933 && TREE_TYPE (type) != NULL_TREE
13934 && subrange_type_for_debug_p (type, &low, &high))
13935 {
13936 tree bias = NULL_TREE;
13937 if (lang_hooks.types.get_type_bias)
13938 bias = lang_hooks.types.get_type_bias (type);
13939 mod_type_die = subrange_type_die (type, low, high, bias, context_die: mod_scope);
13940 item_type = TREE_TYPE (type);
13941 }
13942 else if (is_base_type (type))
13943 {
13944 /* If a target supports long double as different floating point
13945 modes with the same 16-byte size, use normal DW_TAG_base_type
13946 only for the composite (ibm_extended_real_format) type and
13947 for the other for the time being emit instead a "_Float128"
13948 or "complex _Float128" DW_TAG_base_type and a "long double"
13949 or "complex long double" typedef to it. */
13950 if (tree other_type = long_double_as_float128 (type))
13951 {
13952 dw_die_ref other_die;
13953 if (TYPE_NAME (other_type))
13954 other_die
13955 = modified_type_die (type: other_type, cv_quals: TYPE_UNQUALIFIED, reverse,
13956 context_die);
13957 else
13958 {
13959 other_die = base_type_die (type, reverse);
13960 add_child_die (die: comp_unit_die (), child_die: other_die);
13961 add_name_attribute (other_die,
13962 TREE_CODE (type) == COMPLEX_TYPE
13963 ? "complex _Float128" : "_Float128");
13964 }
13965 mod_type_die = new_die_raw (tag_value: DW_TAG_typedef);
13966 add_AT_die_ref (die: mod_type_die, attr_kind: DW_AT_type, targ_die: other_die);
13967 }
13968 else
13969 mod_type_die = base_type_die (type, reverse);
13970
13971 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13972 if (reverse_type)
13973 {
13974 dw_die_ref after_die
13975 = modified_type_die (type, cv_quals, reverse: false, context_die);
13976 add_child_die_after (die: mod_scope, child_die: mod_type_die, after_die);
13977 }
13978 else
13979 add_child_die (die: mod_scope, child_die: mod_type_die);
13980
13981 add_pubtype (decl: type, die: mod_type_die);
13982 }
13983 else
13984 {
13985 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13986 if (reverse_type)
13987 {
13988 dw_die_ref after_die
13989 = modified_type_die (type, cv_quals, reverse: false, context_die);
13990 gen_type_die (type, context_die, true);
13991 gcc_assert (after_die->die_sib
13992 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13993 return after_die->die_sib;
13994 }
13995
13996 gen_type_die (type, context_die);
13997
13998 /* We have to get the type_main_variant here (and pass that to the
13999 `lookup_type_die' routine) because the ..._TYPE node we have
14000 might simply be a *copy* of some original type node (where the
14001 copy was created to help us keep track of typedef names) and
14002 that copy might have a different TYPE_UID from the original
14003 ..._TYPE node. */
14004 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
14005 {
14006 /* For function/method types, can't just use type_main_variant here,
14007 because that can have different ref-qualifiers for C++,
14008 but try to canonicalize. */
14009 tree main = TYPE_MAIN_VARIANT (type);
14010 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
14011 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14012 && check_base_type (cand: t, base: main)
14013 && check_lang_type (cand: t, base: type))
14014 return lookup_type_die (type: t);
14015 return lookup_type_die (type);
14016 }
14017 /* Vectors have the debugging information in the type,
14018 not the main variant. */
14019 else if (code == VECTOR_TYPE)
14020 return lookup_type_die (type);
14021 else
14022 return lookup_type_die (type: type_main_variant (type));
14023 }
14024
14025 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14026 don't output a DW_TAG_typedef, since there isn't one in the
14027 user's program; just attach a DW_AT_name to the type.
14028 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14029 if the base type already has the same name. */
14030 if (name
14031 && ((TREE_CODE (name) != TYPE_DECL
14032 && (qualified_type == TYPE_MAIN_VARIANT (type)
14033 || (cv_quals == TYPE_UNQUALIFIED)))
14034 || (TREE_CODE (name) == TYPE_DECL
14035 && DECL_NAME (name)
14036 && !DECL_NAMELESS (name)
14037 && (TREE_TYPE (name) == qualified_type
14038 || (lang_hooks.types.get_debug_type
14039 && (lang_hooks.types.get_debug_type (TREE_TYPE (name))
14040 == qualified_type))))))
14041 {
14042 if (TREE_CODE (name) == TYPE_DECL)
14043 /* Could just call add_name_and_src_coords_attributes here,
14044 but since this is a builtin type it doesn't have any
14045 useful source coordinates anyway. */
14046 name = DECL_NAME (name);
14047 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14048 }
14049 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14050 {
14051 if (TREE_CODE (type) == BITINT_TYPE)
14052 {
14053 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14054 snprintf (s: name_buf, maxlen: sizeof (name_buf),
14055 format: "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14056 TYPE_PRECISION (type));
14057 add_name_attribute (mod_type_die, name_buf);
14058 }
14059 else
14060 {
14061 /* This probably indicates a bug. */
14062 name = TYPE_IDENTIFIER (type);
14063 add_name_attribute (mod_type_die,
14064 name
14065 ? IDENTIFIER_POINTER (name) : "__unknown__");
14066 }
14067 }
14068
14069 if (qualified_type && !reverse_type)
14070 equate_type_number_to_die (type: qualified_type, type_die: mod_type_die);
14071
14072 if (item_type)
14073 /* We must do this after the equate_type_number_to_die call, in case
14074 this is a recursive type. This ensures that the modified_type_die
14075 recursion will terminate even if the type is recursive. Recursive
14076 types are possible in Ada. */
14077 sub_die = modified_type_die (type: item_type,
14078 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14079 reverse,
14080 context_die);
14081
14082 if (sub_die != NULL)
14083 add_AT_die_ref (die: mod_type_die, attr_kind: DW_AT_type, targ_die: sub_die);
14084
14085 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14086 if (TYPE_ARTIFICIAL (type))
14087 add_AT_flag (die: mod_type_die, attr_kind: DW_AT_artificial, flag: 1);
14088
14089 return mod_type_die;
14090}
14091
14092/* Generate DIEs for the generic parameters of T.
14093 T must be either a generic type or a generic function.
14094 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14095
14096static void
14097gen_generic_params_dies (tree t)
14098{
14099 tree parms, args;
14100 int parms_num, i;
14101 dw_die_ref die = NULL;
14102 int non_default;
14103
14104 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14105 return;
14106
14107 if (TYPE_P (t))
14108 die = lookup_type_die (type: t);
14109 else if (DECL_P (t))
14110 die = lookup_decl_die (decl: t);
14111
14112 gcc_assert (die);
14113
14114 parms = lang_hooks.get_innermost_generic_parms (t);
14115 if (!parms)
14116 /* T has no generic parameter. It means T is neither a generic type
14117 or function. End of story. */
14118 return;
14119
14120 parms_num = TREE_VEC_LENGTH (parms);
14121 args = lang_hooks.get_innermost_generic_args (t);
14122 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14123 non_default = int_cst_value (TREE_CHAIN (args));
14124 else
14125 non_default = TREE_VEC_LENGTH (args);
14126 for (i = 0; i < parms_num; i++)
14127 {
14128 tree parm, arg, arg_pack_elems;
14129 dw_die_ref parm_die;
14130
14131 parm = TREE_VEC_ELT (parms, i);
14132 arg = TREE_VEC_ELT (args, i);
14133 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14134 gcc_assert (parm && TREE_VALUE (parm) && arg);
14135
14136 if (parm && TREE_VALUE (parm) && arg)
14137 {
14138 /* If PARM represents a template parameter pack,
14139 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14140 by DW_TAG_template_*_parameter DIEs for the argument
14141 pack elements of ARG. Note that ARG would then be
14142 an argument pack. */
14143 if (arg_pack_elems)
14144 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14145 arg_pack_elems,
14146 die);
14147 else
14148 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14149 true /* emit name */, die);
14150 if (i >= non_default)
14151 add_AT_flag (die: parm_die, attr_kind: DW_AT_default_value, flag: 1);
14152 }
14153 }
14154}
14155
14156/* Create and return a DIE for PARM which should be
14157 the representation of a generic type parameter.
14158 For instance, in the C++ front end, PARM would be a template parameter.
14159 ARG is the argument to PARM.
14160 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14161 name of the PARM.
14162 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14163 as a child node. */
14164
14165static dw_die_ref
14166generic_parameter_die (tree parm, tree arg,
14167 bool emit_name_p,
14168 dw_die_ref parent_die)
14169{
14170 dw_die_ref tmpl_die = NULL;
14171 const char *name = NULL;
14172
14173 /* C++20 accepts class literals as template parameters, and var
14174 decls with initializers represent them. The VAR_DECLs would be
14175 rejected, but we can take the DECL_INITIAL constructor and
14176 attempt to expand it. */
14177 if (arg && VAR_P (arg))
14178 arg = DECL_INITIAL (arg);
14179
14180 if (!parm || !DECL_NAME (parm) || !arg)
14181 return NULL;
14182
14183 /* We support non-type generic parameters and arguments,
14184 type generic parameters and arguments, as well as
14185 generic generic parameters (a.k.a. template template parameters in C++)
14186 and arguments. */
14187 if (TREE_CODE (parm) == PARM_DECL)
14188 /* PARM is a nontype generic parameter */
14189 tmpl_die = new_die (tag_value: DW_TAG_template_value_param, parent_die, t: parm);
14190 else if (TREE_CODE (parm) == TYPE_DECL)
14191 /* PARM is a type generic parameter. */
14192 tmpl_die = new_die (tag_value: DW_TAG_template_type_param, parent_die, t: parm);
14193 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14194 /* PARM is a generic generic parameter.
14195 Its DIE is a GNU extension. It shall have a
14196 DW_AT_name attribute to represent the name of the template template
14197 parameter, and a DW_AT_GNU_template_name attribute to represent the
14198 name of the template template argument. */
14199 tmpl_die = new_die (tag_value: DW_TAG_GNU_template_template_param,
14200 parent_die, t: parm);
14201 else
14202 gcc_unreachable ();
14203
14204 if (tmpl_die)
14205 {
14206 tree tmpl_type;
14207
14208 /* If PARM is a generic parameter pack, it means we are
14209 emitting debug info for a template argument pack element.
14210 In other terms, ARG is a template argument pack element.
14211 In that case, we don't emit any DW_AT_name attribute for
14212 the die. */
14213 if (emit_name_p)
14214 {
14215 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14216 gcc_assert (name);
14217 add_AT_string (die: tmpl_die, attr_kind: DW_AT_name, str: name);
14218 }
14219
14220 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14221 {
14222 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14223 TMPL_DIE should have a child DW_AT_type attribute that is set
14224 to the type of the argument to PARM, which is ARG.
14225 If PARM is a type generic parameter, TMPL_DIE should have a
14226 child DW_AT_type that is set to ARG. */
14227 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14228 add_type_attribute (tmpl_die, tmpl_type,
14229 (TREE_THIS_VOLATILE (tmpl_type)
14230 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14231 false, parent_die);
14232 }
14233 else
14234 {
14235 /* So TMPL_DIE is a DIE representing a
14236 a generic generic template parameter, a.k.a template template
14237 parameter in C++ and arg is a template. */
14238
14239 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14240 to the name of the argument. */
14241 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, scope: 1);
14242 if (name)
14243 add_AT_string (die: tmpl_die, attr_kind: DW_AT_GNU_template_name, str: name);
14244 }
14245
14246 if (TREE_CODE (parm) == PARM_DECL)
14247 /* So PARM is a non-type generic parameter.
14248 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14249 attribute of TMPL_DIE which value represents the value
14250 of ARG.
14251 We must be careful here:
14252 The value of ARG might reference some function decls.
14253 We might currently be emitting debug info for a generic
14254 type and types are emitted before function decls, we don't
14255 know if the function decls referenced by ARG will actually be
14256 emitted after cgraph computations.
14257 So must defer the generation of the DW_AT_const_value to
14258 after cgraph is ready. */
14259 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14260 }
14261
14262 return tmpl_die;
14263}
14264
14265/* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14266 PARM_PACK must be a template parameter pack. The returned DIE
14267 will be child DIE of PARENT_DIE. */
14268
14269static dw_die_ref
14270template_parameter_pack_die (tree parm_pack,
14271 tree parm_pack_args,
14272 dw_die_ref parent_die)
14273{
14274 dw_die_ref die;
14275 int j;
14276
14277 gcc_assert (parent_die && parm_pack);
14278
14279 die = new_die (tag_value: DW_TAG_GNU_template_parameter_pack, parent_die, t: parm_pack);
14280 add_name_and_src_coords_attributes (die, parm_pack);
14281 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14282 generic_parameter_die (parm: parm_pack,
14283 TREE_VEC_ELT (parm_pack_args, j),
14284 emit_name_p: false /* Don't emit DW_AT_name */,
14285 parent_die: die);
14286 return die;
14287}
14288
14289/* Return the debugger register number described by a given RTL node. */
14290
14291static unsigned int
14292debugger_reg_number (const_rtx rtl)
14293{
14294 unsigned regno = REGNO (rtl);
14295
14296 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14297
14298#ifdef LEAF_REG_REMAP
14299 if (crtl->uses_only_leaf_regs)
14300 {
14301 int leaf_reg = LEAF_REG_REMAP (regno);
14302 if (leaf_reg != -1)
14303 regno = (unsigned) leaf_reg;
14304 }
14305#endif
14306
14307 regno = DEBUGGER_REGNO (regno);
14308 gcc_assert (regno != INVALID_REGNUM);
14309 return regno;
14310}
14311
14312/* Optionally add a DW_OP_piece term to a location description expression.
14313 DW_OP_piece is only added if the location description expression already
14314 doesn't end with DW_OP_piece. */
14315
14316static void
14317add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14318{
14319 dw_loc_descr_ref loc;
14320
14321 if (*list_head != NULL)
14322 {
14323 /* Find the end of the chain. */
14324 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14325 ;
14326
14327 if (loc->dw_loc_opc != DW_OP_piece)
14328 loc->dw_loc_next = new_loc_descr (op: DW_OP_piece, oprnd1: size, oprnd2: 0);
14329 }
14330}
14331
14332/* Return a location descriptor that designates a machine register or
14333 zero if there is none. */
14334
14335static dw_loc_descr_ref
14336reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14337{
14338 rtx regs;
14339
14340 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14341 return 0;
14342
14343 /* We only use "frame base" when we're sure we're talking about the
14344 post-prologue local stack frame. We do this by *not* running
14345 register elimination until this point, and recognizing the special
14346 argument pointer and soft frame pointer rtx's.
14347 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14348 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14349 && (ira_use_lra_p
14350 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14351 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14352 {
14353 dw_loc_descr_ref result = NULL;
14354
14355 if (dwarf_version >= 4 || !dwarf_strict)
14356 {
14357 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14358 initialized);
14359 if (result)
14360 add_loc_descr (list_head: &result,
14361 descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
14362 }
14363 return result;
14364 }
14365
14366 regs = targetm.dwarf_register_span (rtl);
14367
14368 if (REG_NREGS (rtl) > 1 || regs)
14369 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14370 else
14371 {
14372 unsigned int debugger_regnum = debugger_reg_number (rtl);
14373 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14374 return 0;
14375 return one_reg_loc_descriptor (debugger_regnum, initialized);
14376 }
14377}
14378
14379/* Return a location descriptor that designates a machine register for
14380 a given hard register number. */
14381
14382static dw_loc_descr_ref
14383one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14384{
14385 dw_loc_descr_ref reg_loc_descr;
14386
14387 if (regno <= 31)
14388 reg_loc_descr
14389 = new_loc_descr (op: (enum dwarf_location_atom) (DW_OP_reg0 + regno), oprnd1: 0, oprnd2: 0);
14390 else
14391 reg_loc_descr = new_loc_descr (op: DW_OP_regx, oprnd1: regno, oprnd2: 0);
14392
14393 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14394 add_loc_descr (list_head: &reg_loc_descr, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
14395
14396 return reg_loc_descr;
14397}
14398
14399/* Given an RTL of a register, return a location descriptor that
14400 designates a value that spans more than one register. */
14401
14402static dw_loc_descr_ref
14403multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14404 enum var_init_status initialized)
14405{
14406 int size, i;
14407 dw_loc_descr_ref loc_result = NULL;
14408
14409 /* Simple, contiguous registers. */
14410 if (regs == NULL_RTX)
14411 {
14412 unsigned reg = REGNO (rtl);
14413 int nregs;
14414
14415#ifdef LEAF_REG_REMAP
14416 if (crtl->uses_only_leaf_regs)
14417 {
14418 int leaf_reg = LEAF_REG_REMAP (reg);
14419 if (leaf_reg != -1)
14420 reg = (unsigned) leaf_reg;
14421 }
14422#endif
14423
14424 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14425 nregs = REG_NREGS (rtl);
14426
14427 /* At present we only track constant-sized pieces. */
14428 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (const_value: &size))
14429 return NULL;
14430 size /= nregs;
14431
14432 loc_result = NULL;
14433 while (nregs--)
14434 {
14435 dw_loc_descr_ref t;
14436
14437 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14438 initialized: VAR_INIT_STATUS_INITIALIZED);
14439 add_loc_descr (list_head: &loc_result, descr: t);
14440 add_loc_descr_op_piece (list_head: &loc_result, size);
14441 ++reg;
14442 }
14443 return loc_result;
14444 }
14445
14446 /* Now onto stupid register sets in non contiguous locations. */
14447
14448 gcc_assert (GET_CODE (regs) == PARALLEL);
14449
14450 /* At present we only track constant-sized pieces. */
14451 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (const_value: &size))
14452 return NULL;
14453 loc_result = NULL;
14454
14455 for (i = 0; i < XVECLEN (regs, 0); ++i)
14456 {
14457 dw_loc_descr_ref t;
14458
14459 t = one_reg_loc_descriptor (regno: debugger_reg_number (XVECEXP (regs, 0, i)),
14460 initialized: VAR_INIT_STATUS_INITIALIZED);
14461 add_loc_descr (list_head: &loc_result, descr: t);
14462 add_loc_descr_op_piece (list_head: &loc_result, size);
14463 }
14464
14465 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14466 add_loc_descr (list_head: &loc_result, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
14467 return loc_result;
14468}
14469
14470static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14471
14472/* Return a location descriptor that designates a constant i,
14473 as a compound operation from constant (i >> shift), constant shift
14474 and DW_OP_shl. */
14475
14476static dw_loc_descr_ref
14477int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14478{
14479 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14480 add_loc_descr (list_head: &ret, descr: int_loc_descriptor (shift));
14481 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
14482 return ret;
14483}
14484
14485/* Return a location descriptor that designates constant POLY_I. */
14486
14487static dw_loc_descr_ref
14488int_loc_descriptor (poly_int64 poly_i)
14489{
14490 enum dwarf_location_atom op;
14491
14492 HOST_WIDE_INT i;
14493 if (!poly_i.is_constant (const_value: &i))
14494 {
14495 /* Create location descriptions for the non-constant part and
14496 add any constant offset at the end. */
14497 dw_loc_descr_ref ret = NULL;
14498 HOST_WIDE_INT constant = poly_i.coeffs[0];
14499 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14500 {
14501 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14502 if (coeff != 0)
14503 {
14504 dw_loc_descr_ref start = ret;
14505 unsigned int factor;
14506 int bias;
14507 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14508 (j, &factor, &bias);
14509
14510 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14511 add COEFF * (REGNO / FACTOR) now and subtract
14512 COEFF * BIAS from the final constant part. */
14513 constant -= coeff * bias;
14514 add_loc_descr (list_head: &ret, descr: new_reg_loc_descr (reg: regno, offset: 0));
14515 if (coeff % factor == 0)
14516 coeff /= factor;
14517 else
14518 {
14519 int amount = exact_log2 (x: factor);
14520 gcc_assert (amount >= 0);
14521 add_loc_descr (list_head: &ret, descr: int_loc_descriptor (poly_i: amount));
14522 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
14523 }
14524 if (coeff != 1)
14525 {
14526 add_loc_descr (list_head: &ret, descr: int_loc_descriptor (poly_i: coeff));
14527 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_mul, oprnd1: 0, oprnd2: 0));
14528 }
14529 if (start)
14530 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
14531 }
14532 }
14533 loc_descr_plus_const (list_head: &ret, poly_offset: constant);
14534 return ret;
14535 }
14536
14537 /* Pick the smallest representation of a constant, rather than just
14538 defaulting to the LEB encoding. */
14539 if (i >= 0)
14540 {
14541 int clz = clz_hwi (x: i);
14542 int ctz = ctz_hwi (x: i);
14543 if (i <= 31)
14544 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14545 else if (i <= 0xff)
14546 op = DW_OP_const1u;
14547 else if (i <= 0xffff)
14548 op = DW_OP_const2u;
14549 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14550 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14551 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14552 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14553 while DW_OP_const4u is 5 bytes. */
14554 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14555 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14556 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14557 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14558 while DW_OP_const4u is 5 bytes. */
14559 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14560
14561 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14562 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14563 <= 4)
14564 {
14565 /* As i >= 2**31, the double cast above will yield a negative number.
14566 Since wrapping is defined in DWARF expressions we can output big
14567 positive integers as small negative ones, regardless of the size
14568 of host wide ints.
14569
14570 Here, since the evaluator will handle 32-bit values and since i >=
14571 2**31, we know it's going to be interpreted as a negative literal:
14572 store it this way if we can do better than 5 bytes this way. */
14573 return int_loc_descriptor (poly_i: (HOST_WIDE_INT) (int32_t) i);
14574 }
14575 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14576 op = DW_OP_const4u;
14577
14578 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14579 least 6 bytes: see if we can do better before falling back to it. */
14580 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14581 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14582 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14583 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14584 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14585 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14586 >= HOST_BITS_PER_WIDE_INT)
14587 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14588 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14589 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14590 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14591 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14592 && size_of_uleb128 (i) > 6)
14593 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14594 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14595 else
14596 op = DW_OP_constu;
14597 }
14598 else
14599 {
14600 if (i >= -0x80)
14601 op = DW_OP_const1s;
14602 else if (i >= -0x8000)
14603 op = DW_OP_const2s;
14604 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14605 {
14606 if (size_of_int_loc_descriptor (i) < 5)
14607 {
14608 dw_loc_descr_ref ret = int_loc_descriptor (poly_i: -i);
14609 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_neg, oprnd1: 0, oprnd2: 0));
14610 return ret;
14611 }
14612 op = DW_OP_const4s;
14613 }
14614 else
14615 {
14616 if (size_of_int_loc_descriptor (i)
14617 < (unsigned long) 1 + size_of_sleb128 (i))
14618 {
14619 dw_loc_descr_ref ret = int_loc_descriptor (poly_i: -i);
14620 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_neg, oprnd1: 0, oprnd2: 0));
14621 return ret;
14622 }
14623 op = DW_OP_consts;
14624 }
14625 }
14626
14627 return new_loc_descr (op, oprnd1: i, oprnd2: 0);
14628}
14629
14630/* Likewise, for unsigned constants. */
14631
14632static dw_loc_descr_ref
14633uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14634{
14635 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14636 const unsigned HOST_WIDE_INT max_uint
14637 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14638
14639 /* If possible, use the clever signed constants handling. */
14640 if (i <= max_int)
14641 return int_loc_descriptor (poly_i: (HOST_WIDE_INT) i);
14642
14643 /* Here, we are left with positive numbers that cannot be represented as
14644 HOST_WIDE_INT, i.e.:
14645 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14646
14647 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14648 whereas may be better to output a negative integer: thanks to integer
14649 wrapping, we know that:
14650 x = x - 2 ** DWARF2_ADDR_SIZE
14651 = x - 2 * (max (HOST_WIDE_INT) + 1)
14652 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14653 small negative integers. Let's try that in cases it will clearly improve
14654 the encoding: there is no gain turning DW_OP_const4u into
14655 DW_OP_const4s. */
14656 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14657 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14658 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14659 {
14660 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14661
14662 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14663 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14664 const HOST_WIDE_INT second_shift
14665 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14666
14667 /* So we finally have:
14668 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14669 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14670 return int_loc_descriptor (poly_i: second_shift);
14671 }
14672
14673 /* Last chance: fallback to a simple constant operation. */
14674 return new_loc_descr
14675 (op: (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14676 ? DW_OP_const4u
14677 : DW_OP_const8u,
14678 oprnd1: i, oprnd2: 0);
14679}
14680
14681/* Generate and return a location description that computes the unsigned
14682 comparison of the two stack top entries (a OP b where b is the top-most
14683 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14684 LE_EXPR, GT_EXPR or GE_EXPR. */
14685
14686static dw_loc_descr_ref
14687uint_comparison_loc_list (enum tree_code kind)
14688{
14689 enum dwarf_location_atom op, flip_op;
14690 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14691
14692 switch (kind)
14693 {
14694 case LT_EXPR:
14695 op = DW_OP_lt;
14696 break;
14697 case LE_EXPR:
14698 op = DW_OP_le;
14699 break;
14700 case GT_EXPR:
14701 op = DW_OP_gt;
14702 break;
14703 case GE_EXPR:
14704 op = DW_OP_ge;
14705 break;
14706 default:
14707 gcc_unreachable ();
14708 }
14709
14710 bra_node = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
14711 jmp_node = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
14712
14713 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14714 possible to perform unsigned comparisons: we just have to distinguish
14715 three cases:
14716
14717 1. when a and b have the same sign (as signed integers); then we should
14718 return: a OP(signed) b;
14719
14720 2. when a is a negative signed integer while b is a positive one, then a
14721 is a greater unsigned integer than b; likewise when a and b's roles
14722 are flipped.
14723
14724 So first, compare the sign of the two operands. */
14725 ret = new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0);
14726 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
14727 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_xor, oprnd1: 0, oprnd2: 0));
14728 /* If they have different signs (i.e. they have different sign bits), then
14729 the stack top value has now the sign bit set and thus it's smaller than
14730 zero. */
14731 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_lit0, oprnd1: 0, oprnd2: 0));
14732 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_lt, oprnd1: 0, oprnd2: 0));
14733 add_loc_descr (list_head: &ret, descr: bra_node);
14734
14735 /* We are in case 1. At this point, we know both operands have the same
14736 sign, to it's safe to use the built-in signed comparison. */
14737 add_loc_descr (list_head: &ret, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
14738 add_loc_descr (list_head: &ret, descr: jmp_node);
14739
14740 /* We are in case 2. Here, we know both operands do not have the same sign,
14741 so we have to flip the signed comparison. */
14742 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14743 tmp = new_loc_descr (op: flip_op, oprnd1: 0, oprnd2: 0);
14744 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14745 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14746 add_loc_descr (list_head: &ret, descr: tmp);
14747
14748 /* This dummy operation is necessary to make the two branches join. */
14749 tmp = new_loc_descr (op: DW_OP_nop, oprnd1: 0, oprnd2: 0);
14750 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14751 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14752 add_loc_descr (list_head: &ret, descr: tmp);
14753
14754 return ret;
14755}
14756
14757/* Likewise, but takes the location description lists (might be destructive on
14758 them). Return NULL if either is NULL or if concatenation fails. */
14759
14760static dw_loc_list_ref
14761loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14762 enum tree_code kind)
14763{
14764 if (left == NULL || right == NULL)
14765 return NULL;
14766
14767 add_loc_list (ret: &left, list: right);
14768 if (left == NULL)
14769 return NULL;
14770
14771 add_loc_descr_to_each (list: left, ref: uint_comparison_loc_list (kind));
14772 return left;
14773}
14774
14775/* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14776 without actually allocating it. */
14777
14778static unsigned long
14779size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14780{
14781 return size_of_int_loc_descriptor (i >> shift)
14782 + size_of_int_loc_descriptor (shift)
14783 + 1;
14784}
14785
14786/* Return size_of_locs (int_loc_descriptor (i)) without
14787 actually allocating it. */
14788
14789static unsigned long
14790size_of_int_loc_descriptor (HOST_WIDE_INT i)
14791{
14792 unsigned long s;
14793
14794 if (i >= 0)
14795 {
14796 int clz, ctz;
14797 if (i <= 31)
14798 return 1;
14799 else if (i <= 0xff)
14800 return 2;
14801 else if (i <= 0xffff)
14802 return 3;
14803 clz = clz_hwi (x: i);
14804 ctz = ctz_hwi (x: i);
14805 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14806 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14807 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14808 - clz - 5);
14809 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14810 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14811 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14812 - clz - 8);
14813 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14814 && size_of_int_loc_descriptor (i: (HOST_WIDE_INT) (int32_t) i)
14815 <= 4)
14816 return size_of_int_loc_descriptor (i: (HOST_WIDE_INT) (int32_t) i);
14817 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14818 return 5;
14819 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14820 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14821 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14822 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14823 - clz - 8);
14824 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14825 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14826 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14827 - clz - 16);
14828 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14829 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14830 && s > 6)
14831 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14832 - clz - 32);
14833 else
14834 return 1 + s;
14835 }
14836 else
14837 {
14838 if (i >= -0x80)
14839 return 2;
14840 else if (i >= -0x8000)
14841 return 3;
14842 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14843 {
14844 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14845 {
14846 s = size_of_int_loc_descriptor (i: -i) + 1;
14847 if (s < 5)
14848 return s;
14849 }
14850 return 5;
14851 }
14852 else
14853 {
14854 unsigned long r = 1 + size_of_sleb128 (i);
14855 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14856 {
14857 s = size_of_int_loc_descriptor (i: -i) + 1;
14858 if (s < r)
14859 return s;
14860 }
14861 return r;
14862 }
14863 }
14864}
14865
14866/* Return loc description representing "address" of integer value.
14867 This can appear only as toplevel expression. */
14868
14869static dw_loc_descr_ref
14870address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14871{
14872 int litsize;
14873 dw_loc_descr_ref loc_result = NULL;
14874
14875 if (!(dwarf_version >= 4 || !dwarf_strict))
14876 return NULL;
14877
14878 litsize = size_of_int_loc_descriptor (i);
14879 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14880 is more compact. For DW_OP_stack_value we need:
14881 litsize + 1 (DW_OP_stack_value)
14882 and for DW_OP_implicit_value:
14883 1 (DW_OP_implicit_value) + 1 (length) + size. */
14884 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14885 {
14886 loc_result = int_loc_descriptor (poly_i: i);
14887 add_loc_descr (list_head: &loc_result,
14888 descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
14889 return loc_result;
14890 }
14891
14892 loc_result = new_loc_descr (op: DW_OP_implicit_value,
14893 oprnd1: size, oprnd2: 0);
14894 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14895 loc_result->dw_loc_oprnd2.v.val_int = i;
14896 return loc_result;
14897}
14898
14899/* Return a location descriptor that designates a base+offset location. */
14900
14901static dw_loc_descr_ref
14902based_loc_descr (rtx reg, poly_int64 offset,
14903 enum var_init_status initialized)
14904{
14905 unsigned int regno;
14906 dw_loc_descr_ref result;
14907 dw_fde_ref fde = cfun->fde;
14908
14909 /* We only use "frame base" when we're sure we're talking about the
14910 post-prologue local stack frame. We do this by *not* running
14911 register elimination until this point, and recognizing the special
14912 argument pointer and soft frame pointer rtx's. */
14913 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14914 {
14915 rtx elim = (ira_use_lra_p
14916 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14917 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14918
14919 if (elim != reg)
14920 {
14921 /* Allow hard frame pointer here even if frame pointer
14922 isn't used since hard frame pointer is encoded with
14923 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14924 not hard frame pointer directly. */
14925 elim = strip_offset_and_add (x: elim, offset: &offset);
14926 gcc_assert (elim == hard_frame_pointer_rtx
14927 || elim == stack_pointer_rtx);
14928
14929 /* If drap register is used to align stack, use frame
14930 pointer + offset to access stack variables. If stack
14931 is aligned without drap, use stack pointer + offset to
14932 access stack variables. */
14933 if (crtl->stack_realign_tried
14934 && reg == frame_pointer_rtx)
14935 {
14936 int base_reg
14937 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14938 ? HARD_FRAME_POINTER_REGNUM
14939 : REGNO (elim));
14940 return new_reg_loc_descr (reg: base_reg, offset);
14941 }
14942
14943 gcc_assert (frame_pointer_fb_offset_valid);
14944 offset += frame_pointer_fb_offset;
14945 HOST_WIDE_INT const_offset;
14946 if (offset.is_constant (const_value: &const_offset))
14947 return new_loc_descr (op: DW_OP_fbreg, oprnd1: const_offset, oprnd2: 0);
14948 else
14949 {
14950 dw_loc_descr_ref ret = new_loc_descr (op: DW_OP_fbreg, oprnd1: 0, oprnd2: 0);
14951 loc_descr_plus_const (list_head: &ret, poly_offset: offset);
14952 return ret;
14953 }
14954 }
14955 }
14956
14957 regno = REGNO (reg);
14958#ifdef LEAF_REG_REMAP
14959 if (crtl->uses_only_leaf_regs)
14960 {
14961 int leaf_reg = LEAF_REG_REMAP (regno);
14962 if (leaf_reg != -1)
14963 regno = (unsigned) leaf_reg;
14964 }
14965#endif
14966 regno = DWARF_FRAME_REGNUM (regno);
14967
14968 HOST_WIDE_INT const_offset;
14969 if (!optimize && fde
14970 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14971 && offset.is_constant (const_value: &const_offset))
14972 {
14973 /* Use cfa+offset to represent the location of arguments passed
14974 on the stack when drap is used to align stack.
14975 Only do this when not optimizing, for optimized code var-tracking
14976 is supposed to track where the arguments live and the register
14977 used as vdrap or drap in some spot might be used for something
14978 else in other part of the routine. */
14979 return new_loc_descr (op: DW_OP_fbreg, oprnd1: const_offset, oprnd2: 0);
14980 }
14981
14982 result = new_reg_loc_descr (reg: regno, offset);
14983
14984 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14985 add_loc_descr (list_head: &result, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
14986
14987 return result;
14988}
14989
14990/* Return true if this RTL expression describes a base+offset calculation. */
14991
14992static inline bool
14993is_based_loc (const_rtx rtl)
14994{
14995 return (GET_CODE (rtl) == PLUS
14996 && ((REG_P (XEXP (rtl, 0))
14997 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14998 && CONST_INT_P (XEXP (rtl, 1)))));
14999}
15000
15001/* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
15002 failed. */
15003
15004static dw_loc_descr_ref
15005tls_mem_loc_descriptor (rtx mem)
15006{
15007 tree base;
15008 dw_loc_descr_ref loc_result;
15009
15010 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
15011 return NULL;
15012
15013 base = get_base_address (MEM_EXPR (mem));
15014 if (base == NULL
15015 || !VAR_P (base)
15016 || !DECL_THREAD_LOCAL_P (base))
15017 return NULL;
15018
15019 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15020 if (loc_result == NULL)
15021 return NULL;
15022
15023 if (maybe_ne (MEM_OFFSET (mem), b: 0))
15024 loc_descr_plus_const (list_head: &loc_result, MEM_OFFSET (mem));
15025
15026 return loc_result;
15027}
15028
15029/* Output debug info about reason why we failed to expand expression as dwarf
15030 expression. */
15031
15032static void
15033expansion_failed (tree expr, rtx rtl, char const *reason)
15034{
15035 if (dump_file && (dump_flags & TDF_DETAILS))
15036 {
15037 fprintf (stream: dump_file, format: "Failed to expand as dwarf: ");
15038 if (expr)
15039 print_generic_expr (dump_file, expr, dump_flags);
15040 if (rtl)
15041 {
15042 fprintf (stream: dump_file, format: "\n");
15043 print_rtl (dump_file, rtl);
15044 }
15045 fprintf (stream: dump_file, format: "\nReason: %s\n", reason);
15046 }
15047}
15048
15049/* Helper function for const_ok_for_output. */
15050
15051static bool
15052const_ok_for_output_1 (rtx rtl)
15053{
15054 if (targetm.const_not_ok_for_debug_p (rtl))
15055 {
15056 if (GET_CODE (rtl) != UNSPEC)
15057 {
15058 expansion_failed (NULL_TREE, rtl,
15059 reason: "Expression rejected for debug by the backend.\n");
15060 return false;
15061 }
15062
15063 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15064 the target hook doesn't explicitly allow it in debug info, assume
15065 we can't express it in the debug info. */
15066 /* Don't complain about TLS UNSPECs, those are just too hard to
15067 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15068 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15069 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15070 if (flag_checking
15071 && (XVECLEN (rtl, 0) == 0
15072 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15073 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15074 inform (current_function_decl
15075 ? DECL_SOURCE_LOCATION (current_function_decl)
15076 : UNKNOWN_LOCATION,
15077#if NUM_UNSPEC_VALUES > 0
15078 "non-delegitimized UNSPEC %s (%d) found in variable location",
15079 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15080 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15081#else
15082 "non-delegitimized UNSPEC %d found in variable location",
15083#endif
15084 XINT (rtl, 1));
15085 expansion_failed (NULL_TREE, rtl,
15086 reason: "UNSPEC hasn't been delegitimized.\n");
15087 return false;
15088 }
15089
15090 if (CONST_POLY_INT_P (rtl))
15091 return false;
15092
15093 /* FIXME: Refer to PR60655. It is possible for simplification
15094 of rtl expressions in var tracking to produce such expressions.
15095 We should really identify / validate expressions
15096 enclosed in CONST that can be handled by assemblers on various
15097 targets and only handle legitimate cases here. */
15098 switch (GET_CODE (rtl))
15099 {
15100 case SYMBOL_REF:
15101 break;
15102 case NOT:
15103 case NEG:
15104 return false;
15105 case PLUS:
15106 {
15107 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15108 operands. */
15109 subrtx_var_iterator::array_type array;
15110 bool first = false;
15111 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15112 if (SYMBOL_REF_P (*iter)
15113 || LABEL_P (*iter)
15114 || GET_CODE (*iter) == UNSPEC)
15115 {
15116 first = true;
15117 break;
15118 }
15119 if (!first)
15120 return true;
15121 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15122 if (SYMBOL_REF_P (*iter)
15123 || LABEL_P (*iter)
15124 || GET_CODE (*iter) == UNSPEC)
15125 return false;
15126 return true;
15127 }
15128 case MINUS:
15129 {
15130 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15131 appear in the second operand of MINUS. */
15132 subrtx_var_iterator::array_type array;
15133 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15134 if (SYMBOL_REF_P (*iter)
15135 || LABEL_P (*iter)
15136 || GET_CODE (*iter) == UNSPEC)
15137 return false;
15138 return true;
15139 }
15140 default:
15141 return true;
15142 }
15143
15144 if (CONSTANT_POOL_ADDRESS_P (rtl))
15145 {
15146 bool marked;
15147 get_pool_constant_mark (rtl, &marked);
15148 /* If all references to this pool constant were optimized away,
15149 it was not output and thus we can't represent it. */
15150 if (!marked)
15151 {
15152 expansion_failed (NULL_TREE, rtl,
15153 reason: "Constant was removed from constant pool.\n");
15154 return false;
15155 }
15156 }
15157
15158 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15159 return false;
15160
15161 /* Avoid references to external symbols in debug info, on several targets
15162 the linker might even refuse to link when linking a shared library,
15163 and in many other cases the relocations for .debug_info/.debug_loc are
15164 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15165 to be defined within the same shared library or executable are fine. */
15166 if (SYMBOL_REF_EXTERNAL_P (rtl))
15167 {
15168 tree decl = SYMBOL_REF_DECL (rtl);
15169
15170 if (decl == NULL || !targetm.binds_local_p (decl))
15171 {
15172 expansion_failed (NULL_TREE, rtl,
15173 reason: "Symbol not defined in current TU.\n");
15174 return false;
15175 }
15176 }
15177
15178 return true;
15179}
15180
15181/* Return true if constant RTL can be emitted in DW_OP_addr or
15182 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15183 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15184
15185static bool
15186const_ok_for_output (rtx rtl)
15187{
15188 if (GET_CODE (rtl) == SYMBOL_REF)
15189 return const_ok_for_output_1 (rtl);
15190
15191 if (GET_CODE (rtl) == CONST)
15192 {
15193 subrtx_var_iterator::array_type array;
15194 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15195 if (!const_ok_for_output_1 (rtl: *iter))
15196 return false;
15197 return true;
15198 }
15199
15200 return true;
15201}
15202
15203/* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15204 if possible, NULL otherwise. */
15205
15206static dw_die_ref
15207base_type_for_mode (machine_mode mode, bool unsignedp)
15208{
15209 dw_die_ref type_die;
15210 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15211
15212 if (type == NULL)
15213 return NULL;
15214 switch (TREE_CODE (type))
15215 {
15216 case INTEGER_TYPE:
15217 case REAL_TYPE:
15218 break;
15219 default:
15220 return NULL;
15221 }
15222 type_die = lookup_type_die (type);
15223 if (!type_die)
15224 type_die = modified_type_die (type, cv_quals: TYPE_UNQUALIFIED, reverse: false,
15225 context_die: comp_unit_die ());
15226 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15227 return NULL;
15228 return type_die;
15229}
15230
15231/* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15232 type matching MODE, or, if MODE is narrower than or as wide as
15233 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15234 possible. */
15235
15236static dw_loc_descr_ref
15237convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15238{
15239 machine_mode outer_mode = mode;
15240 dw_die_ref type_die;
15241 dw_loc_descr_ref cvt;
15242
15243 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15244 {
15245 add_loc_descr (list_head: &op, descr: new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0));
15246 return op;
15247 }
15248 type_die = base_type_for_mode (mode: outer_mode, unsignedp: 1);
15249 if (type_die == NULL)
15250 return NULL;
15251 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15252 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15253 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15254 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15255 add_loc_descr (list_head: &op, descr: cvt);
15256 return op;
15257}
15258
15259/* Return location descriptor for comparison OP with operands OP0 and OP1. */
15260
15261static dw_loc_descr_ref
15262compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15263 dw_loc_descr_ref op1)
15264{
15265 dw_loc_descr_ref ret = op0;
15266 add_loc_descr (list_head: &ret, descr: op1);
15267 add_loc_descr (list_head: &ret, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
15268 if (STORE_FLAG_VALUE != 1)
15269 {
15270 add_loc_descr (list_head: &ret, descr: int_loc_descriptor (STORE_FLAG_VALUE));
15271 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_mul, oprnd1: 0, oprnd2: 0));
15272 }
15273 return ret;
15274}
15275
15276/* Subroutine of scompare_loc_descriptor for the case in which we're
15277 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15278 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15279
15280static dw_loc_descr_ref
15281scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15282 scalar_int_mode op_mode,
15283 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15284{
15285 dw_die_ref type_die = base_type_for_mode (mode: op_mode, unsignedp: 0);
15286 dw_loc_descr_ref cvt;
15287
15288 if (type_die == NULL)
15289 return NULL;
15290 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15291 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15292 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15293 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15294 add_loc_descr (list_head: &op0, descr: cvt);
15295 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15296 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15297 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15298 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15299 add_loc_descr (list_head: &op1, descr: cvt);
15300 return compare_loc_descriptor (op, op0, op1);
15301}
15302
15303/* Subroutine of scompare_loc_descriptor for the case in which we're
15304 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15305 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15306
15307static dw_loc_descr_ref
15308scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15309 scalar_int_mode op_mode,
15310 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15311{
15312 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode: op_mode)) * BITS_PER_UNIT;
15313 /* For eq/ne, if the operands are known to be zero-extended,
15314 there is no need to do the fancy shifting up. */
15315 if (op == DW_OP_eq || op == DW_OP_ne)
15316 {
15317 dw_loc_descr_ref last0, last1;
15318 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15319 ;
15320 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15321 ;
15322 /* deref_size zero extends, and for constants we can check
15323 whether they are zero extended or not. */
15324 if (((last0->dw_loc_opc == DW_OP_deref_size
15325 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (mode: op_mode))
15326 || (CONST_INT_P (XEXP (rtl, 0))
15327 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15328 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15329 && ((last1->dw_loc_opc == DW_OP_deref_size
15330 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (mode: op_mode))
15331 || (CONST_INT_P (XEXP (rtl, 1))
15332 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15333 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15334 return compare_loc_descriptor (op, op0, op1);
15335
15336 /* EQ/NE comparison against constant in narrower type than
15337 DWARF2_ADDR_SIZE can be performed either as
15338 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15339 DW_OP_{eq,ne}
15340 or
15341 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15342 DW_OP_{eq,ne}. Pick whatever is shorter. */
15343 if (CONST_INT_P (XEXP (rtl, 1))
15344 && GET_MODE_BITSIZE (mode: op_mode) < HOST_BITS_PER_WIDE_INT
15345 && (size_of_int_loc_descriptor (i: shift) + 1
15346 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15347 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15348 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15349 & GET_MODE_MASK (op_mode))))
15350 {
15351 add_loc_descr (list_head: &op0, descr: int_loc_descriptor (GET_MODE_MASK (op_mode)));
15352 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15353 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15354 & GET_MODE_MASK (op_mode));
15355 return compare_loc_descriptor (op, op0, op1);
15356 }
15357 }
15358 add_loc_descr (list_head: &op0, descr: int_loc_descriptor (poly_i: shift));
15359 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15360 if (CONST_INT_P (XEXP (rtl, 1)))
15361 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15362 else
15363 {
15364 add_loc_descr (list_head: &op1, descr: int_loc_descriptor (poly_i: shift));
15365 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15366 }
15367 return compare_loc_descriptor (op, op0, op1);
15368}
15369
15370/* Return location descriptor for signed comparison OP RTL. */
15371
15372static dw_loc_descr_ref
15373scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15374 machine_mode mem_mode)
15375{
15376 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15377 dw_loc_descr_ref op0, op1;
15378
15379 if (op_mode == VOIDmode)
15380 op_mode = GET_MODE (XEXP (rtl, 1));
15381 if (op_mode == VOIDmode)
15382 return NULL;
15383
15384 scalar_int_mode int_op_mode;
15385 if (dwarf_strict
15386 && dwarf_version < 5
15387 && (!is_a <scalar_int_mode> (m: op_mode, result: &int_op_mode)
15388 || GET_MODE_SIZE (mode: int_op_mode) > DWARF2_ADDR_SIZE))
15389 return NULL;
15390
15391 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode: op_mode, mem_mode,
15392 VAR_INIT_STATUS_INITIALIZED);
15393 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode: op_mode, mem_mode,
15394 VAR_INIT_STATUS_INITIALIZED);
15395
15396 if (op0 == NULL || op1 == NULL)
15397 return NULL;
15398
15399 if (is_a <scalar_int_mode> (m: op_mode, result: &int_op_mode))
15400 {
15401 if (GET_MODE_SIZE (mode: int_op_mode) < DWARF2_ADDR_SIZE)
15402 return scompare_loc_descriptor_narrow (op, rtl, op_mode: int_op_mode, op0, op1);
15403
15404 if (GET_MODE_SIZE (mode: int_op_mode) > DWARF2_ADDR_SIZE)
15405 return scompare_loc_descriptor_wide (op, op_mode: int_op_mode, op0, op1);
15406 }
15407 return compare_loc_descriptor (op, op0, op1);
15408}
15409
15410/* Return location descriptor for unsigned comparison OP RTL. */
15411
15412static dw_loc_descr_ref
15413ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15414 machine_mode mem_mode)
15415{
15416 dw_loc_descr_ref op0, op1;
15417
15418 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15419 if (test_op_mode == VOIDmode)
15420 test_op_mode = GET_MODE (XEXP (rtl, 1));
15421
15422 scalar_int_mode op_mode;
15423 if (!is_a <scalar_int_mode> (m: test_op_mode, result: &op_mode))
15424 return NULL;
15425
15426 if (dwarf_strict
15427 && dwarf_version < 5
15428 && GET_MODE_SIZE (mode: op_mode) > DWARF2_ADDR_SIZE)
15429 return NULL;
15430
15431 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode: op_mode, mem_mode,
15432 VAR_INIT_STATUS_INITIALIZED);
15433 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode: op_mode, mem_mode,
15434 VAR_INIT_STATUS_INITIALIZED);
15435
15436 if (op0 == NULL || op1 == NULL)
15437 return NULL;
15438
15439 if (GET_MODE_SIZE (mode: op_mode) < DWARF2_ADDR_SIZE)
15440 {
15441 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15442 dw_loc_descr_ref last0, last1;
15443 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15444 ;
15445 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15446 ;
15447 if (CONST_INT_P (XEXP (rtl, 0)))
15448 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15449 /* deref_size zero extends, so no need to mask it again. */
15450 else if (last0->dw_loc_opc != DW_OP_deref_size
15451 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (mode: op_mode))
15452 {
15453 add_loc_descr (list_head: &op0, descr: int_loc_descriptor (poly_i: mask));
15454 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15455 }
15456 if (CONST_INT_P (XEXP (rtl, 1)))
15457 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15458 /* deref_size zero extends, so no need to mask it again. */
15459 else if (last1->dw_loc_opc != DW_OP_deref_size
15460 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (mode: op_mode))
15461 {
15462 add_loc_descr (list_head: &op1, descr: int_loc_descriptor (poly_i: mask));
15463 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15464 }
15465 }
15466 else if (GET_MODE_SIZE (mode: op_mode) == DWARF2_ADDR_SIZE)
15467 {
15468 HOST_WIDE_INT bias = 1;
15469 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15470 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_plus_uconst, oprnd1: bias, oprnd2: 0));
15471 if (CONST_INT_P (XEXP (rtl, 1)))
15472 op1 = int_loc_descriptor (poly_i: (unsigned HOST_WIDE_INT) bias
15473 + INTVAL (XEXP (rtl, 1)));
15474 else
15475 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_plus_uconst,
15476 oprnd1: bias, oprnd2: 0));
15477 }
15478 return compare_loc_descriptor (op, op0, op1);
15479}
15480
15481/* Return location descriptor for {U,S}{MIN,MAX}. */
15482
15483static dw_loc_descr_ref
15484minmax_loc_descriptor (rtx rtl, machine_mode mode,
15485 machine_mode mem_mode)
15486{
15487 enum dwarf_location_atom op;
15488 dw_loc_descr_ref op0, op1, ret;
15489 dw_loc_descr_ref bra_node, drop_node;
15490
15491 scalar_int_mode int_mode;
15492 if (dwarf_strict
15493 && dwarf_version < 5
15494 && (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
15495 || GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE))
15496 return NULL;
15497
15498 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15499 VAR_INIT_STATUS_INITIALIZED);
15500 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15501 VAR_INIT_STATUS_INITIALIZED);
15502
15503 if (op0 == NULL || op1 == NULL)
15504 return NULL;
15505
15506 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0));
15507 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15508 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
15509 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15510 {
15511 /* Checked by the caller. */
15512 int_mode = as_a <scalar_int_mode> (m: mode);
15513 if (GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
15514 {
15515 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15516 add_loc_descr (list_head: &op0, descr: int_loc_descriptor (poly_i: mask));
15517 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15518 add_loc_descr (list_head: &op1, descr: int_loc_descriptor (poly_i: mask));
15519 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15520 }
15521 else if (GET_MODE_SIZE (mode: int_mode) == DWARF2_ADDR_SIZE)
15522 {
15523 HOST_WIDE_INT bias = 1;
15524 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15525 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_plus_uconst, oprnd1: bias, oprnd2: 0));
15526 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_plus_uconst, oprnd1: bias, oprnd2: 0));
15527 }
15528 }
15529 else if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
15530 && GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
15531 {
15532 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode: int_mode)) * BITS_PER_UNIT;
15533 add_loc_descr (list_head: &op0, descr: int_loc_descriptor (poly_i: shift));
15534 add_loc_descr (list_head: &op0, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15535 add_loc_descr (list_head: &op1, descr: int_loc_descriptor (poly_i: shift));
15536 add_loc_descr (list_head: &op1, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15537 }
15538 else if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
15539 && GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
15540 {
15541 dw_die_ref type_die = base_type_for_mode (mode: int_mode, unsignedp: 0);
15542 dw_loc_descr_ref cvt;
15543 if (type_die == NULL)
15544 return NULL;
15545 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15546 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15547 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15548 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15549 add_loc_descr (list_head: &op0, descr: cvt);
15550 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15551 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15552 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15553 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15554 add_loc_descr (list_head: &op1, descr: cvt);
15555 }
15556
15557 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15558 op = DW_OP_lt;
15559 else
15560 op = DW_OP_gt;
15561 ret = op0;
15562 add_loc_descr (list_head: &ret, descr: op1);
15563 add_loc_descr (list_head: &ret, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
15564 bra_node = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
15565 add_loc_descr (list_head: &ret, descr: bra_node);
15566 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15567 drop_node = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
15568 add_loc_descr (list_head: &ret, descr: drop_node);
15569 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15570 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15571 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15572 && is_a <scalar_int_mode> (m: mode, result: &int_mode)
15573 && GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
15574 ret = convert_descriptor_to_mode (mode: int_mode, op: ret);
15575 return ret;
15576}
15577
15578/* Helper function for mem_loc_descriptor. Perform OP binary op,
15579 but after converting arguments to type_die, afterwards
15580 convert back to unsigned. */
15581
15582static dw_loc_descr_ref
15583typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15584 scalar_int_mode mode, machine_mode mem_mode)
15585{
15586 dw_loc_descr_ref cvt, op0, op1;
15587
15588 if (type_die == NULL)
15589 return NULL;
15590 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15591 VAR_INIT_STATUS_INITIALIZED);
15592 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15593 VAR_INIT_STATUS_INITIALIZED);
15594 if (op0 == NULL || op1 == NULL)
15595 return NULL;
15596 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15597 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15598 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15599 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15600 add_loc_descr (list_head: &op0, descr: cvt);
15601 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
15602 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15603 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15604 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15605 add_loc_descr (list_head: &op1, descr: cvt);
15606 add_loc_descr (list_head: &op0, descr: op1);
15607 add_loc_descr (list_head: &op0, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
15608 return convert_descriptor_to_mode (mode, op: op0);
15609}
15610
15611/* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15612 const0 is DW_OP_lit0 or corresponding typed constant,
15613 const1 is DW_OP_lit1 or corresponding typed constant
15614 and constMSB is constant with just the MSB bit set
15615 for the mode):
15616 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15617 L1: const0 DW_OP_swap
15618 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15619 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15620 L3: DW_OP_drop
15621 L4: DW_OP_nop
15622
15623 CTZ is similar:
15624 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15625 L1: const0 DW_OP_swap
15626 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15627 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15628 L3: DW_OP_drop
15629 L4: DW_OP_nop
15630
15631 FFS is similar:
15632 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15633 L1: const1 DW_OP_swap
15634 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15635 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15636 L3: DW_OP_drop
15637 L4: DW_OP_nop */
15638
15639static dw_loc_descr_ref
15640clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15641 machine_mode mem_mode)
15642{
15643 dw_loc_descr_ref op0, ret, tmp;
15644 HOST_WIDE_INT valv;
15645 dw_loc_descr_ref l1jump, l1label;
15646 dw_loc_descr_ref l2jump, l2label;
15647 dw_loc_descr_ref l3jump, l3label;
15648 dw_loc_descr_ref l4jump, l4label;
15649 rtx msb;
15650
15651 if (GET_MODE (XEXP (rtl, 0)) != mode)
15652 return NULL;
15653
15654 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15655 VAR_INIT_STATUS_INITIALIZED);
15656 if (op0 == NULL)
15657 return NULL;
15658 ret = op0;
15659 if (GET_CODE (rtl) == CLZ)
15660 {
15661 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15662 valv = GET_MODE_BITSIZE (mode);
15663 }
15664 else if (GET_CODE (rtl) == FFS)
15665 valv = 0;
15666 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15667 valv = GET_MODE_BITSIZE (mode);
15668 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0));
15669 l1jump = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
15670 add_loc_descr (list_head: &ret, descr: l1jump);
15671 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0));
15672 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15673 VAR_INIT_STATUS_INITIALIZED);
15674 if (tmp == NULL)
15675 return NULL;
15676 add_loc_descr (list_head: &ret, descr: tmp);
15677 l4jump = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
15678 add_loc_descr (list_head: &ret, descr: l4jump);
15679 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15680 ? const1_rtx : const0_rtx,
15681 mode, mem_mode,
15682 VAR_INIT_STATUS_INITIALIZED);
15683 if (l1label == NULL)
15684 return NULL;
15685 add_loc_descr (list_head: &ret, descr: l1label);
15686 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15687 l2label = new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0);
15688 add_loc_descr (list_head: &ret, descr: l2label);
15689 if (GET_CODE (rtl) != CLZ)
15690 msb = const1_rtx;
15691 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15692 msb = GEN_INT (HOST_WIDE_INT_1U
15693 << (GET_MODE_BITSIZE (mode) - 1));
15694 else
15695 msb = immed_wide_int_const
15696 (wi::set_bit_in_zero (bit: GET_MODE_PRECISION (mode) - 1,
15697 precision: GET_MODE_PRECISION (mode)), mode);
15698 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15699 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15700 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15701 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), oprnd2: 0);
15702 else
15703 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15704 VAR_INIT_STATUS_INITIALIZED);
15705 if (tmp == NULL)
15706 return NULL;
15707 add_loc_descr (list_head: &ret, descr: tmp);
15708 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15709 l3jump = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
15710 add_loc_descr (list_head: &ret, descr: l3jump);
15711 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15712 VAR_INIT_STATUS_INITIALIZED);
15713 if (tmp == NULL)
15714 return NULL;
15715 add_loc_descr (list_head: &ret, descr: tmp);
15716 add_loc_descr (list_head: &ret, descr: new_loc_descr (GET_CODE (rtl) == CLZ
15717 ? DW_OP_shl : DW_OP_shr, oprnd1: 0, oprnd2: 0));
15718 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15719 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_plus_uconst, oprnd1: 1, oprnd2: 0));
15720 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15721 l2jump = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
15722 add_loc_descr (list_head: &ret, descr: l2jump);
15723 l3label = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
15724 add_loc_descr (list_head: &ret, descr: l3label);
15725 l4label = new_loc_descr (op: DW_OP_nop, oprnd1: 0, oprnd2: 0);
15726 add_loc_descr (list_head: &ret, descr: l4label);
15727 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15728 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15729 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15730 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15731 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15732 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15733 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15734 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15735 return ret;
15736}
15737
15738/* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15739 const1 is DW_OP_lit1 or corresponding typed constant):
15740 const0 DW_OP_swap
15741 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15742 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15743 L2: DW_OP_drop
15744
15745 PARITY is similar:
15746 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15747 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15748 L2: DW_OP_drop */
15749
15750static dw_loc_descr_ref
15751popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15752 machine_mode mem_mode)
15753{
15754 dw_loc_descr_ref op0, ret, tmp;
15755 dw_loc_descr_ref l1jump, l1label;
15756 dw_loc_descr_ref l2jump, l2label;
15757
15758 if (GET_MODE (XEXP (rtl, 0)) != mode)
15759 return NULL;
15760
15761 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15762 VAR_INIT_STATUS_INITIALIZED);
15763 if (op0 == NULL)
15764 return NULL;
15765 ret = op0;
15766 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15767 VAR_INIT_STATUS_INITIALIZED);
15768 if (tmp == NULL)
15769 return NULL;
15770 add_loc_descr (list_head: &ret, descr: tmp);
15771 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15772 l1label = new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0);
15773 add_loc_descr (list_head: &ret, descr: l1label);
15774 l2jump = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
15775 add_loc_descr (list_head: &ret, descr: l2jump);
15776 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0));
15777 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_rot, oprnd1: 0, oprnd2: 0));
15778 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15779 VAR_INIT_STATUS_INITIALIZED);
15780 if (tmp == NULL)
15781 return NULL;
15782 add_loc_descr (list_head: &ret, descr: tmp);
15783 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15784 add_loc_descr (list_head: &ret, descr: new_loc_descr (GET_CODE (rtl) == POPCOUNT
15785 ? DW_OP_plus : DW_OP_xor, oprnd1: 0, oprnd2: 0));
15786 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15787 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15788 VAR_INIT_STATUS_INITIALIZED);
15789 add_loc_descr (list_head: &ret, descr: tmp);
15790 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
15791 l1jump = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
15792 add_loc_descr (list_head: &ret, descr: l1jump);
15793 l2label = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
15794 add_loc_descr (list_head: &ret, descr: l2label);
15795 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15796 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15797 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15798 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15799 return ret;
15800}
15801
15802/* BSWAP (constS is initial shift count, either 56 or 24):
15803 constS const0
15804 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15805 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15806 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15807 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15808 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15809
15810static dw_loc_descr_ref
15811bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15812 machine_mode mem_mode)
15813{
15814 dw_loc_descr_ref op0, ret, tmp;
15815 dw_loc_descr_ref l1jump, l1label;
15816 dw_loc_descr_ref l2jump, l2label;
15817
15818 if (BITS_PER_UNIT != 8
15819 || (GET_MODE_BITSIZE (mode) != 32
15820 && GET_MODE_BITSIZE (mode) != 64))
15821 return NULL;
15822
15823 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15824 VAR_INIT_STATUS_INITIALIZED);
15825 if (op0 == NULL)
15826 return NULL;
15827
15828 ret = op0;
15829 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15830 mode, mem_mode,
15831 VAR_INIT_STATUS_INITIALIZED);
15832 if (tmp == NULL)
15833 return NULL;
15834 add_loc_descr (list_head: &ret, descr: tmp);
15835 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15836 VAR_INIT_STATUS_INITIALIZED);
15837 if (tmp == NULL)
15838 return NULL;
15839 add_loc_descr (list_head: &ret, descr: tmp);
15840 l1label = new_loc_descr (op: DW_OP_pick, oprnd1: 2, oprnd2: 0);
15841 add_loc_descr (list_head: &ret, descr: l1label);
15842 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15843 mode, mem_mode,
15844 VAR_INIT_STATUS_INITIALIZED);
15845 add_loc_descr (list_head: &ret, descr: tmp);
15846 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_pick, oprnd1: 3, oprnd2: 0));
15847 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
15848 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
15849 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15850 VAR_INIT_STATUS_INITIALIZED);
15851 if (tmp == NULL)
15852 return NULL;
15853 add_loc_descr (list_head: &ret, descr: tmp);
15854 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15855 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_pick, oprnd1: 2, oprnd2: 0));
15856 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15857 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_or, oprnd1: 0, oprnd2: 0));
15858 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15859 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0));
15860 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15861 VAR_INIT_STATUS_INITIALIZED);
15862 add_loc_descr (list_head: &ret, descr: tmp);
15863 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_eq, oprnd1: 0, oprnd2: 0));
15864 l2jump = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
15865 add_loc_descr (list_head: &ret, descr: l2jump);
15866 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15867 VAR_INIT_STATUS_INITIALIZED);
15868 add_loc_descr (list_head: &ret, descr: tmp);
15869 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
15870 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15871 l1jump = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
15872 add_loc_descr (list_head: &ret, descr: l1jump);
15873 l2label = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
15874 add_loc_descr (list_head: &ret, descr: l2label);
15875 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15876 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0));
15877 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15878 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15879 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15880 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15881 return ret;
15882}
15883
15884/* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15885 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15886 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15887 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15888
15889 ROTATERT is similar:
15890 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15891 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15892 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15893
15894static dw_loc_descr_ref
15895rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15896 machine_mode mem_mode)
15897{
15898 rtx rtlop1 = XEXP (rtl, 1);
15899 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15900 int i;
15901
15902 if (is_narrower_int_mode (GET_MODE (rtlop1), limit: mode))
15903 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15904 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15905 VAR_INIT_STATUS_INITIALIZED);
15906 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15907 VAR_INIT_STATUS_INITIALIZED);
15908 if (op0 == NULL || op1 == NULL)
15909 return NULL;
15910 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15911 for (i = 0; i < 2; i++)
15912 {
15913 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15914 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15915 mode, mem_mode,
15916 VAR_INIT_STATUS_INITIALIZED);
15917 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15918 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15919 ? DW_OP_const4u
15920 : HOST_BITS_PER_WIDE_INT == 64
15921 ? DW_OP_const8u : DW_OP_constu,
15922 GET_MODE_MASK (mode), oprnd2: 0);
15923 else
15924 mask[i] = NULL;
15925 if (mask[i] == NULL)
15926 return NULL;
15927 add_loc_descr (list_head: &mask[i], descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
15928 }
15929 ret = op0;
15930 add_loc_descr (list_head: &ret, descr: op1);
15931 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
15932 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
15933 if (GET_CODE (rtl) == ROTATERT)
15934 {
15935 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_neg, oprnd1: 0, oprnd2: 0));
15936 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_plus_uconst,
15937 oprnd1: GET_MODE_BITSIZE (mode), oprnd2: 0));
15938 }
15939 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
15940 if (mask[0] != NULL)
15941 add_loc_descr (list_head: &ret, descr: mask[0]);
15942 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_rot, oprnd1: 0, oprnd2: 0));
15943 if (mask[1] != NULL)
15944 {
15945 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15946 add_loc_descr (list_head: &ret, descr: mask[1]);
15947 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
15948 }
15949 if (GET_CODE (rtl) == ROTATE)
15950 {
15951 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_neg, oprnd1: 0, oprnd2: 0));
15952 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_plus_uconst,
15953 oprnd1: GET_MODE_BITSIZE (mode), oprnd2: 0));
15954 }
15955 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
15956 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_or, oprnd1: 0, oprnd2: 0));
15957 return ret;
15958}
15959
15960/* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15961 for DEBUG_PARAMETER_REF RTL. */
15962
15963static dw_loc_descr_ref
15964parameter_ref_descriptor (rtx rtl)
15965{
15966 dw_loc_descr_ref ret;
15967 dw_die_ref ref;
15968
15969 if (dwarf_strict)
15970 return NULL;
15971 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15972 /* With LTO during LTRANS we get the late DIE that refers to the early
15973 DIE, thus we add another indirection here. This seems to confuse
15974 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15975 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15976 ret = new_loc_descr (op: DW_OP_GNU_parameter_ref, oprnd1: 0, oprnd2: 0);
15977 if (ref)
15978 {
15979 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15980 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15981 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15982 }
15983 else
15984 {
15985 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15986 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15987 }
15988 return ret;
15989}
15990
15991/* The following routine converts the RTL for a variable or parameter
15992 (resident in memory) into an equivalent Dwarf representation of a
15993 mechanism for getting the address of that same variable onto the top of a
15994 hypothetical "address evaluation" stack.
15995
15996 When creating memory location descriptors, we are effectively transforming
15997 the RTL for a memory-resident object into its Dwarf postfix expression
15998 equivalent. This routine recursively descends an RTL tree, turning
15999 it into Dwarf postfix code as it goes.
16000
16001 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
16002
16003 MEM_MODE is the mode of the memory reference, needed to handle some
16004 autoincrement addressing modes.
16005
16006 Return 0 if we can't represent the location. */
16007
16008dw_loc_descr_ref
16009mem_loc_descriptor (rtx rtl, machine_mode mode,
16010 machine_mode mem_mode,
16011 enum var_init_status initialized)
16012{
16013 dw_loc_descr_ref mem_loc_result = NULL;
16014 enum dwarf_location_atom op;
16015 dw_loc_descr_ref op0, op1;
16016 rtx inner = NULL_RTX;
16017
16018 if (mode == VOIDmode)
16019 mode = GET_MODE (rtl);
16020
16021 /* Note that for a dynamically sized array, the location we will generate a
16022 description of here will be the lowest numbered location which is
16023 actually within the array. That's *not* necessarily the same as the
16024 zeroth element of the array. */
16025
16026 rtl = targetm.delegitimize_address (rtl);
16027
16028 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16029 return NULL;
16030
16031 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16032 switch (GET_CODE (rtl))
16033 {
16034 case POST_INC:
16035 case POST_DEC:
16036 case POST_MODIFY:
16037 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16038
16039 case SUBREG:
16040 /* The case of a subreg may arise when we have a local (register)
16041 variable or a formal (register) parameter which doesn't quite fill
16042 up an entire register. For now, just assume that it is
16043 legitimate to make the Dwarf info refer to the whole register which
16044 contains the given subreg. */
16045 if (!subreg_lowpart_p (rtl))
16046 break;
16047 inner = SUBREG_REG (rtl);
16048 /* FALLTHRU */
16049 case TRUNCATE:
16050 if (inner == NULL_RTX)
16051 inner = XEXP (rtl, 0);
16052 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
16053 && is_a <scalar_int_mode> (GET_MODE (inner), result: &inner_mode)
16054 && (GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
16055#ifdef POINTERS_EXTEND_UNSIGNED
16056 || (int_mode == Pmode && mem_mode != VOIDmode)
16057#endif
16058 )
16059 && GET_MODE_SIZE (mode: inner_mode) <= DWARF2_ADDR_SIZE)
16060 {
16061 mem_loc_result = mem_loc_descriptor (rtl: inner,
16062 mode: inner_mode,
16063 mem_mode, initialized);
16064 break;
16065 }
16066 if (dwarf_strict && dwarf_version < 5)
16067 break;
16068 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
16069 && is_a <scalar_int_mode> (GET_MODE (inner), result: &inner_mode)
16070 ? GET_MODE_SIZE (mode: int_mode) <= GET_MODE_SIZE (mode: inner_mode)
16071 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16072 {
16073 dw_die_ref type_die;
16074 dw_loc_descr_ref cvt;
16075
16076 mem_loc_result = mem_loc_descriptor (rtl: inner,
16077 GET_MODE (inner),
16078 mem_mode, initialized);
16079 if (mem_loc_result == NULL)
16080 break;
16081 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16082 if (type_die == NULL)
16083 {
16084 mem_loc_result = NULL;
16085 break;
16086 }
16087 if (maybe_ne (a: GET_MODE_SIZE (mode), b: GET_MODE_SIZE (GET_MODE (inner))))
16088 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16089 else
16090 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_reinterpret), oprnd1: 0, oprnd2: 0);
16091 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16092 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16093 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16094 add_loc_descr (list_head: &mem_loc_result, descr: cvt);
16095 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
16096 && GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE)
16097 {
16098 /* Convert it to untyped afterwards. */
16099 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16100 add_loc_descr (list_head: &mem_loc_result, descr: cvt);
16101 }
16102 }
16103 break;
16104
16105 case REG:
16106 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16107 || (GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE
16108 && rtl != arg_pointer_rtx
16109 && rtl != frame_pointer_rtx
16110#ifdef POINTERS_EXTEND_UNSIGNED
16111 && (int_mode != Pmode || mem_mode == VOIDmode)
16112#endif
16113 ))
16114 {
16115 dw_die_ref type_die;
16116 unsigned int debugger_regnum;
16117
16118 if (dwarf_strict && dwarf_version < 5)
16119 break;
16120 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16121 break;
16122 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16123 if (type_die == NULL)
16124 break;
16125
16126 debugger_regnum = debugger_reg_number (rtl);
16127 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16128 break;
16129 mem_loc_result = new_loc_descr (op: dwarf_OP (op: DW_OP_regval_type),
16130 oprnd1: debugger_regnum, oprnd2: 0);
16131 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16132 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16133 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16134 break;
16135 }
16136 /* Whenever a register number forms a part of the description of the
16137 method for calculating the (dynamic) address of a memory resident
16138 object, DWARF rules require the register number be referred to as
16139 a "base register". This distinction is not based in any way upon
16140 what category of register the hardware believes the given register
16141 belongs to. This is strictly DWARF terminology we're dealing with
16142 here. Note that in cases where the location of a memory-resident
16143 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16144 OP_CONST (0)) the actual DWARF location descriptor that we generate
16145 may just be OP_BASEREG (basereg). This may look deceptively like
16146 the object in question was allocated to a register (rather than in
16147 memory) so DWARF consumers need to be aware of the subtle
16148 distinction between OP_REG and OP_BASEREG. */
16149 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16150 mem_loc_result = based_loc_descr (reg: rtl, offset: 0, initialized: VAR_INIT_STATUS_INITIALIZED);
16151 else if (stack_realign_drap
16152 && crtl->drap_reg
16153 && crtl->args.internal_arg_pointer == rtl
16154 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16155 {
16156 /* If RTL is internal_arg_pointer, which has been optimized
16157 out, use DRAP instead. */
16158 mem_loc_result = based_loc_descr (crtl->drap_reg, offset: 0,
16159 initialized: VAR_INIT_STATUS_INITIALIZED);
16160 }
16161 break;
16162
16163 case SIGN_EXTEND:
16164 case ZERO_EXTEND:
16165 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16166 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), result: &inner_mode))
16167 break;
16168 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode: inner_mode,
16169 mem_mode, initialized: VAR_INIT_STATUS_INITIALIZED);
16170 if (op0 == 0)
16171 break;
16172 else if (GET_CODE (rtl) == ZERO_EXTEND
16173 && GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
16174 && GET_MODE_BITSIZE (mode: inner_mode) < HOST_BITS_PER_WIDE_INT
16175 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16176 to expand zero extend as two shifts instead of
16177 masking. */
16178 && GET_MODE_SIZE (mode: inner_mode) <= 4)
16179 {
16180 mem_loc_result = op0;
16181 add_loc_descr (list_head: &mem_loc_result,
16182 descr: int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16183 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
16184 }
16185 else if (GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE)
16186 {
16187 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode: inner_mode);
16188 shift *= BITS_PER_UNIT;
16189 if (GET_CODE (rtl) == SIGN_EXTEND)
16190 op = DW_OP_shra;
16191 else
16192 op = DW_OP_shr;
16193 mem_loc_result = op0;
16194 add_loc_descr (list_head: &mem_loc_result, descr: int_loc_descriptor (poly_i: shift));
16195 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
16196 add_loc_descr (list_head: &mem_loc_result, descr: int_loc_descriptor (poly_i: shift));
16197 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16198 }
16199 else if (!dwarf_strict || dwarf_version >= 5)
16200 {
16201 dw_die_ref type_die1, type_die2;
16202 dw_loc_descr_ref cvt;
16203
16204 type_die1 = base_type_for_mode (mode: inner_mode,
16205 GET_CODE (rtl) == ZERO_EXTEND);
16206 if (type_die1 == NULL)
16207 break;
16208 type_die2 = base_type_for_mode (mode: int_mode, unsignedp: 1);
16209 if (type_die2 == NULL)
16210 break;
16211 mem_loc_result = op0;
16212 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16213 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16214 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16215 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16216 add_loc_descr (list_head: &mem_loc_result, descr: cvt);
16217 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16218 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16219 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16220 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16221 add_loc_descr (list_head: &mem_loc_result, descr: cvt);
16222 }
16223 break;
16224
16225 case MEM:
16226 {
16227 rtx new_rtl = avoid_constant_pool_reference (rtl);
16228 if (new_rtl != rtl)
16229 {
16230 mem_loc_result = mem_loc_descriptor (rtl: new_rtl, mode, mem_mode,
16231 initialized);
16232 if (mem_loc_result != NULL)
16233 return mem_loc_result;
16234 }
16235 }
16236 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16237 mode: get_address_mode (mem: rtl), mem_mode: mode,
16238 initialized: VAR_INIT_STATUS_INITIALIZED);
16239 if (mem_loc_result == NULL)
16240 mem_loc_result = tls_mem_loc_descriptor (mem: rtl);
16241 if (mem_loc_result != NULL)
16242 {
16243 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16244 || GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
16245 {
16246 dw_die_ref type_die;
16247 dw_loc_descr_ref deref;
16248 HOST_WIDE_INT size;
16249
16250 if (dwarf_strict && dwarf_version < 5)
16251 return NULL;
16252 if (!GET_MODE_SIZE (mode).is_constant (const_value: &size))
16253 return NULL;
16254 type_die
16255 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16256 if (type_die == NULL)
16257 return NULL;
16258 deref = new_loc_descr (op: dwarf_OP (op: DW_OP_deref_type), oprnd1: size, oprnd2: 0);
16259 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16260 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16261 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16262 add_loc_descr (list_head: &mem_loc_result, descr: deref);
16263 }
16264 else if (GET_MODE_SIZE (mode: int_mode) == DWARF2_ADDR_SIZE)
16265 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0));
16266 else
16267 add_loc_descr (list_head: &mem_loc_result,
16268 descr: new_loc_descr (op: DW_OP_deref_size,
16269 oprnd1: GET_MODE_SIZE (mode: int_mode), oprnd2: 0));
16270 }
16271 break;
16272
16273 case LO_SUM:
16274 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16275
16276 case LABEL_REF:
16277 /* Some ports can transform a symbol ref into a label ref, because
16278 the symbol ref is too far away and has to be dumped into a constant
16279 pool. */
16280 case CONST:
16281 case SYMBOL_REF:
16282 case UNSPEC:
16283 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16284 || (GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE
16285#ifdef POINTERS_EXTEND_UNSIGNED
16286 && (int_mode != Pmode || mem_mode == VOIDmode)
16287#endif
16288 ))
16289 break;
16290
16291 if (GET_CODE (rtl) == UNSPEC)
16292 {
16293 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16294 can't express it in the debug info. This can happen e.g. with some
16295 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16296 approves. */
16297 bool not_ok = false;
16298 subrtx_var_iterator::array_type array;
16299 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16300 if (*iter != rtl && !CONSTANT_P (*iter))
16301 {
16302 not_ok = true;
16303 break;
16304 }
16305
16306 if (not_ok)
16307 break;
16308
16309 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16310 if (!const_ok_for_output_1 (rtl: *iter))
16311 {
16312 not_ok = true;
16313 break;
16314 }
16315
16316 if (not_ok)
16317 break;
16318
16319 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16320 goto symref;
16321 }
16322
16323 if (GET_CODE (rtl) == SYMBOL_REF
16324 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16325 {
16326 dw_loc_descr_ref temp;
16327
16328 /* If this is not defined, we have no way to emit the data. */
16329 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16330 break;
16331
16332 temp = new_addr_loc_descr (addr: rtl, dtprel: dtprel_true);
16333
16334 /* We check for DWARF 5 here because gdb did not implement
16335 DW_OP_form_tls_address until after 7.12. */
16336 mem_loc_result = new_loc_descr (op: (dwarf_version >= 5
16337 ? DW_OP_form_tls_address
16338 : DW_OP_GNU_push_tls_address),
16339 oprnd1: 0, oprnd2: 0);
16340 add_loc_descr (list_head: &mem_loc_result, descr: temp);
16341
16342 break;
16343 }
16344
16345 if (!const_ok_for_output (rtl))
16346 {
16347 if (GET_CODE (rtl) == CONST)
16348 switch (GET_CODE (XEXP (rtl, 0)))
16349 {
16350 case NOT:
16351 op = DW_OP_not;
16352 goto try_const_unop;
16353 case NEG:
16354 op = DW_OP_neg;
16355 goto try_const_unop;
16356 try_const_unop:
16357 rtx arg;
16358 arg = XEXP (XEXP (rtl, 0), 0);
16359 if (!CONSTANT_P (arg))
16360 arg = gen_rtx_CONST (int_mode, arg);
16361 op0 = mem_loc_descriptor (rtl: arg, mode: int_mode, mem_mode,
16362 initialized);
16363 if (op0)
16364 {
16365 mem_loc_result = op0;
16366 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16367 }
16368 break;
16369 default:
16370 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode: int_mode,
16371 mem_mode, initialized);
16372 break;
16373 }
16374 break;
16375 }
16376
16377 symref:
16378 mem_loc_result = new_addr_loc_descr (addr: rtl, dtprel: dtprel_false);
16379 vec_safe_push (v&: used_rtx_array, obj: rtl);
16380 break;
16381
16382 case CONCAT:
16383 case CONCATN:
16384 case VAR_LOCATION:
16385 case DEBUG_IMPLICIT_PTR:
16386 expansion_failed (NULL_TREE, rtl,
16387 reason: "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16388 return 0;
16389
16390 case ENTRY_VALUE:
16391 if (dwarf_strict && dwarf_version < 5)
16392 return NULL;
16393 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16394 {
16395 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16396 || GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
16397 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16398 VOIDmode, initialized: VAR_INIT_STATUS_INITIALIZED);
16399 else
16400 {
16401 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16402 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16403 return NULL;
16404 op0 = one_reg_loc_descriptor (regno: debugger_regnum,
16405 initialized: VAR_INIT_STATUS_INITIALIZED);
16406 }
16407 }
16408 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16409 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16410 {
16411 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16412 VOIDmode, initialized: VAR_INIT_STATUS_INITIALIZED);
16413 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16414 return NULL;
16415 }
16416 else
16417 gcc_unreachable ();
16418 if (op0 == NULL)
16419 return NULL;
16420 mem_loc_result = new_loc_descr (op: dwarf_OP (op: DW_OP_entry_value), oprnd1: 0, oprnd2: 0);
16421 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16422 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16423 break;
16424
16425 case DEBUG_PARAMETER_REF:
16426 mem_loc_result = parameter_ref_descriptor (rtl);
16427 break;
16428
16429 case PRE_MODIFY:
16430 /* Extract the PLUS expression nested inside and fall into
16431 PLUS code below. */
16432 rtl = XEXP (rtl, 1);
16433 goto plus;
16434
16435 case PRE_INC:
16436 case PRE_DEC:
16437 /* Turn these into a PLUS expression and fall into the PLUS code
16438 below. */
16439 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16440 gen_int_mode (GET_CODE (rtl) == PRE_INC
16441 ? GET_MODE_UNIT_SIZE (mem_mode)
16442 : -GET_MODE_UNIT_SIZE (mem_mode),
16443 mode));
16444
16445 /* fall through */
16446
16447 case PLUS:
16448 plus:
16449 if (is_based_loc (rtl)
16450 && is_a <scalar_int_mode> (m: mode, result: &int_mode)
16451 && (GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
16452 || XEXP (rtl, 0) == arg_pointer_rtx
16453 || XEXP (rtl, 0) == frame_pointer_rtx))
16454 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16455 INTVAL (XEXP (rtl, 1)),
16456 initialized: VAR_INIT_STATUS_INITIALIZED);
16457 else
16458 {
16459 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16460 initialized: VAR_INIT_STATUS_INITIALIZED);
16461 if (mem_loc_result == 0)
16462 break;
16463
16464 if (CONST_INT_P (XEXP (rtl, 1))
16465 && (GET_MODE_SIZE (mode: as_a <scalar_int_mode> (m: mode))
16466 <= DWARF2_ADDR_SIZE))
16467 loc_descr_plus_const (list_head: &mem_loc_result, INTVAL (XEXP (rtl, 1)));
16468 else
16469 {
16470 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16471 initialized: VAR_INIT_STATUS_INITIALIZED);
16472 if (op1 == 0)
16473 return NULL;
16474 add_loc_descr (list_head: &mem_loc_result, descr: op1);
16475 add_loc_descr (list_head: &mem_loc_result,
16476 descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
16477 }
16478 }
16479 break;
16480
16481 /* If a pseudo-reg is optimized away, it is possible for it to
16482 be replaced with a MEM containing a multiply or shift. */
16483 case MINUS:
16484 op = DW_OP_minus;
16485 goto do_binop;
16486
16487 case MULT:
16488 op = DW_OP_mul;
16489 goto do_binop;
16490
16491 case DIV:
16492 if ((!dwarf_strict || dwarf_version >= 5)
16493 && is_a <scalar_int_mode> (m: mode, result: &int_mode)
16494 && GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
16495 {
16496 mem_loc_result = typed_binop (op: DW_OP_div, rtl,
16497 type_die: base_type_for_mode (mode, unsignedp: 0),
16498 mode: int_mode, mem_mode);
16499 break;
16500 }
16501 op = DW_OP_div;
16502 goto do_binop;
16503
16504 case UMOD:
16505 op = DW_OP_mod;
16506 goto do_binop;
16507
16508 case ASHIFT:
16509 op = DW_OP_shl;
16510 goto do_shift;
16511
16512 case ASHIFTRT:
16513 op = DW_OP_shra;
16514 goto do_shift;
16515
16516 case LSHIFTRT:
16517 op = DW_OP_shr;
16518 goto do_shift;
16519
16520 do_shift:
16521 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode))
16522 break;
16523 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode: int_mode, mem_mode,
16524 initialized: VAR_INIT_STATUS_INITIALIZED);
16525 {
16526 rtx rtlop1 = XEXP (rtl, 1);
16527 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), result: &op1_mode)
16528 && GET_MODE_BITSIZE (mode: op1_mode) < GET_MODE_BITSIZE (mode: int_mode))
16529 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16530 op1 = mem_loc_descriptor (rtl: rtlop1, mode: int_mode, mem_mode,
16531 initialized: VAR_INIT_STATUS_INITIALIZED);
16532 }
16533
16534 if (op0 == 0 || op1 == 0)
16535 break;
16536
16537 mem_loc_result = op0;
16538 add_loc_descr (list_head: &mem_loc_result, descr: op1);
16539 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16540 break;
16541
16542 case AND:
16543 op = DW_OP_and;
16544 goto do_binop;
16545
16546 case IOR:
16547 op = DW_OP_or;
16548 goto do_binop;
16549
16550 case XOR:
16551 op = DW_OP_xor;
16552 goto do_binop;
16553
16554 do_binop:
16555 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16556 initialized: VAR_INIT_STATUS_INITIALIZED);
16557 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16558 {
16559 if (op0 == 0)
16560 break;
16561 mem_loc_result = op0;
16562 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0));
16563 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16564 break;
16565 }
16566 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16567 initialized: VAR_INIT_STATUS_INITIALIZED);
16568
16569 if (op0 == 0 || op1 == 0)
16570 break;
16571
16572 mem_loc_result = op0;
16573 add_loc_descr (list_head: &mem_loc_result, descr: op1);
16574 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16575 break;
16576
16577 case MOD:
16578 if ((!dwarf_strict || dwarf_version >= 5)
16579 && is_a <scalar_int_mode> (m: mode, result: &int_mode)
16580 && GET_MODE_SIZE (mode: int_mode) > DWARF2_ADDR_SIZE)
16581 {
16582 mem_loc_result = typed_binop (op: DW_OP_mod, rtl,
16583 type_die: base_type_for_mode (mode, unsignedp: 0),
16584 mode: int_mode, mem_mode);
16585 break;
16586 }
16587
16588 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16589 initialized: VAR_INIT_STATUS_INITIALIZED);
16590 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16591 initialized: VAR_INIT_STATUS_INITIALIZED);
16592
16593 if (op0 == 0 || op1 == 0)
16594 break;
16595
16596 mem_loc_result = op0;
16597 add_loc_descr (list_head: &mem_loc_result, descr: op1);
16598 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
16599 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
16600 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_div, oprnd1: 0, oprnd2: 0));
16601 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_mul, oprnd1: 0, oprnd2: 0));
16602 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
16603 break;
16604
16605 case UDIV:
16606 if ((!dwarf_strict || dwarf_version >= 5)
16607 && is_a <scalar_int_mode> (m: mode, result: &int_mode))
16608 {
16609 /* We can use a signed divide if the sign bit is not set. */
16610 if (GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
16611 {
16612 op = DW_OP_div;
16613 goto do_binop;
16614 }
16615
16616 mem_loc_result = typed_binop (op: DW_OP_div, rtl,
16617 type_die: base_type_for_mode (mode: int_mode, unsignedp: 1),
16618 mode: int_mode, mem_mode);
16619 }
16620 break;
16621
16622 case NOT:
16623 op = DW_OP_not;
16624 goto do_unop;
16625
16626 case ABS:
16627 op = DW_OP_abs;
16628 goto do_unop;
16629
16630 case NEG:
16631 op = DW_OP_neg;
16632 goto do_unop;
16633
16634 do_unop:
16635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16636 initialized: VAR_INIT_STATUS_INITIALIZED);
16637
16638 if (op0 == 0)
16639 break;
16640
16641 mem_loc_result = op0;
16642 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16643 break;
16644
16645 case CONST_INT:
16646 if (!is_a <scalar_int_mode> (m: mode, result: &int_mode)
16647 || GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
16648#ifdef POINTERS_EXTEND_UNSIGNED
16649 || (int_mode == Pmode
16650 && mem_mode != VOIDmode
16651 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16652#endif
16653 )
16654 {
16655 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16656 break;
16657 }
16658 if ((!dwarf_strict || dwarf_version >= 5)
16659 && (GET_MODE_BITSIZE (mode: int_mode) == HOST_BITS_PER_WIDE_INT
16660 || GET_MODE_BITSIZE (mode: int_mode) == HOST_BITS_PER_DOUBLE_INT))
16661 {
16662 dw_die_ref type_die = base_type_for_mode (mode: int_mode, unsignedp: 1);
16663 scalar_int_mode amode;
16664 if (type_die == NULL)
16665 return NULL;
16666 if (INTVAL (rtl) >= 0
16667 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, limit: 0)
16668 .exists (mode: &amode))
16669 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16670 /* const DW_OP_convert <XXX> vs.
16671 DW_OP_const_type <XXX, 1, const>. */
16672 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16673 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode: int_mode))
16674 {
16675 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16676 op0 = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16677 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16678 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16679 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16680 add_loc_descr (list_head: &mem_loc_result, descr: op0);
16681 return mem_loc_result;
16682 }
16683 mem_loc_result = new_loc_descr (op: dwarf_OP (op: DW_OP_const_type), oprnd1: 0,
16684 INTVAL (rtl));
16685 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16686 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16687 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16688 if (GET_MODE_BITSIZE (mode: int_mode) == HOST_BITS_PER_WIDE_INT)
16689 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16690 else
16691 {
16692 mem_loc_result->dw_loc_oprnd2.val_class
16693 = dw_val_class_const_double;
16694 mem_loc_result->dw_loc_oprnd2.v.val_double
16695 = double_int::from_shwi (INTVAL (rtl));
16696 }
16697 }
16698 break;
16699
16700 case CONST_DOUBLE:
16701 if (!dwarf_strict || dwarf_version >= 5)
16702 {
16703 dw_die_ref type_die;
16704
16705 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16706 CONST_DOUBLE rtx could represent either a large integer
16707 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16708 the value is always a floating point constant.
16709
16710 When it is an integer, a CONST_DOUBLE is used whenever
16711 the constant requires 2 HWIs to be adequately represented.
16712 We output CONST_DOUBLEs as blocks. */
16713 if (mode == VOIDmode
16714 || (GET_MODE (rtl) == VOIDmode
16715 && maybe_ne (a: GET_MODE_BITSIZE (mode),
16716 HOST_BITS_PER_DOUBLE_INT)))
16717 break;
16718 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16719 if (type_die == NULL)
16720 return NULL;
16721 mem_loc_result = new_loc_descr (op: dwarf_OP (op: DW_OP_const_type), oprnd1: 0, oprnd2: 0);
16722 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16723 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16725#if TARGET_SUPPORTS_WIDE_INT == 0
16726 if (!SCALAR_FLOAT_MODE_P (mode))
16727 {
16728 mem_loc_result->dw_loc_oprnd2.val_class
16729 = dw_val_class_const_double;
16730 mem_loc_result->dw_loc_oprnd2.v.val_double
16731 = rtx_to_double_int (rtl);
16732 }
16733 else
16734#endif
16735 {
16736 scalar_float_mode float_mode = as_a <scalar_float_mode> (m: mode);
16737 unsigned int length = GET_MODE_SIZE (mode: float_mode);
16738 unsigned char *array = ggc_vec_alloc<unsigned char> (c: length);
16739 unsigned int elt_size = insert_float (rtl, array);
16740
16741 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16742 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16743 = length / elt_size;
16744 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16745 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16746 }
16747 }
16748 break;
16749
16750 case CONST_WIDE_INT:
16751 if (!dwarf_strict || dwarf_version >= 5)
16752 {
16753 dw_die_ref type_die;
16754
16755 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16756 if (type_die == NULL)
16757 return NULL;
16758 mem_loc_result = new_loc_descr (op: dwarf_OP (op: DW_OP_const_type), oprnd1: 0, oprnd2: 0);
16759 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16760 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16761 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16762 mem_loc_result->dw_loc_oprnd2.val_class
16763 = dw_val_class_wide_int;
16764 mem_loc_result->dw_loc_oprnd2.v.val_wide
16765 = alloc_dw_wide_int (w: rtx_mode_t (rtl, mode));
16766 }
16767 break;
16768
16769 case CONST_POLY_INT:
16770 mem_loc_result = int_loc_descriptor (poly_i: rtx_to_poly_int64 (x: rtl));
16771 break;
16772
16773 case EQ:
16774 mem_loc_result = scompare_loc_descriptor (op: DW_OP_eq, rtl, mem_mode);
16775 break;
16776
16777 case GE:
16778 mem_loc_result = scompare_loc_descriptor (op: DW_OP_ge, rtl, mem_mode);
16779 break;
16780
16781 case GT:
16782 mem_loc_result = scompare_loc_descriptor (op: DW_OP_gt, rtl, mem_mode);
16783 break;
16784
16785 case LE:
16786 mem_loc_result = scompare_loc_descriptor (op: DW_OP_le, rtl, mem_mode);
16787 break;
16788
16789 case LT:
16790 mem_loc_result = scompare_loc_descriptor (op: DW_OP_lt, rtl, mem_mode);
16791 break;
16792
16793 case NE:
16794 mem_loc_result = scompare_loc_descriptor (op: DW_OP_ne, rtl, mem_mode);
16795 break;
16796
16797 case GEU:
16798 mem_loc_result = ucompare_loc_descriptor (op: DW_OP_ge, rtl, mem_mode);
16799 break;
16800
16801 case GTU:
16802 mem_loc_result = ucompare_loc_descriptor (op: DW_OP_gt, rtl, mem_mode);
16803 break;
16804
16805 case LEU:
16806 mem_loc_result = ucompare_loc_descriptor (op: DW_OP_le, rtl, mem_mode);
16807 break;
16808
16809 case LTU:
16810 mem_loc_result = ucompare_loc_descriptor (op: DW_OP_lt, rtl, mem_mode);
16811 break;
16812
16813 case UMIN:
16814 case UMAX:
16815 if (!SCALAR_INT_MODE_P (mode))
16816 break;
16817 /* FALLTHRU */
16818 case SMIN:
16819 case SMAX:
16820 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16821 break;
16822
16823 case ZERO_EXTRACT:
16824 case SIGN_EXTRACT:
16825 if (CONST_INT_P (XEXP (rtl, 1))
16826 && CONST_INT_P (XEXP (rtl, 2))
16827 && is_a <scalar_int_mode> (m: mode, result: &int_mode)
16828 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), result: &inner_mode)
16829 && GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
16830 && GET_MODE_SIZE (mode: inner_mode) <= DWARF2_ADDR_SIZE
16831 && ((unsigned) INTVAL (XEXP (rtl, 1))
16832 + (unsigned) INTVAL (XEXP (rtl, 2))
16833 <= GET_MODE_BITSIZE (mode: int_mode)))
16834 {
16835 int shift, size;
16836 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode: inner_mode,
16837 mem_mode, initialized: VAR_INIT_STATUS_INITIALIZED);
16838 if (op0 == 0)
16839 break;
16840 if (GET_CODE (rtl) == SIGN_EXTRACT)
16841 op = DW_OP_shra;
16842 else
16843 op = DW_OP_shr;
16844 mem_loc_result = op0;
16845 size = INTVAL (XEXP (rtl, 1));
16846 shift = INTVAL (XEXP (rtl, 2));
16847 if (BITS_BIG_ENDIAN)
16848 shift = GET_MODE_BITSIZE (mode: inner_mode) - shift - size;
16849 if (shift + size != (int) DWARF2_ADDR_SIZE)
16850 {
16851 add_loc_descr (list_head: &mem_loc_result,
16852 descr: int_loc_descriptor (DWARF2_ADDR_SIZE
16853 - shift - size));
16854 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
16855 }
16856 if (size != (int) DWARF2_ADDR_SIZE)
16857 {
16858 add_loc_descr (list_head: &mem_loc_result,
16859 descr: int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16860 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
16861 }
16862 }
16863 break;
16864
16865 case IF_THEN_ELSE:
16866 {
16867 dw_loc_descr_ref op2, bra_node, drop_node;
16868 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16869 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16870 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16871 mem_mode, initialized: VAR_INIT_STATUS_INITIALIZED);
16872 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16873 initialized: VAR_INIT_STATUS_INITIALIZED);
16874 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16875 initialized: VAR_INIT_STATUS_INITIALIZED);
16876 if (op0 == NULL || op1 == NULL || op2 == NULL)
16877 break;
16878
16879 mem_loc_result = op1;
16880 add_loc_descr (list_head: &mem_loc_result, descr: op2);
16881 add_loc_descr (list_head: &mem_loc_result, descr: op0);
16882 bra_node = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
16883 add_loc_descr (list_head: &mem_loc_result, descr: bra_node);
16884 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0));
16885 drop_node = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
16886 add_loc_descr (list_head: &mem_loc_result, descr: drop_node);
16887 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16888 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16889 }
16890 break;
16891
16892 case FLOAT_EXTEND:
16893 case FLOAT_TRUNCATE:
16894 case FLOAT:
16895 case UNSIGNED_FLOAT:
16896 case FIX:
16897 case UNSIGNED_FIX:
16898 if (!dwarf_strict || dwarf_version >= 5)
16899 {
16900 dw_die_ref type_die;
16901 dw_loc_descr_ref cvt;
16902
16903 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16904 mem_mode, initialized: VAR_INIT_STATUS_INITIALIZED);
16905 if (op0 == NULL)
16906 break;
16907 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), result: &int_mode)
16908 && (GET_CODE (rtl) == FLOAT
16909 || GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE))
16910 {
16911 type_die = base_type_for_mode (mode: int_mode,
16912 GET_CODE (rtl) == UNSIGNED_FLOAT);
16913 if (type_die == NULL)
16914 break;
16915 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16916 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16917 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16918 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16919 add_loc_descr (list_head: &op0, descr: cvt);
16920 }
16921 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16922 if (type_die == NULL)
16923 break;
16924 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
16925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16928 add_loc_descr (list_head: &op0, descr: cvt);
16929 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
16930 && (GET_CODE (rtl) == FIX
16931 || GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE))
16932 {
16933 op0 = convert_descriptor_to_mode (mode: int_mode, op: op0);
16934 if (op0 == NULL)
16935 break;
16936 }
16937 mem_loc_result = op0;
16938 }
16939 break;
16940
16941 case CLZ:
16942 case CTZ:
16943 case FFS:
16944 if (is_a <scalar_int_mode> (m: mode, result: &int_mode))
16945 mem_loc_result = clz_loc_descriptor (rtl, mode: int_mode, mem_mode);
16946 break;
16947
16948 case POPCOUNT:
16949 case PARITY:
16950 if (is_a <scalar_int_mode> (m: mode, result: &int_mode))
16951 mem_loc_result = popcount_loc_descriptor (rtl, mode: int_mode, mem_mode);
16952 break;
16953
16954 case BSWAP:
16955 if (is_a <scalar_int_mode> (m: mode, result: &int_mode))
16956 mem_loc_result = bswap_loc_descriptor (rtl, mode: int_mode, mem_mode);
16957 break;
16958
16959 case ROTATE:
16960 case ROTATERT:
16961 if (is_a <scalar_int_mode> (m: mode, result: &int_mode))
16962 mem_loc_result = rotate_loc_descriptor (rtl, mode: int_mode, mem_mode);
16963 break;
16964
16965 case COMPARE:
16966 /* In theory, we could implement the above. */
16967 /* DWARF cannot represent the unsigned compare operations
16968 natively. */
16969 case SS_MULT:
16970 case US_MULT:
16971 case SS_DIV:
16972 case US_DIV:
16973 case SS_PLUS:
16974 case US_PLUS:
16975 case SS_MINUS:
16976 case US_MINUS:
16977 case SS_NEG:
16978 case US_NEG:
16979 case SS_ABS:
16980 case SS_ASHIFT:
16981 case US_ASHIFT:
16982 case SS_TRUNCATE:
16983 case US_TRUNCATE:
16984 case UNORDERED:
16985 case ORDERED:
16986 case UNEQ:
16987 case UNGE:
16988 case UNGT:
16989 case UNLE:
16990 case UNLT:
16991 case LTGT:
16992 case FRACT_CONVERT:
16993 case UNSIGNED_FRACT_CONVERT:
16994 case SAT_FRACT:
16995 case UNSIGNED_SAT_FRACT:
16996 case SQRT:
16997 case ASM_OPERANDS:
16998 case VEC_MERGE:
16999 case VEC_SELECT:
17000 case VEC_CONCAT:
17001 case VEC_DUPLICATE:
17002 case VEC_SERIES:
17003 case HIGH:
17004 case FMA:
17005 case STRICT_LOW_PART:
17006 case CONST_VECTOR:
17007 case CONST_FIXED:
17008 case CLRSB:
17009 case CLOBBER:
17010 case SMUL_HIGHPART:
17011 case UMUL_HIGHPART:
17012 case BITREVERSE:
17013 case COPYSIGN:
17014 break;
17015
17016 case CONST_STRING:
17017 resolve_one_addr (&rtl);
17018 goto symref;
17019
17020 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17021 the expression. An UNSPEC rtx represents a raw DWARF operation,
17022 new_loc_descr is called for it to build the operation directly.
17023 Otherwise mem_loc_descriptor is called recursively. */
17024 case PARALLEL:
17025 {
17026 int index = 0;
17027 dw_loc_descr_ref exp_result = NULL;
17028
17029 for (; index < XVECLEN (rtl, 0); index++)
17030 {
17031 rtx elem = XVECEXP (rtl, 0, index);
17032 if (GET_CODE (elem) == UNSPEC)
17033 {
17034 /* Each DWARF operation UNSPEC contain two operands, if
17035 one operand is not used for the operation, const0_rtx is
17036 passed. */
17037 gcc_assert (XVECLEN (elem, 0) == 2);
17038
17039 HOST_WIDE_INT dw_op = XINT (elem, 1);
17040 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17041 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17042 exp_result
17043 = new_loc_descr (op: (enum dwarf_location_atom) dw_op, oprnd1,
17044 oprnd2);
17045 }
17046 else
17047 exp_result
17048 = mem_loc_descriptor (rtl: elem, mode, mem_mode,
17049 initialized: VAR_INIT_STATUS_INITIALIZED);
17050
17051 if (!mem_loc_result)
17052 mem_loc_result = exp_result;
17053 else
17054 add_loc_descr (list_head: &mem_loc_result, descr: exp_result);
17055 }
17056
17057 break;
17058 }
17059
17060 default:
17061 if (flag_checking)
17062 {
17063 print_rtl (stderr, rtl);
17064 gcc_unreachable ();
17065 }
17066 break;
17067 }
17068
17069 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17070 add_loc_descr (list_head: &mem_loc_result, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
17071
17072 return mem_loc_result;
17073}
17074
17075/* Return a descriptor that describes the concatenation of two locations.
17076 This is typically a complex variable. */
17077
17078static dw_loc_descr_ref
17079concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17080{
17081 /* At present we only track constant-sized pieces. */
17082 unsigned int size0, size1;
17083 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (const_value: &size0)
17084 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (const_value: &size1))
17085 return 0;
17086
17087 dw_loc_descr_ref cc_loc_result = NULL;
17088 dw_loc_descr_ref x0_ref
17089 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17090 dw_loc_descr_ref x1_ref
17091 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17092
17093 if (x0_ref == 0 || x1_ref == 0)
17094 return 0;
17095
17096 cc_loc_result = x0_ref;
17097 add_loc_descr_op_piece (list_head: &cc_loc_result, size: size0);
17098
17099 add_loc_descr (list_head: &cc_loc_result, descr: x1_ref);
17100 add_loc_descr_op_piece (list_head: &cc_loc_result, size: size1);
17101
17102 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17103 add_loc_descr (list_head: &cc_loc_result, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
17104
17105 return cc_loc_result;
17106}
17107
17108/* Return a descriptor that describes the concatenation of N
17109 locations. */
17110
17111static dw_loc_descr_ref
17112concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17113{
17114 unsigned int i;
17115 dw_loc_descr_ref cc_loc_result = NULL;
17116 unsigned int n = XVECLEN (concatn, 0);
17117 unsigned int size;
17118
17119 for (i = 0; i < n; ++i)
17120 {
17121 dw_loc_descr_ref ref;
17122 rtx x = XVECEXP (concatn, 0, i);
17123
17124 /* At present we only track constant-sized pieces. */
17125 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (const_value: &size))
17126 return NULL;
17127
17128 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17129 if (ref == NULL)
17130 return NULL;
17131
17132 add_loc_descr (list_head: &cc_loc_result, descr: ref);
17133 add_loc_descr_op_piece (list_head: &cc_loc_result, size);
17134 }
17135
17136 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17137 add_loc_descr (list_head: &cc_loc_result, descr: new_loc_descr (op: DW_OP_GNU_uninit, oprnd1: 0, oprnd2: 0));
17138
17139 return cc_loc_result;
17140}
17141
17142/* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17143 for DEBUG_IMPLICIT_PTR RTL. */
17144
17145static dw_loc_descr_ref
17146implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17147{
17148 dw_loc_descr_ref ret;
17149 dw_die_ref ref;
17150
17151 if (dwarf_strict && dwarf_version < 5)
17152 return NULL;
17153 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17154 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17155 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17156 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17157 ret = new_loc_descr (op: dwarf_OP (op: DW_OP_implicit_pointer), oprnd1: 0, oprnd2: offset);
17158 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17159 if (ref)
17160 {
17161 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17162 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17163 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17164 }
17165 else
17166 {
17167 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17168 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17169 }
17170 return ret;
17171}
17172
17173/* Output a proper Dwarf location descriptor for a variable or parameter
17174 which is either allocated in a register or in a memory location. For a
17175 register, we just generate an OP_REG and the register number. For a
17176 memory location we provide a Dwarf postfix expression describing how to
17177 generate the (dynamic) address of the object onto the address stack.
17178
17179 MODE is mode of the decl if this loc_descriptor is going to be used in
17180 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17181 allowed, VOIDmode otherwise.
17182
17183 If we don't know how to describe it, return 0. */
17184
17185static dw_loc_descr_ref
17186loc_descriptor (rtx rtl, machine_mode mode,
17187 enum var_init_status initialized)
17188{
17189 dw_loc_descr_ref loc_result = NULL;
17190 scalar_int_mode int_mode;
17191
17192 switch (GET_CODE (rtl))
17193 {
17194 case SUBREG:
17195 /* The case of a subreg may arise when we have a local (register)
17196 variable or a formal (register) parameter which doesn't quite fill
17197 up an entire register. For now, just assume that it is
17198 legitimate to make the Dwarf info refer to the whole register which
17199 contains the given subreg. */
17200 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17201 loc_result = loc_descriptor (SUBREG_REG (rtl),
17202 GET_MODE (SUBREG_REG (rtl)), initialized);
17203 else
17204 goto do_default;
17205 break;
17206
17207 case REG:
17208 loc_result = reg_loc_descriptor (rtl, initialized);
17209 break;
17210
17211 case MEM:
17212 loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode: get_address_mode (mem: rtl),
17213 GET_MODE (rtl), initialized);
17214 if (loc_result == NULL)
17215 loc_result = tls_mem_loc_descriptor (mem: rtl);
17216 if (loc_result == NULL)
17217 {
17218 rtx new_rtl = avoid_constant_pool_reference (rtl);
17219 if (new_rtl != rtl)
17220 loc_result = loc_descriptor (rtl: new_rtl, mode, initialized);
17221 }
17222 break;
17223
17224 case CONCAT:
17225 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17226 initialized);
17227 break;
17228
17229 case CONCATN:
17230 loc_result = concatn_loc_descriptor (concatn: rtl, initialized);
17231 break;
17232
17233 case VAR_LOCATION:
17234 /* Single part. */
17235 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17236 {
17237 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17238 if (GET_CODE (loc) == EXPR_LIST)
17239 loc = XEXP (loc, 0);
17240 loc_result = loc_descriptor (rtl: loc, mode, initialized);
17241 break;
17242 }
17243
17244 rtl = XEXP (rtl, 1);
17245 /* FALLTHRU */
17246
17247 case PARALLEL:
17248 {
17249 rtvec par_elems = XVEC (rtl, 0);
17250 int num_elem = GET_NUM_ELEM (par_elems);
17251 machine_mode mode;
17252 int i, size;
17253
17254 /* Create the first one, so we have something to add to. */
17255 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17256 VOIDmode, initialized);
17257 if (loc_result == NULL)
17258 return NULL;
17259 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17260 /* At present we only track constant-sized pieces. */
17261 if (!GET_MODE_SIZE (mode).is_constant (const_value: &size))
17262 return NULL;
17263 add_loc_descr_op_piece (list_head: &loc_result, size);
17264 for (i = 1; i < num_elem; i++)
17265 {
17266 dw_loc_descr_ref temp;
17267
17268 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17269 VOIDmode, initialized);
17270 if (temp == NULL)
17271 return NULL;
17272 add_loc_descr (list_head: &loc_result, descr: temp);
17273 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17274 /* At present we only track constant-sized pieces. */
17275 if (!GET_MODE_SIZE (mode).is_constant (const_value: &size))
17276 return NULL;
17277 add_loc_descr_op_piece (list_head: &loc_result, size);
17278 }
17279 }
17280 break;
17281
17282 case CONST_INT:
17283 if (mode != VOIDmode && mode != BLKmode)
17284 {
17285 int_mode = as_a <scalar_int_mode> (m: mode);
17286 loc_result = address_of_int_loc_descriptor (size: GET_MODE_SIZE (mode: int_mode),
17287 INTVAL (rtl));
17288 }
17289 break;
17290
17291 case CONST_DOUBLE:
17292 if (mode == VOIDmode)
17293 mode = GET_MODE (rtl);
17294
17295 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17296 {
17297 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17298
17299 /* Note that a CONST_DOUBLE rtx could represent either an integer
17300 or a floating-point constant. A CONST_DOUBLE is used whenever
17301 the constant requires more than one word in order to be
17302 adequately represented. We output CONST_DOUBLEs as blocks. */
17303 scalar_mode smode = as_a <scalar_mode> (m: mode);
17304 loc_result = new_loc_descr (op: DW_OP_implicit_value,
17305 oprnd1: GET_MODE_SIZE (mode: smode), oprnd2: 0);
17306#if TARGET_SUPPORTS_WIDE_INT == 0
17307 if (!SCALAR_FLOAT_MODE_P (smode))
17308 {
17309 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17310 loc_result->dw_loc_oprnd2.v.val_double
17311 = rtx_to_double_int (rtl);
17312 }
17313 else
17314#endif
17315 {
17316 unsigned int length = GET_MODE_SIZE (mode: smode);
17317 unsigned char *array = ggc_vec_alloc<unsigned char> (c: length);
17318 unsigned int elt_size = insert_float (rtl, array);
17319
17320 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17321 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17322 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17323 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17324 }
17325 }
17326 break;
17327
17328 case CONST_WIDE_INT:
17329 if (mode == VOIDmode)
17330 mode = GET_MODE (rtl);
17331
17332 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17333 {
17334 int_mode = as_a <scalar_int_mode> (m: mode);
17335 loc_result = new_loc_descr (op: DW_OP_implicit_value,
17336 oprnd1: GET_MODE_SIZE (mode: int_mode), oprnd2: 0);
17337 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17338 loc_result->dw_loc_oprnd2.v.val_wide
17339 = alloc_dw_wide_int (w: rtx_mode_t (rtl, int_mode));
17340 }
17341 break;
17342
17343 case CONST_VECTOR:
17344 if (mode == VOIDmode)
17345 mode = GET_MODE (rtl);
17346
17347 if (mode != VOIDmode
17348 /* The combination of a length and byte elt_size doesn't extend
17349 naturally to boolean vectors, where several elements are packed
17350 into the same byte. */
17351 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17352 && (dwarf_version >= 4 || !dwarf_strict))
17353 {
17354 unsigned int length;
17355 if (!CONST_VECTOR_NUNITS (rtl).is_constant (const_value: &length))
17356 return NULL;
17357
17358 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17359 unsigned char *array
17360 = ggc_vec_alloc<unsigned char> (c: length * elt_size);
17361 unsigned int i;
17362 unsigned char *p;
17363 machine_mode imode = GET_MODE_INNER (mode);
17364
17365 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17366 switch (GET_MODE_CLASS (mode))
17367 {
17368 case MODE_VECTOR_INT:
17369 for (i = 0, p = array; i < length; i++, p += elt_size)
17370 {
17371 rtx elt = CONST_VECTOR_ELT (rtl, i);
17372 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17373 }
17374 break;
17375
17376 case MODE_VECTOR_FLOAT:
17377 for (i = 0, p = array; i < length; i++, p += elt_size)
17378 {
17379 rtx elt = CONST_VECTOR_ELT (rtl, i);
17380 insert_float (elt, p);
17381 }
17382 break;
17383
17384 default:
17385 gcc_unreachable ();
17386 }
17387
17388 loc_result = new_loc_descr (op: DW_OP_implicit_value,
17389 oprnd1: length * elt_size, oprnd2: 0);
17390 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17391 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17392 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17393 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17394 }
17395 break;
17396
17397 case CONST:
17398 if (mode == VOIDmode
17399 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17400 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17401 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17402 {
17403 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17404 break;
17405 }
17406 /* FALLTHROUGH */
17407 case SYMBOL_REF:
17408 if (!const_ok_for_output (rtl))
17409 break;
17410 /* FALLTHROUGH */
17411 case LABEL_REF:
17412 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
17413 && GET_MODE_SIZE (mode: int_mode) == DWARF2_ADDR_SIZE
17414 && (dwarf_version >= 4 || !dwarf_strict))
17415 {
17416 loc_result = new_addr_loc_descr (addr: rtl, dtprel: dtprel_false);
17417 add_loc_descr (list_head: &loc_result, descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
17418 vec_safe_push (v&: used_rtx_array, obj: rtl);
17419 }
17420 break;
17421
17422 case DEBUG_IMPLICIT_PTR:
17423 loc_result = implicit_ptr_descriptor (rtl, offset: 0);
17424 break;
17425
17426 case PLUS:
17427 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17428 && CONST_INT_P (XEXP (rtl, 1)))
17429 {
17430 loc_result
17431 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17432 break;
17433 }
17434 /* FALLTHRU */
17435 do_default:
17436 default:
17437 if ((is_a <scalar_int_mode> (m: mode, result: &int_mode)
17438 && GET_MODE (rtl) == int_mode
17439 && GET_MODE_SIZE (mode: int_mode) <= DWARF2_ADDR_SIZE
17440 && dwarf_version >= 4)
17441 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17442 {
17443 /* Value expression. */
17444 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17445 if (loc_result)
17446 add_loc_descr (list_head: &loc_result,
17447 descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
17448 }
17449 break;
17450 }
17451
17452 return loc_result;
17453}
17454
17455/* We need to figure out what section we should use as the base for the
17456 address ranges where a given location is valid.
17457 1. If this particular DECL has a section associated with it, use that.
17458 2. If this function has a section associated with it, use that.
17459 3. Otherwise, use the text section.
17460 XXX: If you split a variable across multiple sections, we won't notice. */
17461
17462static const char *
17463secname_for_decl (const_tree decl)
17464{
17465 const char *secname;
17466
17467 if (VAR_OR_FUNCTION_DECL_P (decl)
17468 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17469 && DECL_SECTION_NAME (decl))
17470 secname = DECL_SECTION_NAME (decl);
17471 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17472 {
17473 if (in_cold_section_p)
17474 {
17475 section *sec = current_function_section ();
17476 if (sec->common.flags & SECTION_NAMED)
17477 return sec->named.name;
17478 }
17479 secname = DECL_SECTION_NAME (current_function_decl);
17480 }
17481 else if (cfun && in_cold_section_p)
17482 secname = crtl->subsections.cold_section_label;
17483 else
17484 secname = text_section_label;
17485
17486 return secname;
17487}
17488
17489/* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17490
17491static bool
17492decl_by_reference_p (tree decl)
17493{
17494 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17495 || VAR_P (decl))
17496 && DECL_BY_REFERENCE (decl));
17497}
17498
17499/* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17500 for VARLOC. */
17501
17502static dw_loc_descr_ref
17503dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17504 enum var_init_status initialized)
17505{
17506 int have_address = 0;
17507 dw_loc_descr_ref descr;
17508 machine_mode mode;
17509
17510 if (want_address != 2)
17511 {
17512 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17513 /* Single part. */
17514 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17515 {
17516 varloc = PAT_VAR_LOCATION_LOC (varloc);
17517 if (GET_CODE (varloc) == EXPR_LIST)
17518 varloc = XEXP (varloc, 0);
17519 mode = GET_MODE (varloc);
17520 if (MEM_P (varloc))
17521 {
17522 rtx addr = XEXP (varloc, 0);
17523 descr = mem_loc_descriptor (rtl: addr, mode: get_address_mode (mem: varloc),
17524 mem_mode: mode, initialized);
17525 if (descr)
17526 have_address = 1;
17527 else
17528 {
17529 rtx x = avoid_constant_pool_reference (varloc);
17530 if (x != varloc)
17531 descr = mem_loc_descriptor (rtl: x, mode, VOIDmode,
17532 initialized);
17533 }
17534 }
17535 else
17536 descr = mem_loc_descriptor (rtl: varloc, mode, VOIDmode, initialized);
17537 }
17538 else
17539 return 0;
17540 }
17541 else
17542 {
17543 if (GET_CODE (varloc) == VAR_LOCATION)
17544 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17545 else
17546 mode = DECL_MODE (loc);
17547 descr = loc_descriptor (rtl: varloc, mode, initialized);
17548 have_address = 1;
17549 }
17550
17551 if (!descr)
17552 return 0;
17553
17554 if (want_address == 2 && !have_address
17555 && (dwarf_version >= 4 || !dwarf_strict))
17556 {
17557 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17558 {
17559 expansion_failed (expr: loc, NULL_RTX,
17560 reason: "DWARF address size mismatch");
17561 return 0;
17562 }
17563 add_loc_descr (list_head: &descr, descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
17564 have_address = 1;
17565 }
17566 /* Show if we can't fill the request for an address. */
17567 if (want_address && !have_address)
17568 {
17569 expansion_failed (expr: loc, NULL_RTX,
17570 reason: "Want address and only have value");
17571 return 0;
17572 }
17573
17574 /* If we've got an address and don't want one, dereference. */
17575 if (!want_address && have_address)
17576 {
17577 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17578 enum dwarf_location_atom op;
17579
17580 if (size > DWARF2_ADDR_SIZE || size == -1)
17581 {
17582 expansion_failed (expr: loc, NULL_RTX,
17583 reason: "DWARF address size mismatch");
17584 return 0;
17585 }
17586 else if (size == DWARF2_ADDR_SIZE)
17587 op = DW_OP_deref;
17588 else
17589 op = DW_OP_deref_size;
17590
17591 add_loc_descr (list_head: &descr, descr: new_loc_descr (op, oprnd1: size, oprnd2: 0));
17592 }
17593
17594 return descr;
17595}
17596
17597/* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17598 if it is not possible. */
17599
17600static dw_loc_descr_ref
17601new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17602{
17603 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17604 return new_loc_descr (op: DW_OP_piece, oprnd1: bitsize / BITS_PER_UNIT, oprnd2: 0);
17605 else if (dwarf_version >= 3 || !dwarf_strict)
17606 return new_loc_descr (op: DW_OP_bit_piece, oprnd1: bitsize, oprnd2: offset);
17607 else
17608 return NULL;
17609}
17610
17611/* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17612 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17613
17614static dw_loc_descr_ref
17615dw_sra_loc_expr (tree decl, rtx loc)
17616{
17617 rtx p;
17618 unsigned HOST_WIDE_INT padsize = 0;
17619 dw_loc_descr_ref descr, *descr_tail;
17620 unsigned HOST_WIDE_INT decl_size;
17621 rtx varloc;
17622 enum var_init_status initialized;
17623
17624 if (DECL_SIZE (decl) == NULL
17625 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17626 return NULL;
17627
17628 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17629 descr = NULL;
17630 descr_tail = &descr;
17631
17632 for (p = loc; p; p = XEXP (p, 1))
17633 {
17634 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (piece: p);
17635 rtx loc_note = *decl_piece_varloc_ptr (piece: p);
17636 dw_loc_descr_ref cur_descr;
17637 dw_loc_descr_ref *tail, last = NULL;
17638 unsigned HOST_WIDE_INT opsize = 0;
17639
17640 if (loc_note == NULL_RTX
17641 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17642 {
17643 padsize += bitsize;
17644 continue;
17645 }
17646 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17647 varloc = NOTE_VAR_LOCATION (loc_note);
17648 cur_descr = dw_loc_list_1 (loc: decl, varloc, want_address: 2, initialized);
17649 if (cur_descr == NULL)
17650 {
17651 padsize += bitsize;
17652 continue;
17653 }
17654
17655 /* Check that cur_descr either doesn't use
17656 DW_OP_*piece operations, or their sum is equal
17657 to bitsize. Otherwise we can't embed it. */
17658 for (tail = &cur_descr; *tail != NULL;
17659 tail = &(*tail)->dw_loc_next)
17660 if ((*tail)->dw_loc_opc == DW_OP_piece)
17661 {
17662 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17663 * BITS_PER_UNIT;
17664 last = *tail;
17665 }
17666 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17667 {
17668 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17669 last = *tail;
17670 }
17671
17672 if (last != NULL && opsize != bitsize)
17673 {
17674 padsize += bitsize;
17675 /* Discard the current piece of the descriptor and release any
17676 addr_table entries it uses. */
17677 remove_loc_list_addr_table_entries (descr: cur_descr);
17678 continue;
17679 }
17680
17681 /* If there is a hole, add DW_OP_*piece after empty DWARF
17682 expression, which means that those bits are optimized out. */
17683 if (padsize)
17684 {
17685 if (padsize > decl_size)
17686 {
17687 remove_loc_list_addr_table_entries (descr: cur_descr);
17688 goto discard_descr;
17689 }
17690 decl_size -= padsize;
17691 *descr_tail = new_loc_descr_op_bit_piece (bitsize: padsize, offset: 0);
17692 if (*descr_tail == NULL)
17693 {
17694 remove_loc_list_addr_table_entries (descr: cur_descr);
17695 goto discard_descr;
17696 }
17697 descr_tail = &(*descr_tail)->dw_loc_next;
17698 padsize = 0;
17699 }
17700 *descr_tail = cur_descr;
17701 descr_tail = tail;
17702 if (bitsize > decl_size)
17703 goto discard_descr;
17704 decl_size -= bitsize;
17705 if (last == NULL)
17706 {
17707 HOST_WIDE_INT offset = 0;
17708 if (GET_CODE (varloc) == VAR_LOCATION
17709 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17710 {
17711 varloc = PAT_VAR_LOCATION_LOC (varloc);
17712 if (GET_CODE (varloc) == EXPR_LIST)
17713 varloc = XEXP (varloc, 0);
17714 }
17715 do
17716 {
17717 if (GET_CODE (varloc) == CONST
17718 || GET_CODE (varloc) == SIGN_EXTEND
17719 || GET_CODE (varloc) == ZERO_EXTEND)
17720 varloc = XEXP (varloc, 0);
17721 else if (GET_CODE (varloc) == SUBREG)
17722 varloc = SUBREG_REG (varloc);
17723 else
17724 break;
17725 }
17726 while (1);
17727 /* DW_OP_bit_size offset should be zero for register
17728 or implicit location descriptions and empty location
17729 descriptions, but for memory addresses needs big endian
17730 adjustment. */
17731 if (MEM_P (varloc))
17732 {
17733 unsigned HOST_WIDE_INT memsize;
17734 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (const_value: &memsize))
17735 goto discard_descr;
17736 memsize *= BITS_PER_UNIT;
17737 if (memsize != bitsize)
17738 {
17739 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17740 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17741 goto discard_descr;
17742 if (memsize < bitsize)
17743 goto discard_descr;
17744 if (BITS_BIG_ENDIAN)
17745 offset = memsize - bitsize;
17746 }
17747 }
17748
17749 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17750 if (*descr_tail == NULL)
17751 goto discard_descr;
17752 descr_tail = &(*descr_tail)->dw_loc_next;
17753 }
17754 }
17755
17756 /* If there were any non-empty expressions, add padding till the end of
17757 the decl. */
17758 if (descr != NULL && decl_size != 0)
17759 {
17760 *descr_tail = new_loc_descr_op_bit_piece (bitsize: decl_size, offset: 0);
17761 if (*descr_tail == NULL)
17762 goto discard_descr;
17763 }
17764 return descr;
17765
17766discard_descr:
17767 /* Discard the descriptor and release any addr_table entries it uses. */
17768 remove_loc_list_addr_table_entries (descr);
17769 return NULL;
17770}
17771
17772/* Return the dwarf representation of the location list LOC_LIST of
17773 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17774 function. */
17775
17776static dw_loc_list_ref
17777dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17778{
17779 const char *endname, *secname;
17780 var_loc_view endview;
17781 rtx varloc;
17782 enum var_init_status initialized;
17783 struct var_loc_node *node;
17784 dw_loc_descr_ref descr;
17785 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17786 dw_loc_list_ref list = NULL;
17787 dw_loc_list_ref *listp = &list;
17788
17789 /* Now that we know what section we are using for a base,
17790 actually construct the list of locations.
17791 The first location information is what is passed to the
17792 function that creates the location list, and the remaining
17793 locations just get added on to that list.
17794 Note that we only know the start address for a location
17795 (IE location changes), so to build the range, we use
17796 the range [current location start, next location start].
17797 This means we have to special case the last node, and generate
17798 a range of [last location start, end of function label]. */
17799
17800 if (cfun && crtl->has_bb_partition)
17801 {
17802 bool save_in_cold_section_p = in_cold_section_p;
17803 in_cold_section_p = first_function_block_is_cold;
17804 if (loc_list->last_before_switch == NULL)
17805 in_cold_section_p = !in_cold_section_p;
17806 secname = secname_for_decl (decl);
17807 in_cold_section_p = save_in_cold_section_p;
17808 }
17809 else
17810 secname = secname_for_decl (decl);
17811
17812 for (node = loc_list->first; node; node = node->next)
17813 {
17814 bool range_across_switch = false;
17815 if (GET_CODE (node->loc) == EXPR_LIST
17816 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17817 {
17818 if (GET_CODE (node->loc) == EXPR_LIST)
17819 {
17820 descr = NULL;
17821 /* This requires DW_OP_{,bit_}piece, which is not usable
17822 inside DWARF expressions. */
17823 if (want_address == 2)
17824 descr = dw_sra_loc_expr (decl, loc: node->loc);
17825 }
17826 else
17827 {
17828 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17829 varloc = NOTE_VAR_LOCATION (node->loc);
17830 descr = dw_loc_list_1 (loc: decl, varloc, want_address, initialized);
17831 }
17832 if (descr)
17833 {
17834 /* If section switch happens in between node->label
17835 and node->next->label (or end of function) and
17836 we can't emit it as a single entry list,
17837 emit two ranges, first one ending at the end
17838 of first partition and second one starting at the
17839 beginning of second partition. */
17840 if (node == loc_list->last_before_switch
17841 && (node != loc_list->first || loc_list->first->next
17842 /* If we are to emit a view number, we will emit
17843 a loclist rather than a single location
17844 expression for the entire function (see
17845 loc_list_has_views), so we have to split the
17846 range that straddles across partitions. */
17847 || !ZERO_VIEW_P (node->view))
17848 && current_function_decl)
17849 {
17850 endname = cfun->fde->dw_fde_end;
17851 endview = 0;
17852 range_across_switch = true;
17853 }
17854 /* The variable has a location between NODE->LABEL and
17855 NODE->NEXT->LABEL. */
17856 else if (node->next)
17857 endname = node->next->label, endview = node->next->view;
17858 /* If the variable has a location at the last label
17859 it keeps its location until the end of function. */
17860 else if (!current_function_decl)
17861 endname = text_end_label, endview = 0;
17862 else
17863 {
17864 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17865 current_function_funcdef_no);
17866 endname = ggc_strdup (label_id);
17867 endview = 0;
17868 }
17869
17870 *listp = new_loc_list (expr: descr, begin: node->label, vbegin: node->view,
17871 end: endname, vend: endview, section: secname);
17872 if (TREE_CODE (decl) == PARM_DECL
17873 && node == loc_list->first
17874 && NOTE_P (node->loc)
17875 && strcmp (s1: node->label, s2: endname) == 0)
17876 (*listp)->force = true;
17877 listp = &(*listp)->dw_loc_next;
17878 }
17879 }
17880
17881 if (cfun
17882 && crtl->has_bb_partition
17883 && node == loc_list->last_before_switch)
17884 {
17885 bool save_in_cold_section_p = in_cold_section_p;
17886 in_cold_section_p = !first_function_block_is_cold;
17887 secname = secname_for_decl (decl);
17888 in_cold_section_p = save_in_cold_section_p;
17889 }
17890
17891 if (range_across_switch)
17892 {
17893 if (GET_CODE (node->loc) == EXPR_LIST)
17894 descr = dw_sra_loc_expr (decl, loc: node->loc);
17895 else
17896 {
17897 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17898 varloc = NOTE_VAR_LOCATION (node->loc);
17899 descr = dw_loc_list_1 (loc: decl, varloc, want_address,
17900 initialized);
17901 }
17902 gcc_assert (descr);
17903 /* The variable has a location between NODE->LABEL and
17904 NODE->NEXT->LABEL. */
17905 if (node->next)
17906 endname = node->next->label, endview = node->next->view;
17907 else
17908 endname = cfun->fde->dw_fde_second_end, endview = 0;
17909 *listp = new_loc_list (expr: descr, cfun->fde->dw_fde_second_begin, vbegin: 0,
17910 end: endname, vend: endview, section: secname);
17911 listp = &(*listp)->dw_loc_next;
17912 }
17913 }
17914
17915 /* Try to avoid the overhead of a location list emitting a location
17916 expression instead, but only if we didn't have more than one
17917 location entry in the first place. If some entries were not
17918 representable, we don't want to pretend a single entry that was
17919 applies to the entire scope in which the variable is
17920 available. */
17921 if (list && loc_list->first->next)
17922 gen_llsym (list);
17923 else
17924 maybe_gen_llsym (list);
17925
17926 return list;
17927}
17928
17929/* Return true if the loc_list has only single element and thus
17930 can be represented as location description. */
17931
17932static bool
17933single_element_loc_list_p (dw_loc_list_ref list)
17934{
17935 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17936 return !list->ll_symbol;
17937}
17938
17939/* Duplicate a single element of location list. */
17940
17941static inline dw_loc_descr_ref
17942copy_loc_descr (dw_loc_descr_ref ref)
17943{
17944 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17945 memcpy (dest: copy, src: ref, n: sizeof (dw_loc_descr_node));
17946 return copy;
17947}
17948
17949/* To each location in list LIST append loc descr REF. */
17950
17951static void
17952add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17953{
17954 dw_loc_descr_ref copy;
17955 add_loc_descr (list_head: &list->expr, descr: ref);
17956 list = list->dw_loc_next;
17957 while (list)
17958 {
17959 copy = copy_loc_descr (ref);
17960 add_loc_descr (list_head: &list->expr, descr: copy);
17961 while (copy->dw_loc_next)
17962 copy = copy->dw_loc_next = copy_loc_descr (ref: copy->dw_loc_next);
17963 list = list->dw_loc_next;
17964 }
17965}
17966
17967/* To each location in list LIST prepend loc descr REF. */
17968
17969static void
17970prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17971{
17972 dw_loc_descr_ref copy;
17973 dw_loc_descr_ref ref_end = list->expr;
17974 add_loc_descr (list_head: &ref, descr: list->expr);
17975 list->expr = ref;
17976 list = list->dw_loc_next;
17977 while (list)
17978 {
17979 dw_loc_descr_ref end = list->expr;
17980 list->expr = copy = copy_loc_descr (ref);
17981 while (copy->dw_loc_next != ref_end)
17982 copy = copy->dw_loc_next = copy_loc_descr (ref: copy->dw_loc_next);
17983 copy->dw_loc_next = end;
17984 list = list->dw_loc_next;
17985 }
17986}
17987
17988/* Given two lists RET and LIST
17989 produce location list that is result of adding expression in LIST
17990 to expression in RET on each position in program.
17991 Might be destructive on both RET and LIST.
17992
17993 TODO: We handle only simple cases of RET or LIST having at most one
17994 element. General case would involve sorting the lists in program order
17995 and merging them that will need some additional work.
17996 Adding that will improve quality of debug info especially for SRA-ed
17997 structures. */
17998
17999static void
18000add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
18001{
18002 if (!list)
18003 return;
18004 if (!*ret)
18005 {
18006 *ret = list;
18007 return;
18008 }
18009 if (!list->dw_loc_next)
18010 {
18011 add_loc_descr_to_each (list: *ret, ref: list->expr);
18012 return;
18013 }
18014 if (!(*ret)->dw_loc_next)
18015 {
18016 prepend_loc_descr_to_each (list, ref: (*ret)->expr);
18017 *ret = list;
18018 return;
18019 }
18020 expansion_failed (NULL_TREE, NULL_RTX,
18021 reason: "Don't know how to merge two non-trivial"
18022 " location lists.\n");
18023 *ret = NULL;
18024 return;
18025}
18026
18027/* LOC is constant expression. Try a luck, look it up in constant
18028 pool and return its loc_descr of its address. */
18029
18030static dw_loc_descr_ref
18031cst_pool_loc_descr (tree loc)
18032{
18033 /* Get an RTL for this, if something has been emitted. */
18034 rtx rtl = lookup_constant_def (loc);
18035
18036 if (!rtl || !MEM_P (rtl))
18037 {
18038 gcc_assert (!rtl);
18039 return 0;
18040 }
18041 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18042
18043 /* TODO: We might get more coverage if we was actually delaying expansion
18044 of all expressions till end of compilation when constant pools are fully
18045 populated. */
18046 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18047 {
18048 expansion_failed (expr: loc, NULL_RTX,
18049 reason: "CST value in contant pool but not marked.");
18050 return 0;
18051 }
18052 return mem_loc_descriptor (XEXP (rtl, 0), mode: get_address_mode (mem: rtl),
18053 GET_MODE (rtl), initialized: VAR_INIT_STATUS_INITIALIZED);
18054}
18055
18056/* Return dw_loc_list representing address of addr_expr LOC
18057 by looking for inner INDIRECT_REF expression and turning
18058 it into simple arithmetics.
18059
18060 See loc_list_from_tree for the meaning of CONTEXT. */
18061
18062static dw_loc_list_ref
18063loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18064 loc_descr_context *context)
18065{
18066 tree obj, offset;
18067 poly_int64 bitsize, bitpos, bytepos;
18068 machine_mode mode;
18069 int unsignedp, reversep, volatilep = 0;
18070 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18071
18072 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18073 &bitsize, &bitpos, &offset, &mode,
18074 &unsignedp, &reversep, &volatilep);
18075 STRIP_NOPS (obj);
18076 if (!multiple_p (a: bitpos, BITS_PER_UNIT, multiple: &bytepos))
18077 {
18078 expansion_failed (expr: loc, NULL_RTX, reason: "bitfield access");
18079 return 0;
18080 }
18081 if (!INDIRECT_REF_P (obj))
18082 {
18083 expansion_failed (expr: obj,
18084 NULL_RTX, reason: "no indirect ref in inner refrence");
18085 return 0;
18086 }
18087 if (!offset && known_eq (bitpos, 0))
18088 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18089 context);
18090 else if (toplev
18091 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18092 && (dwarf_version >= 4 || !dwarf_strict))
18093 {
18094 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18095 if (!list_ret)
18096 return 0;
18097 if (offset)
18098 {
18099 /* Variable offset. */
18100 list_ret1 = loc_list_from_tree (offset, 0, context);
18101 if (list_ret1 == 0)
18102 return 0;
18103 add_loc_list (ret: &list_ret, list: list_ret1);
18104 if (!list_ret)
18105 return 0;
18106 add_loc_descr_to_each (list: list_ret,
18107 ref: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
18108 }
18109 HOST_WIDE_INT value;
18110 if (bytepos.is_constant (const_value: &value) && value > 0)
18111 add_loc_descr_to_each (list: list_ret,
18112 ref: new_loc_descr (op: DW_OP_plus_uconst, oprnd1: value, oprnd2: 0));
18113 else if (maybe_ne (a: bytepos, b: 0))
18114 loc_list_plus_const (list_head: list_ret, offset: bytepos);
18115 add_loc_descr_to_each (list: list_ret,
18116 ref: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
18117 }
18118 return list_ret;
18119}
18120
18121/* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18122 all operations from LOC are nops, move to the last one. Insert in NOPS all
18123 operations that are skipped. */
18124
18125static void
18126loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18127 hash_set<dw_loc_descr_ref> &nops)
18128{
18129 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18130 {
18131 nops.add (k: loc);
18132 loc = loc->dw_loc_next;
18133 }
18134}
18135
18136/* Helper for loc_descr_without_nops: free the location description operation
18137 P. */
18138
18139bool
18140free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18141{
18142 ggc_free (loc);
18143 return true;
18144}
18145
18146/* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18147 finishes LOC. */
18148
18149static void
18150loc_descr_without_nops (dw_loc_descr_ref &loc)
18151{
18152 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18153 return;
18154
18155 /* Set of all DW_OP_nop operations we remove. */
18156 hash_set<dw_loc_descr_ref> nops;
18157
18158 /* First, strip all prefix NOP operations in order to keep the head of the
18159 operations list. */
18160 loc_descr_to_next_no_nop (loc, nops);
18161
18162 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18163 {
18164 /* For control flow operations: strip "prefix" nops in destination
18165 labels. */
18166 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18167 loc_descr_to_next_no_nop (loc&: cur->dw_loc_oprnd1.v.val_loc, nops);
18168 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18169 loc_descr_to_next_no_nop (loc&: cur->dw_loc_oprnd2.v.val_loc, nops);
18170
18171 /* Do the same for the operations that follow, then move to the next
18172 iteration. */
18173 if (cur->dw_loc_next != NULL)
18174 loc_descr_to_next_no_nop (loc&: cur->dw_loc_next, nops);
18175 cur = cur->dw_loc_next;
18176 }
18177
18178 nops.traverse<void *, free_loc_descr> (NULL);
18179}
18180
18181
18182struct dwarf_procedure_info;
18183
18184/* Helper structure for location descriptions generation. */
18185struct loc_descr_context
18186{
18187 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18188 NULL_TREE if DW_OP_push_object_address in invalid for this location
18189 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18190 tree context_type;
18191 /* The ..._DECL node that should be translated as a
18192 DW_OP_push_object_address operation. */
18193 tree base_decl;
18194 /* Information about the DWARF procedure we are currently generating. NULL if
18195 we are not generating a DWARF procedure. */
18196 struct dwarf_procedure_info *dpi;
18197 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18198 by consumer. Used for DW_TAG_generic_subrange attributes. */
18199 bool placeholder_arg;
18200 /* True if PLACEHOLDER_EXPR has been seen. */
18201 bool placeholder_seen;
18202 /* True if strict preservation of signedness has been requested. */
18203 bool strict_signedness;
18204};
18205
18206/* DWARF procedures generation
18207
18208 DWARF expressions (aka. location descriptions) are used to encode variable
18209 things such as sizes or offsets. Such computations can have redundant parts
18210 that can be factorized in order to reduce the size of the output debug
18211 information. This is the whole point of DWARF procedures.
18212
18213 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18214 already factorized into functions ("size functions") in order to handle very
18215 big and complex types. Such functions are quite simple: they have integral
18216 arguments, they return an integral result and their body contains only a
18217 return statement with arithmetic expressions. This is the only kind of
18218 function we are interested in translating into DWARF procedures, here.
18219
18220 DWARF expressions and DWARF procedure are executed using a stack, so we have
18221 to define some calling convention for them to interact. Let's say that:
18222
18223 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18224 all arguments in reverse order (right-to-left) so that when the DWARF
18225 procedure execution starts, the first argument is the top of the stack.
18226
18227 - Then, when returning, the DWARF procedure must have consumed all arguments
18228 on the stack, must have pushed the result and touched nothing else.
18229
18230 - Each integral argument and the result are integral types can be hold in a
18231 single stack slot.
18232
18233 - We call "frame offset" the number of stack slots that are "under DWARF
18234 procedure control": it includes the arguments slots, the temporaries and
18235 the result slot. Thus, it is equal to the number of arguments when the
18236 procedure execution starts and must be equal to one (the result) when it
18237 returns. */
18238
18239/* Helper structure used when generating operations for a DWARF procedure. */
18240struct dwarf_procedure_info
18241{
18242 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18243 currently translated. */
18244 tree fndecl;
18245 /* The number of arguments FNDECL takes. */
18246 unsigned args_count;
18247};
18248
18249/* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18250 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18251 equate it to this DIE. */
18252
18253static dw_die_ref
18254new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18255 dw_die_ref parent_die)
18256{
18257 dw_die_ref dwarf_proc_die;
18258
18259 if ((dwarf_version < 3 && dwarf_strict)
18260 || location == NULL)
18261 return NULL;
18262
18263 dwarf_proc_die = new_die (tag_value: DW_TAG_dwarf_procedure, parent_die, t: fndecl);
18264 if (fndecl)
18265 equate_decl_number_to_die (decl: fndecl, decl_die: dwarf_proc_die);
18266 add_AT_loc (die: dwarf_proc_die, attr_kind: DW_AT_location, loc: location);
18267 return dwarf_proc_die;
18268}
18269
18270/* Return whether TYPE is a supported type as a DWARF procedure argument
18271 type or return type (we handle only scalar types and pointer types that
18272 aren't wider than the DWARF expression evaluation stack). */
18273
18274static bool
18275is_handled_procedure_type (tree type)
18276{
18277 return ((INTEGRAL_TYPE_P (type)
18278 || TREE_CODE (type) == OFFSET_TYPE
18279 || TREE_CODE (type) == POINTER_TYPE)
18280 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18281}
18282
18283/* Helper for resolve_args_picking: do the same but stop when coming across
18284 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18285 offset *before* evaluating the corresponding operation. */
18286
18287static bool
18288resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18289 struct dwarf_procedure_info *dpi,
18290 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18291{
18292 /* The "frame_offset" identifier is already used to name a macro... */
18293 unsigned frame_offset_ = initial_frame_offset;
18294 dw_loc_descr_ref l;
18295
18296 for (l = loc; l != NULL;)
18297 {
18298 bool existed;
18299 unsigned &l_frame_offset = frame_offsets.get_or_insert (k: l, existed: &existed);
18300
18301 /* If we already met this node, there is nothing to compute anymore. */
18302 if (existed)
18303 {
18304 /* Make sure that the stack size is consistent wherever the execution
18305 flow comes from. */
18306 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18307 break;
18308 }
18309 l_frame_offset = frame_offset_;
18310
18311 /* If needed, relocate the picking offset with respect to the frame
18312 offset. */
18313 if (l->dw_loc_frame_offset_rel)
18314 {
18315 unsigned HOST_WIDE_INT off;
18316 switch (l->dw_loc_opc)
18317 {
18318 case DW_OP_pick:
18319 off = l->dw_loc_oprnd1.v.val_unsigned;
18320 break;
18321 case DW_OP_dup:
18322 off = 0;
18323 break;
18324 case DW_OP_over:
18325 off = 1;
18326 break;
18327 default:
18328 gcc_unreachable ();
18329 }
18330 /* frame_offset_ is the size of the current stack frame, including
18331 incoming arguments. Besides, the arguments are pushed
18332 right-to-left. Thus, in order to access the Nth argument from
18333 this operation node, the picking has to skip temporaries *plus*
18334 one stack slot per argument (0 for the first one, 1 for the second
18335 one, etc.).
18336
18337 The targetted argument number (N) is already set as the operand,
18338 and the number of temporaries can be computed with:
18339 frame_offsets_ - dpi->args_count */
18340 off += frame_offset_ - dpi->args_count;
18341
18342 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18343 if (off > 255)
18344 return false;
18345
18346 if (off == 0)
18347 {
18348 l->dw_loc_opc = DW_OP_dup;
18349 l->dw_loc_oprnd1.v.val_unsigned = 0;
18350 }
18351 else if (off == 1)
18352 {
18353 l->dw_loc_opc = DW_OP_over;
18354 l->dw_loc_oprnd1.v.val_unsigned = 0;
18355 }
18356 else
18357 {
18358 l->dw_loc_opc = DW_OP_pick;
18359 l->dw_loc_oprnd1.v.val_unsigned = off;
18360 }
18361 }
18362
18363 /* Update frame_offset according to the effect the current operation has
18364 on the stack. */
18365 switch (l->dw_loc_opc)
18366 {
18367 case DW_OP_deref:
18368 case DW_OP_swap:
18369 case DW_OP_rot:
18370 case DW_OP_abs:
18371 case DW_OP_neg:
18372 case DW_OP_not:
18373 case DW_OP_plus_uconst:
18374 case DW_OP_skip:
18375 case DW_OP_reg0:
18376 case DW_OP_reg1:
18377 case DW_OP_reg2:
18378 case DW_OP_reg3:
18379 case DW_OP_reg4:
18380 case DW_OP_reg5:
18381 case DW_OP_reg6:
18382 case DW_OP_reg7:
18383 case DW_OP_reg8:
18384 case DW_OP_reg9:
18385 case DW_OP_reg10:
18386 case DW_OP_reg11:
18387 case DW_OP_reg12:
18388 case DW_OP_reg13:
18389 case DW_OP_reg14:
18390 case DW_OP_reg15:
18391 case DW_OP_reg16:
18392 case DW_OP_reg17:
18393 case DW_OP_reg18:
18394 case DW_OP_reg19:
18395 case DW_OP_reg20:
18396 case DW_OP_reg21:
18397 case DW_OP_reg22:
18398 case DW_OP_reg23:
18399 case DW_OP_reg24:
18400 case DW_OP_reg25:
18401 case DW_OP_reg26:
18402 case DW_OP_reg27:
18403 case DW_OP_reg28:
18404 case DW_OP_reg29:
18405 case DW_OP_reg30:
18406 case DW_OP_reg31:
18407 case DW_OP_bregx:
18408 case DW_OP_piece:
18409 case DW_OP_deref_size:
18410 case DW_OP_nop:
18411 case DW_OP_bit_piece:
18412 case DW_OP_implicit_value:
18413 case DW_OP_stack_value:
18414 case DW_OP_deref_type:
18415 case DW_OP_convert:
18416 case DW_OP_reinterpret:
18417 case DW_OP_GNU_deref_type:
18418 case DW_OP_GNU_convert:
18419 case DW_OP_GNU_reinterpret:
18420 break;
18421
18422 case DW_OP_addr:
18423 case DW_OP_const1u:
18424 case DW_OP_const1s:
18425 case DW_OP_const2u:
18426 case DW_OP_const2s:
18427 case DW_OP_const4u:
18428 case DW_OP_const4s:
18429 case DW_OP_const8u:
18430 case DW_OP_const8s:
18431 case DW_OP_constu:
18432 case DW_OP_consts:
18433 case DW_OP_dup:
18434 case DW_OP_over:
18435 case DW_OP_pick:
18436 case DW_OP_lit0:
18437 case DW_OP_lit1:
18438 case DW_OP_lit2:
18439 case DW_OP_lit3:
18440 case DW_OP_lit4:
18441 case DW_OP_lit5:
18442 case DW_OP_lit6:
18443 case DW_OP_lit7:
18444 case DW_OP_lit8:
18445 case DW_OP_lit9:
18446 case DW_OP_lit10:
18447 case DW_OP_lit11:
18448 case DW_OP_lit12:
18449 case DW_OP_lit13:
18450 case DW_OP_lit14:
18451 case DW_OP_lit15:
18452 case DW_OP_lit16:
18453 case DW_OP_lit17:
18454 case DW_OP_lit18:
18455 case DW_OP_lit19:
18456 case DW_OP_lit20:
18457 case DW_OP_lit21:
18458 case DW_OP_lit22:
18459 case DW_OP_lit23:
18460 case DW_OP_lit24:
18461 case DW_OP_lit25:
18462 case DW_OP_lit26:
18463 case DW_OP_lit27:
18464 case DW_OP_lit28:
18465 case DW_OP_lit29:
18466 case DW_OP_lit30:
18467 case DW_OP_lit31:
18468 case DW_OP_breg0:
18469 case DW_OP_breg1:
18470 case DW_OP_breg2:
18471 case DW_OP_breg3:
18472 case DW_OP_breg4:
18473 case DW_OP_breg5:
18474 case DW_OP_breg6:
18475 case DW_OP_breg7:
18476 case DW_OP_breg8:
18477 case DW_OP_breg9:
18478 case DW_OP_breg10:
18479 case DW_OP_breg11:
18480 case DW_OP_breg12:
18481 case DW_OP_breg13:
18482 case DW_OP_breg14:
18483 case DW_OP_breg15:
18484 case DW_OP_breg16:
18485 case DW_OP_breg17:
18486 case DW_OP_breg18:
18487 case DW_OP_breg19:
18488 case DW_OP_breg20:
18489 case DW_OP_breg21:
18490 case DW_OP_breg22:
18491 case DW_OP_breg23:
18492 case DW_OP_breg24:
18493 case DW_OP_breg25:
18494 case DW_OP_breg26:
18495 case DW_OP_breg27:
18496 case DW_OP_breg28:
18497 case DW_OP_breg29:
18498 case DW_OP_breg30:
18499 case DW_OP_breg31:
18500 case DW_OP_fbreg:
18501 case DW_OP_push_object_address:
18502 case DW_OP_call_frame_cfa:
18503 case DW_OP_GNU_variable_value:
18504 case DW_OP_GNU_addr_index:
18505 case DW_OP_GNU_const_index:
18506 ++frame_offset_;
18507 break;
18508
18509 case DW_OP_drop:
18510 case DW_OP_xderef:
18511 case DW_OP_and:
18512 case DW_OP_div:
18513 case DW_OP_minus:
18514 case DW_OP_mod:
18515 case DW_OP_mul:
18516 case DW_OP_or:
18517 case DW_OP_plus:
18518 case DW_OP_shl:
18519 case DW_OP_shr:
18520 case DW_OP_shra:
18521 case DW_OP_xor:
18522 case DW_OP_bra:
18523 case DW_OP_eq:
18524 case DW_OP_ge:
18525 case DW_OP_gt:
18526 case DW_OP_le:
18527 case DW_OP_lt:
18528 case DW_OP_ne:
18529 case DW_OP_regx:
18530 case DW_OP_xderef_size:
18531 --frame_offset_;
18532 break;
18533
18534 case DW_OP_call2:
18535 case DW_OP_call4:
18536 case DW_OP_call_ref:
18537 {
18538 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18539 int *stack_usage = dwarf_proc_stack_usage_map->get (k: dwarf_proc);
18540
18541 if (stack_usage == NULL)
18542 return false;
18543 frame_offset_ += *stack_usage;
18544 break;
18545 }
18546
18547 case DW_OP_implicit_pointer:
18548 case DW_OP_entry_value:
18549 case DW_OP_const_type:
18550 case DW_OP_regval_type:
18551 case DW_OP_form_tls_address:
18552 case DW_OP_GNU_push_tls_address:
18553 case DW_OP_GNU_uninit:
18554 case DW_OP_GNU_encoded_addr:
18555 case DW_OP_GNU_implicit_pointer:
18556 case DW_OP_GNU_entry_value:
18557 case DW_OP_GNU_const_type:
18558 case DW_OP_GNU_regval_type:
18559 case DW_OP_GNU_parameter_ref:
18560 /* loc_list_from_tree will probably not output these operations for
18561 size functions, so assume they will not appear here. */
18562 /* Fall through... */
18563
18564 default:
18565 gcc_unreachable ();
18566 }
18567
18568 /* Now, follow the control flow (except subroutine calls). */
18569 switch (l->dw_loc_opc)
18570 {
18571 case DW_OP_bra:
18572 if (!resolve_args_picking_1 (loc: l->dw_loc_next, initial_frame_offset: frame_offset_, dpi,
18573 frame_offsets))
18574 return false;
18575 /* Fall through. */
18576
18577 case DW_OP_skip:
18578 l = l->dw_loc_oprnd1.v.val_loc;
18579 break;
18580
18581 case DW_OP_stack_value:
18582 return true;
18583
18584 default:
18585 l = l->dw_loc_next;
18586 break;
18587 }
18588 }
18589
18590 return true;
18591}
18592
18593/* Make a DFS over operations reachable through LOC (i.e. follow branch
18594 operations) in order to resolve the operand of DW_OP_pick operations that
18595 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18596 offset *before* LOC is executed. Return if all relocations were
18597 successful. */
18598
18599static bool
18600resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18601 struct dwarf_procedure_info *dpi)
18602{
18603 /* Associate to all visited operations the frame offset *before* evaluating
18604 this operation. */
18605 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18606
18607 return
18608 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18609}
18610
18611/* Try to generate a DWARF procedure that computes the same result as FNDECL.
18612 Return NULL if it is not possible. */
18613
18614static dw_die_ref
18615function_to_dwarf_procedure (tree fndecl)
18616{
18617 struct dwarf_procedure_info dpi;
18618 struct loc_descr_context ctx = {
18619 NULL_TREE, /* context_type */
18620 NULL_TREE, /* base_decl */
18621 .dpi: &dpi, /* dpi */
18622 .placeholder_arg: false, /* placeholder_arg */
18623 .placeholder_seen: false, /* placeholder_seen */
18624 .strict_signedness: true /* strict_signedness */
18625 };
18626 dw_die_ref dwarf_proc_die;
18627 tree tree_body = DECL_SAVED_TREE (fndecl);
18628 dw_loc_descr_ref loc_body, epilogue;
18629
18630 tree cursor;
18631 unsigned i;
18632
18633 /* Do not generate multiple DWARF procedures for the same function
18634 declaration. */
18635 dwarf_proc_die = lookup_decl_die (decl: fndecl);
18636 if (dwarf_proc_die != NULL)
18637 return dwarf_proc_die;
18638
18639 /* DWARF procedures are available starting with the DWARFv3 standard. */
18640 if (dwarf_version < 3 && dwarf_strict)
18641 return NULL;
18642
18643 /* We handle only functions for which we still have a body, that return a
18644 supported type and that takes arguments with supported types. Note that
18645 there is no point translating functions that return nothing. */
18646 if (tree_body == NULL_TREE
18647 || DECL_RESULT (fndecl) == NULL_TREE
18648 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18649 return NULL;
18650
18651 for (cursor = DECL_ARGUMENTS (fndecl);
18652 cursor != NULL_TREE;
18653 cursor = TREE_CHAIN (cursor))
18654 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18655 return NULL;
18656
18657 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18658 if (TREE_CODE (tree_body) != RETURN_EXPR)
18659 return NULL;
18660 tree_body = TREE_OPERAND (tree_body, 0);
18661 if (TREE_CODE (tree_body) != MODIFY_EXPR
18662 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18663 return NULL;
18664 tree_body = TREE_OPERAND (tree_body, 1);
18665
18666 /* Try to translate the body expression itself. Note that this will probably
18667 cause an infinite recursion if its call graph has a cycle. This is very
18668 unlikely for size functions, however, so don't bother with such things at
18669 the moment. */
18670 dpi.fndecl = fndecl;
18671 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18672 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18673 if (!loc_body)
18674 return NULL;
18675
18676 /* After evaluating all operands in "loc_body", we should still have on the
18677 stack all arguments plus the desired function result (top of the stack).
18678 Generate code in order to keep only the result in our stack frame. */
18679 epilogue = NULL;
18680 for (i = 0; i < dpi.args_count; ++i)
18681 {
18682 dw_loc_descr_ref op_couple = new_loc_descr (op: DW_OP_swap, oprnd1: 0, oprnd2: 0);
18683 op_couple->dw_loc_next = new_loc_descr (op: DW_OP_drop, oprnd1: 0, oprnd2: 0);
18684 op_couple->dw_loc_next->dw_loc_next = epilogue;
18685 epilogue = op_couple;
18686 }
18687 add_loc_descr (list_head: &loc_body, descr: epilogue);
18688 if (!resolve_args_picking (loc: loc_body, initial_frame_offset: dpi.args_count, dpi: &dpi))
18689 return NULL;
18690
18691 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18692 because they are considered useful. Now there is an epilogue, they are
18693 not anymore, so give it another try. */
18694 loc_descr_without_nops (loc&: loc_body);
18695
18696 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18697 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18698 though, given that size functions do not come from source, so they should
18699 not have a dedicated DW_TAG_subprogram DIE. */
18700 dwarf_proc_die
18701 = new_dwarf_proc_die (location: loc_body, fndecl,
18702 parent_die: get_context_die (DECL_CONTEXT (fndecl)));
18703
18704 /* The called DWARF procedure consumes one stack slot per argument and
18705 returns one stack slot. */
18706 dwarf_proc_stack_usage_map->put (k: dwarf_proc_die, v: 1 - dpi.args_count);
18707
18708 return dwarf_proc_die;
18709}
18710
18711/* Helper function for loc_list_from_tree. Perform OP binary op,
18712 but after converting arguments to type_die, afterwards convert
18713 back to unsigned. */
18714
18715static dw_loc_list_ref
18716typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18717 dw_die_ref type_die, scalar_int_mode mode,
18718 struct loc_descr_context *context)
18719{
18720 dw_loc_list_ref op0, op1;
18721 dw_loc_descr_ref cvt, binop;
18722
18723 if (type_die == NULL)
18724 return NULL;
18725
18726 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18727 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18728 if (op0 == NULL || op1 == NULL)
18729 return NULL;
18730
18731 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
18732 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18733 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18734 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18735 add_loc_descr_to_each (list: op0, ref: cvt);
18736
18737 cvt = new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0);
18738 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18739 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18740 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18741 add_loc_descr_to_each (list: op1, ref: cvt);
18742
18743 add_loc_list (ret: &op0, list: op1);
18744 if (op0 == NULL)
18745 return NULL;
18746
18747 binop = new_loc_descr (op, oprnd1: 0, oprnd2: 0);
18748 convert_descriptor_to_mode (mode, op: binop);
18749 add_loc_descr_to_each (list: op0, ref: binop);
18750
18751 return op0;
18752}
18753
18754/* Generate Dwarf location list representing LOC.
18755 If WANT_ADDRESS is false, expression computing LOC will be computed
18756 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18757 if WANT_ADDRESS is 2, expression computing address useable in location
18758 will be returned (i.e. DW_OP_reg can be used
18759 to refer to register values).
18760
18761 CONTEXT provides information to customize the location descriptions
18762 generation. Its context_type field specifies what type is implicitly
18763 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18764 will not be generated.
18765
18766 Its DPI field determines whether we are generating a DWARF expression for a
18767 DWARF procedure, so PARM_DECL references are processed specifically.
18768
18769 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18770 and dpi fields were null. */
18771
18772static dw_loc_list_ref
18773loc_list_from_tree_1 (tree loc, int want_address,
18774 struct loc_descr_context *context)
18775{
18776 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18777 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18778 int have_address = 0;
18779 enum dwarf_location_atom op;
18780
18781 /* ??? Most of the time we do not take proper care for sign/zero
18782 extending the values properly. Hopefully this won't be a real
18783 problem... */
18784
18785 if (context != NULL
18786 && context->base_decl == loc
18787 && want_address == 0)
18788 {
18789 if (dwarf_version >= 3 || !dwarf_strict)
18790 return new_loc_list (expr: new_loc_descr (op: DW_OP_push_object_address, oprnd1: 0, oprnd2: 0),
18791 NULL, vbegin: 0, NULL, vend: 0, NULL);
18792 else
18793 return NULL;
18794 }
18795
18796 switch (TREE_CODE (loc))
18797 {
18798 case ERROR_MARK:
18799 expansion_failed (expr: loc, NULL_RTX, reason: "ERROR_MARK");
18800 return 0;
18801
18802 case PLACEHOLDER_EXPR:
18803 /* This case involves extracting fields from an object to determine the
18804 position of other fields. It is supposed to appear only as the first
18805 operand of COMPONENT_REF nodes and to reference precisely the type
18806 that the context allows or its enclosing type. */
18807 if (context != NULL
18808 && (TREE_TYPE (loc) == context->context_type
18809 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18810 && want_address >= 1)
18811 {
18812 if (dwarf_version >= 3 || !dwarf_strict)
18813 {
18814 ret = new_loc_descr (op: DW_OP_push_object_address, oprnd1: 0, oprnd2: 0);
18815 have_address = 1;
18816 break;
18817 }
18818 else
18819 return NULL;
18820 }
18821 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18822 the single argument passed by consumer. */
18823 else if (context != NULL
18824 && context->placeholder_arg
18825 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18826 && want_address == 0)
18827 {
18828 ret = new_loc_descr (op: DW_OP_pick, oprnd1: 0, oprnd2: 0);
18829 ret->dw_loc_frame_offset_rel = 1;
18830 context->placeholder_seen = true;
18831 break;
18832 }
18833 else
18834 expansion_failed (expr: loc, NULL_RTX,
18835 reason: "PLACEHOLDER_EXPR for an unexpected type");
18836 break;
18837
18838 case CALL_EXPR:
18839 {
18840 tree callee = get_callee_fndecl (loc);
18841 dw_die_ref dwarf_proc;
18842
18843 if (callee
18844 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18845 && (dwarf_proc = function_to_dwarf_procedure (fndecl: callee)))
18846 {
18847 /* DWARF procedures are used for size functions, which are built
18848 when size expressions contain conditional constructs, so we
18849 request strict preservation of signedness for comparisons. */
18850 bool old_strict_signedness;
18851 if (context)
18852 {
18853 old_strict_signedness = context->strict_signedness;
18854 context->strict_signedness = true;
18855 }
18856
18857 /* Evaluate arguments right-to-left so that the first argument
18858 will be the top-most one on the stack. */
18859 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18860 {
18861 tree arg = CALL_EXPR_ARG (loc, i);
18862 ret1 = loc_descriptor_from_tree (arg, 0, context);
18863 if (!ret1)
18864 {
18865 expansion_failed (expr: arg, NULL_RTX, reason: "CALL_EXPR argument");
18866 return NULL;
18867 }
18868 add_loc_descr (list_head: &ret, descr: ret1);
18869 }
18870
18871 ret1 = new_loc_descr (op: DW_OP_call4, oprnd1: 0, oprnd2: 0);
18872 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18873 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18874 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18875 add_loc_descr (list_head: &ret, descr: ret1);
18876 if (context)
18877 context->strict_signedness = old_strict_signedness;
18878 }
18879 else
18880 expansion_failed (expr: loc, NULL_RTX, reason: "CALL_EXPR target");
18881 break;
18882 }
18883
18884 case PREINCREMENT_EXPR:
18885 case PREDECREMENT_EXPR:
18886 case POSTINCREMENT_EXPR:
18887 case POSTDECREMENT_EXPR:
18888 expansion_failed (expr: loc, NULL_RTX, reason: "PRE/POST INDCREMENT/DECREMENT");
18889 /* There are no opcodes for these operations. */
18890 return 0;
18891
18892 case ADDR_EXPR:
18893 /* If we already want an address, see if there is INDIRECT_REF inside
18894 e.g. for &this->field. */
18895 if (want_address)
18896 {
18897 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18898 (loc, toplev: want_address == 2, context);
18899 if (list_ret)
18900 have_address = 1;
18901 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18902 && (ret = cst_pool_loc_descr (loc)))
18903 have_address = 1;
18904 }
18905 /* Otherwise, process the argument and look for the address. */
18906 if (!list_ret && !ret)
18907 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 1, context);
18908 else
18909 {
18910 if (want_address)
18911 expansion_failed (expr: loc, NULL_RTX, reason: "need address of ADDR_EXPR");
18912 return NULL;
18913 }
18914 break;
18915
18916 case VAR_DECL:
18917 if (DECL_THREAD_LOCAL_P (loc))
18918 {
18919 rtx rtl;
18920 enum dwarf_location_atom tls_op;
18921 enum dtprel_bool dtprel = dtprel_false;
18922
18923 if (targetm.have_tls)
18924 {
18925 /* If this is not defined, we have no way to emit the
18926 data. */
18927 if (!targetm.asm_out.output_dwarf_dtprel)
18928 return 0;
18929
18930 /* The way DW_OP_GNU_push_tls_address is specified, we
18931 can only look up addresses of objects in the current
18932 module. We used DW_OP_addr as first op, but that's
18933 wrong, because DW_OP_addr is relocated by the debug
18934 info consumer, while DW_OP_GNU_push_tls_address
18935 operand shouldn't be. */
18936 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18937 return 0;
18938 dtprel = dtprel_true;
18939 /* We check for DWARF 5 here because gdb did not implement
18940 DW_OP_form_tls_address until after 7.12. */
18941 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18942 : DW_OP_GNU_push_tls_address);
18943 }
18944 else
18945 {
18946 if (!targetm.emutls.debug_form_tls_address
18947 || !(dwarf_version >= 3 || !dwarf_strict))
18948 return 0;
18949 /* We stuffed the control variable into the DECL_VALUE_EXPR
18950 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18951 no longer appear in gimple code. We used the control
18952 variable in specific so that we could pick it up here. */
18953 loc = DECL_VALUE_EXPR (loc);
18954 tls_op = DW_OP_form_tls_address;
18955 }
18956
18957 rtl = rtl_for_decl_location (loc);
18958 if (rtl == NULL_RTX)
18959 return 0;
18960
18961 if (!MEM_P (rtl))
18962 return 0;
18963 rtl = XEXP (rtl, 0);
18964 if (! CONSTANT_P (rtl))
18965 return 0;
18966
18967 ret = new_addr_loc_descr (addr: rtl, dtprel);
18968 ret1 = new_loc_descr (op: tls_op, oprnd1: 0, oprnd2: 0);
18969 add_loc_descr (list_head: &ret, descr: ret1);
18970
18971 have_address = 1;
18972 break;
18973 }
18974 /* FALLTHRU */
18975
18976 case PARM_DECL:
18977 if (context != NULL && context->dpi != NULL
18978 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18979 {
18980 /* We are generating code for a DWARF procedure and we want to access
18981 one of its arguments: find the appropriate argument offset and let
18982 the resolve_args_picking pass compute the offset that complies
18983 with the stack frame size. */
18984 unsigned i = 0;
18985 tree cursor;
18986
18987 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18988 cursor != NULL_TREE && cursor != loc;
18989 cursor = TREE_CHAIN (cursor), ++i)
18990 ;
18991 /* If we are translating a DWARF procedure, all referenced parameters
18992 must belong to the current function. */
18993 gcc_assert (cursor != NULL_TREE);
18994
18995 ret = new_loc_descr (op: DW_OP_pick, oprnd1: i, oprnd2: 0);
18996 ret->dw_loc_frame_offset_rel = 1;
18997 break;
18998 }
18999 /* FALLTHRU */
19000
19001 case RESULT_DECL:
19002 if (DECL_HAS_VALUE_EXPR_P (loc))
19003 {
19004 tree value_expr = DECL_VALUE_EXPR (loc);
19005
19006 /* Non-local frame structures are DECL_IGNORED_P variables so we need
19007 to wait until they get an RTX in order to reference them. */
19008 if (early_dwarf
19009 && TREE_CODE (value_expr) == COMPONENT_REF
19010 && VAR_P (TREE_OPERAND (value_expr, 0))
19011 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
19012 ;
19013 else
19014 return loc_list_from_tree_1 (loc: value_expr, want_address, context);
19015 }
19016
19017 /* FALLTHRU */
19018
19019 case FUNCTION_DECL:
19020 {
19021 rtx rtl;
19022 var_loc_list *loc_list = lookup_decl_loc (decl: loc);
19023
19024 if (loc_list && loc_list->first)
19025 {
19026 list_ret = dw_loc_list (loc_list, decl: loc, want_address);
19027 have_address = want_address != 0;
19028 break;
19029 }
19030 rtl = rtl_for_decl_location (loc);
19031 if (rtl == NULL_RTX)
19032 {
19033 if (TREE_CODE (loc) != FUNCTION_DECL
19034 && early_dwarf
19035 && want_address != 1
19036 && ! DECL_IGNORED_P (loc)
19037 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19038 || POINTER_TYPE_P (TREE_TYPE (loc)))
19039 && TYPE_MODE (TREE_TYPE (loc)) != BLKmode
19040 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19041 <= DWARF2_ADDR_SIZE))
19042 {
19043 dw_die_ref ref = lookup_decl_die (decl: loc);
19044 if (ref)
19045 {
19046 ret = new_loc_descr (op: DW_OP_GNU_variable_value, oprnd1: 0, oprnd2: 0);
19047 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19048 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19049 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19050 }
19051 else if (current_function_decl
19052 && DECL_CONTEXT (loc) == current_function_decl)
19053 {
19054 ret = new_loc_descr (op: DW_OP_GNU_variable_value, oprnd1: 0, oprnd2: 0);
19055 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19056 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19057 }
19058 break;
19059 }
19060 expansion_failed (expr: loc, NULL_RTX, reason: "DECL has no RTL");
19061 return 0;
19062 }
19063 else if (CONST_INT_P (rtl))
19064 {
19065 HOST_WIDE_INT val = INTVAL (rtl);
19066 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19067 val &= GET_MODE_MASK (DECL_MODE (loc));
19068 ret = int_loc_descriptor (poly_i: val);
19069 }
19070 else if (GET_CODE (rtl) == CONST_STRING)
19071 {
19072 expansion_failed (expr: loc, NULL_RTX, reason: "CONST_STRING");
19073 return 0;
19074 }
19075 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19076 ret = new_addr_loc_descr (addr: rtl, dtprel: dtprel_false);
19077 else
19078 {
19079 machine_mode mode, mem_mode;
19080
19081 /* Certain constructs can only be represented at top-level. */
19082 if (want_address == 2)
19083 {
19084 ret = loc_descriptor (rtl, VOIDmode,
19085 initialized: VAR_INIT_STATUS_INITIALIZED);
19086 have_address = 1;
19087 }
19088 else
19089 {
19090 mode = GET_MODE (rtl);
19091 mem_mode = VOIDmode;
19092 if (MEM_P (rtl))
19093 {
19094 mem_mode = mode;
19095 mode = get_address_mode (mem: rtl);
19096 rtl = XEXP (rtl, 0);
19097 have_address = 1;
19098 }
19099 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19100 initialized: VAR_INIT_STATUS_INITIALIZED);
19101 }
19102 if (!ret)
19103 expansion_failed (expr: loc, rtl,
19104 reason: "failed to produce loc descriptor for rtl");
19105 }
19106 }
19107 break;
19108
19109 case MEM_REF:
19110 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19111 {
19112 have_address = 1;
19113 goto do_plus;
19114 }
19115 /* Fallthru. */
19116 case INDIRECT_REF:
19117 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19118 have_address = 1;
19119 break;
19120
19121 case TARGET_MEM_REF:
19122 case SSA_NAME:
19123 case DEBUG_EXPR_DECL:
19124 return NULL;
19125
19126 case COMPOUND_EXPR:
19127 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19128 context);
19129
19130 CASE_CONVERT:
19131 case VIEW_CONVERT_EXPR:
19132 case SAVE_EXPR:
19133 case MODIFY_EXPR:
19134 case NON_LVALUE_EXPR:
19135 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19136 context);
19137
19138 case COMPONENT_REF:
19139 case BIT_FIELD_REF:
19140 case ARRAY_REF:
19141 case ARRAY_RANGE_REF:
19142 case REALPART_EXPR:
19143 case IMAGPART_EXPR:
19144 {
19145 tree obj, offset;
19146 poly_int64 bitsize, bitpos, bytepos;
19147 machine_mode mode;
19148 int unsignedp, reversep, volatilep = 0;
19149
19150 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19151 &unsignedp, &reversep, &volatilep);
19152
19153 gcc_assert (obj != loc);
19154
19155 list_ret = loc_list_from_tree_1 (loc: obj,
19156 want_address: want_address == 2
19157 && known_eq (bitpos, 0)
19158 && !offset ? 2 : 1,
19159 context);
19160 /* TODO: We can extract value of the small expression via shifting even
19161 for nonzero bitpos. */
19162 if (list_ret == 0)
19163 return 0;
19164 if (!multiple_p (a: bitpos, BITS_PER_UNIT, multiple: &bytepos))
19165 {
19166 expansion_failed (expr: loc, NULL_RTX, reason: "bitfield access");
19167 return 0;
19168 }
19169
19170 if (offset != NULL_TREE)
19171 {
19172 /* Variable offset. */
19173 list_ret1 = loc_list_from_tree_1 (loc: offset, want_address: 0, context);
19174 if (list_ret1 == 0)
19175 return 0;
19176 add_loc_list (ret: &list_ret, list: list_ret1);
19177 if (!list_ret)
19178 return 0;
19179 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
19180 }
19181
19182 HOST_WIDE_INT value;
19183 if (bytepos.is_constant (const_value: &value) && value > 0)
19184 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_plus_uconst,
19185 oprnd1: value, oprnd2: 0));
19186 else if (maybe_ne (a: bytepos, b: 0))
19187 loc_list_plus_const (list_head: list_ret, offset: bytepos);
19188
19189 have_address = 1;
19190 break;
19191 }
19192
19193 case INTEGER_CST:
19194 if ((want_address || !tree_fits_shwi_p (loc))
19195 && (ret = cst_pool_loc_descr (loc)))
19196 have_address = 1;
19197 else if (want_address == 2
19198 && tree_fits_shwi_p (loc)
19199 && (ret = address_of_int_loc_descriptor
19200 (size: int_size_in_bytes (TREE_TYPE (loc)),
19201 i: tree_to_shwi (loc))))
19202 have_address = 1;
19203 else if (tree_fits_shwi_p (loc))
19204 ret = int_loc_descriptor (poly_i: tree_to_shwi (loc));
19205 else if (tree_fits_uhwi_p (loc))
19206 ret = uint_loc_descriptor (i: tree_to_uhwi (loc));
19207 else
19208 {
19209 expansion_failed (expr: loc, NULL_RTX,
19210 reason: "Integer operand is not host integer");
19211 return 0;
19212 }
19213 break;
19214
19215 case POLY_INT_CST:
19216 {
19217 if (want_address)
19218 {
19219 expansion_failed (expr: loc, NULL_RTX,
19220 reason: "constant address with a runtime component");
19221 return 0;
19222 }
19223 poly_int64 value;
19224 if (!poly_int_tree_p (t: loc, value: &value))
19225 {
19226 expansion_failed (expr: loc, NULL_RTX, reason: "constant too big");
19227 return 0;
19228 }
19229 ret = int_loc_descriptor (poly_i: value);
19230 }
19231 break;
19232
19233 case CONSTRUCTOR:
19234 case REAL_CST:
19235 case STRING_CST:
19236 case COMPLEX_CST:
19237 if ((ret = cst_pool_loc_descr (loc)))
19238 have_address = 1;
19239 else if (TREE_CODE (loc) == CONSTRUCTOR)
19240 {
19241 tree type = TREE_TYPE (loc);
19242 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19243 unsigned HOST_WIDE_INT offset = 0;
19244 unsigned HOST_WIDE_INT cnt;
19245 constructor_elt *ce;
19246
19247 if (TREE_CODE (type) == RECORD_TYPE)
19248 {
19249 /* This is very limited, but it's enough to output
19250 pointers to member functions, as long as the
19251 referenced function is defined in the current
19252 translation unit. */
19253 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19254 {
19255 tree val = ce->value;
19256
19257 tree field = ce->index;
19258
19259 if (val)
19260 STRIP_NOPS (val);
19261
19262 if (!field || DECL_BIT_FIELD (field))
19263 {
19264 expansion_failed (expr: loc, NULL_RTX,
19265 reason: "bitfield in record type constructor");
19266 size = offset = (unsigned HOST_WIDE_INT)-1;
19267 ret = NULL;
19268 break;
19269 }
19270
19271 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19272 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19273 gcc_assert (pos + fieldsize <= size);
19274 if (pos < offset)
19275 {
19276 expansion_failed (expr: loc, NULL_RTX,
19277 reason: "out-of-order fields in record constructor");
19278 size = offset = (unsigned HOST_WIDE_INT)-1;
19279 ret = NULL;
19280 break;
19281 }
19282 if (pos > offset)
19283 {
19284 ret1 = new_loc_descr (op: DW_OP_piece, oprnd1: pos - offset, oprnd2: 0);
19285 add_loc_descr (list_head: &ret, descr: ret1);
19286 offset = pos;
19287 }
19288 if (val && fieldsize != 0)
19289 {
19290 ret1 = loc_descriptor_from_tree (val, want_address, context);
19291 if (!ret1)
19292 {
19293 expansion_failed (expr: loc, NULL_RTX,
19294 reason: "unsupported expression in field");
19295 size = offset = (unsigned HOST_WIDE_INT)-1;
19296 ret = NULL;
19297 break;
19298 }
19299 add_loc_descr (list_head: &ret, descr: ret1);
19300 }
19301 if (fieldsize)
19302 {
19303 ret1 = new_loc_descr (op: DW_OP_piece, oprnd1: fieldsize, oprnd2: 0);
19304 add_loc_descr (list_head: &ret, descr: ret1);
19305 offset = pos + fieldsize;
19306 }
19307 }
19308
19309 if (offset != size)
19310 {
19311 ret1 = new_loc_descr (op: DW_OP_piece, oprnd1: size - offset, oprnd2: 0);
19312 add_loc_descr (list_head: &ret, descr: ret1);
19313 offset = size;
19314 }
19315
19316 have_address = !!want_address;
19317 }
19318 else
19319 expansion_failed (expr: loc, NULL_RTX,
19320 reason: "constructor of non-record type");
19321 }
19322 else
19323 /* We can construct small constants here using int_loc_descriptor. */
19324 expansion_failed (expr: loc, NULL_RTX,
19325 reason: "constructor or constant not in constant pool");
19326 break;
19327
19328 case TRUTH_AND_EXPR:
19329 case TRUTH_ANDIF_EXPR:
19330 case BIT_AND_EXPR:
19331 op = DW_OP_and;
19332 goto do_binop;
19333
19334 case TRUTH_XOR_EXPR:
19335 case BIT_XOR_EXPR:
19336 op = DW_OP_xor;
19337 goto do_binop;
19338
19339 case TRUTH_OR_EXPR:
19340 case TRUTH_ORIF_EXPR:
19341 case BIT_IOR_EXPR:
19342 op = DW_OP_or;
19343 goto do_binop;
19344
19345 case EXACT_DIV_EXPR:
19346 case FLOOR_DIV_EXPR:
19347 case TRUNC_DIV_EXPR:
19348 /* Turn a divide by a power of 2 into a shift when possible. */
19349 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19350 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19351 {
19352 const int log2 = exact_log2 (x: tree_to_uhwi (TREE_OPERAND (loc, 1)));
19353 if (log2 > 0)
19354 {
19355 list_ret
19356 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19357 if (list_ret == 0)
19358 return 0;
19359
19360 add_loc_descr_to_each (list: list_ret, ref: uint_loc_descriptor (i: log2));
19361 add_loc_descr_to_each (list: list_ret,
19362 ref: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
19363 break;
19364 }
19365 }
19366
19367 /* fall through */
19368
19369 case CEIL_DIV_EXPR:
19370 case ROUND_DIV_EXPR:
19371 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19372 {
19373 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19374 scalar_int_mode int_mode;
19375
19376 if ((dwarf_strict && dwarf_version < 5)
19377 || !is_a <scalar_int_mode> (m: mode, result: &int_mode))
19378 return 0;
19379
19380 /* We can use a signed divide if the sign bit is not set. */
19381 if (GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
19382 {
19383 op = DW_OP_div;
19384 goto do_binop;
19385 }
19386
19387 list_ret = typed_binop_from_tree (op: DW_OP_div, loc,
19388 type_die: base_type_for_mode (mode: int_mode, unsignedp: 1),
19389 mode: int_mode, context);
19390 break;
19391 }
19392 op = DW_OP_div;
19393 goto do_binop;
19394
19395 case MINUS_EXPR:
19396 op = DW_OP_minus;
19397 goto do_binop;
19398
19399 case FLOOR_MOD_EXPR:
19400 case CEIL_MOD_EXPR:
19401 case ROUND_MOD_EXPR:
19402 case TRUNC_MOD_EXPR:
19403 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19404 {
19405 op = DW_OP_mod;
19406 goto do_binop;
19407 }
19408 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19409 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address: 0, context);
19410 if (list_ret == 0 || list_ret1 == 0)
19411 return 0;
19412
19413 add_loc_list (ret: &list_ret, list: list_ret1);
19414 if (list_ret == 0)
19415 return 0;
19416 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
19417 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_over, oprnd1: 0, oprnd2: 0));
19418 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_div, oprnd1: 0, oprnd2: 0));
19419 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_mul, oprnd1: 0, oprnd2: 0));
19420 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
19421 break;
19422
19423 case MULT_EXPR:
19424 op = DW_OP_mul;
19425 goto do_binop;
19426
19427 case LSHIFT_EXPR:
19428 op = DW_OP_shl;
19429 goto do_binop;
19430
19431 case RSHIFT_EXPR:
19432 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19433 goto do_binop;
19434
19435 case POINTER_PLUS_EXPR:
19436 case PLUS_EXPR:
19437 do_plus:
19438 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19439 {
19440 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19441 smarter to encode their opposite. The DW_OP_plus_uconst operation
19442 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19443 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19444 bytes, Y being the size of the operation that pushes the opposite
19445 of the addend. So let's choose the smallest representation. */
19446 const tree tree_addend = TREE_OPERAND (loc, 1);
19447 offset_int wi_addend;
19448 HOST_WIDE_INT shwi_addend;
19449 dw_loc_descr_ref loc_naddend;
19450
19451 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19452 if (list_ret == 0)
19453 return 0;
19454
19455 /* Try to get the literal to push. It is the opposite of the addend,
19456 so as we rely on wrapping during DWARF evaluation, first decode
19457 the literal as a "DWARF-sized" signed number. */
19458 wi_addend = wi::to_offset (t: tree_addend);
19459 wi_addend = wi::sext (x: wi_addend, DWARF2_ADDR_SIZE * 8);
19460 shwi_addend = wi_addend.to_shwi ();
19461 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19462 ? int_loc_descriptor (poly_i: -shwi_addend)
19463 : NULL;
19464
19465 if (loc_naddend != NULL
19466 && ((unsigned) size_of_uleb128 (shwi_addend)
19467 > size_of_loc_descr (loc: loc_naddend)))
19468 {
19469 add_loc_descr_to_each (list: list_ret, ref: loc_naddend);
19470 add_loc_descr_to_each (list: list_ret,
19471 ref: new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0));
19472 }
19473 else
19474 {
19475 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19476 {
19477 loc_naddend = loc_cur;
19478 loc_cur = loc_cur->dw_loc_next;
19479 ggc_free (loc_naddend);
19480 }
19481 loc_list_plus_const (list_head: list_ret, offset: wi_addend.to_shwi ());
19482 }
19483 break;
19484 }
19485
19486 op = DW_OP_plus;
19487 goto do_binop;
19488
19489 case LE_EXPR:
19490 op = DW_OP_le;
19491 goto do_comp_binop;
19492
19493 case GE_EXPR:
19494 op = DW_OP_ge;
19495 goto do_comp_binop;
19496
19497 case LT_EXPR:
19498 op = DW_OP_lt;
19499 goto do_comp_binop;
19500
19501 case GT_EXPR:
19502 op = DW_OP_gt;
19503 goto do_comp_binop;
19504
19505 do_comp_binop:
19506 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19507 {
19508 const enum machine_mode mode
19509 = TYPE_MODE (TREE_TYPE (TREE_OPERAND (loc, 0)));
19510 scalar_int_mode int_mode;
19511
19512 /* We can use a signed comparison if the sign bit is not set. */
19513 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
19514 && GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
19515 goto do_binop;
19516
19517 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19518 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19519 list_ret = loc_list_from_uint_comparison (left: list_ret, right: list_ret1,
19520 TREE_CODE (loc));
19521 break;
19522 }
19523 else
19524 goto do_binop;
19525
19526 case EQ_EXPR:
19527 op = DW_OP_eq;
19528 goto do_binop;
19529
19530 case NE_EXPR:
19531 op = DW_OP_ne;
19532 goto do_binop;
19533
19534 do_binop:
19535 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19536 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address: 0, context);
19537 if (list_ret == 0 || list_ret1 == 0)
19538 return 0;
19539
19540 add_loc_list (ret: &list_ret, list: list_ret1);
19541 if (list_ret == 0)
19542 return 0;
19543
19544 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
19545 break;
19546
19547 case TRUTH_NOT_EXPR:
19548 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19549 if (list_ret == 0)
19550 return 0;
19551
19552 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_lit0, oprnd1: 0, oprnd2: 0));
19553 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_eq, oprnd1: 0, oprnd2: 0));
19554 break;
19555
19556 case BIT_NOT_EXPR:
19557 op = DW_OP_not;
19558 goto do_unop;
19559
19560 case ABS_EXPR:
19561 op = DW_OP_abs;
19562 goto do_unop;
19563
19564 case NEGATE_EXPR:
19565 op = DW_OP_neg;
19566 goto do_unop;
19567
19568 do_unop:
19569 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19570 if (list_ret == 0)
19571 return 0;
19572
19573 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op, oprnd1: 0, oprnd2: 0));
19574 break;
19575
19576 case MIN_EXPR:
19577 case MAX_EXPR:
19578 {
19579 const enum tree_code code =
19580 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19581
19582 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19583 build2 (code, integer_type_node,
19584 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19585 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19586 }
19587
19588 /* fall through */
19589
19590 case COND_EXPR:
19591 {
19592 dw_loc_descr_ref lhs
19593 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19594 dw_loc_list_ref rhs
19595 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), want_address: 0, context);
19596 dw_loc_descr_ref bra_node, jump_node, tmp;
19597
19598 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19599 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19600 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19601 list_ret
19602 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19603 want_address: 0, context);
19604 /* Likewise, swap the operands for a logically negated condition. */
19605 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19606 {
19607 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19608 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address: 0, context);
19609 list_ret
19610 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19611 want_address: 0, context);
19612 }
19613 else
19614 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address: 0, context);
19615 if (list_ret == 0 || lhs == 0 || rhs == 0)
19616 return 0;
19617
19618 bra_node = new_loc_descr (op: DW_OP_bra, oprnd1: 0, oprnd2: 0);
19619 add_loc_descr_to_each (list: list_ret, ref: bra_node);
19620
19621 add_loc_list (ret: &list_ret, list: rhs);
19622 jump_node = new_loc_descr (op: DW_OP_skip, oprnd1: 0, oprnd2: 0);
19623 add_loc_descr_to_each (list: list_ret, ref: jump_node);
19624
19625 add_loc_descr_to_each (list: list_ret, ref: lhs);
19626 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19627 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19628
19629 /* ??? Need a node to point the skip at. Use a nop. */
19630 tmp = new_loc_descr (op: DW_OP_nop, oprnd1: 0, oprnd2: 0);
19631 add_loc_descr_to_each (list: list_ret, ref: tmp);
19632 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19633 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19634 }
19635 break;
19636
19637 case FIX_TRUNC_EXPR:
19638 return 0;
19639
19640 case COMPOUND_LITERAL_EXPR:
19641 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19642 want_address: 0, context);
19643
19644 default:
19645 /* Leave front-end specific codes as simply unknown. This comes
19646 up, for instance, with the C STMT_EXPR. */
19647 if ((unsigned int) TREE_CODE (loc)
19648 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19649 {
19650 expansion_failed (expr: loc, NULL_RTX,
19651 reason: "language specific tree node");
19652 return 0;
19653 }
19654
19655 /* Otherwise this is a generic code; we should just lists all of
19656 these explicitly. We forgot one. */
19657 if (flag_checking)
19658 gcc_unreachable ();
19659
19660 /* In a release build, we want to degrade gracefully: better to
19661 generate incomplete debugging information than to crash. */
19662 return NULL;
19663 }
19664
19665 if (!ret && !list_ret)
19666 return 0;
19667
19668 /* Implement wrap-around arithmetics for small integer types. */
19669 if ((TREE_CODE (loc) == PLUS_EXPR
19670 || TREE_CODE (loc) == MINUS_EXPR
19671 || TREE_CODE (loc) == MULT_EXPR
19672 || TREE_CODE (loc) == NEGATE_EXPR
19673 || TREE_CODE (loc) == LSHIFT_EXPR)
19674 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
19675 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (loc)))
19676 {
19677 const enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19678 scalar_int_mode int_mode;
19679
19680 if (is_a <scalar_int_mode> (m: mode, result: &int_mode)
19681 && GET_MODE_SIZE (mode: int_mode) < DWARF2_ADDR_SIZE)
19682 {
19683 const unsigned HOST_WIDE_INT mask
19684 = (HOST_WIDE_INT_1U << GET_MODE_BITSIZE (mode: int_mode)) - 1;
19685 add_loc_descr_to_each (list: list_ret, ref: uint_loc_descriptor (i: mask));
19686 add_loc_descr_to_each (list: list_ret, ref: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
19687 }
19688 }
19689
19690 if (want_address == 2 && !have_address
19691 && (dwarf_version >= 4 || !dwarf_strict))
19692 {
19693 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19694 {
19695 expansion_failed (expr: loc, NULL_RTX,
19696 reason: "DWARF address size mismatch");
19697 return 0;
19698 }
19699 if (ret)
19700 add_loc_descr (list_head: &ret, descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
19701 else
19702 add_loc_descr_to_each (list: list_ret,
19703 ref: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
19704 have_address = 1;
19705 }
19706 /* Show if we can't fill the request for an address. */
19707 if (want_address && !have_address)
19708 {
19709 expansion_failed (expr: loc, NULL_RTX,
19710 reason: "Want address and only have value");
19711 return 0;
19712 }
19713
19714 gcc_assert (!ret || !list_ret);
19715
19716 /* If we've got an address and don't want one, dereference. */
19717 if (!want_address && have_address)
19718 {
19719 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19720 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19721 scalar_int_mode int_mode;
19722 dw_die_ref type_die;
19723 dw_loc_descr_ref deref;
19724
19725 /* Bail out if the size is variable or greater than DWARF2_ADDR_SIZE. */
19726 if (size < 0 || size > DWARF2_ADDR_SIZE)
19727 {
19728 expansion_failed (expr: loc, NULL_RTX, reason: "DWARF address size mismatch");
19729 return 0;
19730 }
19731
19732 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19733 else if (size == DWARF2_ADDR_SIZE)
19734 deref = new_loc_descr (op: DW_OP_deref, oprnd1: size, oprnd2: 0);
19735
19736 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19737 extend the value, which is really OK for unsigned types only. */
19738 else if (!(context && context->strict_signedness)
19739 || TYPE_UNSIGNED (TREE_TYPE (loc))
19740 || (dwarf_strict && dwarf_version < 5)
19741 || !is_a <scalar_int_mode> (m: mode, result: &int_mode)
19742 || !(type_die = base_type_for_mode (mode, unsignedp: false)))
19743 deref = new_loc_descr (op: DW_OP_deref_size, oprnd1: size, oprnd2: 0);
19744
19745 /* Use DW_OP_deref_type for signed integral types if possible, but
19746 convert back to the generic type to avoid type mismatches later. */
19747 else
19748 {
19749 deref = new_loc_descr (op: dwarf_OP (op: DW_OP_deref_type), oprnd1: size, oprnd2: 0);
19750 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19751 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19752 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19753 add_loc_descr (list_head: &deref,
19754 descr: new_loc_descr (op: dwarf_OP (op: DW_OP_convert), oprnd1: 0, oprnd2: 0));
19755 }
19756
19757 /* Deal with bit-fields whose size is not a multiple of a byte. */
19758 if (TREE_CODE (loc) == COMPONENT_REF
19759 && DECL_BIT_FIELD (TREE_OPERAND (loc, 1)))
19760 {
19761 const unsigned HOST_WIDE_INT bitsize
19762 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (loc, 1)));
19763 if (bitsize < (unsigned HOST_WIDE_INT)size * BITS_PER_UNIT)
19764 {
19765 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19766 {
19767 if (BYTES_BIG_ENDIAN)
19768 {
19769 const unsigned HOST_WIDE_INT shift
19770 = size * BITS_PER_UNIT - bitsize;
19771 add_loc_descr (list_head: &deref, descr: uint_loc_descriptor (i: shift));
19772 add_loc_descr (list_head: &deref, descr: new_loc_descr (op: DW_OP_shr, oprnd1: 0, oprnd2: 0));
19773 }
19774 else
19775 {
19776 const unsigned HOST_WIDE_INT mask
19777 = (HOST_WIDE_INT_1U << bitsize) - 1;
19778 add_loc_descr (list_head: &deref, descr: uint_loc_descriptor (i: mask));
19779 add_loc_descr (list_head: &deref, descr: new_loc_descr (op: DW_OP_and, oprnd1: 0, oprnd2: 0));
19780 }
19781 }
19782 else
19783 {
19784 const unsigned HOST_WIDE_INT shiftr
19785 = DWARF2_ADDR_SIZE * BITS_PER_UNIT - bitsize;
19786 const unsigned HOST_WIDE_INT shiftl
19787 = BYTES_BIG_ENDIAN
19788 ? (DWARF2_ADDR_SIZE - size) * BITS_PER_UNIT
19789 : shiftr;
19790 if (shiftl > 0)
19791 {
19792 add_loc_descr (list_head: &deref, descr: uint_loc_descriptor (i: shiftl));
19793 add_loc_descr (list_head: &deref, descr: new_loc_descr (op: DW_OP_shl, oprnd1: 0, oprnd2: 0));
19794 }
19795 add_loc_descr (list_head: &deref, descr: uint_loc_descriptor (i: shiftr));
19796 add_loc_descr (list_head: &deref, descr: new_loc_descr (op: DW_OP_shra, oprnd1: 0, oprnd2: 0));
19797 }
19798 }
19799 }
19800
19801 if (ret)
19802 add_loc_descr (list_head: &ret, descr: deref);
19803 else
19804 add_loc_descr_to_each (list: list_ret, ref: deref);
19805 }
19806
19807 if (ret)
19808 list_ret = new_loc_list (expr: ret, NULL, vbegin: 0, NULL, vend: 0, NULL);
19809
19810 return list_ret;
19811}
19812
19813/* Likewise, but strip useless DW_OP_nop operations in the resulting
19814 expressions. */
19815
19816static dw_loc_list_ref
19817loc_list_from_tree (tree loc, int want_address,
19818 struct loc_descr_context *context)
19819{
19820 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19821
19822 for (dw_loc_list_ref loc_cur = result;
19823 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19824 loc_descr_without_nops (loc&: loc_cur->expr);
19825 return result;
19826}
19827
19828/* Same as above but return only single location expression. */
19829static dw_loc_descr_ref
19830loc_descriptor_from_tree (tree loc, int want_address,
19831 struct loc_descr_context *context)
19832{
19833 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19834 if (!ret)
19835 return NULL;
19836 if (ret->dw_loc_next)
19837 {
19838 expansion_failed (expr: loc, NULL_RTX,
19839 reason: "Location list where only loc descriptor needed");
19840 return NULL;
19841 }
19842 return ret->expr;
19843}
19844
19845/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19846 pointer to the declared type for the relevant field variable, or return
19847 `integer_type_node' if the given node turns out to be an
19848 ERROR_MARK node. */
19849
19850static inline tree
19851field_type (const_tree decl)
19852{
19853 tree type;
19854
19855 if (TREE_CODE (decl) == ERROR_MARK)
19856 return integer_type_node;
19857
19858 type = DECL_BIT_FIELD_TYPE (decl);
19859 if (type == NULL_TREE)
19860 type = TREE_TYPE (decl);
19861
19862 return type;
19863}
19864
19865/* Given a pointer to a tree node, return the alignment in bits for
19866 it, or else return BITS_PER_WORD if the node actually turns out to
19867 be an ERROR_MARK node. */
19868
19869static inline unsigned
19870simple_type_align_in_bits (const_tree type)
19871{
19872 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19873}
19874
19875static inline unsigned
19876simple_decl_align_in_bits (const_tree decl)
19877{
19878 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19879}
19880
19881/* Return the result of rounding T up to ALIGN. */
19882
19883static inline offset_int
19884round_up_to_align (const offset_int &t, unsigned int align)
19885{
19886 return wi::udiv_trunc (x: t + align - 1, y: align) * align;
19887}
19888
19889/* Helper structure for RECORD_TYPE processing. */
19890struct vlr_context
19891{
19892 /* Root RECORD_TYPE. It is needed to generate data member location
19893 descriptions in variable-length records (VLR), but also to cope with
19894 variants, which are composed of nested structures multiplexed with
19895 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19896 function processing a FIELD_DECL, it is required to be non null. */
19897 tree struct_type;
19898
19899 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19900 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19901 this variant part as part of the root record (in storage units). For
19902 regular records, it must be NULL_TREE. */
19903 tree variant_part_offset;
19904};
19905
19906/* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19907 addressed byte of the "containing object" for the given FIELD_DECL. If
19908 possible, return a native constant through CST_OFFSET (in which case NULL is
19909 returned); otherwise return a DWARF expression that computes the offset.
19910
19911 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19912 that offset is, either because the argument turns out to be a pointer to an
19913 ERROR_MARK node, or because the offset expression is too complex for us.
19914
19915 CTX is required: see the comment for VLR_CONTEXT. */
19916
19917static dw_loc_descr_ref
19918field_byte_offset (const_tree decl, struct vlr_context *ctx,
19919 HOST_WIDE_INT *cst_offset)
19920{
19921 tree tree_result;
19922 dw_loc_list_ref loc_result;
19923
19924 *cst_offset = 0;
19925
19926 if (TREE_CODE (decl) == ERROR_MARK)
19927 return NULL;
19928 else
19929 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19930
19931 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19932 case. */
19933 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19934 return NULL;
19935
19936 /* We used to handle only constant offsets in all cases. Now, we handle
19937 properly dynamic byte offsets only when PCC bitfield type doesn't
19938 matter. */
19939 if (PCC_BITFIELD_TYPE_MATTERS
19940 && DECL_BIT_FIELD_TYPE (decl)
19941 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19942 {
19943 offset_int object_offset_in_bits;
19944 offset_int object_offset_in_bytes;
19945 offset_int bitpos_int;
19946 tree type;
19947 tree field_size_tree;
19948 offset_int deepest_bitpos;
19949 offset_int field_size_in_bits;
19950 unsigned int type_align_in_bits;
19951 unsigned int decl_align_in_bits;
19952 offset_int type_size_in_bits;
19953
19954 bitpos_int = wi::to_offset (t: bit_position (decl));
19955 type = field_type (decl);
19956 type_size_in_bits = offset_int_type_size_in_bits (type);
19957 type_align_in_bits = simple_type_align_in_bits (type);
19958
19959 field_size_tree = DECL_SIZE (decl);
19960
19961 /* The size could be unspecified if there was an error, or for
19962 a flexible array member. */
19963 if (!field_size_tree)
19964 field_size_tree = bitsize_zero_node;
19965
19966 /* If the size of the field is not constant, use the type size. */
19967 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19968 field_size_in_bits = wi::to_offset (t: field_size_tree);
19969 else
19970 field_size_in_bits = type_size_in_bits;
19971
19972 decl_align_in_bits = simple_decl_align_in_bits (decl);
19973
19974 /* The GCC front-end doesn't make any attempt to keep track of the
19975 starting bit offset (relative to the start of the containing
19976 structure type) of the hypothetical "containing object" for a
19977 bit-field. Thus, when computing the byte offset value for the
19978 start of the "containing object" of a bit-field, we must deduce
19979 this information on our own. This can be rather tricky to do in
19980 some cases. For example, handling the following structure type
19981 definition when compiling for an i386/i486 target (which only
19982 aligns long long's to 32-bit boundaries) can be very tricky:
19983
19984 struct S { int field1; long long field2:31; };
19985
19986 Fortunately, there is a simple rule-of-thumb which can be used
19987 in such cases. When compiling for an i386/i486, GCC will
19988 allocate 8 bytes for the structure shown above. It decides to
19989 do this based upon one simple rule for bit-field allocation.
19990 GCC allocates each "containing object" for each bit-field at
19991 the first (i.e. lowest addressed) legitimate alignment boundary
19992 (based upon the required minimum alignment for the declared
19993 type of the field) which it can possibly use, subject to the
19994 condition that there is still enough available space remaining
19995 in the containing object (when allocated at the selected point)
19996 to fully accommodate all of the bits of the bit-field itself.
19997
19998 This simple rule makes it obvious why GCC allocates 8 bytes for
19999 each object of the structure type shown above. When looking
20000 for a place to allocate the "containing object" for `field2',
20001 the compiler simply tries to allocate a 64-bit "containing
20002 object" at each successive 32-bit boundary (starting at zero)
20003 until it finds a place to allocate that 64- bit field such that
20004 at least 31 contiguous (and previously unallocated) bits remain
20005 within that selected 64 bit field. (As it turns out, for the
20006 example above, the compiler finds it is OK to allocate the
20007 "containing object" 64-bit field at bit-offset zero within the
20008 structure type.)
20009
20010 Here we attempt to work backwards from the limited set of facts
20011 we're given, and we try to deduce from those facts, where GCC
20012 must have believed that the containing object started (within
20013 the structure type). The value we deduce is then used (by the
20014 callers of this routine) to generate DW_AT_location and
20015 DW_AT_bit_offset attributes for fields (both bit-fields and, in
20016 the case of DW_AT_location, regular fields as well). */
20017
20018 /* Figure out the bit-distance from the start of the structure to
20019 the "deepest" bit of the bit-field. */
20020 deepest_bitpos = bitpos_int + field_size_in_bits;
20021
20022 /* This is the tricky part. Use some fancy footwork to deduce
20023 where the lowest addressed bit of the containing object must
20024 be. */
20025 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
20026
20027 /* Round up to type_align by default. This works best for
20028 bitfields. */
20029 object_offset_in_bits
20030 = round_up_to_align (t: object_offset_in_bits, align: type_align_in_bits);
20031
20032 if (wi::gtu_p (x: object_offset_in_bits, y: bitpos_int))
20033 {
20034 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
20035
20036 /* Round up to decl_align instead. */
20037 object_offset_in_bits
20038 = round_up_to_align (t: object_offset_in_bits, align: decl_align_in_bits);
20039 }
20040
20041 object_offset_in_bytes
20042 = wi::lrshift (x: object_offset_in_bits, LOG2_BITS_PER_UNIT);
20043 if (ctx->variant_part_offset == NULL_TREE)
20044 {
20045 *cst_offset = object_offset_in_bytes.to_shwi ();
20046 return NULL;
20047 }
20048 tree_result = wide_int_to_tree (sizetype, cst: object_offset_in_bytes);
20049 }
20050 else
20051 tree_result = byte_position (decl);
20052
20053 if (ctx->variant_part_offset != NULL_TREE)
20054 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
20055 ctx->variant_part_offset, tree_result);
20056
20057 /* If the byte offset is a constant, it's simplier to handle a native
20058 constant rather than a DWARF expression. */
20059 if (TREE_CODE (tree_result) == INTEGER_CST)
20060 {
20061 *cst_offset = wi::to_offset (t: tree_result).to_shwi ();
20062 return NULL;
20063 }
20064
20065 struct loc_descr_context loc_ctx = {
20066 .context_type: ctx->struct_type, /* context_type */
20067 NULL_TREE, /* base_decl */
20068 NULL, /* dpi */
20069 .placeholder_arg: false, /* placeholder_arg */
20070 .placeholder_seen: false, /* placeholder_seen */
20071 .strict_signedness: false /* strict_signedness */
20072 };
20073 loc_result = loc_list_from_tree (loc: tree_result, want_address: 0, context: &loc_ctx);
20074
20075 /* We want a DWARF expression: abort if we only have a location list with
20076 multiple elements. */
20077 if (!loc_result || !single_element_loc_list_p (list: loc_result))
20078 return NULL;
20079 else
20080 return loc_result->expr;
20081}
20082
20083/* The following routines define various Dwarf attributes and any data
20084 associated with them. */
20085
20086/* Add a location description attribute value to a DIE.
20087
20088 This emits location attributes suitable for whole variables and
20089 whole parameters. Note that the location attributes for struct fields are
20090 generated by the routine `data_member_location_attribute' below. */
20091
20092static inline void
20093add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20094 dw_loc_list_ref descr)
20095{
20096 bool check_no_locviews = true;
20097 if (descr == 0)
20098 return;
20099 if (single_element_loc_list_p (list: descr))
20100 add_AT_loc (die, attr_kind, loc: descr->expr);
20101 else
20102 {
20103 add_AT_loc_list (die, attr_kind, loc_list: descr);
20104 gcc_assert (descr->ll_symbol);
20105 if (attr_kind == DW_AT_location && descr->vl_symbol
20106 && dwarf2out_locviews_in_attribute ())
20107 {
20108 add_AT_view_list (die, attr_kind: DW_AT_GNU_locviews);
20109 check_no_locviews = false;
20110 }
20111 }
20112
20113 if (check_no_locviews)
20114 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20115}
20116
20117/* Add DW_AT_accessibility attribute to DIE if needed. */
20118
20119static void
20120add_accessibility_attribute (dw_die_ref die, tree decl)
20121{
20122 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20123 children, otherwise the default is DW_ACCESS_public. In DWARF2
20124 the default has always been DW_ACCESS_public. */
20125 if (TREE_PROTECTED (decl))
20126 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_protected);
20127 else if (TREE_PRIVATE (decl))
20128 {
20129 if (dwarf_version == 2
20130 || die->die_parent == NULL
20131 || die->die_parent->die_tag != DW_TAG_class_type)
20132 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_private);
20133 }
20134 else if (dwarf_version > 2
20135 && die->die_parent
20136 && die->die_parent->die_tag == DW_TAG_class_type)
20137 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_public);
20138}
20139
20140/* Attach the specialized form of location attribute used for data members of
20141 struct and union types. In the special case of a FIELD_DECL node which
20142 represents a bit-field, the "offset" part of this special location
20143 descriptor must indicate the distance in bytes from the lowest-addressed
20144 byte of the containing struct or union type to the lowest-addressed byte of
20145 the "containing object" for the bit-field. (See the `field_byte_offset'
20146 function above).
20147
20148 For any given bit-field, the "containing object" is a hypothetical object
20149 (of some integral or enum type) within which the given bit-field lives. The
20150 type of this hypothetical "containing object" is always the same as the
20151 declared type of the individual bit-field itself (for GCC anyway... the
20152 DWARF spec doesn't actually mandate this). Note that it is the size (in
20153 bytes) of the hypothetical "containing object" which will be given in the
20154 DW_AT_byte_size attribute for this bit-field. (See the
20155 `byte_size_attribute' function below.) It is also used when calculating the
20156 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20157 function below.)
20158
20159 CTX is required: see the comment for VLR_CONTEXT. */
20160
20161static void
20162add_data_member_location_attribute (dw_die_ref die,
20163 tree decl,
20164 struct vlr_context *ctx)
20165{
20166 HOST_WIDE_INT offset;
20167 dw_loc_descr_ref loc_descr = 0;
20168
20169 if (TREE_CODE (decl) == TREE_BINFO)
20170 {
20171 /* We're working on the TAG_inheritance for a base class. */
20172 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20173 {
20174 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20175 aren't at a fixed offset from all (sub)objects of the same
20176 type. We need to extract the appropriate offset from our
20177 vtable. The following dwarf expression means
20178
20179 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20180
20181 This is specific to the V3 ABI, of course. */
20182
20183 dw_loc_descr_ref tmp;
20184
20185 /* Make a copy of the object address. */
20186 tmp = new_loc_descr (op: DW_OP_dup, oprnd1: 0, oprnd2: 0);
20187 add_loc_descr (list_head: &loc_descr, descr: tmp);
20188
20189 /* Extract the vtable address. */
20190 tmp = new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0);
20191 add_loc_descr (list_head: &loc_descr, descr: tmp);
20192
20193 /* Calculate the address of the offset. */
20194 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20195 gcc_assert (offset < 0);
20196
20197 tmp = int_loc_descriptor (poly_i: -offset);
20198 add_loc_descr (list_head: &loc_descr, descr: tmp);
20199 tmp = new_loc_descr (op: DW_OP_minus, oprnd1: 0, oprnd2: 0);
20200 add_loc_descr (list_head: &loc_descr, descr: tmp);
20201
20202 /* Extract the offset. */
20203 tmp = new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0);
20204 add_loc_descr (list_head: &loc_descr, descr: tmp);
20205
20206 /* Add it to the object address. */
20207 tmp = new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0);
20208 add_loc_descr (list_head: &loc_descr, descr: tmp);
20209 }
20210 else
20211 offset = tree_to_shwi (BINFO_OFFSET (decl));
20212 }
20213 else
20214 {
20215 loc_descr = field_byte_offset (decl, ctx, cst_offset: &offset);
20216
20217 if (!loc_descr)
20218 ;
20219
20220 /* If loc_descr is available, then we know the offset is dynamic. */
20221 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20222 {
20223 loc_descr = NULL;
20224 offset = 0;
20225 }
20226
20227 /* Data member location evaluation starts with the base address on the
20228 stack. Compute the field offset and add it to this base address. */
20229 else
20230 add_loc_descr (list_head: &loc_descr, descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
20231 }
20232
20233 if (!loc_descr)
20234 {
20235 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20236 e.g. GDB only added support to it in November 2016. For DWARF5
20237 we need newer debug info consumers anyway. We might change this
20238 to dwarf_version >= 4 once most consumers catched up. */
20239 if (dwarf_version >= 5
20240 && TREE_CODE (decl) == FIELD_DECL
20241 && DECL_BIT_FIELD_TYPE (decl)
20242 && (ctx->variant_part_offset == NULL_TREE
20243 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20244 {
20245 tree off = bit_position (decl);
20246 if (ctx->variant_part_offset)
20247 off = bit_from_pos (ctx->variant_part_offset, off);
20248 if (tree_fits_uhwi_p (off) && get_AT (die, attr_kind: DW_AT_bit_size))
20249 {
20250 remove_AT (die, attr_kind: DW_AT_byte_size);
20251 remove_AT (die, attr_kind: DW_AT_bit_offset);
20252 add_AT_unsigned (die, attr_kind: DW_AT_data_bit_offset, unsigned_val: tree_to_uhwi (off));
20253 return;
20254 }
20255 }
20256 if (dwarf_version > 2)
20257 {
20258 /* Don't need to output a location expression, just the constant. */
20259 if (offset < 0)
20260 add_AT_int (die, attr_kind: DW_AT_data_member_location, int_val: offset);
20261 else
20262 add_AT_unsigned (die, attr_kind: DW_AT_data_member_location, unsigned_val: offset);
20263 return;
20264 }
20265 else
20266 {
20267 enum dwarf_location_atom op;
20268
20269 /* The DWARF2 standard says that we should assume that the structure
20270 address is already on the stack, so we can specify a structure
20271 field address by using DW_OP_plus_uconst. */
20272 op = DW_OP_plus_uconst;
20273 loc_descr = new_loc_descr (op, oprnd1: offset, oprnd2: 0);
20274 }
20275 }
20276
20277 add_AT_loc (die, attr_kind: DW_AT_data_member_location, loc: loc_descr);
20278}
20279
20280/* Writes integer values to dw_vec_const array. */
20281
20282static void
20283insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20284{
20285 while (size != 0)
20286 {
20287 *dest++ = val & 0xff;
20288 val >>= 8;
20289 --size;
20290 }
20291}
20292
20293/* Reads integers from dw_vec_const array. Inverse of insert_int. */
20294
20295static HOST_WIDE_INT
20296extract_int (const unsigned char *src, unsigned int size)
20297{
20298 HOST_WIDE_INT val = 0;
20299
20300 src += size;
20301 while (size != 0)
20302 {
20303 val <<= 8;
20304 val |= *--src & 0xff;
20305 --size;
20306 }
20307 return val;
20308}
20309
20310/* Writes wide_int values to dw_vec_const array. */
20311
20312static void
20313insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20314{
20315 int i;
20316
20317 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20318 {
20319 insert_int (val: (HOST_WIDE_INT) val.elt (i: 0), size: elt_size, dest);
20320 return;
20321 }
20322
20323 /* We'd have to extend this code to support odd sizes. */
20324 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20325
20326 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20327
20328 if (WORDS_BIG_ENDIAN)
20329 for (i = n - 1; i >= 0; i--)
20330 {
20331 insert_int (val: (HOST_WIDE_INT) val.elt (i), size: sizeof (HOST_WIDE_INT), dest);
20332 dest += sizeof (HOST_WIDE_INT);
20333 }
20334 else
20335 for (i = 0; i < n; i++)
20336 {
20337 insert_int (val: (HOST_WIDE_INT) val.elt (i), size: sizeof (HOST_WIDE_INT), dest);
20338 dest += sizeof (HOST_WIDE_INT);
20339 }
20340}
20341
20342/* Writes floating point values to dw_vec_const array. */
20343
20344static unsigned
20345insert_float (const_rtx rtl, unsigned char *array)
20346{
20347 long val[4];
20348 int i;
20349 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20350
20351 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20352
20353 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20354 if (GET_MODE_SIZE (mode) < 4)
20355 {
20356 gcc_assert (GET_MODE_SIZE (mode) == 2);
20357 insert_int (val: val[0], size: 2, dest: array);
20358 return 2;
20359 }
20360
20361 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20362 {
20363 insert_int (val: val[i], size: 4, dest: array);
20364 array += 4;
20365 }
20366 return 4;
20367}
20368
20369/* Attach a DW_AT_const_value attribute for a variable or a parameter which
20370 does not have a "location" either in memory or in a register. These
20371 things can arise in GNU C when a constant is passed as an actual parameter
20372 to an inlined function. They can also arise in C++ where declared
20373 constants do not necessarily get memory "homes". */
20374
20375static bool
20376add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20377{
20378 scalar_mode int_mode;
20379
20380 switch (GET_CODE (rtl))
20381 {
20382 case CONST_INT:
20383 {
20384 HOST_WIDE_INT val = INTVAL (rtl);
20385
20386 if (val < 0)
20387 add_AT_int (die, attr_kind: DW_AT_const_value, int_val: val);
20388 else
20389 add_AT_unsigned (die, attr_kind: DW_AT_const_value, unsigned_val: (unsigned HOST_WIDE_INT) val);
20390 }
20391 return true;
20392
20393 case CONST_WIDE_INT:
20394 if (is_int_mode (mode, int_mode: &int_mode)
20395 && (GET_MODE_PRECISION (mode: int_mode)
20396 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20397 {
20398 add_AT_wide (die, attr_kind: DW_AT_const_value, w: rtx_mode_t (rtl, int_mode));
20399 return true;
20400 }
20401 return false;
20402
20403 case CONST_DOUBLE:
20404 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20405 floating-point constant. A CONST_DOUBLE is used whenever the
20406 constant requires more than one word in order to be adequately
20407 represented. */
20408 if (TARGET_SUPPORTS_WIDE_INT == 0
20409 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20410 add_AT_double (die, attr_kind: DW_AT_const_value,
20411 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20412 else
20413 {
20414 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20415 unsigned int length = GET_MODE_SIZE (mode);
20416 unsigned char *array = ggc_vec_alloc<unsigned char> (c: length);
20417 unsigned int elt_size = insert_float (rtl, array);
20418
20419 add_AT_vec (die, attr_kind: DW_AT_const_value, length: length / elt_size, elt_size,
20420 array);
20421 }
20422 return true;
20423
20424 case CONST_VECTOR:
20425 {
20426 unsigned int length;
20427 if (!CONST_VECTOR_NUNITS (rtl).is_constant (const_value: &length))
20428 return false;
20429
20430 machine_mode mode = GET_MODE (rtl);
20431 /* The combination of a length and byte elt_size doesn't extend
20432 naturally to boolean vectors, where several elements are packed
20433 into the same byte. */
20434 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20435 return false;
20436
20437 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20438 unsigned char *array
20439 = ggc_vec_alloc<unsigned char> (c: length * elt_size);
20440 unsigned int i;
20441 unsigned char *p;
20442 machine_mode imode = GET_MODE_INNER (mode);
20443
20444 switch (GET_MODE_CLASS (mode))
20445 {
20446 case MODE_VECTOR_INT:
20447 for (i = 0, p = array; i < length; i++, p += elt_size)
20448 {
20449 rtx elt = CONST_VECTOR_ELT (rtl, i);
20450 insert_wide_int (val: rtx_mode_t (elt, imode), dest: p, elt_size);
20451 }
20452 break;
20453
20454 case MODE_VECTOR_FLOAT:
20455 for (i = 0, p = array; i < length; i++, p += elt_size)
20456 {
20457 rtx elt = CONST_VECTOR_ELT (rtl, i);
20458 insert_float (rtl: elt, array: p);
20459 }
20460 break;
20461
20462 default:
20463 gcc_unreachable ();
20464 }
20465
20466 add_AT_vec (die, attr_kind: DW_AT_const_value, length, elt_size, array);
20467 }
20468 return true;
20469
20470 case CONST_STRING:
20471 if (dwarf_version >= 4 || !dwarf_strict)
20472 {
20473 dw_loc_descr_ref loc_result;
20474 resolve_one_addr (&rtl);
20475 rtl_addr:
20476 loc_result = new_addr_loc_descr (addr: rtl, dtprel: dtprel_false);
20477 add_loc_descr (list_head: &loc_result, descr: new_loc_descr (op: DW_OP_stack_value, oprnd1: 0, oprnd2: 0));
20478 add_AT_loc (die, attr_kind: DW_AT_location, loc: loc_result);
20479 vec_safe_push (v&: used_rtx_array, obj: rtl);
20480 return true;
20481 }
20482 return false;
20483
20484 case CONST:
20485 if (CONSTANT_P (XEXP (rtl, 0)))
20486 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20487 /* FALLTHROUGH */
20488 case SYMBOL_REF:
20489 if (!const_ok_for_output (rtl))
20490 return false;
20491 /* FALLTHROUGH */
20492 case LABEL_REF:
20493 if (dwarf_version >= 4 || !dwarf_strict)
20494 goto rtl_addr;
20495 return false;
20496
20497 case PLUS:
20498 /* In cases where an inlined instance of an inline function is passed
20499 the address of an `auto' variable (which is local to the caller) we
20500 can get a situation where the DECL_RTL of the artificial local
20501 variable (for the inlining) which acts as a stand-in for the
20502 corresponding formal parameter (of the inline function) will look
20503 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20504 exactly a compile-time constant expression, but it isn't the address
20505 of the (artificial) local variable either. Rather, it represents the
20506 *value* which the artificial local variable always has during its
20507 lifetime. We currently have no way to represent such quasi-constant
20508 values in Dwarf, so for now we just punt and generate nothing. */
20509 return false;
20510
20511 case HIGH:
20512 case CONST_FIXED:
20513 case MINUS:
20514 case SIGN_EXTEND:
20515 case ZERO_EXTEND:
20516 case CONST_POLY_INT:
20517 return false;
20518
20519 case MEM:
20520 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20521 && MEM_READONLY_P (rtl)
20522 && GET_MODE (rtl) == BLKmode)
20523 {
20524 add_AT_string (die, attr_kind: DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20525 return true;
20526 }
20527 return false;
20528
20529 default:
20530 /* No other kinds of rtx should be possible here. */
20531 gcc_unreachable ();
20532 }
20533}
20534
20535/* Determine whether the evaluation of EXPR references any variables
20536 or functions which aren't otherwise used (and therefore may not be
20537 output). */
20538static tree
20539reference_to_unused (tree * tp, int * walk_subtrees,
20540 void * data ATTRIBUTE_UNUSED)
20541{
20542 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20543 *walk_subtrees = 0;
20544
20545 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20546 && ! TREE_ASM_WRITTEN (*tp))
20547 return *tp;
20548 /* ??? The C++ FE emits debug information for using decls, so
20549 putting gcc_unreachable here falls over. See PR31899. For now
20550 be conservative. */
20551 else if (!symtab->global_info_ready && VAR_P (*tp))
20552 return *tp;
20553 else if (VAR_P (*tp))
20554 {
20555 varpool_node *node = varpool_node::get (decl: *tp);
20556 if (!node || !node->definition)
20557 return *tp;
20558 }
20559 else if (TREE_CODE (*tp) == FUNCTION_DECL
20560 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20561 {
20562 /* The call graph machinery must have finished analyzing,
20563 optimizing and gimplifying the CU by now.
20564 So if *TP has no call graph node associated
20565 to it, it means *TP will not be emitted. */
20566 if (!symtab->global_info_ready || !cgraph_node::get (decl: *tp))
20567 return *tp;
20568 }
20569 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20570 return *tp;
20571
20572 return NULL_TREE;
20573}
20574
20575/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20576 for use in a later add_const_value_attribute call. */
20577
20578static rtx
20579rtl_for_decl_init (tree init, tree type)
20580{
20581 rtx rtl = NULL_RTX;
20582
20583 STRIP_NOPS (init);
20584
20585 /* If a variable is initialized with a string constant without embedded
20586 zeros, build CONST_STRING. */
20587 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20588 {
20589 tree enttype = TREE_TYPE (type);
20590 tree domain = TYPE_DOMAIN (type);
20591 scalar_int_mode mode;
20592
20593 if (is_int_mode (TYPE_MODE (enttype), int_mode: &mode)
20594 && GET_MODE_SIZE (mode) == 1
20595 && domain
20596 && TYPE_MAX_VALUE (domain)
20597 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20598 && integer_zerop (TYPE_MIN_VALUE (domain))
20599 && compare_tree_int (TYPE_MAX_VALUE (domain),
20600 TREE_STRING_LENGTH (init) - 1) == 0
20601 && ((size_t) TREE_STRING_LENGTH (init)
20602 == strlen (TREE_STRING_POINTER (init)) + 1))
20603 {
20604 rtl = gen_rtx_CONST_STRING (VOIDmode,
20605 ggc_strdup (TREE_STRING_POINTER (init)));
20606 rtl = gen_rtx_MEM (BLKmode, rtl);
20607 MEM_READONLY_P (rtl) = 1;
20608 }
20609 }
20610 /* Other aggregates, and complex values, could be represented using
20611 CONCAT: FIXME!
20612 If this changes, please adjust tree_add_const_value_attribute
20613 so that for early_dwarf it will for such initializers mangle referenced
20614 decls. */
20615 else if (AGGREGATE_TYPE_P (type)
20616 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20617 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20618 || TREE_CODE (type) == COMPLEX_TYPE)
20619 ;
20620 /* Vectors only work if their mode is supported by the target.
20621 FIXME: generic vectors ought to work too. */
20622 else if (TREE_CODE (type) == VECTOR_TYPE
20623 && !VECTOR_MODE_P (TYPE_MODE (type)))
20624 ;
20625 /* If the initializer is something that we know will expand into an
20626 immediate RTL constant, expand it now. We must be careful not to
20627 reference variables which won't be output. */
20628 else if (initializer_constant_valid_p (init, type)
20629 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20630 {
20631 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20632 possible. */
20633 if (TREE_CODE (type) == VECTOR_TYPE)
20634 switch (TREE_CODE (init))
20635 {
20636 case VECTOR_CST:
20637 break;
20638 case CONSTRUCTOR:
20639 if (TREE_CONSTANT (init))
20640 {
20641 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20642 bool constant_p = true;
20643 tree value;
20644 unsigned HOST_WIDE_INT ix;
20645
20646 /* Even when ctor is constant, it might contain non-*_CST
20647 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20648 belong into VECTOR_CST nodes. */
20649 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20650 if (!CONSTANT_CLASS_P (value))
20651 {
20652 constant_p = false;
20653 break;
20654 }
20655
20656 if (constant_p)
20657 {
20658 init = build_vector_from_ctor (type, elts);
20659 break;
20660 }
20661 }
20662 /* FALLTHRU */
20663
20664 default:
20665 return NULL;
20666 }
20667
20668 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20669 if (TREE_CODE (init) == INTEGER_CST
20670 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20671 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20672 {
20673 if (tree_fits_shwi_p (init))
20674 return GEN_INT (tree_to_shwi (init));
20675 else
20676 return NULL;
20677 }
20678
20679 rtl = expand_expr (exp: init, NULL_RTX, VOIDmode, modifier: EXPAND_INITIALIZER);
20680
20681 /* If expand_expr returns a MEM, it wasn't immediate. */
20682 gcc_assert (!rtl || !MEM_P (rtl));
20683 }
20684
20685 return rtl;
20686}
20687
20688/* Generate RTL for the variable DECL to represent its location. */
20689
20690static rtx
20691rtl_for_decl_location (tree decl)
20692{
20693 rtx rtl;
20694
20695 /* Here we have to decide where we are going to say the parameter "lives"
20696 (as far as the debugger is concerned). We only have a couple of
20697 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20698
20699 DECL_RTL normally indicates where the parameter lives during most of the
20700 activation of the function. If optimization is enabled however, this
20701 could be either NULL or else a pseudo-reg. Both of those cases indicate
20702 that the parameter doesn't really live anywhere (as far as the code
20703 generation parts of GCC are concerned) during most of the function's
20704 activation. That will happen (for example) if the parameter is never
20705 referenced within the function.
20706
20707 We could just generate a location descriptor here for all non-NULL
20708 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20709 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20710 where DECL_RTL is NULL or is a pseudo-reg.
20711
20712 Note however that we can only get away with using DECL_INCOMING_RTL as
20713 a backup substitute for DECL_RTL in certain limited cases. In cases
20714 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20715 we can be sure that the parameter was passed using the same type as it is
20716 declared to have within the function, and that its DECL_INCOMING_RTL
20717 points us to a place where a value of that type is passed.
20718
20719 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20720 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20721 because in these cases DECL_INCOMING_RTL points us to a value of some
20722 type which is *different* from the type of the parameter itself. Thus,
20723 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20724 such cases, the debugger would end up (for example) trying to fetch a
20725 `float' from a place which actually contains the first part of a
20726 `double'. That would lead to really incorrect and confusing
20727 output at debug-time.
20728
20729 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20730 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20731 are a couple of exceptions however. On little-endian machines we can
20732 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20733 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20734 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20735 when (on a little-endian machine) a non-prototyped function has a
20736 parameter declared to be of type `short' or `char'. In such cases,
20737 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20738 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20739 passed `int' value. If the debugger then uses that address to fetch
20740 a `short' or a `char' (on a little-endian machine) the result will be
20741 the correct data, so we allow for such exceptional cases below.
20742
20743 Note that our goal here is to describe the place where the given formal
20744 parameter lives during most of the function's activation (i.e. between the
20745 end of the prologue and the start of the epilogue). We'll do that as best
20746 as we can. Note however that if the given formal parameter is modified
20747 sometime during the execution of the function, then a stack backtrace (at
20748 debug-time) will show the function as having been called with the *new*
20749 value rather than the value which was originally passed in. This happens
20750 rarely enough that it is not a major problem, but it *is* a problem, and
20751 I'd like to fix it.
20752
20753 A future version of dwarf2out.cc may generate two additional attributes for
20754 any given DW_TAG_formal_parameter DIE which will describe the "passed
20755 type" and the "passed location" for the given formal parameter in addition
20756 to the attributes we now generate to indicate the "declared type" and the
20757 "active location" for each parameter. This additional set of attributes
20758 could be used by debuggers for stack backtraces. Separately, note that
20759 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20760 This happens (for example) for inlined-instances of inline function formal
20761 parameters which are never referenced. This really shouldn't be
20762 happening. All PARM_DECL nodes should get valid non-NULL
20763 DECL_INCOMING_RTL values. FIXME. */
20764
20765 /* Use DECL_RTL as the "location" unless we find something better. */
20766 rtl = DECL_RTL_IF_SET (decl);
20767
20768 /* When generating abstract instances, ignore everything except
20769 constants, symbols living in memory, and symbols living in
20770 fixed registers. */
20771 if (! reload_completed)
20772 {
20773 if (rtl
20774 && (CONSTANT_P (rtl)
20775 || (MEM_P (rtl)
20776 && CONSTANT_P (XEXP (rtl, 0)))
20777 || (REG_P (rtl)
20778 && VAR_P (decl)
20779 && TREE_STATIC (decl))))
20780 {
20781 rtl = targetm.delegitimize_address (rtl);
20782 return rtl;
20783 }
20784 rtl = NULL_RTX;
20785 }
20786 else if (TREE_CODE (decl) == PARM_DECL)
20787 {
20788 if (rtl == NULL_RTX
20789 || is_pseudo_reg (rtl)
20790 || (MEM_P (rtl)
20791 && is_pseudo_reg (XEXP (rtl, 0))
20792 && DECL_INCOMING_RTL (decl)
20793 && MEM_P (DECL_INCOMING_RTL (decl))
20794 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20795 {
20796 tree declared_type = TREE_TYPE (decl);
20797 tree passed_type = DECL_ARG_TYPE (decl);
20798 machine_mode dmode = TYPE_MODE (declared_type);
20799 machine_mode pmode = TYPE_MODE (passed_type);
20800
20801 /* This decl represents a formal parameter which was optimized out.
20802 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20803 all cases where (rtl == NULL_RTX) just below. */
20804 if (dmode == pmode)
20805 rtl = DECL_INCOMING_RTL (decl);
20806 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20807 && SCALAR_INT_MODE_P (dmode)
20808 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20809 && DECL_INCOMING_RTL (decl))
20810 {
20811 rtx inc = DECL_INCOMING_RTL (decl);
20812 if (REG_P (inc))
20813 rtl = inc;
20814 else if (MEM_P (inc))
20815 {
20816 if (BYTES_BIG_ENDIAN)
20817 rtl = adjust_address_nv (inc, dmode,
20818 GET_MODE_SIZE (pmode)
20819 - GET_MODE_SIZE (dmode));
20820 else
20821 rtl = inc;
20822 }
20823 }
20824 }
20825
20826 /* If the parm was passed in registers, but lives on the stack, then
20827 make a big endian correction if the mode of the type of the
20828 parameter is not the same as the mode of the rtl. */
20829 /* ??? This is the same series of checks that are made in dbxout.cc before
20830 we reach the big endian correction code there. It isn't clear if all
20831 of these checks are necessary here, but keeping them all is the safe
20832 thing to do. */
20833 else if (MEM_P (rtl)
20834 && XEXP (rtl, 0) != const0_rtx
20835 && ! CONSTANT_P (XEXP (rtl, 0))
20836 /* Not passed in memory. */
20837 && !MEM_P (DECL_INCOMING_RTL (decl))
20838 /* Not passed by invisible reference. */
20839 && (!REG_P (XEXP (rtl, 0))
20840 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20841 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20842#if !HARD_FRAME_POINTER_IS_ARG_POINTER
20843 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20844#endif
20845 )
20846 /* Big endian correction check. */
20847 && BYTES_BIG_ENDIAN
20848 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20849 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20850 UNITS_PER_WORD))
20851 {
20852 machine_mode addr_mode = get_address_mode (mem: rtl);
20853 poly_int64 offset = (UNITS_PER_WORD
20854 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20855
20856 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20857 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20858 }
20859 }
20860 else if (VAR_P (decl)
20861 && rtl
20862 && MEM_P (rtl)
20863 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20864 {
20865 machine_mode addr_mode = get_address_mode (mem: rtl);
20866 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20867 GET_MODE (rtl));
20868
20869 /* If a variable is declared "register" yet is smaller than
20870 a register, then if we store the variable to memory, it
20871 looks like we're storing a register-sized value, when in
20872 fact we are not. We need to adjust the offset of the
20873 storage location to reflect the actual value's bytes,
20874 else gdb will not be able to display it. */
20875 if (maybe_ne (a: offset, b: 0))
20876 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20877 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20878 }
20879
20880 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20881 and will have been substituted directly into all expressions that use it.
20882 C does not have such a concept, but C++ and other languages do. */
20883 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20884 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20885
20886 if (rtl)
20887 rtl = targetm.delegitimize_address (rtl);
20888
20889 /* If we don't look past the constant pool, we risk emitting a
20890 reference to a constant pool entry that isn't referenced from
20891 code, and thus is not emitted. */
20892 if (rtl)
20893 rtl = avoid_constant_pool_reference (rtl);
20894
20895 /* Try harder to get a rtl. If this symbol ends up not being emitted
20896 in the current CU, resolve_addr will remove the expression referencing
20897 it. */
20898 if (rtl == NULL_RTX
20899 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20900 && VAR_P (decl)
20901 && !DECL_EXTERNAL (decl)
20902 && TREE_STATIC (decl)
20903 && DECL_NAME (decl)
20904 && !DECL_HARD_REGISTER (decl)
20905 && DECL_MODE (decl) != VOIDmode)
20906 {
20907 rtl = make_decl_rtl_for_debug (decl);
20908 if (!MEM_P (rtl)
20909 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20910 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20911 rtl = NULL_RTX;
20912 }
20913
20914 return rtl;
20915}
20916
20917/* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20918 returned. If so, the decl for the COMMON block is returned, and the
20919 value is the offset into the common block for the symbol. */
20920
20921static tree
20922fortran_common (tree decl, HOST_WIDE_INT *value)
20923{
20924 tree val_expr, cvar;
20925 machine_mode mode;
20926 poly_int64 bitsize, bitpos;
20927 tree offset;
20928 HOST_WIDE_INT cbitpos;
20929 int unsignedp, reversep, volatilep = 0;
20930
20931 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20932 it does not have a value (the offset into the common area), or if it
20933 is thread local (as opposed to global) then it isn't common, and shouldn't
20934 be handled as such. */
20935 if (!VAR_P (decl)
20936 || !TREE_STATIC (decl)
20937 || !DECL_HAS_VALUE_EXPR_P (decl)
20938 || !is_fortran ())
20939 return NULL_TREE;
20940
20941 val_expr = DECL_VALUE_EXPR (decl);
20942 if (TREE_CODE (val_expr) != COMPONENT_REF)
20943 return NULL_TREE;
20944
20945 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20946 &unsignedp, &reversep, &volatilep);
20947
20948 if (cvar == NULL_TREE
20949 || !VAR_P (cvar)
20950 || DECL_ARTIFICIAL (cvar)
20951 || !TREE_PUBLIC (cvar)
20952 /* We don't expect to have to cope with variable offsets,
20953 since at present all static data must have a constant size. */
20954 || !bitpos.is_constant (const_value: &cbitpos))
20955 return NULL_TREE;
20956
20957 *value = 0;
20958 if (offset != NULL)
20959 {
20960 if (!tree_fits_shwi_p (offset))
20961 return NULL_TREE;
20962 *value = tree_to_shwi (offset);
20963 }
20964 if (cbitpos != 0)
20965 *value += cbitpos / BITS_PER_UNIT;
20966
20967 return cvar;
20968}
20969
20970/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20971 data attribute for a variable or a parameter. We generate the
20972 DW_AT_const_value attribute only in those cases where the given variable
20973 or parameter does not have a true "location" either in memory or in a
20974 register. This can happen (for example) when a constant is passed as an
20975 actual argument in a call to an inline function. (It's possible that
20976 these things can crop up in other ways also.) Note that one type of
20977 constant value which can be passed into an inlined function is a constant
20978 pointer. This can happen for example if an actual argument in an inlined
20979 function call evaluates to a compile-time constant address.
20980
20981 CACHE_P is true if it is worth caching the location list for DECL,
20982 so that future calls can reuse it rather than regenerate it from scratch.
20983 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20984 since we will need to refer to them each time the function is inlined. */
20985
20986static bool
20987add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20988{
20989 rtx rtl;
20990 dw_loc_list_ref list;
20991 var_loc_list *loc_list;
20992 cached_dw_loc_list *cache;
20993
20994 if (early_dwarf)
20995 return false;
20996
20997 if (TREE_CODE (decl) == ERROR_MARK)
20998 return false;
20999
21000 if (get_AT (die, attr_kind: DW_AT_location)
21001 || get_AT (die, attr_kind: DW_AT_const_value))
21002 return true;
21003
21004 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
21005 || TREE_CODE (decl) == RESULT_DECL);
21006
21007 /* Try to get some constant RTL for this decl, and use that as the value of
21008 the location. */
21009
21010 rtl = rtl_for_decl_location (decl);
21011 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
21012 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
21013 return true;
21014
21015 /* See if we have single element location list that is equivalent to
21016 a constant value. That way we are better to use add_const_value_attribute
21017 rather than expanding constant value equivalent. */
21018 loc_list = lookup_decl_loc (decl);
21019 if (loc_list
21020 && loc_list->first
21021 && loc_list->first->next == NULL
21022 && NOTE_P (loc_list->first->loc)
21023 && NOTE_VAR_LOCATION (loc_list->first->loc)
21024 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
21025 {
21026 struct var_loc_node *node;
21027
21028 node = loc_list->first;
21029 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
21030 if (GET_CODE (rtl) == EXPR_LIST)
21031 rtl = XEXP (rtl, 0);
21032 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
21033 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
21034 return true;
21035 }
21036 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
21037 list several times. See if we've already cached the contents. */
21038 list = NULL;
21039 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
21040 cache_p = false;
21041 if (cache_p)
21042 {
21043 cache = cached_dw_loc_list_table->find_with_hash (comparable: decl, DECL_UID (decl));
21044 if (cache)
21045 list = cache->loc_list;
21046 }
21047 if (list == NULL)
21048 {
21049 list = loc_list_from_tree (loc: decl, want_address: decl_by_reference_p (decl) ? 0 : 2,
21050 NULL);
21051 /* It is usually worth caching this result if the decl is from
21052 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
21053 if (cache_p && list && list->dw_loc_next)
21054 {
21055 cached_dw_loc_list **slot
21056 = cached_dw_loc_list_table->find_slot_with_hash (comparable: decl,
21057 DECL_UID (decl),
21058 insert: INSERT);
21059 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
21060 cache->decl_id = DECL_UID (decl);
21061 cache->loc_list = list;
21062 *slot = cache;
21063 }
21064 }
21065 if (list)
21066 {
21067 add_AT_location_description (die, attr_kind: DW_AT_location, descr: list);
21068 return true;
21069 }
21070 /* None of that worked, so it must not really have a location;
21071 try adding a constant value attribute from the DECL_INITIAL. */
21072 return tree_add_const_value_attribute_for_decl (die, decl);
21073}
21074
21075/* Mangle referenced decls. */
21076static tree
21077mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
21078{
21079 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
21080 *walk_subtrees = 0;
21081
21082 if (VAR_OR_FUNCTION_DECL_P (*tp))
21083 assign_assembler_name_if_needed (*tp);
21084
21085 return NULL_TREE;
21086}
21087
21088/* Attach a DW_AT_const_value attribute to DIE. The value of the
21089 attribute is the const value T. */
21090
21091static bool
21092tree_add_const_value_attribute (dw_die_ref die, tree t)
21093{
21094 tree init;
21095 tree type = TREE_TYPE (t);
21096
21097 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21098 return false;
21099
21100 init = t;
21101 gcc_assert (!DECL_P (init));
21102
21103 if (TREE_CODE (init) == INTEGER_CST)
21104 {
21105 if (tree_fits_uhwi_p (init))
21106 {
21107 add_AT_unsigned (die, attr_kind: DW_AT_const_value, unsigned_val: tree_to_uhwi (init));
21108 return true;
21109 }
21110 if (tree_fits_shwi_p (init))
21111 {
21112 add_AT_int (die, attr_kind: DW_AT_const_value, int_val: tree_to_shwi (init));
21113 return true;
21114 }
21115 }
21116 if (!early_dwarf)
21117 {
21118 rtx rtl = rtl_for_decl_init (init, type);
21119 if (rtl)
21120 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21121 }
21122 else
21123 {
21124 /* For early_dwarf force mangling of all referenced symbols. */
21125 tree initializer = init;
21126 STRIP_NOPS (initializer);
21127 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21128 if (AGGREGATE_TYPE_P (type)
21129 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21130 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21131 || TREE_CODE (type) == COMPLEX_TYPE)
21132 ;
21133 else if (initializer_constant_valid_p (initializer, type))
21134 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21135 }
21136 /* If the host and target are sane, try harder. */
21137 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21138 && initializer_constant_valid_p (init, type))
21139 {
21140 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21141 if (size > 0 && (int) size == size)
21142 {
21143 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (c: size);
21144
21145 if (native_encode_initializer (init, array, size) == size)
21146 {
21147 add_AT_vec (die, attr_kind: DW_AT_const_value, length: size, elt_size: 1, array);
21148 return true;
21149 }
21150 ggc_free (array);
21151 }
21152 }
21153 return false;
21154}
21155
21156/* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21157 attribute is the const value of T, where T is an integral constant
21158 variable with static storage duration
21159 (so it can't be a PARM_DECL or a RESULT_DECL). */
21160
21161static bool
21162tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21163{
21164
21165 if (!decl
21166 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21167 || (VAR_P (decl) && !TREE_STATIC (decl)))
21168 return false;
21169
21170 if (TREE_READONLY (decl)
21171 && ! TREE_THIS_VOLATILE (decl)
21172 && DECL_INITIAL (decl))
21173 /* OK */;
21174 else
21175 return false;
21176
21177 /* Don't add DW_AT_const_value if abstract origin already has one. */
21178 if (get_AT (die: var_die, attr_kind: DW_AT_const_value))
21179 return false;
21180
21181 return tree_add_const_value_attribute (die: var_die, DECL_INITIAL (decl));
21182}
21183
21184/* Convert the CFI instructions for the current function into a
21185 location list. This is used for DW_AT_frame_base when we targeting
21186 a dwarf2 consumer that does not support the dwarf3
21187 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21188 expressions. */
21189
21190static dw_loc_list_ref
21191convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21192{
21193 int ix;
21194 dw_fde_ref fde;
21195 dw_loc_list_ref list, *list_tail;
21196 dw_cfi_ref cfi;
21197 dw_cfa_location last_cfa, next_cfa;
21198 const char *start_label, *last_label, *section;
21199 dw_cfa_location remember;
21200
21201 fde = cfun->fde;
21202 gcc_assert (fde != NULL);
21203
21204 section = secname_for_decl (decl: current_function_decl);
21205 list_tail = &list;
21206 list = NULL;
21207
21208 memset (s: &next_cfa, c: 0, n: sizeof (next_cfa));
21209
21210#ifdef CODEVIEW_DEBUGGING_INFO
21211 /* We can write simplified frame base information for CodeView, as we're
21212 not using it for rewinding. */
21213 if (codeview_debuginfo_p ())
21214 {
21215 int dwreg = DEBUGGER_REGNO (cfun->machine->fs.cfa_reg->u.reg.regno);
21216
21217 next_cfa.reg.set_by_dwreg (dwreg);
21218 next_cfa.offset = cfun->machine->fs.fp_valid
21219 ? cfun->machine->fs.fp_offset : cfun->machine->fs.sp_offset;
21220
21221 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21222 fde->dw_fde_begin, 0,
21223 fde->dw_fde_second_begin
21224 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21225 section);
21226 maybe_gen_llsym (list);
21227
21228 return list;
21229 }
21230#endif
21231
21232 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21233 remember = next_cfa;
21234
21235 start_label = fde->dw_fde_begin;
21236
21237 /* ??? Bald assumption that the CIE opcode list does not contain
21238 advance opcodes. */
21239 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21240 lookup_cfa_1 (cfi, loc: &next_cfa, remember: &remember);
21241
21242 last_cfa = next_cfa;
21243 last_label = start_label;
21244
21245 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21246 {
21247 /* If the first partition contained no CFI adjustments, the
21248 CIE opcodes apply to the whole first partition. */
21249 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &last_cfa, offset),
21250 begin: fde->dw_fde_begin, vbegin: 0, end: fde->dw_fde_end, vend: 0, section);
21251 list_tail =&(*list_tail)->dw_loc_next;
21252 start_label = last_label = fde->dw_fde_second_begin;
21253 }
21254
21255 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21256 {
21257 switch (cfi->dw_cfi_opc)
21258 {
21259 case DW_CFA_set_loc:
21260 case DW_CFA_advance_loc1:
21261 case DW_CFA_advance_loc2:
21262 case DW_CFA_advance_loc4:
21263 if (!cfa_equal_p (&last_cfa, &next_cfa))
21264 {
21265 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &last_cfa, offset),
21266 begin: start_label, vbegin: 0, end: last_label, vend: 0, section);
21267
21268 list_tail = &(*list_tail)->dw_loc_next;
21269 last_cfa = next_cfa;
21270 start_label = last_label;
21271 }
21272 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21273 break;
21274
21275 case DW_CFA_advance_loc:
21276 /* The encoding is complex enough that we should never emit this. */
21277 gcc_unreachable ();
21278
21279 default:
21280 lookup_cfa_1 (cfi, loc: &next_cfa, remember: &remember);
21281 break;
21282 }
21283 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21284 {
21285 if (!cfa_equal_p (&last_cfa, &next_cfa))
21286 {
21287 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &last_cfa, offset),
21288 begin: start_label, vbegin: 0, end: last_label, vend: 0, section);
21289
21290 list_tail = &(*list_tail)->dw_loc_next;
21291 last_cfa = next_cfa;
21292 start_label = last_label;
21293 }
21294 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &last_cfa, offset),
21295 begin: start_label, vbegin: 0, end: fde->dw_fde_end, vend: 0, section);
21296 list_tail = &(*list_tail)->dw_loc_next;
21297 start_label = last_label = fde->dw_fde_second_begin;
21298 }
21299 }
21300
21301 if (!cfa_equal_p (&last_cfa, &next_cfa))
21302 {
21303 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &last_cfa, offset),
21304 begin: start_label, vbegin: 0, end: last_label, vend: 0, section);
21305 list_tail = &(*list_tail)->dw_loc_next;
21306 start_label = last_label;
21307 }
21308
21309 *list_tail = new_loc_list (expr: build_cfa_loc (cfa: &next_cfa, offset),
21310 begin: start_label, vbegin: 0,
21311 end: fde->dw_fde_second_begin
21312 ? fde->dw_fde_second_end : fde->dw_fde_end, vend: 0,
21313 section);
21314
21315 maybe_gen_llsym (list);
21316
21317 return list;
21318}
21319
21320/* Compute a displacement from the "steady-state frame pointer" to the
21321 frame base (often the same as the CFA), and store it in
21322 frame_pointer_fb_offset. OFFSET is added to the displacement
21323 before the latter is negated. */
21324
21325static void
21326compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21327{
21328 rtx reg, elim;
21329
21330#ifdef FRAME_POINTER_CFA_OFFSET
21331 reg = frame_pointer_rtx;
21332 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21333#else
21334 reg = arg_pointer_rtx;
21335 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21336#endif
21337
21338 elim = (ira_use_lra_p
21339 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21340 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21341 elim = strip_offset_and_add (x: elim, offset: &offset);
21342
21343 frame_pointer_fb_offset = -offset;
21344
21345 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21346 in which to eliminate. This is because it's stack pointer isn't
21347 directly accessible as a register within the ISA. To work around
21348 this, assume that while we cannot provide a proper value for
21349 frame_pointer_fb_offset, we won't need one either. We can use
21350 hard frame pointer in debug info even if frame pointer isn't used
21351 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21352 which uses the DW_AT_frame_base attribute, not hard frame pointer
21353 directly. */
21354 frame_pointer_fb_offset_valid
21355 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21356}
21357
21358/* Generate a DW_AT_name attribute given some string value to be included as
21359 the value of the attribute. */
21360
21361void
21362add_name_attribute (dw_die_ref die, const char *name_string)
21363{
21364 if (name_string != NULL && *name_string != 0)
21365 {
21366 if (demangle_name_func)
21367 name_string = (*demangle_name_func) (name_string);
21368
21369 add_AT_string (die, attr_kind: DW_AT_name, str: name_string);
21370 }
21371}
21372
21373/* Generate a DW_AT_name attribute given some string value representing a
21374 file or filepath to be included as value of the attribute. */
21375static void
21376add_filename_attribute (dw_die_ref die, const char *name_string)
21377{
21378 if (name_string != NULL && *name_string != 0)
21379 add_filepath_AT_string (die, attr_kind: DW_AT_name, str: name_string);
21380}
21381
21382/* Generate a DW_AT_description attribute given some string value to be included
21383 as the value of the attribute. */
21384
21385static void
21386add_desc_attribute (dw_die_ref die, const char *name_string)
21387{
21388 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21389 return;
21390
21391 if (name_string == NULL || *name_string == 0)
21392 return;
21393
21394 if (demangle_name_func)
21395 name_string = (*demangle_name_func) (name_string);
21396
21397 add_AT_string (die, attr_kind: DW_AT_description, str: name_string);
21398}
21399
21400/* Generate a DW_AT_description attribute given some decl to be included
21401 as the value of the attribute. */
21402
21403static void
21404add_desc_attribute (dw_die_ref die, tree decl)
21405{
21406 tree decl_name;
21407
21408 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21409 return;
21410
21411 if (decl == NULL_TREE || !DECL_P (decl))
21412 return;
21413 decl_name = DECL_NAME (decl);
21414
21415 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21416 {
21417 const char *name = dwarf2_name (decl, scope: 0);
21418 add_desc_attribute (die, name_string: name ? name : IDENTIFIER_POINTER (decl_name));
21419 }
21420 else
21421 {
21422 char *desc = print_generic_expr_to_str (decl);
21423 add_desc_attribute (die, name_string: desc);
21424 free (ptr: desc);
21425 }
21426}
21427
21428/* Retrieve the descriptive type of TYPE, if any, make sure it has a
21429 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21430 of TYPE accordingly.
21431
21432 ??? This is a temporary measure until after we're able to generate
21433 regular DWARF for the complex Ada type system. */
21434
21435static void
21436add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21437 dw_die_ref context_die)
21438{
21439 tree dtype;
21440 dw_die_ref dtype_die;
21441
21442 if (!lang_hooks.types.descriptive_type)
21443 return;
21444
21445 dtype = lang_hooks.types.descriptive_type (type);
21446 if (!dtype)
21447 return;
21448
21449 dtype_die = lookup_type_die (type: dtype);
21450 if (!dtype_die)
21451 {
21452 gen_type_die (dtype, context_die);
21453 dtype_die = lookup_type_die (type: dtype);
21454 gcc_assert (dtype_die);
21455 }
21456
21457 add_AT_die_ref (die, attr_kind: DW_AT_GNAT_descriptive_type, targ_die: dtype_die);
21458}
21459
21460/* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21461
21462static const char *
21463comp_dir_string (void)
21464{
21465 const char *wd;
21466 char *wd_plus_sep = NULL;
21467 static const char *cached_wd = NULL;
21468
21469 if (cached_wd != NULL)
21470 return cached_wd;
21471
21472 wd = get_src_pwd ();
21473 if (wd == NULL)
21474 return NULL;
21475
21476 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21477 {
21478 size_t wdlen = strlen (s: wd);
21479 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21480 strcpy (dest: wd_plus_sep, src: wd);
21481 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21482 wd_plus_sep [wdlen + 1] = 0;
21483 wd = wd_plus_sep;
21484 }
21485
21486 cached_wd = remap_debug_filename (wd);
21487
21488 /* remap_debug_filename can just pass through wd or return a new gc string.
21489 These two types can't be both stored in a GTY(())-tagged string, but since
21490 the cached value lives forever just copy it if needed. */
21491 if (cached_wd != wd)
21492 {
21493 cached_wd = xstrdup (cached_wd);
21494 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21495 free (ptr: wd_plus_sep);
21496 }
21497
21498 return cached_wd;
21499}
21500
21501/* Generate a DW_AT_comp_dir attribute for DIE. */
21502
21503static void
21504add_comp_dir_attribute (dw_die_ref die)
21505{
21506 const char * wd = comp_dir_string ();
21507 if (wd != NULL)
21508 add_filepath_AT_string (die, attr_kind: DW_AT_comp_dir, str: wd);
21509}
21510
21511/* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21512 pointer computation, ...), output a representation for that bound according
21513 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21514 loc_list_from_tree for the meaning of CONTEXT. */
21515
21516static void
21517add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21518 int forms, struct loc_descr_context *context)
21519{
21520 dw_die_ref context_die, decl_die = NULL;
21521 dw_loc_list_ref list;
21522 bool strip_conversions = true;
21523 bool placeholder_seen = false;
21524
21525 while (strip_conversions)
21526 switch (TREE_CODE (value))
21527 {
21528 case ERROR_MARK:
21529 case SAVE_EXPR:
21530 return;
21531
21532 CASE_CONVERT:
21533 case VIEW_CONVERT_EXPR:
21534 value = TREE_OPERAND (value, 0);
21535 break;
21536
21537 default:
21538 strip_conversions = false;
21539 break;
21540 }
21541
21542 /* If possible and permitted, output the attribute as a constant. */
21543 if ((forms & dw_scalar_form_constant) != 0
21544 && TREE_CODE (value) == INTEGER_CST)
21545 {
21546 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21547
21548 /* If HOST_WIDE_INT is big enough then represent the bound as
21549 a constant value. We need to choose a form based on
21550 whether the type is signed or unsigned. We cannot just
21551 call add_AT_unsigned if the value itself is positive
21552 (add_AT_unsigned might add the unsigned value encoded as
21553 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21554 bounds type and then sign extend any unsigned values found
21555 for signed types. This is needed only for
21556 DW_AT_{lower,upper}_bound, since for most other attributes,
21557 consumers will treat DW_FORM_data[1248] as unsigned values,
21558 regardless of the underlying type. */
21559 if (prec <= HOST_BITS_PER_WIDE_INT
21560 || tree_fits_uhwi_p (value))
21561 {
21562 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21563 add_AT_unsigned (die, attr_kind: attr, TREE_INT_CST_LOW (value));
21564 else
21565 add_AT_int (die, attr_kind: attr, TREE_INT_CST_LOW (value));
21566 }
21567 else if (dwarf_version >= 5
21568 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21569 /* Otherwise represent the bound as an unsigned value with
21570 the precision of its type. The precision and signedness
21571 of the type will be necessary to re-interpret it
21572 unambiguously. */
21573 add_AT_wide (die, attr_kind: attr, w: wi::to_wide (t: value));
21574 else
21575 {
21576 rtx v = immed_wide_int_const (wi::to_wide (t: value),
21577 TYPE_MODE (TREE_TYPE (value)));
21578 dw_loc_descr_ref loc
21579 = loc_descriptor (rtl: v, TYPE_MODE (TREE_TYPE (value)),
21580 initialized: VAR_INIT_STATUS_INITIALIZED);
21581 if (loc)
21582 add_AT_loc (die, attr_kind: attr, loc);
21583 }
21584 return;
21585 }
21586
21587 /* Otherwise, if it's possible and permitted too, output a reference to
21588 another DIE. */
21589 if ((forms & dw_scalar_form_reference) != 0)
21590 {
21591 tree decl = NULL_TREE;
21592
21593 /* Some type attributes reference an outer type. For instance, the upper
21594 bound of an array may reference an embedding record (this happens in
21595 Ada). */
21596 if (TREE_CODE (value) == COMPONENT_REF
21597 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21598 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21599 decl = TREE_OPERAND (value, 1);
21600
21601 else if (VAR_P (value)
21602 || TREE_CODE (value) == PARM_DECL
21603 || TREE_CODE (value) == RESULT_DECL)
21604 decl = value;
21605
21606 if (decl != NULL_TREE)
21607 {
21608 decl_die = lookup_decl_die (decl);
21609
21610 /* ??? Can this happen, or should the variable have been bound
21611 first? Probably it can, since I imagine that we try to create
21612 the types of parameters in the order in which they exist in
21613 the list, and won't have created a forward reference to a
21614 later parameter. */
21615 if (decl_die != NULL)
21616 {
21617 if (get_AT (die: decl_die, attr_kind: DW_AT_location)
21618 || get_AT (die: decl_die, attr_kind: DW_AT_data_member_location)
21619 || get_AT (die: decl_die, attr_kind: DW_AT_data_bit_offset)
21620 || get_AT (die: decl_die, attr_kind: DW_AT_const_value))
21621 {
21622 add_AT_die_ref (die, attr_kind: attr, targ_die: decl_die);
21623 return;
21624 }
21625 }
21626 }
21627 }
21628
21629 /* Last chance: try to create a stack operation procedure to evaluate the
21630 value. Do nothing if even that is not possible or permitted. */
21631 if ((forms & dw_scalar_form_exprloc) == 0)
21632 return;
21633
21634 list = loc_list_from_tree (loc: value, want_address: 2, context);
21635 if (context && context->placeholder_arg)
21636 {
21637 placeholder_seen = context->placeholder_seen;
21638 context->placeholder_seen = false;
21639 }
21640 if (list == NULL || single_element_loc_list_p (list))
21641 {
21642 /* If this attribute is not a reference nor constant, it is
21643 a DWARF expression rather than location description. For that
21644 loc_list_from_tree (value, 0, &context) is needed. */
21645 dw_loc_list_ref list2 = loc_list_from_tree (loc: value, want_address: 0, context);
21646 if (list2 && single_element_loc_list_p (list: list2))
21647 {
21648 if (placeholder_seen)
21649 {
21650 struct dwarf_procedure_info dpi;
21651 dpi.fndecl = NULL_TREE;
21652 dpi.args_count = 1;
21653 if (!resolve_args_picking (loc: list2->expr, initial_frame_offset: 1, dpi: &dpi))
21654 return;
21655 }
21656 add_AT_loc (die, attr_kind: attr, loc: list2->expr);
21657 return;
21658 }
21659 }
21660
21661 /* If that failed to give a single element location list, fall back to
21662 outputting this as a reference... still if permitted. */
21663 if (list == NULL
21664 || (forms & dw_scalar_form_reference) == 0
21665 || placeholder_seen)
21666 return;
21667
21668 if (!decl_die)
21669 {
21670 if (current_function_decl == 0)
21671 context_die = comp_unit_die ();
21672 else
21673 context_die = lookup_decl_die (decl: current_function_decl);
21674
21675 decl_die = new_die (tag_value: DW_TAG_variable, parent_die: context_die, t: value);
21676 add_AT_flag (die: decl_die, attr_kind: DW_AT_artificial, flag: 1);
21677 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21678 context_die);
21679 }
21680
21681 add_AT_location_description (die: decl_die, attr_kind: DW_AT_location, descr: list);
21682 add_AT_die_ref (die, attr_kind: attr, targ_die: decl_die);
21683}
21684
21685/* Return the default for DW_AT_lower_bound, or -1 if there is not any
21686 default. */
21687
21688static int
21689lower_bound_default (void)
21690{
21691 switch (get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language))
21692 {
21693 case DW_LANG_C:
21694 case DW_LANG_C89:
21695 case DW_LANG_C99:
21696 case DW_LANG_C11:
21697 case DW_LANG_C_plus_plus:
21698 case DW_LANG_C_plus_plus_11:
21699 case DW_LANG_C_plus_plus_14:
21700 case DW_LANG_ObjC:
21701 case DW_LANG_ObjC_plus_plus:
21702 return 0;
21703 case DW_LANG_Fortran77:
21704 case DW_LANG_Fortran90:
21705 case DW_LANG_Fortran95:
21706 case DW_LANG_Fortran03:
21707 case DW_LANG_Fortran08:
21708 return 1;
21709 case DW_LANG_UPC:
21710 case DW_LANG_D:
21711 case DW_LANG_Python:
21712 return dwarf_version >= 4 ? 0 : -1;
21713 case DW_LANG_Ada95:
21714 case DW_LANG_Ada83:
21715 case DW_LANG_Cobol74:
21716 case DW_LANG_Cobol85:
21717 case DW_LANG_Modula2:
21718 case DW_LANG_PLI:
21719 return dwarf_version >= 4 ? 1 : -1;
21720 default:
21721 return -1;
21722 }
21723}
21724
21725/* Given a tree node describing an array bound (either lower or upper) output
21726 a representation for that bound. */
21727
21728static void
21729add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21730 tree bound, struct loc_descr_context *context)
21731{
21732 int dflt;
21733
21734 while (1)
21735 switch (TREE_CODE (bound))
21736 {
21737 /* Strip all conversions. */
21738 CASE_CONVERT:
21739 case VIEW_CONVERT_EXPR:
21740 bound = TREE_OPERAND (bound, 0);
21741 break;
21742
21743 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21744 are even omitted when they are the default. */
21745 case INTEGER_CST:
21746 /* If the value for this bound is the default one, we can even omit the
21747 attribute. */
21748 if (bound_attr == DW_AT_lower_bound
21749 && tree_fits_shwi_p (bound)
21750 && (dflt = lower_bound_default ()) != -1
21751 && tree_to_shwi (bound) == dflt)
21752 return;
21753
21754 /* FALLTHRU */
21755
21756 default:
21757 /* Let GNAT encodings do the magic for self-referential bounds. */
21758 if (is_ada ()
21759 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21760 && contains_placeholder_p (bound))
21761 return;
21762
21763 add_scalar_info (die: subrange_die, attr: bound_attr, value: bound,
21764 forms: dw_scalar_form_constant
21765 | dw_scalar_form_exprloc
21766 | dw_scalar_form_reference,
21767 context);
21768 return;
21769 }
21770}
21771
21772/* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21773 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21774
21775 This function reuses previously set type and bound information if
21776 available. */
21777
21778static void
21779add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21780{
21781 dw_die_ref child = type_die->die_child;
21782 struct array_descr_info info;
21783 int dimension_number;
21784
21785 if (lang_hooks.types.get_array_descr_info)
21786 {
21787 memset (s: &info, c: 0, n: sizeof (info));
21788 if (lang_hooks.types.get_array_descr_info (type, &info))
21789 /* Fortran sometimes emits array types with no dimension. */
21790 gcc_assert (info.ndimensions >= 0
21791 && info.ndimensions
21792 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21793 }
21794 else
21795 info.ndimensions = 0;
21796
21797 for (dimension_number = 0;
21798 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21799 type = TREE_TYPE (type), dimension_number++)
21800 {
21801 tree domain = TYPE_DOMAIN (type);
21802
21803 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21804 break;
21805
21806 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21807 and (in GNU C only) variable bounds. Handle all three forms
21808 here. */
21809
21810 /* Find and reuse a previously generated DW_TAG_subrange_type if
21811 available.
21812
21813 For multi-dimensional arrays, as we iterate through the
21814 various dimensions in the enclosing for loop above, we also
21815 iterate through the DIE children and pick at each
21816 DW_TAG_subrange_type previously generated (if available).
21817 Each child DW_TAG_subrange_type DIE describes the range of
21818 the current dimension. At this point we should have as many
21819 DW_TAG_subrange_type's as we have dimensions in the
21820 array. */
21821 dw_die_ref subrange_die = NULL;
21822 if (child)
21823 while (1)
21824 {
21825 child = child->die_sib;
21826 if (child->die_tag == DW_TAG_subrange_type)
21827 subrange_die = child;
21828 if (child == type_die->die_child)
21829 {
21830 /* If we wrapped around, stop looking next time. */
21831 child = NULL;
21832 break;
21833 }
21834 if (child->die_tag == DW_TAG_subrange_type)
21835 break;
21836 }
21837 if (!subrange_die)
21838 subrange_die = new_die (tag_value: DW_TAG_subrange_type, parent_die: type_die, NULL);
21839
21840 if (domain)
21841 {
21842 /* We have an array type with specified bounds. */
21843 tree lower = TYPE_MIN_VALUE (domain);
21844 tree upper = TYPE_MAX_VALUE (domain);
21845 tree index_type = TREE_TYPE (domain);
21846
21847 if (dimension_number <= info.ndimensions - 1)
21848 {
21849 lower = info.dimen[dimension_number].lower_bound;
21850 upper = info.dimen[dimension_number].upper_bound;
21851 index_type = info.dimen[dimension_number].bounds_type;
21852 }
21853
21854 /* Define the index type. */
21855 if (index_type && !get_AT (die: subrange_die, attr_kind: DW_AT_type))
21856 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21857 false, type_die);
21858
21859 /* ??? If upper is NULL, the array has unspecified length,
21860 but it does have a lower bound. This happens with Fortran
21861 dimension arr(N:*)
21862 Since the debugger is definitely going to need to know N
21863 to produce useful results, go ahead and output the lower
21864 bound solo, and hope the debugger can cope. */
21865
21866 if (lower && !get_AT (die: subrange_die, attr_kind: DW_AT_lower_bound))
21867 add_bound_info (subrange_die, bound_attr: DW_AT_lower_bound, bound: lower, NULL);
21868
21869 if (!get_AT (die: subrange_die, attr_kind: DW_AT_upper_bound)
21870 && !get_AT (die: subrange_die, attr_kind: DW_AT_count))
21871 {
21872 if (upper)
21873 add_bound_info (subrange_die, bound_attr: DW_AT_upper_bound, bound: upper, NULL);
21874 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21875 /* Zero-length array. */
21876 add_bound_info (subrange_die, bound_attr: DW_AT_count,
21877 bound: build_int_cst (TREE_TYPE (lower), 0), NULL);
21878 }
21879 }
21880
21881 /* Otherwise we have an array type with an unspecified length. The
21882 DWARF-2 spec does not say how to handle this; let's just leave out the
21883 bounds. */
21884 }
21885}
21886
21887/* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21888
21889static void
21890add_byte_size_attribute (dw_die_ref die, tree tree_node)
21891{
21892 dw_die_ref decl_die;
21893 HOST_WIDE_INT size;
21894
21895 switch (TREE_CODE (tree_node))
21896 {
21897 case ERROR_MARK:
21898 size = 0;
21899 break;
21900 case ENUMERAL_TYPE:
21901 case RECORD_TYPE:
21902 case UNION_TYPE:
21903 case QUAL_UNION_TYPE:
21904 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21905 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21906 {
21907 add_AT_die_ref (die, attr_kind: DW_AT_byte_size, targ_die: decl_die);
21908 return;
21909 }
21910 size = int_size_in_bytes (tree_node);
21911 break;
21912 case FIELD_DECL:
21913 /* For a data member of a struct or union, the DW_AT_byte_size is
21914 generally given as the number of bytes normally allocated for an
21915 object of the *declared* type of the member itself. This is true
21916 even for bit-fields. */
21917 size = int_size_in_bytes (field_type (decl: tree_node));
21918 break;
21919 default:
21920 gcc_unreachable ();
21921 }
21922
21923 /* Note that `size' might be -1 when we get to this point. If it is, that
21924 indicates that the byte size of the entity in question is variable. */
21925 if (size >= 0)
21926 add_AT_unsigned (die, attr_kind: DW_AT_byte_size, unsigned_val: size);
21927
21928 /* Support for dynamically-sized objects was introduced in DWARF3. */
21929 else if (TYPE_P (tree_node)
21930 && (dwarf_version >= 3 || !dwarf_strict)
21931 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21932 {
21933 struct loc_descr_context ctx = {
21934 .context_type: const_cast<tree> (tree_node), /* context_type */
21935 NULL_TREE, /* base_decl */
21936 NULL, /* dpi */
21937 .placeholder_arg: false, /* placeholder_arg */
21938 .placeholder_seen: false, /* placeholder_seen */
21939 .strict_signedness: false /* strict_signedness */
21940 };
21941
21942 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21943 add_scalar_info (die, attr: DW_AT_byte_size, value: tree_size,
21944 forms: dw_scalar_form_constant
21945 | dw_scalar_form_exprloc
21946 | dw_scalar_form_reference,
21947 context: &ctx);
21948 }
21949}
21950
21951/* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21952 alignment. */
21953
21954static void
21955add_alignment_attribute (dw_die_ref die, tree tree_node)
21956{
21957 if (dwarf_version < 5 && dwarf_strict)
21958 return;
21959
21960 unsigned align;
21961
21962 if (DECL_P (tree_node))
21963 {
21964 if (!DECL_USER_ALIGN (tree_node))
21965 return;
21966
21967 align = DECL_ALIGN_UNIT (tree_node);
21968 }
21969 else if (TYPE_P (tree_node))
21970 {
21971 if (!TYPE_USER_ALIGN (tree_node))
21972 return;
21973
21974 align = TYPE_ALIGN_UNIT (tree_node);
21975 }
21976 else
21977 gcc_unreachable ();
21978
21979 add_AT_unsigned (die, attr_kind: DW_AT_alignment, unsigned_val: align);
21980}
21981
21982/* For a FIELD_DECL node which represents a bit-field, output an attribute
21983 which specifies the distance in bits from the highest order bit of the
21984 "containing object" for the bit-field to the highest order bit of the
21985 bit-field itself.
21986
21987 For any given bit-field, the "containing object" is a hypothetical object
21988 (of some integral or enum type) within which the given bit-field lives. The
21989 type of this hypothetical "containing object" is always the same as the
21990 declared type of the individual bit-field itself. The determination of the
21991 exact location of the "containing object" for a bit-field is rather
21992 complicated. It's handled by the `field_byte_offset' function (above).
21993
21994 Note that it is the size (in bytes) of the hypothetical "containing object"
21995 which will be given in the DW_AT_byte_size attribute for this bit-field.
21996 (See `byte_size_attribute' above). */
21997
21998static inline void
21999add_bit_offset_attribute (dw_die_ref die, tree decl)
22000{
22001 HOST_WIDE_INT object_offset_in_bytes;
22002 tree original_type = DECL_BIT_FIELD_TYPE (decl);
22003 HOST_WIDE_INT bitpos_int;
22004 HOST_WIDE_INT highest_order_object_bit_offset;
22005 HOST_WIDE_INT highest_order_field_bit_offset;
22006 HOST_WIDE_INT bit_offset;
22007
22008 /* The containing object is within the DECL_CONTEXT. */
22009 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
22010
22011 field_byte_offset (decl, ctx: &ctx, cst_offset: &object_offset_in_bytes);
22012
22013 /* Must be a field and a bit field. */
22014 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
22015
22016 /* We can't yet handle bit-fields whose offsets are variable, so if we
22017 encounter such things, just return without generating any attribute
22018 whatsoever. Likewise for variable or too large size. */
22019 if (! tree_fits_shwi_p (bit_position (decl))
22020 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
22021 return;
22022
22023 bitpos_int = int_bit_position (field: decl);
22024
22025 /* Note that the bit offset is always the distance (in bits) from the
22026 highest-order bit of the "containing object" to the highest-order bit of
22027 the bit-field itself. Since the "high-order end" of any object or field
22028 is different on big-endian and little-endian machines, the computation
22029 below must take account of these differences. */
22030 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
22031 highest_order_field_bit_offset = bitpos_int;
22032
22033 if (! BYTES_BIG_ENDIAN)
22034 {
22035 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
22036 highest_order_object_bit_offset +=
22037 simple_type_size_in_bits (type: original_type);
22038 }
22039
22040 bit_offset
22041 = (! BYTES_BIG_ENDIAN
22042 ? highest_order_object_bit_offset - highest_order_field_bit_offset
22043 : highest_order_field_bit_offset - highest_order_object_bit_offset);
22044
22045 if (bit_offset < 0)
22046 add_AT_int (die, attr_kind: DW_AT_bit_offset, int_val: bit_offset);
22047 else
22048 add_AT_unsigned (die, attr_kind: DW_AT_bit_offset, unsigned_val: (unsigned HOST_WIDE_INT) bit_offset);
22049}
22050
22051/* For a FIELD_DECL node which represents a bit field, output an attribute
22052 which specifies the length in bits of the given field. */
22053
22054static inline void
22055add_bit_size_attribute (dw_die_ref die, tree decl)
22056{
22057 /* Must be a field and a bit field. */
22058 gcc_assert (TREE_CODE (decl) == FIELD_DECL
22059 && DECL_BIT_FIELD_TYPE (decl));
22060
22061 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
22062 add_AT_unsigned (die, attr_kind: DW_AT_bit_size, unsigned_val: tree_to_uhwi (DECL_SIZE (decl)));
22063}
22064
22065/* If the compiled language is ANSI C, then add a 'prototyped'
22066 attribute, if arg types are given for the parameters of a function. */
22067
22068static inline void
22069add_prototyped_attribute (dw_die_ref die, tree func_type)
22070{
22071 switch (get_AT_unsigned (die: comp_unit_die (), attr_kind: DW_AT_language))
22072 {
22073 case DW_LANG_C:
22074 case DW_LANG_C89:
22075 case DW_LANG_C99:
22076 case DW_LANG_C11:
22077 case DW_LANG_ObjC:
22078 if (prototype_p (func_type))
22079 add_AT_flag (die, attr_kind: DW_AT_prototyped, flag: 1);
22080 break;
22081 default:
22082 break;
22083 }
22084}
22085
22086/* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
22087 by looking in the type declaration, the object declaration equate table or
22088 the block mapping. */
22089
22090static inline void
22091add_abstract_origin_attribute (dw_die_ref die, tree origin)
22092{
22093 dw_die_ref origin_die = NULL;
22094
22095 /* For late LTO debug output we want to refer directly to the abstract
22096 DIE in the early debug rather to the possibly existing concrete
22097 instance and avoid creating that just for this purpose. */
22098 sym_off_pair *desc;
22099 if (in_lto_p
22100 && external_die_map
22101 && (desc = external_die_map->get (k: origin)))
22102 {
22103 add_AT_external_die_ref (die, attr_kind: DW_AT_abstract_origin,
22104 symbol: desc->sym, offset: desc->off);
22105 return;
22106 }
22107
22108 if (DECL_P (origin))
22109 origin_die = lookup_decl_die (decl: origin);
22110 else if (TYPE_P (origin))
22111 origin_die = lookup_type_die (type: origin);
22112 else if (TREE_CODE (origin) == BLOCK)
22113 origin_die = lookup_block_die (block: origin);
22114
22115 /* XXX: Functions that are never lowered don't always have correct block
22116 trees (in the case of java, they simply have no block tree, in some other
22117 languages). For these functions, there is nothing we can really do to
22118 output correct debug info for inlined functions in all cases. Rather
22119 than die, we'll just produce deficient debug info now, in that we will
22120 have variables without a proper abstract origin. In the future, when all
22121 functions are lowered, we should re-add a gcc_assert (origin_die)
22122 here. */
22123
22124 if (origin_die)
22125 {
22126 dw_attr_node *a;
22127 /* Like above, if we already created a concrete instance DIE
22128 do not use that for the abstract origin but the early DIE
22129 if present. */
22130 if (in_lto_p
22131 && (a = get_AT (die: origin_die, attr_kind: DW_AT_abstract_origin)))
22132 origin_die = AT_ref (a);
22133 add_AT_die_ref (die, attr_kind: DW_AT_abstract_origin, targ_die: origin_die);
22134 }
22135}
22136
22137/* We do not currently support the pure_virtual attribute. */
22138
22139static inline void
22140add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22141{
22142 if (DECL_VINDEX (func_decl))
22143 {
22144 add_AT_unsigned (die, attr_kind: DW_AT_virtuality, unsigned_val: DW_VIRTUALITY_virtual);
22145
22146 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22147 add_AT_loc (die, attr_kind: DW_AT_vtable_elem_location,
22148 loc: new_loc_descr (op: DW_OP_constu,
22149 oprnd1: tree_to_shwi (DECL_VINDEX (func_decl)),
22150 oprnd2: 0));
22151
22152 /* GNU extension: Record what type this method came from originally. */
22153 if (debug_info_level > DINFO_LEVEL_TERSE
22154 && DECL_CONTEXT (func_decl))
22155 add_AT_die_ref (die, attr_kind: DW_AT_containing_type,
22156 targ_die: lookup_type_die (DECL_CONTEXT (func_decl)));
22157 }
22158}
22159
22160/* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22161 given decl. This used to be a vendor extension until after DWARF 4
22162 standardized it. */
22163
22164static void
22165add_linkage_attr (dw_die_ref die, tree decl)
22166{
22167 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22168
22169 /* Mimic what assemble_name_raw does with a leading '*'. */
22170 if (name[0] == '*')
22171 name = &name[1];
22172
22173 if (dwarf_version >= 4)
22174 add_AT_string (die, attr_kind: DW_AT_linkage_name, str: name);
22175 else
22176 add_AT_string (die, attr_kind: DW_AT_MIPS_linkage_name, str: name);
22177}
22178
22179/* Add source coordinate attributes for the given decl. */
22180
22181static void
22182add_src_coords_attributes (dw_die_ref die, tree decl)
22183{
22184 expanded_location s;
22185
22186 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22187 return;
22188 s = expand_location (DECL_SOURCE_LOCATION (decl));
22189 add_AT_file (die, attr_kind: DW_AT_decl_file, fd: lookup_filename (s.file));
22190 add_AT_unsigned (die, attr_kind: DW_AT_decl_line, unsigned_val: s.line);
22191 if (debug_column_info && s.column)
22192 add_AT_unsigned (die, attr_kind: DW_AT_decl_column, unsigned_val: s.column);
22193}
22194
22195/* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22196
22197static void
22198add_linkage_name_raw (dw_die_ref die, tree decl)
22199{
22200 /* Defer until we have an assembler name set. */
22201 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22202 {
22203 limbo_die_node *asm_name;
22204
22205 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22206 asm_name->die = die;
22207 asm_name->created_for = decl;
22208 asm_name->next = deferred_asm_name;
22209 deferred_asm_name = asm_name;
22210 }
22211 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22212 add_linkage_attr (die, decl);
22213}
22214
22215/* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22216
22217static void
22218add_linkage_name (dw_die_ref die, tree decl)
22219{
22220 if (debug_info_level > DINFO_LEVEL_NONE
22221 && VAR_OR_FUNCTION_DECL_P (decl)
22222 && TREE_PUBLIC (decl)
22223 && !(VAR_P (decl) && DECL_REGISTER (decl))
22224 && die->die_tag != DW_TAG_member)
22225 add_linkage_name_raw (die, decl);
22226}
22227
22228/* Add a DW_AT_name attribute and source coordinate attribute for the
22229 given decl, but only if it actually has a name. */
22230
22231static void
22232add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22233 bool no_linkage_name)
22234{
22235 tree decl_name;
22236
22237 decl_name = DECL_NAME (decl);
22238 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22239 {
22240 const char *name = dwarf2_name (decl, scope: 0);
22241 if (name)
22242 add_name_attribute (die, name_string: name);
22243 else
22244 add_desc_attribute (die, decl);
22245
22246 if (! DECL_ARTIFICIAL (decl))
22247 add_src_coords_attributes (die, decl);
22248
22249 if (!no_linkage_name)
22250 add_linkage_name (die, decl);
22251 }
22252 else
22253 add_desc_attribute (die, decl);
22254
22255#ifdef VMS_DEBUGGING_INFO
22256 /* Get the function's name, as described by its RTL. This may be different
22257 from the DECL_NAME name used in the source file. */
22258 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22259 {
22260 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22261 XEXP (DECL_RTL (decl), 0), false);
22262 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22263 }
22264#endif /* VMS_DEBUGGING_INFO */
22265}
22266
22267/* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22268
22269static void
22270add_discr_value (dw_die_ref die, dw_discr_value *value)
22271{
22272 dw_attr_node attr;
22273
22274 attr.dw_attr = DW_AT_discr_value;
22275 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22276 attr.dw_attr_val.val_entry = NULL;
22277 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22278 if (value->pos)
22279 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22280 else
22281 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22282 add_dwarf_attr (die, attr: &attr);
22283}
22284
22285/* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22286
22287static void
22288add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22289{
22290 dw_attr_node attr;
22291
22292 attr.dw_attr = DW_AT_discr_list;
22293 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22294 attr.dw_attr_val.val_entry = NULL;
22295 attr.dw_attr_val.v.val_discr_list = discr_list;
22296 add_dwarf_attr (die, attr: &attr);
22297}
22298
22299static inline dw_discr_list_ref
22300AT_discr_list (dw_attr_node *attr)
22301{
22302 return attr->dw_attr_val.v.val_discr_list;
22303}
22304
22305#ifdef VMS_DEBUGGING_INFO
22306/* Output the debug main pointer die for VMS */
22307
22308void
22309dwarf2out_vms_debug_main_pointer (void)
22310{
22311 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22312 dw_die_ref die;
22313
22314 /* Allocate the VMS debug main subprogram die. */
22315 die = new_die_raw (DW_TAG_subprogram);
22316 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22317 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22318 current_function_funcdef_no);
22319 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22320
22321 /* Make it the first child of comp_unit_die (). */
22322 die->die_parent = comp_unit_die ();
22323 if (comp_unit_die ()->die_child)
22324 {
22325 die->die_sib = comp_unit_die ()->die_child->die_sib;
22326 comp_unit_die ()->die_child->die_sib = die;
22327 }
22328 else
22329 {
22330 die->die_sib = die;
22331 comp_unit_die ()->die_child = die;
22332 }
22333}
22334#endif /* VMS_DEBUGGING_INFO */
22335
22336/* walk_tree helper function for uses_local_type, below. */
22337
22338static tree
22339uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22340{
22341 if (!TYPE_P (*tp))
22342 *walk_subtrees = 0;
22343 else
22344 {
22345 tree name = TYPE_NAME (*tp);
22346 if (name && DECL_P (name) && decl_function_context (name))
22347 return *tp;
22348 }
22349 return NULL_TREE;
22350}
22351
22352/* If TYPE involves a function-local type (including a local typedef to a
22353 non-local type), returns that type; otherwise returns NULL_TREE. */
22354
22355static tree
22356uses_local_type (tree type)
22357{
22358 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22359 return used;
22360}
22361
22362/* Return the DIE for the scope that immediately contains this type.
22363 Non-named types that do not involve a function-local type get global
22364 scope. Named types nested in namespaces or other types get their
22365 containing scope. All other types (i.e. function-local named types) get
22366 the current active scope. */
22367
22368static dw_die_ref
22369scope_die_for (tree t, dw_die_ref context_die)
22370{
22371 dw_die_ref scope_die = NULL;
22372 tree containing_scope;
22373
22374 /* Non-types always go in the current scope. */
22375 gcc_assert (TYPE_P (t));
22376
22377 /* Use the scope of the typedef, rather than the scope of the type
22378 it refers to. */
22379 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22380 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22381 else
22382 containing_scope = TYPE_CONTEXT (t);
22383
22384 /* Use the containing namespace if there is one. */
22385 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22386 {
22387 if (context_die == lookup_decl_die (decl: containing_scope))
22388 /* OK */;
22389 else if (debug_info_level > DINFO_LEVEL_TERSE)
22390 context_die = get_context_die (containing_scope);
22391 else
22392 containing_scope = NULL_TREE;
22393 }
22394
22395 /* Ignore function type "scopes" from the C frontend. They mean that
22396 a tagged type is local to a parmlist of a function declarator, but
22397 that isn't useful to DWARF. */
22398 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22399 containing_scope = NULL_TREE;
22400
22401 if (SCOPE_FILE_SCOPE_P (containing_scope))
22402 {
22403 /* If T uses a local type keep it local as well, to avoid references
22404 to function-local DIEs from outside the function. */
22405 if (current_function_decl && uses_local_type (type: t))
22406 scope_die = context_die;
22407 else
22408 scope_die = comp_unit_die ();
22409 }
22410 else if (TYPE_P (containing_scope))
22411 {
22412 /* For types, we can just look up the appropriate DIE. */
22413 if (debug_info_level > DINFO_LEVEL_TERSE)
22414 scope_die = get_context_die (containing_scope);
22415 else
22416 {
22417 scope_die = lookup_type_die_strip_naming_typedef (type: containing_scope);
22418 if (scope_die == NULL)
22419 scope_die = comp_unit_die ();
22420 }
22421 }
22422 else
22423 scope_die = context_die;
22424
22425 return scope_die;
22426}
22427
22428/* Returns true if CONTEXT_DIE is internal to a function. */
22429
22430static inline bool
22431local_scope_p (dw_die_ref context_die)
22432{
22433 for (; context_die; context_die = context_die->die_parent)
22434 if (context_die->die_tag == DW_TAG_inlined_subroutine
22435 || context_die->die_tag == DW_TAG_subprogram)
22436 return true;
22437
22438 return false;
22439}
22440
22441/* Returns true if CONTEXT_DIE is a class. */
22442
22443static inline bool
22444class_scope_p (dw_die_ref context_die)
22445{
22446 return (context_die
22447 && (context_die->die_tag == DW_TAG_structure_type
22448 || context_die->die_tag == DW_TAG_class_type
22449 || context_die->die_tag == DW_TAG_interface_type
22450 || context_die->die_tag == DW_TAG_union_type));
22451}
22452
22453/* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22454 whether or not to treat a DIE in this context as a declaration. */
22455
22456static inline bool
22457class_or_namespace_scope_p (dw_die_ref context_die)
22458{
22459 return (class_scope_p (context_die)
22460 || (context_die && context_die->die_tag == DW_TAG_namespace));
22461}
22462
22463/* Many forms of DIEs require a "type description" attribute. This
22464 routine locates the proper "type descriptor" die for the type given
22465 by 'type' plus any additional qualifiers given by 'cv_quals', and
22466 adds a DW_AT_type attribute below the given die. */
22467
22468static void
22469add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22470 bool reverse, dw_die_ref context_die)
22471{
22472 enum tree_code code = TREE_CODE (type);
22473 dw_die_ref type_die = NULL;
22474
22475 if (debug_info_level <= DINFO_LEVEL_TERSE)
22476 return;
22477
22478 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22479 or fixed-point type, use the inner type. This is because we have no
22480 support for unnamed types in base_type_die. This can happen if this is
22481 an Ada subrange type. Correct solution is emit a subrange type die. */
22482 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22483 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22484 type = TREE_TYPE (type), code = TREE_CODE (type);
22485
22486 if (code == ERROR_MARK
22487 /* Handle a special case. For functions whose return type is void, we
22488 generate *no* type attribute. (Note that no object may have type
22489 `void', so this only applies to function return types). */
22490 || code == VOID_TYPE)
22491 return;
22492
22493 type_die = modified_type_die (type,
22494 cv_quals: cv_quals | TYPE_QUALS (type),
22495 reverse,
22496 context_die);
22497
22498 if (type_die != NULL)
22499 add_AT_die_ref (die: object_die, attr_kind: DW_AT_type, targ_die: type_die);
22500}
22501
22502/* Given an object die, add the calling convention attribute for the
22503 function call type. */
22504static void
22505add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22506{
22507 enum dwarf_calling_convention value = DW_CC_normal;
22508
22509 value = ((enum dwarf_calling_convention)
22510 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22511
22512 if (is_fortran ()
22513 && id_equal (DECL_ASSEMBLER_NAME (decl), str: "MAIN__"))
22514 {
22515 /* DWARF 2 doesn't provide a way to identify a program's source-level
22516 entry point. DW_AT_calling_convention attributes are only meant
22517 to describe functions' calling conventions. However, lacking a
22518 better way to signal the Fortran main program, we used this for
22519 a long time, following existing custom. Now, DWARF 4 has
22520 DW_AT_main_subprogram, which we add below, but some tools still
22521 rely on the old way, which we thus keep. */
22522 value = DW_CC_program;
22523
22524 if (dwarf_version >= 4 || !dwarf_strict)
22525 add_AT_flag (die: subr_die, attr_kind: DW_AT_main_subprogram, flag: 1);
22526 }
22527
22528 /* Only add the attribute if the backend requests it, and
22529 is not DW_CC_normal. */
22530 if (value && (value != DW_CC_normal))
22531 add_AT_unsigned (die: subr_die, attr_kind: DW_AT_calling_convention, unsigned_val: value);
22532}
22533
22534/* Given a tree pointer to a struct, class, union, or enum type node, return
22535 a pointer to the (string) tag name for the given type, or zero if the type
22536 was declared without a tag. */
22537
22538static const char *
22539type_tag (const_tree type)
22540{
22541 const char *name = 0;
22542
22543 if (TYPE_NAME (type) != 0)
22544 {
22545 tree t = 0;
22546
22547 /* Find the IDENTIFIER_NODE for the type name. */
22548 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22549 && !TYPE_NAMELESS (type))
22550 t = TYPE_NAME (type);
22551
22552 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22553 a TYPE_DECL node, regardless of whether or not a `typedef' was
22554 involved. */
22555 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22556 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22557 {
22558 /* We want to be extra verbose. Don't call dwarf_name if
22559 DECL_NAME isn't set. The default hook for decl_printable_name
22560 doesn't like that, and in this context it's correct to return
22561 0, instead of "<anonymous>" or the like. */
22562 if (DECL_NAME (TYPE_NAME (type))
22563 && !DECL_NAMELESS (TYPE_NAME (type)))
22564 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22565 }
22566
22567 /* Now get the name as a string, or invent one. */
22568 if (!name && t != 0)
22569 name = IDENTIFIER_POINTER (t);
22570 }
22571
22572 return (name == 0 || *name == '\0') ? 0 : name;
22573}
22574
22575/* Return the type associated with a data member, make a special check
22576 for bit field types. */
22577
22578static inline tree
22579member_declared_type (const_tree member)
22580{
22581 return (DECL_BIT_FIELD_TYPE (member)
22582 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22583}
22584
22585/* Get the decl's label, as described by its RTL. This may be different
22586 from the DECL_NAME name used in the source file. */
22587
22588#if 0
22589static const char *
22590decl_start_label (tree decl)
22591{
22592 rtx x;
22593 const char *fnname;
22594
22595 x = DECL_RTL (decl);
22596 gcc_assert (MEM_P (x));
22597
22598 x = XEXP (x, 0);
22599 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22600
22601 fnname = XSTR (x, 0);
22602 return fnname;
22603}
22604#endif
22605
22606/* For variable-length arrays that have been previously generated, but
22607 may be incomplete due to missing subscript info, fill the subscript
22608 info. Return TRUE if this is one of those cases. */
22609
22610static bool
22611fill_variable_array_bounds (tree type)
22612{
22613 if (TREE_ASM_WRITTEN (type)
22614 && TREE_CODE (type) == ARRAY_TYPE
22615 && variably_modified_type_p (type, NULL))
22616 {
22617 dw_die_ref array_die = lookup_type_die (type);
22618 if (!array_die)
22619 return false;
22620 add_subscript_info (type_die: array_die, type, collapse_p: !is_ada ());
22621 return true;
22622 }
22623 return false;
22624}
22625
22626/* These routines generate the internal representation of the DIE's for
22627 the compilation unit. Debugging information is collected by walking
22628 the declaration trees passed in from dwarf2out_decl(). */
22629
22630static void
22631gen_array_type_die (tree type, dw_die_ref context_die)
22632{
22633 dw_die_ref array_die;
22634
22635 /* GNU compilers represent multidimensional array types as sequences of one
22636 dimensional array types whose element types are themselves array types.
22637 We sometimes squish that down to a single array_type DIE with multiple
22638 subscripts in the Dwarf debugging info. The draft Dwarf specification
22639 say that we are allowed to do this kind of compression in C, because
22640 there is no difference between an array of arrays and a multidimensional
22641 array. We don't do this for Ada to remain as close as possible to the
22642 actual representation, which is especially important against the language
22643 flexibilty wrt arrays of variable size. */
22644
22645 bool collapse_nested_arrays = !is_ada ();
22646
22647 if (fill_variable_array_bounds (type))
22648 return;
22649
22650 dw_die_ref scope_die = scope_die_for (t: type, context_die);
22651 tree element_type;
22652
22653 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22654 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22655 if (TREE_CODE (type) == ARRAY_TYPE
22656 && TYPE_STRING_FLAG (type)
22657 && is_fortran ()
22658 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22659 {
22660 HOST_WIDE_INT size;
22661
22662 array_die = new_die (tag_value: DW_TAG_string_type, parent_die: scope_die, t: type);
22663 add_name_attribute (die: array_die, name_string: type_tag (type));
22664 equate_type_number_to_die (type, type_die: array_die);
22665 size = int_size_in_bytes (type);
22666 if (size >= 0)
22667 add_AT_unsigned (die: array_die, attr_kind: DW_AT_byte_size, unsigned_val: size);
22668 /* ??? We can't annotate types late, but for LTO we may not
22669 generate a location early either (gfortran.dg/save_6.f90). */
22670 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22671 && TYPE_DOMAIN (type) != NULL_TREE
22672 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22673 {
22674 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22675 tree rszdecl = szdecl;
22676
22677 size = int_size_in_bytes (TREE_TYPE (szdecl));
22678 if (!DECL_P (szdecl))
22679 {
22680 if (INDIRECT_REF_P (szdecl)
22681 && DECL_P (TREE_OPERAND (szdecl, 0)))
22682 {
22683 rszdecl = TREE_OPERAND (szdecl, 0);
22684 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22685 != DWARF2_ADDR_SIZE)
22686 size = 0;
22687 }
22688 else
22689 size = 0;
22690 }
22691 if (size > 0)
22692 {
22693 dw_loc_list_ref loc
22694 = loc_list_from_tree (loc: rszdecl, want_address: szdecl == rszdecl ? 2 : 0,
22695 NULL);
22696 if (loc)
22697 {
22698 add_AT_location_description (die: array_die, attr_kind: DW_AT_string_length,
22699 descr: loc);
22700 if (size != DWARF2_ADDR_SIZE)
22701 add_AT_unsigned (die: array_die, dwarf_version >= 5
22702 ? DW_AT_string_length_byte_size
22703 : DW_AT_byte_size, unsigned_val: size);
22704 }
22705 }
22706 }
22707 return;
22708 }
22709
22710 array_die = new_die (tag_value: DW_TAG_array_type, parent_die: scope_die, t: type);
22711 add_name_attribute (die: array_die, name_string: type_tag (type));
22712 equate_type_number_to_die (type, type_die: array_die);
22713
22714 if (VECTOR_TYPE_P (type))
22715 add_AT_flag (die: array_die, attr_kind: DW_AT_GNU_vector, flag: 1);
22716
22717 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22718 if (is_fortran ()
22719 && TREE_CODE (type) == ARRAY_TYPE
22720 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22721 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22722 add_AT_unsigned (die: array_die, attr_kind: DW_AT_ordering, unsigned_val: DW_ORD_col_major);
22723
22724#if 0
22725 /* We default the array ordering. Debuggers will probably do the right
22726 things even if DW_AT_ordering is not present. It's not even an issue
22727 until we start to get into multidimensional arrays anyway. If a debugger
22728 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22729 then we'll have to put the DW_AT_ordering attribute back in. (But if
22730 and when we find out that we need to put these in, we will only do so
22731 for multidimensional arrays. */
22732 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22733#endif
22734
22735 if (VECTOR_TYPE_P (type))
22736 {
22737 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22738 dw_die_ref subrange_die = new_die (tag_value: DW_TAG_subrange_type, parent_die: array_die, NULL);
22739 int lb = lower_bound_default ();
22740 if (lb == -1)
22741 lb = 0;
22742 add_bound_info (subrange_die, bound_attr: DW_AT_lower_bound, size_int (lb), NULL);
22743 add_bound_info (subrange_die, bound_attr: DW_AT_upper_bound,
22744 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22745 }
22746 else
22747 add_subscript_info (type_die: array_die, type, collapse_p: collapse_nested_arrays);
22748
22749 /* Add representation of the type of the elements of this array type and
22750 emit the corresponding DIE if we haven't done it already. */
22751 element_type = TREE_TYPE (type);
22752 if (collapse_nested_arrays)
22753 while (TREE_CODE (element_type) == ARRAY_TYPE)
22754 {
22755 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22756 break;
22757 element_type = TREE_TYPE (element_type);
22758 }
22759
22760 add_type_attribute (object_die: array_die, type: element_type, cv_quals: TYPE_UNQUALIFIED,
22761 TREE_CODE (type) == ARRAY_TYPE
22762 && TYPE_REVERSE_STORAGE_ORDER (type),
22763 context_die);
22764
22765 add_gnat_descriptive_type_attribute (die: array_die, type, context_die);
22766 if (TYPE_ARTIFICIAL (type))
22767 add_AT_flag (die: array_die, attr_kind: DW_AT_artificial, flag: 1);
22768
22769 if (get_AT (die: array_die, attr_kind: DW_AT_name))
22770 add_pubtype (decl: type, die: array_die);
22771
22772 add_alignment_attribute (die: array_die, tree_node: type);
22773}
22774
22775/* This routine generates DIE for array with hidden descriptor, details
22776 are filled into *info by a langhook. */
22777
22778static void
22779gen_descr_array_type_die (tree type, struct array_descr_info *info,
22780 dw_die_ref context_die)
22781{
22782 const dw_die_ref scope_die = scope_die_for (t: type, context_die);
22783 const dw_die_ref array_die = new_die (tag_value: DW_TAG_array_type, parent_die: scope_die, t: type);
22784 struct loc_descr_context context = {
22785 .context_type: type, /* context_type */
22786 .base_decl: info->base_decl, /* base_decl */
22787 NULL, /* dpi */
22788 .placeholder_arg: false, /* placeholder_arg */
22789 .placeholder_seen: false, /* placeholder_seen */
22790 .strict_signedness: false /* strict_signedness */
22791 };
22792 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22793 int dim;
22794
22795 add_name_attribute (die: array_die, name_string: type_tag (type));
22796 equate_type_number_to_die (type, type_die: array_die);
22797
22798 if (info->ndimensions > 1)
22799 switch (info->ordering)
22800 {
22801 case array_descr_ordering_row_major:
22802 add_AT_unsigned (die: array_die, attr_kind: DW_AT_ordering, unsigned_val: DW_ORD_row_major);
22803 break;
22804 case array_descr_ordering_column_major:
22805 add_AT_unsigned (die: array_die, attr_kind: DW_AT_ordering, unsigned_val: DW_ORD_col_major);
22806 break;
22807 default:
22808 break;
22809 }
22810
22811 if (dwarf_version >= 3 || !dwarf_strict)
22812 {
22813 if (info->data_location)
22814 add_scalar_info (die: array_die, attr: DW_AT_data_location, value: info->data_location,
22815 forms: dw_scalar_form_exprloc, context: &context);
22816 if (info->associated)
22817 add_scalar_info (die: array_die, attr: DW_AT_associated, value: info->associated,
22818 forms: dw_scalar_form_constant
22819 | dw_scalar_form_exprloc
22820 | dw_scalar_form_reference, context: &context);
22821 if (info->allocated)
22822 add_scalar_info (die: array_die, attr: DW_AT_allocated, value: info->allocated,
22823 forms: dw_scalar_form_constant
22824 | dw_scalar_form_exprloc
22825 | dw_scalar_form_reference, context: &context);
22826 if (info->stride)
22827 {
22828 const enum dwarf_attribute attr
22829 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22830 const int forms
22831 = (info->stride_in_bits)
22832 ? dw_scalar_form_constant
22833 : (dw_scalar_form_constant
22834 | dw_scalar_form_exprloc
22835 | dw_scalar_form_reference);
22836
22837 add_scalar_info (die: array_die, attr, value: info->stride, forms, context: &context);
22838 }
22839 }
22840 if (dwarf_version >= 5)
22841 {
22842 if (info->rank)
22843 {
22844 add_scalar_info (die: array_die, attr: DW_AT_rank, value: info->rank,
22845 forms: dw_scalar_form_constant
22846 | dw_scalar_form_exprloc, context: &context);
22847 subrange_tag = DW_TAG_generic_subrange;
22848 context.placeholder_arg = true;
22849 }
22850 }
22851
22852 add_gnat_descriptive_type_attribute (die: array_die, type, context_die);
22853
22854 for (dim = 0; dim < info->ndimensions; dim++)
22855 {
22856 dw_die_ref subrange_die = new_die (tag_value: subrange_tag, parent_die: array_die, NULL);
22857
22858 if (info->dimen[dim].bounds_type)
22859 add_type_attribute (object_die: subrange_die,
22860 type: info->dimen[dim].bounds_type, cv_quals: TYPE_UNQUALIFIED,
22861 reverse: false, context_die);
22862 if (info->dimen[dim].lower_bound)
22863 add_bound_info (subrange_die, bound_attr: DW_AT_lower_bound,
22864 bound: info->dimen[dim].lower_bound, context: &context);
22865 if (info->dimen[dim].upper_bound)
22866 add_bound_info (subrange_die, bound_attr: DW_AT_upper_bound,
22867 bound: info->dimen[dim].upper_bound, context: &context);
22868 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22869 add_scalar_info (die: subrange_die, attr: DW_AT_byte_stride,
22870 value: info->dimen[dim].stride,
22871 forms: dw_scalar_form_constant
22872 | dw_scalar_form_exprloc
22873 | dw_scalar_form_reference,
22874 context: &context);
22875 }
22876
22877 gen_type_die (info->element_type, context_die);
22878 add_type_attribute (object_die: array_die, type: info->element_type, cv_quals: TYPE_UNQUALIFIED,
22879 TREE_CODE (type) == ARRAY_TYPE
22880 && TYPE_REVERSE_STORAGE_ORDER (type),
22881 context_die);
22882
22883 if (get_AT (die: array_die, attr_kind: DW_AT_name))
22884 add_pubtype (decl: type, die: array_die);
22885
22886 add_alignment_attribute (die: array_die, tree_node: type);
22887}
22888
22889#if 0
22890static void
22891gen_entry_point_die (tree decl, dw_die_ref context_die)
22892{
22893 tree origin = decl_ultimate_origin (decl);
22894 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22895
22896 if (origin != NULL)
22897 add_abstract_origin_attribute (decl_die, origin);
22898 else
22899 {
22900 add_name_and_src_coords_attributes (decl_die, decl);
22901 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22902 TYPE_UNQUALIFIED, false, context_die);
22903 }
22904
22905 if (DECL_ABSTRACT_P (decl))
22906 equate_decl_number_to_die (decl, decl_die);
22907 else
22908 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22909}
22910#endif
22911
22912/* Walk through the list of incomplete types again, trying once more to
22913 emit full debugging info for them. */
22914
22915static void
22916retry_incomplete_types (void)
22917{
22918 set_early_dwarf s;
22919 int i;
22920
22921 for (i = vec_safe_length (v: incomplete_types) - 1; i >= 0; i--)
22922 if (should_emit_struct_debug (type: (*incomplete_types)[i], usage: DINFO_USAGE_DIR_USE))
22923 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22924 vec_safe_truncate (v: incomplete_types, size: 0);
22925}
22926
22927/* Determine what tag to use for a record type. */
22928
22929static enum dwarf_tag
22930record_type_tag (tree type)
22931{
22932 if (! lang_hooks.types.classify_record)
22933 return DW_TAG_structure_type;
22934
22935 switch (lang_hooks.types.classify_record (type))
22936 {
22937 case RECORD_IS_STRUCT:
22938 return DW_TAG_structure_type;
22939
22940 case RECORD_IS_CLASS:
22941 return DW_TAG_class_type;
22942
22943 case RECORD_IS_INTERFACE:
22944 if (dwarf_version >= 3 || !dwarf_strict)
22945 return DW_TAG_interface_type;
22946 return DW_TAG_structure_type;
22947
22948 default:
22949 gcc_unreachable ();
22950 }
22951}
22952
22953/* Generate a DIE to represent an enumeration type. Note that these DIEs
22954 include all of the information about the enumeration values also. Each
22955 enumerated type name/value is listed as a child of the enumerated type
22956 DIE. REVERSE is true if the type is to be interpreted in the reverse
22957 storage order wrt the target order. */
22958
22959static dw_die_ref
22960gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22961{
22962 dw_die_ref type_die = lookup_type_die (type);
22963 dw_die_ref orig_type_die = type_die;
22964
22965 if (type_die == NULL || reverse)
22966 {
22967 dw_die_ref scope_die = scope_die_for (t: type, context_die);
22968
22969 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22970 if (reverse)
22971 {
22972 gcc_assert (type_die);
22973 dw_die_ref after_die = type_die;
22974 type_die = new_die_raw (tag_value: DW_TAG_enumeration_type);
22975 add_child_die_after (die: scope_die, child_die: type_die, after_die);
22976 }
22977 else
22978 {
22979 type_die = new_die (tag_value: DW_TAG_enumeration_type, parent_die: scope_die, t: type);
22980 equate_type_number_to_die (type, type_die);
22981 }
22982 add_name_attribute (die: type_die, name_string: type_tag (type));
22983 if ((dwarf_version >= 4 || !dwarf_strict)
22984 && ENUM_IS_SCOPED (type))
22985 add_AT_flag (die: type_die, attr_kind: DW_AT_enum_class, flag: 1);
22986 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22987 add_AT_flag (die: type_die, attr_kind: DW_AT_declaration, flag: 1);
22988 if (!dwarf_strict)
22989 add_AT_unsigned (die: type_die, attr_kind: DW_AT_encoding,
22990 TYPE_UNSIGNED (type)
22991 ? DW_ATE_unsigned
22992 : DW_ATE_signed);
22993 if (reverse)
22994 add_AT_unsigned (die: type_die, attr_kind: DW_AT_endianity,
22995 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22996 }
22997 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22998 return type_die;
22999 else
23000 remove_AT (die: type_die, attr_kind: DW_AT_declaration);
23001
23002 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
23003 given enum type is incomplete, do not generate the DW_AT_byte_size
23004 attribute or the DW_AT_element_list attribute. */
23005 if (TYPE_SIZE (type))
23006 {
23007 tree link;
23008
23009 if (!ENUM_IS_OPAQUE (type))
23010 TREE_ASM_WRITTEN (type) = 1;
23011 if (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_byte_size))
23012 add_byte_size_attribute (die: type_die, tree_node: type);
23013 if (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_alignment))
23014 add_alignment_attribute (die: type_die, tree_node: type);
23015 if ((dwarf_version >= 3 || !dwarf_strict)
23016 && (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_type)))
23017 {
23018 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
23019 add_type_attribute (object_die: type_die, type: underlying, cv_quals: TYPE_UNQUALIFIED, reverse: false,
23020 context_die);
23021 }
23022 if (TYPE_STUB_DECL (type) != NULL_TREE)
23023 {
23024 if (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_decl_file))
23025 add_src_coords_attributes (die: type_die, TYPE_STUB_DECL (type));
23026 if (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_accessibility))
23027 add_accessibility_attribute (die: type_die, TYPE_STUB_DECL (type));
23028 }
23029
23030 /* If the first reference to this type was as the return type of an
23031 inline function, then it may not have a parent. Fix this now. */
23032 if (type_die->die_parent == NULL)
23033 add_child_die (die: scope_die_for (t: type, context_die), child_die: type_die);
23034
23035 for (link = TYPE_VALUES (type);
23036 link != NULL; link = TREE_CHAIN (link))
23037 {
23038 dw_die_ref enum_die = new_die (tag_value: DW_TAG_enumerator, parent_die: type_die, t: link);
23039 tree value = TREE_VALUE (link);
23040
23041 if (DECL_P (value))
23042 equate_decl_number_to_die (decl: value, decl_die: enum_die);
23043
23044 gcc_assert (!ENUM_IS_OPAQUE (type));
23045 add_name_attribute (die: enum_die,
23046 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
23047
23048 if (TREE_CODE (value) == CONST_DECL)
23049 value = DECL_INITIAL (value);
23050
23051 if (simple_type_size_in_bits (TREE_TYPE (value))
23052 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
23053 {
23054 /* For constant forms created by add_AT_unsigned DWARF
23055 consumers (GDB, elfutils, etc.) always zero extend
23056 the value. Only when the actual value is negative
23057 do we need to use add_AT_int to generate a constant
23058 form that can represent negative values. */
23059 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
23060 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
23061 add_AT_unsigned (die: enum_die, attr_kind: DW_AT_const_value,
23062 unsigned_val: (unsigned HOST_WIDE_INT) val);
23063 else
23064 add_AT_int (die: enum_die, attr_kind: DW_AT_const_value, int_val: val);
23065 }
23066 else
23067 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
23068 that here. TODO: This should be re-worked to use correct
23069 signed/unsigned double tags for all cases. */
23070 add_AT_wide (die: enum_die, attr_kind: DW_AT_const_value, w: wi::to_wide (t: value));
23071 }
23072
23073 add_gnat_descriptive_type_attribute (die: type_die, type, context_die);
23074 if (TYPE_ARTIFICIAL (type)
23075 && (!orig_type_die || !get_AT (die: type_die, attr_kind: DW_AT_artificial)))
23076 add_AT_flag (die: type_die, attr_kind: DW_AT_artificial, flag: 1);
23077 }
23078 else
23079 add_AT_flag (die: type_die, attr_kind: DW_AT_declaration, flag: 1);
23080
23081 add_pubtype (decl: type, die: type_die);
23082
23083 return type_die;
23084}
23085
23086/* Generate a DIE to represent either a real live formal parameter decl or to
23087 represent just the type of some formal parameter position in some function
23088 type.
23089
23090 Note that this routine is a bit unusual because its argument may be a
23091 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
23092 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
23093 node. If it's the former then this function is being called to output a
23094 DIE to represent a formal parameter object (or some inlining thereof). If
23095 it's the latter, then this function is only being called to output a
23096 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
23097 argument type of some subprogram type.
23098 If EMIT_NAME_P is true, name and source coordinate attributes
23099 are emitted. */
23100
23101static dw_die_ref
23102gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
23103 dw_die_ref context_die)
23104{
23105 tree node_or_origin = node ? node : origin;
23106 tree ultimate_origin;
23107 dw_die_ref parm_die = NULL;
23108
23109 if (DECL_P (node_or_origin))
23110 {
23111 parm_die = lookup_decl_die (decl: node);
23112
23113 /* If the contexts differ, we may not be talking about the same
23114 thing.
23115 ??? When in LTO the DIE parent is the "abstract" copy and the
23116 context_die is the specification "copy". */
23117 if (parm_die
23118 && parm_die->die_parent != context_die
23119 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23120 || parm_die->die_parent->die_parent != context_die)
23121 && !in_lto_p)
23122 {
23123 gcc_assert (!DECL_ABSTRACT_P (node));
23124 /* This can happen when creating a concrete instance, in
23125 which case we need to create a new DIE that will get
23126 annotated with DW_AT_abstract_origin. */
23127 parm_die = NULL;
23128 }
23129
23130 if (parm_die && parm_die->die_parent == NULL)
23131 {
23132 /* Check that parm_die already has the right attributes that
23133 we would have added below. If any attributes are
23134 missing, fall through to add them. */
23135 if (! DECL_ABSTRACT_P (node_or_origin)
23136 && !get_AT (die: parm_die, attr_kind: DW_AT_location)
23137 && !get_AT (die: parm_die, attr_kind: DW_AT_const_value))
23138 /* We are missing location info, and are about to add it. */
23139 ;
23140 else
23141 {
23142 add_child_die (die: context_die, child_die: parm_die);
23143 return parm_die;
23144 }
23145 }
23146 }
23147
23148 /* If we have a previously generated DIE, use it, unless this is an
23149 concrete instance (origin != NULL), in which case we need a new
23150 DIE with a corresponding DW_AT_abstract_origin. */
23151 bool reusing_die;
23152 if (parm_die && origin == NULL)
23153 reusing_die = true;
23154 else
23155 {
23156 parm_die = new_die (tag_value: DW_TAG_formal_parameter, parent_die: context_die, t: node);
23157 reusing_die = false;
23158 }
23159
23160 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23161 {
23162 case tcc_declaration:
23163 ultimate_origin = decl_ultimate_origin (decl: node_or_origin);
23164 if (node || ultimate_origin)
23165 origin = ultimate_origin;
23166
23167 if (reusing_die)
23168 goto add_location;
23169
23170 if (origin != NULL)
23171 add_abstract_origin_attribute (die: parm_die, origin);
23172 else if (emit_name_p)
23173 add_name_and_src_coords_attributes (die: parm_die, decl: node);
23174 if (origin == NULL
23175 || (! DECL_ABSTRACT_P (node_or_origin)
23176 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23177 decl_function_context
23178 (node_or_origin))))
23179 {
23180 tree type = TREE_TYPE (node_or_origin);
23181 if (decl_by_reference_p (decl: node_or_origin))
23182 add_type_attribute (object_die: parm_die, TREE_TYPE (type),
23183 cv_quals: TYPE_UNQUALIFIED,
23184 reverse: false, context_die);
23185 else
23186 add_type_attribute (object_die: parm_die, type,
23187 cv_quals: decl_quals (decl: node_or_origin),
23188 reverse: false, context_die);
23189 }
23190 if (origin == NULL && DECL_ARTIFICIAL (node))
23191 add_AT_flag (die: parm_die, attr_kind: DW_AT_artificial, flag: 1);
23192 add_location:
23193 if (node && node != origin)
23194 equate_decl_number_to_die (decl: node, decl_die: parm_die);
23195 if (! DECL_ABSTRACT_P (node_or_origin))
23196 add_location_or_const_value_attribute (die: parm_die, decl: node_or_origin,
23197 cache_p: node == NULL);
23198
23199 break;
23200
23201 case tcc_type:
23202 /* We were called with some kind of a ..._TYPE node. */
23203 add_type_attribute (object_die: parm_die, type: node_or_origin, cv_quals: TYPE_UNQUALIFIED, reverse: false,
23204 context_die);
23205 break;
23206
23207 default:
23208 gcc_unreachable ();
23209 }
23210
23211 return parm_die;
23212}
23213
23214/* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23215 children DW_TAG_formal_parameter DIEs representing the arguments of the
23216 parameter pack.
23217
23218 PARM_PACK must be a function parameter pack.
23219 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23220 must point to the subsequent arguments of the function PACK_ARG belongs to.
23221 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23222 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23223 following the last one for which a DIE was generated. */
23224
23225static dw_die_ref
23226gen_formal_parameter_pack_die (tree parm_pack,
23227 tree pack_arg,
23228 dw_die_ref subr_die,
23229 tree *next_arg)
23230{
23231 tree arg;
23232 dw_die_ref parm_pack_die;
23233
23234 gcc_assert (parm_pack
23235 && lang_hooks.function_parameter_pack_p (parm_pack)
23236 && subr_die);
23237
23238 parm_pack_die = new_die (tag_value: DW_TAG_GNU_formal_parameter_pack, parent_die: subr_die, t: parm_pack);
23239 add_name_and_src_coords_attributes (die: parm_pack_die, decl: parm_pack);
23240
23241 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23242 {
23243 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23244 parm_pack))
23245 break;
23246 gen_formal_parameter_die (node: arg, NULL,
23247 emit_name_p: false /* Don't emit name attribute. */,
23248 context_die: parm_pack_die);
23249 }
23250 if (next_arg)
23251 *next_arg = arg;
23252 return parm_pack_die;
23253}
23254
23255/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23256 at the end of an (ANSI prototyped) formal parameters list. */
23257
23258static void
23259gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23260{
23261 new_die (tag_value: DW_TAG_unspecified_parameters, parent_die: context_die, t: decl_or_type);
23262}
23263
23264/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23265 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23266 parameters as specified in some function type specification (except for
23267 those which appear as part of a function *definition*). */
23268
23269static void
23270gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23271{
23272 tree link;
23273 tree formal_type = NULL;
23274 tree first_parm_type;
23275 tree arg;
23276
23277 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23278 {
23279 arg = DECL_ARGUMENTS (function_or_method_type);
23280 function_or_method_type = TREE_TYPE (function_or_method_type);
23281 }
23282 else
23283 arg = NULL_TREE;
23284
23285 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23286
23287 /* Make our first pass over the list of formal parameter types and output a
23288 DW_TAG_formal_parameter DIE for each one. */
23289 for (link = first_parm_type; link; )
23290 {
23291 dw_die_ref parm_die;
23292
23293 formal_type = TREE_VALUE (link);
23294 if (formal_type == void_type_node)
23295 break;
23296
23297 /* Output a (nameless) DIE to represent the formal parameter itself. */
23298 parm_die = gen_formal_parameter_die (node: formal_type, NULL,
23299 emit_name_p: true /* Emit name attribute. */,
23300 context_die);
23301 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23302 && link == first_parm_type)
23303 {
23304 add_AT_flag (die: parm_die, attr_kind: DW_AT_artificial, flag: 1);
23305 if (dwarf_version >= 3 || !dwarf_strict)
23306 add_AT_die_ref (die: context_die, attr_kind: DW_AT_object_pointer, targ_die: parm_die);
23307 }
23308 else if (arg && DECL_ARTIFICIAL (arg))
23309 add_AT_flag (die: parm_die, attr_kind: DW_AT_artificial, flag: 1);
23310
23311 link = TREE_CHAIN (link);
23312 if (arg)
23313 arg = DECL_CHAIN (arg);
23314 }
23315
23316 /* If this function type has an ellipsis, add a
23317 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23318 if (formal_type != void_type_node)
23319 gen_unspecified_parameters_die (decl_or_type: function_or_method_type, context_die);
23320
23321 /* Make our second (and final) pass over the list of formal parameter types
23322 and output DIEs to represent those types (as necessary). */
23323 for (link = TYPE_ARG_TYPES (function_or_method_type);
23324 link && TREE_VALUE (link);
23325 link = TREE_CHAIN (link))
23326 gen_type_die (TREE_VALUE (link), context_die);
23327}
23328
23329/* We want to generate the DIE for TYPE so that we can generate the
23330 die for MEMBER, which has been defined; we will need to refer back
23331 to the member declaration nested within TYPE. If we're trying to
23332 generate minimal debug info for TYPE, processing TYPE won't do the
23333 trick; we need to attach the member declaration by hand. */
23334
23335static void
23336gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23337{
23338 gen_type_die (type, context_die);
23339
23340 /* If we're trying to avoid duplicate debug info, we may not have
23341 emitted the member decl for this function. Emit it now. */
23342 if (TYPE_STUB_DECL (type)
23343 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23344 && ! lookup_decl_die (decl: member))
23345 {
23346 dw_die_ref type_die;
23347 gcc_assert (!decl_ultimate_origin (member));
23348
23349 type_die = lookup_type_die_strip_naming_typedef (type);
23350 if (TREE_CODE (member) == FUNCTION_DECL)
23351 gen_subprogram_die (member, type_die);
23352 else if (TREE_CODE (member) == FIELD_DECL)
23353 {
23354 /* Ignore the nameless fields that are used to skip bits but handle
23355 C++ anonymous unions and structs. */
23356 if (DECL_NAME (member) != NULL_TREE
23357 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23358 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23359 {
23360 struct vlr_context vlr_ctx = {
23361 DECL_CONTEXT (member), /* struct_type */
23362 NULL_TREE /* variant_part_offset */
23363 };
23364 gen_type_die (member_declared_type (member), type_die);
23365 gen_field_die (member, &vlr_ctx, type_die);
23366 }
23367 }
23368 else
23369 gen_variable_die (member, NULL_TREE, type_die);
23370 }
23371}
23372
23373/* Forward declare these functions, because they are mutually recursive
23374 with their set_block_* pairing functions. */
23375static void set_decl_origin_self (tree);
23376
23377/* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23378 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23379 that it points to the node itself, thus indicating that the node is its
23380 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23381 the given node is NULL, recursively descend the decl/block tree which
23382 it is the root of, and for each other ..._DECL or BLOCK node contained
23383 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23384 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23385 values to point to themselves. */
23386
23387static void
23388set_block_origin_self (tree stmt)
23389{
23390 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23391 {
23392 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23393
23394 {
23395 tree local_decl;
23396
23397 for (local_decl = BLOCK_VARS (stmt);
23398 local_decl != NULL_TREE;
23399 local_decl = DECL_CHAIN (local_decl))
23400 /* Do not recurse on nested functions since the inlining status
23401 of parent and child can be different as per the DWARF spec. */
23402 if (TREE_CODE (local_decl) != FUNCTION_DECL
23403 && !DECL_EXTERNAL (local_decl))
23404 set_decl_origin_self (local_decl);
23405 }
23406
23407 {
23408 tree subblock;
23409
23410 for (subblock = BLOCK_SUBBLOCKS (stmt);
23411 subblock != NULL_TREE;
23412 subblock = BLOCK_CHAIN (subblock))
23413 set_block_origin_self (subblock); /* Recurse. */
23414 }
23415 }
23416}
23417
23418/* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23419 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23420 node to so that it points to the node itself, thus indicating that the
23421 node represents its own (abstract) origin. Additionally, if the
23422 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23423 the decl/block tree of which the given node is the root of, and for
23424 each other ..._DECL or BLOCK node contained therein whose
23425 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23426 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23427 point to themselves. */
23428
23429static void
23430set_decl_origin_self (tree decl)
23431{
23432 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23433 {
23434 DECL_ABSTRACT_ORIGIN (decl) = decl;
23435 if (TREE_CODE (decl) == FUNCTION_DECL)
23436 {
23437 tree arg;
23438
23439 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23440 DECL_ABSTRACT_ORIGIN (arg) = arg;
23441 if (DECL_INITIAL (decl) != NULL_TREE
23442 && DECL_INITIAL (decl) != error_mark_node)
23443 set_block_origin_self (DECL_INITIAL (decl));
23444 }
23445 }
23446}
23447
23448/* Mark the early DIE for DECL as the abstract instance. */
23449
23450static void
23451dwarf2out_abstract_function (tree decl)
23452{
23453 dw_die_ref old_die;
23454
23455 /* Make sure we have the actual abstract inline, not a clone. */
23456 decl = DECL_ORIGIN (decl);
23457
23458 if (DECL_IGNORED_P (decl))
23459 return;
23460
23461#ifdef CODEVIEW_DEBUGGING_INFO
23462 if (codeview_debuginfo_p ())
23463 codeview_abstract_function (decl);
23464#endif
23465
23466 /* In LTO we're all set. We already created abstract instances
23467 early and we want to avoid creating a concrete instance of that
23468 if we don't output it. */
23469 if (in_lto_p)
23470 return;
23471
23472 old_die = lookup_decl_die (decl);
23473 gcc_assert (old_die != NULL);
23474 if (get_AT (die: old_die, attr_kind: DW_AT_inline))
23475 /* We've already generated the abstract instance. */
23476 return;
23477
23478 /* Go ahead and put DW_AT_inline on the DIE. */
23479 if (DECL_DECLARED_INLINE_P (decl))
23480 {
23481 if (cgraph_function_possibly_inlined_p (decl))
23482 add_AT_unsigned (die: old_die, attr_kind: DW_AT_inline, unsigned_val: DW_INL_declared_inlined);
23483 else
23484 add_AT_unsigned (die: old_die, attr_kind: DW_AT_inline, unsigned_val: DW_INL_declared_not_inlined);
23485 }
23486 else
23487 {
23488 if (cgraph_function_possibly_inlined_p (decl))
23489 add_AT_unsigned (die: old_die, attr_kind: DW_AT_inline, unsigned_val: DW_INL_inlined);
23490 else
23491 add_AT_unsigned (die: old_die, attr_kind: DW_AT_inline, unsigned_val: DW_INL_not_inlined);
23492 }
23493
23494 if (DECL_DECLARED_INLINE_P (decl)
23495 && lookup_attribute (attr_name: "artificial", DECL_ATTRIBUTES (decl)))
23496 add_AT_flag (die: old_die, attr_kind: DW_AT_artificial, flag: 1);
23497
23498 set_decl_origin_self (decl);
23499}
23500
23501/* Helper function of premark_used_types() which gets called through
23502 htab_traverse.
23503
23504 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23505 marked as unused by prune_unused_types. */
23506
23507bool
23508premark_used_types_helper (tree const &type, void *)
23509{
23510 dw_die_ref die;
23511
23512 die = lookup_type_die (type);
23513 if (die != NULL)
23514 die->die_perennial_p = 1;
23515 return true;
23516}
23517
23518/* Helper function of premark_types_used_by_global_vars which gets called
23519 through htab_traverse.
23520
23521 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23522 marked as unused by prune_unused_types. The DIE of the type is marked
23523 only if the global variable using the type will actually be emitted. */
23524
23525int
23526premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23527 void *)
23528{
23529 struct types_used_by_vars_entry *entry;
23530 dw_die_ref die;
23531
23532 entry = (struct types_used_by_vars_entry *) *slot;
23533 gcc_assert (entry->type != NULL
23534 && entry->var_decl != NULL);
23535 die = lookup_type_die (type: entry->type);
23536 if (die)
23537 {
23538 /* Ask cgraph if the global variable really is to be emitted.
23539 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23540 varpool_node *node = varpool_node::get (decl: entry->var_decl);
23541 if (node && node->definition)
23542 {
23543 die->die_perennial_p = 1;
23544 /* Keep the parent DIEs as well. */
23545 while ((die = die->die_parent) && die->die_perennial_p == 0)
23546 die->die_perennial_p = 1;
23547 }
23548 }
23549 return 1;
23550}
23551
23552/* Mark all members of used_types_hash as perennial. */
23553
23554static void
23555premark_used_types (struct function *fun)
23556{
23557 if (fun && fun->used_types_hash)
23558 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23559}
23560
23561/* Mark all members of types_used_by_vars_entry as perennial. */
23562
23563static void
23564premark_types_used_by_global_vars (void)
23565{
23566 if (types_used_by_vars_hash)
23567 types_used_by_vars_hash
23568 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23569}
23570
23571/* Mark all variables used by the symtab as perennial. */
23572
23573static void
23574premark_used_variables (void)
23575{
23576 /* Mark DIEs in the symtab as used. */
23577 varpool_node *var;
23578 FOR_EACH_VARIABLE (var)
23579 {
23580 dw_die_ref die = lookup_decl_die (decl: var->decl);
23581 if (die)
23582 die->die_perennial_p = 1;
23583 }
23584}
23585
23586/* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23587 for CA_LOC call arg loc node. */
23588
23589static dw_die_ref
23590gen_call_site_die (tree decl, dw_die_ref subr_die,
23591 struct call_arg_loc_node *ca_loc)
23592{
23593 dw_die_ref stmt_die = NULL, die;
23594 tree block = ca_loc->block;
23595
23596 while (block
23597 && block != DECL_INITIAL (decl)
23598 && TREE_CODE (block) == BLOCK)
23599 {
23600 stmt_die = lookup_block_die (block);
23601 if (stmt_die)
23602 break;
23603 block = BLOCK_SUPERCONTEXT (block);
23604 }
23605 if (stmt_die == NULL)
23606 stmt_die = subr_die;
23607 die = new_die (tag_value: dwarf_TAG (tag: DW_TAG_call_site), parent_die: stmt_die, NULL_TREE);
23608 add_AT_lbl_id (die, attr_kind: dwarf_AT (at: DW_AT_call_return_pc),
23609 lbl_id: ca_loc->label,
23610 offset: targetm.calls.call_offset_return_label (ca_loc->call_insn));
23611 if (ca_loc->tail_call_p)
23612 add_AT_flag (die, attr_kind: dwarf_AT (at: DW_AT_call_tail_call), flag: 1);
23613 if (ca_loc->symbol_ref)
23614 {
23615 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23616 if (tdie)
23617 add_AT_die_ref (die, attr_kind: dwarf_AT (at: DW_AT_call_origin), targ_die: tdie);
23618 else
23619 add_AT_addr (die, attr_kind: dwarf_AT (at: DW_AT_call_origin), addr: ca_loc->symbol_ref,
23620 force_direct: false);
23621 }
23622 return die;
23623}
23624
23625/* Generate a DIE to represent a declared function (either file-scope or
23626 block-local). */
23627
23628static void
23629gen_subprogram_die (tree decl, dw_die_ref context_die)
23630{
23631 tree origin = decl_ultimate_origin (decl);
23632 dw_die_ref subr_die;
23633 dw_die_ref old_die = lookup_decl_die (decl);
23634 bool old_die_had_no_children = false;
23635
23636 /* This function gets called multiple times for different stages of
23637 the debug process. For example, for func() in this code:
23638
23639 namespace S
23640 {
23641 void func() { ... }
23642 }
23643
23644 ...we get called 4 times. Twice in early debug and twice in
23645 late debug:
23646
23647 Early debug
23648 -----------
23649
23650 1. Once while generating func() within the namespace. This is
23651 the declaration. The declaration bit below is set, as the
23652 context is the namespace.
23653
23654 A new DIE will be generated with DW_AT_declaration set.
23655
23656 2. Once for func() itself. This is the specification. The
23657 declaration bit below is clear as the context is the CU.
23658
23659 We will use the cached DIE from (1) to create a new DIE with
23660 DW_AT_specification pointing to the declaration in (1).
23661
23662 Late debug via rest_of_handle_final()
23663 -------------------------------------
23664
23665 3. Once generating func() within the namespace. This is also the
23666 declaration, as in (1), but this time we will early exit below
23667 as we have a cached DIE and a declaration needs no additional
23668 annotations (no locations), as the source declaration line
23669 info is enough.
23670
23671 4. Once for func() itself. As in (2), this is the specification,
23672 but this time we will re-use the cached DIE, and just annotate
23673 it with the location information that should now be available.
23674
23675 For something without namespaces, but with abstract instances, we
23676 are also called a multiple times:
23677
23678 class Base
23679 {
23680 public:
23681 Base (); // constructor declaration (1)
23682 };
23683
23684 Base::Base () { } // constructor specification (2)
23685
23686 Early debug
23687 -----------
23688
23689 1. Once for the Base() constructor by virtue of it being a
23690 member of the Base class. This is done via
23691 rest_of_type_compilation.
23692
23693 This is a declaration, so a new DIE will be created with
23694 DW_AT_declaration.
23695
23696 2. Once for the Base() constructor definition, but this time
23697 while generating the abstract instance of the base
23698 constructor (__base_ctor) which is being generated via early
23699 debug of reachable functions.
23700
23701 Even though we have a cached version of the declaration (1),
23702 we will create a DW_AT_specification of the declaration DIE
23703 in (1).
23704
23705 3. Once for the __base_ctor itself, but this time, we generate
23706 an DW_AT_abstract_origin version of the DW_AT_specification in
23707 (2).
23708
23709 Late debug via rest_of_handle_final
23710 -----------------------------------
23711
23712 4. One final time for the __base_ctor (which will have a cached
23713 DIE with DW_AT_abstract_origin created in (3). This time,
23714 we will just annotate the location information now
23715 available.
23716 */
23717 int declaration = (current_function_decl != decl
23718 || (!DECL_INITIAL (decl) && !origin)
23719 || class_or_namespace_scope_p (context_die));
23720
23721 /* A declaration that has been previously dumped needs no
23722 additional information. */
23723 if (old_die && declaration)
23724 return;
23725
23726 if (in_lto_p && old_die && old_die->die_child == NULL)
23727 old_die_had_no_children = true;
23728
23729 /* Now that the C++ front end lazily declares artificial member fns, we
23730 might need to retrofit the declaration into its class. */
23731 if (!declaration && !origin && !old_die
23732 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23733 && !class_or_namespace_scope_p (context_die)
23734 && debug_info_level > DINFO_LEVEL_TERSE)
23735 old_die = force_decl_die (decl);
23736
23737 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23738 if (origin != NULL)
23739 {
23740 gcc_assert (!declaration || local_scope_p (context_die));
23741
23742 /* Fixup die_parent for the abstract instance of a nested
23743 inline function. */
23744 if (old_die && old_die->die_parent == NULL)
23745 add_child_die (die: context_die, child_die: old_die);
23746
23747 if (old_die && get_AT_ref (die: old_die, attr_kind: DW_AT_abstract_origin))
23748 {
23749 /* If we have a DW_AT_abstract_origin we have a working
23750 cached version. */
23751 subr_die = old_die;
23752 }
23753 else
23754 {
23755 subr_die = new_die (tag_value: DW_TAG_subprogram, parent_die: context_die, t: decl);
23756 add_abstract_origin_attribute (die: subr_die, origin);
23757 /* This is where the actual code for a cloned function is.
23758 Let's emit linkage name attribute for it. This helps
23759 debuggers to e.g, set breakpoints into
23760 constructors/destructors when the user asks "break
23761 K::K". */
23762 add_linkage_name (die: subr_die, decl);
23763 }
23764 }
23765 /* A cached copy, possibly from early dwarf generation. Reuse as
23766 much as possible. */
23767 else if (old_die)
23768 {
23769 if (!get_AT_flag (die: old_die, attr_kind: DW_AT_declaration)
23770 /* We can have a normal definition following an inline one in the
23771 case of redefinition of GNU C extern inlines.
23772 It seems reasonable to use AT_specification in this case. */
23773 && !get_AT (die: old_die, attr_kind: DW_AT_inline))
23774 {
23775 /* Detect and ignore this case, where we are trying to output
23776 something we have already output. */
23777 if (get_AT (die: old_die, attr_kind: DW_AT_low_pc)
23778 || get_AT (die: old_die, attr_kind: DW_AT_ranges))
23779 return;
23780
23781 /* If we have no location information, this must be a
23782 partially generated DIE from early dwarf generation.
23783 Fall through and generate it. */
23784 }
23785
23786 /* If the definition comes from the same place as the declaration,
23787 maybe use the old DIE. We always want the DIE for this function
23788 that has the *_pc attributes to be under comp_unit_die so the
23789 debugger can find it. We also need to do this for abstract
23790 instances of inlines, since the spec requires the out-of-line copy
23791 to have the same parent. For local class methods, this doesn't
23792 apply; we just use the old DIE. */
23793 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23794 struct dwarf_file_data * file_index = lookup_filename (s.file);
23795 if (((is_unit_die (c: old_die->die_parent)
23796 /* This condition fixes the inconsistency/ICE with the
23797 following Fortran test (or some derivative thereof) while
23798 building libgfortran:
23799
23800 module some_m
23801 contains
23802 logical function funky (FLAG)
23803 funky = .true.
23804 end function
23805 end module
23806 */
23807 || (old_die->die_parent
23808 && old_die->die_parent->die_tag == DW_TAG_module)
23809 || local_scope_p (context_die: old_die->die_parent)
23810 || context_die == NULL)
23811 && (DECL_ARTIFICIAL (decl)
23812 || (get_AT_file (die: old_die, attr_kind: DW_AT_decl_file) == file_index
23813 && (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_line)
23814 == (unsigned) s.line)
23815 && (!debug_column_info
23816 || s.column == 0
23817 || (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_column)
23818 == (unsigned) s.column)))))
23819 /* With LTO if there's an abstract instance for
23820 the old DIE, this is a concrete instance and
23821 thus re-use the DIE. */
23822 || get_AT (die: old_die, attr_kind: DW_AT_abstract_origin))
23823 {
23824 subr_die = old_die;
23825
23826 /* Clear out the declaration attribute, but leave the
23827 parameters so they can be augmented with location
23828 information later. Unless this was a declaration, in
23829 which case, wipe out the nameless parameters and recreate
23830 them further down. */
23831 if (remove_AT (die: subr_die, attr_kind: DW_AT_declaration))
23832 {
23833
23834 remove_AT (die: subr_die, attr_kind: DW_AT_object_pointer);
23835 remove_child_TAG (die: subr_die, tag: DW_TAG_formal_parameter);
23836 }
23837 }
23838 /* Make a specification pointing to the previously built
23839 declaration. */
23840 else
23841 {
23842 subr_die = new_die (tag_value: DW_TAG_subprogram, parent_die: context_die, t: decl);
23843 add_AT_specification (die: subr_die, targ_die: old_die);
23844 add_pubname (decl, die: subr_die);
23845 if (get_AT_file (die: old_die, attr_kind: DW_AT_decl_file) != file_index)
23846 add_AT_file (die: subr_die, attr_kind: DW_AT_decl_file, fd: file_index);
23847 if (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_line) != (unsigned) s.line)
23848 add_AT_unsigned (die: subr_die, attr_kind: DW_AT_decl_line, unsigned_val: s.line);
23849 if (debug_column_info
23850 && s.column
23851 && (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_column)
23852 != (unsigned) s.column))
23853 add_AT_unsigned (die: subr_die, attr_kind: DW_AT_decl_column, unsigned_val: s.column);
23854
23855 /* If the prototype had an 'auto' or 'decltype(auto)' in
23856 the return type, emit the real type on the definition die. */
23857 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23858 {
23859 dw_die_ref die = get_AT_ref (die: old_die, attr_kind: DW_AT_type);
23860 while (die
23861 && (die->die_tag == DW_TAG_reference_type
23862 || die->die_tag == DW_TAG_rvalue_reference_type
23863 || die->die_tag == DW_TAG_pointer_type
23864 || die->die_tag == DW_TAG_const_type
23865 || die->die_tag == DW_TAG_volatile_type
23866 || die->die_tag == DW_TAG_restrict_type
23867 || die->die_tag == DW_TAG_array_type
23868 || die->die_tag == DW_TAG_ptr_to_member_type
23869 || die->die_tag == DW_TAG_subroutine_type))
23870 die = get_AT_ref (die, attr_kind: DW_AT_type);
23871 if (die == auto_die || die == decltype_auto_die)
23872 add_type_attribute (object_die: subr_die, TREE_TYPE (TREE_TYPE (decl)),
23873 cv_quals: TYPE_UNQUALIFIED, reverse: false, context_die);
23874 }
23875
23876 /* When we process the method declaration, we haven't seen
23877 the out-of-class defaulted definition yet, so we have to
23878 recheck now. */
23879 if ((dwarf_version >= 5 || ! dwarf_strict)
23880 && !get_AT (die: subr_die, attr_kind: DW_AT_defaulted))
23881 {
23882 int defaulted
23883 = lang_hooks.decls.decl_dwarf_attribute (decl,
23884 DW_AT_defaulted);
23885 if (defaulted != -1)
23886 {
23887 /* Other values must have been handled before. */
23888 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23889 add_AT_unsigned (die: subr_die, attr_kind: DW_AT_defaulted, unsigned_val: defaulted);
23890 }
23891 }
23892 }
23893 }
23894 /* Create a fresh DIE for anything else. */
23895 else
23896 {
23897 subr_die = new_die (tag_value: DW_TAG_subprogram, parent_die: context_die, t: decl);
23898
23899 if (TREE_PUBLIC (decl))
23900 add_AT_flag (die: subr_die, attr_kind: DW_AT_external, flag: 1);
23901
23902 add_name_and_src_coords_attributes (die: subr_die, decl);
23903 add_pubname (decl, die: subr_die);
23904 if (debug_info_level > DINFO_LEVEL_TERSE)
23905 {
23906 add_prototyped_attribute (die: subr_die, TREE_TYPE (decl));
23907 add_type_attribute (object_die: subr_die, TREE_TYPE (TREE_TYPE (decl)),
23908 cv_quals: TYPE_UNQUALIFIED, reverse: false, context_die);
23909 }
23910
23911 add_pure_or_virtual_attribute (die: subr_die, func_decl: decl);
23912 if (DECL_ARTIFICIAL (decl))
23913 add_AT_flag (die: subr_die, attr_kind: DW_AT_artificial, flag: 1);
23914
23915 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23916 add_AT_flag (die: subr_die, attr_kind: DW_AT_noreturn, flag: 1);
23917
23918 add_alignment_attribute (die: subr_die, tree_node: decl);
23919
23920 add_accessibility_attribute (die: subr_die, decl);
23921 }
23922
23923 /* Unless we have an existing non-declaration DIE, equate the new
23924 DIE. */
23925 if (!old_die || is_declaration_die (die: old_die))
23926 equate_decl_number_to_die (decl, decl_die: subr_die);
23927
23928 if (declaration)
23929 {
23930 if (!old_die || !get_AT (die: old_die, attr_kind: DW_AT_inline))
23931 {
23932 add_AT_flag (die: subr_die, attr_kind: DW_AT_declaration, flag: 1);
23933
23934 /* If this is an explicit function declaration then generate
23935 a DW_AT_explicit attribute. */
23936 if ((dwarf_version >= 3 || !dwarf_strict)
23937 && lang_hooks.decls.decl_dwarf_attribute (decl,
23938 DW_AT_explicit) == 1)
23939 add_AT_flag (die: subr_die, attr_kind: DW_AT_explicit, flag: 1);
23940
23941 /* If this is a C++11 deleted special function member then generate
23942 a DW_AT_deleted attribute. */
23943 if ((dwarf_version >= 5 || !dwarf_strict)
23944 && lang_hooks.decls.decl_dwarf_attribute (decl,
23945 DW_AT_deleted) == 1)
23946 add_AT_flag (die: subr_die, attr_kind: DW_AT_deleted, flag: 1);
23947
23948 /* If this is a C++11 defaulted special function member then
23949 generate a DW_AT_defaulted attribute. */
23950 if (dwarf_version >= 5 || !dwarf_strict)
23951 {
23952 int defaulted
23953 = lang_hooks.decls.decl_dwarf_attribute (decl,
23954 DW_AT_defaulted);
23955 if (defaulted != -1)
23956 add_AT_unsigned (die: subr_die, attr_kind: DW_AT_defaulted, unsigned_val: defaulted);
23957 }
23958
23959 /* If this is a C++11 non-static member function with & ref-qualifier
23960 then generate a DW_AT_reference attribute. */
23961 if ((dwarf_version >= 5 || !dwarf_strict)
23962 && lang_hooks.decls.decl_dwarf_attribute (decl,
23963 DW_AT_reference) == 1)
23964 add_AT_flag (die: subr_die, attr_kind: DW_AT_reference, flag: 1);
23965
23966 /* If this is a C++11 non-static member function with &&
23967 ref-qualifier then generate a DW_AT_reference attribute. */
23968 if ((dwarf_version >= 5 || !dwarf_strict)
23969 && lang_hooks.decls.decl_dwarf_attribute (decl,
23970 DW_AT_rvalue_reference)
23971 == 1)
23972 add_AT_flag (die: subr_die, attr_kind: DW_AT_rvalue_reference, flag: 1);
23973 }
23974 }
23975 /* For non DECL_EXTERNALs, if range information is available, fill
23976 the DIE with it. */
23977 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23978 {
23979 HOST_WIDE_INT cfa_fb_offset;
23980
23981 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23982
23983 if (!crtl->has_bb_partition)
23984 {
23985 dw_fde_ref fde = fun->fde;
23986 if (fde->dw_fde_begin)
23987 {
23988 /* We have already generated the labels. */
23989 add_AT_low_high_pc (die: subr_die, lbl_low: fde->dw_fde_begin,
23990 lbl_high: fde->dw_fde_end, force_direct: false);
23991 }
23992 else
23993 {
23994 /* Create start/end labels and add the range. */
23995 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23996 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23997 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23998 current_function_funcdef_no);
23999 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
24000 current_function_funcdef_no);
24001 add_AT_low_high_pc (die: subr_die, lbl_low: label_id_low, lbl_high: label_id_high,
24002 force_direct: false);
24003 }
24004
24005#if VMS_DEBUGGING_INFO
24006 /* HP OpenVMS Industry Standard 64: DWARF Extensions
24007 Section 2.3 Prologue and Epilogue Attributes:
24008 When a breakpoint is set on entry to a function, it is generally
24009 desirable for execution to be suspended, not on the very first
24010 instruction of the function, but rather at a point after the
24011 function's frame has been set up, after any language defined local
24012 declaration processing has been completed, and before execution of
24013 the first statement of the function begins. Debuggers generally
24014 cannot properly determine where this point is. Similarly for a
24015 breakpoint set on exit from a function. The prologue and epilogue
24016 attributes allow a compiler to communicate the location(s) to use. */
24017
24018 {
24019 if (fde->dw_fde_vms_end_prologue)
24020 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
24021 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
24022
24023 if (fde->dw_fde_vms_begin_epilogue)
24024 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
24025 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
24026 }
24027#endif
24028
24029 }
24030 else
24031 {
24032 /* Generate pubnames entries for the split function code ranges. */
24033 dw_fde_ref fde = fun->fde;
24034
24035 if (fde->dw_fde_second_begin)
24036 {
24037 if (dwarf_version >= 3 || !dwarf_strict)
24038 {
24039 /* We should use ranges for non-contiguous code section
24040 addresses. Use the actual code range for the initial
24041 section, since the HOT/COLD labels might precede an
24042 alignment offset. */
24043 bool range_list_added = false;
24044 add_ranges_by_labels (die: subr_die, begin: fde->dw_fde_begin,
24045 end: fde->dw_fde_end, added: &range_list_added,
24046 force_direct: false);
24047 add_ranges_by_labels (die: subr_die, begin: fde->dw_fde_second_begin,
24048 end: fde->dw_fde_second_end,
24049 added: &range_list_added, force_direct: false);
24050 if (range_list_added)
24051 add_ranges (NULL);
24052 }
24053 else
24054 {
24055 /* There is no real support in DW2 for this .. so we make
24056 a work-around. First, emit the pub name for the segment
24057 containing the function label. Then make and emit a
24058 simplified subprogram DIE for the second segment with the
24059 name pre-fixed by __hot/cold_sect_of_. We use the same
24060 linkage name for the second die so that gdb will find both
24061 sections when given "b foo". */
24062 const char *name = NULL;
24063 tree decl_name = DECL_NAME (decl);
24064 dw_die_ref seg_die;
24065
24066 /* Do the 'primary' section. */
24067 add_AT_low_high_pc (die: subr_die, lbl_low: fde->dw_fde_begin,
24068 lbl_high: fde->dw_fde_end, force_direct: false);
24069
24070 /* Build a minimal DIE for the secondary section. */
24071 seg_die = new_die (tag_value: DW_TAG_subprogram,
24072 parent_die: subr_die->die_parent, t: decl);
24073
24074 if (TREE_PUBLIC (decl))
24075 add_AT_flag (die: seg_die, attr_kind: DW_AT_external, flag: 1);
24076
24077 if (decl_name != NULL
24078 && IDENTIFIER_POINTER (decl_name) != NULL)
24079 {
24080 name = dwarf2_name (decl, scope: 1);
24081 if (! DECL_ARTIFICIAL (decl))
24082 add_src_coords_attributes (die: seg_die, decl);
24083
24084 add_linkage_name (die: seg_die, decl);
24085 }
24086 gcc_assert (name != NULL);
24087 add_pure_or_virtual_attribute (die: seg_die, func_decl: decl);
24088 if (DECL_ARTIFICIAL (decl))
24089 add_AT_flag (die: seg_die, attr_kind: DW_AT_artificial, flag: 1);
24090
24091 name = concat ("__second_sect_of_", name, NULL);
24092 add_AT_low_high_pc (die: seg_die, lbl_low: fde->dw_fde_second_begin,
24093 lbl_high: fde->dw_fde_second_end, force_direct: false);
24094 add_name_attribute (die: seg_die, name_string: name);
24095 if (want_pubnames ())
24096 add_pubname_string (str: name, die: seg_die);
24097 }
24098 }
24099 else
24100 add_AT_low_high_pc (die: subr_die, lbl_low: fde->dw_fde_begin, lbl_high: fde->dw_fde_end,
24101 force_direct: false);
24102 }
24103
24104 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
24105
24106 /* We define the "frame base" as the function's CFA. This is more
24107 convenient for several reasons: (1) It's stable across the prologue
24108 and epilogue, which makes it better than just a frame pointer,
24109 (2) With dwarf3, there exists a one-byte encoding that allows us
24110 to reference the .debug_frame data by proxy, but failing that,
24111 (3) We can at least reuse the code inspection and interpretation
24112 code that determines the CFA position at various points in the
24113 function. */
24114 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
24115 {
24116 dw_loc_descr_ref op = new_loc_descr (op: DW_OP_call_frame_cfa, oprnd1: 0, oprnd2: 0);
24117 add_AT_loc (die: subr_die, attr_kind: DW_AT_frame_base, loc: op);
24118 }
24119 else
24120 {
24121 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (offset: cfa_fb_offset);
24122 if (list->dw_loc_next)
24123 add_AT_loc_list (die: subr_die, attr_kind: DW_AT_frame_base, loc_list: list);
24124 else
24125 add_AT_loc (die: subr_die, attr_kind: DW_AT_frame_base, loc: list->expr);
24126 }
24127
24128 /* Compute a displacement from the "steady-state frame pointer" to
24129 the CFA. The former is what all stack slots and argument slots
24130 will reference in the rtl; the latter is what we've told the
24131 debugger about. We'll need to adjust all frame_base references
24132 by this displacement. */
24133 compute_frame_pointer_to_fb_displacement (offset: cfa_fb_offset);
24134
24135 if (fun->static_chain_decl)
24136 {
24137 /* DWARF requires here a location expression that computes the
24138 address of the enclosing subprogram's frame base. The machinery
24139 in tree-nested.cc is supposed to store this specific address in the
24140 last field of the FRAME record. */
24141 const tree frame_type
24142 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24143 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24144
24145 tree fb_expr
24146 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24147 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24148 fb_expr, fb_decl, NULL_TREE);
24149
24150 add_AT_location_description (die: subr_die, attr_kind: DW_AT_static_link,
24151 descr: loc_list_from_tree (loc: fb_expr, want_address: 0, NULL));
24152 }
24153
24154 resolve_variable_values ();
24155 }
24156
24157 /* Generate child dies for template parameters. */
24158 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24159 gen_generic_params_dies (t: decl);
24160
24161 /* Now output descriptions of the arguments for this function. This gets
24162 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24163 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24164 `...' at the end of the formal parameter list. In order to find out if
24165 there was a trailing ellipsis or not, we must instead look at the type
24166 associated with the FUNCTION_DECL. This will be a node of type
24167 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24168 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24169 an ellipsis at the end. */
24170
24171 /* In the case where we are describing a mere function declaration, all we
24172 need to do here (and all we *can* do here) is to describe the *types* of
24173 its formal parameters. */
24174 if (debug_info_level <= DINFO_LEVEL_TERSE)
24175 ;
24176 else if (declaration)
24177 gen_formal_types_die (function_or_method_type: decl, context_die: subr_die);
24178 else
24179 {
24180 /* Generate DIEs to represent all known formal parameters. */
24181 tree parm = DECL_ARGUMENTS (decl);
24182 tree generic_decl = early_dwarf
24183 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24184 tree generic_decl_parm = generic_decl
24185 ? DECL_ARGUMENTS (generic_decl)
24186 : NULL;
24187
24188 /* Now we want to walk the list of parameters of the function and
24189 emit their relevant DIEs.
24190
24191 We consider the case of DECL being an instance of a generic function
24192 as well as it being a normal function.
24193
24194 If DECL is an instance of a generic function we walk the
24195 parameters of the generic function declaration _and_ the parameters of
24196 DECL itself. This is useful because we want to emit specific DIEs for
24197 function parameter packs and those are declared as part of the
24198 generic function declaration. In that particular case,
24199 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24200 That DIE has children DIEs representing the set of arguments
24201 of the pack. Note that the set of pack arguments can be empty.
24202 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24203 children DIE.
24204
24205 Otherwise, we just consider the parameters of DECL. */
24206 while (generic_decl_parm || parm)
24207 {
24208 if (generic_decl_parm
24209 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24210 gen_formal_parameter_pack_die (parm_pack: generic_decl_parm,
24211 pack_arg: parm, subr_die,
24212 next_arg: &parm);
24213 else if (parm)
24214 {
24215 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24216
24217 if (early_dwarf
24218 && parm == DECL_ARGUMENTS (decl)
24219 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24220 && parm_die
24221 && (dwarf_version >= 3 || !dwarf_strict))
24222 add_AT_die_ref (die: subr_die, attr_kind: DW_AT_object_pointer, targ_die: parm_die);
24223
24224 parm = DECL_CHAIN (parm);
24225 }
24226
24227 if (generic_decl_parm)
24228 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24229 }
24230
24231 /* Decide whether we need an unspecified_parameters DIE at the end.
24232 There are 2 more cases to do this for: 1) the ansi ... declaration -
24233 this is detectable when the end of the arg list is not a
24234 void_type_node 2) an unprototyped function declaration (not a
24235 definition). This just means that we have no info about the
24236 parameters at all. */
24237 if (early_dwarf)
24238 {
24239 if (prototype_p (TREE_TYPE (decl)))
24240 {
24241 /* This is the prototyped case, check for.... */
24242 if (stdarg_p (TREE_TYPE (decl)))
24243 gen_unspecified_parameters_die (decl_or_type: decl, context_die: subr_die);
24244 }
24245 else if (DECL_INITIAL (decl) == NULL_TREE)
24246 gen_unspecified_parameters_die (decl_or_type: decl, context_die: subr_die);
24247 }
24248 else if ((subr_die != old_die || old_die_had_no_children)
24249 && prototype_p (TREE_TYPE (decl))
24250 && stdarg_p (TREE_TYPE (decl)))
24251 gen_unspecified_parameters_die (decl_or_type: decl, context_die: subr_die);
24252 }
24253
24254 if (subr_die != old_die)
24255 /* Add the calling convention attribute if requested. */
24256 add_calling_convention_attribute (subr_die, decl);
24257
24258 /* Output Dwarf info for all of the stuff within the body of the function
24259 (if it has one - it may be just a declaration).
24260
24261 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24262 a function. This BLOCK actually represents the outermost binding contour
24263 for the function, i.e. the contour in which the function's formal
24264 parameters and labels get declared. Curiously, it appears that the front
24265 end doesn't actually put the PARM_DECL nodes for the current function onto
24266 the BLOCK_VARS list for this outer scope, but are strung off of the
24267 DECL_ARGUMENTS list for the function instead.
24268
24269 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24270 the LABEL_DECL nodes for the function however, and we output DWARF info
24271 for those in decls_for_scope. Just within the `outer_scope' there will be
24272 a BLOCK node representing the function's outermost pair of curly braces,
24273 and any blocks used for the base and member initializers of a C++
24274 constructor function. */
24275 tree outer_scope = DECL_INITIAL (decl);
24276 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24277 {
24278 int call_site_note_count = 0;
24279 int tail_call_site_note_count = 0;
24280
24281 /* Emit a DW_TAG_variable DIE for a named return value. */
24282 if (DECL_NAME (DECL_RESULT (decl)))
24283 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24284
24285 /* The first time through decls_for_scope we will generate the
24286 DIEs for the locals. The second time, we fill in the
24287 location info. */
24288 decls_for_scope (outer_scope, subr_die);
24289
24290 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24291 {
24292 struct call_arg_loc_node *ca_loc;
24293 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24294 {
24295 dw_die_ref die = NULL;
24296 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24297 rtx call_arg_loc_note
24298 = find_reg_note (ca_loc->call_insn,
24299 REG_CALL_ARG_LOCATION, NULL_RTX);
24300 rtx arg, next_arg;
24301 tree arg_decl = NULL_TREE;
24302
24303 for (arg = (call_arg_loc_note != NULL_RTX
24304 ? XEXP (call_arg_loc_note, 0)
24305 : NULL_RTX);
24306 arg; arg = next_arg)
24307 {
24308 dw_loc_descr_ref reg, val;
24309 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24310 dw_die_ref cdie, tdie = NULL;
24311
24312 next_arg = XEXP (arg, 1);
24313 if (REG_P (XEXP (XEXP (arg, 0), 0))
24314 && next_arg
24315 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24316 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24317 && REGNO (XEXP (XEXP (arg, 0), 0))
24318 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24319 next_arg = XEXP (next_arg, 1);
24320 if (mode == VOIDmode)
24321 {
24322 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24323 if (mode == VOIDmode)
24324 mode = GET_MODE (XEXP (arg, 0));
24325 }
24326 if (mode == VOIDmode || mode == BLKmode)
24327 continue;
24328 /* Get dynamic information about call target only if we
24329 have no static information: we cannot generate both
24330 DW_AT_call_origin and DW_AT_call_target
24331 attributes. */
24332 if (ca_loc->symbol_ref == NULL_RTX)
24333 {
24334 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24335 {
24336 tloc = XEXP (XEXP (arg, 0), 1);
24337 continue;
24338 }
24339 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24340 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24341 {
24342 tlocc = XEXP (XEXP (arg, 0), 1);
24343 continue;
24344 }
24345 }
24346 reg = NULL;
24347 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24348 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24349 initialized: VAR_INIT_STATUS_INITIALIZED);
24350 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24351 {
24352 rtx mem = XEXP (XEXP (arg, 0), 0);
24353 reg = mem_loc_descriptor (XEXP (mem, 0),
24354 mode: get_address_mode (mem),
24355 GET_MODE (mem),
24356 initialized: VAR_INIT_STATUS_INITIALIZED);
24357 }
24358 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24359 == DEBUG_PARAMETER_REF)
24360 {
24361 tree tdecl
24362 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24363 tdie = lookup_decl_die (decl: tdecl);
24364 if (tdie == NULL)
24365 continue;
24366 arg_decl = tdecl;
24367 }
24368 else
24369 continue;
24370 if (reg == NULL
24371 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24372 != DEBUG_PARAMETER_REF)
24373 continue;
24374 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24375 VOIDmode,
24376 initialized: VAR_INIT_STATUS_INITIALIZED);
24377 if (val == NULL)
24378 continue;
24379 if (die == NULL)
24380 die = gen_call_site_die (decl, subr_die, ca_loc);
24381 cdie = new_die (tag_value: dwarf_TAG (tag: DW_TAG_call_site_parameter), parent_die: die,
24382 NULL_TREE);
24383 add_desc_attribute (die: cdie, decl: arg_decl);
24384 if (reg != NULL)
24385 add_AT_loc (die: cdie, attr_kind: DW_AT_location, loc: reg);
24386 else if (tdie != NULL)
24387 add_AT_die_ref (die: cdie, attr_kind: dwarf_AT (at: DW_AT_call_parameter),
24388 targ_die: tdie);
24389 add_AT_loc (die: cdie, attr_kind: dwarf_AT (at: DW_AT_call_value), loc: val);
24390 if (next_arg != XEXP (arg, 1))
24391 {
24392 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24393 if (mode == VOIDmode)
24394 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24395 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24396 0), 1),
24397 mode, VOIDmode,
24398 initialized: VAR_INIT_STATUS_INITIALIZED);
24399 if (val != NULL)
24400 add_AT_loc (die: cdie, attr_kind: dwarf_AT (at: DW_AT_call_data_value),
24401 loc: val);
24402 }
24403 }
24404 if (die == NULL
24405 && (ca_loc->symbol_ref || tloc))
24406 die = gen_call_site_die (decl, subr_die, ca_loc);
24407 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24408 {
24409 dw_loc_descr_ref tval = NULL;
24410
24411 if (tloc != NULL_RTX)
24412 tval = mem_loc_descriptor (rtl: tloc,
24413 GET_MODE (tloc) == VOIDmode
24414 ? Pmode : GET_MODE (tloc),
24415 VOIDmode,
24416 initialized: VAR_INIT_STATUS_INITIALIZED);
24417 if (tval)
24418 add_AT_loc (die, attr_kind: dwarf_AT (at: DW_AT_call_target), loc: tval);
24419 else if (tlocc != NULL_RTX)
24420 {
24421 tval = mem_loc_descriptor (rtl: tlocc,
24422 GET_MODE (tlocc) == VOIDmode
24423 ? Pmode : GET_MODE (tlocc),
24424 VOIDmode,
24425 initialized: VAR_INIT_STATUS_INITIALIZED);
24426 if (tval)
24427 add_AT_loc (die,
24428 attr_kind: dwarf_AT (at: DW_AT_call_target_clobbered),
24429 loc: tval);
24430 }
24431 }
24432 if (die != NULL)
24433 {
24434 call_site_note_count++;
24435 if (ca_loc->tail_call_p)
24436 tail_call_site_note_count++;
24437 }
24438 }
24439 }
24440 call_arg_locations = NULL;
24441 call_arg_loc_last = NULL;
24442 if (tail_call_site_count >= 0
24443 && tail_call_site_count == tail_call_site_note_count
24444 && (!dwarf_strict || dwarf_version >= 5))
24445 {
24446 if (call_site_count >= 0
24447 && call_site_count == call_site_note_count)
24448 add_AT_flag (die: subr_die, attr_kind: dwarf_AT (at: DW_AT_call_all_calls), flag: 1);
24449 else
24450 add_AT_flag (die: subr_die, attr_kind: dwarf_AT (at: DW_AT_call_all_tail_calls), flag: 1);
24451 }
24452 call_site_count = -1;
24453 tail_call_site_count = -1;
24454 }
24455
24456 /* Mark used types after we have created DIEs for the functions scopes. */
24457 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24458}
24459
24460/* Returns a hash value for X (which really is a die_struct). */
24461
24462hashval_t
24463block_die_hasher::hash (die_struct *d)
24464{
24465 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24466}
24467
24468/* Return true if decl_id and die_parent of die_struct X is the same
24469 as decl_id and die_parent of die_struct Y. */
24470
24471bool
24472block_die_hasher::equal (die_struct *x, die_struct *y)
24473{
24474 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24475}
24476
24477/* Hold information about markers for inlined entry points. */
24478struct GTY ((for_user)) inline_entry_data
24479{
24480 /* The block that's the inlined_function_outer_scope for an inlined
24481 function. */
24482 tree block;
24483
24484 /* The label at the inlined entry point. */
24485 const char *label_pfx;
24486 unsigned int label_num;
24487
24488 /* The view number to be used as the inlined entry point. */
24489 var_loc_view view;
24490};
24491
24492struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24493{
24494 typedef tree compare_type;
24495 static inline hashval_t hash (const inline_entry_data *);
24496 static inline bool equal (const inline_entry_data *, const_tree);
24497};
24498
24499/* Hash table routines for inline_entry_data. */
24500
24501inline hashval_t
24502inline_entry_data_hasher::hash (const inline_entry_data *data)
24503{
24504 return htab_hash_pointer (data->block);
24505}
24506
24507inline bool
24508inline_entry_data_hasher::equal (const inline_entry_data *data,
24509 const_tree block)
24510{
24511 return data->block == block;
24512}
24513
24514/* Inlined entry points pending DIE creation in this compilation unit. */
24515
24516static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24517
24518
24519/* Return TRUE if DECL, which may have been previously generated as
24520 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24521 true if decl (or its origin) is either an extern declaration or a
24522 class/namespace scoped declaration.
24523
24524 The declare_in_namespace support causes us to get two DIEs for one
24525 variable, both of which are declarations. We want to avoid
24526 considering one to be a specification, so we must test for
24527 DECLARATION and DW_AT_declaration. */
24528static inline bool
24529decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24530{
24531 return (old_die && TREE_STATIC (decl) && !declaration
24532 && get_AT_flag (die: old_die, attr_kind: DW_AT_declaration) == 1);
24533}
24534
24535/* Return true if DECL is a local static. */
24536
24537static inline bool
24538local_function_static (tree decl)
24539{
24540 gcc_assert (VAR_P (decl));
24541 return TREE_STATIC (decl)
24542 && DECL_CONTEXT (decl)
24543 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24544}
24545
24546/* Return true iff DECL overrides (presumably completes) the type of
24547 OLD_DIE within CONTEXT_DIE. */
24548
24549static bool
24550override_type_for_decl_p (tree decl, dw_die_ref old_die,
24551 dw_die_ref context_die)
24552{
24553 tree type = TREE_TYPE (decl);
24554 int cv_quals;
24555
24556 if (decl_by_reference_p (decl))
24557 {
24558 type = TREE_TYPE (type);
24559 cv_quals = TYPE_UNQUALIFIED;
24560 }
24561 else
24562 cv_quals = decl_quals (decl);
24563
24564 dw_die_ref type_die = modified_type_die (type,
24565 cv_quals: cv_quals | TYPE_QUALS (type),
24566 reverse: false,
24567 context_die);
24568
24569 dw_die_ref old_type_die = get_AT_ref (die: old_die, attr_kind: DW_AT_type);
24570
24571 return type_die != old_type_die;
24572}
24573
24574/* Generate a DIE to represent a declared data object.
24575 Either DECL or ORIGIN must be non-null. */
24576
24577static void
24578gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24579{
24580 HOST_WIDE_INT off = 0;
24581 tree com_decl;
24582 tree decl_or_origin = decl ? decl : origin;
24583 tree ultimate_origin;
24584 dw_die_ref var_die;
24585 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24586 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24587 || class_or_namespace_scope_p (context_die));
24588 bool specialization_p = false;
24589 bool no_linkage_name = false;
24590
24591 /* While C++ inline static data members have definitions inside of the
24592 class, force the first DIE to be a declaration, then let gen_member_die
24593 reparent it to the class context and call gen_variable_die again
24594 to create the outside of the class DIE for the definition. */
24595 if (!declaration
24596 && old_die == NULL
24597 && decl
24598 && DECL_CONTEXT (decl)
24599 && TYPE_P (DECL_CONTEXT (decl))
24600 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24601 {
24602 declaration = true;
24603 if (dwarf_version < 5)
24604 no_linkage_name = true;
24605 }
24606
24607 ultimate_origin = decl_ultimate_origin (decl: decl_or_origin);
24608 if (decl || ultimate_origin)
24609 origin = ultimate_origin;
24610 com_decl = fortran_common (decl: decl_or_origin, value: &off);
24611
24612 /* Symbol in common gets emitted as a child of the common block, in the form
24613 of a data member. */
24614 if (com_decl)
24615 {
24616 dw_die_ref com_die;
24617 dw_loc_list_ref loc = NULL;
24618 die_node com_die_arg;
24619
24620 var_die = lookup_decl_die (decl: decl_or_origin);
24621 if (var_die)
24622 {
24623 if (! early_dwarf && get_AT (die: var_die, attr_kind: DW_AT_location) == NULL)
24624 {
24625 loc = loc_list_from_tree (loc: com_decl, want_address: off ? 1 : 2, NULL);
24626 if (loc)
24627 {
24628 if (off)
24629 {
24630 /* Optimize the common case. */
24631 if (single_element_loc_list_p (list: loc)
24632 && loc->expr->dw_loc_opc == DW_OP_addr
24633 && loc->expr->dw_loc_next == NULL
24634 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24635 == SYMBOL_REF)
24636 {
24637 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24638 loc->expr->dw_loc_oprnd1.v.val_addr
24639 = plus_constant (GET_MODE (x), x , off);
24640 }
24641 else
24642 loc_list_plus_const (list_head: loc, offset: off);
24643 }
24644 add_AT_location_description (die: var_die, attr_kind: DW_AT_location, descr: loc);
24645 remove_AT (die: var_die, attr_kind: DW_AT_declaration);
24646 }
24647 }
24648 return;
24649 }
24650
24651 if (common_block_die_table == NULL)
24652 common_block_die_table = hash_table<block_die_hasher>::create_ggc (n: 10);
24653
24654 com_die_arg.decl_id = DECL_UID (com_decl);
24655 com_die_arg.die_parent = context_die;
24656 com_die = common_block_die_table->find (value: &com_die_arg);
24657 if (! early_dwarf)
24658 loc = loc_list_from_tree (loc: com_decl, want_address: 2, NULL);
24659 if (com_die == NULL)
24660 {
24661 const char *cnam
24662 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24663 die_node **slot;
24664
24665 com_die = new_die (tag_value: DW_TAG_common_block, parent_die: context_die, t: decl);
24666 add_name_and_src_coords_attributes (die: com_die, decl: com_decl);
24667 if (loc)
24668 {
24669 add_AT_location_description (die: com_die, attr_kind: DW_AT_location, descr: loc);
24670 /* Avoid sharing the same loc descriptor between
24671 DW_TAG_common_block and DW_TAG_variable. */
24672 loc = loc_list_from_tree (loc: com_decl, want_address: 2, NULL);
24673 }
24674 else if (DECL_EXTERNAL (decl_or_origin))
24675 add_AT_flag (die: com_die, attr_kind: DW_AT_declaration, flag: 1);
24676 if (want_pubnames ())
24677 add_pubname_string (str: cnam, die: com_die); /* ??? needed? */
24678 com_die->decl_id = DECL_UID (com_decl);
24679 slot = common_block_die_table->find_slot (value: com_die, insert: INSERT);
24680 *slot = com_die;
24681 }
24682 else if (get_AT (die: com_die, attr_kind: DW_AT_location) == NULL && loc)
24683 {
24684 add_AT_location_description (die: com_die, attr_kind: DW_AT_location, descr: loc);
24685 loc = loc_list_from_tree (loc: com_decl, want_address: 2, NULL);
24686 remove_AT (die: com_die, attr_kind: DW_AT_declaration);
24687 }
24688 var_die = new_die (tag_value: DW_TAG_variable, parent_die: com_die, t: decl);
24689 add_name_and_src_coords_attributes (die: var_die, decl: decl_or_origin);
24690 add_type_attribute (object_die: var_die, TREE_TYPE (decl_or_origin),
24691 cv_quals: decl_quals (decl: decl_or_origin), reverse: false,
24692 context_die);
24693 add_alignment_attribute (die: var_die, tree_node: decl);
24694 add_AT_flag (die: var_die, attr_kind: DW_AT_external, flag: 1);
24695 if (loc)
24696 {
24697 if (off)
24698 {
24699 /* Optimize the common case. */
24700 if (single_element_loc_list_p (list: loc)
24701 && loc->expr->dw_loc_opc == DW_OP_addr
24702 && loc->expr->dw_loc_next == NULL
24703 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24704 {
24705 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24706 loc->expr->dw_loc_oprnd1.v.val_addr
24707 = plus_constant (GET_MODE (x), x, off);
24708 }
24709 else
24710 loc_list_plus_const (list_head: loc, offset: off);
24711 }
24712 add_AT_location_description (die: var_die, attr_kind: DW_AT_location, descr: loc);
24713 }
24714 else if (DECL_EXTERNAL (decl_or_origin))
24715 add_AT_flag (die: var_die, attr_kind: DW_AT_declaration, flag: 1);
24716 if (decl)
24717 equate_decl_number_to_die (decl, decl_die: var_die);
24718 return;
24719 }
24720
24721 if (old_die)
24722 {
24723 if (declaration)
24724 {
24725 /* A declaration that has been previously dumped, needs no
24726 further annotations, since it doesn't need location on
24727 the second pass. */
24728 return;
24729 }
24730 else if (decl_will_get_specification_p (old_die, decl, declaration)
24731 && !get_AT (die: old_die, attr_kind: DW_AT_specification))
24732 {
24733 /* Fall-thru so we can make a new variable die along with a
24734 DW_AT_specification. */
24735 }
24736 else if (origin && old_die->die_parent != context_die)
24737 {
24738 /* If we will be creating an inlined instance, we need a
24739 new DIE that will get annotated with
24740 DW_AT_abstract_origin. */
24741 gcc_assert (!DECL_ABSTRACT_P (decl));
24742 }
24743 else
24744 {
24745 /* If a DIE was dumped early, it still needs location info.
24746 Skip to where we fill the location bits. */
24747 var_die = old_die;
24748
24749 /* ??? In LTRANS we cannot annotate early created variably
24750 modified type DIEs without copying them and adjusting all
24751 references to them. Thus we dumped them again. Also add a
24752 reference to them but beware of -g0 compile and -g link
24753 in which case the reference will be already present. */
24754 tree type = TREE_TYPE (decl_or_origin);
24755 if (in_lto_p
24756 && ! get_AT (die: var_die, attr_kind: DW_AT_type)
24757 && variably_modified_type_p
24758 (type, decl_function_context (decl_or_origin)))
24759 {
24760 if (decl_by_reference_p (decl: decl_or_origin))
24761 add_type_attribute (object_die: var_die, TREE_TYPE (type),
24762 cv_quals: TYPE_UNQUALIFIED, reverse: false, context_die);
24763 else
24764 add_type_attribute (object_die: var_die, type, cv_quals: decl_quals (decl: decl_or_origin),
24765 reverse: false, context_die);
24766 }
24767
24768 goto gen_variable_die_location;
24769 }
24770 }
24771
24772 /* For static data members, the declaration in the class is supposed
24773 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24774 also in DWARF2; the specification should still be DW_TAG_variable
24775 referencing the DW_TAG_member DIE. */
24776 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24777 var_die = new_die (tag_value: DW_TAG_member, parent_die: context_die, t: decl);
24778 else
24779 var_die = new_die (tag_value: DW_TAG_variable, parent_die: context_die, t: decl);
24780
24781 if (origin != NULL)
24782 add_abstract_origin_attribute (die: var_die, origin);
24783
24784 /* Loop unrolling can create multiple blocks that refer to the same
24785 static variable, so we must test for the DW_AT_declaration flag.
24786
24787 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24788 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24789 sharing them.
24790
24791 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24792 else if (decl_will_get_specification_p (old_die, decl, declaration))
24793 {
24794 /* This is a definition of a C++ class level static. */
24795 add_AT_specification (die: var_die, targ_die: old_die);
24796 specialization_p = true;
24797 if (DECL_NAME (decl))
24798 {
24799 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24800 struct dwarf_file_data * file_index = lookup_filename (s.file);
24801
24802 if (get_AT_file (die: old_die, attr_kind: DW_AT_decl_file) != file_index)
24803 add_AT_file (die: var_die, attr_kind: DW_AT_decl_file, fd: file_index);
24804
24805 if (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_line) != (unsigned) s.line)
24806 add_AT_unsigned (die: var_die, attr_kind: DW_AT_decl_line, unsigned_val: s.line);
24807
24808 if (debug_column_info
24809 && s.column
24810 && (get_AT_unsigned (die: old_die, attr_kind: DW_AT_decl_column)
24811 != (unsigned) s.column))
24812 add_AT_unsigned (die: var_die, attr_kind: DW_AT_decl_column, unsigned_val: s.column);
24813
24814 if (old_die->die_tag == DW_TAG_member)
24815 add_linkage_name (die: var_die, decl);
24816 }
24817 }
24818 else
24819 add_name_and_src_coords_attributes (die: var_die, decl, no_linkage_name);
24820
24821 if ((origin == NULL && !specialization_p)
24822 || (origin != NULL
24823 && !DECL_ABSTRACT_P (decl_or_origin)
24824 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24825 decl_function_context
24826 (decl_or_origin)))
24827 || (old_die && specialization_p
24828 && override_type_for_decl_p (decl: decl_or_origin, old_die, context_die)))
24829 {
24830 tree type = TREE_TYPE (decl_or_origin);
24831
24832 if (decl_by_reference_p (decl: decl_or_origin))
24833 add_type_attribute (object_die: var_die, TREE_TYPE (type), cv_quals: TYPE_UNQUALIFIED, reverse: false,
24834 context_die);
24835 else
24836 add_type_attribute (object_die: var_die, type, cv_quals: decl_quals (decl: decl_or_origin), reverse: false,
24837 context_die);
24838 }
24839
24840 if (origin == NULL && !specialization_p)
24841 {
24842 if (TREE_PUBLIC (decl))
24843 add_AT_flag (die: var_die, attr_kind: DW_AT_external, flag: 1);
24844
24845 if (DECL_ARTIFICIAL (decl))
24846 add_AT_flag (die: var_die, attr_kind: DW_AT_artificial, flag: 1);
24847
24848 add_alignment_attribute (die: var_die, tree_node: decl);
24849
24850 add_accessibility_attribute (die: var_die, decl);
24851 }
24852
24853 if (declaration)
24854 add_AT_flag (die: var_die, attr_kind: DW_AT_declaration, flag: 1);
24855
24856 if (decl && (DECL_ABSTRACT_P (decl)
24857 || !old_die || is_declaration_die (die: old_die)))
24858 equate_decl_number_to_die (decl, decl_die: var_die);
24859
24860 gen_variable_die_location:
24861 if (! declaration
24862 && (! DECL_ABSTRACT_P (decl_or_origin)
24863 /* Local static vars are shared between all clones/inlines,
24864 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24865 already set. */
24866 || (VAR_P (decl_or_origin)
24867 && TREE_STATIC (decl_or_origin)
24868 && DECL_RTL_SET_P (decl_or_origin))))
24869 {
24870 if (early_dwarf)
24871 {
24872 add_pubname (decl: decl_or_origin, die: var_die);
24873 /* For global register variables, emit DW_AT_location if possible
24874 already during early_dwarf, as late_global_decl won't be usually
24875 called. */
24876 if (DECL_HARD_REGISTER (decl_or_origin)
24877 && TREE_STATIC (decl_or_origin)
24878 && !decl_by_reference_p (decl: decl_or_origin)
24879 && !get_AT (die: var_die, attr_kind: DW_AT_location)
24880 && !get_AT (die: var_die, attr_kind: DW_AT_const_value)
24881 && DECL_RTL_SET_P (decl_or_origin)
24882 && REG_P (DECL_RTL (decl_or_origin)))
24883 {
24884 dw_loc_descr_ref descr
24885 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24886 initialized: VAR_INIT_STATUS_INITIALIZED);
24887 if (descr)
24888 add_AT_loc (die: var_die, attr_kind: DW_AT_location, loc: descr);
24889 }
24890 }
24891 else
24892 add_location_or_const_value_attribute (die: var_die, decl: decl_or_origin,
24893 cache_p: decl == NULL);
24894 }
24895 else
24896 tree_add_const_value_attribute_for_decl (var_die, decl: decl_or_origin);
24897
24898 if ((dwarf_version >= 4 || !dwarf_strict)
24899 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24900 DW_AT_const_expr) == 1
24901 && !get_AT (die: var_die, attr_kind: DW_AT_const_expr)
24902 && !specialization_p)
24903 add_AT_flag (die: var_die, attr_kind: DW_AT_const_expr, flag: 1);
24904
24905 if (!dwarf_strict)
24906 {
24907 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24908 DW_AT_inline);
24909 if (inl != -1
24910 && !get_AT (die: var_die, attr_kind: DW_AT_inline)
24911 && !specialization_p)
24912 add_AT_unsigned (die: var_die, attr_kind: DW_AT_inline, unsigned_val: inl);
24913 }
24914}
24915
24916/* Generate a DIE to represent a named constant. */
24917
24918static void
24919gen_const_die (tree decl, dw_die_ref context_die)
24920{
24921 dw_die_ref const_die;
24922 tree type = TREE_TYPE (decl);
24923
24924 const_die = lookup_decl_die (decl);
24925 if (const_die)
24926 return;
24927
24928 const_die = new_die (tag_value: DW_TAG_constant, parent_die: context_die, t: decl);
24929 equate_decl_number_to_die (decl, decl_die: const_die);
24930 add_name_and_src_coords_attributes (die: const_die, decl);
24931 add_type_attribute (object_die: const_die, type, cv_quals: TYPE_QUAL_CONST, reverse: false, context_die);
24932 if (TREE_PUBLIC (decl))
24933 add_AT_flag (die: const_die, attr_kind: DW_AT_external, flag: 1);
24934 if (DECL_ARTIFICIAL (decl))
24935 add_AT_flag (die: const_die, attr_kind: DW_AT_artificial, flag: 1);
24936 tree_add_const_value_attribute_for_decl (var_die: const_die, decl);
24937}
24938
24939/* Generate a DIE to represent a label identifier. */
24940
24941static void
24942gen_label_die (tree decl, dw_die_ref context_die)
24943{
24944 tree origin = decl_ultimate_origin (decl);
24945 dw_die_ref lbl_die = lookup_decl_die (decl);
24946 rtx insn;
24947 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24948
24949 if (!lbl_die)
24950 {
24951 lbl_die = new_die (tag_value: DW_TAG_label, parent_die: context_die, t: decl);
24952 equate_decl_number_to_die (decl, decl_die: lbl_die);
24953
24954 if (origin != NULL)
24955 add_abstract_origin_attribute (die: lbl_die, origin);
24956 else
24957 add_name_and_src_coords_attributes (die: lbl_die, decl);
24958 }
24959
24960 if (DECL_ABSTRACT_P (decl))
24961 equate_decl_number_to_die (decl, decl_die: lbl_die);
24962 else if (! early_dwarf)
24963 {
24964 insn = DECL_RTL_IF_SET (decl);
24965
24966 /* Deleted labels are programmer specified labels which have been
24967 eliminated because of various optimizations. We still emit them
24968 here so that it is possible to put breakpoints on them. */
24969 if (insn
24970 && (LABEL_P (insn)
24971 || ((NOTE_P (insn)
24972 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24973 {
24974 /* When optimization is enabled (via -O) some parts of the compiler
24975 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24976 represent source-level labels which were explicitly declared by
24977 the user. This really shouldn't be happening though, so catch
24978 it if it ever does happen. */
24979 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24980
24981 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24982 add_AT_lbl_id (die: lbl_die, attr_kind: DW_AT_low_pc, lbl_id: label);
24983 }
24984 else if (insn
24985 && NOTE_P (insn)
24986 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24987 && CODE_LABEL_NUMBER (insn) != -1)
24988 {
24989 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24990 add_AT_lbl_id (die: lbl_die, attr_kind: DW_AT_low_pc, lbl_id: label);
24991 }
24992 }
24993}
24994
24995/* A helper function for gen_inlined_subroutine_die. Add source coordinate
24996 attributes to the DIE for a block STMT, to describe where the inlined
24997 function was called from. This is similar to add_src_coords_attributes. */
24998
24999static inline void
25000add_call_src_coords_attributes (tree stmt, dw_die_ref die)
25001{
25002 /* We can end up with BUILTINS_LOCATION here. */
25003 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
25004 return;
25005
25006 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
25007 expanded_location s = expand_location (locus);
25008
25009 if (dwarf_version >= 3 || !dwarf_strict)
25010 {
25011 add_AT_file (die, attr_kind: DW_AT_call_file, fd: lookup_filename (s.file));
25012 add_AT_unsigned (die, attr_kind: DW_AT_call_line, unsigned_val: s.line);
25013 if (debug_column_info && s.column)
25014 add_AT_unsigned (die, attr_kind: DW_AT_call_column, unsigned_val: s.column);
25015 unsigned discr = get_discriminator_from_loc (locus);
25016 if (discr != 0)
25017 add_AT_unsigned (die, attr_kind: DW_AT_GNU_discriminator, unsigned_val: discr);
25018 }
25019}
25020
25021
25022/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
25023 Add low_pc and high_pc attributes to the DIE for a block STMT. */
25024
25025static inline void
25026add_high_low_attributes (tree stmt, dw_die_ref die)
25027{
25028 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25029
25030 if (inline_entry_data **iedp
25031 = !inline_entry_data_table ? NULL
25032 : inline_entry_data_table->find_slot_with_hash (comparable: stmt,
25033 hash: htab_hash_pointer (stmt),
25034 insert: NO_INSERT))
25035 {
25036 inline_entry_data *ied = *iedp;
25037 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
25038 gcc_assert (debug_inline_points);
25039 gcc_assert (inlined_function_outer_scope_p (stmt));
25040
25041 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
25042 add_AT_lbl_id (die, attr_kind: DW_AT_entry_pc, lbl_id: label);
25043
25044 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
25045 && !dwarf_strict)
25046 {
25047 if (!output_asm_line_debug_info ())
25048 add_AT_unsigned (die, attr_kind: DW_AT_GNU_entry_view, unsigned_val: ied->view);
25049 else
25050 {
25051 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
25052 /* FIXME: this will resolve to a small number. Could we
25053 possibly emit smaller data? Ideally we'd emit a
25054 uleb128, but that would make the size of DIEs
25055 impossible for the compiler to compute, since it's
25056 the assembler that computes the value of the view
25057 label in this case. Ideally, we'd have a single form
25058 encompassing both the address and the view, and
25059 indirecting them through a table might make things
25060 easier, but even that would be more wasteful,
25061 space-wise, than what we have now. */
25062 add_AT_symview (die, attr_kind: DW_AT_GNU_entry_view, view_label: label);
25063 }
25064 }
25065
25066 inline_entry_data_table->clear_slot (slot: iedp);
25067 }
25068
25069 if (BLOCK_FRAGMENT_CHAIN (stmt)
25070 && (dwarf_version >= 3 || !dwarf_strict))
25071 {
25072 tree chain, superblock = NULL_TREE;
25073 dw_die_ref pdie;
25074 dw_attr_node *attr = NULL;
25075
25076 if (!debug_inline_points && inlined_function_outer_scope_p (block: stmt))
25077 {
25078 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25079 BLOCK_NUMBER (stmt));
25080 add_AT_lbl_id (die, attr_kind: DW_AT_entry_pc, lbl_id: label);
25081 }
25082
25083 /* Optimize duplicate .debug_ranges lists or even tails of
25084 lists. If this BLOCK has same ranges as its supercontext,
25085 lookup DW_AT_ranges attribute in the supercontext (and
25086 recursively so), verify that the ranges_table contains the
25087 right values and use it instead of adding a new .debug_range. */
25088 for (chain = stmt, pdie = die;
25089 BLOCK_SAME_RANGE (chain);
25090 chain = BLOCK_SUPERCONTEXT (chain))
25091 {
25092 dw_attr_node *new_attr;
25093
25094 pdie = pdie->die_parent;
25095 if (pdie == NULL)
25096 break;
25097 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
25098 break;
25099 new_attr = get_AT (die: pdie, attr_kind: DW_AT_ranges);
25100 if (new_attr == NULL
25101 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
25102 break;
25103 attr = new_attr;
25104 superblock = BLOCK_SUPERCONTEXT (chain);
25105 }
25106 if (attr != NULL
25107 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
25108 == (int)BLOCK_NUMBER (superblock))
25109 && BLOCK_FRAGMENT_CHAIN (superblock))
25110 {
25111 unsigned long off = attr->dw_attr_val.v.val_offset;
25112 unsigned long supercnt = 0, thiscnt = 0;
25113 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
25114 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25115 {
25116 ++supercnt;
25117 gcc_checking_assert ((*ranges_table)[off + supercnt].num
25118 == (int)BLOCK_NUMBER (chain));
25119 }
25120 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
25121 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
25122 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
25123 ++thiscnt;
25124 gcc_assert (supercnt >= thiscnt);
25125 add_AT_range_list (die, attr_kind: DW_AT_ranges, offset: off + supercnt - thiscnt,
25126 force_direct: false);
25127 note_rnglist_head (offset: off + supercnt - thiscnt);
25128 return;
25129 }
25130
25131 unsigned int offset = add_ranges (block: stmt, maybe_new_sec: true);
25132 add_AT_range_list (die, attr_kind: DW_AT_ranges, offset, force_direct: false);
25133 note_rnglist_head (offset);
25134
25135 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25136 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25137 do
25138 {
25139 add_ranges (block: chain, maybe_new_sec: prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25140 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25141 chain = BLOCK_FRAGMENT_CHAIN (chain);
25142 }
25143 while (chain);
25144 add_ranges (NULL);
25145 }
25146 else
25147 {
25148 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25149 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25150 BLOCK_NUMBER (stmt));
25151 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25152 BLOCK_NUMBER (stmt));
25153 add_AT_low_high_pc (die, lbl_low: label, lbl_high: label_high, force_direct: false);
25154 }
25155}
25156
25157/* Generate a DIE for a lexical block. */
25158
25159static void
25160gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25161{
25162 dw_die_ref old_die = lookup_block_die (block: stmt);
25163 dw_die_ref stmt_die = NULL;
25164 if (!old_die)
25165 {
25166 stmt_die = new_die (tag_value: DW_TAG_lexical_block, parent_die: context_die, t: stmt);
25167 equate_block_to_die (block: stmt, die: stmt_die);
25168 }
25169
25170 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25171 {
25172 /* If this is an inlined or conrecte instance, create a new lexical
25173 die for anything below to attach DW_AT_abstract_origin to. */
25174 if (old_die)
25175 stmt_die = new_die (tag_value: DW_TAG_lexical_block, parent_die: context_die, t: stmt);
25176
25177 tree origin = block_ultimate_origin (stmt);
25178 if (origin != NULL_TREE && (origin != stmt || old_die))
25179 add_abstract_origin_attribute (die: stmt_die, origin);
25180
25181 old_die = NULL;
25182 }
25183
25184 if (old_die)
25185 stmt_die = old_die;
25186
25187 /* A non abstract block whose blocks have already been reordered
25188 should have the instruction range for this block. If so, set the
25189 high/low attributes. */
25190 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25191 {
25192 gcc_assert (stmt_die);
25193 add_high_low_attributes (stmt, die: stmt_die);
25194 }
25195
25196 decls_for_scope (stmt, stmt_die);
25197}
25198
25199/* Generate a DIE for an inlined subprogram. */
25200
25201static void
25202gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25203{
25204 tree decl = block_ultimate_origin (stmt);
25205
25206 /* Make sure any inlined functions are known to be inlineable. */
25207 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25208 || cgraph_function_possibly_inlined_p (decl));
25209
25210 dw_die_ref subr_die = new_die (tag_value: DW_TAG_inlined_subroutine, parent_die: context_die, t: stmt);
25211
25212 if (call_arg_locations || debug_inline_points)
25213 equate_block_to_die (block: stmt, die: subr_die);
25214 add_abstract_origin_attribute (die: subr_die, origin: decl);
25215 if (TREE_ASM_WRITTEN (stmt))
25216 add_high_low_attributes (stmt, die: subr_die);
25217 add_call_src_coords_attributes (stmt, die: subr_die);
25218
25219 /* The inliner creates an extra BLOCK for the parameter setup,
25220 we want to merge that with the actual outermost BLOCK of the
25221 inlined function to avoid duplicate locals in consumers.
25222 Do that by doing the recursion to subblocks on the single subblock
25223 of STMT. */
25224 bool unwrap_one = false;
25225 tree sub = BLOCK_SUBBLOCKS (stmt);
25226 if (sub)
25227 {
25228 tree origin = block_ultimate_origin (sub);
25229 if (origin
25230 && TREE_CODE (origin) == BLOCK
25231 && BLOCK_SUPERCONTEXT (origin) == decl)
25232 unwrap_one = true;
25233 for (tree next = BLOCK_CHAIN (sub); unwrap_one && next;
25234 next = BLOCK_CHAIN (next))
25235 if (BLOCK_FRAGMENT_ORIGIN (next) != sub)
25236 unwrap_one = false;
25237 }
25238 decls_for_scope (stmt, subr_die, !unwrap_one);
25239 if (unwrap_one)
25240 {
25241 decls_for_scope (sub, subr_die);
25242 for (sub = BLOCK_CHAIN (sub); sub; sub = BLOCK_CHAIN (sub))
25243 gen_block_die (sub, subr_die);
25244 }
25245}
25246
25247/* Generate a DIE for a field in a record, or structure. CTX is required: see
25248 the comment for VLR_CONTEXT. */
25249
25250static void
25251gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25252{
25253 dw_die_ref decl_die;
25254
25255 if (TREE_TYPE (decl) == error_mark_node)
25256 return;
25257
25258 decl_die = new_die (tag_value: DW_TAG_member, parent_die: context_die, t: decl);
25259 add_name_and_src_coords_attributes (die: decl_die, decl);
25260 add_type_attribute (object_die: decl_die, type: member_declared_type (member: decl), cv_quals: decl_quals (decl),
25261 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25262 context_die);
25263
25264 if (DECL_BIT_FIELD_TYPE (decl))
25265 {
25266 add_byte_size_attribute (die: decl_die, tree_node: decl);
25267 add_bit_size_attribute (die: decl_die, decl);
25268 add_bit_offset_attribute (die: decl_die, decl);
25269 }
25270
25271 add_alignment_attribute (die: decl_die, tree_node: decl);
25272
25273 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25274 add_data_member_location_attribute (die: decl_die, decl, ctx);
25275
25276 if (DECL_ARTIFICIAL (decl))
25277 add_AT_flag (die: decl_die, attr_kind: DW_AT_artificial, flag: 1);
25278
25279 add_accessibility_attribute (die: decl_die, decl);
25280
25281 /* Add DW_AT_export_symbols to anonymous unions or structs. */
25282 if ((dwarf_version >= 5 || !dwarf_strict) && DECL_NAME (decl) == NULL_TREE)
25283 if (tree type = member_declared_type (member: decl))
25284 if (lang_hooks.types.type_dwarf_attribute (TYPE_MAIN_VARIANT (type),
25285 DW_AT_export_symbols) != -1)
25286 {
25287 dw_die_ref type_die = lookup_type_die (TYPE_MAIN_VARIANT (type));
25288 if (type_die && get_AT (die: type_die, attr_kind: DW_AT_export_symbols) == NULL)
25289 add_AT_flag (die: type_die, attr_kind: DW_AT_export_symbols, flag: 1);
25290 }
25291
25292 /* Equate decl number to die, so that we can look up this decl later on. */
25293 equate_decl_number_to_die (decl, decl_die);
25294}
25295
25296/* Generate a DIE for a pointer to a member type. TYPE can be an
25297 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25298 pointer to member function. */
25299
25300static void
25301gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25302{
25303 if (lookup_type_die (type))
25304 return;
25305
25306 dw_die_ref ptr_die = new_die (tag_value: DW_TAG_ptr_to_member_type,
25307 parent_die: scope_die_for (t: type, context_die), t: type);
25308
25309 equate_type_number_to_die (type, type_die: ptr_die);
25310 add_AT_die_ref (die: ptr_die, attr_kind: DW_AT_containing_type,
25311 targ_die: lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25312 add_type_attribute (object_die: ptr_die, TREE_TYPE (type), cv_quals: TYPE_UNQUALIFIED, reverse: false,
25313 context_die);
25314 add_alignment_attribute (die: ptr_die, tree_node: type);
25315
25316 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25317 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25318 {
25319 dw_loc_descr_ref op = new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0);
25320 add_AT_loc (die: ptr_die, attr_kind: DW_AT_use_location, loc: op);
25321 }
25322}
25323
25324static char *producer_string;
25325
25326/* Given a C and/or C++ language/version string return the "highest".
25327 C++ is assumed to be "higher" than C in this case. Used for merging
25328 LTO translation unit languages. */
25329static const char *
25330highest_c_language (const char *lang1, const char *lang2)
25331{
25332 if (strcmp (s1: "GNU C++26", s2: lang1) == 0 || strcmp (s1: "GNU C++26", s2: lang2) == 0)
25333 return "GNU C++26";
25334 if (strcmp (s1: "GNU C++23", s2: lang1) == 0 || strcmp (s1: "GNU C++23", s2: lang2) == 0)
25335 return "GNU C++23";
25336 if (strcmp (s1: "GNU C++20", s2: lang1) == 0 || strcmp (s1: "GNU C++20", s2: lang2) == 0)
25337 return "GNU C++20";
25338 if (strcmp (s1: "GNU C++17", s2: lang1) == 0 || strcmp (s1: "GNU C++17", s2: lang2) == 0)
25339 return "GNU C++17";
25340 if (strcmp (s1: "GNU C++14", s2: lang1) == 0 || strcmp (s1: "GNU C++14", s2: lang2) == 0)
25341 return "GNU C++14";
25342 if (strcmp (s1: "GNU C++11", s2: lang1) == 0 || strcmp (s1: "GNU C++11", s2: lang2) == 0)
25343 return "GNU C++11";
25344 if (strcmp (s1: "GNU C++98", s2: lang1) == 0 || strcmp (s1: "GNU C++98", s2: lang2) == 0)
25345 return "GNU C++98";
25346
25347 if (strcmp (s1: "GNU C2Y", s2: lang1) == 0 || strcmp (s1: "GNU C2Y", s2: lang2) == 0)
25348 return "GNU C2Y";
25349 if (strcmp (s1: "GNU C23", s2: lang1) == 0 || strcmp (s1: "GNU C23", s2: lang2) == 0)
25350 return "GNU C23";
25351 if (strcmp (s1: "GNU C17", s2: lang1) == 0 || strcmp (s1: "GNU C17", s2: lang2) == 0)
25352 return "GNU C17";
25353 if (strcmp (s1: "GNU C11", s2: lang1) == 0 || strcmp (s1: "GNU C11", s2: lang2) == 0)
25354 return "GNU C11";
25355 if (strcmp (s1: "GNU C99", s2: lang1) == 0 || strcmp (s1: "GNU C99", s2: lang2) == 0)
25356 return "GNU C99";
25357 if (strcmp (s1: "GNU C89", s2: lang1) == 0 || strcmp (s1: "GNU C89", s2: lang2) == 0)
25358 return "GNU C89";
25359
25360 gcc_unreachable ();
25361}
25362
25363
25364/* Generate the DIE for the compilation unit. */
25365
25366static dw_die_ref
25367gen_compile_unit_die (const char *filename)
25368{
25369 dw_die_ref die;
25370 const char *language_string = lang_hooks.name;
25371 int language, lname, lversion;
25372
25373 die = new_die (tag_value: DW_TAG_compile_unit, NULL, NULL);
25374
25375 if (filename)
25376 {
25377 add_filename_attribute (die, name_string: filename);
25378 /* Don't add cwd for <built-in>. */
25379 if (filename[0] != '<')
25380 add_comp_dir_attribute (die);
25381 }
25382
25383 add_AT_string (die, attr_kind: DW_AT_producer, str: producer_string ? producer_string : "");
25384
25385 /* If our producer is LTO try to figure out a common language to use
25386 from the global list of translation units. */
25387 if (strcmp (s1: language_string, s2: "GNU GIMPLE") == 0)
25388 {
25389 unsigned i;
25390 tree t;
25391 const char *common_lang = NULL;
25392
25393 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25394 {
25395 if (!TRANSLATION_UNIT_LANGUAGE (t))
25396 continue;
25397 if (!common_lang)
25398 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25399 else if (strcmp (s1: common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25400 ;
25401 else if (startswith (str: common_lang, prefix: "GNU C")
25402 && startswith (TRANSLATION_UNIT_LANGUAGE (t), prefix: "GNU C"))
25403 /* Mixing C and C++ is ok, use C++ in that case. */
25404 common_lang = highest_c_language (lang1: common_lang,
25405 TRANSLATION_UNIT_LANGUAGE (t));
25406 else
25407 {
25408 /* Fall back to C. */
25409 common_lang = NULL;
25410 break;
25411 }
25412 }
25413
25414 if (common_lang)
25415 language_string = common_lang;
25416 }
25417
25418 language = DW_LANG_C;
25419 lname = 0;
25420 lversion = 0;
25421 if (startswith (str: language_string, prefix: "GNU C")
25422 && ISDIGIT (language_string[5]))
25423 {
25424 language = DW_LANG_C89;
25425 if (dwarf_version >= 3 || !dwarf_strict)
25426 {
25427 if (strcmp (s1: language_string, s2: "GNU C89") != 0)
25428 language = DW_LANG_C99;
25429
25430 if (dwarf_version >= 5 /* || !dwarf_strict */)
25431 {
25432 if (strcmp (s1: language_string, s2: "GNU C11") == 0)
25433 language = DW_LANG_C11;
25434 else if (strcmp (s1: language_string, s2: "GNU C17") == 0)
25435 {
25436 language = DW_LANG_C11;
25437 lname = DW_LNAME_C;
25438 lversion = 201710;
25439 }
25440 else if (strcmp (s1: language_string, s2: "GNU C23") == 0)
25441 {
25442 language = DW_LANG_C11;
25443 lname = DW_LNAME_C;
25444 lversion = 202311;
25445 }
25446 else if (strcmp (s1: language_string, s2: "GNU C2Y") == 0)
25447 {
25448 language = DW_LANG_C11;
25449 lname = DW_LNAME_C;
25450 lversion = 202500;
25451 }
25452 }
25453 }
25454 }
25455 else if (startswith (str: language_string, prefix: "GNU C++"))
25456 {
25457 language = DW_LANG_C_plus_plus;
25458 if (dwarf_version >= 5 /* || !dwarf_strict */)
25459 {
25460 if (strcmp (s1: language_string, s2: "GNU C++11") == 0)
25461 language = DW_LANG_C_plus_plus_11;
25462 else if (strcmp (s1: language_string, s2: "GNU C++14") == 0)
25463 language = DW_LANG_C_plus_plus_14;
25464 else if (strcmp (s1: language_string, s2: "GNU C++17") == 0)
25465 {
25466 language = DW_LANG_C_plus_plus_14;
25467 lname = DW_LNAME_C_plus_plus;
25468 lversion = 201703;
25469 }
25470 else if (strcmp (s1: language_string, s2: "GNU C++20") == 0)
25471 {
25472 language = DW_LANG_C_plus_plus_14;
25473 lname = DW_LNAME_C_plus_plus;
25474 lversion = 202002;
25475 }
25476 else if (strcmp (s1: language_string, s2: "GNU C++23") == 0)
25477 {
25478 language = DW_LANG_C_plus_plus_14;
25479 lname = DW_LNAME_C_plus_plus;
25480 lversion = 202302;
25481 }
25482 else if (strcmp (s1: language_string, s2: "GNU C++26") == 0)
25483 {
25484 language = DW_LANG_C_plus_plus_14;
25485 lname = DW_LNAME_C_plus_plus;
25486 lversion = 202400;
25487 }
25488 }
25489 }
25490 else if (strcmp (s1: language_string, s2: "GNU F77") == 0)
25491 language = DW_LANG_Fortran77;
25492 else if (strcmp (s1: language_string, s2: "GCC COBOL") == 0)
25493 language = DW_LANG_Cobol85;
25494 else if (strcmp (s1: language_string, s2: "GNU Modula-2") == 0)
25495 language = DW_LANG_Modula2;
25496 else if (dwarf_version >= 3 || !dwarf_strict)
25497 {
25498 if (strcmp (s1: language_string, s2: "GNU Ada") == 0)
25499 language = DW_LANG_Ada95;
25500 else if (startswith (str: language_string, prefix: "GNU Fortran"))
25501 {
25502 language = DW_LANG_Fortran95;
25503 if (dwarf_version >= 5 /* || !dwarf_strict */)
25504 {
25505 if (strcmp (s1: language_string, s2: "GNU Fortran2003") == 0)
25506 language = DW_LANG_Fortran03;
25507 else if (strcmp (s1: language_string, s2: "GNU Fortran2008") == 0)
25508 language = DW_LANG_Fortran08;
25509 }
25510 }
25511 else if (strcmp (s1: language_string, s2: "GNU Objective-C") == 0)
25512 language = DW_LANG_ObjC;
25513 else if (strcmp (s1: language_string, s2: "GNU Objective-C++") == 0)
25514 language = DW_LANG_ObjC_plus_plus;
25515 else if (strcmp (s1: language_string, s2: "GNU D") == 0)
25516 language = DW_LANG_D;
25517 else if (dwarf_version >= 5 || !dwarf_strict)
25518 {
25519 if (strcmp (s1: language_string, s2: "GNU Go") == 0)
25520 language = DW_LANG_Go;
25521 else if (strcmp (s1: language_string, s2: "GNU Rust") == 0)
25522 language = DW_LANG_Rust;
25523 }
25524 }
25525 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25526 else if (startswith (str: language_string, prefix: "GNU Fortran"))
25527 language = DW_LANG_Fortran90;
25528 /* Likewise for Ada. */
25529 else if (strcmp (s1: language_string, s2: "GNU Ada") == 0)
25530 language = DW_LANG_Ada83;
25531
25532 add_AT_unsigned (die, attr_kind: DW_AT_language, unsigned_val: language);
25533 if (lname && dwarf_version >= 5 && !dwarf_strict)
25534 {
25535 add_AT_unsigned (die, attr_kind: DW_AT_language_name, unsigned_val: lname);
25536 add_AT_unsigned (die, attr_kind: DW_AT_language_version, unsigned_val: lversion);
25537 }
25538
25539 switch (language)
25540 {
25541 case DW_LANG_Fortran77:
25542 case DW_LANG_Fortran90:
25543 case DW_LANG_Fortran95:
25544 case DW_LANG_Fortran03:
25545 case DW_LANG_Fortran08:
25546 /* Fortran has case insensitive identifiers and the front-end
25547 lowercases everything. */
25548 add_AT_unsigned (die, attr_kind: DW_AT_identifier_case, unsigned_val: DW_ID_down_case);
25549 break;
25550 case DW_LANG_Cobol85:
25551 add_AT_unsigned (die, attr_kind: DW_AT_identifier_case, unsigned_val: DW_ID_case_insensitive);
25552 break;
25553 default:
25554 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25555 break;
25556 }
25557 return die;
25558}
25559
25560/* Generate the DIE for a base class. */
25561
25562static void
25563gen_inheritance_die (tree binfo, tree access, tree type,
25564 dw_die_ref context_die)
25565{
25566 dw_die_ref die = new_die (tag_value: DW_TAG_inheritance, parent_die: context_die, t: binfo);
25567 struct vlr_context ctx = { .struct_type: type, NULL };
25568
25569 add_type_attribute (object_die: die, BINFO_TYPE (binfo), cv_quals: TYPE_UNQUALIFIED, reverse: false,
25570 context_die);
25571 add_data_member_location_attribute (die, decl: binfo, ctx: &ctx);
25572
25573 if (BINFO_VIRTUAL_P (binfo))
25574 add_AT_unsigned (die, attr_kind: DW_AT_virtuality, unsigned_val: DW_VIRTUALITY_virtual);
25575
25576 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25577 children, otherwise the default is DW_ACCESS_public. In DWARF2
25578 the default has always been DW_ACCESS_private. */
25579 if (access == access_public_node)
25580 {
25581 if (dwarf_version == 2
25582 || context_die->die_tag == DW_TAG_class_type)
25583 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_public);
25584 }
25585 else if (access == access_protected_node)
25586 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_protected);
25587 else if (dwarf_version > 2
25588 && context_die->die_tag != DW_TAG_class_type)
25589 add_AT_unsigned (die, attr_kind: DW_AT_accessibility, unsigned_val: DW_ACCESS_private);
25590}
25591
25592/* Return whether DECL is a FIELD_DECL that represents the variant part of a
25593 structure. */
25594
25595static bool
25596is_variant_part (tree decl)
25597{
25598 return (TREE_CODE (decl) == FIELD_DECL
25599 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25600}
25601
25602/* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25603 return the FIELD_DECL. Return NULL_TREE otherwise. */
25604
25605static tree
25606analyze_discr_in_predicate (tree operand, tree struct_type)
25607{
25608 while (CONVERT_EXPR_P (operand))
25609 operand = TREE_OPERAND (operand, 0);
25610
25611 /* Match field access to members of struct_type only. */
25612 if (TREE_CODE (operand) == COMPONENT_REF
25613 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25614 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25615 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25616 return TREE_OPERAND (operand, 1);
25617 else
25618 return NULL_TREE;
25619}
25620
25621/* Check that SRC is a constant integer that can be represented as a native
25622 integer constant (either signed or unsigned). If so, store it into DEST and
25623 return true. Return false otherwise. */
25624
25625static bool
25626get_discr_value (tree src, dw_discr_value *dest)
25627{
25628 tree discr_type = TREE_TYPE (src);
25629
25630 if (lang_hooks.types.get_debug_type)
25631 {
25632 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25633 if (debug_type != NULL)
25634 discr_type = debug_type;
25635 }
25636
25637 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25638 return false;
25639
25640 /* Signedness can vary between the original type and the debug type. This
25641 can happen for character types in Ada for instance: the character type
25642 used for code generation can be signed, to be compatible with the C one,
25643 but from a debugger point of view, it must be unsigned. */
25644 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25645 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25646
25647 if (is_orig_unsigned != is_debug_unsigned)
25648 src = fold_convert (discr_type, src);
25649
25650 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25651 return false;
25652
25653 dest->pos = is_debug_unsigned;
25654 if (is_debug_unsigned)
25655 dest->v.uval = tree_to_uhwi (src);
25656 else
25657 dest->v.sval = tree_to_shwi (src);
25658
25659 return true;
25660}
25661
25662/* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25663 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25664 store NULL_TREE in DISCR_DECL. Otherwise:
25665
25666 - store the discriminant field in STRUCT_TYPE that controls the variant
25667 part to *DISCR_DECL
25668
25669 - put in *DISCR_LISTS_P an array where for each variant, the item
25670 represents the corresponding matching list of discriminant values.
25671
25672 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25673 the above array.
25674
25675 Note that when the array is allocated (i.e. when the analysis is
25676 successful), it is up to the caller to free the array. */
25677
25678static void
25679analyze_variants_discr (tree variant_part_decl,
25680 tree struct_type,
25681 tree *discr_decl,
25682 dw_discr_list_ref **discr_lists_p,
25683 unsigned *discr_lists_length)
25684{
25685 tree variant_part_type = TREE_TYPE (variant_part_decl);
25686 tree variant;
25687 dw_discr_list_ref *discr_lists;
25688 unsigned i;
25689
25690 /* Compute how many variants there are in this variant part. */
25691 *discr_lists_length = 0;
25692 for (variant = TYPE_FIELDS (variant_part_type);
25693 variant != NULL_TREE;
25694 variant = DECL_CHAIN (variant))
25695 ++*discr_lists_length;
25696
25697 *discr_decl = NULL_TREE;
25698 *discr_lists_p
25699 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25700 sizeof (**discr_lists_p));
25701 discr_lists = *discr_lists_p;
25702
25703 /* And then analyze all variants to extract discriminant information for all
25704 of them. This analysis is conservative: as soon as we detect something we
25705 do not support, abort everything and pretend we found nothing. */
25706 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25707 variant != NULL_TREE;
25708 variant = DECL_CHAIN (variant), ++i)
25709 {
25710 tree match_expr = DECL_QUALIFIER (variant);
25711
25712 /* Now, try to analyze the predicate and deduce a discriminant for
25713 it. */
25714 if (match_expr == boolean_true_node)
25715 /* Typically happens for the default variant: it matches all cases that
25716 previous variants rejected. Don't output any matching value for
25717 this one. */
25718 continue;
25719
25720 /* The following loop tries to iterate over each discriminant
25721 possibility: single values or ranges. */
25722 while (match_expr != NULL_TREE)
25723 {
25724 tree next_round_match_expr;
25725 tree candidate_discr = NULL_TREE;
25726 dw_discr_list_ref new_node = NULL;
25727
25728 /* Possibilities are matched one after the other by nested
25729 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25730 continue with the rest at next iteration. */
25731 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25732 {
25733 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25734 match_expr = TREE_OPERAND (match_expr, 1);
25735 }
25736 else
25737 next_round_match_expr = NULL_TREE;
25738
25739 if (match_expr == boolean_false_node)
25740 /* This sub-expression matches nothing: just wait for the next
25741 one. */
25742 ;
25743
25744 else if (TREE_CODE (match_expr) == EQ_EXPR)
25745 {
25746 /* We are matching: <discr_field> == <integer_cst>
25747 This sub-expression matches a single value. */
25748 tree integer_cst = TREE_OPERAND (match_expr, 1);
25749
25750 candidate_discr
25751 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25752 struct_type);
25753
25754 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25755 if (!get_discr_value (src: integer_cst,
25756 dest: &new_node->dw_discr_lower_bound))
25757 goto abort;
25758 new_node->dw_discr_range = false;
25759 }
25760
25761 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25762 {
25763 /* We are matching:
25764 <discr_field> > <integer_cst>
25765 && <discr_field> < <integer_cst>.
25766 This sub-expression matches the range of values between the
25767 two matched integer constants. Note that comparisons can be
25768 inclusive or exclusive. */
25769 tree candidate_discr_1, candidate_discr_2;
25770 tree lower_cst, upper_cst;
25771 bool lower_cst_included, upper_cst_included;
25772 tree lower_op = TREE_OPERAND (match_expr, 0);
25773 tree upper_op = TREE_OPERAND (match_expr, 1);
25774
25775 /* When the comparison is exclusive, the integer constant is not
25776 the discriminant range bound we are looking for: we will have
25777 to increment or decrement it. */
25778 if (TREE_CODE (lower_op) == GE_EXPR)
25779 lower_cst_included = true;
25780 else if (TREE_CODE (lower_op) == GT_EXPR)
25781 lower_cst_included = false;
25782 else
25783 goto abort;
25784
25785 if (TREE_CODE (upper_op) == LE_EXPR)
25786 upper_cst_included = true;
25787 else if (TREE_CODE (upper_op) == LT_EXPR)
25788 upper_cst_included = false;
25789 else
25790 goto abort;
25791
25792 /* Extract the discriminant from the first operand and check it
25793 is consistant with the same analysis in the second
25794 operand. */
25795 candidate_discr_1
25796 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25797 struct_type);
25798 candidate_discr_2
25799 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25800 struct_type);
25801 if (candidate_discr_1 == candidate_discr_2)
25802 candidate_discr = candidate_discr_1;
25803 else
25804 goto abort;
25805
25806 /* Extract bounds from both. */
25807 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25808 lower_cst = TREE_OPERAND (lower_op, 1);
25809 upper_cst = TREE_OPERAND (upper_op, 1);
25810
25811 if (!lower_cst_included)
25812 lower_cst
25813 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25814 build_int_cst (TREE_TYPE (lower_cst), 1));
25815 if (!upper_cst_included)
25816 upper_cst
25817 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25818 build_int_cst (TREE_TYPE (upper_cst), 1));
25819
25820 if (!get_discr_value (src: lower_cst,
25821 dest: &new_node->dw_discr_lower_bound)
25822 || !get_discr_value (src: upper_cst,
25823 dest: &new_node->dw_discr_upper_bound))
25824 goto abort;
25825
25826 new_node->dw_discr_range = true;
25827 }
25828
25829 else if ((candidate_discr
25830 = analyze_discr_in_predicate (operand: match_expr, struct_type))
25831 && (TREE_TYPE (candidate_discr) == boolean_type_node
25832 || TREE_TYPE (TREE_TYPE (candidate_discr))
25833 == boolean_type_node))
25834 {
25835 /* We are matching: <discr_field> for a boolean discriminant.
25836 This sub-expression matches boolean_true_node. */
25837 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25838 if (!get_discr_value (boolean_true_node,
25839 dest: &new_node->dw_discr_lower_bound))
25840 goto abort;
25841 new_node->dw_discr_range = false;
25842 }
25843
25844 else
25845 /* Unsupported sub-expression: we cannot determine the set of
25846 matching discriminant values. Abort everything. */
25847 goto abort;
25848
25849 /* If the discriminant info is not consistant with what we saw so
25850 far, consider the analysis failed and abort everything. */
25851 if (candidate_discr == NULL_TREE
25852 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25853 goto abort;
25854 else
25855 *discr_decl = candidate_discr;
25856
25857 if (new_node != NULL)
25858 {
25859 new_node->dw_discr_next = discr_lists[i];
25860 discr_lists[i] = new_node;
25861 }
25862 match_expr = next_round_match_expr;
25863 }
25864 }
25865
25866 /* If we reach this point, we could match everything we were interested
25867 in. */
25868 return;
25869
25870abort:
25871 /* Clean all data structure and return no result. */
25872 free (ptr: *discr_lists_p);
25873 *discr_lists_p = NULL;
25874 *discr_decl = NULL_TREE;
25875}
25876
25877/* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25878 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25879 under CONTEXT_DIE.
25880
25881 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25882 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25883 this type, which are record types, represent the available variants and each
25884 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25885 values are inferred from these attributes.
25886
25887 In trees, the offsets for the fields inside these sub-records are relative
25888 to the variant part itself, whereas the corresponding DIEs should have
25889 offset attributes that are relative to the embedding record base address.
25890 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25891 must be an expression that computes the offset of the variant part to
25892 describe in DWARF. */
25893
25894static void
25895gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25896 dw_die_ref context_die)
25897{
25898 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25899 tree variant_part_offset = vlr_ctx->variant_part_offset;
25900
25901 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25902 NULL_TREE if there is no such field. */
25903 tree discr_decl = NULL_TREE;
25904 dw_discr_list_ref *discr_lists;
25905 unsigned discr_lists_length = 0;
25906 unsigned i;
25907
25908 dw_die_ref dwarf_proc_die = NULL;
25909 dw_die_ref variant_part_die
25910 = new_die (tag_value: DW_TAG_variant_part, parent_die: context_die, t: variant_part_type);
25911
25912 equate_decl_number_to_die (decl: variant_part_decl, decl_die: variant_part_die);
25913
25914 analyze_variants_discr (variant_part_decl, struct_type: vlr_ctx->struct_type,
25915 discr_decl: &discr_decl, discr_lists_p: &discr_lists, discr_lists_length: &discr_lists_length);
25916
25917 if (discr_decl != NULL_TREE)
25918 {
25919 dw_die_ref discr_die = lookup_decl_die (decl: discr_decl);
25920
25921 if (discr_die)
25922 add_AT_die_ref (die: variant_part_die, attr_kind: DW_AT_discr, targ_die: discr_die);
25923 else
25924 /* We have no DIE for the discriminant, so just discard all
25925 discrimimant information in the output. */
25926 discr_decl = NULL_TREE;
25927 }
25928
25929 /* If the offset for this variant part is more complex than a constant,
25930 create a DWARF procedure for it so that we will not have to generate
25931 DWARF expressions for it for each member. */
25932 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25933 && (dwarf_version >= 3 || !dwarf_strict))
25934 {
25935 struct loc_descr_context ctx = {
25936 .context_type: vlr_ctx->struct_type, /* context_type */
25937 NULL_TREE, /* base_decl */
25938 NULL, /* dpi */
25939 .placeholder_arg: false, /* placeholder_arg */
25940 .placeholder_seen: false, /* placeholder_seen */
25941 .strict_signedness: false /* strict_signedness */
25942 };
25943 const tree dwarf_proc_fndecl
25944 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25945 build_function_type (TREE_TYPE (variant_part_offset),
25946 NULL_TREE));
25947 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25948 const dw_loc_descr_ref dwarf_proc_body
25949 = loc_descriptor_from_tree (loc: variant_part_offset, want_address: 0, context: &ctx);
25950
25951 dwarf_proc_die = new_dwarf_proc_die (location: dwarf_proc_body,
25952 fndecl: dwarf_proc_fndecl, parent_die: context_die);
25953 if (dwarf_proc_die != NULL)
25954 variant_part_offset = dwarf_proc_call;
25955 }
25956
25957 /* Output DIEs for all variants. */
25958 i = 0;
25959 for (tree variant = TYPE_FIELDS (variant_part_type);
25960 variant != NULL_TREE;
25961 variant = DECL_CHAIN (variant), ++i)
25962 {
25963 tree variant_type = TREE_TYPE (variant);
25964 dw_die_ref variant_die;
25965
25966 /* All variants (i.e. members of a variant part) are supposed to be
25967 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25968 under these records. */
25969 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25970
25971 variant_die = new_die (tag_value: DW_TAG_variant, parent_die: variant_part_die, t: variant_type);
25972 equate_decl_number_to_die (decl: variant, decl_die: variant_die);
25973
25974 /* Output discriminant values this variant matches, if any. */
25975 if (discr_decl == NULL || discr_lists[i] == NULL)
25976 /* In the case we have discriminant information at all, this is
25977 probably the default variant: as the standard says, don't
25978 output any discriminant value/list attribute. */
25979 ;
25980 else if (discr_lists[i]->dw_discr_next == NULL
25981 && !discr_lists[i]->dw_discr_range)
25982 /* If there is only one accepted value, don't bother outputting a
25983 list. */
25984 add_discr_value (die: variant_die, value: &discr_lists[i]->dw_discr_lower_bound);
25985 else
25986 add_discr_list (die: variant_die, discr_list: discr_lists[i]);
25987
25988 for (tree member = TYPE_FIELDS (variant_type);
25989 member != NULL_TREE;
25990 member = DECL_CHAIN (member))
25991 {
25992 struct vlr_context vlr_sub_ctx = {
25993 .struct_type: vlr_ctx->struct_type, /* struct_type */
25994 NULL /* variant_part_offset */
25995 };
25996 if (is_variant_part (decl: member))
25997 {
25998 /* All offsets for fields inside variant parts are relative to
25999 the top-level embedding RECORD_TYPE's base address. On the
26000 other hand, offsets in GCC's types are relative to the
26001 nested-most variant part. So we have to sum offsets each time
26002 we recurse. */
26003
26004 vlr_sub_ctx.variant_part_offset
26005 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
26006 variant_part_offset, byte_position (member));
26007 gen_variant_part (variant_part_decl: member, vlr_ctx: &vlr_sub_ctx, context_die: variant_die);
26008 }
26009 else
26010 {
26011 vlr_sub_ctx.variant_part_offset = variant_part_offset;
26012 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
26013 }
26014 }
26015 }
26016
26017 free (ptr: discr_lists);
26018}
26019
26020/* Generate a DIE for a class member. */
26021
26022static void
26023gen_member_die (tree type, dw_die_ref context_die)
26024{
26025 tree member;
26026 tree binfo = TYPE_BINFO (type);
26027
26028 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
26029
26030 /* If this is not an incomplete type, output descriptions of each of its
26031 members. Note that as we output the DIEs necessary to represent the
26032 members of this record or union type, we will also be trying to output
26033 DIEs to represent the *types* of those members. However the `type'
26034 function (above) will specifically avoid generating type DIEs for member
26035 types *within* the list of member DIEs for this (containing) type except
26036 for those types (of members) which are explicitly marked as also being
26037 members of this (containing) type themselves. The g++ front- end can
26038 force any given type to be treated as a member of some other (containing)
26039 type by setting the TYPE_CONTEXT of the given (member) type to point to
26040 the TREE node representing the appropriate (containing) type. */
26041
26042 /* First output info about the base classes. */
26043 if (binfo && early_dwarf)
26044 {
26045 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
26046 int i;
26047 tree base;
26048
26049 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
26050 gen_inheritance_die (binfo: base,
26051 access: (accesses ? (*accesses)[i] : access_public_node),
26052 type,
26053 context_die);
26054 }
26055
26056 /* Now output info about the members. */
26057 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
26058 {
26059 /* Ignore clones. */
26060 if (DECL_ABSTRACT_ORIGIN (member))
26061 continue;
26062
26063 struct vlr_context vlr_ctx = { .struct_type: type, NULL_TREE };
26064 bool static_inline_p
26065 = (VAR_P (member)
26066 && TREE_STATIC (member)
26067 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
26068 != -1));
26069
26070 /* If we thought we were generating minimal debug info for TYPE
26071 and then changed our minds, some of the member declarations
26072 may have already been defined. Don't define them again, but
26073 do put them in the right order. */
26074
26075 if (dw_die_ref child = lookup_decl_die (decl: member))
26076 {
26077 /* Handle inline static data members, which only have in-class
26078 declarations. */
26079 bool splice = true;
26080
26081 dw_die_ref ref = NULL;
26082 if (child->die_tag == DW_TAG_variable
26083 && child->die_parent == comp_unit_die ())
26084 {
26085 ref = get_AT_ref (die: child, attr_kind: DW_AT_specification);
26086
26087 /* For C++17 inline static data members followed by redundant
26088 out of class redeclaration, we might get here with
26089 child being the DIE created for the out of class
26090 redeclaration and with its DW_AT_specification being
26091 the DIE created for in-class definition. We want to
26092 reparent the latter, and don't want to create another
26093 DIE with DW_AT_specification in that case, because
26094 we already have one. */
26095 if (ref
26096 && static_inline_p
26097 && ref->die_tag == DW_TAG_variable
26098 && ref->die_parent == comp_unit_die ()
26099 && get_AT (die: ref, attr_kind: DW_AT_specification) == NULL)
26100 {
26101 child = ref;
26102 ref = NULL;
26103 static_inline_p = false;
26104 }
26105
26106 if (!ref)
26107 {
26108 reparent_child (child, new_parent: context_die);
26109 if (dwarf_version < 5)
26110 child->die_tag = DW_TAG_member;
26111 splice = false;
26112 }
26113 }
26114 else if (child->die_tag == DW_TAG_enumerator)
26115 /* Enumerators remain under their enumeration even if
26116 their names are introduced in the enclosing scope. */
26117 splice = false;
26118
26119 if (splice)
26120 splice_child_die (parent: context_die, child);
26121 }
26122
26123 /* Do not generate DWARF for variant parts if we are generating the
26124 corresponding GNAT encodings: DIEs generated for the two schemes
26125 would conflict in our mappings. */
26126 else if (is_variant_part (decl: member)
26127 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
26128 {
26129 vlr_ctx.variant_part_offset = byte_position (member);
26130 gen_variant_part (variant_part_decl: member, vlr_ctx: &vlr_ctx, context_die);
26131 }
26132 else
26133 {
26134 vlr_ctx.variant_part_offset = NULL_TREE;
26135 gen_decl_die (member, NULL, &vlr_ctx, context_die);
26136 }
26137
26138 /* For C++ inline static data members emit immediately a DW_TAG_variable
26139 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
26140 DW_AT_specification. */
26141 if (static_inline_p)
26142 {
26143 int old_extern = DECL_EXTERNAL (member);
26144 DECL_EXTERNAL (member) = 0;
26145 gen_decl_die (member, NULL, NULL, comp_unit_die ());
26146 DECL_EXTERNAL (member) = old_extern;
26147 }
26148 }
26149}
26150
26151/* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
26152 is set, we pretend that the type was never defined, so we only get the
26153 member DIEs needed by later specification DIEs. */
26154
26155static void
26156gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
26157 enum debug_info_usage usage)
26158{
26159 if (TREE_ASM_WRITTEN (type))
26160 {
26161 /* Fill in the bound of variable-length fields in late dwarf if
26162 still incomplete. */
26163 if (!early_dwarf && variably_modified_type_p (type, NULL))
26164 for (tree member = TYPE_FIELDS (type);
26165 member;
26166 member = DECL_CHAIN (member))
26167 fill_variable_array_bounds (TREE_TYPE (member));
26168 return;
26169 }
26170
26171 dw_die_ref type_die = lookup_type_die (type);
26172 dw_die_ref scope_die = 0;
26173 bool nested = false;
26174 bool complete = (TYPE_SIZE (type)
26175 && (! TYPE_STUB_DECL (type)
26176 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
26177 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
26178 complete = complete && should_emit_struct_debug (type, usage);
26179
26180 if (type_die && ! complete)
26181 return;
26182
26183 if (TYPE_CONTEXT (type) != NULL_TREE
26184 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26185 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
26186 nested = true;
26187
26188 scope_die = scope_die_for (t: type, context_die);
26189
26190 /* Generate child dies for template parameters. */
26191 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
26192 schedule_generic_params_dies_gen (t: type);
26193
26194 if (! type_die || (nested && is_cu_die (c: scope_die)))
26195 /* First occurrence of type or toplevel definition of nested class. */
26196 {
26197 dw_die_ref old_die = type_die;
26198
26199 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26200 ? record_type_tag (type) : DW_TAG_union_type,
26201 parent_die: scope_die, t: type);
26202 equate_type_number_to_die (type, type_die);
26203 if (old_die)
26204 add_AT_specification (die: type_die, targ_die: old_die);
26205 else
26206 add_name_attribute (die: type_die, name_string: type_tag (type));
26207 }
26208 else
26209 remove_AT (die: type_die, attr_kind: DW_AT_declaration);
26210
26211 /* If this type has been completed, then give it a byte_size attribute and
26212 then give a list of members. */
26213 if (complete && !ns_decl)
26214 {
26215 /* Prevent infinite recursion in cases where the type of some member of
26216 this type is expressed in terms of this type itself. */
26217 TREE_ASM_WRITTEN (type) = 1;
26218 add_byte_size_attribute (die: type_die, tree_node: type);
26219 add_alignment_attribute (die: type_die, tree_node: type);
26220 if (TYPE_STUB_DECL (type) != NULL_TREE)
26221 {
26222 add_src_coords_attributes (die: type_die, TYPE_STUB_DECL (type));
26223 add_accessibility_attribute (die: type_die, TYPE_STUB_DECL (type));
26224 }
26225
26226 /* If the first reference to this type was as the return type of an
26227 inline function, then it may not have a parent. Fix this now. */
26228 if (type_die->die_parent == NULL)
26229 add_child_die (die: scope_die, child_die: type_die);
26230
26231 gen_member_die (type, context_die: type_die);
26232
26233 add_gnat_descriptive_type_attribute (die: type_die, type, context_die);
26234 if (TYPE_ARTIFICIAL (type))
26235 add_AT_flag (die: type_die, attr_kind: DW_AT_artificial, flag: 1);
26236
26237 /* GNU extension: Record what type our vtable lives in. */
26238 if (TYPE_VFIELD (type))
26239 {
26240 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26241
26242 gen_type_die (vtype, context_die);
26243 add_AT_die_ref (die: type_die, attr_kind: DW_AT_containing_type,
26244 targ_die: lookup_type_die (type: vtype));
26245 }
26246 }
26247 else
26248 {
26249 add_AT_flag (die: type_die, attr_kind: DW_AT_declaration, flag: 1);
26250
26251 /* We don't need to do this for function-local types. */
26252 if (TYPE_STUB_DECL (type)
26253 && ! decl_function_context (TYPE_STUB_DECL (type)))
26254 vec_safe_push (v&: incomplete_types, obj: type);
26255 }
26256
26257 if (get_AT (die: type_die, attr_kind: DW_AT_name))
26258 add_pubtype (decl: type, die: type_die);
26259}
26260
26261/* Generate a DIE for a subroutine _type_. */
26262
26263static void
26264gen_subroutine_type_die (tree type, dw_die_ref context_die)
26265{
26266 tree return_type = TREE_TYPE (type);
26267 dw_die_ref subr_die
26268 = new_die (tag_value: DW_TAG_subroutine_type,
26269 parent_die: scope_die_for (t: type, context_die), t: type);
26270
26271 equate_type_number_to_die (type, type_die: subr_die);
26272 add_prototyped_attribute (die: subr_die, func_type: type);
26273 add_type_attribute (object_die: subr_die, type: return_type, cv_quals: TYPE_UNQUALIFIED, reverse: false,
26274 context_die);
26275 add_alignment_attribute (die: subr_die, tree_node: type);
26276 gen_formal_types_die (function_or_method_type: type, context_die: subr_die);
26277
26278 if (get_AT (die: subr_die, attr_kind: DW_AT_name))
26279 add_pubtype (decl: type, die: subr_die);
26280 if ((dwarf_version >= 5 || !dwarf_strict)
26281 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26282 add_AT_flag (die: subr_die, attr_kind: DW_AT_reference, flag: 1);
26283 if ((dwarf_version >= 5 || !dwarf_strict)
26284 && lang_hooks.types.type_dwarf_attribute (type,
26285 DW_AT_rvalue_reference) != -1)
26286 add_AT_flag (die: subr_die, attr_kind: DW_AT_rvalue_reference, flag: 1);
26287}
26288
26289/* Generate a DIE for a type definition. */
26290
26291static void
26292gen_typedef_die (tree decl, dw_die_ref context_die)
26293{
26294 dw_die_ref type_die;
26295 tree type;
26296
26297 if (TREE_ASM_WRITTEN (decl))
26298 {
26299 if (DECL_ORIGINAL_TYPE (decl))
26300 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26301 return;
26302 }
26303
26304 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26305 checks in process_scope_var and modified_type_die), this should be called
26306 only for original types. */
26307 gcc_assert (decl_ultimate_origin (decl) == NULL
26308 || decl_ultimate_origin (decl) == decl);
26309
26310 TREE_ASM_WRITTEN (decl) = 1;
26311 type_die = new_die (tag_value: DW_TAG_typedef, parent_die: context_die, t: decl);
26312
26313 add_name_and_src_coords_attributes (die: type_die, decl);
26314 if (DECL_ORIGINAL_TYPE (decl))
26315 {
26316 type = DECL_ORIGINAL_TYPE (decl);
26317 if (type == error_mark_node)
26318 return;
26319
26320 gcc_assert (type != TREE_TYPE (decl));
26321 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26322 }
26323 else
26324 {
26325 type = TREE_TYPE (decl);
26326 if (type == error_mark_node)
26327 return;
26328
26329 if (is_naming_typedef_decl (TYPE_NAME (type)))
26330 {
26331 /* Here, we are in the case of decl being a typedef naming
26332 an anonymous type, e.g:
26333 typedef struct {...} foo;
26334 In that case TREE_TYPE (decl) is not a typedef variant
26335 type and TYPE_NAME of the anonymous type is set to the
26336 TYPE_DECL of the typedef. This construct is emitted by
26337 the C++ FE.
26338
26339 TYPE is the anonymous struct named by the typedef
26340 DECL. As we need the DW_AT_type attribute of the
26341 DW_TAG_typedef to point to the DIE of TYPE, let's
26342 generate that DIE right away. add_type_attribute
26343 called below will then pick (via lookup_type_die) that
26344 anonymous struct DIE. */
26345 if (!TREE_ASM_WRITTEN (type))
26346 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26347
26348 /* This is a GNU Extension. We are adding a
26349 DW_AT_linkage_name attribute to the DIE of the
26350 anonymous struct TYPE. The value of that attribute
26351 is the name of the typedef decl naming the anonymous
26352 struct. This greatly eases the work of consumers of
26353 this debug info. */
26354 add_linkage_name_raw (die: lookup_type_die (type), decl);
26355 }
26356 }
26357
26358 add_type_attribute (object_die: type_die, type, cv_quals: decl_quals (decl), reverse: false,
26359 context_die);
26360
26361 if (is_naming_typedef_decl (decl))
26362 /* We want that all subsequent calls to lookup_type_die with
26363 TYPE in argument yield the DW_TAG_typedef we have just
26364 created. */
26365 equate_type_number_to_die (type, type_die);
26366
26367 add_alignment_attribute (die: type_die, TREE_TYPE (decl));
26368
26369 add_accessibility_attribute (die: type_die, decl);
26370
26371 if (DECL_ABSTRACT_P (decl))
26372 equate_decl_number_to_die (decl, decl_die: type_die);
26373
26374 if (get_AT (die: type_die, attr_kind: DW_AT_name))
26375 add_pubtype (decl, die: type_die);
26376}
26377
26378/* Generate a DIE for a struct, class, enum or union type. */
26379
26380static void
26381gen_tagged_type_die (tree type,
26382 dw_die_ref context_die,
26383 enum debug_info_usage usage,
26384 bool reverse)
26385{
26386 if (type == NULL_TREE
26387 || !is_tagged_type (type))
26388 return;
26389
26390 if (TREE_ASM_WRITTEN (type))
26391 ;
26392 /* If this is a nested type whose containing class hasn't been written
26393 out yet, writing it out will cover this one, too. This does not apply
26394 to instantiations of member class templates; they need to be added to
26395 the containing class as they are generated. FIXME: This hurts the
26396 idea of combining type decls from multiple TUs, since we can't predict
26397 what set of template instantiations we'll get. */
26398 else if (TYPE_CONTEXT (type)
26399 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26400 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26401 {
26402 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26403
26404 if (TREE_ASM_WRITTEN (type))
26405 return;
26406
26407 /* If that failed, attach ourselves to the stub. */
26408 context_die = lookup_type_die (TYPE_CONTEXT (type));
26409 }
26410 else if (TYPE_CONTEXT (type) != NULL_TREE
26411 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26412 {
26413 /* If this type is local to a function that hasn't been written
26414 out yet, use a NULL context for now; it will be fixed up in
26415 decls_for_scope. */
26416 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26417 /* A declaration DIE doesn't count; nested types need to go in the
26418 specification. */
26419 if (context_die && is_declaration_die (die: context_die))
26420 context_die = NULL;
26421 }
26422 else
26423 context_die = declare_in_namespace (type, context_die);
26424
26425 if (TREE_CODE (type) == ENUMERAL_TYPE)
26426 {
26427 /* This might have been written out by the call to
26428 declare_in_namespace. */
26429 if (!TREE_ASM_WRITTEN (type) || reverse)
26430 gen_enumeration_type_die (type, context_die, reverse);
26431 }
26432 else
26433 gen_struct_or_union_type_die (type, context_die, usage);
26434
26435 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26436 it up if it is ever completed. gen_*_type_die will set it for us
26437 when appropriate. */
26438}
26439
26440/* Generate a type description DIE. */
26441
26442static void
26443gen_type_die_with_usage (tree type, dw_die_ref context_die,
26444 enum debug_info_usage usage, bool reverse)
26445{
26446 struct array_descr_info info;
26447
26448 if (type == NULL_TREE || type == error_mark_node)
26449 return;
26450
26451 if (flag_checking && type)
26452 verify_type (t: type);
26453
26454 if (TYPE_NAME (type) != NULL_TREE
26455 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26456 && is_redundant_typedef (TYPE_NAME (type))
26457 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26458 /* The DECL of this type is a typedef we don't want to emit debug
26459 info for but we want debug info for its underlying typedef.
26460 This can happen for e.g, the injected-class-name of a C++
26461 type. */
26462 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26463
26464 /* If TYPE is a typedef type variant, let's generate debug info
26465 for the parent typedef which TYPE is a type of. */
26466 if (typedef_variant_p (type))
26467 {
26468 tree name = TYPE_NAME (type);
26469 if (TREE_ASM_WRITTEN (name))
26470 return;
26471
26472 tree origin = decl_ultimate_origin (decl: name);
26473 if (origin != NULL && origin != name)
26474 {
26475 gen_decl_die (origin, NULL, NULL, context_die);
26476 return;
26477 }
26478
26479 /* Prevent broken recursion; we can't hand off to the same type. */
26480 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26481
26482 /* Give typedefs the right scope. */
26483 context_die = scope_die_for (t: type, context_die);
26484
26485 gen_decl_die (name, NULL, NULL, context_die);
26486 return;
26487 }
26488
26489 /* If type is an anonymous tagged type named by a typedef, let's
26490 generate debug info for the typedef. */
26491 if (is_naming_typedef_decl (TYPE_NAME (type)))
26492 {
26493 /* Give typedefs the right scope. */
26494 context_die = scope_die_for (t: type, context_die);
26495
26496 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26497 return;
26498 }
26499
26500 if (lang_hooks.types.get_debug_type)
26501 {
26502 tree debug_type = lang_hooks.types.get_debug_type (type);
26503
26504 if (debug_type != NULL_TREE && debug_type != type)
26505 {
26506 gen_type_die_with_usage (type: debug_type, context_die, usage, reverse);
26507 return;
26508 }
26509 }
26510
26511 /* We are going to output a DIE to represent the unqualified version
26512 of this type (i.e. without any const or volatile qualifiers) so
26513 get the main variant (i.e. the unqualified version) of this type
26514 now. (Vectors and arrays are special because the debugging info is in the
26515 cloned type itself. Similarly function/method types can contain extra
26516 ref-qualification). */
26517 if (FUNC_OR_METHOD_TYPE_P (type))
26518 {
26519 /* For function/method types, can't use type_main_variant here,
26520 because that can have different ref-qualifiers for C++,
26521 but try to canonicalize. */
26522 tree main = TYPE_MAIN_VARIANT (type);
26523 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26524 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26525 && check_base_type (cand: t, base: main)
26526 && check_lang_type (cand: t, base: type))
26527 {
26528 type = t;
26529 break;
26530 }
26531 }
26532 else if (TREE_CODE (type) != VECTOR_TYPE
26533 && TREE_CODE (type) != ARRAY_TYPE)
26534 type = type_main_variant (type);
26535
26536 /* If this is an array type with hidden descriptor, handle it first. */
26537 if (!TREE_ASM_WRITTEN (type)
26538 && lang_hooks.types.get_array_descr_info)
26539 {
26540 memset (s: &info, c: 0, n: sizeof (info));
26541 if (lang_hooks.types.get_array_descr_info (type, &info))
26542 {
26543 /* Fortran sometimes emits array types with no dimension. */
26544 gcc_assert (info.ndimensions >= 0
26545 && (info.ndimensions
26546 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26547 gen_descr_array_type_die (type, info: &info, context_die);
26548 TREE_ASM_WRITTEN (type) = 1;
26549 return;
26550 }
26551 }
26552
26553 if (TREE_ASM_WRITTEN (type) && !reverse)
26554 {
26555 /* Variable-length types may be incomplete even if
26556 TREE_ASM_WRITTEN. For such types, fall through to
26557 gen_array_type_die() and possibly fill in
26558 DW_AT_{upper,lower}_bound attributes. */
26559 if ((TREE_CODE (type) != ARRAY_TYPE
26560 && TREE_CODE (type) != RECORD_TYPE
26561 && TREE_CODE (type) != UNION_TYPE
26562 && TREE_CODE (type) != QUAL_UNION_TYPE)
26563 || !variably_modified_type_p (type, NULL))
26564 return;
26565 }
26566
26567 switch (TREE_CODE (type))
26568 {
26569 case ERROR_MARK:
26570 break;
26571
26572 case POINTER_TYPE:
26573 case REFERENCE_TYPE:
26574 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26575 ensures that the gen_type_die recursion will terminate even if the
26576 type is recursive. Recursive types are possible in Ada. */
26577 /* ??? We could perhaps do this for all types before the switch
26578 statement. */
26579 TREE_ASM_WRITTEN (type) = 1;
26580
26581 /* For these types, all that is required is that we output a DIE (or a
26582 set of DIEs) to represent the "basis" type. */
26583 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26584 usage: DINFO_USAGE_IND_USE);
26585 break;
26586
26587 case OFFSET_TYPE:
26588 /* This code is used for C++ pointer-to-data-member types.
26589 Output a description of the relevant class type. */
26590 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26591 usage: DINFO_USAGE_IND_USE);
26592
26593 /* Output a description of the type of the object pointed to. */
26594 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26595 usage: DINFO_USAGE_IND_USE);
26596
26597 /* Now output a DIE to represent this pointer-to-data-member type
26598 itself. */
26599 gen_ptr_to_mbr_type_die (type, context_die);
26600 break;
26601
26602 case FUNCTION_TYPE:
26603 /* Force out return type (in case it wasn't forced out already). */
26604 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26605 usage: DINFO_USAGE_DIR_USE);
26606 gen_subroutine_type_die (type, context_die);
26607 break;
26608
26609 case METHOD_TYPE:
26610 /* Force out return type (in case it wasn't forced out already). */
26611 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26612 usage: DINFO_USAGE_DIR_USE);
26613 gen_subroutine_type_die (type, context_die);
26614 break;
26615
26616 case ARRAY_TYPE:
26617 case VECTOR_TYPE:
26618 gen_array_type_die (type, context_die);
26619 break;
26620
26621 case ENUMERAL_TYPE:
26622 case RECORD_TYPE:
26623 case UNION_TYPE:
26624 case QUAL_UNION_TYPE:
26625 gen_tagged_type_die (type, context_die, usage, reverse);
26626 return;
26627
26628 case VOID_TYPE:
26629 case OPAQUE_TYPE:
26630 case INTEGER_TYPE:
26631 case REAL_TYPE:
26632 case FIXED_POINT_TYPE:
26633 case COMPLEX_TYPE:
26634 case BOOLEAN_TYPE:
26635 case BITINT_TYPE:
26636 /* No DIEs needed for fundamental types. */
26637 break;
26638
26639 case NULLPTR_TYPE:
26640 case LANG_TYPE:
26641 /* Just use DW_TAG_unspecified_type. */
26642 {
26643 dw_die_ref type_die = lookup_type_die (type);
26644 if (type_die == NULL)
26645 {
26646 tree name = TYPE_IDENTIFIER (type);
26647 type_die = new_die (tag_value: DW_TAG_unspecified_type, parent_die: comp_unit_die (),
26648 t: type);
26649 add_name_attribute (die: type_die, IDENTIFIER_POINTER (name));
26650 equate_type_number_to_die (type, type_die);
26651 }
26652 }
26653 break;
26654
26655 default:
26656 if (is_cxx_auto (type))
26657 {
26658 tree name = TYPE_IDENTIFIER (type);
26659 dw_die_ref *die = (name == get_identifier ("auto")
26660 ? &auto_die : &decltype_auto_die);
26661 if (!*die)
26662 {
26663 *die = new_die (tag_value: DW_TAG_unspecified_type,
26664 parent_die: comp_unit_die (), NULL_TREE);
26665 add_name_attribute (die: *die, IDENTIFIER_POINTER (name));
26666 }
26667 equate_type_number_to_die (type, type_die: *die);
26668 break;
26669 }
26670 gcc_unreachable ();
26671 }
26672
26673 TREE_ASM_WRITTEN (type) = 1;
26674}
26675
26676static void
26677gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26678{
26679 if (type != error_mark_node)
26680 {
26681 gen_type_die_with_usage (type, context_die, usage: DINFO_USAGE_DIR_USE, reverse);
26682 if (flag_checking)
26683 {
26684 dw_die_ref die = lookup_type_die (type);
26685 if (die)
26686 check_die (die);
26687 }
26688 }
26689}
26690
26691/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26692 things which are local to the given block. */
26693
26694static void
26695gen_block_die (tree stmt, dw_die_ref context_die)
26696{
26697 int must_output_die = 0;
26698 bool inlined_func;
26699
26700 /* Ignore blocks that are NULL. */
26701 if (stmt == NULL_TREE)
26702 return;
26703
26704 inlined_func = inlined_function_outer_scope_p (block: stmt);
26705
26706 /* If the block is one fragment of a non-contiguous block, do not
26707 process the variables, since they will have been done by the
26708 origin block. Do process subblocks. */
26709 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26710 {
26711 tree sub;
26712
26713 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26714 gen_block_die (stmt: sub, context_die);
26715
26716 return;
26717 }
26718
26719 /* Determine if we need to output any Dwarf DIEs at all to represent this
26720 block. */
26721 if (inlined_func)
26722 /* The outer scopes for inlinings *must* always be represented. We
26723 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26724 must_output_die = 1;
26725 else if (lookup_block_die (block: stmt))
26726 /* If we already have a DIE then it was filled early. Meanwhile
26727 we might have pruned all BLOCK_VARS as optimized out but we
26728 still want to generate high/low PC attributes so output it. */
26729 must_output_die = 1;
26730 else if (TREE_USED (stmt)
26731 || TREE_ASM_WRITTEN (stmt))
26732 {
26733 /* Determine if this block directly contains any "significant"
26734 local declarations which we will need to output DIEs for. */
26735 if (debug_info_level > DINFO_LEVEL_TERSE)
26736 {
26737 /* We are not in terse mode so any local declaration that
26738 is not ignored for debug purposes counts as being a
26739 "significant" one. */
26740 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26741 must_output_die = 1;
26742 else
26743 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26744 if (!DECL_IGNORED_P (var))
26745 {
26746 must_output_die = 1;
26747 break;
26748 }
26749 }
26750 else if (!dwarf2out_ignore_block (stmt))
26751 must_output_die = 1;
26752 }
26753
26754 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26755 DIE for any block which contains no significant local declarations at
26756 all. Rather, in such cases we just call `decls_for_scope' so that any
26757 needed Dwarf info for any sub-blocks will get properly generated. Note
26758 that in terse mode, our definition of what constitutes a "significant"
26759 local declaration gets restricted to include only inlined function
26760 instances and local (nested) function definitions. */
26761 if (must_output_die)
26762 {
26763 if (inlined_func)
26764 gen_inlined_subroutine_die (stmt, context_die);
26765 else
26766 gen_lexical_block_die (stmt, context_die);
26767 }
26768 else
26769 decls_for_scope (stmt, context_die);
26770}
26771
26772/* Process variable DECL (or variable with origin ORIGIN) within
26773 block STMT and add it to CONTEXT_DIE. */
26774static void
26775process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26776{
26777 dw_die_ref die;
26778 tree decl_or_origin = decl ? decl : origin;
26779
26780 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26781 die = lookup_decl_die (decl: decl_or_origin);
26782 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26783 {
26784 if (TYPE_DECL_IS_STUB (decl_or_origin))
26785 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26786 else
26787 die = lookup_decl_die (decl: decl_or_origin);
26788 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26789 if (! die && ! early_dwarf)
26790 return;
26791 }
26792 else
26793 die = NULL;
26794
26795 /* Avoid creating DIEs for local typedefs and concrete static variables that
26796 will only be pruned later. */
26797 if ((origin || decl_ultimate_origin (decl))
26798 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26799 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26800 {
26801 origin = decl_ultimate_origin (decl: decl_or_origin);
26802 if (decl && VAR_P (decl) && die != NULL)
26803 {
26804 die = lookup_decl_die (decl: origin);
26805 if (die != NULL)
26806 equate_decl_number_to_die (decl, decl_die: die);
26807 }
26808 return;
26809 }
26810
26811 if (die != NULL && die->die_parent == NULL)
26812 add_child_die (die: context_die, child_die: die);
26813
26814 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26815 {
26816 if (early_dwarf)
26817 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26818 stmt, context_die);
26819 }
26820 else
26821 {
26822 if (decl && DECL_P (decl))
26823 {
26824 die = lookup_decl_die (decl);
26825
26826 /* Early created DIEs do not have a parent as the decls refer
26827 to the function as DECL_CONTEXT rather than the BLOCK. */
26828 if (die && die->die_parent == NULL)
26829 {
26830 gcc_assert (in_lto_p);
26831 add_child_die (die: context_die, child_die: die);
26832 }
26833 }
26834
26835 gen_decl_die (decl, origin, NULL, context_die);
26836 }
26837}
26838
26839/* Generate all of the decls declared within a given scope and (recursively)
26840 all of its sub-blocks. */
26841
26842static void
26843decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26844{
26845 tree decl;
26846 unsigned int i;
26847 tree subblocks;
26848
26849 /* Ignore NULL blocks. */
26850 if (stmt == NULL_TREE)
26851 return;
26852
26853 /* Output the DIEs to represent all of the data objects and typedefs
26854 declared directly within this block but not within any nested
26855 sub-blocks. Also, nested function and tag DIEs have been
26856 generated with a parent of NULL; fix that up now. We don't
26857 have to do this if we're at -g1. */
26858 if (debug_info_level > DINFO_LEVEL_TERSE)
26859 {
26860 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26861 process_scope_var (stmt, decl, NULL_TREE, context_die);
26862 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26863 origin - avoid doing this twice as we have no good way to see
26864 if we've done it once already. */
26865 if (! early_dwarf)
26866 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26867 {
26868 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26869 if (decl == current_function_decl)
26870 /* Ignore declarations of the current function, while they
26871 are declarations, gen_subprogram_die would treat them
26872 as definitions again, because they are equal to
26873 current_function_decl and endlessly recurse. */;
26874 else if (TREE_CODE (decl) == FUNCTION_DECL)
26875 process_scope_var (stmt, decl, NULL_TREE, context_die);
26876 else
26877 process_scope_var (stmt, NULL_TREE, origin: decl, context_die);
26878 }
26879 }
26880
26881 /* Even if we're at -g1, we need to process the subblocks in order to get
26882 inlined call information. */
26883
26884 /* Output the DIEs to represent all sub-blocks (and the items declared
26885 therein) of this block. */
26886 if (recurse)
26887 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26888 subblocks != NULL;
26889 subblocks = BLOCK_CHAIN (subblocks))
26890 gen_block_die (stmt: subblocks, context_die);
26891}
26892
26893/* Is this a typedef we can avoid emitting? */
26894
26895static bool
26896is_redundant_typedef (const_tree decl)
26897{
26898 if (TYPE_DECL_IS_STUB (decl))
26899 return true;
26900
26901 if (DECL_ARTIFICIAL (decl)
26902 && DECL_CONTEXT (decl)
26903 && is_tagged_type (DECL_CONTEXT (decl))
26904 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26905 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26906 /* Also ignore the artificial member typedef for the class name. */
26907 return true;
26908
26909 return false;
26910}
26911
26912/* Return TRUE if TYPE is a typedef that names a type for linkage
26913 purposes. This kind of typedefs is produced by the C++ FE for
26914 constructs like:
26915
26916 typedef struct {...} foo;
26917
26918 In that case, there is no typedef variant type produced for foo.
26919 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26920 struct type. */
26921
26922static bool
26923is_naming_typedef_decl (const_tree decl)
26924{
26925 if (decl == NULL_TREE
26926 || TREE_CODE (decl) != TYPE_DECL
26927 || DECL_NAMELESS (decl)
26928 || !is_tagged_type (TREE_TYPE (decl))
26929 || DECL_IS_UNDECLARED_BUILTIN (decl)
26930 || is_redundant_typedef (decl)
26931 /* It looks like Ada produces TYPE_DECLs that are very similar
26932 to C++ naming typedefs but that have different
26933 semantics. Let's be specific to c++ for now. */
26934 || !is_cxx (decl))
26935 return false;
26936
26937 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26938 && TYPE_NAME (TREE_TYPE (decl)) == decl
26939 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26940 != TYPE_NAME (TREE_TYPE (decl))));
26941}
26942
26943/* Looks up the DIE for a context. */
26944
26945static inline dw_die_ref
26946lookup_context_die (tree context)
26947{
26948 if (context)
26949 {
26950 /* Find die that represents this context. */
26951 if (TYPE_P (context))
26952 {
26953 context = TYPE_MAIN_VARIANT (context);
26954 dw_die_ref ctx = lookup_type_die (type: context);
26955 if (!ctx)
26956 return NULL;
26957 return strip_naming_typedef (type: context, type_die: ctx);
26958 }
26959 else
26960 return lookup_decl_die (decl: context);
26961 }
26962 return comp_unit_die ();
26963}
26964
26965/* Returns the DIE for a context. */
26966
26967static inline dw_die_ref
26968get_context_die (tree context)
26969{
26970 if (context)
26971 {
26972 /* Find die that represents this context. */
26973 if (TYPE_P (context))
26974 {
26975 context = TYPE_MAIN_VARIANT (context);
26976 return strip_naming_typedef (type: context, type_die: force_type_die (context));
26977 }
26978 else
26979 return force_decl_die (context);
26980 }
26981 return comp_unit_die ();
26982}
26983
26984/* Returns the DIE for decl. A DIE will always be returned. */
26985
26986static dw_die_ref
26987force_decl_die (tree decl)
26988{
26989 dw_die_ref decl_die;
26990 unsigned saved_external_flag;
26991 tree save_fn = NULL_TREE;
26992 decl_die = lookup_decl_die (decl);
26993 if (!decl_die)
26994 {
26995 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26996
26997 decl_die = lookup_decl_die (decl);
26998 if (decl_die)
26999 return decl_die;
27000
27001 switch (TREE_CODE (decl))
27002 {
27003 case FUNCTION_DECL:
27004 /* Clear current_function_decl, so that gen_subprogram_die thinks
27005 that this is a declaration. At this point, we just want to force
27006 declaration die. */
27007 save_fn = current_function_decl;
27008 current_function_decl = NULL_TREE;
27009 gen_subprogram_die (decl, context_die);
27010 current_function_decl = save_fn;
27011 break;
27012
27013 case VAR_DECL:
27014 /* Set external flag to force declaration die. Restore it after
27015 gen_decl_die() call. */
27016 saved_external_flag = DECL_EXTERNAL (decl);
27017 DECL_EXTERNAL (decl) = 1;
27018 gen_decl_die (decl, NULL, NULL, context_die);
27019 DECL_EXTERNAL (decl) = saved_external_flag;
27020 break;
27021
27022 case NAMESPACE_DECL:
27023 if (dwarf_version >= 3 || !dwarf_strict)
27024 dwarf2out_decl (decl);
27025 else
27026 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
27027 decl_die = comp_unit_die ();
27028 break;
27029
27030 case CONST_DECL:
27031 /* Enumerators shouldn't need force_decl_die. */
27032 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
27033 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
27034 gen_decl_die (decl, NULL, NULL, context_die);
27035 break;
27036
27037 case TRANSLATION_UNIT_DECL:
27038 decl_die = comp_unit_die ();
27039 break;
27040
27041 default:
27042 gcc_unreachable ();
27043 }
27044
27045 /* We should be able to find the DIE now. */
27046 if (!decl_die)
27047 decl_die = lookup_decl_die (decl);
27048 gcc_assert (decl_die);
27049 }
27050
27051 return decl_die;
27052}
27053
27054/* Returns the DIE for TYPE, that must not be a base type. A DIE is
27055 always returned. */
27056
27057static dw_die_ref
27058force_type_die (tree type)
27059{
27060 dw_die_ref type_die;
27061
27062 type_die = lookup_type_die (type);
27063 if (!type_die)
27064 {
27065 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
27066
27067 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
27068 reverse: false, context_die);
27069 gcc_assert (type_die);
27070 }
27071 return type_die;
27072}
27073
27074/* Force out any required namespaces to be able to output DECL,
27075 and return the new context_die for it, if it's changed. */
27076
27077static dw_die_ref
27078setup_namespace_context (tree thing, dw_die_ref context_die)
27079{
27080 tree context = (DECL_P (thing)
27081 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
27082 if (context && TREE_CODE (context) == NAMESPACE_DECL)
27083 /* Force out the namespace. */
27084 context_die = force_decl_die (decl: context);
27085
27086 return context_die;
27087}
27088
27089/* Emit a declaration DIE for THING (which is either a DECL or a tagged
27090 type) within its namespace, if appropriate.
27091
27092 For compatibility with older debuggers, namespace DIEs only contain
27093 declarations; all definitions are emitted at CU scope, with
27094 DW_AT_specification pointing to the declaration (like with class
27095 members). */
27096
27097static dw_die_ref
27098declare_in_namespace (tree thing, dw_die_ref context_die)
27099{
27100 dw_die_ref ns_context;
27101
27102 if (debug_info_level <= DINFO_LEVEL_TERSE)
27103 return context_die;
27104
27105 /* External declarations in the local scope only need to be emitted
27106 once, not once in the namespace and once in the scope.
27107
27108 This avoids declaring the `extern' below in the
27109 namespace DIE as well as in the innermost scope:
27110
27111 namespace S
27112 {
27113 int i=5;
27114 int foo()
27115 {
27116 int i=8;
27117 extern int i;
27118 return i;
27119 }
27120 }
27121 */
27122 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
27123 return context_die;
27124
27125 /* If this decl is from an inlined function, then don't try to emit it in its
27126 namespace, as we will get confused. It would have already been emitted
27127 when the abstract instance of the inline function was emitted anyways. */
27128 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
27129 return context_die;
27130
27131 ns_context = setup_namespace_context (thing, context_die);
27132
27133 if (ns_context != context_die)
27134 {
27135 if (is_fortran () || is_dlang ())
27136 return ns_context;
27137 if (DECL_P (thing))
27138 gen_decl_die (thing, NULL, NULL, ns_context);
27139 else
27140 gen_type_die (type: thing, context_die: ns_context);
27141 }
27142 return context_die;
27143}
27144
27145/* Generate a DIE for a namespace or namespace alias. */
27146
27147static void
27148gen_namespace_die (tree decl, dw_die_ref context_die)
27149{
27150 dw_die_ref namespace_die;
27151
27152 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
27153 they are an alias of. */
27154 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
27155 {
27156 /* Output a real namespace or module. */
27157 context_die = setup_namespace_context (thing: decl, context_die: comp_unit_die ());
27158 namespace_die = new_die (tag_value: is_fortran () || is_dlang () || is_ada ()
27159 ? DW_TAG_module : DW_TAG_namespace,
27160 parent_die: context_die, t: decl);
27161 /* For Fortran modules defined in different CU don't add src coords. */
27162 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
27163 {
27164 const char *name = dwarf2_name (decl, scope: 0);
27165 if (name)
27166 add_name_attribute (die: namespace_die, name_string: name);
27167 }
27168 else
27169 add_name_and_src_coords_attributes (die: namespace_die, decl);
27170 if (DECL_EXTERNAL (decl))
27171 add_AT_flag (die: namespace_die, attr_kind: DW_AT_declaration, flag: 1);
27172 equate_decl_number_to_die (decl, decl_die: namespace_die);
27173 }
27174 else
27175 {
27176 /* Output a namespace alias. */
27177
27178 /* Force out the namespace we are an alias of, if necessary. */
27179 dw_die_ref origin_die
27180 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
27181
27182 if (DECL_FILE_SCOPE_P (decl)
27183 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
27184 context_die = setup_namespace_context (thing: decl, context_die: comp_unit_die ());
27185 /* Now create the namespace alias DIE. */
27186 namespace_die = new_die (tag_value: DW_TAG_imported_declaration, parent_die: context_die, t: decl);
27187 add_name_and_src_coords_attributes (die: namespace_die, decl);
27188 add_AT_die_ref (die: namespace_die, attr_kind: DW_AT_import, targ_die: origin_die);
27189 equate_decl_number_to_die (decl, decl_die: namespace_die);
27190 }
27191 if ((dwarf_version >= 5 || !dwarf_strict)
27192 && lang_hooks.decls.decl_dwarf_attribute (decl,
27193 DW_AT_export_symbols) == 1)
27194 add_AT_flag (die: namespace_die, attr_kind: DW_AT_export_symbols, flag: 1);
27195
27196 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27197 if (want_pubnames ())
27198 add_pubname_string (str: lang_hooks.dwarf_name (decl, 1), die: namespace_die);
27199}
27200
27201/* Generate Dwarf debug information for a decl described by DECL.
27202 The return value is currently only meaningful for PARM_DECLs,
27203 for all other decls it returns NULL.
27204
27205 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27206 It can be NULL otherwise. */
27207
27208static dw_die_ref
27209gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27210 dw_die_ref context_die)
27211{
27212 tree decl_or_origin = decl ? decl : origin;
27213 tree class_origin = NULL, ultimate_origin;
27214
27215 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27216 return NULL;
27217
27218 switch (TREE_CODE (decl_or_origin))
27219 {
27220 case ERROR_MARK:
27221 break;
27222
27223 case CONST_DECL:
27224 if (!is_fortran () && !is_ada () && !is_dlang ())
27225 {
27226 /* The individual enumerators of an enum type get output when we output
27227 the Dwarf representation of the relevant enum type itself. */
27228 break;
27229 }
27230
27231 /* Emit its type. */
27232 gen_type_die (TREE_TYPE (decl), context_die);
27233
27234 /* And its containing namespace. */
27235 context_die = declare_in_namespace (thing: decl, context_die);
27236
27237 gen_const_die (decl, context_die);
27238 break;
27239
27240 case FUNCTION_DECL:
27241#if 0
27242 /* FIXME */
27243 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27244 on local redeclarations of global functions. That seems broken. */
27245 if (current_function_decl != decl)
27246 /* This is only a declaration. */;
27247#endif
27248
27249 /* We should have abstract copies already and should not generate
27250 stray type DIEs in late LTO dumping. */
27251 if (! early_dwarf)
27252 ;
27253
27254 /* If we're emitting a clone, emit info for the abstract instance. */
27255 else if (origin || DECL_ORIGIN (decl) != decl)
27256 dwarf2out_abstract_function (decl: origin
27257 ? DECL_ORIGIN (origin)
27258 : DECL_ABSTRACT_ORIGIN (decl));
27259
27260 /* If we're emitting a possibly inlined function emit it as
27261 abstract instance. */
27262 else if (cgraph_function_possibly_inlined_p (decl)
27263 && ! DECL_ABSTRACT_P (decl)
27264 && ! class_or_namespace_scope_p (context_die)
27265 /* dwarf2out_abstract_function won't emit a die if this is just
27266 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27267 that case, because that works only if we have a die. */
27268 && DECL_INITIAL (decl) != NULL_TREE)
27269 dwarf2out_abstract_function (decl);
27270
27271 /* Otherwise we're emitting the primary DIE for this decl. */
27272 else if (debug_info_level > DINFO_LEVEL_TERSE)
27273 {
27274 /* Before we describe the FUNCTION_DECL itself, make sure that we
27275 have its containing type. */
27276 if (!origin)
27277 origin = decl_class_context (decl);
27278 if (origin != NULL_TREE)
27279 gen_type_die (type: origin, context_die);
27280
27281 /* And its return type. */
27282 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27283
27284 /* And its virtual context. */
27285 if (DECL_VINDEX (decl) != NULL_TREE)
27286 gen_type_die (DECL_CONTEXT (decl), context_die);
27287
27288 /* Make sure we have a member DIE for decl. */
27289 if (origin != NULL_TREE)
27290 gen_type_die_for_member (type: origin, member: decl, context_die);
27291
27292 /* And its containing namespace. */
27293 context_die = declare_in_namespace (thing: decl, context_die);
27294 }
27295
27296 /* Now output a DIE to represent the function itself. */
27297 if (decl)
27298 gen_subprogram_die (decl, context_die);
27299 break;
27300
27301 case TYPE_DECL:
27302 /* If we are in terse mode, don't generate any DIEs to represent any
27303 actual typedefs. */
27304 if (debug_info_level <= DINFO_LEVEL_TERSE)
27305 break;
27306
27307 /* In the special case of a TYPE_DECL node representing the declaration
27308 of some type tag, if the given TYPE_DECL is marked as having been
27309 instantiated from some other (original) TYPE_DECL node (e.g. one which
27310 was generated within the original definition of an inline function) we
27311 used to generate a special (abbreviated) DW_TAG_structure_type,
27312 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27313 should be actually referencing those DIEs, as variable DIEs with that
27314 type would be emitted already in the abstract origin, so it was always
27315 removed during unused type prunning. Don't add anything in this
27316 case. */
27317 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27318 break;
27319
27320 if (is_redundant_typedef (decl))
27321 gen_type_die (TREE_TYPE (decl), context_die);
27322 else
27323 /* Output a DIE to represent the typedef itself. */
27324 gen_typedef_die (decl, context_die);
27325 break;
27326
27327 case LABEL_DECL:
27328 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27329 gen_label_die (decl, context_die);
27330 break;
27331
27332 case VAR_DECL:
27333 case RESULT_DECL:
27334 /* If we are in terse mode, don't generate any DIEs to represent any
27335 variable declarations or definitions unless it is external. */
27336 if (debug_info_level < DINFO_LEVEL_TERSE
27337 || (debug_info_level == DINFO_LEVEL_TERSE
27338 && !TREE_PUBLIC (decl_or_origin)))
27339 break;
27340
27341 if (debug_info_level > DINFO_LEVEL_TERSE)
27342 {
27343 /* Avoid generating stray type DIEs during late dwarf dumping.
27344 All types have been dumped early. */
27345 if (early_dwarf
27346 /* ??? But in LTRANS we cannot annotate early created variably
27347 modified type DIEs without copying them and adjusting all
27348 references to them. Dump them again as happens for inlining
27349 which copies both the decl and the types. */
27350 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27351 in VLA bound information for example. */
27352 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27353 current_function_decl)))
27354 {
27355 /* Output any DIEs that are needed to specify the type of this data
27356 object. */
27357 if (decl_by_reference_p (decl: decl_or_origin))
27358 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27359 else
27360 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27361 }
27362
27363 if (early_dwarf)
27364 {
27365 /* And its containing type. */
27366 class_origin = decl_class_context (decl: decl_or_origin);
27367 if (class_origin != NULL_TREE)
27368 gen_type_die_for_member (type: class_origin, member: decl_or_origin, context_die);
27369
27370 /* And its containing namespace. */
27371 context_die = declare_in_namespace (thing: decl_or_origin, context_die);
27372 }
27373 }
27374
27375 /* Now output the DIE to represent the data object itself. This gets
27376 complicated because of the possibility that the VAR_DECL really
27377 represents an inlined instance of a formal parameter for an inline
27378 function. */
27379 ultimate_origin = decl_ultimate_origin (decl: decl_or_origin);
27380 if (ultimate_origin != NULL_TREE
27381 && TREE_CODE (ultimate_origin) == PARM_DECL)
27382 gen_formal_parameter_die (node: decl, origin,
27383 emit_name_p: true /* Emit name attribute. */,
27384 context_die);
27385 else
27386 gen_variable_die (decl, origin, context_die);
27387 break;
27388
27389 case FIELD_DECL:
27390 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27391 /* Ignore the nameless fields that are used to skip bits but handle C++
27392 anonymous unions and structs. */
27393 if (DECL_NAME (decl) != NULL_TREE
27394 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27395 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27396 {
27397 gen_type_die (type: member_declared_type (member: decl), context_die);
27398 gen_field_die (decl, ctx, context_die);
27399 }
27400 break;
27401
27402 case PARM_DECL:
27403 /* Avoid generating stray type DIEs during late dwarf dumping.
27404 All types have been dumped early. */
27405 if (early_dwarf
27406 /* ??? But in LTRANS we cannot annotate early created variably
27407 modified type DIEs without copying them and adjusting all
27408 references to them. Dump them again as happens for inlining
27409 which copies both the decl and the types. */
27410 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27411 in VLA bound information for example. */
27412 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27413 current_function_decl)))
27414 {
27415 if (DECL_BY_REFERENCE (decl_or_origin))
27416 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27417 else
27418 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27419 }
27420 return gen_formal_parameter_die (node: decl, origin,
27421 emit_name_p: true /* Emit name attribute. */,
27422 context_die);
27423
27424 case NAMESPACE_DECL:
27425 if (dwarf_version >= 3 || !dwarf_strict)
27426 gen_namespace_die (decl, context_die);
27427 break;
27428
27429 case IMPORTED_DECL:
27430 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27431 DECL_CONTEXT (decl), context_die);
27432 break;
27433
27434 case NAMELIST_DECL:
27435 gen_namelist_decl (DECL_NAME (decl), context_die,
27436 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27437 break;
27438
27439 default:
27440 /* Probably some frontend-internal decl. Assume we don't care. */
27441 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27442 break;
27443 }
27444
27445 return NULL;
27446}
27447
27448/* Output initial debug information for global DECL. Called at the
27449 end of the parsing process.
27450
27451 This is the initial debug generation process. As such, the DIEs
27452 generated may be incomplete. A later debug generation pass
27453 (dwarf2out_late_global_decl) will augment the information generated
27454 in this pass (e.g., with complete location info). */
27455
27456static void
27457dwarf2out_early_global_decl (tree decl)
27458{
27459 set_early_dwarf s;
27460
27461 /* gen_decl_die() will set DECL_ABSTRACT because
27462 cgraph_function_possibly_inlined_p() returns true. This is in
27463 turn will cause DW_AT_inline attributes to be set.
27464
27465 This happens because at early dwarf generation, there is no
27466 cgraph information, causing cgraph_function_possibly_inlined_p()
27467 to return true. Trick cgraph_function_possibly_inlined_p()
27468 while we generate dwarf early. */
27469 bool save = symtab->global_info_ready;
27470 symtab->global_info_ready = true;
27471
27472 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27473 other DECLs and they can point to template types or other things
27474 that dwarf2out can't handle when done via dwarf2out_decl. */
27475 if (TREE_CODE (decl) != TYPE_DECL
27476 && TREE_CODE (decl) != PARM_DECL)
27477 {
27478 if (TREE_CODE (decl) == FUNCTION_DECL)
27479 {
27480 tree save_fndecl = current_function_decl;
27481
27482 /* For nested functions, make sure we have DIEs for the parents first
27483 so that all nested DIEs are generated at the proper scope in the
27484 first shot. */
27485 tree context = decl_function_context (decl);
27486 if (context != NULL)
27487 {
27488 dw_die_ref context_die = lookup_decl_die (decl: context);
27489 current_function_decl = context;
27490
27491 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27492 enough so that it lands in its own context. This avoids type
27493 pruning issues later on. */
27494 if (context_die == NULL || is_declaration_die (die: context_die))
27495 dwarf2out_early_global_decl (decl: context);
27496 }
27497
27498 /* Emit an abstract origin of a function first. This happens
27499 with C++ constructor clones for example and makes
27500 dwarf2out_abstract_function happy which requires the early
27501 DIE of the abstract instance to be present. */
27502 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27503 dw_die_ref origin_die;
27504 if (origin != NULL
27505 /* Do not emit the DIE multiple times but make sure to
27506 process it fully here in case we just saw a declaration. */
27507 && ((origin_die = lookup_decl_die (decl: origin)) == NULL
27508 || is_declaration_die (die: origin_die)))
27509 {
27510 current_function_decl = origin;
27511 dwarf2out_decl (origin);
27512 }
27513
27514 /* Emit the DIE for decl but avoid doing that multiple times. */
27515 dw_die_ref old_die;
27516 if ((old_die = lookup_decl_die (decl)) == NULL
27517 || is_declaration_die (die: old_die))
27518 {
27519 current_function_decl = decl;
27520 dwarf2out_decl (decl);
27521 }
27522
27523 current_function_decl = save_fndecl;
27524 }
27525 else
27526 dwarf2out_decl (decl);
27527 }
27528 symtab->global_info_ready = save;
27529}
27530
27531/* Return whether EXPR is an expression with the following pattern:
27532 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27533
27534static bool
27535is_trivial_indirect_ref (tree expr)
27536{
27537 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27538 return false;
27539
27540 tree nop = TREE_OPERAND (expr, 0);
27541 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27542 return false;
27543
27544 tree int_cst = TREE_OPERAND (nop, 0);
27545 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27546}
27547
27548/* Output debug information for global decl DECL. Called from
27549 toplev.cc after compilation proper has finished. */
27550
27551static void
27552dwarf2out_late_global_decl (tree decl)
27553{
27554 /* Fill-in any location information we were unable to determine
27555 on the first pass. */
27556 if (VAR_P (decl))
27557 {
27558 dw_die_ref die = lookup_decl_die (decl);
27559
27560 /* We may have to generate full debug late for LTO in case debug
27561 was not enabled at compile-time or the target doesn't support
27562 the LTO early debug scheme. */
27563 if (! die && in_lto_p
27564 /* Function scope variables are emitted when emitting the
27565 DIE for the function. */
27566 && ! local_function_static (decl))
27567 dwarf2out_decl (decl);
27568 else if (die)
27569 {
27570 /* We get called via the symtab code invoking late_global_decl
27571 for symbols that are optimized out.
27572
27573 Do not add locations for those, except if they have a
27574 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27575 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27576 INDIRECT_REF expression, as this could generate relocations to
27577 text symbols in LTO object files, which is invalid. */
27578 varpool_node *node = varpool_node::get (decl);
27579 if ((! node || ! node->definition)
27580 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27581 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27582 tree_add_const_value_attribute_for_decl (var_die: die, decl);
27583 else
27584 add_location_or_const_value_attribute (die, decl, cache_p: false);
27585 }
27586 }
27587}
27588
27589/* Output debug information for type decl DECL. Called from toplev.cc
27590 and from language front ends (to record built-in types). */
27591static void
27592dwarf2out_type_decl (tree decl, int local)
27593{
27594 if (!local)
27595 {
27596 set_early_dwarf s;
27597 dwarf2out_decl (decl);
27598 }
27599}
27600
27601/* Output debug information for imported module or decl DECL.
27602 NAME is non-NULL name in the lexical block if the decl has been renamed.
27603 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27604 that DECL belongs to.
27605 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27606static void
27607dwarf2out_imported_module_or_decl_1 (tree decl,
27608 tree name,
27609 tree lexical_block,
27610 dw_die_ref lexical_block_die)
27611{
27612 expanded_location xloc;
27613 dw_die_ref imported_die = NULL;
27614 dw_die_ref at_import_die;
27615
27616 if (TREE_CODE (decl) == IMPORTED_DECL)
27617 {
27618 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27619 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27620 gcc_assert (decl);
27621 }
27622 else
27623 xloc = expand_location (input_location);
27624
27625 if (TREE_CODE (decl) == TYPE_DECL)
27626 {
27627 at_import_die = force_type_die (TREE_TYPE (decl));
27628 /* For namespace N { typedef void T; } using N::T; base_type_die
27629 returns NULL, but DW_TAG_imported_declaration requires
27630 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27631 if (!at_import_die)
27632 {
27633 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27634 gen_typedef_die (decl, context_die: get_context_die (DECL_CONTEXT (decl)));
27635 at_import_die = lookup_type_die (TREE_TYPE (decl));
27636 gcc_assert (at_import_die);
27637 }
27638 }
27639 else
27640 {
27641 at_import_die = lookup_decl_die (decl);
27642 if (!at_import_die)
27643 {
27644 /* If we're trying to avoid duplicate debug info, we may not have
27645 emitted the member decl for this field. Emit it now. */
27646 if (TREE_CODE (decl) == FIELD_DECL)
27647 {
27648 tree type = DECL_CONTEXT (decl);
27649
27650 if (TYPE_CONTEXT (type)
27651 && TYPE_P (TYPE_CONTEXT (type))
27652 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27653 usage: DINFO_USAGE_DIR_USE))
27654 return;
27655 gen_type_die_for_member (type, member: decl,
27656 context_die: get_context_die (TYPE_CONTEXT (type)));
27657 }
27658 if (TREE_CODE (decl) == CONST_DECL)
27659 {
27660 /* Individual enumerators of an enum type do not get output here
27661 (see gen_decl_die), so we cannot call force_decl_die. */
27662 if (!is_fortran () && !is_ada () && !is_dlang ())
27663 return;
27664 }
27665 if (TREE_CODE (decl) == NAMELIST_DECL)
27666 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27667 get_context_die (DECL_CONTEXT (decl)),
27668 NULL_TREE);
27669 else
27670 at_import_die = force_decl_die (decl);
27671 }
27672 }
27673
27674 if (TREE_CODE (decl) == NAMESPACE_DECL)
27675 {
27676 if (dwarf_version >= 3 || !dwarf_strict)
27677 imported_die = new_die (tag_value: DW_TAG_imported_module,
27678 parent_die: lexical_block_die,
27679 t: lexical_block);
27680 else
27681 return;
27682 }
27683 else
27684 imported_die = new_die (tag_value: DW_TAG_imported_declaration,
27685 parent_die: lexical_block_die,
27686 t: lexical_block);
27687
27688 add_AT_file (die: imported_die, attr_kind: DW_AT_decl_file, fd: lookup_filename (xloc.file));
27689 add_AT_unsigned (die: imported_die, attr_kind: DW_AT_decl_line, unsigned_val: xloc.line);
27690 if (debug_column_info && xloc.column)
27691 add_AT_unsigned (die: imported_die, attr_kind: DW_AT_decl_column, unsigned_val: xloc.column);
27692 if (name)
27693 add_AT_string (die: imported_die, attr_kind: DW_AT_name,
27694 IDENTIFIER_POINTER (name));
27695 add_AT_die_ref (die: imported_die, attr_kind: DW_AT_import, targ_die: at_import_die);
27696}
27697
27698/* Output debug information for imported module or decl DECL.
27699 NAME is non-NULL name in context if the decl has been renamed.
27700 CHILD is true if decl is one of the renamed decls as part of
27701 importing whole module.
27702 IMPLICIT is set if this hook is called for an implicit import
27703 such as inline namespace. */
27704
27705static void
27706dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27707 bool child, bool implicit)
27708{
27709 /* dw_die_ref at_import_die; */
27710 dw_die_ref scope_die;
27711
27712 if (debug_info_level <= DINFO_LEVEL_TERSE)
27713 return;
27714
27715 gcc_assert (decl);
27716
27717 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27718 should be enough, for DWARF4 and older even if we emit as extension
27719 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27720 for the benefit of consumers unaware of DW_AT_export_symbols. */
27721 if (implicit
27722 && dwarf_version >= 5
27723 && lang_hooks.decls.decl_dwarf_attribute (decl,
27724 DW_AT_export_symbols) == 1)
27725 return;
27726
27727 set_early_dwarf s;
27728
27729 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27730 We need decl DIE for reference and scope die. First, get DIE for the decl
27731 itself. */
27732
27733 /* Get the scope die for decl context. Use comp_unit_die for global module
27734 or decl. If die is not found for non globals, force new die. */
27735 if (context
27736 && TYPE_P (context)
27737 && !should_emit_struct_debug (type: context, usage: DINFO_USAGE_DIR_USE))
27738 return;
27739
27740 scope_die = get_context_die (context);
27741
27742 if (child)
27743 {
27744 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27745 there is nothing we can do, here. */
27746 if (dwarf_version < 3 && dwarf_strict)
27747 return;
27748
27749 gcc_assert (scope_die->die_child);
27750 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27751 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27752 scope_die = scope_die->die_child;
27753 }
27754
27755 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27756 dwarf2out_imported_module_or_decl_1 (decl, name, lexical_block: context, lexical_block_die: scope_die);
27757}
27758
27759/* Output debug information for namelists. */
27760
27761static dw_die_ref
27762gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27763{
27764 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27765 tree value;
27766 unsigned i;
27767
27768 if (debug_info_level <= DINFO_LEVEL_TERSE)
27769 return NULL;
27770
27771 gcc_assert (scope_die != NULL);
27772 nml_die = new_die (tag_value: DW_TAG_namelist, parent_die: scope_die, NULL);
27773 add_AT_string (die: nml_die, attr_kind: DW_AT_name, IDENTIFIER_POINTER (name));
27774
27775 /* If there are no item_decls, we have a nondefining namelist, e.g.
27776 with USE association; hence, set DW_AT_declaration. */
27777 if (item_decls == NULL_TREE)
27778 {
27779 add_AT_flag (die: nml_die, attr_kind: DW_AT_declaration, flag: 1);
27780 return nml_die;
27781 }
27782
27783 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27784 {
27785 nml_item_ref_die = lookup_decl_die (decl: value);
27786 if (!nml_item_ref_die)
27787 nml_item_ref_die = force_decl_die (decl: value);
27788
27789 nml_item_die = new_die (tag_value: DW_TAG_namelist_item, parent_die: nml_die, NULL);
27790 add_AT_die_ref (die: nml_item_die, attr_kind: DW_AT_namelist_item, targ_die: nml_item_ref_die);
27791 }
27792 return nml_die;
27793}
27794
27795
27796/* Write the debugging output for DECL and return the DIE. */
27797
27798static void
27799dwarf2out_decl (tree decl)
27800{
27801 dw_die_ref context_die = comp_unit_die ();
27802
27803 switch (TREE_CODE (decl))
27804 {
27805 case ERROR_MARK:
27806 return;
27807
27808 case FUNCTION_DECL:
27809 /* If we're a nested function, initially use a parent of NULL; if we're
27810 a plain function, this will be fixed up in decls_for_scope. If
27811 we're a method, it will be ignored, since we already have a DIE.
27812 Avoid doing this late though since clones of class methods may
27813 otherwise end up in limbo and create type DIEs late. */
27814 if (early_dwarf
27815 && decl_function_context (decl)
27816 /* But if we're in terse mode, we don't care about scope. */
27817 && debug_info_level > DINFO_LEVEL_TERSE)
27818 context_die = NULL;
27819 break;
27820
27821 case VAR_DECL:
27822 /* For local statics lookup proper context die. */
27823 if (local_function_static (decl))
27824 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27825
27826 /* If we are in terse mode, don't generate any DIEs to represent any
27827 variable declarations or definitions unless it is external. */
27828 if (debug_info_level < DINFO_LEVEL_TERSE
27829 || (debug_info_level == DINFO_LEVEL_TERSE
27830 && !TREE_PUBLIC (decl)))
27831 return;
27832 break;
27833
27834 case CONST_DECL:
27835 if (debug_info_level <= DINFO_LEVEL_TERSE)
27836 return;
27837 if (!is_fortran () && !is_ada () && !is_dlang ())
27838 return;
27839 if (TREE_STATIC (decl) && decl_function_context (decl))
27840 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27841 break;
27842
27843 case NAMESPACE_DECL:
27844 case IMPORTED_DECL:
27845 if (debug_info_level <= DINFO_LEVEL_TERSE)
27846 return;
27847 if (lookup_decl_die (decl) != NULL)
27848 return;
27849 break;
27850
27851 case TYPE_DECL:
27852 /* Don't emit stubs for types unless they are needed by other DIEs. */
27853 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27854 return;
27855
27856 /* Don't bother trying to generate any DIEs to represent any of the
27857 normal built-in types for the language we are compiling. */
27858 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27859 return;
27860
27861 /* If we are in terse mode, don't generate any DIEs for types. */
27862 if (debug_info_level <= DINFO_LEVEL_TERSE)
27863 return;
27864
27865 /* If we're a function-scope tag, initially use a parent of NULL;
27866 this will be fixed up in decls_for_scope. */
27867 if (decl_function_context (decl))
27868 context_die = NULL;
27869
27870 break;
27871
27872 case NAMELIST_DECL:
27873 break;
27874
27875 default:
27876 return;
27877 }
27878
27879 gen_decl_die (decl, NULL, NULL, context_die);
27880
27881 if (flag_checking)
27882 {
27883 dw_die_ref die = lookup_decl_die (decl);
27884 if (die)
27885 check_die (die);
27886 }
27887}
27888
27889/* Write the debugging output for DECL. */
27890
27891static void
27892dwarf2out_function_decl (tree decl)
27893{
27894 dwarf2out_decl (decl);
27895 call_arg_locations = NULL;
27896 call_arg_loc_last = NULL;
27897 call_site_count = -1;
27898 tail_call_site_count = -1;
27899 decl_loc_table->empty ();
27900 cached_dw_loc_list_table->empty ();
27901}
27902
27903/* Output a marker (i.e. a label) for the beginning of the generated code for
27904 a lexical block. */
27905
27906static void
27907dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27908 unsigned int blocknum,
27909 tree block ATTRIBUTE_UNUSED)
27910{
27911#ifdef CODEVIEW_DEBUGGING_INFO
27912 if (codeview_debuginfo_p ())
27913 codeview_begin_block (line, blocknum, block);
27914#endif
27915
27916 switch_to_section (current_function_section ());
27917 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27918}
27919
27920/* Output a marker (i.e. a label) for the end of the generated code for a
27921 lexical block. */
27922
27923static void
27924dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27925{
27926#ifdef CODEVIEW_DEBUGGING_INFO
27927 if (codeview_debuginfo_p ())
27928 codeview_end_block (line, blocknum);
27929#endif
27930
27931 switch_to_section (current_function_section ());
27932 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27933}
27934
27935/* Returns true if it is appropriate not to emit any debugging
27936 information for BLOCK, because it doesn't contain any instructions.
27937
27938 Don't allow this for blocks with nested functions or local classes
27939 as we would end up with orphans, and in the presence of scheduling
27940 we may end up calling them anyway. */
27941
27942static bool
27943dwarf2out_ignore_block (const_tree block)
27944{
27945 tree decl;
27946 unsigned int i;
27947
27948 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27949 if (TREE_CODE (decl) == FUNCTION_DECL
27950 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27951 return false;
27952 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27953 {
27954 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27955 if (TREE_CODE (decl) == FUNCTION_DECL
27956 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27957 return false;
27958 }
27959
27960 return true;
27961}
27962
27963/* Hash table routines for file_hash. */
27964
27965bool
27966dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27967{
27968 return filename_cmp (s1: p1->key, s2: p2) == 0;
27969}
27970
27971hashval_t
27972dwarf_file_hasher::hash (dwarf_file_data *p)
27973{
27974 return htab_hash_string (p->key);
27975}
27976
27977/* Lookup FILE_NAME (in the list of filenames that we know about here in
27978 dwarf2out.cc) and return its "index". The index of each (known) filename is
27979 just a unique number which is associated with only that one filename. We
27980 need such numbers for the sake of generating labels (in the .debug_sfnames
27981 section) and references to those files numbers (in the .debug_srcinfo
27982 and .debug_macinfo sections). If the filename given as an argument is not
27983 found in our current list, add it to the list and assign it the next
27984 available unique index number. */
27985
27986static struct dwarf_file_data *
27987lookup_filename (const char *file_name)
27988{
27989 struct dwarf_file_data * created;
27990
27991 if (!file_name)
27992 return NULL;
27993
27994 if (!file_name[0])
27995 file_name = "<stdin>";
27996
27997 dwarf_file_data **slot
27998 = file_table->find_slot_with_hash (comparable: file_name, hash: htab_hash_string (file_name),
27999 insert: INSERT);
28000 if (*slot)
28001 return *slot;
28002
28003 created = ggc_alloc<dwarf_file_data> ();
28004 created->key = file_name;
28005 created->filename = remap_debug_filename (file_name);
28006 created->emitted_number = 0;
28007 *slot = created;
28008 return created;
28009}
28010
28011/* If the assembler will construct the file table, then translate the compiler
28012 internal file table number into the assembler file table number, and emit
28013 a .file directive if we haven't already emitted one yet. The file table
28014 numbers are different because we prune debug info for unused variables and
28015 types, which may include filenames. */
28016
28017static int
28018maybe_emit_file (struct dwarf_file_data * fd)
28019{
28020 if (! fd->emitted_number)
28021 {
28022 if (last_emitted_file)
28023 fd->emitted_number = last_emitted_file->emitted_number + 1;
28024 else
28025 fd->emitted_number = 1;
28026 last_emitted_file = fd;
28027
28028 if (output_asm_line_debug_info ())
28029 {
28030 fprintf (stream: asm_out_file, format: "\t.file %u ", fd->emitted_number);
28031 output_quoted_string (asm_out_file, fd->filename);
28032 fputc (c: '\n', stream: asm_out_file);
28033 }
28034 }
28035
28036 return fd->emitted_number;
28037}
28038
28039/* Schedule generation of a DW_AT_const_value attribute to DIE.
28040 That generation should happen after function debug info has been
28041 generated. The value of the attribute is the constant value of ARG. */
28042
28043static void
28044append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
28045{
28046 die_arg_entry entry;
28047
28048 if (!die || !arg)
28049 return;
28050
28051 gcc_assert (early_dwarf);
28052
28053 if (!tmpl_value_parm_die_table)
28054 vec_alloc (v&: tmpl_value_parm_die_table, nelems: 32);
28055
28056 entry.die = die;
28057 entry.arg = arg;
28058 vec_safe_push (v&: tmpl_value_parm_die_table, obj: entry);
28059}
28060
28061/* Return TRUE if T is an instance of generic type, FALSE
28062 otherwise. */
28063
28064static bool
28065generic_type_p (tree t)
28066{
28067 if (t == NULL_TREE || !TYPE_P (t))
28068 return false;
28069 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
28070}
28071
28072/* Schedule the generation of the generic parameter dies for the
28073 instance of generic type T. The proper generation itself is later
28074 done by gen_scheduled_generic_parms_dies. */
28075
28076static void
28077schedule_generic_params_dies_gen (tree t)
28078{
28079 if (!generic_type_p (t))
28080 return;
28081
28082 gcc_assert (early_dwarf);
28083
28084 if (!generic_type_instances)
28085 vec_alloc (v&: generic_type_instances, nelems: 256);
28086
28087 vec_safe_push (v&: generic_type_instances, obj: t);
28088}
28089
28090/* Add a DW_AT_const_value attribute to DIEs that were scheduled
28091 by append_entry_to_tmpl_value_parm_die_table. This function must
28092 be called after function DIEs have been generated. */
28093
28094static void
28095gen_remaining_tmpl_value_param_die_attribute (void)
28096{
28097 if (tmpl_value_parm_die_table)
28098 {
28099 unsigned i, j;
28100 die_arg_entry *e;
28101
28102 /* We do this in two phases - first get the cases we can
28103 handle during early-finish, preserving those we cannot
28104 (containing symbolic constants where we don't yet know
28105 whether we are going to output the referenced symbols).
28106 For those we try again at late-finish. */
28107 j = 0;
28108 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
28109 {
28110 if (!e->die->removed
28111 && !tree_add_const_value_attribute (die: e->die, t: e->arg))
28112 {
28113 dw_loc_descr_ref loc = NULL;
28114 if (! early_dwarf
28115 && (dwarf_version >= 5 || !dwarf_strict))
28116 loc = loc_descriptor_from_tree (loc: e->arg, want_address: 2, NULL);
28117 if (loc)
28118 add_AT_loc (die: e->die, attr_kind: DW_AT_location, loc);
28119 else
28120 (*tmpl_value_parm_die_table)[j++] = *e;
28121 }
28122 }
28123 tmpl_value_parm_die_table->truncate (size: j);
28124 }
28125}
28126
28127/* Generate generic parameters DIEs for instances of generic types
28128 that have been previously scheduled by
28129 schedule_generic_params_dies_gen. This function must be called
28130 after all the types of the CU have been laid out. */
28131
28132static void
28133gen_scheduled_generic_parms_dies (void)
28134{
28135 unsigned i;
28136 tree t;
28137
28138 if (!generic_type_instances)
28139 return;
28140
28141 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
28142 if (COMPLETE_TYPE_P (t))
28143 gen_generic_params_dies (t);
28144
28145 generic_type_instances = NULL;
28146}
28147
28148
28149/* Replace DW_AT_name for the decl with name. */
28150
28151static void
28152dwarf2out_set_name (tree decl, tree name)
28153{
28154 dw_die_ref die;
28155 dw_attr_node *attr;
28156 const char *dname;
28157
28158 die = TYPE_SYMTAB_DIE (decl);
28159 if (!die)
28160 return;
28161
28162 dname = dwarf2_name (decl: name, scope: 0);
28163 if (!dname)
28164 return;
28165
28166 attr = get_AT (die, attr_kind: DW_AT_name);
28167 if (attr)
28168 {
28169 struct indirect_string_node *node;
28170
28171 node = find_AT_string (str: dname);
28172 /* replace the string. */
28173 attr->dw_attr_val.v.val_str = node;
28174 }
28175
28176 else
28177 add_name_attribute (die, name_string: dname);
28178}
28179
28180/* True if before or during processing of the first function being emitted. */
28181static bool in_first_function_p = true;
28182/* True if loc_note during dwarf2out_var_location call might still be
28183 before first real instruction at address equal to .Ltext0. */
28184static bool maybe_at_text_label_p = true;
28185/* One above highest N where .LVLN label might be equal to .Ltext0 label. */
28186static unsigned int first_loclabel_num_not_at_text_label;
28187
28188/* Look ahead for a real insn. */
28189
28190static rtx_insn *
28191dwarf2out_next_real_insn (rtx_insn *loc_note)
28192{
28193 rtx_insn *next_real = NEXT_INSN (insn: loc_note);
28194
28195 while (next_real)
28196 if (INSN_P (next_real))
28197 break;
28198 else
28199 next_real = NEXT_INSN (insn: next_real);
28200
28201 return next_real;
28202}
28203
28204/* Called by the final INSN scan whenever we see a var location. We
28205 use it to drop labels in the right places, and throw the location in
28206 our lookup table. */
28207
28208static void
28209dwarf2out_var_location (rtx_insn *loc_note)
28210{
28211 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28212 struct var_loc_node *newloc;
28213 rtx_insn *next_real;
28214 rtx_insn *call_insn = NULL;
28215 static const char *last_label;
28216 static const char *last_postcall_label;
28217 static bool last_in_cold_section_p;
28218 static rtx_insn *expected_next_loc_note;
28219 tree decl;
28220 bool var_loc_p;
28221 var_loc_view view = 0;
28222
28223 if (!NOTE_P (loc_note))
28224 {
28225 if (CALL_P (loc_note))
28226 {
28227 maybe_reset_location_view (insn: loc_note, table: cur_line_info_table);
28228 call_site_count++;
28229 if (SIBLING_CALL_P (loc_note))
28230 tail_call_site_count++;
28231 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28232 {
28233 call_insn = loc_note;
28234 loc_note = NULL;
28235 var_loc_p = false;
28236
28237 next_real = dwarf2out_next_real_insn (loc_note: call_insn);
28238 cached_next_real_insn = NULL;
28239 goto create_label;
28240 }
28241 if (optimize == 0 && !flag_var_tracking)
28242 {
28243 /* When the var-tracking pass is not running, there is no note
28244 for indirect calls whose target is compile-time known. In this
28245 case, process such calls specifically so that we generate call
28246 sites for them anyway. */
28247 rtx x = PATTERN (insn: loc_note);
28248 if (GET_CODE (x) == PARALLEL)
28249 x = XVECEXP (x, 0, 0);
28250 if (GET_CODE (x) == SET)
28251 x = SET_SRC (x);
28252 if (GET_CODE (x) == CALL)
28253 x = XEXP (x, 0);
28254 if (!MEM_P (x)
28255 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28256 || !SYMBOL_REF_DECL (XEXP (x, 0))
28257 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28258 != FUNCTION_DECL))
28259 {
28260 call_insn = loc_note;
28261 loc_note = NULL;
28262 var_loc_p = false;
28263
28264 next_real = dwarf2out_next_real_insn (loc_note: call_insn);
28265 cached_next_real_insn = NULL;
28266 goto create_label;
28267 }
28268 }
28269 }
28270 else if (!debug_variable_location_views)
28271 gcc_unreachable ();
28272 else
28273 maybe_reset_location_view (insn: loc_note, table: cur_line_info_table);
28274
28275 return;
28276 }
28277
28278 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28279 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28280 return;
28281
28282 /* Optimize processing a large consecutive sequence of location
28283 notes so we don't spend too much time in next_real_insn. If the
28284 next insn is another location note, remember the next_real_insn
28285 calculation for next time. */
28286 next_real = cached_next_real_insn;
28287 if (next_real)
28288 {
28289 if (expected_next_loc_note != loc_note)
28290 next_real = NULL;
28291 }
28292
28293 if (! next_real)
28294 next_real = dwarf2out_next_real_insn (loc_note);
28295
28296 if (next_real)
28297 {
28298 rtx_insn *next_note = NEXT_INSN (insn: loc_note);
28299 while (next_note != next_real)
28300 {
28301 if (! next_note->deleted ()
28302 && NOTE_P (next_note)
28303 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28304 break;
28305 next_note = NEXT_INSN (insn: next_note);
28306 }
28307
28308 if (next_note == next_real)
28309 cached_next_real_insn = NULL;
28310 else
28311 {
28312 expected_next_loc_note = next_note;
28313 cached_next_real_insn = next_real;
28314 }
28315 }
28316 else
28317 cached_next_real_insn = NULL;
28318
28319 /* If there are no instructions which would be affected by this note,
28320 don't do anything. */
28321 if (var_loc_p
28322 && next_real == NULL_RTX
28323 && !NOTE_DURING_CALL_P (loc_note))
28324 return;
28325
28326create_label:
28327
28328 if (next_real == NULL_RTX)
28329 next_real = get_last_insn ();
28330
28331 /* If there were any real insns between note we processed last time
28332 and this note (or if it is the first note), clear
28333 last_{,postcall_}label so that they are not reused this time. */
28334 if (last_var_location_insn == NULL_RTX
28335 || last_var_location_insn != next_real
28336 || last_in_cold_section_p != in_cold_section_p)
28337 {
28338 last_label = NULL;
28339 last_postcall_label = NULL;
28340 }
28341
28342 if (var_loc_p)
28343 {
28344 const char *label
28345 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28346 view = cur_line_info_table->view;
28347 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28348 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28349 if (newloc == NULL)
28350 return;
28351 }
28352 else
28353 {
28354 decl = NULL_TREE;
28355 newloc = NULL;
28356 }
28357
28358 /* If there were no real insns between note we processed last time
28359 and this note, use the label we emitted last time. Otherwise
28360 create a new label and emit it. */
28361 if (last_label == NULL)
28362 {
28363 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28364 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28365 loclabel_num++;
28366 last_label = ggc_strdup (loclabel);
28367 /* See if loclabel might be equal to .Ltext0. If yes,
28368 bump first_loclabel_num_not_at_text_label. */
28369 if (!have_multiple_function_sections
28370 && in_first_function_p
28371 && maybe_at_text_label_p)
28372 {
28373 static rtx_insn *last_start;
28374 rtx_insn *insn;
28375 for (insn = loc_note; insn; insn = previous_insn (insn))
28376 if (insn == last_start)
28377 break;
28378 else if (!NONDEBUG_INSN_P (insn))
28379 continue;
28380 else
28381 {
28382 rtx body = PATTERN (insn);
28383 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28384 continue;
28385 /* Inline asm could occupy zero bytes. */
28386 else if (GET_CODE (body) == ASM_INPUT
28387 || asm_noperands (body) >= 0)
28388 continue;
28389#ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28390 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28391 continue;
28392#endif
28393 else
28394 {
28395 /* Assume insn has non-zero length. */
28396 maybe_at_text_label_p = false;
28397 break;
28398 }
28399 }
28400 if (maybe_at_text_label_p)
28401 {
28402 last_start = loc_note;
28403 first_loclabel_num_not_at_text_label = loclabel_num;
28404 }
28405 }
28406 }
28407
28408 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28409 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28410
28411 if (!var_loc_p)
28412 {
28413 struct call_arg_loc_node *ca_loc
28414 = ggc_cleared_alloc<call_arg_loc_node> ();
28415 rtx_insn *prev = call_insn;
28416
28417 ca_loc->call_insn = call_insn;
28418 ca_loc->next = NULL;
28419 ca_loc->label = last_label;
28420 gcc_assert (prev
28421 && (CALL_P (prev)
28422 || (NONJUMP_INSN_P (prev)
28423 && GET_CODE (PATTERN (prev)) == SEQUENCE
28424 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28425 if (!CALL_P (prev))
28426 prev = as_a <rtx_sequence *> (p: PATTERN (insn: prev))->insn (index: 0);
28427 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28428
28429 /* Look for a SYMBOL_REF in the "prev" instruction. */
28430 rtx x = get_call_rtx_from (prev);
28431 if (x)
28432 {
28433 /* Try to get the call symbol, if any. */
28434 if (MEM_P (XEXP (x, 0)))
28435 x = XEXP (x, 0);
28436 /* First, look for a memory access to a symbol_ref. */
28437 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28438 && SYMBOL_REF_DECL (XEXP (x, 0))
28439 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28440 ca_loc->symbol_ref = XEXP (x, 0);
28441 /* Otherwise, look at a compile-time known user-level function
28442 declaration. */
28443 else if (MEM_P (x)
28444 && MEM_EXPR (x)
28445 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28446 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28447 }
28448
28449 ca_loc->block = insn_scope (prev);
28450 if (call_arg_locations)
28451 call_arg_loc_last->next = ca_loc;
28452 else
28453 call_arg_locations = ca_loc;
28454 call_arg_loc_last = ca_loc;
28455 }
28456 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28457 {
28458 newloc->label = last_label;
28459 newloc->view = view;
28460 }
28461 else
28462 {
28463 if (!last_postcall_label)
28464 {
28465 sprintf (s: loclabel, format: "%s-1", last_label);
28466 last_postcall_label = ggc_strdup (loclabel);
28467 }
28468 newloc->label = last_postcall_label;
28469 /* ??? This view is at last_label, not last_label-1, but we
28470 could only assume view at last_label-1 is zero if we could
28471 assume calls always have length greater than one. This is
28472 probably true in general, though there might be a rare
28473 exception to this rule, e.g. if a call insn is optimized out
28474 by target magic. Then, even the -1 in the label will be
28475 wrong, which might invalidate the range. Anyway, using view,
28476 though technically possibly incorrect, will work as far as
28477 ranges go: since L-1 is in the middle of the call insn,
28478 (L-1).0 and (L-1).V shouldn't make any difference, and having
28479 the loclist entry refer to the .loc entry might be useful, so
28480 leave it like this. */
28481 newloc->view = view;
28482 }
28483
28484 if (var_loc_p && flag_debug_asm)
28485 {
28486 const char *name, *sep, *patstr;
28487 if (decl && DECL_NAME (decl))
28488 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28489 else
28490 name = "";
28491 if (NOTE_VAR_LOCATION_LOC (loc_note))
28492 {
28493 sep = " => ";
28494 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28495 }
28496 else
28497 {
28498 sep = " ";
28499 patstr = "RESET";
28500 }
28501 fprintf (stream: asm_out_file, format: "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28502 name, sep, patstr);
28503 }
28504
28505 last_var_location_insn = next_real;
28506 last_in_cold_section_p = in_cold_section_p;
28507}
28508
28509/* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28510 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28511 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28512 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28513 BLOCK_FRAGMENT_ORIGIN links. */
28514static bool
28515block_within_block_p (tree block, tree outer, bool bothways)
28516{
28517 if (block == outer)
28518 return true;
28519
28520 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28521 for (tree context = BLOCK_SUPERCONTEXT (block);
28522 context != outer;
28523 context = BLOCK_SUPERCONTEXT (context))
28524 if (!context || TREE_CODE (context) != BLOCK)
28525 return false;
28526
28527 if (!bothways)
28528 return true;
28529
28530 /* Now check that each block is actually referenced by its
28531 parent. */
28532 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28533 context = BLOCK_SUPERCONTEXT (context))
28534 {
28535 if (BLOCK_FRAGMENT_ORIGIN (context))
28536 {
28537 gcc_assert (!BLOCK_SUBBLOCKS (context));
28538 context = BLOCK_FRAGMENT_ORIGIN (context);
28539 }
28540 for (tree sub = BLOCK_SUBBLOCKS (context);
28541 sub != block;
28542 sub = BLOCK_CHAIN (sub))
28543 if (!sub)
28544 return false;
28545 if (context == outer)
28546 return true;
28547 else
28548 block = context;
28549 }
28550}
28551
28552/* Called during final while assembling the marker of the entry point
28553 for an inlined function. */
28554
28555static void
28556dwarf2out_inline_entry (tree block)
28557{
28558 gcc_assert (debug_inline_points);
28559
28560 /* If we can't represent it, don't bother. */
28561 if (!(dwarf_version >= 3 || !dwarf_strict))
28562 return;
28563
28564 gcc_assert (DECL_P (block_ultimate_origin (block)));
28565
28566 /* Sanity check the block tree. This would catch a case in which
28567 BLOCK got removed from the tree reachable from the outermost
28568 lexical block, but got retained in markers. It would still link
28569 back to its parents, but some ancestor would be missing a link
28570 down the path to the sub BLOCK. If the block got removed, its
28571 BLOCK_NUMBER will not be a usable value. */
28572 if (flag_checking)
28573 gcc_assert (block_within_block_p (block,
28574 DECL_INITIAL (current_function_decl),
28575 true));
28576
28577 gcc_assert (inlined_function_outer_scope_p (block));
28578 gcc_assert (!lookup_block_die (block));
28579
28580 if (BLOCK_FRAGMENT_ORIGIN (block))
28581 block = BLOCK_FRAGMENT_ORIGIN (block);
28582 /* Can the entry point ever not be at the beginning of an
28583 unfragmented lexical block? */
28584 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28585 || (cur_line_info_table
28586 && !ZERO_VIEW_P (cur_line_info_table->view))))
28587 return;
28588
28589 if (!inline_entry_data_table)
28590 inline_entry_data_table
28591 = hash_table<inline_entry_data_hasher>::create_ggc (n: 10);
28592
28593
28594 inline_entry_data **iedp
28595 = inline_entry_data_table->find_slot_with_hash (comparable: block,
28596 hash: htab_hash_pointer (block),
28597 insert: INSERT);
28598 if (*iedp)
28599 /* ??? Ideally, we'd record all entry points for the same inlined
28600 function (some may have been duplicated by e.g. unrolling), but
28601 we have no way to represent that ATM. */
28602 return;
28603
28604 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28605 ied->block = block;
28606 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28607 ied->label_num = BLOCK_NUMBER (block);
28608 if (cur_line_info_table)
28609 ied->view = cur_line_info_table->view;
28610
28611 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28612 BLOCK_NUMBER (block));
28613}
28614
28615/* Called from finalize_size_functions for size functions so that their body
28616 can be encoded in the debug info to describe the layout of variable-length
28617 structures. */
28618
28619static void
28620dwarf2out_size_function (tree decl)
28621{
28622 set_early_dwarf s;
28623 function_to_dwarf_procedure (fndecl: decl);
28624}
28625
28626/* Note in one location list that text section has changed. */
28627
28628int
28629var_location_switch_text_section_1 (var_loc_list **slot, void *)
28630{
28631 var_loc_list *list = *slot;
28632 if (list->first)
28633 list->last_before_switch
28634 = list->last->next ? list->last->next : list->last;
28635 return 1;
28636}
28637
28638/* Note in all location lists that text section has changed. */
28639
28640static void
28641var_location_switch_text_section (void)
28642{
28643 if (decl_loc_table == NULL)
28644 return;
28645
28646 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28647}
28648
28649/* Create a new line number table. */
28650
28651static dw_line_info_table *
28652new_line_info_table (void)
28653{
28654 dw_line_info_table *table;
28655
28656 table = ggc_cleared_alloc<dw_line_info_table> ();
28657 table->file_num = 1;
28658 table->line_num = 1;
28659 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28660 FORCE_RESET_NEXT_VIEW (table->view);
28661 table->symviews_since_reset = 0;
28662
28663 return table;
28664}
28665
28666/* Lookup the "current" table into which we emit line info, so
28667 that we don't have to do it for every source line. */
28668
28669static void
28670set_cur_line_info_table (section *sec)
28671{
28672 dw_line_info_table *table;
28673
28674 if (sec == text_section)
28675 table = text_section_line_info;
28676 else if (sec == cold_text_section)
28677 {
28678 table = cold_text_section_line_info;
28679 if (!table)
28680 {
28681 cold_text_section_line_info = table = new_line_info_table ();
28682 table->end_label = cold_end_label;
28683 }
28684 }
28685 else
28686 {
28687 const char *end_label;
28688
28689 if (crtl->has_bb_partition)
28690 {
28691 if (in_cold_section_p)
28692 end_label = crtl->subsections.cold_section_end_label;
28693 else
28694 end_label = crtl->subsections.hot_section_end_label;
28695 }
28696 else
28697 {
28698 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28699 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28700 current_function_funcdef_no);
28701 end_label = ggc_strdup (label);
28702 }
28703
28704 table = new_line_info_table ();
28705 table->end_label = end_label;
28706
28707 vec_safe_push (v&: separate_line_info, obj: table);
28708 }
28709
28710 if (output_asm_line_debug_info ())
28711 table->is_stmt = (cur_line_info_table
28712 ? cur_line_info_table->is_stmt
28713 : DWARF_LINE_DEFAULT_IS_STMT_START);
28714 cur_line_info_table = table;
28715}
28716
28717
28718/* We need to reset the locations at the beginning of each
28719 function. We can't do this in the end_function hook, because the
28720 declarations that use the locations won't have been output when
28721 that hook is called. Also compute have_multiple_function_sections here. */
28722
28723static void
28724dwarf2out_begin_function (tree fun)
28725{
28726 section *sec = function_section (fun);
28727
28728 if (sec != text_section)
28729 have_multiple_function_sections = true;
28730
28731 if (crtl->has_bb_partition && !cold_text_section)
28732 {
28733 gcc_assert (current_function_decl == fun);
28734 cold_text_section = unlikely_text_section ();
28735 switch_to_section (cold_text_section);
28736 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28737 switch_to_section (sec);
28738 }
28739
28740 call_site_count = 0;
28741 tail_call_site_count = 0;
28742
28743 set_cur_line_info_table (sec);
28744 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28745}
28746
28747/* Helper function of dwarf2out_end_function, called only after emitting
28748 the very first function into assembly. Check if some .debug_loc range
28749 might end with a .LVL* label that could be equal to .Ltext0.
28750 In that case we must force using absolute addresses in .debug_loc ranges,
28751 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28752 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28753 list terminator.
28754 Set have_multiple_function_sections to true in that case and
28755 terminate htab traversal. */
28756
28757int
28758find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28759{
28760 var_loc_list *entry = *slot;
28761 struct var_loc_node *node;
28762
28763 node = entry->first;
28764 if (node && node->next && node->next->label)
28765 {
28766 unsigned int i;
28767 const char *label = node->next->label;
28768 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28769
28770 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28771 {
28772 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28773 if (strcmp (s1: label, s2: loclabel) == 0)
28774 {
28775 have_multiple_function_sections = true;
28776 return 0;
28777 }
28778 }
28779 }
28780 return 1;
28781}
28782
28783/* Hook called after emitting a function into assembly.
28784 This does something only for the very first function emitted. */
28785
28786static void
28787dwarf2out_end_function (unsigned int)
28788{
28789 if (in_first_function_p
28790 && !have_multiple_function_sections
28791 && first_loclabel_num_not_at_text_label
28792 && decl_loc_table)
28793 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (argument: 0);
28794 in_first_function_p = false;
28795 maybe_at_text_label_p = false;
28796}
28797
28798/* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28799 front-ends register a translation unit even before dwarf2out_init is
28800 called. */
28801static tree main_translation_unit = NULL_TREE;
28802
28803/* Hook called by front-ends after they built their main translation unit.
28804 Associate comp_unit_die to UNIT. */
28805
28806static void
28807dwarf2out_register_main_translation_unit (tree unit)
28808{
28809 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28810 && main_translation_unit == NULL_TREE);
28811 main_translation_unit = unit;
28812 /* If dwarf2out_init has not been called yet, it will perform the association
28813 itself looking at main_translation_unit. */
28814 if (decl_die_table != NULL)
28815 equate_decl_number_to_die (decl: unit, decl_die: comp_unit_die ());
28816}
28817
28818/* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28819
28820static void
28821push_dw_line_info_entry (dw_line_info_table *table,
28822 enum dw_line_info_opcode opcode, unsigned int val)
28823{
28824 dw_line_info_entry e;
28825 e.opcode = opcode;
28826 e.val = val;
28827 vec_safe_push (v&: table->entries, obj: e);
28828}
28829
28830/* Output a label to mark the beginning of a source code line entry
28831 and record information relating to this source line, in
28832 'line_info_table' for later output of the .debug_line section. */
28833/* ??? The discriminator parameter ought to be unsigned. */
28834
28835static void
28836dwarf2out_source_line (unsigned int line, unsigned int column,
28837 const char *filename,
28838 int discriminator, bool is_stmt)
28839{
28840 unsigned int file_num;
28841 dw_line_info_table *table;
28842 static var_loc_view lvugid;
28843
28844#ifdef CODEVIEW_DEBUGGING_INFO
28845 if (codeview_debuginfo_p ())
28846 codeview_source_line (line, filename);
28847#endif
28848
28849 /* 'line_info_table' information gathering is not needed when the debug
28850 info level is set to the lowest value. Also, the current DWARF-based
28851 debug formats do not use this info. */
28852 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28853 return;
28854
28855 table = cur_line_info_table;
28856
28857 if (line == 0)
28858 {
28859 if (debug_variable_location_views
28860 && output_asm_line_debug_info ()
28861 && table && !RESETTING_VIEW_P (table->view))
28862 {
28863 /* If we're using the assembler to compute view numbers, we
28864 can't issue a .loc directive for line zero, so we can't
28865 get a view number at this point. We might attempt to
28866 compute it from the previous view, or equate it to a
28867 subsequent view (though it might not be there!), but
28868 since we're omitting the line number entry, we might as
28869 well omit the view number as well. That means pretending
28870 it's a view number zero, which might very well turn out
28871 to be correct. ??? Extend the assembler so that the
28872 compiler could emit e.g. ".locview .LVU#", to output a
28873 view without changing line number information. We'd then
28874 have to count it in symviews_since_reset; when it's omitted,
28875 it doesn't count. */
28876 if (!zero_view_p)
28877 zero_view_p = BITMAP_GGC_ALLOC ();
28878 bitmap_set_bit (zero_view_p, table->view);
28879 if (flag_debug_asm)
28880 {
28881 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28882 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28883 fprintf (stream: asm_out_file, format: "\t%s line 0, omitted view ",
28884 ASM_COMMENT_START);
28885 assemble_name (asm_out_file, label);
28886 putc (c: '\n', stream: asm_out_file);
28887 }
28888 table->view = ++lvugid;
28889 }
28890 return;
28891 }
28892
28893 /* The discriminator column was added in dwarf4. Simplify the below
28894 by simply removing it if we're not supposed to output it. */
28895 if (dwarf_version < 4 && dwarf_strict)
28896 discriminator = 0;
28897
28898 if (!debug_column_info)
28899 column = 0;
28900
28901 file_num = maybe_emit_file (fd: lookup_filename (file_name: filename));
28902
28903 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28904 the debugger has used the second (possibly duplicate) line number
28905 at the beginning of the function to mark the end of the prologue.
28906 We could eliminate any other duplicates within the function. For
28907 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28908 that second line number entry. */
28909 /* Recall that this end-of-prologue indication is *not* the same thing
28910 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28911 to which the hook corresponds, follows the last insn that was
28912 emitted by gen_prologue. What we need is to precede the first insn
28913 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28914 insn that corresponds to something the user wrote. These may be
28915 very different locations once scheduling is enabled. */
28916
28917 if (0 && file_num == table->file_num
28918 && line == table->line_num
28919 && column == table->column_num
28920 && discriminator == table->discrim_num
28921 && is_stmt == table->is_stmt)
28922 return;
28923
28924 switch_to_section (current_function_section ());
28925
28926 /* If requested, emit something human-readable. */
28927 if (flag_debug_asm)
28928 {
28929 if (debug_column_info)
28930 fprintf (stream: asm_out_file, format: "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28931 filename, line, column);
28932 else
28933 fprintf (stream: asm_out_file, format: "\t%s %s:%d\n", ASM_COMMENT_START,
28934 filename, line);
28935 }
28936
28937 if (output_asm_line_debug_info ())
28938 {
28939 /* Emit the .loc directive understood by GNU as. */
28940 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28941 file_num, line, is_stmt, discriminator */
28942 fputs (s: "\t.loc ", stream: asm_out_file);
28943 fprint_ul (asm_out_file, file_num);
28944 putc (c: ' ', stream: asm_out_file);
28945 fprint_ul (asm_out_file, line);
28946 putc (c: ' ', stream: asm_out_file);
28947 fprint_ul (asm_out_file, column);
28948
28949 if (is_stmt != table->is_stmt)
28950 {
28951#if HAVE_GAS_LOC_STMT
28952 fputs (s: " is_stmt ", stream: asm_out_file);
28953 putc (c: is_stmt ? '1' : '0', stream: asm_out_file);
28954#endif
28955 }
28956 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28957 {
28958 gcc_assert (discriminator > 0);
28959 fputs (s: " discriminator ", stream: asm_out_file);
28960 fprint_ul (asm_out_file, (unsigned long) discriminator);
28961 }
28962 if (debug_variable_location_views)
28963 {
28964 if (!RESETTING_VIEW_P (table->view))
28965 {
28966 table->symviews_since_reset++;
28967 if (table->symviews_since_reset > symview_upper_bound)
28968 symview_upper_bound = table->symviews_since_reset;
28969 /* When we're using the assembler to compute view
28970 numbers, we output symbolic labels after "view" in
28971 .loc directives, and the assembler will set them for
28972 us, so that we can refer to the view numbers in
28973 location lists. The only exceptions are when we know
28974 a view will be zero: "-0" is a forced reset, used
28975 e.g. in the beginning of functions, whereas "0" tells
28976 the assembler to check that there was a PC change
28977 since the previous view, in a way that implicitly
28978 resets the next view. */
28979 fputs (s: " view ", stream: asm_out_file);
28980 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28981 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28982 assemble_name (asm_out_file, label);
28983 table->view = ++lvugid;
28984 }
28985 else
28986 {
28987 table->symviews_since_reset = 0;
28988 if (FORCE_RESETTING_VIEW_P (table->view))
28989 fputs (s: " view -0", stream: asm_out_file);
28990 else
28991 fputs (s: " view 0", stream: asm_out_file);
28992 /* Mark the present view as a zero view. Earlier debug
28993 binds may have already added its id to loclists to be
28994 emitted later, so we can't reuse the id for something
28995 else. However, it's good to know whether a view is
28996 known to be zero, because then we may be able to
28997 optimize out locviews that are all zeros, so take
28998 note of it in zero_view_p. */
28999 if (!zero_view_p)
29000 zero_view_p = BITMAP_GGC_ALLOC ();
29001 bitmap_set_bit (zero_view_p, lvugid);
29002 table->view = ++lvugid;
29003 }
29004 }
29005 putc (c: '\n', stream: asm_out_file);
29006 }
29007 else
29008 {
29009 unsigned int label_num = ++line_info_label_num;
29010
29011 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
29012
29013 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
29014 push_dw_line_info_entry (table, opcode: LI_adv_address, val: label_num);
29015 else
29016 push_dw_line_info_entry (table, opcode: LI_set_address, val: label_num);
29017 if (debug_variable_location_views)
29018 {
29019 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
29020 if (resetting)
29021 table->view = 0;
29022
29023 if (flag_debug_asm)
29024 fprintf (stream: asm_out_file, format: "\t%s view %s%d\n",
29025 ASM_COMMENT_START,
29026 resetting ? "-" : "",
29027 table->view);
29028
29029 table->view++;
29030 }
29031 if (file_num != table->file_num)
29032 push_dw_line_info_entry (table, opcode: LI_set_file, val: file_num);
29033 if (discriminator != table->discrim_num)
29034 push_dw_line_info_entry (table, opcode: LI_set_discriminator, val: discriminator);
29035 if (is_stmt != table->is_stmt)
29036 push_dw_line_info_entry (table, opcode: LI_negate_stmt, val: 0);
29037 push_dw_line_info_entry (table, opcode: LI_set_line, val: line);
29038 if (debug_column_info)
29039 push_dw_line_info_entry (table, opcode: LI_set_column, val: column);
29040 }
29041
29042 table->file_num = file_num;
29043 table->line_num = line;
29044 table->column_num = column;
29045 table->discrim_num = discriminator;
29046 table->is_stmt = is_stmt;
29047 table->in_use = true;
29048}
29049
29050/* Record a source file location for a DECL_IGNORED_P function. */
29051
29052static void
29053dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
29054 const char *filename)
29055{
29056 dw_fde_ref fde = cfun->fde;
29057
29058 fde->ignored_debug = false;
29059 set_cur_line_info_table (current_function_section ());
29060
29061 dwarf2out_source_line (line, column, filename, discriminator: 0, is_stmt: true);
29062}
29063
29064/* Record the beginning of a new source file. */
29065
29066static void
29067dwarf2out_start_source_file (unsigned int lineno, const char *filename)
29068{
29069#ifdef CODEVIEW_DEBUGGING_INFO
29070 if (codeview_debuginfo_p ())
29071 codeview_start_source_file (filename);
29072#endif
29073
29074 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29075 {
29076 macinfo_entry e;
29077 e.code = DW_MACINFO_start_file;
29078 e.lineno = lineno;
29079 e.info = ggc_strdup (filename);
29080 vec_safe_push (v&: macinfo_table, obj: e);
29081 }
29082}
29083
29084/* Record the end of a source file. */
29085
29086static void
29087dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
29088{
29089 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29090 {
29091 macinfo_entry e;
29092 e.code = DW_MACINFO_end_file;
29093 e.lineno = lineno;
29094 e.info = NULL;
29095 vec_safe_push (v&: macinfo_table, obj: e);
29096 }
29097}
29098
29099/* Called from debug_define in toplev.cc. The `buffer' parameter contains
29100 the tail part of the directive line, i.e. the part which is past the
29101 initial whitespace, #, whitespace, directive-name, whitespace part. */
29102
29103static void
29104dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
29105 const char *buffer ATTRIBUTE_UNUSED)
29106{
29107 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29108 {
29109 macinfo_entry e;
29110 /* Insert a dummy first entry to be able to optimize the whole
29111 predefined macro block using DW_MACRO_import. */
29112 if (macinfo_table->is_empty () && lineno <= 1)
29113 {
29114 e.code = 0;
29115 e.lineno = 0;
29116 e.info = NULL;
29117 vec_safe_push (v&: macinfo_table, obj: e);
29118 }
29119 e.code = DW_MACINFO_define;
29120 e.lineno = lineno;
29121 e.info = ggc_strdup (buffer);
29122 vec_safe_push (v&: macinfo_table, obj: e);
29123 }
29124}
29125
29126/* Called from debug_undef in toplev.cc. The `buffer' parameter contains
29127 the tail part of the directive line, i.e. the part which is past the
29128 initial whitespace, #, whitespace, directive-name, whitespace part. */
29129
29130static void
29131dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
29132 const char *buffer ATTRIBUTE_UNUSED)
29133{
29134 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29135 {
29136 macinfo_entry e;
29137 /* Insert a dummy first entry to be able to optimize the whole
29138 predefined macro block using DW_MACRO_import. */
29139 if (macinfo_table->is_empty () && lineno <= 1)
29140 {
29141 e.code = 0;
29142 e.lineno = 0;
29143 e.info = NULL;
29144 vec_safe_push (v&: macinfo_table, obj: e);
29145 }
29146 e.code = DW_MACINFO_undef;
29147 e.lineno = lineno;
29148 e.info = ggc_strdup (buffer);
29149 vec_safe_push (v&: macinfo_table, obj: e);
29150 }
29151}
29152
29153/* Helpers to manipulate hash table of CUs. */
29154
29155struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
29156{
29157 static inline hashval_t hash (const macinfo_entry *);
29158 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
29159};
29160
29161inline hashval_t
29162macinfo_entry_hasher::hash (const macinfo_entry *entry)
29163{
29164 return htab_hash_string (entry->info);
29165}
29166
29167inline bool
29168macinfo_entry_hasher::equal (const macinfo_entry *entry1,
29169 const macinfo_entry *entry2)
29170{
29171 return !strcmp (s1: entry1->info, s2: entry2->info);
29172}
29173
29174typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
29175
29176/* Output a single .debug_macinfo entry. */
29177
29178static void
29179output_macinfo_op (macinfo_entry *ref)
29180{
29181 int file_num;
29182 size_t len;
29183 struct indirect_string_node *node;
29184 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29185 struct dwarf_file_data *fd;
29186
29187 switch (ref->code)
29188 {
29189 case DW_MACINFO_start_file:
29190 fd = lookup_filename (file_name: ref->info);
29191 file_num = maybe_emit_file (fd);
29192 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
29193 dw2_asm_output_data_uleb128 (ref->lineno,
29194 "Included from line number "
29195 HOST_WIDE_INT_PRINT_UNSIGNED,
29196 ref->lineno);
29197 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
29198 break;
29199 case DW_MACINFO_end_file:
29200 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
29201 break;
29202 case DW_MACINFO_define:
29203 case DW_MACINFO_undef:
29204 len = strlen (s: ref->info) + 1;
29205 if ((!dwarf_strict || dwarf_version >= 5)
29206 && len > (size_t) dwarf_offset_size
29207 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29208 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29209 {
29210 if (dwarf_split_debug_info)
29211 ref->code = ref->code == DW_MACINFO_define
29212 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29213 else
29214 ref->code = ref->code == DW_MACINFO_define
29215 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29216 output_macinfo_op (ref);
29217 return;
29218 }
29219 dw2_asm_output_data (1, ref->code,
29220 ref->code == DW_MACINFO_define
29221 ? "Define macro" : "Undefine macro");
29222 dw2_asm_output_data_uleb128 (ref->lineno,
29223 "At line number "
29224 HOST_WIDE_INT_PRINT_UNSIGNED,
29225 ref->lineno);
29226 dw2_asm_output_nstring (ref->info, -1, "The macro");
29227 break;
29228 case DW_MACRO_define_strp:
29229 dw2_asm_output_data (1, ref->code, "Define macro strp");
29230 goto do_DW_MACRO_define_strpx;
29231 case DW_MACRO_undef_strp:
29232 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29233 goto do_DW_MACRO_define_strpx;
29234 case DW_MACRO_define_strx:
29235 dw2_asm_output_data (1, ref->code, "Define macro strx");
29236 goto do_DW_MACRO_define_strpx;
29237 case DW_MACRO_undef_strx:
29238 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29239 /* FALLTHRU */
29240 do_DW_MACRO_define_strpx:
29241 /* NB: dwarf2out_finish performs:
29242 1. save_macinfo_strings
29243 2. hash table traverse of index_string
29244 3. output_macinfo -> output_macinfo_op
29245 4. output_indirect_strings
29246 -> hash table traverse of output_index_string
29247
29248 When output_macinfo_op is called, all index strings have been
29249 added to hash table by save_macinfo_strings and we can't pass
29250 INSERT to find_slot_with_hash which may expand hash table, even
29251 if no insertion is needed, and change hash table traverse order
29252 between index_string and output_index_string. */
29253 node = find_AT_string (str: ref->info, insert: NO_INSERT);
29254 gcc_assert (node
29255 && (node->form == DW_FORM_strp
29256 || node->form == dwarf_FORM (DW_FORM_strx)));
29257 dw2_asm_output_data_uleb128 (ref->lineno,
29258 "At line number "
29259 HOST_WIDE_INT_PRINT_UNSIGNED,
29260 ref->lineno);
29261 if (node->form == DW_FORM_strp)
29262 {
29263 gcc_assert (ref->code == DW_MACRO_define_strp
29264 || ref->code == DW_MACRO_undef_strp);
29265 dw2_asm_output_offset (dwarf_offset_size, node->label,
29266 debug_str_section, "The macro: \"%s\"",
29267 ref->info);
29268 }
29269 else
29270 {
29271 gcc_assert (ref->code == DW_MACRO_define_strx
29272 || ref->code == DW_MACRO_undef_strx);
29273 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29274 ref->info);
29275 }
29276 break;
29277 case DW_MACRO_import:
29278 dw2_asm_output_data (1, ref->code, "Import");
29279 ASM_GENERATE_INTERNAL_LABEL (label,
29280 DEBUG_MACRO_SECTION_LABEL,
29281 ref->lineno + macinfo_label_base);
29282 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29283 break;
29284 default:
29285 fprintf (stream: asm_out_file, format: "%s unrecognized macinfo code %lu\n",
29286 ASM_COMMENT_START, (unsigned long) ref->code);
29287 break;
29288 }
29289}
29290
29291/* Attempt to make a sequence of define/undef macinfo ops shareable with
29292 other compilation unit .debug_macinfo sections. IDX is the first
29293 index of a define/undef, return the number of ops that should be
29294 emitted in a comdat .debug_macinfo section and emit
29295 a DW_MACRO_import entry referencing it.
29296 If the define/undef entry should be emitted normally, return 0. */
29297
29298static unsigned
29299optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29300 macinfo_hash_type **macinfo_htab)
29301{
29302 macinfo_entry *first, *second, *cur, *inc;
29303 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29304 unsigned char checksum[16];
29305 struct md5_ctx ctx;
29306 char *grp_name, *tail;
29307 const char *base;
29308 unsigned int i, count, encoded_filename_len, linebuf_len;
29309 macinfo_entry **slot;
29310
29311 first = &(*macinfo_table)[idx];
29312 second = &(*macinfo_table)[idx + 1];
29313
29314 /* Optimize only if there are at least two consecutive define/undef ops,
29315 and either all of them are before first DW_MACINFO_start_file
29316 with lineno {0,1} (i.e. predefined macro block), or all of them are
29317 in some included header file. */
29318 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29319 return 0;
29320 if (vec_safe_is_empty (v: files))
29321 {
29322 if (first->lineno > 1 || second->lineno > 1)
29323 return 0;
29324 }
29325 else if (first->lineno == 0)
29326 return 0;
29327
29328 /* Find the last define/undef entry that can be grouped together
29329 with first and at the same time compute md5 checksum of their
29330 codes, linenumbers and strings. */
29331 md5_init_ctx (ctx: &ctx);
29332 for (i = idx; macinfo_table->iterate (ix: i, ptr: &cur); i++)
29333 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29334 break;
29335 else if (vec_safe_is_empty (v: files) && cur->lineno > 1)
29336 break;
29337 else
29338 {
29339 unsigned char code = cur->code;
29340 md5_process_bytes (buffer: &code, len: 1, ctx: &ctx);
29341 checksum_uleb128 (value: cur->lineno, ctx: &ctx);
29342 md5_process_bytes (buffer: cur->info, len: strlen (s: cur->info) + 1, ctx: &ctx);
29343 }
29344 md5_finish_ctx (ctx: &ctx, resbuf: checksum);
29345 count = i - idx;
29346
29347 /* From the containing include filename (if any) pick up just
29348 usable characters from its basename. */
29349 if (vec_safe_is_empty (v: files))
29350 base = "";
29351 else
29352 base = lbasename (files->last ().info);
29353 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29354 if (ISIDNUM (base[i]) || base[i] == '.')
29355 encoded_filename_len++;
29356 /* Count . at the end. */
29357 if (encoded_filename_len)
29358 encoded_filename_len++;
29359
29360 sprintf (s: linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29361 linebuf_len = strlen (s: linebuf);
29362
29363 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29364 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29365 + 16 * 2 + 1);
29366 memcpy (dest: grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", n: 4);
29367 tail = grp_name + 4;
29368 if (encoded_filename_len)
29369 {
29370 for (i = 0; base[i]; i++)
29371 if (ISIDNUM (base[i]) || base[i] == '.')
29372 *tail++ = base[i];
29373 *tail++ = '.';
29374 }
29375 memcpy (dest: tail, src: linebuf, n: linebuf_len);
29376 tail += linebuf_len;
29377 *tail++ = '.';
29378 for (i = 0; i < 16; i++)
29379 sprintf (s: tail + i * 2, format: "%02x", checksum[i] & 0xff);
29380
29381 /* Construct a macinfo_entry for DW_MACRO_import
29382 in the empty vector entry before the first define/undef. */
29383 inc = &(*macinfo_table)[idx - 1];
29384 inc->code = DW_MACRO_import;
29385 inc->lineno = 0;
29386 inc->info = ggc_strdup (grp_name);
29387 if (!*macinfo_htab)
29388 *macinfo_htab = new macinfo_hash_type (10);
29389 /* Avoid emitting duplicates. */
29390 slot = (*macinfo_htab)->find_slot (value: inc, insert: INSERT);
29391 if (*slot != NULL)
29392 {
29393 inc->code = 0;
29394 inc->info = NULL;
29395 /* If such an entry has been used before, just emit
29396 a DW_MACRO_import op. */
29397 inc = *slot;
29398 output_macinfo_op (ref: inc);
29399 /* And clear all macinfo_entry in the range to avoid emitting them
29400 in the second pass. */
29401 for (i = idx; macinfo_table->iterate (ix: i, ptr: &cur) && i < idx + count; i++)
29402 {
29403 cur->code = 0;
29404 cur->info = NULL;
29405 }
29406 }
29407 else
29408 {
29409 *slot = inc;
29410 inc->lineno = (*macinfo_htab)->elements ();
29411 output_macinfo_op (ref: inc);
29412 }
29413 return count;
29414}
29415
29416/* Save any strings needed by the macinfo table in the debug str
29417 table. All strings must be collected into the table by the time
29418 index_string is called. */
29419
29420static void
29421save_macinfo_strings (void)
29422{
29423 unsigned len;
29424 unsigned i;
29425 macinfo_entry *ref;
29426
29427 for (i = 0; macinfo_table && macinfo_table->iterate (ix: i, ptr: &ref); i++)
29428 {
29429 switch (ref->code)
29430 {
29431 /* Match the logic in output_macinfo_op to decide on
29432 indirect strings. */
29433 case DW_MACINFO_define:
29434 case DW_MACINFO_undef:
29435 len = strlen (s: ref->info) + 1;
29436 if ((!dwarf_strict || dwarf_version >= 5)
29437 && len > (unsigned) dwarf_offset_size
29438 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29439 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29440 set_indirect_string (find_AT_string (str: ref->info));
29441 break;
29442 case DW_MACINFO_start_file:
29443 /* -gsplit-dwarf -g3 will also output filename as indirect
29444 string. */
29445 if (!dwarf_split_debug_info)
29446 break;
29447 /* Fall through. */
29448 case DW_MACRO_define_strp:
29449 case DW_MACRO_undef_strp:
29450 case DW_MACRO_define_strx:
29451 case DW_MACRO_undef_strx:
29452 set_indirect_string (find_AT_string (str: ref->info));
29453 break;
29454 default:
29455 break;
29456 }
29457 }
29458}
29459
29460/* Output macinfo section(s). */
29461
29462static void
29463output_macinfo (const char *debug_line_label, bool early_lto_debug)
29464{
29465 unsigned i;
29466 unsigned long length = vec_safe_length (v: macinfo_table);
29467 macinfo_entry *ref;
29468 vec<macinfo_entry, va_gc> *files = NULL;
29469 macinfo_hash_type *macinfo_htab = NULL;
29470 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29471
29472 if (! length)
29473 return;
29474
29475 /* output_macinfo* uses these interchangeably. */
29476 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29477 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29478 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29479 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29480
29481 /* AIX Assembler inserts the length, so adjust the reference to match the
29482 offset expected by debuggers. */
29483 strcpy (dest: dl_section_ref, src: debug_line_label);
29484 if (XCOFF_DEBUGGING_INFO)
29485 strcat (dest: dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29486
29487 /* For .debug_macro emit the section header. */
29488 if (!dwarf_strict || dwarf_version >= 5)
29489 {
29490 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29491 "DWARF macro version number");
29492 if (dwarf_offset_size == 8)
29493 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29494 else
29495 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29496 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29497 debug_line_section, NULL);
29498 }
29499
29500 /* In the first loop, it emits the primary .debug_macinfo section
29501 and after each emitted op the macinfo_entry is cleared.
29502 If a longer range of define/undef ops can be optimized using
29503 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29504 the vector before the first define/undef in the range and the
29505 whole range of define/undef ops is not emitted and kept. */
29506 for (i = 0; macinfo_table->iterate (ix: i, ptr: &ref); i++)
29507 {
29508 switch (ref->code)
29509 {
29510 case DW_MACINFO_start_file:
29511 vec_safe_push (v&: files, obj: *ref);
29512 break;
29513 case DW_MACINFO_end_file:
29514 if (!vec_safe_is_empty (v: files))
29515 files->pop ();
29516 break;
29517 case DW_MACINFO_define:
29518 case DW_MACINFO_undef:
29519 if ((!dwarf_strict || dwarf_version >= 5)
29520 && !dwarf_split_debug_info
29521 && HAVE_COMDAT_GROUP
29522 && vec_safe_length (v: files) != 1
29523 && i > 0
29524 && i + 1 < length
29525 && (*macinfo_table)[i - 1].code == 0)
29526 {
29527 unsigned count = optimize_macinfo_range (idx: i, files, macinfo_htab: &macinfo_htab);
29528 if (count)
29529 {
29530 i += count - 1;
29531 continue;
29532 }
29533 }
29534 break;
29535 case 0:
29536 /* A dummy entry may be inserted at the beginning to be able
29537 to optimize the whole block of predefined macros. */
29538 if (i == 0)
29539 continue;
29540 default:
29541 break;
29542 }
29543 output_macinfo_op (ref);
29544 ref->info = NULL;
29545 ref->code = 0;
29546 }
29547
29548 if (!macinfo_htab)
29549 return;
29550
29551 /* Save the number of transparent includes so we can adjust the
29552 label number for the fat LTO object DWARF. */
29553 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29554
29555 delete macinfo_htab;
29556 macinfo_htab = NULL;
29557
29558 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29559 terminate the current chain and switch to a new comdat .debug_macinfo
29560 section and emit the define/undef entries within it. */
29561 for (i = 0; macinfo_table->iterate (ix: i, ptr: &ref); i++)
29562 switch (ref->code)
29563 {
29564 case 0:
29565 continue;
29566 case DW_MACRO_import:
29567 {
29568 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29569 tree comdat_key = get_identifier (ref->info);
29570 /* Terminate the previous .debug_macinfo section. */
29571 dw2_asm_output_data (1, 0, "End compilation unit");
29572 targetm.asm_out.named_section (debug_macinfo_section_name,
29573 SECTION_DEBUG
29574 | SECTION_LINKONCE
29575 | (early_lto_debug
29576 ? SECTION_EXCLUDE : 0),
29577 comdat_key);
29578 ASM_GENERATE_INTERNAL_LABEL (label,
29579 DEBUG_MACRO_SECTION_LABEL,
29580 ref->lineno + macinfo_label_base);
29581 ASM_OUTPUT_LABEL (asm_out_file, label);
29582 ref->code = 0;
29583 ref->info = NULL;
29584 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29585 "DWARF macro version number");
29586 if (dwarf_offset_size == 8)
29587 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29588 else
29589 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29590 }
29591 break;
29592 case DW_MACINFO_define:
29593 case DW_MACINFO_undef:
29594 output_macinfo_op (ref);
29595 ref->code = 0;
29596 ref->info = NULL;
29597 break;
29598 default:
29599 gcc_unreachable ();
29600 }
29601
29602 macinfo_label_base += macinfo_label_base_adj;
29603}
29604
29605/* As init_sections_and_labels may get called multiple times, have a
29606 generation count for labels. */
29607static unsigned init_sections_and_labels_generation;
29608
29609/* Initialize the various sections and labels for dwarf output and prefix
29610 them with PREFIX if non-NULL. Returns the generation (zero based
29611 number of times function was called). */
29612
29613static unsigned
29614init_sections_and_labels (bool early_lto_debug)
29615{
29616 if (early_lto_debug)
29617 {
29618 if (!dwarf_split_debug_info)
29619 {
29620 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29621 SECTION_DEBUG | SECTION_EXCLUDE,
29622 NULL);
29623 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29624 SECTION_DEBUG | SECTION_EXCLUDE,
29625 NULL);
29626 debug_macinfo_section_name
29627 = ((dwarf_strict && dwarf_version < 5)
29628 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29629 debug_macinfo_section = get_section (debug_macinfo_section_name,
29630 SECTION_DEBUG
29631 | SECTION_EXCLUDE, NULL);
29632 }
29633 else
29634 {
29635 /* ??? Which of the following do we need early? */
29636 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29637 SECTION_DEBUG | SECTION_EXCLUDE,
29638 NULL);
29639 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29640 SECTION_DEBUG | SECTION_EXCLUDE,
29641 NULL);
29642 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29643 SECTION_DEBUG
29644 | SECTION_EXCLUDE, NULL);
29645 debug_skeleton_abbrev_section
29646 = get_section (DEBUG_LTO_ABBREV_SECTION,
29647 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29648 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29649 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29650 init_sections_and_labels_generation);
29651
29652 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29653 stay in the main .o, but the skeleton_line goes into the split
29654 off dwo. */
29655 debug_skeleton_line_section
29656 = get_section (DEBUG_LTO_LINE_SECTION,
29657 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29658 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29659 DEBUG_SKELETON_LINE_SECTION_LABEL,
29660 init_sections_and_labels_generation);
29661 debug_str_offsets_section
29662 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29663 SECTION_DEBUG | SECTION_EXCLUDE,
29664 NULL);
29665 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29666 DEBUG_SKELETON_INFO_SECTION_LABEL,
29667 init_sections_and_labels_generation);
29668 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29669 DEBUG_STR_DWO_SECTION_FLAGS,
29670 NULL);
29671 debug_macinfo_section_name
29672 = ((dwarf_strict && dwarf_version < 5)
29673 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29674 debug_macinfo_section = get_section (debug_macinfo_section_name,
29675 SECTION_DEBUG | SECTION_EXCLUDE,
29676 NULL);
29677 }
29678 /* For macro info and the file table we have to refer to a
29679 debug_line section. */
29680 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29681 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29682 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29683 DEBUG_LINE_SECTION_LABEL,
29684 init_sections_and_labels_generation);
29685
29686 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29687 DEBUG_STR_SECTION_FLAGS
29688 | SECTION_EXCLUDE, NULL);
29689 if (!dwarf_split_debug_info)
29690 debug_line_str_section
29691 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29692 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29693 }
29694 else
29695 {
29696 if (!dwarf_split_debug_info)
29697 {
29698 debug_info_section = get_section (DEBUG_INFO_SECTION,
29699 SECTION_DEBUG, NULL);
29700 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29701 SECTION_DEBUG, NULL);
29702 debug_loc_section = get_section (dwarf_version >= 5
29703 ? DEBUG_LOCLISTS_SECTION
29704 : DEBUG_LOC_SECTION,
29705 SECTION_DEBUG, NULL);
29706 debug_macinfo_section_name
29707 = ((dwarf_strict && dwarf_version < 5)
29708 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29709 debug_macinfo_section = get_section (debug_macinfo_section_name,
29710 SECTION_DEBUG, NULL);
29711 }
29712 else
29713 {
29714 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29715 SECTION_DEBUG | SECTION_EXCLUDE,
29716 NULL);
29717 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29718 SECTION_DEBUG | SECTION_EXCLUDE,
29719 NULL);
29720 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29721 SECTION_DEBUG, NULL);
29722 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29723 SECTION_DEBUG, NULL);
29724 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29725 SECTION_DEBUG, NULL);
29726 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29727 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29728 init_sections_and_labels_generation);
29729
29730 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29731 stay in the main .o, but the skeleton_line goes into the
29732 split off dwo. */
29733 debug_skeleton_line_section
29734 = get_section (DEBUG_DWO_LINE_SECTION,
29735 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29736 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29737 DEBUG_SKELETON_LINE_SECTION_LABEL,
29738 init_sections_and_labels_generation);
29739 debug_str_offsets_section
29740 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29741 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29742 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29743 DEBUG_SKELETON_INFO_SECTION_LABEL,
29744 init_sections_and_labels_generation);
29745 debug_loc_section = get_section (dwarf_version >= 5
29746 ? DEBUG_DWO_LOCLISTS_SECTION
29747 : DEBUG_DWO_LOC_SECTION,
29748 SECTION_DEBUG | SECTION_EXCLUDE,
29749 NULL);
29750 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29751 DEBUG_STR_DWO_SECTION_FLAGS,
29752 NULL);
29753 debug_macinfo_section_name
29754 = ((dwarf_strict && dwarf_version < 5)
29755 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29756 debug_macinfo_section = get_section (debug_macinfo_section_name,
29757 SECTION_DEBUG | SECTION_EXCLUDE,
29758 NULL);
29759 if (dwarf_version >= 5)
29760 debug_ranges_dwo_section
29761 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29762 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29763 }
29764 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29765 SECTION_DEBUG, NULL);
29766 debug_line_section = get_section (DEBUG_LINE_SECTION,
29767 SECTION_DEBUG, NULL);
29768 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29769 SECTION_DEBUG, NULL);
29770 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29771 SECTION_DEBUG, NULL);
29772 debug_str_section = get_section (DEBUG_STR_SECTION,
29773 DEBUG_STR_SECTION_FLAGS, NULL);
29774 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29775 || asm_outputs_debug_line_str ())
29776 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29777 DEBUG_STR_SECTION_FLAGS, NULL);
29778
29779 debug_ranges_section = get_section (dwarf_version >= 5
29780 ? DEBUG_RNGLISTS_SECTION
29781 : DEBUG_RANGES_SECTION,
29782 SECTION_DEBUG, NULL);
29783 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29784 SECTION_DEBUG, NULL);
29785 }
29786
29787 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29788 DEBUG_ABBREV_SECTION_LABEL,
29789 init_sections_and_labels_generation);
29790 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29791 DEBUG_INFO_SECTION_LABEL,
29792 init_sections_and_labels_generation);
29793 info_section_emitted = false;
29794 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29795 DEBUG_LINE_SECTION_LABEL,
29796 init_sections_and_labels_generation);
29797 /* There are up to 6 unique ranges labels per generation.
29798 See also output_rnglists. */
29799 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29800 DEBUG_RANGES_SECTION_LABEL,
29801 init_sections_and_labels_generation * 6);
29802 if (dwarf_version >= 5 && dwarf_split_debug_info)
29803 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29804 DEBUG_RANGES_SECTION_LABEL,
29805 1 + init_sections_and_labels_generation * 6);
29806 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29807 DEBUG_ADDR_SECTION_LABEL,
29808 init_sections_and_labels_generation);
29809 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29810 (dwarf_strict && dwarf_version < 5)
29811 ? DEBUG_MACINFO_SECTION_LABEL
29812 : DEBUG_MACRO_SECTION_LABEL,
29813 init_sections_and_labels_generation);
29814 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29815 init_sections_and_labels_generation);
29816
29817 ++init_sections_and_labels_generation;
29818 return init_sections_and_labels_generation - 1;
29819}
29820
29821/* Set up for Dwarf output at the start of compilation. */
29822
29823static void
29824dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29825{
29826 /* Allocate the file_table. */
29827 file_table = hash_table<dwarf_file_hasher>::create_ggc (n: 50);
29828
29829#ifndef DWARF2_LINENO_DEBUGGING_INFO
29830 /* Allocate the decl_die_table. */
29831 decl_die_table = hash_table<decl_die_hasher>::create_ggc (n: 10);
29832
29833 /* Allocate the decl_loc_table. */
29834 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (n: 10);
29835
29836 /* Allocate the cached_dw_loc_list_table. */
29837 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (n: 10);
29838
29839 /* Allocate the initial hunk of the abbrev_die_table. */
29840 vec_alloc (v&: abbrev_die_table, nelems: 256);
29841 /* Zero-th entry is allocated, but unused. */
29842 abbrev_die_table->quick_push (NULL);
29843
29844 /* Allocate the dwarf_proc_stack_usage_map. */
29845 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29846
29847 /* Allocate the pubtypes and pubnames vectors. */
29848 vec_alloc (v&: pubname_table, nelems: 32);
29849 vec_alloc (v&: pubtype_table, nelems: 32);
29850
29851 vec_alloc (v&: incomplete_types, nelems: 64);
29852
29853 vec_alloc (v&: used_rtx_array, nelems: 32);
29854
29855 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29856 vec_alloc (v&: macinfo_table, nelems: 64);
29857#endif
29858
29859 /* If front-ends already registered a main translation unit but we were not
29860 ready to perform the association, do this now. */
29861 if (main_translation_unit != NULL_TREE)
29862 equate_decl_number_to_die (decl: main_translation_unit, decl_die: comp_unit_die ());
29863}
29864
29865/* Called before compile () starts outputtting functions, variables
29866 and toplevel asms into assembly. */
29867
29868static void
29869dwarf2out_assembly_start (void)
29870{
29871 if (text_section_line_info)
29872 return;
29873
29874#ifndef DWARF2_LINENO_DEBUGGING_INFO
29875 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29876 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29877 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29878 COLD_TEXT_SECTION_LABEL, 0);
29879 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29880
29881 switch_to_section (text_section);
29882 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29883#endif
29884
29885 /* Make sure the line number table for .text always exists. */
29886 text_section_line_info = new_line_info_table ();
29887 text_section_line_info->end_label = text_end_label;
29888
29889#ifdef DWARF2_LINENO_DEBUGGING_INFO
29890 cur_line_info_table = text_section_line_info;
29891#endif
29892
29893 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29894 && dwarf2out_do_cfi_asm ()
29895 && !dwarf2out_do_eh_frame ())
29896 fprintf (stream: asm_out_file, format: "\t.cfi_sections\t.debug_frame\n");
29897
29898#if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29899 if (output_asm_line_debug_info () && dwarf_version >= 5)
29900 {
29901 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29902 tell it the comp_dir and main file name for the zero entry
29903 line table. */
29904 const char *comp_dir, *filename0;
29905
29906 comp_dir = comp_dir_string ();
29907 if (comp_dir == NULL)
29908 comp_dir = "";
29909
29910 filename0 = get_AT_string (die: comp_unit_die (), attr_kind: DW_AT_name);
29911 if (filename0 == NULL)
29912 filename0 = "";
29913
29914 fprintf (stream: asm_out_file, format: "\t.file 0 ");
29915 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29916 fputc (c: ' ', stream: asm_out_file);
29917 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29918 fputc (c: '\n', stream: asm_out_file);
29919 }
29920 else
29921#endif
29922 /* Work around for PR101575: output a dummy .file directive. */
29923 if (!last_emitted_file && dwarf_debuginfo_p ()
29924 && debug_info_level >= DINFO_LEVEL_TERSE)
29925 {
29926 const char *filename0 = get_AT_string (die: comp_unit_die (), attr_kind: DW_AT_name);
29927
29928 if (filename0 == NULL)
29929 filename0 = "<dummy>";
29930 maybe_emit_file (fd: lookup_filename (file_name: filename0));
29931 }
29932}
29933
29934/* A helper function for dwarf2out_finish called through
29935 htab_traverse. Assign a string its index. All strings must be
29936 collected into the table by the time index_string is called,
29937 because the indexing code relies on htab_traverse to traverse nodes
29938 in the same order for each run. */
29939
29940int
29941index_string (indirect_string_node **h, unsigned int *index)
29942{
29943 indirect_string_node *node = *h;
29944
29945 find_string_form (node);
29946 if (node->form == dwarf_FORM (form: DW_FORM_strx) && node->refcount > 0)
29947 {
29948 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29949 node->index = *index;
29950 *index += 1;
29951 }
29952 return 1;
29953}
29954
29955/* A helper function for output_indirect_strings called through
29956 htab_traverse. Output the offset to a string and update the
29957 current offset. */
29958
29959int
29960output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29961{
29962 indirect_string_node *node = *h;
29963
29964 if (node->form == dwarf_FORM (form: DW_FORM_strx) && node->refcount > 0)
29965 {
29966 /* Assert that this node has been assigned an index. */
29967 gcc_assert (node->index != NO_INDEX_ASSIGNED
29968 && node->index != NOT_INDEXED);
29969 dw2_asm_output_data (dwarf_offset_size, *offset,
29970 "indexed string 0x%x: %s", node->index, node->str);
29971 *offset += strlen (s: node->str) + 1;
29972 }
29973 return 1;
29974}
29975
29976/* A helper function for dwarf2out_finish called through
29977 htab_traverse. Output the indexed string. */
29978
29979int
29980output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29981{
29982 struct indirect_string_node *node = *h;
29983
29984 if (node->form == dwarf_FORM (form: DW_FORM_strx) && node->refcount > 0)
29985 {
29986 /* Assert that the strings are output in the same order as their
29987 indexes were assigned. */
29988 gcc_assert (*cur_idx == node->index);
29989 assemble_string (node->str, strlen (s: node->str) + 1);
29990 *cur_idx += 1;
29991 }
29992 return 1;
29993}
29994
29995/* A helper function for output_indirect_strings. Counts the number
29996 of index strings offsets. Must match the logic of the functions
29997 output_index_string[_offsets] above. */
29998int
29999count_index_strings (indirect_string_node **h, unsigned int *last_idx)
30000{
30001 struct indirect_string_node *node = *h;
30002
30003 if (node->form == dwarf_FORM (form: DW_FORM_strx) && node->refcount > 0)
30004 *last_idx += 1;
30005 return 1;
30006}
30007
30008/* A helper function for dwarf2out_finish called through
30009 htab_traverse. Emit one queued .debug_str string. */
30010
30011int
30012output_indirect_string (indirect_string_node **h, enum dwarf_form form)
30013{
30014 struct indirect_string_node *node = *h;
30015
30016 node->form = find_string_form (node);
30017 if (node->form == form && node->refcount > 0)
30018 {
30019 ASM_OUTPUT_LABEL (asm_out_file, node->label);
30020 assemble_string (node->str, strlen (s: node->str) + 1);
30021 }
30022
30023 return 1;
30024}
30025
30026/* Output the indexed string table. */
30027
30028static void
30029output_indirect_strings (void)
30030{
30031 switch_to_section (debug_str_section);
30032 if (!dwarf_split_debug_info)
30033 debug_str_hash->traverse<enum dwarf_form,
30034 output_indirect_string> (argument: DW_FORM_strp);
30035 else
30036 {
30037 unsigned int offset = 0;
30038 unsigned int cur_idx = 0;
30039
30040 if (skeleton_debug_str_hash)
30041 skeleton_debug_str_hash->traverse<enum dwarf_form,
30042 output_indirect_string> (argument: DW_FORM_strp);
30043
30044 switch_to_section (debug_str_offsets_section);
30045 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
30046 header. Note that we don't need to generate a label to the
30047 actual index table following the header here, because this is
30048 for the split dwarf case only. In an .dwo file there is only
30049 one string offsets table (and one debug info section). But
30050 if we would start using string offset tables for the main (or
30051 skeleton) unit, then we have to add a DW_AT_str_offsets_base
30052 pointing to the actual index after the header. Split dwarf
30053 units will never have a string offsets base attribute. When
30054 a split unit is moved into a .dwp file the string offsets can
30055 be found through the .debug_cu_index section table. */
30056 if (dwarf_version >= 5)
30057 {
30058 unsigned int last_idx = 0;
30059 unsigned long str_offsets_length;
30060
30061 debug_str_hash->traverse_noresize
30062 <unsigned int *, count_index_strings> (argument: &last_idx);
30063 str_offsets_length = last_idx * dwarf_offset_size + 4;
30064 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30065 dw2_asm_output_data (4, 0xffffffff,
30066 "Escape value for 64-bit DWARF extension");
30067 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
30068 "Length of string offsets unit");
30069 dw2_asm_output_data (2, 5, "DWARF string offsets version");
30070 dw2_asm_output_data (2, 0, "Header zero padding");
30071 }
30072 debug_str_hash->traverse_noresize
30073 <unsigned int *, output_index_string_offset> (argument: &offset);
30074 switch_to_section (debug_str_dwo_section);
30075 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
30076 (argument: &cur_idx);
30077 }
30078}
30079
30080/* Callback for htab_traverse to assign an index to an entry in the
30081 table, and to write that entry to the .debug_addr section. */
30082
30083int
30084output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
30085{
30086 addr_table_entry *entry = *slot;
30087
30088 if (entry->refcount == 0)
30089 {
30090 gcc_assert (entry->index == NO_INDEX_ASSIGNED
30091 || entry->index == NOT_INDEXED);
30092 return 1;
30093 }
30094
30095 gcc_assert (entry->index == *cur_index);
30096 (*cur_index)++;
30097
30098 switch (entry->kind)
30099 {
30100 case ate_kind_rtx:
30101 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
30102 "0x%x", entry->index);
30103 break;
30104 case ate_kind_rtx_dtprel:
30105 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
30106 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
30107 DWARF2_ADDR_SIZE,
30108 entry->addr.rtl);
30109 fputc (c: '\n', stream: asm_out_file);
30110 break;
30111 case ate_kind_label:
30112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
30113 "0x%x", entry->index);
30114 break;
30115 default:
30116 gcc_unreachable ();
30117 }
30118 return 1;
30119}
30120
30121/* A helper function for dwarf2out_finish. Counts the number
30122 of indexed addresses. Must match the logic of the functions
30123 output_addr_table_entry above. */
30124int
30125count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
30126{
30127 addr_table_entry *entry = *slot;
30128
30129 if (entry->refcount > 0)
30130 *last_idx += 1;
30131 return 1;
30132}
30133
30134/* Produce the .debug_addr section. */
30135
30136static void
30137output_addr_table (void)
30138{
30139 unsigned int index = 0;
30140 if (addr_index_table == NULL || addr_index_table->size () == 0)
30141 return;
30142
30143 switch_to_section (debug_addr_section);
30144 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
30145 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
30146 before DWARF5, didn't have a header for .debug_addr units.
30147 DWARF5 specifies a small header when address tables are used. */
30148 if (dwarf_version >= 5)
30149 {
30150 unsigned int last_idx = 0;
30151 unsigned long addrs_length;
30152
30153 addr_index_table->traverse_noresize
30154 <unsigned int *, count_index_addrs> (argument: &last_idx);
30155 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
30156
30157 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
30158 dw2_asm_output_data (4, 0xffffffff,
30159 "Escape value for 64-bit DWARF extension");
30160 dw2_asm_output_data (dwarf_offset_size, addrs_length,
30161 "Length of Address Unit");
30162 dw2_asm_output_data (2, 5, "DWARF addr version");
30163 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
30164 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
30165 }
30166 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30167
30168 addr_index_table
30169 ->traverse_noresize<unsigned int *, output_addr_table_entry> (argument: &index);
30170}
30171
30172#if ENABLE_ASSERT_CHECKING
30173/* Verify that all marks are clear. */
30174
30175static void
30176verify_marks_clear (dw_die_ref die)
30177{
30178 dw_die_ref c;
30179
30180 gcc_assert (! die->die_mark);
30181 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
30182}
30183#endif /* ENABLE_ASSERT_CHECKING */
30184
30185/* Clear the marks for a die and its children.
30186 Be cool if the mark isn't set. */
30187
30188static void
30189prune_unmark_dies (dw_die_ref die)
30190{
30191 dw_die_ref c;
30192
30193 if (die->die_mark)
30194 die->die_mark = 0;
30195 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
30196}
30197
30198/* Given LOC that is referenced by a DIE we're marking as used, find all
30199 referenced DWARF procedures it references and mark them as used. */
30200
30201static void
30202prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
30203{
30204 for (; loc != NULL; loc = loc->dw_loc_next)
30205 switch (loc->dw_loc_opc)
30206 {
30207 case DW_OP_implicit_pointer:
30208 case DW_OP_convert:
30209 case DW_OP_reinterpret:
30210 case DW_OP_GNU_implicit_pointer:
30211 case DW_OP_GNU_convert:
30212 case DW_OP_GNU_reinterpret:
30213 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
30214 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30215 break;
30216 case DW_OP_GNU_variable_value:
30217 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30218 {
30219 dw_die_ref ref
30220 = lookup_decl_die (decl: loc->dw_loc_oprnd1.v.val_decl_ref);
30221 if (ref == NULL)
30222 break;
30223 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30224 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30225 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30226 }
30227 /* FALLTHRU */
30228 case DW_OP_call2:
30229 case DW_OP_call4:
30230 case DW_OP_call_ref:
30231 case DW_OP_const_type:
30232 case DW_OP_GNU_const_type:
30233 case DW_OP_GNU_parameter_ref:
30234 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30235 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30236 break;
30237 case DW_OP_regval_type:
30238 case DW_OP_deref_type:
30239 case DW_OP_GNU_regval_type:
30240 case DW_OP_GNU_deref_type:
30241 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30242 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30243 break;
30244 case DW_OP_entry_value:
30245 case DW_OP_GNU_entry_value:
30246 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30247 prune_unused_types_walk_loc_descr (loc: loc->dw_loc_oprnd1.v.val_loc);
30248 break;
30249 default:
30250 break;
30251 }
30252}
30253
30254/* Given DIE that we're marking as used, find any other dies
30255 it references as attributes and mark them as used. */
30256
30257static void
30258prune_unused_types_walk_attribs (dw_die_ref die)
30259{
30260 dw_attr_node *a;
30261 unsigned ix;
30262
30263 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30264 {
30265 switch (AT_class (a))
30266 {
30267 /* Make sure DWARF procedures referenced by location descriptions will
30268 get emitted. */
30269 case dw_val_class_loc:
30270 prune_unused_types_walk_loc_descr (loc: AT_loc (a));
30271 break;
30272 case dw_val_class_loc_list:
30273 for (dw_loc_list_ref list = AT_loc_list (a);
30274 list != NULL;
30275 list = list->dw_loc_next)
30276 prune_unused_types_walk_loc_descr (loc: list->expr);
30277 break;
30278
30279 case dw_val_class_view_list:
30280 /* This points to a loc_list in another attribute, so it's
30281 already covered. */
30282 break;
30283
30284 case dw_val_class_die_ref:
30285 /* A reference to another DIE.
30286 Make sure that it will get emitted.
30287 If it was broken out into a comdat group, don't follow it. */
30288 if (! AT_ref (a)->comdat_type_p
30289 || a->dw_attr == DW_AT_specification)
30290 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30291 break;
30292
30293 case dw_val_class_str:
30294 /* Set the string's refcount to 0 so that prune_unused_types_mark
30295 accounts properly for it. */
30296 a->dw_attr_val.v.val_str->refcount = 0;
30297 break;
30298
30299 default:
30300 break;
30301 }
30302 }
30303}
30304
30305/* Mark the generic parameters and arguments children DIEs of DIE. */
30306
30307static void
30308prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30309{
30310 dw_die_ref c;
30311
30312 if (die == NULL || die->die_child == NULL)
30313 return;
30314 c = die->die_child;
30315 do
30316 {
30317 if (is_template_parameter (die: c))
30318 prune_unused_types_mark (c, 1);
30319 c = c->die_sib;
30320 } while (c && c != die->die_child);
30321}
30322
30323/* Mark DIE as being used. If DOKIDS is true, then walk down
30324 to DIE's children. */
30325
30326static void
30327prune_unused_types_mark (dw_die_ref die, int dokids)
30328{
30329 dw_die_ref c;
30330
30331 if (die->die_mark == 0)
30332 {
30333 /* We haven't done this node yet. Mark it as used. */
30334 die->die_mark = 1;
30335 /* If this is the DIE of a generic type instantiation,
30336 mark the children DIEs that describe its generic parms and
30337 args. */
30338 prune_unused_types_mark_generic_parms_dies (die);
30339
30340 /* We also have to mark its parents as used.
30341 (But we don't want to mark our parent's kids due to this,
30342 unless it is a class.) */
30343 if (die->die_parent)
30344 prune_unused_types_mark (die: die->die_parent,
30345 dokids: class_scope_p (context_die: die->die_parent));
30346
30347 /* Mark any referenced nodes. */
30348 prune_unused_types_walk_attribs (die);
30349
30350 /* If this node is a specification,
30351 also mark the definition, if it exists. */
30352 if (get_AT_flag (die, attr_kind: DW_AT_declaration) && die->die_definition)
30353 prune_unused_types_mark (die: die->die_definition, dokids: 1);
30354 }
30355
30356 if (dokids && die->die_mark != 2)
30357 {
30358 /* We need to walk the children, but haven't done so yet.
30359 Remember that we've walked the kids. */
30360 die->die_mark = 2;
30361
30362 /* If this is an array type, we need to make sure our
30363 kids get marked, even if they're types. If we're
30364 breaking out types into comdat sections, do this
30365 for all type definitions. */
30366 if (die->die_tag == DW_TAG_array_type
30367 || (use_debug_types
30368 && is_type_die (die) && ! is_declaration_die (die)))
30369 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30370 else
30371 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30372 }
30373}
30374
30375/* For local classes, look if any static member functions were emitted
30376 and if so, mark them. */
30377
30378static void
30379prune_unused_types_walk_local_classes (dw_die_ref die)
30380{
30381 dw_die_ref c;
30382
30383 if (die->die_mark == 2)
30384 return;
30385
30386 switch (die->die_tag)
30387 {
30388 case DW_TAG_structure_type:
30389 case DW_TAG_union_type:
30390 case DW_TAG_class_type:
30391 case DW_TAG_interface_type:
30392 break;
30393
30394 case DW_TAG_subprogram:
30395 if (!get_AT_flag (die, attr_kind: DW_AT_declaration)
30396 || die->die_definition != NULL)
30397 prune_unused_types_mark (die, dokids: 1);
30398 return;
30399
30400 default:
30401 return;
30402 }
30403
30404 /* Mark children. */
30405 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30406}
30407
30408/* Walk the tree DIE and mark types that we actually use. */
30409
30410static void
30411prune_unused_types_walk (dw_die_ref die)
30412{
30413 dw_die_ref c;
30414
30415 /* Don't do anything if this node is already marked and
30416 children have been marked as well. */
30417 if (die->die_mark == 2)
30418 return;
30419
30420 switch (die->die_tag)
30421 {
30422 case DW_TAG_structure_type:
30423 case DW_TAG_union_type:
30424 case DW_TAG_class_type:
30425 case DW_TAG_interface_type:
30426 if (die->die_perennial_p)
30427 break;
30428
30429 for (c = die->die_parent; c; c = c->die_parent)
30430 if (c->die_tag == DW_TAG_subprogram)
30431 break;
30432
30433 /* Finding used static member functions inside of classes
30434 is needed just for local classes, because for other classes
30435 static member function DIEs with DW_AT_specification
30436 are emitted outside of the DW_TAG_*_type. If we ever change
30437 it, we'd need to call this even for non-local classes. */
30438 if (c)
30439 prune_unused_types_walk_local_classes (die);
30440
30441 /* It's a type node --- don't mark it. */
30442 return;
30443
30444 case DW_TAG_const_type:
30445 case DW_TAG_packed_type:
30446 case DW_TAG_pointer_type:
30447 case DW_TAG_reference_type:
30448 case DW_TAG_rvalue_reference_type:
30449 case DW_TAG_volatile_type:
30450 case DW_TAG_restrict_type:
30451 case DW_TAG_shared_type:
30452 case DW_TAG_atomic_type:
30453 case DW_TAG_immutable_type:
30454 case DW_TAG_typedef:
30455 case DW_TAG_array_type:
30456 case DW_TAG_coarray_type:
30457 case DW_TAG_friend:
30458 case DW_TAG_enumeration_type:
30459 case DW_TAG_subroutine_type:
30460 case DW_TAG_string_type:
30461 case DW_TAG_set_type:
30462 case DW_TAG_subrange_type:
30463 case DW_TAG_ptr_to_member_type:
30464 case DW_TAG_file_type:
30465 case DW_TAG_unspecified_type:
30466 case DW_TAG_dynamic_type:
30467 /* Type nodes are useful only when other DIEs reference them --- don't
30468 mark them. */
30469 /* FALLTHROUGH */
30470
30471 case DW_TAG_dwarf_procedure:
30472 /* Likewise for DWARF procedures. */
30473
30474 if (die->die_perennial_p)
30475 break;
30476
30477 return;
30478
30479 case DW_TAG_variable:
30480 if (flag_debug_only_used_symbols)
30481 {
30482 if (die->die_perennial_p)
30483 break;
30484
30485 /* For static data members, the declaration in the class is supposed
30486 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30487 DWARF5. DW_TAG_member will be marked, so mark even such
30488 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30489 attribute. */
30490 if (dwarf_version >= 5
30491 && class_scope_p (context_die: die->die_parent)
30492 && get_AT (die, attr_kind: DW_AT_const_value))
30493 break;
30494
30495 /* premark_used_variables marks external variables --- don't mark
30496 them here. But function-local externals are always considered
30497 used. */
30498 if (get_AT (die, attr_kind: DW_AT_external))
30499 {
30500 for (c = die->die_parent; c; c = c->die_parent)
30501 if (c->die_tag == DW_TAG_subprogram)
30502 break;
30503 if (!c)
30504 return;
30505 }
30506 }
30507 /* FALLTHROUGH */
30508
30509 default:
30510 /* Mark everything else. */
30511 break;
30512 }
30513
30514 if (die->die_mark == 0)
30515 {
30516 die->die_mark = 1;
30517
30518 /* Now, mark any dies referenced from here. */
30519 prune_unused_types_walk_attribs (die);
30520 }
30521
30522 die->die_mark = 2;
30523
30524 /* Mark children. */
30525 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30526}
30527
30528/* Increment the string counts on strings referred to from DIE's
30529 attributes. */
30530
30531static void
30532prune_unused_types_update_strings (dw_die_ref die)
30533{
30534 dw_attr_node *a;
30535 unsigned ix;
30536
30537 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30538 if (AT_class (a) == dw_val_class_str)
30539 {
30540 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30541 s->refcount++;
30542 /* Avoid unnecessarily putting strings that are used less than
30543 twice in the hash table. */
30544 if (s->form != DW_FORM_line_strp
30545 && (s->refcount
30546 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30547 {
30548 indirect_string_node **slot
30549 = debug_str_hash->find_slot_with_hash (comparable: s->str,
30550 hash: htab_hash_string (s->str),
30551 insert: INSERT);
30552 gcc_assert (*slot == NULL);
30553 *slot = s;
30554 }
30555 }
30556}
30557
30558/* Mark DIE and its children as removed. */
30559
30560static void
30561mark_removed (dw_die_ref die)
30562{
30563 dw_die_ref c;
30564 die->removed = true;
30565 FOR_EACH_CHILD (die, c, mark_removed (c));
30566}
30567
30568/* Remove from the tree DIE any dies that aren't marked. */
30569
30570static void
30571prune_unused_types_prune (dw_die_ref die)
30572{
30573 dw_die_ref c;
30574
30575 gcc_assert (die->die_mark);
30576 prune_unused_types_update_strings (die);
30577
30578 if (! die->die_child)
30579 return;
30580
30581 c = die->die_child;
30582 do {
30583 dw_die_ref prev = c, next;
30584 for (c = c->die_sib; ! c->die_mark; c = next)
30585 if (c == die->die_child)
30586 {
30587 /* No marked children between 'prev' and the end of the list. */
30588 if (prev == c)
30589 /* No marked children at all. */
30590 die->die_child = NULL;
30591 else
30592 {
30593 prev->die_sib = c->die_sib;
30594 die->die_child = prev;
30595 }
30596 c->die_sib = NULL;
30597 mark_removed (die: c);
30598 return;
30599 }
30600 else
30601 {
30602 next = c->die_sib;
30603 c->die_sib = NULL;
30604 mark_removed (die: c);
30605 }
30606
30607 if (c != prev->die_sib)
30608 prev->die_sib = c;
30609 prune_unused_types_prune (die: c);
30610 } while (c != die->die_child);
30611}
30612
30613/* Remove dies representing declarations that we never use. */
30614
30615static void
30616prune_unused_types (void)
30617{
30618 unsigned int i;
30619 limbo_die_node *node;
30620 comdat_type_node *ctnode;
30621 pubname_entry *pub;
30622 dw_die_ref base_type;
30623
30624#if ENABLE_ASSERT_CHECKING
30625 /* All the marks should already be clear. */
30626 verify_marks_clear (die: comp_unit_die ());
30627 for (node = limbo_die_list; node; node = node->next)
30628 verify_marks_clear (die: node->die);
30629 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30630 verify_marks_clear (die: ctnode->root_die);
30631#endif /* ENABLE_ASSERT_CHECKING */
30632
30633 /* Mark types that are used in global variables. */
30634 premark_types_used_by_global_vars ();
30635
30636 /* Mark variables used in the symtab. */
30637 if (flag_debug_only_used_symbols)
30638 premark_used_variables ();
30639
30640 /* Set the mark on nodes that are actually used. */
30641 prune_unused_types_walk (die: comp_unit_die ());
30642 for (node = limbo_die_list; node; node = node->next)
30643 prune_unused_types_walk (die: node->die);
30644 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30645 {
30646 prune_unused_types_walk (die: ctnode->root_die);
30647 prune_unused_types_mark (die: ctnode->type_die, dokids: 1);
30648 }
30649
30650 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30651 are unusual in that they are pubnames that are the children of pubtypes.
30652 They should only be marked via their parent DW_TAG_enumeration_type die,
30653 not as roots in themselves. */
30654 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30655 if (pub->die->die_tag != DW_TAG_enumerator)
30656 prune_unused_types_mark (die: pub->die, dokids: 1);
30657 for (i = 0; base_types.iterate (ix: i, ptr: &base_type); i++)
30658 prune_unused_types_mark (die: base_type, dokids: 1);
30659
30660 /* Also set the mark on nodes that could be referenced by
30661 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30662 by DW_TAG_inlined_subroutine origins. */
30663 cgraph_node *cnode;
30664 FOR_EACH_FUNCTION (cnode)
30665 if (cnode->referred_to_p (include_self: false))
30666 {
30667 dw_die_ref die = lookup_decl_die (decl: cnode->decl);
30668 if (die == NULL || die->die_mark)
30669 continue;
30670 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30671 if (e->caller != cnode)
30672 {
30673 prune_unused_types_mark (die, dokids: 1);
30674 break;
30675 }
30676 }
30677
30678 if (debug_str_hash)
30679 debug_str_hash->empty ();
30680 if (skeleton_debug_str_hash)
30681 skeleton_debug_str_hash->empty ();
30682 prune_unused_types_prune (die: comp_unit_die ());
30683 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30684 {
30685 node = *pnode;
30686 if (!node->die->die_mark)
30687 *pnode = node->next;
30688 else
30689 {
30690 prune_unused_types_prune (die: node->die);
30691 pnode = &node->next;
30692 }
30693 }
30694 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30695 prune_unused_types_prune (die: ctnode->root_die);
30696
30697 /* Leave the marks clear. */
30698 prune_unmark_dies (die: comp_unit_die ());
30699 for (node = limbo_die_list; node; node = node->next)
30700 prune_unmark_dies (die: node->die);
30701 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30702 prune_unmark_dies (die: ctnode->root_die);
30703}
30704
30705/* Helpers to manipulate hash table of comdat type units. */
30706
30707struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30708{
30709 static inline hashval_t hash (const comdat_type_node *);
30710 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30711};
30712
30713inline hashval_t
30714comdat_type_hasher::hash (const comdat_type_node *type_node)
30715{
30716 hashval_t h;
30717 memcpy (dest: &h, src: type_node->signature, n: sizeof (h));
30718 return h;
30719}
30720
30721inline bool
30722comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30723 const comdat_type_node *type_node_2)
30724{
30725 return (! memcmp (s1: type_node_1->signature, s2: type_node_2->signature,
30726 DWARF_TYPE_SIGNATURE_SIZE));
30727}
30728
30729/* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30730 to the location it would have been added, should we know its
30731 DECL_ASSEMBLER_NAME when we added other attributes. This will
30732 probably improve compactness of debug info, removing equivalent
30733 abbrevs, and hide any differences caused by deferring the
30734 computation of the assembler name, triggered by e.g. PCH. */
30735
30736static inline void
30737move_linkage_attr (dw_die_ref die)
30738{
30739 unsigned ix = vec_safe_length (v: die->die_attr);
30740 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30741
30742 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30743 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30744
30745 while (--ix > 0)
30746 {
30747 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30748
30749 if (prev->dw_attr == DW_AT_decl_line
30750 || prev->dw_attr == DW_AT_decl_column
30751 || prev->dw_attr == DW_AT_name)
30752 break;
30753 }
30754
30755 if (ix != vec_safe_length (v: die->die_attr) - 1)
30756 {
30757 die->die_attr->pop ();
30758 die->die_attr->quick_insert (ix, obj: linkage);
30759 }
30760}
30761
30762/* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30763 referenced from typed stack ops and count how often they are used. */
30764
30765static void
30766mark_base_types (dw_loc_descr_ref loc)
30767{
30768 dw_die_ref base_type = NULL;
30769
30770 for (; loc; loc = loc->dw_loc_next)
30771 {
30772 switch (loc->dw_loc_opc)
30773 {
30774 case DW_OP_regval_type:
30775 case DW_OP_deref_type:
30776 case DW_OP_GNU_regval_type:
30777 case DW_OP_GNU_deref_type:
30778 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30779 break;
30780 case DW_OP_convert:
30781 case DW_OP_reinterpret:
30782 case DW_OP_GNU_convert:
30783 case DW_OP_GNU_reinterpret:
30784 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30785 continue;
30786 /* FALLTHRU */
30787 case DW_OP_const_type:
30788 case DW_OP_GNU_const_type:
30789 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30790 break;
30791 case DW_OP_entry_value:
30792 case DW_OP_GNU_entry_value:
30793 mark_base_types (loc: loc->dw_loc_oprnd1.v.val_loc);
30794 continue;
30795 default:
30796 continue;
30797 }
30798 gcc_assert (base_type->die_parent == comp_unit_die ());
30799 if (base_type->die_mark)
30800 base_type->die_mark++;
30801 else
30802 {
30803 base_types.safe_push (obj: base_type);
30804 base_type->die_mark = 1;
30805 }
30806 }
30807}
30808
30809/* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30810 referenced from typed stack ops and count how often they are used. */
30811
30812static void
30813mark_base_types (dw_die_ref die)
30814{
30815 dw_die_ref c;
30816 dw_attr_node *a;
30817 dw_loc_list_ref *curr;
30818 unsigned ix;
30819
30820 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30821 switch (AT_class (a))
30822 {
30823 case dw_val_class_loc_list:
30824 curr = AT_loc_list_ptr (a);
30825 while (*curr)
30826 {
30827 mark_base_types (loc: (*curr)->expr);
30828 curr = &(*curr)->dw_loc_next;
30829 }
30830 break;
30831
30832 case dw_val_class_loc:
30833 mark_base_types (loc: AT_loc (a));
30834 break;
30835
30836 default:
30837 break;
30838 }
30839
30840 FOR_EACH_CHILD (die, c, mark_base_types (c));
30841}
30842
30843/* Comparison function for sorting marked base types. */
30844
30845static int
30846base_type_cmp (const void *x, const void *y)
30847{
30848 dw_die_ref dx = *(const dw_die_ref *) x;
30849 dw_die_ref dy = *(const dw_die_ref *) y;
30850 unsigned int byte_size1, byte_size2;
30851 unsigned int encoding1, encoding2;
30852 unsigned int align1, align2;
30853 if (dx->die_mark > dy->die_mark)
30854 return -1;
30855 if (dx->die_mark < dy->die_mark)
30856 return 1;
30857 byte_size1 = get_AT_unsigned (die: dx, attr_kind: DW_AT_byte_size);
30858 byte_size2 = get_AT_unsigned (die: dy, attr_kind: DW_AT_byte_size);
30859 if (byte_size1 < byte_size2)
30860 return 1;
30861 if (byte_size1 > byte_size2)
30862 return -1;
30863 encoding1 = get_AT_unsigned (die: dx, attr_kind: DW_AT_encoding);
30864 encoding2 = get_AT_unsigned (die: dy, attr_kind: DW_AT_encoding);
30865 if (encoding1 < encoding2)
30866 return 1;
30867 if (encoding1 > encoding2)
30868 return -1;
30869 align1 = get_AT_unsigned (die: dx, attr_kind: DW_AT_alignment);
30870 align2 = get_AT_unsigned (die: dy, attr_kind: DW_AT_alignment);
30871 if (align1 < align2)
30872 return 1;
30873 if (align1 > align2)
30874 return -1;
30875 return 0;
30876}
30877
30878/* Move base types marked by mark_base_types as early as possible
30879 in the CU, sorted by decreasing usage count both to make the
30880 uleb128 references as small as possible and to make sure they
30881 will have die_offset already computed by calc_die_sizes when
30882 sizes of typed stack loc ops is computed. */
30883
30884static void
30885move_marked_base_types (void)
30886{
30887 unsigned int i;
30888 dw_die_ref base_type, die, c;
30889
30890 if (base_types.is_empty ())
30891 return;
30892
30893 /* Sort by decreasing usage count, they will be added again in that
30894 order later on. */
30895 base_types.qsort (base_type_cmp);
30896 die = comp_unit_die ();
30897 c = die->die_child;
30898 do
30899 {
30900 dw_die_ref prev = c;
30901 c = c->die_sib;
30902 while (c->die_mark)
30903 {
30904 remove_child_with_prev (child: c, prev);
30905 /* As base types got marked, there must be at least
30906 one node other than DW_TAG_base_type. */
30907 gcc_assert (die->die_child != NULL);
30908 c = prev->die_sib;
30909 }
30910 }
30911 while (c != die->die_child);
30912 gcc_assert (die->die_child);
30913 c = die->die_child;
30914 for (i = 0; base_types.iterate (ix: i, ptr: &base_type); i++)
30915 {
30916 base_type->die_mark = 0;
30917 base_type->die_sib = c->die_sib;
30918 c->die_sib = base_type;
30919 c = base_type;
30920 }
30921}
30922
30923/* Helper function for resolve_addr, attempt to resolve
30924 one CONST_STRING, return true if successful. Similarly verify that
30925 SYMBOL_REFs refer to variables emitted in the current CU. */
30926
30927static bool
30928resolve_one_addr (rtx *addr)
30929{
30930 rtx rtl = *addr;
30931
30932 if (GET_CODE (rtl) == CONST_STRING)
30933 {
30934 size_t len = strlen (XSTR (rtl, 0)) + 1;
30935 tree t = build_string (len, XSTR (rtl, 0));
30936 tree tlen = size_int (len - 1);
30937 TREE_TYPE (t)
30938 = build_array_type (char_type_node, build_index_type (tlen));
30939 rtl = lookup_constant_def (t);
30940 if (!rtl || !MEM_P (rtl))
30941 return false;
30942 rtl = XEXP (rtl, 0);
30943 if (GET_CODE (rtl) == SYMBOL_REF
30944 && SYMBOL_REF_DECL (rtl)
30945 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30946 return false;
30947 vec_safe_push (v&: used_rtx_array, obj: rtl);
30948 *addr = rtl;
30949 return true;
30950 }
30951
30952 if (GET_CODE (rtl) == SYMBOL_REF
30953 && SYMBOL_REF_DECL (rtl))
30954 {
30955 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30956 {
30957 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30958 return false;
30959 }
30960 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30961 return false;
30962 }
30963
30964 if (GET_CODE (rtl) == CONST)
30965 {
30966 subrtx_ptr_iterator::array_type array;
30967 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30968 if (!resolve_one_addr (addr: *iter))
30969 return false;
30970 }
30971
30972 return true;
30973}
30974
30975/* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30976 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30977 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30978
30979static rtx
30980string_cst_pool_decl (tree t)
30981{
30982 rtx rtl = output_constant_def (t, 1);
30983 unsigned char *array;
30984 dw_loc_descr_ref l;
30985 tree decl;
30986 size_t len;
30987 dw_die_ref ref;
30988
30989 if (!rtl || !MEM_P (rtl))
30990 return NULL_RTX;
30991 rtl = XEXP (rtl, 0);
30992 if (GET_CODE (rtl) != SYMBOL_REF
30993 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30994 return NULL_RTX;
30995
30996 decl = SYMBOL_REF_DECL (rtl);
30997 if (!lookup_decl_die (decl))
30998 {
30999 len = TREE_STRING_LENGTH (t);
31000 vec_safe_push (v&: used_rtx_array, obj: rtl);
31001 ref = new_die (tag_value: DW_TAG_dwarf_procedure, parent_die: comp_unit_die (), t: decl);
31002 array = ggc_vec_alloc<unsigned char> (c: len);
31003 memcpy (dest: array, TREE_STRING_POINTER (t), n: len);
31004 l = new_loc_descr (op: DW_OP_implicit_value, oprnd1: len, oprnd2: 0);
31005 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
31006 l->dw_loc_oprnd2.v.val_vec.length = len;
31007 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
31008 l->dw_loc_oprnd2.v.val_vec.array = array;
31009 add_AT_loc (die: ref, attr_kind: DW_AT_location, loc: l);
31010 equate_decl_number_to_die (decl, decl_die: ref);
31011 }
31012 return rtl;
31013}
31014
31015/* Helper function of resolve_addr_in_expr. LOC is
31016 a DW_OP_addr followed by DW_OP_stack_value, either at the start
31017 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
31018 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
31019 with DW_OP_implicit_pointer if possible
31020 and return true, if unsuccessful, return false. */
31021
31022static bool
31023optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
31024{
31025 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
31026 HOST_WIDE_INT offset = 0;
31027 dw_die_ref ref = NULL;
31028 tree decl;
31029
31030 if (GET_CODE (rtl) == CONST
31031 && GET_CODE (XEXP (rtl, 0)) == PLUS
31032 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
31033 {
31034 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
31035 rtl = XEXP (XEXP (rtl, 0), 0);
31036 }
31037 if (GET_CODE (rtl) == CONST_STRING)
31038 {
31039 size_t len = strlen (XSTR (rtl, 0)) + 1;
31040 tree t = build_string (len, XSTR (rtl, 0));
31041 tree tlen = size_int (len - 1);
31042
31043 TREE_TYPE (t)
31044 = build_array_type (char_type_node, build_index_type (tlen));
31045 rtl = string_cst_pool_decl (t);
31046 if (!rtl)
31047 return false;
31048 }
31049 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
31050 {
31051 decl = SYMBOL_REF_DECL (rtl);
31052 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
31053 {
31054 ref = lookup_decl_die (decl);
31055 if (ref && (get_AT (die: ref, attr_kind: DW_AT_location)
31056 || get_AT (die: ref, attr_kind: DW_AT_const_value)))
31057 {
31058 loc->dw_loc_opc = dwarf_OP (op: DW_OP_implicit_pointer);
31059 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31060 loc->dw_loc_oprnd1.val_entry = NULL;
31061 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31062 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31063 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31064 loc->dw_loc_oprnd2.v.val_int = offset;
31065 return true;
31066 }
31067 }
31068 }
31069 return false;
31070}
31071
31072/* Helper function for resolve_addr, handle one location
31073 expression, return false if at least one CONST_STRING or SYMBOL_REF in
31074 the location list couldn't be resolved. */
31075
31076static bool
31077resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31078{
31079 dw_loc_descr_ref keep = NULL;
31080 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
31081 switch (loc->dw_loc_opc)
31082 {
31083 case DW_OP_addr:
31084 if (!resolve_one_addr (addr: &loc->dw_loc_oprnd1.v.val_addr))
31085 {
31086 if ((prev == NULL
31087 || prev->dw_loc_opc == DW_OP_piece
31088 || prev->dw_loc_opc == DW_OP_bit_piece)
31089 && loc->dw_loc_next
31090 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
31091 && (!dwarf_strict || dwarf_version >= 5)
31092 && optimize_one_addr_into_implicit_ptr (loc))
31093 break;
31094 return false;
31095 }
31096 break;
31097 case DW_OP_GNU_addr_index:
31098 case DW_OP_addrx:
31099 case DW_OP_GNU_const_index:
31100 case DW_OP_constx:
31101 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
31102 || loc->dw_loc_opc == DW_OP_addrx)
31103 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
31104 || loc->dw_loc_opc == DW_OP_constx)
31105 && loc->dw_loc_dtprel))
31106 {
31107 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
31108 if (!resolve_one_addr (addr: &rtl))
31109 return false;
31110 remove_addr_table_entry (entry: loc->dw_loc_oprnd1.val_entry);
31111 loc->dw_loc_oprnd1.val_entry
31112 = add_addr_table_entry (addr: rtl, kind: loc->dw_loc_dtprel
31113 ? ate_kind_rtx_dtprel : ate_kind_rtx);
31114 }
31115 break;
31116 case DW_OP_const4u:
31117 case DW_OP_const8u:
31118 if (loc->dw_loc_dtprel
31119 && !resolve_one_addr (addr: &loc->dw_loc_oprnd1.v.val_addr))
31120 return false;
31121 break;
31122 case DW_OP_plus_uconst:
31123 if (size_of_loc_descr (loc)
31124 > size_of_int_loc_descriptor (i: loc->dw_loc_oprnd1.v.val_unsigned)
31125 + 1
31126 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
31127 {
31128 dw_loc_descr_ref repl
31129 = int_loc_descriptor (poly_i: loc->dw_loc_oprnd1.v.val_unsigned);
31130 add_loc_descr (list_head: &repl, descr: new_loc_descr (op: DW_OP_plus, oprnd1: 0, oprnd2: 0));
31131 add_loc_descr (list_head: &repl, descr: loc->dw_loc_next);
31132 *loc = *repl;
31133 }
31134 break;
31135 case DW_OP_implicit_value:
31136 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
31137 && !resolve_one_addr (addr: &loc->dw_loc_oprnd2.v.val_addr))
31138 return false;
31139 break;
31140 case DW_OP_implicit_pointer:
31141 case DW_OP_GNU_implicit_pointer:
31142 case DW_OP_GNU_parameter_ref:
31143 case DW_OP_GNU_variable_value:
31144 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31145 {
31146 dw_die_ref ref
31147 = lookup_decl_die (decl: loc->dw_loc_oprnd1.v.val_decl_ref);
31148 if (ref == NULL)
31149 return false;
31150 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31151 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31152 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31153 }
31154 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
31155 {
31156 if (prev == NULL
31157 && loc->dw_loc_next == NULL
31158 && AT_class (a) == dw_val_class_loc)
31159 switch (a->dw_attr)
31160 {
31161 /* Following attributes allow both exprloc and reference,
31162 so if the whole expression is DW_OP_GNU_variable_value
31163 alone we could transform it into reference. */
31164 case DW_AT_byte_size:
31165 case DW_AT_bit_size:
31166 case DW_AT_lower_bound:
31167 case DW_AT_upper_bound:
31168 case DW_AT_bit_stride:
31169 case DW_AT_count:
31170 case DW_AT_allocated:
31171 case DW_AT_associated:
31172 case DW_AT_byte_stride:
31173 a->dw_attr_val.val_class = dw_val_class_die_ref;
31174 a->dw_attr_val.val_entry = NULL;
31175 a->dw_attr_val.v.val_die_ref.die
31176 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31177 a->dw_attr_val.v.val_die_ref.external = 0;
31178 return true;
31179 default:
31180 break;
31181 }
31182 if (dwarf_strict)
31183 return false;
31184 }
31185 break;
31186 case DW_OP_const_type:
31187 case DW_OP_regval_type:
31188 case DW_OP_deref_type:
31189 case DW_OP_convert:
31190 case DW_OP_reinterpret:
31191 case DW_OP_GNU_const_type:
31192 case DW_OP_GNU_regval_type:
31193 case DW_OP_GNU_deref_type:
31194 case DW_OP_GNU_convert:
31195 case DW_OP_GNU_reinterpret:
31196 while (loc->dw_loc_next
31197 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
31198 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
31199 {
31200 dw_die_ref base1, base2;
31201 unsigned enc1, enc2, size1, size2;
31202 if (loc->dw_loc_opc == DW_OP_regval_type
31203 || loc->dw_loc_opc == DW_OP_deref_type
31204 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31205 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31206 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
31207 else if (loc->dw_loc_oprnd1.val_class
31208 == dw_val_class_unsigned_const)
31209 break;
31210 else
31211 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
31212 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
31213 == dw_val_class_unsigned_const)
31214 break;
31215 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
31216 gcc_assert (base1->die_tag == DW_TAG_base_type
31217 && base2->die_tag == DW_TAG_base_type);
31218 enc1 = get_AT_unsigned (die: base1, attr_kind: DW_AT_encoding);
31219 enc2 = get_AT_unsigned (die: base2, attr_kind: DW_AT_encoding);
31220 size1 = get_AT_unsigned (die: base1, attr_kind: DW_AT_byte_size);
31221 size2 = get_AT_unsigned (die: base2, attr_kind: DW_AT_byte_size);
31222 if (size1 == size2
31223 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
31224 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
31225 && loc != keep)
31226 || enc1 == enc2))
31227 {
31228 /* Optimize away next DW_OP_convert after
31229 adjusting LOC's base type die reference. */
31230 if (loc->dw_loc_opc == DW_OP_regval_type
31231 || loc->dw_loc_opc == DW_OP_deref_type
31232 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31233 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31234 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31235 else
31236 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31237 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31238 continue;
31239 }
31240 /* Don't change integer DW_OP_convert after e.g. floating
31241 point typed stack entry. */
31242 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31243 keep = loc->dw_loc_next;
31244 break;
31245 }
31246 break;
31247 default:
31248 break;
31249 }
31250 return true;
31251}
31252
31253/* Helper function of resolve_addr. DIE had DW_AT_location of
31254 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31255 and DW_OP_addr couldn't be resolved. resolve_addr has already
31256 removed the DW_AT_location attribute. This function attempts to
31257 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31258 to it or DW_AT_const_value attribute, if possible. */
31259
31260static void
31261optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31262{
31263 if (!VAR_P (decl)
31264 || lookup_decl_die (decl) != die
31265 || DECL_EXTERNAL (decl)
31266 || !TREE_STATIC (decl)
31267 || DECL_INITIAL (decl) == NULL_TREE
31268 || DECL_P (DECL_INITIAL (decl))
31269 || get_AT (die, attr_kind: DW_AT_const_value))
31270 return;
31271
31272 tree init = DECL_INITIAL (decl);
31273 HOST_WIDE_INT offset = 0;
31274 /* For variables that have been optimized away and thus
31275 don't have a memory location, see if we can emit
31276 DW_AT_const_value instead. */
31277 if (tree_add_const_value_attribute (die, t: init))
31278 return;
31279 if (dwarf_strict && dwarf_version < 5)
31280 return;
31281 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31282 and ADDR_EXPR refers to a decl that has DW_AT_location or
31283 DW_AT_const_value (but isn't addressable, otherwise
31284 resolving the original DW_OP_addr wouldn't fail), see if
31285 we can add DW_OP_implicit_pointer. */
31286 STRIP_NOPS (init);
31287 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31288 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31289 {
31290 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31291 init = TREE_OPERAND (init, 0);
31292 STRIP_NOPS (init);
31293 }
31294 if (TREE_CODE (init) != ADDR_EXPR)
31295 return;
31296 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31297 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31298 || (VAR_P (TREE_OPERAND (init, 0))
31299 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31300 && TREE_OPERAND (init, 0) != decl))
31301 {
31302 dw_die_ref ref;
31303 dw_loc_descr_ref l;
31304
31305 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31306 {
31307 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31308 if (!rtl)
31309 return;
31310 decl = SYMBOL_REF_DECL (rtl);
31311 }
31312 else
31313 decl = TREE_OPERAND (init, 0);
31314 ref = lookup_decl_die (decl);
31315 if (ref == NULL
31316 || (!get_AT (die: ref, attr_kind: DW_AT_location)
31317 && !get_AT (die: ref, attr_kind: DW_AT_const_value)))
31318 return;
31319 l = new_loc_descr (op: dwarf_OP (op: DW_OP_implicit_pointer), oprnd1: 0, oprnd2: offset);
31320 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31321 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31322 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31323 add_AT_loc (die, attr_kind: DW_AT_location, loc: l);
31324 }
31325}
31326
31327/* Return NULL if l is a DWARF expression, or first op that is not
31328 valid DWARF expression. */
31329
31330static dw_loc_descr_ref
31331non_dwarf_expression (dw_loc_descr_ref l)
31332{
31333 while (l)
31334 {
31335 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31336 return l;
31337 switch (l->dw_loc_opc)
31338 {
31339 case DW_OP_regx:
31340 case DW_OP_implicit_value:
31341 case DW_OP_stack_value:
31342 case DW_OP_implicit_pointer:
31343 case DW_OP_GNU_implicit_pointer:
31344 case DW_OP_GNU_parameter_ref:
31345 case DW_OP_piece:
31346 case DW_OP_bit_piece:
31347 return l;
31348 default:
31349 break;
31350 }
31351 l = l->dw_loc_next;
31352 }
31353 return NULL;
31354}
31355
31356/* Return adjusted copy of EXPR:
31357 If it is empty DWARF expression, return it.
31358 If it is valid non-empty DWARF expression,
31359 return copy of EXPR with DW_OP_deref appended to it.
31360 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31361 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31362 If it is DWARF expression followed by DW_OP_stack_value, return
31363 copy of the DWARF expression without anything appended.
31364 Otherwise, return NULL. */
31365
31366static dw_loc_descr_ref
31367copy_deref_exprloc (dw_loc_descr_ref expr)
31368{
31369 dw_loc_descr_ref tail = NULL;
31370
31371 if (expr == NULL)
31372 return NULL;
31373
31374 dw_loc_descr_ref l = non_dwarf_expression (l: expr);
31375 if (l && l->dw_loc_next)
31376 return NULL;
31377
31378 if (l)
31379 {
31380 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31381 tail = new_loc_descr (op: (enum dwarf_location_atom)
31382 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31383 oprnd1: 0, oprnd2: 0);
31384 else
31385 switch (l->dw_loc_opc)
31386 {
31387 case DW_OP_regx:
31388 tail = new_loc_descr (op: DW_OP_bregx,
31389 oprnd1: l->dw_loc_oprnd1.v.val_unsigned, oprnd2: 0);
31390 break;
31391 case DW_OP_stack_value:
31392 break;
31393 default:
31394 return NULL;
31395 }
31396 }
31397 else
31398 tail = new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0);
31399
31400 dw_loc_descr_ref ret = NULL, *p = &ret;
31401 while (expr != l)
31402 {
31403 *p = new_loc_descr (op: expr->dw_loc_opc, oprnd1: 0, oprnd2: 0);
31404 (*p)->dw_loc_oprnd1.val_class = expr->dw_loc_oprnd1.val_class;
31405 (*p)->dw_loc_oprnd1.val_entry = expr->dw_loc_oprnd1.val_entry;
31406 (*p)->dw_loc_oprnd1.v = expr->dw_loc_oprnd1.v;
31407 (*p)->dw_loc_oprnd2.val_class = expr->dw_loc_oprnd2.val_class;
31408 (*p)->dw_loc_oprnd2.val_entry = expr->dw_loc_oprnd2.val_entry;
31409 (*p)->dw_loc_oprnd2.v = expr->dw_loc_oprnd2.v;
31410 p = &(*p)->dw_loc_next;
31411 expr = expr->dw_loc_next;
31412 }
31413 *p = tail;
31414 return ret;
31415}
31416
31417/* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31418 reference to a variable or argument, adjust it if needed and return:
31419 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31420 attribute if present should be removed
31421 0 keep the attribute perhaps with minor modifications, no need to rescan
31422 1 if the attribute has been successfully adjusted. */
31423
31424static int
31425optimize_string_length (dw_attr_node *a)
31426{
31427 dw_loc_descr_ref l = AT_loc (a), lv;
31428 dw_die_ref die;
31429 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31430 {
31431 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31432 die = lookup_decl_die (decl);
31433 if (die)
31434 {
31435 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31436 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31437 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31438 }
31439 else
31440 return -1;
31441 }
31442 else
31443 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31444
31445 /* DWARF5 allows reference class, so we can then reference the DIE.
31446 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31447 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31448 {
31449 a->dw_attr_val.val_class = dw_val_class_die_ref;
31450 a->dw_attr_val.val_entry = NULL;
31451 a->dw_attr_val.v.val_die_ref.die = die;
31452 a->dw_attr_val.v.val_die_ref.external = 0;
31453 return 0;
31454 }
31455
31456 dw_attr_node *av = get_AT (die, attr_kind: DW_AT_location);
31457 dw_loc_list_ref d;
31458 bool non_dwarf_expr = false;
31459
31460 if (av == NULL)
31461 return dwarf_strict ? -1 : 0;
31462 switch (AT_class (a: av))
31463 {
31464 case dw_val_class_loc_list:
31465 for (d = AT_loc_list (a: av); d != NULL; d = d->dw_loc_next)
31466 if (d->expr && non_dwarf_expression (l: d->expr))
31467 non_dwarf_expr = true;
31468 break;
31469 case dw_val_class_view_list:
31470 gcc_unreachable ();
31471 case dw_val_class_loc:
31472 lv = AT_loc (a: av);
31473 if (lv == NULL)
31474 return dwarf_strict ? -1 : 0;
31475 if (non_dwarf_expression (l: lv))
31476 non_dwarf_expr = true;
31477 break;
31478 default:
31479 return dwarf_strict ? -1 : 0;
31480 }
31481
31482 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31483 into DW_OP_call4 or DW_OP_GNU_variable_value into
31484 DW_OP_call4 DW_OP_deref, do so. */
31485 if (!non_dwarf_expr
31486 && (l->dw_loc_next != NULL || AT_class (a: av) == dw_val_class_loc))
31487 {
31488 l->dw_loc_opc = DW_OP_call4;
31489 if (l->dw_loc_next)
31490 l->dw_loc_next = NULL;
31491 else
31492 l->dw_loc_next = new_loc_descr (op: DW_OP_deref, oprnd1: 0, oprnd2: 0);
31493 return 0;
31494 }
31495
31496 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31497 copy over the DW_AT_location attribute from die to a. */
31498 if (l->dw_loc_next != NULL)
31499 {
31500 a->dw_attr_val.val_class = av->dw_attr_val.val_class;
31501 a->dw_attr_val.val_entry = av->dw_attr_val.val_entry;
31502 a->dw_attr_val.v = av->dw_attr_val.v;
31503 return 1;
31504 }
31505
31506 dw_loc_list_ref list, *p;
31507 switch (AT_class (a: av))
31508 {
31509 case dw_val_class_loc_list:
31510 p = &list;
31511 list = NULL;
31512 for (d = AT_loc_list (a: av); d != NULL; d = d->dw_loc_next)
31513 {
31514 lv = copy_deref_exprloc (expr: d->expr);
31515 if (lv)
31516 {
31517 *p = new_loc_list (expr: lv, begin: d->begin, vbegin: d->vbegin, end: d->end, vend: d->vend, section: d->section);
31518 p = &(*p)->dw_loc_next;
31519 }
31520 else if (!dwarf_strict && d->expr)
31521 return 0;
31522 }
31523 if (list == NULL)
31524 return dwarf_strict ? -1 : 0;
31525 a->dw_attr_val.val_class = dw_val_class_loc_list;
31526 gen_llsym (list);
31527 *AT_loc_list_ptr (a) = list;
31528 return 1;
31529 case dw_val_class_loc:
31530 lv = copy_deref_exprloc (expr: AT_loc (a: av));
31531 if (lv == NULL)
31532 return dwarf_strict ? -1 : 0;
31533 a->dw_attr_val.v.val_loc = lv;
31534 return 1;
31535 default:
31536 gcc_unreachable ();
31537 }
31538}
31539
31540/* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31541 an address in .rodata section if the string literal is emitted there,
31542 or remove the containing location list or replace DW_AT_const_value
31543 with DW_AT_location and empty location expression, if it isn't found
31544 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31545 to something that has been emitted in the current CU. */
31546
31547static void
31548resolve_addr (dw_die_ref die)
31549{
31550 dw_die_ref c;
31551 dw_attr_node *a;
31552 dw_loc_list_ref *curr, *start, loc;
31553 unsigned ix;
31554 bool remove_AT_byte_size = false;
31555
31556 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31557 switch (AT_class (a))
31558 {
31559 case dw_val_class_loc_list:
31560 start = curr = AT_loc_list_ptr (a);
31561 loc = *curr;
31562 gcc_assert (loc);
31563 /* The same list can be referenced more than once. See if we have
31564 already recorded the result from a previous pass. */
31565 if (loc->replaced)
31566 *curr = loc->dw_loc_next;
31567 else if (!loc->resolved_addr)
31568 {
31569 /* As things stand, we do not expect or allow one die to
31570 reference a suffix of another die's location list chain.
31571 References must be identical or completely separate.
31572 There is therefore no need to cache the result of this
31573 pass on any list other than the first; doing so
31574 would lead to unnecessary writes. */
31575 while (*curr)
31576 {
31577 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31578 if (!resolve_addr_in_expr (a, loc: (*curr)->expr))
31579 {
31580 dw_loc_list_ref next = (*curr)->dw_loc_next;
31581 dw_loc_descr_ref l = (*curr)->expr;
31582
31583 if (next && (*curr)->ll_symbol)
31584 {
31585 gcc_assert (!next->ll_symbol);
31586 next->ll_symbol = (*curr)->ll_symbol;
31587 next->vl_symbol = (*curr)->vl_symbol;
31588 }
31589 if (dwarf_split_debug_info)
31590 remove_loc_list_addr_table_entries (descr: l);
31591 *curr = next;
31592 }
31593 else
31594 {
31595 mark_base_types (loc: (*curr)->expr);
31596 curr = &(*curr)->dw_loc_next;
31597 }
31598 }
31599 if (loc == *start)
31600 loc->resolved_addr = 1;
31601 else
31602 {
31603 loc->replaced = 1;
31604 loc->dw_loc_next = *start;
31605 }
31606 }
31607 if (!*start)
31608 {
31609 remove_AT (die, attr_kind: a->dw_attr);
31610 ix--;
31611 }
31612 break;
31613 case dw_val_class_view_list:
31614 {
31615 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31616 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31617 dw_val_node *llnode
31618 = view_list_to_loc_list_val_node (val: &a->dw_attr_val);
31619 /* If we no longer have a loclist, or it no longer needs
31620 views, drop this attribute. */
31621 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31622 {
31623 remove_AT (die, attr_kind: a->dw_attr);
31624 ix--;
31625 }
31626 break;
31627 }
31628 case dw_val_class_loc:
31629 {
31630 dw_loc_descr_ref l = AT_loc (a);
31631 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31632 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31633 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31634 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31635 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31636 with DW_FORM_ref referencing the same DIE as
31637 DW_OP_GNU_variable_value used to reference. */
31638 if (a->dw_attr == DW_AT_string_length
31639 && l
31640 && l->dw_loc_opc == DW_OP_GNU_variable_value
31641 && (l->dw_loc_next == NULL
31642 || (l->dw_loc_next->dw_loc_next == NULL
31643 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31644 {
31645 switch (optimize_string_length (a))
31646 {
31647 case -1:
31648 remove_AT (die, attr_kind: a->dw_attr);
31649 ix--;
31650 /* If we drop DW_AT_string_length, we need to drop also
31651 DW_AT_{string_length_,}byte_size. */
31652 remove_AT_byte_size = true;
31653 continue;
31654 default:
31655 break;
31656 case 1:
31657 /* Even if we keep the optimized DW_AT_string_length,
31658 it might have changed AT_class, so process it again. */
31659 ix--;
31660 continue;
31661 }
31662 }
31663 /* For -gdwarf-2 don't attempt to optimize
31664 DW_AT_data_member_location containing
31665 DW_OP_plus_uconst - older consumers might
31666 rely on it being that op instead of a more complex,
31667 but shorter, location description. */
31668 if ((dwarf_version > 2
31669 || a->dw_attr != DW_AT_data_member_location
31670 || l == NULL
31671 || l->dw_loc_opc != DW_OP_plus_uconst
31672 || l->dw_loc_next != NULL)
31673 && !resolve_addr_in_expr (a, loc: l))
31674 {
31675 if (dwarf_split_debug_info)
31676 remove_loc_list_addr_table_entries (descr: l);
31677 if (l != NULL
31678 && l->dw_loc_next == NULL
31679 && l->dw_loc_opc == DW_OP_addr
31680 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31681 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31682 && a->dw_attr == DW_AT_location)
31683 {
31684 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31685 remove_AT (die, attr_kind: a->dw_attr);
31686 ix--;
31687 optimize_location_into_implicit_ptr (die, decl);
31688 break;
31689 }
31690 if (a->dw_attr == DW_AT_string_length)
31691 /* If we drop DW_AT_string_length, we need to drop also
31692 DW_AT_{string_length_,}byte_size. */
31693 remove_AT_byte_size = true;
31694 remove_AT (die, attr_kind: a->dw_attr);
31695 ix--;
31696 }
31697 else
31698 mark_base_types (loc: l);
31699 }
31700 break;
31701 case dw_val_class_addr:
31702 if (a->dw_attr == DW_AT_const_value
31703 && !resolve_one_addr (addr: &a->dw_attr_val.v.val_addr))
31704 {
31705 if (AT_index (a) != NOT_INDEXED)
31706 remove_addr_table_entry (entry: a->dw_attr_val.val_entry);
31707 remove_AT (die, attr_kind: a->dw_attr);
31708 ix--;
31709 }
31710 if ((die->die_tag == DW_TAG_call_site
31711 && a->dw_attr == DW_AT_call_origin)
31712 || (die->die_tag == DW_TAG_GNU_call_site
31713 && a->dw_attr == DW_AT_abstract_origin))
31714 {
31715 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31716 dw_die_ref tdie = lookup_decl_die (decl: tdecl);
31717 dw_die_ref cdie;
31718 if (tdie == NULL
31719 && DECL_EXTERNAL (tdecl)
31720 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31721 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31722 {
31723 dw_die_ref pdie = cdie;
31724 /* Make sure we don't add these DIEs into type units.
31725 We could emit skeleton DIEs for context (namespaces,
31726 outer structs/classes) and a skeleton DIE for the
31727 innermost context with DW_AT_signature pointing to the
31728 type unit. See PR78835. */
31729 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31730 pdie = pdie->die_parent;
31731 if (pdie == NULL)
31732 {
31733 /* Creating a full DIE for tdecl is overly expensive and
31734 at this point even wrong when in the LTO phase
31735 as it can end up generating new type DIEs we didn't
31736 output and thus optimize_external_refs will crash. */
31737 tdie = new_die (tag_value: DW_TAG_subprogram, parent_die: cdie, NULL_TREE);
31738 add_AT_flag (die: tdie, attr_kind: DW_AT_external, flag: 1);
31739 add_AT_flag (die: tdie, attr_kind: DW_AT_declaration, flag: 1);
31740 add_linkage_attr (die: tdie, decl: tdecl);
31741 add_name_and_src_coords_attributes (die: tdie, decl: tdecl, no_linkage_name: true);
31742 equate_decl_number_to_die (decl: tdecl, decl_die: tdie);
31743 }
31744 }
31745 if (tdie)
31746 {
31747 a->dw_attr_val.val_class = dw_val_class_die_ref;
31748 a->dw_attr_val.v.val_die_ref.die = tdie;
31749 a->dw_attr_val.v.val_die_ref.external = 0;
31750 }
31751 else
31752 {
31753 if (AT_index (a) != NOT_INDEXED)
31754 remove_addr_table_entry (entry: a->dw_attr_val.val_entry);
31755 remove_AT (die, attr_kind: a->dw_attr);
31756 ix--;
31757 }
31758 }
31759 break;
31760 default:
31761 break;
31762 }
31763
31764 if (remove_AT_byte_size)
31765 remove_AT (die, dwarf_version >= 5
31766 ? DW_AT_string_length_byte_size
31767 : DW_AT_byte_size);
31768
31769 FOR_EACH_CHILD (die, c, resolve_addr (c));
31770}
31771
31772/* Helper routines for optimize_location_lists.
31773 This pass tries to share identical local lists in .debug_loc
31774 section. */
31775
31776/* Iteratively hash operands of LOC opcode into HSTATE. */
31777
31778static void
31779hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31780{
31781 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31782 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31783
31784 switch (loc->dw_loc_opc)
31785 {
31786 case DW_OP_const4u:
31787 case DW_OP_const8u:
31788 if (loc->dw_loc_dtprel)
31789 goto hash_addr;
31790 /* FALLTHRU */
31791 case DW_OP_const1u:
31792 case DW_OP_const1s:
31793 case DW_OP_const2u:
31794 case DW_OP_const2s:
31795 case DW_OP_const4s:
31796 case DW_OP_const8s:
31797 case DW_OP_constu:
31798 case DW_OP_consts:
31799 case DW_OP_pick:
31800 case DW_OP_plus_uconst:
31801 case DW_OP_breg0:
31802 case DW_OP_breg1:
31803 case DW_OP_breg2:
31804 case DW_OP_breg3:
31805 case DW_OP_breg4:
31806 case DW_OP_breg5:
31807 case DW_OP_breg6:
31808 case DW_OP_breg7:
31809 case DW_OP_breg8:
31810 case DW_OP_breg9:
31811 case DW_OP_breg10:
31812 case DW_OP_breg11:
31813 case DW_OP_breg12:
31814 case DW_OP_breg13:
31815 case DW_OP_breg14:
31816 case DW_OP_breg15:
31817 case DW_OP_breg16:
31818 case DW_OP_breg17:
31819 case DW_OP_breg18:
31820 case DW_OP_breg19:
31821 case DW_OP_breg20:
31822 case DW_OP_breg21:
31823 case DW_OP_breg22:
31824 case DW_OP_breg23:
31825 case DW_OP_breg24:
31826 case DW_OP_breg25:
31827 case DW_OP_breg26:
31828 case DW_OP_breg27:
31829 case DW_OP_breg28:
31830 case DW_OP_breg29:
31831 case DW_OP_breg30:
31832 case DW_OP_breg31:
31833 case DW_OP_regx:
31834 case DW_OP_fbreg:
31835 case DW_OP_piece:
31836 case DW_OP_deref_size:
31837 case DW_OP_xderef_size:
31838 hstate.add_object (obj&: val1->v.val_int);
31839 break;
31840 case DW_OP_skip:
31841 case DW_OP_bra:
31842 {
31843 int offset;
31844
31845 gcc_assert (val1->val_class == dw_val_class_loc);
31846 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31847 hstate.add_object (obj&: offset);
31848 }
31849 break;
31850 case DW_OP_implicit_value:
31851 hstate.add_object (obj&: val1->v.val_unsigned);
31852 switch (val2->val_class)
31853 {
31854 case dw_val_class_const:
31855 hstate.add_object (obj&: val2->v.val_int);
31856 break;
31857 case dw_val_class_vec:
31858 {
31859 unsigned int elt_size = val2->v.val_vec.elt_size;
31860 unsigned int len = val2->v.val_vec.length;
31861
31862 hstate.add_int (v: elt_size);
31863 hstate.add_int (v: len);
31864 hstate.add (data: val2->v.val_vec.array, len: len * elt_size);
31865 }
31866 break;
31867 case dw_val_class_const_double:
31868 hstate.add_object (obj&: val2->v.val_double.low);
31869 hstate.add_object (obj&: val2->v.val_double.high);
31870 break;
31871 case dw_val_class_wide_int:
31872 hstate.add (data: val2->v.val_wide->get_val (),
31873 len: get_full_len (op: *val2->v.val_wide)
31874 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31875 break;
31876 case dw_val_class_addr:
31877 inchash::add_rtx (val2->v.val_addr, hstate);
31878 break;
31879 default:
31880 gcc_unreachable ();
31881 }
31882 break;
31883 case DW_OP_bregx:
31884 case DW_OP_bit_piece:
31885 hstate.add_object (obj&: val1->v.val_int);
31886 hstate.add_object (obj&: val2->v.val_int);
31887 break;
31888 case DW_OP_addr:
31889 hash_addr:
31890 if (loc->dw_loc_dtprel)
31891 {
31892 unsigned char dtprel = 0xd1;
31893 hstate.add_object (obj&: dtprel);
31894 }
31895 inchash::add_rtx (val1->v.val_addr, hstate);
31896 break;
31897 case DW_OP_GNU_addr_index:
31898 case DW_OP_addrx:
31899 case DW_OP_GNU_const_index:
31900 case DW_OP_constx:
31901 {
31902 if (loc->dw_loc_dtprel)
31903 {
31904 unsigned char dtprel = 0xd1;
31905 hstate.add_object (obj&: dtprel);
31906 }
31907 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31908 }
31909 break;
31910 case DW_OP_implicit_pointer:
31911 case DW_OP_GNU_implicit_pointer:
31912 hstate.add_int (v: val2->v.val_int);
31913 break;
31914 case DW_OP_entry_value:
31915 case DW_OP_GNU_entry_value:
31916 hstate.add_object (obj&: val1->v.val_loc);
31917 break;
31918 case DW_OP_regval_type:
31919 case DW_OP_deref_type:
31920 case DW_OP_GNU_regval_type:
31921 case DW_OP_GNU_deref_type:
31922 {
31923 unsigned int byte_size
31924 = get_AT_unsigned (die: val2->v.val_die_ref.die, attr_kind: DW_AT_byte_size);
31925 unsigned int encoding
31926 = get_AT_unsigned (die: val2->v.val_die_ref.die, attr_kind: DW_AT_encoding);
31927 hstate.add_object (obj&: val1->v.val_int);
31928 hstate.add_object (obj&: byte_size);
31929 hstate.add_object (obj&: encoding);
31930 }
31931 break;
31932 case DW_OP_convert:
31933 case DW_OP_reinterpret:
31934 case DW_OP_GNU_convert:
31935 case DW_OP_GNU_reinterpret:
31936 if (val1->val_class == dw_val_class_unsigned_const)
31937 {
31938 hstate.add_object (obj&: val1->v.val_unsigned);
31939 break;
31940 }
31941 /* FALLTHRU */
31942 case DW_OP_const_type:
31943 case DW_OP_GNU_const_type:
31944 {
31945 unsigned int byte_size
31946 = get_AT_unsigned (die: val1->v.val_die_ref.die, attr_kind: DW_AT_byte_size);
31947 unsigned int encoding
31948 = get_AT_unsigned (die: val1->v.val_die_ref.die, attr_kind: DW_AT_encoding);
31949 hstate.add_object (obj&: byte_size);
31950 hstate.add_object (obj&: encoding);
31951 if (loc->dw_loc_opc != DW_OP_const_type
31952 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31953 break;
31954 hstate.add_object (obj&: val2->val_class);
31955 switch (val2->val_class)
31956 {
31957 case dw_val_class_const:
31958 hstate.add_object (obj&: val2->v.val_int);
31959 break;
31960 case dw_val_class_vec:
31961 {
31962 unsigned int elt_size = val2->v.val_vec.elt_size;
31963 unsigned int len = val2->v.val_vec.length;
31964
31965 hstate.add_object (obj&: elt_size);
31966 hstate.add_object (obj&: len);
31967 hstate.add (data: val2->v.val_vec.array, len: len * elt_size);
31968 }
31969 break;
31970 case dw_val_class_const_double:
31971 hstate.add_object (obj&: val2->v.val_double.low);
31972 hstate.add_object (obj&: val2->v.val_double.high);
31973 break;
31974 case dw_val_class_wide_int:
31975 hstate.add (data: val2->v.val_wide->get_val (),
31976 len: get_full_len (op: *val2->v.val_wide)
31977 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31978 break;
31979 default:
31980 gcc_unreachable ();
31981 }
31982 }
31983 break;
31984
31985 default:
31986 /* Other codes have no operands. */
31987 break;
31988 }
31989}
31990
31991/* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31992
31993static inline void
31994hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31995{
31996 dw_loc_descr_ref l;
31997 bool sizes_computed = false;
31998 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31999 size_of_locs (loc);
32000
32001 for (l = loc; l != NULL; l = l->dw_loc_next)
32002 {
32003 enum dwarf_location_atom opc = l->dw_loc_opc;
32004 hstate.add_object (obj&: opc);
32005 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
32006 {
32007 size_of_locs (loc);
32008 sizes_computed = true;
32009 }
32010 hash_loc_operands (loc: l, hstate);
32011 }
32012}
32013
32014/* Compute hash of the whole location list LIST_HEAD. */
32015
32016static inline void
32017hash_loc_list (dw_loc_list_ref list_head)
32018{
32019 dw_loc_list_ref curr = list_head;
32020 inchash::hash hstate;
32021
32022 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
32023 {
32024 hstate.add (data: curr->begin, len: strlen (s: curr->begin) + 1);
32025 hstate.add (data: curr->end, len: strlen (s: curr->end) + 1);
32026 hstate.add_object (obj&: curr->vbegin);
32027 hstate.add_object (obj&: curr->vend);
32028 if (curr->section)
32029 hstate.add (data: curr->section, len: strlen (s: curr->section) + 1);
32030 hash_locs (loc: curr->expr, hstate);
32031 }
32032 list_head->hash = hstate.end ();
32033}
32034
32035/* Return true if X and Y opcodes have the same operands. */
32036
32037static inline bool
32038compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
32039{
32040 dw_val_ref valx1 = &x->dw_loc_oprnd1;
32041 dw_val_ref valx2 = &x->dw_loc_oprnd2;
32042 dw_val_ref valy1 = &y->dw_loc_oprnd1;
32043 dw_val_ref valy2 = &y->dw_loc_oprnd2;
32044
32045 switch (x->dw_loc_opc)
32046 {
32047 case DW_OP_const4u:
32048 case DW_OP_const8u:
32049 if (x->dw_loc_dtprel)
32050 goto hash_addr;
32051 /* FALLTHRU */
32052 case DW_OP_const1u:
32053 case DW_OP_const1s:
32054 case DW_OP_const2u:
32055 case DW_OP_const2s:
32056 case DW_OP_const4s:
32057 case DW_OP_const8s:
32058 case DW_OP_constu:
32059 case DW_OP_consts:
32060 case DW_OP_pick:
32061 case DW_OP_plus_uconst:
32062 case DW_OP_breg0:
32063 case DW_OP_breg1:
32064 case DW_OP_breg2:
32065 case DW_OP_breg3:
32066 case DW_OP_breg4:
32067 case DW_OP_breg5:
32068 case DW_OP_breg6:
32069 case DW_OP_breg7:
32070 case DW_OP_breg8:
32071 case DW_OP_breg9:
32072 case DW_OP_breg10:
32073 case DW_OP_breg11:
32074 case DW_OP_breg12:
32075 case DW_OP_breg13:
32076 case DW_OP_breg14:
32077 case DW_OP_breg15:
32078 case DW_OP_breg16:
32079 case DW_OP_breg17:
32080 case DW_OP_breg18:
32081 case DW_OP_breg19:
32082 case DW_OP_breg20:
32083 case DW_OP_breg21:
32084 case DW_OP_breg22:
32085 case DW_OP_breg23:
32086 case DW_OP_breg24:
32087 case DW_OP_breg25:
32088 case DW_OP_breg26:
32089 case DW_OP_breg27:
32090 case DW_OP_breg28:
32091 case DW_OP_breg29:
32092 case DW_OP_breg30:
32093 case DW_OP_breg31:
32094 case DW_OP_regx:
32095 case DW_OP_fbreg:
32096 case DW_OP_piece:
32097 case DW_OP_deref_size:
32098 case DW_OP_xderef_size:
32099 return valx1->v.val_int == valy1->v.val_int;
32100 case DW_OP_skip:
32101 case DW_OP_bra:
32102 /* If splitting debug info, the use of DW_OP_GNU_addr_index
32103 can cause irrelevant differences in dw_loc_addr. */
32104 gcc_assert (valx1->val_class == dw_val_class_loc
32105 && valy1->val_class == dw_val_class_loc
32106 && (dwarf_split_debug_info
32107 || x->dw_loc_addr == y->dw_loc_addr));
32108 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
32109 case DW_OP_implicit_value:
32110 if (valx1->v.val_unsigned != valy1->v.val_unsigned
32111 || valx2->val_class != valy2->val_class)
32112 return false;
32113 switch (valx2->val_class)
32114 {
32115 case dw_val_class_const:
32116 return valx2->v.val_int == valy2->v.val_int;
32117 case dw_val_class_vec:
32118 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32119 && valx2->v.val_vec.length == valy2->v.val_vec.length
32120 && memcmp (s1: valx2->v.val_vec.array, s2: valy2->v.val_vec.array,
32121 n: valx2->v.val_vec.elt_size
32122 * valx2->v.val_vec.length) == 0;
32123 case dw_val_class_const_double:
32124 return valx2->v.val_double.low == valy2->v.val_double.low
32125 && valx2->v.val_double.high == valy2->v.val_double.high;
32126 case dw_val_class_wide_int:
32127 return *valx2->v.val_wide == *valy2->v.val_wide;
32128 case dw_val_class_addr:
32129 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
32130 default:
32131 gcc_unreachable ();
32132 }
32133 case DW_OP_bregx:
32134 case DW_OP_bit_piece:
32135 return valx1->v.val_int == valy1->v.val_int
32136 && valx2->v.val_int == valy2->v.val_int;
32137 case DW_OP_addr:
32138 hash_addr:
32139 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
32140 case DW_OP_GNU_addr_index:
32141 case DW_OP_addrx:
32142 case DW_OP_GNU_const_index:
32143 case DW_OP_constx:
32144 {
32145 rtx ax1 = valx1->val_entry->addr.rtl;
32146 rtx ay1 = valy1->val_entry->addr.rtl;
32147 return rtx_equal_p (ax1, ay1);
32148 }
32149 case DW_OP_implicit_pointer:
32150 case DW_OP_GNU_implicit_pointer:
32151 return valx1->val_class == dw_val_class_die_ref
32152 && valx1->val_class == valy1->val_class
32153 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
32154 && valx2->v.val_int == valy2->v.val_int;
32155 case DW_OP_entry_value:
32156 case DW_OP_GNU_entry_value:
32157 return compare_loc_operands (x: valx1->v.val_loc, y: valy1->v.val_loc);
32158 case DW_OP_const_type:
32159 case DW_OP_GNU_const_type:
32160 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
32161 || valx2->val_class != valy2->val_class)
32162 return false;
32163 switch (valx2->val_class)
32164 {
32165 case dw_val_class_const:
32166 return valx2->v.val_int == valy2->v.val_int;
32167 case dw_val_class_vec:
32168 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
32169 && valx2->v.val_vec.length == valy2->v.val_vec.length
32170 && memcmp (s1: valx2->v.val_vec.array, s2: valy2->v.val_vec.array,
32171 n: valx2->v.val_vec.elt_size
32172 * valx2->v.val_vec.length) == 0;
32173 case dw_val_class_const_double:
32174 return valx2->v.val_double.low == valy2->v.val_double.low
32175 && valx2->v.val_double.high == valy2->v.val_double.high;
32176 case dw_val_class_wide_int:
32177 return *valx2->v.val_wide == *valy2->v.val_wide;
32178 default:
32179 gcc_unreachable ();
32180 }
32181 case DW_OP_regval_type:
32182 case DW_OP_deref_type:
32183 case DW_OP_GNU_regval_type:
32184 case DW_OP_GNU_deref_type:
32185 return valx1->v.val_int == valy1->v.val_int
32186 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
32187 case DW_OP_convert:
32188 case DW_OP_reinterpret:
32189 case DW_OP_GNU_convert:
32190 case DW_OP_GNU_reinterpret:
32191 if (valx1->val_class != valy1->val_class)
32192 return false;
32193 if (valx1->val_class == dw_val_class_unsigned_const)
32194 return valx1->v.val_unsigned == valy1->v.val_unsigned;
32195 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32196 case DW_OP_GNU_parameter_ref:
32197 return valx1->val_class == dw_val_class_die_ref
32198 && valx1->val_class == valy1->val_class
32199 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
32200 default:
32201 /* Other codes have no operands. */
32202 return true;
32203 }
32204}
32205
32206/* Return true if DWARF location expressions X and Y are the same. */
32207
32208static inline bool
32209compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
32210{
32211 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
32212 if (x->dw_loc_opc != y->dw_loc_opc
32213 || x->dw_loc_dtprel != y->dw_loc_dtprel
32214 || !compare_loc_operands (x, y))
32215 break;
32216 return x == NULL && y == NULL;
32217}
32218
32219/* Hashtable helpers. */
32220
32221struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
32222{
32223 static inline hashval_t hash (const dw_loc_list_struct *);
32224 static inline bool equal (const dw_loc_list_struct *,
32225 const dw_loc_list_struct *);
32226};
32227
32228/* Return precomputed hash of location list X. */
32229
32230inline hashval_t
32231loc_list_hasher::hash (const dw_loc_list_struct *x)
32232{
32233 return x->hash;
32234}
32235
32236/* Return true if location lists A and B are the same. */
32237
32238inline bool
32239loc_list_hasher::equal (const dw_loc_list_struct *a,
32240 const dw_loc_list_struct *b)
32241{
32242 if (a == b)
32243 return true;
32244 if (a->hash != b->hash)
32245 return false;
32246 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32247 if (strcmp (s1: a->begin, s2: b->begin) != 0
32248 || strcmp (s1: a->end, s2: b->end) != 0
32249 || (a->section == NULL) != (b->section == NULL)
32250 || (a->section && strcmp (s1: a->section, s2: b->section) != 0)
32251 || a->vbegin != b->vbegin || a->vend != b->vend
32252 || !compare_locs (x: a->expr, y: b->expr))
32253 break;
32254 return a == NULL && b == NULL;
32255}
32256
32257typedef hash_table<loc_list_hasher> loc_list_hash_type;
32258
32259
32260/* Recursively optimize location lists referenced from DIE
32261 children and share them whenever possible. */
32262
32263static void
32264optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32265{
32266 dw_die_ref c;
32267 dw_attr_node *a;
32268 unsigned ix;
32269 dw_loc_list_struct **slot;
32270 bool drop_locviews = false;
32271 bool has_locviews = false;
32272
32273 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32274 if (AT_class (a) == dw_val_class_loc_list)
32275 {
32276 dw_loc_list_ref list = AT_loc_list (a);
32277 /* TODO: perform some optimizations here, before hashing
32278 it and storing into the hash table. */
32279 hash_loc_list (list_head: list);
32280 slot = htab->find_slot_with_hash (comparable: list, hash: list->hash, insert: INSERT);
32281 if (*slot == NULL)
32282 {
32283 *slot = list;
32284 if (loc_list_has_views (list))
32285 gcc_assert (list->vl_symbol);
32286 else if (list->vl_symbol)
32287 {
32288 drop_locviews = true;
32289 list->vl_symbol = NULL;
32290 }
32291 }
32292 else
32293 {
32294 if (list->vl_symbol && !(*slot)->vl_symbol)
32295 drop_locviews = true;
32296 a->dw_attr_val.v.val_loc_list = *slot;
32297 }
32298 }
32299 else if (AT_class (a) == dw_val_class_view_list)
32300 {
32301 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32302 has_locviews = true;
32303 }
32304
32305
32306 if (drop_locviews && has_locviews)
32307 remove_AT (die, attr_kind: DW_AT_GNU_locviews);
32308
32309 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32310}
32311
32312
32313/* Recursively assign each location list a unique index into the debug_addr
32314 section. */
32315
32316static void
32317index_location_lists (dw_die_ref die)
32318{
32319 dw_die_ref c;
32320 dw_attr_node *a;
32321 unsigned ix;
32322
32323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32324 if (AT_class (a) == dw_val_class_loc_list)
32325 {
32326 dw_loc_list_ref list = AT_loc_list (a);
32327 dw_loc_list_ref curr;
32328 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32329 {
32330 /* Don't index an entry that has already been indexed
32331 or won't be output. Make sure skip_loc_list_entry doesn't
32332 call size_of_locs, because that might cause circular dependency,
32333 index_location_lists requiring address table indexes to be
32334 computed, but adding new indexes through add_addr_table_entry
32335 and address table index computation requiring no new additions
32336 to the hash table. In the rare case of DWARF[234] >= 64KB
32337 location expression, we'll just waste unused address table entry
32338 for it. */
32339 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32340 continue;
32341
32342 curr->begin_entry
32343 = add_addr_table_entry (addr: xstrdup (curr->begin), kind: ate_kind_label);
32344 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32345 curr->end_entry
32346 = add_addr_table_entry (addr: xstrdup (curr->end), kind: ate_kind_label);
32347 }
32348 }
32349
32350 FOR_EACH_CHILD (die, c, index_location_lists (c));
32351}
32352
32353/* Optimize location lists referenced from DIE
32354 children and share them whenever possible. */
32355
32356static void
32357optimize_location_lists (dw_die_ref die)
32358{
32359 loc_list_hash_type htab (500);
32360 optimize_location_lists_1 (die, htab: &htab);
32361}
32362
32363/* Traverse the limbo die list, and add parent/child links. The only
32364 dies without parents that should be here are concrete instances of
32365 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32366 For concrete instances, we can get the parent die from the abstract
32367 instance. */
32368
32369static void
32370flush_limbo_die_list (void)
32371{
32372 limbo_die_node *node;
32373
32374 /* get_context_die calls force_decl_die, which can put new DIEs on the
32375 limbo list in LTO mode when nested functions are put in a different
32376 partition than that of their parent function. */
32377 while ((node = limbo_die_list))
32378 {
32379 dw_die_ref die = node->die;
32380 limbo_die_list = node->next;
32381
32382 if (die->die_parent == NULL)
32383 {
32384 dw_die_ref origin = get_AT_ref (die, attr_kind: DW_AT_abstract_origin);
32385
32386 if (origin && origin->die_parent)
32387 add_child_die (die: origin->die_parent, child_die: die);
32388 else if (is_cu_die (c: die))
32389 ;
32390 else if (seen_error ())
32391 /* It's OK to be confused by errors in the input. */
32392 add_child_die (die: comp_unit_die (), child_die: die);
32393 else
32394 {
32395 /* In certain situations, the lexical block containing a
32396 nested function can be optimized away, which results
32397 in the nested function die being orphaned. Likewise
32398 with the return type of that nested function. Force
32399 this to be a child of the containing function.
32400
32401 It may happen that even the containing function got fully
32402 inlined and optimized out. In that case we are lost and
32403 assign the empty child. This should not be big issue as
32404 the function is likely unreachable too. */
32405 gcc_assert (node->created_for);
32406
32407 if (DECL_P (node->created_for))
32408 origin = get_context_die (DECL_CONTEXT (node->created_for));
32409 else if (TYPE_P (node->created_for))
32410 origin = scope_die_for (t: node->created_for, context_die: comp_unit_die ());
32411 else
32412 origin = comp_unit_die ();
32413
32414 add_child_die (die: origin, child_die: die);
32415 }
32416 }
32417 }
32418}
32419
32420/* Reset DIEs so we can output them again. */
32421
32422static void
32423reset_dies (dw_die_ref die)
32424{
32425 dw_die_ref c;
32426
32427 /* Remove stuff we re-generate. */
32428 die->die_mark = 0;
32429 die->die_offset = 0;
32430 die->die_abbrev = 0;
32431 remove_AT (die, attr_kind: DW_AT_sibling);
32432
32433 FOR_EACH_CHILD (die, c, reset_dies (c));
32434}
32435
32436/* reset_indirect_string removed the references coming from DW_AT_name
32437 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32438 .debug_line_str strings again. */
32439
32440static void
32441adjust_name_comp_dir (dw_die_ref die)
32442{
32443 for (int i = 0; i < 2; i++)
32444 {
32445 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32446 dw_attr_node *a = get_AT (die, attr_kind);
32447 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32448 continue;
32449
32450 if (!debug_line_str_hash)
32451 debug_line_str_hash
32452 = hash_table<indirect_string_hasher>::create_ggc (n: 10);
32453
32454 struct indirect_string_node *node
32455 = find_AT_string_in_table (str: a->dw_attr_val.v.val_str->str,
32456 table: debug_line_str_hash);
32457 set_indirect_string (node);
32458 node->form = DW_FORM_line_strp;
32459 a->dw_attr_val.v.val_str = node;
32460 }
32461}
32462
32463/* Output stuff that dwarf requires at the end of every file,
32464 and generate the DWARF-2 debugging info. */
32465
32466static void
32467dwarf2out_finish (const char *filename)
32468{
32469 comdat_type_node *ctnode;
32470 dw_die_ref main_comp_unit_die;
32471 unsigned char checksum[16];
32472 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32473
32474 /* Generate CTF/BTF debug info. */
32475 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32476 || btf_debuginfo_p ()) && lang_GNU_C ())
32477 ctf_debug_finish ();
32478
32479#ifdef CODEVIEW_DEBUGGING_INFO
32480 if (codeview_debuginfo_p ())
32481 codeview_debug_finish ();
32482#endif
32483
32484 /* Skip emitting DWARF if not required. */
32485 if (!dwarf_debuginfo_p ())
32486 return;
32487
32488 /* Flush out any latecomers to the limbo party. */
32489 flush_limbo_die_list ();
32490
32491 if (inline_entry_data_table)
32492 gcc_assert (inline_entry_data_table->is_empty ());
32493
32494 if (flag_checking)
32495 {
32496 verify_die (die: comp_unit_die ());
32497 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32498 verify_die (die: node->die);
32499 }
32500
32501 /* We shouldn't have any symbols with delayed asm names for
32502 DIEs generated after early finish. */
32503 gcc_assert (deferred_asm_name == NULL);
32504
32505 gen_remaining_tmpl_value_param_die_attribute ();
32506
32507 if (flag_generate_lto || flag_generate_offload)
32508 {
32509 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32510
32511 /* Prune stuff so that dwarf2out_finish runs successfully
32512 for the fat part of the object. */
32513 reset_dies (die: comp_unit_die ());
32514 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32515 reset_dies (die: node->die);
32516 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32517 {
32518 /* Remove the pointer to the line table. */
32519 remove_AT (die: ctnode->root_die, attr_kind: DW_AT_stmt_list);
32520 if (debug_info_level >= DINFO_LEVEL_TERSE)
32521 reset_dies (die: ctnode->root_die);
32522 }
32523
32524 /* Reset die CU symbol so we don't output it twice. */
32525 comp_unit_die ()->die_id.die_symbol = NULL;
32526
32527 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32528 remove_AT (die: comp_unit_die (), attr_kind: DW_AT_stmt_list);
32529 if (have_macinfo)
32530 remove_AT (die: comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32531
32532 /* Remove indirect string decisions. */
32533 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32534 if (debug_line_str_hash)
32535 {
32536 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32537 debug_line_str_hash = NULL;
32538 if (asm_outputs_debug_line_str ())
32539 {
32540 adjust_name_comp_dir (die: comp_unit_die ());
32541 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32542 adjust_name_comp_dir (die: node->die);
32543 }
32544 }
32545 }
32546
32547#if ENABLE_ASSERT_CHECKING
32548 {
32549 dw_die_ref die = comp_unit_die (), c;
32550 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32551 }
32552#endif
32553 base_types.truncate (size: 0);
32554 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32555 resolve_addr (die: ctnode->root_die);
32556 resolve_addr (die: comp_unit_die ());
32557 move_marked_base_types ();
32558
32559 if (dump_file)
32560 {
32561 fprintf (stream: dump_file, format: "DWARF for %s\n", filename);
32562 print_die (die: comp_unit_die (), outfile: dump_file);
32563 }
32564
32565 /* Initialize sections and labels used for actual assembler output. */
32566 unsigned generation = init_sections_and_labels (early_lto_debug: false);
32567
32568 /* Traverse the DIE's and add sibling attributes to those DIE's that
32569 have children. */
32570 add_sibling_attributes (die: comp_unit_die ());
32571 limbo_die_node *node;
32572 for (node = cu_die_list; node; node = node->next)
32573 add_sibling_attributes (die: node->die);
32574 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32575 add_sibling_attributes (die: ctnode->root_die);
32576
32577 /* When splitting DWARF info, we put some attributes in the
32578 skeleton compile_unit DIE that remains in the .o, while
32579 most attributes go in the DWO compile_unit_die. */
32580 if (dwarf_split_debug_info)
32581 {
32582 limbo_die_node *cu;
32583 main_comp_unit_die = gen_compile_unit_die (NULL);
32584 if (dwarf_version >= 5)
32585 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32586 cu = limbo_die_list;
32587 gcc_assert (cu->die == main_comp_unit_die);
32588 limbo_die_list = limbo_die_list->next;
32589 cu->next = cu_die_list;
32590 cu_die_list = cu;
32591 }
32592 else
32593 main_comp_unit_die = comp_unit_die ();
32594
32595 /* Output a terminator label for the .text section. */
32596 switch_to_section (text_section);
32597 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32598 if (cold_text_section)
32599 {
32600 switch_to_section (cold_text_section);
32601 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32602 }
32603
32604 /* We can only use the low/high_pc attributes if all of the code was
32605 in .text. */
32606 if ((!have_multiple_function_sections
32607 && vec_safe_length (v: switch_text_ranges) < 2)
32608 || (dwarf_version < 3 && dwarf_strict))
32609 {
32610 const char *end_label = text_end_label;
32611 if (vec_safe_length (v: switch_text_ranges) == 1)
32612 end_label = (*switch_text_ranges)[0];
32613 /* Don't add if the CU has no associated code. */
32614 if (switch_text_ranges)
32615 add_AT_low_high_pc (die: main_comp_unit_die, lbl_low: text_section_label,
32616 lbl_high: end_label, force_direct: true);
32617 }
32618 else
32619 {
32620 unsigned fde_idx;
32621 dw_fde_ref fde;
32622 bool range_list_added = false;
32623 if (switch_text_ranges)
32624 {
32625 const char *prev_loc = text_section_label;
32626 const char *loc;
32627 unsigned idx;
32628
32629 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32630 if (prev_loc)
32631 {
32632 add_ranges_by_labels (die: main_comp_unit_die, begin: prev_loc,
32633 end: loc, added: &range_list_added, force_direct: true);
32634 prev_loc = NULL;
32635 }
32636 else
32637 prev_loc = loc;
32638
32639 if (prev_loc)
32640 add_ranges_by_labels (die: main_comp_unit_die, begin: prev_loc,
32641 end: text_end_label, added: &range_list_added, force_direct: true);
32642 }
32643
32644 if (switch_cold_ranges)
32645 {
32646 const char *prev_loc = cold_text_section_label;
32647 const char *loc;
32648 unsigned idx;
32649
32650 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32651 if (prev_loc)
32652 {
32653 add_ranges_by_labels (die: main_comp_unit_die, begin: prev_loc,
32654 end: loc, added: &range_list_added, force_direct: true);
32655 prev_loc = NULL;
32656 }
32657 else
32658 prev_loc = loc;
32659
32660 if (prev_loc)
32661 add_ranges_by_labels (die: main_comp_unit_die, begin: prev_loc,
32662 end: cold_end_label, added: &range_list_added, force_direct: true);
32663 }
32664
32665 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32666 {
32667 if (fde->ignored_debug)
32668 continue;
32669 if (!fde->in_std_section)
32670 add_ranges_by_labels (die: main_comp_unit_die, begin: fde->dw_fde_begin,
32671 end: fde->dw_fde_end, added: &range_list_added,
32672 force_direct: true);
32673 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32674 add_ranges_by_labels (die: main_comp_unit_die, begin: fde->dw_fde_second_begin,
32675 end: fde->dw_fde_second_end, added: &range_list_added,
32676 force_direct: true);
32677 }
32678
32679 if (range_list_added)
32680 {
32681 /* We need to give .debug_loc and .debug_ranges an appropriate
32682 "base address". Use zero so that these addresses become
32683 absolute. Historically, we've emitted the unexpected
32684 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32685 Emit both to give time for other tools to adapt. */
32686 add_AT_addr (die: main_comp_unit_die, attr_kind: DW_AT_low_pc, const0_rtx, force_direct: true);
32687 if (! dwarf_strict && dwarf_version < 4)
32688 add_AT_addr (die: main_comp_unit_die, attr_kind: DW_AT_entry_pc, const0_rtx, force_direct: true);
32689
32690 add_ranges (NULL);
32691 have_multiple_function_sections = true;
32692 }
32693 }
32694
32695 /* AIX Assembler inserts the length, so adjust the reference to match the
32696 offset expected by debuggers. */
32697 strcpy (dest: dl_section_ref, src: debug_line_section_label);
32698 if (XCOFF_DEBUGGING_INFO)
32699 strcat (dest: dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32700
32701 if (debug_info_level >= DINFO_LEVEL_TERSE)
32702 add_AT_lineptr (die: main_comp_unit_die, attr_kind: DW_AT_stmt_list,
32703 label: dl_section_ref);
32704
32705 if (have_macinfo)
32706 add_AT_macptr (die: comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32707 label: macinfo_section_label);
32708
32709 if (dwarf_split_debug_info)
32710 {
32711 if (have_location_lists)
32712 {
32713 /* Since we generate the loclists in the split DWARF .dwo
32714 file itself, we don't need to generate a loclists_base
32715 attribute for the split compile unit DIE. That attribute
32716 (and using relocatable sec_offset FORMs) isn't allowed
32717 for a split compile unit. Only if the .debug_loclists
32718 section was in the main file, would we need to generate a
32719 loclists_base attribute here (for the full or skeleton
32720 unit DIE). */
32721
32722 /* optimize_location_lists calculates the size of the lists,
32723 so index them first, and assign indices to the entries.
32724 Although optimize_location_lists will remove entries from
32725 the table, it only does so for duplicates, and therefore
32726 only reduces ref_counts to 1. */
32727 index_location_lists (die: comp_unit_die ());
32728 }
32729
32730 if (dwarf_version >= 5 && !vec_safe_is_empty (v: ranges_table))
32731 index_rnglists ();
32732
32733 if (addr_index_table != NULL)
32734 {
32735 unsigned int index = 0;
32736 addr_index_table
32737 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32738 (argument: &index);
32739 }
32740 }
32741
32742 loc_list_idx = 0;
32743 if (have_location_lists)
32744 {
32745 optimize_location_lists (die: comp_unit_die ());
32746 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32747 if (dwarf_version >= 5 && dwarf_split_debug_info)
32748 assign_location_list_indexes (die: comp_unit_die ());
32749 }
32750
32751 save_macinfo_strings ();
32752
32753 if (dwarf_split_debug_info)
32754 {
32755 unsigned int index = 0;
32756
32757 /* Add attributes common to skeleton compile_units and
32758 type_units. Because these attributes include strings, it
32759 must be done before freezing the string table. Top-level
32760 skeleton die attrs are added when the skeleton type unit is
32761 created, so ensure it is created by this point. */
32762 add_top_level_skeleton_die_attrs (die: main_comp_unit_die);
32763 debug_str_hash->traverse_noresize<unsigned int *, index_string> (argument: &index);
32764 }
32765
32766 /* Output all of the compilation units. We put the main one last so that
32767 the offsets are available to output_pubnames. */
32768 for (node = cu_die_list; node; node = node->next)
32769 output_comp_unit (die: node->die, output_if_empty: 0, NULL);
32770
32771 hash_table<comdat_type_hasher> comdat_type_table (100);
32772 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32773 {
32774 comdat_type_node **slot = comdat_type_table.find_slot (value: ctnode, insert: INSERT);
32775
32776 /* Don't output duplicate types. */
32777 if (*slot != HTAB_EMPTY_ENTRY)
32778 continue;
32779
32780 /* Add a pointer to the line table for the main compilation unit
32781 so that the debugger can make sense of DW_AT_decl_file
32782 attributes. */
32783 if (debug_info_level >= DINFO_LEVEL_TERSE)
32784 add_AT_lineptr (die: ctnode->root_die, attr_kind: DW_AT_stmt_list,
32785 label: (!dwarf_split_debug_info
32786 ? dl_section_ref
32787 : debug_skeleton_line_section_label));
32788
32789 output_comdat_type_unit (node: ctnode, early_lto_debug: false);
32790 *slot = ctnode;
32791 }
32792
32793 if (dwarf_split_debug_info)
32794 {
32795 int mark;
32796 struct md5_ctx ctx;
32797
32798 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32799 md5_init_ctx (ctx: &ctx);
32800 mark = 0;
32801 die_checksum (die: comp_unit_die (), ctx: &ctx, mark: &mark);
32802 unmark_all_dies (die: comp_unit_die ());
32803 md5_finish_ctx (ctx: &ctx, resbuf: checksum);
32804
32805 if (dwarf_version < 5)
32806 {
32807 /* Use the first 8 bytes of the checksum as the dwo_id,
32808 and add it to both comp-unit DIEs. */
32809 add_AT_data8 (die: main_comp_unit_die, attr_kind: DW_AT_GNU_dwo_id, data8: checksum);
32810 add_AT_data8 (die: comp_unit_die (), attr_kind: DW_AT_GNU_dwo_id, data8: checksum);
32811 }
32812
32813 /* Add the base offset of the ranges table to the skeleton
32814 comp-unit DIE. */
32815 if (!vec_safe_is_empty (v: ranges_table))
32816 {
32817 if (dwarf_version < 5)
32818 add_AT_lineptr (die: main_comp_unit_die, attr_kind: DW_AT_GNU_ranges_base,
32819 label: ranges_section_label);
32820 }
32821
32822 output_addr_table ();
32823 }
32824
32825 /* Output the main compilation unit if non-empty or if .debug_macinfo
32826 or .debug_macro will be emitted. */
32827 output_comp_unit (die: comp_unit_die (), have_macinfo,
32828 dwarf_split_debug_info ? checksum : NULL);
32829
32830 if (dwarf_split_debug_info && info_section_emitted)
32831 output_skeleton_debug_sections (comp_unit: main_comp_unit_die, dwo_id: checksum);
32832
32833 /* Output the abbreviation table. */
32834 if (vec_safe_length (v: abbrev_die_table) != 1)
32835 {
32836 switch_to_section (debug_abbrev_section);
32837 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32838 output_abbrev_section ();
32839 }
32840
32841 /* Output location list section if necessary. */
32842 if (have_location_lists)
32843 {
32844 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32845 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32846 /* Output the location lists info. */
32847 switch_to_section (debug_loc_section);
32848 if (dwarf_version >= 5)
32849 {
32850 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32851 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32852 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32853 dw2_asm_output_data (4, 0xffffffff,
32854 "Initial length escape value indicating "
32855 "64-bit DWARF extension");
32856 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32857 "Length of Location Lists");
32858 ASM_OUTPUT_LABEL (asm_out_file, l1);
32859 output_dwarf_version ();
32860 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32861 dw2_asm_output_data (1, 0, "Segment Size");
32862 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32863 "Offset Entry Count");
32864 }
32865 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32866 if (dwarf_version >= 5 && dwarf_split_debug_info)
32867 {
32868 unsigned int save_loc_list_idx = loc_list_idx;
32869 loc_list_idx = 0;
32870 output_loclists_offsets (die: comp_unit_die ());
32871 gcc_assert (save_loc_list_idx == loc_list_idx);
32872 }
32873 output_location_lists (die: comp_unit_die ());
32874 if (dwarf_version >= 5)
32875 ASM_OUTPUT_LABEL (asm_out_file, l2);
32876 }
32877
32878 output_pubtables ();
32879
32880 /* Output the address range information if a CU (.debug_info section)
32881 was emitted. We output an empty table even if we had no functions
32882 to put in it. This because the consumer has no way to tell the
32883 difference between an empty table that we omitted and failure to
32884 generate a table that would have contained data. */
32885 if (info_section_emitted)
32886 {
32887 switch_to_section (debug_aranges_section);
32888 output_aranges ();
32889 }
32890
32891 /* Output ranges section if necessary. */
32892 if (!vec_safe_is_empty (v: ranges_table))
32893 {
32894 if (dwarf_version >= 5)
32895 {
32896 if (dwarf_split_debug_info)
32897 {
32898 /* We don't know right now whether there are any
32899 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32900 Depending on into which of those two belongs the first
32901 ranges_table entry, emit that section first and that
32902 output_rnglists call will return true if the other kind of
32903 ranges needs to be emitted as well. */
32904 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32905 if (output_rnglists (generation, dwo))
32906 output_rnglists (generation, dwo: !dwo);
32907 }
32908 else
32909 output_rnglists (generation, dwo: false);
32910 }
32911 else
32912 output_ranges ();
32913 }
32914
32915 /* Have to end the macro section. */
32916 if (have_macinfo)
32917 {
32918 switch_to_section (debug_macinfo_section);
32919 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32920 output_macinfo (debug_line_label: !dwarf_split_debug_info ? debug_line_section_label
32921 : debug_skeleton_line_section_label, early_lto_debug: false);
32922 dw2_asm_output_data (1, 0, "End compilation unit");
32923 }
32924
32925 /* Output the source line correspondence table. We must do this
32926 even if there is no line information. Otherwise, on an empty
32927 translation unit, we will generate a present, but empty,
32928 .debug_info section. IRIX 6.5 `nm' will then complain when
32929 examining the file. This is done late so that any filenames
32930 used by the debug_info section are marked as 'used'. */
32931 switch_to_section (debug_line_section);
32932 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32933 if (! output_asm_line_debug_info ())
32934 output_line_info (prologue_only: false);
32935
32936 if (dwarf_split_debug_info && info_section_emitted)
32937 {
32938 switch_to_section (debug_skeleton_line_section);
32939 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32940 output_line_info (prologue_only: true);
32941 }
32942
32943 /* If we emitted any indirect strings, output the string table too. */
32944 if (debug_str_hash || skeleton_debug_str_hash)
32945 output_indirect_strings ();
32946 if (debug_line_str_hash)
32947 {
32948 switch_to_section (debug_line_str_section);
32949 const enum dwarf_form form = DW_FORM_line_strp;
32950 debug_line_str_hash->traverse<enum dwarf_form,
32951 output_indirect_string> (argument: form);
32952 }
32953
32954 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32955 symview_upper_bound = 0;
32956 if (zero_view_p)
32957 bitmap_clear (zero_view_p);
32958}
32959
32960/* Returns a hash value for X (which really is a variable_value_struct). */
32961
32962inline hashval_t
32963variable_value_hasher::hash (variable_value_struct *x)
32964{
32965 return (hashval_t) x->decl_id;
32966}
32967
32968/* Return true if decl_id of variable_value_struct X is the same as
32969 UID of decl Y. */
32970
32971inline bool
32972variable_value_hasher::equal (variable_value_struct *x, tree y)
32973{
32974 return x->decl_id == DECL_UID (y);
32975}
32976
32977/* Helper function for resolve_variable_value, handle
32978 DW_OP_GNU_variable_value in one location expression.
32979 Return true if exprloc has been changed into loclist. */
32980
32981static bool
32982resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32983{
32984 dw_loc_descr_ref next;
32985 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32986 {
32987 next = loc->dw_loc_next;
32988 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32989 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32990 continue;
32991
32992 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32993 if (DECL_CONTEXT (decl) != current_function_decl)
32994 continue;
32995
32996 dw_die_ref ref = lookup_decl_die (decl);
32997 if (ref)
32998 {
32999 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33000 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33001 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33002 continue;
33003 }
33004 dw_loc_list_ref l = loc_list_from_tree (loc: decl, want_address: 0, NULL);
33005 if (l == NULL)
33006 continue;
33007 if (l->dw_loc_next)
33008 {
33009 if (AT_class (a) != dw_val_class_loc)
33010 continue;
33011 switch (a->dw_attr)
33012 {
33013 /* Following attributes allow both exprloc and loclist
33014 classes, so we can change them into a loclist. */
33015 case DW_AT_location:
33016 case DW_AT_string_length:
33017 case DW_AT_return_addr:
33018 case DW_AT_data_member_location:
33019 case DW_AT_frame_base:
33020 case DW_AT_segment:
33021 case DW_AT_static_link:
33022 case DW_AT_use_location:
33023 case DW_AT_vtable_elem_location:
33024 if (prev)
33025 {
33026 prev->dw_loc_next = NULL;
33027 prepend_loc_descr_to_each (list: l, ref: AT_loc (a));
33028 }
33029 if (next)
33030 add_loc_descr_to_each (list: l, ref: next);
33031 a->dw_attr_val.val_class = dw_val_class_loc_list;
33032 a->dw_attr_val.val_entry = NULL;
33033 a->dw_attr_val.v.val_loc_list = l;
33034 have_location_lists = true;
33035 return true;
33036 /* Following attributes allow both exprloc and reference,
33037 so if the whole expression is DW_OP_GNU_variable_value alone
33038 we could transform it into reference. */
33039 case DW_AT_byte_size:
33040 case DW_AT_bit_size:
33041 case DW_AT_lower_bound:
33042 case DW_AT_upper_bound:
33043 case DW_AT_bit_stride:
33044 case DW_AT_count:
33045 case DW_AT_allocated:
33046 case DW_AT_associated:
33047 case DW_AT_byte_stride:
33048 if (prev == NULL && next == NULL)
33049 break;
33050 /* FALLTHRU */
33051 default:
33052 if (dwarf_strict)
33053 continue;
33054 break;
33055 }
33056 /* Create DW_TAG_variable that we can refer to. */
33057 gen_decl_die (decl, NULL_TREE, NULL,
33058 context_die: lookup_decl_die (decl: current_function_decl));
33059 ref = lookup_decl_die (decl);
33060 if (ref)
33061 {
33062 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33063 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33064 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33065 }
33066 continue;
33067 }
33068 if (prev)
33069 {
33070 prev->dw_loc_next = l->expr;
33071 add_loc_descr (list_head: &prev->dw_loc_next, descr: next);
33072 free_loc_descr (loc, NULL);
33073 next = prev->dw_loc_next;
33074 }
33075 else
33076 {
33077 memcpy (dest: loc, src: l->expr, n: sizeof (dw_loc_descr_node));
33078 add_loc_descr (list_head: &loc, descr: next);
33079 next = loc;
33080 }
33081 loc = prev;
33082 }
33083 return false;
33084}
33085
33086/* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
33087
33088static void
33089resolve_variable_value (dw_die_ref die)
33090{
33091 dw_attr_node *a;
33092 dw_loc_list_ref loc;
33093 unsigned ix;
33094
33095 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33096 switch (AT_class (a))
33097 {
33098 case dw_val_class_loc:
33099 if (!resolve_variable_value_in_expr (a, loc: AT_loc (a)))
33100 break;
33101 /* FALLTHRU */
33102 case dw_val_class_loc_list:
33103 loc = AT_loc_list (a);
33104 gcc_assert (loc);
33105 for (; loc; loc = loc->dw_loc_next)
33106 resolve_variable_value_in_expr (a, loc: loc->expr);
33107 break;
33108 default:
33109 break;
33110 }
33111}
33112
33113/* Attempt to optimize DW_OP_GNU_variable_value refering to
33114 temporaries in the current function. */
33115
33116static void
33117resolve_variable_values (void)
33118{
33119 if (!variable_value_hash || !current_function_decl)
33120 return;
33121
33122 struct variable_value_struct *node
33123 = variable_value_hash->find_with_hash (comparable: current_function_decl,
33124 DECL_UID (current_function_decl));
33125
33126 if (node == NULL)
33127 return;
33128
33129 unsigned int i;
33130 dw_die_ref die;
33131 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
33132 resolve_variable_value (die);
33133}
33134
33135/* Helper function for note_variable_value, handle one location
33136 expression. */
33137
33138static void
33139note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
33140{
33141 for (; loc; loc = loc->dw_loc_next)
33142 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
33143 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
33144 {
33145 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
33146 dw_die_ref ref = lookup_decl_die (decl);
33147 if (! ref && (flag_generate_lto || flag_generate_offload))
33148 {
33149 /* ??? This is somewhat a hack because we do not create DIEs
33150 for variables not in BLOCK trees early but when generating
33151 early LTO output we need the dw_val_class_decl_ref to be
33152 fully resolved. For fat LTO objects we'd also like to
33153 undo this after LTO dwarf output. */
33154 gcc_assert (DECL_CONTEXT (decl));
33155 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
33156 gcc_assert (ctx != NULL);
33157 gen_decl_die (decl, NULL_TREE, NULL, context_die: ctx);
33158 ref = lookup_decl_die (decl);
33159 gcc_assert (ref != NULL);
33160 }
33161 if (ref)
33162 {
33163 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
33164 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
33165 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
33166 continue;
33167 }
33168 if (VAR_P (decl)
33169 && DECL_CONTEXT (decl)
33170 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
33171 && lookup_decl_die (DECL_CONTEXT (decl)))
33172 {
33173 if (!variable_value_hash)
33174 variable_value_hash
33175 = hash_table<variable_value_hasher>::create_ggc (n: 10);
33176
33177 tree fndecl = DECL_CONTEXT (decl);
33178 struct variable_value_struct *node;
33179 struct variable_value_struct **slot
33180 = variable_value_hash->find_slot_with_hash (comparable: fndecl,
33181 DECL_UID (fndecl),
33182 insert: INSERT);
33183 if (*slot == NULL)
33184 {
33185 node = ggc_cleared_alloc<variable_value_struct> ();
33186 node->decl_id = DECL_UID (fndecl);
33187 *slot = node;
33188 }
33189 else
33190 node = *slot;
33191
33192 vec_safe_push (v&: node->dies, obj: die);
33193 }
33194 }
33195}
33196
33197/* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
33198 with dw_val_class_decl_ref operand. */
33199
33200static void
33201note_variable_value (dw_die_ref die)
33202{
33203 dw_die_ref c;
33204 dw_attr_node *a;
33205 dw_loc_list_ref loc;
33206 unsigned ix;
33207
33208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
33209 switch (AT_class (a))
33210 {
33211 case dw_val_class_loc_list:
33212 loc = AT_loc_list (a);
33213 gcc_assert (loc);
33214 if (!loc->noted_variable_value)
33215 {
33216 loc->noted_variable_value = 1;
33217 for (; loc; loc = loc->dw_loc_next)
33218 note_variable_value_in_expr (die, loc: loc->expr);
33219 }
33220 break;
33221 case dw_val_class_loc:
33222 note_variable_value_in_expr (die, loc: AT_loc (a));
33223 break;
33224 default:
33225 break;
33226 }
33227
33228 /* Mark children. */
33229 FOR_EACH_CHILD (die, c, note_variable_value (c));
33230}
33231
33232/* Process DWARF dies for CTF generation. */
33233
33234static void
33235ctf_debug_do_cu (dw_die_ref die)
33236{
33237 dw_die_ref c;
33238
33239 if (!ctf_do_die (die))
33240 return;
33241
33242 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33243}
33244
33245/* Perform any cleanups needed after the early debug generation pass
33246 has run. */
33247
33248static void
33249dwarf2out_early_finish (const char *filename)
33250{
33251 comdat_type_node *ctnode;
33252 set_early_dwarf s;
33253 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33254
33255 /* PCH might result in DW_AT_producer string being restored from the
33256 header compilation, so always fill it with empty string initially
33257 and overwrite only here. */
33258 dw_attr_node *producer = get_AT (die: comp_unit_die (), attr_kind: DW_AT_producer);
33259
33260 if (dwarf_record_gcc_switches)
33261 producer_string = gen_producer_string (language_string: lang_hooks.name,
33262 options: save_decoded_options,
33263 options_count: save_decoded_options_count);
33264 else
33265 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33266
33267 producer->dw_attr_val.v.val_str->refcount--;
33268 producer->dw_attr_val.v.val_str = find_AT_string (str: producer_string);
33269
33270 /* Add the name for the main input file now. We delayed this from
33271 dwarf2out_init to avoid complications with PCH. */
33272 add_filename_attribute (die: comp_unit_die (), name_string: remap_debug_filename (filename));
33273 add_comp_dir_attribute (die: comp_unit_die ());
33274
33275 /* With LTO early dwarf was really finished at compile-time, so make
33276 sure to adjust the phase after annotating the LTRANS CU DIE. */
33277 if (in_lto_p)
33278 {
33279 early_dwarf_finished = true;
33280 if (dump_file)
33281 {
33282 fprintf (stream: dump_file, format: "LTO EARLY DWARF for %s\n", filename);
33283 print_die (die: comp_unit_die (), outfile: dump_file);
33284 }
33285 return;
33286 }
33287
33288 /* Walk through the list of incomplete types again, trying once more to
33289 emit full debugging info for them. */
33290 retry_incomplete_types ();
33291
33292 gen_scheduled_generic_parms_dies ();
33293 gen_remaining_tmpl_value_param_die_attribute ();
33294
33295 /* The point here is to flush out the limbo list so that it is empty
33296 and we don't need to stream it for LTO. */
33297 flush_limbo_die_list ();
33298
33299 /* Add DW_AT_linkage_name for all deferred DIEs. */
33300 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33301 {
33302 tree decl = node->created_for;
33303 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33304 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33305 ended up in deferred_asm_name before we knew it was
33306 constant and never written to disk. */
33307 && DECL_ASSEMBLER_NAME (decl))
33308 {
33309 add_linkage_attr (die: node->die, decl);
33310 move_linkage_attr (die: node->die);
33311 }
33312 }
33313 deferred_asm_name = NULL;
33314
33315 if (flag_eliminate_unused_debug_types)
33316 prune_unused_types ();
33317
33318 /* Generate separate COMDAT sections for type DIEs. */
33319 if (use_debug_types)
33320 {
33321 break_out_comdat_types (die: comp_unit_die ());
33322
33323 /* Each new type_unit DIE was added to the limbo die list when created.
33324 Since these have all been added to comdat_type_list, clear the
33325 limbo die list. */
33326 limbo_die_list = NULL;
33327
33328 /* For each new comdat type unit, copy declarations for incomplete
33329 types to make the new unit self-contained (i.e., no direct
33330 references to the main compile unit). */
33331 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33332 copy_decls_for_unworthy_types (unit: ctnode->root_die);
33333 copy_decls_for_unworthy_types (unit: comp_unit_die ());
33334
33335 /* In the process of copying declarations from one unit to another,
33336 we may have left some declarations behind that are no longer
33337 referenced. Prune them. */
33338 prune_unused_types ();
33339 }
33340
33341 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33342 with dw_val_class_decl_ref operand. */
33343 note_variable_value (die: comp_unit_die ());
33344 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33345 note_variable_value (die: node->die);
33346 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33347 note_variable_value (die: ctnode->root_die);
33348 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33349 note_variable_value (die: node->die);
33350
33351 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33352 both the main_cu and all skeleton TUs. Making this call unconditional
33353 would end up either adding a second copy of the AT_pubnames attribute, or
33354 requiring a special case in add_top_level_skeleton_die_attrs. */
33355 if (!dwarf_split_debug_info)
33356 add_AT_pubnames (die: comp_unit_die ());
33357
33358 /* The early debug phase is now finished. */
33359 early_dwarf_finished = true;
33360 if (dump_file)
33361 {
33362 fprintf (stream: dump_file, format: "EARLY DWARF for %s\n", filename);
33363 print_die (die: comp_unit_die (), outfile: dump_file);
33364 }
33365
33366 /* Generate CTF/BTF debug info. */
33367 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33368 || btf_debuginfo_p ()) && lang_GNU_C ())
33369 {
33370 ctf_debug_init ();
33371 ctf_debug_do_cu (die: comp_unit_die ());
33372 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33373 ctf_debug_do_cu (die: node->die);
33374
33375 ctf_debug_early_finish (filename);
33376 }
33377
33378#ifdef CODEVIEW_DEBUGGING_INFO
33379 if (codeview_debuginfo_p ())
33380 codeview_debug_early_finish (comp_unit_die ());
33381#endif
33382
33383 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33384 if ((!flag_generate_lto && !flag_generate_offload)
33385 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33386 copy_lto_debug_sections operation of the simple object support in
33387 libiberty is not implemented for them yet. */
33388 || TARGET_PECOFF || TARGET_COFF)
33389 return;
33390
33391 /* Now as we are going to output for LTO initialize sections and labels
33392 to the LTO variants. We don't need a random-seed postfix as other
33393 LTO sections as linking the LTO debug sections into one in a partial
33394 link is fine. */
33395 init_sections_and_labels (early_lto_debug: true);
33396
33397 /* The output below is modeled after dwarf2out_finish with all
33398 location related output removed and some LTO specific changes.
33399 Some refactoring might make both smaller and easier to match up. */
33400
33401 base_types.truncate (size: 0);
33402 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33403 mark_base_types (die: ctnode->root_die);
33404 mark_base_types (die: comp_unit_die ());
33405 move_marked_base_types ();
33406
33407 /* Traverse the DIE's and add sibling attributes to those DIE's
33408 that have children. */
33409 add_sibling_attributes (die: comp_unit_die ());
33410 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33411 add_sibling_attributes (die: node->die);
33412 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33413 add_sibling_attributes (die: ctnode->root_die);
33414
33415 /* AIX Assembler inserts the length, so adjust the reference to match the
33416 offset expected by debuggers. */
33417 strcpy (dest: dl_section_ref, src: debug_line_section_label);
33418 if (XCOFF_DEBUGGING_INFO)
33419 strcat (dest: dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33420
33421 if (debug_info_level >= DINFO_LEVEL_TERSE)
33422 add_AT_lineptr (die: comp_unit_die (), attr_kind: DW_AT_stmt_list, label: dl_section_ref);
33423
33424 if (have_macinfo)
33425 add_AT_macptr (die: comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33426 label: macinfo_section_label);
33427
33428 save_macinfo_strings ();
33429
33430 if (dwarf_split_debug_info)
33431 {
33432 unsigned int index = 0;
33433 debug_str_hash->traverse_noresize<unsigned int *, index_string> (argument: &index);
33434 }
33435
33436 /* Output all of the compilation units. We put the main one last so that
33437 the offsets are available to output_pubnames. */
33438 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33439 output_comp_unit (die: node->die, output_if_empty: 0, NULL);
33440
33441 hash_table<comdat_type_hasher> comdat_type_table (100);
33442 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33443 {
33444 comdat_type_node **slot = comdat_type_table.find_slot (value: ctnode, insert: INSERT);
33445
33446 /* Don't output duplicate types. */
33447 if (*slot != HTAB_EMPTY_ENTRY)
33448 continue;
33449
33450 /* Add a pointer to the line table for the main compilation unit
33451 so that the debugger can make sense of DW_AT_decl_file
33452 attributes. */
33453 if (debug_info_level >= DINFO_LEVEL_TERSE)
33454 add_AT_lineptr (die: ctnode->root_die, attr_kind: DW_AT_stmt_list,
33455 label: (!dwarf_split_debug_info
33456 ? debug_line_section_label
33457 : debug_skeleton_line_section_label));
33458
33459 output_comdat_type_unit (node: ctnode, early_lto_debug: true);
33460 *slot = ctnode;
33461 }
33462
33463 /* Stick a unique symbol to the main debuginfo section. */
33464 compute_comp_unit_symbol (unit_die: comp_unit_die ());
33465
33466 /* Output the main compilation unit. We always need it if only for
33467 the CU symbol. */
33468 output_comp_unit (die: comp_unit_die (), output_if_empty: true, NULL);
33469
33470 /* Output the abbreviation table. */
33471 if (vec_safe_length (v: abbrev_die_table) != 1)
33472 {
33473 switch_to_section (debug_abbrev_section);
33474 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33475 output_abbrev_section ();
33476 }
33477
33478 /* Have to end the macro section. */
33479 if (have_macinfo)
33480 {
33481 /* We have to save macinfo state if we need to output it again
33482 for the FAT part of the object. */
33483 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33484 if (flag_fat_lto_objects)
33485 macinfo_table = macinfo_table->copy ();
33486
33487 switch_to_section (debug_macinfo_section);
33488 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33489 output_macinfo (debug_line_label: debug_line_section_label, early_lto_debug: true);
33490 dw2_asm_output_data (1, 0, "End compilation unit");
33491
33492 if (flag_fat_lto_objects)
33493 {
33494 vec_free (v&: macinfo_table);
33495 macinfo_table = saved_macinfo_table;
33496 }
33497 }
33498
33499 /* Emit a skeleton debug_line section. */
33500 switch_to_section (debug_line_section);
33501 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33502 output_line_info (prologue_only: true);
33503
33504 /* If we emitted any indirect strings, output the string table too. */
33505 if (debug_str_hash || skeleton_debug_str_hash)
33506 output_indirect_strings ();
33507 if (debug_line_str_hash)
33508 {
33509 switch_to_section (debug_line_str_section);
33510 const enum dwarf_form form = DW_FORM_line_strp;
33511 debug_line_str_hash->traverse<enum dwarf_form,
33512 output_indirect_string> (argument: form);
33513 }
33514
33515 /* Switch back to the text section. */
33516 switch_to_section (text_section);
33517}
33518
33519/* Reset all state within dwarf2out.cc so that we can rerun the compiler
33520 within the same process. For use by toplev::finalize. */
33521
33522void
33523dwarf2out_cc_finalize (void)
33524{
33525 last_var_location_insn = NULL;
33526 cached_next_real_insn = NULL;
33527 used_rtx_array = NULL;
33528 incomplete_types = NULL;
33529 debug_info_section = NULL;
33530 debug_skeleton_info_section = NULL;
33531 debug_abbrev_section = NULL;
33532 debug_skeleton_abbrev_section = NULL;
33533 debug_aranges_section = NULL;
33534 debug_addr_section = NULL;
33535 debug_macinfo_section = NULL;
33536 debug_line_section = NULL;
33537 debug_skeleton_line_section = NULL;
33538 debug_loc_section = NULL;
33539 debug_pubnames_section = NULL;
33540 debug_pubtypes_section = NULL;
33541 debug_str_section = NULL;
33542 debug_line_str_section = NULL;
33543 debug_str_dwo_section = NULL;
33544 debug_str_offsets_section = NULL;
33545 debug_ranges_section = NULL;
33546 debug_ranges_dwo_section = NULL;
33547 debug_frame_section = NULL;
33548 fde_vec = NULL;
33549 debug_str_hash = NULL;
33550 debug_line_str_hash = NULL;
33551 skeleton_debug_str_hash = NULL;
33552 dw2_string_counter = 0;
33553 have_multiple_function_sections = false;
33554 in_text_section_p = false;
33555 cold_text_section = NULL;
33556 last_text_label = NULL;
33557 last_cold_label = NULL;
33558 switch_text_ranges = NULL;
33559 switch_cold_ranges = NULL;
33560 current_unit_personality = NULL;
33561
33562 early_dwarf = false;
33563 early_dwarf_finished = false;
33564
33565 next_die_offset = 0;
33566 single_comp_unit_die = NULL;
33567 comdat_type_list = NULL;
33568 limbo_die_list = NULL;
33569 file_table = NULL;
33570 decl_die_table = NULL;
33571 common_block_die_table = NULL;
33572 decl_loc_table = NULL;
33573 call_arg_locations = NULL;
33574 call_arg_loc_last = NULL;
33575 call_site_count = -1;
33576 tail_call_site_count = -1;
33577 cached_dw_loc_list_table = NULL;
33578 abbrev_die_table = NULL;
33579 delete dwarf_proc_stack_usage_map;
33580 dwarf_proc_stack_usage_map = NULL;
33581 line_info_label_num = 0;
33582 cur_line_info_table = NULL;
33583 text_section_line_info = NULL;
33584 cold_text_section_line_info = NULL;
33585 separate_line_info = NULL;
33586 info_section_emitted = false;
33587 pubname_table = NULL;
33588 pubtype_table = NULL;
33589 macinfo_table = NULL;
33590 ranges_table = NULL;
33591 ranges_by_label = NULL;
33592 rnglist_idx = 0;
33593 have_location_lists = false;
33594 loclabel_num = 0;
33595 poc_label_num = 0;
33596 last_emitted_file = NULL;
33597 label_num = 0;
33598 tmpl_value_parm_die_table = NULL;
33599 generic_type_instances = NULL;
33600 frame_pointer_fb_offset = 0;
33601 frame_pointer_fb_offset_valid = false;
33602 base_types.release ();
33603 XDELETEVEC (producer_string);
33604 producer_string = NULL;
33605 output_line_info_generation = 0;
33606 init_sections_and_labels_generation = 0;
33607}
33608
33609#include "gt-dwarf2out.h"
33610

source code of gcc/dwarf2out.cc