2018-02-09 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / gcc / dwarf2out.c
blob984df9fe4e9adf8febf9cfc7f72af67af961f946
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2018 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).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. */
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. */
35 /* DWARF2 Abbreviation Glossary:
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.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
106 #ifndef XCOFF_DEBUGGING_INFO
107 #define XCOFF_DEBUGGING_INFO 0
108 #endif
110 #ifndef HAVE_XCOFF_DWARF_EXTRAS
111 #define HAVE_XCOFF_DWARF_EXTRAS 0
112 #endif
114 #ifdef VMS_DEBUGGING_INFO
115 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117 /* Define this macro to be a nonzero value if the directory specifications
118 which are output in the debug info should end with a separator. */
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
120 /* Define this macro to evaluate to a nonzero value if GCC should refrain
121 from generating indirect strings in DWARF2 debug information, for instance
122 if your target is stuck with an old version of GDB that is unable to
123 process them properly or uses VMS Debug. */
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
125 #else
126 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
128 #endif
130 /* ??? Poison these here until it can be done generically. They've been
131 totally replaced in this file; make sure it stays that way. */
132 #undef DWARF2_UNWIND_INFO
133 #undef DWARF2_FRAME_INFO
134 #if (GCC_VERSION >= 3000)
135 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
136 #endif
138 /* The size of the target's pointer type. */
139 #ifndef PTR_SIZE
140 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
141 #endif
143 /* Array of RTXes referenced by the debugging information, which therefore
144 must be kept around forever. */
145 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147 /* A pointer to the base of a list of incomplete types which might be
148 completed at some later time. incomplete_types_list needs to be a
149 vec<tree, va_gc> *because we want to tell the garbage collector about
150 it. */
151 static GTY(()) vec<tree, va_gc> *incomplete_types;
153 /* A pointer to the base of a table of references to declaration
154 scopes. This table is a display which tracks the nesting
155 of declaration scopes at the current scope and containing
156 scopes. This table is used to find the proper place to
157 define type declaration DIE's. */
158 static GTY(()) vec<tree, va_gc> *decl_scope_table;
160 /* Pointers to various DWARF2 sections. */
161 static GTY(()) section *debug_info_section;
162 static GTY(()) section *debug_skeleton_info_section;
163 static GTY(()) section *debug_abbrev_section;
164 static GTY(()) section *debug_skeleton_abbrev_section;
165 static GTY(()) section *debug_aranges_section;
166 static GTY(()) section *debug_addr_section;
167 static GTY(()) section *debug_macinfo_section;
168 static const char *debug_macinfo_section_name;
169 static unsigned macinfo_label_base = 1;
170 static GTY(()) section *debug_line_section;
171 static GTY(()) section *debug_skeleton_line_section;
172 static GTY(()) section *debug_loc_section;
173 static GTY(()) section *debug_pubnames_section;
174 static GTY(()) section *debug_pubtypes_section;
175 static GTY(()) section *debug_str_section;
176 static GTY(()) section *debug_line_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
182 /* Maximum size (in bytes) of an artificially generated label. */
183 #define MAX_ARTIFICIAL_LABEL_BYTES 40
185 /* According to the (draft) DWARF 3 specification, the initial length
186 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
187 bytes are 0xffffffff, followed by the length stored in the next 8
188 bytes.
190 However, the SGI/MIPS ABI uses an initial length which is equal to
191 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
197 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
198 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
199 #endif
201 /* Round SIZE up to the nearest BOUNDARY. */
202 #define DWARF_ROUND(SIZE,BOUNDARY) \
203 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
205 /* CIE identifier. */
206 #if HOST_BITS_PER_WIDE_INT >= 64
207 #define DWARF_CIE_ID \
208 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
209 #else
210 #define DWARF_CIE_ID DW_CIE_ID
211 #endif
214 /* A vector for a table that contains frame description
215 information for each routine. */
216 #define NOT_INDEXED (-1U)
217 #define NO_INDEX_ASSIGNED (-2U)
219 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
221 struct GTY((for_user)) indirect_string_node {
222 const char *str;
223 unsigned int refcount;
224 enum dwarf_form form;
225 char *label;
226 unsigned int index;
229 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
231 typedef const char *compare_type;
233 static hashval_t hash (indirect_string_node *);
234 static bool equal (indirect_string_node *, const char *);
237 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
239 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
241 /* With split_debug_info, both the comp_dir and dwo_name go in the
242 main object file, rather than the dwo, similar to the force_direct
243 parameter elsewhere but with additional complications:
245 1) The string is needed in both the main object file and the dwo.
246 That is, the comp_dir and dwo_name will appear in both places.
248 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
249 DW_FORM_line_strp or DW_FORM_GNU_str_index.
251 3) GCC chooses the form to use late, depending on the size and
252 reference count.
254 Rather than forcing the all debug string handling functions and
255 callers to deal with these complications, simply use a separate,
256 special-cased string table for any attribute that should go in the
257 main object file. This limits the complexity to just the places
258 that need it. */
260 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
262 static GTY(()) int dw2_string_counter;
264 /* True if the compilation unit places functions in more than one section. */
265 static GTY(()) bool have_multiple_function_sections = false;
267 /* Whether the default text and cold text sections have been used at all. */
268 static GTY(()) bool text_section_used = false;
269 static GTY(()) bool cold_text_section_used = false;
271 /* The default cold text section. */
272 static GTY(()) section *cold_text_section;
274 /* The DIE for C++14 'auto' in a function return type. */
275 static GTY(()) dw_die_ref auto_die;
277 /* The DIE for C++14 'decltype(auto)' in a function return type. */
278 static GTY(()) dw_die_ref decltype_auto_die;
280 /* Forward declarations for functions defined in this file. */
282 static void output_call_frame_info (int);
283 static void dwarf2out_note_section_used (void);
285 /* Personality decl of current unit. Used only when assembler does not support
286 personality CFI. */
287 static GTY(()) rtx current_unit_personality;
289 /* Whether an eh_frame section is required. */
290 static GTY(()) bool do_eh_frame = false;
292 /* .debug_rnglists next index. */
293 static unsigned int rnglist_idx;
295 /* Data and reference forms for relocatable data. */
296 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
297 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
299 #ifndef DEBUG_FRAME_SECTION
300 #define DEBUG_FRAME_SECTION ".debug_frame"
301 #endif
303 #ifndef FUNC_BEGIN_LABEL
304 #define FUNC_BEGIN_LABEL "LFB"
305 #endif
307 #ifndef FUNC_END_LABEL
308 #define FUNC_END_LABEL "LFE"
309 #endif
311 #ifndef PROLOGUE_END_LABEL
312 #define PROLOGUE_END_LABEL "LPE"
313 #endif
315 #ifndef EPILOGUE_BEGIN_LABEL
316 #define EPILOGUE_BEGIN_LABEL "LEB"
317 #endif
319 #ifndef FRAME_BEGIN_LABEL
320 #define FRAME_BEGIN_LABEL "Lframe"
321 #endif
322 #define CIE_AFTER_SIZE_LABEL "LSCIE"
323 #define CIE_END_LABEL "LECIE"
324 #define FDE_LABEL "LSFDE"
325 #define FDE_AFTER_SIZE_LABEL "LASFDE"
326 #define FDE_END_LABEL "LEFDE"
327 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
328 #define LINE_NUMBER_END_LABEL "LELT"
329 #define LN_PROLOG_AS_LABEL "LASLTP"
330 #define LN_PROLOG_END_LABEL "LELTP"
331 #define DIE_LABEL_PREFIX "DW"
333 /* Match the base name of a file to the base name of a compilation unit. */
335 static int
336 matches_main_base (const char *path)
338 /* Cache the last query. */
339 static const char *last_path = NULL;
340 static int last_match = 0;
341 if (path != last_path)
343 const char *base;
344 int length = base_of_path (path, &base);
345 last_path = path;
346 last_match = (length == main_input_baselength
347 && memcmp (base, main_input_basename, length) == 0);
349 return last_match;
352 #ifdef DEBUG_DEBUG_STRUCT
354 static int
355 dump_struct_debug (tree type, enum debug_info_usage usage,
356 enum debug_struct_file criterion, int generic,
357 int matches, int result)
359 /* Find the type name. */
360 tree type_decl = TYPE_STUB_DECL (type);
361 tree t = type_decl;
362 const char *name = 0;
363 if (TREE_CODE (t) == TYPE_DECL)
364 t = DECL_NAME (t);
365 if (t)
366 name = IDENTIFIER_POINTER (t);
368 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
369 criterion,
370 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
371 matches ? "bas" : "hdr",
372 generic ? "gen" : "ord",
373 usage == DINFO_USAGE_DFN ? ";" :
374 usage == DINFO_USAGE_DIR_USE ? "." : "*",
375 result,
376 (void*) type_decl, name);
377 return result;
379 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
380 dump_struct_debug (type, usage, criterion, generic, matches, result)
382 #else
384 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
385 (result)
387 #endif
389 /* Get the number of HOST_WIDE_INTs needed to represent the precision
390 of the number. Some constants have a large uniform precision, so
391 we get the precision needed for the actual value of the number. */
393 static unsigned int
394 get_full_len (const wide_int &op)
396 int prec = wi::min_precision (op, UNSIGNED);
397 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
398 / HOST_BITS_PER_WIDE_INT);
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
404 enum debug_struct_file criterion;
405 tree type_decl;
406 bool generic = lang_hooks.types.generic_p (type);
408 if (generic)
409 criterion = debug_struct_generic[usage];
410 else
411 criterion = debug_struct_ordinary[usage];
413 if (criterion == DINFO_STRUCT_FILE_NONE)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415 if (criterion == DINFO_STRUCT_FILE_ANY)
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
420 if (type_decl != NULL)
422 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
429 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
432 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
433 switch to the data section instead, and write out a synthetic start label
434 for collect2 the first time around. */
436 static void
437 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
439 if (eh_frame_section == 0)
441 int flags;
443 if (EH_TABLES_CAN_BE_READ_ONLY)
445 int fde_encoding;
446 int per_encoding;
447 int lsda_encoding;
449 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
450 /*global=*/0);
451 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
452 /*global=*/1);
453 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
454 /*global=*/0);
455 flags = ((! flag_pic
456 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
457 && (fde_encoding & 0x70) != DW_EH_PE_aligned
458 && (per_encoding & 0x70) != DW_EH_PE_absptr
459 && (per_encoding & 0x70) != DW_EH_PE_aligned
460 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
461 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
462 ? 0 : SECTION_WRITE);
464 else
465 flags = SECTION_WRITE;
467 #ifdef EH_FRAME_SECTION_NAME
468 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
469 #else
470 eh_frame_section = ((flags == SECTION_WRITE)
471 ? data_section : readonly_data_section);
472 #endif /* EH_FRAME_SECTION_NAME */
475 switch_to_section (eh_frame_section);
477 #ifdef EH_FRAME_THROUGH_COLLECT2
478 /* We have no special eh_frame section. Emit special labels to guide
479 collect2. */
480 if (!back)
482 tree label = get_file_function_name ("F");
483 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
484 targetm.asm_out.globalize_label (asm_out_file,
485 IDENTIFIER_POINTER (label));
486 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
488 #endif
491 /* Switch [BACK] to the eh or debug frame table section, depending on
492 FOR_EH. */
494 static void
495 switch_to_frame_table_section (int for_eh, bool back)
497 if (for_eh)
498 switch_to_eh_frame_section (back);
499 else
501 if (!debug_frame_section)
502 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
503 SECTION_DEBUG, NULL);
504 switch_to_section (debug_frame_section);
508 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
510 enum dw_cfi_oprnd_type
511 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
513 switch (cfi)
515 case DW_CFA_nop:
516 case DW_CFA_GNU_window_save:
517 case DW_CFA_remember_state:
518 case DW_CFA_restore_state:
519 return dw_cfi_oprnd_unused;
521 case DW_CFA_set_loc:
522 case DW_CFA_advance_loc1:
523 case DW_CFA_advance_loc2:
524 case DW_CFA_advance_loc4:
525 case DW_CFA_MIPS_advance_loc8:
526 return dw_cfi_oprnd_addr;
528 case DW_CFA_offset:
529 case DW_CFA_offset_extended:
530 case DW_CFA_def_cfa:
531 case DW_CFA_offset_extended_sf:
532 case DW_CFA_def_cfa_sf:
533 case DW_CFA_restore:
534 case DW_CFA_restore_extended:
535 case DW_CFA_undefined:
536 case DW_CFA_same_value:
537 case DW_CFA_def_cfa_register:
538 case DW_CFA_register:
539 case DW_CFA_expression:
540 case DW_CFA_val_expression:
541 return dw_cfi_oprnd_reg_num;
543 case DW_CFA_def_cfa_offset:
544 case DW_CFA_GNU_args_size:
545 case DW_CFA_def_cfa_offset_sf:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_def_cfa_expression:
549 return dw_cfi_oprnd_loc;
551 default:
552 gcc_unreachable ();
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 switch (cfi)
563 case DW_CFA_def_cfa:
564 case DW_CFA_def_cfa_sf:
565 case DW_CFA_offset:
566 case DW_CFA_offset_extended_sf:
567 case DW_CFA_offset_extended:
568 return dw_cfi_oprnd_offset;
570 case DW_CFA_register:
571 return dw_cfi_oprnd_reg_num;
573 case DW_CFA_expression:
574 case DW_CFA_val_expression:
575 return dw_cfi_oprnd_loc;
577 case DW_CFA_def_cfa_expression:
578 return dw_cfi_oprnd_cfa_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (!XCOFF_DEBUGGING_INFO || for_eh)
604 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
605 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
606 " indicating 64-bit DWARF extension");
607 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
608 "FDE Length");
610 ASM_OUTPUT_LABEL (asm_out_file, l1);
612 if (for_eh)
613 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
614 else
615 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
616 debug_frame_section, "FDE CIE offset");
618 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
619 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
621 if (for_eh)
623 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
624 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
625 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
626 "FDE initial location");
627 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
628 end, begin, "FDE address range");
630 else
632 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
633 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
636 if (augmentation[0])
638 if (any_lsda_needed)
640 int size = size_of_encoded_value (lsda_encoding);
642 if (lsda_encoding == DW_EH_PE_aligned)
644 int offset = ( 4 /* Length */
645 + 4 /* CIE offset */
646 + 2 * size_of_encoded_value (fde_encoding)
647 + 1 /* Augmentation size */ );
648 int pad = -offset & (PTR_SIZE - 1);
650 size += pad;
651 gcc_assert (size_of_uleb128 (size) == 1);
654 dw2_asm_output_data_uleb128 (size, "Augmentation size");
656 if (fde->uses_eh_lsda)
658 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
659 fde->funcdef_number);
660 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
661 gen_rtx_SYMBOL_REF (Pmode, l1),
662 false,
663 "Language Specific Data Area");
665 else
667 if (lsda_encoding == DW_EH_PE_aligned)
668 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
669 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
670 "Language Specific Data Area (none)");
673 else
674 dw2_asm_output_data_uleb128 (0, "Augmentation size");
677 /* Loop through the Call Frame Instructions associated with this FDE. */
678 fde->dw_fde_current_label = begin;
680 size_t from, until, i;
682 from = 0;
683 until = vec_safe_length (fde->dw_fde_cfi);
685 if (fde->dw_fde_second_begin == NULL)
687 else if (!second)
688 until = fde->dw_fde_switch_cfi_index;
689 else
690 from = fde->dw_fde_switch_cfi_index;
692 for (i = from; i < until; i++)
693 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
696 /* If we are to emit a ref/link from function bodies to their frame tables,
697 do it now. This is typically performed to make sure that tables
698 associated with functions are dragged with them and not discarded in
699 garbage collecting links. We need to do this on a per function basis to
700 cope with -ffunction-sections. */
702 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
703 /* Switch to the function section, emit the ref to the tables, and
704 switch *back* into the table section. */
705 switch_to_section (function_section (fde->decl));
706 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
707 switch_to_frame_table_section (for_eh, true);
708 #endif
710 /* Pad the FDE out to an address sized boundary. */
711 ASM_OUTPUT_ALIGN (asm_out_file,
712 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
713 ASM_OUTPUT_LABEL (asm_out_file, l2);
715 j += 2;
718 /* Return true if frame description entry FDE is needed for EH. */
720 static bool
721 fde_needed_for_eh_p (dw_fde_ref fde)
723 if (flag_asynchronous_unwind_tables)
724 return true;
726 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
727 return true;
729 if (fde->uses_eh_lsda)
730 return true;
732 /* If exceptions are enabled, we have collected nothrow info. */
733 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
734 return false;
736 return true;
739 /* Output the call frame information used to record information
740 that relates to calculating the frame pointer, and records the
741 location of saved registers. */
743 static void
744 output_call_frame_info (int for_eh)
746 unsigned int i;
747 dw_fde_ref fde;
748 dw_cfi_ref cfi;
749 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
750 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
751 bool any_lsda_needed = false;
752 char augmentation[6];
753 int augmentation_size;
754 int fde_encoding = DW_EH_PE_absptr;
755 int per_encoding = DW_EH_PE_absptr;
756 int lsda_encoding = DW_EH_PE_absptr;
757 int return_reg;
758 rtx personality = NULL;
759 int dw_cie_version;
761 /* Don't emit a CIE if there won't be any FDEs. */
762 if (!fde_vec)
763 return;
765 /* Nothing to do if the assembler's doing it all. */
766 if (dwarf2out_do_cfi_asm ())
767 return;
769 /* If we don't have any functions we'll want to unwind out of, don't emit
770 any EH unwind information. If we make FDEs linkonce, we may have to
771 emit an empty label for an FDE that wouldn't otherwise be emitted. We
772 want to avoid having an FDE kept around when the function it refers to
773 is discarded. Example where this matters: a primary function template
774 in C++ requires EH information, an explicit specialization doesn't. */
775 if (for_eh)
777 bool any_eh_needed = false;
779 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
781 if (fde->uses_eh_lsda)
782 any_eh_needed = any_lsda_needed = true;
783 else if (fde_needed_for_eh_p (fde))
784 any_eh_needed = true;
785 else if (TARGET_USES_WEAK_UNWIND_INFO)
786 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
789 if (!any_eh_needed)
790 return;
793 /* We're going to be generating comments, so turn on app. */
794 if (flag_debug_asm)
795 app_enable ();
797 /* Switch to the proper frame section, first time. */
798 switch_to_frame_table_section (for_eh, false);
800 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
801 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
803 /* Output the CIE. */
804 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
805 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
806 if (!XCOFF_DEBUGGING_INFO || for_eh)
808 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
809 dw2_asm_output_data (4, 0xffffffff,
810 "Initial length escape value indicating 64-bit DWARF extension");
811 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
812 "Length of Common Information Entry");
814 ASM_OUTPUT_LABEL (asm_out_file, l1);
816 /* Now that the CIE pointer is PC-relative for EH,
817 use 0 to identify the CIE. */
818 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
819 (for_eh ? 0 : DWARF_CIE_ID),
820 "CIE Identifier Tag");
822 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
823 use CIE version 1, unless that would produce incorrect results
824 due to overflowing the return register column. */
825 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
826 dw_cie_version = 1;
827 if (return_reg >= 256 || dwarf_version > 2)
828 dw_cie_version = 3;
829 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
831 augmentation[0] = 0;
832 augmentation_size = 0;
834 personality = current_unit_personality;
835 if (for_eh)
837 char *p;
839 /* Augmentation:
840 z Indicates that a uleb128 is present to size the
841 augmentation section.
842 L Indicates the encoding (and thus presence) of
843 an LSDA pointer in the FDE augmentation.
844 R Indicates a non-default pointer encoding for
845 FDE code pointers.
846 P Indicates the presence of an encoding + language
847 personality routine in the CIE augmentation. */
849 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
850 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
851 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
853 p = augmentation + 1;
854 if (personality)
856 *p++ = 'P';
857 augmentation_size += 1 + size_of_encoded_value (per_encoding);
858 assemble_external_libcall (personality);
860 if (any_lsda_needed)
862 *p++ = 'L';
863 augmentation_size += 1;
865 if (fde_encoding != DW_EH_PE_absptr)
867 *p++ = 'R';
868 augmentation_size += 1;
870 if (p > augmentation + 1)
872 augmentation[0] = 'z';
873 *p = '\0';
876 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
877 if (personality && per_encoding == DW_EH_PE_aligned)
879 int offset = ( 4 /* Length */
880 + 4 /* CIE Id */
881 + 1 /* CIE version */
882 + strlen (augmentation) + 1 /* Augmentation */
883 + size_of_uleb128 (1) /* Code alignment */
884 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
885 + 1 /* RA column */
886 + 1 /* Augmentation size */
887 + 1 /* Personality encoding */ );
888 int pad = -offset & (PTR_SIZE - 1);
890 augmentation_size += pad;
892 /* Augmentations should be small, so there's scarce need to
893 iterate for a solution. Die if we exceed one uleb128 byte. */
894 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
898 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
899 if (dw_cie_version >= 4)
901 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
902 dw2_asm_output_data (1, 0, "CIE Segment Size");
904 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
905 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
906 "CIE Data Alignment Factor");
908 if (dw_cie_version == 1)
909 dw2_asm_output_data (1, return_reg, "CIE RA Column");
910 else
911 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
913 if (augmentation[0])
915 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
916 if (personality)
918 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
919 eh_data_format_name (per_encoding));
920 dw2_asm_output_encoded_addr_rtx (per_encoding,
921 personality,
922 true, NULL);
925 if (any_lsda_needed)
926 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
927 eh_data_format_name (lsda_encoding));
929 if (fde_encoding != DW_EH_PE_absptr)
930 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
931 eh_data_format_name (fde_encoding));
934 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
935 output_cfi (cfi, NULL, for_eh);
937 /* Pad the CIE out to an address sized boundary. */
938 ASM_OUTPUT_ALIGN (asm_out_file,
939 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
940 ASM_OUTPUT_LABEL (asm_out_file, l2);
942 /* Loop through all of the FDE's. */
943 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
945 unsigned int k;
947 /* Don't emit EH unwind info for leaf functions that don't need it. */
948 if (for_eh && !fde_needed_for_eh_p (fde))
949 continue;
951 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
952 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
953 augmentation, any_lsda_needed, lsda_encoding);
956 if (for_eh && targetm.terminate_dw2_eh_frame_info)
957 dw2_asm_output_data (4, 0, "End of Table");
959 /* Turn off app to make assembly quicker. */
960 if (flag_debug_asm)
961 app_disable ();
964 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
966 static void
967 dwarf2out_do_cfi_startproc (bool second)
969 int enc;
970 rtx ref;
972 fprintf (asm_out_file, "\t.cfi_startproc\n");
974 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
975 eh unwinders. */
976 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
977 return;
979 rtx personality = get_personality_function (current_function_decl);
981 if (personality)
983 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
984 ref = personality;
986 /* ??? The GAS support isn't entirely consistent. We have to
987 handle indirect support ourselves, but PC-relative is done
988 in the assembler. Further, the assembler can't handle any
989 of the weirder relocation types. */
990 if (enc & DW_EH_PE_indirect)
991 ref = dw2_force_const_mem (ref, true);
993 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
994 output_addr_const (asm_out_file, ref);
995 fputc ('\n', asm_out_file);
998 if (crtl->uses_eh_lsda)
1000 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1002 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1003 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1004 current_function_funcdef_no);
1005 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1006 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008 if (enc & DW_EH_PE_indirect)
1009 ref = dw2_force_const_mem (ref, true);
1011 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1012 output_addr_const (asm_out_file, ref);
1013 fputc ('\n', asm_out_file);
1017 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1018 this allocation may be done before pass_final. */
1020 dw_fde_ref
1021 dwarf2out_alloc_current_fde (void)
1023 dw_fde_ref fde;
1025 fde = ggc_cleared_alloc<dw_fde_node> ();
1026 fde->decl = current_function_decl;
1027 fde->funcdef_number = current_function_funcdef_no;
1028 fde->fde_index = vec_safe_length (fde_vec);
1029 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1030 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1031 fde->nothrow = crtl->nothrow;
1032 fde->drap_reg = INVALID_REGNUM;
1033 fde->vdrap_reg = INVALID_REGNUM;
1035 /* Record the FDE associated with this function. */
1036 cfun->fde = fde;
1037 vec_safe_push (fde_vec, fde);
1039 return fde;
1042 /* Output a marker (i.e. a label) for the beginning of a function, before
1043 the prologue. */
1045 void
1046 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1047 unsigned int column ATTRIBUTE_UNUSED,
1048 const char *file ATTRIBUTE_UNUSED)
1050 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 char * dup_label;
1052 dw_fde_ref fde;
1053 section *fnsec;
1054 bool do_frame;
1056 current_function_func_begin_label = NULL;
1058 do_frame = dwarf2out_do_frame ();
1060 /* ??? current_function_func_begin_label is also used by except.c for
1061 call-site information. We must emit this label if it might be used. */
1062 if (!do_frame
1063 && (!flag_exceptions
1064 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065 return;
1067 fnsec = function_section (current_function_decl);
1068 switch_to_section (fnsec);
1069 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 dup_label = xstrdup (label);
1074 current_function_func_begin_label = dup_label;
1076 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1077 if (!do_frame)
1078 return;
1080 /* Unlike the debug version, the EH version of frame unwind info is a per-
1081 function setting so we need to record whether we need it for the unit. */
1082 do_eh_frame |= dwarf2out_do_eh_frame ();
1084 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1085 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1086 would include pass_dwarf2_frame. If we've not created the FDE yet,
1087 do so now. */
1088 fde = cfun->fde;
1089 if (fde == NULL)
1090 fde = dwarf2out_alloc_current_fde ();
1092 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1093 fde->dw_fde_begin = dup_label;
1094 fde->dw_fde_current_label = dup_label;
1095 fde->in_std_section = (fnsec == text_section
1096 || (cold_text_section && fnsec == cold_text_section));
1098 /* We only want to output line number information for the genuine dwarf2
1099 prologue case, not the eh frame case. */
1100 #ifdef DWARF2_DEBUGGING_INFO
1101 if (file)
1102 dwarf2out_source_line (line, column, file, 0, true);
1103 #endif
1105 if (dwarf2out_do_cfi_asm ())
1106 dwarf2out_do_cfi_startproc (false);
1107 else
1109 rtx personality = get_personality_function (current_function_decl);
1110 if (!current_unit_personality)
1111 current_unit_personality = personality;
1113 /* We cannot keep a current personality per function as without CFI
1114 asm, at the point where we emit the CFI data, there is no current
1115 function anymore. */
1116 if (personality && current_unit_personality != personality)
1117 sorry ("multiple EH personalities are supported only with assemblers "
1118 "supporting .cfi_personality directive");
1122 /* Output a marker (i.e. a label) for the end of the generated code
1123 for a function prologue. This gets called *after* the prologue code has
1124 been generated. */
1126 void
1127 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1128 const char *file ATTRIBUTE_UNUSED)
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 /* Output a label to mark the endpoint of the code generated for this
1133 function. */
1134 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1135 current_function_funcdef_no);
1136 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1137 current_function_funcdef_no);
1138 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1141 /* Output a marker (i.e. a label) for the beginning of the generated code
1142 for a function epilogue. This gets called *before* the prologue code has
1143 been generated. */
1145 void
1146 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1147 const char *file ATTRIBUTE_UNUSED)
1149 dw_fde_ref fde = cfun->fde;
1150 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1152 if (fde->dw_fde_vms_begin_epilogue)
1153 return;
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1160 current_function_funcdef_no);
1161 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the absolute end of the generated code
1165 for a function definition. This gets called *after* the epilogue code has
1166 been generated. */
1168 void
1169 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 last_var_location_insn = NULL;
1176 cached_next_real_insn = NULL;
1178 if (dwarf2out_do_cfi_asm ())
1179 fprintf (asm_out_file, "\t.cfi_endproc\n");
1181 /* Output a label to mark the endpoint of the code generated for this
1182 function. */
1183 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1184 current_function_funcdef_no);
1185 ASM_OUTPUT_LABEL (asm_out_file, label);
1186 fde = cfun->fde;
1187 gcc_assert (fde != NULL);
1188 if (fde->dw_fde_second_begin == NULL)
1189 fde->dw_fde_end = xstrdup (label);
1192 void
1193 dwarf2out_frame_finish (void)
1195 /* Output call frame information. */
1196 if (targetm.debug_unwind_info () == UI_DWARF2)
1197 output_call_frame_info (0);
1199 /* Output another copy for the unwinder. */
1200 if (do_eh_frame)
1201 output_call_frame_info (1);
1204 /* Note that the current function section is being used for code. */
1206 static void
1207 dwarf2out_note_section_used (void)
1209 section *sec = current_function_section ();
1210 if (sec == text_section)
1211 text_section_used = true;
1212 else if (sec == cold_text_section)
1213 cold_text_section_used = true;
1216 static void var_location_switch_text_section (void);
1217 static void set_cur_line_info_table (section *);
1219 void
1220 dwarf2out_switch_text_section (void)
1222 section *sect;
1223 dw_fde_ref fde = cfun->fde;
1225 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1227 if (!in_cold_section_p)
1229 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1230 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1231 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1233 else
1235 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1236 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1237 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1239 have_multiple_function_sections = true;
1241 /* There is no need to mark used sections when not debugging. */
1242 if (cold_text_section != NULL)
1243 dwarf2out_note_section_used ();
1245 if (dwarf2out_do_cfi_asm ())
1246 fprintf (asm_out_file, "\t.cfi_endproc\n");
1248 /* Now do the real section switch. */
1249 sect = current_function_section ();
1250 switch_to_section (sect);
1252 fde->second_in_std_section
1253 = (sect == text_section
1254 || (cold_text_section && sect == cold_text_section));
1256 if (dwarf2out_do_cfi_asm ())
1257 dwarf2out_do_cfi_startproc (true);
1259 var_location_switch_text_section ();
1261 if (cold_text_section != NULL)
1262 set_cur_line_info_table (sect);
1265 /* And now, the subset of the debugging information support code necessary
1266 for emitting location expressions. */
1268 /* Data about a single source file. */
1269 struct GTY((for_user)) dwarf_file_data {
1270 const char * filename;
1271 int emitted_number;
1274 /* Describe an entry into the .debug_addr section. */
1276 enum ate_kind {
1277 ate_kind_rtx,
1278 ate_kind_rtx_dtprel,
1279 ate_kind_label
1282 struct GTY((for_user)) addr_table_entry {
1283 enum ate_kind kind;
1284 unsigned int refcount;
1285 unsigned int index;
1286 union addr_table_entry_struct_union
1288 rtx GTY ((tag ("0"))) rtl;
1289 char * GTY ((tag ("1"))) label;
1291 GTY ((desc ("%1.kind"))) addr;
1294 typedef unsigned int var_loc_view;
1296 /* Location lists are ranges + location descriptions for that range,
1297 so you can track variables that are in different places over
1298 their entire life. */
1299 typedef struct GTY(()) dw_loc_list_struct {
1300 dw_loc_list_ref dw_loc_next;
1301 const char *begin; /* Label and addr_entry for start of range */
1302 addr_table_entry *begin_entry;
1303 const char *end; /* Label for end of range */
1304 char *ll_symbol; /* Label for beginning of location list.
1305 Only on head of list. */
1306 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1307 const char *section; /* Section this loclist is relative to */
1308 dw_loc_descr_ref expr;
1309 var_loc_view vbegin, vend;
1310 hashval_t hash;
1311 /* True if all addresses in this and subsequent lists are known to be
1312 resolved. */
1313 bool resolved_addr;
1314 /* True if this list has been replaced by dw_loc_next. */
1315 bool replaced;
1316 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1317 section. */
1318 unsigned char emitted : 1;
1319 /* True if hash field is index rather than hash value. */
1320 unsigned char num_assigned : 1;
1321 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1322 unsigned char offset_emitted : 1;
1323 /* True if note_variable_value_in_expr has been called on it. */
1324 unsigned char noted_variable_value : 1;
1325 /* True if the range should be emitted even if begin and end
1326 are the same. */
1327 bool force;
1328 } dw_loc_list_node;
1330 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1331 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1333 /* Convert a DWARF stack opcode into its string name. */
1335 static const char *
1336 dwarf_stack_op_name (unsigned int op)
1338 const char *name = get_DW_OP_name (op);
1340 if (name != NULL)
1341 return name;
1343 return "OP_<unknown>";
1346 /* Return TRUE iff we're to output location view lists as a separate
1347 attribute next to the location lists, as an extension compatible
1348 with DWARF 2 and above. */
1350 static inline bool
1351 dwarf2out_locviews_in_attribute ()
1353 return debug_variable_location_views == 1;
1356 /* Return TRUE iff we're to output location view lists as part of the
1357 location lists, as proposed for standardization after DWARF 5. */
1359 static inline bool
1360 dwarf2out_locviews_in_loclist ()
1362 #ifndef DW_LLE_view_pair
1363 return false;
1364 #else
1365 return debug_variable_location_views == -1;
1366 #endif
1369 /* Return a pointer to a newly allocated location description. Location
1370 descriptions are simple expression terms that can be strung
1371 together to form more complicated location (address) descriptions. */
1373 static inline dw_loc_descr_ref
1374 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1375 unsigned HOST_WIDE_INT oprnd2)
1377 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1379 descr->dw_loc_opc = op;
1380 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1381 descr->dw_loc_oprnd1.val_entry = NULL;
1382 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1383 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1384 descr->dw_loc_oprnd2.val_entry = NULL;
1385 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1387 return descr;
1390 /* Add a location description term to a location description expression. */
1392 static inline void
1393 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1395 dw_loc_descr_ref *d;
1397 /* Find the end of the chain. */
1398 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1401 *d = descr;
1404 /* Compare two location operands for exact equality. */
1406 static bool
1407 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1409 if (a->val_class != b->val_class)
1410 return false;
1411 switch (a->val_class)
1413 case dw_val_class_none:
1414 return true;
1415 case dw_val_class_addr:
1416 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1418 case dw_val_class_offset:
1419 case dw_val_class_unsigned_const:
1420 case dw_val_class_const:
1421 case dw_val_class_unsigned_const_implicit:
1422 case dw_val_class_const_implicit:
1423 case dw_val_class_range_list:
1424 /* These are all HOST_WIDE_INT, signed or unsigned. */
1425 return a->v.val_unsigned == b->v.val_unsigned;
1427 case dw_val_class_loc:
1428 return a->v.val_loc == b->v.val_loc;
1429 case dw_val_class_loc_list:
1430 return a->v.val_loc_list == b->v.val_loc_list;
1431 case dw_val_class_view_list:
1432 return a->v.val_view_list == b->v.val_view_list;
1433 case dw_val_class_die_ref:
1434 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1435 case dw_val_class_fde_ref:
1436 return a->v.val_fde_index == b->v.val_fde_index;
1437 case dw_val_class_lbl_id:
1438 case dw_val_class_lineptr:
1439 case dw_val_class_macptr:
1440 case dw_val_class_loclistsptr:
1441 case dw_val_class_high_pc:
1442 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1443 case dw_val_class_str:
1444 return a->v.val_str == b->v.val_str;
1445 case dw_val_class_flag:
1446 return a->v.val_flag == b->v.val_flag;
1447 case dw_val_class_file:
1448 case dw_val_class_file_implicit:
1449 return a->v.val_file == b->v.val_file;
1450 case dw_val_class_decl_ref:
1451 return a->v.val_decl_ref == b->v.val_decl_ref;
1453 case dw_val_class_const_double:
1454 return (a->v.val_double.high == b->v.val_double.high
1455 && a->v.val_double.low == b->v.val_double.low);
1457 case dw_val_class_wide_int:
1458 return *a->v.val_wide == *b->v.val_wide;
1460 case dw_val_class_vec:
1462 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1463 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1465 return (a_len == b_len
1466 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1469 case dw_val_class_data8:
1470 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1472 case dw_val_class_vms_delta:
1473 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1474 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1476 case dw_val_class_discr_value:
1477 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1478 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1479 case dw_val_class_discr_list:
1480 /* It makes no sense comparing two discriminant value lists. */
1481 return false;
1483 gcc_unreachable ();
1486 /* Compare two location atoms for exact equality. */
1488 static bool
1489 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1491 if (a->dw_loc_opc != b->dw_loc_opc)
1492 return false;
1494 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1495 address size, but since we always allocate cleared storage it
1496 should be zero for other types of locations. */
1497 if (a->dtprel != b->dtprel)
1498 return false;
1500 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1501 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1504 /* Compare two complete location expressions for exact equality. */
1506 bool
1507 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1509 while (1)
1511 if (a == b)
1512 return true;
1513 if (a == NULL || b == NULL)
1514 return false;
1515 if (!loc_descr_equal_p_1 (a, b))
1516 return false;
1518 a = a->dw_loc_next;
1519 b = b->dw_loc_next;
1524 /* Add a constant POLY_OFFSET to a location expression. */
1526 static void
1527 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1529 dw_loc_descr_ref loc;
1530 HOST_WIDE_INT *p;
1532 gcc_assert (*list_head != NULL);
1534 if (known_eq (poly_offset, 0))
1535 return;
1537 /* Find the end of the chain. */
1538 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1541 HOST_WIDE_INT offset;
1542 if (!poly_offset.is_constant (&offset))
1544 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1545 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1546 return;
1549 p = NULL;
1550 if (loc->dw_loc_opc == DW_OP_fbreg
1551 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1552 p = &loc->dw_loc_oprnd1.v.val_int;
1553 else if (loc->dw_loc_opc == DW_OP_bregx)
1554 p = &loc->dw_loc_oprnd2.v.val_int;
1556 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1557 offset. Don't optimize if an signed integer overflow would happen. */
1558 if (p != NULL
1559 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1560 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1561 *p += offset;
1563 else if (offset > 0)
1564 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1566 else
1568 loc->dw_loc_next
1569 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1570 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1574 /* Return a pointer to a newly allocated location description for
1575 REG and OFFSET. */
1577 static inline dw_loc_descr_ref
1578 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1580 HOST_WIDE_INT const_offset;
1581 if (offset.is_constant (&const_offset))
1583 if (reg <= 31)
1584 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1585 const_offset, 0);
1586 else
1587 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1589 else
1591 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1592 loc_descr_plus_const (&ret, offset);
1593 return ret;
1597 /* Add a constant OFFSET to a location list. */
1599 static void
1600 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1602 dw_loc_list_ref d;
1603 for (d = list_head; d != NULL; d = d->dw_loc_next)
1604 loc_descr_plus_const (&d->expr, offset);
1607 #define DWARF_REF_SIZE \
1608 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1610 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1611 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1612 DW_FORM_data16 with 128 bits. */
1613 #define DWARF_LARGEST_DATA_FORM_BITS \
1614 (dwarf_version >= 5 ? 128 : 64)
1616 /* Utility inline function for construction of ops that were GNU extension
1617 before DWARF 5. */
1618 static inline enum dwarf_location_atom
1619 dwarf_OP (enum dwarf_location_atom op)
1621 switch (op)
1623 case DW_OP_implicit_pointer:
1624 if (dwarf_version < 5)
1625 return DW_OP_GNU_implicit_pointer;
1626 break;
1628 case DW_OP_entry_value:
1629 if (dwarf_version < 5)
1630 return DW_OP_GNU_entry_value;
1631 break;
1633 case DW_OP_const_type:
1634 if (dwarf_version < 5)
1635 return DW_OP_GNU_const_type;
1636 break;
1638 case DW_OP_regval_type:
1639 if (dwarf_version < 5)
1640 return DW_OP_GNU_regval_type;
1641 break;
1643 case DW_OP_deref_type:
1644 if (dwarf_version < 5)
1645 return DW_OP_GNU_deref_type;
1646 break;
1648 case DW_OP_convert:
1649 if (dwarf_version < 5)
1650 return DW_OP_GNU_convert;
1651 break;
1653 case DW_OP_reinterpret:
1654 if (dwarf_version < 5)
1655 return DW_OP_GNU_reinterpret;
1656 break;
1658 default:
1659 break;
1661 return op;
1664 /* Similarly for attributes. */
1665 static inline enum dwarf_attribute
1666 dwarf_AT (enum dwarf_attribute at)
1668 switch (at)
1670 case DW_AT_call_return_pc:
1671 if (dwarf_version < 5)
1672 return DW_AT_low_pc;
1673 break;
1675 case DW_AT_call_tail_call:
1676 if (dwarf_version < 5)
1677 return DW_AT_GNU_tail_call;
1678 break;
1680 case DW_AT_call_origin:
1681 if (dwarf_version < 5)
1682 return DW_AT_abstract_origin;
1683 break;
1685 case DW_AT_call_target:
1686 if (dwarf_version < 5)
1687 return DW_AT_GNU_call_site_target;
1688 break;
1690 case DW_AT_call_target_clobbered:
1691 if (dwarf_version < 5)
1692 return DW_AT_GNU_call_site_target_clobbered;
1693 break;
1695 case DW_AT_call_parameter:
1696 if (dwarf_version < 5)
1697 return DW_AT_abstract_origin;
1698 break;
1700 case DW_AT_call_value:
1701 if (dwarf_version < 5)
1702 return DW_AT_GNU_call_site_value;
1703 break;
1705 case DW_AT_call_data_value:
1706 if (dwarf_version < 5)
1707 return DW_AT_GNU_call_site_data_value;
1708 break;
1710 case DW_AT_call_all_calls:
1711 if (dwarf_version < 5)
1712 return DW_AT_GNU_all_call_sites;
1713 break;
1715 case DW_AT_call_all_tail_calls:
1716 if (dwarf_version < 5)
1717 return DW_AT_GNU_all_tail_call_sites;
1718 break;
1720 case DW_AT_dwo_name:
1721 if (dwarf_version < 5)
1722 return DW_AT_GNU_dwo_name;
1723 break;
1725 default:
1726 break;
1728 return at;
1731 /* And similarly for tags. */
1732 static inline enum dwarf_tag
1733 dwarf_TAG (enum dwarf_tag tag)
1735 switch (tag)
1737 case DW_TAG_call_site:
1738 if (dwarf_version < 5)
1739 return DW_TAG_GNU_call_site;
1740 break;
1742 case DW_TAG_call_site_parameter:
1743 if (dwarf_version < 5)
1744 return DW_TAG_GNU_call_site_parameter;
1745 break;
1747 default:
1748 break;
1750 return tag;
1753 static unsigned long int get_base_type_offset (dw_die_ref);
1755 /* Return the size of a location descriptor. */
1757 static unsigned long
1758 size_of_loc_descr (dw_loc_descr_ref loc)
1760 unsigned long size = 1;
1762 switch (loc->dw_loc_opc)
1764 case DW_OP_addr:
1765 size += DWARF2_ADDR_SIZE;
1766 break;
1767 case DW_OP_GNU_addr_index:
1768 case DW_OP_GNU_const_index:
1769 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1770 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1771 break;
1772 case DW_OP_const1u:
1773 case DW_OP_const1s:
1774 size += 1;
1775 break;
1776 case DW_OP_const2u:
1777 case DW_OP_const2s:
1778 size += 2;
1779 break;
1780 case DW_OP_const4u:
1781 case DW_OP_const4s:
1782 size += 4;
1783 break;
1784 case DW_OP_const8u:
1785 case DW_OP_const8s:
1786 size += 8;
1787 break;
1788 case DW_OP_constu:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 break;
1791 case DW_OP_consts:
1792 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1793 break;
1794 case DW_OP_pick:
1795 size += 1;
1796 break;
1797 case DW_OP_plus_uconst:
1798 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1799 break;
1800 case DW_OP_skip:
1801 case DW_OP_bra:
1802 size += 2;
1803 break;
1804 case DW_OP_breg0:
1805 case DW_OP_breg1:
1806 case DW_OP_breg2:
1807 case DW_OP_breg3:
1808 case DW_OP_breg4:
1809 case DW_OP_breg5:
1810 case DW_OP_breg6:
1811 case DW_OP_breg7:
1812 case DW_OP_breg8:
1813 case DW_OP_breg9:
1814 case DW_OP_breg10:
1815 case DW_OP_breg11:
1816 case DW_OP_breg12:
1817 case DW_OP_breg13:
1818 case DW_OP_breg14:
1819 case DW_OP_breg15:
1820 case DW_OP_breg16:
1821 case DW_OP_breg17:
1822 case DW_OP_breg18:
1823 case DW_OP_breg19:
1824 case DW_OP_breg20:
1825 case DW_OP_breg21:
1826 case DW_OP_breg22:
1827 case DW_OP_breg23:
1828 case DW_OP_breg24:
1829 case DW_OP_breg25:
1830 case DW_OP_breg26:
1831 case DW_OP_breg27:
1832 case DW_OP_breg28:
1833 case DW_OP_breg29:
1834 case DW_OP_breg30:
1835 case DW_OP_breg31:
1836 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1837 break;
1838 case DW_OP_regx:
1839 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1840 break;
1841 case DW_OP_fbreg:
1842 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1843 break;
1844 case DW_OP_bregx:
1845 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1846 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1847 break;
1848 case DW_OP_piece:
1849 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1850 break;
1851 case DW_OP_bit_piece:
1852 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1854 break;
1855 case DW_OP_deref_size:
1856 case DW_OP_xderef_size:
1857 size += 1;
1858 break;
1859 case DW_OP_call2:
1860 size += 2;
1861 break;
1862 case DW_OP_call4:
1863 size += 4;
1864 break;
1865 case DW_OP_call_ref:
1866 case DW_OP_GNU_variable_value:
1867 size += DWARF_REF_SIZE;
1868 break;
1869 case DW_OP_implicit_value:
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1871 + loc->dw_loc_oprnd1.v.val_unsigned;
1872 break;
1873 case DW_OP_implicit_pointer:
1874 case DW_OP_GNU_implicit_pointer:
1875 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1876 break;
1877 case DW_OP_entry_value:
1878 case DW_OP_GNU_entry_value:
1880 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1881 size += size_of_uleb128 (op_size) + op_size;
1882 break;
1884 case DW_OP_const_type:
1885 case DW_OP_GNU_const_type:
1887 unsigned long o
1888 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1889 size += size_of_uleb128 (o) + 1;
1890 switch (loc->dw_loc_oprnd2.val_class)
1892 case dw_val_class_vec:
1893 size += loc->dw_loc_oprnd2.v.val_vec.length
1894 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1895 break;
1896 case dw_val_class_const:
1897 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1898 break;
1899 case dw_val_class_const_double:
1900 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1901 break;
1902 case dw_val_class_wide_int:
1903 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1904 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1905 break;
1906 default:
1907 gcc_unreachable ();
1909 break;
1911 case DW_OP_regval_type:
1912 case DW_OP_GNU_regval_type:
1914 unsigned long o
1915 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1916 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1917 + size_of_uleb128 (o);
1919 break;
1920 case DW_OP_deref_type:
1921 case DW_OP_GNU_deref_type:
1923 unsigned long o
1924 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1925 size += 1 + size_of_uleb128 (o);
1927 break;
1928 case DW_OP_convert:
1929 case DW_OP_reinterpret:
1930 case DW_OP_GNU_convert:
1931 case DW_OP_GNU_reinterpret:
1932 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1934 else
1936 unsigned long o
1937 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1938 size += size_of_uleb128 (o);
1940 break;
1941 case DW_OP_GNU_parameter_ref:
1942 size += 4;
1943 break;
1944 default:
1945 break;
1948 return size;
1951 /* Return the size of a series of location descriptors. */
1953 unsigned long
1954 size_of_locs (dw_loc_descr_ref loc)
1956 dw_loc_descr_ref l;
1957 unsigned long size;
1959 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1960 field, to avoid writing to a PCH file. */
1961 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1963 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1964 break;
1965 size += size_of_loc_descr (l);
1967 if (! l)
1968 return size;
1970 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1972 l->dw_loc_addr = size;
1973 size += size_of_loc_descr (l);
1976 return size;
1979 /* Return the size of the value in a DW_AT_discr_value attribute. */
1981 static int
1982 size_of_discr_value (dw_discr_value *discr_value)
1984 if (discr_value->pos)
1985 return size_of_uleb128 (discr_value->v.uval);
1986 else
1987 return size_of_sleb128 (discr_value->v.sval);
1990 /* Return the size of the value in a DW_AT_discr_list attribute. */
1992 static int
1993 size_of_discr_list (dw_discr_list_ref discr_list)
1995 int size = 0;
1997 for (dw_discr_list_ref list = discr_list;
1998 list != NULL;
1999 list = list->dw_discr_next)
2001 /* One byte for the discriminant value descriptor, and then one or two
2002 LEB128 numbers, depending on whether it's a single case label or a
2003 range label. */
2004 size += 1;
2005 size += size_of_discr_value (&list->dw_discr_lower_bound);
2006 if (list->dw_discr_range != 0)
2007 size += size_of_discr_value (&list->dw_discr_upper_bound);
2009 return size;
2012 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2013 static void get_ref_die_offset_label (char *, dw_die_ref);
2014 static unsigned long int get_ref_die_offset (dw_die_ref);
2016 /* Output location description stack opcode's operands (if any).
2017 The for_eh_or_skip parameter controls whether register numbers are
2018 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2019 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2020 info). This should be suppressed for the cases that have not been converted
2021 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2023 static void
2024 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2026 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2027 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2029 switch (loc->dw_loc_opc)
2031 #ifdef DWARF2_DEBUGGING_INFO
2032 case DW_OP_const2u:
2033 case DW_OP_const2s:
2034 dw2_asm_output_data (2, val1->v.val_int, NULL);
2035 break;
2036 case DW_OP_const4u:
2037 if (loc->dtprel)
2039 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2040 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2041 val1->v.val_addr);
2042 fputc ('\n', asm_out_file);
2043 break;
2045 /* FALLTHRU */
2046 case DW_OP_const4s:
2047 dw2_asm_output_data (4, val1->v.val_int, NULL);
2048 break;
2049 case DW_OP_const8u:
2050 if (loc->dtprel)
2052 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2053 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2054 val1->v.val_addr);
2055 fputc ('\n', asm_out_file);
2056 break;
2058 /* FALLTHRU */
2059 case DW_OP_const8s:
2060 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2061 dw2_asm_output_data (8, val1->v.val_int, NULL);
2062 break;
2063 case DW_OP_skip:
2064 case DW_OP_bra:
2066 int offset;
2068 gcc_assert (val1->val_class == dw_val_class_loc);
2069 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2071 dw2_asm_output_data (2, offset, NULL);
2073 break;
2074 case DW_OP_implicit_value:
2075 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2076 switch (val2->val_class)
2078 case dw_val_class_const:
2079 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2080 break;
2081 case dw_val_class_vec:
2083 unsigned int elt_size = val2->v.val_vec.elt_size;
2084 unsigned int len = val2->v.val_vec.length;
2085 unsigned int i;
2086 unsigned char *p;
2088 if (elt_size > sizeof (HOST_WIDE_INT))
2090 elt_size /= 2;
2091 len *= 2;
2093 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2094 i < len;
2095 i++, p += elt_size)
2096 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2097 "fp or vector constant word %u", i);
2099 break;
2100 case dw_val_class_const_double:
2102 unsigned HOST_WIDE_INT first, second;
2104 if (WORDS_BIG_ENDIAN)
2106 first = val2->v.val_double.high;
2107 second = val2->v.val_double.low;
2109 else
2111 first = val2->v.val_double.low;
2112 second = val2->v.val_double.high;
2114 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2115 first, NULL);
2116 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2117 second, NULL);
2119 break;
2120 case dw_val_class_wide_int:
2122 int i;
2123 int len = get_full_len (*val2->v.val_wide);
2124 if (WORDS_BIG_ENDIAN)
2125 for (i = len - 1; i >= 0; --i)
2126 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2127 val2->v.val_wide->elt (i), NULL);
2128 else
2129 for (i = 0; i < len; ++i)
2130 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2131 val2->v.val_wide->elt (i), NULL);
2133 break;
2134 case dw_val_class_addr:
2135 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2136 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2137 break;
2138 default:
2139 gcc_unreachable ();
2141 break;
2142 #else
2143 case DW_OP_const2u:
2144 case DW_OP_const2s:
2145 case DW_OP_const4u:
2146 case DW_OP_const4s:
2147 case DW_OP_const8u:
2148 case DW_OP_const8s:
2149 case DW_OP_skip:
2150 case DW_OP_bra:
2151 case DW_OP_implicit_value:
2152 /* We currently don't make any attempt to make sure these are
2153 aligned properly like we do for the main unwind info, so
2154 don't support emitting things larger than a byte if we're
2155 only doing unwinding. */
2156 gcc_unreachable ();
2157 #endif
2158 case DW_OP_const1u:
2159 case DW_OP_const1s:
2160 dw2_asm_output_data (1, val1->v.val_int, NULL);
2161 break;
2162 case DW_OP_constu:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 break;
2165 case DW_OP_consts:
2166 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2167 break;
2168 case DW_OP_pick:
2169 dw2_asm_output_data (1, val1->v.val_int, NULL);
2170 break;
2171 case DW_OP_plus_uconst:
2172 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2173 break;
2174 case DW_OP_breg0:
2175 case DW_OP_breg1:
2176 case DW_OP_breg2:
2177 case DW_OP_breg3:
2178 case DW_OP_breg4:
2179 case DW_OP_breg5:
2180 case DW_OP_breg6:
2181 case DW_OP_breg7:
2182 case DW_OP_breg8:
2183 case DW_OP_breg9:
2184 case DW_OP_breg10:
2185 case DW_OP_breg11:
2186 case DW_OP_breg12:
2187 case DW_OP_breg13:
2188 case DW_OP_breg14:
2189 case DW_OP_breg15:
2190 case DW_OP_breg16:
2191 case DW_OP_breg17:
2192 case DW_OP_breg18:
2193 case DW_OP_breg19:
2194 case DW_OP_breg20:
2195 case DW_OP_breg21:
2196 case DW_OP_breg22:
2197 case DW_OP_breg23:
2198 case DW_OP_breg24:
2199 case DW_OP_breg25:
2200 case DW_OP_breg26:
2201 case DW_OP_breg27:
2202 case DW_OP_breg28:
2203 case DW_OP_breg29:
2204 case DW_OP_breg30:
2205 case DW_OP_breg31:
2206 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2207 break;
2208 case DW_OP_regx:
2210 unsigned r = val1->v.val_unsigned;
2211 if (for_eh_or_skip >= 0)
2212 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2213 gcc_assert (size_of_uleb128 (r)
2214 == size_of_uleb128 (val1->v.val_unsigned));
2215 dw2_asm_output_data_uleb128 (r, NULL);
2217 break;
2218 case DW_OP_fbreg:
2219 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2220 break;
2221 case DW_OP_bregx:
2223 unsigned r = val1->v.val_unsigned;
2224 if (for_eh_or_skip >= 0)
2225 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2226 gcc_assert (size_of_uleb128 (r)
2227 == size_of_uleb128 (val1->v.val_unsigned));
2228 dw2_asm_output_data_uleb128 (r, NULL);
2229 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2231 break;
2232 case DW_OP_piece:
2233 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2234 break;
2235 case DW_OP_bit_piece:
2236 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2237 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2238 break;
2239 case DW_OP_deref_size:
2240 case DW_OP_xderef_size:
2241 dw2_asm_output_data (1, val1->v.val_int, NULL);
2242 break;
2244 case DW_OP_addr:
2245 if (loc->dtprel)
2247 if (targetm.asm_out.output_dwarf_dtprel)
2249 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2250 DWARF2_ADDR_SIZE,
2251 val1->v.val_addr);
2252 fputc ('\n', asm_out_file);
2254 else
2255 gcc_unreachable ();
2257 else
2259 #ifdef DWARF2_DEBUGGING_INFO
2260 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2261 #else
2262 gcc_unreachable ();
2263 #endif
2265 break;
2267 case DW_OP_GNU_addr_index:
2268 case DW_OP_GNU_const_index:
2269 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2270 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2271 "(index into .debug_addr)");
2272 break;
2274 case DW_OP_call2:
2275 case DW_OP_call4:
2277 unsigned long die_offset
2278 = get_ref_die_offset (val1->v.val_die_ref.die);
2279 /* Make sure the offset has been computed and that we can encode it as
2280 an operand. */
2281 gcc_assert (die_offset > 0
2282 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2283 ? 0xffff
2284 : 0xffffffff));
2285 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2286 die_offset, NULL);
2288 break;
2290 case DW_OP_call_ref:
2291 case DW_OP_GNU_variable_value:
2293 char label[MAX_ARTIFICIAL_LABEL_BYTES
2294 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2295 gcc_assert (val1->val_class == dw_val_class_die_ref);
2296 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2297 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2299 break;
2301 case DW_OP_implicit_pointer:
2302 case DW_OP_GNU_implicit_pointer:
2304 char label[MAX_ARTIFICIAL_LABEL_BYTES
2305 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2306 gcc_assert (val1->val_class == dw_val_class_die_ref);
2307 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2308 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2309 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2311 break;
2313 case DW_OP_entry_value:
2314 case DW_OP_GNU_entry_value:
2315 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2316 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2317 break;
2319 case DW_OP_const_type:
2320 case DW_OP_GNU_const_type:
2322 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2323 gcc_assert (o);
2324 dw2_asm_output_data_uleb128 (o, NULL);
2325 switch (val2->val_class)
2327 case dw_val_class_const:
2328 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2329 dw2_asm_output_data (1, l, NULL);
2330 dw2_asm_output_data (l, val2->v.val_int, NULL);
2331 break;
2332 case dw_val_class_vec:
2334 unsigned int elt_size = val2->v.val_vec.elt_size;
2335 unsigned int len = val2->v.val_vec.length;
2336 unsigned int i;
2337 unsigned char *p;
2339 l = len * elt_size;
2340 dw2_asm_output_data (1, l, NULL);
2341 if (elt_size > sizeof (HOST_WIDE_INT))
2343 elt_size /= 2;
2344 len *= 2;
2346 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2347 i < len;
2348 i++, p += elt_size)
2349 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2350 "fp or vector constant word %u", i);
2352 break;
2353 case dw_val_class_const_double:
2355 unsigned HOST_WIDE_INT first, second;
2356 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2358 dw2_asm_output_data (1, 2 * l, NULL);
2359 if (WORDS_BIG_ENDIAN)
2361 first = val2->v.val_double.high;
2362 second = val2->v.val_double.low;
2364 else
2366 first = val2->v.val_double.low;
2367 second = val2->v.val_double.high;
2369 dw2_asm_output_data (l, first, NULL);
2370 dw2_asm_output_data (l, second, NULL);
2372 break;
2373 case dw_val_class_wide_int:
2375 int i;
2376 int len = get_full_len (*val2->v.val_wide);
2377 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2379 dw2_asm_output_data (1, len * l, NULL);
2380 if (WORDS_BIG_ENDIAN)
2381 for (i = len - 1; i >= 0; --i)
2382 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2383 else
2384 for (i = 0; i < len; ++i)
2385 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2387 break;
2388 default:
2389 gcc_unreachable ();
2392 break;
2393 case DW_OP_regval_type:
2394 case DW_OP_GNU_regval_type:
2396 unsigned r = val1->v.val_unsigned;
2397 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2398 gcc_assert (o);
2399 if (for_eh_or_skip >= 0)
2401 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2402 gcc_assert (size_of_uleb128 (r)
2403 == size_of_uleb128 (val1->v.val_unsigned));
2405 dw2_asm_output_data_uleb128 (r, NULL);
2406 dw2_asm_output_data_uleb128 (o, NULL);
2408 break;
2409 case DW_OP_deref_type:
2410 case DW_OP_GNU_deref_type:
2412 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2413 gcc_assert (o);
2414 dw2_asm_output_data (1, val1->v.val_int, NULL);
2415 dw2_asm_output_data_uleb128 (o, NULL);
2417 break;
2418 case DW_OP_convert:
2419 case DW_OP_reinterpret:
2420 case DW_OP_GNU_convert:
2421 case DW_OP_GNU_reinterpret:
2422 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2423 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2424 else
2426 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2427 gcc_assert (o);
2428 dw2_asm_output_data_uleb128 (o, NULL);
2430 break;
2432 case DW_OP_GNU_parameter_ref:
2434 unsigned long o;
2435 gcc_assert (val1->val_class == dw_val_class_die_ref);
2436 o = get_ref_die_offset (val1->v.val_die_ref.die);
2437 dw2_asm_output_data (4, o, NULL);
2439 break;
2441 default:
2442 /* Other codes have no operands. */
2443 break;
2447 /* Output a sequence of location operations.
2448 The for_eh_or_skip parameter controls whether register numbers are
2449 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2450 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2451 info). This should be suppressed for the cases that have not been converted
2452 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2454 void
2455 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2457 for (; loc != NULL; loc = loc->dw_loc_next)
2459 enum dwarf_location_atom opc = loc->dw_loc_opc;
2460 /* Output the opcode. */
2461 if (for_eh_or_skip >= 0
2462 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2464 unsigned r = (opc - DW_OP_breg0);
2465 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2466 gcc_assert (r <= 31);
2467 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2469 else if (for_eh_or_skip >= 0
2470 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2472 unsigned r = (opc - DW_OP_reg0);
2473 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2474 gcc_assert (r <= 31);
2475 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2478 dw2_asm_output_data (1, opc,
2479 "%s", dwarf_stack_op_name (opc));
2481 /* Output the operand(s) (if any). */
2482 output_loc_operands (loc, for_eh_or_skip);
2486 /* Output location description stack opcode's operands (if any).
2487 The output is single bytes on a line, suitable for .cfi_escape. */
2489 static void
2490 output_loc_operands_raw (dw_loc_descr_ref loc)
2492 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2493 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2495 switch (loc->dw_loc_opc)
2497 case DW_OP_addr:
2498 case DW_OP_GNU_addr_index:
2499 case DW_OP_GNU_const_index:
2500 case DW_OP_implicit_value:
2501 /* We cannot output addresses in .cfi_escape, only bytes. */
2502 gcc_unreachable ();
2504 case DW_OP_const1u:
2505 case DW_OP_const1s:
2506 case DW_OP_pick:
2507 case DW_OP_deref_size:
2508 case DW_OP_xderef_size:
2509 fputc (',', asm_out_file);
2510 dw2_asm_output_data_raw (1, val1->v.val_int);
2511 break;
2513 case DW_OP_const2u:
2514 case DW_OP_const2s:
2515 fputc (',', asm_out_file);
2516 dw2_asm_output_data_raw (2, val1->v.val_int);
2517 break;
2519 case DW_OP_const4u:
2520 case DW_OP_const4s:
2521 fputc (',', asm_out_file);
2522 dw2_asm_output_data_raw (4, val1->v.val_int);
2523 break;
2525 case DW_OP_const8u:
2526 case DW_OP_const8s:
2527 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2528 fputc (',', asm_out_file);
2529 dw2_asm_output_data_raw (8, val1->v.val_int);
2530 break;
2532 case DW_OP_skip:
2533 case DW_OP_bra:
2535 int offset;
2537 gcc_assert (val1->val_class == dw_val_class_loc);
2538 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2540 fputc (',', asm_out_file);
2541 dw2_asm_output_data_raw (2, offset);
2543 break;
2545 case DW_OP_regx:
2547 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2548 gcc_assert (size_of_uleb128 (r)
2549 == size_of_uleb128 (val1->v.val_unsigned));
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_uleb128_raw (r);
2553 break;
2555 case DW_OP_constu:
2556 case DW_OP_plus_uconst:
2557 case DW_OP_piece:
2558 fputc (',', asm_out_file);
2559 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2560 break;
2562 case DW_OP_bit_piece:
2563 fputc (',', asm_out_file);
2564 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2565 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2566 break;
2568 case DW_OP_consts:
2569 case DW_OP_breg0:
2570 case DW_OP_breg1:
2571 case DW_OP_breg2:
2572 case DW_OP_breg3:
2573 case DW_OP_breg4:
2574 case DW_OP_breg5:
2575 case DW_OP_breg6:
2576 case DW_OP_breg7:
2577 case DW_OP_breg8:
2578 case DW_OP_breg9:
2579 case DW_OP_breg10:
2580 case DW_OP_breg11:
2581 case DW_OP_breg12:
2582 case DW_OP_breg13:
2583 case DW_OP_breg14:
2584 case DW_OP_breg15:
2585 case DW_OP_breg16:
2586 case DW_OP_breg17:
2587 case DW_OP_breg18:
2588 case DW_OP_breg19:
2589 case DW_OP_breg20:
2590 case DW_OP_breg21:
2591 case DW_OP_breg22:
2592 case DW_OP_breg23:
2593 case DW_OP_breg24:
2594 case DW_OP_breg25:
2595 case DW_OP_breg26:
2596 case DW_OP_breg27:
2597 case DW_OP_breg28:
2598 case DW_OP_breg29:
2599 case DW_OP_breg30:
2600 case DW_OP_breg31:
2601 case DW_OP_fbreg:
2602 fputc (',', asm_out_file);
2603 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2604 break;
2606 case DW_OP_bregx:
2608 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2609 gcc_assert (size_of_uleb128 (r)
2610 == size_of_uleb128 (val1->v.val_unsigned));
2611 fputc (',', asm_out_file);
2612 dw2_asm_output_data_uleb128_raw (r);
2613 fputc (',', asm_out_file);
2614 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2616 break;
2618 case DW_OP_implicit_pointer:
2619 case DW_OP_entry_value:
2620 case DW_OP_const_type:
2621 case DW_OP_regval_type:
2622 case DW_OP_deref_type:
2623 case DW_OP_convert:
2624 case DW_OP_reinterpret:
2625 case DW_OP_GNU_implicit_pointer:
2626 case DW_OP_GNU_entry_value:
2627 case DW_OP_GNU_const_type:
2628 case DW_OP_GNU_regval_type:
2629 case DW_OP_GNU_deref_type:
2630 case DW_OP_GNU_convert:
2631 case DW_OP_GNU_reinterpret:
2632 case DW_OP_GNU_parameter_ref:
2633 gcc_unreachable ();
2634 break;
2636 default:
2637 /* Other codes have no operands. */
2638 break;
2642 void
2643 output_loc_sequence_raw (dw_loc_descr_ref loc)
2645 while (1)
2647 enum dwarf_location_atom opc = loc->dw_loc_opc;
2648 /* Output the opcode. */
2649 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2651 unsigned r = (opc - DW_OP_breg0);
2652 r = DWARF2_FRAME_REG_OUT (r, 1);
2653 gcc_assert (r <= 31);
2654 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2656 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2658 unsigned r = (opc - DW_OP_reg0);
2659 r = DWARF2_FRAME_REG_OUT (r, 1);
2660 gcc_assert (r <= 31);
2661 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2663 /* Output the opcode. */
2664 fprintf (asm_out_file, "%#x", opc);
2665 output_loc_operands_raw (loc);
2667 if (!loc->dw_loc_next)
2668 break;
2669 loc = loc->dw_loc_next;
2671 fputc (',', asm_out_file);
2675 /* This function builds a dwarf location descriptor sequence from a
2676 dw_cfa_location, adding the given OFFSET to the result of the
2677 expression. */
2679 struct dw_loc_descr_node *
2680 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2682 struct dw_loc_descr_node *head, *tmp;
2684 offset += cfa->offset;
2686 if (cfa->indirect)
2688 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2689 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2690 head->dw_loc_oprnd1.val_entry = NULL;
2691 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2692 add_loc_descr (&head, tmp);
2693 loc_descr_plus_const (&head, offset);
2695 else
2696 head = new_reg_loc_descr (cfa->reg, offset);
2698 return head;
2701 /* This function builds a dwarf location descriptor sequence for
2702 the address at OFFSET from the CFA when stack is aligned to
2703 ALIGNMENT byte. */
2705 struct dw_loc_descr_node *
2706 build_cfa_aligned_loc (dw_cfa_location *cfa,
2707 poly_int64 offset, HOST_WIDE_INT alignment)
2709 struct dw_loc_descr_node *head;
2710 unsigned int dwarf_fp
2711 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2713 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2714 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2716 head = new_reg_loc_descr (dwarf_fp, 0);
2717 add_loc_descr (&head, int_loc_descriptor (alignment));
2718 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2719 loc_descr_plus_const (&head, offset);
2721 else
2722 head = new_reg_loc_descr (dwarf_fp, offset);
2723 return head;
2726 /* And now, the support for symbolic debugging information. */
2728 /* .debug_str support. */
2730 static void dwarf2out_init (const char *);
2731 static void dwarf2out_finish (const char *);
2732 static void dwarf2out_early_finish (const char *);
2733 static void dwarf2out_assembly_start (void);
2734 static void dwarf2out_define (unsigned int, const char *);
2735 static void dwarf2out_undef (unsigned int, const char *);
2736 static void dwarf2out_start_source_file (unsigned, const char *);
2737 static void dwarf2out_end_source_file (unsigned);
2738 static void dwarf2out_function_decl (tree);
2739 static void dwarf2out_begin_block (unsigned, unsigned);
2740 static void dwarf2out_end_block (unsigned, unsigned);
2741 static bool dwarf2out_ignore_block (const_tree);
2742 static void dwarf2out_early_global_decl (tree);
2743 static void dwarf2out_late_global_decl (tree);
2744 static void dwarf2out_type_decl (tree, int);
2745 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2746 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2747 dw_die_ref);
2748 static void dwarf2out_abstract_function (tree);
2749 static void dwarf2out_var_location (rtx_insn *);
2750 static void dwarf2out_inline_entry (tree);
2751 static void dwarf2out_size_function (tree);
2752 static void dwarf2out_begin_function (tree);
2753 static void dwarf2out_end_function (unsigned int);
2754 static void dwarf2out_register_main_translation_unit (tree unit);
2755 static void dwarf2out_set_name (tree, tree);
2756 static void dwarf2out_register_external_die (tree decl, const char *sym,
2757 unsigned HOST_WIDE_INT off);
2758 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2759 unsigned HOST_WIDE_INT *off);
2761 /* The debug hooks structure. */
2763 const struct gcc_debug_hooks dwarf2_debug_hooks =
2765 dwarf2out_init,
2766 dwarf2out_finish,
2767 dwarf2out_early_finish,
2768 dwarf2out_assembly_start,
2769 dwarf2out_define,
2770 dwarf2out_undef,
2771 dwarf2out_start_source_file,
2772 dwarf2out_end_source_file,
2773 dwarf2out_begin_block,
2774 dwarf2out_end_block,
2775 dwarf2out_ignore_block,
2776 dwarf2out_source_line,
2777 dwarf2out_begin_prologue,
2778 #if VMS_DEBUGGING_INFO
2779 dwarf2out_vms_end_prologue,
2780 dwarf2out_vms_begin_epilogue,
2781 #else
2782 debug_nothing_int_charstar,
2783 debug_nothing_int_charstar,
2784 #endif
2785 dwarf2out_end_epilogue,
2786 dwarf2out_begin_function,
2787 dwarf2out_end_function, /* end_function */
2788 dwarf2out_register_main_translation_unit,
2789 dwarf2out_function_decl, /* function_decl */
2790 dwarf2out_early_global_decl,
2791 dwarf2out_late_global_decl,
2792 dwarf2out_type_decl, /* type_decl */
2793 dwarf2out_imported_module_or_decl,
2794 dwarf2out_die_ref_for_decl,
2795 dwarf2out_register_external_die,
2796 debug_nothing_tree, /* deferred_inline_function */
2797 /* The DWARF 2 backend tries to reduce debugging bloat by not
2798 emitting the abstract description of inline functions until
2799 something tries to reference them. */
2800 dwarf2out_abstract_function, /* outlining_inline_function */
2801 debug_nothing_rtx_code_label, /* label */
2802 debug_nothing_int, /* handle_pch */
2803 dwarf2out_var_location,
2804 dwarf2out_inline_entry, /* inline_entry */
2805 dwarf2out_size_function, /* size_function */
2806 dwarf2out_switch_text_section,
2807 dwarf2out_set_name,
2808 1, /* start_end_main_source_file */
2809 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2812 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2814 dwarf2out_init,
2815 debug_nothing_charstar,
2816 debug_nothing_charstar,
2817 dwarf2out_assembly_start,
2818 debug_nothing_int_charstar,
2819 debug_nothing_int_charstar,
2820 debug_nothing_int_charstar,
2821 debug_nothing_int,
2822 debug_nothing_int_int, /* begin_block */
2823 debug_nothing_int_int, /* end_block */
2824 debug_true_const_tree, /* ignore_block */
2825 dwarf2out_source_line, /* source_line */
2826 debug_nothing_int_int_charstar, /* begin_prologue */
2827 debug_nothing_int_charstar, /* end_prologue */
2828 debug_nothing_int_charstar, /* begin_epilogue */
2829 debug_nothing_int_charstar, /* end_epilogue */
2830 debug_nothing_tree, /* begin_function */
2831 debug_nothing_int, /* end_function */
2832 debug_nothing_tree, /* register_main_translation_unit */
2833 debug_nothing_tree, /* function_decl */
2834 debug_nothing_tree, /* early_global_decl */
2835 debug_nothing_tree, /* late_global_decl */
2836 debug_nothing_tree_int, /* type_decl */
2837 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2838 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2839 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2840 debug_nothing_tree, /* deferred_inline_function */
2841 debug_nothing_tree, /* outlining_inline_function */
2842 debug_nothing_rtx_code_label, /* label */
2843 debug_nothing_int, /* handle_pch */
2844 debug_nothing_rtx_insn, /* var_location */
2845 debug_nothing_tree, /* inline_entry */
2846 debug_nothing_tree, /* size_function */
2847 debug_nothing_void, /* switch_text_section */
2848 debug_nothing_tree_tree, /* set_name */
2849 0, /* start_end_main_source_file */
2850 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2853 /* NOTE: In the comments in this file, many references are made to
2854 "Debugging Information Entries". This term is abbreviated as `DIE'
2855 throughout the remainder of this file. */
2857 /* An internal representation of the DWARF output is built, and then
2858 walked to generate the DWARF debugging info. The walk of the internal
2859 representation is done after the entire program has been compiled.
2860 The types below are used to describe the internal representation. */
2862 /* Whether to put type DIEs into their own section .debug_types instead
2863 of making them part of the .debug_info section. Only supported for
2864 Dwarf V4 or higher and the user didn't disable them through
2865 -fno-debug-types-section. It is more efficient to put them in a
2866 separate comdat sections since the linker will then be able to
2867 remove duplicates. But not all tools support .debug_types sections
2868 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2869 it is DW_UT_type unit type in .debug_info section. */
2871 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2873 /* Various DIE's use offsets relative to the beginning of the
2874 .debug_info section to refer to each other. */
2876 typedef long int dw_offset;
2878 struct comdat_type_node;
2880 /* The entries in the line_info table more-or-less mirror the opcodes
2881 that are used in the real dwarf line table. Arrays of these entries
2882 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2883 supported. */
2885 enum dw_line_info_opcode {
2886 /* Emit DW_LNE_set_address; the operand is the label index. */
2887 LI_set_address,
2889 /* Emit a row to the matrix with the given line. This may be done
2890 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2891 special opcodes. */
2892 LI_set_line,
2894 /* Emit a DW_LNS_set_file. */
2895 LI_set_file,
2897 /* Emit a DW_LNS_set_column. */
2898 LI_set_column,
2900 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2901 LI_negate_stmt,
2903 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2904 LI_set_prologue_end,
2905 LI_set_epilogue_begin,
2907 /* Emit a DW_LNE_set_discriminator. */
2908 LI_set_discriminator,
2910 /* Output a Fixed Advance PC; the target PC is the label index; the
2911 base PC is the previous LI_adv_address or LI_set_address entry.
2912 We only use this when emitting debug views without assembler
2913 support, at explicit user request. Ideally, we should only use
2914 it when the offset might be zero but we can't tell: it's the only
2915 way to maybe change the PC without resetting the view number. */
2916 LI_adv_address
2919 typedef struct GTY(()) dw_line_info_struct {
2920 enum dw_line_info_opcode opcode;
2921 unsigned int val;
2922 } dw_line_info_entry;
2925 struct GTY(()) dw_line_info_table {
2926 /* The label that marks the end of this section. */
2927 const char *end_label;
2929 /* The values for the last row of the matrix, as collected in the table.
2930 These are used to minimize the changes to the next row. */
2931 unsigned int file_num;
2932 unsigned int line_num;
2933 unsigned int column_num;
2934 int discrim_num;
2935 bool is_stmt;
2936 bool in_use;
2938 /* This denotes the NEXT view number.
2940 If it is 0, it is known that the NEXT view will be the first view
2941 at the given PC.
2943 If it is -1, we've advanced PC but we haven't emitted a line location yet,
2944 so we shouldn't use this view number.
2946 The meaning of other nonzero values depends on whether we're
2947 computing views internally or leaving it for the assembler to do
2948 so. If we're emitting them internally, view denotes the view
2949 number since the last known advance of PC. If we're leaving it
2950 for the assembler, it denotes the LVU label number that we're
2951 going to ask the assembler to assign. */
2952 var_loc_view view;
2954 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
2955 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0)
2957 vec<dw_line_info_entry, va_gc> *entries;
2961 /* Each DIE attribute has a field specifying the attribute kind,
2962 a link to the next attribute in the chain, and an attribute value.
2963 Attributes are typically linked below the DIE they modify. */
2965 typedef struct GTY(()) dw_attr_struct {
2966 enum dwarf_attribute dw_attr;
2967 dw_val_node dw_attr_val;
2969 dw_attr_node;
2972 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2973 The children of each node form a circular list linked by
2974 die_sib. die_child points to the node *before* the "first" child node. */
2976 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2977 union die_symbol_or_type_node
2979 const char * GTY ((tag ("0"))) die_symbol;
2980 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2982 GTY ((desc ("%0.comdat_type_p"))) die_id;
2983 vec<dw_attr_node, va_gc> *die_attr;
2984 dw_die_ref die_parent;
2985 dw_die_ref die_child;
2986 dw_die_ref die_sib;
2987 dw_die_ref die_definition; /* ref from a specification to its definition */
2988 dw_offset die_offset;
2989 unsigned long die_abbrev;
2990 int die_mark;
2991 unsigned int decl_id;
2992 enum dwarf_tag die_tag;
2993 /* Die is used and must not be pruned as unused. */
2994 BOOL_BITFIELD die_perennial_p : 1;
2995 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2996 /* For an external ref to die_symbol if die_offset contains an extra
2997 offset to that symbol. */
2998 BOOL_BITFIELD with_offset : 1;
2999 /* Whether this DIE was removed from the DIE tree, for example via
3000 prune_unused_types. We don't consider those present from the
3001 DIE lookup routines. */
3002 BOOL_BITFIELD removed : 1;
3003 /* Lots of spare bits. */
3005 die_node;
3007 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3008 static bool early_dwarf;
3009 static bool early_dwarf_finished;
3010 struct set_early_dwarf {
3011 bool saved;
3012 set_early_dwarf () : saved(early_dwarf)
3014 gcc_assert (! early_dwarf_finished);
3015 early_dwarf = true;
3017 ~set_early_dwarf () { early_dwarf = saved; }
3020 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3021 #define FOR_EACH_CHILD(die, c, expr) do { \
3022 c = die->die_child; \
3023 if (c) do { \
3024 c = c->die_sib; \
3025 expr; \
3026 } while (c != die->die_child); \
3027 } while (0)
3029 /* The pubname structure */
3031 typedef struct GTY(()) pubname_struct {
3032 dw_die_ref die;
3033 const char *name;
3035 pubname_entry;
3038 struct GTY(()) dw_ranges {
3039 const char *label;
3040 /* If this is positive, it's a block number, otherwise it's a
3041 bitwise-negated index into dw_ranges_by_label. */
3042 int num;
3043 /* Index for the range list for DW_FORM_rnglistx. */
3044 unsigned int idx : 31;
3045 /* True if this range might be possibly in a different section
3046 from previous entry. */
3047 unsigned int maybe_new_sec : 1;
3050 /* A structure to hold a macinfo entry. */
3052 typedef struct GTY(()) macinfo_struct {
3053 unsigned char code;
3054 unsigned HOST_WIDE_INT lineno;
3055 const char *info;
3057 macinfo_entry;
3060 struct GTY(()) dw_ranges_by_label {
3061 const char *begin;
3062 const char *end;
3065 /* The comdat type node structure. */
3066 struct GTY(()) comdat_type_node
3068 dw_die_ref root_die;
3069 dw_die_ref type_die;
3070 dw_die_ref skeleton_die;
3071 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3072 comdat_type_node *next;
3075 /* A list of DIEs for which we can't determine ancestry (parent_die
3076 field) just yet. Later in dwarf2out_finish we will fill in the
3077 missing bits. */
3078 typedef struct GTY(()) limbo_die_struct {
3079 dw_die_ref die;
3080 /* The tree for which this DIE was created. We use this to
3081 determine ancestry later. */
3082 tree created_for;
3083 struct limbo_die_struct *next;
3085 limbo_die_node;
3087 typedef struct skeleton_chain_struct
3089 dw_die_ref old_die;
3090 dw_die_ref new_die;
3091 struct skeleton_chain_struct *parent;
3093 skeleton_chain_node;
3095 /* Define a macro which returns nonzero for a TYPE_DECL which was
3096 implicitly generated for a type.
3098 Note that, unlike the C front-end (which generates a NULL named
3099 TYPE_DECL node for each complete tagged type, each array type,
3100 and each function type node created) the C++ front-end generates
3101 a _named_ TYPE_DECL node for each tagged type node created.
3102 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3103 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3104 front-end, but for each type, tagged or not. */
3106 #define TYPE_DECL_IS_STUB(decl) \
3107 (DECL_NAME (decl) == NULL_TREE \
3108 || (DECL_ARTIFICIAL (decl) \
3109 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3110 /* This is necessary for stub decls that \
3111 appear in nested inline functions. */ \
3112 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3113 && (decl_ultimate_origin (decl) \
3114 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3116 /* Information concerning the compilation unit's programming
3117 language, and compiler version. */
3119 /* Fixed size portion of the DWARF compilation unit header. */
3120 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3121 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3122 + (dwarf_version >= 5 ? 4 : 3))
3124 /* Fixed size portion of the DWARF comdat type unit header. */
3125 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3126 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3127 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3129 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3130 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3131 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3133 /* Fixed size portion of public names info. */
3134 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3136 /* Fixed size portion of the address range info. */
3137 #define DWARF_ARANGES_HEADER_SIZE \
3138 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3139 DWARF2_ADDR_SIZE * 2) \
3140 - DWARF_INITIAL_LENGTH_SIZE)
3142 /* Size of padding portion in the address range info. It must be
3143 aligned to twice the pointer size. */
3144 #define DWARF_ARANGES_PAD_SIZE \
3145 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3146 DWARF2_ADDR_SIZE * 2) \
3147 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3149 /* Use assembler line directives if available. */
3150 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3151 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3152 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3153 #else
3154 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3155 #endif
3156 #endif
3158 /* Use assembler views in line directives if available. */
3159 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3160 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3161 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3162 #else
3163 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3164 #endif
3165 #endif
3167 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3168 view computation, and it refers to a view identifier for which we
3169 will not emit a label because it is known to map to a view number
3170 zero. We won't allocate the bitmap if we're not using assembler
3171 support for location views, but we have to make the variable
3172 visible for GGC and for code that will be optimized out for lack of
3173 support but that's still parsed and compiled. We could abstract it
3174 out with macros, but it's not worth it. */
3175 static GTY(()) bitmap zero_view_p;
3177 /* Evaluate to TRUE iff N is known to identify the first location view
3178 at its PC. When not using assembler location view computation,
3179 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3180 and views label numbers recorded in it are the ones known to be
3181 zero. */
3182 #define ZERO_VIEW_P(N) (zero_view_p \
3183 ? bitmap_bit_p (zero_view_p, (N)) \
3184 : (N) == 0)
3186 /* Return true iff we're to emit .loc directives for the assembler to
3187 generate line number sections.
3189 When we're not emitting views, all we need from the assembler is
3190 support for .loc directives.
3192 If we are emitting views, we can only use the assembler's .loc
3193 support if it also supports views.
3195 When the compiler is emitting the line number programs and
3196 computing view numbers itself, it resets view numbers at known PC
3197 changes and counts from that, and then it emits view numbers as
3198 literal constants in locviewlists. There are cases in which the
3199 compiler is not sure about PC changes, e.g. when extra alignment is
3200 requested for a label. In these cases, the compiler may not reset
3201 the view counter, and the potential PC advance in the line number
3202 program will use an opcode that does not reset the view counter
3203 even if the PC actually changes, so that compiler and debug info
3204 consumer can keep view numbers in sync.
3206 When the compiler defers view computation to the assembler, it
3207 emits symbolic view numbers in locviewlists, with the exception of
3208 views known to be zero (forced resets, or reset after
3209 compiler-visible PC changes): instead of emitting symbols for
3210 these, we emit literal zero and assert the assembler agrees with
3211 the compiler's assessment. We could use symbolic views everywhere,
3212 instead of special-casing zero views, but then we'd be unable to
3213 optimize out locviewlists that contain only zeros. */
3215 static bool
3216 output_asm_line_debug_info (void)
3218 return (DWARF2_ASM_VIEW_DEBUG_INFO
3219 || (DWARF2_ASM_LINE_DEBUG_INFO
3220 && !debug_variable_location_views));
3223 /* Minimum line offset in a special line info. opcode.
3224 This value was chosen to give a reasonable range of values. */
3225 #define DWARF_LINE_BASE -10
3227 /* First special line opcode - leave room for the standard opcodes. */
3228 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3230 /* Range of line offsets in a special line info. opcode. */
3231 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3233 /* Flag that indicates the initial value of the is_stmt_start flag.
3234 In the present implementation, we do not mark any lines as
3235 the beginning of a source statement, because that information
3236 is not made available by the GCC front-end. */
3237 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3239 /* Maximum number of operations per instruction bundle. */
3240 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3241 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3242 #endif
3244 /* This location is used by calc_die_sizes() to keep track
3245 the offset of each DIE within the .debug_info section. */
3246 static unsigned long next_die_offset;
3248 /* Record the root of the DIE's built for the current compilation unit. */
3249 static GTY(()) dw_die_ref single_comp_unit_die;
3251 /* A list of type DIEs that have been separated into comdat sections. */
3252 static GTY(()) comdat_type_node *comdat_type_list;
3254 /* A list of CU DIEs that have been separated. */
3255 static GTY(()) limbo_die_node *cu_die_list;
3257 /* A list of DIEs with a NULL parent waiting to be relocated. */
3258 static GTY(()) limbo_die_node *limbo_die_list;
3260 /* A list of DIEs for which we may have to generate
3261 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3262 static GTY(()) limbo_die_node *deferred_asm_name;
3264 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3266 typedef const char *compare_type;
3268 static hashval_t hash (dwarf_file_data *);
3269 static bool equal (dwarf_file_data *, const char *);
3272 /* Filenames referenced by this compilation unit. */
3273 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3275 struct decl_die_hasher : ggc_ptr_hash<die_node>
3277 typedef tree compare_type;
3279 static hashval_t hash (die_node *);
3280 static bool equal (die_node *, tree);
3282 /* A hash table of references to DIE's that describe declarations.
3283 The key is a DECL_UID() which is a unique number identifying each decl. */
3284 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3286 struct GTY ((for_user)) variable_value_struct {
3287 unsigned int decl_id;
3288 vec<dw_die_ref, va_gc> *dies;
3291 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3293 typedef tree compare_type;
3295 static hashval_t hash (variable_value_struct *);
3296 static bool equal (variable_value_struct *, tree);
3298 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3299 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3300 DECL_CONTEXT of the referenced VAR_DECLs. */
3301 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3303 struct block_die_hasher : ggc_ptr_hash<die_struct>
3305 static hashval_t hash (die_struct *);
3306 static bool equal (die_struct *, die_struct *);
3309 /* A hash table of references to DIE's that describe COMMON blocks.
3310 The key is DECL_UID() ^ die_parent. */
3311 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3313 typedef struct GTY(()) die_arg_entry_struct {
3314 dw_die_ref die;
3315 tree arg;
3316 } die_arg_entry;
3319 /* Node of the variable location list. */
3320 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3321 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3322 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3323 in mode of the EXPR_LIST node and first EXPR_LIST operand
3324 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3325 location or NULL for padding. For larger bitsizes,
3326 mode is 0 and first operand is a CONCAT with bitsize
3327 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3328 NULL as second operand. */
3329 rtx GTY (()) loc;
3330 const char * GTY (()) label;
3331 struct var_loc_node * GTY (()) next;
3332 var_loc_view view;
3335 /* Variable location list. */
3336 struct GTY ((for_user)) var_loc_list_def {
3337 struct var_loc_node * GTY (()) first;
3339 /* Pointer to the last but one or last element of the
3340 chained list. If the list is empty, both first and
3341 last are NULL, if the list contains just one node
3342 or the last node certainly is not redundant, it points
3343 to the last node, otherwise points to the last but one.
3344 Do not mark it for GC because it is marked through the chain. */
3345 struct var_loc_node * GTY ((skip ("%h"))) last;
3347 /* Pointer to the last element before section switch,
3348 if NULL, either sections weren't switched or first
3349 is after section switch. */
3350 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3352 /* DECL_UID of the variable decl. */
3353 unsigned int decl_id;
3355 typedef struct var_loc_list_def var_loc_list;
3357 /* Call argument location list. */
3358 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3359 rtx GTY (()) call_arg_loc_note;
3360 const char * GTY (()) label;
3361 tree GTY (()) block;
3362 bool tail_call_p;
3363 rtx GTY (()) symbol_ref;
3364 struct call_arg_loc_node * GTY (()) next;
3368 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3370 typedef const_tree compare_type;
3372 static hashval_t hash (var_loc_list *);
3373 static bool equal (var_loc_list *, const_tree);
3376 /* Table of decl location linked lists. */
3377 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3379 /* Head and tail of call_arg_loc chain. */
3380 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3381 static struct call_arg_loc_node *call_arg_loc_last;
3383 /* Number of call sites in the current function. */
3384 static int call_site_count = -1;
3385 /* Number of tail call sites in the current function. */
3386 static int tail_call_site_count = -1;
3388 /* A cached location list. */
3389 struct GTY ((for_user)) cached_dw_loc_list_def {
3390 /* The DECL_UID of the decl that this entry describes. */
3391 unsigned int decl_id;
3393 /* The cached location list. */
3394 dw_loc_list_ref loc_list;
3396 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3398 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3401 typedef const_tree compare_type;
3403 static hashval_t hash (cached_dw_loc_list *);
3404 static bool equal (cached_dw_loc_list *, const_tree);
3407 /* Table of cached location lists. */
3408 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3410 /* A vector of references to DIE's that are uniquely identified by their tag,
3411 presence/absence of children DIE's, and list of attribute/value pairs. */
3412 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3414 /* A hash map to remember the stack usage for DWARF procedures. The value
3415 stored is the stack size difference between before the DWARF procedure
3416 invokation and after it returned. In other words, for a DWARF procedure
3417 that consumes N stack slots and that pushes M ones, this stores M - N. */
3418 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3420 /* A global counter for generating labels for line number data. */
3421 static unsigned int line_info_label_num;
3423 /* The current table to which we should emit line number information
3424 for the current function. This will be set up at the beginning of
3425 assembly for the function. */
3426 static GTY(()) dw_line_info_table *cur_line_info_table;
3428 /* The two default tables of line number info. */
3429 static GTY(()) dw_line_info_table *text_section_line_info;
3430 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3432 /* The set of all non-default tables of line number info. */
3433 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3435 /* A flag to tell pubnames/types export if there is an info section to
3436 refer to. */
3437 static bool info_section_emitted;
3439 /* A pointer to the base of a table that contains a list of publicly
3440 accessible names. */
3441 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3443 /* A pointer to the base of a table that contains a list of publicly
3444 accessible types. */
3445 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3447 /* A pointer to the base of a table that contains a list of macro
3448 defines/undefines (and file start/end markers). */
3449 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3451 /* True if .debug_macinfo or .debug_macros section is going to be
3452 emitted. */
3453 #define have_macinfo \
3454 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3455 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3456 && !macinfo_table->is_empty ())
3458 /* Vector of dies for which we should generate .debug_ranges info. */
3459 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3461 /* Vector of pairs of labels referenced in ranges_table. */
3462 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3464 /* Whether we have location lists that need outputting */
3465 static GTY(()) bool have_location_lists;
3467 /* Unique label counter. */
3468 static GTY(()) unsigned int loclabel_num;
3470 /* Unique label counter for point-of-call tables. */
3471 static GTY(()) unsigned int poc_label_num;
3473 /* The last file entry emitted by maybe_emit_file(). */
3474 static GTY(()) struct dwarf_file_data * last_emitted_file;
3476 /* Number of internal labels generated by gen_internal_sym(). */
3477 static GTY(()) int label_num;
3479 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3481 /* Instances of generic types for which we need to generate debug
3482 info that describe their generic parameters and arguments. That
3483 generation needs to happen once all types are properly laid out so
3484 we do it at the end of compilation. */
3485 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3487 /* Offset from the "steady-state frame pointer" to the frame base,
3488 within the current function. */
3489 static poly_int64 frame_pointer_fb_offset;
3490 static bool frame_pointer_fb_offset_valid;
3492 static vec<dw_die_ref> base_types;
3494 /* Flags to represent a set of attribute classes for attributes that represent
3495 a scalar value (bounds, pointers, ...). */
3496 enum dw_scalar_form
3498 dw_scalar_form_constant = 0x01,
3499 dw_scalar_form_exprloc = 0x02,
3500 dw_scalar_form_reference = 0x04
3503 /* Forward declarations for functions defined in this file. */
3505 static int is_pseudo_reg (const_rtx);
3506 static tree type_main_variant (tree);
3507 static int is_tagged_type (const_tree);
3508 static const char *dwarf_tag_name (unsigned);
3509 static const char *dwarf_attr_name (unsigned);
3510 static const char *dwarf_form_name (unsigned);
3511 static tree decl_ultimate_origin (const_tree);
3512 static tree decl_class_context (tree);
3513 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3514 static inline enum dw_val_class AT_class (dw_attr_node *);
3515 static inline unsigned int AT_index (dw_attr_node *);
3516 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3517 static inline unsigned AT_flag (dw_attr_node *);
3518 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3519 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3520 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3521 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3522 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3523 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3524 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3525 unsigned int, unsigned char *);
3526 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3527 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3528 static inline const char *AT_string (dw_attr_node *);
3529 static enum dwarf_form AT_string_form (dw_attr_node *);
3530 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3531 static void add_AT_specification (dw_die_ref, dw_die_ref);
3532 static inline dw_die_ref AT_ref (dw_attr_node *);
3533 static inline int AT_ref_external (dw_attr_node *);
3534 static inline void set_AT_ref_external (dw_attr_node *, int);
3535 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3536 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3537 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3538 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3539 dw_loc_list_ref);
3540 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3541 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3542 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3543 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3544 static void remove_addr_table_entry (addr_table_entry *);
3545 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3546 static inline rtx AT_addr (dw_attr_node *);
3547 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3548 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3549 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3550 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3551 const char *);
3552 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3553 unsigned HOST_WIDE_INT);
3554 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3555 unsigned long, bool);
3556 static inline const char *AT_lbl (dw_attr_node *);
3557 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3558 static const char *get_AT_low_pc (dw_die_ref);
3559 static const char *get_AT_hi_pc (dw_die_ref);
3560 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3561 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3562 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3563 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3564 static bool is_cxx (void);
3565 static bool is_cxx (const_tree);
3566 static bool is_fortran (void);
3567 static bool is_ada (void);
3568 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3569 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3570 static void add_child_die (dw_die_ref, dw_die_ref);
3571 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3572 static dw_die_ref lookup_type_die (tree);
3573 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3574 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3575 static void equate_type_number_to_die (tree, dw_die_ref);
3576 static dw_die_ref lookup_decl_die (tree);
3577 static var_loc_list *lookup_decl_loc (const_tree);
3578 static void equate_decl_number_to_die (tree, dw_die_ref);
3579 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3580 static void print_spaces (FILE *);
3581 static void print_die (dw_die_ref, FILE *);
3582 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3583 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3584 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3585 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3586 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3587 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3588 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3589 struct md5_ctx *, int *);
3590 struct checksum_attributes;
3591 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3592 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3593 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3594 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3595 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3596 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3597 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3598 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3599 static int is_type_die (dw_die_ref);
3600 static int is_comdat_die (dw_die_ref);
3601 static inline bool is_template_instantiation (dw_die_ref);
3602 static int is_declaration_die (dw_die_ref);
3603 static int should_move_die_to_comdat (dw_die_ref);
3604 static dw_die_ref clone_as_declaration (dw_die_ref);
3605 static dw_die_ref clone_die (dw_die_ref);
3606 static dw_die_ref clone_tree (dw_die_ref);
3607 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3608 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3609 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3610 static dw_die_ref generate_skeleton (dw_die_ref);
3611 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3612 dw_die_ref,
3613 dw_die_ref);
3614 static void break_out_comdat_types (dw_die_ref);
3615 static void copy_decls_for_unworthy_types (dw_die_ref);
3617 static void add_sibling_attributes (dw_die_ref);
3618 static void output_location_lists (dw_die_ref);
3619 static int constant_size (unsigned HOST_WIDE_INT);
3620 static unsigned long size_of_die (dw_die_ref);
3621 static void calc_die_sizes (dw_die_ref);
3622 static void calc_base_type_die_sizes (void);
3623 static void mark_dies (dw_die_ref);
3624 static void unmark_dies (dw_die_ref);
3625 static void unmark_all_dies (dw_die_ref);
3626 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3627 static unsigned long size_of_aranges (void);
3628 static enum dwarf_form value_format (dw_attr_node *);
3629 static void output_value_format (dw_attr_node *);
3630 static void output_abbrev_section (void);
3631 static void output_die_abbrevs (unsigned long, dw_die_ref);
3632 static void output_die (dw_die_ref);
3633 static void output_compilation_unit_header (enum dwarf_unit_type);
3634 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3635 static void output_comdat_type_unit (comdat_type_node *);
3636 static const char *dwarf2_name (tree, int);
3637 static void add_pubname (tree, dw_die_ref);
3638 static void add_enumerator_pubname (const char *, dw_die_ref);
3639 static void add_pubname_string (const char *, dw_die_ref);
3640 static void add_pubtype (tree, dw_die_ref);
3641 static void output_pubnames (vec<pubname_entry, va_gc> *);
3642 static void output_aranges (void);
3643 static unsigned int add_ranges (const_tree, bool = false);
3644 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3645 bool *, bool);
3646 static void output_ranges (void);
3647 static dw_line_info_table *new_line_info_table (void);
3648 static void output_line_info (bool);
3649 static void output_file_names (void);
3650 static dw_die_ref base_type_die (tree, bool);
3651 static int is_base_type (tree);
3652 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3653 static int decl_quals (const_tree);
3654 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3655 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3656 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3657 static int type_is_enum (const_tree);
3658 static unsigned int dbx_reg_number (const_rtx);
3659 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3660 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3661 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3662 enum var_init_status);
3663 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3664 enum var_init_status);
3665 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3666 enum var_init_status);
3667 static int is_based_loc (const_rtx);
3668 static bool resolve_one_addr (rtx *);
3669 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3670 enum var_init_status);
3671 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3672 enum var_init_status);
3673 struct loc_descr_context;
3674 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3675 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3676 static dw_loc_list_ref loc_list_from_tree (tree, int,
3677 struct loc_descr_context *);
3678 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3679 struct loc_descr_context *);
3680 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3681 static tree field_type (const_tree);
3682 static unsigned int simple_type_align_in_bits (const_tree);
3683 static unsigned int simple_decl_align_in_bits (const_tree);
3684 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3685 struct vlr_context;
3686 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3687 HOST_WIDE_INT *);
3688 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3689 dw_loc_list_ref);
3690 static void add_data_member_location_attribute (dw_die_ref, tree,
3691 struct vlr_context *);
3692 static bool add_const_value_attribute (dw_die_ref, rtx);
3693 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3694 static void insert_wide_int (const wide_int &, unsigned char *, int);
3695 static void insert_float (const_rtx, unsigned char *);
3696 static rtx rtl_for_decl_location (tree);
3697 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3698 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3699 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3700 static void add_name_attribute (dw_die_ref, const char *);
3701 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3702 static void add_comp_dir_attribute (dw_die_ref);
3703 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3704 struct loc_descr_context *);
3705 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3706 struct loc_descr_context *);
3707 static void add_subscript_info (dw_die_ref, tree, bool);
3708 static void add_byte_size_attribute (dw_die_ref, tree);
3709 static void add_alignment_attribute (dw_die_ref, tree);
3710 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3711 struct vlr_context *);
3712 static void add_bit_size_attribute (dw_die_ref, tree);
3713 static void add_prototyped_attribute (dw_die_ref, tree);
3714 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3715 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3716 static void add_src_coords_attributes (dw_die_ref, tree);
3717 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3718 static void add_discr_value (dw_die_ref, dw_discr_value *);
3719 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3720 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3721 static void push_decl_scope (tree);
3722 static void pop_decl_scope (void);
3723 static dw_die_ref scope_die_for (tree, dw_die_ref);
3724 static inline int local_scope_p (dw_die_ref);
3725 static inline int class_scope_p (dw_die_ref);
3726 static inline int class_or_namespace_scope_p (dw_die_ref);
3727 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3728 static void add_calling_convention_attribute (dw_die_ref, tree);
3729 static const char *type_tag (const_tree);
3730 static tree member_declared_type (const_tree);
3731 #if 0
3732 static const char *decl_start_label (tree);
3733 #endif
3734 static void gen_array_type_die (tree, dw_die_ref);
3735 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3736 #if 0
3737 static void gen_entry_point_die (tree, dw_die_ref);
3738 #endif
3739 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3740 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3741 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3742 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3743 static void gen_formal_types_die (tree, dw_die_ref);
3744 static void gen_subprogram_die (tree, dw_die_ref);
3745 static void gen_variable_die (tree, tree, dw_die_ref);
3746 static void gen_const_die (tree, dw_die_ref);
3747 static void gen_label_die (tree, dw_die_ref);
3748 static void gen_lexical_block_die (tree, dw_die_ref);
3749 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3750 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3751 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3752 static dw_die_ref gen_compile_unit_die (const char *);
3753 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3754 static void gen_member_die (tree, dw_die_ref);
3755 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3756 enum debug_info_usage);
3757 static void gen_subroutine_type_die (tree, dw_die_ref);
3758 static void gen_typedef_die (tree, dw_die_ref);
3759 static void gen_type_die (tree, dw_die_ref);
3760 static void gen_block_die (tree, dw_die_ref);
3761 static void decls_for_scope (tree, dw_die_ref);
3762 static bool is_naming_typedef_decl (const_tree);
3763 static inline dw_die_ref get_context_die (tree);
3764 static void gen_namespace_die (tree, dw_die_ref);
3765 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3766 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3767 static dw_die_ref force_decl_die (tree);
3768 static dw_die_ref force_type_die (tree);
3769 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3770 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3771 static struct dwarf_file_data * lookup_filename (const char *);
3772 static void retry_incomplete_types (void);
3773 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3774 static void gen_generic_params_dies (tree);
3775 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3776 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3777 static void splice_child_die (dw_die_ref, dw_die_ref);
3778 static int file_info_cmp (const void *, const void *);
3779 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3780 const char *, var_loc_view, const char *);
3781 static void output_loc_list (dw_loc_list_ref);
3782 static char *gen_internal_sym (const char *);
3783 static bool want_pubnames (void);
3785 static void prune_unmark_dies (dw_die_ref);
3786 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3787 static void prune_unused_types_mark (dw_die_ref, int);
3788 static void prune_unused_types_walk (dw_die_ref);
3789 static void prune_unused_types_walk_attribs (dw_die_ref);
3790 static void prune_unused_types_prune (dw_die_ref);
3791 static void prune_unused_types (void);
3792 static int maybe_emit_file (struct dwarf_file_data *fd);
3793 static inline const char *AT_vms_delta1 (dw_attr_node *);
3794 static inline const char *AT_vms_delta2 (dw_attr_node *);
3795 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3796 const char *, const char *);
3797 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3798 static void gen_remaining_tmpl_value_param_die_attribute (void);
3799 static bool generic_type_p (tree);
3800 static void schedule_generic_params_dies_gen (tree t);
3801 static void gen_scheduled_generic_parms_dies (void);
3802 static void resolve_variable_values (void);
3804 static const char *comp_dir_string (void);
3806 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3808 /* enum for tracking thread-local variables whose address is really an offset
3809 relative to the TLS pointer, which will need link-time relocation, but will
3810 not need relocation by the DWARF consumer. */
3812 enum dtprel_bool
3814 dtprel_false = 0,
3815 dtprel_true = 1
3818 /* Return the operator to use for an address of a variable. For dtprel_true, we
3819 use DW_OP_const*. For regular variables, which need both link-time
3820 relocation and consumer-level relocation (e.g., to account for shared objects
3821 loaded at a random address), we use DW_OP_addr*. */
3823 static inline enum dwarf_location_atom
3824 dw_addr_op (enum dtprel_bool dtprel)
3826 if (dtprel == dtprel_true)
3827 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3828 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3829 else
3830 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3833 /* Return a pointer to a newly allocated address location description. If
3834 dwarf_split_debug_info is true, then record the address with the appropriate
3835 relocation. */
3836 static inline dw_loc_descr_ref
3837 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3839 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3841 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3842 ref->dw_loc_oprnd1.v.val_addr = addr;
3843 ref->dtprel = dtprel;
3844 if (dwarf_split_debug_info)
3845 ref->dw_loc_oprnd1.val_entry
3846 = add_addr_table_entry (addr,
3847 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3848 else
3849 ref->dw_loc_oprnd1.val_entry = NULL;
3851 return ref;
3854 /* Section names used to hold DWARF debugging information. */
3856 #ifndef DEBUG_INFO_SECTION
3857 #define DEBUG_INFO_SECTION ".debug_info"
3858 #endif
3859 #ifndef DEBUG_DWO_INFO_SECTION
3860 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3861 #endif
3862 #ifndef DEBUG_LTO_INFO_SECTION
3863 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3864 #endif
3865 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3866 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3867 #endif
3868 #ifndef DEBUG_ABBREV_SECTION
3869 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3870 #endif
3871 #ifndef DEBUG_LTO_ABBREV_SECTION
3872 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3873 #endif
3874 #ifndef DEBUG_DWO_ABBREV_SECTION
3875 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3876 #endif
3877 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3878 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3879 #endif
3880 #ifndef DEBUG_ARANGES_SECTION
3881 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3882 #endif
3883 #ifndef DEBUG_ADDR_SECTION
3884 #define DEBUG_ADDR_SECTION ".debug_addr"
3885 #endif
3886 #ifndef DEBUG_MACINFO_SECTION
3887 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3888 #endif
3889 #ifndef DEBUG_LTO_MACINFO_SECTION
3890 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3891 #endif
3892 #ifndef DEBUG_DWO_MACINFO_SECTION
3893 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3894 #endif
3895 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3896 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3897 #endif
3898 #ifndef DEBUG_MACRO_SECTION
3899 #define DEBUG_MACRO_SECTION ".debug_macro"
3900 #endif
3901 #ifndef DEBUG_LTO_MACRO_SECTION
3902 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3903 #endif
3904 #ifndef DEBUG_DWO_MACRO_SECTION
3905 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3906 #endif
3907 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3908 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3909 #endif
3910 #ifndef DEBUG_LINE_SECTION
3911 #define DEBUG_LINE_SECTION ".debug_line"
3912 #endif
3913 #ifndef DEBUG_LTO_LINE_SECTION
3914 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3915 #endif
3916 #ifndef DEBUG_DWO_LINE_SECTION
3917 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3918 #endif
3919 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3920 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3921 #endif
3922 #ifndef DEBUG_LOC_SECTION
3923 #define DEBUG_LOC_SECTION ".debug_loc"
3924 #endif
3925 #ifndef DEBUG_DWO_LOC_SECTION
3926 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3927 #endif
3928 #ifndef DEBUG_LOCLISTS_SECTION
3929 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3930 #endif
3931 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3932 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3933 #endif
3934 #ifndef DEBUG_PUBNAMES_SECTION
3935 #define DEBUG_PUBNAMES_SECTION \
3936 ((debug_generate_pub_sections == 2) \
3937 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3938 #endif
3939 #ifndef DEBUG_PUBTYPES_SECTION
3940 #define DEBUG_PUBTYPES_SECTION \
3941 ((debug_generate_pub_sections == 2) \
3942 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3943 #endif
3944 #ifndef DEBUG_STR_OFFSETS_SECTION
3945 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3946 #endif
3947 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3948 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3949 #endif
3950 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3951 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3952 #endif
3953 #ifndef DEBUG_STR_SECTION
3954 #define DEBUG_STR_SECTION ".debug_str"
3955 #endif
3956 #ifndef DEBUG_LTO_STR_SECTION
3957 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3958 #endif
3959 #ifndef DEBUG_STR_DWO_SECTION
3960 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3961 #endif
3962 #ifndef DEBUG_LTO_STR_DWO_SECTION
3963 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3964 #endif
3965 #ifndef DEBUG_RANGES_SECTION
3966 #define DEBUG_RANGES_SECTION ".debug_ranges"
3967 #endif
3968 #ifndef DEBUG_RNGLISTS_SECTION
3969 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3970 #endif
3971 #ifndef DEBUG_LINE_STR_SECTION
3972 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3973 #endif
3974 #ifndef DEBUG_LTO_LINE_STR_SECTION
3975 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3976 #endif
3978 /* Standard ELF section names for compiled code and data. */
3979 #ifndef TEXT_SECTION_NAME
3980 #define TEXT_SECTION_NAME ".text"
3981 #endif
3983 /* Section flags for .debug_str section. */
3984 #define DEBUG_STR_SECTION_FLAGS \
3985 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3986 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3987 : SECTION_DEBUG)
3989 /* Section flags for .debug_str.dwo section. */
3990 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3992 /* Attribute used to refer to the macro section. */
3993 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3994 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3996 /* Labels we insert at beginning sections we can reference instead of
3997 the section names themselves. */
3999 #ifndef TEXT_SECTION_LABEL
4000 #define TEXT_SECTION_LABEL "Ltext"
4001 #endif
4002 #ifndef COLD_TEXT_SECTION_LABEL
4003 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4004 #endif
4005 #ifndef DEBUG_LINE_SECTION_LABEL
4006 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4007 #endif
4008 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4009 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4010 #endif
4011 #ifndef DEBUG_INFO_SECTION_LABEL
4012 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4013 #endif
4014 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4015 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4016 #endif
4017 #ifndef DEBUG_ABBREV_SECTION_LABEL
4018 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4019 #endif
4020 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4021 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4022 #endif
4023 #ifndef DEBUG_ADDR_SECTION_LABEL
4024 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4025 #endif
4026 #ifndef DEBUG_LOC_SECTION_LABEL
4027 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4028 #endif
4029 #ifndef DEBUG_RANGES_SECTION_LABEL
4030 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4031 #endif
4032 #ifndef DEBUG_MACINFO_SECTION_LABEL
4033 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4034 #endif
4035 #ifndef DEBUG_MACRO_SECTION_LABEL
4036 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4037 #endif
4038 #define SKELETON_COMP_DIE_ABBREV 1
4039 #define SKELETON_TYPE_DIE_ABBREV 2
4041 /* Definitions of defaults for formats and names of various special
4042 (artificial) labels which may be generated within this file (when the -g
4043 options is used and DWARF2_DEBUGGING_INFO is in effect.
4044 If necessary, these may be overridden from within the tm.h file, but
4045 typically, overriding these defaults is unnecessary. */
4047 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4048 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4049 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4050 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4051 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4052 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4053 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4054 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4055 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4056 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4057 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4058 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4059 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4060 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4061 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4063 #ifndef TEXT_END_LABEL
4064 #define TEXT_END_LABEL "Letext"
4065 #endif
4066 #ifndef COLD_END_LABEL
4067 #define COLD_END_LABEL "Letext_cold"
4068 #endif
4069 #ifndef BLOCK_BEGIN_LABEL
4070 #define BLOCK_BEGIN_LABEL "LBB"
4071 #endif
4072 #ifndef BLOCK_INLINE_ENTRY_LABEL
4073 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4074 #endif
4075 #ifndef BLOCK_END_LABEL
4076 #define BLOCK_END_LABEL "LBE"
4077 #endif
4078 #ifndef LINE_CODE_LABEL
4079 #define LINE_CODE_LABEL "LM"
4080 #endif
4083 /* Return the root of the DIE's built for the current compilation unit. */
4084 static dw_die_ref
4085 comp_unit_die (void)
4087 if (!single_comp_unit_die)
4088 single_comp_unit_die = gen_compile_unit_die (NULL);
4089 return single_comp_unit_die;
4092 /* We allow a language front-end to designate a function that is to be
4093 called to "demangle" any name before it is put into a DIE. */
4095 static const char *(*demangle_name_func) (const char *);
4097 void
4098 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4100 demangle_name_func = func;
4103 /* Test if rtl node points to a pseudo register. */
4105 static inline int
4106 is_pseudo_reg (const_rtx rtl)
4108 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4109 || (GET_CODE (rtl) == SUBREG
4110 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4113 /* Return a reference to a type, with its const and volatile qualifiers
4114 removed. */
4116 static inline tree
4117 type_main_variant (tree type)
4119 type = TYPE_MAIN_VARIANT (type);
4121 /* ??? There really should be only one main variant among any group of
4122 variants of a given type (and all of the MAIN_VARIANT values for all
4123 members of the group should point to that one type) but sometimes the C
4124 front-end messes this up for array types, so we work around that bug
4125 here. */
4126 if (TREE_CODE (type) == ARRAY_TYPE)
4127 while (type != TYPE_MAIN_VARIANT (type))
4128 type = TYPE_MAIN_VARIANT (type);
4130 return type;
4133 /* Return nonzero if the given type node represents a tagged type. */
4135 static inline int
4136 is_tagged_type (const_tree type)
4138 enum tree_code code = TREE_CODE (type);
4140 return (code == RECORD_TYPE || code == UNION_TYPE
4141 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4144 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4146 static void
4147 get_ref_die_offset_label (char *label, dw_die_ref ref)
4149 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4152 /* Return die_offset of a DIE reference to a base type. */
4154 static unsigned long int
4155 get_base_type_offset (dw_die_ref ref)
4157 if (ref->die_offset)
4158 return ref->die_offset;
4159 if (comp_unit_die ()->die_abbrev)
4161 calc_base_type_die_sizes ();
4162 gcc_assert (ref->die_offset);
4164 return ref->die_offset;
4167 /* Return die_offset of a DIE reference other than base type. */
4169 static unsigned long int
4170 get_ref_die_offset (dw_die_ref ref)
4172 gcc_assert (ref->die_offset);
4173 return ref->die_offset;
4176 /* Convert a DIE tag into its string name. */
4178 static const char *
4179 dwarf_tag_name (unsigned int tag)
4181 const char *name = get_DW_TAG_name (tag);
4183 if (name != NULL)
4184 return name;
4186 return "DW_TAG_<unknown>";
4189 /* Convert a DWARF attribute code into its string name. */
4191 static const char *
4192 dwarf_attr_name (unsigned int attr)
4194 const char *name;
4196 switch (attr)
4198 #if VMS_DEBUGGING_INFO
4199 case DW_AT_HP_prologue:
4200 return "DW_AT_HP_prologue";
4201 #else
4202 case DW_AT_MIPS_loop_unroll_factor:
4203 return "DW_AT_MIPS_loop_unroll_factor";
4204 #endif
4206 #if VMS_DEBUGGING_INFO
4207 case DW_AT_HP_epilogue:
4208 return "DW_AT_HP_epilogue";
4209 #else
4210 case DW_AT_MIPS_stride:
4211 return "DW_AT_MIPS_stride";
4212 #endif
4215 name = get_DW_AT_name (attr);
4217 if (name != NULL)
4218 return name;
4220 return "DW_AT_<unknown>";
4223 /* Convert a DWARF value form code into its string name. */
4225 static const char *
4226 dwarf_form_name (unsigned int form)
4228 const char *name = get_DW_FORM_name (form);
4230 if (name != NULL)
4231 return name;
4233 return "DW_FORM_<unknown>";
4236 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4237 instance of an inlined instance of a decl which is local to an inline
4238 function, so we have to trace all of the way back through the origin chain
4239 to find out what sort of node actually served as the original seed for the
4240 given block. */
4242 static tree
4243 decl_ultimate_origin (const_tree decl)
4245 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4246 return NULL_TREE;
4248 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4249 we're trying to output the abstract instance of this function. */
4250 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4251 return NULL_TREE;
4253 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4254 most distant ancestor, this should never happen. */
4255 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4257 return DECL_ABSTRACT_ORIGIN (decl);
4260 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4261 of a virtual function may refer to a base class, so we check the 'this'
4262 parameter. */
4264 static tree
4265 decl_class_context (tree decl)
4267 tree context = NULL_TREE;
4269 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4270 context = DECL_CONTEXT (decl);
4271 else
4272 context = TYPE_MAIN_VARIANT
4273 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4275 if (context && !TYPE_P (context))
4276 context = NULL_TREE;
4278 return context;
4281 /* Add an attribute/value pair to a DIE. */
4283 static inline void
4284 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4286 /* Maybe this should be an assert? */
4287 if (die == NULL)
4288 return;
4290 if (flag_checking)
4292 /* Check we do not add duplicate attrs. Can't use get_AT here
4293 because that recurses to the specification/abstract origin DIE. */
4294 dw_attr_node *a;
4295 unsigned ix;
4296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4297 gcc_assert (a->dw_attr != attr->dw_attr);
4300 vec_safe_reserve (die->die_attr, 1);
4301 vec_safe_push (die->die_attr, *attr);
4304 static inline enum dw_val_class
4305 AT_class (dw_attr_node *a)
4307 return a->dw_attr_val.val_class;
4310 /* Return the index for any attribute that will be referenced with a
4311 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4312 are stored in dw_attr_val.v.val_str for reference counting
4313 pruning. */
4315 static inline unsigned int
4316 AT_index (dw_attr_node *a)
4318 if (AT_class (a) == dw_val_class_str)
4319 return a->dw_attr_val.v.val_str->index;
4320 else if (a->dw_attr_val.val_entry != NULL)
4321 return a->dw_attr_val.val_entry->index;
4322 return NOT_INDEXED;
4325 /* Add a flag value attribute to a DIE. */
4327 static inline void
4328 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4330 dw_attr_node attr;
4332 attr.dw_attr = attr_kind;
4333 attr.dw_attr_val.val_class = dw_val_class_flag;
4334 attr.dw_attr_val.val_entry = NULL;
4335 attr.dw_attr_val.v.val_flag = flag;
4336 add_dwarf_attr (die, &attr);
4339 static inline unsigned
4340 AT_flag (dw_attr_node *a)
4342 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4343 return a->dw_attr_val.v.val_flag;
4346 /* Add a signed integer attribute value to a DIE. */
4348 static inline void
4349 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4351 dw_attr_node attr;
4353 attr.dw_attr = attr_kind;
4354 attr.dw_attr_val.val_class = dw_val_class_const;
4355 attr.dw_attr_val.val_entry = NULL;
4356 attr.dw_attr_val.v.val_int = int_val;
4357 add_dwarf_attr (die, &attr);
4360 static inline HOST_WIDE_INT
4361 AT_int (dw_attr_node *a)
4363 gcc_assert (a && (AT_class (a) == dw_val_class_const
4364 || AT_class (a) == dw_val_class_const_implicit));
4365 return a->dw_attr_val.v.val_int;
4368 /* Add an unsigned integer attribute value to a DIE. */
4370 static inline void
4371 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4372 unsigned HOST_WIDE_INT unsigned_val)
4374 dw_attr_node attr;
4376 attr.dw_attr = attr_kind;
4377 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4378 attr.dw_attr_val.val_entry = NULL;
4379 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4380 add_dwarf_attr (die, &attr);
4383 static inline unsigned HOST_WIDE_INT
4384 AT_unsigned (dw_attr_node *a)
4386 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4387 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4388 return a->dw_attr_val.v.val_unsigned;
4391 /* Add an unsigned wide integer attribute value to a DIE. */
4393 static inline void
4394 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4395 const wide_int& w)
4397 dw_attr_node attr;
4399 attr.dw_attr = attr_kind;
4400 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4401 attr.dw_attr_val.val_entry = NULL;
4402 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4403 *attr.dw_attr_val.v.val_wide = w;
4404 add_dwarf_attr (die, &attr);
4407 /* Add an unsigned double integer attribute value to a DIE. */
4409 static inline void
4410 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4411 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4413 dw_attr_node attr;
4415 attr.dw_attr = attr_kind;
4416 attr.dw_attr_val.val_class = dw_val_class_const_double;
4417 attr.dw_attr_val.val_entry = NULL;
4418 attr.dw_attr_val.v.val_double.high = high;
4419 attr.dw_attr_val.v.val_double.low = low;
4420 add_dwarf_attr (die, &attr);
4423 /* Add a floating point attribute value to a DIE and return it. */
4425 static inline void
4426 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4427 unsigned int length, unsigned int elt_size, unsigned char *array)
4429 dw_attr_node attr;
4431 attr.dw_attr = attr_kind;
4432 attr.dw_attr_val.val_class = dw_val_class_vec;
4433 attr.dw_attr_val.val_entry = NULL;
4434 attr.dw_attr_val.v.val_vec.length = length;
4435 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4436 attr.dw_attr_val.v.val_vec.array = array;
4437 add_dwarf_attr (die, &attr);
4440 /* Add an 8-byte data attribute value to a DIE. */
4442 static inline void
4443 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4444 unsigned char data8[8])
4446 dw_attr_node attr;
4448 attr.dw_attr = attr_kind;
4449 attr.dw_attr_val.val_class = dw_val_class_data8;
4450 attr.dw_attr_val.val_entry = NULL;
4451 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4452 add_dwarf_attr (die, &attr);
4455 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4456 dwarf_split_debug_info, address attributes in dies destined for the
4457 final executable have force_direct set to avoid using indexed
4458 references. */
4460 static inline void
4461 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4462 bool force_direct)
4464 dw_attr_node attr;
4465 char * lbl_id;
4467 lbl_id = xstrdup (lbl_low);
4468 attr.dw_attr = DW_AT_low_pc;
4469 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4470 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4471 if (dwarf_split_debug_info && !force_direct)
4472 attr.dw_attr_val.val_entry
4473 = add_addr_table_entry (lbl_id, ate_kind_label);
4474 else
4475 attr.dw_attr_val.val_entry = NULL;
4476 add_dwarf_attr (die, &attr);
4478 attr.dw_attr = DW_AT_high_pc;
4479 if (dwarf_version < 4)
4480 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4481 else
4482 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4483 lbl_id = xstrdup (lbl_high);
4484 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4485 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4486 && dwarf_split_debug_info && !force_direct)
4487 attr.dw_attr_val.val_entry
4488 = add_addr_table_entry (lbl_id, ate_kind_label);
4489 else
4490 attr.dw_attr_val.val_entry = NULL;
4491 add_dwarf_attr (die, &attr);
4494 /* Hash and equality functions for debug_str_hash. */
4496 hashval_t
4497 indirect_string_hasher::hash (indirect_string_node *x)
4499 return htab_hash_string (x->str);
4502 bool
4503 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4505 return strcmp (x1->str, x2) == 0;
4508 /* Add STR to the given string hash table. */
4510 static struct indirect_string_node *
4511 find_AT_string_in_table (const char *str,
4512 hash_table<indirect_string_hasher> *table)
4514 struct indirect_string_node *node;
4516 indirect_string_node **slot
4517 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4518 if (*slot == NULL)
4520 node = ggc_cleared_alloc<indirect_string_node> ();
4521 node->str = ggc_strdup (str);
4522 *slot = node;
4524 else
4525 node = *slot;
4527 node->refcount++;
4528 return node;
4531 /* Add STR to the indirect string hash table. */
4533 static struct indirect_string_node *
4534 find_AT_string (const char *str)
4536 if (! debug_str_hash)
4537 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4539 return find_AT_string_in_table (str, debug_str_hash);
4542 /* Add a string attribute value to a DIE. */
4544 static inline void
4545 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4547 dw_attr_node attr;
4548 struct indirect_string_node *node;
4550 node = find_AT_string (str);
4552 attr.dw_attr = attr_kind;
4553 attr.dw_attr_val.val_class = dw_val_class_str;
4554 attr.dw_attr_val.val_entry = NULL;
4555 attr.dw_attr_val.v.val_str = node;
4556 add_dwarf_attr (die, &attr);
4559 static inline const char *
4560 AT_string (dw_attr_node *a)
4562 gcc_assert (a && AT_class (a) == dw_val_class_str);
4563 return a->dw_attr_val.v.val_str->str;
4566 /* Call this function directly to bypass AT_string_form's logic to put
4567 the string inline in the die. */
4569 static void
4570 set_indirect_string (struct indirect_string_node *node)
4572 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4573 /* Already indirect is a no op. */
4574 if (node->form == DW_FORM_strp
4575 || node->form == DW_FORM_line_strp
4576 || node->form == DW_FORM_GNU_str_index)
4578 gcc_assert (node->label);
4579 return;
4581 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4582 ++dw2_string_counter;
4583 node->label = xstrdup (label);
4585 if (!dwarf_split_debug_info)
4587 node->form = DW_FORM_strp;
4588 node->index = NOT_INDEXED;
4590 else
4592 node->form = DW_FORM_GNU_str_index;
4593 node->index = NO_INDEX_ASSIGNED;
4597 /* A helper function for dwarf2out_finish, called to reset indirect
4598 string decisions done for early LTO dwarf output before fat object
4599 dwarf output. */
4602 reset_indirect_string (indirect_string_node **h, void *)
4604 struct indirect_string_node *node = *h;
4605 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4607 free (node->label);
4608 node->label = NULL;
4609 node->form = (dwarf_form) 0;
4610 node->index = 0;
4612 return 1;
4615 /* Find out whether a string should be output inline in DIE
4616 or out-of-line in .debug_str section. */
4618 static enum dwarf_form
4619 find_string_form (struct indirect_string_node *node)
4621 unsigned int len;
4623 if (node->form)
4624 return node->form;
4626 len = strlen (node->str) + 1;
4628 /* If the string is shorter or equal to the size of the reference, it is
4629 always better to put it inline. */
4630 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4631 return node->form = DW_FORM_string;
4633 /* If we cannot expect the linker to merge strings in .debug_str
4634 section, only put it into .debug_str if it is worth even in this
4635 single module. */
4636 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4637 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4638 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4639 return node->form = DW_FORM_string;
4641 set_indirect_string (node);
4643 return node->form;
4646 /* Find out whether the string referenced from the attribute should be
4647 output inline in DIE or out-of-line in .debug_str section. */
4649 static enum dwarf_form
4650 AT_string_form (dw_attr_node *a)
4652 gcc_assert (a && AT_class (a) == dw_val_class_str);
4653 return find_string_form (a->dw_attr_val.v.val_str);
4656 /* Add a DIE reference attribute value to a DIE. */
4658 static inline void
4659 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4661 dw_attr_node attr;
4662 gcc_checking_assert (targ_die != NULL);
4664 /* With LTO we can end up trying to reference something we didn't create
4665 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4666 if (targ_die == NULL)
4667 return;
4669 attr.dw_attr = attr_kind;
4670 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4671 attr.dw_attr_val.val_entry = NULL;
4672 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4673 attr.dw_attr_val.v.val_die_ref.external = 0;
4674 add_dwarf_attr (die, &attr);
4677 /* Change DIE reference REF to point to NEW_DIE instead. */
4679 static inline void
4680 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4682 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4683 ref->dw_attr_val.v.val_die_ref.die = new_die;
4684 ref->dw_attr_val.v.val_die_ref.external = 0;
4687 /* Add an AT_specification attribute to a DIE, and also make the back
4688 pointer from the specification to the definition. */
4690 static inline void
4691 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4693 add_AT_die_ref (die, DW_AT_specification, targ_die);
4694 gcc_assert (!targ_die->die_definition);
4695 targ_die->die_definition = die;
4698 static inline dw_die_ref
4699 AT_ref (dw_attr_node *a)
4701 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4702 return a->dw_attr_val.v.val_die_ref.die;
4705 static inline int
4706 AT_ref_external (dw_attr_node *a)
4708 if (a && AT_class (a) == dw_val_class_die_ref)
4709 return a->dw_attr_val.v.val_die_ref.external;
4711 return 0;
4714 static inline void
4715 set_AT_ref_external (dw_attr_node *a, int i)
4717 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4718 a->dw_attr_val.v.val_die_ref.external = i;
4721 /* Add an FDE reference attribute value to a DIE. */
4723 static inline void
4724 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4726 dw_attr_node attr;
4728 attr.dw_attr = attr_kind;
4729 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4730 attr.dw_attr_val.val_entry = NULL;
4731 attr.dw_attr_val.v.val_fde_index = targ_fde;
4732 add_dwarf_attr (die, &attr);
4735 /* Add a location description attribute value to a DIE. */
4737 static inline void
4738 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4740 dw_attr_node attr;
4742 attr.dw_attr = attr_kind;
4743 attr.dw_attr_val.val_class = dw_val_class_loc;
4744 attr.dw_attr_val.val_entry = NULL;
4745 attr.dw_attr_val.v.val_loc = loc;
4746 add_dwarf_attr (die, &attr);
4749 static inline dw_loc_descr_ref
4750 AT_loc (dw_attr_node *a)
4752 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4753 return a->dw_attr_val.v.val_loc;
4756 static inline void
4757 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4759 dw_attr_node attr;
4761 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4762 return;
4764 attr.dw_attr = attr_kind;
4765 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4766 attr.dw_attr_val.val_entry = NULL;
4767 attr.dw_attr_val.v.val_loc_list = loc_list;
4768 add_dwarf_attr (die, &attr);
4769 have_location_lists = true;
4772 static inline dw_loc_list_ref
4773 AT_loc_list (dw_attr_node *a)
4775 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4776 return a->dw_attr_val.v.val_loc_list;
4779 /* Add a view list attribute to DIE. It must have a DW_AT_location
4780 attribute, because the view list complements the location list. */
4782 static inline void
4783 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4785 dw_attr_node attr;
4787 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4788 return;
4790 attr.dw_attr = attr_kind;
4791 attr.dw_attr_val.val_class = dw_val_class_view_list;
4792 attr.dw_attr_val.val_entry = NULL;
4793 attr.dw_attr_val.v.val_view_list = die;
4794 add_dwarf_attr (die, &attr);
4795 gcc_checking_assert (get_AT (die, DW_AT_location));
4796 gcc_assert (have_location_lists);
4799 /* Return a pointer to the location list referenced by the attribute.
4800 If the named attribute is a view list, look up the corresponding
4801 DW_AT_location attribute and return its location list. */
4803 static inline dw_loc_list_ref *
4804 AT_loc_list_ptr (dw_attr_node *a)
4806 gcc_assert (a);
4807 switch (AT_class (a))
4809 case dw_val_class_loc_list:
4810 return &a->dw_attr_val.v.val_loc_list;
4811 case dw_val_class_view_list:
4813 dw_attr_node *l;
4814 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4815 if (!l)
4816 return NULL;
4817 gcc_checking_assert (l + 1 == a);
4818 return AT_loc_list_ptr (l);
4820 default:
4821 gcc_unreachable ();
4825 /* Return the location attribute value associated with a view list
4826 attribute value. */
4828 static inline dw_val_node *
4829 view_list_to_loc_list_val_node (dw_val_node *val)
4831 gcc_assert (val->val_class == dw_val_class_view_list);
4832 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4833 if (!loc)
4834 return NULL;
4835 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4836 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4837 return &loc->dw_attr_val;
4840 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4842 static hashval_t hash (addr_table_entry *);
4843 static bool equal (addr_table_entry *, addr_table_entry *);
4846 /* Table of entries into the .debug_addr section. */
4848 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4850 /* Hash an address_table_entry. */
4852 hashval_t
4853 addr_hasher::hash (addr_table_entry *a)
4855 inchash::hash hstate;
4856 switch (a->kind)
4858 case ate_kind_rtx:
4859 hstate.add_int (0);
4860 break;
4861 case ate_kind_rtx_dtprel:
4862 hstate.add_int (1);
4863 break;
4864 case ate_kind_label:
4865 return htab_hash_string (a->addr.label);
4866 default:
4867 gcc_unreachable ();
4869 inchash::add_rtx (a->addr.rtl, hstate);
4870 return hstate.end ();
4873 /* Determine equality for two address_table_entries. */
4875 bool
4876 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4878 if (a1->kind != a2->kind)
4879 return 0;
4880 switch (a1->kind)
4882 case ate_kind_rtx:
4883 case ate_kind_rtx_dtprel:
4884 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4885 case ate_kind_label:
4886 return strcmp (a1->addr.label, a2->addr.label) == 0;
4887 default:
4888 gcc_unreachable ();
4892 /* Initialize an addr_table_entry. */
4894 void
4895 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4897 e->kind = kind;
4898 switch (kind)
4900 case ate_kind_rtx:
4901 case ate_kind_rtx_dtprel:
4902 e->addr.rtl = (rtx) addr;
4903 break;
4904 case ate_kind_label:
4905 e->addr.label = (char *) addr;
4906 break;
4908 e->refcount = 0;
4909 e->index = NO_INDEX_ASSIGNED;
4912 /* Add attr to the address table entry to the table. Defer setting an
4913 index until output time. */
4915 static addr_table_entry *
4916 add_addr_table_entry (void *addr, enum ate_kind kind)
4918 addr_table_entry *node;
4919 addr_table_entry finder;
4921 gcc_assert (dwarf_split_debug_info);
4922 if (! addr_index_table)
4923 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4924 init_addr_table_entry (&finder, kind, addr);
4925 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4927 if (*slot == HTAB_EMPTY_ENTRY)
4929 node = ggc_cleared_alloc<addr_table_entry> ();
4930 init_addr_table_entry (node, kind, addr);
4931 *slot = node;
4933 else
4934 node = *slot;
4936 node->refcount++;
4937 return node;
4940 /* Remove an entry from the addr table by decrementing its refcount.
4941 Strictly, decrementing the refcount would be enough, but the
4942 assertion that the entry is actually in the table has found
4943 bugs. */
4945 static void
4946 remove_addr_table_entry (addr_table_entry *entry)
4948 gcc_assert (dwarf_split_debug_info && addr_index_table);
4949 /* After an index is assigned, the table is frozen. */
4950 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4951 entry->refcount--;
4954 /* Given a location list, remove all addresses it refers to from the
4955 address_table. */
4957 static void
4958 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4960 for (; descr; descr = descr->dw_loc_next)
4961 if (descr->dw_loc_oprnd1.val_entry != NULL)
4963 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4964 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4968 /* A helper function for dwarf2out_finish called through
4969 htab_traverse. Assign an addr_table_entry its index. All entries
4970 must be collected into the table when this function is called,
4971 because the indexing code relies on htab_traverse to traverse nodes
4972 in the same order for each run. */
4975 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4977 addr_table_entry *node = *h;
4979 /* Don't index unreferenced nodes. */
4980 if (node->refcount == 0)
4981 return 1;
4983 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4984 node->index = *index;
4985 *index += 1;
4987 return 1;
4990 /* Add an address constant attribute value to a DIE. When using
4991 dwarf_split_debug_info, address attributes in dies destined for the
4992 final executable should be direct references--setting the parameter
4993 force_direct ensures this behavior. */
4995 static inline void
4996 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4997 bool force_direct)
4999 dw_attr_node attr;
5001 attr.dw_attr = attr_kind;
5002 attr.dw_attr_val.val_class = dw_val_class_addr;
5003 attr.dw_attr_val.v.val_addr = addr;
5004 if (dwarf_split_debug_info && !force_direct)
5005 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5006 else
5007 attr.dw_attr_val.val_entry = NULL;
5008 add_dwarf_attr (die, &attr);
5011 /* Get the RTX from to an address DIE attribute. */
5013 static inline rtx
5014 AT_addr (dw_attr_node *a)
5016 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5017 return a->dw_attr_val.v.val_addr;
5020 /* Add a file attribute value to a DIE. */
5022 static inline void
5023 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5024 struct dwarf_file_data *fd)
5026 dw_attr_node attr;
5028 attr.dw_attr = attr_kind;
5029 attr.dw_attr_val.val_class = dw_val_class_file;
5030 attr.dw_attr_val.val_entry = NULL;
5031 attr.dw_attr_val.v.val_file = fd;
5032 add_dwarf_attr (die, &attr);
5035 /* Get the dwarf_file_data from a file DIE attribute. */
5037 static inline struct dwarf_file_data *
5038 AT_file (dw_attr_node *a)
5040 gcc_assert (a && (AT_class (a) == dw_val_class_file
5041 || AT_class (a) == dw_val_class_file_implicit));
5042 return a->dw_attr_val.v.val_file;
5045 /* Add a vms delta attribute value to a DIE. */
5047 static inline void
5048 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5049 const char *lbl1, const char *lbl2)
5051 dw_attr_node attr;
5053 attr.dw_attr = attr_kind;
5054 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5055 attr.dw_attr_val.val_entry = NULL;
5056 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5057 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5058 add_dwarf_attr (die, &attr);
5061 /* Add a label identifier attribute value to a DIE. */
5063 static inline void
5064 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5065 const char *lbl_id)
5067 dw_attr_node attr;
5069 attr.dw_attr = attr_kind;
5070 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5071 attr.dw_attr_val.val_entry = NULL;
5072 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5073 if (dwarf_split_debug_info)
5074 attr.dw_attr_val.val_entry
5075 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5076 ate_kind_label);
5077 add_dwarf_attr (die, &attr);
5080 /* Add a section offset attribute value to a DIE, an offset into the
5081 debug_line section. */
5083 static inline void
5084 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5085 const char *label)
5087 dw_attr_node attr;
5089 attr.dw_attr = attr_kind;
5090 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5091 attr.dw_attr_val.val_entry = NULL;
5092 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5093 add_dwarf_attr (die, &attr);
5096 /* Add a section offset attribute value to a DIE, an offset into the
5097 debug_loclists section. */
5099 static inline void
5100 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5101 const char *label)
5103 dw_attr_node attr;
5105 attr.dw_attr = attr_kind;
5106 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
5107 attr.dw_attr_val.val_entry = NULL;
5108 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5109 add_dwarf_attr (die, &attr);
5112 /* Add a section offset attribute value to a DIE, an offset into the
5113 debug_macinfo section. */
5115 static inline void
5116 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5117 const char *label)
5119 dw_attr_node attr;
5121 attr.dw_attr = attr_kind;
5122 attr.dw_attr_val.val_class = dw_val_class_macptr;
5123 attr.dw_attr_val.val_entry = NULL;
5124 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5125 add_dwarf_attr (die, &attr);
5128 /* Add an offset attribute value to a DIE. */
5130 static inline void
5131 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5132 unsigned HOST_WIDE_INT offset)
5134 dw_attr_node attr;
5136 attr.dw_attr = attr_kind;
5137 attr.dw_attr_val.val_class = dw_val_class_offset;
5138 attr.dw_attr_val.val_entry = NULL;
5139 attr.dw_attr_val.v.val_offset = offset;
5140 add_dwarf_attr (die, &attr);
5143 /* Add a range_list attribute value to a DIE. When using
5144 dwarf_split_debug_info, address attributes in dies destined for the
5145 final executable should be direct references--setting the parameter
5146 force_direct ensures this behavior. */
5148 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5149 #define RELOCATED_OFFSET (NULL)
5151 static void
5152 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5153 long unsigned int offset, bool force_direct)
5155 dw_attr_node attr;
5157 attr.dw_attr = attr_kind;
5158 attr.dw_attr_val.val_class = dw_val_class_range_list;
5159 /* For the range_list attribute, use val_entry to store whether the
5160 offset should follow split-debug-info or normal semantics. This
5161 value is read in output_range_list_offset. */
5162 if (dwarf_split_debug_info && !force_direct)
5163 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5164 else
5165 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5166 attr.dw_attr_val.v.val_offset = offset;
5167 add_dwarf_attr (die, &attr);
5170 /* Return the start label of a delta attribute. */
5172 static inline const char *
5173 AT_vms_delta1 (dw_attr_node *a)
5175 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5176 return a->dw_attr_val.v.val_vms_delta.lbl1;
5179 /* Return the end label of a delta attribute. */
5181 static inline const char *
5182 AT_vms_delta2 (dw_attr_node *a)
5184 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5185 return a->dw_attr_val.v.val_vms_delta.lbl2;
5188 static inline const char *
5189 AT_lbl (dw_attr_node *a)
5191 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5192 || AT_class (a) == dw_val_class_lineptr
5193 || AT_class (a) == dw_val_class_macptr
5194 || AT_class (a) == dw_val_class_loclistsptr
5195 || AT_class (a) == dw_val_class_high_pc));
5196 return a->dw_attr_val.v.val_lbl_id;
5199 /* Get the attribute of type attr_kind. */
5201 static dw_attr_node *
5202 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5204 dw_attr_node *a;
5205 unsigned ix;
5206 dw_die_ref spec = NULL;
5208 if (! die)
5209 return NULL;
5211 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5212 if (a->dw_attr == attr_kind)
5213 return a;
5214 else if (a->dw_attr == DW_AT_specification
5215 || a->dw_attr == DW_AT_abstract_origin)
5216 spec = AT_ref (a);
5218 if (spec)
5219 return get_AT (spec, attr_kind);
5221 return NULL;
5224 /* Returns the parent of the declaration of DIE. */
5226 static dw_die_ref
5227 get_die_parent (dw_die_ref die)
5229 dw_die_ref t;
5231 if (!die)
5232 return NULL;
5234 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5235 || (t = get_AT_ref (die, DW_AT_specification)))
5236 die = t;
5238 return die->die_parent;
5241 /* Return the "low pc" attribute value, typically associated with a subprogram
5242 DIE. Return null if the "low pc" attribute is either not present, or if it
5243 cannot be represented as an assembler label identifier. */
5245 static inline const char *
5246 get_AT_low_pc (dw_die_ref die)
5248 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5250 return a ? AT_lbl (a) : NULL;
5253 /* Return the "high pc" attribute value, typically associated with a subprogram
5254 DIE. Return null if the "high pc" attribute is either not present, or if it
5255 cannot be represented as an assembler label identifier. */
5257 static inline const char *
5258 get_AT_hi_pc (dw_die_ref die)
5260 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5262 return a ? AT_lbl (a) : NULL;
5265 /* Return the value of the string attribute designated by ATTR_KIND, or
5266 NULL if it is not present. */
5268 static inline const char *
5269 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5271 dw_attr_node *a = get_AT (die, attr_kind);
5273 return a ? AT_string (a) : NULL;
5276 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5277 if it is not present. */
5279 static inline int
5280 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5282 dw_attr_node *a = get_AT (die, attr_kind);
5284 return a ? AT_flag (a) : 0;
5287 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5288 if it is not present. */
5290 static inline unsigned
5291 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5293 dw_attr_node *a = get_AT (die, attr_kind);
5295 return a ? AT_unsigned (a) : 0;
5298 static inline dw_die_ref
5299 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5301 dw_attr_node *a = get_AT (die, attr_kind);
5303 return a ? AT_ref (a) : NULL;
5306 static inline struct dwarf_file_data *
5307 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5309 dw_attr_node *a = get_AT (die, attr_kind);
5311 return a ? AT_file (a) : NULL;
5314 /* Return TRUE if the language is C++. */
5316 static inline bool
5317 is_cxx (void)
5319 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5321 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5322 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5325 /* Return TRUE if DECL was created by the C++ frontend. */
5327 static bool
5328 is_cxx (const_tree decl)
5330 if (in_lto_p)
5332 const_tree context = get_ultimate_context (decl);
5333 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5334 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5336 return is_cxx ();
5339 /* Return TRUE if the language is Fortran. */
5341 static inline bool
5342 is_fortran (void)
5344 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5346 return (lang == DW_LANG_Fortran77
5347 || lang == DW_LANG_Fortran90
5348 || lang == DW_LANG_Fortran95
5349 || lang == DW_LANG_Fortran03
5350 || lang == DW_LANG_Fortran08);
5353 static inline bool
5354 is_fortran (const_tree decl)
5356 if (in_lto_p)
5358 const_tree context = get_ultimate_context (decl);
5359 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5360 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5361 "GNU Fortran", 11) == 0
5362 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5363 "GNU F77") == 0);
5365 return is_fortran ();
5368 /* Return TRUE if the language is Ada. */
5370 static inline bool
5371 is_ada (void)
5373 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5375 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5378 /* Remove the specified attribute if present. Return TRUE if removal
5379 was successful. */
5381 static bool
5382 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5384 dw_attr_node *a;
5385 unsigned ix;
5387 if (! die)
5388 return false;
5390 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5391 if (a->dw_attr == attr_kind)
5393 if (AT_class (a) == dw_val_class_str)
5394 if (a->dw_attr_val.v.val_str->refcount)
5395 a->dw_attr_val.v.val_str->refcount--;
5397 /* vec::ordered_remove should help reduce the number of abbrevs
5398 that are needed. */
5399 die->die_attr->ordered_remove (ix);
5400 return true;
5402 return false;
5405 /* Remove CHILD from its parent. PREV must have the property that
5406 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5408 static void
5409 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5411 gcc_assert (child->die_parent == prev->die_parent);
5412 gcc_assert (prev->die_sib == child);
5413 if (prev == child)
5415 gcc_assert (child->die_parent->die_child == child);
5416 prev = NULL;
5418 else
5419 prev->die_sib = child->die_sib;
5420 if (child->die_parent->die_child == child)
5421 child->die_parent->die_child = prev;
5422 child->die_sib = NULL;
5425 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5426 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5428 static void
5429 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5431 dw_die_ref parent = old_child->die_parent;
5433 gcc_assert (parent == prev->die_parent);
5434 gcc_assert (prev->die_sib == old_child);
5436 new_child->die_parent = parent;
5437 if (prev == old_child)
5439 gcc_assert (parent->die_child == old_child);
5440 new_child->die_sib = new_child;
5442 else
5444 prev->die_sib = new_child;
5445 new_child->die_sib = old_child->die_sib;
5447 if (old_child->die_parent->die_child == old_child)
5448 old_child->die_parent->die_child = new_child;
5449 old_child->die_sib = NULL;
5452 /* Move all children from OLD_PARENT to NEW_PARENT. */
5454 static void
5455 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5457 dw_die_ref c;
5458 new_parent->die_child = old_parent->die_child;
5459 old_parent->die_child = NULL;
5460 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5463 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5464 matches TAG. */
5466 static void
5467 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5469 dw_die_ref c;
5471 c = die->die_child;
5472 if (c) do {
5473 dw_die_ref prev = c;
5474 c = c->die_sib;
5475 while (c->die_tag == tag)
5477 remove_child_with_prev (c, prev);
5478 c->die_parent = NULL;
5479 /* Might have removed every child. */
5480 if (die->die_child == NULL)
5481 return;
5482 c = prev->die_sib;
5484 } while (c != die->die_child);
5487 /* Add a CHILD_DIE as the last child of DIE. */
5489 static void
5490 add_child_die (dw_die_ref die, dw_die_ref child_die)
5492 /* FIXME this should probably be an assert. */
5493 if (! die || ! child_die)
5494 return;
5495 gcc_assert (die != child_die);
5497 child_die->die_parent = die;
5498 if (die->die_child)
5500 child_die->die_sib = die->die_child->die_sib;
5501 die->die_child->die_sib = child_die;
5503 else
5504 child_die->die_sib = child_die;
5505 die->die_child = child_die;
5508 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5510 static void
5511 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5512 dw_die_ref after_die)
5514 gcc_assert (die
5515 && child_die
5516 && after_die
5517 && die->die_child
5518 && die != child_die);
5520 child_die->die_parent = die;
5521 child_die->die_sib = after_die->die_sib;
5522 after_die->die_sib = child_die;
5523 if (die->die_child == after_die)
5524 die->die_child = child_die;
5527 /* Unassociate CHILD from its parent, and make its parent be
5528 NEW_PARENT. */
5530 static void
5531 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5533 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5534 if (p->die_sib == child)
5536 remove_child_with_prev (child, p);
5537 break;
5539 add_child_die (new_parent, child);
5542 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5543 is the specification, to the end of PARENT's list of children.
5544 This is done by removing and re-adding it. */
5546 static void
5547 splice_child_die (dw_die_ref parent, dw_die_ref child)
5549 /* We want the declaration DIE from inside the class, not the
5550 specification DIE at toplevel. */
5551 if (child->die_parent != parent)
5553 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5555 if (tmp)
5556 child = tmp;
5559 gcc_assert (child->die_parent == parent
5560 || (child->die_parent
5561 == get_AT_ref (parent, DW_AT_specification)));
5563 reparent_child (child, parent);
5566 /* Create and return a new die with TAG_VALUE as tag. */
5568 static inline dw_die_ref
5569 new_die_raw (enum dwarf_tag tag_value)
5571 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5572 die->die_tag = tag_value;
5573 return die;
5576 /* Create and return a new die with a parent of PARENT_DIE. If
5577 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5578 associated tree T must be supplied to determine parenthood
5579 later. */
5581 static inline dw_die_ref
5582 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5584 dw_die_ref die = new_die_raw (tag_value);
5586 if (parent_die != NULL)
5587 add_child_die (parent_die, die);
5588 else
5590 limbo_die_node *limbo_node;
5592 /* No DIEs created after early dwarf should end up in limbo,
5593 because the limbo list should not persist past LTO
5594 streaming. */
5595 if (tag_value != DW_TAG_compile_unit
5596 /* These are allowed because they're generated while
5597 breaking out COMDAT units late. */
5598 && tag_value != DW_TAG_type_unit
5599 && tag_value != DW_TAG_skeleton_unit
5600 && !early_dwarf
5601 /* Allow nested functions to live in limbo because they will
5602 only temporarily live there, as decls_for_scope will fix
5603 them up. */
5604 && (TREE_CODE (t) != FUNCTION_DECL
5605 || !decl_function_context (t))
5606 /* Same as nested functions above but for types. Types that
5607 are local to a function will be fixed in
5608 decls_for_scope. */
5609 && (!RECORD_OR_UNION_TYPE_P (t)
5610 || !TYPE_CONTEXT (t)
5611 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5612 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5613 especially in the ltrans stage, but once we implement LTO
5614 dwarf streaming, we should remove this exception. */
5615 && !in_lto_p)
5617 fprintf (stderr, "symbol ended up in limbo too late:");
5618 debug_generic_stmt (t);
5619 gcc_unreachable ();
5622 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5623 limbo_node->die = die;
5624 limbo_node->created_for = t;
5625 limbo_node->next = limbo_die_list;
5626 limbo_die_list = limbo_node;
5629 return die;
5632 /* Return the DIE associated with the given type specifier. */
5634 static inline dw_die_ref
5635 lookup_type_die (tree type)
5637 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5638 if (die && die->removed)
5640 TYPE_SYMTAB_DIE (type) = NULL;
5641 return NULL;
5643 return die;
5646 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5647 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5648 anonymous type instead the one of the naming typedef. */
5650 static inline dw_die_ref
5651 strip_naming_typedef (tree type, dw_die_ref type_die)
5653 if (type
5654 && TREE_CODE (type) == RECORD_TYPE
5655 && type_die
5656 && type_die->die_tag == DW_TAG_typedef
5657 && is_naming_typedef_decl (TYPE_NAME (type)))
5658 type_die = get_AT_ref (type_die, DW_AT_type);
5659 return type_die;
5662 /* Like lookup_type_die, but if type is an anonymous type named by a
5663 typedef[1], return the DIE of the anonymous type instead the one of
5664 the naming typedef. This is because in gen_typedef_die, we did
5665 equate the anonymous struct named by the typedef with the DIE of
5666 the naming typedef. So by default, lookup_type_die on an anonymous
5667 struct yields the DIE of the naming typedef.
5669 [1]: Read the comment of is_naming_typedef_decl to learn about what
5670 a naming typedef is. */
5672 static inline dw_die_ref
5673 lookup_type_die_strip_naming_typedef (tree type)
5675 dw_die_ref die = lookup_type_die (type);
5676 return strip_naming_typedef (type, die);
5679 /* Equate a DIE to a given type specifier. */
5681 static inline void
5682 equate_type_number_to_die (tree type, dw_die_ref type_die)
5684 TYPE_SYMTAB_DIE (type) = type_die;
5687 /* Returns a hash value for X (which really is a die_struct). */
5689 inline hashval_t
5690 decl_die_hasher::hash (die_node *x)
5692 return (hashval_t) x->decl_id;
5695 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5697 inline bool
5698 decl_die_hasher::equal (die_node *x, tree y)
5700 return (x->decl_id == DECL_UID (y));
5703 /* Return the DIE associated with a given declaration. */
5705 static inline dw_die_ref
5706 lookup_decl_die (tree decl)
5708 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5709 NO_INSERT);
5710 if (!die)
5711 return NULL;
5712 if ((*die)->removed)
5714 decl_die_table->clear_slot (die);
5715 return NULL;
5717 return *die;
5721 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5722 style reference. Return true if we found one refering to a DIE for
5723 DECL, otherwise return false. */
5725 static bool
5726 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5727 unsigned HOST_WIDE_INT *off)
5729 dw_die_ref die;
5731 if (flag_wpa && !decl_die_table)
5732 return false;
5734 if (TREE_CODE (decl) == BLOCK)
5735 die = BLOCK_DIE (decl);
5736 else
5737 die = lookup_decl_die (decl);
5738 if (!die)
5739 return false;
5741 /* During WPA stage we currently use DIEs to store the
5742 decl <-> label + offset map. That's quite inefficient but it
5743 works for now. */
5744 if (flag_wpa)
5746 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5747 if (!ref)
5749 gcc_assert (die == comp_unit_die ());
5750 return false;
5752 *off = ref->die_offset;
5753 *sym = ref->die_id.die_symbol;
5754 return true;
5757 /* Similar to get_ref_die_offset_label, but using the "correct"
5758 label. */
5759 *off = die->die_offset;
5760 while (die->die_parent)
5761 die = die->die_parent;
5762 /* For the containing CU DIE we compute a die_symbol in
5763 compute_comp_unit_symbol. */
5764 gcc_assert (die->die_tag == DW_TAG_compile_unit
5765 && die->die_id.die_symbol != NULL);
5766 *sym = die->die_id.die_symbol;
5767 return true;
5770 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5772 static void
5773 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5774 const char *symbol, HOST_WIDE_INT offset)
5776 /* Create a fake DIE that contains the reference. Don't use
5777 new_die because we don't want to end up in the limbo list. */
5778 dw_die_ref ref = new_die_raw (die->die_tag);
5779 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5780 ref->die_offset = offset;
5781 ref->with_offset = 1;
5782 add_AT_die_ref (die, attr_kind, ref);
5785 /* Create a DIE for DECL if required and add a reference to a DIE
5786 at SYMBOL + OFFSET which contains attributes dumped early. */
5788 static void
5789 dwarf2out_register_external_die (tree decl, const char *sym,
5790 unsigned HOST_WIDE_INT off)
5792 if (debug_info_level == DINFO_LEVEL_NONE)
5793 return;
5795 if (flag_wpa && !decl_die_table)
5796 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5798 dw_die_ref die
5799 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5800 gcc_assert (!die);
5802 tree ctx;
5803 dw_die_ref parent = NULL;
5804 /* Need to lookup a DIE for the decls context - the containing
5805 function or translation unit. */
5806 if (TREE_CODE (decl) == BLOCK)
5808 ctx = BLOCK_SUPERCONTEXT (decl);
5809 /* ??? We do not output DIEs for all scopes thus skip as
5810 many DIEs as needed. */
5811 while (TREE_CODE (ctx) == BLOCK
5812 && !BLOCK_DIE (ctx))
5813 ctx = BLOCK_SUPERCONTEXT (ctx);
5815 else
5816 ctx = DECL_CONTEXT (decl);
5817 while (ctx && TYPE_P (ctx))
5818 ctx = TYPE_CONTEXT (ctx);
5819 if (ctx)
5821 if (TREE_CODE (ctx) == BLOCK)
5822 parent = BLOCK_DIE (ctx);
5823 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5824 /* Keep the 1:1 association during WPA. */
5825 && !flag_wpa)
5826 /* Otherwise all late annotations go to the main CU which
5827 imports the original CUs. */
5828 parent = comp_unit_die ();
5829 else if (TREE_CODE (ctx) == FUNCTION_DECL
5830 && TREE_CODE (decl) != PARM_DECL
5831 && TREE_CODE (decl) != BLOCK)
5832 /* Leave function local entities parent determination to when
5833 we process scope vars. */
5835 else
5836 parent = lookup_decl_die (ctx);
5838 else
5839 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5840 Handle this case gracefully by globalizing stuff. */
5841 parent = comp_unit_die ();
5842 /* Create a DIE "stub". */
5843 switch (TREE_CODE (decl))
5845 case TRANSLATION_UNIT_DECL:
5846 if (! flag_wpa)
5848 die = comp_unit_die ();
5849 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5850 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5851 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5852 to create a DIE for the original CUs. */
5853 return;
5855 /* Keep the 1:1 association during WPA. */
5856 die = new_die (DW_TAG_compile_unit, NULL, decl);
5857 break;
5858 case NAMESPACE_DECL:
5859 if (is_fortran (decl))
5860 die = new_die (DW_TAG_module, parent, decl);
5861 else
5862 die = new_die (DW_TAG_namespace, parent, decl);
5863 break;
5864 case FUNCTION_DECL:
5865 die = new_die (DW_TAG_subprogram, parent, decl);
5866 break;
5867 case VAR_DECL:
5868 die = new_die (DW_TAG_variable, parent, decl);
5869 break;
5870 case RESULT_DECL:
5871 die = new_die (DW_TAG_variable, parent, decl);
5872 break;
5873 case PARM_DECL:
5874 die = new_die (DW_TAG_formal_parameter, parent, decl);
5875 break;
5876 case CONST_DECL:
5877 die = new_die (DW_TAG_constant, parent, decl);
5878 break;
5879 case LABEL_DECL:
5880 die = new_die (DW_TAG_label, parent, decl);
5881 break;
5882 case BLOCK:
5883 die = new_die (DW_TAG_lexical_block, parent, decl);
5884 break;
5885 default:
5886 gcc_unreachable ();
5888 if (TREE_CODE (decl) == BLOCK)
5889 BLOCK_DIE (decl) = die;
5890 else
5891 equate_decl_number_to_die (decl, die);
5893 /* Add a reference to the DIE providing early debug at $sym + off. */
5894 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5897 /* Returns a hash value for X (which really is a var_loc_list). */
5899 inline hashval_t
5900 decl_loc_hasher::hash (var_loc_list *x)
5902 return (hashval_t) x->decl_id;
5905 /* Return nonzero if decl_id of var_loc_list X is the same as
5906 UID of decl *Y. */
5908 inline bool
5909 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5911 return (x->decl_id == DECL_UID (y));
5914 /* Return the var_loc list associated with a given declaration. */
5916 static inline var_loc_list *
5917 lookup_decl_loc (const_tree decl)
5919 if (!decl_loc_table)
5920 return NULL;
5921 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5924 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5926 inline hashval_t
5927 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5929 return (hashval_t) x->decl_id;
5932 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5933 UID of decl *Y. */
5935 inline bool
5936 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5938 return (x->decl_id == DECL_UID (y));
5941 /* Equate a DIE to a particular declaration. */
5943 static void
5944 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5946 unsigned int decl_id = DECL_UID (decl);
5948 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5949 decl_die->decl_id = decl_id;
5952 /* Return how many bits covers PIECE EXPR_LIST. */
5954 static HOST_WIDE_INT
5955 decl_piece_bitsize (rtx piece)
5957 int ret = (int) GET_MODE (piece);
5958 if (ret)
5959 return ret;
5960 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5961 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5962 return INTVAL (XEXP (XEXP (piece, 0), 0));
5965 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5967 static rtx *
5968 decl_piece_varloc_ptr (rtx piece)
5970 if ((int) GET_MODE (piece))
5971 return &XEXP (piece, 0);
5972 else
5973 return &XEXP (XEXP (piece, 0), 1);
5976 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5977 Next is the chain of following piece nodes. */
5979 static rtx_expr_list *
5980 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5982 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5983 return alloc_EXPR_LIST (bitsize, loc_note, next);
5984 else
5985 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5986 GEN_INT (bitsize),
5987 loc_note), next);
5990 /* Return rtx that should be stored into loc field for
5991 LOC_NOTE and BITPOS/BITSIZE. */
5993 static rtx
5994 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5995 HOST_WIDE_INT bitsize)
5997 if (bitsize != -1)
5999 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6000 if (bitpos != 0)
6001 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6003 return loc_note;
6006 /* This function either modifies location piece list *DEST in
6007 place (if SRC and INNER is NULL), or copies location piece list
6008 *SRC to *DEST while modifying it. Location BITPOS is modified
6009 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6010 not copied and if needed some padding around it is added.
6011 When modifying in place, DEST should point to EXPR_LIST where
6012 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6013 to the start of the whole list and INNER points to the EXPR_LIST
6014 where earlier pieces cover PIECE_BITPOS bits. */
6016 static void
6017 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6018 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6019 HOST_WIDE_INT bitsize, rtx loc_note)
6021 HOST_WIDE_INT diff;
6022 bool copy = inner != NULL;
6024 if (copy)
6026 /* First copy all nodes preceding the current bitpos. */
6027 while (src != inner)
6029 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6030 decl_piece_bitsize (*src), NULL_RTX);
6031 dest = &XEXP (*dest, 1);
6032 src = &XEXP (*src, 1);
6035 /* Add padding if needed. */
6036 if (bitpos != piece_bitpos)
6038 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6039 copy ? NULL_RTX : *dest);
6040 dest = &XEXP (*dest, 1);
6042 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6044 gcc_assert (!copy);
6045 /* A piece with correct bitpos and bitsize already exist,
6046 just update the location for it and return. */
6047 *decl_piece_varloc_ptr (*dest) = loc_note;
6048 return;
6050 /* Add the piece that changed. */
6051 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6052 dest = &XEXP (*dest, 1);
6053 /* Skip over pieces that overlap it. */
6054 diff = bitpos - piece_bitpos + bitsize;
6055 if (!copy)
6056 src = dest;
6057 while (diff > 0 && *src)
6059 rtx piece = *src;
6060 diff -= decl_piece_bitsize (piece);
6061 if (copy)
6062 src = &XEXP (piece, 1);
6063 else
6065 *src = XEXP (piece, 1);
6066 free_EXPR_LIST_node (piece);
6069 /* Add padding if needed. */
6070 if (diff < 0 && *src)
6072 if (!copy)
6073 dest = src;
6074 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6075 dest = &XEXP (*dest, 1);
6077 if (!copy)
6078 return;
6079 /* Finally copy all nodes following it. */
6080 while (*src)
6082 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6083 decl_piece_bitsize (*src), NULL_RTX);
6084 dest = &XEXP (*dest, 1);
6085 src = &XEXP (*src, 1);
6089 /* Add a variable location node to the linked list for DECL. */
6091 static struct var_loc_node *
6092 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6094 unsigned int decl_id;
6095 var_loc_list *temp;
6096 struct var_loc_node *loc = NULL;
6097 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6099 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6101 tree realdecl = DECL_DEBUG_EXPR (decl);
6102 if (handled_component_p (realdecl)
6103 || (TREE_CODE (realdecl) == MEM_REF
6104 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6106 bool reverse;
6107 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6108 &bitsize, &reverse);
6109 if (!innerdecl
6110 || !DECL_P (innerdecl)
6111 || DECL_IGNORED_P (innerdecl)
6112 || TREE_STATIC (innerdecl)
6113 || bitsize == 0
6114 || bitpos + bitsize > 256)
6115 return NULL;
6116 decl = innerdecl;
6120 decl_id = DECL_UID (decl);
6121 var_loc_list **slot
6122 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6123 if (*slot == NULL)
6125 temp = ggc_cleared_alloc<var_loc_list> ();
6126 temp->decl_id = decl_id;
6127 *slot = temp;
6129 else
6130 temp = *slot;
6132 /* For PARM_DECLs try to keep around the original incoming value,
6133 even if that means we'll emit a zero-range .debug_loc entry. */
6134 if (temp->last
6135 && temp->first == temp->last
6136 && TREE_CODE (decl) == PARM_DECL
6137 && NOTE_P (temp->first->loc)
6138 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6139 && DECL_INCOMING_RTL (decl)
6140 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6141 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6142 == GET_CODE (DECL_INCOMING_RTL (decl))
6143 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6144 && (bitsize != -1
6145 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6146 NOTE_VAR_LOCATION_LOC (loc_note))
6147 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6148 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6150 loc = ggc_cleared_alloc<var_loc_node> ();
6151 temp->first->next = loc;
6152 temp->last = loc;
6153 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6155 else if (temp->last)
6157 struct var_loc_node *last = temp->last, *unused = NULL;
6158 rtx *piece_loc = NULL, last_loc_note;
6159 HOST_WIDE_INT piece_bitpos = 0;
6160 if (last->next)
6162 last = last->next;
6163 gcc_assert (last->next == NULL);
6165 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6167 piece_loc = &last->loc;
6170 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6171 if (piece_bitpos + cur_bitsize > bitpos)
6172 break;
6173 piece_bitpos += cur_bitsize;
6174 piece_loc = &XEXP (*piece_loc, 1);
6176 while (*piece_loc);
6178 /* TEMP->LAST here is either pointer to the last but one or
6179 last element in the chained list, LAST is pointer to the
6180 last element. */
6181 if (label && strcmp (last->label, label) == 0 && last->view == view)
6183 /* For SRA optimized variables if there weren't any real
6184 insns since last note, just modify the last node. */
6185 if (piece_loc != NULL)
6187 adjust_piece_list (piece_loc, NULL, NULL,
6188 bitpos, piece_bitpos, bitsize, loc_note);
6189 return NULL;
6191 /* If the last note doesn't cover any instructions, remove it. */
6192 if (temp->last != last)
6194 temp->last->next = NULL;
6195 unused = last;
6196 last = temp->last;
6197 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6199 else
6201 gcc_assert (temp->first == temp->last
6202 || (temp->first->next == temp->last
6203 && TREE_CODE (decl) == PARM_DECL));
6204 memset (temp->last, '\0', sizeof (*temp->last));
6205 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6206 return temp->last;
6209 if (bitsize == -1 && NOTE_P (last->loc))
6210 last_loc_note = last->loc;
6211 else if (piece_loc != NULL
6212 && *piece_loc != NULL_RTX
6213 && piece_bitpos == bitpos
6214 && decl_piece_bitsize (*piece_loc) == bitsize)
6215 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6216 else
6217 last_loc_note = NULL_RTX;
6218 /* If the current location is the same as the end of the list,
6219 and either both or neither of the locations is uninitialized,
6220 we have nothing to do. */
6221 if (last_loc_note == NULL_RTX
6222 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6223 NOTE_VAR_LOCATION_LOC (loc_note)))
6224 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6225 != NOTE_VAR_LOCATION_STATUS (loc_note))
6226 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6227 == VAR_INIT_STATUS_UNINITIALIZED)
6228 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6229 == VAR_INIT_STATUS_UNINITIALIZED))))
6231 /* Add LOC to the end of list and update LAST. If the last
6232 element of the list has been removed above, reuse its
6233 memory for the new node, otherwise allocate a new one. */
6234 if (unused)
6236 loc = unused;
6237 memset (loc, '\0', sizeof (*loc));
6239 else
6240 loc = ggc_cleared_alloc<var_loc_node> ();
6241 if (bitsize == -1 || piece_loc == NULL)
6242 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6243 else
6244 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6245 bitpos, piece_bitpos, bitsize, loc_note);
6246 last->next = loc;
6247 /* Ensure TEMP->LAST will point either to the new last but one
6248 element of the chain, or to the last element in it. */
6249 if (last != temp->last)
6250 temp->last = last;
6252 else if (unused)
6253 ggc_free (unused);
6255 else
6257 loc = ggc_cleared_alloc<var_loc_node> ();
6258 temp->first = loc;
6259 temp->last = loc;
6260 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6262 return loc;
6265 /* Keep track of the number of spaces used to indent the
6266 output of the debugging routines that print the structure of
6267 the DIE internal representation. */
6268 static int print_indent;
6270 /* Indent the line the number of spaces given by print_indent. */
6272 static inline void
6273 print_spaces (FILE *outfile)
6275 fprintf (outfile, "%*s", print_indent, "");
6278 /* Print a type signature in hex. */
6280 static inline void
6281 print_signature (FILE *outfile, char *sig)
6283 int i;
6285 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6286 fprintf (outfile, "%02x", sig[i] & 0xff);
6289 static inline void
6290 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6292 if (discr_value->pos)
6293 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6294 else
6295 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6298 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6300 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6301 RECURSE, output location descriptor operations. */
6303 static void
6304 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6306 switch (val->val_class)
6308 case dw_val_class_addr:
6309 fprintf (outfile, "address");
6310 break;
6311 case dw_val_class_offset:
6312 fprintf (outfile, "offset");
6313 break;
6314 case dw_val_class_loc:
6315 fprintf (outfile, "location descriptor");
6316 if (val->v.val_loc == NULL)
6317 fprintf (outfile, " -> <null>\n");
6318 else if (recurse)
6320 fprintf (outfile, ":\n");
6321 print_indent += 4;
6322 print_loc_descr (val->v.val_loc, outfile);
6323 print_indent -= 4;
6325 else
6326 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6327 break;
6328 case dw_val_class_loc_list:
6329 fprintf (outfile, "location list -> label:%s",
6330 val->v.val_loc_list->ll_symbol);
6331 break;
6332 case dw_val_class_view_list:
6333 val = view_list_to_loc_list_val_node (val);
6334 fprintf (outfile, "location list with views -> labels:%s and %s",
6335 val->v.val_loc_list->ll_symbol,
6336 val->v.val_loc_list->vl_symbol);
6337 break;
6338 case dw_val_class_range_list:
6339 fprintf (outfile, "range list");
6340 break;
6341 case dw_val_class_const:
6342 case dw_val_class_const_implicit:
6343 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6344 break;
6345 case dw_val_class_unsigned_const:
6346 case dw_val_class_unsigned_const_implicit:
6347 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6348 break;
6349 case dw_val_class_const_double:
6350 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6351 HOST_WIDE_INT_PRINT_UNSIGNED")",
6352 val->v.val_double.high,
6353 val->v.val_double.low);
6354 break;
6355 case dw_val_class_wide_int:
6357 int i = val->v.val_wide->get_len ();
6358 fprintf (outfile, "constant (");
6359 gcc_assert (i > 0);
6360 if (val->v.val_wide->elt (i - 1) == 0)
6361 fprintf (outfile, "0x");
6362 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6363 val->v.val_wide->elt (--i));
6364 while (--i >= 0)
6365 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6366 val->v.val_wide->elt (i));
6367 fprintf (outfile, ")");
6368 break;
6370 case dw_val_class_vec:
6371 fprintf (outfile, "floating-point or vector constant");
6372 break;
6373 case dw_val_class_flag:
6374 fprintf (outfile, "%u", val->v.val_flag);
6375 break;
6376 case dw_val_class_die_ref:
6377 if (val->v.val_die_ref.die != NULL)
6379 dw_die_ref die = val->v.val_die_ref.die;
6381 if (die->comdat_type_p)
6383 fprintf (outfile, "die -> signature: ");
6384 print_signature (outfile,
6385 die->die_id.die_type_node->signature);
6387 else if (die->die_id.die_symbol)
6389 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6390 if (die->with_offset)
6391 fprintf (outfile, " + %ld", die->die_offset);
6393 else
6394 fprintf (outfile, "die -> %ld", die->die_offset);
6395 fprintf (outfile, " (%p)", (void *) die);
6397 else
6398 fprintf (outfile, "die -> <null>");
6399 break;
6400 case dw_val_class_vms_delta:
6401 fprintf (outfile, "delta: @slotcount(%s-%s)",
6402 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6403 break;
6404 case dw_val_class_lbl_id:
6405 case dw_val_class_lineptr:
6406 case dw_val_class_macptr:
6407 case dw_val_class_loclistsptr:
6408 case dw_val_class_high_pc:
6409 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6410 break;
6411 case dw_val_class_str:
6412 if (val->v.val_str->str != NULL)
6413 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6414 else
6415 fprintf (outfile, "<null>");
6416 break;
6417 case dw_val_class_file:
6418 case dw_val_class_file_implicit:
6419 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6420 val->v.val_file->emitted_number);
6421 break;
6422 case dw_val_class_data8:
6424 int i;
6426 for (i = 0; i < 8; i++)
6427 fprintf (outfile, "%02x", val->v.val_data8[i]);
6428 break;
6430 case dw_val_class_discr_value:
6431 print_discr_value (outfile, &val->v.val_discr_value);
6432 break;
6433 case dw_val_class_discr_list:
6434 for (dw_discr_list_ref node = val->v.val_discr_list;
6435 node != NULL;
6436 node = node->dw_discr_next)
6438 if (node->dw_discr_range)
6440 fprintf (outfile, " .. ");
6441 print_discr_value (outfile, &node->dw_discr_lower_bound);
6442 print_discr_value (outfile, &node->dw_discr_upper_bound);
6444 else
6445 print_discr_value (outfile, &node->dw_discr_lower_bound);
6447 if (node->dw_discr_next != NULL)
6448 fprintf (outfile, " | ");
6450 default:
6451 break;
6455 /* Likewise, for a DIE attribute. */
6457 static void
6458 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6460 print_dw_val (&a->dw_attr_val, recurse, outfile);
6464 /* Print the list of operands in the LOC location description to OUTFILE. This
6465 routine is a debugging aid only. */
6467 static void
6468 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6470 dw_loc_descr_ref l = loc;
6472 if (loc == NULL)
6474 print_spaces (outfile);
6475 fprintf (outfile, "<null>\n");
6476 return;
6479 for (l = loc; l != NULL; l = l->dw_loc_next)
6481 print_spaces (outfile);
6482 fprintf (outfile, "(%p) %s",
6483 (void *) l,
6484 dwarf_stack_op_name (l->dw_loc_opc));
6485 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6487 fprintf (outfile, " ");
6488 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6490 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6492 fprintf (outfile, ", ");
6493 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6495 fprintf (outfile, "\n");
6499 /* Print the information associated with a given DIE, and its children.
6500 This routine is a debugging aid only. */
6502 static void
6503 print_die (dw_die_ref die, FILE *outfile)
6505 dw_attr_node *a;
6506 dw_die_ref c;
6507 unsigned ix;
6509 print_spaces (outfile);
6510 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6511 die->die_offset, dwarf_tag_name (die->die_tag),
6512 (void*) die);
6513 print_spaces (outfile);
6514 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6515 fprintf (outfile, " offset: %ld", die->die_offset);
6516 fprintf (outfile, " mark: %d\n", die->die_mark);
6518 if (die->comdat_type_p)
6520 print_spaces (outfile);
6521 fprintf (outfile, " signature: ");
6522 print_signature (outfile, die->die_id.die_type_node->signature);
6523 fprintf (outfile, "\n");
6526 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6528 print_spaces (outfile);
6529 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6531 print_attribute (a, true, outfile);
6532 fprintf (outfile, "\n");
6535 if (die->die_child != NULL)
6537 print_indent += 4;
6538 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6539 print_indent -= 4;
6541 if (print_indent == 0)
6542 fprintf (outfile, "\n");
6545 /* Print the list of operations in the LOC location description. */
6547 DEBUG_FUNCTION void
6548 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6550 print_loc_descr (loc, stderr);
6553 /* Print the information collected for a given DIE. */
6555 DEBUG_FUNCTION void
6556 debug_dwarf_die (dw_die_ref die)
6558 print_die (die, stderr);
6561 DEBUG_FUNCTION void
6562 debug (die_struct &ref)
6564 print_die (&ref, stderr);
6567 DEBUG_FUNCTION void
6568 debug (die_struct *ptr)
6570 if (ptr)
6571 debug (*ptr);
6572 else
6573 fprintf (stderr, "<nil>\n");
6577 /* Print all DWARF information collected for the compilation unit.
6578 This routine is a debugging aid only. */
6580 DEBUG_FUNCTION void
6581 debug_dwarf (void)
6583 print_indent = 0;
6584 print_die (comp_unit_die (), stderr);
6587 /* Verify the DIE tree structure. */
6589 DEBUG_FUNCTION void
6590 verify_die (dw_die_ref die)
6592 gcc_assert (!die->die_mark);
6593 if (die->die_parent == NULL
6594 && die->die_sib == NULL)
6595 return;
6596 /* Verify the die_sib list is cyclic. */
6597 dw_die_ref x = die;
6600 x->die_mark = 1;
6601 x = x->die_sib;
6603 while (x && !x->die_mark);
6604 gcc_assert (x == die);
6605 x = die;
6608 /* Verify all dies have the same parent. */
6609 gcc_assert (x->die_parent == die->die_parent);
6610 if (x->die_child)
6612 /* Verify the child has the proper parent and recurse. */
6613 gcc_assert (x->die_child->die_parent == x);
6614 verify_die (x->die_child);
6616 x->die_mark = 0;
6617 x = x->die_sib;
6619 while (x && x->die_mark);
6622 /* Sanity checks on DIEs. */
6624 static void
6625 check_die (dw_die_ref die)
6627 unsigned ix;
6628 dw_attr_node *a;
6629 bool inline_found = false;
6630 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6631 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6634 switch (a->dw_attr)
6636 case DW_AT_inline:
6637 if (a->dw_attr_val.v.val_unsigned)
6638 inline_found = true;
6639 break;
6640 case DW_AT_location:
6641 ++n_location;
6642 break;
6643 case DW_AT_low_pc:
6644 ++n_low_pc;
6645 break;
6646 case DW_AT_high_pc:
6647 ++n_high_pc;
6648 break;
6649 case DW_AT_artificial:
6650 ++n_artificial;
6651 break;
6652 case DW_AT_decl_column:
6653 ++n_decl_column;
6654 break;
6655 case DW_AT_decl_line:
6656 ++n_decl_line;
6657 break;
6658 case DW_AT_decl_file:
6659 ++n_decl_file;
6660 break;
6661 default:
6662 break;
6665 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6666 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6668 fprintf (stderr, "Duplicate attributes in DIE:\n");
6669 debug_dwarf_die (die);
6670 gcc_unreachable ();
6672 if (inline_found)
6674 /* A debugging information entry that is a member of an abstract
6675 instance tree [that has DW_AT_inline] should not contain any
6676 attributes which describe aspects of the subroutine which vary
6677 between distinct inlined expansions or distinct out-of-line
6678 expansions. */
6679 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6680 gcc_assert (a->dw_attr != DW_AT_low_pc
6681 && a->dw_attr != DW_AT_high_pc
6682 && a->dw_attr != DW_AT_location
6683 && a->dw_attr != DW_AT_frame_base
6684 && a->dw_attr != DW_AT_call_all_calls
6685 && a->dw_attr != DW_AT_GNU_all_call_sites);
6689 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6690 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6691 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6693 /* Calculate the checksum of a location expression. */
6695 static inline void
6696 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6698 int tem;
6699 inchash::hash hstate;
6700 hashval_t hash;
6702 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6703 CHECKSUM (tem);
6704 hash_loc_operands (loc, hstate);
6705 hash = hstate.end();
6706 CHECKSUM (hash);
6709 /* Calculate the checksum of an attribute. */
6711 static void
6712 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6714 dw_loc_descr_ref loc;
6715 rtx r;
6717 CHECKSUM (at->dw_attr);
6719 /* We don't care that this was compiled with a different compiler
6720 snapshot; if the output is the same, that's what matters. */
6721 if (at->dw_attr == DW_AT_producer)
6722 return;
6724 switch (AT_class (at))
6726 case dw_val_class_const:
6727 case dw_val_class_const_implicit:
6728 CHECKSUM (at->dw_attr_val.v.val_int);
6729 break;
6730 case dw_val_class_unsigned_const:
6731 case dw_val_class_unsigned_const_implicit:
6732 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6733 break;
6734 case dw_val_class_const_double:
6735 CHECKSUM (at->dw_attr_val.v.val_double);
6736 break;
6737 case dw_val_class_wide_int:
6738 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6739 get_full_len (*at->dw_attr_val.v.val_wide)
6740 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6741 break;
6742 case dw_val_class_vec:
6743 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6744 (at->dw_attr_val.v.val_vec.length
6745 * at->dw_attr_val.v.val_vec.elt_size));
6746 break;
6747 case dw_val_class_flag:
6748 CHECKSUM (at->dw_attr_val.v.val_flag);
6749 break;
6750 case dw_val_class_str:
6751 CHECKSUM_STRING (AT_string (at));
6752 break;
6754 case dw_val_class_addr:
6755 r = AT_addr (at);
6756 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6757 CHECKSUM_STRING (XSTR (r, 0));
6758 break;
6760 case dw_val_class_offset:
6761 CHECKSUM (at->dw_attr_val.v.val_offset);
6762 break;
6764 case dw_val_class_loc:
6765 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6766 loc_checksum (loc, ctx);
6767 break;
6769 case dw_val_class_die_ref:
6770 die_checksum (AT_ref (at), ctx, mark);
6771 break;
6773 case dw_val_class_fde_ref:
6774 case dw_val_class_vms_delta:
6775 case dw_val_class_lbl_id:
6776 case dw_val_class_lineptr:
6777 case dw_val_class_macptr:
6778 case dw_val_class_loclistsptr:
6779 case dw_val_class_high_pc:
6780 break;
6782 case dw_val_class_file:
6783 case dw_val_class_file_implicit:
6784 CHECKSUM_STRING (AT_file (at)->filename);
6785 break;
6787 case dw_val_class_data8:
6788 CHECKSUM (at->dw_attr_val.v.val_data8);
6789 break;
6791 default:
6792 break;
6796 /* Calculate the checksum of a DIE. */
6798 static void
6799 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6801 dw_die_ref c;
6802 dw_attr_node *a;
6803 unsigned ix;
6805 /* To avoid infinite recursion. */
6806 if (die->die_mark)
6808 CHECKSUM (die->die_mark);
6809 return;
6811 die->die_mark = ++(*mark);
6813 CHECKSUM (die->die_tag);
6815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6816 attr_checksum (a, ctx, mark);
6818 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6821 #undef CHECKSUM
6822 #undef CHECKSUM_BLOCK
6823 #undef CHECKSUM_STRING
6825 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6826 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6827 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6828 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6829 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6830 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6831 #define CHECKSUM_ATTR(FOO) \
6832 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6834 /* Calculate the checksum of a number in signed LEB128 format. */
6836 static void
6837 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6839 unsigned char byte;
6840 bool more;
6842 while (1)
6844 byte = (value & 0x7f);
6845 value >>= 7;
6846 more = !((value == 0 && (byte & 0x40) == 0)
6847 || (value == -1 && (byte & 0x40) != 0));
6848 if (more)
6849 byte |= 0x80;
6850 CHECKSUM (byte);
6851 if (!more)
6852 break;
6856 /* Calculate the checksum of a number in unsigned LEB128 format. */
6858 static void
6859 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6861 while (1)
6863 unsigned char byte = (value & 0x7f);
6864 value >>= 7;
6865 if (value != 0)
6866 /* More bytes to follow. */
6867 byte |= 0x80;
6868 CHECKSUM (byte);
6869 if (value == 0)
6870 break;
6874 /* Checksum the context of the DIE. This adds the names of any
6875 surrounding namespaces or structures to the checksum. */
6877 static void
6878 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6880 const char *name;
6881 dw_die_ref spec;
6882 int tag = die->die_tag;
6884 if (tag != DW_TAG_namespace
6885 && tag != DW_TAG_structure_type
6886 && tag != DW_TAG_class_type)
6887 return;
6889 name = get_AT_string (die, DW_AT_name);
6891 spec = get_AT_ref (die, DW_AT_specification);
6892 if (spec != NULL)
6893 die = spec;
6895 if (die->die_parent != NULL)
6896 checksum_die_context (die->die_parent, ctx);
6898 CHECKSUM_ULEB128 ('C');
6899 CHECKSUM_ULEB128 (tag);
6900 if (name != NULL)
6901 CHECKSUM_STRING (name);
6904 /* Calculate the checksum of a location expression. */
6906 static inline void
6907 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6909 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6910 were emitted as a DW_FORM_sdata instead of a location expression. */
6911 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6913 CHECKSUM_ULEB128 (DW_FORM_sdata);
6914 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6915 return;
6918 /* Otherwise, just checksum the raw location expression. */
6919 while (loc != NULL)
6921 inchash::hash hstate;
6922 hashval_t hash;
6924 CHECKSUM_ULEB128 (loc->dtprel);
6925 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6926 hash_loc_operands (loc, hstate);
6927 hash = hstate.end ();
6928 CHECKSUM (hash);
6929 loc = loc->dw_loc_next;
6933 /* Calculate the checksum of an attribute. */
6935 static void
6936 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6937 struct md5_ctx *ctx, int *mark)
6939 dw_loc_descr_ref loc;
6940 rtx r;
6942 if (AT_class (at) == dw_val_class_die_ref)
6944 dw_die_ref target_die = AT_ref (at);
6946 /* For pointer and reference types, we checksum only the (qualified)
6947 name of the target type (if there is a name). For friend entries,
6948 we checksum only the (qualified) name of the target type or function.
6949 This allows the checksum to remain the same whether the target type
6950 is complete or not. */
6951 if ((at->dw_attr == DW_AT_type
6952 && (tag == DW_TAG_pointer_type
6953 || tag == DW_TAG_reference_type
6954 || tag == DW_TAG_rvalue_reference_type
6955 || tag == DW_TAG_ptr_to_member_type))
6956 || (at->dw_attr == DW_AT_friend
6957 && tag == DW_TAG_friend))
6959 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6961 if (name_attr != NULL)
6963 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6965 if (decl == NULL)
6966 decl = target_die;
6967 CHECKSUM_ULEB128 ('N');
6968 CHECKSUM_ULEB128 (at->dw_attr);
6969 if (decl->die_parent != NULL)
6970 checksum_die_context (decl->die_parent, ctx);
6971 CHECKSUM_ULEB128 ('E');
6972 CHECKSUM_STRING (AT_string (name_attr));
6973 return;
6977 /* For all other references to another DIE, we check to see if the
6978 target DIE has already been visited. If it has, we emit a
6979 backward reference; if not, we descend recursively. */
6980 if (target_die->die_mark > 0)
6982 CHECKSUM_ULEB128 ('R');
6983 CHECKSUM_ULEB128 (at->dw_attr);
6984 CHECKSUM_ULEB128 (target_die->die_mark);
6986 else
6988 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6990 if (decl == NULL)
6991 decl = target_die;
6992 target_die->die_mark = ++(*mark);
6993 CHECKSUM_ULEB128 ('T');
6994 CHECKSUM_ULEB128 (at->dw_attr);
6995 if (decl->die_parent != NULL)
6996 checksum_die_context (decl->die_parent, ctx);
6997 die_checksum_ordered (target_die, ctx, mark);
6999 return;
7002 CHECKSUM_ULEB128 ('A');
7003 CHECKSUM_ULEB128 (at->dw_attr);
7005 switch (AT_class (at))
7007 case dw_val_class_const:
7008 case dw_val_class_const_implicit:
7009 CHECKSUM_ULEB128 (DW_FORM_sdata);
7010 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7011 break;
7013 case dw_val_class_unsigned_const:
7014 case dw_val_class_unsigned_const_implicit:
7015 CHECKSUM_ULEB128 (DW_FORM_sdata);
7016 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7017 break;
7019 case dw_val_class_const_double:
7020 CHECKSUM_ULEB128 (DW_FORM_block);
7021 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7022 CHECKSUM (at->dw_attr_val.v.val_double);
7023 break;
7025 case dw_val_class_wide_int:
7026 CHECKSUM_ULEB128 (DW_FORM_block);
7027 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7028 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7029 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7030 get_full_len (*at->dw_attr_val.v.val_wide)
7031 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7032 break;
7034 case dw_val_class_vec:
7035 CHECKSUM_ULEB128 (DW_FORM_block);
7036 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7037 * at->dw_attr_val.v.val_vec.elt_size);
7038 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7039 (at->dw_attr_val.v.val_vec.length
7040 * at->dw_attr_val.v.val_vec.elt_size));
7041 break;
7043 case dw_val_class_flag:
7044 CHECKSUM_ULEB128 (DW_FORM_flag);
7045 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7046 break;
7048 case dw_val_class_str:
7049 CHECKSUM_ULEB128 (DW_FORM_string);
7050 CHECKSUM_STRING (AT_string (at));
7051 break;
7053 case dw_val_class_addr:
7054 r = AT_addr (at);
7055 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7056 CHECKSUM_ULEB128 (DW_FORM_string);
7057 CHECKSUM_STRING (XSTR (r, 0));
7058 break;
7060 case dw_val_class_offset:
7061 CHECKSUM_ULEB128 (DW_FORM_sdata);
7062 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7063 break;
7065 case dw_val_class_loc:
7066 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7067 loc_checksum_ordered (loc, ctx);
7068 break;
7070 case dw_val_class_fde_ref:
7071 case dw_val_class_lbl_id:
7072 case dw_val_class_lineptr:
7073 case dw_val_class_macptr:
7074 case dw_val_class_loclistsptr:
7075 case dw_val_class_high_pc:
7076 break;
7078 case dw_val_class_file:
7079 case dw_val_class_file_implicit:
7080 CHECKSUM_ULEB128 (DW_FORM_string);
7081 CHECKSUM_STRING (AT_file (at)->filename);
7082 break;
7084 case dw_val_class_data8:
7085 CHECKSUM (at->dw_attr_val.v.val_data8);
7086 break;
7088 default:
7089 break;
7093 struct checksum_attributes
7095 dw_attr_node *at_name;
7096 dw_attr_node *at_type;
7097 dw_attr_node *at_friend;
7098 dw_attr_node *at_accessibility;
7099 dw_attr_node *at_address_class;
7100 dw_attr_node *at_alignment;
7101 dw_attr_node *at_allocated;
7102 dw_attr_node *at_artificial;
7103 dw_attr_node *at_associated;
7104 dw_attr_node *at_binary_scale;
7105 dw_attr_node *at_bit_offset;
7106 dw_attr_node *at_bit_size;
7107 dw_attr_node *at_bit_stride;
7108 dw_attr_node *at_byte_size;
7109 dw_attr_node *at_byte_stride;
7110 dw_attr_node *at_const_value;
7111 dw_attr_node *at_containing_type;
7112 dw_attr_node *at_count;
7113 dw_attr_node *at_data_location;
7114 dw_attr_node *at_data_member_location;
7115 dw_attr_node *at_decimal_scale;
7116 dw_attr_node *at_decimal_sign;
7117 dw_attr_node *at_default_value;
7118 dw_attr_node *at_digit_count;
7119 dw_attr_node *at_discr;
7120 dw_attr_node *at_discr_list;
7121 dw_attr_node *at_discr_value;
7122 dw_attr_node *at_encoding;
7123 dw_attr_node *at_endianity;
7124 dw_attr_node *at_explicit;
7125 dw_attr_node *at_is_optional;
7126 dw_attr_node *at_location;
7127 dw_attr_node *at_lower_bound;
7128 dw_attr_node *at_mutable;
7129 dw_attr_node *at_ordering;
7130 dw_attr_node *at_picture_string;
7131 dw_attr_node *at_prototyped;
7132 dw_attr_node *at_small;
7133 dw_attr_node *at_segment;
7134 dw_attr_node *at_string_length;
7135 dw_attr_node *at_string_length_bit_size;
7136 dw_attr_node *at_string_length_byte_size;
7137 dw_attr_node *at_threads_scaled;
7138 dw_attr_node *at_upper_bound;
7139 dw_attr_node *at_use_location;
7140 dw_attr_node *at_use_UTF8;
7141 dw_attr_node *at_variable_parameter;
7142 dw_attr_node *at_virtuality;
7143 dw_attr_node *at_visibility;
7144 dw_attr_node *at_vtable_elem_location;
7147 /* Collect the attributes that we will want to use for the checksum. */
7149 static void
7150 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7152 dw_attr_node *a;
7153 unsigned ix;
7155 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7157 switch (a->dw_attr)
7159 case DW_AT_name:
7160 attrs->at_name = a;
7161 break;
7162 case DW_AT_type:
7163 attrs->at_type = a;
7164 break;
7165 case DW_AT_friend:
7166 attrs->at_friend = a;
7167 break;
7168 case DW_AT_accessibility:
7169 attrs->at_accessibility = a;
7170 break;
7171 case DW_AT_address_class:
7172 attrs->at_address_class = a;
7173 break;
7174 case DW_AT_alignment:
7175 attrs->at_alignment = a;
7176 break;
7177 case DW_AT_allocated:
7178 attrs->at_allocated = a;
7179 break;
7180 case DW_AT_artificial:
7181 attrs->at_artificial = a;
7182 break;
7183 case DW_AT_associated:
7184 attrs->at_associated = a;
7185 break;
7186 case DW_AT_binary_scale:
7187 attrs->at_binary_scale = a;
7188 break;
7189 case DW_AT_bit_offset:
7190 attrs->at_bit_offset = a;
7191 break;
7192 case DW_AT_bit_size:
7193 attrs->at_bit_size = a;
7194 break;
7195 case DW_AT_bit_stride:
7196 attrs->at_bit_stride = a;
7197 break;
7198 case DW_AT_byte_size:
7199 attrs->at_byte_size = a;
7200 break;
7201 case DW_AT_byte_stride:
7202 attrs->at_byte_stride = a;
7203 break;
7204 case DW_AT_const_value:
7205 attrs->at_const_value = a;
7206 break;
7207 case DW_AT_containing_type:
7208 attrs->at_containing_type = a;
7209 break;
7210 case DW_AT_count:
7211 attrs->at_count = a;
7212 break;
7213 case DW_AT_data_location:
7214 attrs->at_data_location = a;
7215 break;
7216 case DW_AT_data_member_location:
7217 attrs->at_data_member_location = a;
7218 break;
7219 case DW_AT_decimal_scale:
7220 attrs->at_decimal_scale = a;
7221 break;
7222 case DW_AT_decimal_sign:
7223 attrs->at_decimal_sign = a;
7224 break;
7225 case DW_AT_default_value:
7226 attrs->at_default_value = a;
7227 break;
7228 case DW_AT_digit_count:
7229 attrs->at_digit_count = a;
7230 break;
7231 case DW_AT_discr:
7232 attrs->at_discr = a;
7233 break;
7234 case DW_AT_discr_list:
7235 attrs->at_discr_list = a;
7236 break;
7237 case DW_AT_discr_value:
7238 attrs->at_discr_value = a;
7239 break;
7240 case DW_AT_encoding:
7241 attrs->at_encoding = a;
7242 break;
7243 case DW_AT_endianity:
7244 attrs->at_endianity = a;
7245 break;
7246 case DW_AT_explicit:
7247 attrs->at_explicit = a;
7248 break;
7249 case DW_AT_is_optional:
7250 attrs->at_is_optional = a;
7251 break;
7252 case DW_AT_location:
7253 attrs->at_location = a;
7254 break;
7255 case DW_AT_lower_bound:
7256 attrs->at_lower_bound = a;
7257 break;
7258 case DW_AT_mutable:
7259 attrs->at_mutable = a;
7260 break;
7261 case DW_AT_ordering:
7262 attrs->at_ordering = a;
7263 break;
7264 case DW_AT_picture_string:
7265 attrs->at_picture_string = a;
7266 break;
7267 case DW_AT_prototyped:
7268 attrs->at_prototyped = a;
7269 break;
7270 case DW_AT_small:
7271 attrs->at_small = a;
7272 break;
7273 case DW_AT_segment:
7274 attrs->at_segment = a;
7275 break;
7276 case DW_AT_string_length:
7277 attrs->at_string_length = a;
7278 break;
7279 case DW_AT_string_length_bit_size:
7280 attrs->at_string_length_bit_size = a;
7281 break;
7282 case DW_AT_string_length_byte_size:
7283 attrs->at_string_length_byte_size = a;
7284 break;
7285 case DW_AT_threads_scaled:
7286 attrs->at_threads_scaled = a;
7287 break;
7288 case DW_AT_upper_bound:
7289 attrs->at_upper_bound = a;
7290 break;
7291 case DW_AT_use_location:
7292 attrs->at_use_location = a;
7293 break;
7294 case DW_AT_use_UTF8:
7295 attrs->at_use_UTF8 = a;
7296 break;
7297 case DW_AT_variable_parameter:
7298 attrs->at_variable_parameter = a;
7299 break;
7300 case DW_AT_virtuality:
7301 attrs->at_virtuality = a;
7302 break;
7303 case DW_AT_visibility:
7304 attrs->at_visibility = a;
7305 break;
7306 case DW_AT_vtable_elem_location:
7307 attrs->at_vtable_elem_location = a;
7308 break;
7309 default:
7310 break;
7315 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7317 static void
7318 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7320 dw_die_ref c;
7321 dw_die_ref decl;
7322 struct checksum_attributes attrs;
7324 CHECKSUM_ULEB128 ('D');
7325 CHECKSUM_ULEB128 (die->die_tag);
7327 memset (&attrs, 0, sizeof (attrs));
7329 decl = get_AT_ref (die, DW_AT_specification);
7330 if (decl != NULL)
7331 collect_checksum_attributes (&attrs, decl);
7332 collect_checksum_attributes (&attrs, die);
7334 CHECKSUM_ATTR (attrs.at_name);
7335 CHECKSUM_ATTR (attrs.at_accessibility);
7336 CHECKSUM_ATTR (attrs.at_address_class);
7337 CHECKSUM_ATTR (attrs.at_allocated);
7338 CHECKSUM_ATTR (attrs.at_artificial);
7339 CHECKSUM_ATTR (attrs.at_associated);
7340 CHECKSUM_ATTR (attrs.at_binary_scale);
7341 CHECKSUM_ATTR (attrs.at_bit_offset);
7342 CHECKSUM_ATTR (attrs.at_bit_size);
7343 CHECKSUM_ATTR (attrs.at_bit_stride);
7344 CHECKSUM_ATTR (attrs.at_byte_size);
7345 CHECKSUM_ATTR (attrs.at_byte_stride);
7346 CHECKSUM_ATTR (attrs.at_const_value);
7347 CHECKSUM_ATTR (attrs.at_containing_type);
7348 CHECKSUM_ATTR (attrs.at_count);
7349 CHECKSUM_ATTR (attrs.at_data_location);
7350 CHECKSUM_ATTR (attrs.at_data_member_location);
7351 CHECKSUM_ATTR (attrs.at_decimal_scale);
7352 CHECKSUM_ATTR (attrs.at_decimal_sign);
7353 CHECKSUM_ATTR (attrs.at_default_value);
7354 CHECKSUM_ATTR (attrs.at_digit_count);
7355 CHECKSUM_ATTR (attrs.at_discr);
7356 CHECKSUM_ATTR (attrs.at_discr_list);
7357 CHECKSUM_ATTR (attrs.at_discr_value);
7358 CHECKSUM_ATTR (attrs.at_encoding);
7359 CHECKSUM_ATTR (attrs.at_endianity);
7360 CHECKSUM_ATTR (attrs.at_explicit);
7361 CHECKSUM_ATTR (attrs.at_is_optional);
7362 CHECKSUM_ATTR (attrs.at_location);
7363 CHECKSUM_ATTR (attrs.at_lower_bound);
7364 CHECKSUM_ATTR (attrs.at_mutable);
7365 CHECKSUM_ATTR (attrs.at_ordering);
7366 CHECKSUM_ATTR (attrs.at_picture_string);
7367 CHECKSUM_ATTR (attrs.at_prototyped);
7368 CHECKSUM_ATTR (attrs.at_small);
7369 CHECKSUM_ATTR (attrs.at_segment);
7370 CHECKSUM_ATTR (attrs.at_string_length);
7371 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7372 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7373 CHECKSUM_ATTR (attrs.at_threads_scaled);
7374 CHECKSUM_ATTR (attrs.at_upper_bound);
7375 CHECKSUM_ATTR (attrs.at_use_location);
7376 CHECKSUM_ATTR (attrs.at_use_UTF8);
7377 CHECKSUM_ATTR (attrs.at_variable_parameter);
7378 CHECKSUM_ATTR (attrs.at_virtuality);
7379 CHECKSUM_ATTR (attrs.at_visibility);
7380 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7381 CHECKSUM_ATTR (attrs.at_type);
7382 CHECKSUM_ATTR (attrs.at_friend);
7383 CHECKSUM_ATTR (attrs.at_alignment);
7385 /* Checksum the child DIEs. */
7386 c = die->die_child;
7387 if (c) do {
7388 dw_attr_node *name_attr;
7390 c = c->die_sib;
7391 name_attr = get_AT (c, DW_AT_name);
7392 if (is_template_instantiation (c))
7394 /* Ignore instantiations of member type and function templates. */
7396 else if (name_attr != NULL
7397 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7399 /* Use a shallow checksum for named nested types and member
7400 functions. */
7401 CHECKSUM_ULEB128 ('S');
7402 CHECKSUM_ULEB128 (c->die_tag);
7403 CHECKSUM_STRING (AT_string (name_attr));
7405 else
7407 /* Use a deep checksum for other children. */
7408 /* Mark this DIE so it gets processed when unmarking. */
7409 if (c->die_mark == 0)
7410 c->die_mark = -1;
7411 die_checksum_ordered (c, ctx, mark);
7413 } while (c != die->die_child);
7415 CHECKSUM_ULEB128 (0);
7418 /* Add a type name and tag to a hash. */
7419 static void
7420 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7422 CHECKSUM_ULEB128 (tag);
7423 CHECKSUM_STRING (name);
7426 #undef CHECKSUM
7427 #undef CHECKSUM_STRING
7428 #undef CHECKSUM_ATTR
7429 #undef CHECKSUM_LEB128
7430 #undef CHECKSUM_ULEB128
7432 /* Generate the type signature for DIE. This is computed by generating an
7433 MD5 checksum over the DIE's tag, its relevant attributes, and its
7434 children. Attributes that are references to other DIEs are processed
7435 by recursion, using the MARK field to prevent infinite recursion.
7436 If the DIE is nested inside a namespace or another type, we also
7437 need to include that context in the signature. The lower 64 bits
7438 of the resulting MD5 checksum comprise the signature. */
7440 static void
7441 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7443 int mark;
7444 const char *name;
7445 unsigned char checksum[16];
7446 struct md5_ctx ctx;
7447 dw_die_ref decl;
7448 dw_die_ref parent;
7450 name = get_AT_string (die, DW_AT_name);
7451 decl = get_AT_ref (die, DW_AT_specification);
7452 parent = get_die_parent (die);
7454 /* First, compute a signature for just the type name (and its surrounding
7455 context, if any. This is stored in the type unit DIE for link-time
7456 ODR (one-definition rule) checking. */
7458 if (is_cxx () && name != NULL)
7460 md5_init_ctx (&ctx);
7462 /* Checksum the names of surrounding namespaces and structures. */
7463 if (parent != NULL)
7464 checksum_die_context (parent, &ctx);
7466 /* Checksum the current DIE. */
7467 die_odr_checksum (die->die_tag, name, &ctx);
7468 md5_finish_ctx (&ctx, checksum);
7470 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7473 /* Next, compute the complete type signature. */
7475 md5_init_ctx (&ctx);
7476 mark = 1;
7477 die->die_mark = mark;
7479 /* Checksum the names of surrounding namespaces and structures. */
7480 if (parent != NULL)
7481 checksum_die_context (parent, &ctx);
7483 /* Checksum the DIE and its children. */
7484 die_checksum_ordered (die, &ctx, &mark);
7485 unmark_all_dies (die);
7486 md5_finish_ctx (&ctx, checksum);
7488 /* Store the signature in the type node and link the type DIE and the
7489 type node together. */
7490 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7491 DWARF_TYPE_SIGNATURE_SIZE);
7492 die->comdat_type_p = true;
7493 die->die_id.die_type_node = type_node;
7494 type_node->type_die = die;
7496 /* If the DIE is a specification, link its declaration to the type node
7497 as well. */
7498 if (decl != NULL)
7500 decl->comdat_type_p = true;
7501 decl->die_id.die_type_node = type_node;
7505 /* Do the location expressions look same? */
7506 static inline int
7507 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7509 return loc1->dw_loc_opc == loc2->dw_loc_opc
7510 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7511 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7514 /* Do the values look the same? */
7515 static int
7516 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7518 dw_loc_descr_ref loc1, loc2;
7519 rtx r1, r2;
7521 if (v1->val_class != v2->val_class)
7522 return 0;
7524 switch (v1->val_class)
7526 case dw_val_class_const:
7527 case dw_val_class_const_implicit:
7528 return v1->v.val_int == v2->v.val_int;
7529 case dw_val_class_unsigned_const:
7530 case dw_val_class_unsigned_const_implicit:
7531 return v1->v.val_unsigned == v2->v.val_unsigned;
7532 case dw_val_class_const_double:
7533 return v1->v.val_double.high == v2->v.val_double.high
7534 && v1->v.val_double.low == v2->v.val_double.low;
7535 case dw_val_class_wide_int:
7536 return *v1->v.val_wide == *v2->v.val_wide;
7537 case dw_val_class_vec:
7538 if (v1->v.val_vec.length != v2->v.val_vec.length
7539 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7540 return 0;
7541 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7542 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7543 return 0;
7544 return 1;
7545 case dw_val_class_flag:
7546 return v1->v.val_flag == v2->v.val_flag;
7547 case dw_val_class_str:
7548 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7550 case dw_val_class_addr:
7551 r1 = v1->v.val_addr;
7552 r2 = v2->v.val_addr;
7553 if (GET_CODE (r1) != GET_CODE (r2))
7554 return 0;
7555 return !rtx_equal_p (r1, r2);
7557 case dw_val_class_offset:
7558 return v1->v.val_offset == v2->v.val_offset;
7560 case dw_val_class_loc:
7561 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7562 loc1 && loc2;
7563 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7564 if (!same_loc_p (loc1, loc2, mark))
7565 return 0;
7566 return !loc1 && !loc2;
7568 case dw_val_class_die_ref:
7569 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7571 case dw_val_class_fde_ref:
7572 case dw_val_class_vms_delta:
7573 case dw_val_class_lbl_id:
7574 case dw_val_class_lineptr:
7575 case dw_val_class_macptr:
7576 case dw_val_class_loclistsptr:
7577 case dw_val_class_high_pc:
7578 return 1;
7580 case dw_val_class_file:
7581 case dw_val_class_file_implicit:
7582 return v1->v.val_file == v2->v.val_file;
7584 case dw_val_class_data8:
7585 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7587 default:
7588 return 1;
7592 /* Do the attributes look the same? */
7594 static int
7595 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7597 if (at1->dw_attr != at2->dw_attr)
7598 return 0;
7600 /* We don't care that this was compiled with a different compiler
7601 snapshot; if the output is the same, that's what matters. */
7602 if (at1->dw_attr == DW_AT_producer)
7603 return 1;
7605 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7608 /* Do the dies look the same? */
7610 static int
7611 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7613 dw_die_ref c1, c2;
7614 dw_attr_node *a1;
7615 unsigned ix;
7617 /* To avoid infinite recursion. */
7618 if (die1->die_mark)
7619 return die1->die_mark == die2->die_mark;
7620 die1->die_mark = die2->die_mark = ++(*mark);
7622 if (die1->die_tag != die2->die_tag)
7623 return 0;
7625 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7626 return 0;
7628 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7629 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7630 return 0;
7632 c1 = die1->die_child;
7633 c2 = die2->die_child;
7634 if (! c1)
7636 if (c2)
7637 return 0;
7639 else
7640 for (;;)
7642 if (!same_die_p (c1, c2, mark))
7643 return 0;
7644 c1 = c1->die_sib;
7645 c2 = c2->die_sib;
7646 if (c1 == die1->die_child)
7648 if (c2 == die2->die_child)
7649 break;
7650 else
7651 return 0;
7655 return 1;
7658 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7659 children, and set die_symbol. */
7661 static void
7662 compute_comp_unit_symbol (dw_die_ref unit_die)
7664 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7665 const char *base = die_name ? lbasename (die_name) : "anonymous";
7666 char *name = XALLOCAVEC (char, strlen (base) + 64);
7667 char *p;
7668 int i, mark;
7669 unsigned char checksum[16];
7670 struct md5_ctx ctx;
7672 /* Compute the checksum of the DIE, then append part of it as hex digits to
7673 the name filename of the unit. */
7675 md5_init_ctx (&ctx);
7676 mark = 0;
7677 die_checksum (unit_die, &ctx, &mark);
7678 unmark_all_dies (unit_die);
7679 md5_finish_ctx (&ctx, checksum);
7681 /* When we this for comp_unit_die () we have a DW_AT_name that might
7682 not start with a letter but with anything valid for filenames and
7683 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7684 character is not a letter. */
7685 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7686 clean_symbol_name (name);
7688 p = name + strlen (name);
7689 for (i = 0; i < 4; i++)
7691 sprintf (p, "%.2x", checksum[i]);
7692 p += 2;
7695 unit_die->die_id.die_symbol = xstrdup (name);
7698 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7700 static int
7701 is_type_die (dw_die_ref die)
7703 switch (die->die_tag)
7705 case DW_TAG_array_type:
7706 case DW_TAG_class_type:
7707 case DW_TAG_interface_type:
7708 case DW_TAG_enumeration_type:
7709 case DW_TAG_pointer_type:
7710 case DW_TAG_reference_type:
7711 case DW_TAG_rvalue_reference_type:
7712 case DW_TAG_string_type:
7713 case DW_TAG_structure_type:
7714 case DW_TAG_subroutine_type:
7715 case DW_TAG_union_type:
7716 case DW_TAG_ptr_to_member_type:
7717 case DW_TAG_set_type:
7718 case DW_TAG_subrange_type:
7719 case DW_TAG_base_type:
7720 case DW_TAG_const_type:
7721 case DW_TAG_file_type:
7722 case DW_TAG_packed_type:
7723 case DW_TAG_volatile_type:
7724 case DW_TAG_typedef:
7725 return 1;
7726 default:
7727 return 0;
7731 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7732 Basically, we want to choose the bits that are likely to be shared between
7733 compilations (types) and leave out the bits that are specific to individual
7734 compilations (functions). */
7736 static int
7737 is_comdat_die (dw_die_ref c)
7739 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7740 we do for stabs. The advantage is a greater likelihood of sharing between
7741 objects that don't include headers in the same order (and therefore would
7742 put the base types in a different comdat). jason 8/28/00 */
7744 if (c->die_tag == DW_TAG_base_type)
7745 return 0;
7747 if (c->die_tag == DW_TAG_pointer_type
7748 || c->die_tag == DW_TAG_reference_type
7749 || c->die_tag == DW_TAG_rvalue_reference_type
7750 || c->die_tag == DW_TAG_const_type
7751 || c->die_tag == DW_TAG_volatile_type)
7753 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7755 return t ? is_comdat_die (t) : 0;
7758 return is_type_die (c);
7761 /* Returns true iff C is a compile-unit DIE. */
7763 static inline bool
7764 is_cu_die (dw_die_ref c)
7766 return c && (c->die_tag == DW_TAG_compile_unit
7767 || c->die_tag == DW_TAG_skeleton_unit);
7770 /* Returns true iff C is a unit DIE of some sort. */
7772 static inline bool
7773 is_unit_die (dw_die_ref c)
7775 return c && (c->die_tag == DW_TAG_compile_unit
7776 || c->die_tag == DW_TAG_partial_unit
7777 || c->die_tag == DW_TAG_type_unit
7778 || c->die_tag == DW_TAG_skeleton_unit);
7781 /* Returns true iff C is a namespace DIE. */
7783 static inline bool
7784 is_namespace_die (dw_die_ref c)
7786 return c && c->die_tag == DW_TAG_namespace;
7789 /* Returns true iff C is a class or structure DIE. */
7791 static inline bool
7792 is_class_die (dw_die_ref c)
7794 return c && (c->die_tag == DW_TAG_class_type
7795 || c->die_tag == DW_TAG_structure_type);
7798 /* Return non-zero if this DIE is a template parameter. */
7800 static inline bool
7801 is_template_parameter (dw_die_ref die)
7803 switch (die->die_tag)
7805 case DW_TAG_template_type_param:
7806 case DW_TAG_template_value_param:
7807 case DW_TAG_GNU_template_template_param:
7808 case DW_TAG_GNU_template_parameter_pack:
7809 return true;
7810 default:
7811 return false;
7815 /* Return non-zero if this DIE represents a template instantiation. */
7817 static inline bool
7818 is_template_instantiation (dw_die_ref die)
7820 dw_die_ref c;
7822 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7823 return false;
7824 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7825 return false;
7828 static char *
7829 gen_internal_sym (const char *prefix)
7831 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7833 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7834 return xstrdup (buf);
7837 /* Return non-zero if this DIE is a declaration. */
7839 static int
7840 is_declaration_die (dw_die_ref die)
7842 dw_attr_node *a;
7843 unsigned ix;
7845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7846 if (a->dw_attr == DW_AT_declaration)
7847 return 1;
7849 return 0;
7852 /* Return non-zero if this DIE is nested inside a subprogram. */
7854 static int
7855 is_nested_in_subprogram (dw_die_ref die)
7857 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7859 if (decl == NULL)
7860 decl = die;
7861 return local_scope_p (decl);
7864 /* Return non-zero if this DIE contains a defining declaration of a
7865 subprogram. */
7867 static int
7868 contains_subprogram_definition (dw_die_ref die)
7870 dw_die_ref c;
7872 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7873 return 1;
7874 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7875 return 0;
7878 /* Return non-zero if this is a type DIE that should be moved to a
7879 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7880 unit type. */
7882 static int
7883 should_move_die_to_comdat (dw_die_ref die)
7885 switch (die->die_tag)
7887 case DW_TAG_class_type:
7888 case DW_TAG_structure_type:
7889 case DW_TAG_enumeration_type:
7890 case DW_TAG_union_type:
7891 /* Don't move declarations, inlined instances, types nested in a
7892 subprogram, or types that contain subprogram definitions. */
7893 if (is_declaration_die (die)
7894 || get_AT (die, DW_AT_abstract_origin)
7895 || is_nested_in_subprogram (die)
7896 || contains_subprogram_definition (die))
7897 return 0;
7898 return 1;
7899 case DW_TAG_array_type:
7900 case DW_TAG_interface_type:
7901 case DW_TAG_pointer_type:
7902 case DW_TAG_reference_type:
7903 case DW_TAG_rvalue_reference_type:
7904 case DW_TAG_string_type:
7905 case DW_TAG_subroutine_type:
7906 case DW_TAG_ptr_to_member_type:
7907 case DW_TAG_set_type:
7908 case DW_TAG_subrange_type:
7909 case DW_TAG_base_type:
7910 case DW_TAG_const_type:
7911 case DW_TAG_file_type:
7912 case DW_TAG_packed_type:
7913 case DW_TAG_volatile_type:
7914 case DW_TAG_typedef:
7915 default:
7916 return 0;
7920 /* Make a clone of DIE. */
7922 static dw_die_ref
7923 clone_die (dw_die_ref die)
7925 dw_die_ref clone = new_die_raw (die->die_tag);
7926 dw_attr_node *a;
7927 unsigned ix;
7929 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7930 add_dwarf_attr (clone, a);
7932 return clone;
7935 /* Make a clone of the tree rooted at DIE. */
7937 static dw_die_ref
7938 clone_tree (dw_die_ref die)
7940 dw_die_ref c;
7941 dw_die_ref clone = clone_die (die);
7943 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7945 return clone;
7948 /* Make a clone of DIE as a declaration. */
7950 static dw_die_ref
7951 clone_as_declaration (dw_die_ref die)
7953 dw_die_ref clone;
7954 dw_die_ref decl;
7955 dw_attr_node *a;
7956 unsigned ix;
7958 /* If the DIE is already a declaration, just clone it. */
7959 if (is_declaration_die (die))
7960 return clone_die (die);
7962 /* If the DIE is a specification, just clone its declaration DIE. */
7963 decl = get_AT_ref (die, DW_AT_specification);
7964 if (decl != NULL)
7966 clone = clone_die (decl);
7967 if (die->comdat_type_p)
7968 add_AT_die_ref (clone, DW_AT_signature, die);
7969 return clone;
7972 clone = new_die_raw (die->die_tag);
7974 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7976 /* We don't want to copy over all attributes.
7977 For example we don't want DW_AT_byte_size because otherwise we will no
7978 longer have a declaration and GDB will treat it as a definition. */
7980 switch (a->dw_attr)
7982 case DW_AT_abstract_origin:
7983 case DW_AT_artificial:
7984 case DW_AT_containing_type:
7985 case DW_AT_external:
7986 case DW_AT_name:
7987 case DW_AT_type:
7988 case DW_AT_virtuality:
7989 case DW_AT_linkage_name:
7990 case DW_AT_MIPS_linkage_name:
7991 add_dwarf_attr (clone, a);
7992 break;
7993 case DW_AT_byte_size:
7994 case DW_AT_alignment:
7995 default:
7996 break;
8000 if (die->comdat_type_p)
8001 add_AT_die_ref (clone, DW_AT_signature, die);
8003 add_AT_flag (clone, DW_AT_declaration, 1);
8004 return clone;
8008 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8010 struct decl_table_entry
8012 dw_die_ref orig;
8013 dw_die_ref copy;
8016 /* Helpers to manipulate hash table of copied declarations. */
8018 /* Hashtable helpers. */
8020 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8022 typedef die_struct *compare_type;
8023 static inline hashval_t hash (const decl_table_entry *);
8024 static inline bool equal (const decl_table_entry *, const die_struct *);
8027 inline hashval_t
8028 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8030 return htab_hash_pointer (entry->orig);
8033 inline bool
8034 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8035 const die_struct *entry2)
8037 return entry1->orig == entry2;
8040 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8042 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8043 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8044 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8045 to check if the ancestor has already been copied into UNIT. */
8047 static dw_die_ref
8048 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8049 decl_hash_type *decl_table)
8051 dw_die_ref parent = die->die_parent;
8052 dw_die_ref new_parent = unit;
8053 dw_die_ref copy;
8054 decl_table_entry **slot = NULL;
8055 struct decl_table_entry *entry = NULL;
8057 if (decl_table)
8059 /* Check if the entry has already been copied to UNIT. */
8060 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8061 INSERT);
8062 if (*slot != HTAB_EMPTY_ENTRY)
8064 entry = *slot;
8065 return entry->copy;
8068 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8069 entry = XCNEW (struct decl_table_entry);
8070 entry->orig = die;
8071 entry->copy = NULL;
8072 *slot = entry;
8075 if (parent != NULL)
8077 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8078 if (spec != NULL)
8079 parent = spec;
8080 if (!is_unit_die (parent))
8081 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8084 copy = clone_as_declaration (die);
8085 add_child_die (new_parent, copy);
8087 if (decl_table)
8089 /* Record the pointer to the copy. */
8090 entry->copy = copy;
8093 return copy;
8095 /* Copy the declaration context to the new type unit DIE. This includes
8096 any surrounding namespace or type declarations. If the DIE has an
8097 AT_specification attribute, it also includes attributes and children
8098 attached to the specification, and returns a pointer to the original
8099 parent of the declaration DIE. Returns NULL otherwise. */
8101 static dw_die_ref
8102 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8104 dw_die_ref decl;
8105 dw_die_ref new_decl;
8106 dw_die_ref orig_parent = NULL;
8108 decl = get_AT_ref (die, DW_AT_specification);
8109 if (decl == NULL)
8110 decl = die;
8111 else
8113 unsigned ix;
8114 dw_die_ref c;
8115 dw_attr_node *a;
8117 /* The original DIE will be changed to a declaration, and must
8118 be moved to be a child of the original declaration DIE. */
8119 orig_parent = decl->die_parent;
8121 /* Copy the type node pointer from the new DIE to the original
8122 declaration DIE so we can forward references later. */
8123 decl->comdat_type_p = true;
8124 decl->die_id.die_type_node = die->die_id.die_type_node;
8126 remove_AT (die, DW_AT_specification);
8128 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8130 if (a->dw_attr != DW_AT_name
8131 && a->dw_attr != DW_AT_declaration
8132 && a->dw_attr != DW_AT_external)
8133 add_dwarf_attr (die, a);
8136 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8139 if (decl->die_parent != NULL
8140 && !is_unit_die (decl->die_parent))
8142 new_decl = copy_ancestor_tree (unit, decl, NULL);
8143 if (new_decl != NULL)
8145 remove_AT (new_decl, DW_AT_signature);
8146 add_AT_specification (die, new_decl);
8150 return orig_parent;
8153 /* Generate the skeleton ancestor tree for the given NODE, then clone
8154 the DIE and add the clone into the tree. */
8156 static void
8157 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8159 if (node->new_die != NULL)
8160 return;
8162 node->new_die = clone_as_declaration (node->old_die);
8164 if (node->parent != NULL)
8166 generate_skeleton_ancestor_tree (node->parent);
8167 add_child_die (node->parent->new_die, node->new_die);
8171 /* Generate a skeleton tree of DIEs containing any declarations that are
8172 found in the original tree. We traverse the tree looking for declaration
8173 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8175 static void
8176 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8178 skeleton_chain_node node;
8179 dw_die_ref c;
8180 dw_die_ref first;
8181 dw_die_ref prev = NULL;
8182 dw_die_ref next = NULL;
8184 node.parent = parent;
8186 first = c = parent->old_die->die_child;
8187 if (c)
8188 next = c->die_sib;
8189 if (c) do {
8190 if (prev == NULL || prev->die_sib == c)
8191 prev = c;
8192 c = next;
8193 next = (c == first ? NULL : c->die_sib);
8194 node.old_die = c;
8195 node.new_die = NULL;
8196 if (is_declaration_die (c))
8198 if (is_template_instantiation (c))
8200 /* Instantiated templates do not need to be cloned into the
8201 type unit. Just move the DIE and its children back to
8202 the skeleton tree (in the main CU). */
8203 remove_child_with_prev (c, prev);
8204 add_child_die (parent->new_die, c);
8205 c = prev;
8207 else if (c->comdat_type_p)
8209 /* This is the skeleton of earlier break_out_comdat_types
8210 type. Clone the existing DIE, but keep the children
8211 under the original (which is in the main CU). */
8212 dw_die_ref clone = clone_die (c);
8214 replace_child (c, clone, prev);
8215 generate_skeleton_ancestor_tree (parent);
8216 add_child_die (parent->new_die, c);
8217 c = clone;
8218 continue;
8220 else
8222 /* Clone the existing DIE, move the original to the skeleton
8223 tree (which is in the main CU), and put the clone, with
8224 all the original's children, where the original came from
8225 (which is about to be moved to the type unit). */
8226 dw_die_ref clone = clone_die (c);
8227 move_all_children (c, clone);
8229 /* If the original has a DW_AT_object_pointer attribute,
8230 it would now point to a child DIE just moved to the
8231 cloned tree, so we need to remove that attribute from
8232 the original. */
8233 remove_AT (c, DW_AT_object_pointer);
8235 replace_child (c, clone, prev);
8236 generate_skeleton_ancestor_tree (parent);
8237 add_child_die (parent->new_die, c);
8238 node.old_die = clone;
8239 node.new_die = c;
8240 c = clone;
8243 generate_skeleton_bottom_up (&node);
8244 } while (next != NULL);
8247 /* Wrapper function for generate_skeleton_bottom_up. */
8249 static dw_die_ref
8250 generate_skeleton (dw_die_ref die)
8252 skeleton_chain_node node;
8254 node.old_die = die;
8255 node.new_die = NULL;
8256 node.parent = NULL;
8258 /* If this type definition is nested inside another type,
8259 and is not an instantiation of a template, always leave
8260 at least a declaration in its place. */
8261 if (die->die_parent != NULL
8262 && is_type_die (die->die_parent)
8263 && !is_template_instantiation (die))
8264 node.new_die = clone_as_declaration (die);
8266 generate_skeleton_bottom_up (&node);
8267 return node.new_die;
8270 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8271 declaration. The original DIE is moved to a new compile unit so that
8272 existing references to it follow it to the new location. If any of the
8273 original DIE's descendants is a declaration, we need to replace the
8274 original DIE with a skeleton tree and move the declarations back into the
8275 skeleton tree. */
8277 static dw_die_ref
8278 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8279 dw_die_ref prev)
8281 dw_die_ref skeleton, orig_parent;
8283 /* Copy the declaration context to the type unit DIE. If the returned
8284 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8285 that DIE. */
8286 orig_parent = copy_declaration_context (unit, child);
8288 skeleton = generate_skeleton (child);
8289 if (skeleton == NULL)
8290 remove_child_with_prev (child, prev);
8291 else
8293 skeleton->comdat_type_p = true;
8294 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8296 /* If the original DIE was a specification, we need to put
8297 the skeleton under the parent DIE of the declaration.
8298 This leaves the original declaration in the tree, but
8299 it will be pruned later since there are no longer any
8300 references to it. */
8301 if (orig_parent != NULL)
8303 remove_child_with_prev (child, prev);
8304 add_child_die (orig_parent, skeleton);
8306 else
8307 replace_child (child, skeleton, prev);
8310 return skeleton;
8313 static void
8314 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8315 comdat_type_node *type_node,
8316 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8318 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8319 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8320 DWARF procedure references in the DW_AT_location attribute. */
8322 static dw_die_ref
8323 copy_dwarf_procedure (dw_die_ref die,
8324 comdat_type_node *type_node,
8325 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8327 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8329 /* DWARF procedures are not supposed to have children... */
8330 gcc_assert (die->die_child == NULL);
8332 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8333 gcc_assert (vec_safe_length (die->die_attr) == 1
8334 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8336 /* Do not copy more than once DWARF procedures. */
8337 bool existed;
8338 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8339 if (existed)
8340 return die_copy;
8342 die_copy = clone_die (die);
8343 add_child_die (type_node->root_die, die_copy);
8344 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8345 return die_copy;
8348 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8349 procedures in DIE's attributes. */
8351 static void
8352 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8353 comdat_type_node *type_node,
8354 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8356 dw_attr_node *a;
8357 unsigned i;
8359 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8361 dw_loc_descr_ref loc;
8363 if (a->dw_attr_val.val_class != dw_val_class_loc)
8364 continue;
8366 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8368 switch (loc->dw_loc_opc)
8370 case DW_OP_call2:
8371 case DW_OP_call4:
8372 case DW_OP_call_ref:
8373 gcc_assert (loc->dw_loc_oprnd1.val_class
8374 == dw_val_class_die_ref);
8375 loc->dw_loc_oprnd1.v.val_die_ref.die
8376 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8377 type_node,
8378 copied_dwarf_procs);
8380 default:
8381 break;
8387 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8388 rewrite references to point to the copies.
8390 References are looked for in DIE's attributes and recursively in all its
8391 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8392 mapping from old DWARF procedures to their copy. It is used not to copy
8393 twice the same DWARF procedure under TYPE_NODE. */
8395 static void
8396 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8397 comdat_type_node *type_node,
8398 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8400 dw_die_ref c;
8402 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8403 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8404 type_node,
8405 copied_dwarf_procs));
8408 /* Traverse the DIE and set up additional .debug_types or .debug_info
8409 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8410 section. */
8412 static void
8413 break_out_comdat_types (dw_die_ref die)
8415 dw_die_ref c;
8416 dw_die_ref first;
8417 dw_die_ref prev = NULL;
8418 dw_die_ref next = NULL;
8419 dw_die_ref unit = NULL;
8421 first = c = die->die_child;
8422 if (c)
8423 next = c->die_sib;
8424 if (c) do {
8425 if (prev == NULL || prev->die_sib == c)
8426 prev = c;
8427 c = next;
8428 next = (c == first ? NULL : c->die_sib);
8429 if (should_move_die_to_comdat (c))
8431 dw_die_ref replacement;
8432 comdat_type_node *type_node;
8434 /* Break out nested types into their own type units. */
8435 break_out_comdat_types (c);
8437 /* Create a new type unit DIE as the root for the new tree, and
8438 add it to the list of comdat types. */
8439 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8440 add_AT_unsigned (unit, DW_AT_language,
8441 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8442 type_node = ggc_cleared_alloc<comdat_type_node> ();
8443 type_node->root_die = unit;
8444 type_node->next = comdat_type_list;
8445 comdat_type_list = type_node;
8447 /* Generate the type signature. */
8448 generate_type_signature (c, type_node);
8450 /* Copy the declaration context, attributes, and children of the
8451 declaration into the new type unit DIE, then remove this DIE
8452 from the main CU (or replace it with a skeleton if necessary). */
8453 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8454 type_node->skeleton_die = replacement;
8456 /* Add the DIE to the new compunit. */
8457 add_child_die (unit, c);
8459 /* Types can reference DWARF procedures for type size or data location
8460 expressions. Calls in DWARF expressions cannot target procedures
8461 that are not in the same section. So we must copy DWARF procedures
8462 along with this type and then rewrite references to them. */
8463 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8464 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8466 if (replacement != NULL)
8467 c = replacement;
8469 else if (c->die_tag == DW_TAG_namespace
8470 || c->die_tag == DW_TAG_class_type
8471 || c->die_tag == DW_TAG_structure_type
8472 || c->die_tag == DW_TAG_union_type)
8474 /* Look for nested types that can be broken out. */
8475 break_out_comdat_types (c);
8477 } while (next != NULL);
8480 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8481 Enter all the cloned children into the hash table decl_table. */
8483 static dw_die_ref
8484 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8486 dw_die_ref c;
8487 dw_die_ref clone;
8488 struct decl_table_entry *entry;
8489 decl_table_entry **slot;
8491 if (die->die_tag == DW_TAG_subprogram)
8492 clone = clone_as_declaration (die);
8493 else
8494 clone = clone_die (die);
8496 slot = decl_table->find_slot_with_hash (die,
8497 htab_hash_pointer (die), INSERT);
8499 /* Assert that DIE isn't in the hash table yet. If it would be there
8500 before, the ancestors would be necessarily there as well, therefore
8501 clone_tree_partial wouldn't be called. */
8502 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8504 entry = XCNEW (struct decl_table_entry);
8505 entry->orig = die;
8506 entry->copy = clone;
8507 *slot = entry;
8509 if (die->die_tag != DW_TAG_subprogram)
8510 FOR_EACH_CHILD (die, c,
8511 add_child_die (clone, clone_tree_partial (c, decl_table)));
8513 return clone;
8516 /* Walk the DIE and its children, looking for references to incomplete
8517 or trivial types that are unmarked (i.e., that are not in the current
8518 type_unit). */
8520 static void
8521 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8523 dw_die_ref c;
8524 dw_attr_node *a;
8525 unsigned ix;
8527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8529 if (AT_class (a) == dw_val_class_die_ref)
8531 dw_die_ref targ = AT_ref (a);
8532 decl_table_entry **slot;
8533 struct decl_table_entry *entry;
8535 if (targ->die_mark != 0 || targ->comdat_type_p)
8536 continue;
8538 slot = decl_table->find_slot_with_hash (targ,
8539 htab_hash_pointer (targ),
8540 INSERT);
8542 if (*slot != HTAB_EMPTY_ENTRY)
8544 /* TARG has already been copied, so we just need to
8545 modify the reference to point to the copy. */
8546 entry = *slot;
8547 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8549 else
8551 dw_die_ref parent = unit;
8552 dw_die_ref copy = clone_die (targ);
8554 /* Record in DECL_TABLE that TARG has been copied.
8555 Need to do this now, before the recursive call,
8556 because DECL_TABLE may be expanded and SLOT
8557 would no longer be a valid pointer. */
8558 entry = XCNEW (struct decl_table_entry);
8559 entry->orig = targ;
8560 entry->copy = copy;
8561 *slot = entry;
8563 /* If TARG is not a declaration DIE, we need to copy its
8564 children. */
8565 if (!is_declaration_die (targ))
8567 FOR_EACH_CHILD (
8568 targ, c,
8569 add_child_die (copy,
8570 clone_tree_partial (c, decl_table)));
8573 /* Make sure the cloned tree is marked as part of the
8574 type unit. */
8575 mark_dies (copy);
8577 /* If TARG has surrounding context, copy its ancestor tree
8578 into the new type unit. */
8579 if (targ->die_parent != NULL
8580 && !is_unit_die (targ->die_parent))
8581 parent = copy_ancestor_tree (unit, targ->die_parent,
8582 decl_table);
8584 add_child_die (parent, copy);
8585 a->dw_attr_val.v.val_die_ref.die = copy;
8587 /* Make sure the newly-copied DIE is walked. If it was
8588 installed in a previously-added context, it won't
8589 get visited otherwise. */
8590 if (parent != unit)
8592 /* Find the highest point of the newly-added tree,
8593 mark each node along the way, and walk from there. */
8594 parent->die_mark = 1;
8595 while (parent->die_parent
8596 && parent->die_parent->die_mark == 0)
8598 parent = parent->die_parent;
8599 parent->die_mark = 1;
8601 copy_decls_walk (unit, parent, decl_table);
8607 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8610 /* Copy declarations for "unworthy" types into the new comdat section.
8611 Incomplete types, modified types, and certain other types aren't broken
8612 out into comdat sections of their own, so they don't have a signature,
8613 and we need to copy the declaration into the same section so that we
8614 don't have an external reference. */
8616 static void
8617 copy_decls_for_unworthy_types (dw_die_ref unit)
8619 mark_dies (unit);
8620 decl_hash_type decl_table (10);
8621 copy_decls_walk (unit, unit, &decl_table);
8622 unmark_dies (unit);
8625 /* Traverse the DIE and add a sibling attribute if it may have the
8626 effect of speeding up access to siblings. To save some space,
8627 avoid generating sibling attributes for DIE's without children. */
8629 static void
8630 add_sibling_attributes (dw_die_ref die)
8632 dw_die_ref c;
8634 if (! die->die_child)
8635 return;
8637 if (die->die_parent && die != die->die_parent->die_child)
8638 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8640 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8643 /* Output all location lists for the DIE and its children. */
8645 static void
8646 output_location_lists (dw_die_ref die)
8648 dw_die_ref c;
8649 dw_attr_node *a;
8650 unsigned ix;
8652 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8653 if (AT_class (a) == dw_val_class_loc_list)
8654 output_loc_list (AT_loc_list (a));
8656 FOR_EACH_CHILD (die, c, output_location_lists (c));
8659 /* During assign_location_list_indexes and output_loclists_offset the
8660 current index, after it the number of assigned indexes (i.e. how
8661 large the .debug_loclists* offset table should be). */
8662 static unsigned int loc_list_idx;
8664 /* Output all location list offsets for the DIE and its children. */
8666 static void
8667 output_loclists_offsets (dw_die_ref die)
8669 dw_die_ref c;
8670 dw_attr_node *a;
8671 unsigned ix;
8673 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8674 if (AT_class (a) == dw_val_class_loc_list)
8676 dw_loc_list_ref l = AT_loc_list (a);
8677 if (l->offset_emitted)
8678 continue;
8679 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8680 loc_section_label, NULL);
8681 gcc_assert (l->hash == loc_list_idx);
8682 loc_list_idx++;
8683 l->offset_emitted = true;
8686 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8689 /* Recursively set indexes of location lists. */
8691 static void
8692 assign_location_list_indexes (dw_die_ref die)
8694 dw_die_ref c;
8695 dw_attr_node *a;
8696 unsigned ix;
8698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8699 if (AT_class (a) == dw_val_class_loc_list)
8701 dw_loc_list_ref list = AT_loc_list (a);
8702 if (!list->num_assigned)
8704 list->num_assigned = true;
8705 list->hash = loc_list_idx++;
8709 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8712 /* We want to limit the number of external references, because they are
8713 larger than local references: a relocation takes multiple words, and
8714 even a sig8 reference is always eight bytes, whereas a local reference
8715 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8716 So if we encounter multiple external references to the same type DIE, we
8717 make a local typedef stub for it and redirect all references there.
8719 This is the element of the hash table for keeping track of these
8720 references. */
8722 struct external_ref
8724 dw_die_ref type;
8725 dw_die_ref stub;
8726 unsigned n_refs;
8729 /* Hashtable helpers. */
8731 struct external_ref_hasher : free_ptr_hash <external_ref>
8733 static inline hashval_t hash (const external_ref *);
8734 static inline bool equal (const external_ref *, const external_ref *);
8737 inline hashval_t
8738 external_ref_hasher::hash (const external_ref *r)
8740 dw_die_ref die = r->type;
8741 hashval_t h = 0;
8743 /* We can't use the address of the DIE for hashing, because
8744 that will make the order of the stub DIEs non-deterministic. */
8745 if (! die->comdat_type_p)
8746 /* We have a symbol; use it to compute a hash. */
8747 h = htab_hash_string (die->die_id.die_symbol);
8748 else
8750 /* We have a type signature; use a subset of the bits as the hash.
8751 The 8-byte signature is at least as large as hashval_t. */
8752 comdat_type_node *type_node = die->die_id.die_type_node;
8753 memcpy (&h, type_node->signature, sizeof (h));
8755 return h;
8758 inline bool
8759 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8761 return r1->type == r2->type;
8764 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8766 /* Return a pointer to the external_ref for references to DIE. */
8768 static struct external_ref *
8769 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8771 struct external_ref ref, *ref_p;
8772 external_ref **slot;
8774 ref.type = die;
8775 slot = map->find_slot (&ref, INSERT);
8776 if (*slot != HTAB_EMPTY_ENTRY)
8777 return *slot;
8779 ref_p = XCNEW (struct external_ref);
8780 ref_p->type = die;
8781 *slot = ref_p;
8782 return ref_p;
8785 /* Subroutine of optimize_external_refs, below.
8787 If we see a type skeleton, record it as our stub. If we see external
8788 references, remember how many we've seen. */
8790 static void
8791 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8793 dw_die_ref c;
8794 dw_attr_node *a;
8795 unsigned ix;
8796 struct external_ref *ref_p;
8798 if (is_type_die (die)
8799 && (c = get_AT_ref (die, DW_AT_signature)))
8801 /* This is a local skeleton; use it for local references. */
8802 ref_p = lookup_external_ref (map, c);
8803 ref_p->stub = die;
8806 /* Scan the DIE references, and remember any that refer to DIEs from
8807 other CUs (i.e. those which are not marked). */
8808 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8809 if (AT_class (a) == dw_val_class_die_ref
8810 && (c = AT_ref (a))->die_mark == 0
8811 && is_type_die (c))
8813 ref_p = lookup_external_ref (map, c);
8814 ref_p->n_refs++;
8817 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8820 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8821 points to an external_ref, DATA is the CU we're processing. If we don't
8822 already have a local stub, and we have multiple refs, build a stub. */
8825 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8827 struct external_ref *ref_p = *slot;
8829 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8831 /* We have multiple references to this type, so build a small stub.
8832 Both of these forms are a bit dodgy from the perspective of the
8833 DWARF standard, since technically they should have names. */
8834 dw_die_ref cu = data;
8835 dw_die_ref type = ref_p->type;
8836 dw_die_ref stub = NULL;
8838 if (type->comdat_type_p)
8840 /* If we refer to this type via sig8, use AT_signature. */
8841 stub = new_die (type->die_tag, cu, NULL_TREE);
8842 add_AT_die_ref (stub, DW_AT_signature, type);
8844 else
8846 /* Otherwise, use a typedef with no name. */
8847 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8848 add_AT_die_ref (stub, DW_AT_type, type);
8851 stub->die_mark++;
8852 ref_p->stub = stub;
8854 return 1;
8857 /* DIE is a unit; look through all the DIE references to see if there are
8858 any external references to types, and if so, create local stubs for
8859 them which will be applied in build_abbrev_table. This is useful because
8860 references to local DIEs are smaller. */
8862 static external_ref_hash_type *
8863 optimize_external_refs (dw_die_ref die)
8865 external_ref_hash_type *map = new external_ref_hash_type (10);
8866 optimize_external_refs_1 (die, map);
8867 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8868 return map;
8871 /* The following 3 variables are temporaries that are computed only during the
8872 build_abbrev_table call and used and released during the following
8873 optimize_abbrev_table call. */
8875 /* First abbrev_id that can be optimized based on usage. */
8876 static unsigned int abbrev_opt_start;
8878 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8879 abbrev_id smaller than this, because they must be already sized
8880 during build_abbrev_table). */
8881 static unsigned int abbrev_opt_base_type_end;
8883 /* Vector of usage counts during build_abbrev_table. Indexed by
8884 abbrev_id - abbrev_opt_start. */
8885 static vec<unsigned int> abbrev_usage_count;
8887 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8888 static vec<dw_die_ref> sorted_abbrev_dies;
8890 /* The format of each DIE (and its attribute value pairs) is encoded in an
8891 abbreviation table. This routine builds the abbreviation table and assigns
8892 a unique abbreviation id for each abbreviation entry. The children of each
8893 die are visited recursively. */
8895 static void
8896 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8898 unsigned int abbrev_id = 0;
8899 dw_die_ref c;
8900 dw_attr_node *a;
8901 unsigned ix;
8902 dw_die_ref abbrev;
8904 /* Scan the DIE references, and replace any that refer to
8905 DIEs from other CUs (i.e. those which are not marked) with
8906 the local stubs we built in optimize_external_refs. */
8907 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8908 if (AT_class (a) == dw_val_class_die_ref
8909 && (c = AT_ref (a))->die_mark == 0)
8911 struct external_ref *ref_p;
8912 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8914 ref_p = lookup_external_ref (extern_map, c);
8915 if (ref_p->stub && ref_p->stub != die)
8916 change_AT_die_ref (a, ref_p->stub);
8917 else
8918 /* We aren't changing this reference, so mark it external. */
8919 set_AT_ref_external (a, 1);
8922 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8924 dw_attr_node *die_a, *abbrev_a;
8925 unsigned ix;
8926 bool ok = true;
8928 if (abbrev_id == 0)
8929 continue;
8930 if (abbrev->die_tag != die->die_tag)
8931 continue;
8932 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8933 continue;
8935 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8936 continue;
8938 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8940 abbrev_a = &(*abbrev->die_attr)[ix];
8941 if ((abbrev_a->dw_attr != die_a->dw_attr)
8942 || (value_format (abbrev_a) != value_format (die_a)))
8944 ok = false;
8945 break;
8948 if (ok)
8949 break;
8952 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8954 vec_safe_push (abbrev_die_table, die);
8955 if (abbrev_opt_start)
8956 abbrev_usage_count.safe_push (0);
8958 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8960 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8961 sorted_abbrev_dies.safe_push (die);
8964 die->die_abbrev = abbrev_id;
8965 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8968 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8969 by die_abbrev's usage count, from the most commonly used
8970 abbreviation to the least. */
8972 static int
8973 die_abbrev_cmp (const void *p1, const void *p2)
8975 dw_die_ref die1 = *(const dw_die_ref *) p1;
8976 dw_die_ref die2 = *(const dw_die_ref *) p2;
8978 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8979 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8981 if (die1->die_abbrev >= abbrev_opt_base_type_end
8982 && die2->die_abbrev >= abbrev_opt_base_type_end)
8984 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8985 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8986 return -1;
8987 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8988 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8989 return 1;
8992 /* Stabilize the sort. */
8993 if (die1->die_abbrev < die2->die_abbrev)
8994 return -1;
8995 if (die1->die_abbrev > die2->die_abbrev)
8996 return 1;
8998 return 0;
9001 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9002 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9003 into dw_val_class_const_implicit or
9004 dw_val_class_unsigned_const_implicit. */
9006 static void
9007 optimize_implicit_const (unsigned int first_id, unsigned int end,
9008 vec<bool> &implicit_consts)
9010 /* It never makes sense if there is just one DIE using the abbreviation. */
9011 if (end < first_id + 2)
9012 return;
9014 dw_attr_node *a;
9015 unsigned ix, i;
9016 dw_die_ref die = sorted_abbrev_dies[first_id];
9017 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9018 if (implicit_consts[ix])
9020 enum dw_val_class new_class = dw_val_class_none;
9021 switch (AT_class (a))
9023 case dw_val_class_unsigned_const:
9024 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9025 continue;
9027 /* The .debug_abbrev section will grow by
9028 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9029 in all the DIEs using that abbreviation. */
9030 if (constant_size (AT_unsigned (a)) * (end - first_id)
9031 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9032 continue;
9034 new_class = dw_val_class_unsigned_const_implicit;
9035 break;
9037 case dw_val_class_const:
9038 new_class = dw_val_class_const_implicit;
9039 break;
9041 case dw_val_class_file:
9042 new_class = dw_val_class_file_implicit;
9043 break;
9045 default:
9046 continue;
9048 for (i = first_id; i < end; i++)
9049 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9050 = new_class;
9054 /* Attempt to optimize abbreviation table from abbrev_opt_start
9055 abbreviation above. */
9057 static void
9058 optimize_abbrev_table (void)
9060 if (abbrev_opt_start
9061 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9062 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9064 auto_vec<bool, 32> implicit_consts;
9065 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9067 unsigned int abbrev_id = abbrev_opt_start - 1;
9068 unsigned int first_id = ~0U;
9069 unsigned int last_abbrev_id = 0;
9070 unsigned int i;
9071 dw_die_ref die;
9072 if (abbrev_opt_base_type_end > abbrev_opt_start)
9073 abbrev_id = abbrev_opt_base_type_end - 1;
9074 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9075 most commonly used abbreviations come first. */
9076 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9078 dw_attr_node *a;
9079 unsigned ix;
9081 /* If calc_base_type_die_sizes has been called, the CU and
9082 base types after it can't be optimized, because we've already
9083 calculated their DIE offsets. We've sorted them first. */
9084 if (die->die_abbrev < abbrev_opt_base_type_end)
9085 continue;
9086 if (die->die_abbrev != last_abbrev_id)
9088 last_abbrev_id = die->die_abbrev;
9089 if (dwarf_version >= 5 && first_id != ~0U)
9090 optimize_implicit_const (first_id, i, implicit_consts);
9091 abbrev_id++;
9092 (*abbrev_die_table)[abbrev_id] = die;
9093 if (dwarf_version >= 5)
9095 first_id = i;
9096 implicit_consts.truncate (0);
9098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9099 switch (AT_class (a))
9101 case dw_val_class_const:
9102 case dw_val_class_unsigned_const:
9103 case dw_val_class_file:
9104 implicit_consts.safe_push (true);
9105 break;
9106 default:
9107 implicit_consts.safe_push (false);
9108 break;
9112 else if (dwarf_version >= 5)
9114 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9115 if (!implicit_consts[ix])
9116 continue;
9117 else
9119 dw_attr_node *other_a
9120 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9121 if (!dw_val_equal_p (&a->dw_attr_val,
9122 &other_a->dw_attr_val))
9123 implicit_consts[ix] = false;
9126 die->die_abbrev = abbrev_id;
9128 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9129 if (dwarf_version >= 5 && first_id != ~0U)
9130 optimize_implicit_const (first_id, i, implicit_consts);
9133 abbrev_opt_start = 0;
9134 abbrev_opt_base_type_end = 0;
9135 abbrev_usage_count.release ();
9136 sorted_abbrev_dies.release ();
9139 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9141 static int
9142 constant_size (unsigned HOST_WIDE_INT value)
9144 int log;
9146 if (value == 0)
9147 log = 0;
9148 else
9149 log = floor_log2 (value);
9151 log = log / 8;
9152 log = 1 << (floor_log2 (log) + 1);
9154 return log;
9157 /* Return the size of a DIE as it is represented in the
9158 .debug_info section. */
9160 static unsigned long
9161 size_of_die (dw_die_ref die)
9163 unsigned long size = 0;
9164 dw_attr_node *a;
9165 unsigned ix;
9166 enum dwarf_form form;
9168 size += size_of_uleb128 (die->die_abbrev);
9169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9171 switch (AT_class (a))
9173 case dw_val_class_addr:
9174 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9176 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9177 size += size_of_uleb128 (AT_index (a));
9179 else
9180 size += DWARF2_ADDR_SIZE;
9181 break;
9182 case dw_val_class_offset:
9183 size += DWARF_OFFSET_SIZE;
9184 break;
9185 case dw_val_class_loc:
9187 unsigned long lsize = size_of_locs (AT_loc (a));
9189 /* Block length. */
9190 if (dwarf_version >= 4)
9191 size += size_of_uleb128 (lsize);
9192 else
9193 size += constant_size (lsize);
9194 size += lsize;
9196 break;
9197 case dw_val_class_loc_list:
9198 case dw_val_class_view_list:
9199 if (dwarf_split_debug_info && dwarf_version >= 5)
9201 gcc_assert (AT_loc_list (a)->num_assigned);
9202 size += size_of_uleb128 (AT_loc_list (a)->hash);
9204 else
9205 size += DWARF_OFFSET_SIZE;
9206 break;
9207 case dw_val_class_range_list:
9208 if (value_format (a) == DW_FORM_rnglistx)
9210 gcc_assert (rnglist_idx);
9211 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9212 size += size_of_uleb128 (r->idx);
9214 else
9215 size += DWARF_OFFSET_SIZE;
9216 break;
9217 case dw_val_class_const:
9218 size += size_of_sleb128 (AT_int (a));
9219 break;
9220 case dw_val_class_unsigned_const:
9222 int csize = constant_size (AT_unsigned (a));
9223 if (dwarf_version == 3
9224 && a->dw_attr == DW_AT_data_member_location
9225 && csize >= 4)
9226 size += size_of_uleb128 (AT_unsigned (a));
9227 else
9228 size += csize;
9230 break;
9231 case dw_val_class_const_implicit:
9232 case dw_val_class_unsigned_const_implicit:
9233 case dw_val_class_file_implicit:
9234 /* These occupy no size in the DIE, just an extra sleb128 in
9235 .debug_abbrev. */
9236 break;
9237 case dw_val_class_const_double:
9238 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9239 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9240 size++; /* block */
9241 break;
9242 case dw_val_class_wide_int:
9243 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9244 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9245 if (get_full_len (*a->dw_attr_val.v.val_wide)
9246 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9247 size++; /* block */
9248 break;
9249 case dw_val_class_vec:
9250 size += constant_size (a->dw_attr_val.v.val_vec.length
9251 * a->dw_attr_val.v.val_vec.elt_size)
9252 + a->dw_attr_val.v.val_vec.length
9253 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9254 break;
9255 case dw_val_class_flag:
9256 if (dwarf_version >= 4)
9257 /* Currently all add_AT_flag calls pass in 1 as last argument,
9258 so DW_FORM_flag_present can be used. If that ever changes,
9259 we'll need to use DW_FORM_flag and have some optimization
9260 in build_abbrev_table that will change those to
9261 DW_FORM_flag_present if it is set to 1 in all DIEs using
9262 the same abbrev entry. */
9263 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9264 else
9265 size += 1;
9266 break;
9267 case dw_val_class_die_ref:
9268 if (AT_ref_external (a))
9270 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9271 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9272 is sized by target address length, whereas in DWARF3
9273 it's always sized as an offset. */
9274 if (use_debug_types)
9275 size += DWARF_TYPE_SIGNATURE_SIZE;
9276 else if (dwarf_version == 2)
9277 size += DWARF2_ADDR_SIZE;
9278 else
9279 size += DWARF_OFFSET_SIZE;
9281 else
9282 size += DWARF_OFFSET_SIZE;
9283 break;
9284 case dw_val_class_fde_ref:
9285 size += DWARF_OFFSET_SIZE;
9286 break;
9287 case dw_val_class_lbl_id:
9288 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9290 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9291 size += size_of_uleb128 (AT_index (a));
9293 else
9294 size += DWARF2_ADDR_SIZE;
9295 break;
9296 case dw_val_class_lineptr:
9297 case dw_val_class_macptr:
9298 case dw_val_class_loclistsptr:
9299 size += DWARF_OFFSET_SIZE;
9300 break;
9301 case dw_val_class_str:
9302 form = AT_string_form (a);
9303 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9304 size += DWARF_OFFSET_SIZE;
9305 else if (form == DW_FORM_GNU_str_index)
9306 size += size_of_uleb128 (AT_index (a));
9307 else
9308 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9309 break;
9310 case dw_val_class_file:
9311 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9312 break;
9313 case dw_val_class_data8:
9314 size += 8;
9315 break;
9316 case dw_val_class_vms_delta:
9317 size += DWARF_OFFSET_SIZE;
9318 break;
9319 case dw_val_class_high_pc:
9320 size += DWARF2_ADDR_SIZE;
9321 break;
9322 case dw_val_class_discr_value:
9323 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9324 break;
9325 case dw_val_class_discr_list:
9327 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9329 /* This is a block, so we have the block length and then its
9330 data. */
9331 size += constant_size (block_size) + block_size;
9333 break;
9334 default:
9335 gcc_unreachable ();
9339 return size;
9342 /* Size the debugging information associated with a given DIE. Visits the
9343 DIE's children recursively. Updates the global variable next_die_offset, on
9344 each time through. Uses the current value of next_die_offset to update the
9345 die_offset field in each DIE. */
9347 static void
9348 calc_die_sizes (dw_die_ref die)
9350 dw_die_ref c;
9352 gcc_assert (die->die_offset == 0
9353 || (unsigned long int) die->die_offset == next_die_offset);
9354 die->die_offset = next_die_offset;
9355 next_die_offset += size_of_die (die);
9357 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9359 if (die->die_child != NULL)
9360 /* Count the null byte used to terminate sibling lists. */
9361 next_die_offset += 1;
9364 /* Size just the base type children at the start of the CU.
9365 This is needed because build_abbrev needs to size locs
9366 and sizing of type based stack ops needs to know die_offset
9367 values for the base types. */
9369 static void
9370 calc_base_type_die_sizes (void)
9372 unsigned long die_offset = (dwarf_split_debug_info
9373 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9374 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9375 unsigned int i;
9376 dw_die_ref base_type;
9377 #if ENABLE_ASSERT_CHECKING
9378 dw_die_ref prev = comp_unit_die ()->die_child;
9379 #endif
9381 die_offset += size_of_die (comp_unit_die ());
9382 for (i = 0; base_types.iterate (i, &base_type); i++)
9384 #if ENABLE_ASSERT_CHECKING
9385 gcc_assert (base_type->die_offset == 0
9386 && prev->die_sib == base_type
9387 && base_type->die_child == NULL
9388 && base_type->die_abbrev);
9389 prev = base_type;
9390 #endif
9391 if (abbrev_opt_start
9392 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9393 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9394 base_type->die_offset = die_offset;
9395 die_offset += size_of_die (base_type);
9399 /* Set the marks for a die and its children. We do this so
9400 that we know whether or not a reference needs to use FORM_ref_addr; only
9401 DIEs in the same CU will be marked. We used to clear out the offset
9402 and use that as the flag, but ran into ordering problems. */
9404 static void
9405 mark_dies (dw_die_ref die)
9407 dw_die_ref c;
9409 gcc_assert (!die->die_mark);
9411 die->die_mark = 1;
9412 FOR_EACH_CHILD (die, c, mark_dies (c));
9415 /* Clear the marks for a die and its children. */
9417 static void
9418 unmark_dies (dw_die_ref die)
9420 dw_die_ref c;
9422 if (! use_debug_types)
9423 gcc_assert (die->die_mark);
9425 die->die_mark = 0;
9426 FOR_EACH_CHILD (die, c, unmark_dies (c));
9429 /* Clear the marks for a die, its children and referred dies. */
9431 static void
9432 unmark_all_dies (dw_die_ref die)
9434 dw_die_ref c;
9435 dw_attr_node *a;
9436 unsigned ix;
9438 if (!die->die_mark)
9439 return;
9440 die->die_mark = 0;
9442 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9444 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9445 if (AT_class (a) == dw_val_class_die_ref)
9446 unmark_all_dies (AT_ref (a));
9449 /* Calculate if the entry should appear in the final output file. It may be
9450 from a pruned a type. */
9452 static bool
9453 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9455 /* By limiting gnu pubnames to definitions only, gold can generate a
9456 gdb index without entries for declarations, which don't include
9457 enough information to be useful. */
9458 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9459 return false;
9461 if (table == pubname_table)
9463 /* Enumerator names are part of the pubname table, but the
9464 parent DW_TAG_enumeration_type die may have been pruned.
9465 Don't output them if that is the case. */
9466 if (p->die->die_tag == DW_TAG_enumerator &&
9467 (p->die->die_parent == NULL
9468 || !p->die->die_parent->die_perennial_p))
9469 return false;
9471 /* Everything else in the pubname table is included. */
9472 return true;
9475 /* The pubtypes table shouldn't include types that have been
9476 pruned. */
9477 return (p->die->die_offset != 0
9478 || !flag_eliminate_unused_debug_types);
9481 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9482 generated for the compilation unit. */
9484 static unsigned long
9485 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9487 unsigned long size;
9488 unsigned i;
9489 pubname_entry *p;
9490 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9492 size = DWARF_PUBNAMES_HEADER_SIZE;
9493 FOR_EACH_VEC_ELT (*names, i, p)
9494 if (include_pubname_in_output (names, p))
9495 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9497 size += DWARF_OFFSET_SIZE;
9498 return size;
9501 /* Return the size of the information in the .debug_aranges section. */
9503 static unsigned long
9504 size_of_aranges (void)
9506 unsigned long size;
9508 size = DWARF_ARANGES_HEADER_SIZE;
9510 /* Count the address/length pair for this compilation unit. */
9511 if (text_section_used)
9512 size += 2 * DWARF2_ADDR_SIZE;
9513 if (cold_text_section_used)
9514 size += 2 * DWARF2_ADDR_SIZE;
9515 if (have_multiple_function_sections)
9517 unsigned fde_idx;
9518 dw_fde_ref fde;
9520 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9522 if (DECL_IGNORED_P (fde->decl))
9523 continue;
9524 if (!fde->in_std_section)
9525 size += 2 * DWARF2_ADDR_SIZE;
9526 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9527 size += 2 * DWARF2_ADDR_SIZE;
9531 /* Count the two zero words used to terminated the address range table. */
9532 size += 2 * DWARF2_ADDR_SIZE;
9533 return size;
9536 /* Select the encoding of an attribute value. */
9538 static enum dwarf_form
9539 value_format (dw_attr_node *a)
9541 switch (AT_class (a))
9543 case dw_val_class_addr:
9544 /* Only very few attributes allow DW_FORM_addr. */
9545 switch (a->dw_attr)
9547 case DW_AT_low_pc:
9548 case DW_AT_high_pc:
9549 case DW_AT_entry_pc:
9550 case DW_AT_trampoline:
9551 return (AT_index (a) == NOT_INDEXED
9552 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9553 default:
9554 break;
9556 switch (DWARF2_ADDR_SIZE)
9558 case 1:
9559 return DW_FORM_data1;
9560 case 2:
9561 return DW_FORM_data2;
9562 case 4:
9563 return DW_FORM_data4;
9564 case 8:
9565 return DW_FORM_data8;
9566 default:
9567 gcc_unreachable ();
9569 case dw_val_class_loc_list:
9570 case dw_val_class_view_list:
9571 if (dwarf_split_debug_info
9572 && dwarf_version >= 5
9573 && AT_loc_list (a)->num_assigned)
9574 return DW_FORM_loclistx;
9575 /* FALLTHRU */
9576 case dw_val_class_range_list:
9577 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9578 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9579 care about sizes of .debug* sections in shared libraries and
9580 executables and don't take into account relocations that affect just
9581 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9582 table in the .debug_rnglists section. */
9583 if (dwarf_split_debug_info
9584 && dwarf_version >= 5
9585 && AT_class (a) == dw_val_class_range_list
9586 && rnglist_idx
9587 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9588 return DW_FORM_rnglistx;
9589 if (dwarf_version >= 4)
9590 return DW_FORM_sec_offset;
9591 /* FALLTHRU */
9592 case dw_val_class_vms_delta:
9593 case dw_val_class_offset:
9594 switch (DWARF_OFFSET_SIZE)
9596 case 4:
9597 return DW_FORM_data4;
9598 case 8:
9599 return DW_FORM_data8;
9600 default:
9601 gcc_unreachable ();
9603 case dw_val_class_loc:
9604 if (dwarf_version >= 4)
9605 return DW_FORM_exprloc;
9606 switch (constant_size (size_of_locs (AT_loc (a))))
9608 case 1:
9609 return DW_FORM_block1;
9610 case 2:
9611 return DW_FORM_block2;
9612 case 4:
9613 return DW_FORM_block4;
9614 default:
9615 gcc_unreachable ();
9617 case dw_val_class_const:
9618 return DW_FORM_sdata;
9619 case dw_val_class_unsigned_const:
9620 switch (constant_size (AT_unsigned (a)))
9622 case 1:
9623 return DW_FORM_data1;
9624 case 2:
9625 return DW_FORM_data2;
9626 case 4:
9627 /* In DWARF3 DW_AT_data_member_location with
9628 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9629 constant, so we need to use DW_FORM_udata if we need
9630 a large constant. */
9631 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9632 return DW_FORM_udata;
9633 return DW_FORM_data4;
9634 case 8:
9635 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9636 return DW_FORM_udata;
9637 return DW_FORM_data8;
9638 default:
9639 gcc_unreachable ();
9641 case dw_val_class_const_implicit:
9642 case dw_val_class_unsigned_const_implicit:
9643 case dw_val_class_file_implicit:
9644 return DW_FORM_implicit_const;
9645 case dw_val_class_const_double:
9646 switch (HOST_BITS_PER_WIDE_INT)
9648 case 8:
9649 return DW_FORM_data2;
9650 case 16:
9651 return DW_FORM_data4;
9652 case 32:
9653 return DW_FORM_data8;
9654 case 64:
9655 if (dwarf_version >= 5)
9656 return DW_FORM_data16;
9657 /* FALLTHRU */
9658 default:
9659 return DW_FORM_block1;
9661 case dw_val_class_wide_int:
9662 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9664 case 8:
9665 return DW_FORM_data1;
9666 case 16:
9667 return DW_FORM_data2;
9668 case 32:
9669 return DW_FORM_data4;
9670 case 64:
9671 return DW_FORM_data8;
9672 case 128:
9673 if (dwarf_version >= 5)
9674 return DW_FORM_data16;
9675 /* FALLTHRU */
9676 default:
9677 return DW_FORM_block1;
9679 case dw_val_class_vec:
9680 switch (constant_size (a->dw_attr_val.v.val_vec.length
9681 * a->dw_attr_val.v.val_vec.elt_size))
9683 case 1:
9684 return DW_FORM_block1;
9685 case 2:
9686 return DW_FORM_block2;
9687 case 4:
9688 return DW_FORM_block4;
9689 default:
9690 gcc_unreachable ();
9692 case dw_val_class_flag:
9693 if (dwarf_version >= 4)
9695 /* Currently all add_AT_flag calls pass in 1 as last argument,
9696 so DW_FORM_flag_present can be used. If that ever changes,
9697 we'll need to use DW_FORM_flag and have some optimization
9698 in build_abbrev_table that will change those to
9699 DW_FORM_flag_present if it is set to 1 in all DIEs using
9700 the same abbrev entry. */
9701 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9702 return DW_FORM_flag_present;
9704 return DW_FORM_flag;
9705 case dw_val_class_die_ref:
9706 if (AT_ref_external (a))
9707 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9708 else
9709 return DW_FORM_ref;
9710 case dw_val_class_fde_ref:
9711 return DW_FORM_data;
9712 case dw_val_class_lbl_id:
9713 return (AT_index (a) == NOT_INDEXED
9714 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9715 case dw_val_class_lineptr:
9716 case dw_val_class_macptr:
9717 case dw_val_class_loclistsptr:
9718 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9719 case dw_val_class_str:
9720 return AT_string_form (a);
9721 case dw_val_class_file:
9722 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9724 case 1:
9725 return DW_FORM_data1;
9726 case 2:
9727 return DW_FORM_data2;
9728 case 4:
9729 return DW_FORM_data4;
9730 default:
9731 gcc_unreachable ();
9734 case dw_val_class_data8:
9735 return DW_FORM_data8;
9737 case dw_val_class_high_pc:
9738 switch (DWARF2_ADDR_SIZE)
9740 case 1:
9741 return DW_FORM_data1;
9742 case 2:
9743 return DW_FORM_data2;
9744 case 4:
9745 return DW_FORM_data4;
9746 case 8:
9747 return DW_FORM_data8;
9748 default:
9749 gcc_unreachable ();
9752 case dw_val_class_discr_value:
9753 return (a->dw_attr_val.v.val_discr_value.pos
9754 ? DW_FORM_udata
9755 : DW_FORM_sdata);
9756 case dw_val_class_discr_list:
9757 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9759 case 1:
9760 return DW_FORM_block1;
9761 case 2:
9762 return DW_FORM_block2;
9763 case 4:
9764 return DW_FORM_block4;
9765 default:
9766 gcc_unreachable ();
9769 default:
9770 gcc_unreachable ();
9774 /* Output the encoding of an attribute value. */
9776 static void
9777 output_value_format (dw_attr_node *a)
9779 enum dwarf_form form = value_format (a);
9781 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9784 /* Given a die and id, produce the appropriate abbreviations. */
9786 static void
9787 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9789 unsigned ix;
9790 dw_attr_node *a_attr;
9792 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9793 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9794 dwarf_tag_name (abbrev->die_tag));
9796 if (abbrev->die_child != NULL)
9797 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9798 else
9799 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9801 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9803 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9804 dwarf_attr_name (a_attr->dw_attr));
9805 output_value_format (a_attr);
9806 if (value_format (a_attr) == DW_FORM_implicit_const)
9808 if (AT_class (a_attr) == dw_val_class_file_implicit)
9810 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9811 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9812 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9814 else
9815 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9819 dw2_asm_output_data (1, 0, NULL);
9820 dw2_asm_output_data (1, 0, NULL);
9824 /* Output the .debug_abbrev section which defines the DIE abbreviation
9825 table. */
9827 static void
9828 output_abbrev_section (void)
9830 unsigned int abbrev_id;
9831 dw_die_ref abbrev;
9833 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9834 if (abbrev_id != 0)
9835 output_die_abbrevs (abbrev_id, abbrev);
9837 /* Terminate the table. */
9838 dw2_asm_output_data (1, 0, NULL);
9841 /* Return a new location list, given the begin and end range, and the
9842 expression. */
9844 static inline dw_loc_list_ref
9845 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9846 const char *end, var_loc_view vend,
9847 const char *section)
9849 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9851 retlist->begin = begin;
9852 retlist->begin_entry = NULL;
9853 retlist->end = end;
9854 retlist->expr = expr;
9855 retlist->section = section;
9856 retlist->vbegin = vbegin;
9857 retlist->vend = vend;
9859 return retlist;
9862 /* Return true iff there's any nonzero view number in the loc list. */
9864 static bool
9865 loc_list_has_views (dw_loc_list_ref list)
9867 if (!debug_variable_location_views)
9868 return false;
9870 for (dw_loc_list_ref loc = list;
9871 loc != NULL; loc = loc->dw_loc_next)
9872 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
9873 return true;
9875 return false;
9878 /* Generate a new internal symbol for this location list node, if it
9879 hasn't got one yet. */
9881 static inline void
9882 gen_llsym (dw_loc_list_ref list)
9884 gcc_assert (!list->ll_symbol);
9885 list->ll_symbol = gen_internal_sym ("LLST");
9887 if (!loc_list_has_views (list))
9888 return;
9890 if (dwarf2out_locviews_in_attribute ())
9892 /* Use the same label_num for the view list. */
9893 label_num--;
9894 list->vl_symbol = gen_internal_sym ("LVUS");
9896 else
9897 list->vl_symbol = list->ll_symbol;
9900 /* Generate a symbol for the list, but only if we really want to emit
9901 it as a list. */
9903 static inline void
9904 maybe_gen_llsym (dw_loc_list_ref list)
9906 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
9907 return;
9909 gen_llsym (list);
9912 /* Determine whether or not to skip loc_list entry CURR. If we're not
9913 to skip it, and SIZEP is non-null, store the size of CURR->expr's
9914 representation in *SIZEP. */
9916 static bool
9917 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = 0)
9919 /* Don't output an entry that starts and ends at the same address. */
9920 if (strcmp (curr->begin, curr->end) == 0
9921 && curr->vbegin == curr->vend && !curr->force)
9922 return true;
9924 unsigned long size = size_of_locs (curr->expr);
9926 /* If the expression is too large, drop it on the floor. We could
9927 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9928 in the expression, but >= 64KB expressions for a single value
9929 in a single range are unlikely very useful. */
9930 if (dwarf_version < 5 && size > 0xffff)
9931 return true;
9933 if (sizep)
9934 *sizep = size;
9936 return false;
9939 /* Output a view pair loclist entry for CURR, if it requires one. */
9941 static void
9942 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
9944 if (!dwarf2out_locviews_in_loclist ())
9945 return;
9947 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
9948 return;
9950 #ifdef DW_LLE_view_pair
9951 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
9953 # if DWARF2_ASM_VIEW_DEBUG_INFO
9954 if (ZERO_VIEW_P (curr->vbegin))
9955 dw2_asm_output_data_uleb128 (0, "Location view begin");
9956 else
9958 char label[MAX_ARTIFICIAL_LABEL_BYTES];
9959 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
9960 dw2_asm_output_symname_uleb128 (label, "Location view begin");
9963 if (ZERO_VIEW_P (curr->vend))
9964 dw2_asm_output_data_uleb128 (0, "Location view end");
9965 else
9967 char label[MAX_ARTIFICIAL_LABEL_BYTES];
9968 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
9969 dw2_asm_output_symname_uleb128 (label, "Location view end");
9971 # else /* !DWARF2_ASM_VIEW_DEBUG_INFO */
9972 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
9973 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
9974 # endif /* DWARF2_ASM_VIEW_DEBUG_INFO */
9975 #endif /* DW_LLE_view_pair */
9977 return;
9980 /* Output the location list given to us. */
9982 static void
9983 output_loc_list (dw_loc_list_ref list_head)
9985 int vcount = 0, lcount = 0;
9987 if (list_head->emitted)
9988 return;
9989 list_head->emitted = true;
9991 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
9993 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
9995 for (dw_loc_list_ref curr = list_head; curr != NULL;
9996 curr = curr->dw_loc_next)
9998 if (skip_loc_list_entry (curr))
9999 continue;
10001 vcount++;
10003 /* ?? dwarf_split_debug_info? */
10004 #if DWARF2_ASM_VIEW_DEBUG_INFO
10005 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10007 if (!ZERO_VIEW_P (curr->vbegin))
10009 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10010 dw2_asm_output_symname_uleb128 (label,
10011 "View list begin (%s)",
10012 list_head->vl_symbol);
10014 else
10015 dw2_asm_output_data_uleb128 (0,
10016 "View list begin (%s)",
10017 list_head->vl_symbol);
10019 if (!ZERO_VIEW_P (curr->vend))
10021 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10022 dw2_asm_output_symname_uleb128 (label,
10023 "View list end (%s)",
10024 list_head->vl_symbol);
10026 else
10027 dw2_asm_output_data_uleb128 (0,
10028 "View list end (%s)",
10029 list_head->vl_symbol);
10030 #else /* !DWARF2_ASM_VIEW_DEBUG_INFO */
10031 dw2_asm_output_data_uleb128 (curr->vbegin,
10032 "View list begin (%s)",
10033 list_head->vl_symbol);
10034 dw2_asm_output_data_uleb128 (curr->vend,
10035 "View list end (%s)",
10036 list_head->vl_symbol);
10037 #endif
10041 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10043 const char *last_section = NULL;
10044 const char *base_label = NULL;
10046 /* Walk the location list, and output each range + expression. */
10047 for (dw_loc_list_ref curr = list_head; curr != NULL;
10048 curr = curr->dw_loc_next)
10050 unsigned long size;
10052 /* Skip this entry? If we skip it here, we must skip it in the
10053 view list above as well. */
10054 if (skip_loc_list_entry (curr, &size))
10055 continue;
10057 lcount++;
10059 if (dwarf_version >= 5)
10061 if (dwarf_split_debug_info)
10063 dwarf2out_maybe_output_loclist_view_pair (curr);
10064 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10065 uleb128 index into .debug_addr and uleb128 length. */
10066 dw2_asm_output_data (1, DW_LLE_startx_length,
10067 "DW_LLE_startx_length (%s)",
10068 list_head->ll_symbol);
10069 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10070 "Location list range start index "
10071 "(%s)", curr->begin);
10072 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10073 For that case we probably need to emit DW_LLE_startx_endx,
10074 but we'd need 2 .debug_addr entries rather than just one. */
10075 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10076 "Location list length (%s)",
10077 list_head->ll_symbol);
10079 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10081 dwarf2out_maybe_output_loclist_view_pair (curr);
10082 /* If all code is in .text section, the base address is
10083 already provided by the CU attributes. Use
10084 DW_LLE_offset_pair where both addresses are uleb128 encoded
10085 offsets against that base. */
10086 dw2_asm_output_data (1, DW_LLE_offset_pair,
10087 "DW_LLE_offset_pair (%s)",
10088 list_head->ll_symbol);
10089 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10090 "Location list begin address (%s)",
10091 list_head->ll_symbol);
10092 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10093 "Location list end address (%s)",
10094 list_head->ll_symbol);
10096 else if (HAVE_AS_LEB128)
10098 /* Otherwise, find out how many consecutive entries could share
10099 the same base entry. If just one, emit DW_LLE_start_length,
10100 otherwise emit DW_LLE_base_address for the base address
10101 followed by a series of DW_LLE_offset_pair. */
10102 if (last_section == NULL || curr->section != last_section)
10104 dw_loc_list_ref curr2;
10105 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10106 curr2 = curr2->dw_loc_next)
10108 if (strcmp (curr2->begin, curr2->end) == 0
10109 && !curr2->force)
10110 continue;
10111 break;
10113 if (curr2 == NULL || curr->section != curr2->section)
10114 last_section = NULL;
10115 else
10117 last_section = curr->section;
10118 base_label = curr->begin;
10119 dw2_asm_output_data (1, DW_LLE_base_address,
10120 "DW_LLE_base_address (%s)",
10121 list_head->ll_symbol);
10122 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10123 "Base address (%s)",
10124 list_head->ll_symbol);
10127 /* Only one entry with the same base address. Use
10128 DW_LLE_start_length with absolute address and uleb128
10129 length. */
10130 if (last_section == NULL)
10132 dwarf2out_maybe_output_loclist_view_pair (curr);
10133 dw2_asm_output_data (1, DW_LLE_start_length,
10134 "DW_LLE_start_length (%s)",
10135 list_head->ll_symbol);
10136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10137 "Location list begin address (%s)",
10138 list_head->ll_symbol);
10139 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10140 "Location list length "
10141 "(%s)", list_head->ll_symbol);
10143 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10144 DW_LLE_base_address. */
10145 else
10147 dwarf2out_maybe_output_loclist_view_pair (curr);
10148 dw2_asm_output_data (1, DW_LLE_offset_pair,
10149 "DW_LLE_offset_pair (%s)",
10150 list_head->ll_symbol);
10151 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10152 "Location list begin address "
10153 "(%s)", list_head->ll_symbol);
10154 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10155 "Location list end address "
10156 "(%s)", list_head->ll_symbol);
10159 /* The assembler does not support .uleb128 directive. Emit
10160 DW_LLE_start_end with a pair of absolute addresses. */
10161 else
10163 dwarf2out_maybe_output_loclist_view_pair (curr);
10164 dw2_asm_output_data (1, DW_LLE_start_end,
10165 "DW_LLE_start_end (%s)",
10166 list_head->ll_symbol);
10167 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10168 "Location list begin address (%s)",
10169 list_head->ll_symbol);
10170 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10171 "Location list end address (%s)",
10172 list_head->ll_symbol);
10175 else if (dwarf_split_debug_info)
10177 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10178 and 4 byte length. */
10179 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10180 "Location list start/length entry (%s)",
10181 list_head->ll_symbol);
10182 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10183 "Location list range start index (%s)",
10184 curr->begin);
10185 /* The length field is 4 bytes. If we ever need to support
10186 an 8-byte length, we can add a new DW_LLE code or fall back
10187 to DW_LLE_GNU_start_end_entry. */
10188 dw2_asm_output_delta (4, curr->end, curr->begin,
10189 "Location list range length (%s)",
10190 list_head->ll_symbol);
10192 else if (!have_multiple_function_sections)
10194 /* Pair of relative addresses against start of text section. */
10195 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10196 "Location list begin address (%s)",
10197 list_head->ll_symbol);
10198 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10199 "Location list end address (%s)",
10200 list_head->ll_symbol);
10202 else
10204 /* Pair of absolute addresses. */
10205 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10206 "Location list begin address (%s)",
10207 list_head->ll_symbol);
10208 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10209 "Location list end address (%s)",
10210 list_head->ll_symbol);
10213 /* Output the block length for this list of location operations. */
10214 if (dwarf_version >= 5)
10215 dw2_asm_output_data_uleb128 (size, "Location expression size");
10216 else
10218 gcc_assert (size <= 0xffff);
10219 dw2_asm_output_data (2, size, "Location expression size");
10222 output_loc_sequence (curr->expr, -1);
10225 /* And finally list termination. */
10226 if (dwarf_version >= 5)
10227 dw2_asm_output_data (1, DW_LLE_end_of_list,
10228 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10229 else if (dwarf_split_debug_info)
10230 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10231 "Location list terminator (%s)",
10232 list_head->ll_symbol);
10233 else
10235 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10236 "Location list terminator begin (%s)",
10237 list_head->ll_symbol);
10238 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10239 "Location list terminator end (%s)",
10240 list_head->ll_symbol);
10243 gcc_assert (!list_head->vl_symbol
10244 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10247 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10248 section. Emit a relocated reference if val_entry is NULL, otherwise,
10249 emit an indirect reference. */
10251 static void
10252 output_range_list_offset (dw_attr_node *a)
10254 const char *name = dwarf_attr_name (a->dw_attr);
10256 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10258 if (dwarf_version >= 5)
10260 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10261 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10262 debug_ranges_section, "%s", name);
10264 else
10266 char *p = strchr (ranges_section_label, '\0');
10267 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10268 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10270 debug_ranges_section, "%s", name);
10271 *p = '\0';
10274 else if (dwarf_version >= 5)
10276 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10277 gcc_assert (rnglist_idx);
10278 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10280 else
10281 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10282 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10283 "%s (offset from %s)", name, ranges_section_label);
10286 /* Output the offset into the debug_loc section. */
10288 static void
10289 output_loc_list_offset (dw_attr_node *a)
10291 char *sym = AT_loc_list (a)->ll_symbol;
10293 gcc_assert (sym);
10294 if (!dwarf_split_debug_info)
10295 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10296 "%s", dwarf_attr_name (a->dw_attr));
10297 else if (dwarf_version >= 5)
10299 gcc_assert (AT_loc_list (a)->num_assigned);
10300 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10301 dwarf_attr_name (a->dw_attr),
10302 sym);
10304 else
10305 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10306 "%s", dwarf_attr_name (a->dw_attr));
10309 /* Output the offset into the debug_loc section. */
10311 static void
10312 output_view_list_offset (dw_attr_node *a)
10314 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10316 gcc_assert (sym);
10317 if (dwarf_split_debug_info)
10318 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10319 "%s", dwarf_attr_name (a->dw_attr));
10320 else
10321 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10322 "%s", dwarf_attr_name (a->dw_attr));
10325 /* Output an attribute's index or value appropriately. */
10327 static void
10328 output_attr_index_or_value (dw_attr_node *a)
10330 const char *name = dwarf_attr_name (a->dw_attr);
10332 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10334 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10335 return;
10337 switch (AT_class (a))
10339 case dw_val_class_addr:
10340 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10341 break;
10342 case dw_val_class_high_pc:
10343 case dw_val_class_lbl_id:
10344 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10345 break;
10346 default:
10347 gcc_unreachable ();
10351 /* Output a type signature. */
10353 static inline void
10354 output_signature (const char *sig, const char *name)
10356 int i;
10358 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10359 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10362 /* Output a discriminant value. */
10364 static inline void
10365 output_discr_value (dw_discr_value *discr_value, const char *name)
10367 if (discr_value->pos)
10368 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10369 else
10370 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10373 /* Output the DIE and its attributes. Called recursively to generate
10374 the definitions of each child DIE. */
10376 static void
10377 output_die (dw_die_ref die)
10379 dw_attr_node *a;
10380 dw_die_ref c;
10381 unsigned long size;
10382 unsigned ix;
10384 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10385 (unsigned long)die->die_offset,
10386 dwarf_tag_name (die->die_tag));
10388 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10390 const char *name = dwarf_attr_name (a->dw_attr);
10392 switch (AT_class (a))
10394 case dw_val_class_addr:
10395 output_attr_index_or_value (a);
10396 break;
10398 case dw_val_class_offset:
10399 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10400 "%s", name);
10401 break;
10403 case dw_val_class_range_list:
10404 output_range_list_offset (a);
10405 break;
10407 case dw_val_class_loc:
10408 size = size_of_locs (AT_loc (a));
10410 /* Output the block length for this list of location operations. */
10411 if (dwarf_version >= 4)
10412 dw2_asm_output_data_uleb128 (size, "%s", name);
10413 else
10414 dw2_asm_output_data (constant_size (size), size, "%s", name);
10416 output_loc_sequence (AT_loc (a), -1);
10417 break;
10419 case dw_val_class_const:
10420 /* ??? It would be slightly more efficient to use a scheme like is
10421 used for unsigned constants below, but gdb 4.x does not sign
10422 extend. Gdb 5.x does sign extend. */
10423 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10424 break;
10426 case dw_val_class_unsigned_const:
10428 int csize = constant_size (AT_unsigned (a));
10429 if (dwarf_version == 3
10430 && a->dw_attr == DW_AT_data_member_location
10431 && csize >= 4)
10432 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10433 else
10434 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10436 break;
10438 case dw_val_class_const_implicit:
10439 if (flag_debug_asm)
10440 fprintf (asm_out_file, "\t\t\t%s %s ("
10441 HOST_WIDE_INT_PRINT_DEC ")\n",
10442 ASM_COMMENT_START, name, AT_int (a));
10443 break;
10445 case dw_val_class_unsigned_const_implicit:
10446 if (flag_debug_asm)
10447 fprintf (asm_out_file, "\t\t\t%s %s ("
10448 HOST_WIDE_INT_PRINT_HEX ")\n",
10449 ASM_COMMENT_START, name, AT_unsigned (a));
10450 break;
10452 case dw_val_class_const_double:
10454 unsigned HOST_WIDE_INT first, second;
10456 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10457 dw2_asm_output_data (1,
10458 HOST_BITS_PER_DOUBLE_INT
10459 / HOST_BITS_PER_CHAR,
10460 NULL);
10462 if (WORDS_BIG_ENDIAN)
10464 first = a->dw_attr_val.v.val_double.high;
10465 second = a->dw_attr_val.v.val_double.low;
10467 else
10469 first = a->dw_attr_val.v.val_double.low;
10470 second = a->dw_attr_val.v.val_double.high;
10473 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10474 first, "%s", name);
10475 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10476 second, NULL);
10478 break;
10480 case dw_val_class_wide_int:
10482 int i;
10483 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10484 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10485 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10486 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10487 * l, NULL);
10489 if (WORDS_BIG_ENDIAN)
10490 for (i = len - 1; i >= 0; --i)
10492 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10493 "%s", name);
10494 name = "";
10496 else
10497 for (i = 0; i < len; ++i)
10499 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10500 "%s", name);
10501 name = "";
10504 break;
10506 case dw_val_class_vec:
10508 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10509 unsigned int len = a->dw_attr_val.v.val_vec.length;
10510 unsigned int i;
10511 unsigned char *p;
10513 dw2_asm_output_data (constant_size (len * elt_size),
10514 len * elt_size, "%s", name);
10515 if (elt_size > sizeof (HOST_WIDE_INT))
10517 elt_size /= 2;
10518 len *= 2;
10520 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10521 i < len;
10522 i++, p += elt_size)
10523 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10524 "fp or vector constant word %u", i);
10525 break;
10528 case dw_val_class_flag:
10529 if (dwarf_version >= 4)
10531 /* Currently all add_AT_flag calls pass in 1 as last argument,
10532 so DW_FORM_flag_present can be used. If that ever changes,
10533 we'll need to use DW_FORM_flag and have some optimization
10534 in build_abbrev_table that will change those to
10535 DW_FORM_flag_present if it is set to 1 in all DIEs using
10536 the same abbrev entry. */
10537 gcc_assert (AT_flag (a) == 1);
10538 if (flag_debug_asm)
10539 fprintf (asm_out_file, "\t\t\t%s %s\n",
10540 ASM_COMMENT_START, name);
10541 break;
10543 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10544 break;
10546 case dw_val_class_loc_list:
10547 output_loc_list_offset (a);
10548 break;
10550 case dw_val_class_view_list:
10551 output_view_list_offset (a);
10552 break;
10554 case dw_val_class_die_ref:
10555 if (AT_ref_external (a))
10557 if (AT_ref (a)->comdat_type_p)
10559 comdat_type_node *type_node
10560 = AT_ref (a)->die_id.die_type_node;
10562 gcc_assert (type_node);
10563 output_signature (type_node->signature, name);
10565 else
10567 const char *sym = AT_ref (a)->die_id.die_symbol;
10568 int size;
10570 gcc_assert (sym);
10571 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10572 length, whereas in DWARF3 it's always sized as an
10573 offset. */
10574 if (dwarf_version == 2)
10575 size = DWARF2_ADDR_SIZE;
10576 else
10577 size = DWARF_OFFSET_SIZE;
10578 /* ??? We cannot unconditionally output die_offset if
10579 non-zero - others might create references to those
10580 DIEs via symbols.
10581 And we do not clear its DIE offset after outputting it
10582 (and the label refers to the actual DIEs, not the
10583 DWARF CU unit header which is when using label + offset
10584 would be the correct thing to do).
10585 ??? This is the reason for the with_offset flag. */
10586 if (AT_ref (a)->with_offset)
10587 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10588 debug_info_section, "%s", name);
10589 else
10590 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10591 name);
10594 else
10596 gcc_assert (AT_ref (a)->die_offset);
10597 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10598 "%s", name);
10600 break;
10602 case dw_val_class_fde_ref:
10604 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10606 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10607 a->dw_attr_val.v.val_fde_index * 2);
10608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10609 "%s", name);
10611 break;
10613 case dw_val_class_vms_delta:
10614 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10615 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10616 AT_vms_delta2 (a), AT_vms_delta1 (a),
10617 "%s", name);
10618 #else
10619 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10620 AT_vms_delta2 (a), AT_vms_delta1 (a),
10621 "%s", name);
10622 #endif
10623 break;
10625 case dw_val_class_lbl_id:
10626 output_attr_index_or_value (a);
10627 break;
10629 case dw_val_class_lineptr:
10630 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10631 debug_line_section, "%s", name);
10632 break;
10634 case dw_val_class_macptr:
10635 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10636 debug_macinfo_section, "%s", name);
10637 break;
10639 case dw_val_class_loclistsptr:
10640 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10641 debug_loc_section, "%s", name);
10642 break;
10644 case dw_val_class_str:
10645 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10646 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10647 a->dw_attr_val.v.val_str->label,
10648 debug_str_section,
10649 "%s: \"%s\"", name, AT_string (a));
10650 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10651 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10652 a->dw_attr_val.v.val_str->label,
10653 debug_line_str_section,
10654 "%s: \"%s\"", name, AT_string (a));
10655 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10656 dw2_asm_output_data_uleb128 (AT_index (a),
10657 "%s: \"%s\"", name, AT_string (a));
10658 else
10659 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10660 break;
10662 case dw_val_class_file:
10664 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10666 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10667 a->dw_attr_val.v.val_file->filename);
10668 break;
10671 case dw_val_class_file_implicit:
10672 if (flag_debug_asm)
10673 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10674 ASM_COMMENT_START, name,
10675 maybe_emit_file (a->dw_attr_val.v.val_file),
10676 a->dw_attr_val.v.val_file->filename);
10677 break;
10679 case dw_val_class_data8:
10681 int i;
10683 for (i = 0; i < 8; i++)
10684 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10685 i == 0 ? "%s" : NULL, name);
10686 break;
10689 case dw_val_class_high_pc:
10690 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10691 get_AT_low_pc (die), "DW_AT_high_pc");
10692 break;
10694 case dw_val_class_discr_value:
10695 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10696 break;
10698 case dw_val_class_discr_list:
10700 dw_discr_list_ref list = AT_discr_list (a);
10701 const int size = size_of_discr_list (list);
10703 /* This is a block, so output its length first. */
10704 dw2_asm_output_data (constant_size (size), size,
10705 "%s: block size", name);
10707 for (; list != NULL; list = list->dw_discr_next)
10709 /* One byte for the discriminant value descriptor, and then as
10710 many LEB128 numbers as required. */
10711 if (list->dw_discr_range)
10712 dw2_asm_output_data (1, DW_DSC_range,
10713 "%s: DW_DSC_range", name);
10714 else
10715 dw2_asm_output_data (1, DW_DSC_label,
10716 "%s: DW_DSC_label", name);
10718 output_discr_value (&list->dw_discr_lower_bound, name);
10719 if (list->dw_discr_range)
10720 output_discr_value (&list->dw_discr_upper_bound, name);
10722 break;
10725 default:
10726 gcc_unreachable ();
10730 FOR_EACH_CHILD (die, c, output_die (c));
10732 /* Add null byte to terminate sibling list. */
10733 if (die->die_child != NULL)
10734 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10735 (unsigned long) die->die_offset);
10738 /* Output the dwarf version number. */
10740 static void
10741 output_dwarf_version ()
10743 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10744 views in loclist. That will change eventually. */
10745 if (dwarf_version == 6)
10747 static bool once;
10748 if (!once)
10750 warning (0,
10751 "-gdwarf-6 is output as version 5 with incompatibilities");
10752 once = true;
10754 dw2_asm_output_data (2, 5, "DWARF version number");
10756 else
10757 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10760 /* Output the compilation unit that appears at the beginning of the
10761 .debug_info section, and precedes the DIE descriptions. */
10763 static void
10764 output_compilation_unit_header (enum dwarf_unit_type ut)
10766 if (!XCOFF_DEBUGGING_INFO)
10768 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10769 dw2_asm_output_data (4, 0xffffffff,
10770 "Initial length escape value indicating 64-bit DWARF extension");
10771 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10772 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10773 "Length of Compilation Unit Info");
10776 output_dwarf_version ();
10777 if (dwarf_version >= 5)
10779 const char *name;
10780 switch (ut)
10782 case DW_UT_compile: name = "DW_UT_compile"; break;
10783 case DW_UT_type: name = "DW_UT_type"; break;
10784 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10785 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10786 default: gcc_unreachable ();
10788 dw2_asm_output_data (1, ut, "%s", name);
10789 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10791 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10792 debug_abbrev_section,
10793 "Offset Into Abbrev. Section");
10794 if (dwarf_version < 5)
10795 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10798 /* Output the compilation unit DIE and its children. */
10800 static void
10801 output_comp_unit (dw_die_ref die, int output_if_empty,
10802 const unsigned char *dwo_id)
10804 const char *secname, *oldsym;
10805 char *tmp;
10807 /* Unless we are outputting main CU, we may throw away empty ones. */
10808 if (!output_if_empty && die->die_child == NULL)
10809 return;
10811 /* Even if there are no children of this DIE, we must output the information
10812 about the compilation unit. Otherwise, on an empty translation unit, we
10813 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10814 will then complain when examining the file. First mark all the DIEs in
10815 this CU so we know which get local refs. */
10816 mark_dies (die);
10818 external_ref_hash_type *extern_map = optimize_external_refs (die);
10820 /* For now, optimize only the main CU, in order to optimize the rest
10821 we'd need to see all of them earlier. Leave the rest for post-linking
10822 tools like DWZ. */
10823 if (die == comp_unit_die ())
10824 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10826 build_abbrev_table (die, extern_map);
10828 optimize_abbrev_table ();
10830 delete extern_map;
10832 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10833 next_die_offset = (dwo_id
10834 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10835 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10836 calc_die_sizes (die);
10838 oldsym = die->die_id.die_symbol;
10839 if (oldsym && die->comdat_type_p)
10841 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10843 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10844 secname = tmp;
10845 die->die_id.die_symbol = NULL;
10846 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10848 else
10850 switch_to_section (debug_info_section);
10851 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10852 info_section_emitted = true;
10855 /* For LTO cross unit DIE refs we want a symbol on the start of the
10856 debuginfo section, not on the CU DIE. */
10857 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10859 /* ??? No way to get visibility assembled without a decl. */
10860 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10861 get_identifier (oldsym), char_type_node);
10862 TREE_PUBLIC (decl) = true;
10863 TREE_STATIC (decl) = true;
10864 DECL_ARTIFICIAL (decl) = true;
10865 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10866 DECL_VISIBILITY_SPECIFIED (decl) = true;
10867 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10868 #ifdef ASM_WEAKEN_LABEL
10869 /* We prefer a .weak because that handles duplicates from duplicate
10870 archive members in a graceful way. */
10871 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10872 #else
10873 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10874 #endif
10875 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10878 /* Output debugging information. */
10879 output_compilation_unit_header (dwo_id
10880 ? DW_UT_split_compile : DW_UT_compile);
10881 if (dwarf_version >= 5)
10883 if (dwo_id != NULL)
10884 for (int i = 0; i < 8; i++)
10885 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10887 output_die (die);
10889 /* Leave the marks on the main CU, so we can check them in
10890 output_pubnames. */
10891 if (oldsym)
10893 unmark_dies (die);
10894 die->die_id.die_symbol = oldsym;
10898 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10899 and .debug_pubtypes. This is configured per-target, but can be
10900 overridden by the -gpubnames or -gno-pubnames options. */
10902 static inline bool
10903 want_pubnames (void)
10905 if (debug_info_level <= DINFO_LEVEL_TERSE)
10906 return false;
10907 if (debug_generate_pub_sections != -1)
10908 return debug_generate_pub_sections;
10909 return targetm.want_debug_pub_sections;
10912 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10914 static void
10915 add_AT_pubnames (dw_die_ref die)
10917 if (want_pubnames ())
10918 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10921 /* Add a string attribute value to a skeleton DIE. */
10923 static inline void
10924 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10925 const char *str)
10927 dw_attr_node attr;
10928 struct indirect_string_node *node;
10930 if (! skeleton_debug_str_hash)
10931 skeleton_debug_str_hash
10932 = hash_table<indirect_string_hasher>::create_ggc (10);
10934 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10935 find_string_form (node);
10936 if (node->form == DW_FORM_GNU_str_index)
10937 node->form = DW_FORM_strp;
10939 attr.dw_attr = attr_kind;
10940 attr.dw_attr_val.val_class = dw_val_class_str;
10941 attr.dw_attr_val.val_entry = NULL;
10942 attr.dw_attr_val.v.val_str = node;
10943 add_dwarf_attr (die, &attr);
10946 /* Helper function to generate top-level dies for skeleton debug_info and
10947 debug_types. */
10949 static void
10950 add_top_level_skeleton_die_attrs (dw_die_ref die)
10952 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10953 const char *comp_dir = comp_dir_string ();
10955 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10956 if (comp_dir != NULL)
10957 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10958 add_AT_pubnames (die);
10959 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10962 /* Output skeleton debug sections that point to the dwo file. */
10964 static void
10965 output_skeleton_debug_sections (dw_die_ref comp_unit,
10966 const unsigned char *dwo_id)
10968 /* These attributes will be found in the full debug_info section. */
10969 remove_AT (comp_unit, DW_AT_producer);
10970 remove_AT (comp_unit, DW_AT_language);
10972 switch_to_section (debug_skeleton_info_section);
10973 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10975 /* Produce the skeleton compilation-unit header. This one differs enough from
10976 a normal CU header that it's better not to call output_compilation_unit
10977 header. */
10978 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10979 dw2_asm_output_data (4, 0xffffffff,
10980 "Initial length escape value indicating 64-bit "
10981 "DWARF extension");
10983 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10984 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10985 - DWARF_INITIAL_LENGTH_SIZE
10986 + size_of_die (comp_unit),
10987 "Length of Compilation Unit Info");
10988 output_dwarf_version ();
10989 if (dwarf_version >= 5)
10991 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10992 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10994 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10995 debug_skeleton_abbrev_section,
10996 "Offset Into Abbrev. Section");
10997 if (dwarf_version < 5)
10998 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10999 else
11000 for (int i = 0; i < 8; i++)
11001 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11003 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11004 output_die (comp_unit);
11006 /* Build the skeleton debug_abbrev section. */
11007 switch_to_section (debug_skeleton_abbrev_section);
11008 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11010 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11012 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11015 /* Output a comdat type unit DIE and its children. */
11017 static void
11018 output_comdat_type_unit (comdat_type_node *node)
11020 const char *secname;
11021 char *tmp;
11022 int i;
11023 #if defined (OBJECT_FORMAT_ELF)
11024 tree comdat_key;
11025 #endif
11027 /* First mark all the DIEs in this CU so we know which get local refs. */
11028 mark_dies (node->root_die);
11030 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11032 build_abbrev_table (node->root_die, extern_map);
11034 delete extern_map;
11035 extern_map = NULL;
11037 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11038 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11039 calc_die_sizes (node->root_die);
11041 #if defined (OBJECT_FORMAT_ELF)
11042 if (dwarf_version >= 5)
11044 if (!dwarf_split_debug_info)
11045 secname = ".debug_info";
11046 else
11047 secname = ".debug_info.dwo";
11049 else if (!dwarf_split_debug_info)
11050 secname = ".debug_types";
11051 else
11052 secname = ".debug_types.dwo";
11054 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11055 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11056 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11057 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11058 comdat_key = get_identifier (tmp);
11059 targetm.asm_out.named_section (secname,
11060 SECTION_DEBUG | SECTION_LINKONCE,
11061 comdat_key);
11062 #else
11063 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11064 sprintf (tmp, (dwarf_version >= 5
11065 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11066 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11067 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11068 secname = tmp;
11069 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11070 #endif
11072 /* Output debugging information. */
11073 output_compilation_unit_header (dwarf_split_debug_info
11074 ? DW_UT_split_type : DW_UT_type);
11075 output_signature (node->signature, "Type Signature");
11076 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11077 "Offset to Type DIE");
11078 output_die (node->root_die);
11080 unmark_dies (node->root_die);
11083 /* Return the DWARF2/3 pubname associated with a decl. */
11085 static const char *
11086 dwarf2_name (tree decl, int scope)
11088 if (DECL_NAMELESS (decl))
11089 return NULL;
11090 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11093 /* Add a new entry to .debug_pubnames if appropriate. */
11095 static void
11096 add_pubname_string (const char *str, dw_die_ref die)
11098 pubname_entry e;
11100 e.die = die;
11101 e.name = xstrdup (str);
11102 vec_safe_push (pubname_table, e);
11105 static void
11106 add_pubname (tree decl, dw_die_ref die)
11108 if (!want_pubnames ())
11109 return;
11111 /* Don't add items to the table when we expect that the consumer will have
11112 just read the enclosing die. For example, if the consumer is looking at a
11113 class_member, it will either be inside the class already, or will have just
11114 looked up the class to find the member. Either way, searching the class is
11115 faster than searching the index. */
11116 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11117 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11119 const char *name = dwarf2_name (decl, 1);
11121 if (name)
11122 add_pubname_string (name, die);
11126 /* Add an enumerator to the pubnames section. */
11128 static void
11129 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11131 pubname_entry e;
11133 gcc_assert (scope_name);
11134 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11135 e.die = die;
11136 vec_safe_push (pubname_table, e);
11139 /* Add a new entry to .debug_pubtypes if appropriate. */
11141 static void
11142 add_pubtype (tree decl, dw_die_ref die)
11144 pubname_entry e;
11146 if (!want_pubnames ())
11147 return;
11149 if ((TREE_PUBLIC (decl)
11150 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11151 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11153 tree scope = NULL;
11154 const char *scope_name = "";
11155 const char *sep = is_cxx () ? "::" : ".";
11156 const char *name;
11158 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11159 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11161 scope_name = lang_hooks.dwarf_name (scope, 1);
11162 if (scope_name != NULL && scope_name[0] != '\0')
11163 scope_name = concat (scope_name, sep, NULL);
11164 else
11165 scope_name = "";
11168 if (TYPE_P (decl))
11169 name = type_tag (decl);
11170 else
11171 name = lang_hooks.dwarf_name (decl, 1);
11173 /* If we don't have a name for the type, there's no point in adding
11174 it to the table. */
11175 if (name != NULL && name[0] != '\0')
11177 e.die = die;
11178 e.name = concat (scope_name, name, NULL);
11179 vec_safe_push (pubtype_table, e);
11182 /* Although it might be more consistent to add the pubinfo for the
11183 enumerators as their dies are created, they should only be added if the
11184 enum type meets the criteria above. So rather than re-check the parent
11185 enum type whenever an enumerator die is created, just output them all
11186 here. This isn't protected by the name conditional because anonymous
11187 enums don't have names. */
11188 if (die->die_tag == DW_TAG_enumeration_type)
11190 dw_die_ref c;
11192 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11197 /* Output a single entry in the pubnames table. */
11199 static void
11200 output_pubname (dw_offset die_offset, pubname_entry *entry)
11202 dw_die_ref die = entry->die;
11203 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11205 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11207 if (debug_generate_pub_sections == 2)
11209 /* This logic follows gdb's method for determining the value of the flag
11210 byte. */
11211 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11212 switch (die->die_tag)
11214 case DW_TAG_typedef:
11215 case DW_TAG_base_type:
11216 case DW_TAG_subrange_type:
11217 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11218 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11219 break;
11220 case DW_TAG_enumerator:
11221 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11222 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11223 if (!is_cxx ())
11224 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11225 break;
11226 case DW_TAG_subprogram:
11227 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11228 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11229 if (!is_ada ())
11230 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11231 break;
11232 case DW_TAG_constant:
11233 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11234 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11235 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11236 break;
11237 case DW_TAG_variable:
11238 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11239 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11240 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11241 break;
11242 case DW_TAG_namespace:
11243 case DW_TAG_imported_declaration:
11244 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11245 break;
11246 case DW_TAG_class_type:
11247 case DW_TAG_interface_type:
11248 case DW_TAG_structure_type:
11249 case DW_TAG_union_type:
11250 case DW_TAG_enumeration_type:
11251 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11252 if (!is_cxx ())
11253 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11254 break;
11255 default:
11256 /* An unusual tag. Leave the flag-byte empty. */
11257 break;
11259 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11260 "GDB-index flags");
11263 dw2_asm_output_nstring (entry->name, -1, "external name");
11267 /* Output the public names table used to speed up access to externally
11268 visible names; or the public types table used to find type definitions. */
11270 static void
11271 output_pubnames (vec<pubname_entry, va_gc> *names)
11273 unsigned i;
11274 unsigned long pubnames_length = size_of_pubnames (names);
11275 pubname_entry *pub;
11277 if (!XCOFF_DEBUGGING_INFO)
11279 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11280 dw2_asm_output_data (4, 0xffffffff,
11281 "Initial length escape value indicating 64-bit DWARF extension");
11282 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11283 "Pub Info Length");
11286 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11287 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11289 if (dwarf_split_debug_info)
11290 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11291 debug_skeleton_info_section,
11292 "Offset of Compilation Unit Info");
11293 else
11294 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11295 debug_info_section,
11296 "Offset of Compilation Unit Info");
11297 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11298 "Compilation Unit Length");
11300 FOR_EACH_VEC_ELT (*names, i, pub)
11302 if (include_pubname_in_output (names, pub))
11304 dw_offset die_offset = pub->die->die_offset;
11306 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11307 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11308 gcc_assert (pub->die->die_mark);
11310 /* If we're putting types in their own .debug_types sections,
11311 the .debug_pubtypes table will still point to the compile
11312 unit (not the type unit), so we want to use the offset of
11313 the skeleton DIE (if there is one). */
11314 if (pub->die->comdat_type_p && names == pubtype_table)
11316 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11318 if (type_node != NULL)
11319 die_offset = (type_node->skeleton_die != NULL
11320 ? type_node->skeleton_die->die_offset
11321 : comp_unit_die ()->die_offset);
11324 output_pubname (die_offset, pub);
11328 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11331 /* Output public names and types tables if necessary. */
11333 static void
11334 output_pubtables (void)
11336 if (!want_pubnames () || !info_section_emitted)
11337 return;
11339 switch_to_section (debug_pubnames_section);
11340 output_pubnames (pubname_table);
11341 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11342 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11343 simply won't look for the section. */
11344 switch_to_section (debug_pubtypes_section);
11345 output_pubnames (pubtype_table);
11349 /* Output the information that goes into the .debug_aranges table.
11350 Namely, define the beginning and ending address range of the
11351 text section generated for this compilation unit. */
11353 static void
11354 output_aranges (void)
11356 unsigned i;
11357 unsigned long aranges_length = size_of_aranges ();
11359 if (!XCOFF_DEBUGGING_INFO)
11361 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11362 dw2_asm_output_data (4, 0xffffffff,
11363 "Initial length escape value indicating 64-bit DWARF extension");
11364 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11365 "Length of Address Ranges Info");
11368 /* Version number for aranges is still 2, even up to DWARF5. */
11369 dw2_asm_output_data (2, 2, "DWARF aranges version");
11370 if (dwarf_split_debug_info)
11371 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11372 debug_skeleton_info_section,
11373 "Offset of Compilation Unit Info");
11374 else
11375 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11376 debug_info_section,
11377 "Offset of Compilation Unit Info");
11378 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11379 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11381 /* We need to align to twice the pointer size here. */
11382 if (DWARF_ARANGES_PAD_SIZE)
11384 /* Pad using a 2 byte words so that padding is correct for any
11385 pointer size. */
11386 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11387 2 * DWARF2_ADDR_SIZE);
11388 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11389 dw2_asm_output_data (2, 0, NULL);
11392 /* It is necessary not to output these entries if the sections were
11393 not used; if the sections were not used, the length will be 0 and
11394 the address may end up as 0 if the section is discarded by ld
11395 --gc-sections, leaving an invalid (0, 0) entry that can be
11396 confused with the terminator. */
11397 if (text_section_used)
11399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11400 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11401 text_section_label, "Length");
11403 if (cold_text_section_used)
11405 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11406 "Address");
11407 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11408 cold_text_section_label, "Length");
11411 if (have_multiple_function_sections)
11413 unsigned fde_idx;
11414 dw_fde_ref fde;
11416 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11418 if (DECL_IGNORED_P (fde->decl))
11419 continue;
11420 if (!fde->in_std_section)
11422 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11423 "Address");
11424 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11425 fde->dw_fde_begin, "Length");
11427 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11429 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11430 "Address");
11431 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11432 fde->dw_fde_second_begin, "Length");
11437 /* Output the terminator words. */
11438 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11439 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11442 /* Add a new entry to .debug_ranges. Return its index into
11443 ranges_table vector. */
11445 static unsigned int
11446 add_ranges_num (int num, bool maybe_new_sec)
11448 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11449 vec_safe_push (ranges_table, r);
11450 return vec_safe_length (ranges_table) - 1;
11453 /* Add a new entry to .debug_ranges corresponding to a block, or a
11454 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11455 this entry might be in a different section from previous range. */
11457 static unsigned int
11458 add_ranges (const_tree block, bool maybe_new_sec)
11460 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11463 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11464 chain, or middle entry of a chain that will be directly referred to. */
11466 static void
11467 note_rnglist_head (unsigned int offset)
11469 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11470 return;
11471 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11474 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11475 When using dwarf_split_debug_info, address attributes in dies destined
11476 for the final executable should be direct references--setting the
11477 parameter force_direct ensures this behavior. */
11479 static void
11480 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11481 bool *added, bool force_direct)
11483 unsigned int in_use = vec_safe_length (ranges_by_label);
11484 unsigned int offset;
11485 dw_ranges_by_label rbl = { begin, end };
11486 vec_safe_push (ranges_by_label, rbl);
11487 offset = add_ranges_num (-(int)in_use - 1, true);
11488 if (!*added)
11490 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11491 *added = true;
11492 note_rnglist_head (offset);
11496 /* Emit .debug_ranges section. */
11498 static void
11499 output_ranges (void)
11501 unsigned i;
11502 static const char *const start_fmt = "Offset %#x";
11503 const char *fmt = start_fmt;
11504 dw_ranges *r;
11506 switch_to_section (debug_ranges_section);
11507 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11508 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11510 int block_num = r->num;
11512 if (block_num > 0)
11514 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11515 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11517 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11518 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11520 /* If all code is in the text section, then the compilation
11521 unit base address defaults to DW_AT_low_pc, which is the
11522 base of the text section. */
11523 if (!have_multiple_function_sections)
11525 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11526 text_section_label,
11527 fmt, i * 2 * DWARF2_ADDR_SIZE);
11528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11529 text_section_label, NULL);
11532 /* Otherwise, the compilation unit base address is zero,
11533 which allows us to use absolute addresses, and not worry
11534 about whether the target supports cross-section
11535 arithmetic. */
11536 else
11538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11539 fmt, i * 2 * DWARF2_ADDR_SIZE);
11540 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11543 fmt = NULL;
11546 /* Negative block_num stands for an index into ranges_by_label. */
11547 else if (block_num < 0)
11549 int lab_idx = - block_num - 1;
11551 if (!have_multiple_function_sections)
11553 gcc_unreachable ();
11554 #if 0
11555 /* If we ever use add_ranges_by_labels () for a single
11556 function section, all we have to do is to take out
11557 the #if 0 above. */
11558 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11559 (*ranges_by_label)[lab_idx].begin,
11560 text_section_label,
11561 fmt, i * 2 * DWARF2_ADDR_SIZE);
11562 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11563 (*ranges_by_label)[lab_idx].end,
11564 text_section_label, NULL);
11565 #endif
11567 else
11569 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11570 (*ranges_by_label)[lab_idx].begin,
11571 fmt, i * 2 * DWARF2_ADDR_SIZE);
11572 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11573 (*ranges_by_label)[lab_idx].end,
11574 NULL);
11577 else
11579 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11580 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11581 fmt = start_fmt;
11586 /* Non-zero if .debug_line_str should be used for .debug_line section
11587 strings or strings that are likely shareable with those. */
11588 #define DWARF5_USE_DEBUG_LINE_STR \
11589 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11590 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11591 /* FIXME: there is no .debug_line_str.dwo section, \
11592 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11593 && !dwarf_split_debug_info)
11595 /* Assign .debug_rnglists indexes. */
11597 static void
11598 index_rnglists (void)
11600 unsigned i;
11601 dw_ranges *r;
11603 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11604 if (r->label)
11605 r->idx = rnglist_idx++;
11608 /* Emit .debug_rnglists section. */
11610 static void
11611 output_rnglists (unsigned generation)
11613 unsigned i;
11614 dw_ranges *r;
11615 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11616 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11617 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11619 switch_to_section (debug_ranges_section);
11620 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11621 /* There are up to 4 unique ranges labels per generation.
11622 See also init_sections_and_labels. */
11623 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11624 2 + generation * 4);
11625 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11626 3 + generation * 4);
11627 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11628 dw2_asm_output_data (4, 0xffffffff,
11629 "Initial length escape value indicating "
11630 "64-bit DWARF extension");
11631 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11632 "Length of Range Lists");
11633 ASM_OUTPUT_LABEL (asm_out_file, l1);
11634 output_dwarf_version ();
11635 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11636 dw2_asm_output_data (1, 0, "Segment Size");
11637 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11638 about relocation sizes and primarily care about the size of .debug*
11639 sections in linked shared libraries and executables, then
11640 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11641 into it are usually larger than just DW_FORM_sec_offset offsets
11642 into the .debug_rnglists section. */
11643 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11644 "Offset Entry Count");
11645 if (dwarf_split_debug_info)
11647 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11648 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11649 if (r->label)
11650 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11651 ranges_base_label, NULL);
11654 const char *lab = "";
11655 unsigned int len = vec_safe_length (ranges_table);
11656 const char *base = NULL;
11657 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11659 int block_num = r->num;
11661 if (r->label)
11663 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11664 lab = r->label;
11666 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11667 base = NULL;
11668 if (block_num > 0)
11670 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11671 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11673 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11674 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11676 if (HAVE_AS_LEB128)
11678 /* If all code is in the text section, then the compilation
11679 unit base address defaults to DW_AT_low_pc, which is the
11680 base of the text section. */
11681 if (!have_multiple_function_sections)
11683 dw2_asm_output_data (1, DW_RLE_offset_pair,
11684 "DW_RLE_offset_pair (%s)", lab);
11685 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11686 "Range begin address (%s)", lab);
11687 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11688 "Range end address (%s)", lab);
11689 continue;
11691 if (base == NULL)
11693 dw_ranges *r2 = NULL;
11694 if (i < len - 1)
11695 r2 = &(*ranges_table)[i + 1];
11696 if (r2
11697 && r2->num != 0
11698 && r2->label == NULL
11699 && !r2->maybe_new_sec)
11701 dw2_asm_output_data (1, DW_RLE_base_address,
11702 "DW_RLE_base_address (%s)", lab);
11703 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11704 "Base address (%s)", lab);
11705 strcpy (basebuf, blabel);
11706 base = basebuf;
11709 if (base)
11711 dw2_asm_output_data (1, DW_RLE_offset_pair,
11712 "DW_RLE_offset_pair (%s)", lab);
11713 dw2_asm_output_delta_uleb128 (blabel, base,
11714 "Range begin address (%s)", lab);
11715 dw2_asm_output_delta_uleb128 (elabel, base,
11716 "Range end address (%s)", lab);
11717 continue;
11719 dw2_asm_output_data (1, DW_RLE_start_length,
11720 "DW_RLE_start_length (%s)", lab);
11721 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11722 "Range begin address (%s)", lab);
11723 dw2_asm_output_delta_uleb128 (elabel, blabel,
11724 "Range length (%s)", lab);
11726 else
11728 dw2_asm_output_data (1, DW_RLE_start_end,
11729 "DW_RLE_start_end (%s)", lab);
11730 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11731 "Range begin address (%s)", lab);
11732 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11733 "Range end address (%s)", lab);
11737 /* Negative block_num stands for an index into ranges_by_label. */
11738 else if (block_num < 0)
11740 int lab_idx = - block_num - 1;
11741 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11742 const char *elabel = (*ranges_by_label)[lab_idx].end;
11744 if (!have_multiple_function_sections)
11745 gcc_unreachable ();
11746 if (HAVE_AS_LEB128)
11748 dw2_asm_output_data (1, DW_RLE_start_length,
11749 "DW_RLE_start_length (%s)", lab);
11750 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11751 "Range begin address (%s)", lab);
11752 dw2_asm_output_delta_uleb128 (elabel, blabel,
11753 "Range length (%s)", lab);
11755 else
11757 dw2_asm_output_data (1, DW_RLE_start_end,
11758 "DW_RLE_start_end (%s)", lab);
11759 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11760 "Range begin address (%s)", lab);
11761 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11762 "Range end address (%s)", lab);
11765 else
11766 dw2_asm_output_data (1, DW_RLE_end_of_list,
11767 "DW_RLE_end_of_list (%s)", lab);
11769 ASM_OUTPUT_LABEL (asm_out_file, l2);
11772 /* Data structure containing information about input files. */
11773 struct file_info
11775 const char *path; /* Complete file name. */
11776 const char *fname; /* File name part. */
11777 int length; /* Length of entire string. */
11778 struct dwarf_file_data * file_idx; /* Index in input file table. */
11779 int dir_idx; /* Index in directory table. */
11782 /* Data structure containing information about directories with source
11783 files. */
11784 struct dir_info
11786 const char *path; /* Path including directory name. */
11787 int length; /* Path length. */
11788 int prefix; /* Index of directory entry which is a prefix. */
11789 int count; /* Number of files in this directory. */
11790 int dir_idx; /* Index of directory used as base. */
11793 /* Callback function for file_info comparison. We sort by looking at
11794 the directories in the path. */
11796 static int
11797 file_info_cmp (const void *p1, const void *p2)
11799 const struct file_info *const s1 = (const struct file_info *) p1;
11800 const struct file_info *const s2 = (const struct file_info *) p2;
11801 const unsigned char *cp1;
11802 const unsigned char *cp2;
11804 /* Take care of file names without directories. We need to make sure that
11805 we return consistent values to qsort since some will get confused if
11806 we return the same value when identical operands are passed in opposite
11807 orders. So if neither has a directory, return 0 and otherwise return
11808 1 or -1 depending on which one has the directory. */
11809 if ((s1->path == s1->fname || s2->path == s2->fname))
11810 return (s2->path == s2->fname) - (s1->path == s1->fname);
11812 cp1 = (const unsigned char *) s1->path;
11813 cp2 = (const unsigned char *) s2->path;
11815 while (1)
11817 ++cp1;
11818 ++cp2;
11819 /* Reached the end of the first path? If so, handle like above. */
11820 if ((cp1 == (const unsigned char *) s1->fname)
11821 || (cp2 == (const unsigned char *) s2->fname))
11822 return ((cp2 == (const unsigned char *) s2->fname)
11823 - (cp1 == (const unsigned char *) s1->fname));
11825 /* Character of current path component the same? */
11826 else if (*cp1 != *cp2)
11827 return *cp1 - *cp2;
11831 struct file_name_acquire_data
11833 struct file_info *files;
11834 int used_files;
11835 int max_files;
11838 /* Traversal function for the hash table. */
11841 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11843 struct dwarf_file_data *d = *slot;
11844 struct file_info *fi;
11845 const char *f;
11847 gcc_assert (fnad->max_files >= d->emitted_number);
11849 if (! d->emitted_number)
11850 return 1;
11852 gcc_assert (fnad->max_files != fnad->used_files);
11854 fi = fnad->files + fnad->used_files++;
11856 /* Skip all leading "./". */
11857 f = d->filename;
11858 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11859 f += 2;
11861 /* Create a new array entry. */
11862 fi->path = f;
11863 fi->length = strlen (f);
11864 fi->file_idx = d;
11866 /* Search for the file name part. */
11867 f = strrchr (f, DIR_SEPARATOR);
11868 #if defined (DIR_SEPARATOR_2)
11870 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11872 if (g != NULL)
11874 if (f == NULL || f < g)
11875 f = g;
11878 #endif
11880 fi->fname = f == NULL ? fi->path : f + 1;
11881 return 1;
11884 /* Helper function for output_file_names. Emit a FORM encoded
11885 string STR, with assembly comment start ENTRY_KIND and
11886 index IDX */
11888 static void
11889 output_line_string (enum dwarf_form form, const char *str,
11890 const char *entry_kind, unsigned int idx)
11892 switch (form)
11894 case DW_FORM_string:
11895 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11896 break;
11897 case DW_FORM_line_strp:
11898 if (!debug_line_str_hash)
11899 debug_line_str_hash
11900 = hash_table<indirect_string_hasher>::create_ggc (10);
11902 struct indirect_string_node *node;
11903 node = find_AT_string_in_table (str, debug_line_str_hash);
11904 set_indirect_string (node);
11905 node->form = form;
11906 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11907 debug_line_str_section, "%s: %#x: \"%s\"",
11908 entry_kind, 0, node->str);
11909 break;
11910 default:
11911 gcc_unreachable ();
11915 /* Output the directory table and the file name table. We try to minimize
11916 the total amount of memory needed. A heuristic is used to avoid large
11917 slowdowns with many input files. */
11919 static void
11920 output_file_names (void)
11922 struct file_name_acquire_data fnad;
11923 int numfiles;
11924 struct file_info *files;
11925 struct dir_info *dirs;
11926 int *saved;
11927 int *savehere;
11928 int *backmap;
11929 int ndirs;
11930 int idx_offset;
11931 int i;
11933 if (!last_emitted_file)
11935 if (dwarf_version >= 5)
11937 dw2_asm_output_data (1, 0, "Directory entry format count");
11938 dw2_asm_output_data_uleb128 (0, "Directories count");
11939 dw2_asm_output_data (1, 0, "File name entry format count");
11940 dw2_asm_output_data_uleb128 (0, "File names count");
11942 else
11944 dw2_asm_output_data (1, 0, "End directory table");
11945 dw2_asm_output_data (1, 0, "End file name table");
11947 return;
11950 numfiles = last_emitted_file->emitted_number;
11952 /* Allocate the various arrays we need. */
11953 files = XALLOCAVEC (struct file_info, numfiles);
11954 dirs = XALLOCAVEC (struct dir_info, numfiles);
11956 fnad.files = files;
11957 fnad.used_files = 0;
11958 fnad.max_files = numfiles;
11959 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11960 gcc_assert (fnad.used_files == fnad.max_files);
11962 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11964 /* Find all the different directories used. */
11965 dirs[0].path = files[0].path;
11966 dirs[0].length = files[0].fname - files[0].path;
11967 dirs[0].prefix = -1;
11968 dirs[0].count = 1;
11969 dirs[0].dir_idx = 0;
11970 files[0].dir_idx = 0;
11971 ndirs = 1;
11973 for (i = 1; i < numfiles; i++)
11974 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11975 && memcmp (dirs[ndirs - 1].path, files[i].path,
11976 dirs[ndirs - 1].length) == 0)
11978 /* Same directory as last entry. */
11979 files[i].dir_idx = ndirs - 1;
11980 ++dirs[ndirs - 1].count;
11982 else
11984 int j;
11986 /* This is a new directory. */
11987 dirs[ndirs].path = files[i].path;
11988 dirs[ndirs].length = files[i].fname - files[i].path;
11989 dirs[ndirs].count = 1;
11990 dirs[ndirs].dir_idx = ndirs;
11991 files[i].dir_idx = ndirs;
11993 /* Search for a prefix. */
11994 dirs[ndirs].prefix = -1;
11995 for (j = 0; j < ndirs; j++)
11996 if (dirs[j].length < dirs[ndirs].length
11997 && dirs[j].length > 1
11998 && (dirs[ndirs].prefix == -1
11999 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12000 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12001 dirs[ndirs].prefix = j;
12003 ++ndirs;
12006 /* Now to the actual work. We have to find a subset of the directories which
12007 allow expressing the file name using references to the directory table
12008 with the least amount of characters. We do not do an exhaustive search
12009 where we would have to check out every combination of every single
12010 possible prefix. Instead we use a heuristic which provides nearly optimal
12011 results in most cases and never is much off. */
12012 saved = XALLOCAVEC (int, ndirs);
12013 savehere = XALLOCAVEC (int, ndirs);
12015 memset (saved, '\0', ndirs * sizeof (saved[0]));
12016 for (i = 0; i < ndirs; i++)
12018 int j;
12019 int total;
12021 /* We can always save some space for the current directory. But this
12022 does not mean it will be enough to justify adding the directory. */
12023 savehere[i] = dirs[i].length;
12024 total = (savehere[i] - saved[i]) * dirs[i].count;
12026 for (j = i + 1; j < ndirs; j++)
12028 savehere[j] = 0;
12029 if (saved[j] < dirs[i].length)
12031 /* Determine whether the dirs[i] path is a prefix of the
12032 dirs[j] path. */
12033 int k;
12035 k = dirs[j].prefix;
12036 while (k != -1 && k != (int) i)
12037 k = dirs[k].prefix;
12039 if (k == (int) i)
12041 /* Yes it is. We can possibly save some memory by
12042 writing the filenames in dirs[j] relative to
12043 dirs[i]. */
12044 savehere[j] = dirs[i].length;
12045 total += (savehere[j] - saved[j]) * dirs[j].count;
12050 /* Check whether we can save enough to justify adding the dirs[i]
12051 directory. */
12052 if (total > dirs[i].length + 1)
12054 /* It's worthwhile adding. */
12055 for (j = i; j < ndirs; j++)
12056 if (savehere[j] > 0)
12058 /* Remember how much we saved for this directory so far. */
12059 saved[j] = savehere[j];
12061 /* Remember the prefix directory. */
12062 dirs[j].dir_idx = i;
12067 /* Emit the directory name table. */
12068 idx_offset = dirs[0].length > 0 ? 1 : 0;
12069 enum dwarf_form str_form = DW_FORM_string;
12070 enum dwarf_form idx_form = DW_FORM_udata;
12071 if (dwarf_version >= 5)
12073 const char *comp_dir = comp_dir_string ();
12074 if (comp_dir == NULL)
12075 comp_dir = "";
12076 dw2_asm_output_data (1, 1, "Directory entry format count");
12077 if (DWARF5_USE_DEBUG_LINE_STR)
12078 str_form = DW_FORM_line_strp;
12079 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12080 dw2_asm_output_data_uleb128 (str_form, "%s",
12081 get_DW_FORM_name (str_form));
12082 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12083 if (str_form == DW_FORM_string)
12085 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12086 for (i = 1 - idx_offset; i < ndirs; i++)
12087 dw2_asm_output_nstring (dirs[i].path,
12088 dirs[i].length
12089 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12090 "Directory Entry: %#x", i + idx_offset);
12092 else
12094 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12095 for (i = 1 - idx_offset; i < ndirs; i++)
12097 const char *str
12098 = ggc_alloc_string (dirs[i].path,
12099 dirs[i].length
12100 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12101 output_line_string (str_form, str, "Directory Entry",
12102 (unsigned) i + idx_offset);
12106 else
12108 for (i = 1 - idx_offset; i < ndirs; i++)
12109 dw2_asm_output_nstring (dirs[i].path,
12110 dirs[i].length
12111 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12112 "Directory Entry: %#x", i + idx_offset);
12114 dw2_asm_output_data (1, 0, "End directory table");
12117 /* We have to emit them in the order of emitted_number since that's
12118 used in the debug info generation. To do this efficiently we
12119 generate a back-mapping of the indices first. */
12120 backmap = XALLOCAVEC (int, numfiles);
12121 for (i = 0; i < numfiles; i++)
12122 backmap[files[i].file_idx->emitted_number - 1] = i;
12124 if (dwarf_version >= 5)
12126 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12127 if (filename0 == NULL)
12128 filename0 = "";
12129 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12130 DW_FORM_data2. Choose one based on the number of directories
12131 and how much space would they occupy in each encoding.
12132 If we have at most 256 directories, all indexes fit into
12133 a single byte, so DW_FORM_data1 is most compact (if there
12134 are at most 128 directories, DW_FORM_udata would be as
12135 compact as that, but not shorter and slower to decode). */
12136 if (ndirs + idx_offset <= 256)
12137 idx_form = DW_FORM_data1;
12138 /* If there are more than 65536 directories, we have to use
12139 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12140 Otherwise, compute what space would occupy if all the indexes
12141 used DW_FORM_udata - sum - and compare that to how large would
12142 be DW_FORM_data2 encoding, and pick the more efficient one. */
12143 else if (ndirs + idx_offset <= 65536)
12145 unsigned HOST_WIDE_INT sum = 1;
12146 for (i = 0; i < numfiles; i++)
12148 int file_idx = backmap[i];
12149 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12150 sum += size_of_uleb128 (dir_idx);
12152 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12153 idx_form = DW_FORM_data2;
12155 #ifdef VMS_DEBUGGING_INFO
12156 dw2_asm_output_data (1, 4, "File name entry format count");
12157 #else
12158 dw2_asm_output_data (1, 2, "File name entry format count");
12159 #endif
12160 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12161 dw2_asm_output_data_uleb128 (str_form, "%s",
12162 get_DW_FORM_name (str_form));
12163 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12164 "DW_LNCT_directory_index");
12165 dw2_asm_output_data_uleb128 (idx_form, "%s",
12166 get_DW_FORM_name (idx_form));
12167 #ifdef VMS_DEBUGGING_INFO
12168 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12169 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12170 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12171 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12172 #endif
12173 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12175 output_line_string (str_form, filename0, "File Entry", 0);
12177 /* Include directory index. */
12178 if (idx_form != DW_FORM_udata)
12179 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12180 0, NULL);
12181 else
12182 dw2_asm_output_data_uleb128 (0, NULL);
12184 #ifdef VMS_DEBUGGING_INFO
12185 dw2_asm_output_data_uleb128 (0, NULL);
12186 dw2_asm_output_data_uleb128 (0, NULL);
12187 #endif
12190 /* Now write all the file names. */
12191 for (i = 0; i < numfiles; i++)
12193 int file_idx = backmap[i];
12194 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12196 #ifdef VMS_DEBUGGING_INFO
12197 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12199 /* Setting these fields can lead to debugger miscomparisons,
12200 but VMS Debug requires them to be set correctly. */
12202 int ver;
12203 long long cdt;
12204 long siz;
12205 int maxfilelen = (strlen (files[file_idx].path)
12206 + dirs[dir_idx].length
12207 + MAX_VMS_VERSION_LEN + 1);
12208 char *filebuf = XALLOCAVEC (char, maxfilelen);
12210 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12211 snprintf (filebuf, maxfilelen, "%s;%d",
12212 files[file_idx].path + dirs[dir_idx].length, ver);
12214 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12216 /* Include directory index. */
12217 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12218 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12219 dir_idx + idx_offset, NULL);
12220 else
12221 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12223 /* Modification time. */
12224 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12225 &cdt, 0, 0, 0) == 0)
12226 ? cdt : 0, NULL);
12228 /* File length in bytes. */
12229 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12230 0, &siz, 0, 0) == 0)
12231 ? siz : 0, NULL);
12232 #else
12233 output_line_string (str_form,
12234 files[file_idx].path + dirs[dir_idx].length,
12235 "File Entry", (unsigned) i + 1);
12237 /* Include directory index. */
12238 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12239 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12240 dir_idx + idx_offset, NULL);
12241 else
12242 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12244 if (dwarf_version >= 5)
12245 continue;
12247 /* Modification time. */
12248 dw2_asm_output_data_uleb128 (0, NULL);
12250 /* File length in bytes. */
12251 dw2_asm_output_data_uleb128 (0, NULL);
12252 #endif /* VMS_DEBUGGING_INFO */
12255 if (dwarf_version < 5)
12256 dw2_asm_output_data (1, 0, "End file name table");
12260 /* Output one line number table into the .debug_line section. */
12262 static void
12263 output_one_line_info_table (dw_line_info_table *table)
12265 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12266 unsigned int current_line = 1;
12267 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12268 dw_line_info_entry *ent, *prev_addr;
12269 size_t i;
12270 unsigned int view;
12272 view = 0;
12274 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12276 switch (ent->opcode)
12278 case LI_set_address:
12279 /* ??? Unfortunately, we have little choice here currently, and
12280 must always use the most general form. GCC does not know the
12281 address delta itself, so we can't use DW_LNS_advance_pc. Many
12282 ports do have length attributes which will give an upper bound
12283 on the address range. We could perhaps use length attributes
12284 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12285 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12287 view = 0;
12289 /* This can handle any delta. This takes
12290 4+DWARF2_ADDR_SIZE bytes. */
12291 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12292 debug_variable_location_views
12293 ? ", reset view to 0" : "");
12294 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12295 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12296 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12298 prev_addr = ent;
12299 break;
12301 case LI_adv_address:
12303 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12304 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12305 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12307 view++;
12309 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12310 dw2_asm_output_delta (2, line_label, prev_label,
12311 "from %s to %s", prev_label, line_label);
12313 prev_addr = ent;
12314 break;
12317 case LI_set_line:
12318 if (ent->val == current_line)
12320 /* We still need to start a new row, so output a copy insn. */
12321 dw2_asm_output_data (1, DW_LNS_copy,
12322 "copy line %u", current_line);
12324 else
12326 int line_offset = ent->val - current_line;
12327 int line_delta = line_offset - DWARF_LINE_BASE;
12329 current_line = ent->val;
12330 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12332 /* This can handle deltas from -10 to 234, using the current
12333 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12334 This takes 1 byte. */
12335 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12336 "line %u", current_line);
12338 else
12340 /* This can handle any delta. This takes at least 4 bytes,
12341 depending on the value being encoded. */
12342 dw2_asm_output_data (1, DW_LNS_advance_line,
12343 "advance to line %u", current_line);
12344 dw2_asm_output_data_sleb128 (line_offset, NULL);
12345 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12348 break;
12350 case LI_set_file:
12351 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12352 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12353 break;
12355 case LI_set_column:
12356 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12357 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12358 break;
12360 case LI_negate_stmt:
12361 current_is_stmt = !current_is_stmt;
12362 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12363 "is_stmt %d", current_is_stmt);
12364 break;
12366 case LI_set_prologue_end:
12367 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12368 "set prologue end");
12369 break;
12371 case LI_set_epilogue_begin:
12372 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12373 "set epilogue begin");
12374 break;
12376 case LI_set_discriminator:
12377 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12378 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12379 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12380 dw2_asm_output_data_uleb128 (ent->val, NULL);
12381 break;
12385 /* Emit debug info for the address of the end of the table. */
12386 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12387 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12388 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12389 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12391 dw2_asm_output_data (1, 0, "end sequence");
12392 dw2_asm_output_data_uleb128 (1, NULL);
12393 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12396 /* Output the source line number correspondence information. This
12397 information goes into the .debug_line section. */
12399 static void
12400 output_line_info (bool prologue_only)
12402 static unsigned int generation;
12403 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12404 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12405 bool saw_one = false;
12406 int opc;
12408 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12409 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12410 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12411 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12413 if (!XCOFF_DEBUGGING_INFO)
12415 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12416 dw2_asm_output_data (4, 0xffffffff,
12417 "Initial length escape value indicating 64-bit DWARF extension");
12418 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12419 "Length of Source Line Info");
12422 ASM_OUTPUT_LABEL (asm_out_file, l1);
12424 output_dwarf_version ();
12425 if (dwarf_version >= 5)
12427 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12428 dw2_asm_output_data (1, 0, "Segment Size");
12430 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12431 ASM_OUTPUT_LABEL (asm_out_file, p1);
12433 /* Define the architecture-dependent minimum instruction length (in bytes).
12434 In this implementation of DWARF, this field is used for information
12435 purposes only. Since GCC generates assembly language, we have no
12436 a priori knowledge of how many instruction bytes are generated for each
12437 source line, and therefore can use only the DW_LNE_set_address and
12438 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12439 this as '1', which is "correct enough" for all architectures,
12440 and don't let the target override. */
12441 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12443 if (dwarf_version >= 4)
12444 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12445 "Maximum Operations Per Instruction");
12446 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12447 "Default is_stmt_start flag");
12448 dw2_asm_output_data (1, DWARF_LINE_BASE,
12449 "Line Base Value (Special Opcodes)");
12450 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12451 "Line Range Value (Special Opcodes)");
12452 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12453 "Special Opcode Base");
12455 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12457 int n_op_args;
12458 switch (opc)
12460 case DW_LNS_advance_pc:
12461 case DW_LNS_advance_line:
12462 case DW_LNS_set_file:
12463 case DW_LNS_set_column:
12464 case DW_LNS_fixed_advance_pc:
12465 case DW_LNS_set_isa:
12466 n_op_args = 1;
12467 break;
12468 default:
12469 n_op_args = 0;
12470 break;
12473 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12474 opc, n_op_args);
12477 /* Write out the information about the files we use. */
12478 output_file_names ();
12479 ASM_OUTPUT_LABEL (asm_out_file, p2);
12480 if (prologue_only)
12482 /* Output the marker for the end of the line number info. */
12483 ASM_OUTPUT_LABEL (asm_out_file, l2);
12484 return;
12487 if (separate_line_info)
12489 dw_line_info_table *table;
12490 size_t i;
12492 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12493 if (table->in_use)
12495 output_one_line_info_table (table);
12496 saw_one = true;
12499 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12501 output_one_line_info_table (cold_text_section_line_info);
12502 saw_one = true;
12505 /* ??? Some Darwin linkers crash on a .debug_line section with no
12506 sequences. Further, merely a DW_LNE_end_sequence entry is not
12507 sufficient -- the address column must also be initialized.
12508 Make sure to output at least one set_address/end_sequence pair,
12509 choosing .text since that section is always present. */
12510 if (text_section_line_info->in_use || !saw_one)
12511 output_one_line_info_table (text_section_line_info);
12513 /* Output the marker for the end of the line number info. */
12514 ASM_OUTPUT_LABEL (asm_out_file, l2);
12517 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12519 static inline bool
12520 need_endianity_attribute_p (bool reverse)
12522 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12525 /* Given a pointer to a tree node for some base type, return a pointer to
12526 a DIE that describes the given type. REVERSE is true if the type is
12527 to be interpreted in the reverse storage order wrt the target order.
12529 This routine must only be called for GCC type nodes that correspond to
12530 Dwarf base (fundamental) types. */
12532 static dw_die_ref
12533 base_type_die (tree type, bool reverse)
12535 dw_die_ref base_type_result;
12536 enum dwarf_type encoding;
12537 bool fpt_used = false;
12538 struct fixed_point_type_info fpt_info;
12539 tree type_bias = NULL_TREE;
12541 /* If this is a subtype that should not be emitted as a subrange type,
12542 use the base type. See subrange_type_for_debug_p. */
12543 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12544 type = TREE_TYPE (type);
12546 switch (TREE_CODE (type))
12548 case INTEGER_TYPE:
12549 if ((dwarf_version >= 4 || !dwarf_strict)
12550 && TYPE_NAME (type)
12551 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12552 && DECL_IS_BUILTIN (TYPE_NAME (type))
12553 && DECL_NAME (TYPE_NAME (type)))
12555 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12556 if (strcmp (name, "char16_t") == 0
12557 || strcmp (name, "char32_t") == 0)
12559 encoding = DW_ATE_UTF;
12560 break;
12563 if ((dwarf_version >= 3 || !dwarf_strict)
12564 && lang_hooks.types.get_fixed_point_type_info)
12566 memset (&fpt_info, 0, sizeof (fpt_info));
12567 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12569 fpt_used = true;
12570 encoding = ((TYPE_UNSIGNED (type))
12571 ? DW_ATE_unsigned_fixed
12572 : DW_ATE_signed_fixed);
12573 break;
12576 if (TYPE_STRING_FLAG (type))
12578 if (TYPE_UNSIGNED (type))
12579 encoding = DW_ATE_unsigned_char;
12580 else
12581 encoding = DW_ATE_signed_char;
12583 else if (TYPE_UNSIGNED (type))
12584 encoding = DW_ATE_unsigned;
12585 else
12586 encoding = DW_ATE_signed;
12588 if (!dwarf_strict
12589 && lang_hooks.types.get_type_bias)
12590 type_bias = lang_hooks.types.get_type_bias (type);
12591 break;
12593 case REAL_TYPE:
12594 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12596 if (dwarf_version >= 3 || !dwarf_strict)
12597 encoding = DW_ATE_decimal_float;
12598 else
12599 encoding = DW_ATE_lo_user;
12601 else
12602 encoding = DW_ATE_float;
12603 break;
12605 case FIXED_POINT_TYPE:
12606 if (!(dwarf_version >= 3 || !dwarf_strict))
12607 encoding = DW_ATE_lo_user;
12608 else if (TYPE_UNSIGNED (type))
12609 encoding = DW_ATE_unsigned_fixed;
12610 else
12611 encoding = DW_ATE_signed_fixed;
12612 break;
12614 /* Dwarf2 doesn't know anything about complex ints, so use
12615 a user defined type for it. */
12616 case COMPLEX_TYPE:
12617 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12618 encoding = DW_ATE_complex_float;
12619 else
12620 encoding = DW_ATE_lo_user;
12621 break;
12623 case BOOLEAN_TYPE:
12624 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12625 encoding = DW_ATE_boolean;
12626 break;
12628 default:
12629 /* No other TREE_CODEs are Dwarf fundamental types. */
12630 gcc_unreachable ();
12633 base_type_result = new_die_raw (DW_TAG_base_type);
12635 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12636 int_size_in_bytes (type));
12637 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12639 if (need_endianity_attribute_p (reverse))
12640 add_AT_unsigned (base_type_result, DW_AT_endianity,
12641 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12643 add_alignment_attribute (base_type_result, type);
12645 if (fpt_used)
12647 switch (fpt_info.scale_factor_kind)
12649 case fixed_point_scale_factor_binary:
12650 add_AT_int (base_type_result, DW_AT_binary_scale,
12651 fpt_info.scale_factor.binary);
12652 break;
12654 case fixed_point_scale_factor_decimal:
12655 add_AT_int (base_type_result, DW_AT_decimal_scale,
12656 fpt_info.scale_factor.decimal);
12657 break;
12659 case fixed_point_scale_factor_arbitrary:
12660 /* Arbitrary scale factors cannot be described in standard DWARF,
12661 yet. */
12662 if (!dwarf_strict)
12664 /* Describe the scale factor as a rational constant. */
12665 const dw_die_ref scale_factor
12666 = new_die (DW_TAG_constant, comp_unit_die (), type);
12668 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12669 fpt_info.scale_factor.arbitrary.numerator);
12670 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12671 fpt_info.scale_factor.arbitrary.denominator);
12673 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12675 break;
12677 default:
12678 gcc_unreachable ();
12682 if (type_bias)
12683 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12684 dw_scalar_form_constant
12685 | dw_scalar_form_exprloc
12686 | dw_scalar_form_reference,
12687 NULL);
12689 return base_type_result;
12692 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12693 named 'auto' in its type: return true for it, false otherwise. */
12695 static inline bool
12696 is_cxx_auto (tree type)
12698 if (is_cxx ())
12700 tree name = TYPE_IDENTIFIER (type);
12701 if (name == get_identifier ("auto")
12702 || name == get_identifier ("decltype(auto)"))
12703 return true;
12705 return false;
12708 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12709 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12711 static inline int
12712 is_base_type (tree type)
12714 switch (TREE_CODE (type))
12716 case INTEGER_TYPE:
12717 case REAL_TYPE:
12718 case FIXED_POINT_TYPE:
12719 case COMPLEX_TYPE:
12720 case BOOLEAN_TYPE:
12721 case POINTER_BOUNDS_TYPE:
12722 return 1;
12724 case VOID_TYPE:
12725 case ARRAY_TYPE:
12726 case RECORD_TYPE:
12727 case UNION_TYPE:
12728 case QUAL_UNION_TYPE:
12729 case ENUMERAL_TYPE:
12730 case FUNCTION_TYPE:
12731 case METHOD_TYPE:
12732 case POINTER_TYPE:
12733 case REFERENCE_TYPE:
12734 case NULLPTR_TYPE:
12735 case OFFSET_TYPE:
12736 case LANG_TYPE:
12737 case VECTOR_TYPE:
12738 return 0;
12740 default:
12741 if (is_cxx_auto (type))
12742 return 0;
12743 gcc_unreachable ();
12746 return 0;
12749 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12750 node, return the size in bits for the type if it is a constant, or else
12751 return the alignment for the type if the type's size is not constant, or
12752 else return BITS_PER_WORD if the type actually turns out to be an
12753 ERROR_MARK node. */
12755 static inline unsigned HOST_WIDE_INT
12756 simple_type_size_in_bits (const_tree type)
12758 if (TREE_CODE (type) == ERROR_MARK)
12759 return BITS_PER_WORD;
12760 else if (TYPE_SIZE (type) == NULL_TREE)
12761 return 0;
12762 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12763 return tree_to_uhwi (TYPE_SIZE (type));
12764 else
12765 return TYPE_ALIGN (type);
12768 /* Similarly, but return an offset_int instead of UHWI. */
12770 static inline offset_int
12771 offset_int_type_size_in_bits (const_tree type)
12773 if (TREE_CODE (type) == ERROR_MARK)
12774 return BITS_PER_WORD;
12775 else if (TYPE_SIZE (type) == NULL_TREE)
12776 return 0;
12777 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12778 return wi::to_offset (TYPE_SIZE (type));
12779 else
12780 return TYPE_ALIGN (type);
12783 /* Given a pointer to a tree node for a subrange type, return a pointer
12784 to a DIE that describes the given type. */
12786 static dw_die_ref
12787 subrange_type_die (tree type, tree low, tree high, tree bias,
12788 dw_die_ref context_die)
12790 dw_die_ref subrange_die;
12791 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12793 if (context_die == NULL)
12794 context_die = comp_unit_die ();
12796 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12798 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12800 /* The size of the subrange type and its base type do not match,
12801 so we need to generate a size attribute for the subrange type. */
12802 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12805 add_alignment_attribute (subrange_die, type);
12807 if (low)
12808 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12809 if (high)
12810 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12811 if (bias && !dwarf_strict)
12812 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12813 dw_scalar_form_constant
12814 | dw_scalar_form_exprloc
12815 | dw_scalar_form_reference,
12816 NULL);
12818 return subrange_die;
12821 /* Returns the (const and/or volatile) cv_qualifiers associated with
12822 the decl node. This will normally be augmented with the
12823 cv_qualifiers of the underlying type in add_type_attribute. */
12825 static int
12826 decl_quals (const_tree decl)
12828 return ((TREE_READONLY (decl)
12829 /* The C++ front-end correctly marks reference-typed
12830 variables as readonly, but from a language (and debug
12831 info) standpoint they are not const-qualified. */
12832 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12833 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12834 | (TREE_THIS_VOLATILE (decl)
12835 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12838 /* Determine the TYPE whose qualifiers match the largest strict subset
12839 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12840 qualifiers outside QUAL_MASK. */
12842 static int
12843 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12845 tree t;
12846 int best_rank = 0, best_qual = 0, max_rank;
12848 type_quals &= qual_mask;
12849 max_rank = popcount_hwi (type_quals) - 1;
12851 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12852 t = TYPE_NEXT_VARIANT (t))
12854 int q = TYPE_QUALS (t) & qual_mask;
12856 if ((q & type_quals) == q && q != type_quals
12857 && check_base_type (t, type))
12859 int rank = popcount_hwi (q);
12861 if (rank > best_rank)
12863 best_rank = rank;
12864 best_qual = q;
12869 return best_qual;
12872 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12873 static const dwarf_qual_info_t dwarf_qual_info[] =
12875 { TYPE_QUAL_CONST, DW_TAG_const_type },
12876 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12877 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12878 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12880 static const unsigned int dwarf_qual_info_size
12881 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12883 /* If DIE is a qualified DIE of some base DIE with the same parent,
12884 return the base DIE, otherwise return NULL. Set MASK to the
12885 qualifiers added compared to the returned DIE. */
12887 static dw_die_ref
12888 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12890 unsigned int i;
12891 for (i = 0; i < dwarf_qual_info_size; i++)
12892 if (die->die_tag == dwarf_qual_info[i].t)
12893 break;
12894 if (i == dwarf_qual_info_size)
12895 return NULL;
12896 if (vec_safe_length (die->die_attr) != 1)
12897 return NULL;
12898 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12899 if (type == NULL || type->die_parent != die->die_parent)
12900 return NULL;
12901 *mask |= dwarf_qual_info[i].q;
12902 if (depth)
12904 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12905 if (ret)
12906 return ret;
12908 return type;
12911 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12912 entry that chains the modifiers specified by CV_QUALS in front of the
12913 given type. REVERSE is true if the type is to be interpreted in the
12914 reverse storage order wrt the target order. */
12916 static dw_die_ref
12917 modified_type_die (tree type, int cv_quals, bool reverse,
12918 dw_die_ref context_die)
12920 enum tree_code code = TREE_CODE (type);
12921 dw_die_ref mod_type_die;
12922 dw_die_ref sub_die = NULL;
12923 tree item_type = NULL;
12924 tree qualified_type;
12925 tree name, low, high;
12926 dw_die_ref mod_scope;
12927 /* Only these cv-qualifiers are currently handled. */
12928 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12929 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12930 ENCODE_QUAL_ADDR_SPACE(~0U));
12931 const bool reverse_base_type
12932 = need_endianity_attribute_p (reverse) && is_base_type (type);
12934 if (code == ERROR_MARK)
12935 return NULL;
12937 if (lang_hooks.types.get_debug_type)
12939 tree debug_type = lang_hooks.types.get_debug_type (type);
12941 if (debug_type != NULL_TREE && debug_type != type)
12942 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12945 cv_quals &= cv_qual_mask;
12947 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12948 tag modifier (and not an attribute) old consumers won't be able
12949 to handle it. */
12950 if (dwarf_version < 3)
12951 cv_quals &= ~TYPE_QUAL_RESTRICT;
12953 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12954 if (dwarf_version < 5)
12955 cv_quals &= ~TYPE_QUAL_ATOMIC;
12957 /* See if we already have the appropriately qualified variant of
12958 this type. */
12959 qualified_type = get_qualified_type (type, cv_quals);
12961 if (qualified_type == sizetype)
12963 /* Try not to expose the internal sizetype type's name. */
12964 if (TYPE_NAME (qualified_type)
12965 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12967 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12969 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12970 && (TYPE_PRECISION (t)
12971 == TYPE_PRECISION (qualified_type))
12972 && (TYPE_UNSIGNED (t)
12973 == TYPE_UNSIGNED (qualified_type)));
12974 qualified_type = t;
12976 else if (qualified_type == sizetype
12977 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12978 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12979 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12980 qualified_type = size_type_node;
12983 /* If we do, then we can just use its DIE, if it exists. */
12984 if (qualified_type)
12986 mod_type_die = lookup_type_die (qualified_type);
12988 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12989 dealt with specially: the DIE with the attribute, if it exists, is
12990 placed immediately after the regular DIE for the same base type. */
12991 if (mod_type_die
12992 && (!reverse_base_type
12993 || ((mod_type_die = mod_type_die->die_sib) != NULL
12994 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12995 return mod_type_die;
12998 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13000 /* Handle C typedef types. */
13001 if (name
13002 && TREE_CODE (name) == TYPE_DECL
13003 && DECL_ORIGINAL_TYPE (name)
13004 && !DECL_ARTIFICIAL (name))
13006 tree dtype = TREE_TYPE (name);
13008 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13009 if (qualified_type == dtype && !reverse_base_type)
13011 tree origin = decl_ultimate_origin (name);
13013 /* Typedef variants that have an abstract origin don't get their own
13014 type DIE (see gen_typedef_die), so fall back on the ultimate
13015 abstract origin instead. */
13016 if (origin != NULL && origin != name)
13017 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13018 context_die);
13020 /* For a named type, use the typedef. */
13021 gen_type_die (qualified_type, context_die);
13022 return lookup_type_die (qualified_type);
13024 else
13026 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13027 dquals &= cv_qual_mask;
13028 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13029 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13030 /* cv-unqualified version of named type. Just use
13031 the unnamed type to which it refers. */
13032 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13033 reverse, context_die);
13034 /* Else cv-qualified version of named type; fall through. */
13038 mod_scope = scope_die_for (type, context_die);
13040 if (cv_quals)
13042 int sub_quals = 0, first_quals = 0;
13043 unsigned i;
13044 dw_die_ref first = NULL, last = NULL;
13046 /* Determine a lesser qualified type that most closely matches
13047 this one. Then generate DW_TAG_* entries for the remaining
13048 qualifiers. */
13049 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13050 cv_qual_mask);
13051 if (sub_quals && use_debug_types)
13053 bool needed = false;
13054 /* If emitting type units, make sure the order of qualifiers
13055 is canonical. Thus, start from unqualified type if
13056 an earlier qualifier is missing in sub_quals, but some later
13057 one is present there. */
13058 for (i = 0; i < dwarf_qual_info_size; i++)
13059 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13060 needed = true;
13061 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13063 sub_quals = 0;
13064 break;
13067 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13068 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13070 /* As not all intermediate qualified DIEs have corresponding
13071 tree types, ensure that qualified DIEs in the same scope
13072 as their DW_AT_type are emitted after their DW_AT_type,
13073 only with other qualified DIEs for the same type possibly
13074 in between them. Determine the range of such qualified
13075 DIEs now (first being the base type, last being corresponding
13076 last qualified DIE for it). */
13077 unsigned int count = 0;
13078 first = qualified_die_p (mod_type_die, &first_quals,
13079 dwarf_qual_info_size);
13080 if (first == NULL)
13081 first = mod_type_die;
13082 gcc_assert ((first_quals & ~sub_quals) == 0);
13083 for (count = 0, last = first;
13084 count < (1U << dwarf_qual_info_size);
13085 count++, last = last->die_sib)
13087 int quals = 0;
13088 if (last == mod_scope->die_child)
13089 break;
13090 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13091 != first)
13092 break;
13096 for (i = 0; i < dwarf_qual_info_size; i++)
13097 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13099 dw_die_ref d;
13100 if (first && first != last)
13102 for (d = first->die_sib; ; d = d->die_sib)
13104 int quals = 0;
13105 qualified_die_p (d, &quals, dwarf_qual_info_size);
13106 if (quals == (first_quals | dwarf_qual_info[i].q))
13107 break;
13108 if (d == last)
13110 d = NULL;
13111 break;
13114 if (d)
13116 mod_type_die = d;
13117 continue;
13120 if (first)
13122 d = new_die_raw (dwarf_qual_info[i].t);
13123 add_child_die_after (mod_scope, d, last);
13124 last = d;
13126 else
13127 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13128 if (mod_type_die)
13129 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13130 mod_type_die = d;
13131 first_quals |= dwarf_qual_info[i].q;
13134 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13136 dwarf_tag tag = DW_TAG_pointer_type;
13137 if (code == REFERENCE_TYPE)
13139 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13140 tag = DW_TAG_rvalue_reference_type;
13141 else
13142 tag = DW_TAG_reference_type;
13144 mod_type_die = new_die (tag, mod_scope, type);
13146 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13147 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13148 add_alignment_attribute (mod_type_die, type);
13149 item_type = TREE_TYPE (type);
13151 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13152 if (!ADDR_SPACE_GENERIC_P (as))
13154 int action = targetm.addr_space.debug (as);
13155 if (action >= 0)
13157 /* Positive values indicate an address_class. */
13158 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13160 else
13162 /* Negative values indicate an (inverted) segment base reg. */
13163 dw_loc_descr_ref d
13164 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13165 add_AT_loc (mod_type_die, DW_AT_segment, d);
13169 else if (code == INTEGER_TYPE
13170 && TREE_TYPE (type) != NULL_TREE
13171 && subrange_type_for_debug_p (type, &low, &high))
13173 tree bias = NULL_TREE;
13174 if (lang_hooks.types.get_type_bias)
13175 bias = lang_hooks.types.get_type_bias (type);
13176 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13177 item_type = TREE_TYPE (type);
13179 else if (is_base_type (type))
13181 mod_type_die = base_type_die (type, reverse);
13183 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13184 if (reverse_base_type)
13186 dw_die_ref after_die
13187 = modified_type_die (type, cv_quals, false, context_die);
13188 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13190 else
13191 add_child_die (comp_unit_die (), mod_type_die);
13193 add_pubtype (type, mod_type_die);
13195 else
13197 gen_type_die (type, context_die);
13199 /* We have to get the type_main_variant here (and pass that to the
13200 `lookup_type_die' routine) because the ..._TYPE node we have
13201 might simply be a *copy* of some original type node (where the
13202 copy was created to help us keep track of typedef names) and
13203 that copy might have a different TYPE_UID from the original
13204 ..._TYPE node. */
13205 if (TREE_CODE (type) == FUNCTION_TYPE
13206 || TREE_CODE (type) == METHOD_TYPE)
13208 /* For function/method types, can't just use type_main_variant here,
13209 because that can have different ref-qualifiers for C++,
13210 but try to canonicalize. */
13211 tree main = TYPE_MAIN_VARIANT (type);
13212 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13213 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13214 && check_base_type (t, main)
13215 && check_lang_type (t, type))
13216 return lookup_type_die (t);
13217 return lookup_type_die (type);
13219 else if (TREE_CODE (type) != VECTOR_TYPE
13220 && TREE_CODE (type) != ARRAY_TYPE)
13221 return lookup_type_die (type_main_variant (type));
13222 else
13223 /* Vectors have the debugging information in the type,
13224 not the main variant. */
13225 return lookup_type_die (type);
13228 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13229 don't output a DW_TAG_typedef, since there isn't one in the
13230 user's program; just attach a DW_AT_name to the type.
13231 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13232 if the base type already has the same name. */
13233 if (name
13234 && ((TREE_CODE (name) != TYPE_DECL
13235 && (qualified_type == TYPE_MAIN_VARIANT (type)
13236 || (cv_quals == TYPE_UNQUALIFIED)))
13237 || (TREE_CODE (name) == TYPE_DECL
13238 && TREE_TYPE (name) == qualified_type
13239 && DECL_NAME (name))))
13241 if (TREE_CODE (name) == TYPE_DECL)
13242 /* Could just call add_name_and_src_coords_attributes here,
13243 but since this is a builtin type it doesn't have any
13244 useful source coordinates anyway. */
13245 name = DECL_NAME (name);
13246 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13248 /* This probably indicates a bug. */
13249 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13251 name = TYPE_IDENTIFIER (type);
13252 add_name_attribute (mod_type_die,
13253 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13256 if (qualified_type && !reverse_base_type)
13257 equate_type_number_to_die (qualified_type, mod_type_die);
13259 if (item_type)
13260 /* We must do this after the equate_type_number_to_die call, in case
13261 this is a recursive type. This ensures that the modified_type_die
13262 recursion will terminate even if the type is recursive. Recursive
13263 types are possible in Ada. */
13264 sub_die = modified_type_die (item_type,
13265 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13266 reverse,
13267 context_die);
13269 if (sub_die != NULL)
13270 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13272 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13273 if (TYPE_ARTIFICIAL (type))
13274 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13276 return mod_type_die;
13279 /* Generate DIEs for the generic parameters of T.
13280 T must be either a generic type or a generic function.
13281 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13283 static void
13284 gen_generic_params_dies (tree t)
13286 tree parms, args;
13287 int parms_num, i;
13288 dw_die_ref die = NULL;
13289 int non_default;
13291 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13292 return;
13294 if (TYPE_P (t))
13295 die = lookup_type_die (t);
13296 else if (DECL_P (t))
13297 die = lookup_decl_die (t);
13299 gcc_assert (die);
13301 parms = lang_hooks.get_innermost_generic_parms (t);
13302 if (!parms)
13303 /* T has no generic parameter. It means T is neither a generic type
13304 or function. End of story. */
13305 return;
13307 parms_num = TREE_VEC_LENGTH (parms);
13308 args = lang_hooks.get_innermost_generic_args (t);
13309 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13310 non_default = int_cst_value (TREE_CHAIN (args));
13311 else
13312 non_default = TREE_VEC_LENGTH (args);
13313 for (i = 0; i < parms_num; i++)
13315 tree parm, arg, arg_pack_elems;
13316 dw_die_ref parm_die;
13318 parm = TREE_VEC_ELT (parms, i);
13319 arg = TREE_VEC_ELT (args, i);
13320 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13321 gcc_assert (parm && TREE_VALUE (parm) && arg);
13323 if (parm && TREE_VALUE (parm) && arg)
13325 /* If PARM represents a template parameter pack,
13326 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13327 by DW_TAG_template_*_parameter DIEs for the argument
13328 pack elements of ARG. Note that ARG would then be
13329 an argument pack. */
13330 if (arg_pack_elems)
13331 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13332 arg_pack_elems,
13333 die);
13334 else
13335 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13336 true /* emit name */, die);
13337 if (i >= non_default)
13338 add_AT_flag (parm_die, DW_AT_default_value, 1);
13343 /* Create and return a DIE for PARM which should be
13344 the representation of a generic type parameter.
13345 For instance, in the C++ front end, PARM would be a template parameter.
13346 ARG is the argument to PARM.
13347 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13348 name of the PARM.
13349 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13350 as a child node. */
13352 static dw_die_ref
13353 generic_parameter_die (tree parm, tree arg,
13354 bool emit_name_p,
13355 dw_die_ref parent_die)
13357 dw_die_ref tmpl_die = NULL;
13358 const char *name = NULL;
13360 if (!parm || !DECL_NAME (parm) || !arg)
13361 return NULL;
13363 /* We support non-type generic parameters and arguments,
13364 type generic parameters and arguments, as well as
13365 generic generic parameters (a.k.a. template template parameters in C++)
13366 and arguments. */
13367 if (TREE_CODE (parm) == PARM_DECL)
13368 /* PARM is a nontype generic parameter */
13369 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13370 else if (TREE_CODE (parm) == TYPE_DECL)
13371 /* PARM is a type generic parameter. */
13372 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13373 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13374 /* PARM is a generic generic parameter.
13375 Its DIE is a GNU extension. It shall have a
13376 DW_AT_name attribute to represent the name of the template template
13377 parameter, and a DW_AT_GNU_template_name attribute to represent the
13378 name of the template template argument. */
13379 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13380 parent_die, parm);
13381 else
13382 gcc_unreachable ();
13384 if (tmpl_die)
13386 tree tmpl_type;
13388 /* If PARM is a generic parameter pack, it means we are
13389 emitting debug info for a template argument pack element.
13390 In other terms, ARG is a template argument pack element.
13391 In that case, we don't emit any DW_AT_name attribute for
13392 the die. */
13393 if (emit_name_p)
13395 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13396 gcc_assert (name);
13397 add_AT_string (tmpl_die, DW_AT_name, name);
13400 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13402 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13403 TMPL_DIE should have a child DW_AT_type attribute that is set
13404 to the type of the argument to PARM, which is ARG.
13405 If PARM is a type generic parameter, TMPL_DIE should have a
13406 child DW_AT_type that is set to ARG. */
13407 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13408 add_type_attribute (tmpl_die, tmpl_type,
13409 (TREE_THIS_VOLATILE (tmpl_type)
13410 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13411 false, parent_die);
13413 else
13415 /* So TMPL_DIE is a DIE representing a
13416 a generic generic template parameter, a.k.a template template
13417 parameter in C++ and arg is a template. */
13419 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13420 to the name of the argument. */
13421 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13422 if (name)
13423 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13426 if (TREE_CODE (parm) == PARM_DECL)
13427 /* So PARM is a non-type generic parameter.
13428 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13429 attribute of TMPL_DIE which value represents the value
13430 of ARG.
13431 We must be careful here:
13432 The value of ARG might reference some function decls.
13433 We might currently be emitting debug info for a generic
13434 type and types are emitted before function decls, we don't
13435 know if the function decls referenced by ARG will actually be
13436 emitted after cgraph computations.
13437 So must defer the generation of the DW_AT_const_value to
13438 after cgraph is ready. */
13439 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13442 return tmpl_die;
13445 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13446 PARM_PACK must be a template parameter pack. The returned DIE
13447 will be child DIE of PARENT_DIE. */
13449 static dw_die_ref
13450 template_parameter_pack_die (tree parm_pack,
13451 tree parm_pack_args,
13452 dw_die_ref parent_die)
13454 dw_die_ref die;
13455 int j;
13457 gcc_assert (parent_die && parm_pack);
13459 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13460 add_name_and_src_coords_attributes (die, parm_pack);
13461 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13462 generic_parameter_die (parm_pack,
13463 TREE_VEC_ELT (parm_pack_args, j),
13464 false /* Don't emit DW_AT_name */,
13465 die);
13466 return die;
13469 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13470 an enumerated type. */
13472 static inline int
13473 type_is_enum (const_tree type)
13475 return TREE_CODE (type) == ENUMERAL_TYPE;
13478 /* Return the DBX register number described by a given RTL node. */
13480 static unsigned int
13481 dbx_reg_number (const_rtx rtl)
13483 unsigned regno = REGNO (rtl);
13485 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13487 #ifdef LEAF_REG_REMAP
13488 if (crtl->uses_only_leaf_regs)
13490 int leaf_reg = LEAF_REG_REMAP (regno);
13491 if (leaf_reg != -1)
13492 regno = (unsigned) leaf_reg;
13494 #endif
13496 regno = DBX_REGISTER_NUMBER (regno);
13497 gcc_assert (regno != INVALID_REGNUM);
13498 return regno;
13501 /* Optionally add a DW_OP_piece term to a location description expression.
13502 DW_OP_piece is only added if the location description expression already
13503 doesn't end with DW_OP_piece. */
13505 static void
13506 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13508 dw_loc_descr_ref loc;
13510 if (*list_head != NULL)
13512 /* Find the end of the chain. */
13513 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13516 if (loc->dw_loc_opc != DW_OP_piece)
13517 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13521 /* Return a location descriptor that designates a machine register or
13522 zero if there is none. */
13524 static dw_loc_descr_ref
13525 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13527 rtx regs;
13529 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13530 return 0;
13532 /* We only use "frame base" when we're sure we're talking about the
13533 post-prologue local stack frame. We do this by *not* running
13534 register elimination until this point, and recognizing the special
13535 argument pointer and soft frame pointer rtx's.
13536 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13537 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13538 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13540 dw_loc_descr_ref result = NULL;
13542 if (dwarf_version >= 4 || !dwarf_strict)
13544 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13545 initialized);
13546 if (result)
13547 add_loc_descr (&result,
13548 new_loc_descr (DW_OP_stack_value, 0, 0));
13550 return result;
13553 regs = targetm.dwarf_register_span (rtl);
13555 if (REG_NREGS (rtl) > 1 || regs)
13556 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13557 else
13559 unsigned int dbx_regnum = dbx_reg_number (rtl);
13560 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13561 return 0;
13562 return one_reg_loc_descriptor (dbx_regnum, initialized);
13566 /* Return a location descriptor that designates a machine register for
13567 a given hard register number. */
13569 static dw_loc_descr_ref
13570 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13572 dw_loc_descr_ref reg_loc_descr;
13574 if (regno <= 31)
13575 reg_loc_descr
13576 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13577 else
13578 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13580 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13581 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13583 return reg_loc_descr;
13586 /* Given an RTL of a register, return a location descriptor that
13587 designates a value that spans more than one register. */
13589 static dw_loc_descr_ref
13590 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13591 enum var_init_status initialized)
13593 int size, i;
13594 dw_loc_descr_ref loc_result = NULL;
13596 /* Simple, contiguous registers. */
13597 if (regs == NULL_RTX)
13599 unsigned reg = REGNO (rtl);
13600 int nregs;
13602 #ifdef LEAF_REG_REMAP
13603 if (crtl->uses_only_leaf_regs)
13605 int leaf_reg = LEAF_REG_REMAP (reg);
13606 if (leaf_reg != -1)
13607 reg = (unsigned) leaf_reg;
13609 #endif
13611 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13612 nregs = REG_NREGS (rtl);
13614 /* At present we only track constant-sized pieces. */
13615 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13616 return NULL;
13617 size /= nregs;
13619 loc_result = NULL;
13620 while (nregs--)
13622 dw_loc_descr_ref t;
13624 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13625 VAR_INIT_STATUS_INITIALIZED);
13626 add_loc_descr (&loc_result, t);
13627 add_loc_descr_op_piece (&loc_result, size);
13628 ++reg;
13630 return loc_result;
13633 /* Now onto stupid register sets in non contiguous locations. */
13635 gcc_assert (GET_CODE (regs) == PARALLEL);
13637 /* At present we only track constant-sized pieces. */
13638 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13639 return NULL;
13640 loc_result = NULL;
13642 for (i = 0; i < XVECLEN (regs, 0); ++i)
13644 dw_loc_descr_ref t;
13646 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13647 VAR_INIT_STATUS_INITIALIZED);
13648 add_loc_descr (&loc_result, t);
13649 add_loc_descr_op_piece (&loc_result, size);
13652 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13653 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13654 return loc_result;
13657 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13659 /* Return a location descriptor that designates a constant i,
13660 as a compound operation from constant (i >> shift), constant shift
13661 and DW_OP_shl. */
13663 static dw_loc_descr_ref
13664 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13666 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13667 add_loc_descr (&ret, int_loc_descriptor (shift));
13668 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13669 return ret;
13672 /* Return a location descriptor that designates constant POLY_I. */
13674 static dw_loc_descr_ref
13675 int_loc_descriptor (poly_int64 poly_i)
13677 enum dwarf_location_atom op;
13679 HOST_WIDE_INT i;
13680 if (!poly_i.is_constant (&i))
13682 /* Create location descriptions for the non-constant part and
13683 add any constant offset at the end. */
13684 dw_loc_descr_ref ret = NULL;
13685 HOST_WIDE_INT constant = poly_i.coeffs[0];
13686 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13688 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13689 if (coeff != 0)
13691 dw_loc_descr_ref start = ret;
13692 unsigned int factor;
13693 int bias;
13694 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13695 (j, &factor, &bias);
13697 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13698 add COEFF * (REGNO / FACTOR) now and subtract
13699 COEFF * BIAS from the final constant part. */
13700 constant -= coeff * bias;
13701 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13702 if (coeff % factor == 0)
13703 coeff /= factor;
13704 else
13706 int amount = exact_log2 (factor);
13707 gcc_assert (amount >= 0);
13708 add_loc_descr (&ret, int_loc_descriptor (amount));
13709 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13711 if (coeff != 1)
13713 add_loc_descr (&ret, int_loc_descriptor (coeff));
13714 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13716 if (start)
13717 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13720 loc_descr_plus_const (&ret, constant);
13721 return ret;
13724 /* Pick the smallest representation of a constant, rather than just
13725 defaulting to the LEB encoding. */
13726 if (i >= 0)
13728 int clz = clz_hwi (i);
13729 int ctz = ctz_hwi (i);
13730 if (i <= 31)
13731 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13732 else if (i <= 0xff)
13733 op = DW_OP_const1u;
13734 else if (i <= 0xffff)
13735 op = DW_OP_const2u;
13736 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13737 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13738 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13739 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13740 while DW_OP_const4u is 5 bytes. */
13741 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13742 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13743 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13744 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13745 while DW_OP_const4u is 5 bytes. */
13746 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13748 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13749 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13750 <= 4)
13752 /* As i >= 2**31, the double cast above will yield a negative number.
13753 Since wrapping is defined in DWARF expressions we can output big
13754 positive integers as small negative ones, regardless of the size
13755 of host wide ints.
13757 Here, since the evaluator will handle 32-bit values and since i >=
13758 2**31, we know it's going to be interpreted as a negative literal:
13759 store it this way if we can do better than 5 bytes this way. */
13760 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13762 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13763 op = DW_OP_const4u;
13765 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13766 least 6 bytes: see if we can do better before falling back to it. */
13767 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13768 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13769 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13770 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13771 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13772 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13773 >= HOST_BITS_PER_WIDE_INT)
13774 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13775 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13776 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13777 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13778 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13779 && size_of_uleb128 (i) > 6)
13780 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13781 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13782 else
13783 op = DW_OP_constu;
13785 else
13787 if (i >= -0x80)
13788 op = DW_OP_const1s;
13789 else if (i >= -0x8000)
13790 op = DW_OP_const2s;
13791 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13793 if (size_of_int_loc_descriptor (i) < 5)
13795 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13796 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13797 return ret;
13799 op = DW_OP_const4s;
13801 else
13803 if (size_of_int_loc_descriptor (i)
13804 < (unsigned long) 1 + size_of_sleb128 (i))
13806 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13807 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13808 return ret;
13810 op = DW_OP_consts;
13814 return new_loc_descr (op, i, 0);
13817 /* Likewise, for unsigned constants. */
13819 static dw_loc_descr_ref
13820 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13822 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13823 const unsigned HOST_WIDE_INT max_uint
13824 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13826 /* If possible, use the clever signed constants handling. */
13827 if (i <= max_int)
13828 return int_loc_descriptor ((HOST_WIDE_INT) i);
13830 /* Here, we are left with positive numbers that cannot be represented as
13831 HOST_WIDE_INT, i.e.:
13832 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13834 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13835 whereas may be better to output a negative integer: thanks to integer
13836 wrapping, we know that:
13837 x = x - 2 ** DWARF2_ADDR_SIZE
13838 = x - 2 * (max (HOST_WIDE_INT) + 1)
13839 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13840 small negative integers. Let's try that in cases it will clearly improve
13841 the encoding: there is no gain turning DW_OP_const4u into
13842 DW_OP_const4s. */
13843 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13844 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13845 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13847 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13849 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13850 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13851 const HOST_WIDE_INT second_shift
13852 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13854 /* So we finally have:
13855 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13856 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13857 return int_loc_descriptor (second_shift);
13860 /* Last chance: fallback to a simple constant operation. */
13861 return new_loc_descr
13862 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13863 ? DW_OP_const4u
13864 : DW_OP_const8u,
13865 i, 0);
13868 /* Generate and return a location description that computes the unsigned
13869 comparison of the two stack top entries (a OP b where b is the top-most
13870 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13871 LE_EXPR, GT_EXPR or GE_EXPR. */
13873 static dw_loc_descr_ref
13874 uint_comparison_loc_list (enum tree_code kind)
13876 enum dwarf_location_atom op, flip_op;
13877 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13879 switch (kind)
13881 case LT_EXPR:
13882 op = DW_OP_lt;
13883 break;
13884 case LE_EXPR:
13885 op = DW_OP_le;
13886 break;
13887 case GT_EXPR:
13888 op = DW_OP_gt;
13889 break;
13890 case GE_EXPR:
13891 op = DW_OP_ge;
13892 break;
13893 default:
13894 gcc_unreachable ();
13897 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13898 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13900 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13901 possible to perform unsigned comparisons: we just have to distinguish
13902 three cases:
13904 1. when a and b have the same sign (as signed integers); then we should
13905 return: a OP(signed) b;
13907 2. when a is a negative signed integer while b is a positive one, then a
13908 is a greater unsigned integer than b; likewise when a and b's roles
13909 are flipped.
13911 So first, compare the sign of the two operands. */
13912 ret = new_loc_descr (DW_OP_over, 0, 0);
13913 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13914 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13915 /* If they have different signs (i.e. they have different sign bits), then
13916 the stack top value has now the sign bit set and thus it's smaller than
13917 zero. */
13918 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13919 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13920 add_loc_descr (&ret, bra_node);
13922 /* We are in case 1. At this point, we know both operands have the same
13923 sign, to it's safe to use the built-in signed comparison. */
13924 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13925 add_loc_descr (&ret, jmp_node);
13927 /* We are in case 2. Here, we know both operands do not have the same sign,
13928 so we have to flip the signed comparison. */
13929 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13930 tmp = new_loc_descr (flip_op, 0, 0);
13931 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13932 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13933 add_loc_descr (&ret, tmp);
13935 /* This dummy operation is necessary to make the two branches join. */
13936 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13937 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13938 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13939 add_loc_descr (&ret, tmp);
13941 return ret;
13944 /* Likewise, but takes the location description lists (might be destructive on
13945 them). Return NULL if either is NULL or if concatenation fails. */
13947 static dw_loc_list_ref
13948 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13949 enum tree_code kind)
13951 if (left == NULL || right == NULL)
13952 return NULL;
13954 add_loc_list (&left, right);
13955 if (left == NULL)
13956 return NULL;
13958 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13959 return left;
13962 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13963 without actually allocating it. */
13965 static unsigned long
13966 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13968 return size_of_int_loc_descriptor (i >> shift)
13969 + size_of_int_loc_descriptor (shift)
13970 + 1;
13973 /* Return size_of_locs (int_loc_descriptor (i)) without
13974 actually allocating it. */
13976 static unsigned long
13977 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13979 unsigned long s;
13981 if (i >= 0)
13983 int clz, ctz;
13984 if (i <= 31)
13985 return 1;
13986 else if (i <= 0xff)
13987 return 2;
13988 else if (i <= 0xffff)
13989 return 3;
13990 clz = clz_hwi (i);
13991 ctz = ctz_hwi (i);
13992 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13993 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13994 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13995 - clz - 5);
13996 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13997 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13998 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13999 - clz - 8);
14000 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14001 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14002 <= 4)
14003 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14004 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14005 return 5;
14006 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14007 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14008 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14009 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14010 - clz - 8);
14011 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14012 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14013 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14014 - clz - 16);
14015 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14016 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14017 && s > 6)
14018 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14019 - clz - 32);
14020 else
14021 return 1 + s;
14023 else
14025 if (i >= -0x80)
14026 return 2;
14027 else if (i >= -0x8000)
14028 return 3;
14029 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14031 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14033 s = size_of_int_loc_descriptor (-i) + 1;
14034 if (s < 5)
14035 return s;
14037 return 5;
14039 else
14041 unsigned long r = 1 + size_of_sleb128 (i);
14042 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14044 s = size_of_int_loc_descriptor (-i) + 1;
14045 if (s < r)
14046 return s;
14048 return r;
14053 /* Return loc description representing "address" of integer value.
14054 This can appear only as toplevel expression. */
14056 static dw_loc_descr_ref
14057 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14059 int litsize;
14060 dw_loc_descr_ref loc_result = NULL;
14062 if (!(dwarf_version >= 4 || !dwarf_strict))
14063 return NULL;
14065 litsize = size_of_int_loc_descriptor (i);
14066 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14067 is more compact. For DW_OP_stack_value we need:
14068 litsize + 1 (DW_OP_stack_value)
14069 and for DW_OP_implicit_value:
14070 1 (DW_OP_implicit_value) + 1 (length) + size. */
14071 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14073 loc_result = int_loc_descriptor (i);
14074 add_loc_descr (&loc_result,
14075 new_loc_descr (DW_OP_stack_value, 0, 0));
14076 return loc_result;
14079 loc_result = new_loc_descr (DW_OP_implicit_value,
14080 size, 0);
14081 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14082 loc_result->dw_loc_oprnd2.v.val_int = i;
14083 return loc_result;
14086 /* Return a location descriptor that designates a base+offset location. */
14088 static dw_loc_descr_ref
14089 based_loc_descr (rtx reg, poly_int64 offset,
14090 enum var_init_status initialized)
14092 unsigned int regno;
14093 dw_loc_descr_ref result;
14094 dw_fde_ref fde = cfun->fde;
14096 /* We only use "frame base" when we're sure we're talking about the
14097 post-prologue local stack frame. We do this by *not* running
14098 register elimination until this point, and recognizing the special
14099 argument pointer and soft frame pointer rtx's. */
14100 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14102 rtx elim = (ira_use_lra_p
14103 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14104 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14106 if (elim != reg)
14108 elim = strip_offset_and_add (elim, &offset);
14109 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14110 && (elim == hard_frame_pointer_rtx
14111 || elim == stack_pointer_rtx))
14112 || elim == (frame_pointer_needed
14113 ? hard_frame_pointer_rtx
14114 : stack_pointer_rtx));
14116 /* If drap register is used to align stack, use frame
14117 pointer + offset to access stack variables. If stack
14118 is aligned without drap, use stack pointer + offset to
14119 access stack variables. */
14120 if (crtl->stack_realign_tried
14121 && reg == frame_pointer_rtx)
14123 int base_reg
14124 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14125 ? HARD_FRAME_POINTER_REGNUM
14126 : REGNO (elim));
14127 return new_reg_loc_descr (base_reg, offset);
14130 gcc_assert (frame_pointer_fb_offset_valid);
14131 offset += frame_pointer_fb_offset;
14132 HOST_WIDE_INT const_offset;
14133 if (offset.is_constant (&const_offset))
14134 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14135 else
14137 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14138 loc_descr_plus_const (&ret, offset);
14139 return ret;
14144 regno = REGNO (reg);
14145 #ifdef LEAF_REG_REMAP
14146 if (crtl->uses_only_leaf_regs)
14148 int leaf_reg = LEAF_REG_REMAP (regno);
14149 if (leaf_reg != -1)
14150 regno = (unsigned) leaf_reg;
14152 #endif
14153 regno = DWARF_FRAME_REGNUM (regno);
14155 HOST_WIDE_INT const_offset;
14156 if (!optimize && fde
14157 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14158 && offset.is_constant (&const_offset))
14160 /* Use cfa+offset to represent the location of arguments passed
14161 on the stack when drap is used to align stack.
14162 Only do this when not optimizing, for optimized code var-tracking
14163 is supposed to track where the arguments live and the register
14164 used as vdrap or drap in some spot might be used for something
14165 else in other part of the routine. */
14166 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14169 result = new_reg_loc_descr (regno, offset);
14171 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14172 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14174 return result;
14177 /* Return true if this RTL expression describes a base+offset calculation. */
14179 static inline int
14180 is_based_loc (const_rtx rtl)
14182 return (GET_CODE (rtl) == PLUS
14183 && ((REG_P (XEXP (rtl, 0))
14184 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14185 && CONST_INT_P (XEXP (rtl, 1)))));
14188 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14189 failed. */
14191 static dw_loc_descr_ref
14192 tls_mem_loc_descriptor (rtx mem)
14194 tree base;
14195 dw_loc_descr_ref loc_result;
14197 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14198 return NULL;
14200 base = get_base_address (MEM_EXPR (mem));
14201 if (base == NULL
14202 || !VAR_P (base)
14203 || !DECL_THREAD_LOCAL_P (base))
14204 return NULL;
14206 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14207 if (loc_result == NULL)
14208 return NULL;
14210 if (maybe_ne (MEM_OFFSET (mem), 0))
14211 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14213 return loc_result;
14216 /* Output debug info about reason why we failed to expand expression as dwarf
14217 expression. */
14219 static void
14220 expansion_failed (tree expr, rtx rtl, char const *reason)
14222 if (dump_file && (dump_flags & TDF_DETAILS))
14224 fprintf (dump_file, "Failed to expand as dwarf: ");
14225 if (expr)
14226 print_generic_expr (dump_file, expr, dump_flags);
14227 if (rtl)
14229 fprintf (dump_file, "\n");
14230 print_rtl (dump_file, rtl);
14232 fprintf (dump_file, "\nReason: %s\n", reason);
14236 /* Helper function for const_ok_for_output. */
14238 static bool
14239 const_ok_for_output_1 (rtx rtl)
14241 if (targetm.const_not_ok_for_debug_p (rtl))
14243 if (GET_CODE (rtl) != UNSPEC)
14245 expansion_failed (NULL_TREE, rtl,
14246 "Expression rejected for debug by the backend.\n");
14247 return false;
14250 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14251 the target hook doesn't explicitly allow it in debug info, assume
14252 we can't express it in the debug info. */
14253 /* Don't complain about TLS UNSPECs, those are just too hard to
14254 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14255 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14256 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14257 if (flag_checking
14258 && (XVECLEN (rtl, 0) == 0
14259 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14260 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14261 inform (current_function_decl
14262 ? DECL_SOURCE_LOCATION (current_function_decl)
14263 : UNKNOWN_LOCATION,
14264 #if NUM_UNSPEC_VALUES > 0
14265 "non-delegitimized UNSPEC %s (%d) found in variable location",
14266 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14267 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14268 XINT (rtl, 1));
14269 #else
14270 "non-delegitimized UNSPEC %d found in variable location",
14271 XINT (rtl, 1));
14272 #endif
14273 expansion_failed (NULL_TREE, rtl,
14274 "UNSPEC hasn't been delegitimized.\n");
14275 return false;
14278 if (CONST_POLY_INT_P (rtl))
14279 return false;
14281 if (targetm.const_not_ok_for_debug_p (rtl))
14283 expansion_failed (NULL_TREE, rtl,
14284 "Expression rejected for debug by the backend.\n");
14285 return false;
14288 /* FIXME: Refer to PR60655. It is possible for simplification
14289 of rtl expressions in var tracking to produce such expressions.
14290 We should really identify / validate expressions
14291 enclosed in CONST that can be handled by assemblers on various
14292 targets and only handle legitimate cases here. */
14293 switch (GET_CODE (rtl))
14295 case SYMBOL_REF:
14296 break;
14297 case NOT:
14298 case NEG:
14299 return false;
14300 default:
14301 return true;
14304 if (CONSTANT_POOL_ADDRESS_P (rtl))
14306 bool marked;
14307 get_pool_constant_mark (rtl, &marked);
14308 /* If all references to this pool constant were optimized away,
14309 it was not output and thus we can't represent it. */
14310 if (!marked)
14312 expansion_failed (NULL_TREE, rtl,
14313 "Constant was removed from constant pool.\n");
14314 return false;
14318 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14319 return false;
14321 /* Avoid references to external symbols in debug info, on several targets
14322 the linker might even refuse to link when linking a shared library,
14323 and in many other cases the relocations for .debug_info/.debug_loc are
14324 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14325 to be defined within the same shared library or executable are fine. */
14326 if (SYMBOL_REF_EXTERNAL_P (rtl))
14328 tree decl = SYMBOL_REF_DECL (rtl);
14330 if (decl == NULL || !targetm.binds_local_p (decl))
14332 expansion_failed (NULL_TREE, rtl,
14333 "Symbol not defined in current TU.\n");
14334 return false;
14338 return true;
14341 /* Return true if constant RTL can be emitted in DW_OP_addr or
14342 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14343 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14345 static bool
14346 const_ok_for_output (rtx rtl)
14348 if (GET_CODE (rtl) == SYMBOL_REF)
14349 return const_ok_for_output_1 (rtl);
14351 if (GET_CODE (rtl) == CONST)
14353 subrtx_var_iterator::array_type array;
14354 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14355 if (!const_ok_for_output_1 (*iter))
14356 return false;
14357 return true;
14360 return true;
14363 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14364 if possible, NULL otherwise. */
14366 static dw_die_ref
14367 base_type_for_mode (machine_mode mode, bool unsignedp)
14369 dw_die_ref type_die;
14370 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14372 if (type == NULL)
14373 return NULL;
14374 switch (TREE_CODE (type))
14376 case INTEGER_TYPE:
14377 case REAL_TYPE:
14378 break;
14379 default:
14380 return NULL;
14382 type_die = lookup_type_die (type);
14383 if (!type_die)
14384 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14385 comp_unit_die ());
14386 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14387 return NULL;
14388 return type_die;
14391 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14392 type matching MODE, or, if MODE is narrower than or as wide as
14393 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14394 possible. */
14396 static dw_loc_descr_ref
14397 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14399 machine_mode outer_mode = mode;
14400 dw_die_ref type_die;
14401 dw_loc_descr_ref cvt;
14403 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14405 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14406 return op;
14408 type_die = base_type_for_mode (outer_mode, 1);
14409 if (type_die == NULL)
14410 return NULL;
14411 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14412 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14413 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14414 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14415 add_loc_descr (&op, cvt);
14416 return op;
14419 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14421 static dw_loc_descr_ref
14422 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14423 dw_loc_descr_ref op1)
14425 dw_loc_descr_ref ret = op0;
14426 add_loc_descr (&ret, op1);
14427 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14428 if (STORE_FLAG_VALUE != 1)
14430 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14431 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14433 return ret;
14436 /* Subroutine of scompare_loc_descriptor for the case in which we're
14437 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14438 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14440 static dw_loc_descr_ref
14441 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14442 scalar_int_mode op_mode,
14443 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14445 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14446 dw_loc_descr_ref cvt;
14448 if (type_die == NULL)
14449 return NULL;
14450 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14451 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14452 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14453 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14454 add_loc_descr (&op0, cvt);
14455 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14456 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14457 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14458 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14459 add_loc_descr (&op1, cvt);
14460 return compare_loc_descriptor (op, op0, op1);
14463 /* Subroutine of scompare_loc_descriptor for the case in which we're
14464 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14465 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14467 static dw_loc_descr_ref
14468 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14469 scalar_int_mode op_mode,
14470 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14472 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14473 /* For eq/ne, if the operands are known to be zero-extended,
14474 there is no need to do the fancy shifting up. */
14475 if (op == DW_OP_eq || op == DW_OP_ne)
14477 dw_loc_descr_ref last0, last1;
14478 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14480 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14482 /* deref_size zero extends, and for constants we can check
14483 whether they are zero extended or not. */
14484 if (((last0->dw_loc_opc == DW_OP_deref_size
14485 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14486 || (CONST_INT_P (XEXP (rtl, 0))
14487 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14488 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14489 && ((last1->dw_loc_opc == DW_OP_deref_size
14490 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14491 || (CONST_INT_P (XEXP (rtl, 1))
14492 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14493 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14494 return compare_loc_descriptor (op, op0, op1);
14496 /* EQ/NE comparison against constant in narrower type than
14497 DWARF2_ADDR_SIZE can be performed either as
14498 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14499 DW_OP_{eq,ne}
14501 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14502 DW_OP_{eq,ne}. Pick whatever is shorter. */
14503 if (CONST_INT_P (XEXP (rtl, 1))
14504 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14505 && (size_of_int_loc_descriptor (shift) + 1
14506 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14507 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14508 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14509 & GET_MODE_MASK (op_mode))))
14511 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14512 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14513 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14514 & GET_MODE_MASK (op_mode));
14515 return compare_loc_descriptor (op, op0, op1);
14518 add_loc_descr (&op0, int_loc_descriptor (shift));
14519 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14520 if (CONST_INT_P (XEXP (rtl, 1)))
14521 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14522 else
14524 add_loc_descr (&op1, int_loc_descriptor (shift));
14525 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14527 return compare_loc_descriptor (op, op0, op1);
14530 /* Return location descriptor for unsigned comparison OP RTL. */
14532 static dw_loc_descr_ref
14533 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14534 machine_mode mem_mode)
14536 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14537 dw_loc_descr_ref op0, op1;
14539 if (op_mode == VOIDmode)
14540 op_mode = GET_MODE (XEXP (rtl, 1));
14541 if (op_mode == VOIDmode)
14542 return NULL;
14544 scalar_int_mode int_op_mode;
14545 if (dwarf_strict
14546 && dwarf_version < 5
14547 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14548 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14549 return NULL;
14551 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14552 VAR_INIT_STATUS_INITIALIZED);
14553 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14554 VAR_INIT_STATUS_INITIALIZED);
14556 if (op0 == NULL || op1 == NULL)
14557 return NULL;
14559 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14561 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14562 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14564 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14565 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14567 return compare_loc_descriptor (op, op0, op1);
14570 /* Return location descriptor for unsigned comparison OP RTL. */
14572 static dw_loc_descr_ref
14573 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14574 machine_mode mem_mode)
14576 dw_loc_descr_ref op0, op1;
14578 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14579 if (test_op_mode == VOIDmode)
14580 test_op_mode = GET_MODE (XEXP (rtl, 1));
14582 scalar_int_mode op_mode;
14583 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14584 return NULL;
14586 if (dwarf_strict
14587 && dwarf_version < 5
14588 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14589 return NULL;
14591 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14592 VAR_INIT_STATUS_INITIALIZED);
14593 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14594 VAR_INIT_STATUS_INITIALIZED);
14596 if (op0 == NULL || op1 == NULL)
14597 return NULL;
14599 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14601 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14602 dw_loc_descr_ref last0, last1;
14603 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14605 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14607 if (CONST_INT_P (XEXP (rtl, 0)))
14608 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14609 /* deref_size zero extends, so no need to mask it again. */
14610 else if (last0->dw_loc_opc != DW_OP_deref_size
14611 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14613 add_loc_descr (&op0, int_loc_descriptor (mask));
14614 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14616 if (CONST_INT_P (XEXP (rtl, 1)))
14617 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14618 /* deref_size zero extends, so no need to mask it again. */
14619 else if (last1->dw_loc_opc != DW_OP_deref_size
14620 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14622 add_loc_descr (&op1, int_loc_descriptor (mask));
14623 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14626 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14628 HOST_WIDE_INT bias = 1;
14629 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14630 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14631 if (CONST_INT_P (XEXP (rtl, 1)))
14632 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14633 + INTVAL (XEXP (rtl, 1)));
14634 else
14635 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14636 bias, 0));
14638 return compare_loc_descriptor (op, op0, op1);
14641 /* Return location descriptor for {U,S}{MIN,MAX}. */
14643 static dw_loc_descr_ref
14644 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14645 machine_mode mem_mode)
14647 enum dwarf_location_atom op;
14648 dw_loc_descr_ref op0, op1, ret;
14649 dw_loc_descr_ref bra_node, drop_node;
14651 scalar_int_mode int_mode;
14652 if (dwarf_strict
14653 && dwarf_version < 5
14654 && (!is_a <scalar_int_mode> (mode, &int_mode)
14655 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14656 return NULL;
14658 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14659 VAR_INIT_STATUS_INITIALIZED);
14660 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14661 VAR_INIT_STATUS_INITIALIZED);
14663 if (op0 == NULL || op1 == NULL)
14664 return NULL;
14666 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14667 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14668 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14669 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14671 /* Checked by the caller. */
14672 int_mode = as_a <scalar_int_mode> (mode);
14673 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14675 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14676 add_loc_descr (&op0, int_loc_descriptor (mask));
14677 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14678 add_loc_descr (&op1, int_loc_descriptor (mask));
14679 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14681 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14683 HOST_WIDE_INT bias = 1;
14684 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14685 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14686 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14689 else if (is_a <scalar_int_mode> (mode, &int_mode)
14690 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14692 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14693 add_loc_descr (&op0, int_loc_descriptor (shift));
14694 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14695 add_loc_descr (&op1, int_loc_descriptor (shift));
14696 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14698 else if (is_a <scalar_int_mode> (mode, &int_mode)
14699 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14701 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14702 dw_loc_descr_ref cvt;
14703 if (type_die == NULL)
14704 return NULL;
14705 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14706 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14707 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14708 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14709 add_loc_descr (&op0, cvt);
14710 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14711 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14712 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14713 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14714 add_loc_descr (&op1, cvt);
14717 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14718 op = DW_OP_lt;
14719 else
14720 op = DW_OP_gt;
14721 ret = op0;
14722 add_loc_descr (&ret, op1);
14723 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14724 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14725 add_loc_descr (&ret, bra_node);
14726 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14727 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14728 add_loc_descr (&ret, drop_node);
14729 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14730 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14731 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14732 && is_a <scalar_int_mode> (mode, &int_mode)
14733 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14734 ret = convert_descriptor_to_mode (int_mode, ret);
14735 return ret;
14738 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14739 but after converting arguments to type_die, afterwards
14740 convert back to unsigned. */
14742 static dw_loc_descr_ref
14743 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14744 scalar_int_mode mode, machine_mode mem_mode)
14746 dw_loc_descr_ref cvt, op0, op1;
14748 if (type_die == NULL)
14749 return NULL;
14750 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14751 VAR_INIT_STATUS_INITIALIZED);
14752 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14753 VAR_INIT_STATUS_INITIALIZED);
14754 if (op0 == NULL || op1 == NULL)
14755 return NULL;
14756 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14757 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14758 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14759 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14760 add_loc_descr (&op0, cvt);
14761 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14762 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14763 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14764 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14765 add_loc_descr (&op1, cvt);
14766 add_loc_descr (&op0, op1);
14767 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14768 return convert_descriptor_to_mode (mode, op0);
14771 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14772 const0 is DW_OP_lit0 or corresponding typed constant,
14773 const1 is DW_OP_lit1 or corresponding typed constant
14774 and constMSB is constant with just the MSB bit set
14775 for the mode):
14776 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14777 L1: const0 DW_OP_swap
14778 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14779 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14780 L3: DW_OP_drop
14781 L4: DW_OP_nop
14783 CTZ is similar:
14784 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14785 L1: const0 DW_OP_swap
14786 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14787 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14788 L3: DW_OP_drop
14789 L4: DW_OP_nop
14791 FFS is similar:
14792 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14793 L1: const1 DW_OP_swap
14794 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14795 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14796 L3: DW_OP_drop
14797 L4: DW_OP_nop */
14799 static dw_loc_descr_ref
14800 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14801 machine_mode mem_mode)
14803 dw_loc_descr_ref op0, ret, tmp;
14804 HOST_WIDE_INT valv;
14805 dw_loc_descr_ref l1jump, l1label;
14806 dw_loc_descr_ref l2jump, l2label;
14807 dw_loc_descr_ref l3jump, l3label;
14808 dw_loc_descr_ref l4jump, l4label;
14809 rtx msb;
14811 if (GET_MODE (XEXP (rtl, 0)) != mode)
14812 return NULL;
14814 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14815 VAR_INIT_STATUS_INITIALIZED);
14816 if (op0 == NULL)
14817 return NULL;
14818 ret = op0;
14819 if (GET_CODE (rtl) == CLZ)
14821 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14822 valv = GET_MODE_BITSIZE (mode);
14824 else if (GET_CODE (rtl) == FFS)
14825 valv = 0;
14826 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14827 valv = GET_MODE_BITSIZE (mode);
14828 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14829 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14830 add_loc_descr (&ret, l1jump);
14831 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14832 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14833 VAR_INIT_STATUS_INITIALIZED);
14834 if (tmp == NULL)
14835 return NULL;
14836 add_loc_descr (&ret, tmp);
14837 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14838 add_loc_descr (&ret, l4jump);
14839 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14840 ? const1_rtx : const0_rtx,
14841 mode, mem_mode,
14842 VAR_INIT_STATUS_INITIALIZED);
14843 if (l1label == NULL)
14844 return NULL;
14845 add_loc_descr (&ret, l1label);
14846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14847 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14848 add_loc_descr (&ret, l2label);
14849 if (GET_CODE (rtl) != CLZ)
14850 msb = const1_rtx;
14851 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14852 msb = GEN_INT (HOST_WIDE_INT_1U
14853 << (GET_MODE_BITSIZE (mode) - 1));
14854 else
14855 msb = immed_wide_int_const
14856 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14857 GET_MODE_PRECISION (mode)), mode);
14858 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14859 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14860 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14861 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14862 else
14863 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14864 VAR_INIT_STATUS_INITIALIZED);
14865 if (tmp == NULL)
14866 return NULL;
14867 add_loc_descr (&ret, tmp);
14868 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14869 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14870 add_loc_descr (&ret, l3jump);
14871 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14872 VAR_INIT_STATUS_INITIALIZED);
14873 if (tmp == NULL)
14874 return NULL;
14875 add_loc_descr (&ret, tmp);
14876 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14877 ? DW_OP_shl : DW_OP_shr, 0, 0));
14878 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14879 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14880 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14881 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14882 add_loc_descr (&ret, l2jump);
14883 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14884 add_loc_descr (&ret, l3label);
14885 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14886 add_loc_descr (&ret, l4label);
14887 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14888 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14889 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14890 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14891 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14892 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14893 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14894 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14895 return ret;
14898 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14899 const1 is DW_OP_lit1 or corresponding typed constant):
14900 const0 DW_OP_swap
14901 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14902 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14903 L2: DW_OP_drop
14905 PARITY is similar:
14906 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14907 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14908 L2: DW_OP_drop */
14910 static dw_loc_descr_ref
14911 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14912 machine_mode mem_mode)
14914 dw_loc_descr_ref op0, ret, tmp;
14915 dw_loc_descr_ref l1jump, l1label;
14916 dw_loc_descr_ref l2jump, l2label;
14918 if (GET_MODE (XEXP (rtl, 0)) != mode)
14919 return NULL;
14921 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14922 VAR_INIT_STATUS_INITIALIZED);
14923 if (op0 == NULL)
14924 return NULL;
14925 ret = op0;
14926 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14927 VAR_INIT_STATUS_INITIALIZED);
14928 if (tmp == NULL)
14929 return NULL;
14930 add_loc_descr (&ret, tmp);
14931 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14932 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14933 add_loc_descr (&ret, l1label);
14934 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14935 add_loc_descr (&ret, l2jump);
14936 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14937 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14938 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14939 VAR_INIT_STATUS_INITIALIZED);
14940 if (tmp == NULL)
14941 return NULL;
14942 add_loc_descr (&ret, tmp);
14943 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14944 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14945 ? DW_OP_plus : DW_OP_xor, 0, 0));
14946 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14947 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14948 VAR_INIT_STATUS_INITIALIZED);
14949 add_loc_descr (&ret, tmp);
14950 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14951 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14952 add_loc_descr (&ret, l1jump);
14953 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14954 add_loc_descr (&ret, l2label);
14955 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14956 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14957 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14958 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14959 return ret;
14962 /* BSWAP (constS is initial shift count, either 56 or 24):
14963 constS const0
14964 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14965 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14966 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14967 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14968 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14970 static dw_loc_descr_ref
14971 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14972 machine_mode mem_mode)
14974 dw_loc_descr_ref op0, ret, tmp;
14975 dw_loc_descr_ref l1jump, l1label;
14976 dw_loc_descr_ref l2jump, l2label;
14978 if (BITS_PER_UNIT != 8
14979 || (GET_MODE_BITSIZE (mode) != 32
14980 && GET_MODE_BITSIZE (mode) != 64))
14981 return NULL;
14983 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14984 VAR_INIT_STATUS_INITIALIZED);
14985 if (op0 == NULL)
14986 return NULL;
14988 ret = op0;
14989 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14990 mode, mem_mode,
14991 VAR_INIT_STATUS_INITIALIZED);
14992 if (tmp == NULL)
14993 return NULL;
14994 add_loc_descr (&ret, tmp);
14995 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14996 VAR_INIT_STATUS_INITIALIZED);
14997 if (tmp == NULL)
14998 return NULL;
14999 add_loc_descr (&ret, tmp);
15000 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15001 add_loc_descr (&ret, l1label);
15002 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15003 mode, mem_mode,
15004 VAR_INIT_STATUS_INITIALIZED);
15005 add_loc_descr (&ret, tmp);
15006 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15007 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15008 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15009 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15010 VAR_INIT_STATUS_INITIALIZED);
15011 if (tmp == NULL)
15012 return NULL;
15013 add_loc_descr (&ret, tmp);
15014 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15015 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15016 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15017 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15018 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15019 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15020 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15021 VAR_INIT_STATUS_INITIALIZED);
15022 add_loc_descr (&ret, tmp);
15023 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15024 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15025 add_loc_descr (&ret, l2jump);
15026 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15027 VAR_INIT_STATUS_INITIALIZED);
15028 add_loc_descr (&ret, tmp);
15029 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15030 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15031 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15032 add_loc_descr (&ret, l1jump);
15033 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15034 add_loc_descr (&ret, l2label);
15035 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15036 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15037 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15038 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15039 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15040 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15041 return ret;
15044 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15045 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15046 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15047 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15049 ROTATERT is similar:
15050 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15051 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15052 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15054 static dw_loc_descr_ref
15055 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15056 machine_mode mem_mode)
15058 rtx rtlop1 = XEXP (rtl, 1);
15059 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15060 int i;
15062 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15063 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15064 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15065 VAR_INIT_STATUS_INITIALIZED);
15066 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15067 VAR_INIT_STATUS_INITIALIZED);
15068 if (op0 == NULL || op1 == NULL)
15069 return NULL;
15070 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15071 for (i = 0; i < 2; i++)
15073 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15074 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15075 mode, mem_mode,
15076 VAR_INIT_STATUS_INITIALIZED);
15077 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15078 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15079 ? DW_OP_const4u
15080 : HOST_BITS_PER_WIDE_INT == 64
15081 ? DW_OP_const8u : DW_OP_constu,
15082 GET_MODE_MASK (mode), 0);
15083 else
15084 mask[i] = NULL;
15085 if (mask[i] == NULL)
15086 return NULL;
15087 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15089 ret = op0;
15090 add_loc_descr (&ret, op1);
15091 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15092 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15093 if (GET_CODE (rtl) == ROTATERT)
15095 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15096 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15097 GET_MODE_BITSIZE (mode), 0));
15099 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15100 if (mask[0] != NULL)
15101 add_loc_descr (&ret, mask[0]);
15102 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15103 if (mask[1] != NULL)
15105 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15106 add_loc_descr (&ret, mask[1]);
15107 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15109 if (GET_CODE (rtl) == ROTATE)
15111 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15112 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15113 GET_MODE_BITSIZE (mode), 0));
15115 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15116 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15117 return ret;
15120 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15121 for DEBUG_PARAMETER_REF RTL. */
15123 static dw_loc_descr_ref
15124 parameter_ref_descriptor (rtx rtl)
15126 dw_loc_descr_ref ret;
15127 dw_die_ref ref;
15129 if (dwarf_strict)
15130 return NULL;
15131 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15132 /* With LTO during LTRANS we get the late DIE that refers to the early
15133 DIE, thus we add another indirection here. This seems to confuse
15134 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15135 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15136 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15137 if (ref)
15139 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15140 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15141 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15143 else
15145 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15146 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15148 return ret;
15151 /* The following routine converts the RTL for a variable or parameter
15152 (resident in memory) into an equivalent Dwarf representation of a
15153 mechanism for getting the address of that same variable onto the top of a
15154 hypothetical "address evaluation" stack.
15156 When creating memory location descriptors, we are effectively transforming
15157 the RTL for a memory-resident object into its Dwarf postfix expression
15158 equivalent. This routine recursively descends an RTL tree, turning
15159 it into Dwarf postfix code as it goes.
15161 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15163 MEM_MODE is the mode of the memory reference, needed to handle some
15164 autoincrement addressing modes.
15166 Return 0 if we can't represent the location. */
15168 dw_loc_descr_ref
15169 mem_loc_descriptor (rtx rtl, machine_mode mode,
15170 machine_mode mem_mode,
15171 enum var_init_status initialized)
15173 dw_loc_descr_ref mem_loc_result = NULL;
15174 enum dwarf_location_atom op;
15175 dw_loc_descr_ref op0, op1;
15176 rtx inner = NULL_RTX;
15177 poly_int64 offset;
15179 if (mode == VOIDmode)
15180 mode = GET_MODE (rtl);
15182 /* Note that for a dynamically sized array, the location we will generate a
15183 description of here will be the lowest numbered location which is
15184 actually within the array. That's *not* necessarily the same as the
15185 zeroth element of the array. */
15187 rtl = targetm.delegitimize_address (rtl);
15189 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15190 return NULL;
15192 scalar_int_mode int_mode, inner_mode, op1_mode;
15193 switch (GET_CODE (rtl))
15195 case POST_INC:
15196 case POST_DEC:
15197 case POST_MODIFY:
15198 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15200 case SUBREG:
15201 /* The case of a subreg may arise when we have a local (register)
15202 variable or a formal (register) parameter which doesn't quite fill
15203 up an entire register. For now, just assume that it is
15204 legitimate to make the Dwarf info refer to the whole register which
15205 contains the given subreg. */
15206 if (!subreg_lowpart_p (rtl))
15207 break;
15208 inner = SUBREG_REG (rtl);
15209 /* FALLTHRU */
15210 case TRUNCATE:
15211 if (inner == NULL_RTX)
15212 inner = XEXP (rtl, 0);
15213 if (is_a <scalar_int_mode> (mode, &int_mode)
15214 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15215 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15216 #ifdef POINTERS_EXTEND_UNSIGNED
15217 || (int_mode == Pmode && mem_mode != VOIDmode)
15218 #endif
15220 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15222 mem_loc_result = mem_loc_descriptor (inner,
15223 inner_mode,
15224 mem_mode, initialized);
15225 break;
15227 if (dwarf_strict && dwarf_version < 5)
15228 break;
15229 if (is_a <scalar_int_mode> (mode, &int_mode)
15230 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15231 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15232 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15234 dw_die_ref type_die;
15235 dw_loc_descr_ref cvt;
15237 mem_loc_result = mem_loc_descriptor (inner,
15238 GET_MODE (inner),
15239 mem_mode, initialized);
15240 if (mem_loc_result == NULL)
15241 break;
15242 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15243 if (type_die == NULL)
15245 mem_loc_result = NULL;
15246 break;
15248 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15249 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15250 else
15251 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 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 (&mem_loc_result, cvt);
15256 if (is_a <scalar_int_mode> (mode, &int_mode)
15257 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15259 /* Convert it to untyped afterwards. */
15260 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15261 add_loc_descr (&mem_loc_result, cvt);
15264 break;
15266 case REG:
15267 if (!is_a <scalar_int_mode> (mode, &int_mode)
15268 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15269 && rtl != arg_pointer_rtx
15270 && rtl != frame_pointer_rtx
15271 #ifdef POINTERS_EXTEND_UNSIGNED
15272 && (int_mode != Pmode || mem_mode == VOIDmode)
15273 #endif
15276 dw_die_ref type_die;
15277 unsigned int dbx_regnum;
15279 if (dwarf_strict && dwarf_version < 5)
15280 break;
15281 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
15282 break;
15283 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15284 if (type_die == NULL)
15285 break;
15287 dbx_regnum = dbx_reg_number (rtl);
15288 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15289 break;
15290 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15291 dbx_regnum, 0);
15292 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15293 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15294 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15295 break;
15297 /* Whenever a register number forms a part of the description of the
15298 method for calculating the (dynamic) address of a memory resident
15299 object, DWARF rules require the register number be referred to as
15300 a "base register". This distinction is not based in any way upon
15301 what category of register the hardware believes the given register
15302 belongs to. This is strictly DWARF terminology we're dealing with
15303 here. Note that in cases where the location of a memory-resident
15304 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15305 OP_CONST (0)) the actual DWARF location descriptor that we generate
15306 may just be OP_BASEREG (basereg). This may look deceptively like
15307 the object in question was allocated to a register (rather than in
15308 memory) so DWARF consumers need to be aware of the subtle
15309 distinction between OP_REG and OP_BASEREG. */
15310 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15311 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15312 else if (stack_realign_drap
15313 && crtl->drap_reg
15314 && crtl->args.internal_arg_pointer == rtl
15315 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15317 /* If RTL is internal_arg_pointer, which has been optimized
15318 out, use DRAP instead. */
15319 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15320 VAR_INIT_STATUS_INITIALIZED);
15322 break;
15324 case SIGN_EXTEND:
15325 case ZERO_EXTEND:
15326 if (!is_a <scalar_int_mode> (mode, &int_mode)
15327 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15328 break;
15329 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15330 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15331 if (op0 == 0)
15332 break;
15333 else if (GET_CODE (rtl) == ZERO_EXTEND
15334 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15335 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15336 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15337 to expand zero extend as two shifts instead of
15338 masking. */
15339 && GET_MODE_SIZE (inner_mode) <= 4)
15341 mem_loc_result = op0;
15342 add_loc_descr (&mem_loc_result,
15343 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15344 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15346 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15348 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15349 shift *= BITS_PER_UNIT;
15350 if (GET_CODE (rtl) == SIGN_EXTEND)
15351 op = DW_OP_shra;
15352 else
15353 op = DW_OP_shr;
15354 mem_loc_result = op0;
15355 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15356 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15357 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15358 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15360 else if (!dwarf_strict || dwarf_version >= 5)
15362 dw_die_ref type_die1, type_die2;
15363 dw_loc_descr_ref cvt;
15365 type_die1 = base_type_for_mode (inner_mode,
15366 GET_CODE (rtl) == ZERO_EXTEND);
15367 if (type_die1 == NULL)
15368 break;
15369 type_die2 = base_type_for_mode (int_mode, 1);
15370 if (type_die2 == NULL)
15371 break;
15372 mem_loc_result = op0;
15373 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15374 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15375 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15376 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15377 add_loc_descr (&mem_loc_result, cvt);
15378 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15379 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15380 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15381 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15382 add_loc_descr (&mem_loc_result, cvt);
15384 break;
15386 case MEM:
15388 rtx new_rtl = avoid_constant_pool_reference (rtl);
15389 if (new_rtl != rtl)
15391 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15392 initialized);
15393 if (mem_loc_result != NULL)
15394 return mem_loc_result;
15397 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15398 get_address_mode (rtl), mode,
15399 VAR_INIT_STATUS_INITIALIZED);
15400 if (mem_loc_result == NULL)
15401 mem_loc_result = tls_mem_loc_descriptor (rtl);
15402 if (mem_loc_result != NULL)
15404 if (!is_a <scalar_int_mode> (mode, &int_mode)
15405 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15407 dw_die_ref type_die;
15408 dw_loc_descr_ref deref;
15409 HOST_WIDE_INT size;
15411 if (dwarf_strict && dwarf_version < 5)
15412 return NULL;
15413 if (!GET_MODE_SIZE (mode).is_constant (&size))
15414 return NULL;
15415 type_die
15416 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15417 if (type_die == NULL)
15418 return NULL;
15419 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15420 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15421 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15422 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15423 add_loc_descr (&mem_loc_result, deref);
15425 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15426 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15427 else
15428 add_loc_descr (&mem_loc_result,
15429 new_loc_descr (DW_OP_deref_size,
15430 GET_MODE_SIZE (int_mode), 0));
15432 break;
15434 case LO_SUM:
15435 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15437 case LABEL_REF:
15438 /* Some ports can transform a symbol ref into a label ref, because
15439 the symbol ref is too far away and has to be dumped into a constant
15440 pool. */
15441 case CONST:
15442 case SYMBOL_REF:
15443 if (!is_a <scalar_int_mode> (mode, &int_mode)
15444 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15445 #ifdef POINTERS_EXTEND_UNSIGNED
15446 && (int_mode != Pmode || mem_mode == VOIDmode)
15447 #endif
15449 break;
15450 if (GET_CODE (rtl) == SYMBOL_REF
15451 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15453 dw_loc_descr_ref temp;
15455 /* If this is not defined, we have no way to emit the data. */
15456 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15457 break;
15459 temp = new_addr_loc_descr (rtl, dtprel_true);
15461 /* We check for DWARF 5 here because gdb did not implement
15462 DW_OP_form_tls_address until after 7.12. */
15463 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15464 ? DW_OP_form_tls_address
15465 : DW_OP_GNU_push_tls_address),
15466 0, 0);
15467 add_loc_descr (&mem_loc_result, temp);
15469 break;
15472 if (!const_ok_for_output (rtl))
15474 if (GET_CODE (rtl) == CONST)
15475 switch (GET_CODE (XEXP (rtl, 0)))
15477 case NOT:
15478 op = DW_OP_not;
15479 goto try_const_unop;
15480 case NEG:
15481 op = DW_OP_neg;
15482 goto try_const_unop;
15483 try_const_unop:
15484 rtx arg;
15485 arg = XEXP (XEXP (rtl, 0), 0);
15486 if (!CONSTANT_P (arg))
15487 arg = gen_rtx_CONST (int_mode, arg);
15488 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15489 initialized);
15490 if (op0)
15492 mem_loc_result = op0;
15493 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15495 break;
15496 default:
15497 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15498 mem_mode, initialized);
15499 break;
15501 break;
15504 symref:
15505 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15506 vec_safe_push (used_rtx_array, rtl);
15507 break;
15509 case CONCAT:
15510 case CONCATN:
15511 case VAR_LOCATION:
15512 case DEBUG_IMPLICIT_PTR:
15513 expansion_failed (NULL_TREE, rtl,
15514 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15515 return 0;
15517 case ENTRY_VALUE:
15518 if (dwarf_strict && dwarf_version < 5)
15519 return NULL;
15520 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15522 if (!is_a <scalar_int_mode> (mode, &int_mode)
15523 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15524 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15525 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15526 else
15528 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15529 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15530 return NULL;
15531 op0 = one_reg_loc_descriptor (dbx_regnum,
15532 VAR_INIT_STATUS_INITIALIZED);
15535 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15536 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15538 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15539 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15540 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15541 return NULL;
15543 else
15544 gcc_unreachable ();
15545 if (op0 == NULL)
15546 return NULL;
15547 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15548 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15549 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15550 break;
15552 case DEBUG_PARAMETER_REF:
15553 mem_loc_result = parameter_ref_descriptor (rtl);
15554 break;
15556 case PRE_MODIFY:
15557 /* Extract the PLUS expression nested inside and fall into
15558 PLUS code below. */
15559 rtl = XEXP (rtl, 1);
15560 goto plus;
15562 case PRE_INC:
15563 case PRE_DEC:
15564 /* Turn these into a PLUS expression and fall into the PLUS code
15565 below. */
15566 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15567 gen_int_mode (GET_CODE (rtl) == PRE_INC
15568 ? GET_MODE_UNIT_SIZE (mem_mode)
15569 : -GET_MODE_UNIT_SIZE (mem_mode),
15570 mode));
15572 /* fall through */
15574 case PLUS:
15575 plus:
15576 if (is_based_loc (rtl)
15577 && is_a <scalar_int_mode> (mode, &int_mode)
15578 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15579 || XEXP (rtl, 0) == arg_pointer_rtx
15580 || XEXP (rtl, 0) == frame_pointer_rtx))
15581 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15582 INTVAL (XEXP (rtl, 1)),
15583 VAR_INIT_STATUS_INITIALIZED);
15584 else
15586 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15587 VAR_INIT_STATUS_INITIALIZED);
15588 if (mem_loc_result == 0)
15589 break;
15591 if (CONST_INT_P (XEXP (rtl, 1))
15592 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15593 <= DWARF2_ADDR_SIZE))
15594 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15595 else
15597 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15598 VAR_INIT_STATUS_INITIALIZED);
15599 if (op1 == 0)
15600 return NULL;
15601 add_loc_descr (&mem_loc_result, op1);
15602 add_loc_descr (&mem_loc_result,
15603 new_loc_descr (DW_OP_plus, 0, 0));
15606 break;
15608 /* If a pseudo-reg is optimized away, it is possible for it to
15609 be replaced with a MEM containing a multiply or shift. */
15610 case MINUS:
15611 op = DW_OP_minus;
15612 goto do_binop;
15614 case MULT:
15615 op = DW_OP_mul;
15616 goto do_binop;
15618 case DIV:
15619 if ((!dwarf_strict || dwarf_version >= 5)
15620 && is_a <scalar_int_mode> (mode, &int_mode)
15621 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15623 mem_loc_result = typed_binop (DW_OP_div, rtl,
15624 base_type_for_mode (mode, 0),
15625 int_mode, mem_mode);
15626 break;
15628 op = DW_OP_div;
15629 goto do_binop;
15631 case UMOD:
15632 op = DW_OP_mod;
15633 goto do_binop;
15635 case ASHIFT:
15636 op = DW_OP_shl;
15637 goto do_shift;
15639 case ASHIFTRT:
15640 op = DW_OP_shra;
15641 goto do_shift;
15643 case LSHIFTRT:
15644 op = DW_OP_shr;
15645 goto do_shift;
15647 do_shift:
15648 if (!is_a <scalar_int_mode> (mode, &int_mode))
15649 break;
15650 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15651 VAR_INIT_STATUS_INITIALIZED);
15653 rtx rtlop1 = XEXP (rtl, 1);
15654 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15655 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15656 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15657 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15658 VAR_INIT_STATUS_INITIALIZED);
15661 if (op0 == 0 || op1 == 0)
15662 break;
15664 mem_loc_result = op0;
15665 add_loc_descr (&mem_loc_result, op1);
15666 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15667 break;
15669 case AND:
15670 op = DW_OP_and;
15671 goto do_binop;
15673 case IOR:
15674 op = DW_OP_or;
15675 goto do_binop;
15677 case XOR:
15678 op = DW_OP_xor;
15679 goto do_binop;
15681 do_binop:
15682 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15683 VAR_INIT_STATUS_INITIALIZED);
15684 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15685 VAR_INIT_STATUS_INITIALIZED);
15687 if (op0 == 0 || op1 == 0)
15688 break;
15690 mem_loc_result = op0;
15691 add_loc_descr (&mem_loc_result, op1);
15692 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15693 break;
15695 case MOD:
15696 if ((!dwarf_strict || dwarf_version >= 5)
15697 && is_a <scalar_int_mode> (mode, &int_mode)
15698 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15700 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15701 base_type_for_mode (mode, 0),
15702 int_mode, mem_mode);
15703 break;
15706 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15707 VAR_INIT_STATUS_INITIALIZED);
15708 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15709 VAR_INIT_STATUS_INITIALIZED);
15711 if (op0 == 0 || op1 == 0)
15712 break;
15714 mem_loc_result = op0;
15715 add_loc_descr (&mem_loc_result, op1);
15716 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15717 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15718 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15719 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15720 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15721 break;
15723 case UDIV:
15724 if ((!dwarf_strict || dwarf_version >= 5)
15725 && is_a <scalar_int_mode> (mode, &int_mode))
15727 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15729 op = DW_OP_div;
15730 goto do_binop;
15732 mem_loc_result = typed_binop (DW_OP_div, rtl,
15733 base_type_for_mode (int_mode, 1),
15734 int_mode, mem_mode);
15736 break;
15738 case NOT:
15739 op = DW_OP_not;
15740 goto do_unop;
15742 case ABS:
15743 op = DW_OP_abs;
15744 goto do_unop;
15746 case NEG:
15747 op = DW_OP_neg;
15748 goto do_unop;
15750 do_unop:
15751 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15752 VAR_INIT_STATUS_INITIALIZED);
15754 if (op0 == 0)
15755 break;
15757 mem_loc_result = op0;
15758 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15759 break;
15761 case CONST_INT:
15762 if (!is_a <scalar_int_mode> (mode, &int_mode)
15763 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15764 #ifdef POINTERS_EXTEND_UNSIGNED
15765 || (int_mode == Pmode
15766 && mem_mode != VOIDmode
15767 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15768 #endif
15771 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15772 break;
15774 if ((!dwarf_strict || dwarf_version >= 5)
15775 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15776 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15778 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15779 scalar_int_mode amode;
15780 if (type_die == NULL)
15781 return NULL;
15782 if (INTVAL (rtl) >= 0
15783 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15784 .exists (&amode))
15785 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15786 /* const DW_OP_convert <XXX> vs.
15787 DW_OP_const_type <XXX, 1, const>. */
15788 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15789 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15791 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15792 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15793 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15794 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15795 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15796 add_loc_descr (&mem_loc_result, op0);
15797 return mem_loc_result;
15799 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15800 INTVAL (rtl));
15801 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15802 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15803 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15804 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15805 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15806 else
15808 mem_loc_result->dw_loc_oprnd2.val_class
15809 = dw_val_class_const_double;
15810 mem_loc_result->dw_loc_oprnd2.v.val_double
15811 = double_int::from_shwi (INTVAL (rtl));
15814 break;
15816 case CONST_DOUBLE:
15817 if (!dwarf_strict || dwarf_version >= 5)
15819 dw_die_ref type_die;
15821 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15822 CONST_DOUBLE rtx could represent either a large integer
15823 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15824 the value is always a floating point constant.
15826 When it is an integer, a CONST_DOUBLE is used whenever
15827 the constant requires 2 HWIs to be adequately represented.
15828 We output CONST_DOUBLEs as blocks. */
15829 if (mode == VOIDmode
15830 || (GET_MODE (rtl) == VOIDmode
15831 && maybe_ne (GET_MODE_BITSIZE (mode),
15832 HOST_BITS_PER_DOUBLE_INT)))
15833 break;
15834 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15835 if (type_die == NULL)
15836 return NULL;
15837 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15838 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15839 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15840 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15841 #if TARGET_SUPPORTS_WIDE_INT == 0
15842 if (!SCALAR_FLOAT_MODE_P (mode))
15844 mem_loc_result->dw_loc_oprnd2.val_class
15845 = dw_val_class_const_double;
15846 mem_loc_result->dw_loc_oprnd2.v.val_double
15847 = rtx_to_double_int (rtl);
15849 else
15850 #endif
15852 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15853 unsigned int length = GET_MODE_SIZE (float_mode);
15854 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15856 insert_float (rtl, array);
15857 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15858 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15859 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15860 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15863 break;
15865 case CONST_WIDE_INT:
15866 if (!dwarf_strict || dwarf_version >= 5)
15868 dw_die_ref type_die;
15870 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15871 if (type_die == NULL)
15872 return NULL;
15873 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15874 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15875 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15876 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15877 mem_loc_result->dw_loc_oprnd2.val_class
15878 = dw_val_class_wide_int;
15879 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15880 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15882 break;
15884 case CONST_POLY_INT:
15885 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
15886 break;
15888 case EQ:
15889 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15890 break;
15892 case GE:
15893 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15894 break;
15896 case GT:
15897 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15898 break;
15900 case LE:
15901 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15902 break;
15904 case LT:
15905 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15906 break;
15908 case NE:
15909 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15910 break;
15912 case GEU:
15913 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15914 break;
15916 case GTU:
15917 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15918 break;
15920 case LEU:
15921 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15922 break;
15924 case LTU:
15925 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15926 break;
15928 case UMIN:
15929 case UMAX:
15930 if (!SCALAR_INT_MODE_P (mode))
15931 break;
15932 /* FALLTHRU */
15933 case SMIN:
15934 case SMAX:
15935 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15936 break;
15938 case ZERO_EXTRACT:
15939 case SIGN_EXTRACT:
15940 if (CONST_INT_P (XEXP (rtl, 1))
15941 && CONST_INT_P (XEXP (rtl, 2))
15942 && is_a <scalar_int_mode> (mode, &int_mode)
15943 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15944 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15945 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15946 && ((unsigned) INTVAL (XEXP (rtl, 1))
15947 + (unsigned) INTVAL (XEXP (rtl, 2))
15948 <= GET_MODE_BITSIZE (int_mode)))
15950 int shift, size;
15951 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15952 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15953 if (op0 == 0)
15954 break;
15955 if (GET_CODE (rtl) == SIGN_EXTRACT)
15956 op = DW_OP_shra;
15957 else
15958 op = DW_OP_shr;
15959 mem_loc_result = op0;
15960 size = INTVAL (XEXP (rtl, 1));
15961 shift = INTVAL (XEXP (rtl, 2));
15962 if (BITS_BIG_ENDIAN)
15963 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15964 if (shift + size != (int) DWARF2_ADDR_SIZE)
15966 add_loc_descr (&mem_loc_result,
15967 int_loc_descriptor (DWARF2_ADDR_SIZE
15968 - shift - size));
15969 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15971 if (size != (int) DWARF2_ADDR_SIZE)
15973 add_loc_descr (&mem_loc_result,
15974 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15975 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15978 break;
15980 case IF_THEN_ELSE:
15982 dw_loc_descr_ref op2, bra_node, drop_node;
15983 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15984 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15985 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15986 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15987 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15988 VAR_INIT_STATUS_INITIALIZED);
15989 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15990 VAR_INIT_STATUS_INITIALIZED);
15991 if (op0 == NULL || op1 == NULL || op2 == NULL)
15992 break;
15994 mem_loc_result = op1;
15995 add_loc_descr (&mem_loc_result, op2);
15996 add_loc_descr (&mem_loc_result, op0);
15997 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15998 add_loc_descr (&mem_loc_result, bra_node);
15999 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16000 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16001 add_loc_descr (&mem_loc_result, drop_node);
16002 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16003 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16005 break;
16007 case FLOAT_EXTEND:
16008 case FLOAT_TRUNCATE:
16009 case FLOAT:
16010 case UNSIGNED_FLOAT:
16011 case FIX:
16012 case UNSIGNED_FIX:
16013 if (!dwarf_strict || dwarf_version >= 5)
16015 dw_die_ref type_die;
16016 dw_loc_descr_ref cvt;
16018 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16019 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16020 if (op0 == NULL)
16021 break;
16022 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16023 && (GET_CODE (rtl) == FLOAT
16024 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16026 type_die = base_type_for_mode (int_mode,
16027 GET_CODE (rtl) == UNSIGNED_FLOAT);
16028 if (type_die == NULL)
16029 break;
16030 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16031 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16032 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16033 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16034 add_loc_descr (&op0, cvt);
16036 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16037 if (type_die == NULL)
16038 break;
16039 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16040 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16041 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16042 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16043 add_loc_descr (&op0, cvt);
16044 if (is_a <scalar_int_mode> (mode, &int_mode)
16045 && (GET_CODE (rtl) == FIX
16046 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16048 op0 = convert_descriptor_to_mode (int_mode, op0);
16049 if (op0 == NULL)
16050 break;
16052 mem_loc_result = op0;
16054 break;
16056 case CLZ:
16057 case CTZ:
16058 case FFS:
16059 if (is_a <scalar_int_mode> (mode, &int_mode))
16060 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16061 break;
16063 case POPCOUNT:
16064 case PARITY:
16065 if (is_a <scalar_int_mode> (mode, &int_mode))
16066 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16067 break;
16069 case BSWAP:
16070 if (is_a <scalar_int_mode> (mode, &int_mode))
16071 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16072 break;
16074 case ROTATE:
16075 case ROTATERT:
16076 if (is_a <scalar_int_mode> (mode, &int_mode))
16077 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16078 break;
16080 case COMPARE:
16081 /* In theory, we could implement the above. */
16082 /* DWARF cannot represent the unsigned compare operations
16083 natively. */
16084 case SS_MULT:
16085 case US_MULT:
16086 case SS_DIV:
16087 case US_DIV:
16088 case SS_PLUS:
16089 case US_PLUS:
16090 case SS_MINUS:
16091 case US_MINUS:
16092 case SS_NEG:
16093 case US_NEG:
16094 case SS_ABS:
16095 case SS_ASHIFT:
16096 case US_ASHIFT:
16097 case SS_TRUNCATE:
16098 case US_TRUNCATE:
16099 case UNORDERED:
16100 case ORDERED:
16101 case UNEQ:
16102 case UNGE:
16103 case UNGT:
16104 case UNLE:
16105 case UNLT:
16106 case LTGT:
16107 case FRACT_CONVERT:
16108 case UNSIGNED_FRACT_CONVERT:
16109 case SAT_FRACT:
16110 case UNSIGNED_SAT_FRACT:
16111 case SQRT:
16112 case ASM_OPERANDS:
16113 case VEC_MERGE:
16114 case VEC_SELECT:
16115 case VEC_CONCAT:
16116 case VEC_DUPLICATE:
16117 case VEC_SERIES:
16118 case UNSPEC:
16119 case HIGH:
16120 case FMA:
16121 case STRICT_LOW_PART:
16122 case CONST_VECTOR:
16123 case CONST_FIXED:
16124 case CLRSB:
16125 case CLOBBER:
16126 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16127 can't express it in the debug info. This can happen e.g. with some
16128 TLS UNSPECs. */
16129 break;
16131 case CONST_STRING:
16132 resolve_one_addr (&rtl);
16133 goto symref;
16135 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16136 the expression. An UNSPEC rtx represents a raw DWARF operation,
16137 new_loc_descr is called for it to build the operation directly.
16138 Otherwise mem_loc_descriptor is called recursively. */
16139 case PARALLEL:
16141 int index = 0;
16142 dw_loc_descr_ref exp_result = NULL;
16144 for (; index < XVECLEN (rtl, 0); index++)
16146 rtx elem = XVECEXP (rtl, 0, index);
16147 if (GET_CODE (elem) == UNSPEC)
16149 /* Each DWARF operation UNSPEC contain two operands, if
16150 one operand is not used for the operation, const0_rtx is
16151 passed. */
16152 gcc_assert (XVECLEN (elem, 0) == 2);
16154 HOST_WIDE_INT dw_op = XINT (elem, 1);
16155 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16156 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16157 exp_result
16158 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16159 oprnd2);
16161 else
16162 exp_result
16163 = mem_loc_descriptor (elem, mode, mem_mode,
16164 VAR_INIT_STATUS_INITIALIZED);
16166 if (!mem_loc_result)
16167 mem_loc_result = exp_result;
16168 else
16169 add_loc_descr (&mem_loc_result, exp_result);
16172 break;
16175 default:
16176 if (flag_checking)
16178 print_rtl (stderr, rtl);
16179 gcc_unreachable ();
16181 break;
16184 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16185 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16187 return mem_loc_result;
16190 /* Return a descriptor that describes the concatenation of two locations.
16191 This is typically a complex variable. */
16193 static dw_loc_descr_ref
16194 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16196 /* At present we only track constant-sized pieces. */
16197 unsigned int size0, size1;
16198 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16199 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16200 return 0;
16202 dw_loc_descr_ref cc_loc_result = NULL;
16203 dw_loc_descr_ref x0_ref
16204 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16205 dw_loc_descr_ref x1_ref
16206 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16208 if (x0_ref == 0 || x1_ref == 0)
16209 return 0;
16211 cc_loc_result = x0_ref;
16212 add_loc_descr_op_piece (&cc_loc_result, size0);
16214 add_loc_descr (&cc_loc_result, x1_ref);
16215 add_loc_descr_op_piece (&cc_loc_result, size1);
16217 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16218 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16220 return cc_loc_result;
16223 /* Return a descriptor that describes the concatenation of N
16224 locations. */
16226 static dw_loc_descr_ref
16227 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16229 unsigned int i;
16230 dw_loc_descr_ref cc_loc_result = NULL;
16231 unsigned int n = XVECLEN (concatn, 0);
16232 unsigned int size;
16234 for (i = 0; i < n; ++i)
16236 dw_loc_descr_ref ref;
16237 rtx x = XVECEXP (concatn, 0, i);
16239 /* At present we only track constant-sized pieces. */
16240 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16241 return NULL;
16243 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16244 if (ref == NULL)
16245 return NULL;
16247 add_loc_descr (&cc_loc_result, ref);
16248 add_loc_descr_op_piece (&cc_loc_result, size);
16251 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16252 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16254 return cc_loc_result;
16257 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16258 for DEBUG_IMPLICIT_PTR RTL. */
16260 static dw_loc_descr_ref
16261 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16263 dw_loc_descr_ref ret;
16264 dw_die_ref ref;
16266 if (dwarf_strict && dwarf_version < 5)
16267 return NULL;
16268 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16269 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16270 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16271 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16272 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16273 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16274 if (ref)
16276 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16277 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16278 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16280 else
16282 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16283 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16285 return ret;
16288 /* Output a proper Dwarf location descriptor for a variable or parameter
16289 which is either allocated in a register or in a memory location. For a
16290 register, we just generate an OP_REG and the register number. For a
16291 memory location we provide a Dwarf postfix expression describing how to
16292 generate the (dynamic) address of the object onto the address stack.
16294 MODE is mode of the decl if this loc_descriptor is going to be used in
16295 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16296 allowed, VOIDmode otherwise.
16298 If we don't know how to describe it, return 0. */
16300 static dw_loc_descr_ref
16301 loc_descriptor (rtx rtl, machine_mode mode,
16302 enum var_init_status initialized)
16304 dw_loc_descr_ref loc_result = NULL;
16305 scalar_int_mode int_mode;
16307 switch (GET_CODE (rtl))
16309 case SUBREG:
16310 /* The case of a subreg may arise when we have a local (register)
16311 variable or a formal (register) parameter which doesn't quite fill
16312 up an entire register. For now, just assume that it is
16313 legitimate to make the Dwarf info refer to the whole register which
16314 contains the given subreg. */
16315 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16316 loc_result = loc_descriptor (SUBREG_REG (rtl),
16317 GET_MODE (SUBREG_REG (rtl)), initialized);
16318 else
16319 goto do_default;
16320 break;
16322 case REG:
16323 loc_result = reg_loc_descriptor (rtl, initialized);
16324 break;
16326 case MEM:
16327 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16328 GET_MODE (rtl), initialized);
16329 if (loc_result == NULL)
16330 loc_result = tls_mem_loc_descriptor (rtl);
16331 if (loc_result == NULL)
16333 rtx new_rtl = avoid_constant_pool_reference (rtl);
16334 if (new_rtl != rtl)
16335 loc_result = loc_descriptor (new_rtl, mode, initialized);
16337 break;
16339 case CONCAT:
16340 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16341 initialized);
16342 break;
16344 case CONCATN:
16345 loc_result = concatn_loc_descriptor (rtl, initialized);
16346 break;
16348 case VAR_LOCATION:
16349 /* Single part. */
16350 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16352 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16353 if (GET_CODE (loc) == EXPR_LIST)
16354 loc = XEXP (loc, 0);
16355 loc_result = loc_descriptor (loc, mode, initialized);
16356 break;
16359 rtl = XEXP (rtl, 1);
16360 /* FALLTHRU */
16362 case PARALLEL:
16364 rtvec par_elems = XVEC (rtl, 0);
16365 int num_elem = GET_NUM_ELEM (par_elems);
16366 machine_mode mode;
16367 int i, size;
16369 /* Create the first one, so we have something to add to. */
16370 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16371 VOIDmode, initialized);
16372 if (loc_result == NULL)
16373 return NULL;
16374 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16375 /* At present we only track constant-sized pieces. */
16376 if (!GET_MODE_SIZE (mode).is_constant (&size))
16377 return NULL;
16378 add_loc_descr_op_piece (&loc_result, size);
16379 for (i = 1; i < num_elem; i++)
16381 dw_loc_descr_ref temp;
16383 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16384 VOIDmode, initialized);
16385 if (temp == NULL)
16386 return NULL;
16387 add_loc_descr (&loc_result, temp);
16388 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16389 /* At present we only track constant-sized pieces. */
16390 if (!GET_MODE_SIZE (mode).is_constant (&size))
16391 return NULL;
16392 add_loc_descr_op_piece (&loc_result, size);
16395 break;
16397 case CONST_INT:
16398 if (mode != VOIDmode && mode != BLKmode)
16400 int_mode = as_a <scalar_int_mode> (mode);
16401 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16402 INTVAL (rtl));
16404 break;
16406 case CONST_DOUBLE:
16407 if (mode == VOIDmode)
16408 mode = GET_MODE (rtl);
16410 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16412 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16414 /* Note that a CONST_DOUBLE rtx could represent either an integer
16415 or a floating-point constant. A CONST_DOUBLE is used whenever
16416 the constant requires more than one word in order to be
16417 adequately represented. We output CONST_DOUBLEs as blocks. */
16418 scalar_mode smode = as_a <scalar_mode> (mode);
16419 loc_result = new_loc_descr (DW_OP_implicit_value,
16420 GET_MODE_SIZE (smode), 0);
16421 #if TARGET_SUPPORTS_WIDE_INT == 0
16422 if (!SCALAR_FLOAT_MODE_P (smode))
16424 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16425 loc_result->dw_loc_oprnd2.v.val_double
16426 = rtx_to_double_int (rtl);
16428 else
16429 #endif
16431 unsigned int length = GET_MODE_SIZE (smode);
16432 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16434 insert_float (rtl, array);
16435 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16436 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16437 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16438 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16441 break;
16443 case CONST_WIDE_INT:
16444 if (mode == VOIDmode)
16445 mode = GET_MODE (rtl);
16447 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16449 int_mode = as_a <scalar_int_mode> (mode);
16450 loc_result = new_loc_descr (DW_OP_implicit_value,
16451 GET_MODE_SIZE (int_mode), 0);
16452 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16453 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16454 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16456 break;
16458 case CONST_VECTOR:
16459 if (mode == VOIDmode)
16460 mode = GET_MODE (rtl);
16462 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16464 unsigned int length;
16465 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16466 return NULL;
16468 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16469 unsigned char *array
16470 = ggc_vec_alloc<unsigned char> (length * elt_size);
16471 unsigned int i;
16472 unsigned char *p;
16473 machine_mode imode = GET_MODE_INNER (mode);
16475 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16476 switch (GET_MODE_CLASS (mode))
16478 case MODE_VECTOR_INT:
16479 for (i = 0, p = array; i < length; i++, p += elt_size)
16481 rtx elt = CONST_VECTOR_ELT (rtl, i);
16482 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16484 break;
16486 case MODE_VECTOR_FLOAT:
16487 for (i = 0, p = array; i < length; i++, p += elt_size)
16489 rtx elt = CONST_VECTOR_ELT (rtl, i);
16490 insert_float (elt, p);
16492 break;
16494 default:
16495 gcc_unreachable ();
16498 loc_result = new_loc_descr (DW_OP_implicit_value,
16499 length * elt_size, 0);
16500 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16501 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16502 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16503 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16505 break;
16507 case CONST:
16508 if (mode == VOIDmode
16509 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16510 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16511 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16513 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16514 break;
16516 /* FALLTHROUGH */
16517 case SYMBOL_REF:
16518 if (!const_ok_for_output (rtl))
16519 break;
16520 /* FALLTHROUGH */
16521 case LABEL_REF:
16522 if (is_a <scalar_int_mode> (mode, &int_mode)
16523 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16524 && (dwarf_version >= 4 || !dwarf_strict))
16526 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16527 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16528 vec_safe_push (used_rtx_array, rtl);
16530 break;
16532 case DEBUG_IMPLICIT_PTR:
16533 loc_result = implicit_ptr_descriptor (rtl, 0);
16534 break;
16536 case PLUS:
16537 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16538 && CONST_INT_P (XEXP (rtl, 1)))
16540 loc_result
16541 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16542 break;
16544 /* FALLTHRU */
16545 do_default:
16546 default:
16547 if ((is_a <scalar_int_mode> (mode, &int_mode)
16548 && GET_MODE (rtl) == int_mode
16549 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16550 && dwarf_version >= 4)
16551 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16553 /* Value expression. */
16554 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16555 if (loc_result)
16556 add_loc_descr (&loc_result,
16557 new_loc_descr (DW_OP_stack_value, 0, 0));
16559 break;
16562 return loc_result;
16565 /* We need to figure out what section we should use as the base for the
16566 address ranges where a given location is valid.
16567 1. If this particular DECL has a section associated with it, use that.
16568 2. If this function has a section associated with it, use that.
16569 3. Otherwise, use the text section.
16570 XXX: If you split a variable across multiple sections, we won't notice. */
16572 static const char *
16573 secname_for_decl (const_tree decl)
16575 const char *secname;
16577 if (VAR_OR_FUNCTION_DECL_P (decl)
16578 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16579 && DECL_SECTION_NAME (decl))
16580 secname = DECL_SECTION_NAME (decl);
16581 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16582 secname = DECL_SECTION_NAME (current_function_decl);
16583 else if (cfun && in_cold_section_p)
16584 secname = crtl->subsections.cold_section_label;
16585 else
16586 secname = text_section_label;
16588 return secname;
16591 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16593 static bool
16594 decl_by_reference_p (tree decl)
16596 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16597 || VAR_P (decl))
16598 && DECL_BY_REFERENCE (decl));
16601 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16602 for VARLOC. */
16604 static dw_loc_descr_ref
16605 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16606 enum var_init_status initialized)
16608 int have_address = 0;
16609 dw_loc_descr_ref descr;
16610 machine_mode mode;
16612 if (want_address != 2)
16614 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16615 /* Single part. */
16616 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16618 varloc = PAT_VAR_LOCATION_LOC (varloc);
16619 if (GET_CODE (varloc) == EXPR_LIST)
16620 varloc = XEXP (varloc, 0);
16621 mode = GET_MODE (varloc);
16622 if (MEM_P (varloc))
16624 rtx addr = XEXP (varloc, 0);
16625 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16626 mode, initialized);
16627 if (descr)
16628 have_address = 1;
16629 else
16631 rtx x = avoid_constant_pool_reference (varloc);
16632 if (x != varloc)
16633 descr = mem_loc_descriptor (x, mode, VOIDmode,
16634 initialized);
16637 else
16638 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16640 else
16641 return 0;
16643 else
16645 if (GET_CODE (varloc) == VAR_LOCATION)
16646 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16647 else
16648 mode = DECL_MODE (loc);
16649 descr = loc_descriptor (varloc, mode, initialized);
16650 have_address = 1;
16653 if (!descr)
16654 return 0;
16656 if (want_address == 2 && !have_address
16657 && (dwarf_version >= 4 || !dwarf_strict))
16659 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16661 expansion_failed (loc, NULL_RTX,
16662 "DWARF address size mismatch");
16663 return 0;
16665 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16666 have_address = 1;
16668 /* Show if we can't fill the request for an address. */
16669 if (want_address && !have_address)
16671 expansion_failed (loc, NULL_RTX,
16672 "Want address and only have value");
16673 return 0;
16676 /* If we've got an address and don't want one, dereference. */
16677 if (!want_address && have_address)
16679 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16680 enum dwarf_location_atom op;
16682 if (size > DWARF2_ADDR_SIZE || size == -1)
16684 expansion_failed (loc, NULL_RTX,
16685 "DWARF address size mismatch");
16686 return 0;
16688 else if (size == DWARF2_ADDR_SIZE)
16689 op = DW_OP_deref;
16690 else
16691 op = DW_OP_deref_size;
16693 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16696 return descr;
16699 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16700 if it is not possible. */
16702 static dw_loc_descr_ref
16703 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16705 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16706 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16707 else if (dwarf_version >= 3 || !dwarf_strict)
16708 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16709 else
16710 return NULL;
16713 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16714 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16716 static dw_loc_descr_ref
16717 dw_sra_loc_expr (tree decl, rtx loc)
16719 rtx p;
16720 unsigned HOST_WIDE_INT padsize = 0;
16721 dw_loc_descr_ref descr, *descr_tail;
16722 unsigned HOST_WIDE_INT decl_size;
16723 rtx varloc;
16724 enum var_init_status initialized;
16726 if (DECL_SIZE (decl) == NULL
16727 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16728 return NULL;
16730 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16731 descr = NULL;
16732 descr_tail = &descr;
16734 for (p = loc; p; p = XEXP (p, 1))
16736 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16737 rtx loc_note = *decl_piece_varloc_ptr (p);
16738 dw_loc_descr_ref cur_descr;
16739 dw_loc_descr_ref *tail, last = NULL;
16740 unsigned HOST_WIDE_INT opsize = 0;
16742 if (loc_note == NULL_RTX
16743 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16745 padsize += bitsize;
16746 continue;
16748 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16749 varloc = NOTE_VAR_LOCATION (loc_note);
16750 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16751 if (cur_descr == NULL)
16753 padsize += bitsize;
16754 continue;
16757 /* Check that cur_descr either doesn't use
16758 DW_OP_*piece operations, or their sum is equal
16759 to bitsize. Otherwise we can't embed it. */
16760 for (tail = &cur_descr; *tail != NULL;
16761 tail = &(*tail)->dw_loc_next)
16762 if ((*tail)->dw_loc_opc == DW_OP_piece)
16764 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16765 * BITS_PER_UNIT;
16766 last = *tail;
16768 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16770 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16771 last = *tail;
16774 if (last != NULL && opsize != bitsize)
16776 padsize += bitsize;
16777 /* Discard the current piece of the descriptor and release any
16778 addr_table entries it uses. */
16779 remove_loc_list_addr_table_entries (cur_descr);
16780 continue;
16783 /* If there is a hole, add DW_OP_*piece after empty DWARF
16784 expression, which means that those bits are optimized out. */
16785 if (padsize)
16787 if (padsize > decl_size)
16789 remove_loc_list_addr_table_entries (cur_descr);
16790 goto discard_descr;
16792 decl_size -= padsize;
16793 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16794 if (*descr_tail == NULL)
16796 remove_loc_list_addr_table_entries (cur_descr);
16797 goto discard_descr;
16799 descr_tail = &(*descr_tail)->dw_loc_next;
16800 padsize = 0;
16802 *descr_tail = cur_descr;
16803 descr_tail = tail;
16804 if (bitsize > decl_size)
16805 goto discard_descr;
16806 decl_size -= bitsize;
16807 if (last == NULL)
16809 HOST_WIDE_INT offset = 0;
16810 if (GET_CODE (varloc) == VAR_LOCATION
16811 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16813 varloc = PAT_VAR_LOCATION_LOC (varloc);
16814 if (GET_CODE (varloc) == EXPR_LIST)
16815 varloc = XEXP (varloc, 0);
16819 if (GET_CODE (varloc) == CONST
16820 || GET_CODE (varloc) == SIGN_EXTEND
16821 || GET_CODE (varloc) == ZERO_EXTEND)
16822 varloc = XEXP (varloc, 0);
16823 else if (GET_CODE (varloc) == SUBREG)
16824 varloc = SUBREG_REG (varloc);
16825 else
16826 break;
16828 while (1);
16829 /* DW_OP_bit_size offset should be zero for register
16830 or implicit location descriptions and empty location
16831 descriptions, but for memory addresses needs big endian
16832 adjustment. */
16833 if (MEM_P (varloc))
16835 unsigned HOST_WIDE_INT memsize;
16836 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16837 goto discard_descr;
16838 memsize *= BITS_PER_UNIT;
16839 if (memsize != bitsize)
16841 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16842 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16843 goto discard_descr;
16844 if (memsize < bitsize)
16845 goto discard_descr;
16846 if (BITS_BIG_ENDIAN)
16847 offset = memsize - bitsize;
16851 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16852 if (*descr_tail == NULL)
16853 goto discard_descr;
16854 descr_tail = &(*descr_tail)->dw_loc_next;
16858 /* If there were any non-empty expressions, add padding till the end of
16859 the decl. */
16860 if (descr != NULL && decl_size != 0)
16862 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16863 if (*descr_tail == NULL)
16864 goto discard_descr;
16866 return descr;
16868 discard_descr:
16869 /* Discard the descriptor and release any addr_table entries it uses. */
16870 remove_loc_list_addr_table_entries (descr);
16871 return NULL;
16874 /* Return the dwarf representation of the location list LOC_LIST of
16875 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16876 function. */
16878 static dw_loc_list_ref
16879 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16881 const char *endname, *secname;
16882 var_loc_view endview;
16883 rtx varloc;
16884 enum var_init_status initialized;
16885 struct var_loc_node *node;
16886 dw_loc_descr_ref descr;
16887 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16888 dw_loc_list_ref list = NULL;
16889 dw_loc_list_ref *listp = &list;
16891 /* Now that we know what section we are using for a base,
16892 actually construct the list of locations.
16893 The first location information is what is passed to the
16894 function that creates the location list, and the remaining
16895 locations just get added on to that list.
16896 Note that we only know the start address for a location
16897 (IE location changes), so to build the range, we use
16898 the range [current location start, next location start].
16899 This means we have to special case the last node, and generate
16900 a range of [last location start, end of function label]. */
16902 if (cfun && crtl->has_bb_partition)
16904 bool save_in_cold_section_p = in_cold_section_p;
16905 in_cold_section_p = first_function_block_is_cold;
16906 if (loc_list->last_before_switch == NULL)
16907 in_cold_section_p = !in_cold_section_p;
16908 secname = secname_for_decl (decl);
16909 in_cold_section_p = save_in_cold_section_p;
16911 else
16912 secname = secname_for_decl (decl);
16914 for (node = loc_list->first; node; node = node->next)
16916 bool range_across_switch = false;
16917 if (GET_CODE (node->loc) == EXPR_LIST
16918 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16920 if (GET_CODE (node->loc) == EXPR_LIST)
16922 descr = NULL;
16923 /* This requires DW_OP_{,bit_}piece, which is not usable
16924 inside DWARF expressions. */
16925 if (want_address == 2)
16926 descr = dw_sra_loc_expr (decl, node->loc);
16928 else
16930 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16931 varloc = NOTE_VAR_LOCATION (node->loc);
16932 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16934 if (descr)
16936 /* If section switch happens in between node->label
16937 and node->next->label (or end of function) and
16938 we can't emit it as a single entry list,
16939 emit two ranges, first one ending at the end
16940 of first partition and second one starting at the
16941 beginning of second partition. */
16942 if (node == loc_list->last_before_switch
16943 && (node != loc_list->first || loc_list->first->next)
16944 && current_function_decl)
16946 endname = cfun->fde->dw_fde_end;
16947 endview = 0;
16948 range_across_switch = true;
16950 /* The variable has a location between NODE->LABEL and
16951 NODE->NEXT->LABEL. */
16952 else if (node->next)
16953 endname = node->next->label, endview = node->next->view;
16954 /* If the variable has a location at the last label
16955 it keeps its location until the end of function. */
16956 else if (!current_function_decl)
16957 endname = text_end_label, endview = 0;
16958 else
16960 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16961 current_function_funcdef_no);
16962 endname = ggc_strdup (label_id);
16963 endview = 0;
16966 *listp = new_loc_list (descr, node->label, node->view,
16967 endname, endview, secname);
16968 if (TREE_CODE (decl) == PARM_DECL
16969 && node == loc_list->first
16970 && NOTE_P (node->loc)
16971 && strcmp (node->label, endname) == 0)
16972 (*listp)->force = true;
16973 listp = &(*listp)->dw_loc_next;
16977 if (cfun
16978 && crtl->has_bb_partition
16979 && node == loc_list->last_before_switch)
16981 bool save_in_cold_section_p = in_cold_section_p;
16982 in_cold_section_p = !first_function_block_is_cold;
16983 secname = secname_for_decl (decl);
16984 in_cold_section_p = save_in_cold_section_p;
16987 if (range_across_switch)
16989 if (GET_CODE (node->loc) == EXPR_LIST)
16990 descr = dw_sra_loc_expr (decl, node->loc);
16991 else
16993 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16994 varloc = NOTE_VAR_LOCATION (node->loc);
16995 descr = dw_loc_list_1 (decl, varloc, want_address,
16996 initialized);
16998 gcc_assert (descr);
16999 /* The variable has a location between NODE->LABEL and
17000 NODE->NEXT->LABEL. */
17001 if (node->next)
17002 endname = node->next->label, endview = node->next->view;
17003 else
17004 endname = cfun->fde->dw_fde_second_end, endview = 0;
17005 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17006 endname, endview, secname);
17007 listp = &(*listp)->dw_loc_next;
17011 /* Try to avoid the overhead of a location list emitting a location
17012 expression instead, but only if we didn't have more than one
17013 location entry in the first place. If some entries were not
17014 representable, we don't want to pretend a single entry that was
17015 applies to the entire scope in which the variable is
17016 available. */
17017 maybe_gen_llsym (list);
17019 return list;
17022 /* Return if the loc_list has only single element and thus can be represented
17023 as location description. */
17025 static bool
17026 single_element_loc_list_p (dw_loc_list_ref list)
17028 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17029 return !list->ll_symbol;
17032 /* Duplicate a single element of location list. */
17034 static inline dw_loc_descr_ref
17035 copy_loc_descr (dw_loc_descr_ref ref)
17037 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17038 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17039 return copy;
17042 /* To each location in list LIST append loc descr REF. */
17044 static void
17045 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17047 dw_loc_descr_ref copy;
17048 add_loc_descr (&list->expr, ref);
17049 list = list->dw_loc_next;
17050 while (list)
17052 copy = copy_loc_descr (ref);
17053 add_loc_descr (&list->expr, copy);
17054 while (copy->dw_loc_next)
17055 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17056 list = list->dw_loc_next;
17060 /* To each location in list LIST prepend loc descr REF. */
17062 static void
17063 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17065 dw_loc_descr_ref copy;
17066 dw_loc_descr_ref ref_end = list->expr;
17067 add_loc_descr (&ref, list->expr);
17068 list->expr = ref;
17069 list = list->dw_loc_next;
17070 while (list)
17072 dw_loc_descr_ref end = list->expr;
17073 list->expr = copy = copy_loc_descr (ref);
17074 while (copy->dw_loc_next != ref_end)
17075 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17076 copy->dw_loc_next = end;
17077 list = list->dw_loc_next;
17081 /* Given two lists RET and LIST
17082 produce location list that is result of adding expression in LIST
17083 to expression in RET on each position in program.
17084 Might be destructive on both RET and LIST.
17086 TODO: We handle only simple cases of RET or LIST having at most one
17087 element. General case would involve sorting the lists in program order
17088 and merging them that will need some additional work.
17089 Adding that will improve quality of debug info especially for SRA-ed
17090 structures. */
17092 static void
17093 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17095 if (!list)
17096 return;
17097 if (!*ret)
17099 *ret = list;
17100 return;
17102 if (!list->dw_loc_next)
17104 add_loc_descr_to_each (*ret, list->expr);
17105 return;
17107 if (!(*ret)->dw_loc_next)
17109 prepend_loc_descr_to_each (list, (*ret)->expr);
17110 *ret = list;
17111 return;
17113 expansion_failed (NULL_TREE, NULL_RTX,
17114 "Don't know how to merge two non-trivial"
17115 " location lists.\n");
17116 *ret = NULL;
17117 return;
17120 /* LOC is constant expression. Try a luck, look it up in constant
17121 pool and return its loc_descr of its address. */
17123 static dw_loc_descr_ref
17124 cst_pool_loc_descr (tree loc)
17126 /* Get an RTL for this, if something has been emitted. */
17127 rtx rtl = lookup_constant_def (loc);
17129 if (!rtl || !MEM_P (rtl))
17131 gcc_assert (!rtl);
17132 return 0;
17134 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17136 /* TODO: We might get more coverage if we was actually delaying expansion
17137 of all expressions till end of compilation when constant pools are fully
17138 populated. */
17139 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17141 expansion_failed (loc, NULL_RTX,
17142 "CST value in contant pool but not marked.");
17143 return 0;
17145 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17146 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17149 /* Return dw_loc_list representing address of addr_expr LOC
17150 by looking for inner INDIRECT_REF expression and turning
17151 it into simple arithmetics.
17153 See loc_list_from_tree for the meaning of CONTEXT. */
17155 static dw_loc_list_ref
17156 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17157 loc_descr_context *context)
17159 tree obj, offset;
17160 poly_int64 bitsize, bitpos, bytepos;
17161 machine_mode mode;
17162 int unsignedp, reversep, volatilep = 0;
17163 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17165 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17166 &bitsize, &bitpos, &offset, &mode,
17167 &unsignedp, &reversep, &volatilep);
17168 STRIP_NOPS (obj);
17169 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17171 expansion_failed (loc, NULL_RTX, "bitfield access");
17172 return 0;
17174 if (!INDIRECT_REF_P (obj))
17176 expansion_failed (obj,
17177 NULL_RTX, "no indirect ref in inner refrence");
17178 return 0;
17180 if (!offset && known_eq (bitpos, 0))
17181 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17182 context);
17183 else if (toplev
17184 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17185 && (dwarf_version >= 4 || !dwarf_strict))
17187 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17188 if (!list_ret)
17189 return 0;
17190 if (offset)
17192 /* Variable offset. */
17193 list_ret1 = loc_list_from_tree (offset, 0, context);
17194 if (list_ret1 == 0)
17195 return 0;
17196 add_loc_list (&list_ret, list_ret1);
17197 if (!list_ret)
17198 return 0;
17199 add_loc_descr_to_each (list_ret,
17200 new_loc_descr (DW_OP_plus, 0, 0));
17202 HOST_WIDE_INT value;
17203 if (bytepos.is_constant (&value) && value > 0)
17204 add_loc_descr_to_each (list_ret,
17205 new_loc_descr (DW_OP_plus_uconst, value, 0));
17206 else if (maybe_ne (bytepos, 0))
17207 loc_list_plus_const (list_ret, bytepos);
17208 add_loc_descr_to_each (list_ret,
17209 new_loc_descr (DW_OP_stack_value, 0, 0));
17211 return list_ret;
17214 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17215 all operations from LOC are nops, move to the last one. Insert in NOPS all
17216 operations that are skipped. */
17218 static void
17219 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17220 hash_set<dw_loc_descr_ref> &nops)
17222 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17224 nops.add (loc);
17225 loc = loc->dw_loc_next;
17229 /* Helper for loc_descr_without_nops: free the location description operation
17230 P. */
17232 bool
17233 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17235 ggc_free (loc);
17236 return true;
17239 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17240 finishes LOC. */
17242 static void
17243 loc_descr_without_nops (dw_loc_descr_ref &loc)
17245 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17246 return;
17248 /* Set of all DW_OP_nop operations we remove. */
17249 hash_set<dw_loc_descr_ref> nops;
17251 /* First, strip all prefix NOP operations in order to keep the head of the
17252 operations list. */
17253 loc_descr_to_next_no_nop (loc, nops);
17255 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17257 /* For control flow operations: strip "prefix" nops in destination
17258 labels. */
17259 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17260 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17261 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17262 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17264 /* Do the same for the operations that follow, then move to the next
17265 iteration. */
17266 if (cur->dw_loc_next != NULL)
17267 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17268 cur = cur->dw_loc_next;
17271 nops.traverse<void *, free_loc_descr> (NULL);
17275 struct dwarf_procedure_info;
17277 /* Helper structure for location descriptions generation. */
17278 struct loc_descr_context
17280 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17281 NULL_TREE if DW_OP_push_object_address in invalid for this location
17282 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17283 tree context_type;
17284 /* The ..._DECL node that should be translated as a
17285 DW_OP_push_object_address operation. */
17286 tree base_decl;
17287 /* Information about the DWARF procedure we are currently generating. NULL if
17288 we are not generating a DWARF procedure. */
17289 struct dwarf_procedure_info *dpi;
17290 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17291 by consumer. Used for DW_TAG_generic_subrange attributes. */
17292 bool placeholder_arg;
17293 /* True if PLACEHOLDER_EXPR has been seen. */
17294 bool placeholder_seen;
17297 /* DWARF procedures generation
17299 DWARF expressions (aka. location descriptions) are used to encode variable
17300 things such as sizes or offsets. Such computations can have redundant parts
17301 that can be factorized in order to reduce the size of the output debug
17302 information. This is the whole point of DWARF procedures.
17304 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17305 already factorized into functions ("size functions") in order to handle very
17306 big and complex types. Such functions are quite simple: they have integral
17307 arguments, they return an integral result and their body contains only a
17308 return statement with arithmetic expressions. This is the only kind of
17309 function we are interested in translating into DWARF procedures, here.
17311 DWARF expressions and DWARF procedure are executed using a stack, so we have
17312 to define some calling convention for them to interact. Let's say that:
17314 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17315 all arguments in reverse order (right-to-left) so that when the DWARF
17316 procedure execution starts, the first argument is the top of the stack.
17318 - Then, when returning, the DWARF procedure must have consumed all arguments
17319 on the stack, must have pushed the result and touched nothing else.
17321 - Each integral argument and the result are integral types can be hold in a
17322 single stack slot.
17324 - We call "frame offset" the number of stack slots that are "under DWARF
17325 procedure control": it includes the arguments slots, the temporaries and
17326 the result slot. Thus, it is equal to the number of arguments when the
17327 procedure execution starts and must be equal to one (the result) when it
17328 returns. */
17330 /* Helper structure used when generating operations for a DWARF procedure. */
17331 struct dwarf_procedure_info
17333 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17334 currently translated. */
17335 tree fndecl;
17336 /* The number of arguments FNDECL takes. */
17337 unsigned args_count;
17340 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17341 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17342 equate it to this DIE. */
17344 static dw_die_ref
17345 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17346 dw_die_ref parent_die)
17348 dw_die_ref dwarf_proc_die;
17350 if ((dwarf_version < 3 && dwarf_strict)
17351 || location == NULL)
17352 return NULL;
17354 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17355 if (fndecl)
17356 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17357 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17358 return dwarf_proc_die;
17361 /* Return whether TYPE is a supported type as a DWARF procedure argument
17362 type or return type (we handle only scalar types and pointer types that
17363 aren't wider than the DWARF expression evaluation stack. */
17365 static bool
17366 is_handled_procedure_type (tree type)
17368 return ((INTEGRAL_TYPE_P (type)
17369 || TREE_CODE (type) == OFFSET_TYPE
17370 || TREE_CODE (type) == POINTER_TYPE)
17371 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17374 /* Helper for resolve_args_picking: do the same but stop when coming across
17375 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17376 offset *before* evaluating the corresponding operation. */
17378 static bool
17379 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17380 struct dwarf_procedure_info *dpi,
17381 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17383 /* The "frame_offset" identifier is already used to name a macro... */
17384 unsigned frame_offset_ = initial_frame_offset;
17385 dw_loc_descr_ref l;
17387 for (l = loc; l != NULL;)
17389 bool existed;
17390 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17392 /* If we already met this node, there is nothing to compute anymore. */
17393 if (existed)
17395 /* Make sure that the stack size is consistent wherever the execution
17396 flow comes from. */
17397 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17398 break;
17400 l_frame_offset = frame_offset_;
17402 /* If needed, relocate the picking offset with respect to the frame
17403 offset. */
17404 if (l->frame_offset_rel)
17406 unsigned HOST_WIDE_INT off;
17407 switch (l->dw_loc_opc)
17409 case DW_OP_pick:
17410 off = l->dw_loc_oprnd1.v.val_unsigned;
17411 break;
17412 case DW_OP_dup:
17413 off = 0;
17414 break;
17415 case DW_OP_over:
17416 off = 1;
17417 break;
17418 default:
17419 gcc_unreachable ();
17421 /* frame_offset_ is the size of the current stack frame, including
17422 incoming arguments. Besides, the arguments are pushed
17423 right-to-left. Thus, in order to access the Nth argument from
17424 this operation node, the picking has to skip temporaries *plus*
17425 one stack slot per argument (0 for the first one, 1 for the second
17426 one, etc.).
17428 The targetted argument number (N) is already set as the operand,
17429 and the number of temporaries can be computed with:
17430 frame_offsets_ - dpi->args_count */
17431 off += frame_offset_ - dpi->args_count;
17433 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17434 if (off > 255)
17435 return false;
17437 if (off == 0)
17439 l->dw_loc_opc = DW_OP_dup;
17440 l->dw_loc_oprnd1.v.val_unsigned = 0;
17442 else if (off == 1)
17444 l->dw_loc_opc = DW_OP_over;
17445 l->dw_loc_oprnd1.v.val_unsigned = 0;
17447 else
17449 l->dw_loc_opc = DW_OP_pick;
17450 l->dw_loc_oprnd1.v.val_unsigned = off;
17454 /* Update frame_offset according to the effect the current operation has
17455 on the stack. */
17456 switch (l->dw_loc_opc)
17458 case DW_OP_deref:
17459 case DW_OP_swap:
17460 case DW_OP_rot:
17461 case DW_OP_abs:
17462 case DW_OP_neg:
17463 case DW_OP_not:
17464 case DW_OP_plus_uconst:
17465 case DW_OP_skip:
17466 case DW_OP_reg0:
17467 case DW_OP_reg1:
17468 case DW_OP_reg2:
17469 case DW_OP_reg3:
17470 case DW_OP_reg4:
17471 case DW_OP_reg5:
17472 case DW_OP_reg6:
17473 case DW_OP_reg7:
17474 case DW_OP_reg8:
17475 case DW_OP_reg9:
17476 case DW_OP_reg10:
17477 case DW_OP_reg11:
17478 case DW_OP_reg12:
17479 case DW_OP_reg13:
17480 case DW_OP_reg14:
17481 case DW_OP_reg15:
17482 case DW_OP_reg16:
17483 case DW_OP_reg17:
17484 case DW_OP_reg18:
17485 case DW_OP_reg19:
17486 case DW_OP_reg20:
17487 case DW_OP_reg21:
17488 case DW_OP_reg22:
17489 case DW_OP_reg23:
17490 case DW_OP_reg24:
17491 case DW_OP_reg25:
17492 case DW_OP_reg26:
17493 case DW_OP_reg27:
17494 case DW_OP_reg28:
17495 case DW_OP_reg29:
17496 case DW_OP_reg30:
17497 case DW_OP_reg31:
17498 case DW_OP_bregx:
17499 case DW_OP_piece:
17500 case DW_OP_deref_size:
17501 case DW_OP_nop:
17502 case DW_OP_bit_piece:
17503 case DW_OP_implicit_value:
17504 case DW_OP_stack_value:
17505 break;
17507 case DW_OP_addr:
17508 case DW_OP_const1u:
17509 case DW_OP_const1s:
17510 case DW_OP_const2u:
17511 case DW_OP_const2s:
17512 case DW_OP_const4u:
17513 case DW_OP_const4s:
17514 case DW_OP_const8u:
17515 case DW_OP_const8s:
17516 case DW_OP_constu:
17517 case DW_OP_consts:
17518 case DW_OP_dup:
17519 case DW_OP_over:
17520 case DW_OP_pick:
17521 case DW_OP_lit0:
17522 case DW_OP_lit1:
17523 case DW_OP_lit2:
17524 case DW_OP_lit3:
17525 case DW_OP_lit4:
17526 case DW_OP_lit5:
17527 case DW_OP_lit6:
17528 case DW_OP_lit7:
17529 case DW_OP_lit8:
17530 case DW_OP_lit9:
17531 case DW_OP_lit10:
17532 case DW_OP_lit11:
17533 case DW_OP_lit12:
17534 case DW_OP_lit13:
17535 case DW_OP_lit14:
17536 case DW_OP_lit15:
17537 case DW_OP_lit16:
17538 case DW_OP_lit17:
17539 case DW_OP_lit18:
17540 case DW_OP_lit19:
17541 case DW_OP_lit20:
17542 case DW_OP_lit21:
17543 case DW_OP_lit22:
17544 case DW_OP_lit23:
17545 case DW_OP_lit24:
17546 case DW_OP_lit25:
17547 case DW_OP_lit26:
17548 case DW_OP_lit27:
17549 case DW_OP_lit28:
17550 case DW_OP_lit29:
17551 case DW_OP_lit30:
17552 case DW_OP_lit31:
17553 case DW_OP_breg0:
17554 case DW_OP_breg1:
17555 case DW_OP_breg2:
17556 case DW_OP_breg3:
17557 case DW_OP_breg4:
17558 case DW_OP_breg5:
17559 case DW_OP_breg6:
17560 case DW_OP_breg7:
17561 case DW_OP_breg8:
17562 case DW_OP_breg9:
17563 case DW_OP_breg10:
17564 case DW_OP_breg11:
17565 case DW_OP_breg12:
17566 case DW_OP_breg13:
17567 case DW_OP_breg14:
17568 case DW_OP_breg15:
17569 case DW_OP_breg16:
17570 case DW_OP_breg17:
17571 case DW_OP_breg18:
17572 case DW_OP_breg19:
17573 case DW_OP_breg20:
17574 case DW_OP_breg21:
17575 case DW_OP_breg22:
17576 case DW_OP_breg23:
17577 case DW_OP_breg24:
17578 case DW_OP_breg25:
17579 case DW_OP_breg26:
17580 case DW_OP_breg27:
17581 case DW_OP_breg28:
17582 case DW_OP_breg29:
17583 case DW_OP_breg30:
17584 case DW_OP_breg31:
17585 case DW_OP_fbreg:
17586 case DW_OP_push_object_address:
17587 case DW_OP_call_frame_cfa:
17588 case DW_OP_GNU_variable_value:
17589 ++frame_offset_;
17590 break;
17592 case DW_OP_drop:
17593 case DW_OP_xderef:
17594 case DW_OP_and:
17595 case DW_OP_div:
17596 case DW_OP_minus:
17597 case DW_OP_mod:
17598 case DW_OP_mul:
17599 case DW_OP_or:
17600 case DW_OP_plus:
17601 case DW_OP_shl:
17602 case DW_OP_shr:
17603 case DW_OP_shra:
17604 case DW_OP_xor:
17605 case DW_OP_bra:
17606 case DW_OP_eq:
17607 case DW_OP_ge:
17608 case DW_OP_gt:
17609 case DW_OP_le:
17610 case DW_OP_lt:
17611 case DW_OP_ne:
17612 case DW_OP_regx:
17613 case DW_OP_xderef_size:
17614 --frame_offset_;
17615 break;
17617 case DW_OP_call2:
17618 case DW_OP_call4:
17619 case DW_OP_call_ref:
17621 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17622 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17624 if (stack_usage == NULL)
17625 return false;
17626 frame_offset_ += *stack_usage;
17627 break;
17630 case DW_OP_implicit_pointer:
17631 case DW_OP_entry_value:
17632 case DW_OP_const_type:
17633 case DW_OP_regval_type:
17634 case DW_OP_deref_type:
17635 case DW_OP_convert:
17636 case DW_OP_reinterpret:
17637 case DW_OP_form_tls_address:
17638 case DW_OP_GNU_push_tls_address:
17639 case DW_OP_GNU_uninit:
17640 case DW_OP_GNU_encoded_addr:
17641 case DW_OP_GNU_implicit_pointer:
17642 case DW_OP_GNU_entry_value:
17643 case DW_OP_GNU_const_type:
17644 case DW_OP_GNU_regval_type:
17645 case DW_OP_GNU_deref_type:
17646 case DW_OP_GNU_convert:
17647 case DW_OP_GNU_reinterpret:
17648 case DW_OP_GNU_parameter_ref:
17649 /* loc_list_from_tree will probably not output these operations for
17650 size functions, so assume they will not appear here. */
17651 /* Fall through... */
17653 default:
17654 gcc_unreachable ();
17657 /* Now, follow the control flow (except subroutine calls). */
17658 switch (l->dw_loc_opc)
17660 case DW_OP_bra:
17661 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17662 frame_offsets))
17663 return false;
17664 /* Fall through. */
17666 case DW_OP_skip:
17667 l = l->dw_loc_oprnd1.v.val_loc;
17668 break;
17670 case DW_OP_stack_value:
17671 return true;
17673 default:
17674 l = l->dw_loc_next;
17675 break;
17679 return true;
17682 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17683 operations) in order to resolve the operand of DW_OP_pick operations that
17684 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17685 offset *before* LOC is executed. Return if all relocations were
17686 successful. */
17688 static bool
17689 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17690 struct dwarf_procedure_info *dpi)
17692 /* Associate to all visited operations the frame offset *before* evaluating
17693 this operation. */
17694 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17696 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17697 frame_offsets);
17700 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17701 Return NULL if it is not possible. */
17703 static dw_die_ref
17704 function_to_dwarf_procedure (tree fndecl)
17706 struct loc_descr_context ctx;
17707 struct dwarf_procedure_info dpi;
17708 dw_die_ref dwarf_proc_die;
17709 tree tree_body = DECL_SAVED_TREE (fndecl);
17710 dw_loc_descr_ref loc_body, epilogue;
17712 tree cursor;
17713 unsigned i;
17715 /* Do not generate multiple DWARF procedures for the same function
17716 declaration. */
17717 dwarf_proc_die = lookup_decl_die (fndecl);
17718 if (dwarf_proc_die != NULL)
17719 return dwarf_proc_die;
17721 /* DWARF procedures are available starting with the DWARFv3 standard. */
17722 if (dwarf_version < 3 && dwarf_strict)
17723 return NULL;
17725 /* We handle only functions for which we still have a body, that return a
17726 supported type and that takes arguments with supported types. Note that
17727 there is no point translating functions that return nothing. */
17728 if (tree_body == NULL_TREE
17729 || DECL_RESULT (fndecl) == NULL_TREE
17730 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17731 return NULL;
17733 for (cursor = DECL_ARGUMENTS (fndecl);
17734 cursor != NULL_TREE;
17735 cursor = TREE_CHAIN (cursor))
17736 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17737 return NULL;
17739 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17740 if (TREE_CODE (tree_body) != RETURN_EXPR)
17741 return NULL;
17742 tree_body = TREE_OPERAND (tree_body, 0);
17743 if (TREE_CODE (tree_body) != MODIFY_EXPR
17744 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17745 return NULL;
17746 tree_body = TREE_OPERAND (tree_body, 1);
17748 /* Try to translate the body expression itself. Note that this will probably
17749 cause an infinite recursion if its call graph has a cycle. This is very
17750 unlikely for size functions, however, so don't bother with such things at
17751 the moment. */
17752 ctx.context_type = NULL_TREE;
17753 ctx.base_decl = NULL_TREE;
17754 ctx.dpi = &dpi;
17755 ctx.placeholder_arg = false;
17756 ctx.placeholder_seen = false;
17757 dpi.fndecl = fndecl;
17758 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17759 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17760 if (!loc_body)
17761 return NULL;
17763 /* After evaluating all operands in "loc_body", we should still have on the
17764 stack all arguments plus the desired function result (top of the stack).
17765 Generate code in order to keep only the result in our stack frame. */
17766 epilogue = NULL;
17767 for (i = 0; i < dpi.args_count; ++i)
17769 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17770 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17771 op_couple->dw_loc_next->dw_loc_next = epilogue;
17772 epilogue = op_couple;
17774 add_loc_descr (&loc_body, epilogue);
17775 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17776 return NULL;
17778 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17779 because they are considered useful. Now there is an epilogue, they are
17780 not anymore, so give it another try. */
17781 loc_descr_without_nops (loc_body);
17783 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17784 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17785 though, given that size functions do not come from source, so they should
17786 not have a dedicated DW_TAG_subprogram DIE. */
17787 dwarf_proc_die
17788 = new_dwarf_proc_die (loc_body, fndecl,
17789 get_context_die (DECL_CONTEXT (fndecl)));
17791 /* The called DWARF procedure consumes one stack slot per argument and
17792 returns one stack slot. */
17793 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17795 return dwarf_proc_die;
17799 /* Generate Dwarf location list representing LOC.
17800 If WANT_ADDRESS is false, expression computing LOC will be computed
17801 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17802 if WANT_ADDRESS is 2, expression computing address useable in location
17803 will be returned (i.e. DW_OP_reg can be used
17804 to refer to register values).
17806 CONTEXT provides information to customize the location descriptions
17807 generation. Its context_type field specifies what type is implicitly
17808 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17809 will not be generated.
17811 Its DPI field determines whether we are generating a DWARF expression for a
17812 DWARF procedure, so PARM_DECL references are processed specifically.
17814 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17815 and dpi fields were null. */
17817 static dw_loc_list_ref
17818 loc_list_from_tree_1 (tree loc, int want_address,
17819 struct loc_descr_context *context)
17821 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17822 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17823 int have_address = 0;
17824 enum dwarf_location_atom op;
17826 /* ??? Most of the time we do not take proper care for sign/zero
17827 extending the values properly. Hopefully this won't be a real
17828 problem... */
17830 if (context != NULL
17831 && context->base_decl == loc
17832 && want_address == 0)
17834 if (dwarf_version >= 3 || !dwarf_strict)
17835 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17836 NULL, 0, NULL, 0, NULL);
17837 else
17838 return NULL;
17841 switch (TREE_CODE (loc))
17843 case ERROR_MARK:
17844 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17845 return 0;
17847 case PLACEHOLDER_EXPR:
17848 /* This case involves extracting fields from an object to determine the
17849 position of other fields. It is supposed to appear only as the first
17850 operand of COMPONENT_REF nodes and to reference precisely the type
17851 that the context allows. */
17852 if (context != NULL
17853 && TREE_TYPE (loc) == context->context_type
17854 && want_address >= 1)
17856 if (dwarf_version >= 3 || !dwarf_strict)
17858 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17859 have_address = 1;
17860 break;
17862 else
17863 return NULL;
17865 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17866 the single argument passed by consumer. */
17867 else if (context != NULL
17868 && context->placeholder_arg
17869 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17870 && want_address == 0)
17872 ret = new_loc_descr (DW_OP_pick, 0, 0);
17873 ret->frame_offset_rel = 1;
17874 context->placeholder_seen = true;
17875 break;
17877 else
17878 expansion_failed (loc, NULL_RTX,
17879 "PLACEHOLDER_EXPR for an unexpected type");
17880 break;
17882 case CALL_EXPR:
17884 const int nargs = call_expr_nargs (loc);
17885 tree callee = get_callee_fndecl (loc);
17886 int i;
17887 dw_die_ref dwarf_proc;
17889 if (callee == NULL_TREE)
17890 goto call_expansion_failed;
17892 /* We handle only functions that return an integer. */
17893 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17894 goto call_expansion_failed;
17896 dwarf_proc = function_to_dwarf_procedure (callee);
17897 if (dwarf_proc == NULL)
17898 goto call_expansion_failed;
17900 /* Evaluate arguments right-to-left so that the first argument will
17901 be the top-most one on the stack. */
17902 for (i = nargs - 1; i >= 0; --i)
17904 dw_loc_descr_ref loc_descr
17905 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17906 context);
17908 if (loc_descr == NULL)
17909 goto call_expansion_failed;
17911 add_loc_descr (&ret, loc_descr);
17914 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17915 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17916 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17917 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17918 add_loc_descr (&ret, ret1);
17919 break;
17921 call_expansion_failed:
17922 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17923 /* There are no opcodes for these operations. */
17924 return 0;
17927 case PREINCREMENT_EXPR:
17928 case PREDECREMENT_EXPR:
17929 case POSTINCREMENT_EXPR:
17930 case POSTDECREMENT_EXPR:
17931 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17932 /* There are no opcodes for these operations. */
17933 return 0;
17935 case ADDR_EXPR:
17936 /* If we already want an address, see if there is INDIRECT_REF inside
17937 e.g. for &this->field. */
17938 if (want_address)
17940 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17941 (loc, want_address == 2, context);
17942 if (list_ret)
17943 have_address = 1;
17944 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17945 && (ret = cst_pool_loc_descr (loc)))
17946 have_address = 1;
17948 /* Otherwise, process the argument and look for the address. */
17949 if (!list_ret && !ret)
17950 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17951 else
17953 if (want_address)
17954 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17955 return NULL;
17957 break;
17959 case VAR_DECL:
17960 if (DECL_THREAD_LOCAL_P (loc))
17962 rtx rtl;
17963 enum dwarf_location_atom tls_op;
17964 enum dtprel_bool dtprel = dtprel_false;
17966 if (targetm.have_tls)
17968 /* If this is not defined, we have no way to emit the
17969 data. */
17970 if (!targetm.asm_out.output_dwarf_dtprel)
17971 return 0;
17973 /* The way DW_OP_GNU_push_tls_address is specified, we
17974 can only look up addresses of objects in the current
17975 module. We used DW_OP_addr as first op, but that's
17976 wrong, because DW_OP_addr is relocated by the debug
17977 info consumer, while DW_OP_GNU_push_tls_address
17978 operand shouldn't be. */
17979 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17980 return 0;
17981 dtprel = dtprel_true;
17982 /* We check for DWARF 5 here because gdb did not implement
17983 DW_OP_form_tls_address until after 7.12. */
17984 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17985 : DW_OP_GNU_push_tls_address);
17987 else
17989 if (!targetm.emutls.debug_form_tls_address
17990 || !(dwarf_version >= 3 || !dwarf_strict))
17991 return 0;
17992 /* We stuffed the control variable into the DECL_VALUE_EXPR
17993 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17994 no longer appear in gimple code. We used the control
17995 variable in specific so that we could pick it up here. */
17996 loc = DECL_VALUE_EXPR (loc);
17997 tls_op = DW_OP_form_tls_address;
18000 rtl = rtl_for_decl_location (loc);
18001 if (rtl == NULL_RTX)
18002 return 0;
18004 if (!MEM_P (rtl))
18005 return 0;
18006 rtl = XEXP (rtl, 0);
18007 if (! CONSTANT_P (rtl))
18008 return 0;
18010 ret = new_addr_loc_descr (rtl, dtprel);
18011 ret1 = new_loc_descr (tls_op, 0, 0);
18012 add_loc_descr (&ret, ret1);
18014 have_address = 1;
18015 break;
18017 /* FALLTHRU */
18019 case PARM_DECL:
18020 if (context != NULL && context->dpi != NULL
18021 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18023 /* We are generating code for a DWARF procedure and we want to access
18024 one of its arguments: find the appropriate argument offset and let
18025 the resolve_args_picking pass compute the offset that complies
18026 with the stack frame size. */
18027 unsigned i = 0;
18028 tree cursor;
18030 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18031 cursor != NULL_TREE && cursor != loc;
18032 cursor = TREE_CHAIN (cursor), ++i)
18034 /* If we are translating a DWARF procedure, all referenced parameters
18035 must belong to the current function. */
18036 gcc_assert (cursor != NULL_TREE);
18038 ret = new_loc_descr (DW_OP_pick, i, 0);
18039 ret->frame_offset_rel = 1;
18040 break;
18042 /* FALLTHRU */
18044 case RESULT_DECL:
18045 if (DECL_HAS_VALUE_EXPR_P (loc))
18046 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18047 want_address, context);
18048 /* FALLTHRU */
18050 case FUNCTION_DECL:
18052 rtx rtl;
18053 var_loc_list *loc_list = lookup_decl_loc (loc);
18055 if (loc_list && loc_list->first)
18057 list_ret = dw_loc_list (loc_list, loc, want_address);
18058 have_address = want_address != 0;
18059 break;
18061 rtl = rtl_for_decl_location (loc);
18062 if (rtl == NULL_RTX)
18064 if (TREE_CODE (loc) != FUNCTION_DECL
18065 && early_dwarf
18066 && current_function_decl
18067 && want_address != 1
18068 && ! DECL_IGNORED_P (loc)
18069 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18070 || POINTER_TYPE_P (TREE_TYPE (loc)))
18071 && DECL_CONTEXT (loc) == current_function_decl
18072 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18073 <= DWARF2_ADDR_SIZE))
18075 dw_die_ref ref = lookup_decl_die (loc);
18076 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18077 if (ref)
18079 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18080 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18081 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18083 else
18085 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18086 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18088 break;
18090 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18091 return 0;
18093 else if (CONST_INT_P (rtl))
18095 HOST_WIDE_INT val = INTVAL (rtl);
18096 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18097 val &= GET_MODE_MASK (DECL_MODE (loc));
18098 ret = int_loc_descriptor (val);
18100 else if (GET_CODE (rtl) == CONST_STRING)
18102 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18103 return 0;
18105 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18106 ret = new_addr_loc_descr (rtl, dtprel_false);
18107 else
18109 machine_mode mode, mem_mode;
18111 /* Certain constructs can only be represented at top-level. */
18112 if (want_address == 2)
18114 ret = loc_descriptor (rtl, VOIDmode,
18115 VAR_INIT_STATUS_INITIALIZED);
18116 have_address = 1;
18118 else
18120 mode = GET_MODE (rtl);
18121 mem_mode = VOIDmode;
18122 if (MEM_P (rtl))
18124 mem_mode = mode;
18125 mode = get_address_mode (rtl);
18126 rtl = XEXP (rtl, 0);
18127 have_address = 1;
18129 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18130 VAR_INIT_STATUS_INITIALIZED);
18132 if (!ret)
18133 expansion_failed (loc, rtl,
18134 "failed to produce loc descriptor for rtl");
18137 break;
18139 case MEM_REF:
18140 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18142 have_address = 1;
18143 goto do_plus;
18145 /* Fallthru. */
18146 case INDIRECT_REF:
18147 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18148 have_address = 1;
18149 break;
18151 case TARGET_MEM_REF:
18152 case SSA_NAME:
18153 case DEBUG_EXPR_DECL:
18154 return NULL;
18156 case COMPOUND_EXPR:
18157 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18158 context);
18160 CASE_CONVERT:
18161 case VIEW_CONVERT_EXPR:
18162 case SAVE_EXPR:
18163 case MODIFY_EXPR:
18164 case NON_LVALUE_EXPR:
18165 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18166 context);
18168 case COMPONENT_REF:
18169 case BIT_FIELD_REF:
18170 case ARRAY_REF:
18171 case ARRAY_RANGE_REF:
18172 case REALPART_EXPR:
18173 case IMAGPART_EXPR:
18175 tree obj, offset;
18176 poly_int64 bitsize, bitpos, bytepos;
18177 machine_mode mode;
18178 int unsignedp, reversep, volatilep = 0;
18180 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18181 &unsignedp, &reversep, &volatilep);
18183 gcc_assert (obj != loc);
18185 list_ret = loc_list_from_tree_1 (obj,
18186 want_address == 2
18187 && known_eq (bitpos, 0)
18188 && !offset ? 2 : 1,
18189 context);
18190 /* TODO: We can extract value of the small expression via shifting even
18191 for nonzero bitpos. */
18192 if (list_ret == 0)
18193 return 0;
18194 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18195 || !multiple_p (bitsize, BITS_PER_UNIT))
18197 expansion_failed (loc, NULL_RTX,
18198 "bitfield access");
18199 return 0;
18202 if (offset != NULL_TREE)
18204 /* Variable offset. */
18205 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18206 if (list_ret1 == 0)
18207 return 0;
18208 add_loc_list (&list_ret, list_ret1);
18209 if (!list_ret)
18210 return 0;
18211 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18214 HOST_WIDE_INT value;
18215 if (bytepos.is_constant (&value) && value > 0)
18216 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18217 value, 0));
18218 else if (maybe_ne (bytepos, 0))
18219 loc_list_plus_const (list_ret, bytepos);
18221 have_address = 1;
18222 break;
18225 case INTEGER_CST:
18226 if ((want_address || !tree_fits_shwi_p (loc))
18227 && (ret = cst_pool_loc_descr (loc)))
18228 have_address = 1;
18229 else if (want_address == 2
18230 && tree_fits_shwi_p (loc)
18231 && (ret = address_of_int_loc_descriptor
18232 (int_size_in_bytes (TREE_TYPE (loc)),
18233 tree_to_shwi (loc))))
18234 have_address = 1;
18235 else if (tree_fits_shwi_p (loc))
18236 ret = int_loc_descriptor (tree_to_shwi (loc));
18237 else if (tree_fits_uhwi_p (loc))
18238 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18239 else
18241 expansion_failed (loc, NULL_RTX,
18242 "Integer operand is not host integer");
18243 return 0;
18245 break;
18247 case CONSTRUCTOR:
18248 case REAL_CST:
18249 case STRING_CST:
18250 case COMPLEX_CST:
18251 if ((ret = cst_pool_loc_descr (loc)))
18252 have_address = 1;
18253 else if (TREE_CODE (loc) == CONSTRUCTOR)
18255 tree type = TREE_TYPE (loc);
18256 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18257 unsigned HOST_WIDE_INT offset = 0;
18258 unsigned HOST_WIDE_INT cnt;
18259 constructor_elt *ce;
18261 if (TREE_CODE (type) == RECORD_TYPE)
18263 /* This is very limited, but it's enough to output
18264 pointers to member functions, as long as the
18265 referenced function is defined in the current
18266 translation unit. */
18267 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18269 tree val = ce->value;
18271 tree field = ce->index;
18273 if (val)
18274 STRIP_NOPS (val);
18276 if (!field || DECL_BIT_FIELD (field))
18278 expansion_failed (loc, NULL_RTX,
18279 "bitfield in record type constructor");
18280 size = offset = (unsigned HOST_WIDE_INT)-1;
18281 ret = NULL;
18282 break;
18285 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18286 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18287 gcc_assert (pos + fieldsize <= size);
18288 if (pos < offset)
18290 expansion_failed (loc, NULL_RTX,
18291 "out-of-order fields in record constructor");
18292 size = offset = (unsigned HOST_WIDE_INT)-1;
18293 ret = NULL;
18294 break;
18296 if (pos > offset)
18298 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18299 add_loc_descr (&ret, ret1);
18300 offset = pos;
18302 if (val && fieldsize != 0)
18304 ret1 = loc_descriptor_from_tree (val, want_address, context);
18305 if (!ret1)
18307 expansion_failed (loc, NULL_RTX,
18308 "unsupported expression in field");
18309 size = offset = (unsigned HOST_WIDE_INT)-1;
18310 ret = NULL;
18311 break;
18313 add_loc_descr (&ret, ret1);
18315 if (fieldsize)
18317 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18318 add_loc_descr (&ret, ret1);
18319 offset = pos + fieldsize;
18323 if (offset != size)
18325 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18326 add_loc_descr (&ret, ret1);
18327 offset = size;
18330 have_address = !!want_address;
18332 else
18333 expansion_failed (loc, NULL_RTX,
18334 "constructor of non-record type");
18336 else
18337 /* We can construct small constants here using int_loc_descriptor. */
18338 expansion_failed (loc, NULL_RTX,
18339 "constructor or constant not in constant pool");
18340 break;
18342 case TRUTH_AND_EXPR:
18343 case TRUTH_ANDIF_EXPR:
18344 case BIT_AND_EXPR:
18345 op = DW_OP_and;
18346 goto do_binop;
18348 case TRUTH_XOR_EXPR:
18349 case BIT_XOR_EXPR:
18350 op = DW_OP_xor;
18351 goto do_binop;
18353 case TRUTH_OR_EXPR:
18354 case TRUTH_ORIF_EXPR:
18355 case BIT_IOR_EXPR:
18356 op = DW_OP_or;
18357 goto do_binop;
18359 case FLOOR_DIV_EXPR:
18360 case CEIL_DIV_EXPR:
18361 case ROUND_DIV_EXPR:
18362 case TRUNC_DIV_EXPR:
18363 case EXACT_DIV_EXPR:
18364 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18365 return 0;
18366 op = DW_OP_div;
18367 goto do_binop;
18369 case MINUS_EXPR:
18370 op = DW_OP_minus;
18371 goto do_binop;
18373 case FLOOR_MOD_EXPR:
18374 case CEIL_MOD_EXPR:
18375 case ROUND_MOD_EXPR:
18376 case TRUNC_MOD_EXPR:
18377 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18379 op = DW_OP_mod;
18380 goto do_binop;
18382 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18383 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18384 if (list_ret == 0 || list_ret1 == 0)
18385 return 0;
18387 add_loc_list (&list_ret, list_ret1);
18388 if (list_ret == 0)
18389 return 0;
18390 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18391 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18392 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18393 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18394 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18395 break;
18397 case MULT_EXPR:
18398 op = DW_OP_mul;
18399 goto do_binop;
18401 case LSHIFT_EXPR:
18402 op = DW_OP_shl;
18403 goto do_binop;
18405 case RSHIFT_EXPR:
18406 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18407 goto do_binop;
18409 case POINTER_PLUS_EXPR:
18410 case PLUS_EXPR:
18411 do_plus:
18412 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18414 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18415 smarter to encode their opposite. The DW_OP_plus_uconst operation
18416 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18417 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18418 bytes, Y being the size of the operation that pushes the opposite
18419 of the addend. So let's choose the smallest representation. */
18420 const tree tree_addend = TREE_OPERAND (loc, 1);
18421 offset_int wi_addend;
18422 HOST_WIDE_INT shwi_addend;
18423 dw_loc_descr_ref loc_naddend;
18425 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18426 if (list_ret == 0)
18427 return 0;
18429 /* Try to get the literal to push. It is the opposite of the addend,
18430 so as we rely on wrapping during DWARF evaluation, first decode
18431 the literal as a "DWARF-sized" signed number. */
18432 wi_addend = wi::to_offset (tree_addend);
18433 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18434 shwi_addend = wi_addend.to_shwi ();
18435 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18436 ? int_loc_descriptor (-shwi_addend)
18437 : NULL;
18439 if (loc_naddend != NULL
18440 && ((unsigned) size_of_uleb128 (shwi_addend)
18441 > size_of_loc_descr (loc_naddend)))
18443 add_loc_descr_to_each (list_ret, loc_naddend);
18444 add_loc_descr_to_each (list_ret,
18445 new_loc_descr (DW_OP_minus, 0, 0));
18447 else
18449 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18451 loc_naddend = loc_cur;
18452 loc_cur = loc_cur->dw_loc_next;
18453 ggc_free (loc_naddend);
18455 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18457 break;
18460 op = DW_OP_plus;
18461 goto do_binop;
18463 case LE_EXPR:
18464 op = DW_OP_le;
18465 goto do_comp_binop;
18467 case GE_EXPR:
18468 op = DW_OP_ge;
18469 goto do_comp_binop;
18471 case LT_EXPR:
18472 op = DW_OP_lt;
18473 goto do_comp_binop;
18475 case GT_EXPR:
18476 op = DW_OP_gt;
18477 goto do_comp_binop;
18479 do_comp_binop:
18480 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18482 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18483 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18484 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18485 TREE_CODE (loc));
18486 break;
18488 else
18489 goto do_binop;
18491 case EQ_EXPR:
18492 op = DW_OP_eq;
18493 goto do_binop;
18495 case NE_EXPR:
18496 op = DW_OP_ne;
18497 goto do_binop;
18499 do_binop:
18500 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18501 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18502 if (list_ret == 0 || list_ret1 == 0)
18503 return 0;
18505 add_loc_list (&list_ret, list_ret1);
18506 if (list_ret == 0)
18507 return 0;
18508 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18509 break;
18511 case TRUTH_NOT_EXPR:
18512 case BIT_NOT_EXPR:
18513 op = DW_OP_not;
18514 goto do_unop;
18516 case ABS_EXPR:
18517 op = DW_OP_abs;
18518 goto do_unop;
18520 case NEGATE_EXPR:
18521 op = DW_OP_neg;
18522 goto do_unop;
18524 do_unop:
18525 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18526 if (list_ret == 0)
18527 return 0;
18529 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18530 break;
18532 case MIN_EXPR:
18533 case MAX_EXPR:
18535 const enum tree_code code =
18536 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18538 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18539 build2 (code, integer_type_node,
18540 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18541 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18544 /* fall through */
18546 case COND_EXPR:
18548 dw_loc_descr_ref lhs
18549 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18550 dw_loc_list_ref rhs
18551 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18552 dw_loc_descr_ref bra_node, jump_node, tmp;
18554 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18555 if (list_ret == 0 || lhs == 0 || rhs == 0)
18556 return 0;
18558 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18559 add_loc_descr_to_each (list_ret, bra_node);
18561 add_loc_list (&list_ret, rhs);
18562 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18563 add_loc_descr_to_each (list_ret, jump_node);
18565 add_loc_descr_to_each (list_ret, lhs);
18566 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18567 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18569 /* ??? Need a node to point the skip at. Use a nop. */
18570 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18571 add_loc_descr_to_each (list_ret, tmp);
18572 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18573 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18575 break;
18577 case FIX_TRUNC_EXPR:
18578 return 0;
18580 default:
18581 /* Leave front-end specific codes as simply unknown. This comes
18582 up, for instance, with the C STMT_EXPR. */
18583 if ((unsigned int) TREE_CODE (loc)
18584 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18586 expansion_failed (loc, NULL_RTX,
18587 "language specific tree node");
18588 return 0;
18591 /* Otherwise this is a generic code; we should just lists all of
18592 these explicitly. We forgot one. */
18593 if (flag_checking)
18594 gcc_unreachable ();
18596 /* In a release build, we want to degrade gracefully: better to
18597 generate incomplete debugging information than to crash. */
18598 return NULL;
18601 if (!ret && !list_ret)
18602 return 0;
18604 if (want_address == 2 && !have_address
18605 && (dwarf_version >= 4 || !dwarf_strict))
18607 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18609 expansion_failed (loc, NULL_RTX,
18610 "DWARF address size mismatch");
18611 return 0;
18613 if (ret)
18614 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18615 else
18616 add_loc_descr_to_each (list_ret,
18617 new_loc_descr (DW_OP_stack_value, 0, 0));
18618 have_address = 1;
18620 /* Show if we can't fill the request for an address. */
18621 if (want_address && !have_address)
18623 expansion_failed (loc, NULL_RTX,
18624 "Want address and only have value");
18625 return 0;
18628 gcc_assert (!ret || !list_ret);
18630 /* If we've got an address and don't want one, dereference. */
18631 if (!want_address && have_address)
18633 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18635 if (size > DWARF2_ADDR_SIZE || size == -1)
18637 expansion_failed (loc, NULL_RTX,
18638 "DWARF address size mismatch");
18639 return 0;
18641 else if (size == DWARF2_ADDR_SIZE)
18642 op = DW_OP_deref;
18643 else
18644 op = DW_OP_deref_size;
18646 if (ret)
18647 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18648 else
18649 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18651 if (ret)
18652 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18654 return list_ret;
18657 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18658 expressions. */
18660 static dw_loc_list_ref
18661 loc_list_from_tree (tree loc, int want_address,
18662 struct loc_descr_context *context)
18664 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18666 for (dw_loc_list_ref loc_cur = result;
18667 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18668 loc_descr_without_nops (loc_cur->expr);
18669 return result;
18672 /* Same as above but return only single location expression. */
18673 static dw_loc_descr_ref
18674 loc_descriptor_from_tree (tree loc, int want_address,
18675 struct loc_descr_context *context)
18677 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18678 if (!ret)
18679 return NULL;
18680 if (ret->dw_loc_next)
18682 expansion_failed (loc, NULL_RTX,
18683 "Location list where only loc descriptor needed");
18684 return NULL;
18686 return ret->expr;
18689 /* Given a value, round it up to the lowest multiple of `boundary'
18690 which is not less than the value itself. */
18692 static inline HOST_WIDE_INT
18693 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18695 return (((value + boundary - 1) / boundary) * boundary);
18698 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18699 pointer to the declared type for the relevant field variable, or return
18700 `integer_type_node' if the given node turns out to be an
18701 ERROR_MARK node. */
18703 static inline tree
18704 field_type (const_tree decl)
18706 tree type;
18708 if (TREE_CODE (decl) == ERROR_MARK)
18709 return integer_type_node;
18711 type = DECL_BIT_FIELD_TYPE (decl);
18712 if (type == NULL_TREE)
18713 type = TREE_TYPE (decl);
18715 return type;
18718 /* Given a pointer to a tree node, return the alignment in bits for
18719 it, or else return BITS_PER_WORD if the node actually turns out to
18720 be an ERROR_MARK node. */
18722 static inline unsigned
18723 simple_type_align_in_bits (const_tree type)
18725 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18728 static inline unsigned
18729 simple_decl_align_in_bits (const_tree decl)
18731 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18734 /* Return the result of rounding T up to ALIGN. */
18736 static inline offset_int
18737 round_up_to_align (const offset_int &t, unsigned int align)
18739 return wi::udiv_trunc (t + align - 1, align) * align;
18742 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18743 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18744 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18745 if we fail to return the size in one of these two forms. */
18747 static dw_loc_descr_ref
18748 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18750 tree tree_size;
18751 struct loc_descr_context ctx;
18753 /* Return a constant integer in priority, if possible. */
18754 *cst_size = int_size_in_bytes (type);
18755 if (*cst_size != -1)
18756 return NULL;
18758 ctx.context_type = const_cast<tree> (type);
18759 ctx.base_decl = NULL_TREE;
18760 ctx.dpi = NULL;
18761 ctx.placeholder_arg = false;
18762 ctx.placeholder_seen = false;
18764 type = TYPE_MAIN_VARIANT (type);
18765 tree_size = TYPE_SIZE_UNIT (type);
18766 return ((tree_size != NULL_TREE)
18767 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18768 : NULL);
18771 /* Helper structure for RECORD_TYPE processing. */
18772 struct vlr_context
18774 /* Root RECORD_TYPE. It is needed to generate data member location
18775 descriptions in variable-length records (VLR), but also to cope with
18776 variants, which are composed of nested structures multiplexed with
18777 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18778 function processing a FIELD_DECL, it is required to be non null. */
18779 tree struct_type;
18780 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18781 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18782 this variant part as part of the root record (in storage units). For
18783 regular records, it must be NULL_TREE. */
18784 tree variant_part_offset;
18787 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18788 addressed byte of the "containing object" for the given FIELD_DECL. If
18789 possible, return a native constant through CST_OFFSET (in which case NULL is
18790 returned); otherwise return a DWARF expression that computes the offset.
18792 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18793 that offset is, either because the argument turns out to be a pointer to an
18794 ERROR_MARK node, or because the offset expression is too complex for us.
18796 CTX is required: see the comment for VLR_CONTEXT. */
18798 static dw_loc_descr_ref
18799 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18800 HOST_WIDE_INT *cst_offset)
18802 tree tree_result;
18803 dw_loc_list_ref loc_result;
18805 *cst_offset = 0;
18807 if (TREE_CODE (decl) == ERROR_MARK)
18808 return NULL;
18809 else
18810 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18812 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18813 case. */
18814 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18815 return NULL;
18817 #ifdef PCC_BITFIELD_TYPE_MATTERS
18818 /* We used to handle only constant offsets in all cases. Now, we handle
18819 properly dynamic byte offsets only when PCC bitfield type doesn't
18820 matter. */
18821 if (PCC_BITFIELD_TYPE_MATTERS
18822 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18824 offset_int object_offset_in_bits;
18825 offset_int object_offset_in_bytes;
18826 offset_int bitpos_int;
18827 tree type;
18828 tree field_size_tree;
18829 offset_int deepest_bitpos;
18830 offset_int field_size_in_bits;
18831 unsigned int type_align_in_bits;
18832 unsigned int decl_align_in_bits;
18833 offset_int type_size_in_bits;
18835 bitpos_int = wi::to_offset (bit_position (decl));
18836 type = field_type (decl);
18837 type_size_in_bits = offset_int_type_size_in_bits (type);
18838 type_align_in_bits = simple_type_align_in_bits (type);
18840 field_size_tree = DECL_SIZE (decl);
18842 /* The size could be unspecified if there was an error, or for
18843 a flexible array member. */
18844 if (!field_size_tree)
18845 field_size_tree = bitsize_zero_node;
18847 /* If the size of the field is not constant, use the type size. */
18848 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18849 field_size_in_bits = wi::to_offset (field_size_tree);
18850 else
18851 field_size_in_bits = type_size_in_bits;
18853 decl_align_in_bits = simple_decl_align_in_bits (decl);
18855 /* The GCC front-end doesn't make any attempt to keep track of the
18856 starting bit offset (relative to the start of the containing
18857 structure type) of the hypothetical "containing object" for a
18858 bit-field. Thus, when computing the byte offset value for the
18859 start of the "containing object" of a bit-field, we must deduce
18860 this information on our own. This can be rather tricky to do in
18861 some cases. For example, handling the following structure type
18862 definition when compiling for an i386/i486 target (which only
18863 aligns long long's to 32-bit boundaries) can be very tricky:
18865 struct S { int field1; long long field2:31; };
18867 Fortunately, there is a simple rule-of-thumb which can be used
18868 in such cases. When compiling for an i386/i486, GCC will
18869 allocate 8 bytes for the structure shown above. It decides to
18870 do this based upon one simple rule for bit-field allocation.
18871 GCC allocates each "containing object" for each bit-field at
18872 the first (i.e. lowest addressed) legitimate alignment boundary
18873 (based upon the required minimum alignment for the declared
18874 type of the field) which it can possibly use, subject to the
18875 condition that there is still enough available space remaining
18876 in the containing object (when allocated at the selected point)
18877 to fully accommodate all of the bits of the bit-field itself.
18879 This simple rule makes it obvious why GCC allocates 8 bytes for
18880 each object of the structure type shown above. When looking
18881 for a place to allocate the "containing object" for `field2',
18882 the compiler simply tries to allocate a 64-bit "containing
18883 object" at each successive 32-bit boundary (starting at zero)
18884 until it finds a place to allocate that 64- bit field such that
18885 at least 31 contiguous (and previously unallocated) bits remain
18886 within that selected 64 bit field. (As it turns out, for the
18887 example above, the compiler finds it is OK to allocate the
18888 "containing object" 64-bit field at bit-offset zero within the
18889 structure type.)
18891 Here we attempt to work backwards from the limited set of facts
18892 we're given, and we try to deduce from those facts, where GCC
18893 must have believed that the containing object started (within
18894 the structure type). The value we deduce is then used (by the
18895 callers of this routine) to generate DW_AT_location and
18896 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18897 the case of DW_AT_location, regular fields as well). */
18899 /* Figure out the bit-distance from the start of the structure to
18900 the "deepest" bit of the bit-field. */
18901 deepest_bitpos = bitpos_int + field_size_in_bits;
18903 /* This is the tricky part. Use some fancy footwork to deduce
18904 where the lowest addressed bit of the containing object must
18905 be. */
18906 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18908 /* Round up to type_align by default. This works best for
18909 bitfields. */
18910 object_offset_in_bits
18911 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18913 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18915 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18917 /* Round up to decl_align instead. */
18918 object_offset_in_bits
18919 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18922 object_offset_in_bytes
18923 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18924 if (ctx->variant_part_offset == NULL_TREE)
18926 *cst_offset = object_offset_in_bytes.to_shwi ();
18927 return NULL;
18929 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18931 else
18932 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18933 tree_result = byte_position (decl);
18935 if (ctx->variant_part_offset != NULL_TREE)
18936 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18937 ctx->variant_part_offset, tree_result);
18939 /* If the byte offset is a constant, it's simplier to handle a native
18940 constant rather than a DWARF expression. */
18941 if (TREE_CODE (tree_result) == INTEGER_CST)
18943 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18944 return NULL;
18946 struct loc_descr_context loc_ctx = {
18947 ctx->struct_type, /* context_type */
18948 NULL_TREE, /* base_decl */
18949 NULL, /* dpi */
18950 false, /* placeholder_arg */
18951 false /* placeholder_seen */
18953 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18955 /* We want a DWARF expression: abort if we only have a location list with
18956 multiple elements. */
18957 if (!loc_result || !single_element_loc_list_p (loc_result))
18958 return NULL;
18959 else
18960 return loc_result->expr;
18963 /* The following routines define various Dwarf attributes and any data
18964 associated with them. */
18966 /* Add a location description attribute value to a DIE.
18968 This emits location attributes suitable for whole variables and
18969 whole parameters. Note that the location attributes for struct fields are
18970 generated by the routine `data_member_location_attribute' below. */
18972 static inline void
18973 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18974 dw_loc_list_ref descr)
18976 bool check_no_locviews = true;
18977 if (descr == 0)
18978 return;
18979 if (single_element_loc_list_p (descr))
18980 add_AT_loc (die, attr_kind, descr->expr);
18981 else
18983 add_AT_loc_list (die, attr_kind, descr);
18984 gcc_assert (descr->ll_symbol);
18985 if (attr_kind == DW_AT_location && descr->vl_symbol
18986 && dwarf2out_locviews_in_attribute ())
18988 add_AT_view_list (die, DW_AT_GNU_locviews);
18989 check_no_locviews = false;
18993 if (check_no_locviews)
18994 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
18997 /* Add DW_AT_accessibility attribute to DIE if needed. */
18999 static void
19000 add_accessibility_attribute (dw_die_ref die, tree decl)
19002 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19003 children, otherwise the default is DW_ACCESS_public. In DWARF2
19004 the default has always been DW_ACCESS_public. */
19005 if (TREE_PROTECTED (decl))
19006 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19007 else if (TREE_PRIVATE (decl))
19009 if (dwarf_version == 2
19010 || die->die_parent == NULL
19011 || die->die_parent->die_tag != DW_TAG_class_type)
19012 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19014 else if (dwarf_version > 2
19015 && die->die_parent
19016 && die->die_parent->die_tag == DW_TAG_class_type)
19017 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19020 /* Attach the specialized form of location attribute used for data members of
19021 struct and union types. In the special case of a FIELD_DECL node which
19022 represents a bit-field, the "offset" part of this special location
19023 descriptor must indicate the distance in bytes from the lowest-addressed
19024 byte of the containing struct or union type to the lowest-addressed byte of
19025 the "containing object" for the bit-field. (See the `field_byte_offset'
19026 function above).
19028 For any given bit-field, the "containing object" is a hypothetical object
19029 (of some integral or enum type) within which the given bit-field lives. The
19030 type of this hypothetical "containing object" is always the same as the
19031 declared type of the individual bit-field itself (for GCC anyway... the
19032 DWARF spec doesn't actually mandate this). Note that it is the size (in
19033 bytes) of the hypothetical "containing object" which will be given in the
19034 DW_AT_byte_size attribute for this bit-field. (See the
19035 `byte_size_attribute' function below.) It is also used when calculating the
19036 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19037 function below.)
19039 CTX is required: see the comment for VLR_CONTEXT. */
19041 static void
19042 add_data_member_location_attribute (dw_die_ref die,
19043 tree decl,
19044 struct vlr_context *ctx)
19046 HOST_WIDE_INT offset;
19047 dw_loc_descr_ref loc_descr = 0;
19049 if (TREE_CODE (decl) == TREE_BINFO)
19051 /* We're working on the TAG_inheritance for a base class. */
19052 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19054 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19055 aren't at a fixed offset from all (sub)objects of the same
19056 type. We need to extract the appropriate offset from our
19057 vtable. The following dwarf expression means
19059 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19061 This is specific to the V3 ABI, of course. */
19063 dw_loc_descr_ref tmp;
19065 /* Make a copy of the object address. */
19066 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19067 add_loc_descr (&loc_descr, tmp);
19069 /* Extract the vtable address. */
19070 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19071 add_loc_descr (&loc_descr, tmp);
19073 /* Calculate the address of the offset. */
19074 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19075 gcc_assert (offset < 0);
19077 tmp = int_loc_descriptor (-offset);
19078 add_loc_descr (&loc_descr, tmp);
19079 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19080 add_loc_descr (&loc_descr, tmp);
19082 /* Extract the offset. */
19083 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19084 add_loc_descr (&loc_descr, tmp);
19086 /* Add it to the object address. */
19087 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19088 add_loc_descr (&loc_descr, tmp);
19090 else
19091 offset = tree_to_shwi (BINFO_OFFSET (decl));
19093 else
19095 loc_descr = field_byte_offset (decl, ctx, &offset);
19097 /* If loc_descr is available then we know the field offset is dynamic.
19098 However, GDB does not handle dynamic field offsets very well at the
19099 moment. */
19100 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19102 loc_descr = NULL;
19103 offset = 0;
19106 /* Data member location evalutation starts with the base address on the
19107 stack. Compute the field offset and add it to this base address. */
19108 else if (loc_descr != NULL)
19109 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19112 if (! loc_descr)
19114 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19115 e.g. GDB only added support to it in November 2016. For DWARF5
19116 we need newer debug info consumers anyway. We might change this
19117 to dwarf_version >= 4 once most consumers catched up. */
19118 if (dwarf_version >= 5
19119 && TREE_CODE (decl) == FIELD_DECL
19120 && DECL_BIT_FIELD_TYPE (decl))
19122 tree off = bit_position (decl);
19123 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19125 remove_AT (die, DW_AT_byte_size);
19126 remove_AT (die, DW_AT_bit_offset);
19127 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19128 return;
19131 if (dwarf_version > 2)
19133 /* Don't need to output a location expression, just the constant. */
19134 if (offset < 0)
19135 add_AT_int (die, DW_AT_data_member_location, offset);
19136 else
19137 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19138 return;
19140 else
19142 enum dwarf_location_atom op;
19144 /* The DWARF2 standard says that we should assume that the structure
19145 address is already on the stack, so we can specify a structure
19146 field address by using DW_OP_plus_uconst. */
19147 op = DW_OP_plus_uconst;
19148 loc_descr = new_loc_descr (op, offset, 0);
19152 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19155 /* Writes integer values to dw_vec_const array. */
19157 static void
19158 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19160 while (size != 0)
19162 *dest++ = val & 0xff;
19163 val >>= 8;
19164 --size;
19168 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19170 static HOST_WIDE_INT
19171 extract_int (const unsigned char *src, unsigned int size)
19173 HOST_WIDE_INT val = 0;
19175 src += size;
19176 while (size != 0)
19178 val <<= 8;
19179 val |= *--src & 0xff;
19180 --size;
19182 return val;
19185 /* Writes wide_int values to dw_vec_const array. */
19187 static void
19188 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19190 int i;
19192 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19194 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19195 return;
19198 /* We'd have to extend this code to support odd sizes. */
19199 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19201 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19203 if (WORDS_BIG_ENDIAN)
19204 for (i = n - 1; i >= 0; i--)
19206 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19207 dest += sizeof (HOST_WIDE_INT);
19209 else
19210 for (i = 0; i < n; i++)
19212 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19213 dest += sizeof (HOST_WIDE_INT);
19217 /* Writes floating point values to dw_vec_const array. */
19219 static void
19220 insert_float (const_rtx rtl, unsigned char *array)
19222 long val[4];
19223 int i;
19224 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19226 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19228 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19229 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19231 insert_int (val[i], 4, array);
19232 array += 4;
19236 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19237 does not have a "location" either in memory or in a register. These
19238 things can arise in GNU C when a constant is passed as an actual parameter
19239 to an inlined function. They can also arise in C++ where declared
19240 constants do not necessarily get memory "homes". */
19242 static bool
19243 add_const_value_attribute (dw_die_ref die, rtx rtl)
19245 switch (GET_CODE (rtl))
19247 case CONST_INT:
19249 HOST_WIDE_INT val = INTVAL (rtl);
19251 if (val < 0)
19252 add_AT_int (die, DW_AT_const_value, val);
19253 else
19254 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19256 return true;
19258 case CONST_WIDE_INT:
19260 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19261 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19262 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19263 wide_int w = wi::zext (w1, prec);
19264 add_AT_wide (die, DW_AT_const_value, w);
19266 return true;
19268 case CONST_DOUBLE:
19269 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19270 floating-point constant. A CONST_DOUBLE is used whenever the
19271 constant requires more than one word in order to be adequately
19272 represented. */
19273 if (TARGET_SUPPORTS_WIDE_INT == 0
19274 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19275 add_AT_double (die, DW_AT_const_value,
19276 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19277 else
19279 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19280 unsigned int length = GET_MODE_SIZE (mode);
19281 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19283 insert_float (rtl, array);
19284 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19286 return true;
19288 case CONST_VECTOR:
19290 unsigned int length;
19291 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19292 return false;
19294 machine_mode mode = GET_MODE (rtl);
19295 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19296 unsigned char *array
19297 = ggc_vec_alloc<unsigned char> (length * elt_size);
19298 unsigned int i;
19299 unsigned char *p;
19300 machine_mode imode = GET_MODE_INNER (mode);
19302 switch (GET_MODE_CLASS (mode))
19304 case MODE_VECTOR_INT:
19305 for (i = 0, p = array; i < length; i++, p += elt_size)
19307 rtx elt = CONST_VECTOR_ELT (rtl, i);
19308 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19310 break;
19312 case MODE_VECTOR_FLOAT:
19313 for (i = 0, p = array; i < length; i++, p += elt_size)
19315 rtx elt = CONST_VECTOR_ELT (rtl, i);
19316 insert_float (elt, p);
19318 break;
19320 default:
19321 gcc_unreachable ();
19324 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19326 return true;
19328 case CONST_STRING:
19329 if (dwarf_version >= 4 || !dwarf_strict)
19331 dw_loc_descr_ref loc_result;
19332 resolve_one_addr (&rtl);
19333 rtl_addr:
19334 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19335 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19336 add_AT_loc (die, DW_AT_location, loc_result);
19337 vec_safe_push (used_rtx_array, rtl);
19338 return true;
19340 return false;
19342 case CONST:
19343 if (CONSTANT_P (XEXP (rtl, 0)))
19344 return add_const_value_attribute (die, XEXP (rtl, 0));
19345 /* FALLTHROUGH */
19346 case SYMBOL_REF:
19347 if (!const_ok_for_output (rtl))
19348 return false;
19349 /* FALLTHROUGH */
19350 case LABEL_REF:
19351 if (dwarf_version >= 4 || !dwarf_strict)
19352 goto rtl_addr;
19353 return false;
19355 case PLUS:
19356 /* In cases where an inlined instance of an inline function is passed
19357 the address of an `auto' variable (which is local to the caller) we
19358 can get a situation where the DECL_RTL of the artificial local
19359 variable (for the inlining) which acts as a stand-in for the
19360 corresponding formal parameter (of the inline function) will look
19361 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19362 exactly a compile-time constant expression, but it isn't the address
19363 of the (artificial) local variable either. Rather, it represents the
19364 *value* which the artificial local variable always has during its
19365 lifetime. We currently have no way to represent such quasi-constant
19366 values in Dwarf, so for now we just punt and generate nothing. */
19367 return false;
19369 case HIGH:
19370 case CONST_FIXED:
19371 return false;
19373 case MEM:
19374 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19375 && MEM_READONLY_P (rtl)
19376 && GET_MODE (rtl) == BLKmode)
19378 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19379 return true;
19381 return false;
19383 default:
19384 /* No other kinds of rtx should be possible here. */
19385 gcc_unreachable ();
19387 return false;
19390 /* Determine whether the evaluation of EXPR references any variables
19391 or functions which aren't otherwise used (and therefore may not be
19392 output). */
19393 static tree
19394 reference_to_unused (tree * tp, int * walk_subtrees,
19395 void * data ATTRIBUTE_UNUSED)
19397 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19398 *walk_subtrees = 0;
19400 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19401 && ! TREE_ASM_WRITTEN (*tp))
19402 return *tp;
19403 /* ??? The C++ FE emits debug information for using decls, so
19404 putting gcc_unreachable here falls over. See PR31899. For now
19405 be conservative. */
19406 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19407 return *tp;
19408 else if (VAR_P (*tp))
19410 varpool_node *node = varpool_node::get (*tp);
19411 if (!node || !node->definition)
19412 return *tp;
19414 else if (TREE_CODE (*tp) == FUNCTION_DECL
19415 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19417 /* The call graph machinery must have finished analyzing,
19418 optimizing and gimplifying the CU by now.
19419 So if *TP has no call graph node associated
19420 to it, it means *TP will not be emitted. */
19421 if (!cgraph_node::get (*tp))
19422 return *tp;
19424 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19425 return *tp;
19427 return NULL_TREE;
19430 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19431 for use in a later add_const_value_attribute call. */
19433 static rtx
19434 rtl_for_decl_init (tree init, tree type)
19436 rtx rtl = NULL_RTX;
19438 STRIP_NOPS (init);
19440 /* If a variable is initialized with a string constant without embedded
19441 zeros, build CONST_STRING. */
19442 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19444 tree enttype = TREE_TYPE (type);
19445 tree domain = TYPE_DOMAIN (type);
19446 scalar_int_mode mode;
19448 if (is_int_mode (TYPE_MODE (enttype), &mode)
19449 && GET_MODE_SIZE (mode) == 1
19450 && domain
19451 && integer_zerop (TYPE_MIN_VALUE (domain))
19452 && compare_tree_int (TYPE_MAX_VALUE (domain),
19453 TREE_STRING_LENGTH (init) - 1) == 0
19454 && ((size_t) TREE_STRING_LENGTH (init)
19455 == strlen (TREE_STRING_POINTER (init)) + 1))
19457 rtl = gen_rtx_CONST_STRING (VOIDmode,
19458 ggc_strdup (TREE_STRING_POINTER (init)));
19459 rtl = gen_rtx_MEM (BLKmode, rtl);
19460 MEM_READONLY_P (rtl) = 1;
19463 /* Other aggregates, and complex values, could be represented using
19464 CONCAT: FIXME! */
19465 else if (AGGREGATE_TYPE_P (type)
19466 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19467 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19468 || TREE_CODE (type) == COMPLEX_TYPE)
19470 /* Vectors only work if their mode is supported by the target.
19471 FIXME: generic vectors ought to work too. */
19472 else if (TREE_CODE (type) == VECTOR_TYPE
19473 && !VECTOR_MODE_P (TYPE_MODE (type)))
19475 /* If the initializer is something that we know will expand into an
19476 immediate RTL constant, expand it now. We must be careful not to
19477 reference variables which won't be output. */
19478 else if (initializer_constant_valid_p (init, type)
19479 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19481 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19482 possible. */
19483 if (TREE_CODE (type) == VECTOR_TYPE)
19484 switch (TREE_CODE (init))
19486 case VECTOR_CST:
19487 break;
19488 case CONSTRUCTOR:
19489 if (TREE_CONSTANT (init))
19491 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19492 bool constant_p = true;
19493 tree value;
19494 unsigned HOST_WIDE_INT ix;
19496 /* Even when ctor is constant, it might contain non-*_CST
19497 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19498 belong into VECTOR_CST nodes. */
19499 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19500 if (!CONSTANT_CLASS_P (value))
19502 constant_p = false;
19503 break;
19506 if (constant_p)
19508 init = build_vector_from_ctor (type, elts);
19509 break;
19512 /* FALLTHRU */
19514 default:
19515 return NULL;
19518 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19520 /* If expand_expr returns a MEM, it wasn't immediate. */
19521 gcc_assert (!rtl || !MEM_P (rtl));
19524 return rtl;
19527 /* Generate RTL for the variable DECL to represent its location. */
19529 static rtx
19530 rtl_for_decl_location (tree decl)
19532 rtx rtl;
19534 /* Here we have to decide where we are going to say the parameter "lives"
19535 (as far as the debugger is concerned). We only have a couple of
19536 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19538 DECL_RTL normally indicates where the parameter lives during most of the
19539 activation of the function. If optimization is enabled however, this
19540 could be either NULL or else a pseudo-reg. Both of those cases indicate
19541 that the parameter doesn't really live anywhere (as far as the code
19542 generation parts of GCC are concerned) during most of the function's
19543 activation. That will happen (for example) if the parameter is never
19544 referenced within the function.
19546 We could just generate a location descriptor here for all non-NULL
19547 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19548 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19549 where DECL_RTL is NULL or is a pseudo-reg.
19551 Note however that we can only get away with using DECL_INCOMING_RTL as
19552 a backup substitute for DECL_RTL in certain limited cases. In cases
19553 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19554 we can be sure that the parameter was passed using the same type as it is
19555 declared to have within the function, and that its DECL_INCOMING_RTL
19556 points us to a place where a value of that type is passed.
19558 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19559 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19560 because in these cases DECL_INCOMING_RTL points us to a value of some
19561 type which is *different* from the type of the parameter itself. Thus,
19562 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19563 such cases, the debugger would end up (for example) trying to fetch a
19564 `float' from a place which actually contains the first part of a
19565 `double'. That would lead to really incorrect and confusing
19566 output at debug-time.
19568 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19569 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19570 are a couple of exceptions however. On little-endian machines we can
19571 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19572 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19573 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19574 when (on a little-endian machine) a non-prototyped function has a
19575 parameter declared to be of type `short' or `char'. In such cases,
19576 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19577 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19578 passed `int' value. If the debugger then uses that address to fetch
19579 a `short' or a `char' (on a little-endian machine) the result will be
19580 the correct data, so we allow for such exceptional cases below.
19582 Note that our goal here is to describe the place where the given formal
19583 parameter lives during most of the function's activation (i.e. between the
19584 end of the prologue and the start of the epilogue). We'll do that as best
19585 as we can. Note however that if the given formal parameter is modified
19586 sometime during the execution of the function, then a stack backtrace (at
19587 debug-time) will show the function as having been called with the *new*
19588 value rather than the value which was originally passed in. This happens
19589 rarely enough that it is not a major problem, but it *is* a problem, and
19590 I'd like to fix it.
19592 A future version of dwarf2out.c may generate two additional attributes for
19593 any given DW_TAG_formal_parameter DIE which will describe the "passed
19594 type" and the "passed location" for the given formal parameter in addition
19595 to the attributes we now generate to indicate the "declared type" and the
19596 "active location" for each parameter. This additional set of attributes
19597 could be used by debuggers for stack backtraces. Separately, note that
19598 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19599 This happens (for example) for inlined-instances of inline function formal
19600 parameters which are never referenced. This really shouldn't be
19601 happening. All PARM_DECL nodes should get valid non-NULL
19602 DECL_INCOMING_RTL values. FIXME. */
19604 /* Use DECL_RTL as the "location" unless we find something better. */
19605 rtl = DECL_RTL_IF_SET (decl);
19607 /* When generating abstract instances, ignore everything except
19608 constants, symbols living in memory, and symbols living in
19609 fixed registers. */
19610 if (! reload_completed)
19612 if (rtl
19613 && (CONSTANT_P (rtl)
19614 || (MEM_P (rtl)
19615 && CONSTANT_P (XEXP (rtl, 0)))
19616 || (REG_P (rtl)
19617 && VAR_P (decl)
19618 && TREE_STATIC (decl))))
19620 rtl = targetm.delegitimize_address (rtl);
19621 return rtl;
19623 rtl = NULL_RTX;
19625 else if (TREE_CODE (decl) == PARM_DECL)
19627 if (rtl == NULL_RTX
19628 || is_pseudo_reg (rtl)
19629 || (MEM_P (rtl)
19630 && is_pseudo_reg (XEXP (rtl, 0))
19631 && DECL_INCOMING_RTL (decl)
19632 && MEM_P (DECL_INCOMING_RTL (decl))
19633 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19635 tree declared_type = TREE_TYPE (decl);
19636 tree passed_type = DECL_ARG_TYPE (decl);
19637 machine_mode dmode = TYPE_MODE (declared_type);
19638 machine_mode pmode = TYPE_MODE (passed_type);
19640 /* This decl represents a formal parameter which was optimized out.
19641 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19642 all cases where (rtl == NULL_RTX) just below. */
19643 if (dmode == pmode)
19644 rtl = DECL_INCOMING_RTL (decl);
19645 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19646 && SCALAR_INT_MODE_P (dmode)
19647 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19648 && DECL_INCOMING_RTL (decl))
19650 rtx inc = DECL_INCOMING_RTL (decl);
19651 if (REG_P (inc))
19652 rtl = inc;
19653 else if (MEM_P (inc))
19655 if (BYTES_BIG_ENDIAN)
19656 rtl = adjust_address_nv (inc, dmode,
19657 GET_MODE_SIZE (pmode)
19658 - GET_MODE_SIZE (dmode));
19659 else
19660 rtl = inc;
19665 /* If the parm was passed in registers, but lives on the stack, then
19666 make a big endian correction if the mode of the type of the
19667 parameter is not the same as the mode of the rtl. */
19668 /* ??? This is the same series of checks that are made in dbxout.c before
19669 we reach the big endian correction code there. It isn't clear if all
19670 of these checks are necessary here, but keeping them all is the safe
19671 thing to do. */
19672 else if (MEM_P (rtl)
19673 && XEXP (rtl, 0) != const0_rtx
19674 && ! CONSTANT_P (XEXP (rtl, 0))
19675 /* Not passed in memory. */
19676 && !MEM_P (DECL_INCOMING_RTL (decl))
19677 /* Not passed by invisible reference. */
19678 && (!REG_P (XEXP (rtl, 0))
19679 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19680 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19681 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19682 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19683 #endif
19685 /* Big endian correction check. */
19686 && BYTES_BIG_ENDIAN
19687 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19688 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19689 UNITS_PER_WORD))
19691 machine_mode addr_mode = get_address_mode (rtl);
19692 poly_int64 offset = (UNITS_PER_WORD
19693 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19695 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19696 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19699 else if (VAR_P (decl)
19700 && rtl
19701 && MEM_P (rtl)
19702 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19704 machine_mode addr_mode = get_address_mode (rtl);
19705 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19706 GET_MODE (rtl));
19708 /* If a variable is declared "register" yet is smaller than
19709 a register, then if we store the variable to memory, it
19710 looks like we're storing a register-sized value, when in
19711 fact we are not. We need to adjust the offset of the
19712 storage location to reflect the actual value's bytes,
19713 else gdb will not be able to display it. */
19714 if (maybe_ne (offset, 0))
19715 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19716 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19719 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19720 and will have been substituted directly into all expressions that use it.
19721 C does not have such a concept, but C++ and other languages do. */
19722 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19723 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19725 if (rtl)
19726 rtl = targetm.delegitimize_address (rtl);
19728 /* If we don't look past the constant pool, we risk emitting a
19729 reference to a constant pool entry that isn't referenced from
19730 code, and thus is not emitted. */
19731 if (rtl)
19732 rtl = avoid_constant_pool_reference (rtl);
19734 /* Try harder to get a rtl. If this symbol ends up not being emitted
19735 in the current CU, resolve_addr will remove the expression referencing
19736 it. */
19737 if (rtl == NULL_RTX
19738 && VAR_P (decl)
19739 && !DECL_EXTERNAL (decl)
19740 && TREE_STATIC (decl)
19741 && DECL_NAME (decl)
19742 && !DECL_HARD_REGISTER (decl)
19743 && DECL_MODE (decl) != VOIDmode)
19745 rtl = make_decl_rtl_for_debug (decl);
19746 if (!MEM_P (rtl)
19747 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19748 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19749 rtl = NULL_RTX;
19752 return rtl;
19755 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19756 returned. If so, the decl for the COMMON block is returned, and the
19757 value is the offset into the common block for the symbol. */
19759 static tree
19760 fortran_common (tree decl, HOST_WIDE_INT *value)
19762 tree val_expr, cvar;
19763 machine_mode mode;
19764 poly_int64 bitsize, bitpos;
19765 tree offset;
19766 HOST_WIDE_INT cbitpos;
19767 int unsignedp, reversep, volatilep = 0;
19769 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19770 it does not have a value (the offset into the common area), or if it
19771 is thread local (as opposed to global) then it isn't common, and shouldn't
19772 be handled as such. */
19773 if (!VAR_P (decl)
19774 || !TREE_STATIC (decl)
19775 || !DECL_HAS_VALUE_EXPR_P (decl)
19776 || !is_fortran ())
19777 return NULL_TREE;
19779 val_expr = DECL_VALUE_EXPR (decl);
19780 if (TREE_CODE (val_expr) != COMPONENT_REF)
19781 return NULL_TREE;
19783 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19784 &unsignedp, &reversep, &volatilep);
19786 if (cvar == NULL_TREE
19787 || !VAR_P (cvar)
19788 || DECL_ARTIFICIAL (cvar)
19789 || !TREE_PUBLIC (cvar)
19790 /* We don't expect to have to cope with variable offsets,
19791 since at present all static data must have a constant size. */
19792 || !bitpos.is_constant (&cbitpos))
19793 return NULL_TREE;
19795 *value = 0;
19796 if (offset != NULL)
19798 if (!tree_fits_shwi_p (offset))
19799 return NULL_TREE;
19800 *value = tree_to_shwi (offset);
19802 if (cbitpos != 0)
19803 *value += cbitpos / BITS_PER_UNIT;
19805 return cvar;
19808 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19809 data attribute for a variable or a parameter. We generate the
19810 DW_AT_const_value attribute only in those cases where the given variable
19811 or parameter does not have a true "location" either in memory or in a
19812 register. This can happen (for example) when a constant is passed as an
19813 actual argument in a call to an inline function. (It's possible that
19814 these things can crop up in other ways also.) Note that one type of
19815 constant value which can be passed into an inlined function is a constant
19816 pointer. This can happen for example if an actual argument in an inlined
19817 function call evaluates to a compile-time constant address.
19819 CACHE_P is true if it is worth caching the location list for DECL,
19820 so that future calls can reuse it rather than regenerate it from scratch.
19821 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19822 since we will need to refer to them each time the function is inlined. */
19824 static bool
19825 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19827 rtx rtl;
19828 dw_loc_list_ref list;
19829 var_loc_list *loc_list;
19830 cached_dw_loc_list *cache;
19832 if (early_dwarf)
19833 return false;
19835 if (TREE_CODE (decl) == ERROR_MARK)
19836 return false;
19838 if (get_AT (die, DW_AT_location)
19839 || get_AT (die, DW_AT_const_value))
19840 return true;
19842 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19843 || TREE_CODE (decl) == RESULT_DECL);
19845 /* Try to get some constant RTL for this decl, and use that as the value of
19846 the location. */
19848 rtl = rtl_for_decl_location (decl);
19849 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19850 && add_const_value_attribute (die, rtl))
19851 return true;
19853 /* See if we have single element location list that is equivalent to
19854 a constant value. That way we are better to use add_const_value_attribute
19855 rather than expanding constant value equivalent. */
19856 loc_list = lookup_decl_loc (decl);
19857 if (loc_list
19858 && loc_list->first
19859 && loc_list->first->next == NULL
19860 && NOTE_P (loc_list->first->loc)
19861 && NOTE_VAR_LOCATION (loc_list->first->loc)
19862 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19864 struct var_loc_node *node;
19866 node = loc_list->first;
19867 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19868 if (GET_CODE (rtl) == EXPR_LIST)
19869 rtl = XEXP (rtl, 0);
19870 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19871 && add_const_value_attribute (die, rtl))
19872 return true;
19874 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19875 list several times. See if we've already cached the contents. */
19876 list = NULL;
19877 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19878 cache_p = false;
19879 if (cache_p)
19881 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19882 if (cache)
19883 list = cache->loc_list;
19885 if (list == NULL)
19887 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19888 NULL);
19889 /* It is usually worth caching this result if the decl is from
19890 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19891 if (cache_p && list && list->dw_loc_next)
19893 cached_dw_loc_list **slot
19894 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19895 DECL_UID (decl),
19896 INSERT);
19897 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19898 cache->decl_id = DECL_UID (decl);
19899 cache->loc_list = list;
19900 *slot = cache;
19903 if (list)
19905 add_AT_location_description (die, DW_AT_location, list);
19906 return true;
19908 /* None of that worked, so it must not really have a location;
19909 try adding a constant value attribute from the DECL_INITIAL. */
19910 return tree_add_const_value_attribute_for_decl (die, decl);
19913 /* Helper function for tree_add_const_value_attribute. Natively encode
19914 initializer INIT into an array. Return true if successful. */
19916 static bool
19917 native_encode_initializer (tree init, unsigned char *array, int size)
19919 tree type;
19921 if (init == NULL_TREE)
19922 return false;
19924 STRIP_NOPS (init);
19925 switch (TREE_CODE (init))
19927 case STRING_CST:
19928 type = TREE_TYPE (init);
19929 if (TREE_CODE (type) == ARRAY_TYPE)
19931 tree enttype = TREE_TYPE (type);
19932 scalar_int_mode mode;
19934 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19935 || GET_MODE_SIZE (mode) != 1)
19936 return false;
19937 if (int_size_in_bytes (type) != size)
19938 return false;
19939 if (size > TREE_STRING_LENGTH (init))
19941 memcpy (array, TREE_STRING_POINTER (init),
19942 TREE_STRING_LENGTH (init));
19943 memset (array + TREE_STRING_LENGTH (init),
19944 '\0', size - TREE_STRING_LENGTH (init));
19946 else
19947 memcpy (array, TREE_STRING_POINTER (init), size);
19948 return true;
19950 return false;
19951 case CONSTRUCTOR:
19952 type = TREE_TYPE (init);
19953 if (int_size_in_bytes (type) != size)
19954 return false;
19955 if (TREE_CODE (type) == ARRAY_TYPE)
19957 HOST_WIDE_INT min_index;
19958 unsigned HOST_WIDE_INT cnt;
19959 int curpos = 0, fieldsize;
19960 constructor_elt *ce;
19962 if (TYPE_DOMAIN (type) == NULL_TREE
19963 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19964 return false;
19966 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19967 if (fieldsize <= 0)
19968 return false;
19970 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19971 memset (array, '\0', size);
19972 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19974 tree val = ce->value;
19975 tree index = ce->index;
19976 int pos = curpos;
19977 if (index && TREE_CODE (index) == RANGE_EXPR)
19978 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19979 * fieldsize;
19980 else if (index)
19981 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19983 if (val)
19985 STRIP_NOPS (val);
19986 if (!native_encode_initializer (val, array + pos, fieldsize))
19987 return false;
19989 curpos = pos + fieldsize;
19990 if (index && TREE_CODE (index) == RANGE_EXPR)
19992 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19993 - tree_to_shwi (TREE_OPERAND (index, 0));
19994 while (count-- > 0)
19996 if (val)
19997 memcpy (array + curpos, array + pos, fieldsize);
19998 curpos += fieldsize;
20001 gcc_assert (curpos <= size);
20003 return true;
20005 else if (TREE_CODE (type) == RECORD_TYPE
20006 || TREE_CODE (type) == UNION_TYPE)
20008 tree field = NULL_TREE;
20009 unsigned HOST_WIDE_INT cnt;
20010 constructor_elt *ce;
20012 if (int_size_in_bytes (type) != size)
20013 return false;
20015 if (TREE_CODE (type) == RECORD_TYPE)
20016 field = TYPE_FIELDS (type);
20018 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20020 tree val = ce->value;
20021 int pos, fieldsize;
20023 if (ce->index != 0)
20024 field = ce->index;
20026 if (val)
20027 STRIP_NOPS (val);
20029 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20030 return false;
20032 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20033 && TYPE_DOMAIN (TREE_TYPE (field))
20034 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20035 return false;
20036 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20037 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20038 return false;
20039 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20040 pos = int_byte_position (field);
20041 gcc_assert (pos + fieldsize <= size);
20042 if (val && fieldsize != 0
20043 && !native_encode_initializer (val, array + pos, fieldsize))
20044 return false;
20046 return true;
20048 return false;
20049 case VIEW_CONVERT_EXPR:
20050 case NON_LVALUE_EXPR:
20051 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20052 default:
20053 return native_encode_expr (init, array, size) == size;
20057 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20058 attribute is the const value T. */
20060 static bool
20061 tree_add_const_value_attribute (dw_die_ref die, tree t)
20063 tree init;
20064 tree type = TREE_TYPE (t);
20065 rtx rtl;
20067 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20068 return false;
20070 init = t;
20071 gcc_assert (!DECL_P (init));
20073 if (TREE_CODE (init) == INTEGER_CST)
20075 if (tree_fits_uhwi_p (init))
20077 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20078 return true;
20080 if (tree_fits_shwi_p (init))
20082 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20083 return true;
20086 if (! early_dwarf)
20088 rtl = rtl_for_decl_init (init, type);
20089 if (rtl)
20090 return add_const_value_attribute (die, rtl);
20092 /* If the host and target are sane, try harder. */
20093 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20094 && initializer_constant_valid_p (init, type))
20096 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20097 if (size > 0 && (int) size == size)
20099 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20101 if (native_encode_initializer (init, array, size))
20103 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20104 return true;
20106 ggc_free (array);
20109 return false;
20112 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20113 attribute is the const value of T, where T is an integral constant
20114 variable with static storage duration
20115 (so it can't be a PARM_DECL or a RESULT_DECL). */
20117 static bool
20118 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20121 if (!decl
20122 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20123 || (VAR_P (decl) && !TREE_STATIC (decl)))
20124 return false;
20126 if (TREE_READONLY (decl)
20127 && ! TREE_THIS_VOLATILE (decl)
20128 && DECL_INITIAL (decl))
20129 /* OK */;
20130 else
20131 return false;
20133 /* Don't add DW_AT_const_value if abstract origin already has one. */
20134 if (get_AT (var_die, DW_AT_const_value))
20135 return false;
20137 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20140 /* Convert the CFI instructions for the current function into a
20141 location list. This is used for DW_AT_frame_base when we targeting
20142 a dwarf2 consumer that does not support the dwarf3
20143 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20144 expressions. */
20146 static dw_loc_list_ref
20147 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20149 int ix;
20150 dw_fde_ref fde;
20151 dw_loc_list_ref list, *list_tail;
20152 dw_cfi_ref cfi;
20153 dw_cfa_location last_cfa, next_cfa;
20154 const char *start_label, *last_label, *section;
20155 dw_cfa_location remember;
20157 fde = cfun->fde;
20158 gcc_assert (fde != NULL);
20160 section = secname_for_decl (current_function_decl);
20161 list_tail = &list;
20162 list = NULL;
20164 memset (&next_cfa, 0, sizeof (next_cfa));
20165 next_cfa.reg = INVALID_REGNUM;
20166 remember = next_cfa;
20168 start_label = fde->dw_fde_begin;
20170 /* ??? Bald assumption that the CIE opcode list does not contain
20171 advance opcodes. */
20172 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20173 lookup_cfa_1 (cfi, &next_cfa, &remember);
20175 last_cfa = next_cfa;
20176 last_label = start_label;
20178 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20180 /* If the first partition contained no CFI adjustments, the
20181 CIE opcodes apply to the whole first partition. */
20182 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20183 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20184 list_tail =&(*list_tail)->dw_loc_next;
20185 start_label = last_label = fde->dw_fde_second_begin;
20188 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20190 switch (cfi->dw_cfi_opc)
20192 case DW_CFA_set_loc:
20193 case DW_CFA_advance_loc1:
20194 case DW_CFA_advance_loc2:
20195 case DW_CFA_advance_loc4:
20196 if (!cfa_equal_p (&last_cfa, &next_cfa))
20198 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20199 start_label, 0, last_label, 0, section);
20201 list_tail = &(*list_tail)->dw_loc_next;
20202 last_cfa = next_cfa;
20203 start_label = last_label;
20205 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20206 break;
20208 case DW_CFA_advance_loc:
20209 /* The encoding is complex enough that we should never emit this. */
20210 gcc_unreachable ();
20212 default:
20213 lookup_cfa_1 (cfi, &next_cfa, &remember);
20214 break;
20216 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20218 if (!cfa_equal_p (&last_cfa, &next_cfa))
20220 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20221 start_label, 0, last_label, 0, section);
20223 list_tail = &(*list_tail)->dw_loc_next;
20224 last_cfa = next_cfa;
20225 start_label = last_label;
20227 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20228 start_label, 0, fde->dw_fde_end, 0, section);
20229 list_tail = &(*list_tail)->dw_loc_next;
20230 start_label = last_label = fde->dw_fde_second_begin;
20234 if (!cfa_equal_p (&last_cfa, &next_cfa))
20236 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20237 start_label, 0, last_label, 0, section);
20238 list_tail = &(*list_tail)->dw_loc_next;
20239 start_label = last_label;
20242 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20243 start_label, 0,
20244 fde->dw_fde_second_begin
20245 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20246 section);
20248 maybe_gen_llsym (list);
20250 return list;
20253 /* Compute a displacement from the "steady-state frame pointer" to the
20254 frame base (often the same as the CFA), and store it in
20255 frame_pointer_fb_offset. OFFSET is added to the displacement
20256 before the latter is negated. */
20258 static void
20259 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20261 rtx reg, elim;
20263 #ifdef FRAME_POINTER_CFA_OFFSET
20264 reg = frame_pointer_rtx;
20265 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20266 #else
20267 reg = arg_pointer_rtx;
20268 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20269 #endif
20271 elim = (ira_use_lra_p
20272 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20273 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20274 elim = strip_offset_and_add (elim, &offset);
20276 frame_pointer_fb_offset = -offset;
20278 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20279 in which to eliminate. This is because it's stack pointer isn't
20280 directly accessible as a register within the ISA. To work around
20281 this, assume that while we cannot provide a proper value for
20282 frame_pointer_fb_offset, we won't need one either. */
20283 frame_pointer_fb_offset_valid
20284 = ((SUPPORTS_STACK_ALIGNMENT
20285 && (elim == hard_frame_pointer_rtx
20286 || elim == stack_pointer_rtx))
20287 || elim == (frame_pointer_needed
20288 ? hard_frame_pointer_rtx
20289 : stack_pointer_rtx));
20292 /* Generate a DW_AT_name attribute given some string value to be included as
20293 the value of the attribute. */
20295 static void
20296 add_name_attribute (dw_die_ref die, const char *name_string)
20298 if (name_string != NULL && *name_string != 0)
20300 if (demangle_name_func)
20301 name_string = (*demangle_name_func) (name_string);
20303 add_AT_string (die, DW_AT_name, name_string);
20307 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20308 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20309 of TYPE accordingly.
20311 ??? This is a temporary measure until after we're able to generate
20312 regular DWARF for the complex Ada type system. */
20314 static void
20315 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20316 dw_die_ref context_die)
20318 tree dtype;
20319 dw_die_ref dtype_die;
20321 if (!lang_hooks.types.descriptive_type)
20322 return;
20324 dtype = lang_hooks.types.descriptive_type (type);
20325 if (!dtype)
20326 return;
20328 dtype_die = lookup_type_die (dtype);
20329 if (!dtype_die)
20331 gen_type_die (dtype, context_die);
20332 dtype_die = lookup_type_die (dtype);
20333 gcc_assert (dtype_die);
20336 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20339 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20341 static const char *
20342 comp_dir_string (void)
20344 const char *wd;
20345 char *wd1;
20346 static const char *cached_wd = NULL;
20348 if (cached_wd != NULL)
20349 return cached_wd;
20351 wd = get_src_pwd ();
20352 if (wd == NULL)
20353 return NULL;
20355 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20357 int wdlen;
20359 wdlen = strlen (wd);
20360 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20361 strcpy (wd1, wd);
20362 wd1 [wdlen] = DIR_SEPARATOR;
20363 wd1 [wdlen + 1] = 0;
20364 wd = wd1;
20367 cached_wd = remap_debug_filename (wd);
20368 return cached_wd;
20371 /* Generate a DW_AT_comp_dir attribute for DIE. */
20373 static void
20374 add_comp_dir_attribute (dw_die_ref die)
20376 const char * wd = comp_dir_string ();
20377 if (wd != NULL)
20378 add_AT_string (die, DW_AT_comp_dir, wd);
20381 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20382 pointer computation, ...), output a representation for that bound according
20383 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20384 loc_list_from_tree for the meaning of CONTEXT. */
20386 static void
20387 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20388 int forms, struct loc_descr_context *context)
20390 dw_die_ref context_die, decl_die;
20391 dw_loc_list_ref list;
20392 bool strip_conversions = true;
20393 bool placeholder_seen = false;
20395 while (strip_conversions)
20396 switch (TREE_CODE (value))
20398 case ERROR_MARK:
20399 case SAVE_EXPR:
20400 return;
20402 CASE_CONVERT:
20403 case VIEW_CONVERT_EXPR:
20404 value = TREE_OPERAND (value, 0);
20405 break;
20407 default:
20408 strip_conversions = false;
20409 break;
20412 /* If possible and permitted, output the attribute as a constant. */
20413 if ((forms & dw_scalar_form_constant) != 0
20414 && TREE_CODE (value) == INTEGER_CST)
20416 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20418 /* If HOST_WIDE_INT is big enough then represent the bound as
20419 a constant value. We need to choose a form based on
20420 whether the type is signed or unsigned. We cannot just
20421 call add_AT_unsigned if the value itself is positive
20422 (add_AT_unsigned might add the unsigned value encoded as
20423 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20424 bounds type and then sign extend any unsigned values found
20425 for signed types. This is needed only for
20426 DW_AT_{lower,upper}_bound, since for most other attributes,
20427 consumers will treat DW_FORM_data[1248] as unsigned values,
20428 regardless of the underlying type. */
20429 if (prec <= HOST_BITS_PER_WIDE_INT
20430 || tree_fits_uhwi_p (value))
20432 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20433 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20434 else
20435 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20437 else
20438 /* Otherwise represent the bound as an unsigned value with
20439 the precision of its type. The precision and signedness
20440 of the type will be necessary to re-interpret it
20441 unambiguously. */
20442 add_AT_wide (die, attr, wi::to_wide (value));
20443 return;
20446 /* Otherwise, if it's possible and permitted too, output a reference to
20447 another DIE. */
20448 if ((forms & dw_scalar_form_reference) != 0)
20450 tree decl = NULL_TREE;
20452 /* Some type attributes reference an outer type. For instance, the upper
20453 bound of an array may reference an embedding record (this happens in
20454 Ada). */
20455 if (TREE_CODE (value) == COMPONENT_REF
20456 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20457 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20458 decl = TREE_OPERAND (value, 1);
20460 else if (VAR_P (value)
20461 || TREE_CODE (value) == PARM_DECL
20462 || TREE_CODE (value) == RESULT_DECL)
20463 decl = value;
20465 if (decl != NULL_TREE)
20467 dw_die_ref decl_die = lookup_decl_die (decl);
20469 /* ??? Can this happen, or should the variable have been bound
20470 first? Probably it can, since I imagine that we try to create
20471 the types of parameters in the order in which they exist in
20472 the list, and won't have created a forward reference to a
20473 later parameter. */
20474 if (decl_die != NULL)
20476 add_AT_die_ref (die, attr, decl_die);
20477 return;
20482 /* Last chance: try to create a stack operation procedure to evaluate the
20483 value. Do nothing if even that is not possible or permitted. */
20484 if ((forms & dw_scalar_form_exprloc) == 0)
20485 return;
20487 list = loc_list_from_tree (value, 2, context);
20488 if (context && context->placeholder_arg)
20490 placeholder_seen = context->placeholder_seen;
20491 context->placeholder_seen = false;
20493 if (list == NULL || single_element_loc_list_p (list))
20495 /* If this attribute is not a reference nor constant, it is
20496 a DWARF expression rather than location description. For that
20497 loc_list_from_tree (value, 0, &context) is needed. */
20498 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20499 if (list2 && single_element_loc_list_p (list2))
20501 if (placeholder_seen)
20503 struct dwarf_procedure_info dpi;
20504 dpi.fndecl = NULL_TREE;
20505 dpi.args_count = 1;
20506 if (!resolve_args_picking (list2->expr, 1, &dpi))
20507 return;
20509 add_AT_loc (die, attr, list2->expr);
20510 return;
20514 /* If that failed to give a single element location list, fall back to
20515 outputting this as a reference... still if permitted. */
20516 if (list == NULL
20517 || (forms & dw_scalar_form_reference) == 0
20518 || placeholder_seen)
20519 return;
20521 if (current_function_decl == 0)
20522 context_die = comp_unit_die ();
20523 else
20524 context_die = lookup_decl_die (current_function_decl);
20526 decl_die = new_die (DW_TAG_variable, context_die, value);
20527 add_AT_flag (decl_die, DW_AT_artificial, 1);
20528 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20529 context_die);
20530 add_AT_location_description (decl_die, DW_AT_location, list);
20531 add_AT_die_ref (die, attr, decl_die);
20534 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20535 default. */
20537 static int
20538 lower_bound_default (void)
20540 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20542 case DW_LANG_C:
20543 case DW_LANG_C89:
20544 case DW_LANG_C99:
20545 case DW_LANG_C11:
20546 case DW_LANG_C_plus_plus:
20547 case DW_LANG_C_plus_plus_11:
20548 case DW_LANG_C_plus_plus_14:
20549 case DW_LANG_ObjC:
20550 case DW_LANG_ObjC_plus_plus:
20551 return 0;
20552 case DW_LANG_Fortran77:
20553 case DW_LANG_Fortran90:
20554 case DW_LANG_Fortran95:
20555 case DW_LANG_Fortran03:
20556 case DW_LANG_Fortran08:
20557 return 1;
20558 case DW_LANG_UPC:
20559 case DW_LANG_D:
20560 case DW_LANG_Python:
20561 return dwarf_version >= 4 ? 0 : -1;
20562 case DW_LANG_Ada95:
20563 case DW_LANG_Ada83:
20564 case DW_LANG_Cobol74:
20565 case DW_LANG_Cobol85:
20566 case DW_LANG_Modula2:
20567 case DW_LANG_PLI:
20568 return dwarf_version >= 4 ? 1 : -1;
20569 default:
20570 return -1;
20574 /* Given a tree node describing an array bound (either lower or upper) output
20575 a representation for that bound. */
20577 static void
20578 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20579 tree bound, struct loc_descr_context *context)
20581 int dflt;
20583 while (1)
20584 switch (TREE_CODE (bound))
20586 /* Strip all conversions. */
20587 CASE_CONVERT:
20588 case VIEW_CONVERT_EXPR:
20589 bound = TREE_OPERAND (bound, 0);
20590 break;
20592 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20593 are even omitted when they are the default. */
20594 case INTEGER_CST:
20595 /* If the value for this bound is the default one, we can even omit the
20596 attribute. */
20597 if (bound_attr == DW_AT_lower_bound
20598 && tree_fits_shwi_p (bound)
20599 && (dflt = lower_bound_default ()) != -1
20600 && tree_to_shwi (bound) == dflt)
20601 return;
20603 /* FALLTHRU */
20605 default:
20606 /* Because of the complex interaction there can be with other GNAT
20607 encodings, GDB isn't ready yet to handle proper DWARF description
20608 for self-referencial subrange bounds: let GNAT encodings do the
20609 magic in such a case. */
20610 if (is_ada ()
20611 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20612 && contains_placeholder_p (bound))
20613 return;
20615 add_scalar_info (subrange_die, bound_attr, bound,
20616 dw_scalar_form_constant
20617 | dw_scalar_form_exprloc
20618 | dw_scalar_form_reference,
20619 context);
20620 return;
20624 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20625 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20626 Note that the block of subscript information for an array type also
20627 includes information about the element type of the given array type.
20629 This function reuses previously set type and bound information if
20630 available. */
20632 static void
20633 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20635 unsigned dimension_number;
20636 tree lower, upper;
20637 dw_die_ref child = type_die->die_child;
20639 for (dimension_number = 0;
20640 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20641 type = TREE_TYPE (type), dimension_number++)
20643 tree domain = TYPE_DOMAIN (type);
20645 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20646 break;
20648 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20649 and (in GNU C only) variable bounds. Handle all three forms
20650 here. */
20652 /* Find and reuse a previously generated DW_TAG_subrange_type if
20653 available.
20655 For multi-dimensional arrays, as we iterate through the
20656 various dimensions in the enclosing for loop above, we also
20657 iterate through the DIE children and pick at each
20658 DW_TAG_subrange_type previously generated (if available).
20659 Each child DW_TAG_subrange_type DIE describes the range of
20660 the current dimension. At this point we should have as many
20661 DW_TAG_subrange_type's as we have dimensions in the
20662 array. */
20663 dw_die_ref subrange_die = NULL;
20664 if (child)
20665 while (1)
20667 child = child->die_sib;
20668 if (child->die_tag == DW_TAG_subrange_type)
20669 subrange_die = child;
20670 if (child == type_die->die_child)
20672 /* If we wrapped around, stop looking next time. */
20673 child = NULL;
20674 break;
20676 if (child->die_tag == DW_TAG_subrange_type)
20677 break;
20679 if (!subrange_die)
20680 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20682 if (domain)
20684 /* We have an array type with specified bounds. */
20685 lower = TYPE_MIN_VALUE (domain);
20686 upper = TYPE_MAX_VALUE (domain);
20688 /* Define the index type. */
20689 if (TREE_TYPE (domain)
20690 && !get_AT (subrange_die, DW_AT_type))
20692 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20693 TREE_TYPE field. We can't emit debug info for this
20694 because it is an unnamed integral type. */
20695 if (TREE_CODE (domain) == INTEGER_TYPE
20696 && TYPE_NAME (domain) == NULL_TREE
20697 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20698 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20700 else
20701 add_type_attribute (subrange_die, TREE_TYPE (domain),
20702 TYPE_UNQUALIFIED, false, type_die);
20705 /* ??? If upper is NULL, the array has unspecified length,
20706 but it does have a lower bound. This happens with Fortran
20707 dimension arr(N:*)
20708 Since the debugger is definitely going to need to know N
20709 to produce useful results, go ahead and output the lower
20710 bound solo, and hope the debugger can cope. */
20712 if (!get_AT (subrange_die, DW_AT_lower_bound))
20713 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20714 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20715 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20718 /* Otherwise we have an array type with an unspecified length. The
20719 DWARF-2 spec does not say how to handle this; let's just leave out the
20720 bounds. */
20724 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20726 static void
20727 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20729 dw_die_ref decl_die;
20730 HOST_WIDE_INT size;
20731 dw_loc_descr_ref size_expr = NULL;
20733 switch (TREE_CODE (tree_node))
20735 case ERROR_MARK:
20736 size = 0;
20737 break;
20738 case ENUMERAL_TYPE:
20739 case RECORD_TYPE:
20740 case UNION_TYPE:
20741 case QUAL_UNION_TYPE:
20742 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20743 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20745 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20746 return;
20748 size_expr = type_byte_size (tree_node, &size);
20749 break;
20750 case FIELD_DECL:
20751 /* For a data member of a struct or union, the DW_AT_byte_size is
20752 generally given as the number of bytes normally allocated for an
20753 object of the *declared* type of the member itself. This is true
20754 even for bit-fields. */
20755 size = int_size_in_bytes (field_type (tree_node));
20756 break;
20757 default:
20758 gcc_unreachable ();
20761 /* Support for dynamically-sized objects was introduced by DWARFv3.
20762 At the moment, GDB does not handle variable byte sizes very well,
20763 though. */
20764 if ((dwarf_version >= 3 || !dwarf_strict)
20765 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20766 && size_expr != NULL)
20767 add_AT_loc (die, DW_AT_byte_size, size_expr);
20769 /* Note that `size' might be -1 when we get to this point. If it is, that
20770 indicates that the byte size of the entity in question is variable and
20771 that we could not generate a DWARF expression that computes it. */
20772 if (size >= 0)
20773 add_AT_unsigned (die, DW_AT_byte_size, size);
20776 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20777 alignment. */
20779 static void
20780 add_alignment_attribute (dw_die_ref die, tree tree_node)
20782 if (dwarf_version < 5 && dwarf_strict)
20783 return;
20785 unsigned align;
20787 if (DECL_P (tree_node))
20789 if (!DECL_USER_ALIGN (tree_node))
20790 return;
20792 align = DECL_ALIGN_UNIT (tree_node);
20794 else if (TYPE_P (tree_node))
20796 if (!TYPE_USER_ALIGN (tree_node))
20797 return;
20799 align = TYPE_ALIGN_UNIT (tree_node);
20801 else
20802 gcc_unreachable ();
20804 add_AT_unsigned (die, DW_AT_alignment, align);
20807 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20808 which specifies the distance in bits from the highest order bit of the
20809 "containing object" for the bit-field to the highest order bit of the
20810 bit-field itself.
20812 For any given bit-field, the "containing object" is a hypothetical object
20813 (of some integral or enum type) within which the given bit-field lives. The
20814 type of this hypothetical "containing object" is always the same as the
20815 declared type of the individual bit-field itself. The determination of the
20816 exact location of the "containing object" for a bit-field is rather
20817 complicated. It's handled by the `field_byte_offset' function (above).
20819 CTX is required: see the comment for VLR_CONTEXT.
20821 Note that it is the size (in bytes) of the hypothetical "containing object"
20822 which will be given in the DW_AT_byte_size attribute for this bit-field.
20823 (See `byte_size_attribute' above). */
20825 static inline void
20826 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20828 HOST_WIDE_INT object_offset_in_bytes;
20829 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20830 HOST_WIDE_INT bitpos_int;
20831 HOST_WIDE_INT highest_order_object_bit_offset;
20832 HOST_WIDE_INT highest_order_field_bit_offset;
20833 HOST_WIDE_INT bit_offset;
20835 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20837 /* Must be a field and a bit field. */
20838 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20840 /* We can't yet handle bit-fields whose offsets are variable, so if we
20841 encounter such things, just return without generating any attribute
20842 whatsoever. Likewise for variable or too large size. */
20843 if (! tree_fits_shwi_p (bit_position (decl))
20844 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20845 return;
20847 bitpos_int = int_bit_position (decl);
20849 /* Note that the bit offset is always the distance (in bits) from the
20850 highest-order bit of the "containing object" to the highest-order bit of
20851 the bit-field itself. Since the "high-order end" of any object or field
20852 is different on big-endian and little-endian machines, the computation
20853 below must take account of these differences. */
20854 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20855 highest_order_field_bit_offset = bitpos_int;
20857 if (! BYTES_BIG_ENDIAN)
20859 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20860 highest_order_object_bit_offset +=
20861 simple_type_size_in_bits (original_type);
20864 bit_offset
20865 = (! BYTES_BIG_ENDIAN
20866 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20867 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20869 if (bit_offset < 0)
20870 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20871 else
20872 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20875 /* For a FIELD_DECL node which represents a bit field, output an attribute
20876 which specifies the length in bits of the given field. */
20878 static inline void
20879 add_bit_size_attribute (dw_die_ref die, tree decl)
20881 /* Must be a field and a bit field. */
20882 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20883 && DECL_BIT_FIELD_TYPE (decl));
20885 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20886 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20889 /* If the compiled language is ANSI C, then add a 'prototyped'
20890 attribute, if arg types are given for the parameters of a function. */
20892 static inline void
20893 add_prototyped_attribute (dw_die_ref die, tree func_type)
20895 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20897 case DW_LANG_C:
20898 case DW_LANG_C89:
20899 case DW_LANG_C99:
20900 case DW_LANG_C11:
20901 case DW_LANG_ObjC:
20902 if (prototype_p (func_type))
20903 add_AT_flag (die, DW_AT_prototyped, 1);
20904 break;
20905 default:
20906 break;
20910 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20911 by looking in the type declaration, the object declaration equate table or
20912 the block mapping. */
20914 static inline dw_die_ref
20915 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20917 dw_die_ref origin_die = NULL;
20919 if (DECL_P (origin))
20921 dw_die_ref c;
20922 origin_die = lookup_decl_die (origin);
20923 /* "Unwrap" the decls DIE which we put in the imported unit context.
20924 We are looking for the abstract copy here. */
20925 if (in_lto_p
20926 && origin_die
20927 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20928 /* ??? Identify this better. */
20929 && c->with_offset)
20930 origin_die = c;
20932 else if (TYPE_P (origin))
20933 origin_die = lookup_type_die (origin);
20934 else if (TREE_CODE (origin) == BLOCK)
20935 origin_die = BLOCK_DIE (origin);
20937 /* XXX: Functions that are never lowered don't always have correct block
20938 trees (in the case of java, they simply have no block tree, in some other
20939 languages). For these functions, there is nothing we can really do to
20940 output correct debug info for inlined functions in all cases. Rather
20941 than die, we'll just produce deficient debug info now, in that we will
20942 have variables without a proper abstract origin. In the future, when all
20943 functions are lowered, we should re-add a gcc_assert (origin_die)
20944 here. */
20946 if (origin_die)
20947 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20948 return origin_die;
20951 /* We do not currently support the pure_virtual attribute. */
20953 static inline void
20954 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20956 if (DECL_VINDEX (func_decl))
20958 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20960 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20961 add_AT_loc (die, DW_AT_vtable_elem_location,
20962 new_loc_descr (DW_OP_constu,
20963 tree_to_shwi (DECL_VINDEX (func_decl)),
20964 0));
20966 /* GNU extension: Record what type this method came from originally. */
20967 if (debug_info_level > DINFO_LEVEL_TERSE
20968 && DECL_CONTEXT (func_decl))
20969 add_AT_die_ref (die, DW_AT_containing_type,
20970 lookup_type_die (DECL_CONTEXT (func_decl)));
20974 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20975 given decl. This used to be a vendor extension until after DWARF 4
20976 standardized it. */
20978 static void
20979 add_linkage_attr (dw_die_ref die, tree decl)
20981 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20983 /* Mimic what assemble_name_raw does with a leading '*'. */
20984 if (name[0] == '*')
20985 name = &name[1];
20987 if (dwarf_version >= 4)
20988 add_AT_string (die, DW_AT_linkage_name, name);
20989 else
20990 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20993 /* Add source coordinate attributes for the given decl. */
20995 static void
20996 add_src_coords_attributes (dw_die_ref die, tree decl)
20998 expanded_location s;
21000 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21001 return;
21002 s = expand_location (DECL_SOURCE_LOCATION (decl));
21003 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21004 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21005 if (debug_column_info && s.column)
21006 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21009 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21011 static void
21012 add_linkage_name_raw (dw_die_ref die, tree decl)
21014 /* Defer until we have an assembler name set. */
21015 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21017 limbo_die_node *asm_name;
21019 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21020 asm_name->die = die;
21021 asm_name->created_for = decl;
21022 asm_name->next = deferred_asm_name;
21023 deferred_asm_name = asm_name;
21025 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21026 add_linkage_attr (die, decl);
21029 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21031 static void
21032 add_linkage_name (dw_die_ref die, tree decl)
21034 if (debug_info_level > DINFO_LEVEL_NONE
21035 && VAR_OR_FUNCTION_DECL_P (decl)
21036 && TREE_PUBLIC (decl)
21037 && !(VAR_P (decl) && DECL_REGISTER (decl))
21038 && die->die_tag != DW_TAG_member)
21039 add_linkage_name_raw (die, decl);
21042 /* Add a DW_AT_name attribute and source coordinate attribute for the
21043 given decl, but only if it actually has a name. */
21045 static void
21046 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21047 bool no_linkage_name)
21049 tree decl_name;
21051 decl_name = DECL_NAME (decl);
21052 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21054 const char *name = dwarf2_name (decl, 0);
21055 if (name)
21056 add_name_attribute (die, name);
21057 if (! DECL_ARTIFICIAL (decl))
21058 add_src_coords_attributes (die, decl);
21060 if (!no_linkage_name)
21061 add_linkage_name (die, decl);
21064 #ifdef VMS_DEBUGGING_INFO
21065 /* Get the function's name, as described by its RTL. This may be different
21066 from the DECL_NAME name used in the source file. */
21067 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21069 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21070 XEXP (DECL_RTL (decl), 0), false);
21071 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21073 #endif /* VMS_DEBUGGING_INFO */
21076 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21078 static void
21079 add_discr_value (dw_die_ref die, dw_discr_value *value)
21081 dw_attr_node attr;
21083 attr.dw_attr = DW_AT_discr_value;
21084 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21085 attr.dw_attr_val.val_entry = NULL;
21086 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21087 if (value->pos)
21088 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21089 else
21090 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21091 add_dwarf_attr (die, &attr);
21094 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21096 static void
21097 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21099 dw_attr_node attr;
21101 attr.dw_attr = DW_AT_discr_list;
21102 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21103 attr.dw_attr_val.val_entry = NULL;
21104 attr.dw_attr_val.v.val_discr_list = discr_list;
21105 add_dwarf_attr (die, &attr);
21108 static inline dw_discr_list_ref
21109 AT_discr_list (dw_attr_node *attr)
21111 return attr->dw_attr_val.v.val_discr_list;
21114 #ifdef VMS_DEBUGGING_INFO
21115 /* Output the debug main pointer die for VMS */
21117 void
21118 dwarf2out_vms_debug_main_pointer (void)
21120 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21121 dw_die_ref die;
21123 /* Allocate the VMS debug main subprogram die. */
21124 die = new_die_raw (DW_TAG_subprogram);
21125 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21126 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21127 current_function_funcdef_no);
21128 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21130 /* Make it the first child of comp_unit_die (). */
21131 die->die_parent = comp_unit_die ();
21132 if (comp_unit_die ()->die_child)
21134 die->die_sib = comp_unit_die ()->die_child->die_sib;
21135 comp_unit_die ()->die_child->die_sib = die;
21137 else
21139 die->die_sib = die;
21140 comp_unit_die ()->die_child = die;
21143 #endif /* VMS_DEBUGGING_INFO */
21145 /* Push a new declaration scope. */
21147 static void
21148 push_decl_scope (tree scope)
21150 vec_safe_push (decl_scope_table, scope);
21153 /* Pop a declaration scope. */
21155 static inline void
21156 pop_decl_scope (void)
21158 decl_scope_table->pop ();
21161 /* walk_tree helper function for uses_local_type, below. */
21163 static tree
21164 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21166 if (!TYPE_P (*tp))
21167 *walk_subtrees = 0;
21168 else
21170 tree name = TYPE_NAME (*tp);
21171 if (name && DECL_P (name) && decl_function_context (name))
21172 return *tp;
21174 return NULL_TREE;
21177 /* If TYPE involves a function-local type (including a local typedef to a
21178 non-local type), returns that type; otherwise returns NULL_TREE. */
21180 static tree
21181 uses_local_type (tree type)
21183 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21184 return used;
21187 /* Return the DIE for the scope that immediately contains this type.
21188 Non-named types that do not involve a function-local type get global
21189 scope. Named types nested in namespaces or other types get their
21190 containing scope. All other types (i.e. function-local named types) get
21191 the current active scope. */
21193 static dw_die_ref
21194 scope_die_for (tree t, dw_die_ref context_die)
21196 dw_die_ref scope_die = NULL;
21197 tree containing_scope;
21199 /* Non-types always go in the current scope. */
21200 gcc_assert (TYPE_P (t));
21202 /* Use the scope of the typedef, rather than the scope of the type
21203 it refers to. */
21204 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21205 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21206 else
21207 containing_scope = TYPE_CONTEXT (t);
21209 /* Use the containing namespace if there is one. */
21210 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21212 if (context_die == lookup_decl_die (containing_scope))
21213 /* OK */;
21214 else if (debug_info_level > DINFO_LEVEL_TERSE)
21215 context_die = get_context_die (containing_scope);
21216 else
21217 containing_scope = NULL_TREE;
21220 /* Ignore function type "scopes" from the C frontend. They mean that
21221 a tagged type is local to a parmlist of a function declarator, but
21222 that isn't useful to DWARF. */
21223 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21224 containing_scope = NULL_TREE;
21226 if (SCOPE_FILE_SCOPE_P (containing_scope))
21228 /* If T uses a local type keep it local as well, to avoid references
21229 to function-local DIEs from outside the function. */
21230 if (current_function_decl && uses_local_type (t))
21231 scope_die = context_die;
21232 else
21233 scope_die = comp_unit_die ();
21235 else if (TYPE_P (containing_scope))
21237 /* For types, we can just look up the appropriate DIE. */
21238 if (debug_info_level > DINFO_LEVEL_TERSE)
21239 scope_die = get_context_die (containing_scope);
21240 else
21242 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21243 if (scope_die == NULL)
21244 scope_die = comp_unit_die ();
21247 else
21248 scope_die = context_die;
21250 return scope_die;
21253 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21255 static inline int
21256 local_scope_p (dw_die_ref context_die)
21258 for (; context_die; context_die = context_die->die_parent)
21259 if (context_die->die_tag == DW_TAG_inlined_subroutine
21260 || context_die->die_tag == DW_TAG_subprogram)
21261 return 1;
21263 return 0;
21266 /* Returns nonzero if CONTEXT_DIE is a class. */
21268 static inline int
21269 class_scope_p (dw_die_ref context_die)
21271 return (context_die
21272 && (context_die->die_tag == DW_TAG_structure_type
21273 || context_die->die_tag == DW_TAG_class_type
21274 || context_die->die_tag == DW_TAG_interface_type
21275 || context_die->die_tag == DW_TAG_union_type));
21278 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21279 whether or not to treat a DIE in this context as a declaration. */
21281 static inline int
21282 class_or_namespace_scope_p (dw_die_ref context_die)
21284 return (class_scope_p (context_die)
21285 || (context_die && context_die->die_tag == DW_TAG_namespace));
21288 /* Many forms of DIEs require a "type description" attribute. This
21289 routine locates the proper "type descriptor" die for the type given
21290 by 'type' plus any additional qualifiers given by 'cv_quals', and
21291 adds a DW_AT_type attribute below the given die. */
21293 static void
21294 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21295 bool reverse, dw_die_ref context_die)
21297 enum tree_code code = TREE_CODE (type);
21298 dw_die_ref type_die = NULL;
21300 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21301 or fixed-point type, use the inner type. This is because we have no
21302 support for unnamed types in base_type_die. This can happen if this is
21303 an Ada subrange type. Correct solution is emit a subrange type die. */
21304 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21305 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21306 type = TREE_TYPE (type), code = TREE_CODE (type);
21308 if (code == ERROR_MARK
21309 /* Handle a special case. For functions whose return type is void, we
21310 generate *no* type attribute. (Note that no object may have type
21311 `void', so this only applies to function return types). */
21312 || code == VOID_TYPE)
21313 return;
21315 type_die = modified_type_die (type,
21316 cv_quals | TYPE_QUALS (type),
21317 reverse,
21318 context_die);
21320 if (type_die != NULL)
21321 add_AT_die_ref (object_die, DW_AT_type, type_die);
21324 /* Given an object die, add the calling convention attribute for the
21325 function call type. */
21326 static void
21327 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21329 enum dwarf_calling_convention value = DW_CC_normal;
21331 value = ((enum dwarf_calling_convention)
21332 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21334 if (is_fortran ()
21335 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21337 /* DWARF 2 doesn't provide a way to identify a program's source-level
21338 entry point. DW_AT_calling_convention attributes are only meant
21339 to describe functions' calling conventions. However, lacking a
21340 better way to signal the Fortran main program, we used this for
21341 a long time, following existing custom. Now, DWARF 4 has
21342 DW_AT_main_subprogram, which we add below, but some tools still
21343 rely on the old way, which we thus keep. */
21344 value = DW_CC_program;
21346 if (dwarf_version >= 4 || !dwarf_strict)
21347 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21350 /* Only add the attribute if the backend requests it, and
21351 is not DW_CC_normal. */
21352 if (value && (value != DW_CC_normal))
21353 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21356 /* Given a tree pointer to a struct, class, union, or enum type node, return
21357 a pointer to the (string) tag name for the given type, or zero if the type
21358 was declared without a tag. */
21360 static const char *
21361 type_tag (const_tree type)
21363 const char *name = 0;
21365 if (TYPE_NAME (type) != 0)
21367 tree t = 0;
21369 /* Find the IDENTIFIER_NODE for the type name. */
21370 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21371 && !TYPE_NAMELESS (type))
21372 t = TYPE_NAME (type);
21374 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21375 a TYPE_DECL node, regardless of whether or not a `typedef' was
21376 involved. */
21377 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21378 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21380 /* We want to be extra verbose. Don't call dwarf_name if
21381 DECL_NAME isn't set. The default hook for decl_printable_name
21382 doesn't like that, and in this context it's correct to return
21383 0, instead of "<anonymous>" or the like. */
21384 if (DECL_NAME (TYPE_NAME (type))
21385 && !DECL_NAMELESS (TYPE_NAME (type)))
21386 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21389 /* Now get the name as a string, or invent one. */
21390 if (!name && t != 0)
21391 name = IDENTIFIER_POINTER (t);
21394 return (name == 0 || *name == '\0') ? 0 : name;
21397 /* Return the type associated with a data member, make a special check
21398 for bit field types. */
21400 static inline tree
21401 member_declared_type (const_tree member)
21403 return (DECL_BIT_FIELD_TYPE (member)
21404 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21407 /* Get the decl's label, as described by its RTL. This may be different
21408 from the DECL_NAME name used in the source file. */
21410 #if 0
21411 static const char *
21412 decl_start_label (tree decl)
21414 rtx x;
21415 const char *fnname;
21417 x = DECL_RTL (decl);
21418 gcc_assert (MEM_P (x));
21420 x = XEXP (x, 0);
21421 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21423 fnname = XSTR (x, 0);
21424 return fnname;
21426 #endif
21428 /* For variable-length arrays that have been previously generated, but
21429 may be incomplete due to missing subscript info, fill the subscript
21430 info. Return TRUE if this is one of those cases. */
21431 static bool
21432 fill_variable_array_bounds (tree type)
21434 if (TREE_ASM_WRITTEN (type)
21435 && TREE_CODE (type) == ARRAY_TYPE
21436 && variably_modified_type_p (type, NULL))
21438 dw_die_ref array_die = lookup_type_die (type);
21439 if (!array_die)
21440 return false;
21441 add_subscript_info (array_die, type, !is_ada ());
21442 return true;
21444 return false;
21447 /* These routines generate the internal representation of the DIE's for
21448 the compilation unit. Debugging information is collected by walking
21449 the declaration trees passed in from dwarf2out_decl(). */
21451 static void
21452 gen_array_type_die (tree type, dw_die_ref context_die)
21454 dw_die_ref array_die;
21456 /* GNU compilers represent multidimensional array types as sequences of one
21457 dimensional array types whose element types are themselves array types.
21458 We sometimes squish that down to a single array_type DIE with multiple
21459 subscripts in the Dwarf debugging info. The draft Dwarf specification
21460 say that we are allowed to do this kind of compression in C, because
21461 there is no difference between an array of arrays and a multidimensional
21462 array. We don't do this for Ada to remain as close as possible to the
21463 actual representation, which is especially important against the language
21464 flexibilty wrt arrays of variable size. */
21466 bool collapse_nested_arrays = !is_ada ();
21468 if (fill_variable_array_bounds (type))
21469 return;
21471 dw_die_ref scope_die = scope_die_for (type, context_die);
21472 tree element_type;
21474 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21475 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21476 if (TYPE_STRING_FLAG (type)
21477 && TREE_CODE (type) == ARRAY_TYPE
21478 && is_fortran ()
21479 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21481 HOST_WIDE_INT size;
21483 array_die = new_die (DW_TAG_string_type, scope_die, type);
21484 add_name_attribute (array_die, type_tag (type));
21485 equate_type_number_to_die (type, array_die);
21486 size = int_size_in_bytes (type);
21487 if (size >= 0)
21488 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21489 /* ??? We can't annotate types late, but for LTO we may not
21490 generate a location early either (gfortran.dg/save_6.f90). */
21491 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21492 && TYPE_DOMAIN (type) != NULL_TREE
21493 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21495 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21496 tree rszdecl = szdecl;
21498 size = int_size_in_bytes (TREE_TYPE (szdecl));
21499 if (!DECL_P (szdecl))
21501 if (TREE_CODE (szdecl) == INDIRECT_REF
21502 && DECL_P (TREE_OPERAND (szdecl, 0)))
21504 rszdecl = TREE_OPERAND (szdecl, 0);
21505 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21506 != DWARF2_ADDR_SIZE)
21507 size = 0;
21509 else
21510 size = 0;
21512 if (size > 0)
21514 dw_loc_list_ref loc
21515 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21516 NULL);
21517 if (loc)
21519 add_AT_location_description (array_die, DW_AT_string_length,
21520 loc);
21521 if (size != DWARF2_ADDR_SIZE)
21522 add_AT_unsigned (array_die, dwarf_version >= 5
21523 ? DW_AT_string_length_byte_size
21524 : DW_AT_byte_size, size);
21528 return;
21531 array_die = new_die (DW_TAG_array_type, scope_die, type);
21532 add_name_attribute (array_die, type_tag (type));
21533 equate_type_number_to_die (type, array_die);
21535 if (TREE_CODE (type) == VECTOR_TYPE)
21536 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21538 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21539 if (is_fortran ()
21540 && TREE_CODE (type) == ARRAY_TYPE
21541 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21542 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21543 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21545 #if 0
21546 /* We default the array ordering. Debuggers will probably do the right
21547 things even if DW_AT_ordering is not present. It's not even an issue
21548 until we start to get into multidimensional arrays anyway. If a debugger
21549 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21550 then we'll have to put the DW_AT_ordering attribute back in. (But if
21551 and when we find out that we need to put these in, we will only do so
21552 for multidimensional arrays. */
21553 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21554 #endif
21556 if (TREE_CODE (type) == VECTOR_TYPE)
21558 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21559 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21560 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21561 add_bound_info (subrange_die, DW_AT_upper_bound,
21562 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21564 else
21565 add_subscript_info (array_die, type, collapse_nested_arrays);
21567 /* Add representation of the type of the elements of this array type and
21568 emit the corresponding DIE if we haven't done it already. */
21569 element_type = TREE_TYPE (type);
21570 if (collapse_nested_arrays)
21571 while (TREE_CODE (element_type) == ARRAY_TYPE)
21573 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21574 break;
21575 element_type = TREE_TYPE (element_type);
21578 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21579 TREE_CODE (type) == ARRAY_TYPE
21580 && TYPE_REVERSE_STORAGE_ORDER (type),
21581 context_die);
21583 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21584 if (TYPE_ARTIFICIAL (type))
21585 add_AT_flag (array_die, DW_AT_artificial, 1);
21587 if (get_AT (array_die, DW_AT_name))
21588 add_pubtype (type, array_die);
21590 add_alignment_attribute (array_die, type);
21593 /* This routine generates DIE for array with hidden descriptor, details
21594 are filled into *info by a langhook. */
21596 static void
21597 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21598 dw_die_ref context_die)
21600 const dw_die_ref scope_die = scope_die_for (type, context_die);
21601 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21602 struct loc_descr_context context = { type, info->base_decl, NULL,
21603 false, false };
21604 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21605 int dim;
21607 add_name_attribute (array_die, type_tag (type));
21608 equate_type_number_to_die (type, array_die);
21610 if (info->ndimensions > 1)
21611 switch (info->ordering)
21613 case array_descr_ordering_row_major:
21614 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21615 break;
21616 case array_descr_ordering_column_major:
21617 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21618 break;
21619 default:
21620 break;
21623 if (dwarf_version >= 3 || !dwarf_strict)
21625 if (info->data_location)
21626 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21627 dw_scalar_form_exprloc, &context);
21628 if (info->associated)
21629 add_scalar_info (array_die, DW_AT_associated, info->associated,
21630 dw_scalar_form_constant
21631 | dw_scalar_form_exprloc
21632 | dw_scalar_form_reference, &context);
21633 if (info->allocated)
21634 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21635 dw_scalar_form_constant
21636 | dw_scalar_form_exprloc
21637 | dw_scalar_form_reference, &context);
21638 if (info->stride)
21640 const enum dwarf_attribute attr
21641 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21642 const int forms
21643 = (info->stride_in_bits)
21644 ? dw_scalar_form_constant
21645 : (dw_scalar_form_constant
21646 | dw_scalar_form_exprloc
21647 | dw_scalar_form_reference);
21649 add_scalar_info (array_die, attr, info->stride, forms, &context);
21652 if (dwarf_version >= 5)
21654 if (info->rank)
21656 add_scalar_info (array_die, DW_AT_rank, info->rank,
21657 dw_scalar_form_constant
21658 | dw_scalar_form_exprloc, &context);
21659 subrange_tag = DW_TAG_generic_subrange;
21660 context.placeholder_arg = true;
21664 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21666 for (dim = 0; dim < info->ndimensions; dim++)
21668 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21670 if (info->dimen[dim].bounds_type)
21671 add_type_attribute (subrange_die,
21672 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21673 false, context_die);
21674 if (info->dimen[dim].lower_bound)
21675 add_bound_info (subrange_die, DW_AT_lower_bound,
21676 info->dimen[dim].lower_bound, &context);
21677 if (info->dimen[dim].upper_bound)
21678 add_bound_info (subrange_die, DW_AT_upper_bound,
21679 info->dimen[dim].upper_bound, &context);
21680 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21681 add_scalar_info (subrange_die, DW_AT_byte_stride,
21682 info->dimen[dim].stride,
21683 dw_scalar_form_constant
21684 | dw_scalar_form_exprloc
21685 | dw_scalar_form_reference,
21686 &context);
21689 gen_type_die (info->element_type, context_die);
21690 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21691 TREE_CODE (type) == ARRAY_TYPE
21692 && TYPE_REVERSE_STORAGE_ORDER (type),
21693 context_die);
21695 if (get_AT (array_die, DW_AT_name))
21696 add_pubtype (type, array_die);
21698 add_alignment_attribute (array_die, type);
21701 #if 0
21702 static void
21703 gen_entry_point_die (tree decl, dw_die_ref context_die)
21705 tree origin = decl_ultimate_origin (decl);
21706 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21708 if (origin != NULL)
21709 add_abstract_origin_attribute (decl_die, origin);
21710 else
21712 add_name_and_src_coords_attributes (decl_die, decl);
21713 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21714 TYPE_UNQUALIFIED, false, context_die);
21717 if (DECL_ABSTRACT_P (decl))
21718 equate_decl_number_to_die (decl, decl_die);
21719 else
21720 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21722 #endif
21724 /* Walk through the list of incomplete types again, trying once more to
21725 emit full debugging info for them. */
21727 static void
21728 retry_incomplete_types (void)
21730 set_early_dwarf s;
21731 int i;
21733 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21734 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21735 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21736 vec_safe_truncate (incomplete_types, 0);
21739 /* Determine what tag to use for a record type. */
21741 static enum dwarf_tag
21742 record_type_tag (tree type)
21744 if (! lang_hooks.types.classify_record)
21745 return DW_TAG_structure_type;
21747 switch (lang_hooks.types.classify_record (type))
21749 case RECORD_IS_STRUCT:
21750 return DW_TAG_structure_type;
21752 case RECORD_IS_CLASS:
21753 return DW_TAG_class_type;
21755 case RECORD_IS_INTERFACE:
21756 if (dwarf_version >= 3 || !dwarf_strict)
21757 return DW_TAG_interface_type;
21758 return DW_TAG_structure_type;
21760 default:
21761 gcc_unreachable ();
21765 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21766 include all of the information about the enumeration values also. Each
21767 enumerated type name/value is listed as a child of the enumerated type
21768 DIE. */
21770 static dw_die_ref
21771 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21773 dw_die_ref type_die = lookup_type_die (type);
21775 if (type_die == NULL)
21777 type_die = new_die (DW_TAG_enumeration_type,
21778 scope_die_for (type, context_die), type);
21779 equate_type_number_to_die (type, type_die);
21780 add_name_attribute (type_die, type_tag (type));
21781 if (dwarf_version >= 4 || !dwarf_strict)
21783 if (ENUM_IS_SCOPED (type))
21784 add_AT_flag (type_die, DW_AT_enum_class, 1);
21785 if (ENUM_IS_OPAQUE (type))
21786 add_AT_flag (type_die, DW_AT_declaration, 1);
21788 if (!dwarf_strict)
21789 add_AT_unsigned (type_die, DW_AT_encoding,
21790 TYPE_UNSIGNED (type)
21791 ? DW_ATE_unsigned
21792 : DW_ATE_signed);
21794 else if (! TYPE_SIZE (type))
21795 return type_die;
21796 else
21797 remove_AT (type_die, DW_AT_declaration);
21799 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21800 given enum type is incomplete, do not generate the DW_AT_byte_size
21801 attribute or the DW_AT_element_list attribute. */
21802 if (TYPE_SIZE (type))
21804 tree link;
21806 TREE_ASM_WRITTEN (type) = 1;
21807 add_byte_size_attribute (type_die, type);
21808 add_alignment_attribute (type_die, type);
21809 if (dwarf_version >= 3 || !dwarf_strict)
21811 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21812 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21813 context_die);
21815 if (TYPE_STUB_DECL (type) != NULL_TREE)
21817 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21818 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21821 /* If the first reference to this type was as the return type of an
21822 inline function, then it may not have a parent. Fix this now. */
21823 if (type_die->die_parent == NULL)
21824 add_child_die (scope_die_for (type, context_die), type_die);
21826 for (link = TYPE_VALUES (type);
21827 link != NULL; link = TREE_CHAIN (link))
21829 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21830 tree value = TREE_VALUE (link);
21832 add_name_attribute (enum_die,
21833 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21835 if (TREE_CODE (value) == CONST_DECL)
21836 value = DECL_INITIAL (value);
21838 if (simple_type_size_in_bits (TREE_TYPE (value))
21839 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21841 /* For constant forms created by add_AT_unsigned DWARF
21842 consumers (GDB, elfutils, etc.) always zero extend
21843 the value. Only when the actual value is negative
21844 do we need to use add_AT_int to generate a constant
21845 form that can represent negative values. */
21846 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21847 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21848 add_AT_unsigned (enum_die, DW_AT_const_value,
21849 (unsigned HOST_WIDE_INT) val);
21850 else
21851 add_AT_int (enum_die, DW_AT_const_value, val);
21853 else
21854 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21855 that here. TODO: This should be re-worked to use correct
21856 signed/unsigned double tags for all cases. */
21857 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21860 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21861 if (TYPE_ARTIFICIAL (type))
21862 add_AT_flag (type_die, DW_AT_artificial, 1);
21864 else
21865 add_AT_flag (type_die, DW_AT_declaration, 1);
21867 add_pubtype (type, type_die);
21869 return type_die;
21872 /* Generate a DIE to represent either a real live formal parameter decl or to
21873 represent just the type of some formal parameter position in some function
21874 type.
21876 Note that this routine is a bit unusual because its argument may be a
21877 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21878 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21879 node. If it's the former then this function is being called to output a
21880 DIE to represent a formal parameter object (or some inlining thereof). If
21881 it's the latter, then this function is only being called to output a
21882 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21883 argument type of some subprogram type.
21884 If EMIT_NAME_P is true, name and source coordinate attributes
21885 are emitted. */
21887 static dw_die_ref
21888 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21889 dw_die_ref context_die)
21891 tree node_or_origin = node ? node : origin;
21892 tree ultimate_origin;
21893 dw_die_ref parm_die = NULL;
21895 if (DECL_P (node_or_origin))
21897 parm_die = lookup_decl_die (node);
21899 /* If the contexts differ, we may not be talking about the same
21900 thing.
21901 ??? When in LTO the DIE parent is the "abstract" copy and the
21902 context_die is the specification "copy". But this whole block
21903 should eventually be no longer needed. */
21904 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21906 if (!DECL_ABSTRACT_P (node))
21908 /* This can happen when creating an inlined instance, in
21909 which case we need to create a new DIE that will get
21910 annotated with DW_AT_abstract_origin. */
21911 parm_die = NULL;
21913 else
21914 gcc_unreachable ();
21917 if (parm_die && parm_die->die_parent == NULL)
21919 /* Check that parm_die already has the right attributes that
21920 we would have added below. If any attributes are
21921 missing, fall through to add them. */
21922 if (! DECL_ABSTRACT_P (node_or_origin)
21923 && !get_AT (parm_die, DW_AT_location)
21924 && !get_AT (parm_die, DW_AT_const_value))
21925 /* We are missing location info, and are about to add it. */
21927 else
21929 add_child_die (context_die, parm_die);
21930 return parm_die;
21935 /* If we have a previously generated DIE, use it, unless this is an
21936 concrete instance (origin != NULL), in which case we need a new
21937 DIE with a corresponding DW_AT_abstract_origin. */
21938 bool reusing_die;
21939 if (parm_die && origin == NULL)
21940 reusing_die = true;
21941 else
21943 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21944 reusing_die = false;
21947 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21949 case tcc_declaration:
21950 ultimate_origin = decl_ultimate_origin (node_or_origin);
21951 if (node || ultimate_origin)
21952 origin = ultimate_origin;
21954 if (reusing_die)
21955 goto add_location;
21957 if (origin != NULL)
21958 add_abstract_origin_attribute (parm_die, origin);
21959 else if (emit_name_p)
21960 add_name_and_src_coords_attributes (parm_die, node);
21961 if (origin == NULL
21962 || (! DECL_ABSTRACT_P (node_or_origin)
21963 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21964 decl_function_context
21965 (node_or_origin))))
21967 tree type = TREE_TYPE (node_or_origin);
21968 if (decl_by_reference_p (node_or_origin))
21969 add_type_attribute (parm_die, TREE_TYPE (type),
21970 TYPE_UNQUALIFIED,
21971 false, context_die);
21972 else
21973 add_type_attribute (parm_die, type,
21974 decl_quals (node_or_origin),
21975 false, context_die);
21977 if (origin == NULL && DECL_ARTIFICIAL (node))
21978 add_AT_flag (parm_die, DW_AT_artificial, 1);
21979 add_location:
21980 if (node && node != origin)
21981 equate_decl_number_to_die (node, parm_die);
21982 if (! DECL_ABSTRACT_P (node_or_origin))
21983 add_location_or_const_value_attribute (parm_die, node_or_origin,
21984 node == NULL);
21986 break;
21988 case tcc_type:
21989 /* We were called with some kind of a ..._TYPE node. */
21990 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21991 context_die);
21992 break;
21994 default:
21995 gcc_unreachable ();
21998 return parm_die;
22001 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22002 children DW_TAG_formal_parameter DIEs representing the arguments of the
22003 parameter pack.
22005 PARM_PACK must be a function parameter pack.
22006 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22007 must point to the subsequent arguments of the function PACK_ARG belongs to.
22008 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22009 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22010 following the last one for which a DIE was generated. */
22012 static dw_die_ref
22013 gen_formal_parameter_pack_die (tree parm_pack,
22014 tree pack_arg,
22015 dw_die_ref subr_die,
22016 tree *next_arg)
22018 tree arg;
22019 dw_die_ref parm_pack_die;
22021 gcc_assert (parm_pack
22022 && lang_hooks.function_parameter_pack_p (parm_pack)
22023 && subr_die);
22025 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22026 add_src_coords_attributes (parm_pack_die, parm_pack);
22028 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22030 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22031 parm_pack))
22032 break;
22033 gen_formal_parameter_die (arg, NULL,
22034 false /* Don't emit name attribute. */,
22035 parm_pack_die);
22037 if (next_arg)
22038 *next_arg = arg;
22039 return parm_pack_die;
22042 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22043 at the end of an (ANSI prototyped) formal parameters list. */
22045 static void
22046 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22048 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22051 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22052 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22053 parameters as specified in some function type specification (except for
22054 those which appear as part of a function *definition*). */
22056 static void
22057 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22059 tree link;
22060 tree formal_type = NULL;
22061 tree first_parm_type;
22062 tree arg;
22064 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22066 arg = DECL_ARGUMENTS (function_or_method_type);
22067 function_or_method_type = TREE_TYPE (function_or_method_type);
22069 else
22070 arg = NULL_TREE;
22072 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22074 /* Make our first pass over the list of formal parameter types and output a
22075 DW_TAG_formal_parameter DIE for each one. */
22076 for (link = first_parm_type; link; )
22078 dw_die_ref parm_die;
22080 formal_type = TREE_VALUE (link);
22081 if (formal_type == void_type_node)
22082 break;
22084 /* Output a (nameless) DIE to represent the formal parameter itself. */
22085 if (!POINTER_BOUNDS_TYPE_P (formal_type))
22087 parm_die = gen_formal_parameter_die (formal_type, NULL,
22088 true /* Emit name attribute. */,
22089 context_die);
22090 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22091 && link == first_parm_type)
22093 add_AT_flag (parm_die, DW_AT_artificial, 1);
22094 if (dwarf_version >= 3 || !dwarf_strict)
22095 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22097 else if (arg && DECL_ARTIFICIAL (arg))
22098 add_AT_flag (parm_die, DW_AT_artificial, 1);
22101 link = TREE_CHAIN (link);
22102 if (arg)
22103 arg = DECL_CHAIN (arg);
22106 /* If this function type has an ellipsis, add a
22107 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22108 if (formal_type != void_type_node)
22109 gen_unspecified_parameters_die (function_or_method_type, context_die);
22111 /* Make our second (and final) pass over the list of formal parameter types
22112 and output DIEs to represent those types (as necessary). */
22113 for (link = TYPE_ARG_TYPES (function_or_method_type);
22114 link && TREE_VALUE (link);
22115 link = TREE_CHAIN (link))
22116 gen_type_die (TREE_VALUE (link), context_die);
22119 /* We want to generate the DIE for TYPE so that we can generate the
22120 die for MEMBER, which has been defined; we will need to refer back
22121 to the member declaration nested within TYPE. If we're trying to
22122 generate minimal debug info for TYPE, processing TYPE won't do the
22123 trick; we need to attach the member declaration by hand. */
22125 static void
22126 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22128 gen_type_die (type, context_die);
22130 /* If we're trying to avoid duplicate debug info, we may not have
22131 emitted the member decl for this function. Emit it now. */
22132 if (TYPE_STUB_DECL (type)
22133 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22134 && ! lookup_decl_die (member))
22136 dw_die_ref type_die;
22137 gcc_assert (!decl_ultimate_origin (member));
22139 push_decl_scope (type);
22140 type_die = lookup_type_die_strip_naming_typedef (type);
22141 if (TREE_CODE (member) == FUNCTION_DECL)
22142 gen_subprogram_die (member, type_die);
22143 else if (TREE_CODE (member) == FIELD_DECL)
22145 /* Ignore the nameless fields that are used to skip bits but handle
22146 C++ anonymous unions and structs. */
22147 if (DECL_NAME (member) != NULL_TREE
22148 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22149 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22151 struct vlr_context vlr_ctx = {
22152 DECL_CONTEXT (member), /* struct_type */
22153 NULL_TREE /* variant_part_offset */
22155 gen_type_die (member_declared_type (member), type_die);
22156 gen_field_die (member, &vlr_ctx, type_die);
22159 else
22160 gen_variable_die (member, NULL_TREE, type_die);
22162 pop_decl_scope ();
22166 /* Forward declare these functions, because they are mutually recursive
22167 with their set_block_* pairing functions. */
22168 static void set_decl_origin_self (tree);
22170 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22171 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22172 that it points to the node itself, thus indicating that the node is its
22173 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22174 the given node is NULL, recursively descend the decl/block tree which
22175 it is the root of, and for each other ..._DECL or BLOCK node contained
22176 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22177 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22178 values to point to themselves. */
22180 static void
22181 set_block_origin_self (tree stmt)
22183 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22185 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22188 tree local_decl;
22190 for (local_decl = BLOCK_VARS (stmt);
22191 local_decl != NULL_TREE;
22192 local_decl = DECL_CHAIN (local_decl))
22193 /* Do not recurse on nested functions since the inlining status
22194 of parent and child can be different as per the DWARF spec. */
22195 if (TREE_CODE (local_decl) != FUNCTION_DECL
22196 && !DECL_EXTERNAL (local_decl))
22197 set_decl_origin_self (local_decl);
22201 tree subblock;
22203 for (subblock = BLOCK_SUBBLOCKS (stmt);
22204 subblock != NULL_TREE;
22205 subblock = BLOCK_CHAIN (subblock))
22206 set_block_origin_self (subblock); /* Recurse. */
22211 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22212 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22213 node to so that it points to the node itself, thus indicating that the
22214 node represents its own (abstract) origin. Additionally, if the
22215 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22216 the decl/block tree of which the given node is the root of, and for
22217 each other ..._DECL or BLOCK node contained therein whose
22218 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22219 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22220 point to themselves. */
22222 static void
22223 set_decl_origin_self (tree decl)
22225 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22227 DECL_ABSTRACT_ORIGIN (decl) = decl;
22228 if (TREE_CODE (decl) == FUNCTION_DECL)
22230 tree arg;
22232 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22233 DECL_ABSTRACT_ORIGIN (arg) = arg;
22234 if (DECL_INITIAL (decl) != NULL_TREE
22235 && DECL_INITIAL (decl) != error_mark_node)
22236 set_block_origin_self (DECL_INITIAL (decl));
22241 /* Mark the early DIE for DECL as the abstract instance. */
22243 static void
22244 dwarf2out_abstract_function (tree decl)
22246 dw_die_ref old_die;
22248 /* Make sure we have the actual abstract inline, not a clone. */
22249 decl = DECL_ORIGIN (decl);
22251 if (DECL_IGNORED_P (decl))
22252 return;
22254 old_die = lookup_decl_die (decl);
22255 /* With early debug we always have an old DIE unless we are in LTO
22256 and the user did not compile but only link with debug. */
22257 if (in_lto_p && ! old_die)
22258 return;
22259 gcc_assert (old_die != NULL);
22260 if (get_AT (old_die, DW_AT_inline)
22261 || get_AT (old_die, DW_AT_abstract_origin))
22262 /* We've already generated the abstract instance. */
22263 return;
22265 /* Go ahead and put DW_AT_inline on the DIE. */
22266 if (DECL_DECLARED_INLINE_P (decl))
22268 if (cgraph_function_possibly_inlined_p (decl))
22269 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22270 else
22271 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22273 else
22275 if (cgraph_function_possibly_inlined_p (decl))
22276 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22277 else
22278 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22281 if (DECL_DECLARED_INLINE_P (decl)
22282 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22283 add_AT_flag (old_die, DW_AT_artificial, 1);
22285 set_decl_origin_self (decl);
22288 /* Helper function of premark_used_types() which gets called through
22289 htab_traverse.
22291 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22292 marked as unused by prune_unused_types. */
22294 bool
22295 premark_used_types_helper (tree const &type, void *)
22297 dw_die_ref die;
22299 die = lookup_type_die (type);
22300 if (die != NULL)
22301 die->die_perennial_p = 1;
22302 return true;
22305 /* Helper function of premark_types_used_by_global_vars which gets called
22306 through htab_traverse.
22308 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22309 marked as unused by prune_unused_types. The DIE of the type is marked
22310 only if the global variable using the type will actually be emitted. */
22313 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22314 void *)
22316 struct types_used_by_vars_entry *entry;
22317 dw_die_ref die;
22319 entry = (struct types_used_by_vars_entry *) *slot;
22320 gcc_assert (entry->type != NULL
22321 && entry->var_decl != NULL);
22322 die = lookup_type_die (entry->type);
22323 if (die)
22325 /* Ask cgraph if the global variable really is to be emitted.
22326 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22327 varpool_node *node = varpool_node::get (entry->var_decl);
22328 if (node && node->definition)
22330 die->die_perennial_p = 1;
22331 /* Keep the parent DIEs as well. */
22332 while ((die = die->die_parent) && die->die_perennial_p == 0)
22333 die->die_perennial_p = 1;
22336 return 1;
22339 /* Mark all members of used_types_hash as perennial. */
22341 static void
22342 premark_used_types (struct function *fun)
22344 if (fun && fun->used_types_hash)
22345 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22348 /* Mark all members of types_used_by_vars_entry as perennial. */
22350 static void
22351 premark_types_used_by_global_vars (void)
22353 if (types_used_by_vars_hash)
22354 types_used_by_vars_hash
22355 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22358 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22359 for CA_LOC call arg loc node. */
22361 static dw_die_ref
22362 gen_call_site_die (tree decl, dw_die_ref subr_die,
22363 struct call_arg_loc_node *ca_loc)
22365 dw_die_ref stmt_die = NULL, die;
22366 tree block = ca_loc->block;
22368 while (block
22369 && block != DECL_INITIAL (decl)
22370 && TREE_CODE (block) == BLOCK)
22372 stmt_die = BLOCK_DIE (block);
22373 if (stmt_die)
22374 break;
22375 block = BLOCK_SUPERCONTEXT (block);
22377 if (stmt_die == NULL)
22378 stmt_die = subr_die;
22379 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22380 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22381 if (ca_loc->tail_call_p)
22382 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22383 if (ca_loc->symbol_ref)
22385 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22386 if (tdie)
22387 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22388 else
22389 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22390 false);
22392 return die;
22395 /* Generate a DIE to represent a declared function (either file-scope or
22396 block-local). */
22398 static void
22399 gen_subprogram_die (tree decl, dw_die_ref context_die)
22401 tree origin = decl_ultimate_origin (decl);
22402 dw_die_ref subr_die;
22403 dw_die_ref old_die = lookup_decl_die (decl);
22405 /* This function gets called multiple times for different stages of
22406 the debug process. For example, for func() in this code:
22408 namespace S
22410 void func() { ... }
22413 ...we get called 4 times. Twice in early debug and twice in
22414 late debug:
22416 Early debug
22417 -----------
22419 1. Once while generating func() within the namespace. This is
22420 the declaration. The declaration bit below is set, as the
22421 context is the namespace.
22423 A new DIE will be generated with DW_AT_declaration set.
22425 2. Once for func() itself. This is the specification. The
22426 declaration bit below is clear as the context is the CU.
22428 We will use the cached DIE from (1) to create a new DIE with
22429 DW_AT_specification pointing to the declaration in (1).
22431 Late debug via rest_of_handle_final()
22432 -------------------------------------
22434 3. Once generating func() within the namespace. This is also the
22435 declaration, as in (1), but this time we will early exit below
22436 as we have a cached DIE and a declaration needs no additional
22437 annotations (no locations), as the source declaration line
22438 info is enough.
22440 4. Once for func() itself. As in (2), this is the specification,
22441 but this time we will re-use the cached DIE, and just annotate
22442 it with the location information that should now be available.
22444 For something without namespaces, but with abstract instances, we
22445 are also called a multiple times:
22447 class Base
22449 public:
22450 Base (); // constructor declaration (1)
22453 Base::Base () { } // constructor specification (2)
22455 Early debug
22456 -----------
22458 1. Once for the Base() constructor by virtue of it being a
22459 member of the Base class. This is done via
22460 rest_of_type_compilation.
22462 This is a declaration, so a new DIE will be created with
22463 DW_AT_declaration.
22465 2. Once for the Base() constructor definition, but this time
22466 while generating the abstract instance of the base
22467 constructor (__base_ctor) which is being generated via early
22468 debug of reachable functions.
22470 Even though we have a cached version of the declaration (1),
22471 we will create a DW_AT_specification of the declaration DIE
22472 in (1).
22474 3. Once for the __base_ctor itself, but this time, we generate
22475 an DW_AT_abstract_origin version of the DW_AT_specification in
22476 (2).
22478 Late debug via rest_of_handle_final
22479 -----------------------------------
22481 4. One final time for the __base_ctor (which will have a cached
22482 DIE with DW_AT_abstract_origin created in (3). This time,
22483 we will just annotate the location information now
22484 available.
22486 int declaration = (current_function_decl != decl
22487 || class_or_namespace_scope_p (context_die));
22489 /* A declaration that has been previously dumped needs no
22490 additional information. */
22491 if (old_die && declaration)
22492 return;
22494 /* Now that the C++ front end lazily declares artificial member fns, we
22495 might need to retrofit the declaration into its class. */
22496 if (!declaration && !origin && !old_die
22497 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22498 && !class_or_namespace_scope_p (context_die)
22499 && debug_info_level > DINFO_LEVEL_TERSE)
22500 old_die = force_decl_die (decl);
22502 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22503 if (origin != NULL)
22505 gcc_assert (!declaration || local_scope_p (context_die));
22507 /* Fixup die_parent for the abstract instance of a nested
22508 inline function. */
22509 if (old_die && old_die->die_parent == NULL)
22510 add_child_die (context_die, old_die);
22512 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22514 /* If we have a DW_AT_abstract_origin we have a working
22515 cached version. */
22516 subr_die = old_die;
22518 else
22520 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22521 add_abstract_origin_attribute (subr_die, origin);
22522 /* This is where the actual code for a cloned function is.
22523 Let's emit linkage name attribute for it. This helps
22524 debuggers to e.g, set breakpoints into
22525 constructors/destructors when the user asks "break
22526 K::K". */
22527 add_linkage_name (subr_die, decl);
22530 /* A cached copy, possibly from early dwarf generation. Reuse as
22531 much as possible. */
22532 else if (old_die)
22534 if (!get_AT_flag (old_die, DW_AT_declaration)
22535 /* We can have a normal definition following an inline one in the
22536 case of redefinition of GNU C extern inlines.
22537 It seems reasonable to use AT_specification in this case. */
22538 && !get_AT (old_die, DW_AT_inline))
22540 /* Detect and ignore this case, where we are trying to output
22541 something we have already output. */
22542 if (get_AT (old_die, DW_AT_low_pc)
22543 || get_AT (old_die, DW_AT_ranges))
22544 return;
22546 /* If we have no location information, this must be a
22547 partially generated DIE from early dwarf generation.
22548 Fall through and generate it. */
22551 /* If the definition comes from the same place as the declaration,
22552 maybe use the old DIE. We always want the DIE for this function
22553 that has the *_pc attributes to be under comp_unit_die so the
22554 debugger can find it. We also need to do this for abstract
22555 instances of inlines, since the spec requires the out-of-line copy
22556 to have the same parent. For local class methods, this doesn't
22557 apply; we just use the old DIE. */
22558 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22559 struct dwarf_file_data * file_index = lookup_filename (s.file);
22560 if ((is_cu_die (old_die->die_parent)
22561 /* This condition fixes the inconsistency/ICE with the
22562 following Fortran test (or some derivative thereof) while
22563 building libgfortran:
22565 module some_m
22566 contains
22567 logical function funky (FLAG)
22568 funky = .true.
22569 end function
22570 end module
22572 || (old_die->die_parent
22573 && old_die->die_parent->die_tag == DW_TAG_module)
22574 || context_die == NULL)
22575 && (DECL_ARTIFICIAL (decl)
22576 /* The location attributes may be in the abstract origin
22577 which in the case of LTO might be not available to
22578 look at. */
22579 || get_AT (old_die, DW_AT_abstract_origin)
22580 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22581 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22582 == (unsigned) s.line)
22583 && (!debug_column_info
22584 || s.column == 0
22585 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22586 == (unsigned) s.column)))))
22588 subr_die = old_die;
22590 /* Clear out the declaration attribute, but leave the
22591 parameters so they can be augmented with location
22592 information later. Unless this was a declaration, in
22593 which case, wipe out the nameless parameters and recreate
22594 them further down. */
22595 if (remove_AT (subr_die, DW_AT_declaration))
22598 remove_AT (subr_die, DW_AT_object_pointer);
22599 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22602 /* Make a specification pointing to the previously built
22603 declaration. */
22604 else
22606 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22607 add_AT_specification (subr_die, old_die);
22608 add_pubname (decl, subr_die);
22609 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22610 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22611 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22612 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22613 if (debug_column_info
22614 && s.column
22615 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22616 != (unsigned) s.column))
22617 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22619 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22620 emit the real type on the definition die. */
22621 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22623 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22624 if (die == auto_die || die == decltype_auto_die)
22625 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22626 TYPE_UNQUALIFIED, false, context_die);
22629 /* When we process the method declaration, we haven't seen
22630 the out-of-class defaulted definition yet, so we have to
22631 recheck now. */
22632 if ((dwarf_version >= 5 || ! dwarf_strict)
22633 && !get_AT (subr_die, DW_AT_defaulted))
22635 int defaulted
22636 = lang_hooks.decls.decl_dwarf_attribute (decl,
22637 DW_AT_defaulted);
22638 if (defaulted != -1)
22640 /* Other values must have been handled before. */
22641 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22642 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22647 /* Create a fresh DIE for anything else. */
22648 else
22650 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22652 if (TREE_PUBLIC (decl))
22653 add_AT_flag (subr_die, DW_AT_external, 1);
22655 add_name_and_src_coords_attributes (subr_die, decl);
22656 add_pubname (decl, subr_die);
22657 if (debug_info_level > DINFO_LEVEL_TERSE)
22659 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22660 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22661 TYPE_UNQUALIFIED, false, context_die);
22664 add_pure_or_virtual_attribute (subr_die, decl);
22665 if (DECL_ARTIFICIAL (decl))
22666 add_AT_flag (subr_die, DW_AT_artificial, 1);
22668 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22669 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22671 add_alignment_attribute (subr_die, decl);
22673 add_accessibility_attribute (subr_die, decl);
22676 /* Unless we have an existing non-declaration DIE, equate the new
22677 DIE. */
22678 if (!old_die || is_declaration_die (old_die))
22679 equate_decl_number_to_die (decl, subr_die);
22681 if (declaration)
22683 if (!old_die || !get_AT (old_die, DW_AT_inline))
22685 add_AT_flag (subr_die, DW_AT_declaration, 1);
22687 /* If this is an explicit function declaration then generate
22688 a DW_AT_explicit attribute. */
22689 if ((dwarf_version >= 3 || !dwarf_strict)
22690 && lang_hooks.decls.decl_dwarf_attribute (decl,
22691 DW_AT_explicit) == 1)
22692 add_AT_flag (subr_die, DW_AT_explicit, 1);
22694 /* If this is a C++11 deleted special function member then generate
22695 a DW_AT_deleted attribute. */
22696 if ((dwarf_version >= 5 || !dwarf_strict)
22697 && lang_hooks.decls.decl_dwarf_attribute (decl,
22698 DW_AT_deleted) == 1)
22699 add_AT_flag (subr_die, DW_AT_deleted, 1);
22701 /* If this is a C++11 defaulted special function member then
22702 generate a DW_AT_defaulted attribute. */
22703 if (dwarf_version >= 5 || !dwarf_strict)
22705 int defaulted
22706 = lang_hooks.decls.decl_dwarf_attribute (decl,
22707 DW_AT_defaulted);
22708 if (defaulted != -1)
22709 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22712 /* If this is a C++11 non-static member function with & ref-qualifier
22713 then generate a DW_AT_reference attribute. */
22714 if ((dwarf_version >= 5 || !dwarf_strict)
22715 && lang_hooks.decls.decl_dwarf_attribute (decl,
22716 DW_AT_reference) == 1)
22717 add_AT_flag (subr_die, DW_AT_reference, 1);
22719 /* If this is a C++11 non-static member function with &&
22720 ref-qualifier then generate a DW_AT_reference attribute. */
22721 if ((dwarf_version >= 5 || !dwarf_strict)
22722 && lang_hooks.decls.decl_dwarf_attribute (decl,
22723 DW_AT_rvalue_reference)
22724 == 1)
22725 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22728 /* For non DECL_EXTERNALs, if range information is available, fill
22729 the DIE with it. */
22730 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22732 HOST_WIDE_INT cfa_fb_offset;
22734 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22736 if (!crtl->has_bb_partition)
22738 dw_fde_ref fde = fun->fde;
22739 if (fde->dw_fde_begin)
22741 /* We have already generated the labels. */
22742 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22743 fde->dw_fde_end, false);
22745 else
22747 /* Create start/end labels and add the range. */
22748 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22749 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22750 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22751 current_function_funcdef_no);
22752 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22753 current_function_funcdef_no);
22754 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22755 false);
22758 #if VMS_DEBUGGING_INFO
22759 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22760 Section 2.3 Prologue and Epilogue Attributes:
22761 When a breakpoint is set on entry to a function, it is generally
22762 desirable for execution to be suspended, not on the very first
22763 instruction of the function, but rather at a point after the
22764 function's frame has been set up, after any language defined local
22765 declaration processing has been completed, and before execution of
22766 the first statement of the function begins. Debuggers generally
22767 cannot properly determine where this point is. Similarly for a
22768 breakpoint set on exit from a function. The prologue and epilogue
22769 attributes allow a compiler to communicate the location(s) to use. */
22772 if (fde->dw_fde_vms_end_prologue)
22773 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22774 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22776 if (fde->dw_fde_vms_begin_epilogue)
22777 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22778 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22780 #endif
22783 else
22785 /* Generate pubnames entries for the split function code ranges. */
22786 dw_fde_ref fde = fun->fde;
22788 if (fde->dw_fde_second_begin)
22790 if (dwarf_version >= 3 || !dwarf_strict)
22792 /* We should use ranges for non-contiguous code section
22793 addresses. Use the actual code range for the initial
22794 section, since the HOT/COLD labels might precede an
22795 alignment offset. */
22796 bool range_list_added = false;
22797 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22798 fde->dw_fde_end, &range_list_added,
22799 false);
22800 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22801 fde->dw_fde_second_end,
22802 &range_list_added, false);
22803 if (range_list_added)
22804 add_ranges (NULL);
22806 else
22808 /* There is no real support in DW2 for this .. so we make
22809 a work-around. First, emit the pub name for the segment
22810 containing the function label. Then make and emit a
22811 simplified subprogram DIE for the second segment with the
22812 name pre-fixed by __hot/cold_sect_of_. We use the same
22813 linkage name for the second die so that gdb will find both
22814 sections when given "b foo". */
22815 const char *name = NULL;
22816 tree decl_name = DECL_NAME (decl);
22817 dw_die_ref seg_die;
22819 /* Do the 'primary' section. */
22820 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22821 fde->dw_fde_end, false);
22823 /* Build a minimal DIE for the secondary section. */
22824 seg_die = new_die (DW_TAG_subprogram,
22825 subr_die->die_parent, decl);
22827 if (TREE_PUBLIC (decl))
22828 add_AT_flag (seg_die, DW_AT_external, 1);
22830 if (decl_name != NULL
22831 && IDENTIFIER_POINTER (decl_name) != NULL)
22833 name = dwarf2_name (decl, 1);
22834 if (! DECL_ARTIFICIAL (decl))
22835 add_src_coords_attributes (seg_die, decl);
22837 add_linkage_name (seg_die, decl);
22839 gcc_assert (name != NULL);
22840 add_pure_or_virtual_attribute (seg_die, decl);
22841 if (DECL_ARTIFICIAL (decl))
22842 add_AT_flag (seg_die, DW_AT_artificial, 1);
22844 name = concat ("__second_sect_of_", name, NULL);
22845 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22846 fde->dw_fde_second_end, false);
22847 add_name_attribute (seg_die, name);
22848 if (want_pubnames ())
22849 add_pubname_string (name, seg_die);
22852 else
22853 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22854 false);
22857 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22859 /* We define the "frame base" as the function's CFA. This is more
22860 convenient for several reasons: (1) It's stable across the prologue
22861 and epilogue, which makes it better than just a frame pointer,
22862 (2) With dwarf3, there exists a one-byte encoding that allows us
22863 to reference the .debug_frame data by proxy, but failing that,
22864 (3) We can at least reuse the code inspection and interpretation
22865 code that determines the CFA position at various points in the
22866 function. */
22867 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22869 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22870 add_AT_loc (subr_die, DW_AT_frame_base, op);
22872 else
22874 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22875 if (list->dw_loc_next)
22876 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22877 else
22878 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22881 /* Compute a displacement from the "steady-state frame pointer" to
22882 the CFA. The former is what all stack slots and argument slots
22883 will reference in the rtl; the latter is what we've told the
22884 debugger about. We'll need to adjust all frame_base references
22885 by this displacement. */
22886 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22888 if (fun->static_chain_decl)
22890 /* DWARF requires here a location expression that computes the
22891 address of the enclosing subprogram's frame base. The machinery
22892 in tree-nested.c is supposed to store this specific address in the
22893 last field of the FRAME record. */
22894 const tree frame_type
22895 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22896 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22898 tree fb_expr
22899 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22900 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22901 fb_expr, fb_decl, NULL_TREE);
22903 add_AT_location_description (subr_die, DW_AT_static_link,
22904 loc_list_from_tree (fb_expr, 0, NULL));
22907 resolve_variable_values ();
22910 /* Generate child dies for template paramaters. */
22911 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22912 gen_generic_params_dies (decl);
22914 /* Now output descriptions of the arguments for this function. This gets
22915 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22916 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22917 `...' at the end of the formal parameter list. In order to find out if
22918 there was a trailing ellipsis or not, we must instead look at the type
22919 associated with the FUNCTION_DECL. This will be a node of type
22920 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22921 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22922 an ellipsis at the end. */
22924 /* In the case where we are describing a mere function declaration, all we
22925 need to do here (and all we *can* do here) is to describe the *types* of
22926 its formal parameters. */
22927 if (debug_info_level <= DINFO_LEVEL_TERSE)
22929 else if (declaration)
22930 gen_formal_types_die (decl, subr_die);
22931 else
22933 /* Generate DIEs to represent all known formal parameters. */
22934 tree parm = DECL_ARGUMENTS (decl);
22935 tree generic_decl = early_dwarf
22936 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22937 tree generic_decl_parm = generic_decl
22938 ? DECL_ARGUMENTS (generic_decl)
22939 : NULL;
22941 /* Now we want to walk the list of parameters of the function and
22942 emit their relevant DIEs.
22944 We consider the case of DECL being an instance of a generic function
22945 as well as it being a normal function.
22947 If DECL is an instance of a generic function we walk the
22948 parameters of the generic function declaration _and_ the parameters of
22949 DECL itself. This is useful because we want to emit specific DIEs for
22950 function parameter packs and those are declared as part of the
22951 generic function declaration. In that particular case,
22952 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22953 That DIE has children DIEs representing the set of arguments
22954 of the pack. Note that the set of pack arguments can be empty.
22955 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22956 children DIE.
22958 Otherwise, we just consider the parameters of DECL. */
22959 while (generic_decl_parm || parm)
22961 if (generic_decl_parm
22962 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22963 gen_formal_parameter_pack_die (generic_decl_parm,
22964 parm, subr_die,
22965 &parm);
22966 else if (parm && !POINTER_BOUNDS_P (parm))
22968 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22970 if (early_dwarf
22971 && parm == DECL_ARGUMENTS (decl)
22972 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22973 && parm_die
22974 && (dwarf_version >= 3 || !dwarf_strict))
22975 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22977 parm = DECL_CHAIN (parm);
22979 else if (parm)
22980 parm = DECL_CHAIN (parm);
22982 if (generic_decl_parm)
22983 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22986 /* Decide whether we need an unspecified_parameters DIE at the end.
22987 There are 2 more cases to do this for: 1) the ansi ... declaration -
22988 this is detectable when the end of the arg list is not a
22989 void_type_node 2) an unprototyped function declaration (not a
22990 definition). This just means that we have no info about the
22991 parameters at all. */
22992 if (early_dwarf)
22994 if (prototype_p (TREE_TYPE (decl)))
22996 /* This is the prototyped case, check for.... */
22997 if (stdarg_p (TREE_TYPE (decl)))
22998 gen_unspecified_parameters_die (decl, subr_die);
23000 else if (DECL_INITIAL (decl) == NULL_TREE)
23001 gen_unspecified_parameters_die (decl, subr_die);
23005 if (subr_die != old_die)
23006 /* Add the calling convention attribute if requested. */
23007 add_calling_convention_attribute (subr_die, decl);
23009 /* Output Dwarf info for all of the stuff within the body of the function
23010 (if it has one - it may be just a declaration).
23012 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23013 a function. This BLOCK actually represents the outermost binding contour
23014 for the function, i.e. the contour in which the function's formal
23015 parameters and labels get declared. Curiously, it appears that the front
23016 end doesn't actually put the PARM_DECL nodes for the current function onto
23017 the BLOCK_VARS list for this outer scope, but are strung off of the
23018 DECL_ARGUMENTS list for the function instead.
23020 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23021 the LABEL_DECL nodes for the function however, and we output DWARF info
23022 for those in decls_for_scope. Just within the `outer_scope' there will be
23023 a BLOCK node representing the function's outermost pair of curly braces,
23024 and any blocks used for the base and member initializers of a C++
23025 constructor function. */
23026 tree outer_scope = DECL_INITIAL (decl);
23027 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23029 int call_site_note_count = 0;
23030 int tail_call_site_note_count = 0;
23032 /* Emit a DW_TAG_variable DIE for a named return value. */
23033 if (DECL_NAME (DECL_RESULT (decl)))
23034 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23036 /* The first time through decls_for_scope we will generate the
23037 DIEs for the locals. The second time, we fill in the
23038 location info. */
23039 decls_for_scope (outer_scope, subr_die);
23041 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23043 struct call_arg_loc_node *ca_loc;
23044 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23046 dw_die_ref die = NULL;
23047 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23048 rtx arg, next_arg;
23050 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23051 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
23052 : NULL_RTX);
23053 arg; arg = next_arg)
23055 dw_loc_descr_ref reg, val;
23056 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23057 dw_die_ref cdie, tdie = NULL;
23059 next_arg = XEXP (arg, 1);
23060 if (REG_P (XEXP (XEXP (arg, 0), 0))
23061 && next_arg
23062 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23063 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23064 && REGNO (XEXP (XEXP (arg, 0), 0))
23065 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23066 next_arg = XEXP (next_arg, 1);
23067 if (mode == VOIDmode)
23069 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23070 if (mode == VOIDmode)
23071 mode = GET_MODE (XEXP (arg, 0));
23073 if (mode == VOIDmode || mode == BLKmode)
23074 continue;
23075 /* Get dynamic information about call target only if we
23076 have no static information: we cannot generate both
23077 DW_AT_call_origin and DW_AT_call_target
23078 attributes. */
23079 if (ca_loc->symbol_ref == NULL_RTX)
23081 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23083 tloc = XEXP (XEXP (arg, 0), 1);
23084 continue;
23086 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23087 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23089 tlocc = XEXP (XEXP (arg, 0), 1);
23090 continue;
23093 reg = NULL;
23094 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23095 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23096 VAR_INIT_STATUS_INITIALIZED);
23097 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23099 rtx mem = XEXP (XEXP (arg, 0), 0);
23100 reg = mem_loc_descriptor (XEXP (mem, 0),
23101 get_address_mode (mem),
23102 GET_MODE (mem),
23103 VAR_INIT_STATUS_INITIALIZED);
23105 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23106 == DEBUG_PARAMETER_REF)
23108 tree tdecl
23109 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23110 tdie = lookup_decl_die (tdecl);
23111 if (tdie == NULL)
23112 continue;
23114 else
23115 continue;
23116 if (reg == NULL
23117 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23118 != DEBUG_PARAMETER_REF)
23119 continue;
23120 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23121 VOIDmode,
23122 VAR_INIT_STATUS_INITIALIZED);
23123 if (val == NULL)
23124 continue;
23125 if (die == NULL)
23126 die = gen_call_site_die (decl, subr_die, ca_loc);
23127 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23128 NULL_TREE);
23129 if (reg != NULL)
23130 add_AT_loc (cdie, DW_AT_location, reg);
23131 else if (tdie != NULL)
23132 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23133 tdie);
23134 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23135 if (next_arg != XEXP (arg, 1))
23137 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23138 if (mode == VOIDmode)
23139 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23140 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23141 0), 1),
23142 mode, VOIDmode,
23143 VAR_INIT_STATUS_INITIALIZED);
23144 if (val != NULL)
23145 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23146 val);
23149 if (die == NULL
23150 && (ca_loc->symbol_ref || tloc))
23151 die = gen_call_site_die (decl, subr_die, ca_loc);
23152 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23154 dw_loc_descr_ref tval = NULL;
23156 if (tloc != NULL_RTX)
23157 tval = mem_loc_descriptor (tloc,
23158 GET_MODE (tloc) == VOIDmode
23159 ? Pmode : GET_MODE (tloc),
23160 VOIDmode,
23161 VAR_INIT_STATUS_INITIALIZED);
23162 if (tval)
23163 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23164 else if (tlocc != NULL_RTX)
23166 tval = mem_loc_descriptor (tlocc,
23167 GET_MODE (tlocc) == VOIDmode
23168 ? Pmode : GET_MODE (tlocc),
23169 VOIDmode,
23170 VAR_INIT_STATUS_INITIALIZED);
23171 if (tval)
23172 add_AT_loc (die,
23173 dwarf_AT (DW_AT_call_target_clobbered),
23174 tval);
23177 if (die != NULL)
23179 call_site_note_count++;
23180 if (ca_loc->tail_call_p)
23181 tail_call_site_note_count++;
23185 call_arg_locations = NULL;
23186 call_arg_loc_last = NULL;
23187 if (tail_call_site_count >= 0
23188 && tail_call_site_count == tail_call_site_note_count
23189 && (!dwarf_strict || dwarf_version >= 5))
23191 if (call_site_count >= 0
23192 && call_site_count == call_site_note_count)
23193 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23194 else
23195 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23197 call_site_count = -1;
23198 tail_call_site_count = -1;
23201 /* Mark used types after we have created DIEs for the functions scopes. */
23202 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23205 /* Returns a hash value for X (which really is a die_struct). */
23207 hashval_t
23208 block_die_hasher::hash (die_struct *d)
23210 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23213 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23214 as decl_id and die_parent of die_struct Y. */
23216 bool
23217 block_die_hasher::equal (die_struct *x, die_struct *y)
23219 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23222 /* Hold information about markers for inlined entry points. */
23223 struct GTY ((for_user)) inline_entry_data
23225 /* The block that's the inlined_function_outer_scope for an inlined
23226 function. */
23227 tree block;
23229 /* The label at the inlined entry point. */
23230 const char *label_pfx;
23231 unsigned int label_num;
23233 /* The view number to be used as the inlined entry point. */
23234 var_loc_view view;
23237 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23239 typedef tree compare_type;
23240 static inline hashval_t hash (const inline_entry_data *);
23241 static inline bool equal (const inline_entry_data *, const_tree);
23244 /* Hash table routines for inline_entry_data. */
23246 inline hashval_t
23247 inline_entry_data_hasher::hash (const inline_entry_data *data)
23249 return htab_hash_pointer (data->block);
23252 inline bool
23253 inline_entry_data_hasher::equal (const inline_entry_data *data,
23254 const_tree block)
23256 return data->block == block;
23259 /* Inlined entry points pending DIE creation in this compilation unit. */
23261 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23264 /* Return TRUE if DECL, which may have been previously generated as
23265 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23266 true if decl (or its origin) is either an extern declaration or a
23267 class/namespace scoped declaration.
23269 The declare_in_namespace support causes us to get two DIEs for one
23270 variable, both of which are declarations. We want to avoid
23271 considering one to be a specification, so we must test for
23272 DECLARATION and DW_AT_declaration. */
23273 static inline bool
23274 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23276 return (old_die && TREE_STATIC (decl) && !declaration
23277 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23280 /* Return true if DECL is a local static. */
23282 static inline bool
23283 local_function_static (tree decl)
23285 gcc_assert (VAR_P (decl));
23286 return TREE_STATIC (decl)
23287 && DECL_CONTEXT (decl)
23288 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23291 /* Generate a DIE to represent a declared data object.
23292 Either DECL or ORIGIN must be non-null. */
23294 static void
23295 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23297 HOST_WIDE_INT off = 0;
23298 tree com_decl;
23299 tree decl_or_origin = decl ? decl : origin;
23300 tree ultimate_origin;
23301 dw_die_ref var_die;
23302 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23303 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23304 || class_or_namespace_scope_p (context_die));
23305 bool specialization_p = false;
23306 bool no_linkage_name = false;
23308 /* While C++ inline static data members have definitions inside of the
23309 class, force the first DIE to be a declaration, then let gen_member_die
23310 reparent it to the class context and call gen_variable_die again
23311 to create the outside of the class DIE for the definition. */
23312 if (!declaration
23313 && old_die == NULL
23314 && decl
23315 && DECL_CONTEXT (decl)
23316 && TYPE_P (DECL_CONTEXT (decl))
23317 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23319 declaration = true;
23320 if (dwarf_version < 5)
23321 no_linkage_name = true;
23324 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23325 if (decl || ultimate_origin)
23326 origin = ultimate_origin;
23327 com_decl = fortran_common (decl_or_origin, &off);
23329 /* Symbol in common gets emitted as a child of the common block, in the form
23330 of a data member. */
23331 if (com_decl)
23333 dw_die_ref com_die;
23334 dw_loc_list_ref loc = NULL;
23335 die_node com_die_arg;
23337 var_die = lookup_decl_die (decl_or_origin);
23338 if (var_die)
23340 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23342 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23343 if (loc)
23345 if (off)
23347 /* Optimize the common case. */
23348 if (single_element_loc_list_p (loc)
23349 && loc->expr->dw_loc_opc == DW_OP_addr
23350 && loc->expr->dw_loc_next == NULL
23351 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23352 == SYMBOL_REF)
23354 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23355 loc->expr->dw_loc_oprnd1.v.val_addr
23356 = plus_constant (GET_MODE (x), x , off);
23358 else
23359 loc_list_plus_const (loc, off);
23361 add_AT_location_description (var_die, DW_AT_location, loc);
23362 remove_AT (var_die, DW_AT_declaration);
23365 return;
23368 if (common_block_die_table == NULL)
23369 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23371 com_die_arg.decl_id = DECL_UID (com_decl);
23372 com_die_arg.die_parent = context_die;
23373 com_die = common_block_die_table->find (&com_die_arg);
23374 if (! early_dwarf)
23375 loc = loc_list_from_tree (com_decl, 2, NULL);
23376 if (com_die == NULL)
23378 const char *cnam
23379 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23380 die_node **slot;
23382 com_die = new_die (DW_TAG_common_block, context_die, decl);
23383 add_name_and_src_coords_attributes (com_die, com_decl);
23384 if (loc)
23386 add_AT_location_description (com_die, DW_AT_location, loc);
23387 /* Avoid sharing the same loc descriptor between
23388 DW_TAG_common_block and DW_TAG_variable. */
23389 loc = loc_list_from_tree (com_decl, 2, NULL);
23391 else if (DECL_EXTERNAL (decl_or_origin))
23392 add_AT_flag (com_die, DW_AT_declaration, 1);
23393 if (want_pubnames ())
23394 add_pubname_string (cnam, com_die); /* ??? needed? */
23395 com_die->decl_id = DECL_UID (com_decl);
23396 slot = common_block_die_table->find_slot (com_die, INSERT);
23397 *slot = com_die;
23399 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23401 add_AT_location_description (com_die, DW_AT_location, loc);
23402 loc = loc_list_from_tree (com_decl, 2, NULL);
23403 remove_AT (com_die, DW_AT_declaration);
23405 var_die = new_die (DW_TAG_variable, com_die, decl);
23406 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23407 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23408 decl_quals (decl_or_origin), false,
23409 context_die);
23410 add_alignment_attribute (var_die, decl);
23411 add_AT_flag (var_die, DW_AT_external, 1);
23412 if (loc)
23414 if (off)
23416 /* Optimize the common case. */
23417 if (single_element_loc_list_p (loc)
23418 && loc->expr->dw_loc_opc == DW_OP_addr
23419 && loc->expr->dw_loc_next == NULL
23420 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23422 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23423 loc->expr->dw_loc_oprnd1.v.val_addr
23424 = plus_constant (GET_MODE (x), x, off);
23426 else
23427 loc_list_plus_const (loc, off);
23429 add_AT_location_description (var_die, DW_AT_location, loc);
23431 else if (DECL_EXTERNAL (decl_or_origin))
23432 add_AT_flag (var_die, DW_AT_declaration, 1);
23433 if (decl)
23434 equate_decl_number_to_die (decl, var_die);
23435 return;
23438 if (old_die)
23440 if (declaration)
23442 /* A declaration that has been previously dumped, needs no
23443 further annotations, since it doesn't need location on
23444 the second pass. */
23445 return;
23447 else if (decl_will_get_specification_p (old_die, decl, declaration)
23448 && !get_AT (old_die, DW_AT_specification))
23450 /* Fall-thru so we can make a new variable die along with a
23451 DW_AT_specification. */
23453 else if (origin && old_die->die_parent != context_die)
23455 /* If we will be creating an inlined instance, we need a
23456 new DIE that will get annotated with
23457 DW_AT_abstract_origin. */
23458 gcc_assert (!DECL_ABSTRACT_P (decl));
23460 else
23462 /* If a DIE was dumped early, it still needs location info.
23463 Skip to where we fill the location bits. */
23464 var_die = old_die;
23466 /* ??? In LTRANS we cannot annotate early created variably
23467 modified type DIEs without copying them and adjusting all
23468 references to them. Thus we dumped them again, also add a
23469 reference to them. */
23470 tree type = TREE_TYPE (decl_or_origin);
23471 if (in_lto_p
23472 && variably_modified_type_p
23473 (type, decl_function_context (decl_or_origin)))
23475 if (decl_by_reference_p (decl_or_origin))
23476 add_type_attribute (var_die, TREE_TYPE (type),
23477 TYPE_UNQUALIFIED, false, context_die);
23478 else
23479 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23480 false, context_die);
23483 goto gen_variable_die_location;
23487 /* For static data members, the declaration in the class is supposed
23488 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23489 also in DWARF2; the specification should still be DW_TAG_variable
23490 referencing the DW_TAG_member DIE. */
23491 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23492 var_die = new_die (DW_TAG_member, context_die, decl);
23493 else
23494 var_die = new_die (DW_TAG_variable, context_die, decl);
23496 if (origin != NULL)
23497 add_abstract_origin_attribute (var_die, origin);
23499 /* Loop unrolling can create multiple blocks that refer to the same
23500 static variable, so we must test for the DW_AT_declaration flag.
23502 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23503 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23504 sharing them.
23506 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23507 else if (decl_will_get_specification_p (old_die, decl, declaration))
23509 /* This is a definition of a C++ class level static. */
23510 add_AT_specification (var_die, old_die);
23511 specialization_p = true;
23512 if (DECL_NAME (decl))
23514 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23515 struct dwarf_file_data * file_index = lookup_filename (s.file);
23517 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23518 add_AT_file (var_die, DW_AT_decl_file, file_index);
23520 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23521 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23523 if (debug_column_info
23524 && s.column
23525 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23526 != (unsigned) s.column))
23527 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23529 if (old_die->die_tag == DW_TAG_member)
23530 add_linkage_name (var_die, decl);
23533 else
23534 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23536 if ((origin == NULL && !specialization_p)
23537 || (origin != NULL
23538 && !DECL_ABSTRACT_P (decl_or_origin)
23539 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23540 decl_function_context
23541 (decl_or_origin))))
23543 tree type = TREE_TYPE (decl_or_origin);
23545 if (decl_by_reference_p (decl_or_origin))
23546 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23547 context_die);
23548 else
23549 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23550 context_die);
23553 if (origin == NULL && !specialization_p)
23555 if (TREE_PUBLIC (decl))
23556 add_AT_flag (var_die, DW_AT_external, 1);
23558 if (DECL_ARTIFICIAL (decl))
23559 add_AT_flag (var_die, DW_AT_artificial, 1);
23561 add_alignment_attribute (var_die, decl);
23563 add_accessibility_attribute (var_die, decl);
23566 if (declaration)
23567 add_AT_flag (var_die, DW_AT_declaration, 1);
23569 if (decl && (DECL_ABSTRACT_P (decl)
23570 || !old_die || is_declaration_die (old_die)))
23571 equate_decl_number_to_die (decl, var_die);
23573 gen_variable_die_location:
23574 if (! declaration
23575 && (! DECL_ABSTRACT_P (decl_or_origin)
23576 /* Local static vars are shared between all clones/inlines,
23577 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23578 already set. */
23579 || (VAR_P (decl_or_origin)
23580 && TREE_STATIC (decl_or_origin)
23581 && DECL_RTL_SET_P (decl_or_origin))))
23583 if (early_dwarf)
23584 add_pubname (decl_or_origin, var_die);
23585 else
23586 add_location_or_const_value_attribute (var_die, decl_or_origin,
23587 decl == NULL);
23589 else
23590 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23592 if ((dwarf_version >= 4 || !dwarf_strict)
23593 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23594 DW_AT_const_expr) == 1
23595 && !get_AT (var_die, DW_AT_const_expr)
23596 && !specialization_p)
23597 add_AT_flag (var_die, DW_AT_const_expr, 1);
23599 if (!dwarf_strict)
23601 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23602 DW_AT_inline);
23603 if (inl != -1
23604 && !get_AT (var_die, DW_AT_inline)
23605 && !specialization_p)
23606 add_AT_unsigned (var_die, DW_AT_inline, inl);
23610 /* Generate a DIE to represent a named constant. */
23612 static void
23613 gen_const_die (tree decl, dw_die_ref context_die)
23615 dw_die_ref const_die;
23616 tree type = TREE_TYPE (decl);
23618 const_die = lookup_decl_die (decl);
23619 if (const_die)
23620 return;
23622 const_die = new_die (DW_TAG_constant, context_die, decl);
23623 equate_decl_number_to_die (decl, const_die);
23624 add_name_and_src_coords_attributes (const_die, decl);
23625 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23626 if (TREE_PUBLIC (decl))
23627 add_AT_flag (const_die, DW_AT_external, 1);
23628 if (DECL_ARTIFICIAL (decl))
23629 add_AT_flag (const_die, DW_AT_artificial, 1);
23630 tree_add_const_value_attribute_for_decl (const_die, decl);
23633 /* Generate a DIE to represent a label identifier. */
23635 static void
23636 gen_label_die (tree decl, dw_die_ref context_die)
23638 tree origin = decl_ultimate_origin (decl);
23639 dw_die_ref lbl_die = lookup_decl_die (decl);
23640 rtx insn;
23641 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23643 if (!lbl_die)
23645 lbl_die = new_die (DW_TAG_label, context_die, decl);
23646 equate_decl_number_to_die (decl, lbl_die);
23648 if (origin != NULL)
23649 add_abstract_origin_attribute (lbl_die, origin);
23650 else
23651 add_name_and_src_coords_attributes (lbl_die, decl);
23654 if (DECL_ABSTRACT_P (decl))
23655 equate_decl_number_to_die (decl, lbl_die);
23656 else if (! early_dwarf)
23658 insn = DECL_RTL_IF_SET (decl);
23660 /* Deleted labels are programmer specified labels which have been
23661 eliminated because of various optimizations. We still emit them
23662 here so that it is possible to put breakpoints on them. */
23663 if (insn
23664 && (LABEL_P (insn)
23665 || ((NOTE_P (insn)
23666 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23668 /* When optimization is enabled (via -O) some parts of the compiler
23669 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23670 represent source-level labels which were explicitly declared by
23671 the user. This really shouldn't be happening though, so catch
23672 it if it ever does happen. */
23673 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23675 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23676 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23678 else if (insn
23679 && NOTE_P (insn)
23680 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23681 && CODE_LABEL_NUMBER (insn) != -1)
23683 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23684 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23689 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23690 attributes to the DIE for a block STMT, to describe where the inlined
23691 function was called from. This is similar to add_src_coords_attributes. */
23693 static inline void
23694 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23696 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23698 if (dwarf_version >= 3 || !dwarf_strict)
23700 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23701 add_AT_unsigned (die, DW_AT_call_line, s.line);
23702 if (debug_column_info && s.column)
23703 add_AT_unsigned (die, DW_AT_call_column, s.column);
23708 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23709 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23711 static inline void
23712 add_high_low_attributes (tree stmt, dw_die_ref die)
23714 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23716 if (inline_entry_data **iedp
23717 = !inline_entry_data_table ? NULL
23718 : inline_entry_data_table->find_slot_with_hash (stmt,
23719 htab_hash_pointer (stmt),
23720 NO_INSERT))
23722 inline_entry_data *ied = *iedp;
23723 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23724 gcc_assert (inlined_function_outer_scope_p (stmt));
23725 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23726 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23728 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view))
23730 if (!output_asm_line_debug_info ())
23731 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23732 else
23734 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23735 /* FIXME: this will resolve to a small number. Could we
23736 possibly emit smaller data? Ideally we'd emit a
23737 uleb128, but that would make the size of DIEs
23738 impossible for the compiler to compute, since it's
23739 the assembler that computes the value of the view
23740 label in this case. Ideally, we'd have a single form
23741 encompassing both the address and the view, and
23742 indirecting them through a table might make things
23743 easier, but even that would be more wasteful,
23744 space-wise, than what we have now. */
23745 add_AT_lbl_id (die, DW_AT_GNU_entry_view, label);
23749 inline_entry_data_table->clear_slot (iedp);
23752 if (BLOCK_FRAGMENT_CHAIN (stmt)
23753 && (dwarf_version >= 3 || !dwarf_strict))
23755 tree chain, superblock = NULL_TREE;
23756 dw_die_ref pdie;
23757 dw_attr_node *attr = NULL;
23759 if (!MAY_HAVE_DEBUG_MARKER_INSNS && inlined_function_outer_scope_p (stmt))
23761 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23762 BLOCK_NUMBER (stmt));
23763 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23766 /* Optimize duplicate .debug_ranges lists or even tails of
23767 lists. If this BLOCK has same ranges as its supercontext,
23768 lookup DW_AT_ranges attribute in the supercontext (and
23769 recursively so), verify that the ranges_table contains the
23770 right values and use it instead of adding a new .debug_range. */
23771 for (chain = stmt, pdie = die;
23772 BLOCK_SAME_RANGE (chain);
23773 chain = BLOCK_SUPERCONTEXT (chain))
23775 dw_attr_node *new_attr;
23777 pdie = pdie->die_parent;
23778 if (pdie == NULL)
23779 break;
23780 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23781 break;
23782 new_attr = get_AT (pdie, DW_AT_ranges);
23783 if (new_attr == NULL
23784 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23785 break;
23786 attr = new_attr;
23787 superblock = BLOCK_SUPERCONTEXT (chain);
23789 if (attr != NULL
23790 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23791 == BLOCK_NUMBER (superblock))
23792 && BLOCK_FRAGMENT_CHAIN (superblock))
23794 unsigned long off = attr->dw_attr_val.v.val_offset;
23795 unsigned long supercnt = 0, thiscnt = 0;
23796 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23797 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23799 ++supercnt;
23800 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23801 == BLOCK_NUMBER (chain));
23803 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23804 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23805 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23806 ++thiscnt;
23807 gcc_assert (supercnt >= thiscnt);
23808 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23809 false);
23810 note_rnglist_head (off + supercnt - thiscnt);
23811 return;
23814 unsigned int offset = add_ranges (stmt, true);
23815 add_AT_range_list (die, DW_AT_ranges, offset, false);
23816 note_rnglist_head (offset);
23818 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23819 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23822 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23823 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23824 chain = BLOCK_FRAGMENT_CHAIN (chain);
23826 while (chain);
23827 add_ranges (NULL);
23829 else
23831 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23832 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23833 BLOCK_NUMBER (stmt));
23834 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23835 BLOCK_NUMBER (stmt));
23836 add_AT_low_high_pc (die, label, label_high, false);
23840 /* Generate a DIE for a lexical block. */
23842 static void
23843 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23845 dw_die_ref old_die = BLOCK_DIE (stmt);
23846 dw_die_ref stmt_die = NULL;
23847 if (!old_die)
23849 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23850 BLOCK_DIE (stmt) = stmt_die;
23853 if (BLOCK_ABSTRACT (stmt))
23855 if (old_die)
23857 /* This must have been generated early and it won't even
23858 need location information since it's a DW_AT_inline
23859 function. */
23860 if (flag_checking)
23861 for (dw_die_ref c = context_die; c; c = c->die_parent)
23862 if (c->die_tag == DW_TAG_inlined_subroutine
23863 || c->die_tag == DW_TAG_subprogram)
23865 gcc_assert (get_AT (c, DW_AT_inline));
23866 break;
23868 return;
23871 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23873 /* If this is an inlined instance, create a new lexical die for
23874 anything below to attach DW_AT_abstract_origin to. */
23875 if (old_die)
23877 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23878 BLOCK_DIE (stmt) = stmt_die;
23879 old_die = NULL;
23882 tree origin = block_ultimate_origin (stmt);
23883 if (origin != NULL_TREE && origin != stmt)
23884 add_abstract_origin_attribute (stmt_die, origin);
23887 if (old_die)
23888 stmt_die = old_die;
23890 /* A non abstract block whose blocks have already been reordered
23891 should have the instruction range for this block. If so, set the
23892 high/low attributes. */
23893 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23895 gcc_assert (stmt_die);
23896 add_high_low_attributes (stmt, stmt_die);
23899 decls_for_scope (stmt, stmt_die);
23902 /* Generate a DIE for an inlined subprogram. */
23904 static void
23905 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23907 tree decl;
23909 /* The instance of function that is effectively being inlined shall not
23910 be abstract. */
23911 gcc_assert (! BLOCK_ABSTRACT (stmt));
23913 decl = block_ultimate_origin (stmt);
23915 /* Make sure any inlined functions are known to be inlineable. */
23916 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23917 || cgraph_function_possibly_inlined_p (decl));
23919 if (! BLOCK_ABSTRACT (stmt))
23921 dw_die_ref subr_die
23922 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23924 if (call_arg_locations || MAY_HAVE_DEBUG_MARKER_INSNS)
23925 BLOCK_DIE (stmt) = subr_die;
23926 add_abstract_origin_attribute (subr_die, decl);
23927 if (TREE_ASM_WRITTEN (stmt))
23928 add_high_low_attributes (stmt, subr_die);
23929 add_call_src_coords_attributes (stmt, subr_die);
23931 decls_for_scope (stmt, subr_die);
23935 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23936 the comment for VLR_CONTEXT. */
23938 static void
23939 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23941 dw_die_ref decl_die;
23943 if (TREE_TYPE (decl) == error_mark_node)
23944 return;
23946 decl_die = new_die (DW_TAG_member, context_die, decl);
23947 add_name_and_src_coords_attributes (decl_die, decl);
23948 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23949 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23950 context_die);
23952 if (DECL_BIT_FIELD_TYPE (decl))
23954 add_byte_size_attribute (decl_die, decl);
23955 add_bit_size_attribute (decl_die, decl);
23956 add_bit_offset_attribute (decl_die, decl, ctx);
23959 add_alignment_attribute (decl_die, decl);
23961 /* If we have a variant part offset, then we are supposed to process a member
23962 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23963 trees. */
23964 gcc_assert (ctx->variant_part_offset == NULL_TREE
23965 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23966 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23967 add_data_member_location_attribute (decl_die, decl, ctx);
23969 if (DECL_ARTIFICIAL (decl))
23970 add_AT_flag (decl_die, DW_AT_artificial, 1);
23972 add_accessibility_attribute (decl_die, decl);
23974 /* Equate decl number to die, so that we can look up this decl later on. */
23975 equate_decl_number_to_die (decl, decl_die);
23978 /* Generate a DIE for a pointer to a member type. TYPE can be an
23979 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23980 pointer to member function. */
23982 static void
23983 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23985 if (lookup_type_die (type))
23986 return;
23988 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23989 scope_die_for (type, context_die), type);
23991 equate_type_number_to_die (type, ptr_die);
23992 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23993 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23994 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23995 context_die);
23996 add_alignment_attribute (ptr_die, type);
23998 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23999 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24001 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24002 add_AT_loc (ptr_die, DW_AT_use_location, op);
24006 static char *producer_string;
24008 /* Return a heap allocated producer string including command line options
24009 if -grecord-gcc-switches. */
24011 static char *
24012 gen_producer_string (void)
24014 size_t j;
24015 auto_vec<const char *> switches;
24016 const char *language_string = lang_hooks.name;
24017 char *producer, *tail;
24018 const char *p;
24019 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24020 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24022 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24023 switch (save_decoded_options[j].opt_index)
24025 case OPT_o:
24026 case OPT_d:
24027 case OPT_dumpbase:
24028 case OPT_dumpdir:
24029 case OPT_auxbase:
24030 case OPT_auxbase_strip:
24031 case OPT_quiet:
24032 case OPT_version:
24033 case OPT_v:
24034 case OPT_w:
24035 case OPT_L:
24036 case OPT_D:
24037 case OPT_I:
24038 case OPT_U:
24039 case OPT_SPECIAL_unknown:
24040 case OPT_SPECIAL_ignore:
24041 case OPT_SPECIAL_program_name:
24042 case OPT_SPECIAL_input_file:
24043 case OPT_grecord_gcc_switches:
24044 case OPT__output_pch_:
24045 case OPT_fdiagnostics_show_location_:
24046 case OPT_fdiagnostics_show_option:
24047 case OPT_fdiagnostics_show_caret:
24048 case OPT_fdiagnostics_color_:
24049 case OPT_fverbose_asm:
24050 case OPT____:
24051 case OPT__sysroot_:
24052 case OPT_nostdinc:
24053 case OPT_nostdinc__:
24054 case OPT_fpreprocessed:
24055 case OPT_fltrans_output_list_:
24056 case OPT_fresolution_:
24057 case OPT_fdebug_prefix_map_:
24058 case OPT_fmacro_prefix_map_:
24059 case OPT_ffile_prefix_map_:
24060 case OPT_fcompare_debug:
24061 /* Ignore these. */
24062 continue;
24063 default:
24064 if (cl_options[save_decoded_options[j].opt_index].flags
24065 & CL_NO_DWARF_RECORD)
24066 continue;
24067 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24068 == '-');
24069 switch (save_decoded_options[j].canonical_option[0][1])
24071 case 'M':
24072 case 'i':
24073 case 'W':
24074 continue;
24075 case 'f':
24076 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24077 "dump", 4) == 0)
24078 continue;
24079 break;
24080 default:
24081 break;
24083 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24084 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24085 break;
24088 producer = XNEWVEC (char, plen + 1 + len + 1);
24089 tail = producer;
24090 sprintf (tail, "%s %s", language_string, version_string);
24091 tail += plen;
24093 FOR_EACH_VEC_ELT (switches, j, p)
24095 len = strlen (p);
24096 *tail = ' ';
24097 memcpy (tail + 1, p, len);
24098 tail += len + 1;
24101 *tail = '\0';
24102 return producer;
24105 /* Given a C and/or C++ language/version string return the "highest".
24106 C++ is assumed to be "higher" than C in this case. Used for merging
24107 LTO translation unit languages. */
24108 static const char *
24109 highest_c_language (const char *lang1, const char *lang2)
24111 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24112 return "GNU C++17";
24113 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24114 return "GNU C++14";
24115 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24116 return "GNU C++11";
24117 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24118 return "GNU C++98";
24120 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24121 return "GNU C17";
24122 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24123 return "GNU C11";
24124 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24125 return "GNU C99";
24126 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24127 return "GNU C89";
24129 gcc_unreachable ();
24133 /* Generate the DIE for the compilation unit. */
24135 static dw_die_ref
24136 gen_compile_unit_die (const char *filename)
24138 dw_die_ref die;
24139 const char *language_string = lang_hooks.name;
24140 int language;
24142 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24144 if (filename)
24146 add_name_attribute (die, filename);
24147 /* Don't add cwd for <built-in>. */
24148 if (filename[0] != '<')
24149 add_comp_dir_attribute (die);
24152 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24154 /* If our producer is LTO try to figure out a common language to use
24155 from the global list of translation units. */
24156 if (strcmp (language_string, "GNU GIMPLE") == 0)
24158 unsigned i;
24159 tree t;
24160 const char *common_lang = NULL;
24162 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24164 if (!TRANSLATION_UNIT_LANGUAGE (t))
24165 continue;
24166 if (!common_lang)
24167 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24168 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24170 else if (strncmp (common_lang, "GNU C", 5) == 0
24171 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24172 /* Mixing C and C++ is ok, use C++ in that case. */
24173 common_lang = highest_c_language (common_lang,
24174 TRANSLATION_UNIT_LANGUAGE (t));
24175 else
24177 /* Fall back to C. */
24178 common_lang = NULL;
24179 break;
24183 if (common_lang)
24184 language_string = common_lang;
24187 language = DW_LANG_C;
24188 if (strncmp (language_string, "GNU C", 5) == 0
24189 && ISDIGIT (language_string[5]))
24191 language = DW_LANG_C89;
24192 if (dwarf_version >= 3 || !dwarf_strict)
24194 if (strcmp (language_string, "GNU C89") != 0)
24195 language = DW_LANG_C99;
24197 if (dwarf_version >= 5 /* || !dwarf_strict */)
24198 if (strcmp (language_string, "GNU C11") == 0
24199 || strcmp (language_string, "GNU C17") == 0)
24200 language = DW_LANG_C11;
24203 else if (strncmp (language_string, "GNU C++", 7) == 0)
24205 language = DW_LANG_C_plus_plus;
24206 if (dwarf_version >= 5 /* || !dwarf_strict */)
24208 if (strcmp (language_string, "GNU C++11") == 0)
24209 language = DW_LANG_C_plus_plus_11;
24210 else if (strcmp (language_string, "GNU C++14") == 0)
24211 language = DW_LANG_C_plus_plus_14;
24212 else if (strcmp (language_string, "GNU C++17") == 0)
24213 /* For now. */
24214 language = DW_LANG_C_plus_plus_14;
24217 else if (strcmp (language_string, "GNU F77") == 0)
24218 language = DW_LANG_Fortran77;
24219 else if (dwarf_version >= 3 || !dwarf_strict)
24221 if (strcmp (language_string, "GNU Ada") == 0)
24222 language = DW_LANG_Ada95;
24223 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24225 language = DW_LANG_Fortran95;
24226 if (dwarf_version >= 5 /* || !dwarf_strict */)
24228 if (strcmp (language_string, "GNU Fortran2003") == 0)
24229 language = DW_LANG_Fortran03;
24230 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24231 language = DW_LANG_Fortran08;
24234 else if (strcmp (language_string, "GNU Objective-C") == 0)
24235 language = DW_LANG_ObjC;
24236 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24237 language = DW_LANG_ObjC_plus_plus;
24238 else if (dwarf_version >= 5 || !dwarf_strict)
24240 if (strcmp (language_string, "GNU Go") == 0)
24241 language = DW_LANG_Go;
24244 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24245 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24246 language = DW_LANG_Fortran90;
24248 add_AT_unsigned (die, DW_AT_language, language);
24250 switch (language)
24252 case DW_LANG_Fortran77:
24253 case DW_LANG_Fortran90:
24254 case DW_LANG_Fortran95:
24255 case DW_LANG_Fortran03:
24256 case DW_LANG_Fortran08:
24257 /* Fortran has case insensitive identifiers and the front-end
24258 lowercases everything. */
24259 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24260 break;
24261 default:
24262 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24263 break;
24265 return die;
24268 /* Generate the DIE for a base class. */
24270 static void
24271 gen_inheritance_die (tree binfo, tree access, tree type,
24272 dw_die_ref context_die)
24274 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24275 struct vlr_context ctx = { type, NULL };
24277 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24278 context_die);
24279 add_data_member_location_attribute (die, binfo, &ctx);
24281 if (BINFO_VIRTUAL_P (binfo))
24282 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24284 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24285 children, otherwise the default is DW_ACCESS_public. In DWARF2
24286 the default has always been DW_ACCESS_private. */
24287 if (access == access_public_node)
24289 if (dwarf_version == 2
24290 || context_die->die_tag == DW_TAG_class_type)
24291 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24293 else if (access == access_protected_node)
24294 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24295 else if (dwarf_version > 2
24296 && context_die->die_tag != DW_TAG_class_type)
24297 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24300 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24301 structure. */
24302 static bool
24303 is_variant_part (tree decl)
24305 return (TREE_CODE (decl) == FIELD_DECL
24306 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24309 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24310 return the FIELD_DECL. Return NULL_TREE otherwise. */
24312 static tree
24313 analyze_discr_in_predicate (tree operand, tree struct_type)
24315 bool continue_stripping = true;
24316 while (continue_stripping)
24317 switch (TREE_CODE (operand))
24319 CASE_CONVERT:
24320 operand = TREE_OPERAND (operand, 0);
24321 break;
24322 default:
24323 continue_stripping = false;
24324 break;
24327 /* Match field access to members of struct_type only. */
24328 if (TREE_CODE (operand) == COMPONENT_REF
24329 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24330 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24331 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24332 return TREE_OPERAND (operand, 1);
24333 else
24334 return NULL_TREE;
24337 /* Check that SRC is a constant integer that can be represented as a native
24338 integer constant (either signed or unsigned). If so, store it into DEST and
24339 return true. Return false otherwise. */
24341 static bool
24342 get_discr_value (tree src, dw_discr_value *dest)
24344 tree discr_type = TREE_TYPE (src);
24346 if (lang_hooks.types.get_debug_type)
24348 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24349 if (debug_type != NULL)
24350 discr_type = debug_type;
24353 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24354 return false;
24356 /* Signedness can vary between the original type and the debug type. This
24357 can happen for character types in Ada for instance: the character type
24358 used for code generation can be signed, to be compatible with the C one,
24359 but from a debugger point of view, it must be unsigned. */
24360 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24361 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24363 if (is_orig_unsigned != is_debug_unsigned)
24364 src = fold_convert (discr_type, src);
24366 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24367 return false;
24369 dest->pos = is_debug_unsigned;
24370 if (is_debug_unsigned)
24371 dest->v.uval = tree_to_uhwi (src);
24372 else
24373 dest->v.sval = tree_to_shwi (src);
24375 return true;
24378 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24379 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24380 store NULL_TREE in DISCR_DECL. Otherwise:
24382 - store the discriminant field in STRUCT_TYPE that controls the variant
24383 part to *DISCR_DECL
24385 - put in *DISCR_LISTS_P an array where for each variant, the item
24386 represents the corresponding matching list of discriminant values.
24388 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24389 the above array.
24391 Note that when the array is allocated (i.e. when the analysis is
24392 successful), it is up to the caller to free the array. */
24394 static void
24395 analyze_variants_discr (tree variant_part_decl,
24396 tree struct_type,
24397 tree *discr_decl,
24398 dw_discr_list_ref **discr_lists_p,
24399 unsigned *discr_lists_length)
24401 tree variant_part_type = TREE_TYPE (variant_part_decl);
24402 tree variant;
24403 dw_discr_list_ref *discr_lists;
24404 unsigned i;
24406 /* Compute how many variants there are in this variant part. */
24407 *discr_lists_length = 0;
24408 for (variant = TYPE_FIELDS (variant_part_type);
24409 variant != NULL_TREE;
24410 variant = DECL_CHAIN (variant))
24411 ++*discr_lists_length;
24413 *discr_decl = NULL_TREE;
24414 *discr_lists_p
24415 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24416 sizeof (**discr_lists_p));
24417 discr_lists = *discr_lists_p;
24419 /* And then analyze all variants to extract discriminant information for all
24420 of them. This analysis is conservative: as soon as we detect something we
24421 do not support, abort everything and pretend we found nothing. */
24422 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24423 variant != NULL_TREE;
24424 variant = DECL_CHAIN (variant), ++i)
24426 tree match_expr = DECL_QUALIFIER (variant);
24428 /* Now, try to analyze the predicate and deduce a discriminant for
24429 it. */
24430 if (match_expr == boolean_true_node)
24431 /* Typically happens for the default variant: it matches all cases that
24432 previous variants rejected. Don't output any matching value for
24433 this one. */
24434 continue;
24436 /* The following loop tries to iterate over each discriminant
24437 possibility: single values or ranges. */
24438 while (match_expr != NULL_TREE)
24440 tree next_round_match_expr;
24441 tree candidate_discr = NULL_TREE;
24442 dw_discr_list_ref new_node = NULL;
24444 /* Possibilities are matched one after the other by nested
24445 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24446 continue with the rest at next iteration. */
24447 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24449 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24450 match_expr = TREE_OPERAND (match_expr, 1);
24452 else
24453 next_round_match_expr = NULL_TREE;
24455 if (match_expr == boolean_false_node)
24456 /* This sub-expression matches nothing: just wait for the next
24457 one. */
24460 else if (TREE_CODE (match_expr) == EQ_EXPR)
24462 /* We are matching: <discr_field> == <integer_cst>
24463 This sub-expression matches a single value. */
24464 tree integer_cst = TREE_OPERAND (match_expr, 1);
24466 candidate_discr
24467 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24468 struct_type);
24470 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24471 if (!get_discr_value (integer_cst,
24472 &new_node->dw_discr_lower_bound))
24473 goto abort;
24474 new_node->dw_discr_range = false;
24477 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24479 /* We are matching:
24480 <discr_field> > <integer_cst>
24481 && <discr_field> < <integer_cst>.
24482 This sub-expression matches the range of values between the
24483 two matched integer constants. Note that comparisons can be
24484 inclusive or exclusive. */
24485 tree candidate_discr_1, candidate_discr_2;
24486 tree lower_cst, upper_cst;
24487 bool lower_cst_included, upper_cst_included;
24488 tree lower_op = TREE_OPERAND (match_expr, 0);
24489 tree upper_op = TREE_OPERAND (match_expr, 1);
24491 /* When the comparison is exclusive, the integer constant is not
24492 the discriminant range bound we are looking for: we will have
24493 to increment or decrement it. */
24494 if (TREE_CODE (lower_op) == GE_EXPR)
24495 lower_cst_included = true;
24496 else if (TREE_CODE (lower_op) == GT_EXPR)
24497 lower_cst_included = false;
24498 else
24499 goto abort;
24501 if (TREE_CODE (upper_op) == LE_EXPR)
24502 upper_cst_included = true;
24503 else if (TREE_CODE (upper_op) == LT_EXPR)
24504 upper_cst_included = false;
24505 else
24506 goto abort;
24508 /* Extract the discriminant from the first operand and check it
24509 is consistant with the same analysis in the second
24510 operand. */
24511 candidate_discr_1
24512 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24513 struct_type);
24514 candidate_discr_2
24515 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24516 struct_type);
24517 if (candidate_discr_1 == candidate_discr_2)
24518 candidate_discr = candidate_discr_1;
24519 else
24520 goto abort;
24522 /* Extract bounds from both. */
24523 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24524 lower_cst = TREE_OPERAND (lower_op, 1);
24525 upper_cst = TREE_OPERAND (upper_op, 1);
24527 if (!lower_cst_included)
24528 lower_cst
24529 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24530 build_int_cst (TREE_TYPE (lower_cst), 1));
24531 if (!upper_cst_included)
24532 upper_cst
24533 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24534 build_int_cst (TREE_TYPE (upper_cst), 1));
24536 if (!get_discr_value (lower_cst,
24537 &new_node->dw_discr_lower_bound)
24538 || !get_discr_value (upper_cst,
24539 &new_node->dw_discr_upper_bound))
24540 goto abort;
24542 new_node->dw_discr_range = true;
24545 else
24546 /* Unsupported sub-expression: we cannot determine the set of
24547 matching discriminant values. Abort everything. */
24548 goto abort;
24550 /* If the discriminant info is not consistant with what we saw so
24551 far, consider the analysis failed and abort everything. */
24552 if (candidate_discr == NULL_TREE
24553 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24554 goto abort;
24555 else
24556 *discr_decl = candidate_discr;
24558 if (new_node != NULL)
24560 new_node->dw_discr_next = discr_lists[i];
24561 discr_lists[i] = new_node;
24563 match_expr = next_round_match_expr;
24567 /* If we reach this point, we could match everything we were interested
24568 in. */
24569 return;
24571 abort:
24572 /* Clean all data structure and return no result. */
24573 free (*discr_lists_p);
24574 *discr_lists_p = NULL;
24575 *discr_decl = NULL_TREE;
24578 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24579 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24580 under CONTEXT_DIE.
24582 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24583 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24584 this type, which are record types, represent the available variants and each
24585 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24586 values are inferred from these attributes.
24588 In trees, the offsets for the fields inside these sub-records are relative
24589 to the variant part itself, whereas the corresponding DIEs should have
24590 offset attributes that are relative to the embedding record base address.
24591 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24592 must be an expression that computes the offset of the variant part to
24593 describe in DWARF. */
24595 static void
24596 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24597 dw_die_ref context_die)
24599 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24600 tree variant_part_offset = vlr_ctx->variant_part_offset;
24601 struct loc_descr_context ctx = {
24602 vlr_ctx->struct_type, /* context_type */
24603 NULL_TREE, /* base_decl */
24604 NULL, /* dpi */
24605 false, /* placeholder_arg */
24606 false /* placeholder_seen */
24609 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24610 NULL_TREE if there is no such field. */
24611 tree discr_decl = NULL_TREE;
24612 dw_discr_list_ref *discr_lists;
24613 unsigned discr_lists_length = 0;
24614 unsigned i;
24616 dw_die_ref dwarf_proc_die = NULL;
24617 dw_die_ref variant_part_die
24618 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24620 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24622 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24623 &discr_decl, &discr_lists, &discr_lists_length);
24625 if (discr_decl != NULL_TREE)
24627 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24629 if (discr_die)
24630 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24631 else
24632 /* We have no DIE for the discriminant, so just discard all
24633 discrimimant information in the output. */
24634 discr_decl = NULL_TREE;
24637 /* If the offset for this variant part is more complex than a constant,
24638 create a DWARF procedure for it so that we will not have to generate DWARF
24639 expressions for it for each member. */
24640 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24641 && (dwarf_version >= 3 || !dwarf_strict))
24643 const tree dwarf_proc_fndecl
24644 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24645 build_function_type (TREE_TYPE (variant_part_offset),
24646 NULL_TREE));
24647 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24648 const dw_loc_descr_ref dwarf_proc_body
24649 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24651 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24652 dwarf_proc_fndecl, context_die);
24653 if (dwarf_proc_die != NULL)
24654 variant_part_offset = dwarf_proc_call;
24657 /* Output DIEs for all variants. */
24658 i = 0;
24659 for (tree variant = TYPE_FIELDS (variant_part_type);
24660 variant != NULL_TREE;
24661 variant = DECL_CHAIN (variant), ++i)
24663 tree variant_type = TREE_TYPE (variant);
24664 dw_die_ref variant_die;
24666 /* All variants (i.e. members of a variant part) are supposed to be
24667 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24668 under these records. */
24669 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24671 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24672 equate_decl_number_to_die (variant, variant_die);
24674 /* Output discriminant values this variant matches, if any. */
24675 if (discr_decl == NULL || discr_lists[i] == NULL)
24676 /* In the case we have discriminant information at all, this is
24677 probably the default variant: as the standard says, don't
24678 output any discriminant value/list attribute. */
24680 else if (discr_lists[i]->dw_discr_next == NULL
24681 && !discr_lists[i]->dw_discr_range)
24682 /* If there is only one accepted value, don't bother outputting a
24683 list. */
24684 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24685 else
24686 add_discr_list (variant_die, discr_lists[i]);
24688 for (tree member = TYPE_FIELDS (variant_type);
24689 member != NULL_TREE;
24690 member = DECL_CHAIN (member))
24692 struct vlr_context vlr_sub_ctx = {
24693 vlr_ctx->struct_type, /* struct_type */
24694 NULL /* variant_part_offset */
24696 if (is_variant_part (member))
24698 /* All offsets for fields inside variant parts are relative to
24699 the top-level embedding RECORD_TYPE's base address. On the
24700 other hand, offsets in GCC's types are relative to the
24701 nested-most variant part. So we have to sum offsets each time
24702 we recurse. */
24704 vlr_sub_ctx.variant_part_offset
24705 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24706 variant_part_offset, byte_position (member));
24707 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24709 else
24711 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24712 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24717 free (discr_lists);
24720 /* Generate a DIE for a class member. */
24722 static void
24723 gen_member_die (tree type, dw_die_ref context_die)
24725 tree member;
24726 tree binfo = TYPE_BINFO (type);
24728 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24730 /* If this is not an incomplete type, output descriptions of each of its
24731 members. Note that as we output the DIEs necessary to represent the
24732 members of this record or union type, we will also be trying to output
24733 DIEs to represent the *types* of those members. However the `type'
24734 function (above) will specifically avoid generating type DIEs for member
24735 types *within* the list of member DIEs for this (containing) type except
24736 for those types (of members) which are explicitly marked as also being
24737 members of this (containing) type themselves. The g++ front- end can
24738 force any given type to be treated as a member of some other (containing)
24739 type by setting the TYPE_CONTEXT of the given (member) type to point to
24740 the TREE node representing the appropriate (containing) type. */
24742 /* First output info about the base classes. */
24743 if (binfo)
24745 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24746 int i;
24747 tree base;
24749 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24750 gen_inheritance_die (base,
24751 (accesses ? (*accesses)[i] : access_public_node),
24752 type,
24753 context_die);
24756 /* Now output info about the data members and type members. */
24757 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24759 struct vlr_context vlr_ctx = { type, NULL_TREE };
24760 bool static_inline_p
24761 = (TREE_STATIC (member)
24762 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24763 != -1));
24765 /* Ignore clones. */
24766 if (DECL_ABSTRACT_ORIGIN (member))
24767 continue;
24769 /* If we thought we were generating minimal debug info for TYPE
24770 and then changed our minds, some of the member declarations
24771 may have already been defined. Don't define them again, but
24772 do put them in the right order. */
24774 if (dw_die_ref child = lookup_decl_die (member))
24776 /* Handle inline static data members, which only have in-class
24777 declarations. */
24778 dw_die_ref ref = NULL;
24779 if (child->die_tag == DW_TAG_variable
24780 && child->die_parent == comp_unit_die ())
24782 ref = get_AT_ref (child, DW_AT_specification);
24783 /* For C++17 inline static data members followed by redundant
24784 out of class redeclaration, we might get here with
24785 child being the DIE created for the out of class
24786 redeclaration and with its DW_AT_specification being
24787 the DIE created for in-class definition. We want to
24788 reparent the latter, and don't want to create another
24789 DIE with DW_AT_specification in that case, because
24790 we already have one. */
24791 if (ref
24792 && static_inline_p
24793 && ref->die_tag == DW_TAG_variable
24794 && ref->die_parent == comp_unit_die ()
24795 && get_AT (ref, DW_AT_specification) == NULL)
24797 child = ref;
24798 ref = NULL;
24799 static_inline_p = false;
24803 if (child->die_tag == DW_TAG_variable
24804 && child->die_parent == comp_unit_die ()
24805 && ref == NULL)
24807 reparent_child (child, context_die);
24808 if (dwarf_version < 5)
24809 child->die_tag = DW_TAG_member;
24811 else
24812 splice_child_die (context_die, child);
24815 /* Do not generate standard DWARF for variant parts if we are generating
24816 the corresponding GNAT encodings: DIEs generated for both would
24817 conflict in our mappings. */
24818 else if (is_variant_part (member)
24819 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24821 vlr_ctx.variant_part_offset = byte_position (member);
24822 gen_variant_part (member, &vlr_ctx, context_die);
24824 else
24826 vlr_ctx.variant_part_offset = NULL_TREE;
24827 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24830 /* For C++ inline static data members emit immediately a DW_TAG_variable
24831 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24832 DW_AT_specification. */
24833 if (static_inline_p)
24835 int old_extern = DECL_EXTERNAL (member);
24836 DECL_EXTERNAL (member) = 0;
24837 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24838 DECL_EXTERNAL (member) = old_extern;
24843 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24844 is set, we pretend that the type was never defined, so we only get the
24845 member DIEs needed by later specification DIEs. */
24847 static void
24848 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24849 enum debug_info_usage usage)
24851 if (TREE_ASM_WRITTEN (type))
24853 /* Fill in the bound of variable-length fields in late dwarf if
24854 still incomplete. */
24855 if (!early_dwarf && variably_modified_type_p (type, NULL))
24856 for (tree member = TYPE_FIELDS (type);
24857 member;
24858 member = DECL_CHAIN (member))
24859 fill_variable_array_bounds (TREE_TYPE (member));
24860 return;
24863 dw_die_ref type_die = lookup_type_die (type);
24864 dw_die_ref scope_die = 0;
24865 int nested = 0;
24866 int complete = (TYPE_SIZE (type)
24867 && (! TYPE_STUB_DECL (type)
24868 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24869 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24870 complete = complete && should_emit_struct_debug (type, usage);
24872 if (type_die && ! complete)
24873 return;
24875 if (TYPE_CONTEXT (type) != NULL_TREE
24876 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24877 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24878 nested = 1;
24880 scope_die = scope_die_for (type, context_die);
24882 /* Generate child dies for template paramaters. */
24883 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24884 schedule_generic_params_dies_gen (type);
24886 if (! type_die || (nested && is_cu_die (scope_die)))
24887 /* First occurrence of type or toplevel definition of nested class. */
24889 dw_die_ref old_die = type_die;
24891 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24892 ? record_type_tag (type) : DW_TAG_union_type,
24893 scope_die, type);
24894 equate_type_number_to_die (type, type_die);
24895 if (old_die)
24896 add_AT_specification (type_die, old_die);
24897 else
24898 add_name_attribute (type_die, type_tag (type));
24900 else
24901 remove_AT (type_die, DW_AT_declaration);
24903 /* If this type has been completed, then give it a byte_size attribute and
24904 then give a list of members. */
24905 if (complete && !ns_decl)
24907 /* Prevent infinite recursion in cases where the type of some member of
24908 this type is expressed in terms of this type itself. */
24909 TREE_ASM_WRITTEN (type) = 1;
24910 add_byte_size_attribute (type_die, type);
24911 add_alignment_attribute (type_die, type);
24912 if (TYPE_STUB_DECL (type) != NULL_TREE)
24914 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24915 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24918 /* If the first reference to this type was as the return type of an
24919 inline function, then it may not have a parent. Fix this now. */
24920 if (type_die->die_parent == NULL)
24921 add_child_die (scope_die, type_die);
24923 push_decl_scope (type);
24924 gen_member_die (type, type_die);
24925 pop_decl_scope ();
24927 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24928 if (TYPE_ARTIFICIAL (type))
24929 add_AT_flag (type_die, DW_AT_artificial, 1);
24931 /* GNU extension: Record what type our vtable lives in. */
24932 if (TYPE_VFIELD (type))
24934 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24936 gen_type_die (vtype, context_die);
24937 add_AT_die_ref (type_die, DW_AT_containing_type,
24938 lookup_type_die (vtype));
24941 else
24943 add_AT_flag (type_die, DW_AT_declaration, 1);
24945 /* We don't need to do this for function-local types. */
24946 if (TYPE_STUB_DECL (type)
24947 && ! decl_function_context (TYPE_STUB_DECL (type)))
24948 vec_safe_push (incomplete_types, type);
24951 if (get_AT (type_die, DW_AT_name))
24952 add_pubtype (type, type_die);
24955 /* Generate a DIE for a subroutine _type_. */
24957 static void
24958 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24960 tree return_type = TREE_TYPE (type);
24961 dw_die_ref subr_die
24962 = new_die (DW_TAG_subroutine_type,
24963 scope_die_for (type, context_die), type);
24965 equate_type_number_to_die (type, subr_die);
24966 add_prototyped_attribute (subr_die, type);
24967 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24968 context_die);
24969 add_alignment_attribute (subr_die, type);
24970 gen_formal_types_die (type, subr_die);
24972 if (get_AT (subr_die, DW_AT_name))
24973 add_pubtype (type, subr_die);
24974 if ((dwarf_version >= 5 || !dwarf_strict)
24975 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24976 add_AT_flag (subr_die, DW_AT_reference, 1);
24977 if ((dwarf_version >= 5 || !dwarf_strict)
24978 && lang_hooks.types.type_dwarf_attribute (type,
24979 DW_AT_rvalue_reference) != -1)
24980 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24983 /* Generate a DIE for a type definition. */
24985 static void
24986 gen_typedef_die (tree decl, dw_die_ref context_die)
24988 dw_die_ref type_die;
24989 tree type;
24991 if (TREE_ASM_WRITTEN (decl))
24993 if (DECL_ORIGINAL_TYPE (decl))
24994 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24995 return;
24998 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24999 checks in process_scope_var and modified_type_die), this should be called
25000 only for original types. */
25001 gcc_assert (decl_ultimate_origin (decl) == NULL
25002 || decl_ultimate_origin (decl) == decl);
25004 TREE_ASM_WRITTEN (decl) = 1;
25005 type_die = new_die (DW_TAG_typedef, context_die, decl);
25007 add_name_and_src_coords_attributes (type_die, decl);
25008 if (DECL_ORIGINAL_TYPE (decl))
25010 type = DECL_ORIGINAL_TYPE (decl);
25011 if (type == error_mark_node)
25012 return;
25014 gcc_assert (type != TREE_TYPE (decl));
25015 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25017 else
25019 type = TREE_TYPE (decl);
25020 if (type == error_mark_node)
25021 return;
25023 if (is_naming_typedef_decl (TYPE_NAME (type)))
25025 /* Here, we are in the case of decl being a typedef naming
25026 an anonymous type, e.g:
25027 typedef struct {...} foo;
25028 In that case TREE_TYPE (decl) is not a typedef variant
25029 type and TYPE_NAME of the anonymous type is set to the
25030 TYPE_DECL of the typedef. This construct is emitted by
25031 the C++ FE.
25033 TYPE is the anonymous struct named by the typedef
25034 DECL. As we need the DW_AT_type attribute of the
25035 DW_TAG_typedef to point to the DIE of TYPE, let's
25036 generate that DIE right away. add_type_attribute
25037 called below will then pick (via lookup_type_die) that
25038 anonymous struct DIE. */
25039 if (!TREE_ASM_WRITTEN (type))
25040 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25042 /* This is a GNU Extension. We are adding a
25043 DW_AT_linkage_name attribute to the DIE of the
25044 anonymous struct TYPE. The value of that attribute
25045 is the name of the typedef decl naming the anonymous
25046 struct. This greatly eases the work of consumers of
25047 this debug info. */
25048 add_linkage_name_raw (lookup_type_die (type), decl);
25052 add_type_attribute (type_die, type, decl_quals (decl), false,
25053 context_die);
25055 if (is_naming_typedef_decl (decl))
25056 /* We want that all subsequent calls to lookup_type_die with
25057 TYPE in argument yield the DW_TAG_typedef we have just
25058 created. */
25059 equate_type_number_to_die (type, type_die);
25061 add_alignment_attribute (type_die, TREE_TYPE (decl));
25063 add_accessibility_attribute (type_die, decl);
25065 if (DECL_ABSTRACT_P (decl))
25066 equate_decl_number_to_die (decl, type_die);
25068 if (get_AT (type_die, DW_AT_name))
25069 add_pubtype (decl, type_die);
25072 /* Generate a DIE for a struct, class, enum or union type. */
25074 static void
25075 gen_tagged_type_die (tree type,
25076 dw_die_ref context_die,
25077 enum debug_info_usage usage)
25079 int need_pop;
25081 if (type == NULL_TREE
25082 || !is_tagged_type (type))
25083 return;
25085 if (TREE_ASM_WRITTEN (type))
25086 need_pop = 0;
25087 /* If this is a nested type whose containing class hasn't been written
25088 out yet, writing it out will cover this one, too. This does not apply
25089 to instantiations of member class templates; they need to be added to
25090 the containing class as they are generated. FIXME: This hurts the
25091 idea of combining type decls from multiple TUs, since we can't predict
25092 what set of template instantiations we'll get. */
25093 else if (TYPE_CONTEXT (type)
25094 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25095 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25097 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25099 if (TREE_ASM_WRITTEN (type))
25100 return;
25102 /* If that failed, attach ourselves to the stub. */
25103 push_decl_scope (TYPE_CONTEXT (type));
25104 context_die = lookup_type_die (TYPE_CONTEXT (type));
25105 need_pop = 1;
25107 else if (TYPE_CONTEXT (type) != NULL_TREE
25108 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25110 /* If this type is local to a function that hasn't been written
25111 out yet, use a NULL context for now; it will be fixed up in
25112 decls_for_scope. */
25113 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25114 /* A declaration DIE doesn't count; nested types need to go in the
25115 specification. */
25116 if (context_die && is_declaration_die (context_die))
25117 context_die = NULL;
25118 need_pop = 0;
25120 else
25122 context_die = declare_in_namespace (type, context_die);
25123 need_pop = 0;
25126 if (TREE_CODE (type) == ENUMERAL_TYPE)
25128 /* This might have been written out by the call to
25129 declare_in_namespace. */
25130 if (!TREE_ASM_WRITTEN (type))
25131 gen_enumeration_type_die (type, context_die);
25133 else
25134 gen_struct_or_union_type_die (type, context_die, usage);
25136 if (need_pop)
25137 pop_decl_scope ();
25139 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25140 it up if it is ever completed. gen_*_type_die will set it for us
25141 when appropriate. */
25144 /* Generate a type description DIE. */
25146 static void
25147 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25148 enum debug_info_usage usage)
25150 struct array_descr_info info;
25152 if (type == NULL_TREE || type == error_mark_node)
25153 return;
25155 if (flag_checking && type)
25156 verify_type (type);
25158 if (TYPE_NAME (type) != NULL_TREE
25159 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25160 && is_redundant_typedef (TYPE_NAME (type))
25161 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25162 /* The DECL of this type is a typedef we don't want to emit debug
25163 info for but we want debug info for its underlying typedef.
25164 This can happen for e.g, the injected-class-name of a C++
25165 type. */
25166 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25168 /* If TYPE is a typedef type variant, let's generate debug info
25169 for the parent typedef which TYPE is a type of. */
25170 if (typedef_variant_p (type))
25172 if (TREE_ASM_WRITTEN (type))
25173 return;
25175 tree name = TYPE_NAME (type);
25176 tree origin = decl_ultimate_origin (name);
25177 if (origin != NULL && origin != name)
25179 gen_decl_die (origin, NULL, NULL, context_die);
25180 return;
25183 /* Prevent broken recursion; we can't hand off to the same type. */
25184 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25186 /* Give typedefs the right scope. */
25187 context_die = scope_die_for (type, context_die);
25189 TREE_ASM_WRITTEN (type) = 1;
25191 gen_decl_die (name, NULL, NULL, context_die);
25192 return;
25195 /* If type is an anonymous tagged type named by a typedef, let's
25196 generate debug info for the typedef. */
25197 if (is_naming_typedef_decl (TYPE_NAME (type)))
25199 /* Use the DIE of the containing namespace as the parent DIE of
25200 the type description DIE we want to generate. */
25201 if (DECL_CONTEXT (TYPE_NAME (type))
25202 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
25203 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
25205 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25206 return;
25209 if (lang_hooks.types.get_debug_type)
25211 tree debug_type = lang_hooks.types.get_debug_type (type);
25213 if (debug_type != NULL_TREE && debug_type != type)
25215 gen_type_die_with_usage (debug_type, context_die, usage);
25216 return;
25220 /* We are going to output a DIE to represent the unqualified version
25221 of this type (i.e. without any const or volatile qualifiers) so
25222 get the main variant (i.e. the unqualified version) of this type
25223 now. (Vectors and arrays are special because the debugging info is in the
25224 cloned type itself. Similarly function/method types can contain extra
25225 ref-qualification). */
25226 if (TREE_CODE (type) == FUNCTION_TYPE
25227 || TREE_CODE (type) == METHOD_TYPE)
25229 /* For function/method types, can't use type_main_variant here,
25230 because that can have different ref-qualifiers for C++,
25231 but try to canonicalize. */
25232 tree main = TYPE_MAIN_VARIANT (type);
25233 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25234 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25235 && check_base_type (t, main)
25236 && check_lang_type (t, type))
25238 type = t;
25239 break;
25242 else if (TREE_CODE (type) != VECTOR_TYPE
25243 && TREE_CODE (type) != ARRAY_TYPE)
25244 type = type_main_variant (type);
25246 /* If this is an array type with hidden descriptor, handle it first. */
25247 if (!TREE_ASM_WRITTEN (type)
25248 && lang_hooks.types.get_array_descr_info)
25250 memset (&info, 0, sizeof (info));
25251 if (lang_hooks.types.get_array_descr_info (type, &info))
25253 /* Fortran sometimes emits array types with no dimension. */
25254 gcc_assert (info.ndimensions >= 0
25255 && (info.ndimensions
25256 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25257 gen_descr_array_type_die (type, &info, context_die);
25258 TREE_ASM_WRITTEN (type) = 1;
25259 return;
25263 if (TREE_ASM_WRITTEN (type))
25265 /* Variable-length types may be incomplete even if
25266 TREE_ASM_WRITTEN. For such types, fall through to
25267 gen_array_type_die() and possibly fill in
25268 DW_AT_{upper,lower}_bound attributes. */
25269 if ((TREE_CODE (type) != ARRAY_TYPE
25270 && TREE_CODE (type) != RECORD_TYPE
25271 && TREE_CODE (type) != UNION_TYPE
25272 && TREE_CODE (type) != QUAL_UNION_TYPE)
25273 || !variably_modified_type_p (type, NULL))
25274 return;
25277 switch (TREE_CODE (type))
25279 case ERROR_MARK:
25280 break;
25282 case POINTER_TYPE:
25283 case REFERENCE_TYPE:
25284 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25285 ensures that the gen_type_die recursion will terminate even if the
25286 type is recursive. Recursive types are possible in Ada. */
25287 /* ??? We could perhaps do this for all types before the switch
25288 statement. */
25289 TREE_ASM_WRITTEN (type) = 1;
25291 /* For these types, all that is required is that we output a DIE (or a
25292 set of DIEs) to represent the "basis" type. */
25293 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25294 DINFO_USAGE_IND_USE);
25295 break;
25297 case OFFSET_TYPE:
25298 /* This code is used for C++ pointer-to-data-member types.
25299 Output a description of the relevant class type. */
25300 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25301 DINFO_USAGE_IND_USE);
25303 /* Output a description of the type of the object pointed to. */
25304 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25305 DINFO_USAGE_IND_USE);
25307 /* Now output a DIE to represent this pointer-to-data-member type
25308 itself. */
25309 gen_ptr_to_mbr_type_die (type, context_die);
25310 break;
25312 case FUNCTION_TYPE:
25313 /* Force out return type (in case it wasn't forced out already). */
25314 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25315 DINFO_USAGE_DIR_USE);
25316 gen_subroutine_type_die (type, context_die);
25317 break;
25319 case METHOD_TYPE:
25320 /* Force out return type (in case it wasn't forced out already). */
25321 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25322 DINFO_USAGE_DIR_USE);
25323 gen_subroutine_type_die (type, context_die);
25324 break;
25326 case ARRAY_TYPE:
25327 case VECTOR_TYPE:
25328 gen_array_type_die (type, context_die);
25329 break;
25331 case ENUMERAL_TYPE:
25332 case RECORD_TYPE:
25333 case UNION_TYPE:
25334 case QUAL_UNION_TYPE:
25335 gen_tagged_type_die (type, context_die, usage);
25336 return;
25338 case VOID_TYPE:
25339 case INTEGER_TYPE:
25340 case REAL_TYPE:
25341 case FIXED_POINT_TYPE:
25342 case COMPLEX_TYPE:
25343 case BOOLEAN_TYPE:
25344 case POINTER_BOUNDS_TYPE:
25345 /* No DIEs needed for fundamental types. */
25346 break;
25348 case NULLPTR_TYPE:
25349 case LANG_TYPE:
25350 /* Just use DW_TAG_unspecified_type. */
25352 dw_die_ref type_die = lookup_type_die (type);
25353 if (type_die == NULL)
25355 tree name = TYPE_IDENTIFIER (type);
25356 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25357 type);
25358 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25359 equate_type_number_to_die (type, type_die);
25362 break;
25364 default:
25365 if (is_cxx_auto (type))
25367 tree name = TYPE_IDENTIFIER (type);
25368 dw_die_ref *die = (name == get_identifier ("auto")
25369 ? &auto_die : &decltype_auto_die);
25370 if (!*die)
25372 *die = new_die (DW_TAG_unspecified_type,
25373 comp_unit_die (), NULL_TREE);
25374 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25376 equate_type_number_to_die (type, *die);
25377 break;
25379 gcc_unreachable ();
25382 TREE_ASM_WRITTEN (type) = 1;
25385 static void
25386 gen_type_die (tree type, dw_die_ref context_die)
25388 if (type != error_mark_node)
25390 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25391 if (flag_checking)
25393 dw_die_ref die = lookup_type_die (type);
25394 if (die)
25395 check_die (die);
25400 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25401 things which are local to the given block. */
25403 static void
25404 gen_block_die (tree stmt, dw_die_ref context_die)
25406 int must_output_die = 0;
25407 bool inlined_func;
25409 /* Ignore blocks that are NULL. */
25410 if (stmt == NULL_TREE)
25411 return;
25413 inlined_func = inlined_function_outer_scope_p (stmt);
25415 /* If the block is one fragment of a non-contiguous block, do not
25416 process the variables, since they will have been done by the
25417 origin block. Do process subblocks. */
25418 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25420 tree sub;
25422 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25423 gen_block_die (sub, context_die);
25425 return;
25428 /* Determine if we need to output any Dwarf DIEs at all to represent this
25429 block. */
25430 if (inlined_func)
25431 /* The outer scopes for inlinings *must* always be represented. We
25432 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25433 must_output_die = 1;
25434 else
25436 /* Determine if this block directly contains any "significant"
25437 local declarations which we will need to output DIEs for. */
25438 if (debug_info_level > DINFO_LEVEL_TERSE)
25439 /* We are not in terse mode so *any* local declaration counts
25440 as being a "significant" one. */
25441 must_output_die = ((BLOCK_VARS (stmt) != NULL
25442 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25443 && (TREE_USED (stmt)
25444 || TREE_ASM_WRITTEN (stmt)
25445 || BLOCK_ABSTRACT (stmt)));
25446 else if ((TREE_USED (stmt)
25447 || TREE_ASM_WRITTEN (stmt)
25448 || BLOCK_ABSTRACT (stmt))
25449 && !dwarf2out_ignore_block (stmt))
25450 must_output_die = 1;
25453 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25454 DIE for any block which contains no significant local declarations at
25455 all. Rather, in such cases we just call `decls_for_scope' so that any
25456 needed Dwarf info for any sub-blocks will get properly generated. Note
25457 that in terse mode, our definition of what constitutes a "significant"
25458 local declaration gets restricted to include only inlined function
25459 instances and local (nested) function definitions. */
25460 if (must_output_die)
25462 if (inlined_func)
25464 /* If STMT block is abstract, that means we have been called
25465 indirectly from dwarf2out_abstract_function.
25466 That function rightfully marks the descendent blocks (of
25467 the abstract function it is dealing with) as being abstract,
25468 precisely to prevent us from emitting any
25469 DW_TAG_inlined_subroutine DIE as a descendent
25470 of an abstract function instance. So in that case, we should
25471 not call gen_inlined_subroutine_die.
25473 Later though, when cgraph asks dwarf2out to emit info
25474 for the concrete instance of the function decl into which
25475 the concrete instance of STMT got inlined, the later will lead
25476 to the generation of a DW_TAG_inlined_subroutine DIE. */
25477 if (! BLOCK_ABSTRACT (stmt))
25478 gen_inlined_subroutine_die (stmt, context_die);
25480 else
25481 gen_lexical_block_die (stmt, context_die);
25483 else
25484 decls_for_scope (stmt, context_die);
25487 /* Process variable DECL (or variable with origin ORIGIN) within
25488 block STMT and add it to CONTEXT_DIE. */
25489 static void
25490 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25492 dw_die_ref die;
25493 tree decl_or_origin = decl ? decl : origin;
25495 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25496 die = lookup_decl_die (decl_or_origin);
25497 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25499 if (TYPE_DECL_IS_STUB (decl_or_origin))
25500 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25501 else
25502 die = lookup_decl_die (decl_or_origin);
25503 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25504 if (! die && ! early_dwarf)
25505 return;
25507 else
25508 die = NULL;
25510 /* Avoid creating DIEs for local typedefs and concrete static variables that
25511 will only be pruned later. */
25512 if ((origin || decl_ultimate_origin (decl))
25513 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25514 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25516 origin = decl_ultimate_origin (decl_or_origin);
25517 if (decl && VAR_P (decl) && die != NULL)
25519 die = lookup_decl_die (origin);
25520 if (die != NULL)
25521 equate_decl_number_to_die (decl, die);
25523 return;
25526 if (die != NULL && die->die_parent == NULL)
25527 add_child_die (context_die, die);
25528 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25530 if (early_dwarf)
25531 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25532 stmt, context_die);
25534 else
25536 if (decl && DECL_P (decl))
25538 die = lookup_decl_die (decl);
25540 /* Early created DIEs do not have a parent as the decls refer
25541 to the function as DECL_CONTEXT rather than the BLOCK. */
25542 if (die && die->die_parent == NULL)
25544 gcc_assert (in_lto_p);
25545 add_child_die (context_die, die);
25549 gen_decl_die (decl, origin, NULL, context_die);
25553 /* Generate all of the decls declared within a given scope and (recursively)
25554 all of its sub-blocks. */
25556 static void
25557 decls_for_scope (tree stmt, dw_die_ref context_die)
25559 tree decl;
25560 unsigned int i;
25561 tree subblocks;
25563 /* Ignore NULL blocks. */
25564 if (stmt == NULL_TREE)
25565 return;
25567 /* Output the DIEs to represent all of the data objects and typedefs
25568 declared directly within this block but not within any nested
25569 sub-blocks. Also, nested function and tag DIEs have been
25570 generated with a parent of NULL; fix that up now. We don't
25571 have to do this if we're at -g1. */
25572 if (debug_info_level > DINFO_LEVEL_TERSE)
25574 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25575 process_scope_var (stmt, decl, NULL_TREE, context_die);
25576 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25577 origin - avoid doing this twice as we have no good way to see
25578 if we've done it once already. */
25579 if (! early_dwarf)
25580 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25582 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25583 if (decl == current_function_decl)
25584 /* Ignore declarations of the current function, while they
25585 are declarations, gen_subprogram_die would treat them
25586 as definitions again, because they are equal to
25587 current_function_decl and endlessly recurse. */;
25588 else if (TREE_CODE (decl) == FUNCTION_DECL)
25589 process_scope_var (stmt, decl, NULL_TREE, context_die);
25590 else
25591 process_scope_var (stmt, NULL_TREE, decl, context_die);
25595 /* Even if we're at -g1, we need to process the subblocks in order to get
25596 inlined call information. */
25598 /* Output the DIEs to represent all sub-blocks (and the items declared
25599 therein) of this block. */
25600 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25601 subblocks != NULL;
25602 subblocks = BLOCK_CHAIN (subblocks))
25603 gen_block_die (subblocks, context_die);
25606 /* Is this a typedef we can avoid emitting? */
25608 bool
25609 is_redundant_typedef (const_tree decl)
25611 if (TYPE_DECL_IS_STUB (decl))
25612 return true;
25614 if (DECL_ARTIFICIAL (decl)
25615 && DECL_CONTEXT (decl)
25616 && is_tagged_type (DECL_CONTEXT (decl))
25617 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25618 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25619 /* Also ignore the artificial member typedef for the class name. */
25620 return true;
25622 return false;
25625 /* Return TRUE if TYPE is a typedef that names a type for linkage
25626 purposes. This kind of typedefs is produced by the C++ FE for
25627 constructs like:
25629 typedef struct {...} foo;
25631 In that case, there is no typedef variant type produced for foo.
25632 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25633 struct type. */
25635 static bool
25636 is_naming_typedef_decl (const_tree decl)
25638 if (decl == NULL_TREE
25639 || TREE_CODE (decl) != TYPE_DECL
25640 || DECL_NAMELESS (decl)
25641 || !is_tagged_type (TREE_TYPE (decl))
25642 || DECL_IS_BUILTIN (decl)
25643 || is_redundant_typedef (decl)
25644 /* It looks like Ada produces TYPE_DECLs that are very similar
25645 to C++ naming typedefs but that have different
25646 semantics. Let's be specific to c++ for now. */
25647 || !is_cxx (decl))
25648 return FALSE;
25650 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25651 && TYPE_NAME (TREE_TYPE (decl)) == decl
25652 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25653 != TYPE_NAME (TREE_TYPE (decl))));
25656 /* Looks up the DIE for a context. */
25658 static inline dw_die_ref
25659 lookup_context_die (tree context)
25661 if (context)
25663 /* Find die that represents this context. */
25664 if (TYPE_P (context))
25666 context = TYPE_MAIN_VARIANT (context);
25667 dw_die_ref ctx = lookup_type_die (context);
25668 if (!ctx)
25669 return NULL;
25670 return strip_naming_typedef (context, ctx);
25672 else
25673 return lookup_decl_die (context);
25675 return comp_unit_die ();
25678 /* Returns the DIE for a context. */
25680 static inline dw_die_ref
25681 get_context_die (tree context)
25683 if (context)
25685 /* Find die that represents this context. */
25686 if (TYPE_P (context))
25688 context = TYPE_MAIN_VARIANT (context);
25689 return strip_naming_typedef (context, force_type_die (context));
25691 else
25692 return force_decl_die (context);
25694 return comp_unit_die ();
25697 /* Returns the DIE for decl. A DIE will always be returned. */
25699 static dw_die_ref
25700 force_decl_die (tree decl)
25702 dw_die_ref decl_die;
25703 unsigned saved_external_flag;
25704 tree save_fn = NULL_TREE;
25705 decl_die = lookup_decl_die (decl);
25706 if (!decl_die)
25708 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25710 decl_die = lookup_decl_die (decl);
25711 if (decl_die)
25712 return decl_die;
25714 switch (TREE_CODE (decl))
25716 case FUNCTION_DECL:
25717 /* Clear current_function_decl, so that gen_subprogram_die thinks
25718 that this is a declaration. At this point, we just want to force
25719 declaration die. */
25720 save_fn = current_function_decl;
25721 current_function_decl = NULL_TREE;
25722 gen_subprogram_die (decl, context_die);
25723 current_function_decl = save_fn;
25724 break;
25726 case VAR_DECL:
25727 /* Set external flag to force declaration die. Restore it after
25728 gen_decl_die() call. */
25729 saved_external_flag = DECL_EXTERNAL (decl);
25730 DECL_EXTERNAL (decl) = 1;
25731 gen_decl_die (decl, NULL, NULL, context_die);
25732 DECL_EXTERNAL (decl) = saved_external_flag;
25733 break;
25735 case NAMESPACE_DECL:
25736 if (dwarf_version >= 3 || !dwarf_strict)
25737 dwarf2out_decl (decl);
25738 else
25739 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25740 decl_die = comp_unit_die ();
25741 break;
25743 case TRANSLATION_UNIT_DECL:
25744 decl_die = comp_unit_die ();
25745 break;
25747 default:
25748 gcc_unreachable ();
25751 /* We should be able to find the DIE now. */
25752 if (!decl_die)
25753 decl_die = lookup_decl_die (decl);
25754 gcc_assert (decl_die);
25757 return decl_die;
25760 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25761 always returned. */
25763 static dw_die_ref
25764 force_type_die (tree type)
25766 dw_die_ref type_die;
25768 type_die = lookup_type_die (type);
25769 if (!type_die)
25771 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25773 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25774 false, context_die);
25775 gcc_assert (type_die);
25777 return type_die;
25780 /* Force out any required namespaces to be able to output DECL,
25781 and return the new context_die for it, if it's changed. */
25783 static dw_die_ref
25784 setup_namespace_context (tree thing, dw_die_ref context_die)
25786 tree context = (DECL_P (thing)
25787 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25788 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25789 /* Force out the namespace. */
25790 context_die = force_decl_die (context);
25792 return context_die;
25795 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25796 type) within its namespace, if appropriate.
25798 For compatibility with older debuggers, namespace DIEs only contain
25799 declarations; all definitions are emitted at CU scope, with
25800 DW_AT_specification pointing to the declaration (like with class
25801 members). */
25803 static dw_die_ref
25804 declare_in_namespace (tree thing, dw_die_ref context_die)
25806 dw_die_ref ns_context;
25808 if (debug_info_level <= DINFO_LEVEL_TERSE)
25809 return context_die;
25811 /* External declarations in the local scope only need to be emitted
25812 once, not once in the namespace and once in the scope.
25814 This avoids declaring the `extern' below in the
25815 namespace DIE as well as in the innermost scope:
25817 namespace S
25819 int i=5;
25820 int foo()
25822 int i=8;
25823 extern int i;
25824 return i;
25828 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25829 return context_die;
25831 /* If this decl is from an inlined function, then don't try to emit it in its
25832 namespace, as we will get confused. It would have already been emitted
25833 when the abstract instance of the inline function was emitted anyways. */
25834 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25835 return context_die;
25837 ns_context = setup_namespace_context (thing, context_die);
25839 if (ns_context != context_die)
25841 if (is_fortran ())
25842 return ns_context;
25843 if (DECL_P (thing))
25844 gen_decl_die (thing, NULL, NULL, ns_context);
25845 else
25846 gen_type_die (thing, ns_context);
25848 return context_die;
25851 /* Generate a DIE for a namespace or namespace alias. */
25853 static void
25854 gen_namespace_die (tree decl, dw_die_ref context_die)
25856 dw_die_ref namespace_die;
25858 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25859 they are an alias of. */
25860 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25862 /* Output a real namespace or module. */
25863 context_die = setup_namespace_context (decl, comp_unit_die ());
25864 namespace_die = new_die (is_fortran ()
25865 ? DW_TAG_module : DW_TAG_namespace,
25866 context_die, decl);
25867 /* For Fortran modules defined in different CU don't add src coords. */
25868 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25870 const char *name = dwarf2_name (decl, 0);
25871 if (name)
25872 add_name_attribute (namespace_die, name);
25874 else
25875 add_name_and_src_coords_attributes (namespace_die, decl);
25876 if (DECL_EXTERNAL (decl))
25877 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25878 equate_decl_number_to_die (decl, namespace_die);
25880 else
25882 /* Output a namespace alias. */
25884 /* Force out the namespace we are an alias of, if necessary. */
25885 dw_die_ref origin_die
25886 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25888 if (DECL_FILE_SCOPE_P (decl)
25889 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25890 context_die = setup_namespace_context (decl, comp_unit_die ());
25891 /* Now create the namespace alias DIE. */
25892 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25893 add_name_and_src_coords_attributes (namespace_die, decl);
25894 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25895 equate_decl_number_to_die (decl, namespace_die);
25897 if ((dwarf_version >= 5 || !dwarf_strict)
25898 && lang_hooks.decls.decl_dwarf_attribute (decl,
25899 DW_AT_export_symbols) == 1)
25900 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25902 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25903 if (want_pubnames ())
25904 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25907 /* Generate Dwarf debug information for a decl described by DECL.
25908 The return value is currently only meaningful for PARM_DECLs,
25909 for all other decls it returns NULL.
25911 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25912 It can be NULL otherwise. */
25914 static dw_die_ref
25915 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25916 dw_die_ref context_die)
25918 tree decl_or_origin = decl ? decl : origin;
25919 tree class_origin = NULL, ultimate_origin;
25921 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25922 return NULL;
25924 /* Ignore pointer bounds decls. */
25925 if (DECL_P (decl_or_origin)
25926 && TREE_TYPE (decl_or_origin)
25927 && POINTER_BOUNDS_P (decl_or_origin))
25928 return NULL;
25930 switch (TREE_CODE (decl_or_origin))
25932 case ERROR_MARK:
25933 break;
25935 case CONST_DECL:
25936 if (!is_fortran () && !is_ada ())
25938 /* The individual enumerators of an enum type get output when we output
25939 the Dwarf representation of the relevant enum type itself. */
25940 break;
25943 /* Emit its type. */
25944 gen_type_die (TREE_TYPE (decl), context_die);
25946 /* And its containing namespace. */
25947 context_die = declare_in_namespace (decl, context_die);
25949 gen_const_die (decl, context_die);
25950 break;
25952 case FUNCTION_DECL:
25953 #if 0
25954 /* FIXME */
25955 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25956 on local redeclarations of global functions. That seems broken. */
25957 if (current_function_decl != decl)
25958 /* This is only a declaration. */;
25959 #endif
25961 /* We should have abstract copies already and should not generate
25962 stray type DIEs in late LTO dumping. */
25963 if (! early_dwarf)
25966 /* If we're emitting a clone, emit info for the abstract instance. */
25967 else if (origin || DECL_ORIGIN (decl) != decl)
25968 dwarf2out_abstract_function (origin
25969 ? DECL_ORIGIN (origin)
25970 : DECL_ABSTRACT_ORIGIN (decl));
25972 /* If we're emitting a possibly inlined function emit it as
25973 abstract instance. */
25974 else if (cgraph_function_possibly_inlined_p (decl)
25975 && ! DECL_ABSTRACT_P (decl)
25976 && ! class_or_namespace_scope_p (context_die)
25977 /* dwarf2out_abstract_function won't emit a die if this is just
25978 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25979 that case, because that works only if we have a die. */
25980 && DECL_INITIAL (decl) != NULL_TREE)
25981 dwarf2out_abstract_function (decl);
25983 /* Otherwise we're emitting the primary DIE for this decl. */
25984 else if (debug_info_level > DINFO_LEVEL_TERSE)
25986 /* Before we describe the FUNCTION_DECL itself, make sure that we
25987 have its containing type. */
25988 if (!origin)
25989 origin = decl_class_context (decl);
25990 if (origin != NULL_TREE)
25991 gen_type_die (origin, context_die);
25993 /* And its return type. */
25994 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25996 /* And its virtual context. */
25997 if (DECL_VINDEX (decl) != NULL_TREE)
25998 gen_type_die (DECL_CONTEXT (decl), context_die);
26000 /* Make sure we have a member DIE for decl. */
26001 if (origin != NULL_TREE)
26002 gen_type_die_for_member (origin, decl, context_die);
26004 /* And its containing namespace. */
26005 context_die = declare_in_namespace (decl, context_die);
26008 /* Now output a DIE to represent the function itself. */
26009 if (decl)
26010 gen_subprogram_die (decl, context_die);
26011 break;
26013 case TYPE_DECL:
26014 /* If we are in terse mode, don't generate any DIEs to represent any
26015 actual typedefs. */
26016 if (debug_info_level <= DINFO_LEVEL_TERSE)
26017 break;
26019 /* In the special case of a TYPE_DECL node representing the declaration
26020 of some type tag, if the given TYPE_DECL is marked as having been
26021 instantiated from some other (original) TYPE_DECL node (e.g. one which
26022 was generated within the original definition of an inline function) we
26023 used to generate a special (abbreviated) DW_TAG_structure_type,
26024 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26025 should be actually referencing those DIEs, as variable DIEs with that
26026 type would be emitted already in the abstract origin, so it was always
26027 removed during unused type prunning. Don't add anything in this
26028 case. */
26029 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26030 break;
26032 if (is_redundant_typedef (decl))
26033 gen_type_die (TREE_TYPE (decl), context_die);
26034 else
26035 /* Output a DIE to represent the typedef itself. */
26036 gen_typedef_die (decl, context_die);
26037 break;
26039 case LABEL_DECL:
26040 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26041 gen_label_die (decl, context_die);
26042 break;
26044 case VAR_DECL:
26045 case RESULT_DECL:
26046 /* If we are in terse mode, don't generate any DIEs to represent any
26047 variable declarations or definitions. */
26048 if (debug_info_level <= DINFO_LEVEL_TERSE)
26049 break;
26051 /* Avoid generating stray type DIEs during late dwarf dumping.
26052 All types have been dumped early. */
26053 if (early_dwarf
26054 /* ??? But in LTRANS we cannot annotate early created variably
26055 modified type DIEs without copying them and adjusting all
26056 references to them. Dump them again as happens for inlining
26057 which copies both the decl and the types. */
26058 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26059 in VLA bound information for example. */
26060 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26061 current_function_decl)))
26063 /* Output any DIEs that are needed to specify the type of this data
26064 object. */
26065 if (decl_by_reference_p (decl_or_origin))
26066 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26067 else
26068 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26071 if (early_dwarf)
26073 /* And its containing type. */
26074 class_origin = decl_class_context (decl_or_origin);
26075 if (class_origin != NULL_TREE)
26076 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26078 /* And its containing namespace. */
26079 context_die = declare_in_namespace (decl_or_origin, context_die);
26082 /* Now output the DIE to represent the data object itself. This gets
26083 complicated because of the possibility that the VAR_DECL really
26084 represents an inlined instance of a formal parameter for an inline
26085 function. */
26086 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26087 if (ultimate_origin != NULL_TREE
26088 && TREE_CODE (ultimate_origin) == PARM_DECL)
26089 gen_formal_parameter_die (decl, origin,
26090 true /* Emit name attribute. */,
26091 context_die);
26092 else
26093 gen_variable_die (decl, origin, context_die);
26094 break;
26096 case FIELD_DECL:
26097 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26098 /* Ignore the nameless fields that are used to skip bits but handle C++
26099 anonymous unions and structs. */
26100 if (DECL_NAME (decl) != NULL_TREE
26101 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26102 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26104 gen_type_die (member_declared_type (decl), context_die);
26105 gen_field_die (decl, ctx, context_die);
26107 break;
26109 case PARM_DECL:
26110 /* Avoid generating stray type DIEs during late dwarf dumping.
26111 All types have been dumped early. */
26112 if (early_dwarf
26113 /* ??? But in LTRANS we cannot annotate early created variably
26114 modified type DIEs without copying them and adjusting all
26115 references to them. Dump them again as happens for inlining
26116 which copies both the decl and the types. */
26117 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26118 in VLA bound information for example. */
26119 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26120 current_function_decl)))
26122 if (DECL_BY_REFERENCE (decl_or_origin))
26123 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26124 else
26125 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26127 return gen_formal_parameter_die (decl, origin,
26128 true /* Emit name attribute. */,
26129 context_die);
26131 case NAMESPACE_DECL:
26132 if (dwarf_version >= 3 || !dwarf_strict)
26133 gen_namespace_die (decl, context_die);
26134 break;
26136 case IMPORTED_DECL:
26137 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26138 DECL_CONTEXT (decl), context_die);
26139 break;
26141 case NAMELIST_DECL:
26142 gen_namelist_decl (DECL_NAME (decl), context_die,
26143 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26144 break;
26146 default:
26147 /* Probably some frontend-internal decl. Assume we don't care. */
26148 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26149 break;
26152 return NULL;
26155 /* Output initial debug information for global DECL. Called at the
26156 end of the parsing process.
26158 This is the initial debug generation process. As such, the DIEs
26159 generated may be incomplete. A later debug generation pass
26160 (dwarf2out_late_global_decl) will augment the information generated
26161 in this pass (e.g., with complete location info). */
26163 static void
26164 dwarf2out_early_global_decl (tree decl)
26166 set_early_dwarf s;
26168 /* gen_decl_die() will set DECL_ABSTRACT because
26169 cgraph_function_possibly_inlined_p() returns true. This is in
26170 turn will cause DW_AT_inline attributes to be set.
26172 This happens because at early dwarf generation, there is no
26173 cgraph information, causing cgraph_function_possibly_inlined_p()
26174 to return true. Trick cgraph_function_possibly_inlined_p()
26175 while we generate dwarf early. */
26176 bool save = symtab->global_info_ready;
26177 symtab->global_info_ready = true;
26179 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26180 other DECLs and they can point to template types or other things
26181 that dwarf2out can't handle when done via dwarf2out_decl. */
26182 if (TREE_CODE (decl) != TYPE_DECL
26183 && TREE_CODE (decl) != PARM_DECL)
26185 if (TREE_CODE (decl) == FUNCTION_DECL)
26187 tree save_fndecl = current_function_decl;
26189 /* For nested functions, make sure we have DIEs for the parents first
26190 so that all nested DIEs are generated at the proper scope in the
26191 first shot. */
26192 tree context = decl_function_context (decl);
26193 if (context != NULL)
26195 dw_die_ref context_die = lookup_decl_die (context);
26196 current_function_decl = context;
26198 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26199 enough so that it lands in its own context. This avoids type
26200 pruning issues later on. */
26201 if (context_die == NULL || is_declaration_die (context_die))
26202 dwarf2out_decl (context);
26205 /* Emit an abstract origin of a function first. This happens
26206 with C++ constructor clones for example and makes
26207 dwarf2out_abstract_function happy which requires the early
26208 DIE of the abstract instance to be present. */
26209 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26210 dw_die_ref origin_die;
26211 if (origin != NULL
26212 /* Do not emit the DIE multiple times but make sure to
26213 process it fully here in case we just saw a declaration. */
26214 && ((origin_die = lookup_decl_die (origin)) == NULL
26215 || is_declaration_die (origin_die)))
26217 current_function_decl = origin;
26218 dwarf2out_decl (origin);
26221 /* Emit the DIE for decl but avoid doing that multiple times. */
26222 dw_die_ref old_die;
26223 if ((old_die = lookup_decl_die (decl)) == NULL
26224 || is_declaration_die (old_die))
26226 current_function_decl = decl;
26227 dwarf2out_decl (decl);
26230 current_function_decl = save_fndecl;
26232 else
26233 dwarf2out_decl (decl);
26235 symtab->global_info_ready = save;
26238 /* Return whether EXPR is an expression with the following pattern:
26239 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26241 static bool
26242 is_trivial_indirect_ref (tree expr)
26244 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26245 return false;
26247 tree nop = TREE_OPERAND (expr, 0);
26248 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26249 return false;
26251 tree int_cst = TREE_OPERAND (nop, 0);
26252 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26255 /* Output debug information for global decl DECL. Called from
26256 toplev.c after compilation proper has finished. */
26258 static void
26259 dwarf2out_late_global_decl (tree decl)
26261 /* Fill-in any location information we were unable to determine
26262 on the first pass. */
26263 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
26265 dw_die_ref die = lookup_decl_die (decl);
26267 /* We may have to generate early debug late for LTO in case debug
26268 was not enabled at compile-time or the target doesn't support
26269 the LTO early debug scheme. */
26270 if (! die && in_lto_p)
26272 dwarf2out_decl (decl);
26273 die = lookup_decl_die (decl);
26276 if (die)
26278 /* We get called via the symtab code invoking late_global_decl
26279 for symbols that are optimized out.
26281 Do not add locations for those, except if they have a
26282 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26283 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26284 INDIRECT_REF expression, as this could generate relocations to
26285 text symbols in LTO object files, which is invalid. */
26286 varpool_node *node = varpool_node::get (decl);
26287 if ((! node || ! node->definition)
26288 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26289 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26290 tree_add_const_value_attribute_for_decl (die, decl);
26291 else
26292 add_location_or_const_value_attribute (die, decl, false);
26297 /* Output debug information for type decl DECL. Called from toplev.c
26298 and from language front ends (to record built-in types). */
26299 static void
26300 dwarf2out_type_decl (tree decl, int local)
26302 if (!local)
26304 set_early_dwarf s;
26305 dwarf2out_decl (decl);
26309 /* Output debug information for imported module or decl DECL.
26310 NAME is non-NULL name in the lexical block if the decl has been renamed.
26311 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26312 that DECL belongs to.
26313 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26314 static void
26315 dwarf2out_imported_module_or_decl_1 (tree decl,
26316 tree name,
26317 tree lexical_block,
26318 dw_die_ref lexical_block_die)
26320 expanded_location xloc;
26321 dw_die_ref imported_die = NULL;
26322 dw_die_ref at_import_die;
26324 if (TREE_CODE (decl) == IMPORTED_DECL)
26326 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26327 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26328 gcc_assert (decl);
26330 else
26331 xloc = expand_location (input_location);
26333 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26335 at_import_die = force_type_die (TREE_TYPE (decl));
26336 /* For namespace N { typedef void T; } using N::T; base_type_die
26337 returns NULL, but DW_TAG_imported_declaration requires
26338 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26339 if (!at_import_die)
26341 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26342 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26343 at_import_die = lookup_type_die (TREE_TYPE (decl));
26344 gcc_assert (at_import_die);
26347 else
26349 at_import_die = lookup_decl_die (decl);
26350 if (!at_import_die)
26352 /* If we're trying to avoid duplicate debug info, we may not have
26353 emitted the member decl for this field. Emit it now. */
26354 if (TREE_CODE (decl) == FIELD_DECL)
26356 tree type = DECL_CONTEXT (decl);
26358 if (TYPE_CONTEXT (type)
26359 && TYPE_P (TYPE_CONTEXT (type))
26360 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26361 DINFO_USAGE_DIR_USE))
26362 return;
26363 gen_type_die_for_member (type, decl,
26364 get_context_die (TYPE_CONTEXT (type)));
26366 if (TREE_CODE (decl) == NAMELIST_DECL)
26367 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26368 get_context_die (DECL_CONTEXT (decl)),
26369 NULL_TREE);
26370 else
26371 at_import_die = force_decl_die (decl);
26375 if (TREE_CODE (decl) == NAMESPACE_DECL)
26377 if (dwarf_version >= 3 || !dwarf_strict)
26378 imported_die = new_die (DW_TAG_imported_module,
26379 lexical_block_die,
26380 lexical_block);
26381 else
26382 return;
26384 else
26385 imported_die = new_die (DW_TAG_imported_declaration,
26386 lexical_block_die,
26387 lexical_block);
26389 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26390 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26391 if (debug_column_info && xloc.column)
26392 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26393 if (name)
26394 add_AT_string (imported_die, DW_AT_name,
26395 IDENTIFIER_POINTER (name));
26396 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26399 /* Output debug information for imported module or decl DECL.
26400 NAME is non-NULL name in context if the decl has been renamed.
26401 CHILD is true if decl is one of the renamed decls as part of
26402 importing whole module.
26403 IMPLICIT is set if this hook is called for an implicit import
26404 such as inline namespace. */
26406 static void
26407 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26408 bool child, bool implicit)
26410 /* dw_die_ref at_import_die; */
26411 dw_die_ref scope_die;
26413 if (debug_info_level <= DINFO_LEVEL_TERSE)
26414 return;
26416 gcc_assert (decl);
26418 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26419 should be enough, for DWARF4 and older even if we emit as extension
26420 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26421 for the benefit of consumers unaware of DW_AT_export_symbols. */
26422 if (implicit
26423 && dwarf_version >= 5
26424 && lang_hooks.decls.decl_dwarf_attribute (decl,
26425 DW_AT_export_symbols) == 1)
26426 return;
26428 set_early_dwarf s;
26430 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26431 We need decl DIE for reference and scope die. First, get DIE for the decl
26432 itself. */
26434 /* Get the scope die for decl context. Use comp_unit_die for global module
26435 or decl. If die is not found for non globals, force new die. */
26436 if (context
26437 && TYPE_P (context)
26438 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26439 return;
26441 scope_die = get_context_die (context);
26443 if (child)
26445 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26446 there is nothing we can do, here. */
26447 if (dwarf_version < 3 && dwarf_strict)
26448 return;
26450 gcc_assert (scope_die->die_child);
26451 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26452 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26453 scope_die = scope_die->die_child;
26456 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26457 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26460 /* Output debug information for namelists. */
26462 static dw_die_ref
26463 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26465 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26466 tree value;
26467 unsigned i;
26469 if (debug_info_level <= DINFO_LEVEL_TERSE)
26470 return NULL;
26472 gcc_assert (scope_die != NULL);
26473 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26474 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26476 /* If there are no item_decls, we have a nondefining namelist, e.g.
26477 with USE association; hence, set DW_AT_declaration. */
26478 if (item_decls == NULL_TREE)
26480 add_AT_flag (nml_die, DW_AT_declaration, 1);
26481 return nml_die;
26484 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26486 nml_item_ref_die = lookup_decl_die (value);
26487 if (!nml_item_ref_die)
26488 nml_item_ref_die = force_decl_die (value);
26490 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26491 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26493 return nml_die;
26497 /* Write the debugging output for DECL and return the DIE. */
26499 static void
26500 dwarf2out_decl (tree decl)
26502 dw_die_ref context_die = comp_unit_die ();
26504 switch (TREE_CODE (decl))
26506 case ERROR_MARK:
26507 return;
26509 case FUNCTION_DECL:
26510 /* If we're a nested function, initially use a parent of NULL; if we're
26511 a plain function, this will be fixed up in decls_for_scope. If
26512 we're a method, it will be ignored, since we already have a DIE. */
26513 if (decl_function_context (decl)
26514 /* But if we're in terse mode, we don't care about scope. */
26515 && debug_info_level > DINFO_LEVEL_TERSE)
26516 context_die = NULL;
26517 break;
26519 case VAR_DECL:
26520 /* For local statics lookup proper context die. */
26521 if (local_function_static (decl))
26522 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26524 /* If we are in terse mode, don't generate any DIEs to represent any
26525 variable declarations or definitions. */
26526 if (debug_info_level <= DINFO_LEVEL_TERSE)
26527 return;
26528 break;
26530 case CONST_DECL:
26531 if (debug_info_level <= DINFO_LEVEL_TERSE)
26532 return;
26533 if (!is_fortran () && !is_ada ())
26534 return;
26535 if (TREE_STATIC (decl) && decl_function_context (decl))
26536 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26537 break;
26539 case NAMESPACE_DECL:
26540 case IMPORTED_DECL:
26541 if (debug_info_level <= DINFO_LEVEL_TERSE)
26542 return;
26543 if (lookup_decl_die (decl) != NULL)
26544 return;
26545 break;
26547 case TYPE_DECL:
26548 /* Don't emit stubs for types unless they are needed by other DIEs. */
26549 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26550 return;
26552 /* Don't bother trying to generate any DIEs to represent any of the
26553 normal built-in types for the language we are compiling. */
26554 if (DECL_IS_BUILTIN (decl))
26555 return;
26557 /* If we are in terse mode, don't generate any DIEs for types. */
26558 if (debug_info_level <= DINFO_LEVEL_TERSE)
26559 return;
26561 /* If we're a function-scope tag, initially use a parent of NULL;
26562 this will be fixed up in decls_for_scope. */
26563 if (decl_function_context (decl))
26564 context_die = NULL;
26566 break;
26568 case NAMELIST_DECL:
26569 break;
26571 default:
26572 return;
26575 gen_decl_die (decl, NULL, NULL, context_die);
26577 if (flag_checking)
26579 dw_die_ref die = lookup_decl_die (decl);
26580 if (die)
26581 check_die (die);
26585 /* Write the debugging output for DECL. */
26587 static void
26588 dwarf2out_function_decl (tree decl)
26590 dwarf2out_decl (decl);
26591 call_arg_locations = NULL;
26592 call_arg_loc_last = NULL;
26593 call_site_count = -1;
26594 tail_call_site_count = -1;
26595 decl_loc_table->empty ();
26596 cached_dw_loc_list_table->empty ();
26599 /* Output a marker (i.e. a label) for the beginning of the generated code for
26600 a lexical block. */
26602 static void
26603 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26604 unsigned int blocknum)
26606 switch_to_section (current_function_section ());
26607 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26610 /* Output a marker (i.e. a label) for the end of the generated code for a
26611 lexical block. */
26613 static void
26614 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26616 switch_to_section (current_function_section ());
26617 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26620 /* Returns nonzero if it is appropriate not to emit any debugging
26621 information for BLOCK, because it doesn't contain any instructions.
26623 Don't allow this for blocks with nested functions or local classes
26624 as we would end up with orphans, and in the presence of scheduling
26625 we may end up calling them anyway. */
26627 static bool
26628 dwarf2out_ignore_block (const_tree block)
26630 tree decl;
26631 unsigned int i;
26633 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26634 if (TREE_CODE (decl) == FUNCTION_DECL
26635 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26636 return 0;
26637 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26639 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26640 if (TREE_CODE (decl) == FUNCTION_DECL
26641 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26642 return 0;
26645 return 1;
26648 /* Hash table routines for file_hash. */
26650 bool
26651 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26653 return filename_cmp (p1->filename, p2) == 0;
26656 hashval_t
26657 dwarf_file_hasher::hash (dwarf_file_data *p)
26659 return htab_hash_string (p->filename);
26662 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26663 dwarf2out.c) and return its "index". The index of each (known) filename is
26664 just a unique number which is associated with only that one filename. We
26665 need such numbers for the sake of generating labels (in the .debug_sfnames
26666 section) and references to those files numbers (in the .debug_srcinfo
26667 and .debug_macinfo sections). If the filename given as an argument is not
26668 found in our current list, add it to the list and assign it the next
26669 available unique index number. */
26671 static struct dwarf_file_data *
26672 lookup_filename (const char *file_name)
26674 struct dwarf_file_data * created;
26676 if (!file_name)
26677 return NULL;
26679 dwarf_file_data **slot
26680 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26681 INSERT);
26682 if (*slot)
26683 return *slot;
26685 created = ggc_alloc<dwarf_file_data> ();
26686 created->filename = file_name;
26687 created->emitted_number = 0;
26688 *slot = created;
26689 return created;
26692 /* If the assembler will construct the file table, then translate the compiler
26693 internal file table number into the assembler file table number, and emit
26694 a .file directive if we haven't already emitted one yet. The file table
26695 numbers are different because we prune debug info for unused variables and
26696 types, which may include filenames. */
26698 static int
26699 maybe_emit_file (struct dwarf_file_data * fd)
26701 if (! fd->emitted_number)
26703 if (last_emitted_file)
26704 fd->emitted_number = last_emitted_file->emitted_number + 1;
26705 else
26706 fd->emitted_number = 1;
26707 last_emitted_file = fd;
26709 if (output_asm_line_debug_info ())
26711 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26712 output_quoted_string (asm_out_file,
26713 remap_debug_filename (fd->filename));
26714 fputc ('\n', asm_out_file);
26718 return fd->emitted_number;
26721 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26722 That generation should happen after function debug info has been
26723 generated. The value of the attribute is the constant value of ARG. */
26725 static void
26726 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26728 die_arg_entry entry;
26730 if (!die || !arg)
26731 return;
26733 gcc_assert (early_dwarf);
26735 if (!tmpl_value_parm_die_table)
26736 vec_alloc (tmpl_value_parm_die_table, 32);
26738 entry.die = die;
26739 entry.arg = arg;
26740 vec_safe_push (tmpl_value_parm_die_table, entry);
26743 /* Return TRUE if T is an instance of generic type, FALSE
26744 otherwise. */
26746 static bool
26747 generic_type_p (tree t)
26749 if (t == NULL_TREE || !TYPE_P (t))
26750 return false;
26751 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26754 /* Schedule the generation of the generic parameter dies for the
26755 instance of generic type T. The proper generation itself is later
26756 done by gen_scheduled_generic_parms_dies. */
26758 static void
26759 schedule_generic_params_dies_gen (tree t)
26761 if (!generic_type_p (t))
26762 return;
26764 gcc_assert (early_dwarf);
26766 if (!generic_type_instances)
26767 vec_alloc (generic_type_instances, 256);
26769 vec_safe_push (generic_type_instances, t);
26772 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26773 by append_entry_to_tmpl_value_parm_die_table. This function must
26774 be called after function DIEs have been generated. */
26776 static void
26777 gen_remaining_tmpl_value_param_die_attribute (void)
26779 if (tmpl_value_parm_die_table)
26781 unsigned i, j;
26782 die_arg_entry *e;
26784 /* We do this in two phases - first get the cases we can
26785 handle during early-finish, preserving those we cannot
26786 (containing symbolic constants where we don't yet know
26787 whether we are going to output the referenced symbols).
26788 For those we try again at late-finish. */
26789 j = 0;
26790 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26792 if (!e->die->removed
26793 && !tree_add_const_value_attribute (e->die, e->arg))
26795 dw_loc_descr_ref loc = NULL;
26796 if (! early_dwarf
26797 && (dwarf_version >= 5 || !dwarf_strict))
26798 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26799 if (loc)
26800 add_AT_loc (e->die, DW_AT_location, loc);
26801 else
26802 (*tmpl_value_parm_die_table)[j++] = *e;
26805 tmpl_value_parm_die_table->truncate (j);
26809 /* Generate generic parameters DIEs for instances of generic types
26810 that have been previously scheduled by
26811 schedule_generic_params_dies_gen. This function must be called
26812 after all the types of the CU have been laid out. */
26814 static void
26815 gen_scheduled_generic_parms_dies (void)
26817 unsigned i;
26818 tree t;
26820 if (!generic_type_instances)
26821 return;
26823 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26824 if (COMPLETE_TYPE_P (t))
26825 gen_generic_params_dies (t);
26827 generic_type_instances = NULL;
26831 /* Replace DW_AT_name for the decl with name. */
26833 static void
26834 dwarf2out_set_name (tree decl, tree name)
26836 dw_die_ref die;
26837 dw_attr_node *attr;
26838 const char *dname;
26840 die = TYPE_SYMTAB_DIE (decl);
26841 if (!die)
26842 return;
26844 dname = dwarf2_name (name, 0);
26845 if (!dname)
26846 return;
26848 attr = get_AT (die, DW_AT_name);
26849 if (attr)
26851 struct indirect_string_node *node;
26853 node = find_AT_string (dname);
26854 /* replace the string. */
26855 attr->dw_attr_val.v.val_str = node;
26858 else
26859 add_name_attribute (die, dname);
26862 /* True if before or during processing of the first function being emitted. */
26863 static bool in_first_function_p = true;
26864 /* True if loc_note during dwarf2out_var_location call might still be
26865 before first real instruction at address equal to .Ltext0. */
26866 static bool maybe_at_text_label_p = true;
26867 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26868 static unsigned int first_loclabel_num_not_at_text_label;
26870 /* Look ahead for a real insn, or for a begin stmt marker. */
26872 static rtx_insn *
26873 dwarf2out_next_real_insn (rtx_insn *loc_note)
26875 rtx_insn *next_real = NEXT_INSN (loc_note);
26877 while (next_real)
26878 if (INSN_P (next_real))
26879 break;
26880 else
26881 next_real = NEXT_INSN (next_real);
26883 return next_real;
26886 /* Called by the final INSN scan whenever we see a var location. We
26887 use it to drop labels in the right places, and throw the location in
26888 our lookup table. */
26890 static void
26891 dwarf2out_var_location (rtx_insn *loc_note)
26893 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26894 struct var_loc_node *newloc;
26895 rtx_insn *next_real, *next_note;
26896 rtx_insn *call_insn = NULL;
26897 static const char *last_label;
26898 static const char *last_postcall_label;
26899 static bool last_in_cold_section_p;
26900 static rtx_insn *expected_next_loc_note;
26901 tree decl;
26902 bool var_loc_p;
26903 var_loc_view view = 0;
26905 if (!NOTE_P (loc_note))
26907 if (CALL_P (loc_note))
26909 RESET_NEXT_VIEW (cur_line_info_table->view);
26910 call_site_count++;
26911 if (SIBLING_CALL_P (loc_note))
26912 tail_call_site_count++;
26913 if (optimize == 0 && !flag_var_tracking)
26915 /* When the var-tracking pass is not running, there is no note
26916 for indirect calls whose target is compile-time known. In this
26917 case, process such calls specifically so that we generate call
26918 sites for them anyway. */
26919 rtx x = PATTERN (loc_note);
26920 if (GET_CODE (x) == PARALLEL)
26921 x = XVECEXP (x, 0, 0);
26922 if (GET_CODE (x) == SET)
26923 x = SET_SRC (x);
26924 if (GET_CODE (x) == CALL)
26925 x = XEXP (x, 0);
26926 if (!MEM_P (x)
26927 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26928 || !SYMBOL_REF_DECL (XEXP (x, 0))
26929 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26930 != FUNCTION_DECL))
26932 call_insn = loc_note;
26933 loc_note = NULL;
26934 var_loc_p = false;
26936 next_real = dwarf2out_next_real_insn (call_insn);
26937 next_note = NULL;
26938 cached_next_real_insn = NULL;
26939 goto create_label;
26943 else if (!debug_variable_location_views)
26944 gcc_unreachable ();
26945 else if (JUMP_TABLE_DATA_P (loc_note))
26946 RESET_NEXT_VIEW (cur_line_info_table->view);
26947 else if (GET_CODE (loc_note) == USE
26948 || GET_CODE (loc_note) == CLOBBER
26949 || GET_CODE (loc_note) == ASM_INPUT
26950 || asm_noperands (loc_note) >= 0)
26952 else if (get_attr_min_length (loc_note) > 0)
26953 RESET_NEXT_VIEW (cur_line_info_table->view);
26955 return;
26958 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26959 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26960 return;
26962 /* Optimize processing a large consecutive sequence of location
26963 notes so we don't spend too much time in next_real_insn. If the
26964 next insn is another location note, remember the next_real_insn
26965 calculation for next time. */
26966 next_real = cached_next_real_insn;
26967 if (next_real)
26969 if (expected_next_loc_note != loc_note)
26970 next_real = NULL;
26973 next_note = NEXT_INSN (loc_note);
26974 if (! next_note
26975 || next_note->deleted ()
26976 || ! NOTE_P (next_note)
26977 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26978 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
26979 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY
26980 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26981 next_note = NULL;
26983 if (! next_real)
26984 next_real = dwarf2out_next_real_insn (loc_note);
26986 if (next_note)
26988 expected_next_loc_note = next_note;
26989 cached_next_real_insn = next_real;
26991 else
26992 cached_next_real_insn = NULL;
26994 /* If there are no instructions which would be affected by this note,
26995 don't do anything. */
26996 if (var_loc_p
26997 && next_real == NULL_RTX
26998 && !NOTE_DURING_CALL_P (loc_note))
26999 return;
27001 create_label:
27003 if (next_real == NULL_RTX)
27004 next_real = get_last_insn ();
27006 /* If there were any real insns between note we processed last time
27007 and this note (or if it is the first note), clear
27008 last_{,postcall_}label so that they are not reused this time. */
27009 if (last_var_location_insn == NULL_RTX
27010 || last_var_location_insn != next_real
27011 || last_in_cold_section_p != in_cold_section_p)
27013 last_label = NULL;
27014 last_postcall_label = NULL;
27017 if (var_loc_p)
27019 const char *label
27020 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27021 view = cur_line_info_table->view;
27022 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27023 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27024 if (newloc == NULL)
27025 return;
27027 else
27029 decl = NULL_TREE;
27030 newloc = NULL;
27033 /* If there were no real insns between note we processed last time
27034 and this note, use the label we emitted last time. Otherwise
27035 create a new label and emit it. */
27036 if (last_label == NULL)
27038 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27039 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27040 loclabel_num++;
27041 last_label = ggc_strdup (loclabel);
27042 /* See if loclabel might be equal to .Ltext0. If yes,
27043 bump first_loclabel_num_not_at_text_label. */
27044 if (!have_multiple_function_sections
27045 && in_first_function_p
27046 && maybe_at_text_label_p)
27048 static rtx_insn *last_start;
27049 rtx_insn *insn;
27050 for (insn = loc_note; insn; insn = previous_insn (insn))
27051 if (insn == last_start)
27052 break;
27053 else if (!NONDEBUG_INSN_P (insn))
27054 continue;
27055 else
27057 rtx body = PATTERN (insn);
27058 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27059 continue;
27060 /* Inline asm could occupy zero bytes. */
27061 else if (GET_CODE (body) == ASM_INPUT
27062 || asm_noperands (body) >= 0)
27063 continue;
27064 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27065 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27066 continue;
27067 #endif
27068 else
27070 /* Assume insn has non-zero length. */
27071 maybe_at_text_label_p = false;
27072 break;
27075 if (maybe_at_text_label_p)
27077 last_start = loc_note;
27078 first_loclabel_num_not_at_text_label = loclabel_num;
27083 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27084 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27086 if (!var_loc_p)
27088 struct call_arg_loc_node *ca_loc
27089 = ggc_cleared_alloc<call_arg_loc_node> ();
27090 rtx_insn *prev
27091 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
27093 ca_loc->call_arg_loc_note = loc_note;
27094 ca_loc->next = NULL;
27095 ca_loc->label = last_label;
27096 gcc_assert (prev
27097 && (CALL_P (prev)
27098 || (NONJUMP_INSN_P (prev)
27099 && GET_CODE (PATTERN (prev)) == SEQUENCE
27100 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27101 if (!CALL_P (prev))
27102 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27103 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27105 /* Look for a SYMBOL_REF in the "prev" instruction. */
27106 rtx x = get_call_rtx_from (PATTERN (prev));
27107 if (x)
27109 /* Try to get the call symbol, if any. */
27110 if (MEM_P (XEXP (x, 0)))
27111 x = XEXP (x, 0);
27112 /* First, look for a memory access to a symbol_ref. */
27113 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27114 && SYMBOL_REF_DECL (XEXP (x, 0))
27115 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27116 ca_loc->symbol_ref = XEXP (x, 0);
27117 /* Otherwise, look at a compile-time known user-level function
27118 declaration. */
27119 else if (MEM_P (x)
27120 && MEM_EXPR (x)
27121 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27122 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27125 ca_loc->block = insn_scope (prev);
27126 if (call_arg_locations)
27127 call_arg_loc_last->next = ca_loc;
27128 else
27129 call_arg_locations = ca_loc;
27130 call_arg_loc_last = ca_loc;
27132 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27134 newloc->label = last_label;
27135 newloc->view = view;
27137 else
27139 if (!last_postcall_label)
27141 sprintf (loclabel, "%s-1", last_label);
27142 last_postcall_label = ggc_strdup (loclabel);
27144 newloc->label = last_postcall_label;
27145 newloc->view = view;
27148 if (var_loc_p && flag_debug_asm)
27150 const char *name, *sep, *patstr;
27151 if (decl && DECL_NAME (decl))
27152 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27153 else
27154 name = "";
27155 if (NOTE_VAR_LOCATION_LOC (loc_note))
27157 sep = " => ";
27158 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27160 else
27162 sep = " ";
27163 patstr = "RESET";
27165 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27166 name, sep, patstr);
27169 last_var_location_insn = next_real;
27170 last_in_cold_section_p = in_cold_section_p;
27173 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27174 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27175 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27176 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27177 BLOCK_FRAGMENT_ORIGIN links. */
27178 static bool
27179 block_within_block_p (tree block, tree outer, bool bothways)
27181 if (block == outer)
27182 return true;
27184 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27185 for (tree context = BLOCK_SUPERCONTEXT (block);
27186 context != outer;
27187 context = BLOCK_SUPERCONTEXT (context))
27188 if (!context || TREE_CODE (context) != BLOCK)
27189 return false;
27191 if (!bothways)
27192 return true;
27194 /* Now check that each block is actually referenced by its
27195 parent. */
27196 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27197 context = BLOCK_SUPERCONTEXT (context))
27199 if (BLOCK_FRAGMENT_ORIGIN (context))
27201 gcc_assert (!BLOCK_SUBBLOCKS (context));
27202 context = BLOCK_FRAGMENT_ORIGIN (context);
27204 for (tree sub = BLOCK_SUBBLOCKS (context);
27205 sub != block;
27206 sub = BLOCK_CHAIN (sub))
27207 if (!sub)
27208 return false;
27209 if (context == outer)
27210 return true;
27211 else
27212 block = context;
27216 /* Called during final while assembling the marker of the entry point
27217 for an inlined function. */
27219 static void
27220 dwarf2out_inline_entry (tree block)
27222 /* If we can't represent it, don't bother. */
27223 if (!(dwarf_version >= 3 || !dwarf_strict))
27224 return;
27226 gcc_assert (DECL_P (block_ultimate_origin (block)));
27228 /* Sanity check the block tree. This would catch a case in which
27229 BLOCK got removed from the tree reachable from the outermost
27230 lexical block, but got retained in markers. It would still link
27231 back to its parents, but some ancestor would be missing a link
27232 down the path to the sub BLOCK. If the block got removed, its
27233 BLOCK_NUMBER will not be a usable value. */
27234 if (flag_checking)
27235 gcc_assert (block_within_block_p (block,
27236 DECL_INITIAL (current_function_decl),
27237 true));
27239 gcc_assert (inlined_function_outer_scope_p (block));
27240 gcc_assert (!BLOCK_DIE (block));
27242 if (BLOCK_FRAGMENT_ORIGIN (block))
27243 block = BLOCK_FRAGMENT_ORIGIN (block);
27244 /* Can the entry point ever not be at the beginning of an
27245 unfragmented lexical block? */
27246 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27247 || (cur_line_info_table
27248 && !ZERO_VIEW_P (cur_line_info_table->view))))
27249 return;
27251 if (!inline_entry_data_table)
27252 inline_entry_data_table
27253 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27256 inline_entry_data **iedp
27257 = inline_entry_data_table->find_slot_with_hash (block,
27258 htab_hash_pointer (block),
27259 INSERT);
27260 if (*iedp)
27261 /* ??? Ideally, we'd record all entry points for the same inlined
27262 function (some may have been duplicated by e.g. unrolling), but
27263 we have no way to represent that ATM. */
27264 return;
27266 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27267 ied->block = block;
27268 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27269 ied->label_num = BLOCK_NUMBER (block);
27270 if (cur_line_info_table)
27271 ied->view = cur_line_info_table->view;
27273 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27275 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27276 BLOCK_NUMBER (block));
27277 ASM_OUTPUT_LABEL (asm_out_file, label);
27280 /* Called from finalize_size_functions for size functions so that their body
27281 can be encoded in the debug info to describe the layout of variable-length
27282 structures. */
27284 static void
27285 dwarf2out_size_function (tree decl)
27287 function_to_dwarf_procedure (decl);
27290 /* Note in one location list that text section has changed. */
27293 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27295 var_loc_list *list = *slot;
27296 if (list->first)
27297 list->last_before_switch
27298 = list->last->next ? list->last->next : list->last;
27299 return 1;
27302 /* Note in all location lists that text section has changed. */
27304 static void
27305 var_location_switch_text_section (void)
27307 if (decl_loc_table == NULL)
27308 return;
27310 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27313 /* Create a new line number table. */
27315 static dw_line_info_table *
27316 new_line_info_table (void)
27318 dw_line_info_table *table;
27320 table = ggc_cleared_alloc<dw_line_info_table> ();
27321 table->file_num = 1;
27322 table->line_num = 1;
27323 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27324 RESET_NEXT_VIEW (table->view);
27326 return table;
27329 /* Lookup the "current" table into which we emit line info, so
27330 that we don't have to do it for every source line. */
27332 static void
27333 set_cur_line_info_table (section *sec)
27335 dw_line_info_table *table;
27337 if (sec == text_section)
27338 table = text_section_line_info;
27339 else if (sec == cold_text_section)
27341 table = cold_text_section_line_info;
27342 if (!table)
27344 cold_text_section_line_info = table = new_line_info_table ();
27345 table->end_label = cold_end_label;
27348 else
27350 const char *end_label;
27352 if (crtl->has_bb_partition)
27354 if (in_cold_section_p)
27355 end_label = crtl->subsections.cold_section_end_label;
27356 else
27357 end_label = crtl->subsections.hot_section_end_label;
27359 else
27361 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27362 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27363 current_function_funcdef_no);
27364 end_label = ggc_strdup (label);
27367 table = new_line_info_table ();
27368 table->end_label = end_label;
27370 vec_safe_push (separate_line_info, table);
27373 if (output_asm_line_debug_info ())
27374 table->is_stmt = (cur_line_info_table
27375 ? cur_line_info_table->is_stmt
27376 : DWARF_LINE_DEFAULT_IS_STMT_START);
27377 cur_line_info_table = table;
27381 /* We need to reset the locations at the beginning of each
27382 function. We can't do this in the end_function hook, because the
27383 declarations that use the locations won't have been output when
27384 that hook is called. Also compute have_multiple_function_sections here. */
27386 static void
27387 dwarf2out_begin_function (tree fun)
27389 section *sec = function_section (fun);
27391 if (sec != text_section)
27392 have_multiple_function_sections = true;
27394 if (crtl->has_bb_partition && !cold_text_section)
27396 gcc_assert (current_function_decl == fun);
27397 cold_text_section = unlikely_text_section ();
27398 switch_to_section (cold_text_section);
27399 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27400 switch_to_section (sec);
27403 dwarf2out_note_section_used ();
27404 call_site_count = 0;
27405 tail_call_site_count = 0;
27407 set_cur_line_info_table (sec);
27410 /* Helper function of dwarf2out_end_function, called only after emitting
27411 the very first function into assembly. Check if some .debug_loc range
27412 might end with a .LVL* label that could be equal to .Ltext0.
27413 In that case we must force using absolute addresses in .debug_loc ranges,
27414 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27415 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27416 list terminator.
27417 Set have_multiple_function_sections to true in that case and
27418 terminate htab traversal. */
27421 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27423 var_loc_list *entry = *slot;
27424 struct var_loc_node *node;
27426 node = entry->first;
27427 if (node && node->next && node->next->label)
27429 unsigned int i;
27430 const char *label = node->next->label;
27431 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27433 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27435 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27436 if (strcmp (label, loclabel) == 0)
27438 have_multiple_function_sections = true;
27439 return 0;
27443 return 1;
27446 /* Hook called after emitting a function into assembly.
27447 This does something only for the very first function emitted. */
27449 static void
27450 dwarf2out_end_function (unsigned int)
27452 if (in_first_function_p
27453 && !have_multiple_function_sections
27454 && first_loclabel_num_not_at_text_label
27455 && decl_loc_table)
27456 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27457 in_first_function_p = false;
27458 maybe_at_text_label_p = false;
27461 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27462 front-ends register a translation unit even before dwarf2out_init is
27463 called. */
27464 static tree main_translation_unit = NULL_TREE;
27466 /* Hook called by front-ends after they built their main translation unit.
27467 Associate comp_unit_die to UNIT. */
27469 static void
27470 dwarf2out_register_main_translation_unit (tree unit)
27472 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27473 && main_translation_unit == NULL_TREE);
27474 main_translation_unit = unit;
27475 /* If dwarf2out_init has not been called yet, it will perform the association
27476 itself looking at main_translation_unit. */
27477 if (decl_die_table != NULL)
27478 equate_decl_number_to_die (unit, comp_unit_die ());
27481 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27483 static void
27484 push_dw_line_info_entry (dw_line_info_table *table,
27485 enum dw_line_info_opcode opcode, unsigned int val)
27487 dw_line_info_entry e;
27488 e.opcode = opcode;
27489 e.val = val;
27490 vec_safe_push (table->entries, e);
27493 /* Output a label to mark the beginning of a source code line entry
27494 and record information relating to this source line, in
27495 'line_info_table' for later output of the .debug_line section. */
27496 /* ??? The discriminator parameter ought to be unsigned. */
27498 static void
27499 dwarf2out_source_line (unsigned int line, unsigned int column,
27500 const char *filename,
27501 int discriminator, bool is_stmt)
27503 unsigned int file_num;
27504 dw_line_info_table *table;
27506 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
27507 return;
27509 /* The discriminator column was added in dwarf4. Simplify the below
27510 by simply removing it if we're not supposed to output it. */
27511 if (dwarf_version < 4 && dwarf_strict)
27512 discriminator = 0;
27514 if (!debug_column_info)
27515 column = 0;
27517 table = cur_line_info_table;
27518 file_num = maybe_emit_file (lookup_filename (filename));
27520 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27521 the debugger has used the second (possibly duplicate) line number
27522 at the beginning of the function to mark the end of the prologue.
27523 We could eliminate any other duplicates within the function. For
27524 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27525 that second line number entry. */
27526 /* Recall that this end-of-prologue indication is *not* the same thing
27527 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27528 to which the hook corresponds, follows the last insn that was
27529 emitted by gen_prologue. What we need is to precede the first insn
27530 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27531 insn that corresponds to something the user wrote. These may be
27532 very different locations once scheduling is enabled. */
27534 if (0 && file_num == table->file_num
27535 && line == table->line_num
27536 && column == table->column_num
27537 && discriminator == table->discrim_num
27538 && is_stmt == table->is_stmt)
27539 return;
27541 switch_to_section (current_function_section ());
27543 /* If requested, emit something human-readable. */
27544 if (flag_debug_asm)
27546 if (debug_column_info)
27547 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27548 filename, line, column);
27549 else
27550 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27551 filename, line);
27554 if (output_asm_line_debug_info ())
27556 /* Emit the .loc directive understood by GNU as. */
27557 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27558 file_num, line, is_stmt, discriminator */
27559 fputs ("\t.loc ", asm_out_file);
27560 fprint_ul (asm_out_file, file_num);
27561 putc (' ', asm_out_file);
27562 fprint_ul (asm_out_file, line);
27563 putc (' ', asm_out_file);
27564 fprint_ul (asm_out_file, column);
27566 if (is_stmt != table->is_stmt)
27568 fputs (" is_stmt ", asm_out_file);
27569 putc (is_stmt ? '1' : '0', asm_out_file);
27571 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27573 gcc_assert (discriminator > 0);
27574 fputs (" discriminator ", asm_out_file);
27575 fprint_ul (asm_out_file, (unsigned long) discriminator);
27577 if (debug_variable_location_views)
27579 static var_loc_view lvugid;
27580 if (!lvugid)
27582 gcc_assert (!zero_view_p);
27583 zero_view_p = BITMAP_GGC_ALLOC ();
27584 bitmap_set_bit (zero_view_p, 0);
27586 if (!RESETTING_VIEW_P (table->view))
27588 /* When we're using the assembler to compute view
27589 numbers, we output symbolic labels after "view" in
27590 .loc directives, and the assembler will set them for
27591 us, so that we can refer to the view numbers in
27592 location lists. The only exceptions are when we know
27593 a view will be zero: "-0" is a forced reset, used
27594 e.g. in the beginning of functions, whereas "0" tells
27595 the assembler to check that there was a PC change
27596 since the previous view, in a way that implicitly
27597 resets the next view. */
27598 fputs (" view ", asm_out_file);
27599 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27600 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27601 assemble_name (asm_out_file, label);
27602 table->view = ++lvugid;
27604 else
27606 if (!table->in_use)
27607 fputs (" view -0", asm_out_file);
27608 else
27609 fputs (" view 0", asm_out_file);
27610 /* Mark the present view as a zero view. Earlier debug
27611 binds may have already added its id to loclists to be
27612 emitted later, so we can't reuse the id for something
27613 else. However, it's good to know whether a view is
27614 known to be zero, because then we may be able to
27615 optimize out locviews that are all zeros, so take
27616 note of it in zero_view_p. */
27617 bitmap_set_bit (zero_view_p, lvugid);
27618 table->view = ++lvugid;
27621 putc ('\n', asm_out_file);
27623 else
27625 unsigned int label_num = ++line_info_label_num;
27627 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27629 if (debug_variable_location_views && table->view)
27630 push_dw_line_info_entry (table, LI_adv_address, label_num);
27631 else
27632 push_dw_line_info_entry (table, LI_set_address, label_num);
27633 if (debug_variable_location_views)
27635 if (flag_debug_asm)
27636 fprintf (asm_out_file, "\t%s view %s%d\n",
27637 ASM_COMMENT_START,
27638 table->in_use ? "" : "-",
27639 table->view);
27640 table->view++;
27642 if (file_num != table->file_num)
27643 push_dw_line_info_entry (table, LI_set_file, file_num);
27644 if (discriminator != table->discrim_num)
27645 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27646 if (is_stmt != table->is_stmt)
27647 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27648 push_dw_line_info_entry (table, LI_set_line, line);
27649 if (debug_column_info)
27650 push_dw_line_info_entry (table, LI_set_column, column);
27653 table->file_num = file_num;
27654 table->line_num = line;
27655 table->column_num = column;
27656 table->discrim_num = discriminator;
27657 table->is_stmt = is_stmt;
27658 table->in_use = true;
27661 /* Record the beginning of a new source file. */
27663 static void
27664 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27666 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27668 macinfo_entry e;
27669 e.code = DW_MACINFO_start_file;
27670 e.lineno = lineno;
27671 e.info = ggc_strdup (filename);
27672 vec_safe_push (macinfo_table, e);
27676 /* Record the end of a source file. */
27678 static void
27679 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27681 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27683 macinfo_entry e;
27684 e.code = DW_MACINFO_end_file;
27685 e.lineno = lineno;
27686 e.info = NULL;
27687 vec_safe_push (macinfo_table, e);
27691 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27692 the tail part of the directive line, i.e. the part which is past the
27693 initial whitespace, #, whitespace, directive-name, whitespace part. */
27695 static void
27696 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27697 const char *buffer ATTRIBUTE_UNUSED)
27699 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27701 macinfo_entry e;
27702 /* Insert a dummy first entry to be able to optimize the whole
27703 predefined macro block using DW_MACRO_import. */
27704 if (macinfo_table->is_empty () && lineno <= 1)
27706 e.code = 0;
27707 e.lineno = 0;
27708 e.info = NULL;
27709 vec_safe_push (macinfo_table, e);
27711 e.code = DW_MACINFO_define;
27712 e.lineno = lineno;
27713 e.info = ggc_strdup (buffer);
27714 vec_safe_push (macinfo_table, e);
27718 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27719 the tail part of the directive line, i.e. the part which is past the
27720 initial whitespace, #, whitespace, directive-name, whitespace part. */
27722 static void
27723 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27724 const char *buffer ATTRIBUTE_UNUSED)
27726 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27728 macinfo_entry e;
27729 /* Insert a dummy first entry to be able to optimize the whole
27730 predefined macro block using DW_MACRO_import. */
27731 if (macinfo_table->is_empty () && lineno <= 1)
27733 e.code = 0;
27734 e.lineno = 0;
27735 e.info = NULL;
27736 vec_safe_push (macinfo_table, e);
27738 e.code = DW_MACINFO_undef;
27739 e.lineno = lineno;
27740 e.info = ggc_strdup (buffer);
27741 vec_safe_push (macinfo_table, e);
27745 /* Helpers to manipulate hash table of CUs. */
27747 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27749 static inline hashval_t hash (const macinfo_entry *);
27750 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27753 inline hashval_t
27754 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27756 return htab_hash_string (entry->info);
27759 inline bool
27760 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
27761 const macinfo_entry *entry2)
27763 return !strcmp (entry1->info, entry2->info);
27766 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
27768 /* Output a single .debug_macinfo entry. */
27770 static void
27771 output_macinfo_op (macinfo_entry *ref)
27773 int file_num;
27774 size_t len;
27775 struct indirect_string_node *node;
27776 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27777 struct dwarf_file_data *fd;
27779 switch (ref->code)
27781 case DW_MACINFO_start_file:
27782 fd = lookup_filename (ref->info);
27783 file_num = maybe_emit_file (fd);
27784 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27785 dw2_asm_output_data_uleb128 (ref->lineno,
27786 "Included from line number %lu",
27787 (unsigned long) ref->lineno);
27788 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27789 break;
27790 case DW_MACINFO_end_file:
27791 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27792 break;
27793 case DW_MACINFO_define:
27794 case DW_MACINFO_undef:
27795 len = strlen (ref->info) + 1;
27796 if (!dwarf_strict
27797 && len > DWARF_OFFSET_SIZE
27798 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27799 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27801 ref->code = ref->code == DW_MACINFO_define
27802 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27803 output_macinfo_op (ref);
27804 return;
27806 dw2_asm_output_data (1, ref->code,
27807 ref->code == DW_MACINFO_define
27808 ? "Define macro" : "Undefine macro");
27809 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27810 (unsigned long) ref->lineno);
27811 dw2_asm_output_nstring (ref->info, -1, "The macro");
27812 break;
27813 case DW_MACRO_define_strp:
27814 case DW_MACRO_undef_strp:
27815 node = find_AT_string (ref->info);
27816 gcc_assert (node
27817 && (node->form == DW_FORM_strp
27818 || node->form == DW_FORM_GNU_str_index));
27819 dw2_asm_output_data (1, ref->code,
27820 ref->code == DW_MACRO_define_strp
27821 ? "Define macro strp"
27822 : "Undefine macro strp");
27823 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27824 (unsigned long) ref->lineno);
27825 if (node->form == DW_FORM_strp)
27826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27827 debug_str_section, "The macro: \"%s\"",
27828 ref->info);
27829 else
27830 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27831 ref->info);
27832 break;
27833 case DW_MACRO_import:
27834 dw2_asm_output_data (1, ref->code, "Import");
27835 ASM_GENERATE_INTERNAL_LABEL (label,
27836 DEBUG_MACRO_SECTION_LABEL,
27837 ref->lineno + macinfo_label_base);
27838 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27839 break;
27840 default:
27841 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27842 ASM_COMMENT_START, (unsigned long) ref->code);
27843 break;
27847 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27848 other compilation unit .debug_macinfo sections. IDX is the first
27849 index of a define/undef, return the number of ops that should be
27850 emitted in a comdat .debug_macinfo section and emit
27851 a DW_MACRO_import entry referencing it.
27852 If the define/undef entry should be emitted normally, return 0. */
27854 static unsigned
27855 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27856 macinfo_hash_type **macinfo_htab)
27858 macinfo_entry *first, *second, *cur, *inc;
27859 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27860 unsigned char checksum[16];
27861 struct md5_ctx ctx;
27862 char *grp_name, *tail;
27863 const char *base;
27864 unsigned int i, count, encoded_filename_len, linebuf_len;
27865 macinfo_entry **slot;
27867 first = &(*macinfo_table)[idx];
27868 second = &(*macinfo_table)[idx + 1];
27870 /* Optimize only if there are at least two consecutive define/undef ops,
27871 and either all of them are before first DW_MACINFO_start_file
27872 with lineno {0,1} (i.e. predefined macro block), or all of them are
27873 in some included header file. */
27874 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27875 return 0;
27876 if (vec_safe_is_empty (files))
27878 if (first->lineno > 1 || second->lineno > 1)
27879 return 0;
27881 else if (first->lineno == 0)
27882 return 0;
27884 /* Find the last define/undef entry that can be grouped together
27885 with first and at the same time compute md5 checksum of their
27886 codes, linenumbers and strings. */
27887 md5_init_ctx (&ctx);
27888 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27889 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27890 break;
27891 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27892 break;
27893 else
27895 unsigned char code = cur->code;
27896 md5_process_bytes (&code, 1, &ctx);
27897 checksum_uleb128 (cur->lineno, &ctx);
27898 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27900 md5_finish_ctx (&ctx, checksum);
27901 count = i - idx;
27903 /* From the containing include filename (if any) pick up just
27904 usable characters from its basename. */
27905 if (vec_safe_is_empty (files))
27906 base = "";
27907 else
27908 base = lbasename (files->last ().info);
27909 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27910 if (ISIDNUM (base[i]) || base[i] == '.')
27911 encoded_filename_len++;
27912 /* Count . at the end. */
27913 if (encoded_filename_len)
27914 encoded_filename_len++;
27916 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27917 linebuf_len = strlen (linebuf);
27919 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27920 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27921 + 16 * 2 + 1);
27922 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27923 tail = grp_name + 4;
27924 if (encoded_filename_len)
27926 for (i = 0; base[i]; i++)
27927 if (ISIDNUM (base[i]) || base[i] == '.')
27928 *tail++ = base[i];
27929 *tail++ = '.';
27931 memcpy (tail, linebuf, linebuf_len);
27932 tail += linebuf_len;
27933 *tail++ = '.';
27934 for (i = 0; i < 16; i++)
27935 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27937 /* Construct a macinfo_entry for DW_MACRO_import
27938 in the empty vector entry before the first define/undef. */
27939 inc = &(*macinfo_table)[idx - 1];
27940 inc->code = DW_MACRO_import;
27941 inc->lineno = 0;
27942 inc->info = ggc_strdup (grp_name);
27943 if (!*macinfo_htab)
27944 *macinfo_htab = new macinfo_hash_type (10);
27945 /* Avoid emitting duplicates. */
27946 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27947 if (*slot != NULL)
27949 inc->code = 0;
27950 inc->info = NULL;
27951 /* If such an entry has been used before, just emit
27952 a DW_MACRO_import op. */
27953 inc = *slot;
27954 output_macinfo_op (inc);
27955 /* And clear all macinfo_entry in the range to avoid emitting them
27956 in the second pass. */
27957 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27959 cur->code = 0;
27960 cur->info = NULL;
27963 else
27965 *slot = inc;
27966 inc->lineno = (*macinfo_htab)->elements ();
27967 output_macinfo_op (inc);
27969 return count;
27972 /* Save any strings needed by the macinfo table in the debug str
27973 table. All strings must be collected into the table by the time
27974 index_string is called. */
27976 static void
27977 save_macinfo_strings (void)
27979 unsigned len;
27980 unsigned i;
27981 macinfo_entry *ref;
27983 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27985 switch (ref->code)
27987 /* Match the logic in output_macinfo_op to decide on
27988 indirect strings. */
27989 case DW_MACINFO_define:
27990 case DW_MACINFO_undef:
27991 len = strlen (ref->info) + 1;
27992 if (!dwarf_strict
27993 && len > DWARF_OFFSET_SIZE
27994 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27995 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27996 set_indirect_string (find_AT_string (ref->info));
27997 break;
27998 case DW_MACRO_define_strp:
27999 case DW_MACRO_undef_strp:
28000 set_indirect_string (find_AT_string (ref->info));
28001 break;
28002 default:
28003 break;
28008 /* Output macinfo section(s). */
28010 static void
28011 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28013 unsigned i;
28014 unsigned long length = vec_safe_length (macinfo_table);
28015 macinfo_entry *ref;
28016 vec<macinfo_entry, va_gc> *files = NULL;
28017 macinfo_hash_type *macinfo_htab = NULL;
28018 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28020 if (! length)
28021 return;
28023 /* output_macinfo* uses these interchangeably. */
28024 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28025 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28026 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28027 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28029 /* AIX Assembler inserts the length, so adjust the reference to match the
28030 offset expected by debuggers. */
28031 strcpy (dl_section_ref, debug_line_label);
28032 if (XCOFF_DEBUGGING_INFO)
28033 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28035 /* For .debug_macro emit the section header. */
28036 if (!dwarf_strict || dwarf_version >= 5)
28038 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28039 "DWARF macro version number");
28040 if (DWARF_OFFSET_SIZE == 8)
28041 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28042 else
28043 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28044 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28045 debug_line_section, NULL);
28048 /* In the first loop, it emits the primary .debug_macinfo section
28049 and after each emitted op the macinfo_entry is cleared.
28050 If a longer range of define/undef ops can be optimized using
28051 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28052 the vector before the first define/undef in the range and the
28053 whole range of define/undef ops is not emitted and kept. */
28054 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28056 switch (ref->code)
28058 case DW_MACINFO_start_file:
28059 vec_safe_push (files, *ref);
28060 break;
28061 case DW_MACINFO_end_file:
28062 if (!vec_safe_is_empty (files))
28063 files->pop ();
28064 break;
28065 case DW_MACINFO_define:
28066 case DW_MACINFO_undef:
28067 if ((!dwarf_strict || dwarf_version >= 5)
28068 && HAVE_COMDAT_GROUP
28069 && vec_safe_length (files) != 1
28070 && i > 0
28071 && i + 1 < length
28072 && (*macinfo_table)[i - 1].code == 0)
28074 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28075 if (count)
28077 i += count - 1;
28078 continue;
28081 break;
28082 case 0:
28083 /* A dummy entry may be inserted at the beginning to be able
28084 to optimize the whole block of predefined macros. */
28085 if (i == 0)
28086 continue;
28087 default:
28088 break;
28090 output_macinfo_op (ref);
28091 ref->info = NULL;
28092 ref->code = 0;
28095 if (!macinfo_htab)
28096 return;
28098 /* Save the number of transparent includes so we can adjust the
28099 label number for the fat LTO object DWARF. */
28100 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28102 delete macinfo_htab;
28103 macinfo_htab = NULL;
28105 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28106 terminate the current chain and switch to a new comdat .debug_macinfo
28107 section and emit the define/undef entries within it. */
28108 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28109 switch (ref->code)
28111 case 0:
28112 continue;
28113 case DW_MACRO_import:
28115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28116 tree comdat_key = get_identifier (ref->info);
28117 /* Terminate the previous .debug_macinfo section. */
28118 dw2_asm_output_data (1, 0, "End compilation unit");
28119 targetm.asm_out.named_section (debug_macinfo_section_name,
28120 SECTION_DEBUG
28121 | SECTION_LINKONCE
28122 | (early_lto_debug
28123 ? SECTION_EXCLUDE : 0),
28124 comdat_key);
28125 ASM_GENERATE_INTERNAL_LABEL (label,
28126 DEBUG_MACRO_SECTION_LABEL,
28127 ref->lineno + macinfo_label_base);
28128 ASM_OUTPUT_LABEL (asm_out_file, label);
28129 ref->code = 0;
28130 ref->info = NULL;
28131 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28132 "DWARF macro version number");
28133 if (DWARF_OFFSET_SIZE == 8)
28134 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28135 else
28136 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28138 break;
28139 case DW_MACINFO_define:
28140 case DW_MACINFO_undef:
28141 output_macinfo_op (ref);
28142 ref->code = 0;
28143 ref->info = NULL;
28144 break;
28145 default:
28146 gcc_unreachable ();
28149 macinfo_label_base += macinfo_label_base_adj;
28152 /* Initialize the various sections and labels for dwarf output and prefix
28153 them with PREFIX if non-NULL. Returns the generation (zero based
28154 number of times function was called). */
28156 static unsigned
28157 init_sections_and_labels (bool early_lto_debug)
28159 /* As we may get called multiple times have a generation count for
28160 labels. */
28161 static unsigned generation = 0;
28163 if (early_lto_debug)
28165 if (!dwarf_split_debug_info)
28167 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28168 SECTION_DEBUG | SECTION_EXCLUDE,
28169 NULL);
28170 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28171 SECTION_DEBUG | SECTION_EXCLUDE,
28172 NULL);
28173 debug_macinfo_section_name
28174 = ((dwarf_strict && dwarf_version < 5)
28175 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28176 debug_macinfo_section = get_section (debug_macinfo_section_name,
28177 SECTION_DEBUG
28178 | SECTION_EXCLUDE, NULL);
28179 /* For macro info we have to refer to a debug_line section, so
28180 similar to split-dwarf emit a skeleton one for early debug. */
28181 debug_skeleton_line_section
28182 = get_section (DEBUG_LTO_LINE_SECTION,
28183 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28184 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28185 DEBUG_SKELETON_LINE_SECTION_LABEL,
28186 generation);
28188 else
28190 /* ??? Which of the following do we need early? */
28191 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28192 SECTION_DEBUG | SECTION_EXCLUDE,
28193 NULL);
28194 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28195 SECTION_DEBUG | SECTION_EXCLUDE,
28196 NULL);
28197 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28198 SECTION_DEBUG
28199 | SECTION_EXCLUDE, NULL);
28200 debug_skeleton_abbrev_section
28201 = get_section (DEBUG_LTO_ABBREV_SECTION,
28202 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28203 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28204 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28205 generation);
28207 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28208 stay in the main .o, but the skeleton_line goes into the split
28209 off dwo. */
28210 debug_skeleton_line_section
28211 = get_section (DEBUG_LTO_LINE_SECTION,
28212 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28213 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28214 DEBUG_SKELETON_LINE_SECTION_LABEL,
28215 generation);
28216 debug_str_offsets_section
28217 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28218 SECTION_DEBUG | SECTION_EXCLUDE,
28219 NULL);
28220 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28221 DEBUG_SKELETON_INFO_SECTION_LABEL,
28222 generation);
28223 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28224 DEBUG_STR_DWO_SECTION_FLAGS,
28225 NULL);
28226 debug_macinfo_section_name
28227 = ((dwarf_strict && dwarf_version < 5)
28228 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28229 debug_macinfo_section = get_section (debug_macinfo_section_name,
28230 SECTION_DEBUG | SECTION_EXCLUDE,
28231 NULL);
28233 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28234 DEBUG_STR_SECTION_FLAGS
28235 | SECTION_EXCLUDE, NULL);
28236 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
28237 debug_line_str_section
28238 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28239 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28241 else
28243 if (!dwarf_split_debug_info)
28245 debug_info_section = get_section (DEBUG_INFO_SECTION,
28246 SECTION_DEBUG, NULL);
28247 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28248 SECTION_DEBUG, NULL);
28249 debug_loc_section = get_section (dwarf_version >= 5
28250 ? DEBUG_LOCLISTS_SECTION
28251 : DEBUG_LOC_SECTION,
28252 SECTION_DEBUG, NULL);
28253 debug_macinfo_section_name
28254 = ((dwarf_strict && dwarf_version < 5)
28255 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28256 debug_macinfo_section = get_section (debug_macinfo_section_name,
28257 SECTION_DEBUG, NULL);
28259 else
28261 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28262 SECTION_DEBUG | SECTION_EXCLUDE,
28263 NULL);
28264 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28265 SECTION_DEBUG | SECTION_EXCLUDE,
28266 NULL);
28267 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28268 SECTION_DEBUG, NULL);
28269 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28270 SECTION_DEBUG, NULL);
28271 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28272 SECTION_DEBUG, NULL);
28273 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28274 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28275 generation);
28277 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28278 stay in the main .o, but the skeleton_line goes into the
28279 split off dwo. */
28280 debug_skeleton_line_section
28281 = get_section (DEBUG_DWO_LINE_SECTION,
28282 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28283 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28284 DEBUG_SKELETON_LINE_SECTION_LABEL,
28285 generation);
28286 debug_str_offsets_section
28287 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28288 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28289 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28290 DEBUG_SKELETON_INFO_SECTION_LABEL,
28291 generation);
28292 debug_loc_section = get_section (dwarf_version >= 5
28293 ? DEBUG_DWO_LOCLISTS_SECTION
28294 : DEBUG_DWO_LOC_SECTION,
28295 SECTION_DEBUG | SECTION_EXCLUDE,
28296 NULL);
28297 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28298 DEBUG_STR_DWO_SECTION_FLAGS,
28299 NULL);
28300 debug_macinfo_section_name
28301 = ((dwarf_strict && dwarf_version < 5)
28302 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28303 debug_macinfo_section = get_section (debug_macinfo_section_name,
28304 SECTION_DEBUG | SECTION_EXCLUDE,
28305 NULL);
28307 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28308 SECTION_DEBUG, NULL);
28309 debug_line_section = get_section (DEBUG_LINE_SECTION,
28310 SECTION_DEBUG, NULL);
28311 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28312 SECTION_DEBUG, NULL);
28313 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28314 SECTION_DEBUG, NULL);
28315 debug_str_section = get_section (DEBUG_STR_SECTION,
28316 DEBUG_STR_SECTION_FLAGS, NULL);
28317 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28318 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28319 DEBUG_STR_SECTION_FLAGS, NULL);
28321 debug_ranges_section = get_section (dwarf_version >= 5
28322 ? DEBUG_RNGLISTS_SECTION
28323 : DEBUG_RANGES_SECTION,
28324 SECTION_DEBUG, NULL);
28325 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28326 SECTION_DEBUG, NULL);
28329 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28330 DEBUG_ABBREV_SECTION_LABEL, generation);
28331 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28332 DEBUG_INFO_SECTION_LABEL, generation);
28333 info_section_emitted = false;
28334 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28335 DEBUG_LINE_SECTION_LABEL, generation);
28336 /* There are up to 4 unique ranges labels per generation.
28337 See also output_rnglists. */
28338 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28339 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28340 if (dwarf_version >= 5 && dwarf_split_debug_info)
28341 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28342 DEBUG_RANGES_SECTION_LABEL,
28343 1 + generation * 4);
28344 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28345 DEBUG_ADDR_SECTION_LABEL, generation);
28346 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28347 (dwarf_strict && dwarf_version < 5)
28348 ? DEBUG_MACINFO_SECTION_LABEL
28349 : DEBUG_MACRO_SECTION_LABEL, generation);
28350 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28351 generation);
28353 ++generation;
28354 return generation - 1;
28357 /* Set up for Dwarf output at the start of compilation. */
28359 static void
28360 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28362 /* Allocate the file_table. */
28363 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28365 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28366 /* Allocate the decl_die_table. */
28367 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28369 /* Allocate the decl_loc_table. */
28370 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28372 /* Allocate the cached_dw_loc_list_table. */
28373 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28375 /* Allocate the initial hunk of the decl_scope_table. */
28376 vec_alloc (decl_scope_table, 256);
28378 /* Allocate the initial hunk of the abbrev_die_table. */
28379 vec_alloc (abbrev_die_table, 256);
28380 /* Zero-th entry is allocated, but unused. */
28381 abbrev_die_table->quick_push (NULL);
28383 /* Allocate the dwarf_proc_stack_usage_map. */
28384 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28386 /* Allocate the pubtypes and pubnames vectors. */
28387 vec_alloc (pubname_table, 32);
28388 vec_alloc (pubtype_table, 32);
28390 vec_alloc (incomplete_types, 64);
28392 vec_alloc (used_rtx_array, 32);
28394 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28395 vec_alloc (macinfo_table, 64);
28396 #endif
28398 /* If front-ends already registered a main translation unit but we were not
28399 ready to perform the association, do this now. */
28400 if (main_translation_unit != NULL_TREE)
28401 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28404 /* Called before compile () starts outputtting functions, variables
28405 and toplevel asms into assembly. */
28407 static void
28408 dwarf2out_assembly_start (void)
28410 if (text_section_line_info)
28411 return;
28413 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28414 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28415 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28416 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28417 COLD_TEXT_SECTION_LABEL, 0);
28418 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28420 switch_to_section (text_section);
28421 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28422 #endif
28424 /* Make sure the line number table for .text always exists. */
28425 text_section_line_info = new_line_info_table ();
28426 text_section_line_info->end_label = text_end_label;
28428 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28429 cur_line_info_table = text_section_line_info;
28430 #endif
28432 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28433 && dwarf2out_do_cfi_asm ()
28434 && !dwarf2out_do_eh_frame ())
28435 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28438 /* A helper function for dwarf2out_finish called through
28439 htab_traverse. Assign a string its index. All strings must be
28440 collected into the table by the time index_string is called,
28441 because the indexing code relies on htab_traverse to traverse nodes
28442 in the same order for each run. */
28445 index_string (indirect_string_node **h, unsigned int *index)
28447 indirect_string_node *node = *h;
28449 find_string_form (node);
28450 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28452 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28453 node->index = *index;
28454 *index += 1;
28456 return 1;
28459 /* A helper function for output_indirect_strings called through
28460 htab_traverse. Output the offset to a string and update the
28461 current offset. */
28464 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28466 indirect_string_node *node = *h;
28468 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28470 /* Assert that this node has been assigned an index. */
28471 gcc_assert (node->index != NO_INDEX_ASSIGNED
28472 && node->index != NOT_INDEXED);
28473 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28474 "indexed string 0x%x: %s", node->index, node->str);
28475 *offset += strlen (node->str) + 1;
28477 return 1;
28480 /* A helper function for dwarf2out_finish called through
28481 htab_traverse. Output the indexed string. */
28484 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28486 struct indirect_string_node *node = *h;
28488 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28490 /* Assert that the strings are output in the same order as their
28491 indexes were assigned. */
28492 gcc_assert (*cur_idx == node->index);
28493 assemble_string (node->str, strlen (node->str) + 1);
28494 *cur_idx += 1;
28496 return 1;
28499 /* A helper function for dwarf2out_finish called through
28500 htab_traverse. Emit one queued .debug_str string. */
28503 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28505 struct indirect_string_node *node = *h;
28507 node->form = find_string_form (node);
28508 if (node->form == form && node->refcount > 0)
28510 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28511 assemble_string (node->str, strlen (node->str) + 1);
28514 return 1;
28517 /* Output the indexed string table. */
28519 static void
28520 output_indirect_strings (void)
28522 switch_to_section (debug_str_section);
28523 if (!dwarf_split_debug_info)
28524 debug_str_hash->traverse<enum dwarf_form,
28525 output_indirect_string> (DW_FORM_strp);
28526 else
28528 unsigned int offset = 0;
28529 unsigned int cur_idx = 0;
28531 if (skeleton_debug_str_hash)
28532 skeleton_debug_str_hash->traverse<enum dwarf_form,
28533 output_indirect_string> (DW_FORM_strp);
28535 switch_to_section (debug_str_offsets_section);
28536 debug_str_hash->traverse_noresize
28537 <unsigned int *, output_index_string_offset> (&offset);
28538 switch_to_section (debug_str_dwo_section);
28539 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28540 (&cur_idx);
28544 /* Callback for htab_traverse to assign an index to an entry in the
28545 table, and to write that entry to the .debug_addr section. */
28548 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28550 addr_table_entry *entry = *slot;
28552 if (entry->refcount == 0)
28554 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28555 || entry->index == NOT_INDEXED);
28556 return 1;
28559 gcc_assert (entry->index == *cur_index);
28560 (*cur_index)++;
28562 switch (entry->kind)
28564 case ate_kind_rtx:
28565 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28566 "0x%x", entry->index);
28567 break;
28568 case ate_kind_rtx_dtprel:
28569 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28570 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28571 DWARF2_ADDR_SIZE,
28572 entry->addr.rtl);
28573 fputc ('\n', asm_out_file);
28574 break;
28575 case ate_kind_label:
28576 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28577 "0x%x", entry->index);
28578 break;
28579 default:
28580 gcc_unreachable ();
28582 return 1;
28585 /* Produce the .debug_addr section. */
28587 static void
28588 output_addr_table (void)
28590 unsigned int index = 0;
28591 if (addr_index_table == NULL || addr_index_table->size () == 0)
28592 return;
28594 switch_to_section (debug_addr_section);
28595 addr_index_table
28596 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28599 #if ENABLE_ASSERT_CHECKING
28600 /* Verify that all marks are clear. */
28602 static void
28603 verify_marks_clear (dw_die_ref die)
28605 dw_die_ref c;
28607 gcc_assert (! die->die_mark);
28608 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28610 #endif /* ENABLE_ASSERT_CHECKING */
28612 /* Clear the marks for a die and its children.
28613 Be cool if the mark isn't set. */
28615 static void
28616 prune_unmark_dies (dw_die_ref die)
28618 dw_die_ref c;
28620 if (die->die_mark)
28621 die->die_mark = 0;
28622 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28625 /* Given LOC that is referenced by a DIE we're marking as used, find all
28626 referenced DWARF procedures it references and mark them as used. */
28628 static void
28629 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28631 for (; loc != NULL; loc = loc->dw_loc_next)
28632 switch (loc->dw_loc_opc)
28634 case DW_OP_implicit_pointer:
28635 case DW_OP_convert:
28636 case DW_OP_reinterpret:
28637 case DW_OP_GNU_implicit_pointer:
28638 case DW_OP_GNU_convert:
28639 case DW_OP_GNU_reinterpret:
28640 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28641 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28642 break;
28643 case DW_OP_GNU_variable_value:
28644 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28646 dw_die_ref ref
28647 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28648 if (ref == NULL)
28649 break;
28650 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28651 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28652 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28654 /* FALLTHRU */
28655 case DW_OP_call2:
28656 case DW_OP_call4:
28657 case DW_OP_call_ref:
28658 case DW_OP_const_type:
28659 case DW_OP_GNU_const_type:
28660 case DW_OP_GNU_parameter_ref:
28661 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28662 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28663 break;
28664 case DW_OP_regval_type:
28665 case DW_OP_deref_type:
28666 case DW_OP_GNU_regval_type:
28667 case DW_OP_GNU_deref_type:
28668 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28669 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28670 break;
28671 case DW_OP_entry_value:
28672 case DW_OP_GNU_entry_value:
28673 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28674 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28675 break;
28676 default:
28677 break;
28681 /* Given DIE that we're marking as used, find any other dies
28682 it references as attributes and mark them as used. */
28684 static void
28685 prune_unused_types_walk_attribs (dw_die_ref die)
28687 dw_attr_node *a;
28688 unsigned ix;
28690 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28692 switch (AT_class (a))
28694 /* Make sure DWARF procedures referenced by location descriptions will
28695 get emitted. */
28696 case dw_val_class_loc:
28697 prune_unused_types_walk_loc_descr (AT_loc (a));
28698 break;
28699 case dw_val_class_loc_list:
28700 for (dw_loc_list_ref list = AT_loc_list (a);
28701 list != NULL;
28702 list = list->dw_loc_next)
28703 prune_unused_types_walk_loc_descr (list->expr);
28704 break;
28706 case dw_val_class_view_list:
28707 /* This points to a loc_list in another attribute, so it's
28708 already covered. */
28709 break;
28711 case dw_val_class_die_ref:
28712 /* A reference to another DIE.
28713 Make sure that it will get emitted.
28714 If it was broken out into a comdat group, don't follow it. */
28715 if (! AT_ref (a)->comdat_type_p
28716 || a->dw_attr == DW_AT_specification)
28717 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
28718 break;
28720 case dw_val_class_str:
28721 /* Set the string's refcount to 0 so that prune_unused_types_mark
28722 accounts properly for it. */
28723 a->dw_attr_val.v.val_str->refcount = 0;
28724 break;
28726 default:
28727 break;
28732 /* Mark the generic parameters and arguments children DIEs of DIE. */
28734 static void
28735 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28737 dw_die_ref c;
28739 if (die == NULL || die->die_child == NULL)
28740 return;
28741 c = die->die_child;
28744 if (is_template_parameter (c))
28745 prune_unused_types_mark (c, 1);
28746 c = c->die_sib;
28747 } while (c && c != die->die_child);
28750 /* Mark DIE as being used. If DOKIDS is true, then walk down
28751 to DIE's children. */
28753 static void
28754 prune_unused_types_mark (dw_die_ref die, int dokids)
28756 dw_die_ref c;
28758 if (die->die_mark == 0)
28760 /* We haven't done this node yet. Mark it as used. */
28761 die->die_mark = 1;
28762 /* If this is the DIE of a generic type instantiation,
28763 mark the children DIEs that describe its generic parms and
28764 args. */
28765 prune_unused_types_mark_generic_parms_dies (die);
28767 /* We also have to mark its parents as used.
28768 (But we don't want to mark our parent's kids due to this,
28769 unless it is a class.) */
28770 if (die->die_parent)
28771 prune_unused_types_mark (die->die_parent,
28772 class_scope_p (die->die_parent));
28774 /* Mark any referenced nodes. */
28775 prune_unused_types_walk_attribs (die);
28777 /* If this node is a specification,
28778 also mark the definition, if it exists. */
28779 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28780 prune_unused_types_mark (die->die_definition, 1);
28783 if (dokids && die->die_mark != 2)
28785 /* We need to walk the children, but haven't done so yet.
28786 Remember that we've walked the kids. */
28787 die->die_mark = 2;
28789 /* If this is an array type, we need to make sure our
28790 kids get marked, even if they're types. If we're
28791 breaking out types into comdat sections, do this
28792 for all type definitions. */
28793 if (die->die_tag == DW_TAG_array_type
28794 || (use_debug_types
28795 && is_type_die (die) && ! is_declaration_die (die)))
28796 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28797 else
28798 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28802 /* For local classes, look if any static member functions were emitted
28803 and if so, mark them. */
28805 static void
28806 prune_unused_types_walk_local_classes (dw_die_ref die)
28808 dw_die_ref c;
28810 if (die->die_mark == 2)
28811 return;
28813 switch (die->die_tag)
28815 case DW_TAG_structure_type:
28816 case DW_TAG_union_type:
28817 case DW_TAG_class_type:
28818 break;
28820 case DW_TAG_subprogram:
28821 if (!get_AT_flag (die, DW_AT_declaration)
28822 || die->die_definition != NULL)
28823 prune_unused_types_mark (die, 1);
28824 return;
28826 default:
28827 return;
28830 /* Mark children. */
28831 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28834 /* Walk the tree DIE and mark types that we actually use. */
28836 static void
28837 prune_unused_types_walk (dw_die_ref die)
28839 dw_die_ref c;
28841 /* Don't do anything if this node is already marked and
28842 children have been marked as well. */
28843 if (die->die_mark == 2)
28844 return;
28846 switch (die->die_tag)
28848 case DW_TAG_structure_type:
28849 case DW_TAG_union_type:
28850 case DW_TAG_class_type:
28851 if (die->die_perennial_p)
28852 break;
28854 for (c = die->die_parent; c; c = c->die_parent)
28855 if (c->die_tag == DW_TAG_subprogram)
28856 break;
28858 /* Finding used static member functions inside of classes
28859 is needed just for local classes, because for other classes
28860 static member function DIEs with DW_AT_specification
28861 are emitted outside of the DW_TAG_*_type. If we ever change
28862 it, we'd need to call this even for non-local classes. */
28863 if (c)
28864 prune_unused_types_walk_local_classes (die);
28866 /* It's a type node --- don't mark it. */
28867 return;
28869 case DW_TAG_const_type:
28870 case DW_TAG_packed_type:
28871 case DW_TAG_pointer_type:
28872 case DW_TAG_reference_type:
28873 case DW_TAG_rvalue_reference_type:
28874 case DW_TAG_volatile_type:
28875 case DW_TAG_typedef:
28876 case DW_TAG_array_type:
28877 case DW_TAG_interface_type:
28878 case DW_TAG_friend:
28879 case DW_TAG_enumeration_type:
28880 case DW_TAG_subroutine_type:
28881 case DW_TAG_string_type:
28882 case DW_TAG_set_type:
28883 case DW_TAG_subrange_type:
28884 case DW_TAG_ptr_to_member_type:
28885 case DW_TAG_file_type:
28886 /* Type nodes are useful only when other DIEs reference them --- don't
28887 mark them. */
28888 /* FALLTHROUGH */
28890 case DW_TAG_dwarf_procedure:
28891 /* Likewise for DWARF procedures. */
28893 if (die->die_perennial_p)
28894 break;
28896 return;
28898 default:
28899 /* Mark everything else. */
28900 break;
28903 if (die->die_mark == 0)
28905 die->die_mark = 1;
28907 /* Now, mark any dies referenced from here. */
28908 prune_unused_types_walk_attribs (die);
28911 die->die_mark = 2;
28913 /* Mark children. */
28914 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28917 /* Increment the string counts on strings referred to from DIE's
28918 attributes. */
28920 static void
28921 prune_unused_types_update_strings (dw_die_ref die)
28923 dw_attr_node *a;
28924 unsigned ix;
28926 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28927 if (AT_class (a) == dw_val_class_str)
28929 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28930 s->refcount++;
28931 /* Avoid unnecessarily putting strings that are used less than
28932 twice in the hash table. */
28933 if (s->refcount
28934 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28936 indirect_string_node **slot
28937 = debug_str_hash->find_slot_with_hash (s->str,
28938 htab_hash_string (s->str),
28939 INSERT);
28940 gcc_assert (*slot == NULL);
28941 *slot = s;
28946 /* Mark DIE and its children as removed. */
28948 static void
28949 mark_removed (dw_die_ref die)
28951 dw_die_ref c;
28952 die->removed = true;
28953 FOR_EACH_CHILD (die, c, mark_removed (c));
28956 /* Remove from the tree DIE any dies that aren't marked. */
28958 static void
28959 prune_unused_types_prune (dw_die_ref die)
28961 dw_die_ref c;
28963 gcc_assert (die->die_mark);
28964 prune_unused_types_update_strings (die);
28966 if (! die->die_child)
28967 return;
28969 c = die->die_child;
28970 do {
28971 dw_die_ref prev = c, next;
28972 for (c = c->die_sib; ! c->die_mark; c = next)
28973 if (c == die->die_child)
28975 /* No marked children between 'prev' and the end of the list. */
28976 if (prev == c)
28977 /* No marked children at all. */
28978 die->die_child = NULL;
28979 else
28981 prev->die_sib = c->die_sib;
28982 die->die_child = prev;
28984 c->die_sib = NULL;
28985 mark_removed (c);
28986 return;
28988 else
28990 next = c->die_sib;
28991 c->die_sib = NULL;
28992 mark_removed (c);
28995 if (c != prev->die_sib)
28996 prev->die_sib = c;
28997 prune_unused_types_prune (c);
28998 } while (c != die->die_child);
29001 /* Remove dies representing declarations that we never use. */
29003 static void
29004 prune_unused_types (void)
29006 unsigned int i;
29007 limbo_die_node *node;
29008 comdat_type_node *ctnode;
29009 pubname_entry *pub;
29010 dw_die_ref base_type;
29012 #if ENABLE_ASSERT_CHECKING
29013 /* All the marks should already be clear. */
29014 verify_marks_clear (comp_unit_die ());
29015 for (node = limbo_die_list; node; node = node->next)
29016 verify_marks_clear (node->die);
29017 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29018 verify_marks_clear (ctnode->root_die);
29019 #endif /* ENABLE_ASSERT_CHECKING */
29021 /* Mark types that are used in global variables. */
29022 premark_types_used_by_global_vars ();
29024 /* Set the mark on nodes that are actually used. */
29025 prune_unused_types_walk (comp_unit_die ());
29026 for (node = limbo_die_list; node; node = node->next)
29027 prune_unused_types_walk (node->die);
29028 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29030 prune_unused_types_walk (ctnode->root_die);
29031 prune_unused_types_mark (ctnode->type_die, 1);
29034 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29035 are unusual in that they are pubnames that are the children of pubtypes.
29036 They should only be marked via their parent DW_TAG_enumeration_type die,
29037 not as roots in themselves. */
29038 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29039 if (pub->die->die_tag != DW_TAG_enumerator)
29040 prune_unused_types_mark (pub->die, 1);
29041 for (i = 0; base_types.iterate (i, &base_type); i++)
29042 prune_unused_types_mark (base_type, 1);
29044 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29045 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29046 callees). */
29047 cgraph_node *cnode;
29048 FOR_EACH_FUNCTION (cnode)
29049 if (cnode->referred_to_p (false))
29051 dw_die_ref die = lookup_decl_die (cnode->decl);
29052 if (die == NULL || die->die_mark)
29053 continue;
29054 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29055 if (e->caller != cnode
29056 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29058 prune_unused_types_mark (die, 1);
29059 break;
29063 if (debug_str_hash)
29064 debug_str_hash->empty ();
29065 if (skeleton_debug_str_hash)
29066 skeleton_debug_str_hash->empty ();
29067 prune_unused_types_prune (comp_unit_die ());
29068 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29070 node = *pnode;
29071 if (!node->die->die_mark)
29072 *pnode = node->next;
29073 else
29075 prune_unused_types_prune (node->die);
29076 pnode = &node->next;
29079 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29080 prune_unused_types_prune (ctnode->root_die);
29082 /* Leave the marks clear. */
29083 prune_unmark_dies (comp_unit_die ());
29084 for (node = limbo_die_list; node; node = node->next)
29085 prune_unmark_dies (node->die);
29086 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29087 prune_unmark_dies (ctnode->root_die);
29090 /* Helpers to manipulate hash table of comdat type units. */
29092 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29094 static inline hashval_t hash (const comdat_type_node *);
29095 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29098 inline hashval_t
29099 comdat_type_hasher::hash (const comdat_type_node *type_node)
29101 hashval_t h;
29102 memcpy (&h, type_node->signature, sizeof (h));
29103 return h;
29106 inline bool
29107 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29108 const comdat_type_node *type_node_2)
29110 return (! memcmp (type_node_1->signature, type_node_2->signature,
29111 DWARF_TYPE_SIGNATURE_SIZE));
29114 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29115 to the location it would have been added, should we know its
29116 DECL_ASSEMBLER_NAME when we added other attributes. This will
29117 probably improve compactness of debug info, removing equivalent
29118 abbrevs, and hide any differences caused by deferring the
29119 computation of the assembler name, triggered by e.g. PCH. */
29121 static inline void
29122 move_linkage_attr (dw_die_ref die)
29124 unsigned ix = vec_safe_length (die->die_attr);
29125 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29127 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29128 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29130 while (--ix > 0)
29132 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29134 if (prev->dw_attr == DW_AT_decl_line
29135 || prev->dw_attr == DW_AT_decl_column
29136 || prev->dw_attr == DW_AT_name)
29137 break;
29140 if (ix != vec_safe_length (die->die_attr) - 1)
29142 die->die_attr->pop ();
29143 die->die_attr->quick_insert (ix, linkage);
29147 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29148 referenced from typed stack ops and count how often they are used. */
29150 static void
29151 mark_base_types (dw_loc_descr_ref loc)
29153 dw_die_ref base_type = NULL;
29155 for (; loc; loc = loc->dw_loc_next)
29157 switch (loc->dw_loc_opc)
29159 case DW_OP_regval_type:
29160 case DW_OP_deref_type:
29161 case DW_OP_GNU_regval_type:
29162 case DW_OP_GNU_deref_type:
29163 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29164 break;
29165 case DW_OP_convert:
29166 case DW_OP_reinterpret:
29167 case DW_OP_GNU_convert:
29168 case DW_OP_GNU_reinterpret:
29169 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29170 continue;
29171 /* FALLTHRU */
29172 case DW_OP_const_type:
29173 case DW_OP_GNU_const_type:
29174 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29175 break;
29176 case DW_OP_entry_value:
29177 case DW_OP_GNU_entry_value:
29178 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29179 continue;
29180 default:
29181 continue;
29183 gcc_assert (base_type->die_parent == comp_unit_die ());
29184 if (base_type->die_mark)
29185 base_type->die_mark++;
29186 else
29188 base_types.safe_push (base_type);
29189 base_type->die_mark = 1;
29194 /* Comparison function for sorting marked base types. */
29196 static int
29197 base_type_cmp (const void *x, const void *y)
29199 dw_die_ref dx = *(const dw_die_ref *) x;
29200 dw_die_ref dy = *(const dw_die_ref *) y;
29201 unsigned int byte_size1, byte_size2;
29202 unsigned int encoding1, encoding2;
29203 unsigned int align1, align2;
29204 if (dx->die_mark > dy->die_mark)
29205 return -1;
29206 if (dx->die_mark < dy->die_mark)
29207 return 1;
29208 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29209 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29210 if (byte_size1 < byte_size2)
29211 return 1;
29212 if (byte_size1 > byte_size2)
29213 return -1;
29214 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29215 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29216 if (encoding1 < encoding2)
29217 return 1;
29218 if (encoding1 > encoding2)
29219 return -1;
29220 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29221 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29222 if (align1 < align2)
29223 return 1;
29224 if (align1 > align2)
29225 return -1;
29226 return 0;
29229 /* Move base types marked by mark_base_types as early as possible
29230 in the CU, sorted by decreasing usage count both to make the
29231 uleb128 references as small as possible and to make sure they
29232 will have die_offset already computed by calc_die_sizes when
29233 sizes of typed stack loc ops is computed. */
29235 static void
29236 move_marked_base_types (void)
29238 unsigned int i;
29239 dw_die_ref base_type, die, c;
29241 if (base_types.is_empty ())
29242 return;
29244 /* Sort by decreasing usage count, they will be added again in that
29245 order later on. */
29246 base_types.qsort (base_type_cmp);
29247 die = comp_unit_die ();
29248 c = die->die_child;
29251 dw_die_ref prev = c;
29252 c = c->die_sib;
29253 while (c->die_mark)
29255 remove_child_with_prev (c, prev);
29256 /* As base types got marked, there must be at least
29257 one node other than DW_TAG_base_type. */
29258 gcc_assert (die->die_child != NULL);
29259 c = prev->die_sib;
29262 while (c != die->die_child);
29263 gcc_assert (die->die_child);
29264 c = die->die_child;
29265 for (i = 0; base_types.iterate (i, &base_type); i++)
29267 base_type->die_mark = 0;
29268 base_type->die_sib = c->die_sib;
29269 c->die_sib = base_type;
29270 c = base_type;
29274 /* Helper function for resolve_addr, attempt to resolve
29275 one CONST_STRING, return true if successful. Similarly verify that
29276 SYMBOL_REFs refer to variables emitted in the current CU. */
29278 static bool
29279 resolve_one_addr (rtx *addr)
29281 rtx rtl = *addr;
29283 if (GET_CODE (rtl) == CONST_STRING)
29285 size_t len = strlen (XSTR (rtl, 0)) + 1;
29286 tree t = build_string (len, XSTR (rtl, 0));
29287 tree tlen = size_int (len - 1);
29288 TREE_TYPE (t)
29289 = build_array_type (char_type_node, build_index_type (tlen));
29290 rtl = lookup_constant_def (t);
29291 if (!rtl || !MEM_P (rtl))
29292 return false;
29293 rtl = XEXP (rtl, 0);
29294 if (GET_CODE (rtl) == SYMBOL_REF
29295 && SYMBOL_REF_DECL (rtl)
29296 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29297 return false;
29298 vec_safe_push (used_rtx_array, rtl);
29299 *addr = rtl;
29300 return true;
29303 if (GET_CODE (rtl) == SYMBOL_REF
29304 && SYMBOL_REF_DECL (rtl))
29306 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29308 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29309 return false;
29311 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29312 return false;
29315 if (GET_CODE (rtl) == CONST)
29317 subrtx_ptr_iterator::array_type array;
29318 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29319 if (!resolve_one_addr (*iter))
29320 return false;
29323 return true;
29326 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29327 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29328 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29330 static rtx
29331 string_cst_pool_decl (tree t)
29333 rtx rtl = output_constant_def (t, 1);
29334 unsigned char *array;
29335 dw_loc_descr_ref l;
29336 tree decl;
29337 size_t len;
29338 dw_die_ref ref;
29340 if (!rtl || !MEM_P (rtl))
29341 return NULL_RTX;
29342 rtl = XEXP (rtl, 0);
29343 if (GET_CODE (rtl) != SYMBOL_REF
29344 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29345 return NULL_RTX;
29347 decl = SYMBOL_REF_DECL (rtl);
29348 if (!lookup_decl_die (decl))
29350 len = TREE_STRING_LENGTH (t);
29351 vec_safe_push (used_rtx_array, rtl);
29352 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29353 array = ggc_vec_alloc<unsigned char> (len);
29354 memcpy (array, TREE_STRING_POINTER (t), len);
29355 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29356 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29357 l->dw_loc_oprnd2.v.val_vec.length = len;
29358 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29359 l->dw_loc_oprnd2.v.val_vec.array = array;
29360 add_AT_loc (ref, DW_AT_location, l);
29361 equate_decl_number_to_die (decl, ref);
29363 return rtl;
29366 /* Helper function of resolve_addr_in_expr. LOC is
29367 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29368 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29369 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29370 with DW_OP_implicit_pointer if possible
29371 and return true, if unsuccessful, return false. */
29373 static bool
29374 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29376 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29377 HOST_WIDE_INT offset = 0;
29378 dw_die_ref ref = NULL;
29379 tree decl;
29381 if (GET_CODE (rtl) == CONST
29382 && GET_CODE (XEXP (rtl, 0)) == PLUS
29383 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29385 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29386 rtl = XEXP (XEXP (rtl, 0), 0);
29388 if (GET_CODE (rtl) == CONST_STRING)
29390 size_t len = strlen (XSTR (rtl, 0)) + 1;
29391 tree t = build_string (len, XSTR (rtl, 0));
29392 tree tlen = size_int (len - 1);
29394 TREE_TYPE (t)
29395 = build_array_type (char_type_node, build_index_type (tlen));
29396 rtl = string_cst_pool_decl (t);
29397 if (!rtl)
29398 return false;
29400 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29402 decl = SYMBOL_REF_DECL (rtl);
29403 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29405 ref = lookup_decl_die (decl);
29406 if (ref && (get_AT (ref, DW_AT_location)
29407 || get_AT (ref, DW_AT_const_value)))
29409 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29410 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29411 loc->dw_loc_oprnd1.val_entry = NULL;
29412 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29413 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29414 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29415 loc->dw_loc_oprnd2.v.val_int = offset;
29416 return true;
29420 return false;
29423 /* Helper function for resolve_addr, handle one location
29424 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29425 the location list couldn't be resolved. */
29427 static bool
29428 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29430 dw_loc_descr_ref keep = NULL;
29431 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29432 switch (loc->dw_loc_opc)
29434 case DW_OP_addr:
29435 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29437 if ((prev == NULL
29438 || prev->dw_loc_opc == DW_OP_piece
29439 || prev->dw_loc_opc == DW_OP_bit_piece)
29440 && loc->dw_loc_next
29441 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29442 && (!dwarf_strict || dwarf_version >= 5)
29443 && optimize_one_addr_into_implicit_ptr (loc))
29444 break;
29445 return false;
29447 break;
29448 case DW_OP_GNU_addr_index:
29449 case DW_OP_GNU_const_index:
29450 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
29451 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
29453 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29454 if (!resolve_one_addr (&rtl))
29455 return false;
29456 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29457 loc->dw_loc_oprnd1.val_entry
29458 = add_addr_table_entry (rtl, ate_kind_rtx);
29460 break;
29461 case DW_OP_const4u:
29462 case DW_OP_const8u:
29463 if (loc->dtprel
29464 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29465 return false;
29466 break;
29467 case DW_OP_plus_uconst:
29468 if (size_of_loc_descr (loc)
29469 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29471 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29473 dw_loc_descr_ref repl
29474 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29475 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29476 add_loc_descr (&repl, loc->dw_loc_next);
29477 *loc = *repl;
29479 break;
29480 case DW_OP_implicit_value:
29481 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29482 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29483 return false;
29484 break;
29485 case DW_OP_implicit_pointer:
29486 case DW_OP_GNU_implicit_pointer:
29487 case DW_OP_GNU_parameter_ref:
29488 case DW_OP_GNU_variable_value:
29489 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29491 dw_die_ref ref
29492 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29493 if (ref == NULL)
29494 return false;
29495 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29496 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29497 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29499 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29501 if (prev == NULL
29502 && loc->dw_loc_next == NULL
29503 && AT_class (a) == dw_val_class_loc)
29504 switch (a->dw_attr)
29506 /* Following attributes allow both exprloc and reference,
29507 so if the whole expression is DW_OP_GNU_variable_value
29508 alone we could transform it into reference. */
29509 case DW_AT_byte_size:
29510 case DW_AT_bit_size:
29511 case DW_AT_lower_bound:
29512 case DW_AT_upper_bound:
29513 case DW_AT_bit_stride:
29514 case DW_AT_count:
29515 case DW_AT_allocated:
29516 case DW_AT_associated:
29517 case DW_AT_byte_stride:
29518 a->dw_attr_val.val_class = dw_val_class_die_ref;
29519 a->dw_attr_val.val_entry = NULL;
29520 a->dw_attr_val.v.val_die_ref.die
29521 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29522 a->dw_attr_val.v.val_die_ref.external = 0;
29523 return true;
29524 default:
29525 break;
29527 if (dwarf_strict)
29528 return false;
29530 break;
29531 case DW_OP_const_type:
29532 case DW_OP_regval_type:
29533 case DW_OP_deref_type:
29534 case DW_OP_convert:
29535 case DW_OP_reinterpret:
29536 case DW_OP_GNU_const_type:
29537 case DW_OP_GNU_regval_type:
29538 case DW_OP_GNU_deref_type:
29539 case DW_OP_GNU_convert:
29540 case DW_OP_GNU_reinterpret:
29541 while (loc->dw_loc_next
29542 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29543 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29545 dw_die_ref base1, base2;
29546 unsigned enc1, enc2, size1, size2;
29547 if (loc->dw_loc_opc == DW_OP_regval_type
29548 || loc->dw_loc_opc == DW_OP_deref_type
29549 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29550 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29551 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29552 else if (loc->dw_loc_oprnd1.val_class
29553 == dw_val_class_unsigned_const)
29554 break;
29555 else
29556 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29557 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29558 == dw_val_class_unsigned_const)
29559 break;
29560 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29561 gcc_assert (base1->die_tag == DW_TAG_base_type
29562 && base2->die_tag == DW_TAG_base_type);
29563 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29564 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29565 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29566 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29567 if (size1 == size2
29568 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29569 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29570 && loc != keep)
29571 || enc1 == enc2))
29573 /* Optimize away next DW_OP_convert after
29574 adjusting LOC's base type die reference. */
29575 if (loc->dw_loc_opc == DW_OP_regval_type
29576 || loc->dw_loc_opc == DW_OP_deref_type
29577 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29578 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29579 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29580 else
29581 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29582 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29583 continue;
29585 /* Don't change integer DW_OP_convert after e.g. floating
29586 point typed stack entry. */
29587 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29588 keep = loc->dw_loc_next;
29589 break;
29591 break;
29592 default:
29593 break;
29595 return true;
29598 /* Helper function of resolve_addr. DIE had DW_AT_location of
29599 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29600 and DW_OP_addr couldn't be resolved. resolve_addr has already
29601 removed the DW_AT_location attribute. This function attempts to
29602 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29603 to it or DW_AT_const_value attribute, if possible. */
29605 static void
29606 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29608 if (!VAR_P (decl)
29609 || lookup_decl_die (decl) != die
29610 || DECL_EXTERNAL (decl)
29611 || !TREE_STATIC (decl)
29612 || DECL_INITIAL (decl) == NULL_TREE
29613 || DECL_P (DECL_INITIAL (decl))
29614 || get_AT (die, DW_AT_const_value))
29615 return;
29617 tree init = DECL_INITIAL (decl);
29618 HOST_WIDE_INT offset = 0;
29619 /* For variables that have been optimized away and thus
29620 don't have a memory location, see if we can emit
29621 DW_AT_const_value instead. */
29622 if (tree_add_const_value_attribute (die, init))
29623 return;
29624 if (dwarf_strict && dwarf_version < 5)
29625 return;
29626 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29627 and ADDR_EXPR refers to a decl that has DW_AT_location or
29628 DW_AT_const_value (but isn't addressable, otherwise
29629 resolving the original DW_OP_addr wouldn't fail), see if
29630 we can add DW_OP_implicit_pointer. */
29631 STRIP_NOPS (init);
29632 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29633 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29635 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29636 init = TREE_OPERAND (init, 0);
29637 STRIP_NOPS (init);
29639 if (TREE_CODE (init) != ADDR_EXPR)
29640 return;
29641 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29642 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29643 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29644 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29645 && TREE_OPERAND (init, 0) != decl))
29647 dw_die_ref ref;
29648 dw_loc_descr_ref l;
29650 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29652 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29653 if (!rtl)
29654 return;
29655 decl = SYMBOL_REF_DECL (rtl);
29657 else
29658 decl = TREE_OPERAND (init, 0);
29659 ref = lookup_decl_die (decl);
29660 if (ref == NULL
29661 || (!get_AT (ref, DW_AT_location)
29662 && !get_AT (ref, DW_AT_const_value)))
29663 return;
29664 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29665 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29666 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29667 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29668 add_AT_loc (die, DW_AT_location, l);
29672 /* Return NULL if l is a DWARF expression, or first op that is not
29673 valid DWARF expression. */
29675 static dw_loc_descr_ref
29676 non_dwarf_expression (dw_loc_descr_ref l)
29678 while (l)
29680 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29681 return l;
29682 switch (l->dw_loc_opc)
29684 case DW_OP_regx:
29685 case DW_OP_implicit_value:
29686 case DW_OP_stack_value:
29687 case DW_OP_implicit_pointer:
29688 case DW_OP_GNU_implicit_pointer:
29689 case DW_OP_GNU_parameter_ref:
29690 case DW_OP_piece:
29691 case DW_OP_bit_piece:
29692 return l;
29693 default:
29694 break;
29696 l = l->dw_loc_next;
29698 return NULL;
29701 /* Return adjusted copy of EXPR:
29702 If it is empty DWARF expression, return it.
29703 If it is valid non-empty DWARF expression,
29704 return copy of EXPR with DW_OP_deref appended to it.
29705 If it is DWARF expression followed by DW_OP_reg{N,x}, return
29706 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
29707 If it is DWARF expression followed by DW_OP_stack_value, return
29708 copy of the DWARF expression without anything appended.
29709 Otherwise, return NULL. */
29711 static dw_loc_descr_ref
29712 copy_deref_exprloc (dw_loc_descr_ref expr)
29714 dw_loc_descr_ref tail = NULL;
29716 if (expr == NULL)
29717 return NULL;
29719 dw_loc_descr_ref l = non_dwarf_expression (expr);
29720 if (l && l->dw_loc_next)
29721 return NULL;
29723 if (l)
29725 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29726 tail = new_loc_descr ((enum dwarf_location_atom)
29727 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
29728 0, 0);
29729 else
29730 switch (l->dw_loc_opc)
29732 case DW_OP_regx:
29733 tail = new_loc_descr (DW_OP_bregx,
29734 l->dw_loc_oprnd1.v.val_unsigned, 0);
29735 break;
29736 case DW_OP_stack_value:
29737 break;
29738 default:
29739 return NULL;
29742 else
29743 tail = new_loc_descr (DW_OP_deref, 0, 0);
29745 dw_loc_descr_ref ret = NULL, *p = &ret;
29746 while (expr != l)
29748 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29749 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29750 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29751 p = &(*p)->dw_loc_next;
29752 expr = expr->dw_loc_next;
29754 *p = tail;
29755 return ret;
29758 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29759 reference to a variable or argument, adjust it if needed and return:
29760 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29761 attribute if present should be removed
29762 0 keep the attribute perhaps with minor modifications, no need to rescan
29763 1 if the attribute has been successfully adjusted. */
29765 static int
29766 optimize_string_length (dw_attr_node *a)
29768 dw_loc_descr_ref l = AT_loc (a), lv;
29769 dw_die_ref die;
29770 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29772 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
29773 die = lookup_decl_die (decl);
29774 if (die)
29776 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29777 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29778 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29780 else
29781 return -1;
29783 else
29784 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29786 /* DWARF5 allows reference class, so we can then reference the DIE.
29787 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29788 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29790 a->dw_attr_val.val_class = dw_val_class_die_ref;
29791 a->dw_attr_val.val_entry = NULL;
29792 a->dw_attr_val.v.val_die_ref.die = die;
29793 a->dw_attr_val.v.val_die_ref.external = 0;
29794 return 0;
29797 dw_attr_node *av = get_AT (die, DW_AT_location);
29798 dw_loc_list_ref d;
29799 bool non_dwarf_expr = false;
29801 if (av == NULL)
29802 return dwarf_strict ? -1 : 0;
29803 switch (AT_class (av))
29805 case dw_val_class_loc_list:
29806 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29807 if (d->expr && non_dwarf_expression (d->expr))
29808 non_dwarf_expr = true;
29809 break;
29810 case dw_val_class_view_list:
29811 gcc_unreachable ();
29812 case dw_val_class_loc:
29813 lv = AT_loc (av);
29814 if (lv == NULL)
29815 return dwarf_strict ? -1 : 0;
29816 if (non_dwarf_expression (lv))
29817 non_dwarf_expr = true;
29818 break;
29819 default:
29820 return dwarf_strict ? -1 : 0;
29823 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29824 into DW_OP_call4 or DW_OP_GNU_variable_value into
29825 DW_OP_call4 DW_OP_deref, do so. */
29826 if (!non_dwarf_expr
29827 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29829 l->dw_loc_opc = DW_OP_call4;
29830 if (l->dw_loc_next)
29831 l->dw_loc_next = NULL;
29832 else
29833 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29834 return 0;
29837 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29838 copy over the DW_AT_location attribute from die to a. */
29839 if (l->dw_loc_next != NULL)
29841 a->dw_attr_val = av->dw_attr_val;
29842 return 1;
29845 dw_loc_list_ref list, *p;
29846 switch (AT_class (av))
29848 case dw_val_class_loc_list:
29849 p = &list;
29850 list = NULL;
29851 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29853 lv = copy_deref_exprloc (d->expr);
29854 if (lv)
29856 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
29857 p = &(*p)->dw_loc_next;
29859 else if (!dwarf_strict && d->expr)
29860 return 0;
29862 if (list == NULL)
29863 return dwarf_strict ? -1 : 0;
29864 a->dw_attr_val.val_class = dw_val_class_loc_list;
29865 gen_llsym (list);
29866 *AT_loc_list_ptr (a) = list;
29867 return 1;
29868 case dw_val_class_loc:
29869 lv = copy_deref_exprloc (AT_loc (av));
29870 if (lv == NULL)
29871 return dwarf_strict ? -1 : 0;
29872 a->dw_attr_val.v.val_loc = lv;
29873 return 1;
29874 default:
29875 gcc_unreachable ();
29879 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29880 an address in .rodata section if the string literal is emitted there,
29881 or remove the containing location list or replace DW_AT_const_value
29882 with DW_AT_location and empty location expression, if it isn't found
29883 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29884 to something that has been emitted in the current CU. */
29886 static void
29887 resolve_addr (dw_die_ref die)
29889 dw_die_ref c;
29890 dw_attr_node *a;
29891 dw_loc_list_ref *curr, *start, loc;
29892 unsigned ix;
29893 bool remove_AT_byte_size = false;
29895 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29896 switch (AT_class (a))
29898 case dw_val_class_loc_list:
29899 start = curr = AT_loc_list_ptr (a);
29900 loc = *curr;
29901 gcc_assert (loc);
29902 /* The same list can be referenced more than once. See if we have
29903 already recorded the result from a previous pass. */
29904 if (loc->replaced)
29905 *curr = loc->dw_loc_next;
29906 else if (!loc->resolved_addr)
29908 /* As things stand, we do not expect or allow one die to
29909 reference a suffix of another die's location list chain.
29910 References must be identical or completely separate.
29911 There is therefore no need to cache the result of this
29912 pass on any list other than the first; doing so
29913 would lead to unnecessary writes. */
29914 while (*curr)
29916 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29917 if (!resolve_addr_in_expr (a, (*curr)->expr))
29919 dw_loc_list_ref next = (*curr)->dw_loc_next;
29920 dw_loc_descr_ref l = (*curr)->expr;
29922 if (next && (*curr)->ll_symbol)
29924 gcc_assert (!next->ll_symbol);
29925 next->ll_symbol = (*curr)->ll_symbol;
29926 next->vl_symbol = (*curr)->vl_symbol;
29928 if (dwarf_split_debug_info)
29929 remove_loc_list_addr_table_entries (l);
29930 *curr = next;
29932 else
29934 mark_base_types ((*curr)->expr);
29935 curr = &(*curr)->dw_loc_next;
29938 if (loc == *start)
29939 loc->resolved_addr = 1;
29940 else
29942 loc->replaced = 1;
29943 loc->dw_loc_next = *start;
29946 if (!*start)
29948 remove_AT (die, a->dw_attr);
29949 ix--;
29951 break;
29952 case dw_val_class_view_list:
29954 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
29955 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
29956 dw_val_node *llnode
29957 = view_list_to_loc_list_val_node (&a->dw_attr_val);
29958 /* If we no longer have a loclist, or it no longer needs
29959 views, drop this attribute. */
29960 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
29962 remove_AT (die, a->dw_attr);
29963 ix--;
29965 break;
29967 case dw_val_class_loc:
29969 dw_loc_descr_ref l = AT_loc (a);
29970 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29971 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29972 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29973 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29974 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29975 with DW_FORM_ref referencing the same DIE as
29976 DW_OP_GNU_variable_value used to reference. */
29977 if (a->dw_attr == DW_AT_string_length
29978 && l
29979 && l->dw_loc_opc == DW_OP_GNU_variable_value
29980 && (l->dw_loc_next == NULL
29981 || (l->dw_loc_next->dw_loc_next == NULL
29982 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29984 switch (optimize_string_length (a))
29986 case -1:
29987 remove_AT (die, a->dw_attr);
29988 ix--;
29989 /* If we drop DW_AT_string_length, we need to drop also
29990 DW_AT_{string_length_,}byte_size. */
29991 remove_AT_byte_size = true;
29992 continue;
29993 default:
29994 break;
29995 case 1:
29996 /* Even if we keep the optimized DW_AT_string_length,
29997 it might have changed AT_class, so process it again. */
29998 ix--;
29999 continue;
30002 /* For -gdwarf-2 don't attempt to optimize
30003 DW_AT_data_member_location containing
30004 DW_OP_plus_uconst - older consumers might
30005 rely on it being that op instead of a more complex,
30006 but shorter, location description. */
30007 if ((dwarf_version > 2
30008 || a->dw_attr != DW_AT_data_member_location
30009 || l == NULL
30010 || l->dw_loc_opc != DW_OP_plus_uconst
30011 || l->dw_loc_next != NULL)
30012 && !resolve_addr_in_expr (a, l))
30014 if (dwarf_split_debug_info)
30015 remove_loc_list_addr_table_entries (l);
30016 if (l != NULL
30017 && l->dw_loc_next == NULL
30018 && l->dw_loc_opc == DW_OP_addr
30019 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30020 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30021 && a->dw_attr == DW_AT_location)
30023 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30024 remove_AT (die, a->dw_attr);
30025 ix--;
30026 optimize_location_into_implicit_ptr (die, decl);
30027 break;
30029 if (a->dw_attr == DW_AT_string_length)
30030 /* If we drop DW_AT_string_length, we need to drop also
30031 DW_AT_{string_length_,}byte_size. */
30032 remove_AT_byte_size = true;
30033 remove_AT (die, a->dw_attr);
30034 ix--;
30036 else
30037 mark_base_types (l);
30039 break;
30040 case dw_val_class_addr:
30041 if (a->dw_attr == DW_AT_const_value
30042 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30044 if (AT_index (a) != NOT_INDEXED)
30045 remove_addr_table_entry (a->dw_attr_val.val_entry);
30046 remove_AT (die, a->dw_attr);
30047 ix--;
30049 if ((die->die_tag == DW_TAG_call_site
30050 && a->dw_attr == DW_AT_call_origin)
30051 || (die->die_tag == DW_TAG_GNU_call_site
30052 && a->dw_attr == DW_AT_abstract_origin))
30054 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30055 dw_die_ref tdie = lookup_decl_die (tdecl);
30056 dw_die_ref cdie;
30057 if (tdie == NULL
30058 && DECL_EXTERNAL (tdecl)
30059 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30060 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30062 dw_die_ref pdie = cdie;
30063 /* Make sure we don't add these DIEs into type units.
30064 We could emit skeleton DIEs for context (namespaces,
30065 outer structs/classes) and a skeleton DIE for the
30066 innermost context with DW_AT_signature pointing to the
30067 type unit. See PR78835. */
30068 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30069 pdie = pdie->die_parent;
30070 if (pdie == NULL)
30072 /* Creating a full DIE for tdecl is overly expensive and
30073 at this point even wrong when in the LTO phase
30074 as it can end up generating new type DIEs we didn't
30075 output and thus optimize_external_refs will crash. */
30076 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30077 add_AT_flag (tdie, DW_AT_external, 1);
30078 add_AT_flag (tdie, DW_AT_declaration, 1);
30079 add_linkage_attr (tdie, tdecl);
30080 add_name_and_src_coords_attributes (tdie, tdecl, true);
30081 equate_decl_number_to_die (tdecl, tdie);
30084 if (tdie)
30086 a->dw_attr_val.val_class = dw_val_class_die_ref;
30087 a->dw_attr_val.v.val_die_ref.die = tdie;
30088 a->dw_attr_val.v.val_die_ref.external = 0;
30090 else
30092 if (AT_index (a) != NOT_INDEXED)
30093 remove_addr_table_entry (a->dw_attr_val.val_entry);
30094 remove_AT (die, a->dw_attr);
30095 ix--;
30098 break;
30099 default:
30100 break;
30103 if (remove_AT_byte_size)
30104 remove_AT (die, dwarf_version >= 5
30105 ? DW_AT_string_length_byte_size
30106 : DW_AT_byte_size);
30108 FOR_EACH_CHILD (die, c, resolve_addr (c));
30111 /* Helper routines for optimize_location_lists.
30112 This pass tries to share identical local lists in .debug_loc
30113 section. */
30115 /* Iteratively hash operands of LOC opcode into HSTATE. */
30117 static void
30118 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30120 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30121 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30123 switch (loc->dw_loc_opc)
30125 case DW_OP_const4u:
30126 case DW_OP_const8u:
30127 if (loc->dtprel)
30128 goto hash_addr;
30129 /* FALLTHRU */
30130 case DW_OP_const1u:
30131 case DW_OP_const1s:
30132 case DW_OP_const2u:
30133 case DW_OP_const2s:
30134 case DW_OP_const4s:
30135 case DW_OP_const8s:
30136 case DW_OP_constu:
30137 case DW_OP_consts:
30138 case DW_OP_pick:
30139 case DW_OP_plus_uconst:
30140 case DW_OP_breg0:
30141 case DW_OP_breg1:
30142 case DW_OP_breg2:
30143 case DW_OP_breg3:
30144 case DW_OP_breg4:
30145 case DW_OP_breg5:
30146 case DW_OP_breg6:
30147 case DW_OP_breg7:
30148 case DW_OP_breg8:
30149 case DW_OP_breg9:
30150 case DW_OP_breg10:
30151 case DW_OP_breg11:
30152 case DW_OP_breg12:
30153 case DW_OP_breg13:
30154 case DW_OP_breg14:
30155 case DW_OP_breg15:
30156 case DW_OP_breg16:
30157 case DW_OP_breg17:
30158 case DW_OP_breg18:
30159 case DW_OP_breg19:
30160 case DW_OP_breg20:
30161 case DW_OP_breg21:
30162 case DW_OP_breg22:
30163 case DW_OP_breg23:
30164 case DW_OP_breg24:
30165 case DW_OP_breg25:
30166 case DW_OP_breg26:
30167 case DW_OP_breg27:
30168 case DW_OP_breg28:
30169 case DW_OP_breg29:
30170 case DW_OP_breg30:
30171 case DW_OP_breg31:
30172 case DW_OP_regx:
30173 case DW_OP_fbreg:
30174 case DW_OP_piece:
30175 case DW_OP_deref_size:
30176 case DW_OP_xderef_size:
30177 hstate.add_object (val1->v.val_int);
30178 break;
30179 case DW_OP_skip:
30180 case DW_OP_bra:
30182 int offset;
30184 gcc_assert (val1->val_class == dw_val_class_loc);
30185 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30186 hstate.add_object (offset);
30188 break;
30189 case DW_OP_implicit_value:
30190 hstate.add_object (val1->v.val_unsigned);
30191 switch (val2->val_class)
30193 case dw_val_class_const:
30194 hstate.add_object (val2->v.val_int);
30195 break;
30196 case dw_val_class_vec:
30198 unsigned int elt_size = val2->v.val_vec.elt_size;
30199 unsigned int len = val2->v.val_vec.length;
30201 hstate.add_int (elt_size);
30202 hstate.add_int (len);
30203 hstate.add (val2->v.val_vec.array, len * elt_size);
30205 break;
30206 case dw_val_class_const_double:
30207 hstate.add_object (val2->v.val_double.low);
30208 hstate.add_object (val2->v.val_double.high);
30209 break;
30210 case dw_val_class_wide_int:
30211 hstate.add (val2->v.val_wide->get_val (),
30212 get_full_len (*val2->v.val_wide)
30213 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30214 break;
30215 case dw_val_class_addr:
30216 inchash::add_rtx (val2->v.val_addr, hstate);
30217 break;
30218 default:
30219 gcc_unreachable ();
30221 break;
30222 case DW_OP_bregx:
30223 case DW_OP_bit_piece:
30224 hstate.add_object (val1->v.val_int);
30225 hstate.add_object (val2->v.val_int);
30226 break;
30227 case DW_OP_addr:
30228 hash_addr:
30229 if (loc->dtprel)
30231 unsigned char dtprel = 0xd1;
30232 hstate.add_object (dtprel);
30234 inchash::add_rtx (val1->v.val_addr, hstate);
30235 break;
30236 case DW_OP_GNU_addr_index:
30237 case DW_OP_GNU_const_index:
30239 if (loc->dtprel)
30241 unsigned char dtprel = 0xd1;
30242 hstate.add_object (dtprel);
30244 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30246 break;
30247 case DW_OP_implicit_pointer:
30248 case DW_OP_GNU_implicit_pointer:
30249 hstate.add_int (val2->v.val_int);
30250 break;
30251 case DW_OP_entry_value:
30252 case DW_OP_GNU_entry_value:
30253 hstate.add_object (val1->v.val_loc);
30254 break;
30255 case DW_OP_regval_type:
30256 case DW_OP_deref_type:
30257 case DW_OP_GNU_regval_type:
30258 case DW_OP_GNU_deref_type:
30260 unsigned int byte_size
30261 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30262 unsigned int encoding
30263 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30264 hstate.add_object (val1->v.val_int);
30265 hstate.add_object (byte_size);
30266 hstate.add_object (encoding);
30268 break;
30269 case DW_OP_convert:
30270 case DW_OP_reinterpret:
30271 case DW_OP_GNU_convert:
30272 case DW_OP_GNU_reinterpret:
30273 if (val1->val_class == dw_val_class_unsigned_const)
30275 hstate.add_object (val1->v.val_unsigned);
30276 break;
30278 /* FALLTHRU */
30279 case DW_OP_const_type:
30280 case DW_OP_GNU_const_type:
30282 unsigned int byte_size
30283 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30284 unsigned int encoding
30285 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30286 hstate.add_object (byte_size);
30287 hstate.add_object (encoding);
30288 if (loc->dw_loc_opc != DW_OP_const_type
30289 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30290 break;
30291 hstate.add_object (val2->val_class);
30292 switch (val2->val_class)
30294 case dw_val_class_const:
30295 hstate.add_object (val2->v.val_int);
30296 break;
30297 case dw_val_class_vec:
30299 unsigned int elt_size = val2->v.val_vec.elt_size;
30300 unsigned int len = val2->v.val_vec.length;
30302 hstate.add_object (elt_size);
30303 hstate.add_object (len);
30304 hstate.add (val2->v.val_vec.array, len * elt_size);
30306 break;
30307 case dw_val_class_const_double:
30308 hstate.add_object (val2->v.val_double.low);
30309 hstate.add_object (val2->v.val_double.high);
30310 break;
30311 case dw_val_class_wide_int:
30312 hstate.add (val2->v.val_wide->get_val (),
30313 get_full_len (*val2->v.val_wide)
30314 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30315 break;
30316 default:
30317 gcc_unreachable ();
30320 break;
30322 default:
30323 /* Other codes have no operands. */
30324 break;
30328 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30330 static inline void
30331 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30333 dw_loc_descr_ref l;
30334 bool sizes_computed = false;
30335 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30336 size_of_locs (loc);
30338 for (l = loc; l != NULL; l = l->dw_loc_next)
30340 enum dwarf_location_atom opc = l->dw_loc_opc;
30341 hstate.add_object (opc);
30342 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30344 size_of_locs (loc);
30345 sizes_computed = true;
30347 hash_loc_operands (l, hstate);
30351 /* Compute hash of the whole location list LIST_HEAD. */
30353 static inline void
30354 hash_loc_list (dw_loc_list_ref list_head)
30356 dw_loc_list_ref curr = list_head;
30357 inchash::hash hstate;
30359 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30361 hstate.add (curr->begin, strlen (curr->begin) + 1);
30362 hstate.add (curr->end, strlen (curr->end) + 1);
30363 hstate.add_object (curr->vbegin);
30364 hstate.add_object (curr->vend);
30365 if (curr->section)
30366 hstate.add (curr->section, strlen (curr->section) + 1);
30367 hash_locs (curr->expr, hstate);
30369 list_head->hash = hstate.end ();
30372 /* Return true if X and Y opcodes have the same operands. */
30374 static inline bool
30375 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30377 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30378 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30379 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30380 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30382 switch (x->dw_loc_opc)
30384 case DW_OP_const4u:
30385 case DW_OP_const8u:
30386 if (x->dtprel)
30387 goto hash_addr;
30388 /* FALLTHRU */
30389 case DW_OP_const1u:
30390 case DW_OP_const1s:
30391 case DW_OP_const2u:
30392 case DW_OP_const2s:
30393 case DW_OP_const4s:
30394 case DW_OP_const8s:
30395 case DW_OP_constu:
30396 case DW_OP_consts:
30397 case DW_OP_pick:
30398 case DW_OP_plus_uconst:
30399 case DW_OP_breg0:
30400 case DW_OP_breg1:
30401 case DW_OP_breg2:
30402 case DW_OP_breg3:
30403 case DW_OP_breg4:
30404 case DW_OP_breg5:
30405 case DW_OP_breg6:
30406 case DW_OP_breg7:
30407 case DW_OP_breg8:
30408 case DW_OP_breg9:
30409 case DW_OP_breg10:
30410 case DW_OP_breg11:
30411 case DW_OP_breg12:
30412 case DW_OP_breg13:
30413 case DW_OP_breg14:
30414 case DW_OP_breg15:
30415 case DW_OP_breg16:
30416 case DW_OP_breg17:
30417 case DW_OP_breg18:
30418 case DW_OP_breg19:
30419 case DW_OP_breg20:
30420 case DW_OP_breg21:
30421 case DW_OP_breg22:
30422 case DW_OP_breg23:
30423 case DW_OP_breg24:
30424 case DW_OP_breg25:
30425 case DW_OP_breg26:
30426 case DW_OP_breg27:
30427 case DW_OP_breg28:
30428 case DW_OP_breg29:
30429 case DW_OP_breg30:
30430 case DW_OP_breg31:
30431 case DW_OP_regx:
30432 case DW_OP_fbreg:
30433 case DW_OP_piece:
30434 case DW_OP_deref_size:
30435 case DW_OP_xderef_size:
30436 return valx1->v.val_int == valy1->v.val_int;
30437 case DW_OP_skip:
30438 case DW_OP_bra:
30439 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30440 can cause irrelevant differences in dw_loc_addr. */
30441 gcc_assert (valx1->val_class == dw_val_class_loc
30442 && valy1->val_class == dw_val_class_loc
30443 && (dwarf_split_debug_info
30444 || x->dw_loc_addr == y->dw_loc_addr));
30445 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30446 case DW_OP_implicit_value:
30447 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30448 || valx2->val_class != valy2->val_class)
30449 return false;
30450 switch (valx2->val_class)
30452 case dw_val_class_const:
30453 return valx2->v.val_int == valy2->v.val_int;
30454 case dw_val_class_vec:
30455 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30456 && valx2->v.val_vec.length == valy2->v.val_vec.length
30457 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30458 valx2->v.val_vec.elt_size
30459 * valx2->v.val_vec.length) == 0;
30460 case dw_val_class_const_double:
30461 return valx2->v.val_double.low == valy2->v.val_double.low
30462 && valx2->v.val_double.high == valy2->v.val_double.high;
30463 case dw_val_class_wide_int:
30464 return *valx2->v.val_wide == *valy2->v.val_wide;
30465 case dw_val_class_addr:
30466 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30467 default:
30468 gcc_unreachable ();
30470 case DW_OP_bregx:
30471 case DW_OP_bit_piece:
30472 return valx1->v.val_int == valy1->v.val_int
30473 && valx2->v.val_int == valy2->v.val_int;
30474 case DW_OP_addr:
30475 hash_addr:
30476 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30477 case DW_OP_GNU_addr_index:
30478 case DW_OP_GNU_const_index:
30480 rtx ax1 = valx1->val_entry->addr.rtl;
30481 rtx ay1 = valy1->val_entry->addr.rtl;
30482 return rtx_equal_p (ax1, ay1);
30484 case DW_OP_implicit_pointer:
30485 case DW_OP_GNU_implicit_pointer:
30486 return valx1->val_class == dw_val_class_die_ref
30487 && valx1->val_class == valy1->val_class
30488 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30489 && valx2->v.val_int == valy2->v.val_int;
30490 case DW_OP_entry_value:
30491 case DW_OP_GNU_entry_value:
30492 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30493 case DW_OP_const_type:
30494 case DW_OP_GNU_const_type:
30495 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30496 || valx2->val_class != valy2->val_class)
30497 return false;
30498 switch (valx2->val_class)
30500 case dw_val_class_const:
30501 return valx2->v.val_int == valy2->v.val_int;
30502 case dw_val_class_vec:
30503 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30504 && valx2->v.val_vec.length == valy2->v.val_vec.length
30505 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30506 valx2->v.val_vec.elt_size
30507 * valx2->v.val_vec.length) == 0;
30508 case dw_val_class_const_double:
30509 return valx2->v.val_double.low == valy2->v.val_double.low
30510 && valx2->v.val_double.high == valy2->v.val_double.high;
30511 case dw_val_class_wide_int:
30512 return *valx2->v.val_wide == *valy2->v.val_wide;
30513 default:
30514 gcc_unreachable ();
30516 case DW_OP_regval_type:
30517 case DW_OP_deref_type:
30518 case DW_OP_GNU_regval_type:
30519 case DW_OP_GNU_deref_type:
30520 return valx1->v.val_int == valy1->v.val_int
30521 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30522 case DW_OP_convert:
30523 case DW_OP_reinterpret:
30524 case DW_OP_GNU_convert:
30525 case DW_OP_GNU_reinterpret:
30526 if (valx1->val_class != valy1->val_class)
30527 return false;
30528 if (valx1->val_class == dw_val_class_unsigned_const)
30529 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30530 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30531 case DW_OP_GNU_parameter_ref:
30532 return valx1->val_class == dw_val_class_die_ref
30533 && valx1->val_class == valy1->val_class
30534 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30535 default:
30536 /* Other codes have no operands. */
30537 return true;
30541 /* Return true if DWARF location expressions X and Y are the same. */
30543 static inline bool
30544 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30546 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30547 if (x->dw_loc_opc != y->dw_loc_opc
30548 || x->dtprel != y->dtprel
30549 || !compare_loc_operands (x, y))
30550 break;
30551 return x == NULL && y == NULL;
30554 /* Hashtable helpers. */
30556 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30558 static inline hashval_t hash (const dw_loc_list_struct *);
30559 static inline bool equal (const dw_loc_list_struct *,
30560 const dw_loc_list_struct *);
30563 /* Return precomputed hash of location list X. */
30565 inline hashval_t
30566 loc_list_hasher::hash (const dw_loc_list_struct *x)
30568 return x->hash;
30571 /* Return true if location lists A and B are the same. */
30573 inline bool
30574 loc_list_hasher::equal (const dw_loc_list_struct *a,
30575 const dw_loc_list_struct *b)
30577 if (a == b)
30578 return 1;
30579 if (a->hash != b->hash)
30580 return 0;
30581 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30582 if (strcmp (a->begin, b->begin) != 0
30583 || strcmp (a->end, b->end) != 0
30584 || (a->section == NULL) != (b->section == NULL)
30585 || (a->section && strcmp (a->section, b->section) != 0)
30586 || a->vbegin != b->vbegin || a->vend != b->vend
30587 || !compare_locs (a->expr, b->expr))
30588 break;
30589 return a == NULL && b == NULL;
30592 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30595 /* Recursively optimize location lists referenced from DIE
30596 children and share them whenever possible. */
30598 static void
30599 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30601 dw_die_ref c;
30602 dw_attr_node *a;
30603 unsigned ix;
30604 dw_loc_list_struct **slot;
30605 bool drop_locviews = false;
30606 bool has_locviews = false;
30608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30609 if (AT_class (a) == dw_val_class_loc_list)
30611 dw_loc_list_ref list = AT_loc_list (a);
30612 /* TODO: perform some optimizations here, before hashing
30613 it and storing into the hash table. */
30614 hash_loc_list (list);
30615 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30616 if (*slot == NULL)
30618 *slot = list;
30619 if (loc_list_has_views (list))
30620 gcc_assert (list->vl_symbol);
30621 else if (list->vl_symbol)
30623 drop_locviews = true;
30624 list->vl_symbol = NULL;
30627 else
30629 if (list->vl_symbol && !(*slot)->vl_symbol)
30630 drop_locviews = true;
30631 a->dw_attr_val.v.val_loc_list = *slot;
30634 else if (AT_class (a) == dw_val_class_view_list)
30636 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30637 has_locviews = true;
30641 if (drop_locviews && has_locviews)
30642 remove_AT (die, DW_AT_GNU_locviews);
30644 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30648 /* Recursively assign each location list a unique index into the debug_addr
30649 section. */
30651 static void
30652 index_location_lists (dw_die_ref die)
30654 dw_die_ref c;
30655 dw_attr_node *a;
30656 unsigned ix;
30658 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30659 if (AT_class (a) == dw_val_class_loc_list)
30661 dw_loc_list_ref list = AT_loc_list (a);
30662 dw_loc_list_ref curr;
30663 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30665 /* Don't index an entry that has already been indexed
30666 or won't be output. */
30667 if (curr->begin_entry != NULL
30668 || skip_loc_list_entry (curr))
30669 continue;
30671 curr->begin_entry
30672 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
30676 FOR_EACH_CHILD (die, c, index_location_lists (c));
30679 /* Optimize location lists referenced from DIE
30680 children and share them whenever possible. */
30682 static void
30683 optimize_location_lists (dw_die_ref die)
30685 loc_list_hash_type htab (500);
30686 optimize_location_lists_1 (die, &htab);
30689 /* Traverse the limbo die list, and add parent/child links. The only
30690 dies without parents that should be here are concrete instances of
30691 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
30692 For concrete instances, we can get the parent die from the abstract
30693 instance. */
30695 static void
30696 flush_limbo_die_list (void)
30698 limbo_die_node *node;
30700 /* get_context_die calls force_decl_die, which can put new DIEs on the
30701 limbo list in LTO mode when nested functions are put in a different
30702 partition than that of their parent function. */
30703 while ((node = limbo_die_list))
30705 dw_die_ref die = node->die;
30706 limbo_die_list = node->next;
30708 if (die->die_parent == NULL)
30710 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
30712 if (origin && origin->die_parent)
30713 add_child_die (origin->die_parent, die);
30714 else if (is_cu_die (die))
30716 else if (seen_error ())
30717 /* It's OK to be confused by errors in the input. */
30718 add_child_die (comp_unit_die (), die);
30719 else
30721 /* In certain situations, the lexical block containing a
30722 nested function can be optimized away, which results
30723 in the nested function die being orphaned. Likewise
30724 with the return type of that nested function. Force
30725 this to be a child of the containing function.
30727 It may happen that even the containing function got fully
30728 inlined and optimized out. In that case we are lost and
30729 assign the empty child. This should not be big issue as
30730 the function is likely unreachable too. */
30731 gcc_assert (node->created_for);
30733 if (DECL_P (node->created_for))
30734 origin = get_context_die (DECL_CONTEXT (node->created_for));
30735 else if (TYPE_P (node->created_for))
30736 origin = scope_die_for (node->created_for, comp_unit_die ());
30737 else
30738 origin = comp_unit_die ();
30740 add_child_die (origin, die);
30746 /* Reset DIEs so we can output them again. */
30748 static void
30749 reset_dies (dw_die_ref die)
30751 dw_die_ref c;
30753 /* Remove stuff we re-generate. */
30754 die->die_mark = 0;
30755 die->die_offset = 0;
30756 die->die_abbrev = 0;
30757 remove_AT (die, DW_AT_sibling);
30759 FOR_EACH_CHILD (die, c, reset_dies (c));
30762 /* Output stuff that dwarf requires at the end of every file,
30763 and generate the DWARF-2 debugging info. */
30765 static void
30766 dwarf2out_finish (const char *)
30768 comdat_type_node *ctnode;
30769 dw_die_ref main_comp_unit_die;
30770 unsigned char checksum[16];
30771 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
30773 /* Flush out any latecomers to the limbo party. */
30774 flush_limbo_die_list ();
30776 if (inline_entry_data_table)
30777 gcc_assert (inline_entry_data_table->elements () == 0);
30779 if (flag_checking)
30781 verify_die (comp_unit_die ());
30782 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30783 verify_die (node->die);
30786 /* We shouldn't have any symbols with delayed asm names for
30787 DIEs generated after early finish. */
30788 gcc_assert (deferred_asm_name == NULL);
30790 gen_remaining_tmpl_value_param_die_attribute ();
30792 if (flag_generate_lto || flag_generate_offload)
30794 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
30796 /* Prune stuff so that dwarf2out_finish runs successfully
30797 for the fat part of the object. */
30798 reset_dies (comp_unit_die ());
30799 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30800 reset_dies (node->die);
30802 hash_table<comdat_type_hasher> comdat_type_table (100);
30803 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30805 comdat_type_node **slot
30806 = comdat_type_table.find_slot (ctnode, INSERT);
30808 /* Don't reset types twice. */
30809 if (*slot != HTAB_EMPTY_ENTRY)
30810 continue;
30812 /* Add a pointer to the line table for the main compilation unit
30813 so that the debugger can make sense of DW_AT_decl_file
30814 attributes. */
30815 if (debug_info_level >= DINFO_LEVEL_TERSE)
30816 reset_dies (ctnode->root_die);
30818 *slot = ctnode;
30821 /* Reset die CU symbol so we don't output it twice. */
30822 comp_unit_die ()->die_id.die_symbol = NULL;
30824 /* Remove DW_AT_macro from the early output. */
30825 if (have_macinfo)
30826 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30828 /* Remove indirect string decisions. */
30829 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30832 #if ENABLE_ASSERT_CHECKING
30834 dw_die_ref die = comp_unit_die (), c;
30835 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30837 #endif
30838 resolve_addr (comp_unit_die ());
30839 move_marked_base_types ();
30841 /* Initialize sections and labels used for actual assembler output. */
30842 unsigned generation = init_sections_and_labels (false);
30844 /* Traverse the DIE's and add sibling attributes to those DIE's that
30845 have children. */
30846 add_sibling_attributes (comp_unit_die ());
30847 limbo_die_node *node;
30848 for (node = cu_die_list; node; node = node->next)
30849 add_sibling_attributes (node->die);
30850 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30851 add_sibling_attributes (ctnode->root_die);
30853 /* When splitting DWARF info, we put some attributes in the
30854 skeleton compile_unit DIE that remains in the .o, while
30855 most attributes go in the DWO compile_unit_die. */
30856 if (dwarf_split_debug_info)
30858 limbo_die_node *cu;
30859 main_comp_unit_die = gen_compile_unit_die (NULL);
30860 if (dwarf_version >= 5)
30861 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30862 cu = limbo_die_list;
30863 gcc_assert (cu->die == main_comp_unit_die);
30864 limbo_die_list = limbo_die_list->next;
30865 cu->next = cu_die_list;
30866 cu_die_list = cu;
30868 else
30869 main_comp_unit_die = comp_unit_die ();
30871 /* Output a terminator label for the .text section. */
30872 switch_to_section (text_section);
30873 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30874 if (cold_text_section)
30876 switch_to_section (cold_text_section);
30877 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
30880 /* We can only use the low/high_pc attributes if all of the code was
30881 in .text. */
30882 if (!have_multiple_function_sections
30883 || (dwarf_version < 3 && dwarf_strict))
30885 /* Don't add if the CU has no associated code. */
30886 if (text_section_used)
30887 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
30888 text_end_label, true);
30890 else
30892 unsigned fde_idx;
30893 dw_fde_ref fde;
30894 bool range_list_added = false;
30896 if (text_section_used)
30897 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30898 text_end_label, &range_list_added, true);
30899 if (cold_text_section_used)
30900 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30901 cold_end_label, &range_list_added, true);
30903 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30905 if (DECL_IGNORED_P (fde->decl))
30906 continue;
30907 if (!fde->in_std_section)
30908 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30909 fde->dw_fde_end, &range_list_added,
30910 true);
30911 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30912 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30913 fde->dw_fde_second_end, &range_list_added,
30914 true);
30917 if (range_list_added)
30919 /* We need to give .debug_loc and .debug_ranges an appropriate
30920 "base address". Use zero so that these addresses become
30921 absolute. Historically, we've emitted the unexpected
30922 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30923 Emit both to give time for other tools to adapt. */
30924 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30925 if (! dwarf_strict && dwarf_version < 4)
30926 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30928 add_ranges (NULL);
30932 /* AIX Assembler inserts the length, so adjust the reference to match the
30933 offset expected by debuggers. */
30934 strcpy (dl_section_ref, debug_line_section_label);
30935 if (XCOFF_DEBUGGING_INFO)
30936 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30938 if (debug_info_level >= DINFO_LEVEL_TERSE)
30939 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30940 dl_section_ref);
30942 if (have_macinfo)
30943 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30944 macinfo_section_label);
30946 if (dwarf_split_debug_info)
30948 if (have_location_lists)
30950 if (dwarf_version >= 5)
30951 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30952 loc_section_label);
30953 /* optimize_location_lists calculates the size of the lists,
30954 so index them first, and assign indices to the entries.
30955 Although optimize_location_lists will remove entries from
30956 the table, it only does so for duplicates, and therefore
30957 only reduces ref_counts to 1. */
30958 index_location_lists (comp_unit_die ());
30961 if (addr_index_table != NULL)
30963 unsigned int index = 0;
30964 addr_index_table
30965 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30966 (&index);
30970 loc_list_idx = 0;
30971 if (have_location_lists)
30973 optimize_location_lists (comp_unit_die ());
30974 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30975 if (dwarf_version >= 5 && dwarf_split_debug_info)
30976 assign_location_list_indexes (comp_unit_die ());
30979 save_macinfo_strings ();
30981 if (dwarf_split_debug_info)
30983 unsigned int index = 0;
30985 /* Add attributes common to skeleton compile_units and
30986 type_units. Because these attributes include strings, it
30987 must be done before freezing the string table. Top-level
30988 skeleton die attrs are added when the skeleton type unit is
30989 created, so ensure it is created by this point. */
30990 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30991 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30994 /* Output all of the compilation units. We put the main one last so that
30995 the offsets are available to output_pubnames. */
30996 for (node = cu_die_list; node; node = node->next)
30997 output_comp_unit (node->die, 0, NULL);
30999 hash_table<comdat_type_hasher> comdat_type_table (100);
31000 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31002 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31004 /* Don't output duplicate types. */
31005 if (*slot != HTAB_EMPTY_ENTRY)
31006 continue;
31008 /* Add a pointer to the line table for the main compilation unit
31009 so that the debugger can make sense of DW_AT_decl_file
31010 attributes. */
31011 if (debug_info_level >= DINFO_LEVEL_TERSE)
31012 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31013 (!dwarf_split_debug_info
31014 ? dl_section_ref
31015 : debug_skeleton_line_section_label));
31017 output_comdat_type_unit (ctnode);
31018 *slot = ctnode;
31021 if (dwarf_split_debug_info)
31023 int mark;
31024 struct md5_ctx ctx;
31026 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31027 index_rnglists ();
31029 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31030 md5_init_ctx (&ctx);
31031 mark = 0;
31032 die_checksum (comp_unit_die (), &ctx, &mark);
31033 unmark_all_dies (comp_unit_die ());
31034 md5_finish_ctx (&ctx, checksum);
31036 if (dwarf_version < 5)
31038 /* Use the first 8 bytes of the checksum as the dwo_id,
31039 and add it to both comp-unit DIEs. */
31040 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31041 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31044 /* Add the base offset of the ranges table to the skeleton
31045 comp-unit DIE. */
31046 if (!vec_safe_is_empty (ranges_table))
31048 if (dwarf_version >= 5)
31049 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31050 ranges_base_label);
31051 else
31052 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31053 ranges_section_label);
31056 switch_to_section (debug_addr_section);
31057 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31058 output_addr_table ();
31061 /* Output the main compilation unit if non-empty or if .debug_macinfo
31062 or .debug_macro will be emitted. */
31063 output_comp_unit (comp_unit_die (), have_macinfo,
31064 dwarf_split_debug_info ? checksum : NULL);
31066 if (dwarf_split_debug_info && info_section_emitted)
31067 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31069 /* Output the abbreviation table. */
31070 if (vec_safe_length (abbrev_die_table) != 1)
31072 switch_to_section (debug_abbrev_section);
31073 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31074 output_abbrev_section ();
31077 /* Output location list section if necessary. */
31078 if (have_location_lists)
31080 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31081 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31082 /* Output the location lists info. */
31083 switch_to_section (debug_loc_section);
31084 if (dwarf_version >= 5)
31086 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
31087 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
31088 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31089 dw2_asm_output_data (4, 0xffffffff,
31090 "Initial length escape value indicating "
31091 "64-bit DWARF extension");
31092 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31093 "Length of Location Lists");
31094 ASM_OUTPUT_LABEL (asm_out_file, l1);
31095 output_dwarf_version ();
31096 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31097 dw2_asm_output_data (1, 0, "Segment Size");
31098 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31099 "Offset Entry Count");
31101 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31102 if (dwarf_version >= 5 && dwarf_split_debug_info)
31104 unsigned int save_loc_list_idx = loc_list_idx;
31105 loc_list_idx = 0;
31106 output_loclists_offsets (comp_unit_die ());
31107 gcc_assert (save_loc_list_idx == loc_list_idx);
31109 output_location_lists (comp_unit_die ());
31110 if (dwarf_version >= 5)
31111 ASM_OUTPUT_LABEL (asm_out_file, l2);
31114 output_pubtables ();
31116 /* Output the address range information if a CU (.debug_info section)
31117 was emitted. We output an empty table even if we had no functions
31118 to put in it. This because the consumer has no way to tell the
31119 difference between an empty table that we omitted and failure to
31120 generate a table that would have contained data. */
31121 if (info_section_emitted)
31123 switch_to_section (debug_aranges_section);
31124 output_aranges ();
31127 /* Output ranges section if necessary. */
31128 if (!vec_safe_is_empty (ranges_table))
31130 if (dwarf_version >= 5)
31131 output_rnglists (generation);
31132 else
31133 output_ranges ();
31136 /* Have to end the macro section. */
31137 if (have_macinfo)
31139 switch_to_section (debug_macinfo_section);
31140 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31141 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31142 : debug_skeleton_line_section_label, false);
31143 dw2_asm_output_data (1, 0, "End compilation unit");
31146 /* Output the source line correspondence table. We must do this
31147 even if there is no line information. Otherwise, on an empty
31148 translation unit, we will generate a present, but empty,
31149 .debug_info section. IRIX 6.5 `nm' will then complain when
31150 examining the file. This is done late so that any filenames
31151 used by the debug_info section are marked as 'used'. */
31152 switch_to_section (debug_line_section);
31153 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31154 if (! output_asm_line_debug_info ())
31155 output_line_info (false);
31157 if (dwarf_split_debug_info && info_section_emitted)
31159 switch_to_section (debug_skeleton_line_section);
31160 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31161 output_line_info (true);
31164 /* If we emitted any indirect strings, output the string table too. */
31165 if (debug_str_hash || skeleton_debug_str_hash)
31166 output_indirect_strings ();
31167 if (debug_line_str_hash)
31169 switch_to_section (debug_line_str_section);
31170 const enum dwarf_form form = DW_FORM_line_strp;
31171 debug_line_str_hash->traverse<enum dwarf_form,
31172 output_indirect_string> (form);
31176 /* Returns a hash value for X (which really is a variable_value_struct). */
31178 inline hashval_t
31179 variable_value_hasher::hash (variable_value_struct *x)
31181 return (hashval_t) x->decl_id;
31184 /* Return nonzero if decl_id of variable_value_struct X is the same as
31185 UID of decl Y. */
31187 inline bool
31188 variable_value_hasher::equal (variable_value_struct *x, tree y)
31190 return x->decl_id == DECL_UID (y);
31193 /* Helper function for resolve_variable_value, handle
31194 DW_OP_GNU_variable_value in one location expression.
31195 Return true if exprloc has been changed into loclist. */
31197 static bool
31198 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31200 dw_loc_descr_ref next;
31201 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31203 next = loc->dw_loc_next;
31204 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31205 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31206 continue;
31208 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31209 if (DECL_CONTEXT (decl) != current_function_decl)
31210 continue;
31212 dw_die_ref ref = lookup_decl_die (decl);
31213 if (ref)
31215 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31216 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31217 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31218 continue;
31220 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31221 if (l == NULL)
31222 continue;
31223 if (l->dw_loc_next)
31225 if (AT_class (a) != dw_val_class_loc)
31226 continue;
31227 switch (a->dw_attr)
31229 /* Following attributes allow both exprloc and loclist
31230 classes, so we can change them into a loclist. */
31231 case DW_AT_location:
31232 case DW_AT_string_length:
31233 case DW_AT_return_addr:
31234 case DW_AT_data_member_location:
31235 case DW_AT_frame_base:
31236 case DW_AT_segment:
31237 case DW_AT_static_link:
31238 case DW_AT_use_location:
31239 case DW_AT_vtable_elem_location:
31240 if (prev)
31242 prev->dw_loc_next = NULL;
31243 prepend_loc_descr_to_each (l, AT_loc (a));
31245 if (next)
31246 add_loc_descr_to_each (l, next);
31247 a->dw_attr_val.val_class = dw_val_class_loc_list;
31248 a->dw_attr_val.val_entry = NULL;
31249 a->dw_attr_val.v.val_loc_list = l;
31250 have_location_lists = true;
31251 return true;
31252 /* Following attributes allow both exprloc and reference,
31253 so if the whole expression is DW_OP_GNU_variable_value alone
31254 we could transform it into reference. */
31255 case DW_AT_byte_size:
31256 case DW_AT_bit_size:
31257 case DW_AT_lower_bound:
31258 case DW_AT_upper_bound:
31259 case DW_AT_bit_stride:
31260 case DW_AT_count:
31261 case DW_AT_allocated:
31262 case DW_AT_associated:
31263 case DW_AT_byte_stride:
31264 if (prev == NULL && next == NULL)
31265 break;
31266 /* FALLTHRU */
31267 default:
31268 if (dwarf_strict)
31269 continue;
31270 break;
31272 /* Create DW_TAG_variable that we can refer to. */
31273 gen_decl_die (decl, NULL_TREE, NULL,
31274 lookup_decl_die (current_function_decl));
31275 ref = lookup_decl_die (decl);
31276 if (ref)
31278 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31279 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31280 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31282 continue;
31284 if (prev)
31286 prev->dw_loc_next = l->expr;
31287 add_loc_descr (&prev->dw_loc_next, next);
31288 free_loc_descr (loc, NULL);
31289 next = prev->dw_loc_next;
31291 else
31293 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31294 add_loc_descr (&loc, next);
31295 next = loc;
31297 loc = prev;
31299 return false;
31302 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31304 static void
31305 resolve_variable_value (dw_die_ref die)
31307 dw_attr_node *a;
31308 dw_loc_list_ref loc;
31309 unsigned ix;
31311 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31312 switch (AT_class (a))
31314 case dw_val_class_loc:
31315 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31316 break;
31317 /* FALLTHRU */
31318 case dw_val_class_loc_list:
31319 loc = AT_loc_list (a);
31320 gcc_assert (loc);
31321 for (; loc; loc = loc->dw_loc_next)
31322 resolve_variable_value_in_expr (a, loc->expr);
31323 break;
31324 default:
31325 break;
31329 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31330 temporaries in the current function. */
31332 static void
31333 resolve_variable_values (void)
31335 if (!variable_value_hash || !current_function_decl)
31336 return;
31338 struct variable_value_struct *node
31339 = variable_value_hash->find_with_hash (current_function_decl,
31340 DECL_UID (current_function_decl));
31342 if (node == NULL)
31343 return;
31345 unsigned int i;
31346 dw_die_ref die;
31347 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31348 resolve_variable_value (die);
31351 /* Helper function for note_variable_value, handle one location
31352 expression. */
31354 static void
31355 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31357 for (; loc; loc = loc->dw_loc_next)
31358 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31359 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31361 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31362 dw_die_ref ref = lookup_decl_die (decl);
31363 if (! ref && (flag_generate_lto || flag_generate_offload))
31365 /* ??? This is somewhat a hack because we do not create DIEs
31366 for variables not in BLOCK trees early but when generating
31367 early LTO output we need the dw_val_class_decl_ref to be
31368 fully resolved. For fat LTO objects we'd also like to
31369 undo this after LTO dwarf output. */
31370 gcc_assert (DECL_CONTEXT (decl));
31371 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31372 gcc_assert (ctx != NULL);
31373 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31374 ref = lookup_decl_die (decl);
31375 gcc_assert (ref != NULL);
31377 if (ref)
31379 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31380 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31381 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31382 continue;
31384 if (VAR_P (decl)
31385 && DECL_CONTEXT (decl)
31386 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31387 && lookup_decl_die (DECL_CONTEXT (decl)))
31389 if (!variable_value_hash)
31390 variable_value_hash
31391 = hash_table<variable_value_hasher>::create_ggc (10);
31393 tree fndecl = DECL_CONTEXT (decl);
31394 struct variable_value_struct *node;
31395 struct variable_value_struct **slot
31396 = variable_value_hash->find_slot_with_hash (fndecl,
31397 DECL_UID (fndecl),
31398 INSERT);
31399 if (*slot == NULL)
31401 node = ggc_cleared_alloc<variable_value_struct> ();
31402 node->decl_id = DECL_UID (fndecl);
31403 *slot = node;
31405 else
31406 node = *slot;
31408 vec_safe_push (node->dies, die);
31413 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31414 with dw_val_class_decl_ref operand. */
31416 static void
31417 note_variable_value (dw_die_ref die)
31419 dw_die_ref c;
31420 dw_attr_node *a;
31421 dw_loc_list_ref loc;
31422 unsigned ix;
31424 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31425 switch (AT_class (a))
31427 case dw_val_class_loc_list:
31428 loc = AT_loc_list (a);
31429 gcc_assert (loc);
31430 if (!loc->noted_variable_value)
31432 loc->noted_variable_value = 1;
31433 for (; loc; loc = loc->dw_loc_next)
31434 note_variable_value_in_expr (die, loc->expr);
31436 break;
31437 case dw_val_class_loc:
31438 note_variable_value_in_expr (die, AT_loc (a));
31439 break;
31440 default:
31441 break;
31444 /* Mark children. */
31445 FOR_EACH_CHILD (die, c, note_variable_value (c));
31448 /* Perform any cleanups needed after the early debug generation pass
31449 has run. */
31451 static void
31452 dwarf2out_early_finish (const char *filename)
31454 set_early_dwarf s;
31456 /* PCH might result in DW_AT_producer string being restored from the
31457 header compilation, so always fill it with empty string initially
31458 and overwrite only here. */
31459 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31460 producer_string = gen_producer_string ();
31461 producer->dw_attr_val.v.val_str->refcount--;
31462 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31464 /* Add the name for the main input file now. We delayed this from
31465 dwarf2out_init to avoid complications with PCH. */
31466 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31467 add_comp_dir_attribute (comp_unit_die ());
31469 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31470 DW_AT_comp_dir into .debug_line_str section. */
31471 if (!DWARF2_ASM_LINE_DEBUG_INFO
31472 && dwarf_version >= 5
31473 && DWARF5_USE_DEBUG_LINE_STR)
31475 for (int i = 0; i < 2; i++)
31477 dw_attr_node *a = get_AT (comp_unit_die (),
31478 i ? DW_AT_comp_dir : DW_AT_name);
31479 if (a == NULL
31480 || AT_class (a) != dw_val_class_str
31481 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31482 continue;
31484 if (! debug_line_str_hash)
31485 debug_line_str_hash
31486 = hash_table<indirect_string_hasher>::create_ggc (10);
31488 struct indirect_string_node *node
31489 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31490 set_indirect_string (node);
31491 node->form = DW_FORM_line_strp;
31492 a->dw_attr_val.v.val_str->refcount--;
31493 a->dw_attr_val.v.val_str = node;
31497 /* With LTO early dwarf was really finished at compile-time, so make
31498 sure to adjust the phase after annotating the LTRANS CU DIE. */
31499 if (in_lto_p)
31501 early_dwarf_finished = true;
31502 return;
31505 /* Walk through the list of incomplete types again, trying once more to
31506 emit full debugging info for them. */
31507 retry_incomplete_types ();
31509 /* The point here is to flush out the limbo list so that it is empty
31510 and we don't need to stream it for LTO. */
31511 flush_limbo_die_list ();
31513 gen_scheduled_generic_parms_dies ();
31514 gen_remaining_tmpl_value_param_die_attribute ();
31516 /* Add DW_AT_linkage_name for all deferred DIEs. */
31517 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31519 tree decl = node->created_for;
31520 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31521 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31522 ended up in deferred_asm_name before we knew it was
31523 constant and never written to disk. */
31524 && DECL_ASSEMBLER_NAME (decl))
31526 add_linkage_attr (node->die, decl);
31527 move_linkage_attr (node->die);
31530 deferred_asm_name = NULL;
31532 if (flag_eliminate_unused_debug_types)
31533 prune_unused_types ();
31535 /* Generate separate COMDAT sections for type DIEs. */
31536 if (use_debug_types)
31538 break_out_comdat_types (comp_unit_die ());
31540 /* Each new type_unit DIE was added to the limbo die list when created.
31541 Since these have all been added to comdat_type_list, clear the
31542 limbo die list. */
31543 limbo_die_list = NULL;
31545 /* For each new comdat type unit, copy declarations for incomplete
31546 types to make the new unit self-contained (i.e., no direct
31547 references to the main compile unit). */
31548 for (comdat_type_node *ctnode = comdat_type_list;
31549 ctnode != NULL; ctnode = ctnode->next)
31550 copy_decls_for_unworthy_types (ctnode->root_die);
31551 copy_decls_for_unworthy_types (comp_unit_die ());
31553 /* In the process of copying declarations from one unit to another,
31554 we may have left some declarations behind that are no longer
31555 referenced. Prune them. */
31556 prune_unused_types ();
31559 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31560 with dw_val_class_decl_ref operand. */
31561 note_variable_value (comp_unit_die ());
31562 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31563 note_variable_value (node->die);
31564 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31565 ctnode = ctnode->next)
31566 note_variable_value (ctnode->root_die);
31567 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31568 note_variable_value (node->die);
31570 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31571 both the main_cu and all skeleton TUs. Making this call unconditional
31572 would end up either adding a second copy of the AT_pubnames attribute, or
31573 requiring a special case in add_top_level_skeleton_die_attrs. */
31574 if (!dwarf_split_debug_info)
31575 add_AT_pubnames (comp_unit_die ());
31577 /* The early debug phase is now finished. */
31578 early_dwarf_finished = true;
31580 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31581 if (!flag_generate_lto && !flag_generate_offload)
31582 return;
31584 /* Now as we are going to output for LTO initialize sections and labels
31585 to the LTO variants. We don't need a random-seed postfix as other
31586 LTO sections as linking the LTO debug sections into one in a partial
31587 link is fine. */
31588 init_sections_and_labels (true);
31590 /* The output below is modeled after dwarf2out_finish with all
31591 location related output removed and some LTO specific changes.
31592 Some refactoring might make both smaller and easier to match up. */
31594 /* Traverse the DIE's and add add sibling attributes to those DIE's
31595 that have children. */
31596 add_sibling_attributes (comp_unit_die ());
31597 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31598 add_sibling_attributes (node->die);
31599 for (comdat_type_node *ctnode = comdat_type_list;
31600 ctnode != NULL; ctnode = ctnode->next)
31601 add_sibling_attributes (ctnode->root_die);
31603 if (have_macinfo)
31604 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31605 macinfo_section_label);
31607 save_macinfo_strings ();
31609 if (dwarf_split_debug_info)
31611 unsigned int index = 0;
31612 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31615 /* Output all of the compilation units. We put the main one last so that
31616 the offsets are available to output_pubnames. */
31617 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31618 output_comp_unit (node->die, 0, NULL);
31620 hash_table<comdat_type_hasher> comdat_type_table (100);
31621 for (comdat_type_node *ctnode = comdat_type_list;
31622 ctnode != NULL; ctnode = ctnode->next)
31624 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31626 /* Don't output duplicate types. */
31627 if (*slot != HTAB_EMPTY_ENTRY)
31628 continue;
31630 /* Add a pointer to the line table for the main compilation unit
31631 so that the debugger can make sense of DW_AT_decl_file
31632 attributes. */
31633 if (debug_info_level >= DINFO_LEVEL_TERSE)
31634 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31635 (!dwarf_split_debug_info
31636 ? debug_line_section_label
31637 : debug_skeleton_line_section_label));
31639 output_comdat_type_unit (ctnode);
31640 *slot = ctnode;
31643 /* Stick a unique symbol to the main debuginfo section. */
31644 compute_comp_unit_symbol (comp_unit_die ());
31646 /* Output the main compilation unit. We always need it if only for
31647 the CU symbol. */
31648 output_comp_unit (comp_unit_die (), true, NULL);
31650 /* Output the abbreviation table. */
31651 if (vec_safe_length (abbrev_die_table) != 1)
31653 switch_to_section (debug_abbrev_section);
31654 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31655 output_abbrev_section ();
31658 /* Have to end the macro section. */
31659 if (have_macinfo)
31661 /* We have to save macinfo state if we need to output it again
31662 for the FAT part of the object. */
31663 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
31664 if (flag_fat_lto_objects)
31665 macinfo_table = macinfo_table->copy ();
31667 switch_to_section (debug_macinfo_section);
31668 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31669 output_macinfo (debug_skeleton_line_section_label, true);
31670 dw2_asm_output_data (1, 0, "End compilation unit");
31672 /* Emit a skeleton debug_line section. */
31673 switch_to_section (debug_skeleton_line_section);
31674 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31675 output_line_info (true);
31677 if (flag_fat_lto_objects)
31679 vec_free (macinfo_table);
31680 macinfo_table = saved_macinfo_table;
31685 /* If we emitted any indirect strings, output the string table too. */
31686 if (debug_str_hash || skeleton_debug_str_hash)
31687 output_indirect_strings ();
31689 /* Switch back to the text section. */
31690 switch_to_section (text_section);
31693 /* Reset all state within dwarf2out.c so that we can rerun the compiler
31694 within the same process. For use by toplev::finalize. */
31696 void
31697 dwarf2out_c_finalize (void)
31699 last_var_location_insn = NULL;
31700 cached_next_real_insn = NULL;
31701 used_rtx_array = NULL;
31702 incomplete_types = NULL;
31703 decl_scope_table = NULL;
31704 debug_info_section = NULL;
31705 debug_skeleton_info_section = NULL;
31706 debug_abbrev_section = NULL;
31707 debug_skeleton_abbrev_section = NULL;
31708 debug_aranges_section = NULL;
31709 debug_addr_section = NULL;
31710 debug_macinfo_section = NULL;
31711 debug_line_section = NULL;
31712 debug_skeleton_line_section = NULL;
31713 debug_loc_section = NULL;
31714 debug_pubnames_section = NULL;
31715 debug_pubtypes_section = NULL;
31716 debug_str_section = NULL;
31717 debug_line_str_section = NULL;
31718 debug_str_dwo_section = NULL;
31719 debug_str_offsets_section = NULL;
31720 debug_ranges_section = NULL;
31721 debug_frame_section = NULL;
31722 fde_vec = NULL;
31723 debug_str_hash = NULL;
31724 debug_line_str_hash = NULL;
31725 skeleton_debug_str_hash = NULL;
31726 dw2_string_counter = 0;
31727 have_multiple_function_sections = false;
31728 text_section_used = false;
31729 cold_text_section_used = false;
31730 cold_text_section = NULL;
31731 current_unit_personality = NULL;
31733 early_dwarf = false;
31734 early_dwarf_finished = false;
31736 next_die_offset = 0;
31737 single_comp_unit_die = NULL;
31738 comdat_type_list = NULL;
31739 limbo_die_list = NULL;
31740 file_table = NULL;
31741 decl_die_table = NULL;
31742 common_block_die_table = NULL;
31743 decl_loc_table = NULL;
31744 call_arg_locations = NULL;
31745 call_arg_loc_last = NULL;
31746 call_site_count = -1;
31747 tail_call_site_count = -1;
31748 cached_dw_loc_list_table = NULL;
31749 abbrev_die_table = NULL;
31750 delete dwarf_proc_stack_usage_map;
31751 dwarf_proc_stack_usage_map = NULL;
31752 line_info_label_num = 0;
31753 cur_line_info_table = NULL;
31754 text_section_line_info = NULL;
31755 cold_text_section_line_info = NULL;
31756 separate_line_info = NULL;
31757 info_section_emitted = false;
31758 pubname_table = NULL;
31759 pubtype_table = NULL;
31760 macinfo_table = NULL;
31761 ranges_table = NULL;
31762 ranges_by_label = NULL;
31763 rnglist_idx = 0;
31764 have_location_lists = false;
31765 loclabel_num = 0;
31766 poc_label_num = 0;
31767 last_emitted_file = NULL;
31768 label_num = 0;
31769 tmpl_value_parm_die_table = NULL;
31770 generic_type_instances = NULL;
31771 frame_pointer_fb_offset = 0;
31772 frame_pointer_fb_offset_valid = false;
31773 base_types.release ();
31774 XDELETEVEC (producer_string);
31775 producer_string = NULL;
31778 #include "gt-dwarf2out.h"