* tree-vect-loop-manip.c (vect_do_peeling): Do not use
[official-gcc.git] / gcc / dwarf2out.c
blob76a538f1ff97a69a0356feed2e8a9f3630b8971a
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 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 "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260 static GTY(()) int dw2_string_counter;
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
265 /* Whether the default text and cold text sections have been used at all. */
266 static GTY(()) bool text_section_used = false;
267 static GTY(()) bool cold_text_section_used = false;
269 /* The default cold text section. */
270 static GTY(()) section *cold_text_section;
272 /* The DIE for C++14 'auto' in a function return type. */
273 static GTY(()) dw_die_ref auto_die;
275 /* The DIE for C++14 'decltype(auto)' in a function return type. */
276 static GTY(()) dw_die_ref decltype_auto_die;
278 /* Forward declarations for functions defined in this file. */
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
283 /* Personality decl of current unit. Used only when assembler does not support
284 personality CFI. */
285 static GTY(()) rtx current_unit_personality;
287 /* Whether an eh_frame section is required. */
288 static GTY(()) bool do_eh_frame = false;
290 /* .debug_rnglists next index. */
291 static unsigned int rnglist_idx;
293 /* Data and reference forms for relocatable data. */
294 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
295 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
297 #ifndef DEBUG_FRAME_SECTION
298 #define DEBUG_FRAME_SECTION ".debug_frame"
299 #endif
301 #ifndef FUNC_BEGIN_LABEL
302 #define FUNC_BEGIN_LABEL "LFB"
303 #endif
305 #ifndef FUNC_END_LABEL
306 #define FUNC_END_LABEL "LFE"
307 #endif
309 #ifndef PROLOGUE_END_LABEL
310 #define PROLOGUE_END_LABEL "LPE"
311 #endif
313 #ifndef EPILOGUE_BEGIN_LABEL
314 #define EPILOGUE_BEGIN_LABEL "LEB"
315 #endif
317 #ifndef FRAME_BEGIN_LABEL
318 #define FRAME_BEGIN_LABEL "Lframe"
319 #endif
320 #define CIE_AFTER_SIZE_LABEL "LSCIE"
321 #define CIE_END_LABEL "LECIE"
322 #define FDE_LABEL "LSFDE"
323 #define FDE_AFTER_SIZE_LABEL "LASFDE"
324 #define FDE_END_LABEL "LEFDE"
325 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
326 #define LINE_NUMBER_END_LABEL "LELT"
327 #define LN_PROLOG_AS_LABEL "LASLTP"
328 #define LN_PROLOG_END_LABEL "LELTP"
329 #define DIE_LABEL_PREFIX "DW"
331 /* Match the base name of a file to the base name of a compilation unit. */
333 static int
334 matches_main_base (const char *path)
336 /* Cache the last query. */
337 static const char *last_path = NULL;
338 static int last_match = 0;
339 if (path != last_path)
341 const char *base;
342 int length = base_of_path (path, &base);
343 last_path = path;
344 last_match = (length == main_input_baselength
345 && memcmp (base, main_input_basename, length) == 0);
347 return last_match;
350 #ifdef DEBUG_DEBUG_STRUCT
352 static int
353 dump_struct_debug (tree type, enum debug_info_usage usage,
354 enum debug_struct_file criterion, int generic,
355 int matches, int result)
357 /* Find the type name. */
358 tree type_decl = TYPE_STUB_DECL (type);
359 tree t = type_decl;
360 const char *name = 0;
361 if (TREE_CODE (t) == TYPE_DECL)
362 t = DECL_NAME (t);
363 if (t)
364 name = IDENTIFIER_POINTER (t);
366 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
367 criterion,
368 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
369 matches ? "bas" : "hdr",
370 generic ? "gen" : "ord",
371 usage == DINFO_USAGE_DFN ? ";" :
372 usage == DINFO_USAGE_DIR_USE ? "." : "*",
373 result,
374 (void*) type_decl, name);
375 return result;
377 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
378 dump_struct_debug (type, usage, criterion, generic, matches, result)
380 #else
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 (result)
385 #endif
387 /* Get the number of HOST_WIDE_INTs needed to represent the precision
388 of the number. Some constants have a large uniform precision, so
389 we get the precision needed for the actual value of the number. */
391 static unsigned int
392 get_full_len (const wide_int &op)
394 int prec = wi::min_precision (op, UNSIGNED);
395 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
396 / HOST_BITS_PER_WIDE_INT);
399 static bool
400 should_emit_struct_debug (tree type, enum debug_info_usage usage)
402 enum debug_struct_file criterion;
403 tree type_decl;
404 bool generic = lang_hooks.types.generic_p (type);
406 if (generic)
407 criterion = debug_struct_generic[usage];
408 else
409 criterion = debug_struct_ordinary[usage];
411 if (criterion == DINFO_STRUCT_FILE_NONE)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 if (criterion == DINFO_STRUCT_FILE_ANY)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
416 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
418 if (type_decl != NULL)
420 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
423 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
431 switch to the data section instead, and write out a synthetic start label
432 for collect2 the first time around. */
434 static void
435 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 if (eh_frame_section == 0)
439 int flags;
441 if (EH_TABLES_CAN_BE_READ_ONLY)
443 int fde_encoding;
444 int per_encoding;
445 int lsda_encoding;
447 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
448 /*global=*/0);
449 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
450 /*global=*/1);
451 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
452 /*global=*/0);
453 flags = ((! flag_pic
454 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
455 && (fde_encoding & 0x70) != DW_EH_PE_aligned
456 && (per_encoding & 0x70) != DW_EH_PE_absptr
457 && (per_encoding & 0x70) != DW_EH_PE_aligned
458 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
459 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
460 ? 0 : SECTION_WRITE);
462 else
463 flags = SECTION_WRITE;
465 #ifdef EH_FRAME_SECTION_NAME
466 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
467 #else
468 eh_frame_section = ((flags == SECTION_WRITE)
469 ? data_section : readonly_data_section);
470 #endif /* EH_FRAME_SECTION_NAME */
473 switch_to_section (eh_frame_section);
475 #ifdef EH_FRAME_THROUGH_COLLECT2
476 /* We have no special eh_frame section. Emit special labels to guide
477 collect2. */
478 if (!back)
480 tree label = get_file_function_name ("F");
481 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
482 targetm.asm_out.globalize_label (asm_out_file,
483 IDENTIFIER_POINTER (label));
484 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486 #endif
489 /* Switch [BACK] to the eh or debug frame table section, depending on
490 FOR_EH. */
492 static void
493 switch_to_frame_table_section (int for_eh, bool back)
495 if (for_eh)
496 switch_to_eh_frame_section (back);
497 else
499 if (!debug_frame_section)
500 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
501 SECTION_DEBUG, NULL);
502 switch_to_section (debug_frame_section);
506 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
508 enum dw_cfi_oprnd_type
509 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 switch (cfi)
513 case DW_CFA_nop:
514 case DW_CFA_GNU_window_save:
515 case DW_CFA_remember_state:
516 case DW_CFA_restore_state:
517 return dw_cfi_oprnd_unused;
519 case DW_CFA_set_loc:
520 case DW_CFA_advance_loc1:
521 case DW_CFA_advance_loc2:
522 case DW_CFA_advance_loc4:
523 case DW_CFA_MIPS_advance_loc8:
524 return dw_cfi_oprnd_addr;
526 case DW_CFA_offset:
527 case DW_CFA_offset_extended:
528 case DW_CFA_def_cfa:
529 case DW_CFA_offset_extended_sf:
530 case DW_CFA_def_cfa_sf:
531 case DW_CFA_restore:
532 case DW_CFA_restore_extended:
533 case DW_CFA_undefined:
534 case DW_CFA_same_value:
535 case DW_CFA_def_cfa_register:
536 case DW_CFA_register:
537 case DW_CFA_expression:
538 case DW_CFA_val_expression:
539 return dw_cfi_oprnd_reg_num;
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 gcc_unreachable ();
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 switch (cfi)
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
571 case DW_CFA_expression:
572 case DW_CFA_val_expression:
573 return dw_cfi_oprnd_loc;
575 default:
576 return dw_cfi_oprnd_unused;
580 /* Output one FDE. */
582 static void
583 output_fde (dw_fde_ref fde, bool for_eh, bool second,
584 char *section_start_label, int fde_encoding, char *augmentation,
585 bool any_lsda_needed, int lsda_encoding)
587 const char *begin, *end;
588 static unsigned int j;
589 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
591 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
592 /* empty */ 0);
593 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
594 for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
597 if (!XCOFF_DEBUGGING_INFO || for_eh)
599 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
600 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
601 " indicating 64-bit DWARF extension");
602 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
603 "FDE Length");
605 ASM_OUTPUT_LABEL (asm_out_file, l1);
607 if (for_eh)
608 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
609 else
610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
611 debug_frame_section, "FDE CIE offset");
613 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
614 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
616 if (for_eh)
618 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
619 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
620 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
621 "FDE initial location");
622 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
623 end, begin, "FDE address range");
625 else
627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
628 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
631 if (augmentation[0])
633 if (any_lsda_needed)
635 int size = size_of_encoded_value (lsda_encoding);
637 if (lsda_encoding == DW_EH_PE_aligned)
639 int offset = ( 4 /* Length */
640 + 4 /* CIE offset */
641 + 2 * size_of_encoded_value (fde_encoding)
642 + 1 /* Augmentation size */ );
643 int pad = -offset & (PTR_SIZE - 1);
645 size += pad;
646 gcc_assert (size_of_uleb128 (size) == 1);
649 dw2_asm_output_data_uleb128 (size, "Augmentation size");
651 if (fde->uses_eh_lsda)
653 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
654 fde->funcdef_number);
655 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
656 gen_rtx_SYMBOL_REF (Pmode, l1),
657 false,
658 "Language Specific Data Area");
660 else
662 if (lsda_encoding == DW_EH_PE_aligned)
663 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
664 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
665 "Language Specific Data Area (none)");
668 else
669 dw2_asm_output_data_uleb128 (0, "Augmentation size");
672 /* Loop through the Call Frame Instructions associated with this FDE. */
673 fde->dw_fde_current_label = begin;
675 size_t from, until, i;
677 from = 0;
678 until = vec_safe_length (fde->dw_fde_cfi);
680 if (fde->dw_fde_second_begin == NULL)
682 else if (!second)
683 until = fde->dw_fde_switch_cfi_index;
684 else
685 from = fde->dw_fde_switch_cfi_index;
687 for (i = from; i < until; i++)
688 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
691 /* If we are to emit a ref/link from function bodies to their frame tables,
692 do it now. This is typically performed to make sure that tables
693 associated with functions are dragged with them and not discarded in
694 garbage collecting links. We need to do this on a per function basis to
695 cope with -ffunction-sections. */
697 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
698 /* Switch to the function section, emit the ref to the tables, and
699 switch *back* into the table section. */
700 switch_to_section (function_section (fde->decl));
701 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
702 switch_to_frame_table_section (for_eh, true);
703 #endif
705 /* Pad the FDE out to an address sized boundary. */
706 ASM_OUTPUT_ALIGN (asm_out_file,
707 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
708 ASM_OUTPUT_LABEL (asm_out_file, l2);
710 j += 2;
713 /* Return true if frame description entry FDE is needed for EH. */
715 static bool
716 fde_needed_for_eh_p (dw_fde_ref fde)
718 if (flag_asynchronous_unwind_tables)
719 return true;
721 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
722 return true;
724 if (fde->uses_eh_lsda)
725 return true;
727 /* If exceptions are enabled, we have collected nothrow info. */
728 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
729 return false;
731 return true;
734 /* Output the call frame information used to record information
735 that relates to calculating the frame pointer, and records the
736 location of saved registers. */
738 static void
739 output_call_frame_info (int for_eh)
741 unsigned int i;
742 dw_fde_ref fde;
743 dw_cfi_ref cfi;
744 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
745 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
746 bool any_lsda_needed = false;
747 char augmentation[6];
748 int augmentation_size;
749 int fde_encoding = DW_EH_PE_absptr;
750 int per_encoding = DW_EH_PE_absptr;
751 int lsda_encoding = DW_EH_PE_absptr;
752 int return_reg;
753 rtx personality = NULL;
754 int dw_cie_version;
756 /* Don't emit a CIE if there won't be any FDEs. */
757 if (!fde_vec)
758 return;
760 /* Nothing to do if the assembler's doing it all. */
761 if (dwarf2out_do_cfi_asm ())
762 return;
764 /* If we don't have any functions we'll want to unwind out of, don't emit
765 any EH unwind information. If we make FDEs linkonce, we may have to
766 emit an empty label for an FDE that wouldn't otherwise be emitted. We
767 want to avoid having an FDE kept around when the function it refers to
768 is discarded. Example where this matters: a primary function template
769 in C++ requires EH information, an explicit specialization doesn't. */
770 if (for_eh)
772 bool any_eh_needed = false;
774 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
776 if (fde->uses_eh_lsda)
777 any_eh_needed = any_lsda_needed = true;
778 else if (fde_needed_for_eh_p (fde))
779 any_eh_needed = true;
780 else if (TARGET_USES_WEAK_UNWIND_INFO)
781 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
784 if (!any_eh_needed)
785 return;
788 /* We're going to be generating comments, so turn on app. */
789 if (flag_debug_asm)
790 app_enable ();
792 /* Switch to the proper frame section, first time. */
793 switch_to_frame_table_section (for_eh, false);
795 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
796 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
798 /* Output the CIE. */
799 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
800 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
801 if (!XCOFF_DEBUGGING_INFO || for_eh)
803 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
804 dw2_asm_output_data (4, 0xffffffff,
805 "Initial length escape value indicating 64-bit DWARF extension");
806 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
807 "Length of Common Information Entry");
809 ASM_OUTPUT_LABEL (asm_out_file, l1);
811 /* Now that the CIE pointer is PC-relative for EH,
812 use 0 to identify the CIE. */
813 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
814 (for_eh ? 0 : DWARF_CIE_ID),
815 "CIE Identifier Tag");
817 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
818 use CIE version 1, unless that would produce incorrect results
819 due to overflowing the return register column. */
820 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
821 dw_cie_version = 1;
822 if (return_reg >= 256 || dwarf_version > 2)
823 dw_cie_version = 3;
824 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
826 augmentation[0] = 0;
827 augmentation_size = 0;
829 personality = current_unit_personality;
830 if (for_eh)
832 char *p;
834 /* Augmentation:
835 z Indicates that a uleb128 is present to size the
836 augmentation section.
837 L Indicates the encoding (and thus presence) of
838 an LSDA pointer in the FDE augmentation.
839 R Indicates a non-default pointer encoding for
840 FDE code pointers.
841 P Indicates the presence of an encoding + language
842 personality routine in the CIE augmentation. */
844 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
845 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
846 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
848 p = augmentation + 1;
849 if (personality)
851 *p++ = 'P';
852 augmentation_size += 1 + size_of_encoded_value (per_encoding);
853 assemble_external_libcall (personality);
855 if (any_lsda_needed)
857 *p++ = 'L';
858 augmentation_size += 1;
860 if (fde_encoding != DW_EH_PE_absptr)
862 *p++ = 'R';
863 augmentation_size += 1;
865 if (p > augmentation + 1)
867 augmentation[0] = 'z';
868 *p = '\0';
871 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
872 if (personality && per_encoding == DW_EH_PE_aligned)
874 int offset = ( 4 /* Length */
875 + 4 /* CIE Id */
876 + 1 /* CIE version */
877 + strlen (augmentation) + 1 /* Augmentation */
878 + size_of_uleb128 (1) /* Code alignment */
879 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
880 + 1 /* RA column */
881 + 1 /* Augmentation size */
882 + 1 /* Personality encoding */ );
883 int pad = -offset & (PTR_SIZE - 1);
885 augmentation_size += pad;
887 /* Augmentations should be small, so there's scarce need to
888 iterate for a solution. Die if we exceed one uleb128 byte. */
889 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
893 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
894 if (dw_cie_version >= 4)
896 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
897 dw2_asm_output_data (1, 0, "CIE Segment Size");
899 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
900 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
901 "CIE Data Alignment Factor");
903 if (dw_cie_version == 1)
904 dw2_asm_output_data (1, return_reg, "CIE RA Column");
905 else
906 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
908 if (augmentation[0])
910 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
911 if (personality)
913 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
914 eh_data_format_name (per_encoding));
915 dw2_asm_output_encoded_addr_rtx (per_encoding,
916 personality,
917 true, NULL);
920 if (any_lsda_needed)
921 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
922 eh_data_format_name (lsda_encoding));
924 if (fde_encoding != DW_EH_PE_absptr)
925 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
926 eh_data_format_name (fde_encoding));
929 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
930 output_cfi (cfi, NULL, for_eh);
932 /* Pad the CIE out to an address sized boundary. */
933 ASM_OUTPUT_ALIGN (asm_out_file,
934 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
935 ASM_OUTPUT_LABEL (asm_out_file, l2);
937 /* Loop through all of the FDE's. */
938 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
940 unsigned int k;
942 /* Don't emit EH unwind info for leaf functions that don't need it. */
943 if (for_eh && !fde_needed_for_eh_p (fde))
944 continue;
946 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
947 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
948 augmentation, any_lsda_needed, lsda_encoding);
951 if (for_eh && targetm.terminate_dw2_eh_frame_info)
952 dw2_asm_output_data (4, 0, "End of Table");
954 /* Turn off app to make assembly quicker. */
955 if (flag_debug_asm)
956 app_disable ();
959 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
961 static void
962 dwarf2out_do_cfi_startproc (bool second)
964 int enc;
965 rtx ref;
966 rtx personality = get_personality_function (current_function_decl);
968 fprintf (asm_out_file, "\t.cfi_startproc\n");
970 if (personality)
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
973 ref = personality;
975 /* ??? The GAS support isn't entirely consistent. We have to
976 handle indirect support ourselves, but PC-relative is done
977 in the assembler. Further, the assembler can't handle any
978 of the weirder relocation types. */
979 if (enc & DW_EH_PE_indirect)
980 ref = dw2_force_const_mem (ref, true);
982 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
983 output_addr_const (asm_out_file, ref);
984 fputc ('\n', asm_out_file);
987 if (crtl->uses_eh_lsda)
989 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
991 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
992 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
993 current_function_funcdef_no);
994 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
995 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
997 if (enc & DW_EH_PE_indirect)
998 ref = dw2_force_const_mem (ref, true);
1000 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1001 output_addr_const (asm_out_file, ref);
1002 fputc ('\n', asm_out_file);
1006 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1007 this allocation may be done before pass_final. */
1009 dw_fde_ref
1010 dwarf2out_alloc_current_fde (void)
1012 dw_fde_ref fde;
1014 fde = ggc_cleared_alloc<dw_fde_node> ();
1015 fde->decl = current_function_decl;
1016 fde->funcdef_number = current_function_funcdef_no;
1017 fde->fde_index = vec_safe_length (fde_vec);
1018 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1019 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1020 fde->nothrow = crtl->nothrow;
1021 fde->drap_reg = INVALID_REGNUM;
1022 fde->vdrap_reg = INVALID_REGNUM;
1024 /* Record the FDE associated with this function. */
1025 cfun->fde = fde;
1026 vec_safe_push (fde_vec, fde);
1028 return fde;
1031 /* Output a marker (i.e. a label) for the beginning of a function, before
1032 the prologue. */
1034 void
1035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1036 unsigned int column ATTRIBUTE_UNUSED,
1037 const char *file ATTRIBUTE_UNUSED)
1039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1040 char * dup_label;
1041 dw_fde_ref fde;
1042 section *fnsec;
1043 bool do_frame;
1045 current_function_func_begin_label = NULL;
1047 do_frame = dwarf2out_do_frame ();
1049 /* ??? current_function_func_begin_label is also used by except.c for
1050 call-site information. We must emit this label if it might be used. */
1051 if (!do_frame
1052 && (!flag_exceptions
1053 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1054 return;
1056 fnsec = function_section (current_function_decl);
1057 switch_to_section (fnsec);
1058 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1061 current_function_funcdef_no);
1062 dup_label = xstrdup (label);
1063 current_function_func_begin_label = dup_label;
1065 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1066 if (!do_frame)
1067 return;
1069 /* Unlike the debug version, the EH version of frame unwind info is a per-
1070 function setting so we need to record whether we need it for the unit. */
1071 do_eh_frame |= dwarf2out_do_eh_frame ();
1073 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1074 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1075 would include pass_dwarf2_frame. If we've not created the FDE yet,
1076 do so now. */
1077 fde = cfun->fde;
1078 if (fde == NULL)
1079 fde = dwarf2out_alloc_current_fde ();
1081 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1082 fde->dw_fde_begin = dup_label;
1083 fde->dw_fde_current_label = dup_label;
1084 fde->in_std_section = (fnsec == text_section
1085 || (cold_text_section && fnsec == cold_text_section));
1087 /* We only want to output line number information for the genuine dwarf2
1088 prologue case, not the eh frame case. */
1089 #ifdef DWARF2_DEBUGGING_INFO
1090 if (file)
1091 dwarf2out_source_line (line, column, file, 0, true);
1092 #endif
1094 if (dwarf2out_do_cfi_asm ())
1095 dwarf2out_do_cfi_startproc (false);
1096 else
1098 rtx personality = get_personality_function (current_function_decl);
1099 if (!current_unit_personality)
1100 current_unit_personality = personality;
1102 /* We cannot keep a current personality per function as without CFI
1103 asm, at the point where we emit the CFI data, there is no current
1104 function anymore. */
1105 if (personality && current_unit_personality != personality)
1106 sorry ("multiple EH personalities are supported only with assemblers "
1107 "supporting .cfi_personality directive");
1111 /* Output a marker (i.e. a label) for the end of the generated code
1112 for a function prologue. This gets called *after* the prologue code has
1113 been generated. */
1115 void
1116 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1117 const char *file ATTRIBUTE_UNUSED)
1119 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1126 current_function_funcdef_no);
1127 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1130 /* Output a marker (i.e. a label) for the beginning of the generated code
1131 for a function epilogue. This gets called *before* the prologue code has
1132 been generated. */
1134 void
1135 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1136 const char *file ATTRIBUTE_UNUSED)
1138 dw_fde_ref fde = cfun->fde;
1139 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1141 if (fde->dw_fde_vms_begin_epilogue)
1142 return;
1144 /* Output a label to mark the endpoint of the code generated for this
1145 function. */
1146 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1147 current_function_funcdef_no);
1148 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1149 current_function_funcdef_no);
1150 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1153 /* Output a marker (i.e. a label) for the absolute end of the generated code
1154 for a function definition. This gets called *after* the epilogue code has
1155 been generated. */
1157 void
1158 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1159 const char *file ATTRIBUTE_UNUSED)
1161 dw_fde_ref fde;
1162 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1164 last_var_location_insn = NULL;
1165 cached_next_real_insn = NULL;
1167 if (dwarf2out_do_cfi_asm ())
1168 fprintf (asm_out_file, "\t.cfi_endproc\n");
1170 /* Output a label to mark the endpoint of the code generated for this
1171 function. */
1172 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1173 current_function_funcdef_no);
1174 ASM_OUTPUT_LABEL (asm_out_file, label);
1175 fde = cfun->fde;
1176 gcc_assert (fde != NULL);
1177 if (fde->dw_fde_second_begin == NULL)
1178 fde->dw_fde_end = xstrdup (label);
1181 void
1182 dwarf2out_frame_finish (void)
1184 /* Output call frame information. */
1185 if (targetm.debug_unwind_info () == UI_DWARF2)
1186 output_call_frame_info (0);
1188 /* Output another copy for the unwinder. */
1189 if (do_eh_frame)
1190 output_call_frame_info (1);
1193 /* Note that the current function section is being used for code. */
1195 static void
1196 dwarf2out_note_section_used (void)
1198 section *sec = current_function_section ();
1199 if (sec == text_section)
1200 text_section_used = true;
1201 else if (sec == cold_text_section)
1202 cold_text_section_used = true;
1205 static void var_location_switch_text_section (void);
1206 static void set_cur_line_info_table (section *);
1208 void
1209 dwarf2out_switch_text_section (void)
1211 section *sect;
1212 dw_fde_ref fde = cfun->fde;
1214 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1216 if (!in_cold_section_p)
1218 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1222 else
1224 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1225 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1226 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1228 have_multiple_function_sections = true;
1230 /* There is no need to mark used sections when not debugging. */
1231 if (cold_text_section != NULL)
1232 dwarf2out_note_section_used ();
1234 if (dwarf2out_do_cfi_asm ())
1235 fprintf (asm_out_file, "\t.cfi_endproc\n");
1237 /* Now do the real section switch. */
1238 sect = current_function_section ();
1239 switch_to_section (sect);
1241 fde->second_in_std_section
1242 = (sect == text_section
1243 || (cold_text_section && sect == cold_text_section));
1245 if (dwarf2out_do_cfi_asm ())
1246 dwarf2out_do_cfi_startproc (true);
1248 var_location_switch_text_section ();
1250 if (cold_text_section != NULL)
1251 set_cur_line_info_table (sect);
1254 /* And now, the subset of the debugging information support code necessary
1255 for emitting location expressions. */
1257 /* Data about a single source file. */
1258 struct GTY((for_user)) dwarf_file_data {
1259 const char * filename;
1260 int emitted_number;
1263 /* Describe an entry into the .debug_addr section. */
1265 enum ate_kind {
1266 ate_kind_rtx,
1267 ate_kind_rtx_dtprel,
1268 ate_kind_label
1271 struct GTY((for_user)) addr_table_entry {
1272 enum ate_kind kind;
1273 unsigned int refcount;
1274 unsigned int index;
1275 union addr_table_entry_struct_union
1277 rtx GTY ((tag ("0"))) rtl;
1278 char * GTY ((tag ("1"))) label;
1280 GTY ((desc ("%1.kind"))) addr;
1283 /* Location lists are ranges + location descriptions for that range,
1284 so you can track variables that are in different places over
1285 their entire life. */
1286 typedef struct GTY(()) dw_loc_list_struct {
1287 dw_loc_list_ref dw_loc_next;
1288 const char *begin; /* Label and addr_entry for start of range */
1289 addr_table_entry *begin_entry;
1290 const char *end; /* Label for end of range */
1291 char *ll_symbol; /* Label for beginning of location list.
1292 Only on head of list */
1293 const char *section; /* Section this loclist is relative to */
1294 dw_loc_descr_ref expr;
1295 hashval_t hash;
1296 /* True if all addresses in this and subsequent lists are known to be
1297 resolved. */
1298 bool resolved_addr;
1299 /* True if this list has been replaced by dw_loc_next. */
1300 bool replaced;
1301 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1302 section. */
1303 unsigned char emitted : 1;
1304 /* True if hash field is index rather than hash value. */
1305 unsigned char num_assigned : 1;
1306 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1307 unsigned char offset_emitted : 1;
1308 /* True if note_variable_value_in_expr has been called on it. */
1309 unsigned char noted_variable_value : 1;
1310 /* True if the range should be emitted even if begin and end
1311 are the same. */
1312 bool force;
1313 } dw_loc_list_node;
1315 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1316 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1318 /* Convert a DWARF stack opcode into its string name. */
1320 static const char *
1321 dwarf_stack_op_name (unsigned int op)
1323 const char *name = get_DW_OP_name (op);
1325 if (name != NULL)
1326 return name;
1328 return "OP_<unknown>";
1331 /* Return a pointer to a newly allocated location description. Location
1332 descriptions are simple expression terms that can be strung
1333 together to form more complicated location (address) descriptions. */
1335 static inline dw_loc_descr_ref
1336 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1337 unsigned HOST_WIDE_INT oprnd2)
1339 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1341 descr->dw_loc_opc = op;
1342 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1343 descr->dw_loc_oprnd1.val_entry = NULL;
1344 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1345 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1346 descr->dw_loc_oprnd2.val_entry = NULL;
1347 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1349 return descr;
1352 /* Return a pointer to a newly allocated location description for
1353 REG and OFFSET. */
1355 static inline dw_loc_descr_ref
1356 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1358 if (reg <= 31)
1359 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1360 offset, 0);
1361 else
1362 return new_loc_descr (DW_OP_bregx, reg, offset);
1365 /* Add a location description term to a location description expression. */
1367 static inline void
1368 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1370 dw_loc_descr_ref *d;
1372 /* Find the end of the chain. */
1373 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1376 *d = descr;
1379 /* Compare two location operands for exact equality. */
1381 static bool
1382 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1384 if (a->val_class != b->val_class)
1385 return false;
1386 switch (a->val_class)
1388 case dw_val_class_none:
1389 return true;
1390 case dw_val_class_addr:
1391 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1393 case dw_val_class_offset:
1394 case dw_val_class_unsigned_const:
1395 case dw_val_class_const:
1396 case dw_val_class_unsigned_const_implicit:
1397 case dw_val_class_const_implicit:
1398 case dw_val_class_range_list:
1399 /* These are all HOST_WIDE_INT, signed or unsigned. */
1400 return a->v.val_unsigned == b->v.val_unsigned;
1402 case dw_val_class_loc:
1403 return a->v.val_loc == b->v.val_loc;
1404 case dw_val_class_loc_list:
1405 return a->v.val_loc_list == b->v.val_loc_list;
1406 case dw_val_class_die_ref:
1407 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1408 case dw_val_class_fde_ref:
1409 return a->v.val_fde_index == b->v.val_fde_index;
1410 case dw_val_class_lbl_id:
1411 case dw_val_class_lineptr:
1412 case dw_val_class_macptr:
1413 case dw_val_class_loclistsptr:
1414 case dw_val_class_high_pc:
1415 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1416 case dw_val_class_str:
1417 return a->v.val_str == b->v.val_str;
1418 case dw_val_class_flag:
1419 return a->v.val_flag == b->v.val_flag;
1420 case dw_val_class_file:
1421 case dw_val_class_file_implicit:
1422 return a->v.val_file == b->v.val_file;
1423 case dw_val_class_decl_ref:
1424 return a->v.val_decl_ref == b->v.val_decl_ref;
1426 case dw_val_class_const_double:
1427 return (a->v.val_double.high == b->v.val_double.high
1428 && a->v.val_double.low == b->v.val_double.low);
1430 case dw_val_class_wide_int:
1431 return *a->v.val_wide == *b->v.val_wide;
1433 case dw_val_class_vec:
1435 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1436 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1438 return (a_len == b_len
1439 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1442 case dw_val_class_data8:
1443 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1445 case dw_val_class_vms_delta:
1446 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1447 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1449 case dw_val_class_discr_value:
1450 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1451 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1452 case dw_val_class_discr_list:
1453 /* It makes no sense comparing two discriminant value lists. */
1454 return false;
1456 gcc_unreachable ();
1459 /* Compare two location atoms for exact equality. */
1461 static bool
1462 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1464 if (a->dw_loc_opc != b->dw_loc_opc)
1465 return false;
1467 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1468 address size, but since we always allocate cleared storage it
1469 should be zero for other types of locations. */
1470 if (a->dtprel != b->dtprel)
1471 return false;
1473 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1474 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1477 /* Compare two complete location expressions for exact equality. */
1479 bool
1480 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1482 while (1)
1484 if (a == b)
1485 return true;
1486 if (a == NULL || b == NULL)
1487 return false;
1488 if (!loc_descr_equal_p_1 (a, b))
1489 return false;
1491 a = a->dw_loc_next;
1492 b = b->dw_loc_next;
1497 /* Add a constant OFFSET to a location expression. */
1499 static void
1500 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1502 dw_loc_descr_ref loc;
1503 HOST_WIDE_INT *p;
1505 gcc_assert (*list_head != NULL);
1507 if (!offset)
1508 return;
1510 /* Find the end of the chain. */
1511 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1514 p = NULL;
1515 if (loc->dw_loc_opc == DW_OP_fbreg
1516 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1517 p = &loc->dw_loc_oprnd1.v.val_int;
1518 else if (loc->dw_loc_opc == DW_OP_bregx)
1519 p = &loc->dw_loc_oprnd2.v.val_int;
1521 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1522 offset. Don't optimize if an signed integer overflow would happen. */
1523 if (p != NULL
1524 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1525 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1526 *p += offset;
1528 else if (offset > 0)
1529 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1531 else
1533 loc->dw_loc_next
1534 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1535 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1539 /* Add a constant OFFSET to a location list. */
1541 static void
1542 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1544 dw_loc_list_ref d;
1545 for (d = list_head; d != NULL; d = d->dw_loc_next)
1546 loc_descr_plus_const (&d->expr, offset);
1549 #define DWARF_REF_SIZE \
1550 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1552 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1553 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1554 DW_FORM_data16 with 128 bits. */
1555 #define DWARF_LARGEST_DATA_FORM_BITS \
1556 (dwarf_version >= 5 ? 128 : 64)
1558 /* Utility inline function for construction of ops that were GNU extension
1559 before DWARF 5. */
1560 static inline enum dwarf_location_atom
1561 dwarf_OP (enum dwarf_location_atom op)
1563 switch (op)
1565 case DW_OP_implicit_pointer:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_implicit_pointer;
1568 break;
1570 case DW_OP_entry_value:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_entry_value;
1573 break;
1575 case DW_OP_const_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_const_type;
1578 break;
1580 case DW_OP_regval_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_regval_type;
1583 break;
1585 case DW_OP_deref_type:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_deref_type;
1588 break;
1590 case DW_OP_convert:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_convert;
1593 break;
1595 case DW_OP_reinterpret:
1596 if (dwarf_version < 5)
1597 return DW_OP_GNU_reinterpret;
1598 break;
1600 default:
1601 break;
1603 return op;
1606 /* Similarly for attributes. */
1607 static inline enum dwarf_attribute
1608 dwarf_AT (enum dwarf_attribute at)
1610 switch (at)
1612 case DW_AT_call_return_pc:
1613 if (dwarf_version < 5)
1614 return DW_AT_low_pc;
1615 break;
1617 case DW_AT_call_tail_call:
1618 if (dwarf_version < 5)
1619 return DW_AT_GNU_tail_call;
1620 break;
1622 case DW_AT_call_origin:
1623 if (dwarf_version < 5)
1624 return DW_AT_abstract_origin;
1625 break;
1627 case DW_AT_call_target:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target;
1630 break;
1632 case DW_AT_call_target_clobbered:
1633 if (dwarf_version < 5)
1634 return DW_AT_GNU_call_site_target_clobbered;
1635 break;
1637 case DW_AT_call_parameter:
1638 if (dwarf_version < 5)
1639 return DW_AT_abstract_origin;
1640 break;
1642 case DW_AT_call_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_value;
1645 break;
1647 case DW_AT_call_data_value:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_call_site_data_value;
1650 break;
1652 case DW_AT_call_all_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_call_sites;
1655 break;
1657 case DW_AT_call_all_tail_calls:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_all_tail_call_sites;
1660 break;
1662 case DW_AT_dwo_name:
1663 if (dwarf_version < 5)
1664 return DW_AT_GNU_dwo_name;
1665 break;
1667 default:
1668 break;
1670 return at;
1673 /* And similarly for tags. */
1674 static inline enum dwarf_tag
1675 dwarf_TAG (enum dwarf_tag tag)
1677 switch (tag)
1679 case DW_TAG_call_site:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site;
1682 break;
1684 case DW_TAG_call_site_parameter:
1685 if (dwarf_version < 5)
1686 return DW_TAG_GNU_call_site_parameter;
1687 break;
1689 default:
1690 break;
1692 return tag;
1695 static unsigned long int get_base_type_offset (dw_die_ref);
1697 /* Return the size of a location descriptor. */
1699 static unsigned long
1700 size_of_loc_descr (dw_loc_descr_ref loc)
1702 unsigned long size = 1;
1704 switch (loc->dw_loc_opc)
1706 case DW_OP_addr:
1707 size += DWARF2_ADDR_SIZE;
1708 break;
1709 case DW_OP_GNU_addr_index:
1710 case DW_OP_GNU_const_index:
1711 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1712 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1713 break;
1714 case DW_OP_const1u:
1715 case DW_OP_const1s:
1716 size += 1;
1717 break;
1718 case DW_OP_const2u:
1719 case DW_OP_const2s:
1720 size += 2;
1721 break;
1722 case DW_OP_const4u:
1723 case DW_OP_const4s:
1724 size += 4;
1725 break;
1726 case DW_OP_const8u:
1727 case DW_OP_const8s:
1728 size += 8;
1729 break;
1730 case DW_OP_constu:
1731 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1732 break;
1733 case DW_OP_consts:
1734 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1735 break;
1736 case DW_OP_pick:
1737 size += 1;
1738 break;
1739 case DW_OP_plus_uconst:
1740 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1741 break;
1742 case DW_OP_skip:
1743 case DW_OP_bra:
1744 size += 2;
1745 break;
1746 case DW_OP_breg0:
1747 case DW_OP_breg1:
1748 case DW_OP_breg2:
1749 case DW_OP_breg3:
1750 case DW_OP_breg4:
1751 case DW_OP_breg5:
1752 case DW_OP_breg6:
1753 case DW_OP_breg7:
1754 case DW_OP_breg8:
1755 case DW_OP_breg9:
1756 case DW_OP_breg10:
1757 case DW_OP_breg11:
1758 case DW_OP_breg12:
1759 case DW_OP_breg13:
1760 case DW_OP_breg14:
1761 case DW_OP_breg15:
1762 case DW_OP_breg16:
1763 case DW_OP_breg17:
1764 case DW_OP_breg18:
1765 case DW_OP_breg19:
1766 case DW_OP_breg20:
1767 case DW_OP_breg21:
1768 case DW_OP_breg22:
1769 case DW_OP_breg23:
1770 case DW_OP_breg24:
1771 case DW_OP_breg25:
1772 case DW_OP_breg26:
1773 case DW_OP_breg27:
1774 case DW_OP_breg28:
1775 case DW_OP_breg29:
1776 case DW_OP_breg30:
1777 case DW_OP_breg31:
1778 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1779 break;
1780 case DW_OP_regx:
1781 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1782 break;
1783 case DW_OP_fbreg:
1784 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1785 break;
1786 case DW_OP_bregx:
1787 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1788 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1789 break;
1790 case DW_OP_piece:
1791 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1792 break;
1793 case DW_OP_bit_piece:
1794 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1795 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1796 break;
1797 case DW_OP_deref_size:
1798 case DW_OP_xderef_size:
1799 size += 1;
1800 break;
1801 case DW_OP_call2:
1802 size += 2;
1803 break;
1804 case DW_OP_call4:
1805 size += 4;
1806 break;
1807 case DW_OP_call_ref:
1808 case DW_OP_GNU_variable_value:
1809 size += DWARF_REF_SIZE;
1810 break;
1811 case DW_OP_implicit_value:
1812 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1813 + loc->dw_loc_oprnd1.v.val_unsigned;
1814 break;
1815 case DW_OP_implicit_pointer:
1816 case DW_OP_GNU_implicit_pointer:
1817 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1818 break;
1819 case DW_OP_entry_value:
1820 case DW_OP_GNU_entry_value:
1822 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1823 size += size_of_uleb128 (op_size) + op_size;
1824 break;
1826 case DW_OP_const_type:
1827 case DW_OP_GNU_const_type:
1829 unsigned long o
1830 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1831 size += size_of_uleb128 (o) + 1;
1832 switch (loc->dw_loc_oprnd2.val_class)
1834 case dw_val_class_vec:
1835 size += loc->dw_loc_oprnd2.v.val_vec.length
1836 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1837 break;
1838 case dw_val_class_const:
1839 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1840 break;
1841 case dw_val_class_const_double:
1842 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1843 break;
1844 case dw_val_class_wide_int:
1845 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1846 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1847 break;
1848 default:
1849 gcc_unreachable ();
1851 break;
1853 case DW_OP_regval_type:
1854 case DW_OP_GNU_regval_type:
1856 unsigned long o
1857 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1858 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1859 + size_of_uleb128 (o);
1861 break;
1862 case DW_OP_deref_type:
1863 case DW_OP_GNU_deref_type:
1865 unsigned long o
1866 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1867 size += 1 + size_of_uleb128 (o);
1869 break;
1870 case DW_OP_convert:
1871 case DW_OP_reinterpret:
1872 case DW_OP_GNU_convert:
1873 case DW_OP_GNU_reinterpret:
1874 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1875 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1876 else
1878 unsigned long o
1879 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1880 size += size_of_uleb128 (o);
1882 break;
1883 case DW_OP_GNU_parameter_ref:
1884 size += 4;
1885 break;
1886 default:
1887 break;
1890 return size;
1893 /* Return the size of a series of location descriptors. */
1895 unsigned long
1896 size_of_locs (dw_loc_descr_ref loc)
1898 dw_loc_descr_ref l;
1899 unsigned long size;
1901 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1902 field, to avoid writing to a PCH file. */
1903 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1905 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1906 break;
1907 size += size_of_loc_descr (l);
1909 if (! l)
1910 return size;
1912 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1914 l->dw_loc_addr = size;
1915 size += size_of_loc_descr (l);
1918 return size;
1921 /* Return the size of the value in a DW_AT_discr_value attribute. */
1923 static int
1924 size_of_discr_value (dw_discr_value *discr_value)
1926 if (discr_value->pos)
1927 return size_of_uleb128 (discr_value->v.uval);
1928 else
1929 return size_of_sleb128 (discr_value->v.sval);
1932 /* Return the size of the value in a DW_AT_discr_list attribute. */
1934 static int
1935 size_of_discr_list (dw_discr_list_ref discr_list)
1937 int size = 0;
1939 for (dw_discr_list_ref list = discr_list;
1940 list != NULL;
1941 list = list->dw_discr_next)
1943 /* One byte for the discriminant value descriptor, and then one or two
1944 LEB128 numbers, depending on whether it's a single case label or a
1945 range label. */
1946 size += 1;
1947 size += size_of_discr_value (&list->dw_discr_lower_bound);
1948 if (list->dw_discr_range != 0)
1949 size += size_of_discr_value (&list->dw_discr_upper_bound);
1951 return size;
1954 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1955 static void get_ref_die_offset_label (char *, dw_die_ref);
1956 static unsigned long int get_ref_die_offset (dw_die_ref);
1958 /* Output location description stack opcode's operands (if any).
1959 The for_eh_or_skip parameter controls whether register numbers are
1960 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1961 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1962 info). This should be suppressed for the cases that have not been converted
1963 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1965 static void
1966 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1968 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1969 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1971 switch (loc->dw_loc_opc)
1973 #ifdef DWARF2_DEBUGGING_INFO
1974 case DW_OP_const2u:
1975 case DW_OP_const2s:
1976 dw2_asm_output_data (2, val1->v.val_int, NULL);
1977 break;
1978 case DW_OP_const4u:
1979 if (loc->dtprel)
1981 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1982 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1983 val1->v.val_addr);
1984 fputc ('\n', asm_out_file);
1985 break;
1987 /* FALLTHRU */
1988 case DW_OP_const4s:
1989 dw2_asm_output_data (4, val1->v.val_int, NULL);
1990 break;
1991 case DW_OP_const8u:
1992 if (loc->dtprel)
1994 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1995 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1996 val1->v.val_addr);
1997 fputc ('\n', asm_out_file);
1998 break;
2000 /* FALLTHRU */
2001 case DW_OP_const8s:
2002 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2003 dw2_asm_output_data (8, val1->v.val_int, NULL);
2004 break;
2005 case DW_OP_skip:
2006 case DW_OP_bra:
2008 int offset;
2010 gcc_assert (val1->val_class == dw_val_class_loc);
2011 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2013 dw2_asm_output_data (2, offset, NULL);
2015 break;
2016 case DW_OP_implicit_value:
2017 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2018 switch (val2->val_class)
2020 case dw_val_class_const:
2021 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2022 break;
2023 case dw_val_class_vec:
2025 unsigned int elt_size = val2->v.val_vec.elt_size;
2026 unsigned int len = val2->v.val_vec.length;
2027 unsigned int i;
2028 unsigned char *p;
2030 if (elt_size > sizeof (HOST_WIDE_INT))
2032 elt_size /= 2;
2033 len *= 2;
2035 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2036 i < len;
2037 i++, p += elt_size)
2038 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2039 "fp or vector constant word %u", i);
2041 break;
2042 case dw_val_class_const_double:
2044 unsigned HOST_WIDE_INT first, second;
2046 if (WORDS_BIG_ENDIAN)
2048 first = val2->v.val_double.high;
2049 second = val2->v.val_double.low;
2051 else
2053 first = val2->v.val_double.low;
2054 second = val2->v.val_double.high;
2056 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2057 first, NULL);
2058 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2059 second, NULL);
2061 break;
2062 case dw_val_class_wide_int:
2064 int i;
2065 int len = get_full_len (*val2->v.val_wide);
2066 if (WORDS_BIG_ENDIAN)
2067 for (i = len - 1; i >= 0; --i)
2068 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2069 val2->v.val_wide->elt (i), NULL);
2070 else
2071 for (i = 0; i < len; ++i)
2072 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2073 val2->v.val_wide->elt (i), NULL);
2075 break;
2076 case dw_val_class_addr:
2077 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2078 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2079 break;
2080 default:
2081 gcc_unreachable ();
2083 break;
2084 #else
2085 case DW_OP_const2u:
2086 case DW_OP_const2s:
2087 case DW_OP_const4u:
2088 case DW_OP_const4s:
2089 case DW_OP_const8u:
2090 case DW_OP_const8s:
2091 case DW_OP_skip:
2092 case DW_OP_bra:
2093 case DW_OP_implicit_value:
2094 /* We currently don't make any attempt to make sure these are
2095 aligned properly like we do for the main unwind info, so
2096 don't support emitting things larger than a byte if we're
2097 only doing unwinding. */
2098 gcc_unreachable ();
2099 #endif
2100 case DW_OP_const1u:
2101 case DW_OP_const1s:
2102 dw2_asm_output_data (1, val1->v.val_int, NULL);
2103 break;
2104 case DW_OP_constu:
2105 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2106 break;
2107 case DW_OP_consts:
2108 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2109 break;
2110 case DW_OP_pick:
2111 dw2_asm_output_data (1, val1->v.val_int, NULL);
2112 break;
2113 case DW_OP_plus_uconst:
2114 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115 break;
2116 case DW_OP_breg0:
2117 case DW_OP_breg1:
2118 case DW_OP_breg2:
2119 case DW_OP_breg3:
2120 case DW_OP_breg4:
2121 case DW_OP_breg5:
2122 case DW_OP_breg6:
2123 case DW_OP_breg7:
2124 case DW_OP_breg8:
2125 case DW_OP_breg9:
2126 case DW_OP_breg10:
2127 case DW_OP_breg11:
2128 case DW_OP_breg12:
2129 case DW_OP_breg13:
2130 case DW_OP_breg14:
2131 case DW_OP_breg15:
2132 case DW_OP_breg16:
2133 case DW_OP_breg17:
2134 case DW_OP_breg18:
2135 case DW_OP_breg19:
2136 case DW_OP_breg20:
2137 case DW_OP_breg21:
2138 case DW_OP_breg22:
2139 case DW_OP_breg23:
2140 case DW_OP_breg24:
2141 case DW_OP_breg25:
2142 case DW_OP_breg26:
2143 case DW_OP_breg27:
2144 case DW_OP_breg28:
2145 case DW_OP_breg29:
2146 case DW_OP_breg30:
2147 case DW_OP_breg31:
2148 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2149 break;
2150 case DW_OP_regx:
2152 unsigned r = val1->v.val_unsigned;
2153 if (for_eh_or_skip >= 0)
2154 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2155 gcc_assert (size_of_uleb128 (r)
2156 == size_of_uleb128 (val1->v.val_unsigned));
2157 dw2_asm_output_data_uleb128 (r, NULL);
2159 break;
2160 case DW_OP_fbreg:
2161 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2162 break;
2163 case DW_OP_bregx:
2165 unsigned r = val1->v.val_unsigned;
2166 if (for_eh_or_skip >= 0)
2167 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2168 gcc_assert (size_of_uleb128 (r)
2169 == size_of_uleb128 (val1->v.val_unsigned));
2170 dw2_asm_output_data_uleb128 (r, NULL);
2171 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2173 break;
2174 case DW_OP_piece:
2175 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2176 break;
2177 case DW_OP_bit_piece:
2178 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2179 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2180 break;
2181 case DW_OP_deref_size:
2182 case DW_OP_xderef_size:
2183 dw2_asm_output_data (1, val1->v.val_int, NULL);
2184 break;
2186 case DW_OP_addr:
2187 if (loc->dtprel)
2189 if (targetm.asm_out.output_dwarf_dtprel)
2191 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2192 DWARF2_ADDR_SIZE,
2193 val1->v.val_addr);
2194 fputc ('\n', asm_out_file);
2196 else
2197 gcc_unreachable ();
2199 else
2201 #ifdef DWARF2_DEBUGGING_INFO
2202 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2203 #else
2204 gcc_unreachable ();
2205 #endif
2207 break;
2209 case DW_OP_GNU_addr_index:
2210 case DW_OP_GNU_const_index:
2211 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2212 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2213 "(index into .debug_addr)");
2214 break;
2216 case DW_OP_call2:
2217 case DW_OP_call4:
2219 unsigned long die_offset
2220 = get_ref_die_offset (val1->v.val_die_ref.die);
2221 /* Make sure the offset has been computed and that we can encode it as
2222 an operand. */
2223 gcc_assert (die_offset > 0
2224 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2225 ? 0xffff
2226 : 0xffffffff));
2227 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2228 die_offset, NULL);
2230 break;
2232 case DW_OP_call_ref:
2233 case DW_OP_GNU_variable_value:
2235 char label[MAX_ARTIFICIAL_LABEL_BYTES
2236 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2237 gcc_assert (val1->val_class == dw_val_class_die_ref);
2238 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2239 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2241 break;
2243 case DW_OP_implicit_pointer:
2244 case DW_OP_GNU_implicit_pointer:
2246 char label[MAX_ARTIFICIAL_LABEL_BYTES
2247 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2248 gcc_assert (val1->val_class == dw_val_class_die_ref);
2249 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2250 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2251 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2253 break;
2255 case DW_OP_entry_value:
2256 case DW_OP_GNU_entry_value:
2257 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2258 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2259 break;
2261 case DW_OP_const_type:
2262 case DW_OP_GNU_const_type:
2264 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2265 gcc_assert (o);
2266 dw2_asm_output_data_uleb128 (o, NULL);
2267 switch (val2->val_class)
2269 case dw_val_class_const:
2270 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2271 dw2_asm_output_data (1, l, NULL);
2272 dw2_asm_output_data (l, val2->v.val_int, NULL);
2273 break;
2274 case dw_val_class_vec:
2276 unsigned int elt_size = val2->v.val_vec.elt_size;
2277 unsigned int len = val2->v.val_vec.length;
2278 unsigned int i;
2279 unsigned char *p;
2281 l = len * elt_size;
2282 dw2_asm_output_data (1, l, NULL);
2283 if (elt_size > sizeof (HOST_WIDE_INT))
2285 elt_size /= 2;
2286 len *= 2;
2288 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2289 i < len;
2290 i++, p += elt_size)
2291 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2292 "fp or vector constant word %u", i);
2294 break;
2295 case dw_val_class_const_double:
2297 unsigned HOST_WIDE_INT first, second;
2298 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2300 dw2_asm_output_data (1, 2 * l, NULL);
2301 if (WORDS_BIG_ENDIAN)
2303 first = val2->v.val_double.high;
2304 second = val2->v.val_double.low;
2306 else
2308 first = val2->v.val_double.low;
2309 second = val2->v.val_double.high;
2311 dw2_asm_output_data (l, first, NULL);
2312 dw2_asm_output_data (l, second, NULL);
2314 break;
2315 case dw_val_class_wide_int:
2317 int i;
2318 int len = get_full_len (*val2->v.val_wide);
2319 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2321 dw2_asm_output_data (1, len * l, NULL);
2322 if (WORDS_BIG_ENDIAN)
2323 for (i = len - 1; i >= 0; --i)
2324 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2325 else
2326 for (i = 0; i < len; ++i)
2327 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2329 break;
2330 default:
2331 gcc_unreachable ();
2334 break;
2335 case DW_OP_regval_type:
2336 case DW_OP_GNU_regval_type:
2338 unsigned r = val1->v.val_unsigned;
2339 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2340 gcc_assert (o);
2341 if (for_eh_or_skip >= 0)
2343 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2344 gcc_assert (size_of_uleb128 (r)
2345 == size_of_uleb128 (val1->v.val_unsigned));
2347 dw2_asm_output_data_uleb128 (r, NULL);
2348 dw2_asm_output_data_uleb128 (o, NULL);
2350 break;
2351 case DW_OP_deref_type:
2352 case DW_OP_GNU_deref_type:
2354 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2355 gcc_assert (o);
2356 dw2_asm_output_data (1, val1->v.val_int, NULL);
2357 dw2_asm_output_data_uleb128 (o, NULL);
2359 break;
2360 case DW_OP_convert:
2361 case DW_OP_reinterpret:
2362 case DW_OP_GNU_convert:
2363 case DW_OP_GNU_reinterpret:
2364 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2365 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2366 else
2368 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2369 gcc_assert (o);
2370 dw2_asm_output_data_uleb128 (o, NULL);
2372 break;
2374 case DW_OP_GNU_parameter_ref:
2376 unsigned long o;
2377 gcc_assert (val1->val_class == dw_val_class_die_ref);
2378 o = get_ref_die_offset (val1->v.val_die_ref.die);
2379 dw2_asm_output_data (4, o, NULL);
2381 break;
2383 default:
2384 /* Other codes have no operands. */
2385 break;
2389 /* Output a sequence of location operations.
2390 The for_eh_or_skip parameter controls whether register numbers are
2391 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2392 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2393 info). This should be suppressed for the cases that have not been converted
2394 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2396 void
2397 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2399 for (; loc != NULL; loc = loc->dw_loc_next)
2401 enum dwarf_location_atom opc = loc->dw_loc_opc;
2402 /* Output the opcode. */
2403 if (for_eh_or_skip >= 0
2404 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2406 unsigned r = (opc - DW_OP_breg0);
2407 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2408 gcc_assert (r <= 31);
2409 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2411 else if (for_eh_or_skip >= 0
2412 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2414 unsigned r = (opc - DW_OP_reg0);
2415 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2416 gcc_assert (r <= 31);
2417 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2420 dw2_asm_output_data (1, opc,
2421 "%s", dwarf_stack_op_name (opc));
2423 /* Output the operand(s) (if any). */
2424 output_loc_operands (loc, for_eh_or_skip);
2428 /* Output location description stack opcode's operands (if any).
2429 The output is single bytes on a line, suitable for .cfi_escape. */
2431 static void
2432 output_loc_operands_raw (dw_loc_descr_ref loc)
2434 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2435 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2437 switch (loc->dw_loc_opc)
2439 case DW_OP_addr:
2440 case DW_OP_GNU_addr_index:
2441 case DW_OP_GNU_const_index:
2442 case DW_OP_implicit_value:
2443 /* We cannot output addresses in .cfi_escape, only bytes. */
2444 gcc_unreachable ();
2446 case DW_OP_const1u:
2447 case DW_OP_const1s:
2448 case DW_OP_pick:
2449 case DW_OP_deref_size:
2450 case DW_OP_xderef_size:
2451 fputc (',', asm_out_file);
2452 dw2_asm_output_data_raw (1, val1->v.val_int);
2453 break;
2455 case DW_OP_const2u:
2456 case DW_OP_const2s:
2457 fputc (',', asm_out_file);
2458 dw2_asm_output_data_raw (2, val1->v.val_int);
2459 break;
2461 case DW_OP_const4u:
2462 case DW_OP_const4s:
2463 fputc (',', asm_out_file);
2464 dw2_asm_output_data_raw (4, val1->v.val_int);
2465 break;
2467 case DW_OP_const8u:
2468 case DW_OP_const8s:
2469 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2470 fputc (',', asm_out_file);
2471 dw2_asm_output_data_raw (8, val1->v.val_int);
2472 break;
2474 case DW_OP_skip:
2475 case DW_OP_bra:
2477 int offset;
2479 gcc_assert (val1->val_class == dw_val_class_loc);
2480 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2482 fputc (',', asm_out_file);
2483 dw2_asm_output_data_raw (2, offset);
2485 break;
2487 case DW_OP_regx:
2489 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2490 gcc_assert (size_of_uleb128 (r)
2491 == size_of_uleb128 (val1->v.val_unsigned));
2492 fputc (',', asm_out_file);
2493 dw2_asm_output_data_uleb128_raw (r);
2495 break;
2497 case DW_OP_constu:
2498 case DW_OP_plus_uconst:
2499 case DW_OP_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 break;
2504 case DW_OP_bit_piece:
2505 fputc (',', asm_out_file);
2506 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2507 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2508 break;
2510 case DW_OP_consts:
2511 case DW_OP_breg0:
2512 case DW_OP_breg1:
2513 case DW_OP_breg2:
2514 case DW_OP_breg3:
2515 case DW_OP_breg4:
2516 case DW_OP_breg5:
2517 case DW_OP_breg6:
2518 case DW_OP_breg7:
2519 case DW_OP_breg8:
2520 case DW_OP_breg9:
2521 case DW_OP_breg10:
2522 case DW_OP_breg11:
2523 case DW_OP_breg12:
2524 case DW_OP_breg13:
2525 case DW_OP_breg14:
2526 case DW_OP_breg15:
2527 case DW_OP_breg16:
2528 case DW_OP_breg17:
2529 case DW_OP_breg18:
2530 case DW_OP_breg19:
2531 case DW_OP_breg20:
2532 case DW_OP_breg21:
2533 case DW_OP_breg22:
2534 case DW_OP_breg23:
2535 case DW_OP_breg24:
2536 case DW_OP_breg25:
2537 case DW_OP_breg26:
2538 case DW_OP_breg27:
2539 case DW_OP_breg28:
2540 case DW_OP_breg29:
2541 case DW_OP_breg30:
2542 case DW_OP_breg31:
2543 case DW_OP_fbreg:
2544 fputc (',', asm_out_file);
2545 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2546 break;
2548 case DW_OP_bregx:
2550 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2551 gcc_assert (size_of_uleb128 (r)
2552 == size_of_uleb128 (val1->v.val_unsigned));
2553 fputc (',', asm_out_file);
2554 dw2_asm_output_data_uleb128_raw (r);
2555 fputc (',', asm_out_file);
2556 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2558 break;
2560 case DW_OP_implicit_pointer:
2561 case DW_OP_entry_value:
2562 case DW_OP_const_type:
2563 case DW_OP_regval_type:
2564 case DW_OP_deref_type:
2565 case DW_OP_convert:
2566 case DW_OP_reinterpret:
2567 case DW_OP_GNU_implicit_pointer:
2568 case DW_OP_GNU_entry_value:
2569 case DW_OP_GNU_const_type:
2570 case DW_OP_GNU_regval_type:
2571 case DW_OP_GNU_deref_type:
2572 case DW_OP_GNU_convert:
2573 case DW_OP_GNU_reinterpret:
2574 case DW_OP_GNU_parameter_ref:
2575 gcc_unreachable ();
2576 break;
2578 default:
2579 /* Other codes have no operands. */
2580 break;
2584 void
2585 output_loc_sequence_raw (dw_loc_descr_ref loc)
2587 while (1)
2589 enum dwarf_location_atom opc = loc->dw_loc_opc;
2590 /* Output the opcode. */
2591 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2593 unsigned r = (opc - DW_OP_breg0);
2594 r = DWARF2_FRAME_REG_OUT (r, 1);
2595 gcc_assert (r <= 31);
2596 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2598 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2600 unsigned r = (opc - DW_OP_reg0);
2601 r = DWARF2_FRAME_REG_OUT (r, 1);
2602 gcc_assert (r <= 31);
2603 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2605 /* Output the opcode. */
2606 fprintf (asm_out_file, "%#x", opc);
2607 output_loc_operands_raw (loc);
2609 if (!loc->dw_loc_next)
2610 break;
2611 loc = loc->dw_loc_next;
2613 fputc (',', asm_out_file);
2617 /* This function builds a dwarf location descriptor sequence from a
2618 dw_cfa_location, adding the given OFFSET to the result of the
2619 expression. */
2621 struct dw_loc_descr_node *
2622 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2624 struct dw_loc_descr_node *head, *tmp;
2626 offset += cfa->offset;
2628 if (cfa->indirect)
2630 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2631 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2632 head->dw_loc_oprnd1.val_entry = NULL;
2633 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2634 add_loc_descr (&head, tmp);
2635 if (offset != 0)
2637 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2638 add_loc_descr (&head, tmp);
2641 else
2642 head = new_reg_loc_descr (cfa->reg, offset);
2644 return head;
2647 /* This function builds a dwarf location descriptor sequence for
2648 the address at OFFSET from the CFA when stack is aligned to
2649 ALIGNMENT byte. */
2651 struct dw_loc_descr_node *
2652 build_cfa_aligned_loc (dw_cfa_location *cfa,
2653 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2655 struct dw_loc_descr_node *head;
2656 unsigned int dwarf_fp
2657 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2659 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2660 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2662 head = new_reg_loc_descr (dwarf_fp, 0);
2663 add_loc_descr (&head, int_loc_descriptor (alignment));
2664 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2665 loc_descr_plus_const (&head, offset);
2667 else
2668 head = new_reg_loc_descr (dwarf_fp, offset);
2669 return head;
2672 /* And now, the support for symbolic debugging information. */
2674 /* .debug_str support. */
2676 static void dwarf2out_init (const char *);
2677 static void dwarf2out_finish (const char *);
2678 static void dwarf2out_early_finish (const char *);
2679 static void dwarf2out_assembly_start (void);
2680 static void dwarf2out_define (unsigned int, const char *);
2681 static void dwarf2out_undef (unsigned int, const char *);
2682 static void dwarf2out_start_source_file (unsigned, const char *);
2683 static void dwarf2out_end_source_file (unsigned);
2684 static void dwarf2out_function_decl (tree);
2685 static void dwarf2out_begin_block (unsigned, unsigned);
2686 static void dwarf2out_end_block (unsigned, unsigned);
2687 static bool dwarf2out_ignore_block (const_tree);
2688 static void dwarf2out_early_global_decl (tree);
2689 static void dwarf2out_late_global_decl (tree);
2690 static void dwarf2out_type_decl (tree, int);
2691 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2692 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2693 dw_die_ref);
2694 static void dwarf2out_abstract_function (tree);
2695 static void dwarf2out_var_location (rtx_insn *);
2696 static void dwarf2out_size_function (tree);
2697 static void dwarf2out_begin_function (tree);
2698 static void dwarf2out_end_function (unsigned int);
2699 static void dwarf2out_register_main_translation_unit (tree unit);
2700 static void dwarf2out_set_name (tree, tree);
2701 static void dwarf2out_register_external_die (tree decl, const char *sym,
2702 unsigned HOST_WIDE_INT off);
2703 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2704 unsigned HOST_WIDE_INT *off);
2706 /* The debug hooks structure. */
2708 const struct gcc_debug_hooks dwarf2_debug_hooks =
2710 dwarf2out_init,
2711 dwarf2out_finish,
2712 dwarf2out_early_finish,
2713 dwarf2out_assembly_start,
2714 dwarf2out_define,
2715 dwarf2out_undef,
2716 dwarf2out_start_source_file,
2717 dwarf2out_end_source_file,
2718 dwarf2out_begin_block,
2719 dwarf2out_end_block,
2720 dwarf2out_ignore_block,
2721 dwarf2out_source_line,
2722 dwarf2out_begin_prologue,
2723 #if VMS_DEBUGGING_INFO
2724 dwarf2out_vms_end_prologue,
2725 dwarf2out_vms_begin_epilogue,
2726 #else
2727 debug_nothing_int_charstar,
2728 debug_nothing_int_charstar,
2729 #endif
2730 dwarf2out_end_epilogue,
2731 dwarf2out_begin_function,
2732 dwarf2out_end_function, /* end_function */
2733 dwarf2out_register_main_translation_unit,
2734 dwarf2out_function_decl, /* function_decl */
2735 dwarf2out_early_global_decl,
2736 dwarf2out_late_global_decl,
2737 dwarf2out_type_decl, /* type_decl */
2738 dwarf2out_imported_module_or_decl,
2739 dwarf2out_die_ref_for_decl,
2740 dwarf2out_register_external_die,
2741 debug_nothing_tree, /* deferred_inline_function */
2742 /* The DWARF 2 backend tries to reduce debugging bloat by not
2743 emitting the abstract description of inline functions until
2744 something tries to reference them. */
2745 dwarf2out_abstract_function, /* outlining_inline_function */
2746 debug_nothing_rtx_code_label, /* label */
2747 debug_nothing_int, /* handle_pch */
2748 dwarf2out_var_location,
2749 dwarf2out_size_function, /* size_function */
2750 dwarf2out_switch_text_section,
2751 dwarf2out_set_name,
2752 1, /* start_end_main_source_file */
2753 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2756 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2758 dwarf2out_init,
2759 debug_nothing_charstar,
2760 debug_nothing_charstar,
2761 dwarf2out_assembly_start,
2762 debug_nothing_int_charstar,
2763 debug_nothing_int_charstar,
2764 debug_nothing_int_charstar,
2765 debug_nothing_int,
2766 debug_nothing_int_int, /* begin_block */
2767 debug_nothing_int_int, /* end_block */
2768 debug_true_const_tree, /* ignore_block */
2769 dwarf2out_source_line, /* source_line */
2770 debug_nothing_int_int_charstar, /* begin_prologue */
2771 debug_nothing_int_charstar, /* end_prologue */
2772 debug_nothing_int_charstar, /* begin_epilogue */
2773 debug_nothing_int_charstar, /* end_epilogue */
2774 debug_nothing_tree, /* begin_function */
2775 debug_nothing_int, /* end_function */
2776 debug_nothing_tree, /* register_main_translation_unit */
2777 debug_nothing_tree, /* function_decl */
2778 debug_nothing_tree, /* early_global_decl */
2779 debug_nothing_tree, /* late_global_decl */
2780 debug_nothing_tree_int, /* type_decl */
2781 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2782 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2783 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2784 debug_nothing_tree, /* deferred_inline_function */
2785 debug_nothing_tree, /* outlining_inline_function */
2786 debug_nothing_rtx_code_label, /* label */
2787 debug_nothing_int, /* handle_pch */
2788 debug_nothing_rtx_insn, /* var_location */
2789 debug_nothing_tree, /* size_function */
2790 debug_nothing_void, /* switch_text_section */
2791 debug_nothing_tree_tree, /* set_name */
2792 0, /* start_end_main_source_file */
2793 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2796 /* NOTE: In the comments in this file, many references are made to
2797 "Debugging Information Entries". This term is abbreviated as `DIE'
2798 throughout the remainder of this file. */
2800 /* An internal representation of the DWARF output is built, and then
2801 walked to generate the DWARF debugging info. The walk of the internal
2802 representation is done after the entire program has been compiled.
2803 The types below are used to describe the internal representation. */
2805 /* Whether to put type DIEs into their own section .debug_types instead
2806 of making them part of the .debug_info section. Only supported for
2807 Dwarf V4 or higher and the user didn't disable them through
2808 -fno-debug-types-section. It is more efficient to put them in a
2809 separate comdat sections since the linker will then be able to
2810 remove duplicates. But not all tools support .debug_types sections
2811 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2812 it is DW_UT_type unit type in .debug_info section. */
2814 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2816 /* Various DIE's use offsets relative to the beginning of the
2817 .debug_info section to refer to each other. */
2819 typedef long int dw_offset;
2821 struct comdat_type_node;
2823 /* The entries in the line_info table more-or-less mirror the opcodes
2824 that are used in the real dwarf line table. Arrays of these entries
2825 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2826 supported. */
2828 enum dw_line_info_opcode {
2829 /* Emit DW_LNE_set_address; the operand is the label index. */
2830 LI_set_address,
2832 /* Emit a row to the matrix with the given line. This may be done
2833 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2834 special opcodes. */
2835 LI_set_line,
2837 /* Emit a DW_LNS_set_file. */
2838 LI_set_file,
2840 /* Emit a DW_LNS_set_column. */
2841 LI_set_column,
2843 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2844 LI_negate_stmt,
2846 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2847 LI_set_prologue_end,
2848 LI_set_epilogue_begin,
2850 /* Emit a DW_LNE_set_discriminator. */
2851 LI_set_discriminator
2854 typedef struct GTY(()) dw_line_info_struct {
2855 enum dw_line_info_opcode opcode;
2856 unsigned int val;
2857 } dw_line_info_entry;
2860 struct GTY(()) dw_line_info_table {
2861 /* The label that marks the end of this section. */
2862 const char *end_label;
2864 /* The values for the last row of the matrix, as collected in the table.
2865 These are used to minimize the changes to the next row. */
2866 unsigned int file_num;
2867 unsigned int line_num;
2868 unsigned int column_num;
2869 int discrim_num;
2870 bool is_stmt;
2871 bool in_use;
2873 vec<dw_line_info_entry, va_gc> *entries;
2877 /* Each DIE attribute has a field specifying the attribute kind,
2878 a link to the next attribute in the chain, and an attribute value.
2879 Attributes are typically linked below the DIE they modify. */
2881 typedef struct GTY(()) dw_attr_struct {
2882 enum dwarf_attribute dw_attr;
2883 dw_val_node dw_attr_val;
2885 dw_attr_node;
2888 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2889 The children of each node form a circular list linked by
2890 die_sib. die_child points to the node *before* the "first" child node. */
2892 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2893 union die_symbol_or_type_node
2895 const char * GTY ((tag ("0"))) die_symbol;
2896 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2898 GTY ((desc ("%0.comdat_type_p"))) die_id;
2899 vec<dw_attr_node, va_gc> *die_attr;
2900 dw_die_ref die_parent;
2901 dw_die_ref die_child;
2902 dw_die_ref die_sib;
2903 dw_die_ref die_definition; /* ref from a specification to its definition */
2904 dw_offset die_offset;
2905 unsigned long die_abbrev;
2906 int die_mark;
2907 unsigned int decl_id;
2908 enum dwarf_tag die_tag;
2909 /* Die is used and must not be pruned as unused. */
2910 BOOL_BITFIELD die_perennial_p : 1;
2911 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2912 /* For an external ref to die_symbol if die_offset contains an extra
2913 offset to that symbol. */
2914 BOOL_BITFIELD with_offset : 1;
2915 /* Whether this DIE was removed from the DIE tree, for example via
2916 prune_unused_types. We don't consider those present from the
2917 DIE lookup routines. */
2918 BOOL_BITFIELD removed : 1;
2919 /* Lots of spare bits. */
2921 die_node;
2923 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2924 static bool early_dwarf;
2925 static bool early_dwarf_finished;
2926 struct set_early_dwarf {
2927 bool saved;
2928 set_early_dwarf () : saved(early_dwarf)
2930 gcc_assert (! early_dwarf_finished);
2931 early_dwarf = true;
2933 ~set_early_dwarf () { early_dwarf = saved; }
2936 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2937 #define FOR_EACH_CHILD(die, c, expr) do { \
2938 c = die->die_child; \
2939 if (c) do { \
2940 c = c->die_sib; \
2941 expr; \
2942 } while (c != die->die_child); \
2943 } while (0)
2945 /* The pubname structure */
2947 typedef struct GTY(()) pubname_struct {
2948 dw_die_ref die;
2949 const char *name;
2951 pubname_entry;
2954 struct GTY(()) dw_ranges {
2955 const char *label;
2956 /* If this is positive, it's a block number, otherwise it's a
2957 bitwise-negated index into dw_ranges_by_label. */
2958 int num;
2959 /* Index for the range list for DW_FORM_rnglistx. */
2960 unsigned int idx : 31;
2961 /* True if this range might be possibly in a different section
2962 from previous entry. */
2963 unsigned int maybe_new_sec : 1;
2966 /* A structure to hold a macinfo entry. */
2968 typedef struct GTY(()) macinfo_struct {
2969 unsigned char code;
2970 unsigned HOST_WIDE_INT lineno;
2971 const char *info;
2973 macinfo_entry;
2976 struct GTY(()) dw_ranges_by_label {
2977 const char *begin;
2978 const char *end;
2981 /* The comdat type node structure. */
2982 struct GTY(()) comdat_type_node
2984 dw_die_ref root_die;
2985 dw_die_ref type_die;
2986 dw_die_ref skeleton_die;
2987 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2988 comdat_type_node *next;
2991 /* A list of DIEs for which we can't determine ancestry (parent_die
2992 field) just yet. Later in dwarf2out_finish we will fill in the
2993 missing bits. */
2994 typedef struct GTY(()) limbo_die_struct {
2995 dw_die_ref die;
2996 /* The tree for which this DIE was created. We use this to
2997 determine ancestry later. */
2998 tree created_for;
2999 struct limbo_die_struct *next;
3001 limbo_die_node;
3003 typedef struct skeleton_chain_struct
3005 dw_die_ref old_die;
3006 dw_die_ref new_die;
3007 struct skeleton_chain_struct *parent;
3009 skeleton_chain_node;
3011 /* Define a macro which returns nonzero for a TYPE_DECL which was
3012 implicitly generated for a type.
3014 Note that, unlike the C front-end (which generates a NULL named
3015 TYPE_DECL node for each complete tagged type, each array type,
3016 and each function type node created) the C++ front-end generates
3017 a _named_ TYPE_DECL node for each tagged type node created.
3018 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3019 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3020 front-end, but for each type, tagged or not. */
3022 #define TYPE_DECL_IS_STUB(decl) \
3023 (DECL_NAME (decl) == NULL_TREE \
3024 || (DECL_ARTIFICIAL (decl) \
3025 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3026 /* This is necessary for stub decls that \
3027 appear in nested inline functions. */ \
3028 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3029 && (decl_ultimate_origin (decl) \
3030 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3032 /* Information concerning the compilation unit's programming
3033 language, and compiler version. */
3035 /* Fixed size portion of the DWARF compilation unit header. */
3036 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3037 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3038 + (dwarf_version >= 5 ? 4 : 3))
3040 /* Fixed size portion of the DWARF comdat type unit header. */
3041 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3043 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3045 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3046 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3047 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3049 /* Fixed size portion of public names info. */
3050 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3052 /* Fixed size portion of the address range info. */
3053 #define DWARF_ARANGES_HEADER_SIZE \
3054 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3055 DWARF2_ADDR_SIZE * 2) \
3056 - DWARF_INITIAL_LENGTH_SIZE)
3058 /* Size of padding portion in the address range info. It must be
3059 aligned to twice the pointer size. */
3060 #define DWARF_ARANGES_PAD_SIZE \
3061 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3062 DWARF2_ADDR_SIZE * 2) \
3063 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3065 /* Use assembler line directives if available. */
3066 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3067 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3068 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3069 #else
3070 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3071 #endif
3072 #endif
3074 /* Minimum line offset in a special line info. opcode.
3075 This value was chosen to give a reasonable range of values. */
3076 #define DWARF_LINE_BASE -10
3078 /* First special line opcode - leave room for the standard opcodes. */
3079 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3081 /* Range of line offsets in a special line info. opcode. */
3082 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3084 /* Flag that indicates the initial value of the is_stmt_start flag.
3085 In the present implementation, we do not mark any lines as
3086 the beginning of a source statement, because that information
3087 is not made available by the GCC front-end. */
3088 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3090 /* Maximum number of operations per instruction bundle. */
3091 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3092 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3093 #endif
3095 /* This location is used by calc_die_sizes() to keep track
3096 the offset of each DIE within the .debug_info section. */
3097 static unsigned long next_die_offset;
3099 /* Record the root of the DIE's built for the current compilation unit. */
3100 static GTY(()) dw_die_ref single_comp_unit_die;
3102 /* A list of type DIEs that have been separated into comdat sections. */
3103 static GTY(()) comdat_type_node *comdat_type_list;
3105 /* A list of CU DIEs that have been separated. */
3106 static GTY(()) limbo_die_node *cu_die_list;
3108 /* A list of DIEs with a NULL parent waiting to be relocated. */
3109 static GTY(()) limbo_die_node *limbo_die_list;
3111 /* A list of DIEs for which we may have to generate
3112 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3113 static GTY(()) limbo_die_node *deferred_asm_name;
3115 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3117 typedef const char *compare_type;
3119 static hashval_t hash (dwarf_file_data *);
3120 static bool equal (dwarf_file_data *, const char *);
3123 /* Filenames referenced by this compilation unit. */
3124 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3126 struct decl_die_hasher : ggc_ptr_hash<die_node>
3128 typedef tree compare_type;
3130 static hashval_t hash (die_node *);
3131 static bool equal (die_node *, tree);
3133 /* A hash table of references to DIE's that describe declarations.
3134 The key is a DECL_UID() which is a unique number identifying each decl. */
3135 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3137 struct GTY ((for_user)) variable_value_struct {
3138 unsigned int decl_id;
3139 vec<dw_die_ref, va_gc> *dies;
3142 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3144 typedef tree compare_type;
3146 static hashval_t hash (variable_value_struct *);
3147 static bool equal (variable_value_struct *, tree);
3149 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3150 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3151 DECL_CONTEXT of the referenced VAR_DECLs. */
3152 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3154 struct block_die_hasher : ggc_ptr_hash<die_struct>
3156 static hashval_t hash (die_struct *);
3157 static bool equal (die_struct *, die_struct *);
3160 /* A hash table of references to DIE's that describe COMMON blocks.
3161 The key is DECL_UID() ^ die_parent. */
3162 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3164 typedef struct GTY(()) die_arg_entry_struct {
3165 dw_die_ref die;
3166 tree arg;
3167 } die_arg_entry;
3170 /* Node of the variable location list. */
3171 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3172 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3173 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3174 in mode of the EXPR_LIST node and first EXPR_LIST operand
3175 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3176 location or NULL for padding. For larger bitsizes,
3177 mode is 0 and first operand is a CONCAT with bitsize
3178 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3179 NULL as second operand. */
3180 rtx GTY (()) loc;
3181 const char * GTY (()) label;
3182 struct var_loc_node * GTY (()) next;
3185 /* Variable location list. */
3186 struct GTY ((for_user)) var_loc_list_def {
3187 struct var_loc_node * GTY (()) first;
3189 /* Pointer to the last but one or last element of the
3190 chained list. If the list is empty, both first and
3191 last are NULL, if the list contains just one node
3192 or the last node certainly is not redundant, it points
3193 to the last node, otherwise points to the last but one.
3194 Do not mark it for GC because it is marked through the chain. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last;
3197 /* Pointer to the last element before section switch,
3198 if NULL, either sections weren't switched or first
3199 is after section switch. */
3200 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3202 /* DECL_UID of the variable decl. */
3203 unsigned int decl_id;
3205 typedef struct var_loc_list_def var_loc_list;
3207 /* Call argument location list. */
3208 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3209 rtx GTY (()) call_arg_loc_note;
3210 const char * GTY (()) label;
3211 tree GTY (()) block;
3212 bool tail_call_p;
3213 rtx GTY (()) symbol_ref;
3214 struct call_arg_loc_node * GTY (()) next;
3218 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3220 typedef const_tree compare_type;
3222 static hashval_t hash (var_loc_list *);
3223 static bool equal (var_loc_list *, const_tree);
3226 /* Table of decl location linked lists. */
3227 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3229 /* Head and tail of call_arg_loc chain. */
3230 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3231 static struct call_arg_loc_node *call_arg_loc_last;
3233 /* Number of call sites in the current function. */
3234 static int call_site_count = -1;
3235 /* Number of tail call sites in the current function. */
3236 static int tail_call_site_count = -1;
3238 /* A cached location list. */
3239 struct GTY ((for_user)) cached_dw_loc_list_def {
3240 /* The DECL_UID of the decl that this entry describes. */
3241 unsigned int decl_id;
3243 /* The cached location list. */
3244 dw_loc_list_ref loc_list;
3246 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3248 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3251 typedef const_tree compare_type;
3253 static hashval_t hash (cached_dw_loc_list *);
3254 static bool equal (cached_dw_loc_list *, const_tree);
3257 /* Table of cached location lists. */
3258 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3260 /* A vector of references to DIE's that are uniquely identified by their tag,
3261 presence/absence of children DIE's, and list of attribute/value pairs. */
3262 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3264 /* A hash map to remember the stack usage for DWARF procedures. The value
3265 stored is the stack size difference between before the DWARF procedure
3266 invokation and after it returned. In other words, for a DWARF procedure
3267 that consumes N stack slots and that pushes M ones, this stores M - N. */
3268 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3270 /* A global counter for generating labels for line number data. */
3271 static unsigned int line_info_label_num;
3273 /* The current table to which we should emit line number information
3274 for the current function. This will be set up at the beginning of
3275 assembly for the function. */
3276 static GTY(()) dw_line_info_table *cur_line_info_table;
3278 /* The two default tables of line number info. */
3279 static GTY(()) dw_line_info_table *text_section_line_info;
3280 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3282 /* The set of all non-default tables of line number info. */
3283 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3285 /* A flag to tell pubnames/types export if there is an info section to
3286 refer to. */
3287 static bool info_section_emitted;
3289 /* A pointer to the base of a table that contains a list of publicly
3290 accessible names. */
3291 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3293 /* A pointer to the base of a table that contains a list of publicly
3294 accessible types. */
3295 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3297 /* A pointer to the base of a table that contains a list of macro
3298 defines/undefines (and file start/end markers). */
3299 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3301 /* True if .debug_macinfo or .debug_macros section is going to be
3302 emitted. */
3303 #define have_macinfo \
3304 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3305 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3306 && !macinfo_table->is_empty ())
3308 /* Vector of dies for which we should generate .debug_ranges info. */
3309 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3311 /* Vector of pairs of labels referenced in ranges_table. */
3312 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3314 /* Whether we have location lists that need outputting */
3315 static GTY(()) bool have_location_lists;
3317 /* Unique label counter. */
3318 static GTY(()) unsigned int loclabel_num;
3320 /* Unique label counter for point-of-call tables. */
3321 static GTY(()) unsigned int poc_label_num;
3323 /* The last file entry emitted by maybe_emit_file(). */
3324 static GTY(()) struct dwarf_file_data * last_emitted_file;
3326 /* Number of internal labels generated by gen_internal_sym(). */
3327 static GTY(()) int label_num;
3329 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3331 /* Instances of generic types for which we need to generate debug
3332 info that describe their generic parameters and arguments. That
3333 generation needs to happen once all types are properly laid out so
3334 we do it at the end of compilation. */
3335 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3337 /* Offset from the "steady-state frame pointer" to the frame base,
3338 within the current function. */
3339 static HOST_WIDE_INT frame_pointer_fb_offset;
3340 static bool frame_pointer_fb_offset_valid;
3342 static vec<dw_die_ref> base_types;
3344 /* Flags to represent a set of attribute classes for attributes that represent
3345 a scalar value (bounds, pointers, ...). */
3346 enum dw_scalar_form
3348 dw_scalar_form_constant = 0x01,
3349 dw_scalar_form_exprloc = 0x02,
3350 dw_scalar_form_reference = 0x04
3353 /* Forward declarations for functions defined in this file. */
3355 static int is_pseudo_reg (const_rtx);
3356 static tree type_main_variant (tree);
3357 static int is_tagged_type (const_tree);
3358 static const char *dwarf_tag_name (unsigned);
3359 static const char *dwarf_attr_name (unsigned);
3360 static const char *dwarf_form_name (unsigned);
3361 static tree decl_ultimate_origin (const_tree);
3362 static tree decl_class_context (tree);
3363 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3364 static inline enum dw_val_class AT_class (dw_attr_node *);
3365 static inline unsigned int AT_index (dw_attr_node *);
3366 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3367 static inline unsigned AT_flag (dw_attr_node *);
3368 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3369 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3370 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3371 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3372 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3373 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3374 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3375 unsigned int, unsigned char *);
3376 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3377 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3378 static inline const char *AT_string (dw_attr_node *);
3379 static enum dwarf_form AT_string_form (dw_attr_node *);
3380 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3381 static void add_AT_specification (dw_die_ref, dw_die_ref);
3382 static inline dw_die_ref AT_ref (dw_attr_node *);
3383 static inline int AT_ref_external (dw_attr_node *);
3384 static inline void set_AT_ref_external (dw_attr_node *, int);
3385 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3386 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3387 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3388 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3389 dw_loc_list_ref);
3390 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3391 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3392 static void remove_addr_table_entry (addr_table_entry *);
3393 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3394 static inline rtx AT_addr (dw_attr_node *);
3395 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3396 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3397 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3398 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3399 const char *);
3400 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3401 unsigned HOST_WIDE_INT);
3402 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3403 unsigned long, bool);
3404 static inline const char *AT_lbl (dw_attr_node *);
3405 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3406 static const char *get_AT_low_pc (dw_die_ref);
3407 static const char *get_AT_hi_pc (dw_die_ref);
3408 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3409 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3410 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3411 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3412 static bool is_cxx (void);
3413 static bool is_cxx (const_tree);
3414 static bool is_fortran (void);
3415 static bool is_ada (void);
3416 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3417 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3418 static void add_child_die (dw_die_ref, dw_die_ref);
3419 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3420 static dw_die_ref lookup_type_die (tree);
3421 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3422 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3423 static void equate_type_number_to_die (tree, dw_die_ref);
3424 static dw_die_ref lookup_decl_die (tree);
3425 static var_loc_list *lookup_decl_loc (const_tree);
3426 static void equate_decl_number_to_die (tree, dw_die_ref);
3427 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3428 static void print_spaces (FILE *);
3429 static void print_die (dw_die_ref, FILE *);
3430 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3431 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3432 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3433 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3434 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3435 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3436 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3437 struct md5_ctx *, int *);
3438 struct checksum_attributes;
3439 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3440 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3441 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3442 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3443 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3444 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3445 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3446 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3447 static int is_type_die (dw_die_ref);
3448 static int is_comdat_die (dw_die_ref);
3449 static inline bool is_template_instantiation (dw_die_ref);
3450 static int is_declaration_die (dw_die_ref);
3451 static int should_move_die_to_comdat (dw_die_ref);
3452 static dw_die_ref clone_as_declaration (dw_die_ref);
3453 static dw_die_ref clone_die (dw_die_ref);
3454 static dw_die_ref clone_tree (dw_die_ref);
3455 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3456 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3457 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3458 static dw_die_ref generate_skeleton (dw_die_ref);
3459 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3460 dw_die_ref,
3461 dw_die_ref);
3462 static void break_out_comdat_types (dw_die_ref);
3463 static void copy_decls_for_unworthy_types (dw_die_ref);
3465 static void add_sibling_attributes (dw_die_ref);
3466 static void output_location_lists (dw_die_ref);
3467 static int constant_size (unsigned HOST_WIDE_INT);
3468 static unsigned long size_of_die (dw_die_ref);
3469 static void calc_die_sizes (dw_die_ref);
3470 static void calc_base_type_die_sizes (void);
3471 static void mark_dies (dw_die_ref);
3472 static void unmark_dies (dw_die_ref);
3473 static void unmark_all_dies (dw_die_ref);
3474 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3475 static unsigned long size_of_aranges (void);
3476 static enum dwarf_form value_format (dw_attr_node *);
3477 static void output_value_format (dw_attr_node *);
3478 static void output_abbrev_section (void);
3479 static void output_die_abbrevs (unsigned long, dw_die_ref);
3480 static void output_die (dw_die_ref);
3481 static void output_compilation_unit_header (enum dwarf_unit_type);
3482 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3483 static void output_comdat_type_unit (comdat_type_node *);
3484 static const char *dwarf2_name (tree, int);
3485 static void add_pubname (tree, dw_die_ref);
3486 static void add_enumerator_pubname (const char *, dw_die_ref);
3487 static void add_pubname_string (const char *, dw_die_ref);
3488 static void add_pubtype (tree, dw_die_ref);
3489 static void output_pubnames (vec<pubname_entry, va_gc> *);
3490 static void output_aranges (void);
3491 static unsigned int add_ranges (const_tree, bool = false);
3492 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3493 bool *, bool);
3494 static void output_ranges (void);
3495 static dw_line_info_table *new_line_info_table (void);
3496 static void output_line_info (bool);
3497 static void output_file_names (void);
3498 static dw_die_ref base_type_die (tree, bool);
3499 static int is_base_type (tree);
3500 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3501 static int decl_quals (const_tree);
3502 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3503 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3504 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3505 static int type_is_enum (const_tree);
3506 static unsigned int dbx_reg_number (const_rtx);
3507 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3508 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3509 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3510 enum var_init_status);
3511 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3512 enum var_init_status);
3513 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3514 enum var_init_status);
3515 static int is_based_loc (const_rtx);
3516 static bool resolve_one_addr (rtx *);
3517 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3518 enum var_init_status);
3519 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3520 enum var_init_status);
3521 struct loc_descr_context;
3522 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3523 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3524 static dw_loc_list_ref loc_list_from_tree (tree, int,
3525 struct loc_descr_context *);
3526 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3527 struct loc_descr_context *);
3528 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3529 static tree field_type (const_tree);
3530 static unsigned int simple_type_align_in_bits (const_tree);
3531 static unsigned int simple_decl_align_in_bits (const_tree);
3532 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3533 struct vlr_context;
3534 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3535 HOST_WIDE_INT *);
3536 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3537 dw_loc_list_ref);
3538 static void add_data_member_location_attribute (dw_die_ref, tree,
3539 struct vlr_context *);
3540 static bool add_const_value_attribute (dw_die_ref, rtx);
3541 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3542 static void insert_wide_int (const wide_int &, unsigned char *, int);
3543 static void insert_float (const_rtx, unsigned char *);
3544 static rtx rtl_for_decl_location (tree);
3545 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3546 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3547 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3548 static void add_name_attribute (dw_die_ref, const char *);
3549 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3550 static void add_comp_dir_attribute (dw_die_ref);
3551 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3552 struct loc_descr_context *);
3553 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3554 struct loc_descr_context *);
3555 static void add_subscript_info (dw_die_ref, tree, bool);
3556 static void add_byte_size_attribute (dw_die_ref, tree);
3557 static void add_alignment_attribute (dw_die_ref, tree);
3558 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3559 struct vlr_context *);
3560 static void add_bit_size_attribute (dw_die_ref, tree);
3561 static void add_prototyped_attribute (dw_die_ref, tree);
3562 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3563 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3564 static void add_src_coords_attributes (dw_die_ref, tree);
3565 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3566 static void add_discr_value (dw_die_ref, dw_discr_value *);
3567 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3568 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3569 static void push_decl_scope (tree);
3570 static void pop_decl_scope (void);
3571 static dw_die_ref scope_die_for (tree, dw_die_ref);
3572 static inline int local_scope_p (dw_die_ref);
3573 static inline int class_scope_p (dw_die_ref);
3574 static inline int class_or_namespace_scope_p (dw_die_ref);
3575 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3576 static void add_calling_convention_attribute (dw_die_ref, tree);
3577 static const char *type_tag (const_tree);
3578 static tree member_declared_type (const_tree);
3579 #if 0
3580 static const char *decl_start_label (tree);
3581 #endif
3582 static void gen_array_type_die (tree, dw_die_ref);
3583 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3584 #if 0
3585 static void gen_entry_point_die (tree, dw_die_ref);
3586 #endif
3587 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3588 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3589 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3590 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3591 static void gen_formal_types_die (tree, dw_die_ref);
3592 static void gen_subprogram_die (tree, dw_die_ref);
3593 static void gen_variable_die (tree, tree, dw_die_ref);
3594 static void gen_const_die (tree, dw_die_ref);
3595 static void gen_label_die (tree, dw_die_ref);
3596 static void gen_lexical_block_die (tree, dw_die_ref);
3597 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3598 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3599 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3600 static dw_die_ref gen_compile_unit_die (const char *);
3601 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3602 static void gen_member_die (tree, dw_die_ref);
3603 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3604 enum debug_info_usage);
3605 static void gen_subroutine_type_die (tree, dw_die_ref);
3606 static void gen_typedef_die (tree, dw_die_ref);
3607 static void gen_type_die (tree, dw_die_ref);
3608 static void gen_block_die (tree, dw_die_ref);
3609 static void decls_for_scope (tree, dw_die_ref);
3610 static bool is_naming_typedef_decl (const_tree);
3611 static inline dw_die_ref get_context_die (tree);
3612 static void gen_namespace_die (tree, dw_die_ref);
3613 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3614 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3615 static dw_die_ref force_decl_die (tree);
3616 static dw_die_ref force_type_die (tree);
3617 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3618 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3619 static struct dwarf_file_data * lookup_filename (const char *);
3620 static void retry_incomplete_types (void);
3621 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3622 static void gen_generic_params_dies (tree);
3623 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3624 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3625 static void splice_child_die (dw_die_ref, dw_die_ref);
3626 static int file_info_cmp (const void *, const void *);
3627 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3628 const char *, const char *);
3629 static void output_loc_list (dw_loc_list_ref);
3630 static char *gen_internal_sym (const char *);
3631 static bool want_pubnames (void);
3633 static void prune_unmark_dies (dw_die_ref);
3634 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3635 static void prune_unused_types_mark (dw_die_ref, int);
3636 static void prune_unused_types_walk (dw_die_ref);
3637 static void prune_unused_types_walk_attribs (dw_die_ref);
3638 static void prune_unused_types_prune (dw_die_ref);
3639 static void prune_unused_types (void);
3640 static int maybe_emit_file (struct dwarf_file_data *fd);
3641 static inline const char *AT_vms_delta1 (dw_attr_node *);
3642 static inline const char *AT_vms_delta2 (dw_attr_node *);
3643 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3644 const char *, const char *);
3645 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3646 static void gen_remaining_tmpl_value_param_die_attribute (void);
3647 static bool generic_type_p (tree);
3648 static void schedule_generic_params_dies_gen (tree t);
3649 static void gen_scheduled_generic_parms_dies (void);
3650 static void resolve_variable_values (void);
3652 static const char *comp_dir_string (void);
3654 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3656 /* enum for tracking thread-local variables whose address is really an offset
3657 relative to the TLS pointer, which will need link-time relocation, but will
3658 not need relocation by the DWARF consumer. */
3660 enum dtprel_bool
3662 dtprel_false = 0,
3663 dtprel_true = 1
3666 /* Return the operator to use for an address of a variable. For dtprel_true, we
3667 use DW_OP_const*. For regular variables, which need both link-time
3668 relocation and consumer-level relocation (e.g., to account for shared objects
3669 loaded at a random address), we use DW_OP_addr*. */
3671 static inline enum dwarf_location_atom
3672 dw_addr_op (enum dtprel_bool dtprel)
3674 if (dtprel == dtprel_true)
3675 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3676 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3677 else
3678 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3681 /* Return a pointer to a newly allocated address location description. If
3682 dwarf_split_debug_info is true, then record the address with the appropriate
3683 relocation. */
3684 static inline dw_loc_descr_ref
3685 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3687 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3689 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3690 ref->dw_loc_oprnd1.v.val_addr = addr;
3691 ref->dtprel = dtprel;
3692 if (dwarf_split_debug_info)
3693 ref->dw_loc_oprnd1.val_entry
3694 = add_addr_table_entry (addr,
3695 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3696 else
3697 ref->dw_loc_oprnd1.val_entry = NULL;
3699 return ref;
3702 /* Section names used to hold DWARF debugging information. */
3704 #ifndef DEBUG_INFO_SECTION
3705 #define DEBUG_INFO_SECTION ".debug_info"
3706 #endif
3707 #ifndef DEBUG_DWO_INFO_SECTION
3708 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3709 #endif
3710 #ifndef DEBUG_LTO_INFO_SECTION
3711 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3712 #endif
3713 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3714 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3715 #endif
3716 #ifndef DEBUG_ABBREV_SECTION
3717 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3718 #endif
3719 #ifndef DEBUG_LTO_ABBREV_SECTION
3720 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3721 #endif
3722 #ifndef DEBUG_DWO_ABBREV_SECTION
3723 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3724 #endif
3725 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3726 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3727 #endif
3728 #ifndef DEBUG_ARANGES_SECTION
3729 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3730 #endif
3731 #ifndef DEBUG_ADDR_SECTION
3732 #define DEBUG_ADDR_SECTION ".debug_addr"
3733 #endif
3734 #ifndef DEBUG_MACINFO_SECTION
3735 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3736 #endif
3737 #ifndef DEBUG_LTO_MACINFO_SECTION
3738 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3739 #endif
3740 #ifndef DEBUG_DWO_MACINFO_SECTION
3741 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3742 #endif
3743 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3744 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3745 #endif
3746 #ifndef DEBUG_MACRO_SECTION
3747 #define DEBUG_MACRO_SECTION ".debug_macro"
3748 #endif
3749 #ifndef DEBUG_LTO_MACRO_SECTION
3750 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3751 #endif
3752 #ifndef DEBUG_DWO_MACRO_SECTION
3753 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3754 #endif
3755 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3756 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3757 #endif
3758 #ifndef DEBUG_LINE_SECTION
3759 #define DEBUG_LINE_SECTION ".debug_line"
3760 #endif
3761 #ifndef DEBUG_LTO_LINE_SECTION
3762 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3763 #endif
3764 #ifndef DEBUG_DWO_LINE_SECTION
3765 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3766 #endif
3767 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3768 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3769 #endif
3770 #ifndef DEBUG_LOC_SECTION
3771 #define DEBUG_LOC_SECTION ".debug_loc"
3772 #endif
3773 #ifndef DEBUG_DWO_LOC_SECTION
3774 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3775 #endif
3776 #ifndef DEBUG_LOCLISTS_SECTION
3777 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3778 #endif
3779 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3780 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3781 #endif
3782 #ifndef DEBUG_PUBNAMES_SECTION
3783 #define DEBUG_PUBNAMES_SECTION \
3784 ((debug_generate_pub_sections == 2) \
3785 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3786 #endif
3787 #ifndef DEBUG_PUBTYPES_SECTION
3788 #define DEBUG_PUBTYPES_SECTION \
3789 ((debug_generate_pub_sections == 2) \
3790 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3791 #endif
3792 #ifndef DEBUG_STR_OFFSETS_SECTION
3793 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3794 #endif
3795 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3796 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3797 #endif
3798 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3799 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3800 #endif
3801 #ifndef DEBUG_STR_SECTION
3802 #define DEBUG_STR_SECTION ".debug_str"
3803 #endif
3804 #ifndef DEBUG_LTO_STR_SECTION
3805 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3806 #endif
3807 #ifndef DEBUG_STR_DWO_SECTION
3808 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3809 #endif
3810 #ifndef DEBUG_LTO_STR_DWO_SECTION
3811 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3812 #endif
3813 #ifndef DEBUG_RANGES_SECTION
3814 #define DEBUG_RANGES_SECTION ".debug_ranges"
3815 #endif
3816 #ifndef DEBUG_RNGLISTS_SECTION
3817 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3818 #endif
3819 #ifndef DEBUG_LINE_STR_SECTION
3820 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3821 #endif
3822 #ifndef DEBUG_LTO_LINE_STR_SECTION
3823 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3824 #endif
3826 /* Standard ELF section names for compiled code and data. */
3827 #ifndef TEXT_SECTION_NAME
3828 #define TEXT_SECTION_NAME ".text"
3829 #endif
3831 /* Section flags for .debug_str section. */
3832 #define DEBUG_STR_SECTION_FLAGS \
3833 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3834 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3835 : SECTION_DEBUG)
3837 /* Section flags for .debug_str.dwo section. */
3838 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3840 /* Attribute used to refer to the macro section. */
3841 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3842 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3844 /* Labels we insert at beginning sections we can reference instead of
3845 the section names themselves. */
3847 #ifndef TEXT_SECTION_LABEL
3848 #define TEXT_SECTION_LABEL "Ltext"
3849 #endif
3850 #ifndef COLD_TEXT_SECTION_LABEL
3851 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3852 #endif
3853 #ifndef DEBUG_LINE_SECTION_LABEL
3854 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3855 #endif
3856 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3857 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3858 #endif
3859 #ifndef DEBUG_INFO_SECTION_LABEL
3860 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3861 #endif
3862 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3863 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3864 #endif
3865 #ifndef DEBUG_ABBREV_SECTION_LABEL
3866 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3867 #endif
3868 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3869 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3870 #endif
3871 #ifndef DEBUG_ADDR_SECTION_LABEL
3872 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3873 #endif
3874 #ifndef DEBUG_LOC_SECTION_LABEL
3875 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3876 #endif
3877 #ifndef DEBUG_RANGES_SECTION_LABEL
3878 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3879 #endif
3880 #ifndef DEBUG_MACINFO_SECTION_LABEL
3881 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3882 #endif
3883 #ifndef DEBUG_MACRO_SECTION_LABEL
3884 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3885 #endif
3886 #define SKELETON_COMP_DIE_ABBREV 1
3887 #define SKELETON_TYPE_DIE_ABBREV 2
3889 /* Definitions of defaults for formats and names of various special
3890 (artificial) labels which may be generated within this file (when the -g
3891 options is used and DWARF2_DEBUGGING_INFO is in effect.
3892 If necessary, these may be overridden from within the tm.h file, but
3893 typically, overriding these defaults is unnecessary. */
3895 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3900 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3901 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3905 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3906 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3907 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3908 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3909 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3911 #ifndef TEXT_END_LABEL
3912 #define TEXT_END_LABEL "Letext"
3913 #endif
3914 #ifndef COLD_END_LABEL
3915 #define COLD_END_LABEL "Letext_cold"
3916 #endif
3917 #ifndef BLOCK_BEGIN_LABEL
3918 #define BLOCK_BEGIN_LABEL "LBB"
3919 #endif
3920 #ifndef BLOCK_END_LABEL
3921 #define BLOCK_END_LABEL "LBE"
3922 #endif
3923 #ifndef LINE_CODE_LABEL
3924 #define LINE_CODE_LABEL "LM"
3925 #endif
3928 /* Return the root of the DIE's built for the current compilation unit. */
3929 static dw_die_ref
3930 comp_unit_die (void)
3932 if (!single_comp_unit_die)
3933 single_comp_unit_die = gen_compile_unit_die (NULL);
3934 return single_comp_unit_die;
3937 /* We allow a language front-end to designate a function that is to be
3938 called to "demangle" any name before it is put into a DIE. */
3940 static const char *(*demangle_name_func) (const char *);
3942 void
3943 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3945 demangle_name_func = func;
3948 /* Test if rtl node points to a pseudo register. */
3950 static inline int
3951 is_pseudo_reg (const_rtx rtl)
3953 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3954 || (GET_CODE (rtl) == SUBREG
3955 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3958 /* Return a reference to a type, with its const and volatile qualifiers
3959 removed. */
3961 static inline tree
3962 type_main_variant (tree type)
3964 type = TYPE_MAIN_VARIANT (type);
3966 /* ??? There really should be only one main variant among any group of
3967 variants of a given type (and all of the MAIN_VARIANT values for all
3968 members of the group should point to that one type) but sometimes the C
3969 front-end messes this up for array types, so we work around that bug
3970 here. */
3971 if (TREE_CODE (type) == ARRAY_TYPE)
3972 while (type != TYPE_MAIN_VARIANT (type))
3973 type = TYPE_MAIN_VARIANT (type);
3975 return type;
3978 /* Return nonzero if the given type node represents a tagged type. */
3980 static inline int
3981 is_tagged_type (const_tree type)
3983 enum tree_code code = TREE_CODE (type);
3985 return (code == RECORD_TYPE || code == UNION_TYPE
3986 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3989 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3991 static void
3992 get_ref_die_offset_label (char *label, dw_die_ref ref)
3994 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3997 /* Return die_offset of a DIE reference to a base type. */
3999 static unsigned long int
4000 get_base_type_offset (dw_die_ref ref)
4002 if (ref->die_offset)
4003 return ref->die_offset;
4004 if (comp_unit_die ()->die_abbrev)
4006 calc_base_type_die_sizes ();
4007 gcc_assert (ref->die_offset);
4009 return ref->die_offset;
4012 /* Return die_offset of a DIE reference other than base type. */
4014 static unsigned long int
4015 get_ref_die_offset (dw_die_ref ref)
4017 gcc_assert (ref->die_offset);
4018 return ref->die_offset;
4021 /* Convert a DIE tag into its string name. */
4023 static const char *
4024 dwarf_tag_name (unsigned int tag)
4026 const char *name = get_DW_TAG_name (tag);
4028 if (name != NULL)
4029 return name;
4031 return "DW_TAG_<unknown>";
4034 /* Convert a DWARF attribute code into its string name. */
4036 static const char *
4037 dwarf_attr_name (unsigned int attr)
4039 const char *name;
4041 switch (attr)
4043 #if VMS_DEBUGGING_INFO
4044 case DW_AT_HP_prologue:
4045 return "DW_AT_HP_prologue";
4046 #else
4047 case DW_AT_MIPS_loop_unroll_factor:
4048 return "DW_AT_MIPS_loop_unroll_factor";
4049 #endif
4051 #if VMS_DEBUGGING_INFO
4052 case DW_AT_HP_epilogue:
4053 return "DW_AT_HP_epilogue";
4054 #else
4055 case DW_AT_MIPS_stride:
4056 return "DW_AT_MIPS_stride";
4057 #endif
4060 name = get_DW_AT_name (attr);
4062 if (name != NULL)
4063 return name;
4065 return "DW_AT_<unknown>";
4068 /* Convert a DWARF value form code into its string name. */
4070 static const char *
4071 dwarf_form_name (unsigned int form)
4073 const char *name = get_DW_FORM_name (form);
4075 if (name != NULL)
4076 return name;
4078 return "DW_FORM_<unknown>";
4081 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4082 instance of an inlined instance of a decl which is local to an inline
4083 function, so we have to trace all of the way back through the origin chain
4084 to find out what sort of node actually served as the original seed for the
4085 given block. */
4087 static tree
4088 decl_ultimate_origin (const_tree decl)
4090 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4091 return NULL_TREE;
4093 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4094 we're trying to output the abstract instance of this function. */
4095 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4096 return NULL_TREE;
4098 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4099 most distant ancestor, this should never happen. */
4100 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4102 return DECL_ABSTRACT_ORIGIN (decl);
4105 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4106 of a virtual function may refer to a base class, so we check the 'this'
4107 parameter. */
4109 static tree
4110 decl_class_context (tree decl)
4112 tree context = NULL_TREE;
4114 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4115 context = DECL_CONTEXT (decl);
4116 else
4117 context = TYPE_MAIN_VARIANT
4118 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4120 if (context && !TYPE_P (context))
4121 context = NULL_TREE;
4123 return context;
4126 /* Add an attribute/value pair to a DIE. */
4128 static inline void
4129 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4131 /* Maybe this should be an assert? */
4132 if (die == NULL)
4133 return;
4135 if (flag_checking)
4137 /* Check we do not add duplicate attrs. Can't use get_AT here
4138 because that recurses to the specification/abstract origin DIE. */
4139 dw_attr_node *a;
4140 unsigned ix;
4141 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4142 gcc_assert (a->dw_attr != attr->dw_attr);
4145 vec_safe_reserve (die->die_attr, 1);
4146 vec_safe_push (die->die_attr, *attr);
4149 static inline enum dw_val_class
4150 AT_class (dw_attr_node *a)
4152 return a->dw_attr_val.val_class;
4155 /* Return the index for any attribute that will be referenced with a
4156 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4157 are stored in dw_attr_val.v.val_str for reference counting
4158 pruning. */
4160 static inline unsigned int
4161 AT_index (dw_attr_node *a)
4163 if (AT_class (a) == dw_val_class_str)
4164 return a->dw_attr_val.v.val_str->index;
4165 else if (a->dw_attr_val.val_entry != NULL)
4166 return a->dw_attr_val.val_entry->index;
4167 return NOT_INDEXED;
4170 /* Add a flag value attribute to a DIE. */
4172 static inline void
4173 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4175 dw_attr_node attr;
4177 attr.dw_attr = attr_kind;
4178 attr.dw_attr_val.val_class = dw_val_class_flag;
4179 attr.dw_attr_val.val_entry = NULL;
4180 attr.dw_attr_val.v.val_flag = flag;
4181 add_dwarf_attr (die, &attr);
4184 static inline unsigned
4185 AT_flag (dw_attr_node *a)
4187 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4188 return a->dw_attr_val.v.val_flag;
4191 /* Add a signed integer attribute value to a DIE. */
4193 static inline void
4194 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_const;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_int = int_val;
4202 add_dwarf_attr (die, &attr);
4205 static inline HOST_WIDE_INT
4206 AT_int (dw_attr_node *a)
4208 gcc_assert (a && (AT_class (a) == dw_val_class_const
4209 || AT_class (a) == dw_val_class_const_implicit));
4210 return a->dw_attr_val.v.val_int;
4213 /* Add an unsigned integer attribute value to a DIE. */
4215 static inline void
4216 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4217 unsigned HOST_WIDE_INT unsigned_val)
4219 dw_attr_node attr;
4221 attr.dw_attr = attr_kind;
4222 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4223 attr.dw_attr_val.val_entry = NULL;
4224 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4225 add_dwarf_attr (die, &attr);
4228 static inline unsigned HOST_WIDE_INT
4229 AT_unsigned (dw_attr_node *a)
4231 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4232 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4233 return a->dw_attr_val.v.val_unsigned;
4236 /* Add an unsigned wide integer attribute value to a DIE. */
4238 static inline void
4239 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4240 const wide_int& w)
4242 dw_attr_node attr;
4244 attr.dw_attr = attr_kind;
4245 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4246 attr.dw_attr_val.val_entry = NULL;
4247 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4248 *attr.dw_attr_val.v.val_wide = w;
4249 add_dwarf_attr (die, &attr);
4252 /* Add an unsigned double integer attribute value to a DIE. */
4254 static inline void
4255 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4256 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4258 dw_attr_node attr;
4260 attr.dw_attr = attr_kind;
4261 attr.dw_attr_val.val_class = dw_val_class_const_double;
4262 attr.dw_attr_val.val_entry = NULL;
4263 attr.dw_attr_val.v.val_double.high = high;
4264 attr.dw_attr_val.v.val_double.low = low;
4265 add_dwarf_attr (die, &attr);
4268 /* Add a floating point attribute value to a DIE and return it. */
4270 static inline void
4271 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4272 unsigned int length, unsigned int elt_size, unsigned char *array)
4274 dw_attr_node attr;
4276 attr.dw_attr = attr_kind;
4277 attr.dw_attr_val.val_class = dw_val_class_vec;
4278 attr.dw_attr_val.val_entry = NULL;
4279 attr.dw_attr_val.v.val_vec.length = length;
4280 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4281 attr.dw_attr_val.v.val_vec.array = array;
4282 add_dwarf_attr (die, &attr);
4285 /* Add an 8-byte data attribute value to a DIE. */
4287 static inline void
4288 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4289 unsigned char data8[8])
4291 dw_attr_node attr;
4293 attr.dw_attr = attr_kind;
4294 attr.dw_attr_val.val_class = dw_val_class_data8;
4295 attr.dw_attr_val.val_entry = NULL;
4296 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4297 add_dwarf_attr (die, &attr);
4300 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4301 dwarf_split_debug_info, address attributes in dies destined for the
4302 final executable have force_direct set to avoid using indexed
4303 references. */
4305 static inline void
4306 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4307 bool force_direct)
4309 dw_attr_node attr;
4310 char * lbl_id;
4312 lbl_id = xstrdup (lbl_low);
4313 attr.dw_attr = DW_AT_low_pc;
4314 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4315 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4316 if (dwarf_split_debug_info && !force_direct)
4317 attr.dw_attr_val.val_entry
4318 = add_addr_table_entry (lbl_id, ate_kind_label);
4319 else
4320 attr.dw_attr_val.val_entry = NULL;
4321 add_dwarf_attr (die, &attr);
4323 attr.dw_attr = DW_AT_high_pc;
4324 if (dwarf_version < 4)
4325 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4326 else
4327 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4328 lbl_id = xstrdup (lbl_high);
4329 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4330 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4331 && dwarf_split_debug_info && !force_direct)
4332 attr.dw_attr_val.val_entry
4333 = add_addr_table_entry (lbl_id, ate_kind_label);
4334 else
4335 attr.dw_attr_val.val_entry = NULL;
4336 add_dwarf_attr (die, &attr);
4339 /* Hash and equality functions for debug_str_hash. */
4341 hashval_t
4342 indirect_string_hasher::hash (indirect_string_node *x)
4344 return htab_hash_string (x->str);
4347 bool
4348 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4350 return strcmp (x1->str, x2) == 0;
4353 /* Add STR to the given string hash table. */
4355 static struct indirect_string_node *
4356 find_AT_string_in_table (const char *str,
4357 hash_table<indirect_string_hasher> *table)
4359 struct indirect_string_node *node;
4361 indirect_string_node **slot
4362 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4363 if (*slot == NULL)
4365 node = ggc_cleared_alloc<indirect_string_node> ();
4366 node->str = ggc_strdup (str);
4367 *slot = node;
4369 else
4370 node = *slot;
4372 node->refcount++;
4373 return node;
4376 /* Add STR to the indirect string hash table. */
4378 static struct indirect_string_node *
4379 find_AT_string (const char *str)
4381 if (! debug_str_hash)
4382 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4384 return find_AT_string_in_table (str, debug_str_hash);
4387 /* Add a string attribute value to a DIE. */
4389 static inline void
4390 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4392 dw_attr_node attr;
4393 struct indirect_string_node *node;
4395 node = find_AT_string (str);
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_str;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_str = node;
4401 add_dwarf_attr (die, &attr);
4404 static inline const char *
4405 AT_string (dw_attr_node *a)
4407 gcc_assert (a && AT_class (a) == dw_val_class_str);
4408 return a->dw_attr_val.v.val_str->str;
4411 /* Call this function directly to bypass AT_string_form's logic to put
4412 the string inline in the die. */
4414 static void
4415 set_indirect_string (struct indirect_string_node *node)
4417 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4418 /* Already indirect is a no op. */
4419 if (node->form == DW_FORM_strp
4420 || node->form == DW_FORM_line_strp
4421 || node->form == DW_FORM_GNU_str_index)
4423 gcc_assert (node->label);
4424 return;
4426 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4427 ++dw2_string_counter;
4428 node->label = xstrdup (label);
4430 if (!dwarf_split_debug_info)
4432 node->form = DW_FORM_strp;
4433 node->index = NOT_INDEXED;
4435 else
4437 node->form = DW_FORM_GNU_str_index;
4438 node->index = NO_INDEX_ASSIGNED;
4442 /* A helper function for dwarf2out_finish, called to reset indirect
4443 string decisions done for early LTO dwarf output before fat object
4444 dwarf output. */
4447 reset_indirect_string (indirect_string_node **h, void *)
4449 struct indirect_string_node *node = *h;
4450 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4452 free (node->label);
4453 node->label = NULL;
4454 node->form = (dwarf_form) 0;
4455 node->index = 0;
4457 return 1;
4460 /* Find out whether a string should be output inline in DIE
4461 or out-of-line in .debug_str section. */
4463 static enum dwarf_form
4464 find_string_form (struct indirect_string_node *node)
4466 unsigned int len;
4468 if (node->form)
4469 return node->form;
4471 len = strlen (node->str) + 1;
4473 /* If the string is shorter or equal to the size of the reference, it is
4474 always better to put it inline. */
4475 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4476 return node->form = DW_FORM_string;
4478 /* If we cannot expect the linker to merge strings in .debug_str
4479 section, only put it into .debug_str if it is worth even in this
4480 single module. */
4481 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4482 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4483 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4484 return node->form = DW_FORM_string;
4486 set_indirect_string (node);
4488 return node->form;
4491 /* Find out whether the string referenced from the attribute should be
4492 output inline in DIE or out-of-line in .debug_str section. */
4494 static enum dwarf_form
4495 AT_string_form (dw_attr_node *a)
4497 gcc_assert (a && AT_class (a) == dw_val_class_str);
4498 return find_string_form (a->dw_attr_val.v.val_str);
4501 /* Add a DIE reference attribute value to a DIE. */
4503 static inline void
4504 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4506 dw_attr_node attr;
4507 gcc_checking_assert (targ_die != NULL);
4509 /* With LTO we can end up trying to reference something we didn't create
4510 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4511 if (targ_die == NULL)
4512 return;
4514 attr.dw_attr = attr_kind;
4515 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4516 attr.dw_attr_val.val_entry = NULL;
4517 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4518 attr.dw_attr_val.v.val_die_ref.external = 0;
4519 add_dwarf_attr (die, &attr);
4522 /* Change DIE reference REF to point to NEW_DIE instead. */
4524 static inline void
4525 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4527 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4528 ref->dw_attr_val.v.val_die_ref.die = new_die;
4529 ref->dw_attr_val.v.val_die_ref.external = 0;
4532 /* Add an AT_specification attribute to a DIE, and also make the back
4533 pointer from the specification to the definition. */
4535 static inline void
4536 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4538 add_AT_die_ref (die, DW_AT_specification, targ_die);
4539 gcc_assert (!targ_die->die_definition);
4540 targ_die->die_definition = die;
4543 static inline dw_die_ref
4544 AT_ref (dw_attr_node *a)
4546 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4547 return a->dw_attr_val.v.val_die_ref.die;
4550 static inline int
4551 AT_ref_external (dw_attr_node *a)
4553 if (a && AT_class (a) == dw_val_class_die_ref)
4554 return a->dw_attr_val.v.val_die_ref.external;
4556 return 0;
4559 static inline void
4560 set_AT_ref_external (dw_attr_node *a, int i)
4562 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4563 a->dw_attr_val.v.val_die_ref.external = i;
4566 /* Add an FDE reference attribute value to a DIE. */
4568 static inline void
4569 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_fde_index = targ_fde;
4577 add_dwarf_attr (die, &attr);
4580 /* Add a location description attribute value to a DIE. */
4582 static inline void
4583 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4585 dw_attr_node attr;
4587 attr.dw_attr = attr_kind;
4588 attr.dw_attr_val.val_class = dw_val_class_loc;
4589 attr.dw_attr_val.val_entry = NULL;
4590 attr.dw_attr_val.v.val_loc = loc;
4591 add_dwarf_attr (die, &attr);
4594 static inline dw_loc_descr_ref
4595 AT_loc (dw_attr_node *a)
4597 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4598 return a->dw_attr_val.v.val_loc;
4601 static inline void
4602 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4604 dw_attr_node attr;
4606 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4607 return;
4609 attr.dw_attr = attr_kind;
4610 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4611 attr.dw_attr_val.val_entry = NULL;
4612 attr.dw_attr_val.v.val_loc_list = loc_list;
4613 add_dwarf_attr (die, &attr);
4614 have_location_lists = true;
4617 static inline dw_loc_list_ref
4618 AT_loc_list (dw_attr_node *a)
4620 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4621 return a->dw_attr_val.v.val_loc_list;
4624 static inline dw_loc_list_ref *
4625 AT_loc_list_ptr (dw_attr_node *a)
4627 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4628 return &a->dw_attr_val.v.val_loc_list;
4631 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4633 static hashval_t hash (addr_table_entry *);
4634 static bool equal (addr_table_entry *, addr_table_entry *);
4637 /* Table of entries into the .debug_addr section. */
4639 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4641 /* Hash an address_table_entry. */
4643 hashval_t
4644 addr_hasher::hash (addr_table_entry *a)
4646 inchash::hash hstate;
4647 switch (a->kind)
4649 case ate_kind_rtx:
4650 hstate.add_int (0);
4651 break;
4652 case ate_kind_rtx_dtprel:
4653 hstate.add_int (1);
4654 break;
4655 case ate_kind_label:
4656 return htab_hash_string (a->addr.label);
4657 default:
4658 gcc_unreachable ();
4660 inchash::add_rtx (a->addr.rtl, hstate);
4661 return hstate.end ();
4664 /* Determine equality for two address_table_entries. */
4666 bool
4667 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4669 if (a1->kind != a2->kind)
4670 return 0;
4671 switch (a1->kind)
4673 case ate_kind_rtx:
4674 case ate_kind_rtx_dtprel:
4675 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4676 case ate_kind_label:
4677 return strcmp (a1->addr.label, a2->addr.label) == 0;
4678 default:
4679 gcc_unreachable ();
4683 /* Initialize an addr_table_entry. */
4685 void
4686 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4688 e->kind = kind;
4689 switch (kind)
4691 case ate_kind_rtx:
4692 case ate_kind_rtx_dtprel:
4693 e->addr.rtl = (rtx) addr;
4694 break;
4695 case ate_kind_label:
4696 e->addr.label = (char *) addr;
4697 break;
4699 e->refcount = 0;
4700 e->index = NO_INDEX_ASSIGNED;
4703 /* Add attr to the address table entry to the table. Defer setting an
4704 index until output time. */
4706 static addr_table_entry *
4707 add_addr_table_entry (void *addr, enum ate_kind kind)
4709 addr_table_entry *node;
4710 addr_table_entry finder;
4712 gcc_assert (dwarf_split_debug_info);
4713 if (! addr_index_table)
4714 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4715 init_addr_table_entry (&finder, kind, addr);
4716 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4718 if (*slot == HTAB_EMPTY_ENTRY)
4720 node = ggc_cleared_alloc<addr_table_entry> ();
4721 init_addr_table_entry (node, kind, addr);
4722 *slot = node;
4724 else
4725 node = *slot;
4727 node->refcount++;
4728 return node;
4731 /* Remove an entry from the addr table by decrementing its refcount.
4732 Strictly, decrementing the refcount would be enough, but the
4733 assertion that the entry is actually in the table has found
4734 bugs. */
4736 static void
4737 remove_addr_table_entry (addr_table_entry *entry)
4739 gcc_assert (dwarf_split_debug_info && addr_index_table);
4740 /* After an index is assigned, the table is frozen. */
4741 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4742 entry->refcount--;
4745 /* Given a location list, remove all addresses it refers to from the
4746 address_table. */
4748 static void
4749 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4751 for (; descr; descr = descr->dw_loc_next)
4752 if (descr->dw_loc_oprnd1.val_entry != NULL)
4754 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4755 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4759 /* A helper function for dwarf2out_finish called through
4760 htab_traverse. Assign an addr_table_entry its index. All entries
4761 must be collected into the table when this function is called,
4762 because the indexing code relies on htab_traverse to traverse nodes
4763 in the same order for each run. */
4766 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4768 addr_table_entry *node = *h;
4770 /* Don't index unreferenced nodes. */
4771 if (node->refcount == 0)
4772 return 1;
4774 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4775 node->index = *index;
4776 *index += 1;
4778 return 1;
4781 /* Add an address constant attribute value to a DIE. When using
4782 dwarf_split_debug_info, address attributes in dies destined for the
4783 final executable should be direct references--setting the parameter
4784 force_direct ensures this behavior. */
4786 static inline void
4787 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4788 bool force_direct)
4790 dw_attr_node attr;
4792 attr.dw_attr = attr_kind;
4793 attr.dw_attr_val.val_class = dw_val_class_addr;
4794 attr.dw_attr_val.v.val_addr = addr;
4795 if (dwarf_split_debug_info && !force_direct)
4796 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4797 else
4798 attr.dw_attr_val.val_entry = NULL;
4799 add_dwarf_attr (die, &attr);
4802 /* Get the RTX from to an address DIE attribute. */
4804 static inline rtx
4805 AT_addr (dw_attr_node *a)
4807 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4808 return a->dw_attr_val.v.val_addr;
4811 /* Add a file attribute value to a DIE. */
4813 static inline void
4814 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4815 struct dwarf_file_data *fd)
4817 dw_attr_node attr;
4819 attr.dw_attr = attr_kind;
4820 attr.dw_attr_val.val_class = dw_val_class_file;
4821 attr.dw_attr_val.val_entry = NULL;
4822 attr.dw_attr_val.v.val_file = fd;
4823 add_dwarf_attr (die, &attr);
4826 /* Get the dwarf_file_data from a file DIE attribute. */
4828 static inline struct dwarf_file_data *
4829 AT_file (dw_attr_node *a)
4831 gcc_assert (a && (AT_class (a) == dw_val_class_file
4832 || AT_class (a) == dw_val_class_file_implicit));
4833 return a->dw_attr_val.v.val_file;
4836 /* Add a vms delta attribute value to a DIE. */
4838 static inline void
4839 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 const char *lbl1, const char *lbl2)
4842 dw_attr_node attr;
4844 attr.dw_attr = attr_kind;
4845 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4846 attr.dw_attr_val.val_entry = NULL;
4847 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4848 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4849 add_dwarf_attr (die, &attr);
4852 /* Add a label identifier attribute value to a DIE. */
4854 static inline void
4855 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4856 const char *lbl_id)
4858 dw_attr_node attr;
4860 attr.dw_attr = attr_kind;
4861 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4862 attr.dw_attr_val.val_entry = NULL;
4863 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4864 if (dwarf_split_debug_info)
4865 attr.dw_attr_val.val_entry
4866 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4867 ate_kind_label);
4868 add_dwarf_attr (die, &attr);
4871 /* Add a section offset attribute value to a DIE, an offset into the
4872 debug_line section. */
4874 static inline void
4875 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4876 const char *label)
4878 dw_attr_node attr;
4880 attr.dw_attr = attr_kind;
4881 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4882 attr.dw_attr_val.val_entry = NULL;
4883 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4884 add_dwarf_attr (die, &attr);
4887 /* Add a section offset attribute value to a DIE, an offset into the
4888 debug_loclists section. */
4890 static inline void
4891 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4892 const char *label)
4894 dw_attr_node attr;
4896 attr.dw_attr = attr_kind;
4897 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4898 attr.dw_attr_val.val_entry = NULL;
4899 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4900 add_dwarf_attr (die, &attr);
4903 /* Add a section offset attribute value to a DIE, an offset into the
4904 debug_macinfo section. */
4906 static inline void
4907 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4908 const char *label)
4910 dw_attr_node attr;
4912 attr.dw_attr = attr_kind;
4913 attr.dw_attr_val.val_class = dw_val_class_macptr;
4914 attr.dw_attr_val.val_entry = NULL;
4915 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4916 add_dwarf_attr (die, &attr);
4919 /* Add an offset attribute value to a DIE. */
4921 static inline void
4922 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4923 unsigned HOST_WIDE_INT offset)
4925 dw_attr_node attr;
4927 attr.dw_attr = attr_kind;
4928 attr.dw_attr_val.val_class = dw_val_class_offset;
4929 attr.dw_attr_val.val_entry = NULL;
4930 attr.dw_attr_val.v.val_offset = offset;
4931 add_dwarf_attr (die, &attr);
4934 /* Add a range_list attribute value to a DIE. When using
4935 dwarf_split_debug_info, address attributes in dies destined for the
4936 final executable should be direct references--setting the parameter
4937 force_direct ensures this behavior. */
4939 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4940 #define RELOCATED_OFFSET (NULL)
4942 static void
4943 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4944 long unsigned int offset, bool force_direct)
4946 dw_attr_node attr;
4948 attr.dw_attr = attr_kind;
4949 attr.dw_attr_val.val_class = dw_val_class_range_list;
4950 /* For the range_list attribute, use val_entry to store whether the
4951 offset should follow split-debug-info or normal semantics. This
4952 value is read in output_range_list_offset. */
4953 if (dwarf_split_debug_info && !force_direct)
4954 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4955 else
4956 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4957 attr.dw_attr_val.v.val_offset = offset;
4958 add_dwarf_attr (die, &attr);
4961 /* Return the start label of a delta attribute. */
4963 static inline const char *
4964 AT_vms_delta1 (dw_attr_node *a)
4966 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4967 return a->dw_attr_val.v.val_vms_delta.lbl1;
4970 /* Return the end label of a delta attribute. */
4972 static inline const char *
4973 AT_vms_delta2 (dw_attr_node *a)
4975 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4976 return a->dw_attr_val.v.val_vms_delta.lbl2;
4979 static inline const char *
4980 AT_lbl (dw_attr_node *a)
4982 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4983 || AT_class (a) == dw_val_class_lineptr
4984 || AT_class (a) == dw_val_class_macptr
4985 || AT_class (a) == dw_val_class_loclistsptr
4986 || AT_class (a) == dw_val_class_high_pc));
4987 return a->dw_attr_val.v.val_lbl_id;
4990 /* Get the attribute of type attr_kind. */
4992 static dw_attr_node *
4993 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4995 dw_attr_node *a;
4996 unsigned ix;
4997 dw_die_ref spec = NULL;
4999 if (! die)
5000 return NULL;
5002 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5003 if (a->dw_attr == attr_kind)
5004 return a;
5005 else if (a->dw_attr == DW_AT_specification
5006 || a->dw_attr == DW_AT_abstract_origin)
5007 spec = AT_ref (a);
5009 if (spec)
5010 return get_AT (spec, attr_kind);
5012 return NULL;
5015 /* Returns the parent of the declaration of DIE. */
5017 static dw_die_ref
5018 get_die_parent (dw_die_ref die)
5020 dw_die_ref t;
5022 if (!die)
5023 return NULL;
5025 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5026 || (t = get_AT_ref (die, DW_AT_specification)))
5027 die = t;
5029 return die->die_parent;
5032 /* Return the "low pc" attribute value, typically associated with a subprogram
5033 DIE. Return null if the "low pc" attribute is either not present, or if it
5034 cannot be represented as an assembler label identifier. */
5036 static inline const char *
5037 get_AT_low_pc (dw_die_ref die)
5039 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5041 return a ? AT_lbl (a) : NULL;
5044 /* Return the "high pc" attribute value, typically associated with a subprogram
5045 DIE. Return null if the "high pc" attribute is either not present, or if it
5046 cannot be represented as an assembler label identifier. */
5048 static inline const char *
5049 get_AT_hi_pc (dw_die_ref die)
5051 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5053 return a ? AT_lbl (a) : NULL;
5056 /* Return the value of the string attribute designated by ATTR_KIND, or
5057 NULL if it is not present. */
5059 static inline const char *
5060 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5062 dw_attr_node *a = get_AT (die, attr_kind);
5064 return a ? AT_string (a) : NULL;
5067 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5068 if it is not present. */
5070 static inline int
5071 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5073 dw_attr_node *a = get_AT (die, attr_kind);
5075 return a ? AT_flag (a) : 0;
5078 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5079 if it is not present. */
5081 static inline unsigned
5082 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5084 dw_attr_node *a = get_AT (die, attr_kind);
5086 return a ? AT_unsigned (a) : 0;
5089 static inline dw_die_ref
5090 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5092 dw_attr_node *a = get_AT (die, attr_kind);
5094 return a ? AT_ref (a) : NULL;
5097 static inline struct dwarf_file_data *
5098 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5100 dw_attr_node *a = get_AT (die, attr_kind);
5102 return a ? AT_file (a) : NULL;
5105 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5107 static const_tree
5108 get_ultimate_context (const_tree decl)
5110 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5112 if (TREE_CODE (decl) == BLOCK)
5113 decl = BLOCK_SUPERCONTEXT (decl);
5114 else
5115 decl = get_containing_scope (decl);
5117 return decl;
5120 /* Return TRUE if the language is C++. */
5122 static inline bool
5123 is_cxx (void)
5125 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5127 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5128 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5131 /* Return TRUE if DECL was created by the C++ frontend. */
5133 static bool
5134 is_cxx (const_tree decl)
5136 if (in_lto_p)
5138 const_tree context = get_ultimate_context (decl);
5139 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5140 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5142 return is_cxx ();
5145 /* Return TRUE if the language is Fortran. */
5147 static inline bool
5148 is_fortran (void)
5150 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5152 return (lang == DW_LANG_Fortran77
5153 || lang == DW_LANG_Fortran90
5154 || lang == DW_LANG_Fortran95
5155 || lang == DW_LANG_Fortran03
5156 || lang == DW_LANG_Fortran08);
5159 static inline bool
5160 is_fortran (const_tree decl)
5162 if (in_lto_p)
5164 const_tree context = get_ultimate_context (decl);
5165 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5166 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5167 "GNU Fortran", 11) == 0
5168 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5169 "GNU F77") == 0);
5171 return is_fortran ();
5174 /* Return TRUE if the language is Ada. */
5176 static inline bool
5177 is_ada (void)
5179 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5181 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5184 /* Remove the specified attribute if present. Return TRUE if removal
5185 was successful. */
5187 static bool
5188 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5190 dw_attr_node *a;
5191 unsigned ix;
5193 if (! die)
5194 return false;
5196 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5197 if (a->dw_attr == attr_kind)
5199 if (AT_class (a) == dw_val_class_str)
5200 if (a->dw_attr_val.v.val_str->refcount)
5201 a->dw_attr_val.v.val_str->refcount--;
5203 /* vec::ordered_remove should help reduce the number of abbrevs
5204 that are needed. */
5205 die->die_attr->ordered_remove (ix);
5206 return true;
5208 return false;
5211 /* Remove CHILD from its parent. PREV must have the property that
5212 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5214 static void
5215 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5217 gcc_assert (child->die_parent == prev->die_parent);
5218 gcc_assert (prev->die_sib == child);
5219 if (prev == child)
5221 gcc_assert (child->die_parent->die_child == child);
5222 prev = NULL;
5224 else
5225 prev->die_sib = child->die_sib;
5226 if (child->die_parent->die_child == child)
5227 child->die_parent->die_child = prev;
5228 child->die_sib = NULL;
5231 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5232 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5234 static void
5235 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5237 dw_die_ref parent = old_child->die_parent;
5239 gcc_assert (parent == prev->die_parent);
5240 gcc_assert (prev->die_sib == old_child);
5242 new_child->die_parent = parent;
5243 if (prev == old_child)
5245 gcc_assert (parent->die_child == old_child);
5246 new_child->die_sib = new_child;
5248 else
5250 prev->die_sib = new_child;
5251 new_child->die_sib = old_child->die_sib;
5253 if (old_child->die_parent->die_child == old_child)
5254 old_child->die_parent->die_child = new_child;
5255 old_child->die_sib = NULL;
5258 /* Move all children from OLD_PARENT to NEW_PARENT. */
5260 static void
5261 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5263 dw_die_ref c;
5264 new_parent->die_child = old_parent->die_child;
5265 old_parent->die_child = NULL;
5266 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5269 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5270 matches TAG. */
5272 static void
5273 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5275 dw_die_ref c;
5277 c = die->die_child;
5278 if (c) do {
5279 dw_die_ref prev = c;
5280 c = c->die_sib;
5281 while (c->die_tag == tag)
5283 remove_child_with_prev (c, prev);
5284 c->die_parent = NULL;
5285 /* Might have removed every child. */
5286 if (die->die_child == NULL)
5287 return;
5288 c = prev->die_sib;
5290 } while (c != die->die_child);
5293 /* Add a CHILD_DIE as the last child of DIE. */
5295 static void
5296 add_child_die (dw_die_ref die, dw_die_ref child_die)
5298 /* FIXME this should probably be an assert. */
5299 if (! die || ! child_die)
5300 return;
5301 gcc_assert (die != child_die);
5303 child_die->die_parent = die;
5304 if (die->die_child)
5306 child_die->die_sib = die->die_child->die_sib;
5307 die->die_child->die_sib = child_die;
5309 else
5310 child_die->die_sib = child_die;
5311 die->die_child = child_die;
5314 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5316 static void
5317 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5318 dw_die_ref after_die)
5320 gcc_assert (die
5321 && child_die
5322 && after_die
5323 && die->die_child
5324 && die != child_die);
5326 child_die->die_parent = die;
5327 child_die->die_sib = after_die->die_sib;
5328 after_die->die_sib = child_die;
5329 if (die->die_child == after_die)
5330 die->die_child = child_die;
5333 /* Unassociate CHILD from its parent, and make its parent be
5334 NEW_PARENT. */
5336 static void
5337 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5339 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5340 if (p->die_sib == child)
5342 remove_child_with_prev (child, p);
5343 break;
5345 add_child_die (new_parent, child);
5348 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5349 is the specification, to the end of PARENT's list of children.
5350 This is done by removing and re-adding it. */
5352 static void
5353 splice_child_die (dw_die_ref parent, dw_die_ref child)
5355 /* We want the declaration DIE from inside the class, not the
5356 specification DIE at toplevel. */
5357 if (child->die_parent != parent)
5359 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5361 if (tmp)
5362 child = tmp;
5365 gcc_assert (child->die_parent == parent
5366 || (child->die_parent
5367 == get_AT_ref (parent, DW_AT_specification)));
5369 reparent_child (child, parent);
5372 /* Create and return a new die with TAG_VALUE as tag. */
5374 static inline dw_die_ref
5375 new_die_raw (enum dwarf_tag tag_value)
5377 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5378 die->die_tag = tag_value;
5379 return die;
5382 /* Create and return a new die with a parent of PARENT_DIE. If
5383 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5384 associated tree T must be supplied to determine parenthood
5385 later. */
5387 static inline dw_die_ref
5388 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5390 dw_die_ref die = new_die_raw (tag_value);
5392 if (parent_die != NULL)
5393 add_child_die (parent_die, die);
5394 else
5396 limbo_die_node *limbo_node;
5398 /* No DIEs created after early dwarf should end up in limbo,
5399 because the limbo list should not persist past LTO
5400 streaming. */
5401 if (tag_value != DW_TAG_compile_unit
5402 /* These are allowed because they're generated while
5403 breaking out COMDAT units late. */
5404 && tag_value != DW_TAG_type_unit
5405 && tag_value != DW_TAG_skeleton_unit
5406 && !early_dwarf
5407 /* Allow nested functions to live in limbo because they will
5408 only temporarily live there, as decls_for_scope will fix
5409 them up. */
5410 && (TREE_CODE (t) != FUNCTION_DECL
5411 || !decl_function_context (t))
5412 /* Same as nested functions above but for types. Types that
5413 are local to a function will be fixed in
5414 decls_for_scope. */
5415 && (!RECORD_OR_UNION_TYPE_P (t)
5416 || !TYPE_CONTEXT (t)
5417 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5418 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5419 especially in the ltrans stage, but once we implement LTO
5420 dwarf streaming, we should remove this exception. */
5421 && !in_lto_p)
5423 fprintf (stderr, "symbol ended up in limbo too late:");
5424 debug_generic_stmt (t);
5425 gcc_unreachable ();
5428 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5429 limbo_node->die = die;
5430 limbo_node->created_for = t;
5431 limbo_node->next = limbo_die_list;
5432 limbo_die_list = limbo_node;
5435 return die;
5438 /* Return the DIE associated with the given type specifier. */
5440 static inline dw_die_ref
5441 lookup_type_die (tree type)
5443 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5444 if (die && die->removed)
5446 TYPE_SYMTAB_DIE (type) = NULL;
5447 return NULL;
5449 return die;
5452 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5453 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5454 anonymous type instead the one of the naming typedef. */
5456 static inline dw_die_ref
5457 strip_naming_typedef (tree type, dw_die_ref type_die)
5459 if (type
5460 && TREE_CODE (type) == RECORD_TYPE
5461 && type_die
5462 && type_die->die_tag == DW_TAG_typedef
5463 && is_naming_typedef_decl (TYPE_NAME (type)))
5464 type_die = get_AT_ref (type_die, DW_AT_type);
5465 return type_die;
5468 /* Like lookup_type_die, but if type is an anonymous type named by a
5469 typedef[1], return the DIE of the anonymous type instead the one of
5470 the naming typedef. This is because in gen_typedef_die, we did
5471 equate the anonymous struct named by the typedef with the DIE of
5472 the naming typedef. So by default, lookup_type_die on an anonymous
5473 struct yields the DIE of the naming typedef.
5475 [1]: Read the comment of is_naming_typedef_decl to learn about what
5476 a naming typedef is. */
5478 static inline dw_die_ref
5479 lookup_type_die_strip_naming_typedef (tree type)
5481 dw_die_ref die = lookup_type_die (type);
5482 return strip_naming_typedef (type, die);
5485 /* Equate a DIE to a given type specifier. */
5487 static inline void
5488 equate_type_number_to_die (tree type, dw_die_ref type_die)
5490 TYPE_SYMTAB_DIE (type) = type_die;
5493 /* Returns a hash value for X (which really is a die_struct). */
5495 inline hashval_t
5496 decl_die_hasher::hash (die_node *x)
5498 return (hashval_t) x->decl_id;
5501 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5503 inline bool
5504 decl_die_hasher::equal (die_node *x, tree y)
5506 return (x->decl_id == DECL_UID (y));
5509 /* Return the DIE associated with a given declaration. */
5511 static inline dw_die_ref
5512 lookup_decl_die (tree decl)
5514 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5515 NO_INSERT);
5516 if (!die)
5517 return NULL;
5518 if ((*die)->removed)
5520 decl_die_table->clear_slot (die);
5521 return NULL;
5523 return *die;
5527 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5528 style reference. Return true if we found one refering to a DIE for
5529 DECL, otherwise return false. */
5531 static bool
5532 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5533 unsigned HOST_WIDE_INT *off)
5535 dw_die_ref die;
5537 if (flag_wpa && !decl_die_table)
5538 return false;
5540 if (TREE_CODE (decl) == BLOCK)
5541 die = BLOCK_DIE (decl);
5542 else
5543 die = lookup_decl_die (decl);
5544 if (!die)
5545 return false;
5547 /* During WPA stage we currently use DIEs to store the
5548 decl <-> label + offset map. That's quite inefficient but it
5549 works for now. */
5550 if (flag_wpa)
5552 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5553 if (!ref)
5555 gcc_assert (die == comp_unit_die ());
5556 return false;
5558 *off = ref->die_offset;
5559 *sym = ref->die_id.die_symbol;
5560 return true;
5563 /* Similar to get_ref_die_offset_label, but using the "correct"
5564 label. */
5565 *off = die->die_offset;
5566 while (die->die_parent)
5567 die = die->die_parent;
5568 /* For the containing CU DIE we compute a die_symbol in
5569 compute_comp_unit_symbol. */
5570 gcc_assert (die->die_tag == DW_TAG_compile_unit
5571 && die->die_id.die_symbol != NULL);
5572 *sym = die->die_id.die_symbol;
5573 return true;
5576 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5578 static void
5579 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5580 const char *symbol, HOST_WIDE_INT offset)
5582 /* Create a fake DIE that contains the reference. Don't use
5583 new_die because we don't want to end up in the limbo list. */
5584 dw_die_ref ref = new_die_raw (die->die_tag);
5585 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5586 ref->die_offset = offset;
5587 ref->with_offset = 1;
5588 add_AT_die_ref (die, attr_kind, ref);
5591 /* Create a DIE for DECL if required and add a reference to a DIE
5592 at SYMBOL + OFFSET which contains attributes dumped early. */
5594 static void
5595 dwarf2out_register_external_die (tree decl, const char *sym,
5596 unsigned HOST_WIDE_INT off)
5598 if (debug_info_level == DINFO_LEVEL_NONE)
5599 return;
5601 if (flag_wpa && !decl_die_table)
5602 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5604 dw_die_ref die
5605 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5606 gcc_assert (!die);
5608 tree ctx;
5609 dw_die_ref parent = NULL;
5610 /* Need to lookup a DIE for the decls context - the containing
5611 function or translation unit. */
5612 if (TREE_CODE (decl) == BLOCK)
5614 ctx = BLOCK_SUPERCONTEXT (decl);
5615 /* ??? We do not output DIEs for all scopes thus skip as
5616 many DIEs as needed. */
5617 while (TREE_CODE (ctx) == BLOCK
5618 && !BLOCK_DIE (ctx))
5619 ctx = BLOCK_SUPERCONTEXT (ctx);
5621 else
5622 ctx = DECL_CONTEXT (decl);
5623 while (ctx && TYPE_P (ctx))
5624 ctx = TYPE_CONTEXT (ctx);
5625 if (ctx)
5627 if (TREE_CODE (ctx) == BLOCK)
5628 parent = BLOCK_DIE (ctx);
5629 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5630 /* Keep the 1:1 association during WPA. */
5631 && !flag_wpa)
5632 /* Otherwise all late annotations go to the main CU which
5633 imports the original CUs. */
5634 parent = comp_unit_die ();
5635 else if (TREE_CODE (ctx) == FUNCTION_DECL
5636 && TREE_CODE (decl) != PARM_DECL
5637 && TREE_CODE (decl) != BLOCK)
5638 /* Leave function local entities parent determination to when
5639 we process scope vars. */
5641 else
5642 parent = lookup_decl_die (ctx);
5644 else
5645 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5646 Handle this case gracefully by globalizing stuff. */
5647 parent = comp_unit_die ();
5648 /* Create a DIE "stub". */
5649 switch (TREE_CODE (decl))
5651 case TRANSLATION_UNIT_DECL:
5652 if (! flag_wpa)
5654 die = comp_unit_die ();
5655 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5656 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5657 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5658 to create a DIE for the original CUs. */
5659 return;
5661 /* Keep the 1:1 association during WPA. */
5662 die = new_die (DW_TAG_compile_unit, NULL, decl);
5663 break;
5664 case NAMESPACE_DECL:
5665 if (is_fortran (decl))
5666 die = new_die (DW_TAG_module, parent, decl);
5667 else
5668 die = new_die (DW_TAG_namespace, parent, decl);
5669 break;
5670 case FUNCTION_DECL:
5671 die = new_die (DW_TAG_subprogram, parent, decl);
5672 break;
5673 case VAR_DECL:
5674 die = new_die (DW_TAG_variable, parent, decl);
5675 break;
5676 case RESULT_DECL:
5677 die = new_die (DW_TAG_variable, parent, decl);
5678 break;
5679 case PARM_DECL:
5680 die = new_die (DW_TAG_formal_parameter, parent, decl);
5681 break;
5682 case CONST_DECL:
5683 die = new_die (DW_TAG_constant, parent, decl);
5684 break;
5685 case LABEL_DECL:
5686 die = new_die (DW_TAG_label, parent, decl);
5687 break;
5688 case BLOCK:
5689 die = new_die (DW_TAG_lexical_block, parent, decl);
5690 break;
5691 default:
5692 gcc_unreachable ();
5694 if (TREE_CODE (decl) == BLOCK)
5695 BLOCK_DIE (decl) = die;
5696 else
5697 equate_decl_number_to_die (decl, die);
5699 /* Add a reference to the DIE providing early debug at $sym + off. */
5700 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5703 /* Returns a hash value for X (which really is a var_loc_list). */
5705 inline hashval_t
5706 decl_loc_hasher::hash (var_loc_list *x)
5708 return (hashval_t) x->decl_id;
5711 /* Return nonzero if decl_id of var_loc_list X is the same as
5712 UID of decl *Y. */
5714 inline bool
5715 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5717 return (x->decl_id == DECL_UID (y));
5720 /* Return the var_loc list associated with a given declaration. */
5722 static inline var_loc_list *
5723 lookup_decl_loc (const_tree decl)
5725 if (!decl_loc_table)
5726 return NULL;
5727 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5730 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5732 inline hashval_t
5733 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5735 return (hashval_t) x->decl_id;
5738 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5739 UID of decl *Y. */
5741 inline bool
5742 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5744 return (x->decl_id == DECL_UID (y));
5747 /* Equate a DIE to a particular declaration. */
5749 static void
5750 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5752 unsigned int decl_id = DECL_UID (decl);
5754 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5755 decl_die->decl_id = decl_id;
5758 /* Return how many bits covers PIECE EXPR_LIST. */
5760 static HOST_WIDE_INT
5761 decl_piece_bitsize (rtx piece)
5763 int ret = (int) GET_MODE (piece);
5764 if (ret)
5765 return ret;
5766 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5767 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5768 return INTVAL (XEXP (XEXP (piece, 0), 0));
5771 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5773 static rtx *
5774 decl_piece_varloc_ptr (rtx piece)
5776 if ((int) GET_MODE (piece))
5777 return &XEXP (piece, 0);
5778 else
5779 return &XEXP (XEXP (piece, 0), 1);
5782 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5783 Next is the chain of following piece nodes. */
5785 static rtx_expr_list *
5786 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5788 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5789 return alloc_EXPR_LIST (bitsize, loc_note, next);
5790 else
5791 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5792 GEN_INT (bitsize),
5793 loc_note), next);
5796 /* Return rtx that should be stored into loc field for
5797 LOC_NOTE and BITPOS/BITSIZE. */
5799 static rtx
5800 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5801 HOST_WIDE_INT bitsize)
5803 if (bitsize != -1)
5805 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5806 if (bitpos != 0)
5807 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5809 return loc_note;
5812 /* This function either modifies location piece list *DEST in
5813 place (if SRC and INNER is NULL), or copies location piece list
5814 *SRC to *DEST while modifying it. Location BITPOS is modified
5815 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5816 not copied and if needed some padding around it is added.
5817 When modifying in place, DEST should point to EXPR_LIST where
5818 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5819 to the start of the whole list and INNER points to the EXPR_LIST
5820 where earlier pieces cover PIECE_BITPOS bits. */
5822 static void
5823 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5824 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5825 HOST_WIDE_INT bitsize, rtx loc_note)
5827 HOST_WIDE_INT diff;
5828 bool copy = inner != NULL;
5830 if (copy)
5832 /* First copy all nodes preceding the current bitpos. */
5833 while (src != inner)
5835 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5836 decl_piece_bitsize (*src), NULL_RTX);
5837 dest = &XEXP (*dest, 1);
5838 src = &XEXP (*src, 1);
5841 /* Add padding if needed. */
5842 if (bitpos != piece_bitpos)
5844 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5845 copy ? NULL_RTX : *dest);
5846 dest = &XEXP (*dest, 1);
5848 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5850 gcc_assert (!copy);
5851 /* A piece with correct bitpos and bitsize already exist,
5852 just update the location for it and return. */
5853 *decl_piece_varloc_ptr (*dest) = loc_note;
5854 return;
5856 /* Add the piece that changed. */
5857 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5858 dest = &XEXP (*dest, 1);
5859 /* Skip over pieces that overlap it. */
5860 diff = bitpos - piece_bitpos + bitsize;
5861 if (!copy)
5862 src = dest;
5863 while (diff > 0 && *src)
5865 rtx piece = *src;
5866 diff -= decl_piece_bitsize (piece);
5867 if (copy)
5868 src = &XEXP (piece, 1);
5869 else
5871 *src = XEXP (piece, 1);
5872 free_EXPR_LIST_node (piece);
5875 /* Add padding if needed. */
5876 if (diff < 0 && *src)
5878 if (!copy)
5879 dest = src;
5880 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5881 dest = &XEXP (*dest, 1);
5883 if (!copy)
5884 return;
5885 /* Finally copy all nodes following it. */
5886 while (*src)
5888 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5889 decl_piece_bitsize (*src), NULL_RTX);
5890 dest = &XEXP (*dest, 1);
5891 src = &XEXP (*src, 1);
5895 /* Add a variable location node to the linked list for DECL. */
5897 static struct var_loc_node *
5898 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5900 unsigned int decl_id;
5901 var_loc_list *temp;
5902 struct var_loc_node *loc = NULL;
5903 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5905 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5907 tree realdecl = DECL_DEBUG_EXPR (decl);
5908 if (handled_component_p (realdecl)
5909 || (TREE_CODE (realdecl) == MEM_REF
5910 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5912 HOST_WIDE_INT maxsize;
5913 bool reverse;
5914 tree innerdecl
5915 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5916 &reverse);
5917 if (!DECL_P (innerdecl)
5918 || DECL_IGNORED_P (innerdecl)
5919 || TREE_STATIC (innerdecl)
5920 || bitsize <= 0
5921 || bitpos + bitsize > 256
5922 || bitsize != maxsize)
5923 return NULL;
5924 decl = innerdecl;
5928 decl_id = DECL_UID (decl);
5929 var_loc_list **slot
5930 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5931 if (*slot == NULL)
5933 temp = ggc_cleared_alloc<var_loc_list> ();
5934 temp->decl_id = decl_id;
5935 *slot = temp;
5937 else
5938 temp = *slot;
5940 /* For PARM_DECLs try to keep around the original incoming value,
5941 even if that means we'll emit a zero-range .debug_loc entry. */
5942 if (temp->last
5943 && temp->first == temp->last
5944 && TREE_CODE (decl) == PARM_DECL
5945 && NOTE_P (temp->first->loc)
5946 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5947 && DECL_INCOMING_RTL (decl)
5948 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5949 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5950 == GET_CODE (DECL_INCOMING_RTL (decl))
5951 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5952 && (bitsize != -1
5953 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5954 NOTE_VAR_LOCATION_LOC (loc_note))
5955 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5956 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5958 loc = ggc_cleared_alloc<var_loc_node> ();
5959 temp->first->next = loc;
5960 temp->last = loc;
5961 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5963 else if (temp->last)
5965 struct var_loc_node *last = temp->last, *unused = NULL;
5966 rtx *piece_loc = NULL, last_loc_note;
5967 HOST_WIDE_INT piece_bitpos = 0;
5968 if (last->next)
5970 last = last->next;
5971 gcc_assert (last->next == NULL);
5973 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5975 piece_loc = &last->loc;
5978 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5979 if (piece_bitpos + cur_bitsize > bitpos)
5980 break;
5981 piece_bitpos += cur_bitsize;
5982 piece_loc = &XEXP (*piece_loc, 1);
5984 while (*piece_loc);
5986 /* TEMP->LAST here is either pointer to the last but one or
5987 last element in the chained list, LAST is pointer to the
5988 last element. */
5989 if (label && strcmp (last->label, label) == 0)
5991 /* For SRA optimized variables if there weren't any real
5992 insns since last note, just modify the last node. */
5993 if (piece_loc != NULL)
5995 adjust_piece_list (piece_loc, NULL, NULL,
5996 bitpos, piece_bitpos, bitsize, loc_note);
5997 return NULL;
5999 /* If the last note doesn't cover any instructions, remove it. */
6000 if (temp->last != last)
6002 temp->last->next = NULL;
6003 unused = last;
6004 last = temp->last;
6005 gcc_assert (strcmp (last->label, label) != 0);
6007 else
6009 gcc_assert (temp->first == temp->last
6010 || (temp->first->next == temp->last
6011 && TREE_CODE (decl) == PARM_DECL));
6012 memset (temp->last, '\0', sizeof (*temp->last));
6013 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6014 return temp->last;
6017 if (bitsize == -1 && NOTE_P (last->loc))
6018 last_loc_note = last->loc;
6019 else if (piece_loc != NULL
6020 && *piece_loc != NULL_RTX
6021 && piece_bitpos == bitpos
6022 && decl_piece_bitsize (*piece_loc) == bitsize)
6023 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6024 else
6025 last_loc_note = NULL_RTX;
6026 /* If the current location is the same as the end of the list,
6027 and either both or neither of the locations is uninitialized,
6028 we have nothing to do. */
6029 if (last_loc_note == NULL_RTX
6030 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6031 NOTE_VAR_LOCATION_LOC (loc_note)))
6032 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6033 != NOTE_VAR_LOCATION_STATUS (loc_note))
6034 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6035 == VAR_INIT_STATUS_UNINITIALIZED)
6036 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6037 == VAR_INIT_STATUS_UNINITIALIZED))))
6039 /* Add LOC to the end of list and update LAST. If the last
6040 element of the list has been removed above, reuse its
6041 memory for the new node, otherwise allocate a new one. */
6042 if (unused)
6044 loc = unused;
6045 memset (loc, '\0', sizeof (*loc));
6047 else
6048 loc = ggc_cleared_alloc<var_loc_node> ();
6049 if (bitsize == -1 || piece_loc == NULL)
6050 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6051 else
6052 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6053 bitpos, piece_bitpos, bitsize, loc_note);
6054 last->next = loc;
6055 /* Ensure TEMP->LAST will point either to the new last but one
6056 element of the chain, or to the last element in it. */
6057 if (last != temp->last)
6058 temp->last = last;
6060 else if (unused)
6061 ggc_free (unused);
6063 else
6065 loc = ggc_cleared_alloc<var_loc_node> ();
6066 temp->first = loc;
6067 temp->last = loc;
6068 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6070 return loc;
6073 /* Keep track of the number of spaces used to indent the
6074 output of the debugging routines that print the structure of
6075 the DIE internal representation. */
6076 static int print_indent;
6078 /* Indent the line the number of spaces given by print_indent. */
6080 static inline void
6081 print_spaces (FILE *outfile)
6083 fprintf (outfile, "%*s", print_indent, "");
6086 /* Print a type signature in hex. */
6088 static inline void
6089 print_signature (FILE *outfile, char *sig)
6091 int i;
6093 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6094 fprintf (outfile, "%02x", sig[i] & 0xff);
6097 static inline void
6098 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6100 if (discr_value->pos)
6101 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6102 else
6103 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6106 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6108 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6109 RECURSE, output location descriptor operations. */
6111 static void
6112 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6114 switch (val->val_class)
6116 case dw_val_class_addr:
6117 fprintf (outfile, "address");
6118 break;
6119 case dw_val_class_offset:
6120 fprintf (outfile, "offset");
6121 break;
6122 case dw_val_class_loc:
6123 fprintf (outfile, "location descriptor");
6124 if (val->v.val_loc == NULL)
6125 fprintf (outfile, " -> <null>\n");
6126 else if (recurse)
6128 fprintf (outfile, ":\n");
6129 print_indent += 4;
6130 print_loc_descr (val->v.val_loc, outfile);
6131 print_indent -= 4;
6133 else
6134 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6135 break;
6136 case dw_val_class_loc_list:
6137 fprintf (outfile, "location list -> label:%s",
6138 val->v.val_loc_list->ll_symbol);
6139 break;
6140 case dw_val_class_range_list:
6141 fprintf (outfile, "range list");
6142 break;
6143 case dw_val_class_const:
6144 case dw_val_class_const_implicit:
6145 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6146 break;
6147 case dw_val_class_unsigned_const:
6148 case dw_val_class_unsigned_const_implicit:
6149 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6150 break;
6151 case dw_val_class_const_double:
6152 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6153 HOST_WIDE_INT_PRINT_UNSIGNED")",
6154 val->v.val_double.high,
6155 val->v.val_double.low);
6156 break;
6157 case dw_val_class_wide_int:
6159 int i = val->v.val_wide->get_len ();
6160 fprintf (outfile, "constant (");
6161 gcc_assert (i > 0);
6162 if (val->v.val_wide->elt (i - 1) == 0)
6163 fprintf (outfile, "0x");
6164 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6165 val->v.val_wide->elt (--i));
6166 while (--i >= 0)
6167 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6168 val->v.val_wide->elt (i));
6169 fprintf (outfile, ")");
6170 break;
6172 case dw_val_class_vec:
6173 fprintf (outfile, "floating-point or vector constant");
6174 break;
6175 case dw_val_class_flag:
6176 fprintf (outfile, "%u", val->v.val_flag);
6177 break;
6178 case dw_val_class_die_ref:
6179 if (val->v.val_die_ref.die != NULL)
6181 dw_die_ref die = val->v.val_die_ref.die;
6183 if (die->comdat_type_p)
6185 fprintf (outfile, "die -> signature: ");
6186 print_signature (outfile,
6187 die->die_id.die_type_node->signature);
6189 else if (die->die_id.die_symbol)
6191 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6192 if (die->with_offset)
6193 fprintf (outfile, " + %ld", die->die_offset);
6195 else
6196 fprintf (outfile, "die -> %ld", die->die_offset);
6197 fprintf (outfile, " (%p)", (void *) die);
6199 else
6200 fprintf (outfile, "die -> <null>");
6201 break;
6202 case dw_val_class_vms_delta:
6203 fprintf (outfile, "delta: @slotcount(%s-%s)",
6204 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6205 break;
6206 case dw_val_class_lbl_id:
6207 case dw_val_class_lineptr:
6208 case dw_val_class_macptr:
6209 case dw_val_class_loclistsptr:
6210 case dw_val_class_high_pc:
6211 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6212 break;
6213 case dw_val_class_str:
6214 if (val->v.val_str->str != NULL)
6215 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6216 else
6217 fprintf (outfile, "<null>");
6218 break;
6219 case dw_val_class_file:
6220 case dw_val_class_file_implicit:
6221 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6222 val->v.val_file->emitted_number);
6223 break;
6224 case dw_val_class_data8:
6226 int i;
6228 for (i = 0; i < 8; i++)
6229 fprintf (outfile, "%02x", val->v.val_data8[i]);
6230 break;
6232 case dw_val_class_discr_value:
6233 print_discr_value (outfile, &val->v.val_discr_value);
6234 break;
6235 case dw_val_class_discr_list:
6236 for (dw_discr_list_ref node = val->v.val_discr_list;
6237 node != NULL;
6238 node = node->dw_discr_next)
6240 if (node->dw_discr_range)
6242 fprintf (outfile, " .. ");
6243 print_discr_value (outfile, &node->dw_discr_lower_bound);
6244 print_discr_value (outfile, &node->dw_discr_upper_bound);
6246 else
6247 print_discr_value (outfile, &node->dw_discr_lower_bound);
6249 if (node->dw_discr_next != NULL)
6250 fprintf (outfile, " | ");
6252 default:
6253 break;
6257 /* Likewise, for a DIE attribute. */
6259 static void
6260 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6262 print_dw_val (&a->dw_attr_val, recurse, outfile);
6266 /* Print the list of operands in the LOC location description to OUTFILE. This
6267 routine is a debugging aid only. */
6269 static void
6270 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6272 dw_loc_descr_ref l = loc;
6274 if (loc == NULL)
6276 print_spaces (outfile);
6277 fprintf (outfile, "<null>\n");
6278 return;
6281 for (l = loc; l != NULL; l = l->dw_loc_next)
6283 print_spaces (outfile);
6284 fprintf (outfile, "(%p) %s",
6285 (void *) l,
6286 dwarf_stack_op_name (l->dw_loc_opc));
6287 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6289 fprintf (outfile, " ");
6290 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6292 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6294 fprintf (outfile, ", ");
6295 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6297 fprintf (outfile, "\n");
6301 /* Print the information associated with a given DIE, and its children.
6302 This routine is a debugging aid only. */
6304 static void
6305 print_die (dw_die_ref die, FILE *outfile)
6307 dw_attr_node *a;
6308 dw_die_ref c;
6309 unsigned ix;
6311 print_spaces (outfile);
6312 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6313 die->die_offset, dwarf_tag_name (die->die_tag),
6314 (void*) die);
6315 print_spaces (outfile);
6316 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6317 fprintf (outfile, " offset: %ld", die->die_offset);
6318 fprintf (outfile, " mark: %d\n", die->die_mark);
6320 if (die->comdat_type_p)
6322 print_spaces (outfile);
6323 fprintf (outfile, " signature: ");
6324 print_signature (outfile, die->die_id.die_type_node->signature);
6325 fprintf (outfile, "\n");
6328 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6330 print_spaces (outfile);
6331 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6333 print_attribute (a, true, outfile);
6334 fprintf (outfile, "\n");
6337 if (die->die_child != NULL)
6339 print_indent += 4;
6340 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6341 print_indent -= 4;
6343 if (print_indent == 0)
6344 fprintf (outfile, "\n");
6347 /* Print the list of operations in the LOC location description. */
6349 DEBUG_FUNCTION void
6350 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6352 print_loc_descr (loc, stderr);
6355 /* Print the information collected for a given DIE. */
6357 DEBUG_FUNCTION void
6358 debug_dwarf_die (dw_die_ref die)
6360 print_die (die, stderr);
6363 DEBUG_FUNCTION void
6364 debug (die_struct &ref)
6366 print_die (&ref, stderr);
6369 DEBUG_FUNCTION void
6370 debug (die_struct *ptr)
6372 if (ptr)
6373 debug (*ptr);
6374 else
6375 fprintf (stderr, "<nil>\n");
6379 /* Print all DWARF information collected for the compilation unit.
6380 This routine is a debugging aid only. */
6382 DEBUG_FUNCTION void
6383 debug_dwarf (void)
6385 print_indent = 0;
6386 print_die (comp_unit_die (), stderr);
6389 /* Verify the DIE tree structure. */
6391 DEBUG_FUNCTION void
6392 verify_die (dw_die_ref die)
6394 gcc_assert (!die->die_mark);
6395 if (die->die_parent == NULL
6396 && die->die_sib == NULL)
6397 return;
6398 /* Verify the die_sib list is cyclic. */
6399 dw_die_ref x = die;
6402 x->die_mark = 1;
6403 x = x->die_sib;
6405 while (x && !x->die_mark);
6406 gcc_assert (x == die);
6407 x = die;
6410 /* Verify all dies have the same parent. */
6411 gcc_assert (x->die_parent == die->die_parent);
6412 if (x->die_child)
6414 /* Verify the child has the proper parent and recurse. */
6415 gcc_assert (x->die_child->die_parent == x);
6416 verify_die (x->die_child);
6418 x->die_mark = 0;
6419 x = x->die_sib;
6421 while (x && x->die_mark);
6424 /* Sanity checks on DIEs. */
6426 static void
6427 check_die (dw_die_ref die)
6429 unsigned ix;
6430 dw_attr_node *a;
6431 bool inline_found = false;
6432 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6433 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6434 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6436 switch (a->dw_attr)
6438 case DW_AT_inline:
6439 if (a->dw_attr_val.v.val_unsigned)
6440 inline_found = true;
6441 break;
6442 case DW_AT_location:
6443 ++n_location;
6444 break;
6445 case DW_AT_low_pc:
6446 ++n_low_pc;
6447 break;
6448 case DW_AT_high_pc:
6449 ++n_high_pc;
6450 break;
6451 case DW_AT_artificial:
6452 ++n_artificial;
6453 break;
6454 case DW_AT_decl_column:
6455 ++n_decl_column;
6456 break;
6457 case DW_AT_decl_line:
6458 ++n_decl_line;
6459 break;
6460 case DW_AT_decl_file:
6461 ++n_decl_file;
6462 break;
6463 default:
6464 break;
6467 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6468 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6470 fprintf (stderr, "Duplicate attributes in DIE:\n");
6471 debug_dwarf_die (die);
6472 gcc_unreachable ();
6474 if (inline_found)
6476 /* A debugging information entry that is a member of an abstract
6477 instance tree [that has DW_AT_inline] should not contain any
6478 attributes which describe aspects of the subroutine which vary
6479 between distinct inlined expansions or distinct out-of-line
6480 expansions. */
6481 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6482 gcc_assert (a->dw_attr != DW_AT_low_pc
6483 && a->dw_attr != DW_AT_high_pc
6484 && a->dw_attr != DW_AT_location
6485 && a->dw_attr != DW_AT_frame_base
6486 && a->dw_attr != DW_AT_call_all_calls
6487 && a->dw_attr != DW_AT_GNU_all_call_sites);
6491 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6492 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6493 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6495 /* Calculate the checksum of a location expression. */
6497 static inline void
6498 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6500 int tem;
6501 inchash::hash hstate;
6502 hashval_t hash;
6504 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6505 CHECKSUM (tem);
6506 hash_loc_operands (loc, hstate);
6507 hash = hstate.end();
6508 CHECKSUM (hash);
6511 /* Calculate the checksum of an attribute. */
6513 static void
6514 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6516 dw_loc_descr_ref loc;
6517 rtx r;
6519 CHECKSUM (at->dw_attr);
6521 /* We don't care that this was compiled with a different compiler
6522 snapshot; if the output is the same, that's what matters. */
6523 if (at->dw_attr == DW_AT_producer)
6524 return;
6526 switch (AT_class (at))
6528 case dw_val_class_const:
6529 case dw_val_class_const_implicit:
6530 CHECKSUM (at->dw_attr_val.v.val_int);
6531 break;
6532 case dw_val_class_unsigned_const:
6533 case dw_val_class_unsigned_const_implicit:
6534 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6535 break;
6536 case dw_val_class_const_double:
6537 CHECKSUM (at->dw_attr_val.v.val_double);
6538 break;
6539 case dw_val_class_wide_int:
6540 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6541 get_full_len (*at->dw_attr_val.v.val_wide)
6542 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6543 break;
6544 case dw_val_class_vec:
6545 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6546 (at->dw_attr_val.v.val_vec.length
6547 * at->dw_attr_val.v.val_vec.elt_size));
6548 break;
6549 case dw_val_class_flag:
6550 CHECKSUM (at->dw_attr_val.v.val_flag);
6551 break;
6552 case dw_val_class_str:
6553 CHECKSUM_STRING (AT_string (at));
6554 break;
6556 case dw_val_class_addr:
6557 r = AT_addr (at);
6558 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6559 CHECKSUM_STRING (XSTR (r, 0));
6560 break;
6562 case dw_val_class_offset:
6563 CHECKSUM (at->dw_attr_val.v.val_offset);
6564 break;
6566 case dw_val_class_loc:
6567 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6568 loc_checksum (loc, ctx);
6569 break;
6571 case dw_val_class_die_ref:
6572 die_checksum (AT_ref (at), ctx, mark);
6573 break;
6575 case dw_val_class_fde_ref:
6576 case dw_val_class_vms_delta:
6577 case dw_val_class_lbl_id:
6578 case dw_val_class_lineptr:
6579 case dw_val_class_macptr:
6580 case dw_val_class_loclistsptr:
6581 case dw_val_class_high_pc:
6582 break;
6584 case dw_val_class_file:
6585 case dw_val_class_file_implicit:
6586 CHECKSUM_STRING (AT_file (at)->filename);
6587 break;
6589 case dw_val_class_data8:
6590 CHECKSUM (at->dw_attr_val.v.val_data8);
6591 break;
6593 default:
6594 break;
6598 /* Calculate the checksum of a DIE. */
6600 static void
6601 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6603 dw_die_ref c;
6604 dw_attr_node *a;
6605 unsigned ix;
6607 /* To avoid infinite recursion. */
6608 if (die->die_mark)
6610 CHECKSUM (die->die_mark);
6611 return;
6613 die->die_mark = ++(*mark);
6615 CHECKSUM (die->die_tag);
6617 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6618 attr_checksum (a, ctx, mark);
6620 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6623 #undef CHECKSUM
6624 #undef CHECKSUM_BLOCK
6625 #undef CHECKSUM_STRING
6627 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6628 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6629 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6630 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6631 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6632 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6633 #define CHECKSUM_ATTR(FOO) \
6634 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6636 /* Calculate the checksum of a number in signed LEB128 format. */
6638 static void
6639 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6641 unsigned char byte;
6642 bool more;
6644 while (1)
6646 byte = (value & 0x7f);
6647 value >>= 7;
6648 more = !((value == 0 && (byte & 0x40) == 0)
6649 || (value == -1 && (byte & 0x40) != 0));
6650 if (more)
6651 byte |= 0x80;
6652 CHECKSUM (byte);
6653 if (!more)
6654 break;
6658 /* Calculate the checksum of a number in unsigned LEB128 format. */
6660 static void
6661 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6663 while (1)
6665 unsigned char byte = (value & 0x7f);
6666 value >>= 7;
6667 if (value != 0)
6668 /* More bytes to follow. */
6669 byte |= 0x80;
6670 CHECKSUM (byte);
6671 if (value == 0)
6672 break;
6676 /* Checksum the context of the DIE. This adds the names of any
6677 surrounding namespaces or structures to the checksum. */
6679 static void
6680 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6682 const char *name;
6683 dw_die_ref spec;
6684 int tag = die->die_tag;
6686 if (tag != DW_TAG_namespace
6687 && tag != DW_TAG_structure_type
6688 && tag != DW_TAG_class_type)
6689 return;
6691 name = get_AT_string (die, DW_AT_name);
6693 spec = get_AT_ref (die, DW_AT_specification);
6694 if (spec != NULL)
6695 die = spec;
6697 if (die->die_parent != NULL)
6698 checksum_die_context (die->die_parent, ctx);
6700 CHECKSUM_ULEB128 ('C');
6701 CHECKSUM_ULEB128 (tag);
6702 if (name != NULL)
6703 CHECKSUM_STRING (name);
6706 /* Calculate the checksum of a location expression. */
6708 static inline void
6709 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6711 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6712 were emitted as a DW_FORM_sdata instead of a location expression. */
6713 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6715 CHECKSUM_ULEB128 (DW_FORM_sdata);
6716 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6717 return;
6720 /* Otherwise, just checksum the raw location expression. */
6721 while (loc != NULL)
6723 inchash::hash hstate;
6724 hashval_t hash;
6726 CHECKSUM_ULEB128 (loc->dtprel);
6727 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6728 hash_loc_operands (loc, hstate);
6729 hash = hstate.end ();
6730 CHECKSUM (hash);
6731 loc = loc->dw_loc_next;
6735 /* Calculate the checksum of an attribute. */
6737 static void
6738 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6739 struct md5_ctx *ctx, int *mark)
6741 dw_loc_descr_ref loc;
6742 rtx r;
6744 if (AT_class (at) == dw_val_class_die_ref)
6746 dw_die_ref target_die = AT_ref (at);
6748 /* For pointer and reference types, we checksum only the (qualified)
6749 name of the target type (if there is a name). For friend entries,
6750 we checksum only the (qualified) name of the target type or function.
6751 This allows the checksum to remain the same whether the target type
6752 is complete or not. */
6753 if ((at->dw_attr == DW_AT_type
6754 && (tag == DW_TAG_pointer_type
6755 || tag == DW_TAG_reference_type
6756 || tag == DW_TAG_rvalue_reference_type
6757 || tag == DW_TAG_ptr_to_member_type))
6758 || (at->dw_attr == DW_AT_friend
6759 && tag == DW_TAG_friend))
6761 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6763 if (name_attr != NULL)
6765 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6767 if (decl == NULL)
6768 decl = target_die;
6769 CHECKSUM_ULEB128 ('N');
6770 CHECKSUM_ULEB128 (at->dw_attr);
6771 if (decl->die_parent != NULL)
6772 checksum_die_context (decl->die_parent, ctx);
6773 CHECKSUM_ULEB128 ('E');
6774 CHECKSUM_STRING (AT_string (name_attr));
6775 return;
6779 /* For all other references to another DIE, we check to see if the
6780 target DIE has already been visited. If it has, we emit a
6781 backward reference; if not, we descend recursively. */
6782 if (target_die->die_mark > 0)
6784 CHECKSUM_ULEB128 ('R');
6785 CHECKSUM_ULEB128 (at->dw_attr);
6786 CHECKSUM_ULEB128 (target_die->die_mark);
6788 else
6790 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6792 if (decl == NULL)
6793 decl = target_die;
6794 target_die->die_mark = ++(*mark);
6795 CHECKSUM_ULEB128 ('T');
6796 CHECKSUM_ULEB128 (at->dw_attr);
6797 if (decl->die_parent != NULL)
6798 checksum_die_context (decl->die_parent, ctx);
6799 die_checksum_ordered (target_die, ctx, mark);
6801 return;
6804 CHECKSUM_ULEB128 ('A');
6805 CHECKSUM_ULEB128 (at->dw_attr);
6807 switch (AT_class (at))
6809 case dw_val_class_const:
6810 case dw_val_class_const_implicit:
6811 CHECKSUM_ULEB128 (DW_FORM_sdata);
6812 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6813 break;
6815 case dw_val_class_unsigned_const:
6816 case dw_val_class_unsigned_const_implicit:
6817 CHECKSUM_ULEB128 (DW_FORM_sdata);
6818 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6819 break;
6821 case dw_val_class_const_double:
6822 CHECKSUM_ULEB128 (DW_FORM_block);
6823 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6824 CHECKSUM (at->dw_attr_val.v.val_double);
6825 break;
6827 case dw_val_class_wide_int:
6828 CHECKSUM_ULEB128 (DW_FORM_block);
6829 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6830 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6831 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6832 get_full_len (*at->dw_attr_val.v.val_wide)
6833 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6834 break;
6836 case dw_val_class_vec:
6837 CHECKSUM_ULEB128 (DW_FORM_block);
6838 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6839 * at->dw_attr_val.v.val_vec.elt_size);
6840 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6841 (at->dw_attr_val.v.val_vec.length
6842 * at->dw_attr_val.v.val_vec.elt_size));
6843 break;
6845 case dw_val_class_flag:
6846 CHECKSUM_ULEB128 (DW_FORM_flag);
6847 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6848 break;
6850 case dw_val_class_str:
6851 CHECKSUM_ULEB128 (DW_FORM_string);
6852 CHECKSUM_STRING (AT_string (at));
6853 break;
6855 case dw_val_class_addr:
6856 r = AT_addr (at);
6857 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6858 CHECKSUM_ULEB128 (DW_FORM_string);
6859 CHECKSUM_STRING (XSTR (r, 0));
6860 break;
6862 case dw_val_class_offset:
6863 CHECKSUM_ULEB128 (DW_FORM_sdata);
6864 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6865 break;
6867 case dw_val_class_loc:
6868 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6869 loc_checksum_ordered (loc, ctx);
6870 break;
6872 case dw_val_class_fde_ref:
6873 case dw_val_class_lbl_id:
6874 case dw_val_class_lineptr:
6875 case dw_val_class_macptr:
6876 case dw_val_class_loclistsptr:
6877 case dw_val_class_high_pc:
6878 break;
6880 case dw_val_class_file:
6881 case dw_val_class_file_implicit:
6882 CHECKSUM_ULEB128 (DW_FORM_string);
6883 CHECKSUM_STRING (AT_file (at)->filename);
6884 break;
6886 case dw_val_class_data8:
6887 CHECKSUM (at->dw_attr_val.v.val_data8);
6888 break;
6890 default:
6891 break;
6895 struct checksum_attributes
6897 dw_attr_node *at_name;
6898 dw_attr_node *at_type;
6899 dw_attr_node *at_friend;
6900 dw_attr_node *at_accessibility;
6901 dw_attr_node *at_address_class;
6902 dw_attr_node *at_alignment;
6903 dw_attr_node *at_allocated;
6904 dw_attr_node *at_artificial;
6905 dw_attr_node *at_associated;
6906 dw_attr_node *at_binary_scale;
6907 dw_attr_node *at_bit_offset;
6908 dw_attr_node *at_bit_size;
6909 dw_attr_node *at_bit_stride;
6910 dw_attr_node *at_byte_size;
6911 dw_attr_node *at_byte_stride;
6912 dw_attr_node *at_const_value;
6913 dw_attr_node *at_containing_type;
6914 dw_attr_node *at_count;
6915 dw_attr_node *at_data_location;
6916 dw_attr_node *at_data_member_location;
6917 dw_attr_node *at_decimal_scale;
6918 dw_attr_node *at_decimal_sign;
6919 dw_attr_node *at_default_value;
6920 dw_attr_node *at_digit_count;
6921 dw_attr_node *at_discr;
6922 dw_attr_node *at_discr_list;
6923 dw_attr_node *at_discr_value;
6924 dw_attr_node *at_encoding;
6925 dw_attr_node *at_endianity;
6926 dw_attr_node *at_explicit;
6927 dw_attr_node *at_is_optional;
6928 dw_attr_node *at_location;
6929 dw_attr_node *at_lower_bound;
6930 dw_attr_node *at_mutable;
6931 dw_attr_node *at_ordering;
6932 dw_attr_node *at_picture_string;
6933 dw_attr_node *at_prototyped;
6934 dw_attr_node *at_small;
6935 dw_attr_node *at_segment;
6936 dw_attr_node *at_string_length;
6937 dw_attr_node *at_string_length_bit_size;
6938 dw_attr_node *at_string_length_byte_size;
6939 dw_attr_node *at_threads_scaled;
6940 dw_attr_node *at_upper_bound;
6941 dw_attr_node *at_use_location;
6942 dw_attr_node *at_use_UTF8;
6943 dw_attr_node *at_variable_parameter;
6944 dw_attr_node *at_virtuality;
6945 dw_attr_node *at_visibility;
6946 dw_attr_node *at_vtable_elem_location;
6949 /* Collect the attributes that we will want to use for the checksum. */
6951 static void
6952 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6954 dw_attr_node *a;
6955 unsigned ix;
6957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6959 switch (a->dw_attr)
6961 case DW_AT_name:
6962 attrs->at_name = a;
6963 break;
6964 case DW_AT_type:
6965 attrs->at_type = a;
6966 break;
6967 case DW_AT_friend:
6968 attrs->at_friend = a;
6969 break;
6970 case DW_AT_accessibility:
6971 attrs->at_accessibility = a;
6972 break;
6973 case DW_AT_address_class:
6974 attrs->at_address_class = a;
6975 break;
6976 case DW_AT_alignment:
6977 attrs->at_alignment = a;
6978 break;
6979 case DW_AT_allocated:
6980 attrs->at_allocated = a;
6981 break;
6982 case DW_AT_artificial:
6983 attrs->at_artificial = a;
6984 break;
6985 case DW_AT_associated:
6986 attrs->at_associated = a;
6987 break;
6988 case DW_AT_binary_scale:
6989 attrs->at_binary_scale = a;
6990 break;
6991 case DW_AT_bit_offset:
6992 attrs->at_bit_offset = a;
6993 break;
6994 case DW_AT_bit_size:
6995 attrs->at_bit_size = a;
6996 break;
6997 case DW_AT_bit_stride:
6998 attrs->at_bit_stride = a;
6999 break;
7000 case DW_AT_byte_size:
7001 attrs->at_byte_size = a;
7002 break;
7003 case DW_AT_byte_stride:
7004 attrs->at_byte_stride = a;
7005 break;
7006 case DW_AT_const_value:
7007 attrs->at_const_value = a;
7008 break;
7009 case DW_AT_containing_type:
7010 attrs->at_containing_type = a;
7011 break;
7012 case DW_AT_count:
7013 attrs->at_count = a;
7014 break;
7015 case DW_AT_data_location:
7016 attrs->at_data_location = a;
7017 break;
7018 case DW_AT_data_member_location:
7019 attrs->at_data_member_location = a;
7020 break;
7021 case DW_AT_decimal_scale:
7022 attrs->at_decimal_scale = a;
7023 break;
7024 case DW_AT_decimal_sign:
7025 attrs->at_decimal_sign = a;
7026 break;
7027 case DW_AT_default_value:
7028 attrs->at_default_value = a;
7029 break;
7030 case DW_AT_digit_count:
7031 attrs->at_digit_count = a;
7032 break;
7033 case DW_AT_discr:
7034 attrs->at_discr = a;
7035 break;
7036 case DW_AT_discr_list:
7037 attrs->at_discr_list = a;
7038 break;
7039 case DW_AT_discr_value:
7040 attrs->at_discr_value = a;
7041 break;
7042 case DW_AT_encoding:
7043 attrs->at_encoding = a;
7044 break;
7045 case DW_AT_endianity:
7046 attrs->at_endianity = a;
7047 break;
7048 case DW_AT_explicit:
7049 attrs->at_explicit = a;
7050 break;
7051 case DW_AT_is_optional:
7052 attrs->at_is_optional = a;
7053 break;
7054 case DW_AT_location:
7055 attrs->at_location = a;
7056 break;
7057 case DW_AT_lower_bound:
7058 attrs->at_lower_bound = a;
7059 break;
7060 case DW_AT_mutable:
7061 attrs->at_mutable = a;
7062 break;
7063 case DW_AT_ordering:
7064 attrs->at_ordering = a;
7065 break;
7066 case DW_AT_picture_string:
7067 attrs->at_picture_string = a;
7068 break;
7069 case DW_AT_prototyped:
7070 attrs->at_prototyped = a;
7071 break;
7072 case DW_AT_small:
7073 attrs->at_small = a;
7074 break;
7075 case DW_AT_segment:
7076 attrs->at_segment = a;
7077 break;
7078 case DW_AT_string_length:
7079 attrs->at_string_length = a;
7080 break;
7081 case DW_AT_string_length_bit_size:
7082 attrs->at_string_length_bit_size = a;
7083 break;
7084 case DW_AT_string_length_byte_size:
7085 attrs->at_string_length_byte_size = a;
7086 break;
7087 case DW_AT_threads_scaled:
7088 attrs->at_threads_scaled = a;
7089 break;
7090 case DW_AT_upper_bound:
7091 attrs->at_upper_bound = a;
7092 break;
7093 case DW_AT_use_location:
7094 attrs->at_use_location = a;
7095 break;
7096 case DW_AT_use_UTF8:
7097 attrs->at_use_UTF8 = a;
7098 break;
7099 case DW_AT_variable_parameter:
7100 attrs->at_variable_parameter = a;
7101 break;
7102 case DW_AT_virtuality:
7103 attrs->at_virtuality = a;
7104 break;
7105 case DW_AT_visibility:
7106 attrs->at_visibility = a;
7107 break;
7108 case DW_AT_vtable_elem_location:
7109 attrs->at_vtable_elem_location = a;
7110 break;
7111 default:
7112 break;
7117 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7119 static void
7120 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7122 dw_die_ref c;
7123 dw_die_ref decl;
7124 struct checksum_attributes attrs;
7126 CHECKSUM_ULEB128 ('D');
7127 CHECKSUM_ULEB128 (die->die_tag);
7129 memset (&attrs, 0, sizeof (attrs));
7131 decl = get_AT_ref (die, DW_AT_specification);
7132 if (decl != NULL)
7133 collect_checksum_attributes (&attrs, decl);
7134 collect_checksum_attributes (&attrs, die);
7136 CHECKSUM_ATTR (attrs.at_name);
7137 CHECKSUM_ATTR (attrs.at_accessibility);
7138 CHECKSUM_ATTR (attrs.at_address_class);
7139 CHECKSUM_ATTR (attrs.at_allocated);
7140 CHECKSUM_ATTR (attrs.at_artificial);
7141 CHECKSUM_ATTR (attrs.at_associated);
7142 CHECKSUM_ATTR (attrs.at_binary_scale);
7143 CHECKSUM_ATTR (attrs.at_bit_offset);
7144 CHECKSUM_ATTR (attrs.at_bit_size);
7145 CHECKSUM_ATTR (attrs.at_bit_stride);
7146 CHECKSUM_ATTR (attrs.at_byte_size);
7147 CHECKSUM_ATTR (attrs.at_byte_stride);
7148 CHECKSUM_ATTR (attrs.at_const_value);
7149 CHECKSUM_ATTR (attrs.at_containing_type);
7150 CHECKSUM_ATTR (attrs.at_count);
7151 CHECKSUM_ATTR (attrs.at_data_location);
7152 CHECKSUM_ATTR (attrs.at_data_member_location);
7153 CHECKSUM_ATTR (attrs.at_decimal_scale);
7154 CHECKSUM_ATTR (attrs.at_decimal_sign);
7155 CHECKSUM_ATTR (attrs.at_default_value);
7156 CHECKSUM_ATTR (attrs.at_digit_count);
7157 CHECKSUM_ATTR (attrs.at_discr);
7158 CHECKSUM_ATTR (attrs.at_discr_list);
7159 CHECKSUM_ATTR (attrs.at_discr_value);
7160 CHECKSUM_ATTR (attrs.at_encoding);
7161 CHECKSUM_ATTR (attrs.at_endianity);
7162 CHECKSUM_ATTR (attrs.at_explicit);
7163 CHECKSUM_ATTR (attrs.at_is_optional);
7164 CHECKSUM_ATTR (attrs.at_location);
7165 CHECKSUM_ATTR (attrs.at_lower_bound);
7166 CHECKSUM_ATTR (attrs.at_mutable);
7167 CHECKSUM_ATTR (attrs.at_ordering);
7168 CHECKSUM_ATTR (attrs.at_picture_string);
7169 CHECKSUM_ATTR (attrs.at_prototyped);
7170 CHECKSUM_ATTR (attrs.at_small);
7171 CHECKSUM_ATTR (attrs.at_segment);
7172 CHECKSUM_ATTR (attrs.at_string_length);
7173 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7174 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7175 CHECKSUM_ATTR (attrs.at_threads_scaled);
7176 CHECKSUM_ATTR (attrs.at_upper_bound);
7177 CHECKSUM_ATTR (attrs.at_use_location);
7178 CHECKSUM_ATTR (attrs.at_use_UTF8);
7179 CHECKSUM_ATTR (attrs.at_variable_parameter);
7180 CHECKSUM_ATTR (attrs.at_virtuality);
7181 CHECKSUM_ATTR (attrs.at_visibility);
7182 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7183 CHECKSUM_ATTR (attrs.at_type);
7184 CHECKSUM_ATTR (attrs.at_friend);
7185 CHECKSUM_ATTR (attrs.at_alignment);
7187 /* Checksum the child DIEs. */
7188 c = die->die_child;
7189 if (c) do {
7190 dw_attr_node *name_attr;
7192 c = c->die_sib;
7193 name_attr = get_AT (c, DW_AT_name);
7194 if (is_template_instantiation (c))
7196 /* Ignore instantiations of member type and function templates. */
7198 else if (name_attr != NULL
7199 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7201 /* Use a shallow checksum for named nested types and member
7202 functions. */
7203 CHECKSUM_ULEB128 ('S');
7204 CHECKSUM_ULEB128 (c->die_tag);
7205 CHECKSUM_STRING (AT_string (name_attr));
7207 else
7209 /* Use a deep checksum for other children. */
7210 /* Mark this DIE so it gets processed when unmarking. */
7211 if (c->die_mark == 0)
7212 c->die_mark = -1;
7213 die_checksum_ordered (c, ctx, mark);
7215 } while (c != die->die_child);
7217 CHECKSUM_ULEB128 (0);
7220 /* Add a type name and tag to a hash. */
7221 static void
7222 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7224 CHECKSUM_ULEB128 (tag);
7225 CHECKSUM_STRING (name);
7228 #undef CHECKSUM
7229 #undef CHECKSUM_STRING
7230 #undef CHECKSUM_ATTR
7231 #undef CHECKSUM_LEB128
7232 #undef CHECKSUM_ULEB128
7234 /* Generate the type signature for DIE. This is computed by generating an
7235 MD5 checksum over the DIE's tag, its relevant attributes, and its
7236 children. Attributes that are references to other DIEs are processed
7237 by recursion, using the MARK field to prevent infinite recursion.
7238 If the DIE is nested inside a namespace or another type, we also
7239 need to include that context in the signature. The lower 64 bits
7240 of the resulting MD5 checksum comprise the signature. */
7242 static void
7243 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7245 int mark;
7246 const char *name;
7247 unsigned char checksum[16];
7248 struct md5_ctx ctx;
7249 dw_die_ref decl;
7250 dw_die_ref parent;
7252 name = get_AT_string (die, DW_AT_name);
7253 decl = get_AT_ref (die, DW_AT_specification);
7254 parent = get_die_parent (die);
7256 /* First, compute a signature for just the type name (and its surrounding
7257 context, if any. This is stored in the type unit DIE for link-time
7258 ODR (one-definition rule) checking. */
7260 if (is_cxx () && name != NULL)
7262 md5_init_ctx (&ctx);
7264 /* Checksum the names of surrounding namespaces and structures. */
7265 if (parent != NULL)
7266 checksum_die_context (parent, &ctx);
7268 /* Checksum the current DIE. */
7269 die_odr_checksum (die->die_tag, name, &ctx);
7270 md5_finish_ctx (&ctx, checksum);
7272 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7275 /* Next, compute the complete type signature. */
7277 md5_init_ctx (&ctx);
7278 mark = 1;
7279 die->die_mark = mark;
7281 /* Checksum the names of surrounding namespaces and structures. */
7282 if (parent != NULL)
7283 checksum_die_context (parent, &ctx);
7285 /* Checksum the DIE and its children. */
7286 die_checksum_ordered (die, &ctx, &mark);
7287 unmark_all_dies (die);
7288 md5_finish_ctx (&ctx, checksum);
7290 /* Store the signature in the type node and link the type DIE and the
7291 type node together. */
7292 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7293 DWARF_TYPE_SIGNATURE_SIZE);
7294 die->comdat_type_p = true;
7295 die->die_id.die_type_node = type_node;
7296 type_node->type_die = die;
7298 /* If the DIE is a specification, link its declaration to the type node
7299 as well. */
7300 if (decl != NULL)
7302 decl->comdat_type_p = true;
7303 decl->die_id.die_type_node = type_node;
7307 /* Do the location expressions look same? */
7308 static inline int
7309 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7311 return loc1->dw_loc_opc == loc2->dw_loc_opc
7312 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7313 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7316 /* Do the values look the same? */
7317 static int
7318 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7320 dw_loc_descr_ref loc1, loc2;
7321 rtx r1, r2;
7323 if (v1->val_class != v2->val_class)
7324 return 0;
7326 switch (v1->val_class)
7328 case dw_val_class_const:
7329 case dw_val_class_const_implicit:
7330 return v1->v.val_int == v2->v.val_int;
7331 case dw_val_class_unsigned_const:
7332 case dw_val_class_unsigned_const_implicit:
7333 return v1->v.val_unsigned == v2->v.val_unsigned;
7334 case dw_val_class_const_double:
7335 return v1->v.val_double.high == v2->v.val_double.high
7336 && v1->v.val_double.low == v2->v.val_double.low;
7337 case dw_val_class_wide_int:
7338 return *v1->v.val_wide == *v2->v.val_wide;
7339 case dw_val_class_vec:
7340 if (v1->v.val_vec.length != v2->v.val_vec.length
7341 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7342 return 0;
7343 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7344 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7345 return 0;
7346 return 1;
7347 case dw_val_class_flag:
7348 return v1->v.val_flag == v2->v.val_flag;
7349 case dw_val_class_str:
7350 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7352 case dw_val_class_addr:
7353 r1 = v1->v.val_addr;
7354 r2 = v2->v.val_addr;
7355 if (GET_CODE (r1) != GET_CODE (r2))
7356 return 0;
7357 return !rtx_equal_p (r1, r2);
7359 case dw_val_class_offset:
7360 return v1->v.val_offset == v2->v.val_offset;
7362 case dw_val_class_loc:
7363 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7364 loc1 && loc2;
7365 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7366 if (!same_loc_p (loc1, loc2, mark))
7367 return 0;
7368 return !loc1 && !loc2;
7370 case dw_val_class_die_ref:
7371 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7373 case dw_val_class_fde_ref:
7374 case dw_val_class_vms_delta:
7375 case dw_val_class_lbl_id:
7376 case dw_val_class_lineptr:
7377 case dw_val_class_macptr:
7378 case dw_val_class_loclistsptr:
7379 case dw_val_class_high_pc:
7380 return 1;
7382 case dw_val_class_file:
7383 case dw_val_class_file_implicit:
7384 return v1->v.val_file == v2->v.val_file;
7386 case dw_val_class_data8:
7387 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7389 default:
7390 return 1;
7394 /* Do the attributes look the same? */
7396 static int
7397 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7399 if (at1->dw_attr != at2->dw_attr)
7400 return 0;
7402 /* We don't care that this was compiled with a different compiler
7403 snapshot; if the output is the same, that's what matters. */
7404 if (at1->dw_attr == DW_AT_producer)
7405 return 1;
7407 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7410 /* Do the dies look the same? */
7412 static int
7413 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7415 dw_die_ref c1, c2;
7416 dw_attr_node *a1;
7417 unsigned ix;
7419 /* To avoid infinite recursion. */
7420 if (die1->die_mark)
7421 return die1->die_mark == die2->die_mark;
7422 die1->die_mark = die2->die_mark = ++(*mark);
7424 if (die1->die_tag != die2->die_tag)
7425 return 0;
7427 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7428 return 0;
7430 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7431 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7432 return 0;
7434 c1 = die1->die_child;
7435 c2 = die2->die_child;
7436 if (! c1)
7438 if (c2)
7439 return 0;
7441 else
7442 for (;;)
7444 if (!same_die_p (c1, c2, mark))
7445 return 0;
7446 c1 = c1->die_sib;
7447 c2 = c2->die_sib;
7448 if (c1 == die1->die_child)
7450 if (c2 == die2->die_child)
7451 break;
7452 else
7453 return 0;
7457 return 1;
7460 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7461 children, and set die_symbol. */
7463 static void
7464 compute_comp_unit_symbol (dw_die_ref unit_die)
7466 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7467 const char *base = die_name ? lbasename (die_name) : "anonymous";
7468 char *name = XALLOCAVEC (char, strlen (base) + 64);
7469 char *p;
7470 int i, mark;
7471 unsigned char checksum[16];
7472 struct md5_ctx ctx;
7474 /* Compute the checksum of the DIE, then append part of it as hex digits to
7475 the name filename of the unit. */
7477 md5_init_ctx (&ctx);
7478 mark = 0;
7479 die_checksum (unit_die, &ctx, &mark);
7480 unmark_all_dies (unit_die);
7481 md5_finish_ctx (&ctx, checksum);
7483 /* When we this for comp_unit_die () we have a DW_AT_name that might
7484 not start with a letter but with anything valid for filenames and
7485 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7486 character is not a letter. */
7487 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7488 clean_symbol_name (name);
7490 p = name + strlen (name);
7491 for (i = 0; i < 4; i++)
7493 sprintf (p, "%.2x", checksum[i]);
7494 p += 2;
7497 unit_die->die_id.die_symbol = xstrdup (name);
7500 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7502 static int
7503 is_type_die (dw_die_ref die)
7505 switch (die->die_tag)
7507 case DW_TAG_array_type:
7508 case DW_TAG_class_type:
7509 case DW_TAG_interface_type:
7510 case DW_TAG_enumeration_type:
7511 case DW_TAG_pointer_type:
7512 case DW_TAG_reference_type:
7513 case DW_TAG_rvalue_reference_type:
7514 case DW_TAG_string_type:
7515 case DW_TAG_structure_type:
7516 case DW_TAG_subroutine_type:
7517 case DW_TAG_union_type:
7518 case DW_TAG_ptr_to_member_type:
7519 case DW_TAG_set_type:
7520 case DW_TAG_subrange_type:
7521 case DW_TAG_base_type:
7522 case DW_TAG_const_type:
7523 case DW_TAG_file_type:
7524 case DW_TAG_packed_type:
7525 case DW_TAG_volatile_type:
7526 case DW_TAG_typedef:
7527 return 1;
7528 default:
7529 return 0;
7533 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7534 Basically, we want to choose the bits that are likely to be shared between
7535 compilations (types) and leave out the bits that are specific to individual
7536 compilations (functions). */
7538 static int
7539 is_comdat_die (dw_die_ref c)
7541 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7542 we do for stabs. The advantage is a greater likelihood of sharing between
7543 objects that don't include headers in the same order (and therefore would
7544 put the base types in a different comdat). jason 8/28/00 */
7546 if (c->die_tag == DW_TAG_base_type)
7547 return 0;
7549 if (c->die_tag == DW_TAG_pointer_type
7550 || c->die_tag == DW_TAG_reference_type
7551 || c->die_tag == DW_TAG_rvalue_reference_type
7552 || c->die_tag == DW_TAG_const_type
7553 || c->die_tag == DW_TAG_volatile_type)
7555 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7557 return t ? is_comdat_die (t) : 0;
7560 return is_type_die (c);
7563 /* Returns true iff C is a compile-unit DIE. */
7565 static inline bool
7566 is_cu_die (dw_die_ref c)
7568 return c && (c->die_tag == DW_TAG_compile_unit
7569 || c->die_tag == DW_TAG_skeleton_unit);
7572 /* Returns true iff C is a unit DIE of some sort. */
7574 static inline bool
7575 is_unit_die (dw_die_ref c)
7577 return c && (c->die_tag == DW_TAG_compile_unit
7578 || c->die_tag == DW_TAG_partial_unit
7579 || c->die_tag == DW_TAG_type_unit
7580 || c->die_tag == DW_TAG_skeleton_unit);
7583 /* Returns true iff C is a namespace DIE. */
7585 static inline bool
7586 is_namespace_die (dw_die_ref c)
7588 return c && c->die_tag == DW_TAG_namespace;
7591 /* Returns true iff C is a class or structure DIE. */
7593 static inline bool
7594 is_class_die (dw_die_ref c)
7596 return c && (c->die_tag == DW_TAG_class_type
7597 || c->die_tag == DW_TAG_structure_type);
7600 /* Return non-zero if this DIE is a template parameter. */
7602 static inline bool
7603 is_template_parameter (dw_die_ref die)
7605 switch (die->die_tag)
7607 case DW_TAG_template_type_param:
7608 case DW_TAG_template_value_param:
7609 case DW_TAG_GNU_template_template_param:
7610 case DW_TAG_GNU_template_parameter_pack:
7611 return true;
7612 default:
7613 return false;
7617 /* Return non-zero if this DIE represents a template instantiation. */
7619 static inline bool
7620 is_template_instantiation (dw_die_ref die)
7622 dw_die_ref c;
7624 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7625 return false;
7626 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7627 return false;
7630 static char *
7631 gen_internal_sym (const char *prefix)
7633 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7635 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7636 return xstrdup (buf);
7639 /* Return non-zero if this DIE is a declaration. */
7641 static int
7642 is_declaration_die (dw_die_ref die)
7644 dw_attr_node *a;
7645 unsigned ix;
7647 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7648 if (a->dw_attr == DW_AT_declaration)
7649 return 1;
7651 return 0;
7654 /* Return non-zero if this DIE is nested inside a subprogram. */
7656 static int
7657 is_nested_in_subprogram (dw_die_ref die)
7659 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7661 if (decl == NULL)
7662 decl = die;
7663 return local_scope_p (decl);
7666 /* Return non-zero if this DIE contains a defining declaration of a
7667 subprogram. */
7669 static int
7670 contains_subprogram_definition (dw_die_ref die)
7672 dw_die_ref c;
7674 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7675 return 1;
7676 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7677 return 0;
7680 /* Return non-zero if this is a type DIE that should be moved to a
7681 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7682 unit type. */
7684 static int
7685 should_move_die_to_comdat (dw_die_ref die)
7687 switch (die->die_tag)
7689 case DW_TAG_class_type:
7690 case DW_TAG_structure_type:
7691 case DW_TAG_enumeration_type:
7692 case DW_TAG_union_type:
7693 /* Don't move declarations, inlined instances, types nested in a
7694 subprogram, or types that contain subprogram definitions. */
7695 if (is_declaration_die (die)
7696 || get_AT (die, DW_AT_abstract_origin)
7697 || is_nested_in_subprogram (die)
7698 || contains_subprogram_definition (die))
7699 return 0;
7700 return 1;
7701 case DW_TAG_array_type:
7702 case DW_TAG_interface_type:
7703 case DW_TAG_pointer_type:
7704 case DW_TAG_reference_type:
7705 case DW_TAG_rvalue_reference_type:
7706 case DW_TAG_string_type:
7707 case DW_TAG_subroutine_type:
7708 case DW_TAG_ptr_to_member_type:
7709 case DW_TAG_set_type:
7710 case DW_TAG_subrange_type:
7711 case DW_TAG_base_type:
7712 case DW_TAG_const_type:
7713 case DW_TAG_file_type:
7714 case DW_TAG_packed_type:
7715 case DW_TAG_volatile_type:
7716 case DW_TAG_typedef:
7717 default:
7718 return 0;
7722 /* Make a clone of DIE. */
7724 static dw_die_ref
7725 clone_die (dw_die_ref die)
7727 dw_die_ref clone = new_die_raw (die->die_tag);
7728 dw_attr_node *a;
7729 unsigned ix;
7731 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7732 add_dwarf_attr (clone, a);
7734 return clone;
7737 /* Make a clone of the tree rooted at DIE. */
7739 static dw_die_ref
7740 clone_tree (dw_die_ref die)
7742 dw_die_ref c;
7743 dw_die_ref clone = clone_die (die);
7745 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7747 return clone;
7750 /* Make a clone of DIE as a declaration. */
7752 static dw_die_ref
7753 clone_as_declaration (dw_die_ref die)
7755 dw_die_ref clone;
7756 dw_die_ref decl;
7757 dw_attr_node *a;
7758 unsigned ix;
7760 /* If the DIE is already a declaration, just clone it. */
7761 if (is_declaration_die (die))
7762 return clone_die (die);
7764 /* If the DIE is a specification, just clone its declaration DIE. */
7765 decl = get_AT_ref (die, DW_AT_specification);
7766 if (decl != NULL)
7768 clone = clone_die (decl);
7769 if (die->comdat_type_p)
7770 add_AT_die_ref (clone, DW_AT_signature, die);
7771 return clone;
7774 clone = new_die_raw (die->die_tag);
7776 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7778 /* We don't want to copy over all attributes.
7779 For example we don't want DW_AT_byte_size because otherwise we will no
7780 longer have a declaration and GDB will treat it as a definition. */
7782 switch (a->dw_attr)
7784 case DW_AT_abstract_origin:
7785 case DW_AT_artificial:
7786 case DW_AT_containing_type:
7787 case DW_AT_external:
7788 case DW_AT_name:
7789 case DW_AT_type:
7790 case DW_AT_virtuality:
7791 case DW_AT_linkage_name:
7792 case DW_AT_MIPS_linkage_name:
7793 add_dwarf_attr (clone, a);
7794 break;
7795 case DW_AT_byte_size:
7796 case DW_AT_alignment:
7797 default:
7798 break;
7802 if (die->comdat_type_p)
7803 add_AT_die_ref (clone, DW_AT_signature, die);
7805 add_AT_flag (clone, DW_AT_declaration, 1);
7806 return clone;
7810 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7812 struct decl_table_entry
7814 dw_die_ref orig;
7815 dw_die_ref copy;
7818 /* Helpers to manipulate hash table of copied declarations. */
7820 /* Hashtable helpers. */
7822 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7824 typedef die_struct *compare_type;
7825 static inline hashval_t hash (const decl_table_entry *);
7826 static inline bool equal (const decl_table_entry *, const die_struct *);
7829 inline hashval_t
7830 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7832 return htab_hash_pointer (entry->orig);
7835 inline bool
7836 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7837 const die_struct *entry2)
7839 return entry1->orig == entry2;
7842 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7844 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7845 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7846 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7847 to check if the ancestor has already been copied into UNIT. */
7849 static dw_die_ref
7850 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7851 decl_hash_type *decl_table)
7853 dw_die_ref parent = die->die_parent;
7854 dw_die_ref new_parent = unit;
7855 dw_die_ref copy;
7856 decl_table_entry **slot = NULL;
7857 struct decl_table_entry *entry = NULL;
7859 if (decl_table)
7861 /* Check if the entry has already been copied to UNIT. */
7862 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7863 INSERT);
7864 if (*slot != HTAB_EMPTY_ENTRY)
7866 entry = *slot;
7867 return entry->copy;
7870 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7871 entry = XCNEW (struct decl_table_entry);
7872 entry->orig = die;
7873 entry->copy = NULL;
7874 *slot = entry;
7877 if (parent != NULL)
7879 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7880 if (spec != NULL)
7881 parent = spec;
7882 if (!is_unit_die (parent))
7883 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7886 copy = clone_as_declaration (die);
7887 add_child_die (new_parent, copy);
7889 if (decl_table)
7891 /* Record the pointer to the copy. */
7892 entry->copy = copy;
7895 return copy;
7897 /* Copy the declaration context to the new type unit DIE. This includes
7898 any surrounding namespace or type declarations. If the DIE has an
7899 AT_specification attribute, it also includes attributes and children
7900 attached to the specification, and returns a pointer to the original
7901 parent of the declaration DIE. Returns NULL otherwise. */
7903 static dw_die_ref
7904 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7906 dw_die_ref decl;
7907 dw_die_ref new_decl;
7908 dw_die_ref orig_parent = NULL;
7910 decl = get_AT_ref (die, DW_AT_specification);
7911 if (decl == NULL)
7912 decl = die;
7913 else
7915 unsigned ix;
7916 dw_die_ref c;
7917 dw_attr_node *a;
7919 /* The original DIE will be changed to a declaration, and must
7920 be moved to be a child of the original declaration DIE. */
7921 orig_parent = decl->die_parent;
7923 /* Copy the type node pointer from the new DIE to the original
7924 declaration DIE so we can forward references later. */
7925 decl->comdat_type_p = true;
7926 decl->die_id.die_type_node = die->die_id.die_type_node;
7928 remove_AT (die, DW_AT_specification);
7930 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7932 if (a->dw_attr != DW_AT_name
7933 && a->dw_attr != DW_AT_declaration
7934 && a->dw_attr != DW_AT_external)
7935 add_dwarf_attr (die, a);
7938 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7941 if (decl->die_parent != NULL
7942 && !is_unit_die (decl->die_parent))
7944 new_decl = copy_ancestor_tree (unit, decl, NULL);
7945 if (new_decl != NULL)
7947 remove_AT (new_decl, DW_AT_signature);
7948 add_AT_specification (die, new_decl);
7952 return orig_parent;
7955 /* Generate the skeleton ancestor tree for the given NODE, then clone
7956 the DIE and add the clone into the tree. */
7958 static void
7959 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7961 if (node->new_die != NULL)
7962 return;
7964 node->new_die = clone_as_declaration (node->old_die);
7966 if (node->parent != NULL)
7968 generate_skeleton_ancestor_tree (node->parent);
7969 add_child_die (node->parent->new_die, node->new_die);
7973 /* Generate a skeleton tree of DIEs containing any declarations that are
7974 found in the original tree. We traverse the tree looking for declaration
7975 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7977 static void
7978 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7980 skeleton_chain_node node;
7981 dw_die_ref c;
7982 dw_die_ref first;
7983 dw_die_ref prev = NULL;
7984 dw_die_ref next = NULL;
7986 node.parent = parent;
7988 first = c = parent->old_die->die_child;
7989 if (c)
7990 next = c->die_sib;
7991 if (c) do {
7992 if (prev == NULL || prev->die_sib == c)
7993 prev = c;
7994 c = next;
7995 next = (c == first ? NULL : c->die_sib);
7996 node.old_die = c;
7997 node.new_die = NULL;
7998 if (is_declaration_die (c))
8000 if (is_template_instantiation (c))
8002 /* Instantiated templates do not need to be cloned into the
8003 type unit. Just move the DIE and its children back to
8004 the skeleton tree (in the main CU). */
8005 remove_child_with_prev (c, prev);
8006 add_child_die (parent->new_die, c);
8007 c = prev;
8009 else if (c->comdat_type_p)
8011 /* This is the skeleton of earlier break_out_comdat_types
8012 type. Clone the existing DIE, but keep the children
8013 under the original (which is in the main CU). */
8014 dw_die_ref clone = clone_die (c);
8016 replace_child (c, clone, prev);
8017 generate_skeleton_ancestor_tree (parent);
8018 add_child_die (parent->new_die, c);
8019 c = clone;
8020 continue;
8022 else
8024 /* Clone the existing DIE, move the original to the skeleton
8025 tree (which is in the main CU), and put the clone, with
8026 all the original's children, where the original came from
8027 (which is about to be moved to the type unit). */
8028 dw_die_ref clone = clone_die (c);
8029 move_all_children (c, clone);
8031 /* If the original has a DW_AT_object_pointer attribute,
8032 it would now point to a child DIE just moved to the
8033 cloned tree, so we need to remove that attribute from
8034 the original. */
8035 remove_AT (c, DW_AT_object_pointer);
8037 replace_child (c, clone, prev);
8038 generate_skeleton_ancestor_tree (parent);
8039 add_child_die (parent->new_die, c);
8040 node.old_die = clone;
8041 node.new_die = c;
8042 c = clone;
8045 generate_skeleton_bottom_up (&node);
8046 } while (next != NULL);
8049 /* Wrapper function for generate_skeleton_bottom_up. */
8051 static dw_die_ref
8052 generate_skeleton (dw_die_ref die)
8054 skeleton_chain_node node;
8056 node.old_die = die;
8057 node.new_die = NULL;
8058 node.parent = NULL;
8060 /* If this type definition is nested inside another type,
8061 and is not an instantiation of a template, always leave
8062 at least a declaration in its place. */
8063 if (die->die_parent != NULL
8064 && is_type_die (die->die_parent)
8065 && !is_template_instantiation (die))
8066 node.new_die = clone_as_declaration (die);
8068 generate_skeleton_bottom_up (&node);
8069 return node.new_die;
8072 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8073 declaration. The original DIE is moved to a new compile unit so that
8074 existing references to it follow it to the new location. If any of the
8075 original DIE's descendants is a declaration, we need to replace the
8076 original DIE with a skeleton tree and move the declarations back into the
8077 skeleton tree. */
8079 static dw_die_ref
8080 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8081 dw_die_ref prev)
8083 dw_die_ref skeleton, orig_parent;
8085 /* Copy the declaration context to the type unit DIE. If the returned
8086 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8087 that DIE. */
8088 orig_parent = copy_declaration_context (unit, child);
8090 skeleton = generate_skeleton (child);
8091 if (skeleton == NULL)
8092 remove_child_with_prev (child, prev);
8093 else
8095 skeleton->comdat_type_p = true;
8096 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8098 /* If the original DIE was a specification, we need to put
8099 the skeleton under the parent DIE of the declaration.
8100 This leaves the original declaration in the tree, but
8101 it will be pruned later since there are no longer any
8102 references to it. */
8103 if (orig_parent != NULL)
8105 remove_child_with_prev (child, prev);
8106 add_child_die (orig_parent, skeleton);
8108 else
8109 replace_child (child, skeleton, prev);
8112 return skeleton;
8115 static void
8116 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8117 comdat_type_node *type_node,
8118 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8120 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8121 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8122 DWARF procedure references in the DW_AT_location attribute. */
8124 static dw_die_ref
8125 copy_dwarf_procedure (dw_die_ref die,
8126 comdat_type_node *type_node,
8127 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8129 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8131 /* DWARF procedures are not supposed to have children... */
8132 gcc_assert (die->die_child == NULL);
8134 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8135 gcc_assert (vec_safe_length (die->die_attr) == 1
8136 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8138 /* Do not copy more than once DWARF procedures. */
8139 bool existed;
8140 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8141 if (existed)
8142 return die_copy;
8144 die_copy = clone_die (die);
8145 add_child_die (type_node->root_die, die_copy);
8146 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8147 return die_copy;
8150 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8151 procedures in DIE's attributes. */
8153 static void
8154 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8155 comdat_type_node *type_node,
8156 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8158 dw_attr_node *a;
8159 unsigned i;
8161 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8163 dw_loc_descr_ref loc;
8165 if (a->dw_attr_val.val_class != dw_val_class_loc)
8166 continue;
8168 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8170 switch (loc->dw_loc_opc)
8172 case DW_OP_call2:
8173 case DW_OP_call4:
8174 case DW_OP_call_ref:
8175 gcc_assert (loc->dw_loc_oprnd1.val_class
8176 == dw_val_class_die_ref);
8177 loc->dw_loc_oprnd1.v.val_die_ref.die
8178 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8179 type_node,
8180 copied_dwarf_procs);
8182 default:
8183 break;
8189 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8190 rewrite references to point to the copies.
8192 References are looked for in DIE's attributes and recursively in all its
8193 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8194 mapping from old DWARF procedures to their copy. It is used not to copy
8195 twice the same DWARF procedure under TYPE_NODE. */
8197 static void
8198 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8199 comdat_type_node *type_node,
8200 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8202 dw_die_ref c;
8204 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8205 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8206 type_node,
8207 copied_dwarf_procs));
8210 /* Traverse the DIE and set up additional .debug_types or .debug_info
8211 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8212 section. */
8214 static void
8215 break_out_comdat_types (dw_die_ref die)
8217 dw_die_ref c;
8218 dw_die_ref first;
8219 dw_die_ref prev = NULL;
8220 dw_die_ref next = NULL;
8221 dw_die_ref unit = NULL;
8223 first = c = die->die_child;
8224 if (c)
8225 next = c->die_sib;
8226 if (c) do {
8227 if (prev == NULL || prev->die_sib == c)
8228 prev = c;
8229 c = next;
8230 next = (c == first ? NULL : c->die_sib);
8231 if (should_move_die_to_comdat (c))
8233 dw_die_ref replacement;
8234 comdat_type_node *type_node;
8236 /* Break out nested types into their own type units. */
8237 break_out_comdat_types (c);
8239 /* Create a new type unit DIE as the root for the new tree, and
8240 add it to the list of comdat types. */
8241 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8242 add_AT_unsigned (unit, DW_AT_language,
8243 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8244 type_node = ggc_cleared_alloc<comdat_type_node> ();
8245 type_node->root_die = unit;
8246 type_node->next = comdat_type_list;
8247 comdat_type_list = type_node;
8249 /* Generate the type signature. */
8250 generate_type_signature (c, type_node);
8252 /* Copy the declaration context, attributes, and children of the
8253 declaration into the new type unit DIE, then remove this DIE
8254 from the main CU (or replace it with a skeleton if necessary). */
8255 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8256 type_node->skeleton_die = replacement;
8258 /* Add the DIE to the new compunit. */
8259 add_child_die (unit, c);
8261 /* Types can reference DWARF procedures for type size or data location
8262 expressions. Calls in DWARF expressions cannot target procedures
8263 that are not in the same section. So we must copy DWARF procedures
8264 along with this type and then rewrite references to them. */
8265 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8266 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8268 if (replacement != NULL)
8269 c = replacement;
8271 else if (c->die_tag == DW_TAG_namespace
8272 || c->die_tag == DW_TAG_class_type
8273 || c->die_tag == DW_TAG_structure_type
8274 || c->die_tag == DW_TAG_union_type)
8276 /* Look for nested types that can be broken out. */
8277 break_out_comdat_types (c);
8279 } while (next != NULL);
8282 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8283 Enter all the cloned children into the hash table decl_table. */
8285 static dw_die_ref
8286 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8288 dw_die_ref c;
8289 dw_die_ref clone;
8290 struct decl_table_entry *entry;
8291 decl_table_entry **slot;
8293 if (die->die_tag == DW_TAG_subprogram)
8294 clone = clone_as_declaration (die);
8295 else
8296 clone = clone_die (die);
8298 slot = decl_table->find_slot_with_hash (die,
8299 htab_hash_pointer (die), INSERT);
8301 /* Assert that DIE isn't in the hash table yet. If it would be there
8302 before, the ancestors would be necessarily there as well, therefore
8303 clone_tree_partial wouldn't be called. */
8304 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8306 entry = XCNEW (struct decl_table_entry);
8307 entry->orig = die;
8308 entry->copy = clone;
8309 *slot = entry;
8311 if (die->die_tag != DW_TAG_subprogram)
8312 FOR_EACH_CHILD (die, c,
8313 add_child_die (clone, clone_tree_partial (c, decl_table)));
8315 return clone;
8318 /* Walk the DIE and its children, looking for references to incomplete
8319 or trivial types that are unmarked (i.e., that are not in the current
8320 type_unit). */
8322 static void
8323 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8325 dw_die_ref c;
8326 dw_attr_node *a;
8327 unsigned ix;
8329 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8331 if (AT_class (a) == dw_val_class_die_ref)
8333 dw_die_ref targ = AT_ref (a);
8334 decl_table_entry **slot;
8335 struct decl_table_entry *entry;
8337 if (targ->die_mark != 0 || targ->comdat_type_p)
8338 continue;
8340 slot = decl_table->find_slot_with_hash (targ,
8341 htab_hash_pointer (targ),
8342 INSERT);
8344 if (*slot != HTAB_EMPTY_ENTRY)
8346 /* TARG has already been copied, so we just need to
8347 modify the reference to point to the copy. */
8348 entry = *slot;
8349 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8351 else
8353 dw_die_ref parent = unit;
8354 dw_die_ref copy = clone_die (targ);
8356 /* Record in DECL_TABLE that TARG has been copied.
8357 Need to do this now, before the recursive call,
8358 because DECL_TABLE may be expanded and SLOT
8359 would no longer be a valid pointer. */
8360 entry = XCNEW (struct decl_table_entry);
8361 entry->orig = targ;
8362 entry->copy = copy;
8363 *slot = entry;
8365 /* If TARG is not a declaration DIE, we need to copy its
8366 children. */
8367 if (!is_declaration_die (targ))
8369 FOR_EACH_CHILD (
8370 targ, c,
8371 add_child_die (copy,
8372 clone_tree_partial (c, decl_table)));
8375 /* Make sure the cloned tree is marked as part of the
8376 type unit. */
8377 mark_dies (copy);
8379 /* If TARG has surrounding context, copy its ancestor tree
8380 into the new type unit. */
8381 if (targ->die_parent != NULL
8382 && !is_unit_die (targ->die_parent))
8383 parent = copy_ancestor_tree (unit, targ->die_parent,
8384 decl_table);
8386 add_child_die (parent, copy);
8387 a->dw_attr_val.v.val_die_ref.die = copy;
8389 /* Make sure the newly-copied DIE is walked. If it was
8390 installed in a previously-added context, it won't
8391 get visited otherwise. */
8392 if (parent != unit)
8394 /* Find the highest point of the newly-added tree,
8395 mark each node along the way, and walk from there. */
8396 parent->die_mark = 1;
8397 while (parent->die_parent
8398 && parent->die_parent->die_mark == 0)
8400 parent = parent->die_parent;
8401 parent->die_mark = 1;
8403 copy_decls_walk (unit, parent, decl_table);
8409 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8412 /* Copy declarations for "unworthy" types into the new comdat section.
8413 Incomplete types, modified types, and certain other types aren't broken
8414 out into comdat sections of their own, so they don't have a signature,
8415 and we need to copy the declaration into the same section so that we
8416 don't have an external reference. */
8418 static void
8419 copy_decls_for_unworthy_types (dw_die_ref unit)
8421 mark_dies (unit);
8422 decl_hash_type decl_table (10);
8423 copy_decls_walk (unit, unit, &decl_table);
8424 unmark_dies (unit);
8427 /* Traverse the DIE and add a sibling attribute if it may have the
8428 effect of speeding up access to siblings. To save some space,
8429 avoid generating sibling attributes for DIE's without children. */
8431 static void
8432 add_sibling_attributes (dw_die_ref die)
8434 dw_die_ref c;
8436 if (! die->die_child)
8437 return;
8439 if (die->die_parent && die != die->die_parent->die_child)
8440 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8442 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8445 /* Output all location lists for the DIE and its children. */
8447 static void
8448 output_location_lists (dw_die_ref die)
8450 dw_die_ref c;
8451 dw_attr_node *a;
8452 unsigned ix;
8454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8455 if (AT_class (a) == dw_val_class_loc_list)
8456 output_loc_list (AT_loc_list (a));
8458 FOR_EACH_CHILD (die, c, output_location_lists (c));
8461 /* During assign_location_list_indexes and output_loclists_offset the
8462 current index, after it the number of assigned indexes (i.e. how
8463 large the .debug_loclists* offset table should be). */
8464 static unsigned int loc_list_idx;
8466 /* Output all location list offsets for the DIE and its children. */
8468 static void
8469 output_loclists_offsets (dw_die_ref die)
8471 dw_die_ref c;
8472 dw_attr_node *a;
8473 unsigned ix;
8475 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8476 if (AT_class (a) == dw_val_class_loc_list)
8478 dw_loc_list_ref l = AT_loc_list (a);
8479 if (l->offset_emitted)
8480 continue;
8481 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8482 loc_section_label, NULL);
8483 gcc_assert (l->hash == loc_list_idx);
8484 loc_list_idx++;
8485 l->offset_emitted = true;
8488 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8491 /* Recursively set indexes of location lists. */
8493 static void
8494 assign_location_list_indexes (dw_die_ref die)
8496 dw_die_ref c;
8497 dw_attr_node *a;
8498 unsigned ix;
8500 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8501 if (AT_class (a) == dw_val_class_loc_list)
8503 dw_loc_list_ref list = AT_loc_list (a);
8504 if (!list->num_assigned)
8506 list->num_assigned = true;
8507 list->hash = loc_list_idx++;
8511 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8514 /* We want to limit the number of external references, because they are
8515 larger than local references: a relocation takes multiple words, and
8516 even a sig8 reference is always eight bytes, whereas a local reference
8517 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8518 So if we encounter multiple external references to the same type DIE, we
8519 make a local typedef stub for it and redirect all references there.
8521 This is the element of the hash table for keeping track of these
8522 references. */
8524 struct external_ref
8526 dw_die_ref type;
8527 dw_die_ref stub;
8528 unsigned n_refs;
8531 /* Hashtable helpers. */
8533 struct external_ref_hasher : free_ptr_hash <external_ref>
8535 static inline hashval_t hash (const external_ref *);
8536 static inline bool equal (const external_ref *, const external_ref *);
8539 inline hashval_t
8540 external_ref_hasher::hash (const external_ref *r)
8542 dw_die_ref die = r->type;
8543 hashval_t h = 0;
8545 /* We can't use the address of the DIE for hashing, because
8546 that will make the order of the stub DIEs non-deterministic. */
8547 if (! die->comdat_type_p)
8548 /* We have a symbol; use it to compute a hash. */
8549 h = htab_hash_string (die->die_id.die_symbol);
8550 else
8552 /* We have a type signature; use a subset of the bits as the hash.
8553 The 8-byte signature is at least as large as hashval_t. */
8554 comdat_type_node *type_node = die->die_id.die_type_node;
8555 memcpy (&h, type_node->signature, sizeof (h));
8557 return h;
8560 inline bool
8561 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8563 return r1->type == r2->type;
8566 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8568 /* Return a pointer to the external_ref for references to DIE. */
8570 static struct external_ref *
8571 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8573 struct external_ref ref, *ref_p;
8574 external_ref **slot;
8576 ref.type = die;
8577 slot = map->find_slot (&ref, INSERT);
8578 if (*slot != HTAB_EMPTY_ENTRY)
8579 return *slot;
8581 ref_p = XCNEW (struct external_ref);
8582 ref_p->type = die;
8583 *slot = ref_p;
8584 return ref_p;
8587 /* Subroutine of optimize_external_refs, below.
8589 If we see a type skeleton, record it as our stub. If we see external
8590 references, remember how many we've seen. */
8592 static void
8593 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8595 dw_die_ref c;
8596 dw_attr_node *a;
8597 unsigned ix;
8598 struct external_ref *ref_p;
8600 if (is_type_die (die)
8601 && (c = get_AT_ref (die, DW_AT_signature)))
8603 /* This is a local skeleton; use it for local references. */
8604 ref_p = lookup_external_ref (map, c);
8605 ref_p->stub = die;
8608 /* Scan the DIE references, and remember any that refer to DIEs from
8609 other CUs (i.e. those which are not marked). */
8610 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8611 if (AT_class (a) == dw_val_class_die_ref
8612 && (c = AT_ref (a))->die_mark == 0
8613 && is_type_die (c))
8615 ref_p = lookup_external_ref (map, c);
8616 ref_p->n_refs++;
8619 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8622 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8623 points to an external_ref, DATA is the CU we're processing. If we don't
8624 already have a local stub, and we have multiple refs, build a stub. */
8627 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8629 struct external_ref *ref_p = *slot;
8631 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8633 /* We have multiple references to this type, so build a small stub.
8634 Both of these forms are a bit dodgy from the perspective of the
8635 DWARF standard, since technically they should have names. */
8636 dw_die_ref cu = data;
8637 dw_die_ref type = ref_p->type;
8638 dw_die_ref stub = NULL;
8640 if (type->comdat_type_p)
8642 /* If we refer to this type via sig8, use AT_signature. */
8643 stub = new_die (type->die_tag, cu, NULL_TREE);
8644 add_AT_die_ref (stub, DW_AT_signature, type);
8646 else
8648 /* Otherwise, use a typedef with no name. */
8649 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8650 add_AT_die_ref (stub, DW_AT_type, type);
8653 stub->die_mark++;
8654 ref_p->stub = stub;
8656 return 1;
8659 /* DIE is a unit; look through all the DIE references to see if there are
8660 any external references to types, and if so, create local stubs for
8661 them which will be applied in build_abbrev_table. This is useful because
8662 references to local DIEs are smaller. */
8664 static external_ref_hash_type *
8665 optimize_external_refs (dw_die_ref die)
8667 external_ref_hash_type *map = new external_ref_hash_type (10);
8668 optimize_external_refs_1 (die, map);
8669 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8670 return map;
8673 /* The following 3 variables are temporaries that are computed only during the
8674 build_abbrev_table call and used and released during the following
8675 optimize_abbrev_table call. */
8677 /* First abbrev_id that can be optimized based on usage. */
8678 static unsigned int abbrev_opt_start;
8680 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8681 abbrev_id smaller than this, because they must be already sized
8682 during build_abbrev_table). */
8683 static unsigned int abbrev_opt_base_type_end;
8685 /* Vector of usage counts during build_abbrev_table. Indexed by
8686 abbrev_id - abbrev_opt_start. */
8687 static vec<unsigned int> abbrev_usage_count;
8689 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8690 static vec<dw_die_ref> sorted_abbrev_dies;
8692 /* The format of each DIE (and its attribute value pairs) is encoded in an
8693 abbreviation table. This routine builds the abbreviation table and assigns
8694 a unique abbreviation id for each abbreviation entry. The children of each
8695 die are visited recursively. */
8697 static void
8698 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8700 unsigned int abbrev_id = 0;
8701 dw_die_ref c;
8702 dw_attr_node *a;
8703 unsigned ix;
8704 dw_die_ref abbrev;
8706 /* Scan the DIE references, and replace any that refer to
8707 DIEs from other CUs (i.e. those which are not marked) with
8708 the local stubs we built in optimize_external_refs. */
8709 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8710 if (AT_class (a) == dw_val_class_die_ref
8711 && (c = AT_ref (a))->die_mark == 0)
8713 struct external_ref *ref_p;
8714 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8716 ref_p = lookup_external_ref (extern_map, c);
8717 if (ref_p->stub && ref_p->stub != die)
8718 change_AT_die_ref (a, ref_p->stub);
8719 else
8720 /* We aren't changing this reference, so mark it external. */
8721 set_AT_ref_external (a, 1);
8724 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8726 dw_attr_node *die_a, *abbrev_a;
8727 unsigned ix;
8728 bool ok = true;
8730 if (abbrev_id == 0)
8731 continue;
8732 if (abbrev->die_tag != die->die_tag)
8733 continue;
8734 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8735 continue;
8737 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8738 continue;
8740 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8742 abbrev_a = &(*abbrev->die_attr)[ix];
8743 if ((abbrev_a->dw_attr != die_a->dw_attr)
8744 || (value_format (abbrev_a) != value_format (die_a)))
8746 ok = false;
8747 break;
8750 if (ok)
8751 break;
8754 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8756 vec_safe_push (abbrev_die_table, die);
8757 if (abbrev_opt_start)
8758 abbrev_usage_count.safe_push (0);
8760 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8762 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8763 sorted_abbrev_dies.safe_push (die);
8766 die->die_abbrev = abbrev_id;
8767 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8770 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8771 by die_abbrev's usage count, from the most commonly used
8772 abbreviation to the least. */
8774 static int
8775 die_abbrev_cmp (const void *p1, const void *p2)
8777 dw_die_ref die1 = *(const dw_die_ref *) p1;
8778 dw_die_ref die2 = *(const dw_die_ref *) p2;
8780 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8781 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8783 if (die1->die_abbrev >= abbrev_opt_base_type_end
8784 && die2->die_abbrev >= abbrev_opt_base_type_end)
8786 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8787 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8788 return -1;
8789 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8790 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8791 return 1;
8794 /* Stabilize the sort. */
8795 if (die1->die_abbrev < die2->die_abbrev)
8796 return -1;
8797 if (die1->die_abbrev > die2->die_abbrev)
8798 return 1;
8800 return 0;
8803 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8804 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8805 into dw_val_class_const_implicit or
8806 dw_val_class_unsigned_const_implicit. */
8808 static void
8809 optimize_implicit_const (unsigned int first_id, unsigned int end,
8810 vec<bool> &implicit_consts)
8812 /* It never makes sense if there is just one DIE using the abbreviation. */
8813 if (end < first_id + 2)
8814 return;
8816 dw_attr_node *a;
8817 unsigned ix, i;
8818 dw_die_ref die = sorted_abbrev_dies[first_id];
8819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8820 if (implicit_consts[ix])
8822 enum dw_val_class new_class = dw_val_class_none;
8823 switch (AT_class (a))
8825 case dw_val_class_unsigned_const:
8826 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8827 continue;
8829 /* The .debug_abbrev section will grow by
8830 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8831 in all the DIEs using that abbreviation. */
8832 if (constant_size (AT_unsigned (a)) * (end - first_id)
8833 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8834 continue;
8836 new_class = dw_val_class_unsigned_const_implicit;
8837 break;
8839 case dw_val_class_const:
8840 new_class = dw_val_class_const_implicit;
8841 break;
8843 case dw_val_class_file:
8844 new_class = dw_val_class_file_implicit;
8845 break;
8847 default:
8848 continue;
8850 for (i = first_id; i < end; i++)
8851 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8852 = new_class;
8856 /* Attempt to optimize abbreviation table from abbrev_opt_start
8857 abbreviation above. */
8859 static void
8860 optimize_abbrev_table (void)
8862 if (abbrev_opt_start
8863 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8864 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8866 auto_vec<bool, 32> implicit_consts;
8867 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8869 unsigned int abbrev_id = abbrev_opt_start - 1;
8870 unsigned int first_id = ~0U;
8871 unsigned int last_abbrev_id = 0;
8872 unsigned int i;
8873 dw_die_ref die;
8874 if (abbrev_opt_base_type_end > abbrev_opt_start)
8875 abbrev_id = abbrev_opt_base_type_end - 1;
8876 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8877 most commonly used abbreviations come first. */
8878 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8880 dw_attr_node *a;
8881 unsigned ix;
8883 /* If calc_base_type_die_sizes has been called, the CU and
8884 base types after it can't be optimized, because we've already
8885 calculated their DIE offsets. We've sorted them first. */
8886 if (die->die_abbrev < abbrev_opt_base_type_end)
8887 continue;
8888 if (die->die_abbrev != last_abbrev_id)
8890 last_abbrev_id = die->die_abbrev;
8891 if (dwarf_version >= 5 && first_id != ~0U)
8892 optimize_implicit_const (first_id, i, implicit_consts);
8893 abbrev_id++;
8894 (*abbrev_die_table)[abbrev_id] = die;
8895 if (dwarf_version >= 5)
8897 first_id = i;
8898 implicit_consts.truncate (0);
8900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8901 switch (AT_class (a))
8903 case dw_val_class_const:
8904 case dw_val_class_unsigned_const:
8905 case dw_val_class_file:
8906 implicit_consts.safe_push (true);
8907 break;
8908 default:
8909 implicit_consts.safe_push (false);
8910 break;
8914 else if (dwarf_version >= 5)
8916 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8917 if (!implicit_consts[ix])
8918 continue;
8919 else
8921 dw_attr_node *other_a
8922 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8923 if (!dw_val_equal_p (&a->dw_attr_val,
8924 &other_a->dw_attr_val))
8925 implicit_consts[ix] = false;
8928 die->die_abbrev = abbrev_id;
8930 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8931 if (dwarf_version >= 5 && first_id != ~0U)
8932 optimize_implicit_const (first_id, i, implicit_consts);
8935 abbrev_opt_start = 0;
8936 abbrev_opt_base_type_end = 0;
8937 abbrev_usage_count.release ();
8938 sorted_abbrev_dies.release ();
8941 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8943 static int
8944 constant_size (unsigned HOST_WIDE_INT value)
8946 int log;
8948 if (value == 0)
8949 log = 0;
8950 else
8951 log = floor_log2 (value);
8953 log = log / 8;
8954 log = 1 << (floor_log2 (log) + 1);
8956 return log;
8959 /* Return the size of a DIE as it is represented in the
8960 .debug_info section. */
8962 static unsigned long
8963 size_of_die (dw_die_ref die)
8965 unsigned long size = 0;
8966 dw_attr_node *a;
8967 unsigned ix;
8968 enum dwarf_form form;
8970 size += size_of_uleb128 (die->die_abbrev);
8971 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8973 switch (AT_class (a))
8975 case dw_val_class_addr:
8976 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8978 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8979 size += size_of_uleb128 (AT_index (a));
8981 else
8982 size += DWARF2_ADDR_SIZE;
8983 break;
8984 case dw_val_class_offset:
8985 size += DWARF_OFFSET_SIZE;
8986 break;
8987 case dw_val_class_loc:
8989 unsigned long lsize = size_of_locs (AT_loc (a));
8991 /* Block length. */
8992 if (dwarf_version >= 4)
8993 size += size_of_uleb128 (lsize);
8994 else
8995 size += constant_size (lsize);
8996 size += lsize;
8998 break;
8999 case dw_val_class_loc_list:
9000 if (dwarf_split_debug_info && dwarf_version >= 5)
9002 gcc_assert (AT_loc_list (a)->num_assigned);
9003 size += size_of_uleb128 (AT_loc_list (a)->hash);
9005 else
9006 size += DWARF_OFFSET_SIZE;
9007 break;
9008 case dw_val_class_range_list:
9009 if (value_format (a) == DW_FORM_rnglistx)
9011 gcc_assert (rnglist_idx);
9012 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9013 size += size_of_uleb128 (r->idx);
9015 else
9016 size += DWARF_OFFSET_SIZE;
9017 break;
9018 case dw_val_class_const:
9019 size += size_of_sleb128 (AT_int (a));
9020 break;
9021 case dw_val_class_unsigned_const:
9023 int csize = constant_size (AT_unsigned (a));
9024 if (dwarf_version == 3
9025 && a->dw_attr == DW_AT_data_member_location
9026 && csize >= 4)
9027 size += size_of_uleb128 (AT_unsigned (a));
9028 else
9029 size += csize;
9031 break;
9032 case dw_val_class_const_implicit:
9033 case dw_val_class_unsigned_const_implicit:
9034 case dw_val_class_file_implicit:
9035 /* These occupy no size in the DIE, just an extra sleb128 in
9036 .debug_abbrev. */
9037 break;
9038 case dw_val_class_const_double:
9039 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9040 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9041 size++; /* block */
9042 break;
9043 case dw_val_class_wide_int:
9044 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9045 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9046 if (get_full_len (*a->dw_attr_val.v.val_wide)
9047 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9048 size++; /* block */
9049 break;
9050 case dw_val_class_vec:
9051 size += constant_size (a->dw_attr_val.v.val_vec.length
9052 * a->dw_attr_val.v.val_vec.elt_size)
9053 + a->dw_attr_val.v.val_vec.length
9054 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9055 break;
9056 case dw_val_class_flag:
9057 if (dwarf_version >= 4)
9058 /* Currently all add_AT_flag calls pass in 1 as last argument,
9059 so DW_FORM_flag_present can be used. If that ever changes,
9060 we'll need to use DW_FORM_flag and have some optimization
9061 in build_abbrev_table that will change those to
9062 DW_FORM_flag_present if it is set to 1 in all DIEs using
9063 the same abbrev entry. */
9064 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9065 else
9066 size += 1;
9067 break;
9068 case dw_val_class_die_ref:
9069 if (AT_ref_external (a))
9071 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9072 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9073 is sized by target address length, whereas in DWARF3
9074 it's always sized as an offset. */
9075 if (use_debug_types)
9076 size += DWARF_TYPE_SIGNATURE_SIZE;
9077 else if (dwarf_version == 2)
9078 size += DWARF2_ADDR_SIZE;
9079 else
9080 size += DWARF_OFFSET_SIZE;
9082 else
9083 size += DWARF_OFFSET_SIZE;
9084 break;
9085 case dw_val_class_fde_ref:
9086 size += DWARF_OFFSET_SIZE;
9087 break;
9088 case dw_val_class_lbl_id:
9089 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9091 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9092 size += size_of_uleb128 (AT_index (a));
9094 else
9095 size += DWARF2_ADDR_SIZE;
9096 break;
9097 case dw_val_class_lineptr:
9098 case dw_val_class_macptr:
9099 case dw_val_class_loclistsptr:
9100 size += DWARF_OFFSET_SIZE;
9101 break;
9102 case dw_val_class_str:
9103 form = AT_string_form (a);
9104 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9105 size += DWARF_OFFSET_SIZE;
9106 else if (form == DW_FORM_GNU_str_index)
9107 size += size_of_uleb128 (AT_index (a));
9108 else
9109 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9110 break;
9111 case dw_val_class_file:
9112 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9113 break;
9114 case dw_val_class_data8:
9115 size += 8;
9116 break;
9117 case dw_val_class_vms_delta:
9118 size += DWARF_OFFSET_SIZE;
9119 break;
9120 case dw_val_class_high_pc:
9121 size += DWARF2_ADDR_SIZE;
9122 break;
9123 case dw_val_class_discr_value:
9124 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9125 break;
9126 case dw_val_class_discr_list:
9128 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9130 /* This is a block, so we have the block length and then its
9131 data. */
9132 size += constant_size (block_size) + block_size;
9134 break;
9135 default:
9136 gcc_unreachable ();
9140 return size;
9143 /* Size the debugging information associated with a given DIE. Visits the
9144 DIE's children recursively. Updates the global variable next_die_offset, on
9145 each time through. Uses the current value of next_die_offset to update the
9146 die_offset field in each DIE. */
9148 static void
9149 calc_die_sizes (dw_die_ref die)
9151 dw_die_ref c;
9153 gcc_assert (die->die_offset == 0
9154 || (unsigned long int) die->die_offset == next_die_offset);
9155 die->die_offset = next_die_offset;
9156 next_die_offset += size_of_die (die);
9158 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9160 if (die->die_child != NULL)
9161 /* Count the null byte used to terminate sibling lists. */
9162 next_die_offset += 1;
9165 /* Size just the base type children at the start of the CU.
9166 This is needed because build_abbrev needs to size locs
9167 and sizing of type based stack ops needs to know die_offset
9168 values for the base types. */
9170 static void
9171 calc_base_type_die_sizes (void)
9173 unsigned long die_offset = (dwarf_split_debug_info
9174 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9175 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9176 unsigned int i;
9177 dw_die_ref base_type;
9178 #if ENABLE_ASSERT_CHECKING
9179 dw_die_ref prev = comp_unit_die ()->die_child;
9180 #endif
9182 die_offset += size_of_die (comp_unit_die ());
9183 for (i = 0; base_types.iterate (i, &base_type); i++)
9185 #if ENABLE_ASSERT_CHECKING
9186 gcc_assert (base_type->die_offset == 0
9187 && prev->die_sib == base_type
9188 && base_type->die_child == NULL
9189 && base_type->die_abbrev);
9190 prev = base_type;
9191 #endif
9192 if (abbrev_opt_start
9193 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9194 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9195 base_type->die_offset = die_offset;
9196 die_offset += size_of_die (base_type);
9200 /* Set the marks for a die and its children. We do this so
9201 that we know whether or not a reference needs to use FORM_ref_addr; only
9202 DIEs in the same CU will be marked. We used to clear out the offset
9203 and use that as the flag, but ran into ordering problems. */
9205 static void
9206 mark_dies (dw_die_ref die)
9208 dw_die_ref c;
9210 gcc_assert (!die->die_mark);
9212 die->die_mark = 1;
9213 FOR_EACH_CHILD (die, c, mark_dies (c));
9216 /* Clear the marks for a die and its children. */
9218 static void
9219 unmark_dies (dw_die_ref die)
9221 dw_die_ref c;
9223 if (! use_debug_types)
9224 gcc_assert (die->die_mark);
9226 die->die_mark = 0;
9227 FOR_EACH_CHILD (die, c, unmark_dies (c));
9230 /* Clear the marks for a die, its children and referred dies. */
9232 static void
9233 unmark_all_dies (dw_die_ref die)
9235 dw_die_ref c;
9236 dw_attr_node *a;
9237 unsigned ix;
9239 if (!die->die_mark)
9240 return;
9241 die->die_mark = 0;
9243 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9245 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9246 if (AT_class (a) == dw_val_class_die_ref)
9247 unmark_all_dies (AT_ref (a));
9250 /* Calculate if the entry should appear in the final output file. It may be
9251 from a pruned a type. */
9253 static bool
9254 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9256 /* By limiting gnu pubnames to definitions only, gold can generate a
9257 gdb index without entries for declarations, which don't include
9258 enough information to be useful. */
9259 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9260 return false;
9262 if (table == pubname_table)
9264 /* Enumerator names are part of the pubname table, but the
9265 parent DW_TAG_enumeration_type die may have been pruned.
9266 Don't output them if that is the case. */
9267 if (p->die->die_tag == DW_TAG_enumerator &&
9268 (p->die->die_parent == NULL
9269 || !p->die->die_parent->die_perennial_p))
9270 return false;
9272 /* Everything else in the pubname table is included. */
9273 return true;
9276 /* The pubtypes table shouldn't include types that have been
9277 pruned. */
9278 return (p->die->die_offset != 0
9279 || !flag_eliminate_unused_debug_types);
9282 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9283 generated for the compilation unit. */
9285 static unsigned long
9286 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9288 unsigned long size;
9289 unsigned i;
9290 pubname_entry *p;
9291 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9293 size = DWARF_PUBNAMES_HEADER_SIZE;
9294 FOR_EACH_VEC_ELT (*names, i, p)
9295 if (include_pubname_in_output (names, p))
9296 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9298 size += DWARF_OFFSET_SIZE;
9299 return size;
9302 /* Return the size of the information in the .debug_aranges section. */
9304 static unsigned long
9305 size_of_aranges (void)
9307 unsigned long size;
9309 size = DWARF_ARANGES_HEADER_SIZE;
9311 /* Count the address/length pair for this compilation unit. */
9312 if (text_section_used)
9313 size += 2 * DWARF2_ADDR_SIZE;
9314 if (cold_text_section_used)
9315 size += 2 * DWARF2_ADDR_SIZE;
9316 if (have_multiple_function_sections)
9318 unsigned fde_idx;
9319 dw_fde_ref fde;
9321 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9323 if (DECL_IGNORED_P (fde->decl))
9324 continue;
9325 if (!fde->in_std_section)
9326 size += 2 * DWARF2_ADDR_SIZE;
9327 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9328 size += 2 * DWARF2_ADDR_SIZE;
9332 /* Count the two zero words used to terminated the address range table. */
9333 size += 2 * DWARF2_ADDR_SIZE;
9334 return size;
9337 /* Select the encoding of an attribute value. */
9339 static enum dwarf_form
9340 value_format (dw_attr_node *a)
9342 switch (AT_class (a))
9344 case dw_val_class_addr:
9345 /* Only very few attributes allow DW_FORM_addr. */
9346 switch (a->dw_attr)
9348 case DW_AT_low_pc:
9349 case DW_AT_high_pc:
9350 case DW_AT_entry_pc:
9351 case DW_AT_trampoline:
9352 return (AT_index (a) == NOT_INDEXED
9353 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9354 default:
9355 break;
9357 switch (DWARF2_ADDR_SIZE)
9359 case 1:
9360 return DW_FORM_data1;
9361 case 2:
9362 return DW_FORM_data2;
9363 case 4:
9364 return DW_FORM_data4;
9365 case 8:
9366 return DW_FORM_data8;
9367 default:
9368 gcc_unreachable ();
9370 case dw_val_class_loc_list:
9371 if (dwarf_split_debug_info
9372 && dwarf_version >= 5
9373 && AT_loc_list (a)->num_assigned)
9374 return DW_FORM_loclistx;
9375 /* FALLTHRU */
9376 case dw_val_class_range_list:
9377 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9378 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9379 care about sizes of .debug* sections in shared libraries and
9380 executables and don't take into account relocations that affect just
9381 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9382 table in the .debug_rnglists section. */
9383 if (dwarf_split_debug_info
9384 && dwarf_version >= 5
9385 && AT_class (a) == dw_val_class_range_list
9386 && rnglist_idx
9387 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9388 return DW_FORM_rnglistx;
9389 if (dwarf_version >= 4)
9390 return DW_FORM_sec_offset;
9391 /* FALLTHRU */
9392 case dw_val_class_vms_delta:
9393 case dw_val_class_offset:
9394 switch (DWARF_OFFSET_SIZE)
9396 case 4:
9397 return DW_FORM_data4;
9398 case 8:
9399 return DW_FORM_data8;
9400 default:
9401 gcc_unreachable ();
9403 case dw_val_class_loc:
9404 if (dwarf_version >= 4)
9405 return DW_FORM_exprloc;
9406 switch (constant_size (size_of_locs (AT_loc (a))))
9408 case 1:
9409 return DW_FORM_block1;
9410 case 2:
9411 return DW_FORM_block2;
9412 case 4:
9413 return DW_FORM_block4;
9414 default:
9415 gcc_unreachable ();
9417 case dw_val_class_const:
9418 return DW_FORM_sdata;
9419 case dw_val_class_unsigned_const:
9420 switch (constant_size (AT_unsigned (a)))
9422 case 1:
9423 return DW_FORM_data1;
9424 case 2:
9425 return DW_FORM_data2;
9426 case 4:
9427 /* In DWARF3 DW_AT_data_member_location with
9428 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9429 constant, so we need to use DW_FORM_udata if we need
9430 a large constant. */
9431 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9432 return DW_FORM_udata;
9433 return DW_FORM_data4;
9434 case 8:
9435 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9436 return DW_FORM_udata;
9437 return DW_FORM_data8;
9438 default:
9439 gcc_unreachable ();
9441 case dw_val_class_const_implicit:
9442 case dw_val_class_unsigned_const_implicit:
9443 case dw_val_class_file_implicit:
9444 return DW_FORM_implicit_const;
9445 case dw_val_class_const_double:
9446 switch (HOST_BITS_PER_WIDE_INT)
9448 case 8:
9449 return DW_FORM_data2;
9450 case 16:
9451 return DW_FORM_data4;
9452 case 32:
9453 return DW_FORM_data8;
9454 case 64:
9455 if (dwarf_version >= 5)
9456 return DW_FORM_data16;
9457 /* FALLTHRU */
9458 default:
9459 return DW_FORM_block1;
9461 case dw_val_class_wide_int:
9462 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9464 case 8:
9465 return DW_FORM_data1;
9466 case 16:
9467 return DW_FORM_data2;
9468 case 32:
9469 return DW_FORM_data4;
9470 case 64:
9471 return DW_FORM_data8;
9472 case 128:
9473 if (dwarf_version >= 5)
9474 return DW_FORM_data16;
9475 /* FALLTHRU */
9476 default:
9477 return DW_FORM_block1;
9479 case dw_val_class_vec:
9480 switch (constant_size (a->dw_attr_val.v.val_vec.length
9481 * a->dw_attr_val.v.val_vec.elt_size))
9483 case 1:
9484 return DW_FORM_block1;
9485 case 2:
9486 return DW_FORM_block2;
9487 case 4:
9488 return DW_FORM_block4;
9489 default:
9490 gcc_unreachable ();
9492 case dw_val_class_flag:
9493 if (dwarf_version >= 4)
9495 /* Currently all add_AT_flag calls pass in 1 as last argument,
9496 so DW_FORM_flag_present can be used. If that ever changes,
9497 we'll need to use DW_FORM_flag and have some optimization
9498 in build_abbrev_table that will change those to
9499 DW_FORM_flag_present if it is set to 1 in all DIEs using
9500 the same abbrev entry. */
9501 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9502 return DW_FORM_flag_present;
9504 return DW_FORM_flag;
9505 case dw_val_class_die_ref:
9506 if (AT_ref_external (a))
9507 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9508 else
9509 return DW_FORM_ref;
9510 case dw_val_class_fde_ref:
9511 return DW_FORM_data;
9512 case dw_val_class_lbl_id:
9513 return (AT_index (a) == NOT_INDEXED
9514 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9515 case dw_val_class_lineptr:
9516 case dw_val_class_macptr:
9517 case dw_val_class_loclistsptr:
9518 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9519 case dw_val_class_str:
9520 return AT_string_form (a);
9521 case dw_val_class_file:
9522 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9524 case 1:
9525 return DW_FORM_data1;
9526 case 2:
9527 return DW_FORM_data2;
9528 case 4:
9529 return DW_FORM_data4;
9530 default:
9531 gcc_unreachable ();
9534 case dw_val_class_data8:
9535 return DW_FORM_data8;
9537 case dw_val_class_high_pc:
9538 switch (DWARF2_ADDR_SIZE)
9540 case 1:
9541 return DW_FORM_data1;
9542 case 2:
9543 return DW_FORM_data2;
9544 case 4:
9545 return DW_FORM_data4;
9546 case 8:
9547 return DW_FORM_data8;
9548 default:
9549 gcc_unreachable ();
9552 case dw_val_class_discr_value:
9553 return (a->dw_attr_val.v.val_discr_value.pos
9554 ? DW_FORM_udata
9555 : DW_FORM_sdata);
9556 case dw_val_class_discr_list:
9557 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9559 case 1:
9560 return DW_FORM_block1;
9561 case 2:
9562 return DW_FORM_block2;
9563 case 4:
9564 return DW_FORM_block4;
9565 default:
9566 gcc_unreachable ();
9569 default:
9570 gcc_unreachable ();
9574 /* Output the encoding of an attribute value. */
9576 static void
9577 output_value_format (dw_attr_node *a)
9579 enum dwarf_form form = value_format (a);
9581 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9584 /* Given a die and id, produce the appropriate abbreviations. */
9586 static void
9587 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9589 unsigned ix;
9590 dw_attr_node *a_attr;
9592 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9593 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9594 dwarf_tag_name (abbrev->die_tag));
9596 if (abbrev->die_child != NULL)
9597 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9598 else
9599 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9601 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9603 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9604 dwarf_attr_name (a_attr->dw_attr));
9605 output_value_format (a_attr);
9606 if (value_format (a_attr) == DW_FORM_implicit_const)
9608 if (AT_class (a_attr) == dw_val_class_file_implicit)
9610 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9611 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9612 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9614 else
9615 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9619 dw2_asm_output_data (1, 0, NULL);
9620 dw2_asm_output_data (1, 0, NULL);
9624 /* Output the .debug_abbrev section which defines the DIE abbreviation
9625 table. */
9627 static void
9628 output_abbrev_section (void)
9630 unsigned int abbrev_id;
9631 dw_die_ref abbrev;
9633 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9634 if (abbrev_id != 0)
9635 output_die_abbrevs (abbrev_id, abbrev);
9637 /* Terminate the table. */
9638 dw2_asm_output_data (1, 0, NULL);
9641 /* Return a new location list, given the begin and end range, and the
9642 expression. */
9644 static inline dw_loc_list_ref
9645 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9646 const char *section)
9648 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9650 retlist->begin = begin;
9651 retlist->begin_entry = NULL;
9652 retlist->end = end;
9653 retlist->expr = expr;
9654 retlist->section = section;
9656 return retlist;
9659 /* Generate a new internal symbol for this location list node, if it
9660 hasn't got one yet. */
9662 static inline void
9663 gen_llsym (dw_loc_list_ref list)
9665 gcc_assert (!list->ll_symbol);
9666 list->ll_symbol = gen_internal_sym ("LLST");
9669 /* Output the location list given to us. */
9671 static void
9672 output_loc_list (dw_loc_list_ref list_head)
9674 if (list_head->emitted)
9675 return;
9676 list_head->emitted = true;
9678 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9680 dw_loc_list_ref curr = list_head;
9681 const char *last_section = NULL;
9682 const char *base_label = NULL;
9684 /* Walk the location list, and output each range + expression. */
9685 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9687 unsigned long size;
9688 /* Don't output an entry that starts and ends at the same address. */
9689 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9690 continue;
9691 size = size_of_locs (curr->expr);
9692 /* If the expression is too large, drop it on the floor. We could
9693 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9694 in the expression, but >= 64KB expressions for a single value
9695 in a single range are unlikely very useful. */
9696 if (dwarf_version < 5 && size > 0xffff)
9697 continue;
9698 if (dwarf_version >= 5)
9700 if (dwarf_split_debug_info)
9702 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9703 uleb128 index into .debug_addr and uleb128 length. */
9704 dw2_asm_output_data (1, DW_LLE_startx_length,
9705 "DW_LLE_startx_length (%s)",
9706 list_head->ll_symbol);
9707 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9708 "Location list range start index "
9709 "(%s)", curr->begin);
9710 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9711 For that case we probably need to emit DW_LLE_startx_endx,
9712 but we'd need 2 .debug_addr entries rather than just one. */
9713 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9714 "Location list length (%s)",
9715 list_head->ll_symbol);
9717 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9719 /* If all code is in .text section, the base address is
9720 already provided by the CU attributes. Use
9721 DW_LLE_offset_pair where both addresses are uleb128 encoded
9722 offsets against that base. */
9723 dw2_asm_output_data (1, DW_LLE_offset_pair,
9724 "DW_LLE_offset_pair (%s)",
9725 list_head->ll_symbol);
9726 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9727 "Location list begin address (%s)",
9728 list_head->ll_symbol);
9729 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9730 "Location list end address (%s)",
9731 list_head->ll_symbol);
9733 else if (HAVE_AS_LEB128)
9735 /* Otherwise, find out how many consecutive entries could share
9736 the same base entry. If just one, emit DW_LLE_start_length,
9737 otherwise emit DW_LLE_base_address for the base address
9738 followed by a series of DW_LLE_offset_pair. */
9739 if (last_section == NULL || curr->section != last_section)
9741 dw_loc_list_ref curr2;
9742 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9743 curr2 = curr2->dw_loc_next)
9745 if (strcmp (curr2->begin, curr2->end) == 0
9746 && !curr2->force)
9747 continue;
9748 break;
9750 if (curr2 == NULL || curr->section != curr2->section)
9751 last_section = NULL;
9752 else
9754 last_section = curr->section;
9755 base_label = curr->begin;
9756 dw2_asm_output_data (1, DW_LLE_base_address,
9757 "DW_LLE_base_address (%s)",
9758 list_head->ll_symbol);
9759 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9760 "Base address (%s)",
9761 list_head->ll_symbol);
9764 /* Only one entry with the same base address. Use
9765 DW_LLE_start_length with absolute address and uleb128
9766 length. */
9767 if (last_section == NULL)
9769 dw2_asm_output_data (1, DW_LLE_start_length,
9770 "DW_LLE_start_length (%s)",
9771 list_head->ll_symbol);
9772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9773 "Location list begin address (%s)",
9774 list_head->ll_symbol);
9775 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9776 "Location list length "
9777 "(%s)", list_head->ll_symbol);
9779 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9780 DW_LLE_base_address. */
9781 else
9783 dw2_asm_output_data (1, DW_LLE_offset_pair,
9784 "DW_LLE_offset_pair (%s)",
9785 list_head->ll_symbol);
9786 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9787 "Location list begin address "
9788 "(%s)", list_head->ll_symbol);
9789 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9790 "Location list end address "
9791 "(%s)", list_head->ll_symbol);
9794 /* The assembler does not support .uleb128 directive. Emit
9795 DW_LLE_start_end with a pair of absolute addresses. */
9796 else
9798 dw2_asm_output_data (1, DW_LLE_start_end,
9799 "DW_LLE_start_end (%s)",
9800 list_head->ll_symbol);
9801 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9802 "Location list begin address (%s)",
9803 list_head->ll_symbol);
9804 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9805 "Location list end address (%s)",
9806 list_head->ll_symbol);
9809 else if (dwarf_split_debug_info)
9811 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9812 and 4 byte length. */
9813 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9814 "Location list start/length entry (%s)",
9815 list_head->ll_symbol);
9816 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9817 "Location list range start index (%s)",
9818 curr->begin);
9819 /* The length field is 4 bytes. If we ever need to support
9820 an 8-byte length, we can add a new DW_LLE code or fall back
9821 to DW_LLE_GNU_start_end_entry. */
9822 dw2_asm_output_delta (4, curr->end, curr->begin,
9823 "Location list range length (%s)",
9824 list_head->ll_symbol);
9826 else if (!have_multiple_function_sections)
9828 /* Pair of relative addresses against start of text section. */
9829 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9830 "Location list begin address (%s)",
9831 list_head->ll_symbol);
9832 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9833 "Location list end address (%s)",
9834 list_head->ll_symbol);
9836 else
9838 /* Pair of absolute addresses. */
9839 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9840 "Location list begin address (%s)",
9841 list_head->ll_symbol);
9842 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9843 "Location list end address (%s)",
9844 list_head->ll_symbol);
9847 /* Output the block length for this list of location operations. */
9848 if (dwarf_version >= 5)
9849 dw2_asm_output_data_uleb128 (size, "Location expression size");
9850 else
9852 gcc_assert (size <= 0xffff);
9853 dw2_asm_output_data (2, size, "Location expression size");
9856 output_loc_sequence (curr->expr, -1);
9859 /* And finally list termination. */
9860 if (dwarf_version >= 5)
9861 dw2_asm_output_data (1, DW_LLE_end_of_list,
9862 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9863 else if (dwarf_split_debug_info)
9864 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9865 "Location list terminator (%s)",
9866 list_head->ll_symbol);
9867 else
9869 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9870 "Location list terminator begin (%s)",
9871 list_head->ll_symbol);
9872 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9873 "Location list terminator end (%s)",
9874 list_head->ll_symbol);
9878 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9879 section. Emit a relocated reference if val_entry is NULL, otherwise,
9880 emit an indirect reference. */
9882 static void
9883 output_range_list_offset (dw_attr_node *a)
9885 const char *name = dwarf_attr_name (a->dw_attr);
9887 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9889 if (dwarf_version >= 5)
9891 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9892 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9893 debug_ranges_section, "%s", name);
9895 else
9897 char *p = strchr (ranges_section_label, '\0');
9898 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9899 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9901 debug_ranges_section, "%s", name);
9902 *p = '\0';
9905 else if (dwarf_version >= 5)
9907 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9908 gcc_assert (rnglist_idx);
9909 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9911 else
9912 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9913 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9914 "%s (offset from %s)", name, ranges_section_label);
9917 /* Output the offset into the debug_loc section. */
9919 static void
9920 output_loc_list_offset (dw_attr_node *a)
9922 char *sym = AT_loc_list (a)->ll_symbol;
9924 gcc_assert (sym);
9925 if (!dwarf_split_debug_info)
9926 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9927 "%s", dwarf_attr_name (a->dw_attr));
9928 else if (dwarf_version >= 5)
9930 gcc_assert (AT_loc_list (a)->num_assigned);
9931 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9932 dwarf_attr_name (a->dw_attr),
9933 sym);
9935 else
9936 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9937 "%s", dwarf_attr_name (a->dw_attr));
9940 /* Output an attribute's index or value appropriately. */
9942 static void
9943 output_attr_index_or_value (dw_attr_node *a)
9945 const char *name = dwarf_attr_name (a->dw_attr);
9947 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9949 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9950 return;
9952 switch (AT_class (a))
9954 case dw_val_class_addr:
9955 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9956 break;
9957 case dw_val_class_high_pc:
9958 case dw_val_class_lbl_id:
9959 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9960 break;
9961 default:
9962 gcc_unreachable ();
9966 /* Output a type signature. */
9968 static inline void
9969 output_signature (const char *sig, const char *name)
9971 int i;
9973 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9974 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9977 /* Output a discriminant value. */
9979 static inline void
9980 output_discr_value (dw_discr_value *discr_value, const char *name)
9982 if (discr_value->pos)
9983 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9984 else
9985 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9988 /* Output the DIE and its attributes. Called recursively to generate
9989 the definitions of each child DIE. */
9991 static void
9992 output_die (dw_die_ref die)
9994 dw_attr_node *a;
9995 dw_die_ref c;
9996 unsigned long size;
9997 unsigned ix;
9999 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10000 (unsigned long)die->die_offset,
10001 dwarf_tag_name (die->die_tag));
10003 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10005 const char *name = dwarf_attr_name (a->dw_attr);
10007 switch (AT_class (a))
10009 case dw_val_class_addr:
10010 output_attr_index_or_value (a);
10011 break;
10013 case dw_val_class_offset:
10014 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10015 "%s", name);
10016 break;
10018 case dw_val_class_range_list:
10019 output_range_list_offset (a);
10020 break;
10022 case dw_val_class_loc:
10023 size = size_of_locs (AT_loc (a));
10025 /* Output the block length for this list of location operations. */
10026 if (dwarf_version >= 4)
10027 dw2_asm_output_data_uleb128 (size, "%s", name);
10028 else
10029 dw2_asm_output_data (constant_size (size), size, "%s", name);
10031 output_loc_sequence (AT_loc (a), -1);
10032 break;
10034 case dw_val_class_const:
10035 /* ??? It would be slightly more efficient to use a scheme like is
10036 used for unsigned constants below, but gdb 4.x does not sign
10037 extend. Gdb 5.x does sign extend. */
10038 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10039 break;
10041 case dw_val_class_unsigned_const:
10043 int csize = constant_size (AT_unsigned (a));
10044 if (dwarf_version == 3
10045 && a->dw_attr == DW_AT_data_member_location
10046 && csize >= 4)
10047 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10048 else
10049 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10051 break;
10053 case dw_val_class_const_implicit:
10054 if (flag_debug_asm)
10055 fprintf (asm_out_file, "\t\t\t%s %s ("
10056 HOST_WIDE_INT_PRINT_DEC ")\n",
10057 ASM_COMMENT_START, name, AT_int (a));
10058 break;
10060 case dw_val_class_unsigned_const_implicit:
10061 if (flag_debug_asm)
10062 fprintf (asm_out_file, "\t\t\t%s %s ("
10063 HOST_WIDE_INT_PRINT_HEX ")\n",
10064 ASM_COMMENT_START, name, AT_unsigned (a));
10065 break;
10067 case dw_val_class_const_double:
10069 unsigned HOST_WIDE_INT first, second;
10071 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10072 dw2_asm_output_data (1,
10073 HOST_BITS_PER_DOUBLE_INT
10074 / HOST_BITS_PER_CHAR,
10075 NULL);
10077 if (WORDS_BIG_ENDIAN)
10079 first = a->dw_attr_val.v.val_double.high;
10080 second = a->dw_attr_val.v.val_double.low;
10082 else
10084 first = a->dw_attr_val.v.val_double.low;
10085 second = a->dw_attr_val.v.val_double.high;
10088 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10089 first, "%s", name);
10090 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10091 second, NULL);
10093 break;
10095 case dw_val_class_wide_int:
10097 int i;
10098 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10099 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10100 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10101 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10102 * l, NULL);
10104 if (WORDS_BIG_ENDIAN)
10105 for (i = len - 1; i >= 0; --i)
10107 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10108 "%s", name);
10109 name = "";
10111 else
10112 for (i = 0; i < len; ++i)
10114 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10115 "%s", name);
10116 name = "";
10119 break;
10121 case dw_val_class_vec:
10123 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10124 unsigned int len = a->dw_attr_val.v.val_vec.length;
10125 unsigned int i;
10126 unsigned char *p;
10128 dw2_asm_output_data (constant_size (len * elt_size),
10129 len * elt_size, "%s", name);
10130 if (elt_size > sizeof (HOST_WIDE_INT))
10132 elt_size /= 2;
10133 len *= 2;
10135 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10136 i < len;
10137 i++, p += elt_size)
10138 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10139 "fp or vector constant word %u", i);
10140 break;
10143 case dw_val_class_flag:
10144 if (dwarf_version >= 4)
10146 /* Currently all add_AT_flag calls pass in 1 as last argument,
10147 so DW_FORM_flag_present can be used. If that ever changes,
10148 we'll need to use DW_FORM_flag and have some optimization
10149 in build_abbrev_table that will change those to
10150 DW_FORM_flag_present if it is set to 1 in all DIEs using
10151 the same abbrev entry. */
10152 gcc_assert (AT_flag (a) == 1);
10153 if (flag_debug_asm)
10154 fprintf (asm_out_file, "\t\t\t%s %s\n",
10155 ASM_COMMENT_START, name);
10156 break;
10158 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10159 break;
10161 case dw_val_class_loc_list:
10162 output_loc_list_offset (a);
10163 break;
10165 case dw_val_class_die_ref:
10166 if (AT_ref_external (a))
10168 if (AT_ref (a)->comdat_type_p)
10170 comdat_type_node *type_node
10171 = AT_ref (a)->die_id.die_type_node;
10173 gcc_assert (type_node);
10174 output_signature (type_node->signature, name);
10176 else
10178 const char *sym = AT_ref (a)->die_id.die_symbol;
10179 int size;
10181 gcc_assert (sym);
10182 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10183 length, whereas in DWARF3 it's always sized as an
10184 offset. */
10185 if (dwarf_version == 2)
10186 size = DWARF2_ADDR_SIZE;
10187 else
10188 size = DWARF_OFFSET_SIZE;
10189 /* ??? We cannot unconditionally output die_offset if
10190 non-zero - others might create references to those
10191 DIEs via symbols.
10192 And we do not clear its DIE offset after outputting it
10193 (and the label refers to the actual DIEs, not the
10194 DWARF CU unit header which is when using label + offset
10195 would be the correct thing to do).
10196 ??? This is the reason for the with_offset flag. */
10197 if (AT_ref (a)->with_offset)
10198 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10199 debug_info_section, "%s", name);
10200 else
10201 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10202 name);
10205 else
10207 gcc_assert (AT_ref (a)->die_offset);
10208 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10209 "%s", name);
10211 break;
10213 case dw_val_class_fde_ref:
10215 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10217 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10218 a->dw_attr_val.v.val_fde_index * 2);
10219 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10220 "%s", name);
10222 break;
10224 case dw_val_class_vms_delta:
10225 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10226 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10227 AT_vms_delta2 (a), AT_vms_delta1 (a),
10228 "%s", name);
10229 #else
10230 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10231 AT_vms_delta2 (a), AT_vms_delta1 (a),
10232 "%s", name);
10233 #endif
10234 break;
10236 case dw_val_class_lbl_id:
10237 output_attr_index_or_value (a);
10238 break;
10240 case dw_val_class_lineptr:
10241 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10242 debug_line_section, "%s", name);
10243 break;
10245 case dw_val_class_macptr:
10246 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10247 debug_macinfo_section, "%s", name);
10248 break;
10250 case dw_val_class_loclistsptr:
10251 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10252 debug_loc_section, "%s", name);
10253 break;
10255 case dw_val_class_str:
10256 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10257 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10258 a->dw_attr_val.v.val_str->label,
10259 debug_str_section,
10260 "%s: \"%s\"", name, AT_string (a));
10261 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10262 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10263 a->dw_attr_val.v.val_str->label,
10264 debug_line_str_section,
10265 "%s: \"%s\"", name, AT_string (a));
10266 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10267 dw2_asm_output_data_uleb128 (AT_index (a),
10268 "%s: \"%s\"", name, AT_string (a));
10269 else
10270 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10271 break;
10273 case dw_val_class_file:
10275 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10277 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10278 a->dw_attr_val.v.val_file->filename);
10279 break;
10282 case dw_val_class_file_implicit:
10283 if (flag_debug_asm)
10284 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10285 ASM_COMMENT_START, name,
10286 maybe_emit_file (a->dw_attr_val.v.val_file),
10287 a->dw_attr_val.v.val_file->filename);
10288 break;
10290 case dw_val_class_data8:
10292 int i;
10294 for (i = 0; i < 8; i++)
10295 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10296 i == 0 ? "%s" : NULL, name);
10297 break;
10300 case dw_val_class_high_pc:
10301 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10302 get_AT_low_pc (die), "DW_AT_high_pc");
10303 break;
10305 case dw_val_class_discr_value:
10306 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10307 break;
10309 case dw_val_class_discr_list:
10311 dw_discr_list_ref list = AT_discr_list (a);
10312 const int size = size_of_discr_list (list);
10314 /* This is a block, so output its length first. */
10315 dw2_asm_output_data (constant_size (size), size,
10316 "%s: block size", name);
10318 for (; list != NULL; list = list->dw_discr_next)
10320 /* One byte for the discriminant value descriptor, and then as
10321 many LEB128 numbers as required. */
10322 if (list->dw_discr_range)
10323 dw2_asm_output_data (1, DW_DSC_range,
10324 "%s: DW_DSC_range", name);
10325 else
10326 dw2_asm_output_data (1, DW_DSC_label,
10327 "%s: DW_DSC_label", name);
10329 output_discr_value (&list->dw_discr_lower_bound, name);
10330 if (list->dw_discr_range)
10331 output_discr_value (&list->dw_discr_upper_bound, name);
10333 break;
10336 default:
10337 gcc_unreachable ();
10341 FOR_EACH_CHILD (die, c, output_die (c));
10343 /* Add null byte to terminate sibling list. */
10344 if (die->die_child != NULL)
10345 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10346 (unsigned long) die->die_offset);
10349 /* Output the compilation unit that appears at the beginning of the
10350 .debug_info section, and precedes the DIE descriptions. */
10352 static void
10353 output_compilation_unit_header (enum dwarf_unit_type ut)
10355 if (!XCOFF_DEBUGGING_INFO)
10357 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10358 dw2_asm_output_data (4, 0xffffffff,
10359 "Initial length escape value indicating 64-bit DWARF extension");
10360 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10361 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10362 "Length of Compilation Unit Info");
10365 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10366 if (dwarf_version >= 5)
10368 const char *name;
10369 switch (ut)
10371 case DW_UT_compile: name = "DW_UT_compile"; break;
10372 case DW_UT_type: name = "DW_UT_type"; break;
10373 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10374 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10375 default: gcc_unreachable ();
10377 dw2_asm_output_data (1, ut, "%s", name);
10378 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10380 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10381 debug_abbrev_section,
10382 "Offset Into Abbrev. Section");
10383 if (dwarf_version < 5)
10384 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10387 /* Output the compilation unit DIE and its children. */
10389 static void
10390 output_comp_unit (dw_die_ref die, int output_if_empty,
10391 const unsigned char *dwo_id)
10393 const char *secname, *oldsym;
10394 char *tmp;
10396 /* Unless we are outputting main CU, we may throw away empty ones. */
10397 if (!output_if_empty && die->die_child == NULL)
10398 return;
10400 /* Even if there are no children of this DIE, we must output the information
10401 about the compilation unit. Otherwise, on an empty translation unit, we
10402 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10403 will then complain when examining the file. First mark all the DIEs in
10404 this CU so we know which get local refs. */
10405 mark_dies (die);
10407 external_ref_hash_type *extern_map = optimize_external_refs (die);
10409 /* For now, optimize only the main CU, in order to optimize the rest
10410 we'd need to see all of them earlier. Leave the rest for post-linking
10411 tools like DWZ. */
10412 if (die == comp_unit_die ())
10413 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10415 build_abbrev_table (die, extern_map);
10417 optimize_abbrev_table ();
10419 delete extern_map;
10421 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10422 next_die_offset = (dwo_id
10423 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10424 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10425 calc_die_sizes (die);
10427 oldsym = die->die_id.die_symbol;
10428 if (oldsym && die->comdat_type_p)
10430 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10432 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10433 secname = tmp;
10434 die->die_id.die_symbol = NULL;
10435 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10437 else
10439 switch_to_section (debug_info_section);
10440 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10441 info_section_emitted = true;
10444 /* For LTO cross unit DIE refs we want a symbol on the start of the
10445 debuginfo section, not on the CU DIE. */
10446 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10448 /* ??? No way to get visibility assembled without a decl. */
10449 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10450 get_identifier (oldsym), char_type_node);
10451 TREE_PUBLIC (decl) = true;
10452 TREE_STATIC (decl) = true;
10453 DECL_ARTIFICIAL (decl) = true;
10454 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10455 DECL_VISIBILITY_SPECIFIED (decl) = true;
10456 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10457 #ifdef ASM_WEAKEN_LABEL
10458 /* We prefer a .weak because that handles duplicates from duplicate
10459 archive members in a graceful way. */
10460 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10461 #else
10462 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10463 #endif
10464 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10467 /* Output debugging information. */
10468 output_compilation_unit_header (dwo_id
10469 ? DW_UT_split_compile : DW_UT_compile);
10470 if (dwarf_version >= 5)
10472 if (dwo_id != NULL)
10473 for (int i = 0; i < 8; i++)
10474 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10476 output_die (die);
10478 /* Leave the marks on the main CU, so we can check them in
10479 output_pubnames. */
10480 if (oldsym)
10482 unmark_dies (die);
10483 die->die_id.die_symbol = oldsym;
10487 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10488 and .debug_pubtypes. This is configured per-target, but can be
10489 overridden by the -gpubnames or -gno-pubnames options. */
10491 static inline bool
10492 want_pubnames (void)
10494 if (debug_info_level <= DINFO_LEVEL_TERSE)
10495 return false;
10496 if (debug_generate_pub_sections != -1)
10497 return debug_generate_pub_sections;
10498 return targetm.want_debug_pub_sections;
10501 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10503 static void
10504 add_AT_pubnames (dw_die_ref die)
10506 if (want_pubnames ())
10507 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10510 /* Add a string attribute value to a skeleton DIE. */
10512 static inline void
10513 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10514 const char *str)
10516 dw_attr_node attr;
10517 struct indirect_string_node *node;
10519 if (! skeleton_debug_str_hash)
10520 skeleton_debug_str_hash
10521 = hash_table<indirect_string_hasher>::create_ggc (10);
10523 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10524 find_string_form (node);
10525 if (node->form == DW_FORM_GNU_str_index)
10526 node->form = DW_FORM_strp;
10528 attr.dw_attr = attr_kind;
10529 attr.dw_attr_val.val_class = dw_val_class_str;
10530 attr.dw_attr_val.val_entry = NULL;
10531 attr.dw_attr_val.v.val_str = node;
10532 add_dwarf_attr (die, &attr);
10535 /* Helper function to generate top-level dies for skeleton debug_info and
10536 debug_types. */
10538 static void
10539 add_top_level_skeleton_die_attrs (dw_die_ref die)
10541 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10542 const char *comp_dir = comp_dir_string ();
10544 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10545 if (comp_dir != NULL)
10546 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10547 add_AT_pubnames (die);
10548 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10551 /* Output skeleton debug sections that point to the dwo file. */
10553 static void
10554 output_skeleton_debug_sections (dw_die_ref comp_unit,
10555 const unsigned char *dwo_id)
10557 /* These attributes will be found in the full debug_info section. */
10558 remove_AT (comp_unit, DW_AT_producer);
10559 remove_AT (comp_unit, DW_AT_language);
10561 switch_to_section (debug_skeleton_info_section);
10562 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10564 /* Produce the skeleton compilation-unit header. This one differs enough from
10565 a normal CU header that it's better not to call output_compilation_unit
10566 header. */
10567 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10568 dw2_asm_output_data (4, 0xffffffff,
10569 "Initial length escape value indicating 64-bit "
10570 "DWARF extension");
10572 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10573 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10574 - DWARF_INITIAL_LENGTH_SIZE
10575 + size_of_die (comp_unit),
10576 "Length of Compilation Unit Info");
10577 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10578 if (dwarf_version >= 5)
10580 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10581 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10583 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10584 debug_skeleton_abbrev_section,
10585 "Offset Into Abbrev. Section");
10586 if (dwarf_version < 5)
10587 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10588 else
10589 for (int i = 0; i < 8; i++)
10590 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10592 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10593 output_die (comp_unit);
10595 /* Build the skeleton debug_abbrev section. */
10596 switch_to_section (debug_skeleton_abbrev_section);
10597 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10599 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10601 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10604 /* Output a comdat type unit DIE and its children. */
10606 static void
10607 output_comdat_type_unit (comdat_type_node *node)
10609 const char *secname;
10610 char *tmp;
10611 int i;
10612 #if defined (OBJECT_FORMAT_ELF)
10613 tree comdat_key;
10614 #endif
10616 /* First mark all the DIEs in this CU so we know which get local refs. */
10617 mark_dies (node->root_die);
10619 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10621 build_abbrev_table (node->root_die, extern_map);
10623 delete extern_map;
10624 extern_map = NULL;
10626 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10627 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10628 calc_die_sizes (node->root_die);
10630 #if defined (OBJECT_FORMAT_ELF)
10631 if (dwarf_version >= 5)
10633 if (!dwarf_split_debug_info)
10634 secname = ".debug_info";
10635 else
10636 secname = ".debug_info.dwo";
10638 else if (!dwarf_split_debug_info)
10639 secname = ".debug_types";
10640 else
10641 secname = ".debug_types.dwo";
10643 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10644 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10645 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10646 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10647 comdat_key = get_identifier (tmp);
10648 targetm.asm_out.named_section (secname,
10649 SECTION_DEBUG | SECTION_LINKONCE,
10650 comdat_key);
10651 #else
10652 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10653 sprintf (tmp, (dwarf_version >= 5
10654 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10655 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10656 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10657 secname = tmp;
10658 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10659 #endif
10661 /* Output debugging information. */
10662 output_compilation_unit_header (dwarf_split_debug_info
10663 ? DW_UT_split_type : DW_UT_type);
10664 output_signature (node->signature, "Type Signature");
10665 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10666 "Offset to Type DIE");
10667 output_die (node->root_die);
10669 unmark_dies (node->root_die);
10672 /* Return the DWARF2/3 pubname associated with a decl. */
10674 static const char *
10675 dwarf2_name (tree decl, int scope)
10677 if (DECL_NAMELESS (decl))
10678 return NULL;
10679 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10682 /* Add a new entry to .debug_pubnames if appropriate. */
10684 static void
10685 add_pubname_string (const char *str, dw_die_ref die)
10687 pubname_entry e;
10689 e.die = die;
10690 e.name = xstrdup (str);
10691 vec_safe_push (pubname_table, e);
10694 static void
10695 add_pubname (tree decl, dw_die_ref die)
10697 if (!want_pubnames ())
10698 return;
10700 /* Don't add items to the table when we expect that the consumer will have
10701 just read the enclosing die. For example, if the consumer is looking at a
10702 class_member, it will either be inside the class already, or will have just
10703 looked up the class to find the member. Either way, searching the class is
10704 faster than searching the index. */
10705 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10706 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10708 const char *name = dwarf2_name (decl, 1);
10710 if (name)
10711 add_pubname_string (name, die);
10715 /* Add an enumerator to the pubnames section. */
10717 static void
10718 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10720 pubname_entry e;
10722 gcc_assert (scope_name);
10723 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10724 e.die = die;
10725 vec_safe_push (pubname_table, e);
10728 /* Add a new entry to .debug_pubtypes if appropriate. */
10730 static void
10731 add_pubtype (tree decl, dw_die_ref die)
10733 pubname_entry e;
10735 if (!want_pubnames ())
10736 return;
10738 if ((TREE_PUBLIC (decl)
10739 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10740 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10742 tree scope = NULL;
10743 const char *scope_name = "";
10744 const char *sep = is_cxx () ? "::" : ".";
10745 const char *name;
10747 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10748 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10750 scope_name = lang_hooks.dwarf_name (scope, 1);
10751 if (scope_name != NULL && scope_name[0] != '\0')
10752 scope_name = concat (scope_name, sep, NULL);
10753 else
10754 scope_name = "";
10757 if (TYPE_P (decl))
10758 name = type_tag (decl);
10759 else
10760 name = lang_hooks.dwarf_name (decl, 1);
10762 /* If we don't have a name for the type, there's no point in adding
10763 it to the table. */
10764 if (name != NULL && name[0] != '\0')
10766 e.die = die;
10767 e.name = concat (scope_name, name, NULL);
10768 vec_safe_push (pubtype_table, e);
10771 /* Although it might be more consistent to add the pubinfo for the
10772 enumerators as their dies are created, they should only be added if the
10773 enum type meets the criteria above. So rather than re-check the parent
10774 enum type whenever an enumerator die is created, just output them all
10775 here. This isn't protected by the name conditional because anonymous
10776 enums don't have names. */
10777 if (die->die_tag == DW_TAG_enumeration_type)
10779 dw_die_ref c;
10781 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10786 /* Output a single entry in the pubnames table. */
10788 static void
10789 output_pubname (dw_offset die_offset, pubname_entry *entry)
10791 dw_die_ref die = entry->die;
10792 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10794 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10796 if (debug_generate_pub_sections == 2)
10798 /* This logic follows gdb's method for determining the value of the flag
10799 byte. */
10800 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10801 switch (die->die_tag)
10803 case DW_TAG_typedef:
10804 case DW_TAG_base_type:
10805 case DW_TAG_subrange_type:
10806 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10807 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10808 break;
10809 case DW_TAG_enumerator:
10810 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10811 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10812 if (!is_cxx ())
10813 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10814 break;
10815 case DW_TAG_subprogram:
10816 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10817 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10818 if (!is_ada ())
10819 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10820 break;
10821 case DW_TAG_constant:
10822 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10823 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10824 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10825 break;
10826 case DW_TAG_variable:
10827 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10828 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10829 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10830 break;
10831 case DW_TAG_namespace:
10832 case DW_TAG_imported_declaration:
10833 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10834 break;
10835 case DW_TAG_class_type:
10836 case DW_TAG_interface_type:
10837 case DW_TAG_structure_type:
10838 case DW_TAG_union_type:
10839 case DW_TAG_enumeration_type:
10840 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10841 if (!is_cxx ())
10842 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10843 break;
10844 default:
10845 /* An unusual tag. Leave the flag-byte empty. */
10846 break;
10848 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10849 "GDB-index flags");
10852 dw2_asm_output_nstring (entry->name, -1, "external name");
10856 /* Output the public names table used to speed up access to externally
10857 visible names; or the public types table used to find type definitions. */
10859 static void
10860 output_pubnames (vec<pubname_entry, va_gc> *names)
10862 unsigned i;
10863 unsigned long pubnames_length = size_of_pubnames (names);
10864 pubname_entry *pub;
10866 if (!XCOFF_DEBUGGING_INFO)
10868 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10869 dw2_asm_output_data (4, 0xffffffff,
10870 "Initial length escape value indicating 64-bit DWARF extension");
10871 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10872 "Pub Info Length");
10875 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10876 dw2_asm_output_data (2, 2, "DWARF Version");
10878 if (dwarf_split_debug_info)
10879 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10880 debug_skeleton_info_section,
10881 "Offset of Compilation Unit Info");
10882 else
10883 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10884 debug_info_section,
10885 "Offset of Compilation Unit Info");
10886 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10887 "Compilation Unit Length");
10889 FOR_EACH_VEC_ELT (*names, i, pub)
10891 if (include_pubname_in_output (names, pub))
10893 dw_offset die_offset = pub->die->die_offset;
10895 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10896 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10897 gcc_assert (pub->die->die_mark);
10899 /* If we're putting types in their own .debug_types sections,
10900 the .debug_pubtypes table will still point to the compile
10901 unit (not the type unit), so we want to use the offset of
10902 the skeleton DIE (if there is one). */
10903 if (pub->die->comdat_type_p && names == pubtype_table)
10905 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10907 if (type_node != NULL)
10908 die_offset = (type_node->skeleton_die != NULL
10909 ? type_node->skeleton_die->die_offset
10910 : comp_unit_die ()->die_offset);
10913 output_pubname (die_offset, pub);
10917 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10920 /* Output public names and types tables if necessary. */
10922 static void
10923 output_pubtables (void)
10925 if (!want_pubnames () || !info_section_emitted)
10926 return;
10928 switch_to_section (debug_pubnames_section);
10929 output_pubnames (pubname_table);
10930 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10931 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10932 simply won't look for the section. */
10933 switch_to_section (debug_pubtypes_section);
10934 output_pubnames (pubtype_table);
10938 /* Output the information that goes into the .debug_aranges table.
10939 Namely, define the beginning and ending address range of the
10940 text section generated for this compilation unit. */
10942 static void
10943 output_aranges (void)
10945 unsigned i;
10946 unsigned long aranges_length = size_of_aranges ();
10948 if (!XCOFF_DEBUGGING_INFO)
10950 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10951 dw2_asm_output_data (4, 0xffffffff,
10952 "Initial length escape value indicating 64-bit DWARF extension");
10953 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10954 "Length of Address Ranges Info");
10957 /* Version number for aranges is still 2, even up to DWARF5. */
10958 dw2_asm_output_data (2, 2, "DWARF Version");
10959 if (dwarf_split_debug_info)
10960 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10961 debug_skeleton_info_section,
10962 "Offset of Compilation Unit Info");
10963 else
10964 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10965 debug_info_section,
10966 "Offset of Compilation Unit Info");
10967 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10968 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10970 /* We need to align to twice the pointer size here. */
10971 if (DWARF_ARANGES_PAD_SIZE)
10973 /* Pad using a 2 byte words so that padding is correct for any
10974 pointer size. */
10975 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10976 2 * DWARF2_ADDR_SIZE);
10977 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10978 dw2_asm_output_data (2, 0, NULL);
10981 /* It is necessary not to output these entries if the sections were
10982 not used; if the sections were not used, the length will be 0 and
10983 the address may end up as 0 if the section is discarded by ld
10984 --gc-sections, leaving an invalid (0, 0) entry that can be
10985 confused with the terminator. */
10986 if (text_section_used)
10988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10989 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10990 text_section_label, "Length");
10992 if (cold_text_section_used)
10994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10995 "Address");
10996 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10997 cold_text_section_label, "Length");
11000 if (have_multiple_function_sections)
11002 unsigned fde_idx;
11003 dw_fde_ref fde;
11005 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11007 if (DECL_IGNORED_P (fde->decl))
11008 continue;
11009 if (!fde->in_std_section)
11011 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11012 "Address");
11013 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11014 fde->dw_fde_begin, "Length");
11016 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11018 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11019 "Address");
11020 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11021 fde->dw_fde_second_begin, "Length");
11026 /* Output the terminator words. */
11027 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11028 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11031 /* Add a new entry to .debug_ranges. Return its index into
11032 ranges_table vector. */
11034 static unsigned int
11035 add_ranges_num (int num, bool maybe_new_sec)
11037 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11038 vec_safe_push (ranges_table, r);
11039 return vec_safe_length (ranges_table) - 1;
11042 /* Add a new entry to .debug_ranges corresponding to a block, or a
11043 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11044 this entry might be in a different section from previous range. */
11046 static unsigned int
11047 add_ranges (const_tree block, bool maybe_new_sec)
11049 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11052 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11053 chain, or middle entry of a chain that will be directly referred to. */
11055 static void
11056 note_rnglist_head (unsigned int offset)
11058 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11059 return;
11060 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11063 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11064 When using dwarf_split_debug_info, address attributes in dies destined
11065 for the final executable should be direct references--setting the
11066 parameter force_direct ensures this behavior. */
11068 static void
11069 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11070 bool *added, bool force_direct)
11072 unsigned int in_use = vec_safe_length (ranges_by_label);
11073 unsigned int offset;
11074 dw_ranges_by_label rbl = { begin, end };
11075 vec_safe_push (ranges_by_label, rbl);
11076 offset = add_ranges_num (-(int)in_use - 1, true);
11077 if (!*added)
11079 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11080 *added = true;
11081 note_rnglist_head (offset);
11085 /* Emit .debug_ranges section. */
11087 static void
11088 output_ranges (void)
11090 unsigned i;
11091 static const char *const start_fmt = "Offset %#x";
11092 const char *fmt = start_fmt;
11093 dw_ranges *r;
11095 switch_to_section (debug_ranges_section);
11096 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11097 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11099 int block_num = r->num;
11101 if (block_num > 0)
11103 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11104 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11106 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11107 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11109 /* If all code is in the text section, then the compilation
11110 unit base address defaults to DW_AT_low_pc, which is the
11111 base of the text section. */
11112 if (!have_multiple_function_sections)
11114 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11115 text_section_label,
11116 fmt, i * 2 * DWARF2_ADDR_SIZE);
11117 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11118 text_section_label, NULL);
11121 /* Otherwise, the compilation unit base address is zero,
11122 which allows us to use absolute addresses, and not worry
11123 about whether the target supports cross-section
11124 arithmetic. */
11125 else
11127 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11128 fmt, i * 2 * DWARF2_ADDR_SIZE);
11129 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11132 fmt = NULL;
11135 /* Negative block_num stands for an index into ranges_by_label. */
11136 else if (block_num < 0)
11138 int lab_idx = - block_num - 1;
11140 if (!have_multiple_function_sections)
11142 gcc_unreachable ();
11143 #if 0
11144 /* If we ever use add_ranges_by_labels () for a single
11145 function section, all we have to do is to take out
11146 the #if 0 above. */
11147 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11148 (*ranges_by_label)[lab_idx].begin,
11149 text_section_label,
11150 fmt, i * 2 * DWARF2_ADDR_SIZE);
11151 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11152 (*ranges_by_label)[lab_idx].end,
11153 text_section_label, NULL);
11154 #endif
11156 else
11158 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11159 (*ranges_by_label)[lab_idx].begin,
11160 fmt, i * 2 * DWARF2_ADDR_SIZE);
11161 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11162 (*ranges_by_label)[lab_idx].end,
11163 NULL);
11166 else
11168 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11169 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11170 fmt = start_fmt;
11175 /* Non-zero if .debug_line_str should be used for .debug_line section
11176 strings or strings that are likely shareable with those. */
11177 #define DWARF5_USE_DEBUG_LINE_STR \
11178 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11179 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11180 /* FIXME: there is no .debug_line_str.dwo section, \
11181 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11182 && !dwarf_split_debug_info)
11184 /* Assign .debug_rnglists indexes. */
11186 static void
11187 index_rnglists (void)
11189 unsigned i;
11190 dw_ranges *r;
11192 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11193 if (r->label)
11194 r->idx = rnglist_idx++;
11197 /* Emit .debug_rnglists section. */
11199 static void
11200 output_rnglists (void)
11202 unsigned i;
11203 dw_ranges *r;
11204 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11205 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11206 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11208 switch_to_section (debug_ranges_section);
11209 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11210 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11211 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11212 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11213 dw2_asm_output_data (4, 0xffffffff,
11214 "Initial length escape value indicating "
11215 "64-bit DWARF extension");
11216 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11217 "Length of Range Lists");
11218 ASM_OUTPUT_LABEL (asm_out_file, l1);
11219 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11220 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11221 dw2_asm_output_data (1, 0, "Segment Size");
11222 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11223 about relocation sizes and primarily care about the size of .debug*
11224 sections in linked shared libraries and executables, then
11225 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11226 into it are usually larger than just DW_FORM_sec_offset offsets
11227 into the .debug_rnglists section. */
11228 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11229 "Offset Entry Count");
11230 if (dwarf_split_debug_info)
11232 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11233 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11234 if (r->label)
11235 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11236 ranges_base_label, NULL);
11239 const char *lab = "";
11240 unsigned int len = vec_safe_length (ranges_table);
11241 const char *base = NULL;
11242 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11244 int block_num = r->num;
11246 if (r->label)
11248 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11249 lab = r->label;
11251 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11252 base = NULL;
11253 if (block_num > 0)
11255 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11256 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11258 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11259 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11261 if (HAVE_AS_LEB128)
11263 /* If all code is in the text section, then the compilation
11264 unit base address defaults to DW_AT_low_pc, which is the
11265 base of the text section. */
11266 if (!have_multiple_function_sections)
11268 dw2_asm_output_data (1, DW_RLE_offset_pair,
11269 "DW_RLE_offset_pair (%s)", lab);
11270 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11271 "Range begin address (%s)", lab);
11272 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11273 "Range end address (%s)", lab);
11274 continue;
11276 if (base == NULL)
11278 dw_ranges *r2 = NULL;
11279 if (i < len - 1)
11280 r2 = &(*ranges_table)[i + 1];
11281 if (r2
11282 && r2->num != 0
11283 && r2->label == NULL
11284 && !r2->maybe_new_sec)
11286 dw2_asm_output_data (1, DW_RLE_base_address,
11287 "DW_RLE_base_address (%s)", lab);
11288 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11289 "Base address (%s)", lab);
11290 strcpy (basebuf, blabel);
11291 base = basebuf;
11294 if (base)
11296 dw2_asm_output_data (1, DW_RLE_offset_pair,
11297 "DW_RLE_offset_pair (%s)", lab);
11298 dw2_asm_output_delta_uleb128 (blabel, base,
11299 "Range begin address (%s)", lab);
11300 dw2_asm_output_delta_uleb128 (elabel, base,
11301 "Range end address (%s)", lab);
11302 continue;
11304 dw2_asm_output_data (1, DW_RLE_start_length,
11305 "DW_RLE_start_length (%s)", lab);
11306 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11307 "Range begin address (%s)", lab);
11308 dw2_asm_output_delta_uleb128 (elabel, blabel,
11309 "Range length (%s)", lab);
11311 else
11313 dw2_asm_output_data (1, DW_RLE_start_end,
11314 "DW_RLE_start_end (%s)", lab);
11315 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11316 "Range begin address (%s)", lab);
11317 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11318 "Range end address (%s)", lab);
11322 /* Negative block_num stands for an index into ranges_by_label. */
11323 else if (block_num < 0)
11325 int lab_idx = - block_num - 1;
11326 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11327 const char *elabel = (*ranges_by_label)[lab_idx].end;
11329 if (!have_multiple_function_sections)
11330 gcc_unreachable ();
11331 if (HAVE_AS_LEB128)
11333 dw2_asm_output_data (1, DW_RLE_start_length,
11334 "DW_RLE_start_length (%s)", lab);
11335 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11336 "Range begin address (%s)", lab);
11337 dw2_asm_output_delta_uleb128 (elabel, blabel,
11338 "Range length (%s)", lab);
11340 else
11342 dw2_asm_output_data (1, DW_RLE_start_end,
11343 "DW_RLE_start_end (%s)", lab);
11344 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11345 "Range begin address (%s)", lab);
11346 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11347 "Range end address (%s)", lab);
11350 else
11351 dw2_asm_output_data (1, DW_RLE_end_of_list,
11352 "DW_RLE_end_of_list (%s)", lab);
11354 ASM_OUTPUT_LABEL (asm_out_file, l2);
11357 /* Data structure containing information about input files. */
11358 struct file_info
11360 const char *path; /* Complete file name. */
11361 const char *fname; /* File name part. */
11362 int length; /* Length of entire string. */
11363 struct dwarf_file_data * file_idx; /* Index in input file table. */
11364 int dir_idx; /* Index in directory table. */
11367 /* Data structure containing information about directories with source
11368 files. */
11369 struct dir_info
11371 const char *path; /* Path including directory name. */
11372 int length; /* Path length. */
11373 int prefix; /* Index of directory entry which is a prefix. */
11374 int count; /* Number of files in this directory. */
11375 int dir_idx; /* Index of directory used as base. */
11378 /* Callback function for file_info comparison. We sort by looking at
11379 the directories in the path. */
11381 static int
11382 file_info_cmp (const void *p1, const void *p2)
11384 const struct file_info *const s1 = (const struct file_info *) p1;
11385 const struct file_info *const s2 = (const struct file_info *) p2;
11386 const unsigned char *cp1;
11387 const unsigned char *cp2;
11389 /* Take care of file names without directories. We need to make sure that
11390 we return consistent values to qsort since some will get confused if
11391 we return the same value when identical operands are passed in opposite
11392 orders. So if neither has a directory, return 0 and otherwise return
11393 1 or -1 depending on which one has the directory. */
11394 if ((s1->path == s1->fname || s2->path == s2->fname))
11395 return (s2->path == s2->fname) - (s1->path == s1->fname);
11397 cp1 = (const unsigned char *) s1->path;
11398 cp2 = (const unsigned char *) s2->path;
11400 while (1)
11402 ++cp1;
11403 ++cp2;
11404 /* Reached the end of the first path? If so, handle like above. */
11405 if ((cp1 == (const unsigned char *) s1->fname)
11406 || (cp2 == (const unsigned char *) s2->fname))
11407 return ((cp2 == (const unsigned char *) s2->fname)
11408 - (cp1 == (const unsigned char *) s1->fname));
11410 /* Character of current path component the same? */
11411 else if (*cp1 != *cp2)
11412 return *cp1 - *cp2;
11416 struct file_name_acquire_data
11418 struct file_info *files;
11419 int used_files;
11420 int max_files;
11423 /* Traversal function for the hash table. */
11426 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11428 struct dwarf_file_data *d = *slot;
11429 struct file_info *fi;
11430 const char *f;
11432 gcc_assert (fnad->max_files >= d->emitted_number);
11434 if (! d->emitted_number)
11435 return 1;
11437 gcc_assert (fnad->max_files != fnad->used_files);
11439 fi = fnad->files + fnad->used_files++;
11441 /* Skip all leading "./". */
11442 f = d->filename;
11443 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11444 f += 2;
11446 /* Create a new array entry. */
11447 fi->path = f;
11448 fi->length = strlen (f);
11449 fi->file_idx = d;
11451 /* Search for the file name part. */
11452 f = strrchr (f, DIR_SEPARATOR);
11453 #if defined (DIR_SEPARATOR_2)
11455 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11457 if (g != NULL)
11459 if (f == NULL || f < g)
11460 f = g;
11463 #endif
11465 fi->fname = f == NULL ? fi->path : f + 1;
11466 return 1;
11469 /* Helper function for output_file_names. Emit a FORM encoded
11470 string STR, with assembly comment start ENTRY_KIND and
11471 index IDX */
11473 static void
11474 output_line_string (enum dwarf_form form, const char *str,
11475 const char *entry_kind, unsigned int idx)
11477 switch (form)
11479 case DW_FORM_string:
11480 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11481 break;
11482 case DW_FORM_line_strp:
11483 if (!debug_line_str_hash)
11484 debug_line_str_hash
11485 = hash_table<indirect_string_hasher>::create_ggc (10);
11487 struct indirect_string_node *node;
11488 node = find_AT_string_in_table (str, debug_line_str_hash);
11489 set_indirect_string (node);
11490 node->form = form;
11491 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11492 debug_line_str_section, "%s: %#x: \"%s\"",
11493 entry_kind, 0, node->str);
11494 break;
11495 default:
11496 gcc_unreachable ();
11500 /* Output the directory table and the file name table. We try to minimize
11501 the total amount of memory needed. A heuristic is used to avoid large
11502 slowdowns with many input files. */
11504 static void
11505 output_file_names (void)
11507 struct file_name_acquire_data fnad;
11508 int numfiles;
11509 struct file_info *files;
11510 struct dir_info *dirs;
11511 int *saved;
11512 int *savehere;
11513 int *backmap;
11514 int ndirs;
11515 int idx_offset;
11516 int i;
11518 if (!last_emitted_file)
11520 if (dwarf_version >= 5)
11522 dw2_asm_output_data (1, 0, "Directory entry format count");
11523 dw2_asm_output_data_uleb128 (0, "Directories count");
11524 dw2_asm_output_data (1, 0, "File name entry format count");
11525 dw2_asm_output_data_uleb128 (0, "File names count");
11527 else
11529 dw2_asm_output_data (1, 0, "End directory table");
11530 dw2_asm_output_data (1, 0, "End file name table");
11532 return;
11535 numfiles = last_emitted_file->emitted_number;
11537 /* Allocate the various arrays we need. */
11538 files = XALLOCAVEC (struct file_info, numfiles);
11539 dirs = XALLOCAVEC (struct dir_info, numfiles);
11541 fnad.files = files;
11542 fnad.used_files = 0;
11543 fnad.max_files = numfiles;
11544 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11545 gcc_assert (fnad.used_files == fnad.max_files);
11547 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11549 /* Find all the different directories used. */
11550 dirs[0].path = files[0].path;
11551 dirs[0].length = files[0].fname - files[0].path;
11552 dirs[0].prefix = -1;
11553 dirs[0].count = 1;
11554 dirs[0].dir_idx = 0;
11555 files[0].dir_idx = 0;
11556 ndirs = 1;
11558 for (i = 1; i < numfiles; i++)
11559 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11560 && memcmp (dirs[ndirs - 1].path, files[i].path,
11561 dirs[ndirs - 1].length) == 0)
11563 /* Same directory as last entry. */
11564 files[i].dir_idx = ndirs - 1;
11565 ++dirs[ndirs - 1].count;
11567 else
11569 int j;
11571 /* This is a new directory. */
11572 dirs[ndirs].path = files[i].path;
11573 dirs[ndirs].length = files[i].fname - files[i].path;
11574 dirs[ndirs].count = 1;
11575 dirs[ndirs].dir_idx = ndirs;
11576 files[i].dir_idx = ndirs;
11578 /* Search for a prefix. */
11579 dirs[ndirs].prefix = -1;
11580 for (j = 0; j < ndirs; j++)
11581 if (dirs[j].length < dirs[ndirs].length
11582 && dirs[j].length > 1
11583 && (dirs[ndirs].prefix == -1
11584 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11585 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11586 dirs[ndirs].prefix = j;
11588 ++ndirs;
11591 /* Now to the actual work. We have to find a subset of the directories which
11592 allow expressing the file name using references to the directory table
11593 with the least amount of characters. We do not do an exhaustive search
11594 where we would have to check out every combination of every single
11595 possible prefix. Instead we use a heuristic which provides nearly optimal
11596 results in most cases and never is much off. */
11597 saved = XALLOCAVEC (int, ndirs);
11598 savehere = XALLOCAVEC (int, ndirs);
11600 memset (saved, '\0', ndirs * sizeof (saved[0]));
11601 for (i = 0; i < ndirs; i++)
11603 int j;
11604 int total;
11606 /* We can always save some space for the current directory. But this
11607 does not mean it will be enough to justify adding the directory. */
11608 savehere[i] = dirs[i].length;
11609 total = (savehere[i] - saved[i]) * dirs[i].count;
11611 for (j = i + 1; j < ndirs; j++)
11613 savehere[j] = 0;
11614 if (saved[j] < dirs[i].length)
11616 /* Determine whether the dirs[i] path is a prefix of the
11617 dirs[j] path. */
11618 int k;
11620 k = dirs[j].prefix;
11621 while (k != -1 && k != (int) i)
11622 k = dirs[k].prefix;
11624 if (k == (int) i)
11626 /* Yes it is. We can possibly save some memory by
11627 writing the filenames in dirs[j] relative to
11628 dirs[i]. */
11629 savehere[j] = dirs[i].length;
11630 total += (savehere[j] - saved[j]) * dirs[j].count;
11635 /* Check whether we can save enough to justify adding the dirs[i]
11636 directory. */
11637 if (total > dirs[i].length + 1)
11639 /* It's worthwhile adding. */
11640 for (j = i; j < ndirs; j++)
11641 if (savehere[j] > 0)
11643 /* Remember how much we saved for this directory so far. */
11644 saved[j] = savehere[j];
11646 /* Remember the prefix directory. */
11647 dirs[j].dir_idx = i;
11652 /* Emit the directory name table. */
11653 idx_offset = dirs[0].length > 0 ? 1 : 0;
11654 enum dwarf_form str_form = DW_FORM_string;
11655 enum dwarf_form idx_form = DW_FORM_udata;
11656 if (dwarf_version >= 5)
11658 const char *comp_dir = comp_dir_string ();
11659 if (comp_dir == NULL)
11660 comp_dir = "";
11661 dw2_asm_output_data (1, 1, "Directory entry format count");
11662 if (DWARF5_USE_DEBUG_LINE_STR)
11663 str_form = DW_FORM_line_strp;
11664 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11665 dw2_asm_output_data_uleb128 (str_form, "%s",
11666 get_DW_FORM_name (str_form));
11667 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11668 if (str_form == DW_FORM_string)
11670 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11671 for (i = 1 - idx_offset; i < ndirs; i++)
11672 dw2_asm_output_nstring (dirs[i].path,
11673 dirs[i].length
11674 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11675 "Directory Entry: %#x", i + idx_offset);
11677 else
11679 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11680 for (i = 1 - idx_offset; i < ndirs; i++)
11682 const char *str
11683 = ggc_alloc_string (dirs[i].path,
11684 dirs[i].length
11685 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11686 output_line_string (str_form, str, "Directory Entry",
11687 (unsigned) i + idx_offset);
11691 else
11693 for (i = 1 - idx_offset; i < ndirs; i++)
11694 dw2_asm_output_nstring (dirs[i].path,
11695 dirs[i].length
11696 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11697 "Directory Entry: %#x", i + idx_offset);
11699 dw2_asm_output_data (1, 0, "End directory table");
11702 /* We have to emit them in the order of emitted_number since that's
11703 used in the debug info generation. To do this efficiently we
11704 generate a back-mapping of the indices first. */
11705 backmap = XALLOCAVEC (int, numfiles);
11706 for (i = 0; i < numfiles; i++)
11707 backmap[files[i].file_idx->emitted_number - 1] = i;
11709 if (dwarf_version >= 5)
11711 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11712 if (filename0 == NULL)
11713 filename0 = "";
11714 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11715 DW_FORM_data2. Choose one based on the number of directories
11716 and how much space would they occupy in each encoding.
11717 If we have at most 256 directories, all indexes fit into
11718 a single byte, so DW_FORM_data1 is most compact (if there
11719 are at most 128 directories, DW_FORM_udata would be as
11720 compact as that, but not shorter and slower to decode). */
11721 if (ndirs + idx_offset <= 256)
11722 idx_form = DW_FORM_data1;
11723 /* If there are more than 65536 directories, we have to use
11724 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11725 Otherwise, compute what space would occupy if all the indexes
11726 used DW_FORM_udata - sum - and compare that to how large would
11727 be DW_FORM_data2 encoding, and pick the more efficient one. */
11728 else if (ndirs + idx_offset <= 65536)
11730 unsigned HOST_WIDE_INT sum = 1;
11731 for (i = 0; i < numfiles; i++)
11733 int file_idx = backmap[i];
11734 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11735 sum += size_of_uleb128 (dir_idx);
11737 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11738 idx_form = DW_FORM_data2;
11740 #ifdef VMS_DEBUGGING_INFO
11741 dw2_asm_output_data (1, 4, "File name entry format count");
11742 #else
11743 dw2_asm_output_data (1, 2, "File name entry format count");
11744 #endif
11745 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11746 dw2_asm_output_data_uleb128 (str_form, "%s",
11747 get_DW_FORM_name (str_form));
11748 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11749 "DW_LNCT_directory_index");
11750 dw2_asm_output_data_uleb128 (idx_form, "%s",
11751 get_DW_FORM_name (idx_form));
11752 #ifdef VMS_DEBUGGING_INFO
11753 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11754 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11755 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11756 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11757 #endif
11758 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11760 output_line_string (str_form, filename0, "File Entry", 0);
11762 /* Include directory index. */
11763 if (idx_form != DW_FORM_udata)
11764 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11765 0, NULL);
11766 else
11767 dw2_asm_output_data_uleb128 (0, NULL);
11769 #ifdef VMS_DEBUGGING_INFO
11770 dw2_asm_output_data_uleb128 (0, NULL);
11771 dw2_asm_output_data_uleb128 (0, NULL);
11772 #endif
11775 /* Now write all the file names. */
11776 for (i = 0; i < numfiles; i++)
11778 int file_idx = backmap[i];
11779 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11781 #ifdef VMS_DEBUGGING_INFO
11782 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11784 /* Setting these fields can lead to debugger miscomparisons,
11785 but VMS Debug requires them to be set correctly. */
11787 int ver;
11788 long long cdt;
11789 long siz;
11790 int maxfilelen = (strlen (files[file_idx].path)
11791 + dirs[dir_idx].length
11792 + MAX_VMS_VERSION_LEN + 1);
11793 char *filebuf = XALLOCAVEC (char, maxfilelen);
11795 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11796 snprintf (filebuf, maxfilelen, "%s;%d",
11797 files[file_idx].path + dirs[dir_idx].length, ver);
11799 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11801 /* Include directory index. */
11802 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11803 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11804 dir_idx + idx_offset, NULL);
11805 else
11806 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11808 /* Modification time. */
11809 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11810 &cdt, 0, 0, 0) == 0)
11811 ? cdt : 0, NULL);
11813 /* File length in bytes. */
11814 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11815 0, &siz, 0, 0) == 0)
11816 ? siz : 0, NULL);
11817 #else
11818 output_line_string (str_form,
11819 files[file_idx].path + dirs[dir_idx].length,
11820 "File Entry", (unsigned) i + 1);
11822 /* Include directory index. */
11823 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11824 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11825 dir_idx + idx_offset, NULL);
11826 else
11827 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11829 if (dwarf_version >= 5)
11830 continue;
11832 /* Modification time. */
11833 dw2_asm_output_data_uleb128 (0, NULL);
11835 /* File length in bytes. */
11836 dw2_asm_output_data_uleb128 (0, NULL);
11837 #endif /* VMS_DEBUGGING_INFO */
11840 if (dwarf_version < 5)
11841 dw2_asm_output_data (1, 0, "End file name table");
11845 /* Output one line number table into the .debug_line section. */
11847 static void
11848 output_one_line_info_table (dw_line_info_table *table)
11850 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11851 unsigned int current_line = 1;
11852 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11853 dw_line_info_entry *ent;
11854 size_t i;
11856 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11858 switch (ent->opcode)
11860 case LI_set_address:
11861 /* ??? Unfortunately, we have little choice here currently, and
11862 must always use the most general form. GCC does not know the
11863 address delta itself, so we can't use DW_LNS_advance_pc. Many
11864 ports do have length attributes which will give an upper bound
11865 on the address range. We could perhaps use length attributes
11866 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11867 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11869 /* This can handle any delta. This takes
11870 4+DWARF2_ADDR_SIZE bytes. */
11871 dw2_asm_output_data (1, 0, "set address %s", line_label);
11872 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11873 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11874 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11875 break;
11877 case LI_set_line:
11878 if (ent->val == current_line)
11880 /* We still need to start a new row, so output a copy insn. */
11881 dw2_asm_output_data (1, DW_LNS_copy,
11882 "copy line %u", current_line);
11884 else
11886 int line_offset = ent->val - current_line;
11887 int line_delta = line_offset - DWARF_LINE_BASE;
11889 current_line = ent->val;
11890 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11892 /* This can handle deltas from -10 to 234, using the current
11893 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11894 This takes 1 byte. */
11895 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11896 "line %u", current_line);
11898 else
11900 /* This can handle any delta. This takes at least 4 bytes,
11901 depending on the value being encoded. */
11902 dw2_asm_output_data (1, DW_LNS_advance_line,
11903 "advance to line %u", current_line);
11904 dw2_asm_output_data_sleb128 (line_offset, NULL);
11905 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11908 break;
11910 case LI_set_file:
11911 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11912 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11913 break;
11915 case LI_set_column:
11916 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11917 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11918 break;
11920 case LI_negate_stmt:
11921 current_is_stmt = !current_is_stmt;
11922 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11923 "is_stmt %d", current_is_stmt);
11924 break;
11926 case LI_set_prologue_end:
11927 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11928 "set prologue end");
11929 break;
11931 case LI_set_epilogue_begin:
11932 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11933 "set epilogue begin");
11934 break;
11936 case LI_set_discriminator:
11937 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11938 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11939 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11940 dw2_asm_output_data_uleb128 (ent->val, NULL);
11941 break;
11945 /* Emit debug info for the address of the end of the table. */
11946 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11947 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11948 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11949 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11951 dw2_asm_output_data (1, 0, "end sequence");
11952 dw2_asm_output_data_uleb128 (1, NULL);
11953 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11956 /* Output the source line number correspondence information. This
11957 information goes into the .debug_line section. */
11959 static void
11960 output_line_info (bool prologue_only)
11962 static unsigned int generation;
11963 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11964 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11965 bool saw_one = false;
11966 int opc;
11968 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11969 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11970 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11971 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11973 if (!XCOFF_DEBUGGING_INFO)
11975 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11976 dw2_asm_output_data (4, 0xffffffff,
11977 "Initial length escape value indicating 64-bit DWARF extension");
11978 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11979 "Length of Source Line Info");
11982 ASM_OUTPUT_LABEL (asm_out_file, l1);
11984 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11985 if (dwarf_version >= 5)
11987 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11988 dw2_asm_output_data (1, 0, "Segment Size");
11990 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11991 ASM_OUTPUT_LABEL (asm_out_file, p1);
11993 /* Define the architecture-dependent minimum instruction length (in bytes).
11994 In this implementation of DWARF, this field is used for information
11995 purposes only. Since GCC generates assembly language, we have no
11996 a priori knowledge of how many instruction bytes are generated for each
11997 source line, and therefore can use only the DW_LNE_set_address and
11998 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11999 this as '1', which is "correct enough" for all architectures,
12000 and don't let the target override. */
12001 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12003 if (dwarf_version >= 4)
12004 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12005 "Maximum Operations Per Instruction");
12006 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12007 "Default is_stmt_start flag");
12008 dw2_asm_output_data (1, DWARF_LINE_BASE,
12009 "Line Base Value (Special Opcodes)");
12010 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12011 "Line Range Value (Special Opcodes)");
12012 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12013 "Special Opcode Base");
12015 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12017 int n_op_args;
12018 switch (opc)
12020 case DW_LNS_advance_pc:
12021 case DW_LNS_advance_line:
12022 case DW_LNS_set_file:
12023 case DW_LNS_set_column:
12024 case DW_LNS_fixed_advance_pc:
12025 case DW_LNS_set_isa:
12026 n_op_args = 1;
12027 break;
12028 default:
12029 n_op_args = 0;
12030 break;
12033 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12034 opc, n_op_args);
12037 /* Write out the information about the files we use. */
12038 output_file_names ();
12039 ASM_OUTPUT_LABEL (asm_out_file, p2);
12040 if (prologue_only)
12042 /* Output the marker for the end of the line number info. */
12043 ASM_OUTPUT_LABEL (asm_out_file, l2);
12044 return;
12047 if (separate_line_info)
12049 dw_line_info_table *table;
12050 size_t i;
12052 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12053 if (table->in_use)
12055 output_one_line_info_table (table);
12056 saw_one = true;
12059 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12061 output_one_line_info_table (cold_text_section_line_info);
12062 saw_one = true;
12065 /* ??? Some Darwin linkers crash on a .debug_line section with no
12066 sequences. Further, merely a DW_LNE_end_sequence entry is not
12067 sufficient -- the address column must also be initialized.
12068 Make sure to output at least one set_address/end_sequence pair,
12069 choosing .text since that section is always present. */
12070 if (text_section_line_info->in_use || !saw_one)
12071 output_one_line_info_table (text_section_line_info);
12073 /* Output the marker for the end of the line number info. */
12074 ASM_OUTPUT_LABEL (asm_out_file, l2);
12077 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12079 static inline bool
12080 need_endianity_attribute_p (bool reverse)
12082 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12085 /* Given a pointer to a tree node for some base type, return a pointer to
12086 a DIE that describes the given type. REVERSE is true if the type is
12087 to be interpreted in the reverse storage order wrt the target order.
12089 This routine must only be called for GCC type nodes that correspond to
12090 Dwarf base (fundamental) types. */
12092 static dw_die_ref
12093 base_type_die (tree type, bool reverse)
12095 dw_die_ref base_type_result;
12096 enum dwarf_type encoding;
12097 bool fpt_used = false;
12098 struct fixed_point_type_info fpt_info;
12099 tree type_bias = NULL_TREE;
12101 /* If this is a subtype that should not be emitted as a subrange type,
12102 use the base type. See subrange_type_for_debug_p. */
12103 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12104 type = TREE_TYPE (type);
12106 switch (TREE_CODE (type))
12108 case INTEGER_TYPE:
12109 if ((dwarf_version >= 4 || !dwarf_strict)
12110 && TYPE_NAME (type)
12111 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12112 && DECL_IS_BUILTIN (TYPE_NAME (type))
12113 && DECL_NAME (TYPE_NAME (type)))
12115 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12116 if (strcmp (name, "char16_t") == 0
12117 || strcmp (name, "char32_t") == 0)
12119 encoding = DW_ATE_UTF;
12120 break;
12123 if ((dwarf_version >= 3 || !dwarf_strict)
12124 && lang_hooks.types.get_fixed_point_type_info)
12126 memset (&fpt_info, 0, sizeof (fpt_info));
12127 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12129 fpt_used = true;
12130 encoding = ((TYPE_UNSIGNED (type))
12131 ? DW_ATE_unsigned_fixed
12132 : DW_ATE_signed_fixed);
12133 break;
12136 if (TYPE_STRING_FLAG (type))
12138 if (TYPE_UNSIGNED (type))
12139 encoding = DW_ATE_unsigned_char;
12140 else
12141 encoding = DW_ATE_signed_char;
12143 else if (TYPE_UNSIGNED (type))
12144 encoding = DW_ATE_unsigned;
12145 else
12146 encoding = DW_ATE_signed;
12148 if (!dwarf_strict
12149 && lang_hooks.types.get_type_bias)
12150 type_bias = lang_hooks.types.get_type_bias (type);
12151 break;
12153 case REAL_TYPE:
12154 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12156 if (dwarf_version >= 3 || !dwarf_strict)
12157 encoding = DW_ATE_decimal_float;
12158 else
12159 encoding = DW_ATE_lo_user;
12161 else
12162 encoding = DW_ATE_float;
12163 break;
12165 case FIXED_POINT_TYPE:
12166 if (!(dwarf_version >= 3 || !dwarf_strict))
12167 encoding = DW_ATE_lo_user;
12168 else if (TYPE_UNSIGNED (type))
12169 encoding = DW_ATE_unsigned_fixed;
12170 else
12171 encoding = DW_ATE_signed_fixed;
12172 break;
12174 /* Dwarf2 doesn't know anything about complex ints, so use
12175 a user defined type for it. */
12176 case COMPLEX_TYPE:
12177 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12178 encoding = DW_ATE_complex_float;
12179 else
12180 encoding = DW_ATE_lo_user;
12181 break;
12183 case BOOLEAN_TYPE:
12184 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12185 encoding = DW_ATE_boolean;
12186 break;
12188 default:
12189 /* No other TREE_CODEs are Dwarf fundamental types. */
12190 gcc_unreachable ();
12193 base_type_result = new_die_raw (DW_TAG_base_type);
12195 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12196 int_size_in_bytes (type));
12197 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12199 if (need_endianity_attribute_p (reverse))
12200 add_AT_unsigned (base_type_result, DW_AT_endianity,
12201 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12203 add_alignment_attribute (base_type_result, type);
12205 if (fpt_used)
12207 switch (fpt_info.scale_factor_kind)
12209 case fixed_point_scale_factor_binary:
12210 add_AT_int (base_type_result, DW_AT_binary_scale,
12211 fpt_info.scale_factor.binary);
12212 break;
12214 case fixed_point_scale_factor_decimal:
12215 add_AT_int (base_type_result, DW_AT_decimal_scale,
12216 fpt_info.scale_factor.decimal);
12217 break;
12219 case fixed_point_scale_factor_arbitrary:
12220 /* Arbitrary scale factors cannot be described in standard DWARF,
12221 yet. */
12222 if (!dwarf_strict)
12224 /* Describe the scale factor as a rational constant. */
12225 const dw_die_ref scale_factor
12226 = new_die (DW_TAG_constant, comp_unit_die (), type);
12228 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12229 fpt_info.scale_factor.arbitrary.numerator);
12230 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12231 fpt_info.scale_factor.arbitrary.denominator);
12233 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12235 break;
12237 default:
12238 gcc_unreachable ();
12242 if (type_bias)
12243 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12244 dw_scalar_form_constant
12245 | dw_scalar_form_exprloc
12246 | dw_scalar_form_reference,
12247 NULL);
12249 return base_type_result;
12252 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12253 named 'auto' in its type: return true for it, false otherwise. */
12255 static inline bool
12256 is_cxx_auto (tree type)
12258 if (is_cxx ())
12260 tree name = TYPE_IDENTIFIER (type);
12261 if (name == get_identifier ("auto")
12262 || name == get_identifier ("decltype(auto)"))
12263 return true;
12265 return false;
12268 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12269 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12271 static inline int
12272 is_base_type (tree type)
12274 switch (TREE_CODE (type))
12276 case INTEGER_TYPE:
12277 case REAL_TYPE:
12278 case FIXED_POINT_TYPE:
12279 case COMPLEX_TYPE:
12280 case BOOLEAN_TYPE:
12281 case POINTER_BOUNDS_TYPE:
12282 return 1;
12284 case VOID_TYPE:
12285 case ARRAY_TYPE:
12286 case RECORD_TYPE:
12287 case UNION_TYPE:
12288 case QUAL_UNION_TYPE:
12289 case ENUMERAL_TYPE:
12290 case FUNCTION_TYPE:
12291 case METHOD_TYPE:
12292 case POINTER_TYPE:
12293 case REFERENCE_TYPE:
12294 case NULLPTR_TYPE:
12295 case OFFSET_TYPE:
12296 case LANG_TYPE:
12297 case VECTOR_TYPE:
12298 return 0;
12300 default:
12301 if (is_cxx_auto (type))
12302 return 0;
12303 gcc_unreachable ();
12306 return 0;
12309 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12310 node, return the size in bits for the type if it is a constant, or else
12311 return the alignment for the type if the type's size is not constant, or
12312 else return BITS_PER_WORD if the type actually turns out to be an
12313 ERROR_MARK node. */
12315 static inline unsigned HOST_WIDE_INT
12316 simple_type_size_in_bits (const_tree type)
12318 if (TREE_CODE (type) == ERROR_MARK)
12319 return BITS_PER_WORD;
12320 else if (TYPE_SIZE (type) == NULL_TREE)
12321 return 0;
12322 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12323 return tree_to_uhwi (TYPE_SIZE (type));
12324 else
12325 return TYPE_ALIGN (type);
12328 /* Similarly, but return an offset_int instead of UHWI. */
12330 static inline offset_int
12331 offset_int_type_size_in_bits (const_tree type)
12333 if (TREE_CODE (type) == ERROR_MARK)
12334 return BITS_PER_WORD;
12335 else if (TYPE_SIZE (type) == NULL_TREE)
12336 return 0;
12337 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12338 return wi::to_offset (TYPE_SIZE (type));
12339 else
12340 return TYPE_ALIGN (type);
12343 /* Given a pointer to a tree node for a subrange type, return a pointer
12344 to a DIE that describes the given type. */
12346 static dw_die_ref
12347 subrange_type_die (tree type, tree low, tree high, tree bias,
12348 dw_die_ref context_die)
12350 dw_die_ref subrange_die;
12351 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12353 if (context_die == NULL)
12354 context_die = comp_unit_die ();
12356 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12358 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12360 /* The size of the subrange type and its base type do not match,
12361 so we need to generate a size attribute for the subrange type. */
12362 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12365 add_alignment_attribute (subrange_die, type);
12367 if (low)
12368 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12369 if (high)
12370 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12371 if (bias && !dwarf_strict)
12372 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12373 dw_scalar_form_constant
12374 | dw_scalar_form_exprloc
12375 | dw_scalar_form_reference,
12376 NULL);
12378 return subrange_die;
12381 /* Returns the (const and/or volatile) cv_qualifiers associated with
12382 the decl node. This will normally be augmented with the
12383 cv_qualifiers of the underlying type in add_type_attribute. */
12385 static int
12386 decl_quals (const_tree decl)
12388 return ((TREE_READONLY (decl)
12389 /* The C++ front-end correctly marks reference-typed
12390 variables as readonly, but from a language (and debug
12391 info) standpoint they are not const-qualified. */
12392 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12393 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12394 | (TREE_THIS_VOLATILE (decl)
12395 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12398 /* Determine the TYPE whose qualifiers match the largest strict subset
12399 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12400 qualifiers outside QUAL_MASK. */
12402 static int
12403 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12405 tree t;
12406 int best_rank = 0, best_qual = 0, max_rank;
12408 type_quals &= qual_mask;
12409 max_rank = popcount_hwi (type_quals) - 1;
12411 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12412 t = TYPE_NEXT_VARIANT (t))
12414 int q = TYPE_QUALS (t) & qual_mask;
12416 if ((q & type_quals) == q && q != type_quals
12417 && check_base_type (t, type))
12419 int rank = popcount_hwi (q);
12421 if (rank > best_rank)
12423 best_rank = rank;
12424 best_qual = q;
12429 return best_qual;
12432 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12433 static const dwarf_qual_info_t dwarf_qual_info[] =
12435 { TYPE_QUAL_CONST, DW_TAG_const_type },
12436 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12437 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12438 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12440 static const unsigned int dwarf_qual_info_size
12441 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12443 /* If DIE is a qualified DIE of some base DIE with the same parent,
12444 return the base DIE, otherwise return NULL. Set MASK to the
12445 qualifiers added compared to the returned DIE. */
12447 static dw_die_ref
12448 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12450 unsigned int i;
12451 for (i = 0; i < dwarf_qual_info_size; i++)
12452 if (die->die_tag == dwarf_qual_info[i].t)
12453 break;
12454 if (i == dwarf_qual_info_size)
12455 return NULL;
12456 if (vec_safe_length (die->die_attr) != 1)
12457 return NULL;
12458 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12459 if (type == NULL || type->die_parent != die->die_parent)
12460 return NULL;
12461 *mask |= dwarf_qual_info[i].q;
12462 if (depth)
12464 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12465 if (ret)
12466 return ret;
12468 return type;
12471 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12472 entry that chains the modifiers specified by CV_QUALS in front of the
12473 given type. REVERSE is true if the type is to be interpreted in the
12474 reverse storage order wrt the target order. */
12476 static dw_die_ref
12477 modified_type_die (tree type, int cv_quals, bool reverse,
12478 dw_die_ref context_die)
12480 enum tree_code code = TREE_CODE (type);
12481 dw_die_ref mod_type_die;
12482 dw_die_ref sub_die = NULL;
12483 tree item_type = NULL;
12484 tree qualified_type;
12485 tree name, low, high;
12486 dw_die_ref mod_scope;
12487 /* Only these cv-qualifiers are currently handled. */
12488 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12489 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12490 ENCODE_QUAL_ADDR_SPACE(~0U));
12491 const bool reverse_base_type
12492 = need_endianity_attribute_p (reverse) && is_base_type (type);
12494 if (code == ERROR_MARK)
12495 return NULL;
12497 if (lang_hooks.types.get_debug_type)
12499 tree debug_type = lang_hooks.types.get_debug_type (type);
12501 if (debug_type != NULL_TREE && debug_type != type)
12502 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12505 cv_quals &= cv_qual_mask;
12507 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12508 tag modifier (and not an attribute) old consumers won't be able
12509 to handle it. */
12510 if (dwarf_version < 3)
12511 cv_quals &= ~TYPE_QUAL_RESTRICT;
12513 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12514 if (dwarf_version < 5)
12515 cv_quals &= ~TYPE_QUAL_ATOMIC;
12517 /* See if we already have the appropriately qualified variant of
12518 this type. */
12519 qualified_type = get_qualified_type (type, cv_quals);
12521 if (qualified_type == sizetype)
12523 /* Try not to expose the internal sizetype type's name. */
12524 if (TYPE_NAME (qualified_type)
12525 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12527 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12529 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12530 && (TYPE_PRECISION (t)
12531 == TYPE_PRECISION (qualified_type))
12532 && (TYPE_UNSIGNED (t)
12533 == TYPE_UNSIGNED (qualified_type)));
12534 qualified_type = t;
12536 else if (qualified_type == sizetype
12537 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12538 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12539 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12540 qualified_type = size_type_node;
12543 /* If we do, then we can just use its DIE, if it exists. */
12544 if (qualified_type)
12546 mod_type_die = lookup_type_die (qualified_type);
12548 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12549 dealt with specially: the DIE with the attribute, if it exists, is
12550 placed immediately after the regular DIE for the same base type. */
12551 if (mod_type_die
12552 && (!reverse_base_type
12553 || ((mod_type_die = mod_type_die->die_sib) != NULL
12554 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12555 return mod_type_die;
12558 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12560 /* Handle C typedef types. */
12561 if (name
12562 && TREE_CODE (name) == TYPE_DECL
12563 && DECL_ORIGINAL_TYPE (name)
12564 && !DECL_ARTIFICIAL (name))
12566 tree dtype = TREE_TYPE (name);
12568 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12569 if (qualified_type == dtype && !reverse_base_type)
12571 tree origin = decl_ultimate_origin (name);
12573 /* Typedef variants that have an abstract origin don't get their own
12574 type DIE (see gen_typedef_die), so fall back on the ultimate
12575 abstract origin instead. */
12576 if (origin != NULL && origin != name)
12577 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12578 context_die);
12580 /* For a named type, use the typedef. */
12581 gen_type_die (qualified_type, context_die);
12582 return lookup_type_die (qualified_type);
12584 else
12586 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12587 dquals &= cv_qual_mask;
12588 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12589 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12590 /* cv-unqualified version of named type. Just use
12591 the unnamed type to which it refers. */
12592 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12593 reverse, context_die);
12594 /* Else cv-qualified version of named type; fall through. */
12598 mod_scope = scope_die_for (type, context_die);
12600 if (cv_quals)
12602 int sub_quals = 0, first_quals = 0;
12603 unsigned i;
12604 dw_die_ref first = NULL, last = NULL;
12606 /* Determine a lesser qualified type that most closely matches
12607 this one. Then generate DW_TAG_* entries for the remaining
12608 qualifiers. */
12609 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12610 cv_qual_mask);
12611 if (sub_quals && use_debug_types)
12613 bool needed = false;
12614 /* If emitting type units, make sure the order of qualifiers
12615 is canonical. Thus, start from unqualified type if
12616 an earlier qualifier is missing in sub_quals, but some later
12617 one is present there. */
12618 for (i = 0; i < dwarf_qual_info_size; i++)
12619 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12620 needed = true;
12621 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12623 sub_quals = 0;
12624 break;
12627 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12628 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12630 /* As not all intermediate qualified DIEs have corresponding
12631 tree types, ensure that qualified DIEs in the same scope
12632 as their DW_AT_type are emitted after their DW_AT_type,
12633 only with other qualified DIEs for the same type possibly
12634 in between them. Determine the range of such qualified
12635 DIEs now (first being the base type, last being corresponding
12636 last qualified DIE for it). */
12637 unsigned int count = 0;
12638 first = qualified_die_p (mod_type_die, &first_quals,
12639 dwarf_qual_info_size);
12640 if (first == NULL)
12641 first = mod_type_die;
12642 gcc_assert ((first_quals & ~sub_quals) == 0);
12643 for (count = 0, last = first;
12644 count < (1U << dwarf_qual_info_size);
12645 count++, last = last->die_sib)
12647 int quals = 0;
12648 if (last == mod_scope->die_child)
12649 break;
12650 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12651 != first)
12652 break;
12656 for (i = 0; i < dwarf_qual_info_size; i++)
12657 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12659 dw_die_ref d;
12660 if (first && first != last)
12662 for (d = first->die_sib; ; d = d->die_sib)
12664 int quals = 0;
12665 qualified_die_p (d, &quals, dwarf_qual_info_size);
12666 if (quals == (first_quals | dwarf_qual_info[i].q))
12667 break;
12668 if (d == last)
12670 d = NULL;
12671 break;
12674 if (d)
12676 mod_type_die = d;
12677 continue;
12680 if (first)
12682 d = new_die_raw (dwarf_qual_info[i].t);
12683 add_child_die_after (mod_scope, d, last);
12684 last = d;
12686 else
12687 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12688 if (mod_type_die)
12689 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12690 mod_type_die = d;
12691 first_quals |= dwarf_qual_info[i].q;
12694 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12696 dwarf_tag tag = DW_TAG_pointer_type;
12697 if (code == REFERENCE_TYPE)
12699 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12700 tag = DW_TAG_rvalue_reference_type;
12701 else
12702 tag = DW_TAG_reference_type;
12704 mod_type_die = new_die (tag, mod_scope, type);
12706 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12707 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12708 add_alignment_attribute (mod_type_die, type);
12709 item_type = TREE_TYPE (type);
12711 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12712 if (!ADDR_SPACE_GENERIC_P (as))
12714 int action = targetm.addr_space.debug (as);
12715 if (action >= 0)
12717 /* Positive values indicate an address_class. */
12718 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12720 else
12722 /* Negative values indicate an (inverted) segment base reg. */
12723 dw_loc_descr_ref d
12724 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12725 add_AT_loc (mod_type_die, DW_AT_segment, d);
12729 else if (code == INTEGER_TYPE
12730 && TREE_TYPE (type) != NULL_TREE
12731 && subrange_type_for_debug_p (type, &low, &high))
12733 tree bias = NULL_TREE;
12734 if (lang_hooks.types.get_type_bias)
12735 bias = lang_hooks.types.get_type_bias (type);
12736 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12737 item_type = TREE_TYPE (type);
12739 else if (is_base_type (type))
12741 mod_type_die = base_type_die (type, reverse);
12743 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12744 if (reverse_base_type)
12746 dw_die_ref after_die
12747 = modified_type_die (type, cv_quals, false, context_die);
12748 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12750 else
12751 add_child_die (comp_unit_die (), mod_type_die);
12753 add_pubtype (type, mod_type_die);
12755 else
12757 gen_type_die (type, context_die);
12759 /* We have to get the type_main_variant here (and pass that to the
12760 `lookup_type_die' routine) because the ..._TYPE node we have
12761 might simply be a *copy* of some original type node (where the
12762 copy was created to help us keep track of typedef names) and
12763 that copy might have a different TYPE_UID from the original
12764 ..._TYPE node. */
12765 if (TREE_CODE (type) == FUNCTION_TYPE
12766 || TREE_CODE (type) == METHOD_TYPE)
12768 /* For function/method types, can't just use type_main_variant here,
12769 because that can have different ref-qualifiers for C++,
12770 but try to canonicalize. */
12771 tree main = TYPE_MAIN_VARIANT (type);
12772 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12773 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12774 && check_base_type (t, main)
12775 && check_lang_type (t, type))
12776 return lookup_type_die (t);
12777 return lookup_type_die (type);
12779 else if (TREE_CODE (type) != VECTOR_TYPE
12780 && TREE_CODE (type) != ARRAY_TYPE)
12781 return lookup_type_die (type_main_variant (type));
12782 else
12783 /* Vectors have the debugging information in the type,
12784 not the main variant. */
12785 return lookup_type_die (type);
12788 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12789 don't output a DW_TAG_typedef, since there isn't one in the
12790 user's program; just attach a DW_AT_name to the type.
12791 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12792 if the base type already has the same name. */
12793 if (name
12794 && ((TREE_CODE (name) != TYPE_DECL
12795 && (qualified_type == TYPE_MAIN_VARIANT (type)
12796 || (cv_quals == TYPE_UNQUALIFIED)))
12797 || (TREE_CODE (name) == TYPE_DECL
12798 && TREE_TYPE (name) == qualified_type
12799 && DECL_NAME (name))))
12801 if (TREE_CODE (name) == TYPE_DECL)
12802 /* Could just call add_name_and_src_coords_attributes here,
12803 but since this is a builtin type it doesn't have any
12804 useful source coordinates anyway. */
12805 name = DECL_NAME (name);
12806 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12808 /* This probably indicates a bug. */
12809 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12811 name = TYPE_IDENTIFIER (type);
12812 add_name_attribute (mod_type_die,
12813 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12816 if (qualified_type && !reverse_base_type)
12817 equate_type_number_to_die (qualified_type, mod_type_die);
12819 if (item_type)
12820 /* We must do this after the equate_type_number_to_die call, in case
12821 this is a recursive type. This ensures that the modified_type_die
12822 recursion will terminate even if the type is recursive. Recursive
12823 types are possible in Ada. */
12824 sub_die = modified_type_die (item_type,
12825 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12826 reverse,
12827 context_die);
12829 if (sub_die != NULL)
12830 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12832 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12833 if (TYPE_ARTIFICIAL (type))
12834 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12836 return mod_type_die;
12839 /* Generate DIEs for the generic parameters of T.
12840 T must be either a generic type or a generic function.
12841 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12843 static void
12844 gen_generic_params_dies (tree t)
12846 tree parms, args;
12847 int parms_num, i;
12848 dw_die_ref die = NULL;
12849 int non_default;
12851 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12852 return;
12854 if (TYPE_P (t))
12855 die = lookup_type_die (t);
12856 else if (DECL_P (t))
12857 die = lookup_decl_die (t);
12859 gcc_assert (die);
12861 parms = lang_hooks.get_innermost_generic_parms (t);
12862 if (!parms)
12863 /* T has no generic parameter. It means T is neither a generic type
12864 or function. End of story. */
12865 return;
12867 parms_num = TREE_VEC_LENGTH (parms);
12868 args = lang_hooks.get_innermost_generic_args (t);
12869 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12870 non_default = int_cst_value (TREE_CHAIN (args));
12871 else
12872 non_default = TREE_VEC_LENGTH (args);
12873 for (i = 0; i < parms_num; i++)
12875 tree parm, arg, arg_pack_elems;
12876 dw_die_ref parm_die;
12878 parm = TREE_VEC_ELT (parms, i);
12879 arg = TREE_VEC_ELT (args, i);
12880 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12881 gcc_assert (parm && TREE_VALUE (parm) && arg);
12883 if (parm && TREE_VALUE (parm) && arg)
12885 /* If PARM represents a template parameter pack,
12886 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12887 by DW_TAG_template_*_parameter DIEs for the argument
12888 pack elements of ARG. Note that ARG would then be
12889 an argument pack. */
12890 if (arg_pack_elems)
12891 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12892 arg_pack_elems,
12893 die);
12894 else
12895 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12896 true /* emit name */, die);
12897 if (i >= non_default)
12898 add_AT_flag (parm_die, DW_AT_default_value, 1);
12903 /* Create and return a DIE for PARM which should be
12904 the representation of a generic type parameter.
12905 For instance, in the C++ front end, PARM would be a template parameter.
12906 ARG is the argument to PARM.
12907 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12908 name of the PARM.
12909 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12910 as a child node. */
12912 static dw_die_ref
12913 generic_parameter_die (tree parm, tree arg,
12914 bool emit_name_p,
12915 dw_die_ref parent_die)
12917 dw_die_ref tmpl_die = NULL;
12918 const char *name = NULL;
12920 if (!parm || !DECL_NAME (parm) || !arg)
12921 return NULL;
12923 /* We support non-type generic parameters and arguments,
12924 type generic parameters and arguments, as well as
12925 generic generic parameters (a.k.a. template template parameters in C++)
12926 and arguments. */
12927 if (TREE_CODE (parm) == PARM_DECL)
12928 /* PARM is a nontype generic parameter */
12929 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12930 else if (TREE_CODE (parm) == TYPE_DECL)
12931 /* PARM is a type generic parameter. */
12932 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12933 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12934 /* PARM is a generic generic parameter.
12935 Its DIE is a GNU extension. It shall have a
12936 DW_AT_name attribute to represent the name of the template template
12937 parameter, and a DW_AT_GNU_template_name attribute to represent the
12938 name of the template template argument. */
12939 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12940 parent_die, parm);
12941 else
12942 gcc_unreachable ();
12944 if (tmpl_die)
12946 tree tmpl_type;
12948 /* If PARM is a generic parameter pack, it means we are
12949 emitting debug info for a template argument pack element.
12950 In other terms, ARG is a template argument pack element.
12951 In that case, we don't emit any DW_AT_name attribute for
12952 the die. */
12953 if (emit_name_p)
12955 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12956 gcc_assert (name);
12957 add_AT_string (tmpl_die, DW_AT_name, name);
12960 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12962 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12963 TMPL_DIE should have a child DW_AT_type attribute that is set
12964 to the type of the argument to PARM, which is ARG.
12965 If PARM is a type generic parameter, TMPL_DIE should have a
12966 child DW_AT_type that is set to ARG. */
12967 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12968 add_type_attribute (tmpl_die, tmpl_type,
12969 (TREE_THIS_VOLATILE (tmpl_type)
12970 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12971 false, parent_die);
12973 else
12975 /* So TMPL_DIE is a DIE representing a
12976 a generic generic template parameter, a.k.a template template
12977 parameter in C++ and arg is a template. */
12979 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12980 to the name of the argument. */
12981 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12982 if (name)
12983 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12986 if (TREE_CODE (parm) == PARM_DECL)
12987 /* So PARM is a non-type generic parameter.
12988 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12989 attribute of TMPL_DIE which value represents the value
12990 of ARG.
12991 We must be careful here:
12992 The value of ARG might reference some function decls.
12993 We might currently be emitting debug info for a generic
12994 type and types are emitted before function decls, we don't
12995 know if the function decls referenced by ARG will actually be
12996 emitted after cgraph computations.
12997 So must defer the generation of the DW_AT_const_value to
12998 after cgraph is ready. */
12999 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13002 return tmpl_die;
13005 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13006 PARM_PACK must be a template parameter pack. The returned DIE
13007 will be child DIE of PARENT_DIE. */
13009 static dw_die_ref
13010 template_parameter_pack_die (tree parm_pack,
13011 tree parm_pack_args,
13012 dw_die_ref parent_die)
13014 dw_die_ref die;
13015 int j;
13017 gcc_assert (parent_die && parm_pack);
13019 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13020 add_name_and_src_coords_attributes (die, parm_pack);
13021 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13022 generic_parameter_die (parm_pack,
13023 TREE_VEC_ELT (parm_pack_args, j),
13024 false /* Don't emit DW_AT_name */,
13025 die);
13026 return die;
13029 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13030 an enumerated type. */
13032 static inline int
13033 type_is_enum (const_tree type)
13035 return TREE_CODE (type) == ENUMERAL_TYPE;
13038 /* Return the DBX register number described by a given RTL node. */
13040 static unsigned int
13041 dbx_reg_number (const_rtx rtl)
13043 unsigned regno = REGNO (rtl);
13045 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13047 #ifdef LEAF_REG_REMAP
13048 if (crtl->uses_only_leaf_regs)
13050 int leaf_reg = LEAF_REG_REMAP (regno);
13051 if (leaf_reg != -1)
13052 regno = (unsigned) leaf_reg;
13054 #endif
13056 regno = DBX_REGISTER_NUMBER (regno);
13057 gcc_assert (regno != INVALID_REGNUM);
13058 return regno;
13061 /* Optionally add a DW_OP_piece term to a location description expression.
13062 DW_OP_piece is only added if the location description expression already
13063 doesn't end with DW_OP_piece. */
13065 static void
13066 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13068 dw_loc_descr_ref loc;
13070 if (*list_head != NULL)
13072 /* Find the end of the chain. */
13073 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13076 if (loc->dw_loc_opc != DW_OP_piece)
13077 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13081 /* Return a location descriptor that designates a machine register or
13082 zero if there is none. */
13084 static dw_loc_descr_ref
13085 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13087 rtx regs;
13089 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13090 return 0;
13092 /* We only use "frame base" when we're sure we're talking about the
13093 post-prologue local stack frame. We do this by *not* running
13094 register elimination until this point, and recognizing the special
13095 argument pointer and soft frame pointer rtx's.
13096 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13097 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13098 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13100 dw_loc_descr_ref result = NULL;
13102 if (dwarf_version >= 4 || !dwarf_strict)
13104 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13105 initialized);
13106 if (result)
13107 add_loc_descr (&result,
13108 new_loc_descr (DW_OP_stack_value, 0, 0));
13110 return result;
13113 regs = targetm.dwarf_register_span (rtl);
13115 if (REG_NREGS (rtl) > 1 || regs)
13116 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13117 else
13119 unsigned int dbx_regnum = dbx_reg_number (rtl);
13120 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13121 return 0;
13122 return one_reg_loc_descriptor (dbx_regnum, initialized);
13126 /* Return a location descriptor that designates a machine register for
13127 a given hard register number. */
13129 static dw_loc_descr_ref
13130 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13132 dw_loc_descr_ref reg_loc_descr;
13134 if (regno <= 31)
13135 reg_loc_descr
13136 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13137 else
13138 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13140 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13141 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13143 return reg_loc_descr;
13146 /* Given an RTL of a register, return a location descriptor that
13147 designates a value that spans more than one register. */
13149 static dw_loc_descr_ref
13150 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13151 enum var_init_status initialized)
13153 int size, i;
13154 dw_loc_descr_ref loc_result = NULL;
13156 /* Simple, contiguous registers. */
13157 if (regs == NULL_RTX)
13159 unsigned reg = REGNO (rtl);
13160 int nregs;
13162 #ifdef LEAF_REG_REMAP
13163 if (crtl->uses_only_leaf_regs)
13165 int leaf_reg = LEAF_REG_REMAP (reg);
13166 if (leaf_reg != -1)
13167 reg = (unsigned) leaf_reg;
13169 #endif
13171 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13172 nregs = REG_NREGS (rtl);
13174 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13176 loc_result = NULL;
13177 while (nregs--)
13179 dw_loc_descr_ref t;
13181 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13182 VAR_INIT_STATUS_INITIALIZED);
13183 add_loc_descr (&loc_result, t);
13184 add_loc_descr_op_piece (&loc_result, size);
13185 ++reg;
13187 return loc_result;
13190 /* Now onto stupid register sets in non contiguous locations. */
13192 gcc_assert (GET_CODE (regs) == PARALLEL);
13194 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13195 loc_result = NULL;
13197 for (i = 0; i < XVECLEN (regs, 0); ++i)
13199 dw_loc_descr_ref t;
13201 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13202 VAR_INIT_STATUS_INITIALIZED);
13203 add_loc_descr (&loc_result, t);
13204 add_loc_descr_op_piece (&loc_result, size);
13207 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13208 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13209 return loc_result;
13212 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13214 /* Return a location descriptor that designates a constant i,
13215 as a compound operation from constant (i >> shift), constant shift
13216 and DW_OP_shl. */
13218 static dw_loc_descr_ref
13219 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13221 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13222 add_loc_descr (&ret, int_loc_descriptor (shift));
13223 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13224 return ret;
13227 /* Return a location descriptor that designates a constant. */
13229 static dw_loc_descr_ref
13230 int_loc_descriptor (HOST_WIDE_INT i)
13232 enum dwarf_location_atom op;
13234 /* Pick the smallest representation of a constant, rather than just
13235 defaulting to the LEB encoding. */
13236 if (i >= 0)
13238 int clz = clz_hwi (i);
13239 int ctz = ctz_hwi (i);
13240 if (i <= 31)
13241 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13242 else if (i <= 0xff)
13243 op = DW_OP_const1u;
13244 else if (i <= 0xffff)
13245 op = DW_OP_const2u;
13246 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13247 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13248 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13249 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13250 while DW_OP_const4u is 5 bytes. */
13251 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13252 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13253 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13254 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13255 while DW_OP_const4u is 5 bytes. */
13256 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13258 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13259 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13260 <= 4)
13262 /* As i >= 2**31, the double cast above will yield a negative number.
13263 Since wrapping is defined in DWARF expressions we can output big
13264 positive integers as small negative ones, regardless of the size
13265 of host wide ints.
13267 Here, since the evaluator will handle 32-bit values and since i >=
13268 2**31, we know it's going to be interpreted as a negative literal:
13269 store it this way if we can do better than 5 bytes this way. */
13270 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13272 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13273 op = DW_OP_const4u;
13275 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13276 least 6 bytes: see if we can do better before falling back to it. */
13277 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13278 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13279 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13280 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13281 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13282 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13283 >= HOST_BITS_PER_WIDE_INT)
13284 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13285 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13286 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13287 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13288 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13289 && size_of_uleb128 (i) > 6)
13290 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13291 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13292 else
13293 op = DW_OP_constu;
13295 else
13297 if (i >= -0x80)
13298 op = DW_OP_const1s;
13299 else if (i >= -0x8000)
13300 op = DW_OP_const2s;
13301 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13303 if (size_of_int_loc_descriptor (i) < 5)
13305 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13306 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13307 return ret;
13309 op = DW_OP_const4s;
13311 else
13313 if (size_of_int_loc_descriptor (i)
13314 < (unsigned long) 1 + size_of_sleb128 (i))
13316 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13317 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13318 return ret;
13320 op = DW_OP_consts;
13324 return new_loc_descr (op, i, 0);
13327 /* Likewise, for unsigned constants. */
13329 static dw_loc_descr_ref
13330 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13332 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13333 const unsigned HOST_WIDE_INT max_uint
13334 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13336 /* If possible, use the clever signed constants handling. */
13337 if (i <= max_int)
13338 return int_loc_descriptor ((HOST_WIDE_INT) i);
13340 /* Here, we are left with positive numbers that cannot be represented as
13341 HOST_WIDE_INT, i.e.:
13342 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13344 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13345 whereas may be better to output a negative integer: thanks to integer
13346 wrapping, we know that:
13347 x = x - 2 ** DWARF2_ADDR_SIZE
13348 = x - 2 * (max (HOST_WIDE_INT) + 1)
13349 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13350 small negative integers. Let's try that in cases it will clearly improve
13351 the encoding: there is no gain turning DW_OP_const4u into
13352 DW_OP_const4s. */
13353 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13354 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13355 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13357 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13359 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13360 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13361 const HOST_WIDE_INT second_shift
13362 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13364 /* So we finally have:
13365 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13366 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13367 return int_loc_descriptor (second_shift);
13370 /* Last chance: fallback to a simple constant operation. */
13371 return new_loc_descr
13372 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13373 ? DW_OP_const4u
13374 : DW_OP_const8u,
13375 i, 0);
13378 /* Generate and return a location description that computes the unsigned
13379 comparison of the two stack top entries (a OP b where b is the top-most
13380 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13381 LE_EXPR, GT_EXPR or GE_EXPR. */
13383 static dw_loc_descr_ref
13384 uint_comparison_loc_list (enum tree_code kind)
13386 enum dwarf_location_atom op, flip_op;
13387 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13389 switch (kind)
13391 case LT_EXPR:
13392 op = DW_OP_lt;
13393 break;
13394 case LE_EXPR:
13395 op = DW_OP_le;
13396 break;
13397 case GT_EXPR:
13398 op = DW_OP_gt;
13399 break;
13400 case GE_EXPR:
13401 op = DW_OP_ge;
13402 break;
13403 default:
13404 gcc_unreachable ();
13407 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13408 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13410 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13411 possible to perform unsigned comparisons: we just have to distinguish
13412 three cases:
13414 1. when a and b have the same sign (as signed integers); then we should
13415 return: a OP(signed) b;
13417 2. when a is a negative signed integer while b is a positive one, then a
13418 is a greater unsigned integer than b; likewise when a and b's roles
13419 are flipped.
13421 So first, compare the sign of the two operands. */
13422 ret = new_loc_descr (DW_OP_over, 0, 0);
13423 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13424 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13425 /* If they have different signs (i.e. they have different sign bits), then
13426 the stack top value has now the sign bit set and thus it's smaller than
13427 zero. */
13428 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13429 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13430 add_loc_descr (&ret, bra_node);
13432 /* We are in case 1. At this point, we know both operands have the same
13433 sign, to it's safe to use the built-in signed comparison. */
13434 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13435 add_loc_descr (&ret, jmp_node);
13437 /* We are in case 2. Here, we know both operands do not have the same sign,
13438 so we have to flip the signed comparison. */
13439 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13440 tmp = new_loc_descr (flip_op, 0, 0);
13441 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13442 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13443 add_loc_descr (&ret, tmp);
13445 /* This dummy operation is necessary to make the two branches join. */
13446 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13447 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13448 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13449 add_loc_descr (&ret, tmp);
13451 return ret;
13454 /* Likewise, but takes the location description lists (might be destructive on
13455 them). Return NULL if either is NULL or if concatenation fails. */
13457 static dw_loc_list_ref
13458 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13459 enum tree_code kind)
13461 if (left == NULL || right == NULL)
13462 return NULL;
13464 add_loc_list (&left, right);
13465 if (left == NULL)
13466 return NULL;
13468 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13469 return left;
13472 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13473 without actually allocating it. */
13475 static unsigned long
13476 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13478 return size_of_int_loc_descriptor (i >> shift)
13479 + size_of_int_loc_descriptor (shift)
13480 + 1;
13483 /* Return size_of_locs (int_loc_descriptor (i)) without
13484 actually allocating it. */
13486 static unsigned long
13487 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13489 unsigned long s;
13491 if (i >= 0)
13493 int clz, ctz;
13494 if (i <= 31)
13495 return 1;
13496 else if (i <= 0xff)
13497 return 2;
13498 else if (i <= 0xffff)
13499 return 3;
13500 clz = clz_hwi (i);
13501 ctz = ctz_hwi (i);
13502 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13503 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13504 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13505 - clz - 5);
13506 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13507 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13508 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13509 - clz - 8);
13510 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13511 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13512 <= 4)
13513 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13514 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13515 return 5;
13516 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13517 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13518 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13519 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13520 - clz - 8);
13521 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13522 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13523 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13524 - clz - 16);
13525 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13526 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13527 && s > 6)
13528 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13529 - clz - 32);
13530 else
13531 return 1 + s;
13533 else
13535 if (i >= -0x80)
13536 return 2;
13537 else if (i >= -0x8000)
13538 return 3;
13539 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13541 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13543 s = size_of_int_loc_descriptor (-i) + 1;
13544 if (s < 5)
13545 return s;
13547 return 5;
13549 else
13551 unsigned long r = 1 + size_of_sleb128 (i);
13552 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13554 s = size_of_int_loc_descriptor (-i) + 1;
13555 if (s < r)
13556 return s;
13558 return r;
13563 /* Return loc description representing "address" of integer value.
13564 This can appear only as toplevel expression. */
13566 static dw_loc_descr_ref
13567 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13569 int litsize;
13570 dw_loc_descr_ref loc_result = NULL;
13572 if (!(dwarf_version >= 4 || !dwarf_strict))
13573 return NULL;
13575 litsize = size_of_int_loc_descriptor (i);
13576 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13577 is more compact. For DW_OP_stack_value we need:
13578 litsize + 1 (DW_OP_stack_value)
13579 and for DW_OP_implicit_value:
13580 1 (DW_OP_implicit_value) + 1 (length) + size. */
13581 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13583 loc_result = int_loc_descriptor (i);
13584 add_loc_descr (&loc_result,
13585 new_loc_descr (DW_OP_stack_value, 0, 0));
13586 return loc_result;
13589 loc_result = new_loc_descr (DW_OP_implicit_value,
13590 size, 0);
13591 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13592 loc_result->dw_loc_oprnd2.v.val_int = i;
13593 return loc_result;
13596 /* Return a location descriptor that designates a base+offset location. */
13598 static dw_loc_descr_ref
13599 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13600 enum var_init_status initialized)
13602 unsigned int regno;
13603 dw_loc_descr_ref result;
13604 dw_fde_ref fde = cfun->fde;
13606 /* We only use "frame base" when we're sure we're talking about the
13607 post-prologue local stack frame. We do this by *not* running
13608 register elimination until this point, and recognizing the special
13609 argument pointer and soft frame pointer rtx's. */
13610 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13612 rtx elim = (ira_use_lra_p
13613 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13614 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13616 if (elim != reg)
13618 if (GET_CODE (elim) == PLUS)
13620 offset += INTVAL (XEXP (elim, 1));
13621 elim = XEXP (elim, 0);
13623 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13624 && (elim == hard_frame_pointer_rtx
13625 || elim == stack_pointer_rtx))
13626 || elim == (frame_pointer_needed
13627 ? hard_frame_pointer_rtx
13628 : stack_pointer_rtx));
13630 /* If drap register is used to align stack, use frame
13631 pointer + offset to access stack variables. If stack
13632 is aligned without drap, use stack pointer + offset to
13633 access stack variables. */
13634 if (crtl->stack_realign_tried
13635 && reg == frame_pointer_rtx)
13637 int base_reg
13638 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13639 ? HARD_FRAME_POINTER_REGNUM
13640 : REGNO (elim));
13641 return new_reg_loc_descr (base_reg, offset);
13644 gcc_assert (frame_pointer_fb_offset_valid);
13645 offset += frame_pointer_fb_offset;
13646 return new_loc_descr (DW_OP_fbreg, offset, 0);
13650 regno = REGNO (reg);
13651 #ifdef LEAF_REG_REMAP
13652 if (crtl->uses_only_leaf_regs)
13654 int leaf_reg = LEAF_REG_REMAP (regno);
13655 if (leaf_reg != -1)
13656 regno = (unsigned) leaf_reg;
13658 #endif
13659 regno = DWARF_FRAME_REGNUM (regno);
13661 if (!optimize && fde
13662 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13664 /* Use cfa+offset to represent the location of arguments passed
13665 on the stack when drap is used to align stack.
13666 Only do this when not optimizing, for optimized code var-tracking
13667 is supposed to track where the arguments live and the register
13668 used as vdrap or drap in some spot might be used for something
13669 else in other part of the routine. */
13670 return new_loc_descr (DW_OP_fbreg, offset, 0);
13673 if (regno <= 31)
13674 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13675 offset, 0);
13676 else
13677 result = new_loc_descr (DW_OP_bregx, regno, offset);
13679 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13680 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13682 return result;
13685 /* Return true if this RTL expression describes a base+offset calculation. */
13687 static inline int
13688 is_based_loc (const_rtx rtl)
13690 return (GET_CODE (rtl) == PLUS
13691 && ((REG_P (XEXP (rtl, 0))
13692 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13693 && CONST_INT_P (XEXP (rtl, 1)))));
13696 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13697 failed. */
13699 static dw_loc_descr_ref
13700 tls_mem_loc_descriptor (rtx mem)
13702 tree base;
13703 dw_loc_descr_ref loc_result;
13705 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13706 return NULL;
13708 base = get_base_address (MEM_EXPR (mem));
13709 if (base == NULL
13710 || !VAR_P (base)
13711 || !DECL_THREAD_LOCAL_P (base))
13712 return NULL;
13714 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13715 if (loc_result == NULL)
13716 return NULL;
13718 if (MEM_OFFSET (mem))
13719 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13721 return loc_result;
13724 /* Output debug info about reason why we failed to expand expression as dwarf
13725 expression. */
13727 static void
13728 expansion_failed (tree expr, rtx rtl, char const *reason)
13730 if (dump_file && (dump_flags & TDF_DETAILS))
13732 fprintf (dump_file, "Failed to expand as dwarf: ");
13733 if (expr)
13734 print_generic_expr (dump_file, expr, dump_flags);
13735 if (rtl)
13737 fprintf (dump_file, "\n");
13738 print_rtl (dump_file, rtl);
13740 fprintf (dump_file, "\nReason: %s\n", reason);
13744 /* Helper function for const_ok_for_output. */
13746 static bool
13747 const_ok_for_output_1 (rtx rtl)
13749 if (targetm.const_not_ok_for_debug_p (rtl))
13751 if (GET_CODE (rtl) != UNSPEC)
13753 expansion_failed (NULL_TREE, rtl,
13754 "Expression rejected for debug by the backend.\n");
13755 return false;
13758 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13759 the target hook doesn't explicitly allow it in debug info, assume
13760 we can't express it in the debug info. */
13761 /* Don't complain about TLS UNSPECs, those are just too hard to
13762 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13763 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13764 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13765 if (flag_checking
13766 && (XVECLEN (rtl, 0) == 0
13767 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13768 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13769 inform (current_function_decl
13770 ? DECL_SOURCE_LOCATION (current_function_decl)
13771 : UNKNOWN_LOCATION,
13772 #if NUM_UNSPEC_VALUES > 0
13773 "non-delegitimized UNSPEC %s (%d) found in variable location",
13774 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13775 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13776 XINT (rtl, 1));
13777 #else
13778 "non-delegitimized UNSPEC %d found in variable location",
13779 XINT (rtl, 1));
13780 #endif
13781 expansion_failed (NULL_TREE, rtl,
13782 "UNSPEC hasn't been delegitimized.\n");
13783 return false;
13786 /* FIXME: Refer to PR60655. It is possible for simplification
13787 of rtl expressions in var tracking to produce such expressions.
13788 We should really identify / validate expressions
13789 enclosed in CONST that can be handled by assemblers on various
13790 targets and only handle legitimate cases here. */
13791 switch (GET_CODE (rtl))
13793 case SYMBOL_REF:
13794 break;
13795 case NOT:
13796 case NEG:
13797 return false;
13798 default:
13799 return true;
13802 if (CONSTANT_POOL_ADDRESS_P (rtl))
13804 bool marked;
13805 get_pool_constant_mark (rtl, &marked);
13806 /* If all references to this pool constant were optimized away,
13807 it was not output and thus we can't represent it. */
13808 if (!marked)
13810 expansion_failed (NULL_TREE, rtl,
13811 "Constant was removed from constant pool.\n");
13812 return false;
13816 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13817 return false;
13819 /* Avoid references to external symbols in debug info, on several targets
13820 the linker might even refuse to link when linking a shared library,
13821 and in many other cases the relocations for .debug_info/.debug_loc are
13822 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13823 to be defined within the same shared library or executable are fine. */
13824 if (SYMBOL_REF_EXTERNAL_P (rtl))
13826 tree decl = SYMBOL_REF_DECL (rtl);
13828 if (decl == NULL || !targetm.binds_local_p (decl))
13830 expansion_failed (NULL_TREE, rtl,
13831 "Symbol not defined in current TU.\n");
13832 return false;
13836 return true;
13839 /* Return true if constant RTL can be emitted in DW_OP_addr or
13840 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13841 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13843 static bool
13844 const_ok_for_output (rtx rtl)
13846 if (GET_CODE (rtl) == SYMBOL_REF)
13847 return const_ok_for_output_1 (rtl);
13849 if (GET_CODE (rtl) == CONST)
13851 subrtx_var_iterator::array_type array;
13852 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13853 if (!const_ok_for_output_1 (*iter))
13854 return false;
13855 return true;
13858 return true;
13861 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13862 if possible, NULL otherwise. */
13864 static dw_die_ref
13865 base_type_for_mode (machine_mode mode, bool unsignedp)
13867 dw_die_ref type_die;
13868 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13870 if (type == NULL)
13871 return NULL;
13872 switch (TREE_CODE (type))
13874 case INTEGER_TYPE:
13875 case REAL_TYPE:
13876 break;
13877 default:
13878 return NULL;
13880 type_die = lookup_type_die (type);
13881 if (!type_die)
13882 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13883 comp_unit_die ());
13884 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13885 return NULL;
13886 return type_die;
13889 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13890 type matching MODE, or, if MODE is narrower than or as wide as
13891 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13892 possible. */
13894 static dw_loc_descr_ref
13895 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13897 machine_mode outer_mode = mode;
13898 dw_die_ref type_die;
13899 dw_loc_descr_ref cvt;
13901 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13903 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13904 return op;
13906 type_die = base_type_for_mode (outer_mode, 1);
13907 if (type_die == NULL)
13908 return NULL;
13909 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13913 add_loc_descr (&op, cvt);
13914 return op;
13917 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13919 static dw_loc_descr_ref
13920 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13921 dw_loc_descr_ref op1)
13923 dw_loc_descr_ref ret = op0;
13924 add_loc_descr (&ret, op1);
13925 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13926 if (STORE_FLAG_VALUE != 1)
13928 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13929 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13931 return ret;
13934 /* Subroutine of scompare_loc_descriptor for the case in which we're
13935 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13936 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13938 static dw_loc_descr_ref
13939 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13940 scalar_int_mode op_mode,
13941 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13943 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13944 dw_loc_descr_ref cvt;
13946 if (type_die == NULL)
13947 return NULL;
13948 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13949 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13950 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13951 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13952 add_loc_descr (&op0, cvt);
13953 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13954 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13955 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13956 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13957 add_loc_descr (&op1, cvt);
13958 return compare_loc_descriptor (op, op0, op1);
13961 /* Subroutine of scompare_loc_descriptor for the case in which we're
13962 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13963 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13965 static dw_loc_descr_ref
13966 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13967 scalar_int_mode op_mode,
13968 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13970 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13971 /* For eq/ne, if the operands are known to be zero-extended,
13972 there is no need to do the fancy shifting up. */
13973 if (op == DW_OP_eq || op == DW_OP_ne)
13975 dw_loc_descr_ref last0, last1;
13976 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13978 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13980 /* deref_size zero extends, and for constants we can check
13981 whether they are zero extended or not. */
13982 if (((last0->dw_loc_opc == DW_OP_deref_size
13983 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13984 || (CONST_INT_P (XEXP (rtl, 0))
13985 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13986 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13987 && ((last1->dw_loc_opc == DW_OP_deref_size
13988 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13989 || (CONST_INT_P (XEXP (rtl, 1))
13990 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13991 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13992 return compare_loc_descriptor (op, op0, op1);
13994 /* EQ/NE comparison against constant in narrower type than
13995 DWARF2_ADDR_SIZE can be performed either as
13996 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13997 DW_OP_{eq,ne}
13999 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14000 DW_OP_{eq,ne}. Pick whatever is shorter. */
14001 if (CONST_INT_P (XEXP (rtl, 1))
14002 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14003 && (size_of_int_loc_descriptor (shift) + 1
14004 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14005 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14006 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14007 & GET_MODE_MASK (op_mode))))
14009 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14010 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14011 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14012 & GET_MODE_MASK (op_mode));
14013 return compare_loc_descriptor (op, op0, op1);
14016 add_loc_descr (&op0, int_loc_descriptor (shift));
14017 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14018 if (CONST_INT_P (XEXP (rtl, 1)))
14019 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14020 else
14022 add_loc_descr (&op1, int_loc_descriptor (shift));
14023 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14025 return compare_loc_descriptor (op, op0, op1);
14028 /* Return location descriptor for unsigned comparison OP RTL. */
14030 static dw_loc_descr_ref
14031 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14032 machine_mode mem_mode)
14034 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14035 dw_loc_descr_ref op0, op1;
14037 if (op_mode == VOIDmode)
14038 op_mode = GET_MODE (XEXP (rtl, 1));
14039 if (op_mode == VOIDmode)
14040 return NULL;
14042 scalar_int_mode int_op_mode;
14043 if (dwarf_strict
14044 && dwarf_version < 5
14045 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14046 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14047 return NULL;
14049 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14050 VAR_INIT_STATUS_INITIALIZED);
14051 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14052 VAR_INIT_STATUS_INITIALIZED);
14054 if (op0 == NULL || op1 == NULL)
14055 return NULL;
14057 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14059 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14060 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14062 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14063 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14065 return compare_loc_descriptor (op, op0, op1);
14068 /* Return location descriptor for unsigned comparison OP RTL. */
14070 static dw_loc_descr_ref
14071 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14072 machine_mode mem_mode)
14074 dw_loc_descr_ref op0, op1;
14076 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14077 if (test_op_mode == VOIDmode)
14078 test_op_mode = GET_MODE (XEXP (rtl, 1));
14080 scalar_int_mode op_mode;
14081 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14082 return NULL;
14084 if (dwarf_strict
14085 && dwarf_version < 5
14086 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14087 return NULL;
14089 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14090 VAR_INIT_STATUS_INITIALIZED);
14091 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14092 VAR_INIT_STATUS_INITIALIZED);
14094 if (op0 == NULL || op1 == NULL)
14095 return NULL;
14097 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14099 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14100 dw_loc_descr_ref last0, last1;
14101 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14103 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14105 if (CONST_INT_P (XEXP (rtl, 0)))
14106 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14107 /* deref_size zero extends, so no need to mask it again. */
14108 else if (last0->dw_loc_opc != DW_OP_deref_size
14109 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14111 add_loc_descr (&op0, int_loc_descriptor (mask));
14112 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14114 if (CONST_INT_P (XEXP (rtl, 1)))
14115 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14116 /* deref_size zero extends, so no need to mask it again. */
14117 else if (last1->dw_loc_opc != DW_OP_deref_size
14118 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14120 add_loc_descr (&op1, int_loc_descriptor (mask));
14121 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14124 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14126 HOST_WIDE_INT bias = 1;
14127 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14128 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14129 if (CONST_INT_P (XEXP (rtl, 1)))
14130 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14131 + INTVAL (XEXP (rtl, 1)));
14132 else
14133 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14134 bias, 0));
14136 return compare_loc_descriptor (op, op0, op1);
14139 /* Return location descriptor for {U,S}{MIN,MAX}. */
14141 static dw_loc_descr_ref
14142 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14143 machine_mode mem_mode)
14145 enum dwarf_location_atom op;
14146 dw_loc_descr_ref op0, op1, ret;
14147 dw_loc_descr_ref bra_node, drop_node;
14149 scalar_int_mode int_mode;
14150 if (dwarf_strict
14151 && dwarf_version < 5
14152 && (!is_a <scalar_int_mode> (mode, &int_mode)
14153 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14154 return NULL;
14156 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14158 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14159 VAR_INIT_STATUS_INITIALIZED);
14161 if (op0 == NULL || op1 == NULL)
14162 return NULL;
14164 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14165 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14166 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14167 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14169 /* Checked by the caller. */
14170 int_mode = as_a <scalar_int_mode> (mode);
14171 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14173 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14174 add_loc_descr (&op0, int_loc_descriptor (mask));
14175 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14176 add_loc_descr (&op1, int_loc_descriptor (mask));
14177 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14179 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14181 HOST_WIDE_INT bias = 1;
14182 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14183 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14184 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14187 else if (is_a <scalar_int_mode> (mode, &int_mode)
14188 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14190 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14191 add_loc_descr (&op0, int_loc_descriptor (shift));
14192 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14193 add_loc_descr (&op1, int_loc_descriptor (shift));
14194 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14196 else if (is_a <scalar_int_mode> (mode, &int_mode)
14197 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14199 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14200 dw_loc_descr_ref cvt;
14201 if (type_die == NULL)
14202 return NULL;
14203 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14207 add_loc_descr (&op0, cvt);
14208 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14209 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14210 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14211 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14212 add_loc_descr (&op1, cvt);
14215 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14216 op = DW_OP_lt;
14217 else
14218 op = DW_OP_gt;
14219 ret = op0;
14220 add_loc_descr (&ret, op1);
14221 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14222 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14223 add_loc_descr (&ret, bra_node);
14224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14225 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14226 add_loc_descr (&ret, drop_node);
14227 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14228 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14229 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14230 && is_a <scalar_int_mode> (mode, &int_mode)
14231 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14232 ret = convert_descriptor_to_mode (int_mode, ret);
14233 return ret;
14236 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14237 but after converting arguments to type_die, afterwards
14238 convert back to unsigned. */
14240 static dw_loc_descr_ref
14241 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14242 scalar_int_mode mode, machine_mode mem_mode)
14244 dw_loc_descr_ref cvt, op0, op1;
14246 if (type_die == NULL)
14247 return NULL;
14248 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14249 VAR_INIT_STATUS_INITIALIZED);
14250 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14251 VAR_INIT_STATUS_INITIALIZED);
14252 if (op0 == NULL || op1 == NULL)
14253 return NULL;
14254 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14255 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14256 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14257 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14258 add_loc_descr (&op0, cvt);
14259 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14260 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14261 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14262 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14263 add_loc_descr (&op1, cvt);
14264 add_loc_descr (&op0, op1);
14265 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14266 return convert_descriptor_to_mode (mode, op0);
14269 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14270 const0 is DW_OP_lit0 or corresponding typed constant,
14271 const1 is DW_OP_lit1 or corresponding typed constant
14272 and constMSB is constant with just the MSB bit set
14273 for the mode):
14274 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14275 L1: const0 DW_OP_swap
14276 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14277 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14278 L3: DW_OP_drop
14279 L4: DW_OP_nop
14281 CTZ is similar:
14282 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14283 L1: const0 DW_OP_swap
14284 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14285 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14286 L3: DW_OP_drop
14287 L4: DW_OP_nop
14289 FFS is similar:
14290 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14291 L1: const1 DW_OP_swap
14292 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14293 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14294 L3: DW_OP_drop
14295 L4: DW_OP_nop */
14297 static dw_loc_descr_ref
14298 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14299 machine_mode mem_mode)
14301 dw_loc_descr_ref op0, ret, tmp;
14302 HOST_WIDE_INT valv;
14303 dw_loc_descr_ref l1jump, l1label;
14304 dw_loc_descr_ref l2jump, l2label;
14305 dw_loc_descr_ref l3jump, l3label;
14306 dw_loc_descr_ref l4jump, l4label;
14307 rtx msb;
14309 if (GET_MODE (XEXP (rtl, 0)) != mode)
14310 return NULL;
14312 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14313 VAR_INIT_STATUS_INITIALIZED);
14314 if (op0 == NULL)
14315 return NULL;
14316 ret = op0;
14317 if (GET_CODE (rtl) == CLZ)
14319 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14320 valv = GET_MODE_BITSIZE (mode);
14322 else if (GET_CODE (rtl) == FFS)
14323 valv = 0;
14324 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14325 valv = GET_MODE_BITSIZE (mode);
14326 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14327 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14328 add_loc_descr (&ret, l1jump);
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14330 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14331 VAR_INIT_STATUS_INITIALIZED);
14332 if (tmp == NULL)
14333 return NULL;
14334 add_loc_descr (&ret, tmp);
14335 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14336 add_loc_descr (&ret, l4jump);
14337 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14338 ? const1_rtx : const0_rtx,
14339 mode, mem_mode,
14340 VAR_INIT_STATUS_INITIALIZED);
14341 if (l1label == NULL)
14342 return NULL;
14343 add_loc_descr (&ret, l1label);
14344 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14345 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14346 add_loc_descr (&ret, l2label);
14347 if (GET_CODE (rtl) != CLZ)
14348 msb = const1_rtx;
14349 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14350 msb = GEN_INT (HOST_WIDE_INT_1U
14351 << (GET_MODE_BITSIZE (mode) - 1));
14352 else
14353 msb = immed_wide_int_const
14354 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14355 GET_MODE_PRECISION (mode)), mode);
14356 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14357 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14358 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14359 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14360 else
14361 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14362 VAR_INIT_STATUS_INITIALIZED);
14363 if (tmp == NULL)
14364 return NULL;
14365 add_loc_descr (&ret, tmp);
14366 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14367 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14368 add_loc_descr (&ret, l3jump);
14369 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14370 VAR_INIT_STATUS_INITIALIZED);
14371 if (tmp == NULL)
14372 return NULL;
14373 add_loc_descr (&ret, tmp);
14374 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14375 ? DW_OP_shl : DW_OP_shr, 0, 0));
14376 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14377 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14378 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14379 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14380 add_loc_descr (&ret, l2jump);
14381 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14382 add_loc_descr (&ret, l3label);
14383 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14384 add_loc_descr (&ret, l4label);
14385 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14386 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14387 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14388 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14389 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14390 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14391 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14392 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14393 return ret;
14396 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14397 const1 is DW_OP_lit1 or corresponding typed constant):
14398 const0 DW_OP_swap
14399 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14400 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14401 L2: DW_OP_drop
14403 PARITY is similar:
14404 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14405 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14406 L2: DW_OP_drop */
14408 static dw_loc_descr_ref
14409 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14410 machine_mode mem_mode)
14412 dw_loc_descr_ref op0, ret, tmp;
14413 dw_loc_descr_ref l1jump, l1label;
14414 dw_loc_descr_ref l2jump, l2label;
14416 if (GET_MODE (XEXP (rtl, 0)) != mode)
14417 return NULL;
14419 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14420 VAR_INIT_STATUS_INITIALIZED);
14421 if (op0 == NULL)
14422 return NULL;
14423 ret = op0;
14424 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14425 VAR_INIT_STATUS_INITIALIZED);
14426 if (tmp == NULL)
14427 return NULL;
14428 add_loc_descr (&ret, tmp);
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14430 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14431 add_loc_descr (&ret, l1label);
14432 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14433 add_loc_descr (&ret, l2jump);
14434 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14435 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14436 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14437 VAR_INIT_STATUS_INITIALIZED);
14438 if (tmp == NULL)
14439 return NULL;
14440 add_loc_descr (&ret, tmp);
14441 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14442 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14443 ? DW_OP_plus : DW_OP_xor, 0, 0));
14444 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14445 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14446 VAR_INIT_STATUS_INITIALIZED);
14447 add_loc_descr (&ret, tmp);
14448 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14449 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14450 add_loc_descr (&ret, l1jump);
14451 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14452 add_loc_descr (&ret, l2label);
14453 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14454 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14455 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14456 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14457 return ret;
14460 /* BSWAP (constS is initial shift count, either 56 or 24):
14461 constS const0
14462 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14463 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14464 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14465 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14466 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14468 static dw_loc_descr_ref
14469 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14470 machine_mode mem_mode)
14472 dw_loc_descr_ref op0, ret, tmp;
14473 dw_loc_descr_ref l1jump, l1label;
14474 dw_loc_descr_ref l2jump, l2label;
14476 if (BITS_PER_UNIT != 8
14477 || (GET_MODE_BITSIZE (mode) != 32
14478 && GET_MODE_BITSIZE (mode) != 64))
14479 return NULL;
14481 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14482 VAR_INIT_STATUS_INITIALIZED);
14483 if (op0 == NULL)
14484 return NULL;
14486 ret = op0;
14487 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14488 mode, mem_mode,
14489 VAR_INIT_STATUS_INITIALIZED);
14490 if (tmp == NULL)
14491 return NULL;
14492 add_loc_descr (&ret, tmp);
14493 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14494 VAR_INIT_STATUS_INITIALIZED);
14495 if (tmp == NULL)
14496 return NULL;
14497 add_loc_descr (&ret, tmp);
14498 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14499 add_loc_descr (&ret, l1label);
14500 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14501 mode, mem_mode,
14502 VAR_INIT_STATUS_INITIALIZED);
14503 add_loc_descr (&ret, tmp);
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14507 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14508 VAR_INIT_STATUS_INITIALIZED);
14509 if (tmp == NULL)
14510 return NULL;
14511 add_loc_descr (&ret, tmp);
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14515 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14517 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14518 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14519 VAR_INIT_STATUS_INITIALIZED);
14520 add_loc_descr (&ret, tmp);
14521 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14522 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14523 add_loc_descr (&ret, l2jump);
14524 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14525 VAR_INIT_STATUS_INITIALIZED);
14526 add_loc_descr (&ret, tmp);
14527 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14528 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14529 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14530 add_loc_descr (&ret, l1jump);
14531 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14532 add_loc_descr (&ret, l2label);
14533 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14534 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14535 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14536 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14537 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14538 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14539 return ret;
14542 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14543 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14544 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14545 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14547 ROTATERT is similar:
14548 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14549 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14550 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14552 static dw_loc_descr_ref
14553 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14554 machine_mode mem_mode)
14556 rtx rtlop1 = XEXP (rtl, 1);
14557 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14558 int i;
14560 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14561 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14562 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14563 VAR_INIT_STATUS_INITIALIZED);
14564 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14565 VAR_INIT_STATUS_INITIALIZED);
14566 if (op0 == NULL || op1 == NULL)
14567 return NULL;
14568 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14569 for (i = 0; i < 2; i++)
14571 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14572 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14573 mode, mem_mode,
14574 VAR_INIT_STATUS_INITIALIZED);
14575 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14576 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14577 ? DW_OP_const4u
14578 : HOST_BITS_PER_WIDE_INT == 64
14579 ? DW_OP_const8u : DW_OP_constu,
14580 GET_MODE_MASK (mode), 0);
14581 else
14582 mask[i] = NULL;
14583 if (mask[i] == NULL)
14584 return NULL;
14585 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14587 ret = op0;
14588 add_loc_descr (&ret, op1);
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14590 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14591 if (GET_CODE (rtl) == ROTATERT)
14593 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14594 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14595 GET_MODE_BITSIZE (mode), 0));
14597 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14598 if (mask[0] != NULL)
14599 add_loc_descr (&ret, mask[0]);
14600 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14601 if (mask[1] != NULL)
14603 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14604 add_loc_descr (&ret, mask[1]);
14605 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14607 if (GET_CODE (rtl) == ROTATE)
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14610 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14611 GET_MODE_BITSIZE (mode), 0));
14613 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14614 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14615 return ret;
14618 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14619 for DEBUG_PARAMETER_REF RTL. */
14621 static dw_loc_descr_ref
14622 parameter_ref_descriptor (rtx rtl)
14624 dw_loc_descr_ref ret;
14625 dw_die_ref ref;
14627 if (dwarf_strict)
14628 return NULL;
14629 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14630 /* With LTO during LTRANS we get the late DIE that refers to the early
14631 DIE, thus we add another indirection here. This seems to confuse
14632 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14633 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14634 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14635 if (ref)
14637 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14638 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14639 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14641 else
14643 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14644 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14646 return ret;
14649 /* The following routine converts the RTL for a variable or parameter
14650 (resident in memory) into an equivalent Dwarf representation of a
14651 mechanism for getting the address of that same variable onto the top of a
14652 hypothetical "address evaluation" stack.
14654 When creating memory location descriptors, we are effectively transforming
14655 the RTL for a memory-resident object into its Dwarf postfix expression
14656 equivalent. This routine recursively descends an RTL tree, turning
14657 it into Dwarf postfix code as it goes.
14659 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14661 MEM_MODE is the mode of the memory reference, needed to handle some
14662 autoincrement addressing modes.
14664 Return 0 if we can't represent the location. */
14666 dw_loc_descr_ref
14667 mem_loc_descriptor (rtx rtl, machine_mode mode,
14668 machine_mode mem_mode,
14669 enum var_init_status initialized)
14671 dw_loc_descr_ref mem_loc_result = NULL;
14672 enum dwarf_location_atom op;
14673 dw_loc_descr_ref op0, op1;
14674 rtx inner = NULL_RTX;
14676 if (mode == VOIDmode)
14677 mode = GET_MODE (rtl);
14679 /* Note that for a dynamically sized array, the location we will generate a
14680 description of here will be the lowest numbered location which is
14681 actually within the array. That's *not* necessarily the same as the
14682 zeroth element of the array. */
14684 rtl = targetm.delegitimize_address (rtl);
14686 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14687 return NULL;
14689 scalar_int_mode int_mode, inner_mode, op1_mode;
14690 switch (GET_CODE (rtl))
14692 case POST_INC:
14693 case POST_DEC:
14694 case POST_MODIFY:
14695 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14697 case SUBREG:
14698 /* The case of a subreg may arise when we have a local (register)
14699 variable or a formal (register) parameter which doesn't quite fill
14700 up an entire register. For now, just assume that it is
14701 legitimate to make the Dwarf info refer to the whole register which
14702 contains the given subreg. */
14703 if (!subreg_lowpart_p (rtl))
14704 break;
14705 inner = SUBREG_REG (rtl);
14706 /* FALLTHRU */
14707 case TRUNCATE:
14708 if (inner == NULL_RTX)
14709 inner = XEXP (rtl, 0);
14710 if (is_a <scalar_int_mode> (mode, &int_mode)
14711 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14712 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14713 #ifdef POINTERS_EXTEND_UNSIGNED
14714 || (int_mode == Pmode && mem_mode != VOIDmode)
14715 #endif
14717 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14719 mem_loc_result = mem_loc_descriptor (inner,
14720 inner_mode,
14721 mem_mode, initialized);
14722 break;
14724 if (dwarf_strict && dwarf_version < 5)
14725 break;
14726 if (is_a <scalar_int_mode> (mode, &int_mode)
14727 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14728 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14729 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14731 dw_die_ref type_die;
14732 dw_loc_descr_ref cvt;
14734 mem_loc_result = mem_loc_descriptor (inner,
14735 GET_MODE (inner),
14736 mem_mode, initialized);
14737 if (mem_loc_result == NULL)
14738 break;
14739 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14740 if (type_die == NULL)
14742 mem_loc_result = NULL;
14743 break;
14745 if (GET_MODE_SIZE (mode)
14746 != GET_MODE_SIZE (GET_MODE (inner)))
14747 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14748 else
14749 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14750 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14751 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14752 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14753 add_loc_descr (&mem_loc_result, cvt);
14754 if (is_a <scalar_int_mode> (mode, &int_mode)
14755 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14757 /* Convert it to untyped afterwards. */
14758 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14759 add_loc_descr (&mem_loc_result, cvt);
14762 break;
14764 case REG:
14765 if (!is_a <scalar_int_mode> (mode, &int_mode)
14766 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14767 && rtl != arg_pointer_rtx
14768 && rtl != frame_pointer_rtx
14769 #ifdef POINTERS_EXTEND_UNSIGNED
14770 && (int_mode != Pmode || mem_mode == VOIDmode)
14771 #endif
14774 dw_die_ref type_die;
14775 unsigned int dbx_regnum;
14777 if (dwarf_strict && dwarf_version < 5)
14778 break;
14779 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14780 break;
14781 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14782 if (type_die == NULL)
14783 break;
14785 dbx_regnum = dbx_reg_number (rtl);
14786 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14787 break;
14788 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14789 dbx_regnum, 0);
14790 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14791 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14792 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14793 break;
14795 /* Whenever a register number forms a part of the description of the
14796 method for calculating the (dynamic) address of a memory resident
14797 object, DWARF rules require the register number be referred to as
14798 a "base register". This distinction is not based in any way upon
14799 what category of register the hardware believes the given register
14800 belongs to. This is strictly DWARF terminology we're dealing with
14801 here. Note that in cases where the location of a memory-resident
14802 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14803 OP_CONST (0)) the actual DWARF location descriptor that we generate
14804 may just be OP_BASEREG (basereg). This may look deceptively like
14805 the object in question was allocated to a register (rather than in
14806 memory) so DWARF consumers need to be aware of the subtle
14807 distinction between OP_REG and OP_BASEREG. */
14808 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14809 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14810 else if (stack_realign_drap
14811 && crtl->drap_reg
14812 && crtl->args.internal_arg_pointer == rtl
14813 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14815 /* If RTL is internal_arg_pointer, which has been optimized
14816 out, use DRAP instead. */
14817 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14818 VAR_INIT_STATUS_INITIALIZED);
14820 break;
14822 case SIGN_EXTEND:
14823 case ZERO_EXTEND:
14824 if (!is_a <scalar_int_mode> (mode, &int_mode)
14825 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14826 break;
14827 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14828 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14829 if (op0 == 0)
14830 break;
14831 else if (GET_CODE (rtl) == ZERO_EXTEND
14832 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14833 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14834 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14835 to expand zero extend as two shifts instead of
14836 masking. */
14837 && GET_MODE_SIZE (inner_mode) <= 4)
14839 mem_loc_result = op0;
14840 add_loc_descr (&mem_loc_result,
14841 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14842 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14844 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14846 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14847 shift *= BITS_PER_UNIT;
14848 if (GET_CODE (rtl) == SIGN_EXTEND)
14849 op = DW_OP_shra;
14850 else
14851 op = DW_OP_shr;
14852 mem_loc_result = op0;
14853 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14854 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14855 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14856 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14858 else if (!dwarf_strict || dwarf_version >= 5)
14860 dw_die_ref type_die1, type_die2;
14861 dw_loc_descr_ref cvt;
14863 type_die1 = base_type_for_mode (inner_mode,
14864 GET_CODE (rtl) == ZERO_EXTEND);
14865 if (type_die1 == NULL)
14866 break;
14867 type_die2 = base_type_for_mode (int_mode, 1);
14868 if (type_die2 == NULL)
14869 break;
14870 mem_loc_result = op0;
14871 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14872 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14873 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14874 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14875 add_loc_descr (&mem_loc_result, cvt);
14876 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14877 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14878 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14879 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14880 add_loc_descr (&mem_loc_result, cvt);
14882 break;
14884 case MEM:
14886 rtx new_rtl = avoid_constant_pool_reference (rtl);
14887 if (new_rtl != rtl)
14889 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14890 initialized);
14891 if (mem_loc_result != NULL)
14892 return mem_loc_result;
14895 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14896 get_address_mode (rtl), mode,
14897 VAR_INIT_STATUS_INITIALIZED);
14898 if (mem_loc_result == NULL)
14899 mem_loc_result = tls_mem_loc_descriptor (rtl);
14900 if (mem_loc_result != NULL)
14902 if (!is_a <scalar_int_mode> (mode, &int_mode)
14903 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14905 dw_die_ref type_die;
14906 dw_loc_descr_ref deref;
14908 if (dwarf_strict && dwarf_version < 5)
14909 return NULL;
14910 type_die
14911 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14912 if (type_die == NULL)
14913 return NULL;
14914 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14915 GET_MODE_SIZE (mode), 0);
14916 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14917 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14918 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14919 add_loc_descr (&mem_loc_result, deref);
14921 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14922 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14923 else
14924 add_loc_descr (&mem_loc_result,
14925 new_loc_descr (DW_OP_deref_size,
14926 GET_MODE_SIZE (int_mode), 0));
14928 break;
14930 case LO_SUM:
14931 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14933 case LABEL_REF:
14934 /* Some ports can transform a symbol ref into a label ref, because
14935 the symbol ref is too far away and has to be dumped into a constant
14936 pool. */
14937 case CONST:
14938 case SYMBOL_REF:
14939 if (!is_a <scalar_int_mode> (mode, &int_mode)
14940 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14941 #ifdef POINTERS_EXTEND_UNSIGNED
14942 && (int_mode != Pmode || mem_mode == VOIDmode)
14943 #endif
14945 break;
14946 if (GET_CODE (rtl) == SYMBOL_REF
14947 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14949 dw_loc_descr_ref temp;
14951 /* If this is not defined, we have no way to emit the data. */
14952 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14953 break;
14955 temp = new_addr_loc_descr (rtl, dtprel_true);
14957 /* We check for DWARF 5 here because gdb did not implement
14958 DW_OP_form_tls_address until after 7.12. */
14959 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14960 ? DW_OP_form_tls_address
14961 : DW_OP_GNU_push_tls_address),
14962 0, 0);
14963 add_loc_descr (&mem_loc_result, temp);
14965 break;
14968 if (!const_ok_for_output (rtl))
14970 if (GET_CODE (rtl) == CONST)
14971 switch (GET_CODE (XEXP (rtl, 0)))
14973 case NOT:
14974 op = DW_OP_not;
14975 goto try_const_unop;
14976 case NEG:
14977 op = DW_OP_neg;
14978 goto try_const_unop;
14979 try_const_unop:
14980 rtx arg;
14981 arg = XEXP (XEXP (rtl, 0), 0);
14982 if (!CONSTANT_P (arg))
14983 arg = gen_rtx_CONST (int_mode, arg);
14984 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14985 initialized);
14986 if (op0)
14988 mem_loc_result = op0;
14989 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14991 break;
14992 default:
14993 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14994 mem_mode, initialized);
14995 break;
14997 break;
15000 symref:
15001 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15002 vec_safe_push (used_rtx_array, rtl);
15003 break;
15005 case CONCAT:
15006 case CONCATN:
15007 case VAR_LOCATION:
15008 case DEBUG_IMPLICIT_PTR:
15009 expansion_failed (NULL_TREE, rtl,
15010 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15011 return 0;
15013 case ENTRY_VALUE:
15014 if (dwarf_strict && dwarf_version < 5)
15015 return NULL;
15016 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15018 if (!is_a <scalar_int_mode> (mode, &int_mode)
15019 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15020 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15021 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15022 else
15024 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15025 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15026 return NULL;
15027 op0 = one_reg_loc_descriptor (dbx_regnum,
15028 VAR_INIT_STATUS_INITIALIZED);
15031 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15032 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15034 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15035 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15036 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15037 return NULL;
15039 else
15040 gcc_unreachable ();
15041 if (op0 == NULL)
15042 return NULL;
15043 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15044 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15045 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15046 break;
15048 case DEBUG_PARAMETER_REF:
15049 mem_loc_result = parameter_ref_descriptor (rtl);
15050 break;
15052 case PRE_MODIFY:
15053 /* Extract the PLUS expression nested inside and fall into
15054 PLUS code below. */
15055 rtl = XEXP (rtl, 1);
15056 goto plus;
15058 case PRE_INC:
15059 case PRE_DEC:
15060 /* Turn these into a PLUS expression and fall into the PLUS code
15061 below. */
15062 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15063 gen_int_mode (GET_CODE (rtl) == PRE_INC
15064 ? GET_MODE_UNIT_SIZE (mem_mode)
15065 : -GET_MODE_UNIT_SIZE (mem_mode),
15066 mode));
15068 /* fall through */
15070 case PLUS:
15071 plus:
15072 if (is_based_loc (rtl)
15073 && is_a <scalar_int_mode> (mode, &int_mode)
15074 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15075 || XEXP (rtl, 0) == arg_pointer_rtx
15076 || XEXP (rtl, 0) == frame_pointer_rtx))
15077 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15078 INTVAL (XEXP (rtl, 1)),
15079 VAR_INIT_STATUS_INITIALIZED);
15080 else
15082 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15083 VAR_INIT_STATUS_INITIALIZED);
15084 if (mem_loc_result == 0)
15085 break;
15087 if (CONST_INT_P (XEXP (rtl, 1))
15088 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15089 <= DWARF2_ADDR_SIZE))
15090 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15091 else
15093 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15094 VAR_INIT_STATUS_INITIALIZED);
15095 if (op1 == 0)
15096 return NULL;
15097 add_loc_descr (&mem_loc_result, op1);
15098 add_loc_descr (&mem_loc_result,
15099 new_loc_descr (DW_OP_plus, 0, 0));
15102 break;
15104 /* If a pseudo-reg is optimized away, it is possible for it to
15105 be replaced with a MEM containing a multiply or shift. */
15106 case MINUS:
15107 op = DW_OP_minus;
15108 goto do_binop;
15110 case MULT:
15111 op = DW_OP_mul;
15112 goto do_binop;
15114 case DIV:
15115 if ((!dwarf_strict || dwarf_version >= 5)
15116 && is_a <scalar_int_mode> (mode, &int_mode)
15117 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15119 mem_loc_result = typed_binop (DW_OP_div, rtl,
15120 base_type_for_mode (mode, 0),
15121 int_mode, mem_mode);
15122 break;
15124 op = DW_OP_div;
15125 goto do_binop;
15127 case UMOD:
15128 op = DW_OP_mod;
15129 goto do_binop;
15131 case ASHIFT:
15132 op = DW_OP_shl;
15133 goto do_shift;
15135 case ASHIFTRT:
15136 op = DW_OP_shra;
15137 goto do_shift;
15139 case LSHIFTRT:
15140 op = DW_OP_shr;
15141 goto do_shift;
15143 do_shift:
15144 if (!is_a <scalar_int_mode> (mode, &int_mode))
15145 break;
15146 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15147 VAR_INIT_STATUS_INITIALIZED);
15149 rtx rtlop1 = XEXP (rtl, 1);
15150 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15151 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15152 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15153 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15154 VAR_INIT_STATUS_INITIALIZED);
15157 if (op0 == 0 || op1 == 0)
15158 break;
15160 mem_loc_result = op0;
15161 add_loc_descr (&mem_loc_result, op1);
15162 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15163 break;
15165 case AND:
15166 op = DW_OP_and;
15167 goto do_binop;
15169 case IOR:
15170 op = DW_OP_or;
15171 goto do_binop;
15173 case XOR:
15174 op = DW_OP_xor;
15175 goto do_binop;
15177 do_binop:
15178 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15179 VAR_INIT_STATUS_INITIALIZED);
15180 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15181 VAR_INIT_STATUS_INITIALIZED);
15183 if (op0 == 0 || op1 == 0)
15184 break;
15186 mem_loc_result = op0;
15187 add_loc_descr (&mem_loc_result, op1);
15188 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15189 break;
15191 case MOD:
15192 if ((!dwarf_strict || dwarf_version >= 5)
15193 && is_a <scalar_int_mode> (mode, &int_mode)
15194 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15196 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15197 base_type_for_mode (mode, 0),
15198 int_mode, mem_mode);
15199 break;
15202 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15203 VAR_INIT_STATUS_INITIALIZED);
15204 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15205 VAR_INIT_STATUS_INITIALIZED);
15207 if (op0 == 0 || op1 == 0)
15208 break;
15210 mem_loc_result = op0;
15211 add_loc_descr (&mem_loc_result, op1);
15212 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15213 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15214 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15215 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15216 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15217 break;
15219 case UDIV:
15220 if ((!dwarf_strict || dwarf_version >= 5)
15221 && is_a <scalar_int_mode> (mode, &int_mode))
15223 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15225 op = DW_OP_div;
15226 goto do_binop;
15228 mem_loc_result = typed_binop (DW_OP_div, rtl,
15229 base_type_for_mode (int_mode, 1),
15230 int_mode, mem_mode);
15232 break;
15234 case NOT:
15235 op = DW_OP_not;
15236 goto do_unop;
15238 case ABS:
15239 op = DW_OP_abs;
15240 goto do_unop;
15242 case NEG:
15243 op = DW_OP_neg;
15244 goto do_unop;
15246 do_unop:
15247 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15248 VAR_INIT_STATUS_INITIALIZED);
15250 if (op0 == 0)
15251 break;
15253 mem_loc_result = op0;
15254 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15255 break;
15257 case CONST_INT:
15258 if (!is_a <scalar_int_mode> (mode, &int_mode)
15259 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15260 #ifdef POINTERS_EXTEND_UNSIGNED
15261 || (int_mode == Pmode
15262 && mem_mode != VOIDmode
15263 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15264 #endif
15267 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15268 break;
15270 if ((!dwarf_strict || dwarf_version >= 5)
15271 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15272 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15274 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15275 scalar_int_mode amode;
15276 if (type_die == NULL)
15277 return NULL;
15278 if (INTVAL (rtl) >= 0
15279 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15280 .exists (&amode))
15281 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15282 /* const DW_OP_convert <XXX> vs.
15283 DW_OP_const_type <XXX, 1, const>. */
15284 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15285 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15287 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15288 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15289 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15290 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15291 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15292 add_loc_descr (&mem_loc_result, op0);
15293 return mem_loc_result;
15295 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15296 INTVAL (rtl));
15297 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15298 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15299 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15300 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15301 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15302 else
15304 mem_loc_result->dw_loc_oprnd2.val_class
15305 = dw_val_class_const_double;
15306 mem_loc_result->dw_loc_oprnd2.v.val_double
15307 = double_int::from_shwi (INTVAL (rtl));
15310 break;
15312 case CONST_DOUBLE:
15313 if (!dwarf_strict || dwarf_version >= 5)
15315 dw_die_ref type_die;
15317 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15318 CONST_DOUBLE rtx could represent either a large integer
15319 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15320 the value is always a floating point constant.
15322 When it is an integer, a CONST_DOUBLE is used whenever
15323 the constant requires 2 HWIs to be adequately represented.
15324 We output CONST_DOUBLEs as blocks. */
15325 if (mode == VOIDmode
15326 || (GET_MODE (rtl) == VOIDmode
15327 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15328 break;
15329 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15330 if (type_die == NULL)
15331 return NULL;
15332 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15333 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15334 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15335 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15336 #if TARGET_SUPPORTS_WIDE_INT == 0
15337 if (!SCALAR_FLOAT_MODE_P (mode))
15339 mem_loc_result->dw_loc_oprnd2.val_class
15340 = dw_val_class_const_double;
15341 mem_loc_result->dw_loc_oprnd2.v.val_double
15342 = rtx_to_double_int (rtl);
15344 else
15345 #endif
15347 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15348 unsigned int length = GET_MODE_SIZE (float_mode);
15349 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15351 insert_float (rtl, array);
15352 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15353 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15354 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15355 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15358 break;
15360 case CONST_WIDE_INT:
15361 if (!dwarf_strict || dwarf_version >= 5)
15363 dw_die_ref type_die;
15365 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15366 if (type_die == NULL)
15367 return NULL;
15368 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15369 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15370 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15371 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15372 mem_loc_result->dw_loc_oprnd2.val_class
15373 = dw_val_class_wide_int;
15374 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15375 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15377 break;
15379 case EQ:
15380 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15381 break;
15383 case GE:
15384 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15385 break;
15387 case GT:
15388 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15389 break;
15391 case LE:
15392 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15393 break;
15395 case LT:
15396 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15397 break;
15399 case NE:
15400 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15401 break;
15403 case GEU:
15404 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15405 break;
15407 case GTU:
15408 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15409 break;
15411 case LEU:
15412 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15413 break;
15415 case LTU:
15416 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15417 break;
15419 case UMIN:
15420 case UMAX:
15421 if (!SCALAR_INT_MODE_P (mode))
15422 break;
15423 /* FALLTHRU */
15424 case SMIN:
15425 case SMAX:
15426 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15427 break;
15429 case ZERO_EXTRACT:
15430 case SIGN_EXTRACT:
15431 if (CONST_INT_P (XEXP (rtl, 1))
15432 && CONST_INT_P (XEXP (rtl, 2))
15433 && is_a <scalar_int_mode> (mode, &int_mode)
15434 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15435 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15436 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15437 && ((unsigned) INTVAL (XEXP (rtl, 1))
15438 + (unsigned) INTVAL (XEXP (rtl, 2))
15439 <= GET_MODE_BITSIZE (int_mode)))
15441 int shift, size;
15442 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15443 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15444 if (op0 == 0)
15445 break;
15446 if (GET_CODE (rtl) == SIGN_EXTRACT)
15447 op = DW_OP_shra;
15448 else
15449 op = DW_OP_shr;
15450 mem_loc_result = op0;
15451 size = INTVAL (XEXP (rtl, 1));
15452 shift = INTVAL (XEXP (rtl, 2));
15453 if (BITS_BIG_ENDIAN)
15454 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15455 if (shift + size != (int) DWARF2_ADDR_SIZE)
15457 add_loc_descr (&mem_loc_result,
15458 int_loc_descriptor (DWARF2_ADDR_SIZE
15459 - shift - size));
15460 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15462 if (size != (int) DWARF2_ADDR_SIZE)
15464 add_loc_descr (&mem_loc_result,
15465 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15466 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15469 break;
15471 case IF_THEN_ELSE:
15473 dw_loc_descr_ref op2, bra_node, drop_node;
15474 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15475 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15476 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15477 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15478 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15479 VAR_INIT_STATUS_INITIALIZED);
15480 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15481 VAR_INIT_STATUS_INITIALIZED);
15482 if (op0 == NULL || op1 == NULL || op2 == NULL)
15483 break;
15485 mem_loc_result = op1;
15486 add_loc_descr (&mem_loc_result, op2);
15487 add_loc_descr (&mem_loc_result, op0);
15488 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15489 add_loc_descr (&mem_loc_result, bra_node);
15490 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15491 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15492 add_loc_descr (&mem_loc_result, drop_node);
15493 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15494 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15496 break;
15498 case FLOAT_EXTEND:
15499 case FLOAT_TRUNCATE:
15500 case FLOAT:
15501 case UNSIGNED_FLOAT:
15502 case FIX:
15503 case UNSIGNED_FIX:
15504 if (!dwarf_strict || dwarf_version >= 5)
15506 dw_die_ref type_die;
15507 dw_loc_descr_ref cvt;
15509 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15510 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15511 if (op0 == NULL)
15512 break;
15513 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15514 && (GET_CODE (rtl) == FLOAT
15515 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15517 type_die = base_type_for_mode (int_mode,
15518 GET_CODE (rtl) == UNSIGNED_FLOAT);
15519 if (type_die == NULL)
15520 break;
15521 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15522 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15523 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15524 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15525 add_loc_descr (&op0, cvt);
15527 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15528 if (type_die == NULL)
15529 break;
15530 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15531 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15532 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15533 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15534 add_loc_descr (&op0, cvt);
15535 if (is_a <scalar_int_mode> (mode, &int_mode)
15536 && (GET_CODE (rtl) == FIX
15537 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15539 op0 = convert_descriptor_to_mode (int_mode, op0);
15540 if (op0 == NULL)
15541 break;
15543 mem_loc_result = op0;
15545 break;
15547 case CLZ:
15548 case CTZ:
15549 case FFS:
15550 if (is_a <scalar_int_mode> (mode, &int_mode))
15551 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15552 break;
15554 case POPCOUNT:
15555 case PARITY:
15556 if (is_a <scalar_int_mode> (mode, &int_mode))
15557 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15558 break;
15560 case BSWAP:
15561 if (is_a <scalar_int_mode> (mode, &int_mode))
15562 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15563 break;
15565 case ROTATE:
15566 case ROTATERT:
15567 if (is_a <scalar_int_mode> (mode, &int_mode))
15568 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15569 break;
15571 case COMPARE:
15572 /* In theory, we could implement the above. */
15573 /* DWARF cannot represent the unsigned compare operations
15574 natively. */
15575 case SS_MULT:
15576 case US_MULT:
15577 case SS_DIV:
15578 case US_DIV:
15579 case SS_PLUS:
15580 case US_PLUS:
15581 case SS_MINUS:
15582 case US_MINUS:
15583 case SS_NEG:
15584 case US_NEG:
15585 case SS_ABS:
15586 case SS_ASHIFT:
15587 case US_ASHIFT:
15588 case SS_TRUNCATE:
15589 case US_TRUNCATE:
15590 case UNORDERED:
15591 case ORDERED:
15592 case UNEQ:
15593 case UNGE:
15594 case UNGT:
15595 case UNLE:
15596 case UNLT:
15597 case LTGT:
15598 case FRACT_CONVERT:
15599 case UNSIGNED_FRACT_CONVERT:
15600 case SAT_FRACT:
15601 case UNSIGNED_SAT_FRACT:
15602 case SQRT:
15603 case ASM_OPERANDS:
15604 case VEC_MERGE:
15605 case VEC_SELECT:
15606 case VEC_CONCAT:
15607 case VEC_DUPLICATE:
15608 case UNSPEC:
15609 case HIGH:
15610 case FMA:
15611 case STRICT_LOW_PART:
15612 case CONST_VECTOR:
15613 case CONST_FIXED:
15614 case CLRSB:
15615 case CLOBBER:
15616 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15617 can't express it in the debug info. This can happen e.g. with some
15618 TLS UNSPECs. */
15619 break;
15621 case CONST_STRING:
15622 resolve_one_addr (&rtl);
15623 goto symref;
15625 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15626 the expression. An UNSPEC rtx represents a raw DWARF operation,
15627 new_loc_descr is called for it to build the operation directly.
15628 Otherwise mem_loc_descriptor is called recursively. */
15629 case PARALLEL:
15631 int index = 0;
15632 dw_loc_descr_ref exp_result = NULL;
15634 for (; index < XVECLEN (rtl, 0); index++)
15636 rtx elem = XVECEXP (rtl, 0, index);
15637 if (GET_CODE (elem) == UNSPEC)
15639 /* Each DWARF operation UNSPEC contain two operands, if
15640 one operand is not used for the operation, const0_rtx is
15641 passed. */
15642 gcc_assert (XVECLEN (elem, 0) == 2);
15644 HOST_WIDE_INT dw_op = XINT (elem, 1);
15645 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15646 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15647 exp_result
15648 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15649 oprnd2);
15651 else
15652 exp_result
15653 = mem_loc_descriptor (elem, mode, mem_mode,
15654 VAR_INIT_STATUS_INITIALIZED);
15656 if (!mem_loc_result)
15657 mem_loc_result = exp_result;
15658 else
15659 add_loc_descr (&mem_loc_result, exp_result);
15662 break;
15665 default:
15666 if (flag_checking)
15668 print_rtl (stderr, rtl);
15669 gcc_unreachable ();
15671 break;
15674 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15677 return mem_loc_result;
15680 /* Return a descriptor that describes the concatenation of two locations.
15681 This is typically a complex variable. */
15683 static dw_loc_descr_ref
15684 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15686 dw_loc_descr_ref cc_loc_result = NULL;
15687 dw_loc_descr_ref x0_ref
15688 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15689 dw_loc_descr_ref x1_ref
15690 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15692 if (x0_ref == 0 || x1_ref == 0)
15693 return 0;
15695 cc_loc_result = x0_ref;
15696 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15698 add_loc_descr (&cc_loc_result, x1_ref);
15699 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15701 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15702 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15704 return cc_loc_result;
15707 /* Return a descriptor that describes the concatenation of N
15708 locations. */
15710 static dw_loc_descr_ref
15711 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15713 unsigned int i;
15714 dw_loc_descr_ref cc_loc_result = NULL;
15715 unsigned int n = XVECLEN (concatn, 0);
15717 for (i = 0; i < n; ++i)
15719 dw_loc_descr_ref ref;
15720 rtx x = XVECEXP (concatn, 0, i);
15722 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15723 if (ref == NULL)
15724 return NULL;
15726 add_loc_descr (&cc_loc_result, ref);
15727 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15730 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15731 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15733 return cc_loc_result;
15736 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15737 for DEBUG_IMPLICIT_PTR RTL. */
15739 static dw_loc_descr_ref
15740 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15742 dw_loc_descr_ref ret;
15743 dw_die_ref ref;
15745 if (dwarf_strict && dwarf_version < 5)
15746 return NULL;
15747 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15748 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15749 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15750 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15751 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15752 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15753 if (ref)
15755 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15756 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15757 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15759 else
15761 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15762 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15764 return ret;
15767 /* Output a proper Dwarf location descriptor for a variable or parameter
15768 which is either allocated in a register or in a memory location. For a
15769 register, we just generate an OP_REG and the register number. For a
15770 memory location we provide a Dwarf postfix expression describing how to
15771 generate the (dynamic) address of the object onto the address stack.
15773 MODE is mode of the decl if this loc_descriptor is going to be used in
15774 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15775 allowed, VOIDmode otherwise.
15777 If we don't know how to describe it, return 0. */
15779 static dw_loc_descr_ref
15780 loc_descriptor (rtx rtl, machine_mode mode,
15781 enum var_init_status initialized)
15783 dw_loc_descr_ref loc_result = NULL;
15784 scalar_int_mode int_mode;
15786 switch (GET_CODE (rtl))
15788 case SUBREG:
15789 /* The case of a subreg may arise when we have a local (register)
15790 variable or a formal (register) parameter which doesn't quite fill
15791 up an entire register. For now, just assume that it is
15792 legitimate to make the Dwarf info refer to the whole register which
15793 contains the given subreg. */
15794 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15795 loc_result = loc_descriptor (SUBREG_REG (rtl),
15796 GET_MODE (SUBREG_REG (rtl)), initialized);
15797 else
15798 goto do_default;
15799 break;
15801 case REG:
15802 loc_result = reg_loc_descriptor (rtl, initialized);
15803 break;
15805 case MEM:
15806 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15807 GET_MODE (rtl), initialized);
15808 if (loc_result == NULL)
15809 loc_result = tls_mem_loc_descriptor (rtl);
15810 if (loc_result == NULL)
15812 rtx new_rtl = avoid_constant_pool_reference (rtl);
15813 if (new_rtl != rtl)
15814 loc_result = loc_descriptor (new_rtl, mode, initialized);
15816 break;
15818 case CONCAT:
15819 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15820 initialized);
15821 break;
15823 case CONCATN:
15824 loc_result = concatn_loc_descriptor (rtl, initialized);
15825 break;
15827 case VAR_LOCATION:
15828 /* Single part. */
15829 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15831 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15832 if (GET_CODE (loc) == EXPR_LIST)
15833 loc = XEXP (loc, 0);
15834 loc_result = loc_descriptor (loc, mode, initialized);
15835 break;
15838 rtl = XEXP (rtl, 1);
15839 /* FALLTHRU */
15841 case PARALLEL:
15843 rtvec par_elems = XVEC (rtl, 0);
15844 int num_elem = GET_NUM_ELEM (par_elems);
15845 machine_mode mode;
15846 int i;
15848 /* Create the first one, so we have something to add to. */
15849 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15850 VOIDmode, initialized);
15851 if (loc_result == NULL)
15852 return NULL;
15853 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15854 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15855 for (i = 1; i < num_elem; i++)
15857 dw_loc_descr_ref temp;
15859 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15860 VOIDmode, initialized);
15861 if (temp == NULL)
15862 return NULL;
15863 add_loc_descr (&loc_result, temp);
15864 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15865 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15868 break;
15870 case CONST_INT:
15871 if (mode != VOIDmode && mode != BLKmode)
15873 int_mode = as_a <scalar_int_mode> (mode);
15874 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15875 INTVAL (rtl));
15877 break;
15879 case CONST_DOUBLE:
15880 if (mode == VOIDmode)
15881 mode = GET_MODE (rtl);
15883 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15885 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15887 /* Note that a CONST_DOUBLE rtx could represent either an integer
15888 or a floating-point constant. A CONST_DOUBLE is used whenever
15889 the constant requires more than one word in order to be
15890 adequately represented. We output CONST_DOUBLEs as blocks. */
15891 scalar_mode smode = as_a <scalar_mode> (mode);
15892 loc_result = new_loc_descr (DW_OP_implicit_value,
15893 GET_MODE_SIZE (smode), 0);
15894 #if TARGET_SUPPORTS_WIDE_INT == 0
15895 if (!SCALAR_FLOAT_MODE_P (smode))
15897 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15898 loc_result->dw_loc_oprnd2.v.val_double
15899 = rtx_to_double_int (rtl);
15901 else
15902 #endif
15904 unsigned int length = GET_MODE_SIZE (smode);
15905 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15907 insert_float (rtl, array);
15908 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15909 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15910 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15911 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15914 break;
15916 case CONST_WIDE_INT:
15917 if (mode == VOIDmode)
15918 mode = GET_MODE (rtl);
15920 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15922 int_mode = as_a <scalar_int_mode> (mode);
15923 loc_result = new_loc_descr (DW_OP_implicit_value,
15924 GET_MODE_SIZE (int_mode), 0);
15925 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15926 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15927 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15929 break;
15931 case CONST_VECTOR:
15932 if (mode == VOIDmode)
15933 mode = GET_MODE (rtl);
15935 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15937 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15938 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15939 unsigned char *array
15940 = ggc_vec_alloc<unsigned char> (length * elt_size);
15941 unsigned int i;
15942 unsigned char *p;
15943 machine_mode imode = GET_MODE_INNER (mode);
15945 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15946 switch (GET_MODE_CLASS (mode))
15948 case MODE_VECTOR_INT:
15949 for (i = 0, p = array; i < length; i++, p += elt_size)
15951 rtx elt = CONST_VECTOR_ELT (rtl, i);
15952 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15954 break;
15956 case MODE_VECTOR_FLOAT:
15957 for (i = 0, p = array; i < length; i++, p += elt_size)
15959 rtx elt = CONST_VECTOR_ELT (rtl, i);
15960 insert_float (elt, p);
15962 break;
15964 default:
15965 gcc_unreachable ();
15968 loc_result = new_loc_descr (DW_OP_implicit_value,
15969 length * elt_size, 0);
15970 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15971 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15972 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15973 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15975 break;
15977 case CONST:
15978 if (mode == VOIDmode
15979 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15980 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15981 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15983 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15984 break;
15986 /* FALLTHROUGH */
15987 case SYMBOL_REF:
15988 if (!const_ok_for_output (rtl))
15989 break;
15990 /* FALLTHROUGH */
15991 case LABEL_REF:
15992 if (is_a <scalar_int_mode> (mode, &int_mode)
15993 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15994 && (dwarf_version >= 4 || !dwarf_strict))
15996 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15997 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15998 vec_safe_push (used_rtx_array, rtl);
16000 break;
16002 case DEBUG_IMPLICIT_PTR:
16003 loc_result = implicit_ptr_descriptor (rtl, 0);
16004 break;
16006 case PLUS:
16007 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16008 && CONST_INT_P (XEXP (rtl, 1)))
16010 loc_result
16011 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16012 break;
16014 /* FALLTHRU */
16015 do_default:
16016 default:
16017 if ((is_a <scalar_int_mode> (mode, &int_mode)
16018 && GET_MODE (rtl) == int_mode
16019 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16020 && dwarf_version >= 4)
16021 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16023 /* Value expression. */
16024 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16025 if (loc_result)
16026 add_loc_descr (&loc_result,
16027 new_loc_descr (DW_OP_stack_value, 0, 0));
16029 break;
16032 return loc_result;
16035 /* We need to figure out what section we should use as the base for the
16036 address ranges where a given location is valid.
16037 1. If this particular DECL has a section associated with it, use that.
16038 2. If this function has a section associated with it, use that.
16039 3. Otherwise, use the text section.
16040 XXX: If you split a variable across multiple sections, we won't notice. */
16042 static const char *
16043 secname_for_decl (const_tree decl)
16045 const char *secname;
16047 if (VAR_OR_FUNCTION_DECL_P (decl)
16048 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16049 && DECL_SECTION_NAME (decl))
16050 secname = DECL_SECTION_NAME (decl);
16051 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16052 secname = DECL_SECTION_NAME (current_function_decl);
16053 else if (cfun && in_cold_section_p)
16054 secname = crtl->subsections.cold_section_label;
16055 else
16056 secname = text_section_label;
16058 return secname;
16061 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16063 static bool
16064 decl_by_reference_p (tree decl)
16066 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16067 || VAR_P (decl))
16068 && DECL_BY_REFERENCE (decl));
16071 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16072 for VARLOC. */
16074 static dw_loc_descr_ref
16075 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16076 enum var_init_status initialized)
16078 int have_address = 0;
16079 dw_loc_descr_ref descr;
16080 machine_mode mode;
16082 if (want_address != 2)
16084 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16085 /* Single part. */
16086 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16088 varloc = PAT_VAR_LOCATION_LOC (varloc);
16089 if (GET_CODE (varloc) == EXPR_LIST)
16090 varloc = XEXP (varloc, 0);
16091 mode = GET_MODE (varloc);
16092 if (MEM_P (varloc))
16094 rtx addr = XEXP (varloc, 0);
16095 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16096 mode, initialized);
16097 if (descr)
16098 have_address = 1;
16099 else
16101 rtx x = avoid_constant_pool_reference (varloc);
16102 if (x != varloc)
16103 descr = mem_loc_descriptor (x, mode, VOIDmode,
16104 initialized);
16107 else
16108 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16110 else
16111 return 0;
16113 else
16115 if (GET_CODE (varloc) == VAR_LOCATION)
16116 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16117 else
16118 mode = DECL_MODE (loc);
16119 descr = loc_descriptor (varloc, mode, initialized);
16120 have_address = 1;
16123 if (!descr)
16124 return 0;
16126 if (want_address == 2 && !have_address
16127 && (dwarf_version >= 4 || !dwarf_strict))
16129 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16131 expansion_failed (loc, NULL_RTX,
16132 "DWARF address size mismatch");
16133 return 0;
16135 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16136 have_address = 1;
16138 /* Show if we can't fill the request for an address. */
16139 if (want_address && !have_address)
16141 expansion_failed (loc, NULL_RTX,
16142 "Want address and only have value");
16143 return 0;
16146 /* If we've got an address and don't want one, dereference. */
16147 if (!want_address && have_address)
16149 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16150 enum dwarf_location_atom op;
16152 if (size > DWARF2_ADDR_SIZE || size == -1)
16154 expansion_failed (loc, NULL_RTX,
16155 "DWARF address size mismatch");
16156 return 0;
16158 else if (size == DWARF2_ADDR_SIZE)
16159 op = DW_OP_deref;
16160 else
16161 op = DW_OP_deref_size;
16163 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16166 return descr;
16169 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16170 if it is not possible. */
16172 static dw_loc_descr_ref
16173 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16175 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16176 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16177 else if (dwarf_version >= 3 || !dwarf_strict)
16178 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16179 else
16180 return NULL;
16183 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16184 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16186 static dw_loc_descr_ref
16187 dw_sra_loc_expr (tree decl, rtx loc)
16189 rtx p;
16190 unsigned HOST_WIDE_INT padsize = 0;
16191 dw_loc_descr_ref descr, *descr_tail;
16192 unsigned HOST_WIDE_INT decl_size;
16193 rtx varloc;
16194 enum var_init_status initialized;
16196 if (DECL_SIZE (decl) == NULL
16197 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16198 return NULL;
16200 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16201 descr = NULL;
16202 descr_tail = &descr;
16204 for (p = loc; p; p = XEXP (p, 1))
16206 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16207 rtx loc_note = *decl_piece_varloc_ptr (p);
16208 dw_loc_descr_ref cur_descr;
16209 dw_loc_descr_ref *tail, last = NULL;
16210 unsigned HOST_WIDE_INT opsize = 0;
16212 if (loc_note == NULL_RTX
16213 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16215 padsize += bitsize;
16216 continue;
16218 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16219 varloc = NOTE_VAR_LOCATION (loc_note);
16220 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16221 if (cur_descr == NULL)
16223 padsize += bitsize;
16224 continue;
16227 /* Check that cur_descr either doesn't use
16228 DW_OP_*piece operations, or their sum is equal
16229 to bitsize. Otherwise we can't embed it. */
16230 for (tail = &cur_descr; *tail != NULL;
16231 tail = &(*tail)->dw_loc_next)
16232 if ((*tail)->dw_loc_opc == DW_OP_piece)
16234 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16235 * BITS_PER_UNIT;
16236 last = *tail;
16238 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16240 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16241 last = *tail;
16244 if (last != NULL && opsize != bitsize)
16246 padsize += bitsize;
16247 /* Discard the current piece of the descriptor and release any
16248 addr_table entries it uses. */
16249 remove_loc_list_addr_table_entries (cur_descr);
16250 continue;
16253 /* If there is a hole, add DW_OP_*piece after empty DWARF
16254 expression, which means that those bits are optimized out. */
16255 if (padsize)
16257 if (padsize > decl_size)
16259 remove_loc_list_addr_table_entries (cur_descr);
16260 goto discard_descr;
16262 decl_size -= padsize;
16263 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16264 if (*descr_tail == NULL)
16266 remove_loc_list_addr_table_entries (cur_descr);
16267 goto discard_descr;
16269 descr_tail = &(*descr_tail)->dw_loc_next;
16270 padsize = 0;
16272 *descr_tail = cur_descr;
16273 descr_tail = tail;
16274 if (bitsize > decl_size)
16275 goto discard_descr;
16276 decl_size -= bitsize;
16277 if (last == NULL)
16279 HOST_WIDE_INT offset = 0;
16280 if (GET_CODE (varloc) == VAR_LOCATION
16281 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16283 varloc = PAT_VAR_LOCATION_LOC (varloc);
16284 if (GET_CODE (varloc) == EXPR_LIST)
16285 varloc = XEXP (varloc, 0);
16289 if (GET_CODE (varloc) == CONST
16290 || GET_CODE (varloc) == SIGN_EXTEND
16291 || GET_CODE (varloc) == ZERO_EXTEND)
16292 varloc = XEXP (varloc, 0);
16293 else if (GET_CODE (varloc) == SUBREG)
16294 varloc = SUBREG_REG (varloc);
16295 else
16296 break;
16298 while (1);
16299 /* DW_OP_bit_size offset should be zero for register
16300 or implicit location descriptions and empty location
16301 descriptions, but for memory addresses needs big endian
16302 adjustment. */
16303 if (MEM_P (varloc))
16305 unsigned HOST_WIDE_INT memsize
16306 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16307 if (memsize != bitsize)
16309 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16310 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16311 goto discard_descr;
16312 if (memsize < bitsize)
16313 goto discard_descr;
16314 if (BITS_BIG_ENDIAN)
16315 offset = memsize - bitsize;
16319 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16320 if (*descr_tail == NULL)
16321 goto discard_descr;
16322 descr_tail = &(*descr_tail)->dw_loc_next;
16326 /* If there were any non-empty expressions, add padding till the end of
16327 the decl. */
16328 if (descr != NULL && decl_size != 0)
16330 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16331 if (*descr_tail == NULL)
16332 goto discard_descr;
16334 return descr;
16336 discard_descr:
16337 /* Discard the descriptor and release any addr_table entries it uses. */
16338 remove_loc_list_addr_table_entries (descr);
16339 return NULL;
16342 /* Return the dwarf representation of the location list LOC_LIST of
16343 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16344 function. */
16346 static dw_loc_list_ref
16347 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16349 const char *endname, *secname;
16350 rtx varloc;
16351 enum var_init_status initialized;
16352 struct var_loc_node *node;
16353 dw_loc_descr_ref descr;
16354 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16355 dw_loc_list_ref list = NULL;
16356 dw_loc_list_ref *listp = &list;
16358 /* Now that we know what section we are using for a base,
16359 actually construct the list of locations.
16360 The first location information is what is passed to the
16361 function that creates the location list, and the remaining
16362 locations just get added on to that list.
16363 Note that we only know the start address for a location
16364 (IE location changes), so to build the range, we use
16365 the range [current location start, next location start].
16366 This means we have to special case the last node, and generate
16367 a range of [last location start, end of function label]. */
16369 secname = secname_for_decl (decl);
16371 for (node = loc_list->first; node; node = node->next)
16372 if (GET_CODE (node->loc) == EXPR_LIST
16373 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16375 if (GET_CODE (node->loc) == EXPR_LIST)
16377 /* This requires DW_OP_{,bit_}piece, which is not usable
16378 inside DWARF expressions. */
16379 if (want_address != 2)
16380 continue;
16381 descr = dw_sra_loc_expr (decl, node->loc);
16382 if (descr == NULL)
16383 continue;
16385 else
16387 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16388 varloc = NOTE_VAR_LOCATION (node->loc);
16389 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16391 if (descr)
16393 bool range_across_switch = false;
16394 /* If section switch happens in between node->label
16395 and node->next->label (or end of function) and
16396 we can't emit it as a single entry list,
16397 emit two ranges, first one ending at the end
16398 of first partition and second one starting at the
16399 beginning of second partition. */
16400 if (node == loc_list->last_before_switch
16401 && (node != loc_list->first || loc_list->first->next)
16402 && current_function_decl)
16404 endname = cfun->fde->dw_fde_end;
16405 range_across_switch = true;
16407 /* The variable has a location between NODE->LABEL and
16408 NODE->NEXT->LABEL. */
16409 else if (node->next)
16410 endname = node->next->label;
16411 /* If the variable has a location at the last label
16412 it keeps its location until the end of function. */
16413 else if (!current_function_decl)
16414 endname = text_end_label;
16415 else
16417 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16418 current_function_funcdef_no);
16419 endname = ggc_strdup (label_id);
16422 *listp = new_loc_list (descr, node->label, endname, secname);
16423 if (TREE_CODE (decl) == PARM_DECL
16424 && node == loc_list->first
16425 && NOTE_P (node->loc)
16426 && strcmp (node->label, endname) == 0)
16427 (*listp)->force = true;
16428 listp = &(*listp)->dw_loc_next;
16430 if (range_across_switch)
16432 if (GET_CODE (node->loc) == EXPR_LIST)
16433 descr = dw_sra_loc_expr (decl, node->loc);
16434 else
16436 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16437 varloc = NOTE_VAR_LOCATION (node->loc);
16438 descr = dw_loc_list_1 (decl, varloc, want_address,
16439 initialized);
16441 gcc_assert (descr);
16442 /* The variable has a location between NODE->LABEL and
16443 NODE->NEXT->LABEL. */
16444 if (node->next)
16445 endname = node->next->label;
16446 else
16447 endname = cfun->fde->dw_fde_second_end;
16448 *listp = new_loc_list (descr,
16449 cfun->fde->dw_fde_second_begin,
16450 endname, secname);
16451 listp = &(*listp)->dw_loc_next;
16456 /* Try to avoid the overhead of a location list emitting a location
16457 expression instead, but only if we didn't have more than one
16458 location entry in the first place. If some entries were not
16459 representable, we don't want to pretend a single entry that was
16460 applies to the entire scope in which the variable is
16461 available. */
16462 if (list && loc_list->first->next)
16463 gen_llsym (list);
16465 return list;
16468 /* Return if the loc_list has only single element and thus can be represented
16469 as location description. */
16471 static bool
16472 single_element_loc_list_p (dw_loc_list_ref list)
16474 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16475 return !list->ll_symbol;
16478 /* Duplicate a single element of location list. */
16480 static inline dw_loc_descr_ref
16481 copy_loc_descr (dw_loc_descr_ref ref)
16483 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16484 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16485 return copy;
16488 /* To each location in list LIST append loc descr REF. */
16490 static void
16491 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16493 dw_loc_descr_ref copy;
16494 add_loc_descr (&list->expr, ref);
16495 list = list->dw_loc_next;
16496 while (list)
16498 copy = copy_loc_descr (ref);
16499 add_loc_descr (&list->expr, copy);
16500 while (copy->dw_loc_next)
16501 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16502 list = list->dw_loc_next;
16506 /* To each location in list LIST prepend loc descr REF. */
16508 static void
16509 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16511 dw_loc_descr_ref copy;
16512 dw_loc_descr_ref ref_end = list->expr;
16513 add_loc_descr (&ref, list->expr);
16514 list->expr = ref;
16515 list = list->dw_loc_next;
16516 while (list)
16518 dw_loc_descr_ref end = list->expr;
16519 list->expr = copy = copy_loc_descr (ref);
16520 while (copy->dw_loc_next != ref_end)
16521 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16522 copy->dw_loc_next = end;
16523 list = list->dw_loc_next;
16527 /* Given two lists RET and LIST
16528 produce location list that is result of adding expression in LIST
16529 to expression in RET on each position in program.
16530 Might be destructive on both RET and LIST.
16532 TODO: We handle only simple cases of RET or LIST having at most one
16533 element. General case would involve sorting the lists in program order
16534 and merging them that will need some additional work.
16535 Adding that will improve quality of debug info especially for SRA-ed
16536 structures. */
16538 static void
16539 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16541 if (!list)
16542 return;
16543 if (!*ret)
16545 *ret = list;
16546 return;
16548 if (!list->dw_loc_next)
16550 add_loc_descr_to_each (*ret, list->expr);
16551 return;
16553 if (!(*ret)->dw_loc_next)
16555 prepend_loc_descr_to_each (list, (*ret)->expr);
16556 *ret = list;
16557 return;
16559 expansion_failed (NULL_TREE, NULL_RTX,
16560 "Don't know how to merge two non-trivial"
16561 " location lists.\n");
16562 *ret = NULL;
16563 return;
16566 /* LOC is constant expression. Try a luck, look it up in constant
16567 pool and return its loc_descr of its address. */
16569 static dw_loc_descr_ref
16570 cst_pool_loc_descr (tree loc)
16572 /* Get an RTL for this, if something has been emitted. */
16573 rtx rtl = lookup_constant_def (loc);
16575 if (!rtl || !MEM_P (rtl))
16577 gcc_assert (!rtl);
16578 return 0;
16580 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16582 /* TODO: We might get more coverage if we was actually delaying expansion
16583 of all expressions till end of compilation when constant pools are fully
16584 populated. */
16585 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16587 expansion_failed (loc, NULL_RTX,
16588 "CST value in contant pool but not marked.");
16589 return 0;
16591 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16592 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16595 /* Return dw_loc_list representing address of addr_expr LOC
16596 by looking for inner INDIRECT_REF expression and turning
16597 it into simple arithmetics.
16599 See loc_list_from_tree for the meaning of CONTEXT. */
16601 static dw_loc_list_ref
16602 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16603 loc_descr_context *context)
16605 tree obj, offset;
16606 HOST_WIDE_INT bitsize, bitpos, bytepos;
16607 machine_mode mode;
16608 int unsignedp, reversep, volatilep = 0;
16609 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16611 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16612 &bitsize, &bitpos, &offset, &mode,
16613 &unsignedp, &reversep, &volatilep);
16614 STRIP_NOPS (obj);
16615 if (bitpos % BITS_PER_UNIT)
16617 expansion_failed (loc, NULL_RTX, "bitfield access");
16618 return 0;
16620 if (!INDIRECT_REF_P (obj))
16622 expansion_failed (obj,
16623 NULL_RTX, "no indirect ref in inner refrence");
16624 return 0;
16626 if (!offset && !bitpos)
16627 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16628 context);
16629 else if (toplev
16630 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16631 && (dwarf_version >= 4 || !dwarf_strict))
16633 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16634 if (!list_ret)
16635 return 0;
16636 if (offset)
16638 /* Variable offset. */
16639 list_ret1 = loc_list_from_tree (offset, 0, context);
16640 if (list_ret1 == 0)
16641 return 0;
16642 add_loc_list (&list_ret, list_ret1);
16643 if (!list_ret)
16644 return 0;
16645 add_loc_descr_to_each (list_ret,
16646 new_loc_descr (DW_OP_plus, 0, 0));
16648 bytepos = bitpos / BITS_PER_UNIT;
16649 if (bytepos > 0)
16650 add_loc_descr_to_each (list_ret,
16651 new_loc_descr (DW_OP_plus_uconst,
16652 bytepos, 0));
16653 else if (bytepos < 0)
16654 loc_list_plus_const (list_ret, bytepos);
16655 add_loc_descr_to_each (list_ret,
16656 new_loc_descr (DW_OP_stack_value, 0, 0));
16658 return list_ret;
16661 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16662 all operations from LOC are nops, move to the last one. Insert in NOPS all
16663 operations that are skipped. */
16665 static void
16666 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16667 hash_set<dw_loc_descr_ref> &nops)
16669 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16671 nops.add (loc);
16672 loc = loc->dw_loc_next;
16676 /* Helper for loc_descr_without_nops: free the location description operation
16677 P. */
16679 bool
16680 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16682 ggc_free (loc);
16683 return true;
16686 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16687 finishes LOC. */
16689 static void
16690 loc_descr_without_nops (dw_loc_descr_ref &loc)
16692 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16693 return;
16695 /* Set of all DW_OP_nop operations we remove. */
16696 hash_set<dw_loc_descr_ref> nops;
16698 /* First, strip all prefix NOP operations in order to keep the head of the
16699 operations list. */
16700 loc_descr_to_next_no_nop (loc, nops);
16702 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16704 /* For control flow operations: strip "prefix" nops in destination
16705 labels. */
16706 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16707 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16708 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16709 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16711 /* Do the same for the operations that follow, then move to the next
16712 iteration. */
16713 if (cur->dw_loc_next != NULL)
16714 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16715 cur = cur->dw_loc_next;
16718 nops.traverse<void *, free_loc_descr> (NULL);
16722 struct dwarf_procedure_info;
16724 /* Helper structure for location descriptions generation. */
16725 struct loc_descr_context
16727 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16728 NULL_TREE if DW_OP_push_object_address in invalid for this location
16729 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16730 tree context_type;
16731 /* The ..._DECL node that should be translated as a
16732 DW_OP_push_object_address operation. */
16733 tree base_decl;
16734 /* Information about the DWARF procedure we are currently generating. NULL if
16735 we are not generating a DWARF procedure. */
16736 struct dwarf_procedure_info *dpi;
16737 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16738 by consumer. Used for DW_TAG_generic_subrange attributes. */
16739 bool placeholder_arg;
16740 /* True if PLACEHOLDER_EXPR has been seen. */
16741 bool placeholder_seen;
16744 /* DWARF procedures generation
16746 DWARF expressions (aka. location descriptions) are used to encode variable
16747 things such as sizes or offsets. Such computations can have redundant parts
16748 that can be factorized in order to reduce the size of the output debug
16749 information. This is the whole point of DWARF procedures.
16751 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16752 already factorized into functions ("size functions") in order to handle very
16753 big and complex types. Such functions are quite simple: they have integral
16754 arguments, they return an integral result and their body contains only a
16755 return statement with arithmetic expressions. This is the only kind of
16756 function we are interested in translating into DWARF procedures, here.
16758 DWARF expressions and DWARF procedure are executed using a stack, so we have
16759 to define some calling convention for them to interact. Let's say that:
16761 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16762 all arguments in reverse order (right-to-left) so that when the DWARF
16763 procedure execution starts, the first argument is the top of the stack.
16765 - Then, when returning, the DWARF procedure must have consumed all arguments
16766 on the stack, must have pushed the result and touched nothing else.
16768 - Each integral argument and the result are integral types can be hold in a
16769 single stack slot.
16771 - We call "frame offset" the number of stack slots that are "under DWARF
16772 procedure control": it includes the arguments slots, the temporaries and
16773 the result slot. Thus, it is equal to the number of arguments when the
16774 procedure execution starts and must be equal to one (the result) when it
16775 returns. */
16777 /* Helper structure used when generating operations for a DWARF procedure. */
16778 struct dwarf_procedure_info
16780 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16781 currently translated. */
16782 tree fndecl;
16783 /* The number of arguments FNDECL takes. */
16784 unsigned args_count;
16787 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16788 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16789 equate it to this DIE. */
16791 static dw_die_ref
16792 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16793 dw_die_ref parent_die)
16795 dw_die_ref dwarf_proc_die;
16797 if ((dwarf_version < 3 && dwarf_strict)
16798 || location == NULL)
16799 return NULL;
16801 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16802 if (fndecl)
16803 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16804 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16805 return dwarf_proc_die;
16808 /* Return whether TYPE is a supported type as a DWARF procedure argument
16809 type or return type (we handle only scalar types and pointer types that
16810 aren't wider than the DWARF expression evaluation stack. */
16812 static bool
16813 is_handled_procedure_type (tree type)
16815 return ((INTEGRAL_TYPE_P (type)
16816 || TREE_CODE (type) == OFFSET_TYPE
16817 || TREE_CODE (type) == POINTER_TYPE)
16818 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16821 /* Helper for resolve_args_picking: do the same but stop when coming across
16822 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16823 offset *before* evaluating the corresponding operation. */
16825 static bool
16826 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16827 struct dwarf_procedure_info *dpi,
16828 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16830 /* The "frame_offset" identifier is already used to name a macro... */
16831 unsigned frame_offset_ = initial_frame_offset;
16832 dw_loc_descr_ref l;
16834 for (l = loc; l != NULL;)
16836 bool existed;
16837 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16839 /* If we already met this node, there is nothing to compute anymore. */
16840 if (existed)
16842 /* Make sure that the stack size is consistent wherever the execution
16843 flow comes from. */
16844 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16845 break;
16847 l_frame_offset = frame_offset_;
16849 /* If needed, relocate the picking offset with respect to the frame
16850 offset. */
16851 if (l->frame_offset_rel)
16853 unsigned HOST_WIDE_INT off;
16854 switch (l->dw_loc_opc)
16856 case DW_OP_pick:
16857 off = l->dw_loc_oprnd1.v.val_unsigned;
16858 break;
16859 case DW_OP_dup:
16860 off = 0;
16861 break;
16862 case DW_OP_over:
16863 off = 1;
16864 break;
16865 default:
16866 gcc_unreachable ();
16868 /* frame_offset_ is the size of the current stack frame, including
16869 incoming arguments. Besides, the arguments are pushed
16870 right-to-left. Thus, in order to access the Nth argument from
16871 this operation node, the picking has to skip temporaries *plus*
16872 one stack slot per argument (0 for the first one, 1 for the second
16873 one, etc.).
16875 The targetted argument number (N) is already set as the operand,
16876 and the number of temporaries can be computed with:
16877 frame_offsets_ - dpi->args_count */
16878 off += frame_offset_ - dpi->args_count;
16880 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16881 if (off > 255)
16882 return false;
16884 if (off == 0)
16886 l->dw_loc_opc = DW_OP_dup;
16887 l->dw_loc_oprnd1.v.val_unsigned = 0;
16889 else if (off == 1)
16891 l->dw_loc_opc = DW_OP_over;
16892 l->dw_loc_oprnd1.v.val_unsigned = 0;
16894 else
16896 l->dw_loc_opc = DW_OP_pick;
16897 l->dw_loc_oprnd1.v.val_unsigned = off;
16901 /* Update frame_offset according to the effect the current operation has
16902 on the stack. */
16903 switch (l->dw_loc_opc)
16905 case DW_OP_deref:
16906 case DW_OP_swap:
16907 case DW_OP_rot:
16908 case DW_OP_abs:
16909 case DW_OP_neg:
16910 case DW_OP_not:
16911 case DW_OP_plus_uconst:
16912 case DW_OP_skip:
16913 case DW_OP_reg0:
16914 case DW_OP_reg1:
16915 case DW_OP_reg2:
16916 case DW_OP_reg3:
16917 case DW_OP_reg4:
16918 case DW_OP_reg5:
16919 case DW_OP_reg6:
16920 case DW_OP_reg7:
16921 case DW_OP_reg8:
16922 case DW_OP_reg9:
16923 case DW_OP_reg10:
16924 case DW_OP_reg11:
16925 case DW_OP_reg12:
16926 case DW_OP_reg13:
16927 case DW_OP_reg14:
16928 case DW_OP_reg15:
16929 case DW_OP_reg16:
16930 case DW_OP_reg17:
16931 case DW_OP_reg18:
16932 case DW_OP_reg19:
16933 case DW_OP_reg20:
16934 case DW_OP_reg21:
16935 case DW_OP_reg22:
16936 case DW_OP_reg23:
16937 case DW_OP_reg24:
16938 case DW_OP_reg25:
16939 case DW_OP_reg26:
16940 case DW_OP_reg27:
16941 case DW_OP_reg28:
16942 case DW_OP_reg29:
16943 case DW_OP_reg30:
16944 case DW_OP_reg31:
16945 case DW_OP_bregx:
16946 case DW_OP_piece:
16947 case DW_OP_deref_size:
16948 case DW_OP_nop:
16949 case DW_OP_bit_piece:
16950 case DW_OP_implicit_value:
16951 case DW_OP_stack_value:
16952 break;
16954 case DW_OP_addr:
16955 case DW_OP_const1u:
16956 case DW_OP_const1s:
16957 case DW_OP_const2u:
16958 case DW_OP_const2s:
16959 case DW_OP_const4u:
16960 case DW_OP_const4s:
16961 case DW_OP_const8u:
16962 case DW_OP_const8s:
16963 case DW_OP_constu:
16964 case DW_OP_consts:
16965 case DW_OP_dup:
16966 case DW_OP_over:
16967 case DW_OP_pick:
16968 case DW_OP_lit0:
16969 case DW_OP_lit1:
16970 case DW_OP_lit2:
16971 case DW_OP_lit3:
16972 case DW_OP_lit4:
16973 case DW_OP_lit5:
16974 case DW_OP_lit6:
16975 case DW_OP_lit7:
16976 case DW_OP_lit8:
16977 case DW_OP_lit9:
16978 case DW_OP_lit10:
16979 case DW_OP_lit11:
16980 case DW_OP_lit12:
16981 case DW_OP_lit13:
16982 case DW_OP_lit14:
16983 case DW_OP_lit15:
16984 case DW_OP_lit16:
16985 case DW_OP_lit17:
16986 case DW_OP_lit18:
16987 case DW_OP_lit19:
16988 case DW_OP_lit20:
16989 case DW_OP_lit21:
16990 case DW_OP_lit22:
16991 case DW_OP_lit23:
16992 case DW_OP_lit24:
16993 case DW_OP_lit25:
16994 case DW_OP_lit26:
16995 case DW_OP_lit27:
16996 case DW_OP_lit28:
16997 case DW_OP_lit29:
16998 case DW_OP_lit30:
16999 case DW_OP_lit31:
17000 case DW_OP_breg0:
17001 case DW_OP_breg1:
17002 case DW_OP_breg2:
17003 case DW_OP_breg3:
17004 case DW_OP_breg4:
17005 case DW_OP_breg5:
17006 case DW_OP_breg6:
17007 case DW_OP_breg7:
17008 case DW_OP_breg8:
17009 case DW_OP_breg9:
17010 case DW_OP_breg10:
17011 case DW_OP_breg11:
17012 case DW_OP_breg12:
17013 case DW_OP_breg13:
17014 case DW_OP_breg14:
17015 case DW_OP_breg15:
17016 case DW_OP_breg16:
17017 case DW_OP_breg17:
17018 case DW_OP_breg18:
17019 case DW_OP_breg19:
17020 case DW_OP_breg20:
17021 case DW_OP_breg21:
17022 case DW_OP_breg22:
17023 case DW_OP_breg23:
17024 case DW_OP_breg24:
17025 case DW_OP_breg25:
17026 case DW_OP_breg26:
17027 case DW_OP_breg27:
17028 case DW_OP_breg28:
17029 case DW_OP_breg29:
17030 case DW_OP_breg30:
17031 case DW_OP_breg31:
17032 case DW_OP_fbreg:
17033 case DW_OP_push_object_address:
17034 case DW_OP_call_frame_cfa:
17035 case DW_OP_GNU_variable_value:
17036 ++frame_offset_;
17037 break;
17039 case DW_OP_drop:
17040 case DW_OP_xderef:
17041 case DW_OP_and:
17042 case DW_OP_div:
17043 case DW_OP_minus:
17044 case DW_OP_mod:
17045 case DW_OP_mul:
17046 case DW_OP_or:
17047 case DW_OP_plus:
17048 case DW_OP_shl:
17049 case DW_OP_shr:
17050 case DW_OP_shra:
17051 case DW_OP_xor:
17052 case DW_OP_bra:
17053 case DW_OP_eq:
17054 case DW_OP_ge:
17055 case DW_OP_gt:
17056 case DW_OP_le:
17057 case DW_OP_lt:
17058 case DW_OP_ne:
17059 case DW_OP_regx:
17060 case DW_OP_xderef_size:
17061 --frame_offset_;
17062 break;
17064 case DW_OP_call2:
17065 case DW_OP_call4:
17066 case DW_OP_call_ref:
17068 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17069 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17071 if (stack_usage == NULL)
17072 return false;
17073 frame_offset_ += *stack_usage;
17074 break;
17077 case DW_OP_implicit_pointer:
17078 case DW_OP_entry_value:
17079 case DW_OP_const_type:
17080 case DW_OP_regval_type:
17081 case DW_OP_deref_type:
17082 case DW_OP_convert:
17083 case DW_OP_reinterpret:
17084 case DW_OP_form_tls_address:
17085 case DW_OP_GNU_push_tls_address:
17086 case DW_OP_GNU_uninit:
17087 case DW_OP_GNU_encoded_addr:
17088 case DW_OP_GNU_implicit_pointer:
17089 case DW_OP_GNU_entry_value:
17090 case DW_OP_GNU_const_type:
17091 case DW_OP_GNU_regval_type:
17092 case DW_OP_GNU_deref_type:
17093 case DW_OP_GNU_convert:
17094 case DW_OP_GNU_reinterpret:
17095 case DW_OP_GNU_parameter_ref:
17096 /* loc_list_from_tree will probably not output these operations for
17097 size functions, so assume they will not appear here. */
17098 /* Fall through... */
17100 default:
17101 gcc_unreachable ();
17104 /* Now, follow the control flow (except subroutine calls). */
17105 switch (l->dw_loc_opc)
17107 case DW_OP_bra:
17108 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17109 frame_offsets))
17110 return false;
17111 /* Fall through. */
17113 case DW_OP_skip:
17114 l = l->dw_loc_oprnd1.v.val_loc;
17115 break;
17117 case DW_OP_stack_value:
17118 return true;
17120 default:
17121 l = l->dw_loc_next;
17122 break;
17126 return true;
17129 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17130 operations) in order to resolve the operand of DW_OP_pick operations that
17131 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17132 offset *before* LOC is executed. Return if all relocations were
17133 successful. */
17135 static bool
17136 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17137 struct dwarf_procedure_info *dpi)
17139 /* Associate to all visited operations the frame offset *before* evaluating
17140 this operation. */
17141 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17143 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17144 frame_offsets);
17147 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17148 Return NULL if it is not possible. */
17150 static dw_die_ref
17151 function_to_dwarf_procedure (tree fndecl)
17153 struct loc_descr_context ctx;
17154 struct dwarf_procedure_info dpi;
17155 dw_die_ref dwarf_proc_die;
17156 tree tree_body = DECL_SAVED_TREE (fndecl);
17157 dw_loc_descr_ref loc_body, epilogue;
17159 tree cursor;
17160 unsigned i;
17162 /* Do not generate multiple DWARF procedures for the same function
17163 declaration. */
17164 dwarf_proc_die = lookup_decl_die (fndecl);
17165 if (dwarf_proc_die != NULL)
17166 return dwarf_proc_die;
17168 /* DWARF procedures are available starting with the DWARFv3 standard. */
17169 if (dwarf_version < 3 && dwarf_strict)
17170 return NULL;
17172 /* We handle only functions for which we still have a body, that return a
17173 supported type and that takes arguments with supported types. Note that
17174 there is no point translating functions that return nothing. */
17175 if (tree_body == NULL_TREE
17176 || DECL_RESULT (fndecl) == NULL_TREE
17177 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17178 return NULL;
17180 for (cursor = DECL_ARGUMENTS (fndecl);
17181 cursor != NULL_TREE;
17182 cursor = TREE_CHAIN (cursor))
17183 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17184 return NULL;
17186 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17187 if (TREE_CODE (tree_body) != RETURN_EXPR)
17188 return NULL;
17189 tree_body = TREE_OPERAND (tree_body, 0);
17190 if (TREE_CODE (tree_body) != MODIFY_EXPR
17191 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17192 return NULL;
17193 tree_body = TREE_OPERAND (tree_body, 1);
17195 /* Try to translate the body expression itself. Note that this will probably
17196 cause an infinite recursion if its call graph has a cycle. This is very
17197 unlikely for size functions, however, so don't bother with such things at
17198 the moment. */
17199 ctx.context_type = NULL_TREE;
17200 ctx.base_decl = NULL_TREE;
17201 ctx.dpi = &dpi;
17202 ctx.placeholder_arg = false;
17203 ctx.placeholder_seen = false;
17204 dpi.fndecl = fndecl;
17205 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17206 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17207 if (!loc_body)
17208 return NULL;
17210 /* After evaluating all operands in "loc_body", we should still have on the
17211 stack all arguments plus the desired function result (top of the stack).
17212 Generate code in order to keep only the result in our stack frame. */
17213 epilogue = NULL;
17214 for (i = 0; i < dpi.args_count; ++i)
17216 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17217 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17218 op_couple->dw_loc_next->dw_loc_next = epilogue;
17219 epilogue = op_couple;
17221 add_loc_descr (&loc_body, epilogue);
17222 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17223 return NULL;
17225 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17226 because they are considered useful. Now there is an epilogue, they are
17227 not anymore, so give it another try. */
17228 loc_descr_without_nops (loc_body);
17230 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17231 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17232 though, given that size functions do not come from source, so they should
17233 not have a dedicated DW_TAG_subprogram DIE. */
17234 dwarf_proc_die
17235 = new_dwarf_proc_die (loc_body, fndecl,
17236 get_context_die (DECL_CONTEXT (fndecl)));
17238 /* The called DWARF procedure consumes one stack slot per argument and
17239 returns one stack slot. */
17240 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17242 return dwarf_proc_die;
17246 /* Generate Dwarf location list representing LOC.
17247 If WANT_ADDRESS is false, expression computing LOC will be computed
17248 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17249 if WANT_ADDRESS is 2, expression computing address useable in location
17250 will be returned (i.e. DW_OP_reg can be used
17251 to refer to register values).
17253 CONTEXT provides information to customize the location descriptions
17254 generation. Its context_type field specifies what type is implicitly
17255 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17256 will not be generated.
17258 Its DPI field determines whether we are generating a DWARF expression for a
17259 DWARF procedure, so PARM_DECL references are processed specifically.
17261 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17262 and dpi fields were null. */
17264 static dw_loc_list_ref
17265 loc_list_from_tree_1 (tree loc, int want_address,
17266 struct loc_descr_context *context)
17268 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17269 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17270 int have_address = 0;
17271 enum dwarf_location_atom op;
17273 /* ??? Most of the time we do not take proper care for sign/zero
17274 extending the values properly. Hopefully this won't be a real
17275 problem... */
17277 if (context != NULL
17278 && context->base_decl == loc
17279 && want_address == 0)
17281 if (dwarf_version >= 3 || !dwarf_strict)
17282 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17283 NULL, NULL, NULL);
17284 else
17285 return NULL;
17288 switch (TREE_CODE (loc))
17290 case ERROR_MARK:
17291 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17292 return 0;
17294 case PLACEHOLDER_EXPR:
17295 /* This case involves extracting fields from an object to determine the
17296 position of other fields. It is supposed to appear only as the first
17297 operand of COMPONENT_REF nodes and to reference precisely the type
17298 that the context allows. */
17299 if (context != NULL
17300 && TREE_TYPE (loc) == context->context_type
17301 && want_address >= 1)
17303 if (dwarf_version >= 3 || !dwarf_strict)
17305 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17306 have_address = 1;
17307 break;
17309 else
17310 return NULL;
17312 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17313 the single argument passed by consumer. */
17314 else if (context != NULL
17315 && context->placeholder_arg
17316 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17317 && want_address == 0)
17319 ret = new_loc_descr (DW_OP_pick, 0, 0);
17320 ret->frame_offset_rel = 1;
17321 context->placeholder_seen = true;
17322 break;
17324 else
17325 expansion_failed (loc, NULL_RTX,
17326 "PLACEHOLDER_EXPR for an unexpected type");
17327 break;
17329 case CALL_EXPR:
17331 const int nargs = call_expr_nargs (loc);
17332 tree callee = get_callee_fndecl (loc);
17333 int i;
17334 dw_die_ref dwarf_proc;
17336 if (callee == NULL_TREE)
17337 goto call_expansion_failed;
17339 /* We handle only functions that return an integer. */
17340 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17341 goto call_expansion_failed;
17343 dwarf_proc = function_to_dwarf_procedure (callee);
17344 if (dwarf_proc == NULL)
17345 goto call_expansion_failed;
17347 /* Evaluate arguments right-to-left so that the first argument will
17348 be the top-most one on the stack. */
17349 for (i = nargs - 1; i >= 0; --i)
17351 dw_loc_descr_ref loc_descr
17352 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17353 context);
17355 if (loc_descr == NULL)
17356 goto call_expansion_failed;
17358 add_loc_descr (&ret, loc_descr);
17361 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17362 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17363 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17364 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17365 add_loc_descr (&ret, ret1);
17366 break;
17368 call_expansion_failed:
17369 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17370 /* There are no opcodes for these operations. */
17371 return 0;
17374 case PREINCREMENT_EXPR:
17375 case PREDECREMENT_EXPR:
17376 case POSTINCREMENT_EXPR:
17377 case POSTDECREMENT_EXPR:
17378 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17379 /* There are no opcodes for these operations. */
17380 return 0;
17382 case ADDR_EXPR:
17383 /* If we already want an address, see if there is INDIRECT_REF inside
17384 e.g. for &this->field. */
17385 if (want_address)
17387 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17388 (loc, want_address == 2, context);
17389 if (list_ret)
17390 have_address = 1;
17391 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17392 && (ret = cst_pool_loc_descr (loc)))
17393 have_address = 1;
17395 /* Otherwise, process the argument and look for the address. */
17396 if (!list_ret && !ret)
17397 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17398 else
17400 if (want_address)
17401 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17402 return NULL;
17404 break;
17406 case VAR_DECL:
17407 if (DECL_THREAD_LOCAL_P (loc))
17409 rtx rtl;
17410 enum dwarf_location_atom tls_op;
17411 enum dtprel_bool dtprel = dtprel_false;
17413 if (targetm.have_tls)
17415 /* If this is not defined, we have no way to emit the
17416 data. */
17417 if (!targetm.asm_out.output_dwarf_dtprel)
17418 return 0;
17420 /* The way DW_OP_GNU_push_tls_address is specified, we
17421 can only look up addresses of objects in the current
17422 module. We used DW_OP_addr as first op, but that's
17423 wrong, because DW_OP_addr is relocated by the debug
17424 info consumer, while DW_OP_GNU_push_tls_address
17425 operand shouldn't be. */
17426 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17427 return 0;
17428 dtprel = dtprel_true;
17429 /* We check for DWARF 5 here because gdb did not implement
17430 DW_OP_form_tls_address until after 7.12. */
17431 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17432 : DW_OP_GNU_push_tls_address);
17434 else
17436 if (!targetm.emutls.debug_form_tls_address
17437 || !(dwarf_version >= 3 || !dwarf_strict))
17438 return 0;
17439 /* We stuffed the control variable into the DECL_VALUE_EXPR
17440 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17441 no longer appear in gimple code. We used the control
17442 variable in specific so that we could pick it up here. */
17443 loc = DECL_VALUE_EXPR (loc);
17444 tls_op = DW_OP_form_tls_address;
17447 rtl = rtl_for_decl_location (loc);
17448 if (rtl == NULL_RTX)
17449 return 0;
17451 if (!MEM_P (rtl))
17452 return 0;
17453 rtl = XEXP (rtl, 0);
17454 if (! CONSTANT_P (rtl))
17455 return 0;
17457 ret = new_addr_loc_descr (rtl, dtprel);
17458 ret1 = new_loc_descr (tls_op, 0, 0);
17459 add_loc_descr (&ret, ret1);
17461 have_address = 1;
17462 break;
17464 /* FALLTHRU */
17466 case PARM_DECL:
17467 if (context != NULL && context->dpi != NULL
17468 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17470 /* We are generating code for a DWARF procedure and we want to access
17471 one of its arguments: find the appropriate argument offset and let
17472 the resolve_args_picking pass compute the offset that complies
17473 with the stack frame size. */
17474 unsigned i = 0;
17475 tree cursor;
17477 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17478 cursor != NULL_TREE && cursor != loc;
17479 cursor = TREE_CHAIN (cursor), ++i)
17481 /* If we are translating a DWARF procedure, all referenced parameters
17482 must belong to the current function. */
17483 gcc_assert (cursor != NULL_TREE);
17485 ret = new_loc_descr (DW_OP_pick, i, 0);
17486 ret->frame_offset_rel = 1;
17487 break;
17489 /* FALLTHRU */
17491 case RESULT_DECL:
17492 if (DECL_HAS_VALUE_EXPR_P (loc))
17493 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17494 want_address, context);
17495 /* FALLTHRU */
17497 case FUNCTION_DECL:
17499 rtx rtl;
17500 var_loc_list *loc_list = lookup_decl_loc (loc);
17502 if (loc_list && loc_list->first)
17504 list_ret = dw_loc_list (loc_list, loc, want_address);
17505 have_address = want_address != 0;
17506 break;
17508 rtl = rtl_for_decl_location (loc);
17509 if (rtl == NULL_RTX)
17511 if (TREE_CODE (loc) != FUNCTION_DECL
17512 && early_dwarf
17513 && current_function_decl
17514 && want_address != 1
17515 && ! DECL_IGNORED_P (loc)
17516 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17517 || POINTER_TYPE_P (TREE_TYPE (loc)))
17518 && DECL_CONTEXT (loc) == current_function_decl
17519 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17520 <= DWARF2_ADDR_SIZE))
17522 dw_die_ref ref = lookup_decl_die (loc);
17523 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17524 if (ref)
17526 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17527 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17528 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17530 else
17532 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17533 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17535 break;
17537 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17538 return 0;
17540 else if (CONST_INT_P (rtl))
17542 HOST_WIDE_INT val = INTVAL (rtl);
17543 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17544 val &= GET_MODE_MASK (DECL_MODE (loc));
17545 ret = int_loc_descriptor (val);
17547 else if (GET_CODE (rtl) == CONST_STRING)
17549 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17550 return 0;
17552 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17553 ret = new_addr_loc_descr (rtl, dtprel_false);
17554 else
17556 machine_mode mode, mem_mode;
17558 /* Certain constructs can only be represented at top-level. */
17559 if (want_address == 2)
17561 ret = loc_descriptor (rtl, VOIDmode,
17562 VAR_INIT_STATUS_INITIALIZED);
17563 have_address = 1;
17565 else
17567 mode = GET_MODE (rtl);
17568 mem_mode = VOIDmode;
17569 if (MEM_P (rtl))
17571 mem_mode = mode;
17572 mode = get_address_mode (rtl);
17573 rtl = XEXP (rtl, 0);
17574 have_address = 1;
17576 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17577 VAR_INIT_STATUS_INITIALIZED);
17579 if (!ret)
17580 expansion_failed (loc, rtl,
17581 "failed to produce loc descriptor for rtl");
17584 break;
17586 case MEM_REF:
17587 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17589 have_address = 1;
17590 goto do_plus;
17592 /* Fallthru. */
17593 case INDIRECT_REF:
17594 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17595 have_address = 1;
17596 break;
17598 case TARGET_MEM_REF:
17599 case SSA_NAME:
17600 case DEBUG_EXPR_DECL:
17601 return NULL;
17603 case COMPOUND_EXPR:
17604 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17605 context);
17607 CASE_CONVERT:
17608 case VIEW_CONVERT_EXPR:
17609 case SAVE_EXPR:
17610 case MODIFY_EXPR:
17611 case NON_LVALUE_EXPR:
17612 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17613 context);
17615 case COMPONENT_REF:
17616 case BIT_FIELD_REF:
17617 case ARRAY_REF:
17618 case ARRAY_RANGE_REF:
17619 case REALPART_EXPR:
17620 case IMAGPART_EXPR:
17622 tree obj, offset;
17623 HOST_WIDE_INT bitsize, bitpos, bytepos;
17624 machine_mode mode;
17625 int unsignedp, reversep, volatilep = 0;
17627 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17628 &unsignedp, &reversep, &volatilep);
17630 gcc_assert (obj != loc);
17632 list_ret = loc_list_from_tree_1 (obj,
17633 want_address == 2
17634 && !bitpos && !offset ? 2 : 1,
17635 context);
17636 /* TODO: We can extract value of the small expression via shifting even
17637 for nonzero bitpos. */
17638 if (list_ret == 0)
17639 return 0;
17640 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17642 expansion_failed (loc, NULL_RTX,
17643 "bitfield access");
17644 return 0;
17647 if (offset != NULL_TREE)
17649 /* Variable offset. */
17650 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17651 if (list_ret1 == 0)
17652 return 0;
17653 add_loc_list (&list_ret, list_ret1);
17654 if (!list_ret)
17655 return 0;
17656 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17659 bytepos = bitpos / BITS_PER_UNIT;
17660 if (bytepos > 0)
17661 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17662 else if (bytepos < 0)
17663 loc_list_plus_const (list_ret, bytepos);
17665 have_address = 1;
17666 break;
17669 case INTEGER_CST:
17670 if ((want_address || !tree_fits_shwi_p (loc))
17671 && (ret = cst_pool_loc_descr (loc)))
17672 have_address = 1;
17673 else if (want_address == 2
17674 && tree_fits_shwi_p (loc)
17675 && (ret = address_of_int_loc_descriptor
17676 (int_size_in_bytes (TREE_TYPE (loc)),
17677 tree_to_shwi (loc))))
17678 have_address = 1;
17679 else if (tree_fits_shwi_p (loc))
17680 ret = int_loc_descriptor (tree_to_shwi (loc));
17681 else if (tree_fits_uhwi_p (loc))
17682 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17683 else
17685 expansion_failed (loc, NULL_RTX,
17686 "Integer operand is not host integer");
17687 return 0;
17689 break;
17691 case CONSTRUCTOR:
17692 case REAL_CST:
17693 case STRING_CST:
17694 case COMPLEX_CST:
17695 if ((ret = cst_pool_loc_descr (loc)))
17696 have_address = 1;
17697 else if (TREE_CODE (loc) == CONSTRUCTOR)
17699 tree type = TREE_TYPE (loc);
17700 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17701 unsigned HOST_WIDE_INT offset = 0;
17702 unsigned HOST_WIDE_INT cnt;
17703 constructor_elt *ce;
17705 if (TREE_CODE (type) == RECORD_TYPE)
17707 /* This is very limited, but it's enough to output
17708 pointers to member functions, as long as the
17709 referenced function is defined in the current
17710 translation unit. */
17711 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17713 tree val = ce->value;
17715 tree field = ce->index;
17717 if (val)
17718 STRIP_NOPS (val);
17720 if (!field || DECL_BIT_FIELD (field))
17722 expansion_failed (loc, NULL_RTX,
17723 "bitfield in record type constructor");
17724 size = offset = (unsigned HOST_WIDE_INT)-1;
17725 ret = NULL;
17726 break;
17729 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17730 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17731 gcc_assert (pos + fieldsize <= size);
17732 if (pos < offset)
17734 expansion_failed (loc, NULL_RTX,
17735 "out-of-order fields in record constructor");
17736 size = offset = (unsigned HOST_WIDE_INT)-1;
17737 ret = NULL;
17738 break;
17740 if (pos > offset)
17742 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17743 add_loc_descr (&ret, ret1);
17744 offset = pos;
17746 if (val && fieldsize != 0)
17748 ret1 = loc_descriptor_from_tree (val, want_address, context);
17749 if (!ret1)
17751 expansion_failed (loc, NULL_RTX,
17752 "unsupported expression in field");
17753 size = offset = (unsigned HOST_WIDE_INT)-1;
17754 ret = NULL;
17755 break;
17757 add_loc_descr (&ret, ret1);
17759 if (fieldsize)
17761 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17762 add_loc_descr (&ret, ret1);
17763 offset = pos + fieldsize;
17767 if (offset != size)
17769 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17770 add_loc_descr (&ret, ret1);
17771 offset = size;
17774 have_address = !!want_address;
17776 else
17777 expansion_failed (loc, NULL_RTX,
17778 "constructor of non-record type");
17780 else
17781 /* We can construct small constants here using int_loc_descriptor. */
17782 expansion_failed (loc, NULL_RTX,
17783 "constructor or constant not in constant pool");
17784 break;
17786 case TRUTH_AND_EXPR:
17787 case TRUTH_ANDIF_EXPR:
17788 case BIT_AND_EXPR:
17789 op = DW_OP_and;
17790 goto do_binop;
17792 case TRUTH_XOR_EXPR:
17793 case BIT_XOR_EXPR:
17794 op = DW_OP_xor;
17795 goto do_binop;
17797 case TRUTH_OR_EXPR:
17798 case TRUTH_ORIF_EXPR:
17799 case BIT_IOR_EXPR:
17800 op = DW_OP_or;
17801 goto do_binop;
17803 case FLOOR_DIV_EXPR:
17804 case CEIL_DIV_EXPR:
17805 case ROUND_DIV_EXPR:
17806 case TRUNC_DIV_EXPR:
17807 case EXACT_DIV_EXPR:
17808 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17809 return 0;
17810 op = DW_OP_div;
17811 goto do_binop;
17813 case MINUS_EXPR:
17814 op = DW_OP_minus;
17815 goto do_binop;
17817 case FLOOR_MOD_EXPR:
17818 case CEIL_MOD_EXPR:
17819 case ROUND_MOD_EXPR:
17820 case TRUNC_MOD_EXPR:
17821 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17823 op = DW_OP_mod;
17824 goto do_binop;
17826 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17827 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17828 if (list_ret == 0 || list_ret1 == 0)
17829 return 0;
17831 add_loc_list (&list_ret, list_ret1);
17832 if (list_ret == 0)
17833 return 0;
17834 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17835 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17836 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17837 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17838 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17839 break;
17841 case MULT_EXPR:
17842 op = DW_OP_mul;
17843 goto do_binop;
17845 case LSHIFT_EXPR:
17846 op = DW_OP_shl;
17847 goto do_binop;
17849 case RSHIFT_EXPR:
17850 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17851 goto do_binop;
17853 case POINTER_PLUS_EXPR:
17854 case PLUS_EXPR:
17855 do_plus:
17856 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17858 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17859 smarter to encode their opposite. The DW_OP_plus_uconst operation
17860 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17861 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17862 bytes, Y being the size of the operation that pushes the opposite
17863 of the addend. So let's choose the smallest representation. */
17864 const tree tree_addend = TREE_OPERAND (loc, 1);
17865 offset_int wi_addend;
17866 HOST_WIDE_INT shwi_addend;
17867 dw_loc_descr_ref loc_naddend;
17869 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17870 if (list_ret == 0)
17871 return 0;
17873 /* Try to get the literal to push. It is the opposite of the addend,
17874 so as we rely on wrapping during DWARF evaluation, first decode
17875 the literal as a "DWARF-sized" signed number. */
17876 wi_addend = wi::to_offset (tree_addend);
17877 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17878 shwi_addend = wi_addend.to_shwi ();
17879 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17880 ? int_loc_descriptor (-shwi_addend)
17881 : NULL;
17883 if (loc_naddend != NULL
17884 && ((unsigned) size_of_uleb128 (shwi_addend)
17885 > size_of_loc_descr (loc_naddend)))
17887 add_loc_descr_to_each (list_ret, loc_naddend);
17888 add_loc_descr_to_each (list_ret,
17889 new_loc_descr (DW_OP_minus, 0, 0));
17891 else
17893 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17895 loc_naddend = loc_cur;
17896 loc_cur = loc_cur->dw_loc_next;
17897 ggc_free (loc_naddend);
17899 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17901 break;
17904 op = DW_OP_plus;
17905 goto do_binop;
17907 case LE_EXPR:
17908 op = DW_OP_le;
17909 goto do_comp_binop;
17911 case GE_EXPR:
17912 op = DW_OP_ge;
17913 goto do_comp_binop;
17915 case LT_EXPR:
17916 op = DW_OP_lt;
17917 goto do_comp_binop;
17919 case GT_EXPR:
17920 op = DW_OP_gt;
17921 goto do_comp_binop;
17923 do_comp_binop:
17924 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17926 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17927 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17928 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17929 TREE_CODE (loc));
17930 break;
17932 else
17933 goto do_binop;
17935 case EQ_EXPR:
17936 op = DW_OP_eq;
17937 goto do_binop;
17939 case NE_EXPR:
17940 op = DW_OP_ne;
17941 goto do_binop;
17943 do_binop:
17944 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17945 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17946 if (list_ret == 0 || list_ret1 == 0)
17947 return 0;
17949 add_loc_list (&list_ret, list_ret1);
17950 if (list_ret == 0)
17951 return 0;
17952 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17953 break;
17955 case TRUTH_NOT_EXPR:
17956 case BIT_NOT_EXPR:
17957 op = DW_OP_not;
17958 goto do_unop;
17960 case ABS_EXPR:
17961 op = DW_OP_abs;
17962 goto do_unop;
17964 case NEGATE_EXPR:
17965 op = DW_OP_neg;
17966 goto do_unop;
17968 do_unop:
17969 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17970 if (list_ret == 0)
17971 return 0;
17973 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17974 break;
17976 case MIN_EXPR:
17977 case MAX_EXPR:
17979 const enum tree_code code =
17980 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17982 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17983 build2 (code, integer_type_node,
17984 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17985 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17988 /* fall through */
17990 case COND_EXPR:
17992 dw_loc_descr_ref lhs
17993 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17994 dw_loc_list_ref rhs
17995 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17996 dw_loc_descr_ref bra_node, jump_node, tmp;
17998 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17999 if (list_ret == 0 || lhs == 0 || rhs == 0)
18000 return 0;
18002 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18003 add_loc_descr_to_each (list_ret, bra_node);
18005 add_loc_list (&list_ret, rhs);
18006 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18007 add_loc_descr_to_each (list_ret, jump_node);
18009 add_loc_descr_to_each (list_ret, lhs);
18010 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18011 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18013 /* ??? Need a node to point the skip at. Use a nop. */
18014 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18015 add_loc_descr_to_each (list_ret, tmp);
18016 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18017 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18019 break;
18021 case FIX_TRUNC_EXPR:
18022 return 0;
18024 default:
18025 /* Leave front-end specific codes as simply unknown. This comes
18026 up, for instance, with the C STMT_EXPR. */
18027 if ((unsigned int) TREE_CODE (loc)
18028 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18030 expansion_failed (loc, NULL_RTX,
18031 "language specific tree node");
18032 return 0;
18035 /* Otherwise this is a generic code; we should just lists all of
18036 these explicitly. We forgot one. */
18037 if (flag_checking)
18038 gcc_unreachable ();
18040 /* In a release build, we want to degrade gracefully: better to
18041 generate incomplete debugging information than to crash. */
18042 return NULL;
18045 if (!ret && !list_ret)
18046 return 0;
18048 if (want_address == 2 && !have_address
18049 && (dwarf_version >= 4 || !dwarf_strict))
18051 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18053 expansion_failed (loc, NULL_RTX,
18054 "DWARF address size mismatch");
18055 return 0;
18057 if (ret)
18058 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18059 else
18060 add_loc_descr_to_each (list_ret,
18061 new_loc_descr (DW_OP_stack_value, 0, 0));
18062 have_address = 1;
18064 /* Show if we can't fill the request for an address. */
18065 if (want_address && !have_address)
18067 expansion_failed (loc, NULL_RTX,
18068 "Want address and only have value");
18069 return 0;
18072 gcc_assert (!ret || !list_ret);
18074 /* If we've got an address and don't want one, dereference. */
18075 if (!want_address && have_address)
18077 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18079 if (size > DWARF2_ADDR_SIZE || size == -1)
18081 expansion_failed (loc, NULL_RTX,
18082 "DWARF address size mismatch");
18083 return 0;
18085 else if (size == DWARF2_ADDR_SIZE)
18086 op = DW_OP_deref;
18087 else
18088 op = DW_OP_deref_size;
18090 if (ret)
18091 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18092 else
18093 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18095 if (ret)
18096 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18098 return list_ret;
18101 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18102 expressions. */
18104 static dw_loc_list_ref
18105 loc_list_from_tree (tree loc, int want_address,
18106 struct loc_descr_context *context)
18108 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18110 for (dw_loc_list_ref loc_cur = result;
18111 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18112 loc_descr_without_nops (loc_cur->expr);
18113 return result;
18116 /* Same as above but return only single location expression. */
18117 static dw_loc_descr_ref
18118 loc_descriptor_from_tree (tree loc, int want_address,
18119 struct loc_descr_context *context)
18121 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18122 if (!ret)
18123 return NULL;
18124 if (ret->dw_loc_next)
18126 expansion_failed (loc, NULL_RTX,
18127 "Location list where only loc descriptor needed");
18128 return NULL;
18130 return ret->expr;
18133 /* Given a value, round it up to the lowest multiple of `boundary'
18134 which is not less than the value itself. */
18136 static inline HOST_WIDE_INT
18137 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18139 return (((value + boundary - 1) / boundary) * boundary);
18142 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18143 pointer to the declared type for the relevant field variable, or return
18144 `integer_type_node' if the given node turns out to be an
18145 ERROR_MARK node. */
18147 static inline tree
18148 field_type (const_tree decl)
18150 tree type;
18152 if (TREE_CODE (decl) == ERROR_MARK)
18153 return integer_type_node;
18155 type = DECL_BIT_FIELD_TYPE (decl);
18156 if (type == NULL_TREE)
18157 type = TREE_TYPE (decl);
18159 return type;
18162 /* Given a pointer to a tree node, return the alignment in bits for
18163 it, or else return BITS_PER_WORD if the node actually turns out to
18164 be an ERROR_MARK node. */
18166 static inline unsigned
18167 simple_type_align_in_bits (const_tree type)
18169 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18172 static inline unsigned
18173 simple_decl_align_in_bits (const_tree decl)
18175 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18178 /* Return the result of rounding T up to ALIGN. */
18180 static inline offset_int
18181 round_up_to_align (const offset_int &t, unsigned int align)
18183 return wi::udiv_trunc (t + align - 1, align) * align;
18186 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18187 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18188 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18189 if we fail to return the size in one of these two forms. */
18191 static dw_loc_descr_ref
18192 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18194 tree tree_size;
18195 struct loc_descr_context ctx;
18197 /* Return a constant integer in priority, if possible. */
18198 *cst_size = int_size_in_bytes (type);
18199 if (*cst_size != -1)
18200 return NULL;
18202 ctx.context_type = const_cast<tree> (type);
18203 ctx.base_decl = NULL_TREE;
18204 ctx.dpi = NULL;
18205 ctx.placeholder_arg = false;
18206 ctx.placeholder_seen = false;
18208 type = TYPE_MAIN_VARIANT (type);
18209 tree_size = TYPE_SIZE_UNIT (type);
18210 return ((tree_size != NULL_TREE)
18211 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18212 : NULL);
18215 /* Helper structure for RECORD_TYPE processing. */
18216 struct vlr_context
18218 /* Root RECORD_TYPE. It is needed to generate data member location
18219 descriptions in variable-length records (VLR), but also to cope with
18220 variants, which are composed of nested structures multiplexed with
18221 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18222 function processing a FIELD_DECL, it is required to be non null. */
18223 tree struct_type;
18224 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18225 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18226 this variant part as part of the root record (in storage units). For
18227 regular records, it must be NULL_TREE. */
18228 tree variant_part_offset;
18231 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18232 addressed byte of the "containing object" for the given FIELD_DECL. If
18233 possible, return a native constant through CST_OFFSET (in which case NULL is
18234 returned); otherwise return a DWARF expression that computes the offset.
18236 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18237 that offset is, either because the argument turns out to be a pointer to an
18238 ERROR_MARK node, or because the offset expression is too complex for us.
18240 CTX is required: see the comment for VLR_CONTEXT. */
18242 static dw_loc_descr_ref
18243 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18244 HOST_WIDE_INT *cst_offset)
18246 tree tree_result;
18247 dw_loc_list_ref loc_result;
18249 *cst_offset = 0;
18251 if (TREE_CODE (decl) == ERROR_MARK)
18252 return NULL;
18253 else
18254 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18256 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18257 case. */
18258 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18259 return NULL;
18261 #ifdef PCC_BITFIELD_TYPE_MATTERS
18262 /* We used to handle only constant offsets in all cases. Now, we handle
18263 properly dynamic byte offsets only when PCC bitfield type doesn't
18264 matter. */
18265 if (PCC_BITFIELD_TYPE_MATTERS
18266 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18268 offset_int object_offset_in_bits;
18269 offset_int object_offset_in_bytes;
18270 offset_int bitpos_int;
18271 tree type;
18272 tree field_size_tree;
18273 offset_int deepest_bitpos;
18274 offset_int field_size_in_bits;
18275 unsigned int type_align_in_bits;
18276 unsigned int decl_align_in_bits;
18277 offset_int type_size_in_bits;
18279 bitpos_int = wi::to_offset (bit_position (decl));
18280 type = field_type (decl);
18281 type_size_in_bits = offset_int_type_size_in_bits (type);
18282 type_align_in_bits = simple_type_align_in_bits (type);
18284 field_size_tree = DECL_SIZE (decl);
18286 /* The size could be unspecified if there was an error, or for
18287 a flexible array member. */
18288 if (!field_size_tree)
18289 field_size_tree = bitsize_zero_node;
18291 /* If the size of the field is not constant, use the type size. */
18292 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18293 field_size_in_bits = wi::to_offset (field_size_tree);
18294 else
18295 field_size_in_bits = type_size_in_bits;
18297 decl_align_in_bits = simple_decl_align_in_bits (decl);
18299 /* The GCC front-end doesn't make any attempt to keep track of the
18300 starting bit offset (relative to the start of the containing
18301 structure type) of the hypothetical "containing object" for a
18302 bit-field. Thus, when computing the byte offset value for the
18303 start of the "containing object" of a bit-field, we must deduce
18304 this information on our own. This can be rather tricky to do in
18305 some cases. For example, handling the following structure type
18306 definition when compiling for an i386/i486 target (which only
18307 aligns long long's to 32-bit boundaries) can be very tricky:
18309 struct S { int field1; long long field2:31; };
18311 Fortunately, there is a simple rule-of-thumb which can be used
18312 in such cases. When compiling for an i386/i486, GCC will
18313 allocate 8 bytes for the structure shown above. It decides to
18314 do this based upon one simple rule for bit-field allocation.
18315 GCC allocates each "containing object" for each bit-field at
18316 the first (i.e. lowest addressed) legitimate alignment boundary
18317 (based upon the required minimum alignment for the declared
18318 type of the field) which it can possibly use, subject to the
18319 condition that there is still enough available space remaining
18320 in the containing object (when allocated at the selected point)
18321 to fully accommodate all of the bits of the bit-field itself.
18323 This simple rule makes it obvious why GCC allocates 8 bytes for
18324 each object of the structure type shown above. When looking
18325 for a place to allocate the "containing object" for `field2',
18326 the compiler simply tries to allocate a 64-bit "containing
18327 object" at each successive 32-bit boundary (starting at zero)
18328 until it finds a place to allocate that 64- bit field such that
18329 at least 31 contiguous (and previously unallocated) bits remain
18330 within that selected 64 bit field. (As it turns out, for the
18331 example above, the compiler finds it is OK to allocate the
18332 "containing object" 64-bit field at bit-offset zero within the
18333 structure type.)
18335 Here we attempt to work backwards from the limited set of facts
18336 we're given, and we try to deduce from those facts, where GCC
18337 must have believed that the containing object started (within
18338 the structure type). The value we deduce is then used (by the
18339 callers of this routine) to generate DW_AT_location and
18340 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18341 the case of DW_AT_location, regular fields as well). */
18343 /* Figure out the bit-distance from the start of the structure to
18344 the "deepest" bit of the bit-field. */
18345 deepest_bitpos = bitpos_int + field_size_in_bits;
18347 /* This is the tricky part. Use some fancy footwork to deduce
18348 where the lowest addressed bit of the containing object must
18349 be. */
18350 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18352 /* Round up to type_align by default. This works best for
18353 bitfields. */
18354 object_offset_in_bits
18355 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18357 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18359 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18361 /* Round up to decl_align instead. */
18362 object_offset_in_bits
18363 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18366 object_offset_in_bytes
18367 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18368 if (ctx->variant_part_offset == NULL_TREE)
18370 *cst_offset = object_offset_in_bytes.to_shwi ();
18371 return NULL;
18373 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18375 else
18376 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18377 tree_result = byte_position (decl);
18379 if (ctx->variant_part_offset != NULL_TREE)
18380 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18381 ctx->variant_part_offset, tree_result);
18383 /* If the byte offset is a constant, it's simplier to handle a native
18384 constant rather than a DWARF expression. */
18385 if (TREE_CODE (tree_result) == INTEGER_CST)
18387 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18388 return NULL;
18390 struct loc_descr_context loc_ctx = {
18391 ctx->struct_type, /* context_type */
18392 NULL_TREE, /* base_decl */
18393 NULL, /* dpi */
18394 false, /* placeholder_arg */
18395 false /* placeholder_seen */
18397 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18399 /* We want a DWARF expression: abort if we only have a location list with
18400 multiple elements. */
18401 if (!loc_result || !single_element_loc_list_p (loc_result))
18402 return NULL;
18403 else
18404 return loc_result->expr;
18407 /* The following routines define various Dwarf attributes and any data
18408 associated with them. */
18410 /* Add a location description attribute value to a DIE.
18412 This emits location attributes suitable for whole variables and
18413 whole parameters. Note that the location attributes for struct fields are
18414 generated by the routine `data_member_location_attribute' below. */
18416 static inline void
18417 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18418 dw_loc_list_ref descr)
18420 if (descr == 0)
18421 return;
18422 if (single_element_loc_list_p (descr))
18423 add_AT_loc (die, attr_kind, descr->expr);
18424 else
18425 add_AT_loc_list (die, attr_kind, descr);
18428 /* Add DW_AT_accessibility attribute to DIE if needed. */
18430 static void
18431 add_accessibility_attribute (dw_die_ref die, tree decl)
18433 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18434 children, otherwise the default is DW_ACCESS_public. In DWARF2
18435 the default has always been DW_ACCESS_public. */
18436 if (TREE_PROTECTED (decl))
18437 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18438 else if (TREE_PRIVATE (decl))
18440 if (dwarf_version == 2
18441 || die->die_parent == NULL
18442 || die->die_parent->die_tag != DW_TAG_class_type)
18443 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18445 else if (dwarf_version > 2
18446 && die->die_parent
18447 && die->die_parent->die_tag == DW_TAG_class_type)
18448 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18451 /* Attach the specialized form of location attribute used for data members of
18452 struct and union types. In the special case of a FIELD_DECL node which
18453 represents a bit-field, the "offset" part of this special location
18454 descriptor must indicate the distance in bytes from the lowest-addressed
18455 byte of the containing struct or union type to the lowest-addressed byte of
18456 the "containing object" for the bit-field. (See the `field_byte_offset'
18457 function above).
18459 For any given bit-field, the "containing object" is a hypothetical object
18460 (of some integral or enum type) within which the given bit-field lives. The
18461 type of this hypothetical "containing object" is always the same as the
18462 declared type of the individual bit-field itself (for GCC anyway... the
18463 DWARF spec doesn't actually mandate this). Note that it is the size (in
18464 bytes) of the hypothetical "containing object" which will be given in the
18465 DW_AT_byte_size attribute for this bit-field. (See the
18466 `byte_size_attribute' function below.) It is also used when calculating the
18467 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18468 function below.)
18470 CTX is required: see the comment for VLR_CONTEXT. */
18472 static void
18473 add_data_member_location_attribute (dw_die_ref die,
18474 tree decl,
18475 struct vlr_context *ctx)
18477 HOST_WIDE_INT offset;
18478 dw_loc_descr_ref loc_descr = 0;
18480 if (TREE_CODE (decl) == TREE_BINFO)
18482 /* We're working on the TAG_inheritance for a base class. */
18483 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18485 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18486 aren't at a fixed offset from all (sub)objects of the same
18487 type. We need to extract the appropriate offset from our
18488 vtable. The following dwarf expression means
18490 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18492 This is specific to the V3 ABI, of course. */
18494 dw_loc_descr_ref tmp;
18496 /* Make a copy of the object address. */
18497 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18498 add_loc_descr (&loc_descr, tmp);
18500 /* Extract the vtable address. */
18501 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18502 add_loc_descr (&loc_descr, tmp);
18504 /* Calculate the address of the offset. */
18505 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18506 gcc_assert (offset < 0);
18508 tmp = int_loc_descriptor (-offset);
18509 add_loc_descr (&loc_descr, tmp);
18510 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18511 add_loc_descr (&loc_descr, tmp);
18513 /* Extract the offset. */
18514 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18515 add_loc_descr (&loc_descr, tmp);
18517 /* Add it to the object address. */
18518 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18519 add_loc_descr (&loc_descr, tmp);
18521 else
18522 offset = tree_to_shwi (BINFO_OFFSET (decl));
18524 else
18526 loc_descr = field_byte_offset (decl, ctx, &offset);
18528 /* If loc_descr is available then we know the field offset is dynamic.
18529 However, GDB does not handle dynamic field offsets very well at the
18530 moment. */
18531 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18533 loc_descr = NULL;
18534 offset = 0;
18537 /* Data member location evalutation starts with the base address on the
18538 stack. Compute the field offset and add it to this base address. */
18539 else if (loc_descr != NULL)
18540 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18543 if (! loc_descr)
18545 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18546 e.g. GDB only added support to it in November 2016. For DWARF5
18547 we need newer debug info consumers anyway. We might change this
18548 to dwarf_version >= 4 once most consumers catched up. */
18549 if (dwarf_version >= 5
18550 && TREE_CODE (decl) == FIELD_DECL
18551 && DECL_BIT_FIELD_TYPE (decl))
18553 tree off = bit_position (decl);
18554 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18556 remove_AT (die, DW_AT_byte_size);
18557 remove_AT (die, DW_AT_bit_offset);
18558 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18559 return;
18562 if (dwarf_version > 2)
18564 /* Don't need to output a location expression, just the constant. */
18565 if (offset < 0)
18566 add_AT_int (die, DW_AT_data_member_location, offset);
18567 else
18568 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18569 return;
18571 else
18573 enum dwarf_location_atom op;
18575 /* The DWARF2 standard says that we should assume that the structure
18576 address is already on the stack, so we can specify a structure
18577 field address by using DW_OP_plus_uconst. */
18578 op = DW_OP_plus_uconst;
18579 loc_descr = new_loc_descr (op, offset, 0);
18583 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18586 /* Writes integer values to dw_vec_const array. */
18588 static void
18589 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18591 while (size != 0)
18593 *dest++ = val & 0xff;
18594 val >>= 8;
18595 --size;
18599 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18601 static HOST_WIDE_INT
18602 extract_int (const unsigned char *src, unsigned int size)
18604 HOST_WIDE_INT val = 0;
18606 src += size;
18607 while (size != 0)
18609 val <<= 8;
18610 val |= *--src & 0xff;
18611 --size;
18613 return val;
18616 /* Writes wide_int values to dw_vec_const array. */
18618 static void
18619 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18621 int i;
18623 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18625 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18626 return;
18629 /* We'd have to extend this code to support odd sizes. */
18630 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18632 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18634 if (WORDS_BIG_ENDIAN)
18635 for (i = n - 1; i >= 0; i--)
18637 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18638 dest += sizeof (HOST_WIDE_INT);
18640 else
18641 for (i = 0; i < n; i++)
18643 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18644 dest += sizeof (HOST_WIDE_INT);
18648 /* Writes floating point values to dw_vec_const array. */
18650 static void
18651 insert_float (const_rtx rtl, unsigned char *array)
18653 long val[4];
18654 int i;
18655 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18657 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18659 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18660 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18662 insert_int (val[i], 4, array);
18663 array += 4;
18667 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18668 does not have a "location" either in memory or in a register. These
18669 things can arise in GNU C when a constant is passed as an actual parameter
18670 to an inlined function. They can also arise in C++ where declared
18671 constants do not necessarily get memory "homes". */
18673 static bool
18674 add_const_value_attribute (dw_die_ref die, rtx rtl)
18676 switch (GET_CODE (rtl))
18678 case CONST_INT:
18680 HOST_WIDE_INT val = INTVAL (rtl);
18682 if (val < 0)
18683 add_AT_int (die, DW_AT_const_value, val);
18684 else
18685 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18687 return true;
18689 case CONST_WIDE_INT:
18691 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18692 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18693 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18694 wide_int w = wi::zext (w1, prec);
18695 add_AT_wide (die, DW_AT_const_value, w);
18697 return true;
18699 case CONST_DOUBLE:
18700 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18701 floating-point constant. A CONST_DOUBLE is used whenever the
18702 constant requires more than one word in order to be adequately
18703 represented. */
18704 if (TARGET_SUPPORTS_WIDE_INT == 0
18705 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18706 add_AT_double (die, DW_AT_const_value,
18707 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18708 else
18710 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18711 unsigned int length = GET_MODE_SIZE (mode);
18712 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18714 insert_float (rtl, array);
18715 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18717 return true;
18719 case CONST_VECTOR:
18721 machine_mode mode = GET_MODE (rtl);
18722 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18723 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18724 unsigned char *array
18725 = ggc_vec_alloc<unsigned char> (length * elt_size);
18726 unsigned int i;
18727 unsigned char *p;
18728 machine_mode imode = GET_MODE_INNER (mode);
18730 switch (GET_MODE_CLASS (mode))
18732 case MODE_VECTOR_INT:
18733 for (i = 0, p = array; i < length; i++, p += elt_size)
18735 rtx elt = CONST_VECTOR_ELT (rtl, i);
18736 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18738 break;
18740 case MODE_VECTOR_FLOAT:
18741 for (i = 0, p = array; i < length; i++, p += elt_size)
18743 rtx elt = CONST_VECTOR_ELT (rtl, i);
18744 insert_float (elt, p);
18746 break;
18748 default:
18749 gcc_unreachable ();
18752 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18754 return true;
18756 case CONST_STRING:
18757 if (dwarf_version >= 4 || !dwarf_strict)
18759 dw_loc_descr_ref loc_result;
18760 resolve_one_addr (&rtl);
18761 rtl_addr:
18762 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18763 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18764 add_AT_loc (die, DW_AT_location, loc_result);
18765 vec_safe_push (used_rtx_array, rtl);
18766 return true;
18768 return false;
18770 case CONST:
18771 if (CONSTANT_P (XEXP (rtl, 0)))
18772 return add_const_value_attribute (die, XEXP (rtl, 0));
18773 /* FALLTHROUGH */
18774 case SYMBOL_REF:
18775 if (!const_ok_for_output (rtl))
18776 return false;
18777 /* FALLTHROUGH */
18778 case LABEL_REF:
18779 if (dwarf_version >= 4 || !dwarf_strict)
18780 goto rtl_addr;
18781 return false;
18783 case PLUS:
18784 /* In cases where an inlined instance of an inline function is passed
18785 the address of an `auto' variable (which is local to the caller) we
18786 can get a situation where the DECL_RTL of the artificial local
18787 variable (for the inlining) which acts as a stand-in for the
18788 corresponding formal parameter (of the inline function) will look
18789 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18790 exactly a compile-time constant expression, but it isn't the address
18791 of the (artificial) local variable either. Rather, it represents the
18792 *value* which the artificial local variable always has during its
18793 lifetime. We currently have no way to represent such quasi-constant
18794 values in Dwarf, so for now we just punt and generate nothing. */
18795 return false;
18797 case HIGH:
18798 case CONST_FIXED:
18799 return false;
18801 case MEM:
18802 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18803 && MEM_READONLY_P (rtl)
18804 && GET_MODE (rtl) == BLKmode)
18806 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18807 return true;
18809 return false;
18811 default:
18812 /* No other kinds of rtx should be possible here. */
18813 gcc_unreachable ();
18815 return false;
18818 /* Determine whether the evaluation of EXPR references any variables
18819 or functions which aren't otherwise used (and therefore may not be
18820 output). */
18821 static tree
18822 reference_to_unused (tree * tp, int * walk_subtrees,
18823 void * data ATTRIBUTE_UNUSED)
18825 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18826 *walk_subtrees = 0;
18828 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18829 && ! TREE_ASM_WRITTEN (*tp))
18830 return *tp;
18831 /* ??? The C++ FE emits debug information for using decls, so
18832 putting gcc_unreachable here falls over. See PR31899. For now
18833 be conservative. */
18834 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18835 return *tp;
18836 else if (VAR_P (*tp))
18838 varpool_node *node = varpool_node::get (*tp);
18839 if (!node || !node->definition)
18840 return *tp;
18842 else if (TREE_CODE (*tp) == FUNCTION_DECL
18843 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18845 /* The call graph machinery must have finished analyzing,
18846 optimizing and gimplifying the CU by now.
18847 So if *TP has no call graph node associated
18848 to it, it means *TP will not be emitted. */
18849 if (!cgraph_node::get (*tp))
18850 return *tp;
18852 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18853 return *tp;
18855 return NULL_TREE;
18858 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18859 for use in a later add_const_value_attribute call. */
18861 static rtx
18862 rtl_for_decl_init (tree init, tree type)
18864 rtx rtl = NULL_RTX;
18866 STRIP_NOPS (init);
18868 /* If a variable is initialized with a string constant without embedded
18869 zeros, build CONST_STRING. */
18870 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18872 tree enttype = TREE_TYPE (type);
18873 tree domain = TYPE_DOMAIN (type);
18874 scalar_int_mode mode;
18876 if (is_int_mode (TYPE_MODE (enttype), &mode)
18877 && GET_MODE_SIZE (mode) == 1
18878 && domain
18879 && integer_zerop (TYPE_MIN_VALUE (domain))
18880 && compare_tree_int (TYPE_MAX_VALUE (domain),
18881 TREE_STRING_LENGTH (init) - 1) == 0
18882 && ((size_t) TREE_STRING_LENGTH (init)
18883 == strlen (TREE_STRING_POINTER (init)) + 1))
18885 rtl = gen_rtx_CONST_STRING (VOIDmode,
18886 ggc_strdup (TREE_STRING_POINTER (init)));
18887 rtl = gen_rtx_MEM (BLKmode, rtl);
18888 MEM_READONLY_P (rtl) = 1;
18891 /* Other aggregates, and complex values, could be represented using
18892 CONCAT: FIXME! */
18893 else if (AGGREGATE_TYPE_P (type)
18894 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18895 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18896 || TREE_CODE (type) == COMPLEX_TYPE)
18898 /* Vectors only work if their mode is supported by the target.
18899 FIXME: generic vectors ought to work too. */
18900 else if (TREE_CODE (type) == VECTOR_TYPE
18901 && !VECTOR_MODE_P (TYPE_MODE (type)))
18903 /* If the initializer is something that we know will expand into an
18904 immediate RTL constant, expand it now. We must be careful not to
18905 reference variables which won't be output. */
18906 else if (initializer_constant_valid_p (init, type)
18907 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18909 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18910 possible. */
18911 if (TREE_CODE (type) == VECTOR_TYPE)
18912 switch (TREE_CODE (init))
18914 case VECTOR_CST:
18915 break;
18916 case CONSTRUCTOR:
18917 if (TREE_CONSTANT (init))
18919 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18920 bool constant_p = true;
18921 tree value;
18922 unsigned HOST_WIDE_INT ix;
18924 /* Even when ctor is constant, it might contain non-*_CST
18925 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18926 belong into VECTOR_CST nodes. */
18927 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18928 if (!CONSTANT_CLASS_P (value))
18930 constant_p = false;
18931 break;
18934 if (constant_p)
18936 init = build_vector_from_ctor (type, elts);
18937 break;
18940 /* FALLTHRU */
18942 default:
18943 return NULL;
18946 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18948 /* If expand_expr returns a MEM, it wasn't immediate. */
18949 gcc_assert (!rtl || !MEM_P (rtl));
18952 return rtl;
18955 /* Generate RTL for the variable DECL to represent its location. */
18957 static rtx
18958 rtl_for_decl_location (tree decl)
18960 rtx rtl;
18962 /* Here we have to decide where we are going to say the parameter "lives"
18963 (as far as the debugger is concerned). We only have a couple of
18964 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18966 DECL_RTL normally indicates where the parameter lives during most of the
18967 activation of the function. If optimization is enabled however, this
18968 could be either NULL or else a pseudo-reg. Both of those cases indicate
18969 that the parameter doesn't really live anywhere (as far as the code
18970 generation parts of GCC are concerned) during most of the function's
18971 activation. That will happen (for example) if the parameter is never
18972 referenced within the function.
18974 We could just generate a location descriptor here for all non-NULL
18975 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18976 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18977 where DECL_RTL is NULL or is a pseudo-reg.
18979 Note however that we can only get away with using DECL_INCOMING_RTL as
18980 a backup substitute for DECL_RTL in certain limited cases. In cases
18981 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18982 we can be sure that the parameter was passed using the same type as it is
18983 declared to have within the function, and that its DECL_INCOMING_RTL
18984 points us to a place where a value of that type is passed.
18986 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18987 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18988 because in these cases DECL_INCOMING_RTL points us to a value of some
18989 type which is *different* from the type of the parameter itself. Thus,
18990 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18991 such cases, the debugger would end up (for example) trying to fetch a
18992 `float' from a place which actually contains the first part of a
18993 `double'. That would lead to really incorrect and confusing
18994 output at debug-time.
18996 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18997 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18998 are a couple of exceptions however. On little-endian machines we can
18999 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19000 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19001 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19002 when (on a little-endian machine) a non-prototyped function has a
19003 parameter declared to be of type `short' or `char'. In such cases,
19004 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19005 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19006 passed `int' value. If the debugger then uses that address to fetch
19007 a `short' or a `char' (on a little-endian machine) the result will be
19008 the correct data, so we allow for such exceptional cases below.
19010 Note that our goal here is to describe the place where the given formal
19011 parameter lives during most of the function's activation (i.e. between the
19012 end of the prologue and the start of the epilogue). We'll do that as best
19013 as we can. Note however that if the given formal parameter is modified
19014 sometime during the execution of the function, then a stack backtrace (at
19015 debug-time) will show the function as having been called with the *new*
19016 value rather than the value which was originally passed in. This happens
19017 rarely enough that it is not a major problem, but it *is* a problem, and
19018 I'd like to fix it.
19020 A future version of dwarf2out.c may generate two additional attributes for
19021 any given DW_TAG_formal_parameter DIE which will describe the "passed
19022 type" and the "passed location" for the given formal parameter in addition
19023 to the attributes we now generate to indicate the "declared type" and the
19024 "active location" for each parameter. This additional set of attributes
19025 could be used by debuggers for stack backtraces. Separately, note that
19026 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19027 This happens (for example) for inlined-instances of inline function formal
19028 parameters which are never referenced. This really shouldn't be
19029 happening. All PARM_DECL nodes should get valid non-NULL
19030 DECL_INCOMING_RTL values. FIXME. */
19032 /* Use DECL_RTL as the "location" unless we find something better. */
19033 rtl = DECL_RTL_IF_SET (decl);
19035 /* When generating abstract instances, ignore everything except
19036 constants, symbols living in memory, and symbols living in
19037 fixed registers. */
19038 if (! reload_completed)
19040 if (rtl
19041 && (CONSTANT_P (rtl)
19042 || (MEM_P (rtl)
19043 && CONSTANT_P (XEXP (rtl, 0)))
19044 || (REG_P (rtl)
19045 && VAR_P (decl)
19046 && TREE_STATIC (decl))))
19048 rtl = targetm.delegitimize_address (rtl);
19049 return rtl;
19051 rtl = NULL_RTX;
19053 else if (TREE_CODE (decl) == PARM_DECL)
19055 if (rtl == NULL_RTX
19056 || is_pseudo_reg (rtl)
19057 || (MEM_P (rtl)
19058 && is_pseudo_reg (XEXP (rtl, 0))
19059 && DECL_INCOMING_RTL (decl)
19060 && MEM_P (DECL_INCOMING_RTL (decl))
19061 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19063 tree declared_type = TREE_TYPE (decl);
19064 tree passed_type = DECL_ARG_TYPE (decl);
19065 machine_mode dmode = TYPE_MODE (declared_type);
19066 machine_mode pmode = TYPE_MODE (passed_type);
19068 /* This decl represents a formal parameter which was optimized out.
19069 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19070 all cases where (rtl == NULL_RTX) just below. */
19071 if (dmode == pmode)
19072 rtl = DECL_INCOMING_RTL (decl);
19073 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19074 && SCALAR_INT_MODE_P (dmode)
19075 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19076 && DECL_INCOMING_RTL (decl))
19078 rtx inc = DECL_INCOMING_RTL (decl);
19079 if (REG_P (inc))
19080 rtl = inc;
19081 else if (MEM_P (inc))
19083 if (BYTES_BIG_ENDIAN)
19084 rtl = adjust_address_nv (inc, dmode,
19085 GET_MODE_SIZE (pmode)
19086 - GET_MODE_SIZE (dmode));
19087 else
19088 rtl = inc;
19093 /* If the parm was passed in registers, but lives on the stack, then
19094 make a big endian correction if the mode of the type of the
19095 parameter is not the same as the mode of the rtl. */
19096 /* ??? This is the same series of checks that are made in dbxout.c before
19097 we reach the big endian correction code there. It isn't clear if all
19098 of these checks are necessary here, but keeping them all is the safe
19099 thing to do. */
19100 else if (MEM_P (rtl)
19101 && XEXP (rtl, 0) != const0_rtx
19102 && ! CONSTANT_P (XEXP (rtl, 0))
19103 /* Not passed in memory. */
19104 && !MEM_P (DECL_INCOMING_RTL (decl))
19105 /* Not passed by invisible reference. */
19106 && (!REG_P (XEXP (rtl, 0))
19107 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19108 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19109 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19110 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19111 #endif
19113 /* Big endian correction check. */
19114 && BYTES_BIG_ENDIAN
19115 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19116 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19117 < UNITS_PER_WORD))
19119 machine_mode addr_mode = get_address_mode (rtl);
19120 int offset = (UNITS_PER_WORD
19121 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19123 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19124 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19127 else if (VAR_P (decl)
19128 && rtl
19129 && MEM_P (rtl)
19130 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19132 machine_mode addr_mode = get_address_mode (rtl);
19133 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19134 GET_MODE (rtl));
19136 /* If a variable is declared "register" yet is smaller than
19137 a register, then if we store the variable to memory, it
19138 looks like we're storing a register-sized value, when in
19139 fact we are not. We need to adjust the offset of the
19140 storage location to reflect the actual value's bytes,
19141 else gdb will not be able to display it. */
19142 if (offset != 0)
19143 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19144 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19147 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19148 and will have been substituted directly into all expressions that use it.
19149 C does not have such a concept, but C++ and other languages do. */
19150 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19151 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19153 if (rtl)
19154 rtl = targetm.delegitimize_address (rtl);
19156 /* If we don't look past the constant pool, we risk emitting a
19157 reference to a constant pool entry that isn't referenced from
19158 code, and thus is not emitted. */
19159 if (rtl)
19160 rtl = avoid_constant_pool_reference (rtl);
19162 /* Try harder to get a rtl. If this symbol ends up not being emitted
19163 in the current CU, resolve_addr will remove the expression referencing
19164 it. */
19165 if (rtl == NULL_RTX
19166 && VAR_P (decl)
19167 && !DECL_EXTERNAL (decl)
19168 && TREE_STATIC (decl)
19169 && DECL_NAME (decl)
19170 && !DECL_HARD_REGISTER (decl)
19171 && DECL_MODE (decl) != VOIDmode)
19173 rtl = make_decl_rtl_for_debug (decl);
19174 if (!MEM_P (rtl)
19175 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19176 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19177 rtl = NULL_RTX;
19180 return rtl;
19183 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19184 returned. If so, the decl for the COMMON block is returned, and the
19185 value is the offset into the common block for the symbol. */
19187 static tree
19188 fortran_common (tree decl, HOST_WIDE_INT *value)
19190 tree val_expr, cvar;
19191 machine_mode mode;
19192 HOST_WIDE_INT bitsize, bitpos;
19193 tree offset;
19194 int unsignedp, reversep, volatilep = 0;
19196 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19197 it does not have a value (the offset into the common area), or if it
19198 is thread local (as opposed to global) then it isn't common, and shouldn't
19199 be handled as such. */
19200 if (!VAR_P (decl)
19201 || !TREE_STATIC (decl)
19202 || !DECL_HAS_VALUE_EXPR_P (decl)
19203 || !is_fortran ())
19204 return NULL_TREE;
19206 val_expr = DECL_VALUE_EXPR (decl);
19207 if (TREE_CODE (val_expr) != COMPONENT_REF)
19208 return NULL_TREE;
19210 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19211 &unsignedp, &reversep, &volatilep);
19213 if (cvar == NULL_TREE
19214 || !VAR_P (cvar)
19215 || DECL_ARTIFICIAL (cvar)
19216 || !TREE_PUBLIC (cvar))
19217 return NULL_TREE;
19219 *value = 0;
19220 if (offset != NULL)
19222 if (!tree_fits_shwi_p (offset))
19223 return NULL_TREE;
19224 *value = tree_to_shwi (offset);
19226 if (bitpos != 0)
19227 *value += bitpos / BITS_PER_UNIT;
19229 return cvar;
19232 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19233 data attribute for a variable or a parameter. We generate the
19234 DW_AT_const_value attribute only in those cases where the given variable
19235 or parameter does not have a true "location" either in memory or in a
19236 register. This can happen (for example) when a constant is passed as an
19237 actual argument in a call to an inline function. (It's possible that
19238 these things can crop up in other ways also.) Note that one type of
19239 constant value which can be passed into an inlined function is a constant
19240 pointer. This can happen for example if an actual argument in an inlined
19241 function call evaluates to a compile-time constant address.
19243 CACHE_P is true if it is worth caching the location list for DECL,
19244 so that future calls can reuse it rather than regenerate it from scratch.
19245 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19246 since we will need to refer to them each time the function is inlined. */
19248 static bool
19249 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19251 rtx rtl;
19252 dw_loc_list_ref list;
19253 var_loc_list *loc_list;
19254 cached_dw_loc_list *cache;
19256 if (early_dwarf)
19257 return false;
19259 if (TREE_CODE (decl) == ERROR_MARK)
19260 return false;
19262 if (get_AT (die, DW_AT_location)
19263 || get_AT (die, DW_AT_const_value))
19264 return true;
19266 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19267 || TREE_CODE (decl) == RESULT_DECL);
19269 /* Try to get some constant RTL for this decl, and use that as the value of
19270 the location. */
19272 rtl = rtl_for_decl_location (decl);
19273 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19274 && add_const_value_attribute (die, rtl))
19275 return true;
19277 /* See if we have single element location list that is equivalent to
19278 a constant value. That way we are better to use add_const_value_attribute
19279 rather than expanding constant value equivalent. */
19280 loc_list = lookup_decl_loc (decl);
19281 if (loc_list
19282 && loc_list->first
19283 && loc_list->first->next == NULL
19284 && NOTE_P (loc_list->first->loc)
19285 && NOTE_VAR_LOCATION (loc_list->first->loc)
19286 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19288 struct var_loc_node *node;
19290 node = loc_list->first;
19291 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19292 if (GET_CODE (rtl) == EXPR_LIST)
19293 rtl = XEXP (rtl, 0);
19294 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19295 && add_const_value_attribute (die, rtl))
19296 return true;
19298 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19299 list several times. See if we've already cached the contents. */
19300 list = NULL;
19301 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19302 cache_p = false;
19303 if (cache_p)
19305 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19306 if (cache)
19307 list = cache->loc_list;
19309 if (list == NULL)
19311 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19312 NULL);
19313 /* It is usually worth caching this result if the decl is from
19314 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19315 if (cache_p && list && list->dw_loc_next)
19317 cached_dw_loc_list **slot
19318 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19319 DECL_UID (decl),
19320 INSERT);
19321 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19322 cache->decl_id = DECL_UID (decl);
19323 cache->loc_list = list;
19324 *slot = cache;
19327 if (list)
19329 add_AT_location_description (die, DW_AT_location, list);
19330 return true;
19332 /* None of that worked, so it must not really have a location;
19333 try adding a constant value attribute from the DECL_INITIAL. */
19334 return tree_add_const_value_attribute_for_decl (die, decl);
19337 /* Helper function for tree_add_const_value_attribute. Natively encode
19338 initializer INIT into an array. Return true if successful. */
19340 static bool
19341 native_encode_initializer (tree init, unsigned char *array, int size)
19343 tree type;
19345 if (init == NULL_TREE)
19346 return false;
19348 STRIP_NOPS (init);
19349 switch (TREE_CODE (init))
19351 case STRING_CST:
19352 type = TREE_TYPE (init);
19353 if (TREE_CODE (type) == ARRAY_TYPE)
19355 tree enttype = TREE_TYPE (type);
19356 scalar_int_mode mode;
19358 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19359 || GET_MODE_SIZE (mode) != 1)
19360 return false;
19361 if (int_size_in_bytes (type) != size)
19362 return false;
19363 if (size > TREE_STRING_LENGTH (init))
19365 memcpy (array, TREE_STRING_POINTER (init),
19366 TREE_STRING_LENGTH (init));
19367 memset (array + TREE_STRING_LENGTH (init),
19368 '\0', size - TREE_STRING_LENGTH (init));
19370 else
19371 memcpy (array, TREE_STRING_POINTER (init), size);
19372 return true;
19374 return false;
19375 case CONSTRUCTOR:
19376 type = TREE_TYPE (init);
19377 if (int_size_in_bytes (type) != size)
19378 return false;
19379 if (TREE_CODE (type) == ARRAY_TYPE)
19381 HOST_WIDE_INT min_index;
19382 unsigned HOST_WIDE_INT cnt;
19383 int curpos = 0, fieldsize;
19384 constructor_elt *ce;
19386 if (TYPE_DOMAIN (type) == NULL_TREE
19387 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19388 return false;
19390 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19391 if (fieldsize <= 0)
19392 return false;
19394 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19395 memset (array, '\0', size);
19396 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19398 tree val = ce->value;
19399 tree index = ce->index;
19400 int pos = curpos;
19401 if (index && TREE_CODE (index) == RANGE_EXPR)
19402 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19403 * fieldsize;
19404 else if (index)
19405 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19407 if (val)
19409 STRIP_NOPS (val);
19410 if (!native_encode_initializer (val, array + pos, fieldsize))
19411 return false;
19413 curpos = pos + fieldsize;
19414 if (index && TREE_CODE (index) == RANGE_EXPR)
19416 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19417 - tree_to_shwi (TREE_OPERAND (index, 0));
19418 while (count-- > 0)
19420 if (val)
19421 memcpy (array + curpos, array + pos, fieldsize);
19422 curpos += fieldsize;
19425 gcc_assert (curpos <= size);
19427 return true;
19429 else if (TREE_CODE (type) == RECORD_TYPE
19430 || TREE_CODE (type) == UNION_TYPE)
19432 tree field = NULL_TREE;
19433 unsigned HOST_WIDE_INT cnt;
19434 constructor_elt *ce;
19436 if (int_size_in_bytes (type) != size)
19437 return false;
19439 if (TREE_CODE (type) == RECORD_TYPE)
19440 field = TYPE_FIELDS (type);
19442 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19444 tree val = ce->value;
19445 int pos, fieldsize;
19447 if (ce->index != 0)
19448 field = ce->index;
19450 if (val)
19451 STRIP_NOPS (val);
19453 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19454 return false;
19456 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19457 && TYPE_DOMAIN (TREE_TYPE (field))
19458 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19459 return false;
19460 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19461 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19462 return false;
19463 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19464 pos = int_byte_position (field);
19465 gcc_assert (pos + fieldsize <= size);
19466 if (val && fieldsize != 0
19467 && !native_encode_initializer (val, array + pos, fieldsize))
19468 return false;
19470 return true;
19472 return false;
19473 case VIEW_CONVERT_EXPR:
19474 case NON_LVALUE_EXPR:
19475 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19476 default:
19477 return native_encode_expr (init, array, size) == size;
19481 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19482 attribute is the const value T. */
19484 static bool
19485 tree_add_const_value_attribute (dw_die_ref die, tree t)
19487 tree init;
19488 tree type = TREE_TYPE (t);
19489 rtx rtl;
19491 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19492 return false;
19494 init = t;
19495 gcc_assert (!DECL_P (init));
19497 if (TREE_CODE (init) == INTEGER_CST)
19499 if (tree_fits_uhwi_p (init))
19501 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19502 return true;
19504 if (tree_fits_shwi_p (init))
19506 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19507 return true;
19510 if (! early_dwarf)
19512 rtl = rtl_for_decl_init (init, type);
19513 if (rtl)
19514 return add_const_value_attribute (die, rtl);
19516 /* If the host and target are sane, try harder. */
19517 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19518 && initializer_constant_valid_p (init, type))
19520 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19521 if (size > 0 && (int) size == size)
19523 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19525 if (native_encode_initializer (init, array, size))
19527 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19528 return true;
19530 ggc_free (array);
19533 return false;
19536 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19537 attribute is the const value of T, where T is an integral constant
19538 variable with static storage duration
19539 (so it can't be a PARM_DECL or a RESULT_DECL). */
19541 static bool
19542 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19545 if (!decl
19546 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19547 || (VAR_P (decl) && !TREE_STATIC (decl)))
19548 return false;
19550 if (TREE_READONLY (decl)
19551 && ! TREE_THIS_VOLATILE (decl)
19552 && DECL_INITIAL (decl))
19553 /* OK */;
19554 else
19555 return false;
19557 /* Don't add DW_AT_const_value if abstract origin already has one. */
19558 if (get_AT (var_die, DW_AT_const_value))
19559 return false;
19561 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19564 /* Convert the CFI instructions for the current function into a
19565 location list. This is used for DW_AT_frame_base when we targeting
19566 a dwarf2 consumer that does not support the dwarf3
19567 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19568 expressions. */
19570 static dw_loc_list_ref
19571 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19573 int ix;
19574 dw_fde_ref fde;
19575 dw_loc_list_ref list, *list_tail;
19576 dw_cfi_ref cfi;
19577 dw_cfa_location last_cfa, next_cfa;
19578 const char *start_label, *last_label, *section;
19579 dw_cfa_location remember;
19581 fde = cfun->fde;
19582 gcc_assert (fde != NULL);
19584 section = secname_for_decl (current_function_decl);
19585 list_tail = &list;
19586 list = NULL;
19588 memset (&next_cfa, 0, sizeof (next_cfa));
19589 next_cfa.reg = INVALID_REGNUM;
19590 remember = next_cfa;
19592 start_label = fde->dw_fde_begin;
19594 /* ??? Bald assumption that the CIE opcode list does not contain
19595 advance opcodes. */
19596 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19597 lookup_cfa_1 (cfi, &next_cfa, &remember);
19599 last_cfa = next_cfa;
19600 last_label = start_label;
19602 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19604 /* If the first partition contained no CFI adjustments, the
19605 CIE opcodes apply to the whole first partition. */
19606 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19607 fde->dw_fde_begin, fde->dw_fde_end, section);
19608 list_tail =&(*list_tail)->dw_loc_next;
19609 start_label = last_label = fde->dw_fde_second_begin;
19612 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19614 switch (cfi->dw_cfi_opc)
19616 case DW_CFA_set_loc:
19617 case DW_CFA_advance_loc1:
19618 case DW_CFA_advance_loc2:
19619 case DW_CFA_advance_loc4:
19620 if (!cfa_equal_p (&last_cfa, &next_cfa))
19622 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19623 start_label, last_label, section);
19625 list_tail = &(*list_tail)->dw_loc_next;
19626 last_cfa = next_cfa;
19627 start_label = last_label;
19629 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19630 break;
19632 case DW_CFA_advance_loc:
19633 /* The encoding is complex enough that we should never emit this. */
19634 gcc_unreachable ();
19636 default:
19637 lookup_cfa_1 (cfi, &next_cfa, &remember);
19638 break;
19640 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19642 if (!cfa_equal_p (&last_cfa, &next_cfa))
19644 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19645 start_label, last_label, section);
19647 list_tail = &(*list_tail)->dw_loc_next;
19648 last_cfa = next_cfa;
19649 start_label = last_label;
19651 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19652 start_label, fde->dw_fde_end, section);
19653 list_tail = &(*list_tail)->dw_loc_next;
19654 start_label = last_label = fde->dw_fde_second_begin;
19658 if (!cfa_equal_p (&last_cfa, &next_cfa))
19660 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19661 start_label, last_label, section);
19662 list_tail = &(*list_tail)->dw_loc_next;
19663 start_label = last_label;
19666 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19667 start_label,
19668 fde->dw_fde_second_begin
19669 ? fde->dw_fde_second_end : fde->dw_fde_end,
19670 section);
19672 if (list && list->dw_loc_next)
19673 gen_llsym (list);
19675 return list;
19678 /* Compute a displacement from the "steady-state frame pointer" to the
19679 frame base (often the same as the CFA), and store it in
19680 frame_pointer_fb_offset. OFFSET is added to the displacement
19681 before the latter is negated. */
19683 static void
19684 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19686 rtx reg, elim;
19688 #ifdef FRAME_POINTER_CFA_OFFSET
19689 reg = frame_pointer_rtx;
19690 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19691 #else
19692 reg = arg_pointer_rtx;
19693 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19694 #endif
19696 elim = (ira_use_lra_p
19697 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19698 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19699 if (GET_CODE (elim) == PLUS)
19701 offset += INTVAL (XEXP (elim, 1));
19702 elim = XEXP (elim, 0);
19705 frame_pointer_fb_offset = -offset;
19707 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19708 in which to eliminate. This is because it's stack pointer isn't
19709 directly accessible as a register within the ISA. To work around
19710 this, assume that while we cannot provide a proper value for
19711 frame_pointer_fb_offset, we won't need one either. */
19712 frame_pointer_fb_offset_valid
19713 = ((SUPPORTS_STACK_ALIGNMENT
19714 && (elim == hard_frame_pointer_rtx
19715 || elim == stack_pointer_rtx))
19716 || elim == (frame_pointer_needed
19717 ? hard_frame_pointer_rtx
19718 : stack_pointer_rtx));
19721 /* Generate a DW_AT_name attribute given some string value to be included as
19722 the value of the attribute. */
19724 static void
19725 add_name_attribute (dw_die_ref die, const char *name_string)
19727 if (name_string != NULL && *name_string != 0)
19729 if (demangle_name_func)
19730 name_string = (*demangle_name_func) (name_string);
19732 add_AT_string (die, DW_AT_name, name_string);
19736 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19737 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19738 of TYPE accordingly.
19740 ??? This is a temporary measure until after we're able to generate
19741 regular DWARF for the complex Ada type system. */
19743 static void
19744 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19745 dw_die_ref context_die)
19747 tree dtype;
19748 dw_die_ref dtype_die;
19750 if (!lang_hooks.types.descriptive_type)
19751 return;
19753 dtype = lang_hooks.types.descriptive_type (type);
19754 if (!dtype)
19755 return;
19757 dtype_die = lookup_type_die (dtype);
19758 if (!dtype_die)
19760 gen_type_die (dtype, context_die);
19761 dtype_die = lookup_type_die (dtype);
19762 gcc_assert (dtype_die);
19765 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19768 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19770 static const char *
19771 comp_dir_string (void)
19773 const char *wd;
19774 char *wd1;
19775 static const char *cached_wd = NULL;
19777 if (cached_wd != NULL)
19778 return cached_wd;
19780 wd = get_src_pwd ();
19781 if (wd == NULL)
19782 return NULL;
19784 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19786 int wdlen;
19788 wdlen = strlen (wd);
19789 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19790 strcpy (wd1, wd);
19791 wd1 [wdlen] = DIR_SEPARATOR;
19792 wd1 [wdlen + 1] = 0;
19793 wd = wd1;
19796 cached_wd = remap_debug_filename (wd);
19797 return cached_wd;
19800 /* Generate a DW_AT_comp_dir attribute for DIE. */
19802 static void
19803 add_comp_dir_attribute (dw_die_ref die)
19805 const char * wd = comp_dir_string ();
19806 if (wd != NULL)
19807 add_AT_string (die, DW_AT_comp_dir, wd);
19810 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19811 pointer computation, ...), output a representation for that bound according
19812 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19813 loc_list_from_tree for the meaning of CONTEXT. */
19815 static void
19816 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19817 int forms, struct loc_descr_context *context)
19819 dw_die_ref context_die, decl_die;
19820 dw_loc_list_ref list;
19821 bool strip_conversions = true;
19822 bool placeholder_seen = false;
19824 while (strip_conversions)
19825 switch (TREE_CODE (value))
19827 case ERROR_MARK:
19828 case SAVE_EXPR:
19829 return;
19831 CASE_CONVERT:
19832 case VIEW_CONVERT_EXPR:
19833 value = TREE_OPERAND (value, 0);
19834 break;
19836 default:
19837 strip_conversions = false;
19838 break;
19841 /* If possible and permitted, output the attribute as a constant. */
19842 if ((forms & dw_scalar_form_constant) != 0
19843 && TREE_CODE (value) == INTEGER_CST)
19845 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19847 /* If HOST_WIDE_INT is big enough then represent the bound as
19848 a constant value. We need to choose a form based on
19849 whether the type is signed or unsigned. We cannot just
19850 call add_AT_unsigned if the value itself is positive
19851 (add_AT_unsigned might add the unsigned value encoded as
19852 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19853 bounds type and then sign extend any unsigned values found
19854 for signed types. This is needed only for
19855 DW_AT_{lower,upper}_bound, since for most other attributes,
19856 consumers will treat DW_FORM_data[1248] as unsigned values,
19857 regardless of the underlying type. */
19858 if (prec <= HOST_BITS_PER_WIDE_INT
19859 || tree_fits_uhwi_p (value))
19861 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19862 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19863 else
19864 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19866 else
19867 /* Otherwise represent the bound as an unsigned value with
19868 the precision of its type. The precision and signedness
19869 of the type will be necessary to re-interpret it
19870 unambiguously. */
19871 add_AT_wide (die, attr, wi::to_wide (value));
19872 return;
19875 /* Otherwise, if it's possible and permitted too, output a reference to
19876 another DIE. */
19877 if ((forms & dw_scalar_form_reference) != 0)
19879 tree decl = NULL_TREE;
19881 /* Some type attributes reference an outer type. For instance, the upper
19882 bound of an array may reference an embedding record (this happens in
19883 Ada). */
19884 if (TREE_CODE (value) == COMPONENT_REF
19885 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19886 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19887 decl = TREE_OPERAND (value, 1);
19889 else if (VAR_P (value)
19890 || TREE_CODE (value) == PARM_DECL
19891 || TREE_CODE (value) == RESULT_DECL)
19892 decl = value;
19894 if (decl != NULL_TREE)
19896 dw_die_ref decl_die = lookup_decl_die (decl);
19898 /* ??? Can this happen, or should the variable have been bound
19899 first? Probably it can, since I imagine that we try to create
19900 the types of parameters in the order in which they exist in
19901 the list, and won't have created a forward reference to a
19902 later parameter. */
19903 if (decl_die != NULL)
19905 add_AT_die_ref (die, attr, decl_die);
19906 return;
19911 /* Last chance: try to create a stack operation procedure to evaluate the
19912 value. Do nothing if even that is not possible or permitted. */
19913 if ((forms & dw_scalar_form_exprloc) == 0)
19914 return;
19916 list = loc_list_from_tree (value, 2, context);
19917 if (context && context->placeholder_arg)
19919 placeholder_seen = context->placeholder_seen;
19920 context->placeholder_seen = false;
19922 if (list == NULL || single_element_loc_list_p (list))
19924 /* If this attribute is not a reference nor constant, it is
19925 a DWARF expression rather than location description. For that
19926 loc_list_from_tree (value, 0, &context) is needed. */
19927 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19928 if (list2 && single_element_loc_list_p (list2))
19930 if (placeholder_seen)
19932 struct dwarf_procedure_info dpi;
19933 dpi.fndecl = NULL_TREE;
19934 dpi.args_count = 1;
19935 if (!resolve_args_picking (list2->expr, 1, &dpi))
19936 return;
19938 add_AT_loc (die, attr, list2->expr);
19939 return;
19943 /* If that failed to give a single element location list, fall back to
19944 outputting this as a reference... still if permitted. */
19945 if (list == NULL
19946 || (forms & dw_scalar_form_reference) == 0
19947 || placeholder_seen)
19948 return;
19950 if (current_function_decl == 0)
19951 context_die = comp_unit_die ();
19952 else
19953 context_die = lookup_decl_die (current_function_decl);
19955 decl_die = new_die (DW_TAG_variable, context_die, value);
19956 add_AT_flag (decl_die, DW_AT_artificial, 1);
19957 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19958 context_die);
19959 add_AT_location_description (decl_die, DW_AT_location, list);
19960 add_AT_die_ref (die, attr, decl_die);
19963 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19964 default. */
19966 static int
19967 lower_bound_default (void)
19969 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19971 case DW_LANG_C:
19972 case DW_LANG_C89:
19973 case DW_LANG_C99:
19974 case DW_LANG_C11:
19975 case DW_LANG_C_plus_plus:
19976 case DW_LANG_C_plus_plus_11:
19977 case DW_LANG_C_plus_plus_14:
19978 case DW_LANG_ObjC:
19979 case DW_LANG_ObjC_plus_plus:
19980 return 0;
19981 case DW_LANG_Fortran77:
19982 case DW_LANG_Fortran90:
19983 case DW_LANG_Fortran95:
19984 case DW_LANG_Fortran03:
19985 case DW_LANG_Fortran08:
19986 return 1;
19987 case DW_LANG_UPC:
19988 case DW_LANG_D:
19989 case DW_LANG_Python:
19990 return dwarf_version >= 4 ? 0 : -1;
19991 case DW_LANG_Ada95:
19992 case DW_LANG_Ada83:
19993 case DW_LANG_Cobol74:
19994 case DW_LANG_Cobol85:
19995 case DW_LANG_Modula2:
19996 case DW_LANG_PLI:
19997 return dwarf_version >= 4 ? 1 : -1;
19998 default:
19999 return -1;
20003 /* Given a tree node describing an array bound (either lower or upper) output
20004 a representation for that bound. */
20006 static void
20007 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20008 tree bound, struct loc_descr_context *context)
20010 int dflt;
20012 while (1)
20013 switch (TREE_CODE (bound))
20015 /* Strip all conversions. */
20016 CASE_CONVERT:
20017 case VIEW_CONVERT_EXPR:
20018 bound = TREE_OPERAND (bound, 0);
20019 break;
20021 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20022 are even omitted when they are the default. */
20023 case INTEGER_CST:
20024 /* If the value for this bound is the default one, we can even omit the
20025 attribute. */
20026 if (bound_attr == DW_AT_lower_bound
20027 && tree_fits_shwi_p (bound)
20028 && (dflt = lower_bound_default ()) != -1
20029 && tree_to_shwi (bound) == dflt)
20030 return;
20032 /* FALLTHRU */
20034 default:
20035 /* Because of the complex interaction there can be with other GNAT
20036 encodings, GDB isn't ready yet to handle proper DWARF description
20037 for self-referencial subrange bounds: let GNAT encodings do the
20038 magic in such a case. */
20039 if (is_ada ()
20040 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20041 && contains_placeholder_p (bound))
20042 return;
20044 add_scalar_info (subrange_die, bound_attr, bound,
20045 dw_scalar_form_constant
20046 | dw_scalar_form_exprloc
20047 | dw_scalar_form_reference,
20048 context);
20049 return;
20053 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20054 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20055 Note that the block of subscript information for an array type also
20056 includes information about the element type of the given array type.
20058 This function reuses previously set type and bound information if
20059 available. */
20061 static void
20062 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20064 unsigned dimension_number;
20065 tree lower, upper;
20066 dw_die_ref child = type_die->die_child;
20068 for (dimension_number = 0;
20069 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20070 type = TREE_TYPE (type), dimension_number++)
20072 tree domain = TYPE_DOMAIN (type);
20074 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20075 break;
20077 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20078 and (in GNU C only) variable bounds. Handle all three forms
20079 here. */
20081 /* Find and reuse a previously generated DW_TAG_subrange_type if
20082 available.
20084 For multi-dimensional arrays, as we iterate through the
20085 various dimensions in the enclosing for loop above, we also
20086 iterate through the DIE children and pick at each
20087 DW_TAG_subrange_type previously generated (if available).
20088 Each child DW_TAG_subrange_type DIE describes the range of
20089 the current dimension. At this point we should have as many
20090 DW_TAG_subrange_type's as we have dimensions in the
20091 array. */
20092 dw_die_ref subrange_die = NULL;
20093 if (child)
20094 while (1)
20096 child = child->die_sib;
20097 if (child->die_tag == DW_TAG_subrange_type)
20098 subrange_die = child;
20099 if (child == type_die->die_child)
20101 /* If we wrapped around, stop looking next time. */
20102 child = NULL;
20103 break;
20105 if (child->die_tag == DW_TAG_subrange_type)
20106 break;
20108 if (!subrange_die)
20109 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20111 if (domain)
20113 /* We have an array type with specified bounds. */
20114 lower = TYPE_MIN_VALUE (domain);
20115 upper = TYPE_MAX_VALUE (domain);
20117 /* Define the index type. */
20118 if (TREE_TYPE (domain)
20119 && !get_AT (subrange_die, DW_AT_type))
20121 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20122 TREE_TYPE field. We can't emit debug info for this
20123 because it is an unnamed integral type. */
20124 if (TREE_CODE (domain) == INTEGER_TYPE
20125 && TYPE_NAME (domain) == NULL_TREE
20126 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20127 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20129 else
20130 add_type_attribute (subrange_die, TREE_TYPE (domain),
20131 TYPE_UNQUALIFIED, false, type_die);
20134 /* ??? If upper is NULL, the array has unspecified length,
20135 but it does have a lower bound. This happens with Fortran
20136 dimension arr(N:*)
20137 Since the debugger is definitely going to need to know N
20138 to produce useful results, go ahead and output the lower
20139 bound solo, and hope the debugger can cope. */
20141 if (!get_AT (subrange_die, DW_AT_lower_bound))
20142 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20143 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20144 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20147 /* Otherwise we have an array type with an unspecified length. The
20148 DWARF-2 spec does not say how to handle this; let's just leave out the
20149 bounds. */
20153 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20155 static void
20156 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20158 dw_die_ref decl_die;
20159 HOST_WIDE_INT size;
20160 dw_loc_descr_ref size_expr = NULL;
20162 switch (TREE_CODE (tree_node))
20164 case ERROR_MARK:
20165 size = 0;
20166 break;
20167 case ENUMERAL_TYPE:
20168 case RECORD_TYPE:
20169 case UNION_TYPE:
20170 case QUAL_UNION_TYPE:
20171 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20172 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20174 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20175 return;
20177 size_expr = type_byte_size (tree_node, &size);
20178 break;
20179 case FIELD_DECL:
20180 /* For a data member of a struct or union, the DW_AT_byte_size is
20181 generally given as the number of bytes normally allocated for an
20182 object of the *declared* type of the member itself. This is true
20183 even for bit-fields. */
20184 size = int_size_in_bytes (field_type (tree_node));
20185 break;
20186 default:
20187 gcc_unreachable ();
20190 /* Support for dynamically-sized objects was introduced by DWARFv3.
20191 At the moment, GDB does not handle variable byte sizes very well,
20192 though. */
20193 if ((dwarf_version >= 3 || !dwarf_strict)
20194 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20195 && size_expr != NULL)
20196 add_AT_loc (die, DW_AT_byte_size, size_expr);
20198 /* Note that `size' might be -1 when we get to this point. If it is, that
20199 indicates that the byte size of the entity in question is variable and
20200 that we could not generate a DWARF expression that computes it. */
20201 if (size >= 0)
20202 add_AT_unsigned (die, DW_AT_byte_size, size);
20205 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20206 alignment. */
20208 static void
20209 add_alignment_attribute (dw_die_ref die, tree tree_node)
20211 if (dwarf_version < 5 && dwarf_strict)
20212 return;
20214 unsigned align;
20216 if (DECL_P (tree_node))
20218 if (!DECL_USER_ALIGN (tree_node))
20219 return;
20221 align = DECL_ALIGN_UNIT (tree_node);
20223 else if (TYPE_P (tree_node))
20225 if (!TYPE_USER_ALIGN (tree_node))
20226 return;
20228 align = TYPE_ALIGN_UNIT (tree_node);
20230 else
20231 gcc_unreachable ();
20233 add_AT_unsigned (die, DW_AT_alignment, align);
20236 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20237 which specifies the distance in bits from the highest order bit of the
20238 "containing object" for the bit-field to the highest order bit of the
20239 bit-field itself.
20241 For any given bit-field, the "containing object" is a hypothetical object
20242 (of some integral or enum type) within which the given bit-field lives. The
20243 type of this hypothetical "containing object" is always the same as the
20244 declared type of the individual bit-field itself. The determination of the
20245 exact location of the "containing object" for a bit-field is rather
20246 complicated. It's handled by the `field_byte_offset' function (above).
20248 CTX is required: see the comment for VLR_CONTEXT.
20250 Note that it is the size (in bytes) of the hypothetical "containing object"
20251 which will be given in the DW_AT_byte_size attribute for this bit-field.
20252 (See `byte_size_attribute' above). */
20254 static inline void
20255 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20257 HOST_WIDE_INT object_offset_in_bytes;
20258 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20259 HOST_WIDE_INT bitpos_int;
20260 HOST_WIDE_INT highest_order_object_bit_offset;
20261 HOST_WIDE_INT highest_order_field_bit_offset;
20262 HOST_WIDE_INT bit_offset;
20264 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20266 /* Must be a field and a bit field. */
20267 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20269 /* We can't yet handle bit-fields whose offsets are variable, so if we
20270 encounter such things, just return without generating any attribute
20271 whatsoever. Likewise for variable or too large size. */
20272 if (! tree_fits_shwi_p (bit_position (decl))
20273 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20274 return;
20276 bitpos_int = int_bit_position (decl);
20278 /* Note that the bit offset is always the distance (in bits) from the
20279 highest-order bit of the "containing object" to the highest-order bit of
20280 the bit-field itself. Since the "high-order end" of any object or field
20281 is different on big-endian and little-endian machines, the computation
20282 below must take account of these differences. */
20283 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20284 highest_order_field_bit_offset = bitpos_int;
20286 if (! BYTES_BIG_ENDIAN)
20288 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20289 highest_order_object_bit_offset +=
20290 simple_type_size_in_bits (original_type);
20293 bit_offset
20294 = (! BYTES_BIG_ENDIAN
20295 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20296 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20298 if (bit_offset < 0)
20299 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20300 else
20301 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20304 /* For a FIELD_DECL node which represents a bit field, output an attribute
20305 which specifies the length in bits of the given field. */
20307 static inline void
20308 add_bit_size_attribute (dw_die_ref die, tree decl)
20310 /* Must be a field and a bit field. */
20311 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20312 && DECL_BIT_FIELD_TYPE (decl));
20314 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20315 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20318 /* If the compiled language is ANSI C, then add a 'prototyped'
20319 attribute, if arg types are given for the parameters of a function. */
20321 static inline void
20322 add_prototyped_attribute (dw_die_ref die, tree func_type)
20324 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20326 case DW_LANG_C:
20327 case DW_LANG_C89:
20328 case DW_LANG_C99:
20329 case DW_LANG_C11:
20330 case DW_LANG_ObjC:
20331 if (prototype_p (func_type))
20332 add_AT_flag (die, DW_AT_prototyped, 1);
20333 break;
20334 default:
20335 break;
20339 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20340 by looking in the type declaration, the object declaration equate table or
20341 the block mapping. */
20343 static inline dw_die_ref
20344 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20346 dw_die_ref origin_die = NULL;
20348 if (DECL_P (origin))
20350 dw_die_ref c;
20351 origin_die = lookup_decl_die (origin);
20352 /* "Unwrap" the decls DIE which we put in the imported unit context.
20353 We are looking for the abstract copy here. */
20354 if (in_lto_p
20355 && origin_die
20356 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20357 /* ??? Identify this better. */
20358 && c->with_offset)
20359 origin_die = c;
20361 else if (TYPE_P (origin))
20362 origin_die = lookup_type_die (origin);
20363 else if (TREE_CODE (origin) == BLOCK)
20364 origin_die = BLOCK_DIE (origin);
20366 /* XXX: Functions that are never lowered don't always have correct block
20367 trees (in the case of java, they simply have no block tree, in some other
20368 languages). For these functions, there is nothing we can really do to
20369 output correct debug info for inlined functions in all cases. Rather
20370 than die, we'll just produce deficient debug info now, in that we will
20371 have variables without a proper abstract origin. In the future, when all
20372 functions are lowered, we should re-add a gcc_assert (origin_die)
20373 here. */
20375 if (origin_die)
20376 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20377 return origin_die;
20380 /* We do not currently support the pure_virtual attribute. */
20382 static inline void
20383 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20385 if (DECL_VINDEX (func_decl))
20387 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20389 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20390 add_AT_loc (die, DW_AT_vtable_elem_location,
20391 new_loc_descr (DW_OP_constu,
20392 tree_to_shwi (DECL_VINDEX (func_decl)),
20393 0));
20395 /* GNU extension: Record what type this method came from originally. */
20396 if (debug_info_level > DINFO_LEVEL_TERSE
20397 && DECL_CONTEXT (func_decl))
20398 add_AT_die_ref (die, DW_AT_containing_type,
20399 lookup_type_die (DECL_CONTEXT (func_decl)));
20403 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20404 given decl. This used to be a vendor extension until after DWARF 4
20405 standardized it. */
20407 static void
20408 add_linkage_attr (dw_die_ref die, tree decl)
20410 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20412 /* Mimic what assemble_name_raw does with a leading '*'. */
20413 if (name[0] == '*')
20414 name = &name[1];
20416 if (dwarf_version >= 4)
20417 add_AT_string (die, DW_AT_linkage_name, name);
20418 else
20419 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20422 /* Add source coordinate attributes for the given decl. */
20424 static void
20425 add_src_coords_attributes (dw_die_ref die, tree decl)
20427 expanded_location s;
20429 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20430 return;
20431 s = expand_location (DECL_SOURCE_LOCATION (decl));
20432 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20433 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20434 if (debug_column_info && s.column)
20435 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20438 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20440 static void
20441 add_linkage_name_raw (dw_die_ref die, tree decl)
20443 /* Defer until we have an assembler name set. */
20444 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20446 limbo_die_node *asm_name;
20448 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20449 asm_name->die = die;
20450 asm_name->created_for = decl;
20451 asm_name->next = deferred_asm_name;
20452 deferred_asm_name = asm_name;
20454 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20455 add_linkage_attr (die, decl);
20458 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20460 static void
20461 add_linkage_name (dw_die_ref die, tree decl)
20463 if (debug_info_level > DINFO_LEVEL_NONE
20464 && VAR_OR_FUNCTION_DECL_P (decl)
20465 && TREE_PUBLIC (decl)
20466 && !(VAR_P (decl) && DECL_REGISTER (decl))
20467 && die->die_tag != DW_TAG_member)
20468 add_linkage_name_raw (die, decl);
20471 /* Add a DW_AT_name attribute and source coordinate attribute for the
20472 given decl, but only if it actually has a name. */
20474 static void
20475 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20476 bool no_linkage_name)
20478 tree decl_name;
20480 decl_name = DECL_NAME (decl);
20481 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20483 const char *name = dwarf2_name (decl, 0);
20484 if (name)
20485 add_name_attribute (die, name);
20486 if (! DECL_ARTIFICIAL (decl))
20487 add_src_coords_attributes (die, decl);
20489 if (!no_linkage_name)
20490 add_linkage_name (die, decl);
20493 #ifdef VMS_DEBUGGING_INFO
20494 /* Get the function's name, as described by its RTL. This may be different
20495 from the DECL_NAME name used in the source file. */
20496 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20498 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20499 XEXP (DECL_RTL (decl), 0), false);
20500 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20502 #endif /* VMS_DEBUGGING_INFO */
20505 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20507 static void
20508 add_discr_value (dw_die_ref die, dw_discr_value *value)
20510 dw_attr_node attr;
20512 attr.dw_attr = DW_AT_discr_value;
20513 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20514 attr.dw_attr_val.val_entry = NULL;
20515 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20516 if (value->pos)
20517 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20518 else
20519 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20520 add_dwarf_attr (die, &attr);
20523 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20525 static void
20526 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20528 dw_attr_node attr;
20530 attr.dw_attr = DW_AT_discr_list;
20531 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20532 attr.dw_attr_val.val_entry = NULL;
20533 attr.dw_attr_val.v.val_discr_list = discr_list;
20534 add_dwarf_attr (die, &attr);
20537 static inline dw_discr_list_ref
20538 AT_discr_list (dw_attr_node *attr)
20540 return attr->dw_attr_val.v.val_discr_list;
20543 #ifdef VMS_DEBUGGING_INFO
20544 /* Output the debug main pointer die for VMS */
20546 void
20547 dwarf2out_vms_debug_main_pointer (void)
20549 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20550 dw_die_ref die;
20552 /* Allocate the VMS debug main subprogram die. */
20553 die = new_die_raw (DW_TAG_subprogram);
20554 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20555 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20556 current_function_funcdef_no);
20557 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20559 /* Make it the first child of comp_unit_die (). */
20560 die->die_parent = comp_unit_die ();
20561 if (comp_unit_die ()->die_child)
20563 die->die_sib = comp_unit_die ()->die_child->die_sib;
20564 comp_unit_die ()->die_child->die_sib = die;
20566 else
20568 die->die_sib = die;
20569 comp_unit_die ()->die_child = die;
20572 #endif /* VMS_DEBUGGING_INFO */
20574 /* Push a new declaration scope. */
20576 static void
20577 push_decl_scope (tree scope)
20579 vec_safe_push (decl_scope_table, scope);
20582 /* Pop a declaration scope. */
20584 static inline void
20585 pop_decl_scope (void)
20587 decl_scope_table->pop ();
20590 /* walk_tree helper function for uses_local_type, below. */
20592 static tree
20593 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20595 if (!TYPE_P (*tp))
20596 *walk_subtrees = 0;
20597 else
20599 tree name = TYPE_NAME (*tp);
20600 if (name && DECL_P (name) && decl_function_context (name))
20601 return *tp;
20603 return NULL_TREE;
20606 /* If TYPE involves a function-local type (including a local typedef to a
20607 non-local type), returns that type; otherwise returns NULL_TREE. */
20609 static tree
20610 uses_local_type (tree type)
20612 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20613 return used;
20616 /* Return the DIE for the scope that immediately contains this type.
20617 Non-named types that do not involve a function-local type get global
20618 scope. Named types nested in namespaces or other types get their
20619 containing scope. All other types (i.e. function-local named types) get
20620 the current active scope. */
20622 static dw_die_ref
20623 scope_die_for (tree t, dw_die_ref context_die)
20625 dw_die_ref scope_die = NULL;
20626 tree containing_scope;
20628 /* Non-types always go in the current scope. */
20629 gcc_assert (TYPE_P (t));
20631 /* Use the scope of the typedef, rather than the scope of the type
20632 it refers to. */
20633 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20634 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20635 else
20636 containing_scope = TYPE_CONTEXT (t);
20638 /* Use the containing namespace if there is one. */
20639 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20641 if (context_die == lookup_decl_die (containing_scope))
20642 /* OK */;
20643 else if (debug_info_level > DINFO_LEVEL_TERSE)
20644 context_die = get_context_die (containing_scope);
20645 else
20646 containing_scope = NULL_TREE;
20649 /* Ignore function type "scopes" from the C frontend. They mean that
20650 a tagged type is local to a parmlist of a function declarator, but
20651 that isn't useful to DWARF. */
20652 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20653 containing_scope = NULL_TREE;
20655 if (SCOPE_FILE_SCOPE_P (containing_scope))
20657 /* If T uses a local type keep it local as well, to avoid references
20658 to function-local DIEs from outside the function. */
20659 if (current_function_decl && uses_local_type (t))
20660 scope_die = context_die;
20661 else
20662 scope_die = comp_unit_die ();
20664 else if (TYPE_P (containing_scope))
20666 /* For types, we can just look up the appropriate DIE. */
20667 if (debug_info_level > DINFO_LEVEL_TERSE)
20668 scope_die = get_context_die (containing_scope);
20669 else
20671 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20672 if (scope_die == NULL)
20673 scope_die = comp_unit_die ();
20676 else
20677 scope_die = context_die;
20679 return scope_die;
20682 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20684 static inline int
20685 local_scope_p (dw_die_ref context_die)
20687 for (; context_die; context_die = context_die->die_parent)
20688 if (context_die->die_tag == DW_TAG_inlined_subroutine
20689 || context_die->die_tag == DW_TAG_subprogram)
20690 return 1;
20692 return 0;
20695 /* Returns nonzero if CONTEXT_DIE is a class. */
20697 static inline int
20698 class_scope_p (dw_die_ref context_die)
20700 return (context_die
20701 && (context_die->die_tag == DW_TAG_structure_type
20702 || context_die->die_tag == DW_TAG_class_type
20703 || context_die->die_tag == DW_TAG_interface_type
20704 || context_die->die_tag == DW_TAG_union_type));
20707 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20708 whether or not to treat a DIE in this context as a declaration. */
20710 static inline int
20711 class_or_namespace_scope_p (dw_die_ref context_die)
20713 return (class_scope_p (context_die)
20714 || (context_die && context_die->die_tag == DW_TAG_namespace));
20717 /* Many forms of DIEs require a "type description" attribute. This
20718 routine locates the proper "type descriptor" die for the type given
20719 by 'type' plus any additional qualifiers given by 'cv_quals', and
20720 adds a DW_AT_type attribute below the given die. */
20722 static void
20723 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20724 bool reverse, dw_die_ref context_die)
20726 enum tree_code code = TREE_CODE (type);
20727 dw_die_ref type_die = NULL;
20729 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20730 or fixed-point type, use the inner type. This is because we have no
20731 support for unnamed types in base_type_die. This can happen if this is
20732 an Ada subrange type. Correct solution is emit a subrange type die. */
20733 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20734 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20735 type = TREE_TYPE (type), code = TREE_CODE (type);
20737 if (code == ERROR_MARK
20738 /* Handle a special case. For functions whose return type is void, we
20739 generate *no* type attribute. (Note that no object may have type
20740 `void', so this only applies to function return types). */
20741 || code == VOID_TYPE)
20742 return;
20744 type_die = modified_type_die (type,
20745 cv_quals | TYPE_QUALS (type),
20746 reverse,
20747 context_die);
20749 if (type_die != NULL)
20750 add_AT_die_ref (object_die, DW_AT_type, type_die);
20753 /* Given an object die, add the calling convention attribute for the
20754 function call type. */
20755 static void
20756 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20758 enum dwarf_calling_convention value = DW_CC_normal;
20760 value = ((enum dwarf_calling_convention)
20761 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20763 if (is_fortran ()
20764 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20766 /* DWARF 2 doesn't provide a way to identify a program's source-level
20767 entry point. DW_AT_calling_convention attributes are only meant
20768 to describe functions' calling conventions. However, lacking a
20769 better way to signal the Fortran main program, we used this for
20770 a long time, following existing custom. Now, DWARF 4 has
20771 DW_AT_main_subprogram, which we add below, but some tools still
20772 rely on the old way, which we thus keep. */
20773 value = DW_CC_program;
20775 if (dwarf_version >= 4 || !dwarf_strict)
20776 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20779 /* Only add the attribute if the backend requests it, and
20780 is not DW_CC_normal. */
20781 if (value && (value != DW_CC_normal))
20782 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20785 /* Given a tree pointer to a struct, class, union, or enum type node, return
20786 a pointer to the (string) tag name for the given type, or zero if the type
20787 was declared without a tag. */
20789 static const char *
20790 type_tag (const_tree type)
20792 const char *name = 0;
20794 if (TYPE_NAME (type) != 0)
20796 tree t = 0;
20798 /* Find the IDENTIFIER_NODE for the type name. */
20799 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20800 && !TYPE_NAMELESS (type))
20801 t = TYPE_NAME (type);
20803 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20804 a TYPE_DECL node, regardless of whether or not a `typedef' was
20805 involved. */
20806 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20807 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20809 /* We want to be extra verbose. Don't call dwarf_name if
20810 DECL_NAME isn't set. The default hook for decl_printable_name
20811 doesn't like that, and in this context it's correct to return
20812 0, instead of "<anonymous>" or the like. */
20813 if (DECL_NAME (TYPE_NAME (type))
20814 && !DECL_NAMELESS (TYPE_NAME (type)))
20815 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20818 /* Now get the name as a string, or invent one. */
20819 if (!name && t != 0)
20820 name = IDENTIFIER_POINTER (t);
20823 return (name == 0 || *name == '\0') ? 0 : name;
20826 /* Return the type associated with a data member, make a special check
20827 for bit field types. */
20829 static inline tree
20830 member_declared_type (const_tree member)
20832 return (DECL_BIT_FIELD_TYPE (member)
20833 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20836 /* Get the decl's label, as described by its RTL. This may be different
20837 from the DECL_NAME name used in the source file. */
20839 #if 0
20840 static const char *
20841 decl_start_label (tree decl)
20843 rtx x;
20844 const char *fnname;
20846 x = DECL_RTL (decl);
20847 gcc_assert (MEM_P (x));
20849 x = XEXP (x, 0);
20850 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20852 fnname = XSTR (x, 0);
20853 return fnname;
20855 #endif
20857 /* For variable-length arrays that have been previously generated, but
20858 may be incomplete due to missing subscript info, fill the subscript
20859 info. Return TRUE if this is one of those cases. */
20860 static bool
20861 fill_variable_array_bounds (tree type)
20863 if (TREE_ASM_WRITTEN (type)
20864 && TREE_CODE (type) == ARRAY_TYPE
20865 && variably_modified_type_p (type, NULL))
20867 dw_die_ref array_die = lookup_type_die (type);
20868 if (!array_die)
20869 return false;
20870 add_subscript_info (array_die, type, !is_ada ());
20871 return true;
20873 return false;
20876 /* These routines generate the internal representation of the DIE's for
20877 the compilation unit. Debugging information is collected by walking
20878 the declaration trees passed in from dwarf2out_decl(). */
20880 static void
20881 gen_array_type_die (tree type, dw_die_ref context_die)
20883 dw_die_ref array_die;
20885 /* GNU compilers represent multidimensional array types as sequences of one
20886 dimensional array types whose element types are themselves array types.
20887 We sometimes squish that down to a single array_type DIE with multiple
20888 subscripts in the Dwarf debugging info. The draft Dwarf specification
20889 say that we are allowed to do this kind of compression in C, because
20890 there is no difference between an array of arrays and a multidimensional
20891 array. We don't do this for Ada to remain as close as possible to the
20892 actual representation, which is especially important against the language
20893 flexibilty wrt arrays of variable size. */
20895 bool collapse_nested_arrays = !is_ada ();
20897 if (fill_variable_array_bounds (type))
20898 return;
20900 dw_die_ref scope_die = scope_die_for (type, context_die);
20901 tree element_type;
20903 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20904 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20905 if (TYPE_STRING_FLAG (type)
20906 && TREE_CODE (type) == ARRAY_TYPE
20907 && is_fortran ()
20908 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20910 HOST_WIDE_INT size;
20912 array_die = new_die (DW_TAG_string_type, scope_die, type);
20913 add_name_attribute (array_die, type_tag (type));
20914 equate_type_number_to_die (type, array_die);
20915 size = int_size_in_bytes (type);
20916 if (size >= 0)
20917 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20918 /* ??? We can't annotate types late, but for LTO we may not
20919 generate a location early either (gfortran.dg/save_6.f90). */
20920 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20921 && TYPE_DOMAIN (type) != NULL_TREE
20922 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20924 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20925 tree rszdecl = szdecl;
20927 size = int_size_in_bytes (TREE_TYPE (szdecl));
20928 if (!DECL_P (szdecl))
20930 if (TREE_CODE (szdecl) == INDIRECT_REF
20931 && DECL_P (TREE_OPERAND (szdecl, 0)))
20933 rszdecl = TREE_OPERAND (szdecl, 0);
20934 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20935 != DWARF2_ADDR_SIZE)
20936 size = 0;
20938 else
20939 size = 0;
20941 if (size > 0)
20943 dw_loc_list_ref loc
20944 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20945 NULL);
20946 if (loc)
20948 add_AT_location_description (array_die, DW_AT_string_length,
20949 loc);
20950 if (size != DWARF2_ADDR_SIZE)
20951 add_AT_unsigned (array_die, dwarf_version >= 5
20952 ? DW_AT_string_length_byte_size
20953 : DW_AT_byte_size, size);
20957 return;
20960 array_die = new_die (DW_TAG_array_type, scope_die, type);
20961 add_name_attribute (array_die, type_tag (type));
20962 equate_type_number_to_die (type, array_die);
20964 if (TREE_CODE (type) == VECTOR_TYPE)
20965 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20967 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20968 if (is_fortran ()
20969 && TREE_CODE (type) == ARRAY_TYPE
20970 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20971 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20972 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20974 #if 0
20975 /* We default the array ordering. Debuggers will probably do the right
20976 things even if DW_AT_ordering is not present. It's not even an issue
20977 until we start to get into multidimensional arrays anyway. If a debugger
20978 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20979 then we'll have to put the DW_AT_ordering attribute back in. (But if
20980 and when we find out that we need to put these in, we will only do so
20981 for multidimensional arrays. */
20982 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20983 #endif
20985 if (TREE_CODE (type) == VECTOR_TYPE)
20987 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20988 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20989 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20990 add_bound_info (subrange_die, DW_AT_upper_bound,
20991 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20993 else
20994 add_subscript_info (array_die, type, collapse_nested_arrays);
20996 /* Add representation of the type of the elements of this array type and
20997 emit the corresponding DIE if we haven't done it already. */
20998 element_type = TREE_TYPE (type);
20999 if (collapse_nested_arrays)
21000 while (TREE_CODE (element_type) == ARRAY_TYPE)
21002 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21003 break;
21004 element_type = TREE_TYPE (element_type);
21007 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21008 TREE_CODE (type) == ARRAY_TYPE
21009 && TYPE_REVERSE_STORAGE_ORDER (type),
21010 context_die);
21012 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21013 if (TYPE_ARTIFICIAL (type))
21014 add_AT_flag (array_die, DW_AT_artificial, 1);
21016 if (get_AT (array_die, DW_AT_name))
21017 add_pubtype (type, array_die);
21019 add_alignment_attribute (array_die, type);
21022 /* This routine generates DIE for array with hidden descriptor, details
21023 are filled into *info by a langhook. */
21025 static void
21026 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21027 dw_die_ref context_die)
21029 const dw_die_ref scope_die = scope_die_for (type, context_die);
21030 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21031 struct loc_descr_context context = { type, info->base_decl, NULL,
21032 false, false };
21033 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21034 int dim;
21036 add_name_attribute (array_die, type_tag (type));
21037 equate_type_number_to_die (type, array_die);
21039 if (info->ndimensions > 1)
21040 switch (info->ordering)
21042 case array_descr_ordering_row_major:
21043 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21044 break;
21045 case array_descr_ordering_column_major:
21046 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21047 break;
21048 default:
21049 break;
21052 if (dwarf_version >= 3 || !dwarf_strict)
21054 if (info->data_location)
21055 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21056 dw_scalar_form_exprloc, &context);
21057 if (info->associated)
21058 add_scalar_info (array_die, DW_AT_associated, info->associated,
21059 dw_scalar_form_constant
21060 | dw_scalar_form_exprloc
21061 | dw_scalar_form_reference, &context);
21062 if (info->allocated)
21063 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21064 dw_scalar_form_constant
21065 | dw_scalar_form_exprloc
21066 | dw_scalar_form_reference, &context);
21067 if (info->stride)
21069 const enum dwarf_attribute attr
21070 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21071 const int forms
21072 = (info->stride_in_bits)
21073 ? dw_scalar_form_constant
21074 : (dw_scalar_form_constant
21075 | dw_scalar_form_exprloc
21076 | dw_scalar_form_reference);
21078 add_scalar_info (array_die, attr, info->stride, forms, &context);
21081 if (dwarf_version >= 5)
21083 if (info->rank)
21085 add_scalar_info (array_die, DW_AT_rank, info->rank,
21086 dw_scalar_form_constant
21087 | dw_scalar_form_exprloc, &context);
21088 subrange_tag = DW_TAG_generic_subrange;
21089 context.placeholder_arg = true;
21093 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21095 for (dim = 0; dim < info->ndimensions; dim++)
21097 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21099 if (info->dimen[dim].bounds_type)
21100 add_type_attribute (subrange_die,
21101 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21102 false, context_die);
21103 if (info->dimen[dim].lower_bound)
21104 add_bound_info (subrange_die, DW_AT_lower_bound,
21105 info->dimen[dim].lower_bound, &context);
21106 if (info->dimen[dim].upper_bound)
21107 add_bound_info (subrange_die, DW_AT_upper_bound,
21108 info->dimen[dim].upper_bound, &context);
21109 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21110 add_scalar_info (subrange_die, DW_AT_byte_stride,
21111 info->dimen[dim].stride,
21112 dw_scalar_form_constant
21113 | dw_scalar_form_exprloc
21114 | dw_scalar_form_reference,
21115 &context);
21118 gen_type_die (info->element_type, context_die);
21119 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21120 TREE_CODE (type) == ARRAY_TYPE
21121 && TYPE_REVERSE_STORAGE_ORDER (type),
21122 context_die);
21124 if (get_AT (array_die, DW_AT_name))
21125 add_pubtype (type, array_die);
21127 add_alignment_attribute (array_die, type);
21130 #if 0
21131 static void
21132 gen_entry_point_die (tree decl, dw_die_ref context_die)
21134 tree origin = decl_ultimate_origin (decl);
21135 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21137 if (origin != NULL)
21138 add_abstract_origin_attribute (decl_die, origin);
21139 else
21141 add_name_and_src_coords_attributes (decl_die, decl);
21142 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21143 TYPE_UNQUALIFIED, false, context_die);
21146 if (DECL_ABSTRACT_P (decl))
21147 equate_decl_number_to_die (decl, decl_die);
21148 else
21149 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21151 #endif
21153 /* Walk through the list of incomplete types again, trying once more to
21154 emit full debugging info for them. */
21156 static void
21157 retry_incomplete_types (void)
21159 set_early_dwarf s;
21160 int i;
21162 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21163 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21164 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21165 vec_safe_truncate (incomplete_types, 0);
21168 /* Determine what tag to use for a record type. */
21170 static enum dwarf_tag
21171 record_type_tag (tree type)
21173 if (! lang_hooks.types.classify_record)
21174 return DW_TAG_structure_type;
21176 switch (lang_hooks.types.classify_record (type))
21178 case RECORD_IS_STRUCT:
21179 return DW_TAG_structure_type;
21181 case RECORD_IS_CLASS:
21182 return DW_TAG_class_type;
21184 case RECORD_IS_INTERFACE:
21185 if (dwarf_version >= 3 || !dwarf_strict)
21186 return DW_TAG_interface_type;
21187 return DW_TAG_structure_type;
21189 default:
21190 gcc_unreachable ();
21194 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21195 include all of the information about the enumeration values also. Each
21196 enumerated type name/value is listed as a child of the enumerated type
21197 DIE. */
21199 static dw_die_ref
21200 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21202 dw_die_ref type_die = lookup_type_die (type);
21204 if (type_die == NULL)
21206 type_die = new_die (DW_TAG_enumeration_type,
21207 scope_die_for (type, context_die), type);
21208 equate_type_number_to_die (type, type_die);
21209 add_name_attribute (type_die, type_tag (type));
21210 if (dwarf_version >= 4 || !dwarf_strict)
21212 if (ENUM_IS_SCOPED (type))
21213 add_AT_flag (type_die, DW_AT_enum_class, 1);
21214 if (ENUM_IS_OPAQUE (type))
21215 add_AT_flag (type_die, DW_AT_declaration, 1);
21217 if (!dwarf_strict)
21218 add_AT_unsigned (type_die, DW_AT_encoding,
21219 TYPE_UNSIGNED (type)
21220 ? DW_ATE_unsigned
21221 : DW_ATE_signed);
21223 else if (! TYPE_SIZE (type))
21224 return type_die;
21225 else
21226 remove_AT (type_die, DW_AT_declaration);
21228 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21229 given enum type is incomplete, do not generate the DW_AT_byte_size
21230 attribute or the DW_AT_element_list attribute. */
21231 if (TYPE_SIZE (type))
21233 tree link;
21235 TREE_ASM_WRITTEN (type) = 1;
21236 add_byte_size_attribute (type_die, type);
21237 add_alignment_attribute (type_die, type);
21238 if (dwarf_version >= 3 || !dwarf_strict)
21240 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21241 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21242 context_die);
21244 if (TYPE_STUB_DECL (type) != NULL_TREE)
21246 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21247 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21250 /* If the first reference to this type was as the return type of an
21251 inline function, then it may not have a parent. Fix this now. */
21252 if (type_die->die_parent == NULL)
21253 add_child_die (scope_die_for (type, context_die), type_die);
21255 for (link = TYPE_VALUES (type);
21256 link != NULL; link = TREE_CHAIN (link))
21258 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21259 tree value = TREE_VALUE (link);
21261 add_name_attribute (enum_die,
21262 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21264 if (TREE_CODE (value) == CONST_DECL)
21265 value = DECL_INITIAL (value);
21267 if (simple_type_size_in_bits (TREE_TYPE (value))
21268 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21270 /* For constant forms created by add_AT_unsigned DWARF
21271 consumers (GDB, elfutils, etc.) always zero extend
21272 the value. Only when the actual value is negative
21273 do we need to use add_AT_int to generate a constant
21274 form that can represent negative values. */
21275 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21276 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21277 add_AT_unsigned (enum_die, DW_AT_const_value,
21278 (unsigned HOST_WIDE_INT) val);
21279 else
21280 add_AT_int (enum_die, DW_AT_const_value, val);
21282 else
21283 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21284 that here. TODO: This should be re-worked to use correct
21285 signed/unsigned double tags for all cases. */
21286 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21289 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21290 if (TYPE_ARTIFICIAL (type))
21291 add_AT_flag (type_die, DW_AT_artificial, 1);
21293 else
21294 add_AT_flag (type_die, DW_AT_declaration, 1);
21296 add_pubtype (type, type_die);
21298 return type_die;
21301 /* Generate a DIE to represent either a real live formal parameter decl or to
21302 represent just the type of some formal parameter position in some function
21303 type.
21305 Note that this routine is a bit unusual because its argument may be a
21306 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21307 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21308 node. If it's the former then this function is being called to output a
21309 DIE to represent a formal parameter object (or some inlining thereof). If
21310 it's the latter, then this function is only being called to output a
21311 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21312 argument type of some subprogram type.
21313 If EMIT_NAME_P is true, name and source coordinate attributes
21314 are emitted. */
21316 static dw_die_ref
21317 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21318 dw_die_ref context_die)
21320 tree node_or_origin = node ? node : origin;
21321 tree ultimate_origin;
21322 dw_die_ref parm_die = NULL;
21324 if (DECL_P (node_or_origin))
21326 parm_die = lookup_decl_die (node);
21328 /* If the contexts differ, we may not be talking about the same
21329 thing.
21330 ??? When in LTO the DIE parent is the "abstract" copy and the
21331 context_die is the specification "copy". But this whole block
21332 should eventually be no longer needed. */
21333 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21335 if (!DECL_ABSTRACT_P (node))
21337 /* This can happen when creating an inlined instance, in
21338 which case we need to create a new DIE that will get
21339 annotated with DW_AT_abstract_origin. */
21340 parm_die = NULL;
21342 else
21343 gcc_unreachable ();
21346 if (parm_die && parm_die->die_parent == NULL)
21348 /* Check that parm_die already has the right attributes that
21349 we would have added below. If any attributes are
21350 missing, fall through to add them. */
21351 if (! DECL_ABSTRACT_P (node_or_origin)
21352 && !get_AT (parm_die, DW_AT_location)
21353 && !get_AT (parm_die, DW_AT_const_value))
21354 /* We are missing location info, and are about to add it. */
21356 else
21358 add_child_die (context_die, parm_die);
21359 return parm_die;
21364 /* If we have a previously generated DIE, use it, unless this is an
21365 concrete instance (origin != NULL), in which case we need a new
21366 DIE with a corresponding DW_AT_abstract_origin. */
21367 bool reusing_die;
21368 if (parm_die && origin == NULL)
21369 reusing_die = true;
21370 else
21372 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21373 reusing_die = false;
21376 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21378 case tcc_declaration:
21379 ultimate_origin = decl_ultimate_origin (node_or_origin);
21380 if (node || ultimate_origin)
21381 origin = ultimate_origin;
21383 if (reusing_die)
21384 goto add_location;
21386 if (origin != NULL)
21387 add_abstract_origin_attribute (parm_die, origin);
21388 else if (emit_name_p)
21389 add_name_and_src_coords_attributes (parm_die, node);
21390 if (origin == NULL
21391 || (! DECL_ABSTRACT_P (node_or_origin)
21392 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21393 decl_function_context
21394 (node_or_origin))))
21396 tree type = TREE_TYPE (node_or_origin);
21397 if (decl_by_reference_p (node_or_origin))
21398 add_type_attribute (parm_die, TREE_TYPE (type),
21399 TYPE_UNQUALIFIED,
21400 false, context_die);
21401 else
21402 add_type_attribute (parm_die, type,
21403 decl_quals (node_or_origin),
21404 false, context_die);
21406 if (origin == NULL && DECL_ARTIFICIAL (node))
21407 add_AT_flag (parm_die, DW_AT_artificial, 1);
21408 add_location:
21409 if (node && node != origin)
21410 equate_decl_number_to_die (node, parm_die);
21411 if (! DECL_ABSTRACT_P (node_or_origin))
21412 add_location_or_const_value_attribute (parm_die, node_or_origin,
21413 node == NULL);
21415 break;
21417 case tcc_type:
21418 /* We were called with some kind of a ..._TYPE node. */
21419 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21420 context_die);
21421 break;
21423 default:
21424 gcc_unreachable ();
21427 return parm_die;
21430 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21431 children DW_TAG_formal_parameter DIEs representing the arguments of the
21432 parameter pack.
21434 PARM_PACK must be a function parameter pack.
21435 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21436 must point to the subsequent arguments of the function PACK_ARG belongs to.
21437 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21438 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21439 following the last one for which a DIE was generated. */
21441 static dw_die_ref
21442 gen_formal_parameter_pack_die (tree parm_pack,
21443 tree pack_arg,
21444 dw_die_ref subr_die,
21445 tree *next_arg)
21447 tree arg;
21448 dw_die_ref parm_pack_die;
21450 gcc_assert (parm_pack
21451 && lang_hooks.function_parameter_pack_p (parm_pack)
21452 && subr_die);
21454 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21455 add_src_coords_attributes (parm_pack_die, parm_pack);
21457 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21459 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21460 parm_pack))
21461 break;
21462 gen_formal_parameter_die (arg, NULL,
21463 false /* Don't emit name attribute. */,
21464 parm_pack_die);
21466 if (next_arg)
21467 *next_arg = arg;
21468 return parm_pack_die;
21471 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21472 at the end of an (ANSI prototyped) formal parameters list. */
21474 static void
21475 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21477 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21480 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21481 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21482 parameters as specified in some function type specification (except for
21483 those which appear as part of a function *definition*). */
21485 static void
21486 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21488 tree link;
21489 tree formal_type = NULL;
21490 tree first_parm_type;
21491 tree arg;
21493 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21495 arg = DECL_ARGUMENTS (function_or_method_type);
21496 function_or_method_type = TREE_TYPE (function_or_method_type);
21498 else
21499 arg = NULL_TREE;
21501 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21503 /* Make our first pass over the list of formal parameter types and output a
21504 DW_TAG_formal_parameter DIE for each one. */
21505 for (link = first_parm_type; link; )
21507 dw_die_ref parm_die;
21509 formal_type = TREE_VALUE (link);
21510 if (formal_type == void_type_node)
21511 break;
21513 /* Output a (nameless) DIE to represent the formal parameter itself. */
21514 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21516 parm_die = gen_formal_parameter_die (formal_type, NULL,
21517 true /* Emit name attribute. */,
21518 context_die);
21519 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21520 && link == first_parm_type)
21522 add_AT_flag (parm_die, DW_AT_artificial, 1);
21523 if (dwarf_version >= 3 || !dwarf_strict)
21524 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21526 else if (arg && DECL_ARTIFICIAL (arg))
21527 add_AT_flag (parm_die, DW_AT_artificial, 1);
21530 link = TREE_CHAIN (link);
21531 if (arg)
21532 arg = DECL_CHAIN (arg);
21535 /* If this function type has an ellipsis, add a
21536 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21537 if (formal_type != void_type_node)
21538 gen_unspecified_parameters_die (function_or_method_type, context_die);
21540 /* Make our second (and final) pass over the list of formal parameter types
21541 and output DIEs to represent those types (as necessary). */
21542 for (link = TYPE_ARG_TYPES (function_or_method_type);
21543 link && TREE_VALUE (link);
21544 link = TREE_CHAIN (link))
21545 gen_type_die (TREE_VALUE (link), context_die);
21548 /* We want to generate the DIE for TYPE so that we can generate the
21549 die for MEMBER, which has been defined; we will need to refer back
21550 to the member declaration nested within TYPE. If we're trying to
21551 generate minimal debug info for TYPE, processing TYPE won't do the
21552 trick; we need to attach the member declaration by hand. */
21554 static void
21555 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21557 gen_type_die (type, context_die);
21559 /* If we're trying to avoid duplicate debug info, we may not have
21560 emitted the member decl for this function. Emit it now. */
21561 if (TYPE_STUB_DECL (type)
21562 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21563 && ! lookup_decl_die (member))
21565 dw_die_ref type_die;
21566 gcc_assert (!decl_ultimate_origin (member));
21568 push_decl_scope (type);
21569 type_die = lookup_type_die_strip_naming_typedef (type);
21570 if (TREE_CODE (member) == FUNCTION_DECL)
21571 gen_subprogram_die (member, type_die);
21572 else if (TREE_CODE (member) == FIELD_DECL)
21574 /* Ignore the nameless fields that are used to skip bits but handle
21575 C++ anonymous unions and structs. */
21576 if (DECL_NAME (member) != NULL_TREE
21577 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21578 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21580 struct vlr_context vlr_ctx = {
21581 DECL_CONTEXT (member), /* struct_type */
21582 NULL_TREE /* variant_part_offset */
21584 gen_type_die (member_declared_type (member), type_die);
21585 gen_field_die (member, &vlr_ctx, type_die);
21588 else
21589 gen_variable_die (member, NULL_TREE, type_die);
21591 pop_decl_scope ();
21595 /* Forward declare these functions, because they are mutually recursive
21596 with their set_block_* pairing functions. */
21597 static void set_decl_origin_self (tree);
21599 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21600 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21601 that it points to the node itself, thus indicating that the node is its
21602 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21603 the given node is NULL, recursively descend the decl/block tree which
21604 it is the root of, and for each other ..._DECL or BLOCK node contained
21605 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21606 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21607 values to point to themselves. */
21609 static void
21610 set_block_origin_self (tree stmt)
21612 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21614 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21617 tree local_decl;
21619 for (local_decl = BLOCK_VARS (stmt);
21620 local_decl != NULL_TREE;
21621 local_decl = DECL_CHAIN (local_decl))
21622 /* Do not recurse on nested functions since the inlining status
21623 of parent and child can be different as per the DWARF spec. */
21624 if (TREE_CODE (local_decl) != FUNCTION_DECL
21625 && !DECL_EXTERNAL (local_decl))
21626 set_decl_origin_self (local_decl);
21630 tree subblock;
21632 for (subblock = BLOCK_SUBBLOCKS (stmt);
21633 subblock != NULL_TREE;
21634 subblock = BLOCK_CHAIN (subblock))
21635 set_block_origin_self (subblock); /* Recurse. */
21640 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21641 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21642 node to so that it points to the node itself, thus indicating that the
21643 node represents its own (abstract) origin. Additionally, if the
21644 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21645 the decl/block tree of which the given node is the root of, and for
21646 each other ..._DECL or BLOCK node contained therein whose
21647 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21648 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21649 point to themselves. */
21651 static void
21652 set_decl_origin_self (tree decl)
21654 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21656 DECL_ABSTRACT_ORIGIN (decl) = decl;
21657 if (TREE_CODE (decl) == FUNCTION_DECL)
21659 tree arg;
21661 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21662 DECL_ABSTRACT_ORIGIN (arg) = arg;
21663 if (DECL_INITIAL (decl) != NULL_TREE
21664 && DECL_INITIAL (decl) != error_mark_node)
21665 set_block_origin_self (DECL_INITIAL (decl));
21670 /* Mark the early DIE for DECL as the abstract instance. */
21672 static void
21673 dwarf2out_abstract_function (tree decl)
21675 dw_die_ref old_die;
21677 /* Make sure we have the actual abstract inline, not a clone. */
21678 decl = DECL_ORIGIN (decl);
21680 if (DECL_IGNORED_P (decl))
21681 return;
21683 old_die = lookup_decl_die (decl);
21684 /* With early debug we always have an old DIE unless we are in LTO
21685 and the user did not compile but only link with debug. */
21686 if (in_lto_p && ! old_die)
21687 return;
21688 gcc_assert (old_die != NULL);
21689 if (get_AT (old_die, DW_AT_inline)
21690 || get_AT (old_die, DW_AT_abstract_origin))
21691 /* We've already generated the abstract instance. */
21692 return;
21694 /* Go ahead and put DW_AT_inline on the DIE. */
21695 if (DECL_DECLARED_INLINE_P (decl))
21697 if (cgraph_function_possibly_inlined_p (decl))
21698 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21699 else
21700 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21702 else
21704 if (cgraph_function_possibly_inlined_p (decl))
21705 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21706 else
21707 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21710 if (DECL_DECLARED_INLINE_P (decl)
21711 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21712 add_AT_flag (old_die, DW_AT_artificial, 1);
21714 set_decl_origin_self (decl);
21717 /* Helper function of premark_used_types() which gets called through
21718 htab_traverse.
21720 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21721 marked as unused by prune_unused_types. */
21723 bool
21724 premark_used_types_helper (tree const &type, void *)
21726 dw_die_ref die;
21728 die = lookup_type_die (type);
21729 if (die != NULL)
21730 die->die_perennial_p = 1;
21731 return true;
21734 /* Helper function of premark_types_used_by_global_vars which gets called
21735 through htab_traverse.
21737 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21738 marked as unused by prune_unused_types. The DIE of the type is marked
21739 only if the global variable using the type will actually be emitted. */
21742 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21743 void *)
21745 struct types_used_by_vars_entry *entry;
21746 dw_die_ref die;
21748 entry = (struct types_used_by_vars_entry *) *slot;
21749 gcc_assert (entry->type != NULL
21750 && entry->var_decl != NULL);
21751 die = lookup_type_die (entry->type);
21752 if (die)
21754 /* Ask cgraph if the global variable really is to be emitted.
21755 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21756 varpool_node *node = varpool_node::get (entry->var_decl);
21757 if (node && node->definition)
21759 die->die_perennial_p = 1;
21760 /* Keep the parent DIEs as well. */
21761 while ((die = die->die_parent) && die->die_perennial_p == 0)
21762 die->die_perennial_p = 1;
21765 return 1;
21768 /* Mark all members of used_types_hash as perennial. */
21770 static void
21771 premark_used_types (struct function *fun)
21773 if (fun && fun->used_types_hash)
21774 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21777 /* Mark all members of types_used_by_vars_entry as perennial. */
21779 static void
21780 premark_types_used_by_global_vars (void)
21782 if (types_used_by_vars_hash)
21783 types_used_by_vars_hash
21784 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21787 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21788 for CA_LOC call arg loc node. */
21790 static dw_die_ref
21791 gen_call_site_die (tree decl, dw_die_ref subr_die,
21792 struct call_arg_loc_node *ca_loc)
21794 dw_die_ref stmt_die = NULL, die;
21795 tree block = ca_loc->block;
21797 while (block
21798 && block != DECL_INITIAL (decl)
21799 && TREE_CODE (block) == BLOCK)
21801 stmt_die = BLOCK_DIE (block);
21802 if (stmt_die)
21803 break;
21804 block = BLOCK_SUPERCONTEXT (block);
21806 if (stmt_die == NULL)
21807 stmt_die = subr_die;
21808 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21809 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21810 if (ca_loc->tail_call_p)
21811 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21812 if (ca_loc->symbol_ref)
21814 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21815 if (tdie)
21816 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21817 else
21818 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21819 false);
21821 return die;
21824 /* Generate a DIE to represent a declared function (either file-scope or
21825 block-local). */
21827 static void
21828 gen_subprogram_die (tree decl, dw_die_ref context_die)
21830 tree origin = decl_ultimate_origin (decl);
21831 dw_die_ref subr_die;
21832 dw_die_ref old_die = lookup_decl_die (decl);
21834 /* This function gets called multiple times for different stages of
21835 the debug process. For example, for func() in this code:
21837 namespace S
21839 void func() { ... }
21842 ...we get called 4 times. Twice in early debug and twice in
21843 late debug:
21845 Early debug
21846 -----------
21848 1. Once while generating func() within the namespace. This is
21849 the declaration. The declaration bit below is set, as the
21850 context is the namespace.
21852 A new DIE will be generated with DW_AT_declaration set.
21854 2. Once for func() itself. This is the specification. The
21855 declaration bit below is clear as the context is the CU.
21857 We will use the cached DIE from (1) to create a new DIE with
21858 DW_AT_specification pointing to the declaration in (1).
21860 Late debug via rest_of_handle_final()
21861 -------------------------------------
21863 3. Once generating func() within the namespace. This is also the
21864 declaration, as in (1), but this time we will early exit below
21865 as we have a cached DIE and a declaration needs no additional
21866 annotations (no locations), as the source declaration line
21867 info is enough.
21869 4. Once for func() itself. As in (2), this is the specification,
21870 but this time we will re-use the cached DIE, and just annotate
21871 it with the location information that should now be available.
21873 For something without namespaces, but with abstract instances, we
21874 are also called a multiple times:
21876 class Base
21878 public:
21879 Base (); // constructor declaration (1)
21882 Base::Base () { } // constructor specification (2)
21884 Early debug
21885 -----------
21887 1. Once for the Base() constructor by virtue of it being a
21888 member of the Base class. This is done via
21889 rest_of_type_compilation.
21891 This is a declaration, so a new DIE will be created with
21892 DW_AT_declaration.
21894 2. Once for the Base() constructor definition, but this time
21895 while generating the abstract instance of the base
21896 constructor (__base_ctor) which is being generated via early
21897 debug of reachable functions.
21899 Even though we have a cached version of the declaration (1),
21900 we will create a DW_AT_specification of the declaration DIE
21901 in (1).
21903 3. Once for the __base_ctor itself, but this time, we generate
21904 an DW_AT_abstract_origin version of the DW_AT_specification in
21905 (2).
21907 Late debug via rest_of_handle_final
21908 -----------------------------------
21910 4. One final time for the __base_ctor (which will have a cached
21911 DIE with DW_AT_abstract_origin created in (3). This time,
21912 we will just annotate the location information now
21913 available.
21915 int declaration = (current_function_decl != decl
21916 || class_or_namespace_scope_p (context_die));
21918 /* Now that the C++ front end lazily declares artificial member fns, we
21919 might need to retrofit the declaration into its class. */
21920 if (!declaration && !origin && !old_die
21921 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21922 && !class_or_namespace_scope_p (context_die)
21923 && debug_info_level > DINFO_LEVEL_TERSE)
21924 old_die = force_decl_die (decl);
21926 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21927 if (origin != NULL)
21929 gcc_assert (!declaration || local_scope_p (context_die));
21931 /* Fixup die_parent for the abstract instance of a nested
21932 inline function. */
21933 if (old_die && old_die->die_parent == NULL)
21934 add_child_die (context_die, old_die);
21936 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21938 /* If we have a DW_AT_abstract_origin we have a working
21939 cached version. */
21940 subr_die = old_die;
21942 else
21944 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21945 add_abstract_origin_attribute (subr_die, origin);
21946 /* This is where the actual code for a cloned function is.
21947 Let's emit linkage name attribute for it. This helps
21948 debuggers to e.g, set breakpoints into
21949 constructors/destructors when the user asks "break
21950 K::K". */
21951 add_linkage_name (subr_die, decl);
21954 /* A cached copy, possibly from early dwarf generation. Reuse as
21955 much as possible. */
21956 else if (old_die)
21958 /* A declaration that has been previously dumped needs no
21959 additional information. */
21960 if (declaration)
21961 return;
21963 if (!get_AT_flag (old_die, DW_AT_declaration)
21964 /* We can have a normal definition following an inline one in the
21965 case of redefinition of GNU C extern inlines.
21966 It seems reasonable to use AT_specification in this case. */
21967 && !get_AT (old_die, DW_AT_inline))
21969 /* Detect and ignore this case, where we are trying to output
21970 something we have already output. */
21971 if (get_AT (old_die, DW_AT_low_pc)
21972 || get_AT (old_die, DW_AT_ranges))
21973 return;
21975 /* If we have no location information, this must be a
21976 partially generated DIE from early dwarf generation.
21977 Fall through and generate it. */
21980 /* If the definition comes from the same place as the declaration,
21981 maybe use the old DIE. We always want the DIE for this function
21982 that has the *_pc attributes to be under comp_unit_die so the
21983 debugger can find it. We also need to do this for abstract
21984 instances of inlines, since the spec requires the out-of-line copy
21985 to have the same parent. For local class methods, this doesn't
21986 apply; we just use the old DIE. */
21987 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21988 struct dwarf_file_data * file_index = lookup_filename (s.file);
21989 if ((is_cu_die (old_die->die_parent)
21990 /* This condition fixes the inconsistency/ICE with the
21991 following Fortran test (or some derivative thereof) while
21992 building libgfortran:
21994 module some_m
21995 contains
21996 logical function funky (FLAG)
21997 funky = .true.
21998 end function
21999 end module
22001 || (old_die->die_parent
22002 && old_die->die_parent->die_tag == DW_TAG_module)
22003 || context_die == NULL)
22004 && (DECL_ARTIFICIAL (decl)
22005 /* The location attributes may be in the abstract origin
22006 which in the case of LTO might be not available to
22007 look at. */
22008 || get_AT (old_die, DW_AT_abstract_origin)
22009 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22010 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22011 == (unsigned) s.line)
22012 && (!debug_column_info
22013 || s.column == 0
22014 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22015 == (unsigned) s.column)))))
22017 subr_die = old_die;
22019 /* Clear out the declaration attribute, but leave the
22020 parameters so they can be augmented with location
22021 information later. Unless this was a declaration, in
22022 which case, wipe out the nameless parameters and recreate
22023 them further down. */
22024 if (remove_AT (subr_die, DW_AT_declaration))
22027 remove_AT (subr_die, DW_AT_object_pointer);
22028 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22031 /* Make a specification pointing to the previously built
22032 declaration. */
22033 else
22035 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22036 add_AT_specification (subr_die, old_die);
22037 add_pubname (decl, subr_die);
22038 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22039 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22040 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22041 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22042 if (debug_column_info
22043 && s.column
22044 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22045 != (unsigned) s.column))
22046 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22048 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22049 emit the real type on the definition die. */
22050 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22052 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22053 if (die == auto_die || die == decltype_auto_die)
22054 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22055 TYPE_UNQUALIFIED, false, context_die);
22058 /* When we process the method declaration, we haven't seen
22059 the out-of-class defaulted definition yet, so we have to
22060 recheck now. */
22061 if ((dwarf_version >= 5 || ! dwarf_strict)
22062 && !get_AT (subr_die, DW_AT_defaulted))
22064 int defaulted
22065 = lang_hooks.decls.decl_dwarf_attribute (decl,
22066 DW_AT_defaulted);
22067 if (defaulted != -1)
22069 /* Other values must have been handled before. */
22070 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22071 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22076 /* Create a fresh DIE for anything else. */
22077 else
22079 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22081 if (TREE_PUBLIC (decl))
22082 add_AT_flag (subr_die, DW_AT_external, 1);
22084 add_name_and_src_coords_attributes (subr_die, decl);
22085 add_pubname (decl, subr_die);
22086 if (debug_info_level > DINFO_LEVEL_TERSE)
22088 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22089 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22090 TYPE_UNQUALIFIED, false, context_die);
22093 add_pure_or_virtual_attribute (subr_die, decl);
22094 if (DECL_ARTIFICIAL (decl))
22095 add_AT_flag (subr_die, DW_AT_artificial, 1);
22097 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22098 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22100 add_alignment_attribute (subr_die, decl);
22102 add_accessibility_attribute (subr_die, decl);
22105 /* Unless we have an existing non-declaration DIE, equate the new
22106 DIE. */
22107 if (!old_die || is_declaration_die (old_die))
22108 equate_decl_number_to_die (decl, subr_die);
22110 if (declaration)
22112 if (!old_die || !get_AT (old_die, DW_AT_inline))
22114 add_AT_flag (subr_die, DW_AT_declaration, 1);
22116 /* If this is an explicit function declaration then generate
22117 a DW_AT_explicit attribute. */
22118 if ((dwarf_version >= 3 || !dwarf_strict)
22119 && lang_hooks.decls.decl_dwarf_attribute (decl,
22120 DW_AT_explicit) == 1)
22121 add_AT_flag (subr_die, DW_AT_explicit, 1);
22123 /* If this is a C++11 deleted special function member then generate
22124 a DW_AT_deleted attribute. */
22125 if ((dwarf_version >= 5 || !dwarf_strict)
22126 && lang_hooks.decls.decl_dwarf_attribute (decl,
22127 DW_AT_deleted) == 1)
22128 add_AT_flag (subr_die, DW_AT_deleted, 1);
22130 /* If this is a C++11 defaulted special function member then
22131 generate a DW_AT_defaulted attribute. */
22132 if (dwarf_version >= 5 || !dwarf_strict)
22134 int defaulted
22135 = lang_hooks.decls.decl_dwarf_attribute (decl,
22136 DW_AT_defaulted);
22137 if (defaulted != -1)
22138 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22141 /* If this is a C++11 non-static member function with & ref-qualifier
22142 then generate a DW_AT_reference attribute. */
22143 if ((dwarf_version >= 5 || !dwarf_strict)
22144 && lang_hooks.decls.decl_dwarf_attribute (decl,
22145 DW_AT_reference) == 1)
22146 add_AT_flag (subr_die, DW_AT_reference, 1);
22148 /* If this is a C++11 non-static member function with &&
22149 ref-qualifier then generate a DW_AT_reference attribute. */
22150 if ((dwarf_version >= 5 || !dwarf_strict)
22151 && lang_hooks.decls.decl_dwarf_attribute (decl,
22152 DW_AT_rvalue_reference)
22153 == 1)
22154 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22157 /* For non DECL_EXTERNALs, if range information is available, fill
22158 the DIE with it. */
22159 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22161 HOST_WIDE_INT cfa_fb_offset;
22163 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22165 if (!crtl->has_bb_partition)
22167 dw_fde_ref fde = fun->fde;
22168 if (fde->dw_fde_begin)
22170 /* We have already generated the labels. */
22171 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22172 fde->dw_fde_end, false);
22174 else
22176 /* Create start/end labels and add the range. */
22177 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22178 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22179 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22180 current_function_funcdef_no);
22181 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22182 current_function_funcdef_no);
22183 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22184 false);
22187 #if VMS_DEBUGGING_INFO
22188 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22189 Section 2.3 Prologue and Epilogue Attributes:
22190 When a breakpoint is set on entry to a function, it is generally
22191 desirable for execution to be suspended, not on the very first
22192 instruction of the function, but rather at a point after the
22193 function's frame has been set up, after any language defined local
22194 declaration processing has been completed, and before execution of
22195 the first statement of the function begins. Debuggers generally
22196 cannot properly determine where this point is. Similarly for a
22197 breakpoint set on exit from a function. The prologue and epilogue
22198 attributes allow a compiler to communicate the location(s) to use. */
22201 if (fde->dw_fde_vms_end_prologue)
22202 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22203 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22205 if (fde->dw_fde_vms_begin_epilogue)
22206 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22207 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22209 #endif
22212 else
22214 /* Generate pubnames entries for the split function code ranges. */
22215 dw_fde_ref fde = fun->fde;
22217 if (fde->dw_fde_second_begin)
22219 if (dwarf_version >= 3 || !dwarf_strict)
22221 /* We should use ranges for non-contiguous code section
22222 addresses. Use the actual code range for the initial
22223 section, since the HOT/COLD labels might precede an
22224 alignment offset. */
22225 bool range_list_added = false;
22226 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22227 fde->dw_fde_end, &range_list_added,
22228 false);
22229 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22230 fde->dw_fde_second_end,
22231 &range_list_added, false);
22232 if (range_list_added)
22233 add_ranges (NULL);
22235 else
22237 /* There is no real support in DW2 for this .. so we make
22238 a work-around. First, emit the pub name for the segment
22239 containing the function label. Then make and emit a
22240 simplified subprogram DIE for the second segment with the
22241 name pre-fixed by __hot/cold_sect_of_. We use the same
22242 linkage name for the second die so that gdb will find both
22243 sections when given "b foo". */
22244 const char *name = NULL;
22245 tree decl_name = DECL_NAME (decl);
22246 dw_die_ref seg_die;
22248 /* Do the 'primary' section. */
22249 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22250 fde->dw_fde_end, false);
22252 /* Build a minimal DIE for the secondary section. */
22253 seg_die = new_die (DW_TAG_subprogram,
22254 subr_die->die_parent, decl);
22256 if (TREE_PUBLIC (decl))
22257 add_AT_flag (seg_die, DW_AT_external, 1);
22259 if (decl_name != NULL
22260 && IDENTIFIER_POINTER (decl_name) != NULL)
22262 name = dwarf2_name (decl, 1);
22263 if (! DECL_ARTIFICIAL (decl))
22264 add_src_coords_attributes (seg_die, decl);
22266 add_linkage_name (seg_die, decl);
22268 gcc_assert (name != NULL);
22269 add_pure_or_virtual_attribute (seg_die, decl);
22270 if (DECL_ARTIFICIAL (decl))
22271 add_AT_flag (seg_die, DW_AT_artificial, 1);
22273 name = concat ("__second_sect_of_", name, NULL);
22274 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22275 fde->dw_fde_second_end, false);
22276 add_name_attribute (seg_die, name);
22277 if (want_pubnames ())
22278 add_pubname_string (name, seg_die);
22281 else
22282 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22283 false);
22286 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22288 /* We define the "frame base" as the function's CFA. This is more
22289 convenient for several reasons: (1) It's stable across the prologue
22290 and epilogue, which makes it better than just a frame pointer,
22291 (2) With dwarf3, there exists a one-byte encoding that allows us
22292 to reference the .debug_frame data by proxy, but failing that,
22293 (3) We can at least reuse the code inspection and interpretation
22294 code that determines the CFA position at various points in the
22295 function. */
22296 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22298 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22299 add_AT_loc (subr_die, DW_AT_frame_base, op);
22301 else
22303 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22304 if (list->dw_loc_next)
22305 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22306 else
22307 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22310 /* Compute a displacement from the "steady-state frame pointer" to
22311 the CFA. The former is what all stack slots and argument slots
22312 will reference in the rtl; the latter is what we've told the
22313 debugger about. We'll need to adjust all frame_base references
22314 by this displacement. */
22315 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22317 if (fun->static_chain_decl)
22319 /* DWARF requires here a location expression that computes the
22320 address of the enclosing subprogram's frame base. The machinery
22321 in tree-nested.c is supposed to store this specific address in the
22322 last field of the FRAME record. */
22323 const tree frame_type
22324 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22325 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22327 tree fb_expr
22328 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22329 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22330 fb_expr, fb_decl, NULL_TREE);
22332 add_AT_location_description (subr_die, DW_AT_static_link,
22333 loc_list_from_tree (fb_expr, 0, NULL));
22336 resolve_variable_values ();
22339 /* Generate child dies for template paramaters. */
22340 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22341 gen_generic_params_dies (decl);
22343 /* Now output descriptions of the arguments for this function. This gets
22344 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22345 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22346 `...' at the end of the formal parameter list. In order to find out if
22347 there was a trailing ellipsis or not, we must instead look at the type
22348 associated with the FUNCTION_DECL. This will be a node of type
22349 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22350 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22351 an ellipsis at the end. */
22353 /* In the case where we are describing a mere function declaration, all we
22354 need to do here (and all we *can* do here) is to describe the *types* of
22355 its formal parameters. */
22356 if (debug_info_level <= DINFO_LEVEL_TERSE)
22358 else if (declaration)
22359 gen_formal_types_die (decl, subr_die);
22360 else
22362 /* Generate DIEs to represent all known formal parameters. */
22363 tree parm = DECL_ARGUMENTS (decl);
22364 tree generic_decl = early_dwarf
22365 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22366 tree generic_decl_parm = generic_decl
22367 ? DECL_ARGUMENTS (generic_decl)
22368 : NULL;
22370 /* Now we want to walk the list of parameters of the function and
22371 emit their relevant DIEs.
22373 We consider the case of DECL being an instance of a generic function
22374 as well as it being a normal function.
22376 If DECL is an instance of a generic function we walk the
22377 parameters of the generic function declaration _and_ the parameters of
22378 DECL itself. This is useful because we want to emit specific DIEs for
22379 function parameter packs and those are declared as part of the
22380 generic function declaration. In that particular case,
22381 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22382 That DIE has children DIEs representing the set of arguments
22383 of the pack. Note that the set of pack arguments can be empty.
22384 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22385 children DIE.
22387 Otherwise, we just consider the parameters of DECL. */
22388 while (generic_decl_parm || parm)
22390 if (generic_decl_parm
22391 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22392 gen_formal_parameter_pack_die (generic_decl_parm,
22393 parm, subr_die,
22394 &parm);
22395 else if (parm && !POINTER_BOUNDS_P (parm))
22397 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22399 if (early_dwarf
22400 && parm == DECL_ARGUMENTS (decl)
22401 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22402 && parm_die
22403 && (dwarf_version >= 3 || !dwarf_strict))
22404 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22406 parm = DECL_CHAIN (parm);
22408 else if (parm)
22409 parm = DECL_CHAIN (parm);
22411 if (generic_decl_parm)
22412 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22415 /* Decide whether we need an unspecified_parameters DIE at the end.
22416 There are 2 more cases to do this for: 1) the ansi ... declaration -
22417 this is detectable when the end of the arg list is not a
22418 void_type_node 2) an unprototyped function declaration (not a
22419 definition). This just means that we have no info about the
22420 parameters at all. */
22421 if (early_dwarf)
22423 if (prototype_p (TREE_TYPE (decl)))
22425 /* This is the prototyped case, check for.... */
22426 if (stdarg_p (TREE_TYPE (decl)))
22427 gen_unspecified_parameters_die (decl, subr_die);
22429 else if (DECL_INITIAL (decl) == NULL_TREE)
22430 gen_unspecified_parameters_die (decl, subr_die);
22434 if (subr_die != old_die)
22435 /* Add the calling convention attribute if requested. */
22436 add_calling_convention_attribute (subr_die, decl);
22438 /* Output Dwarf info for all of the stuff within the body of the function
22439 (if it has one - it may be just a declaration).
22441 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22442 a function. This BLOCK actually represents the outermost binding contour
22443 for the function, i.e. the contour in which the function's formal
22444 parameters and labels get declared. Curiously, it appears that the front
22445 end doesn't actually put the PARM_DECL nodes for the current function onto
22446 the BLOCK_VARS list for this outer scope, but are strung off of the
22447 DECL_ARGUMENTS list for the function instead.
22449 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22450 the LABEL_DECL nodes for the function however, and we output DWARF info
22451 for those in decls_for_scope. Just within the `outer_scope' there will be
22452 a BLOCK node representing the function's outermost pair of curly braces,
22453 and any blocks used for the base and member initializers of a C++
22454 constructor function. */
22455 tree outer_scope = DECL_INITIAL (decl);
22456 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22458 int call_site_note_count = 0;
22459 int tail_call_site_note_count = 0;
22461 /* Emit a DW_TAG_variable DIE for a named return value. */
22462 if (DECL_NAME (DECL_RESULT (decl)))
22463 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22465 /* The first time through decls_for_scope we will generate the
22466 DIEs for the locals. The second time, we fill in the
22467 location info. */
22468 decls_for_scope (outer_scope, subr_die);
22470 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22472 struct call_arg_loc_node *ca_loc;
22473 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22475 dw_die_ref die = NULL;
22476 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22477 rtx arg, next_arg;
22479 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22480 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22481 : NULL_RTX);
22482 arg; arg = next_arg)
22484 dw_loc_descr_ref reg, val;
22485 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22486 dw_die_ref cdie, tdie = NULL;
22488 next_arg = XEXP (arg, 1);
22489 if (REG_P (XEXP (XEXP (arg, 0), 0))
22490 && next_arg
22491 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22492 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22493 && REGNO (XEXP (XEXP (arg, 0), 0))
22494 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22495 next_arg = XEXP (next_arg, 1);
22496 if (mode == VOIDmode)
22498 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22499 if (mode == VOIDmode)
22500 mode = GET_MODE (XEXP (arg, 0));
22502 if (mode == VOIDmode || mode == BLKmode)
22503 continue;
22504 /* Get dynamic information about call target only if we
22505 have no static information: we cannot generate both
22506 DW_AT_call_origin and DW_AT_call_target
22507 attributes. */
22508 if (ca_loc->symbol_ref == NULL_RTX)
22510 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22512 tloc = XEXP (XEXP (arg, 0), 1);
22513 continue;
22515 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22516 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22518 tlocc = XEXP (XEXP (arg, 0), 1);
22519 continue;
22522 reg = NULL;
22523 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22524 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22525 VAR_INIT_STATUS_INITIALIZED);
22526 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22528 rtx mem = XEXP (XEXP (arg, 0), 0);
22529 reg = mem_loc_descriptor (XEXP (mem, 0),
22530 get_address_mode (mem),
22531 GET_MODE (mem),
22532 VAR_INIT_STATUS_INITIALIZED);
22534 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22535 == DEBUG_PARAMETER_REF)
22537 tree tdecl
22538 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22539 tdie = lookup_decl_die (tdecl);
22540 if (tdie == NULL)
22541 continue;
22543 else
22544 continue;
22545 if (reg == NULL
22546 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22547 != DEBUG_PARAMETER_REF)
22548 continue;
22549 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22550 VOIDmode,
22551 VAR_INIT_STATUS_INITIALIZED);
22552 if (val == NULL)
22553 continue;
22554 if (die == NULL)
22555 die = gen_call_site_die (decl, subr_die, ca_loc);
22556 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22557 NULL_TREE);
22558 if (reg != NULL)
22559 add_AT_loc (cdie, DW_AT_location, reg);
22560 else if (tdie != NULL)
22561 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22562 tdie);
22563 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22564 if (next_arg != XEXP (arg, 1))
22566 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22567 if (mode == VOIDmode)
22568 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22569 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22570 0), 1),
22571 mode, VOIDmode,
22572 VAR_INIT_STATUS_INITIALIZED);
22573 if (val != NULL)
22574 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22575 val);
22578 if (die == NULL
22579 && (ca_loc->symbol_ref || tloc))
22580 die = gen_call_site_die (decl, subr_die, ca_loc);
22581 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22583 dw_loc_descr_ref tval = NULL;
22585 if (tloc != NULL_RTX)
22586 tval = mem_loc_descriptor (tloc,
22587 GET_MODE (tloc) == VOIDmode
22588 ? Pmode : GET_MODE (tloc),
22589 VOIDmode,
22590 VAR_INIT_STATUS_INITIALIZED);
22591 if (tval)
22592 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22593 else if (tlocc != NULL_RTX)
22595 tval = mem_loc_descriptor (tlocc,
22596 GET_MODE (tlocc) == VOIDmode
22597 ? Pmode : GET_MODE (tlocc),
22598 VOIDmode,
22599 VAR_INIT_STATUS_INITIALIZED);
22600 if (tval)
22601 add_AT_loc (die,
22602 dwarf_AT (DW_AT_call_target_clobbered),
22603 tval);
22606 if (die != NULL)
22608 call_site_note_count++;
22609 if (ca_loc->tail_call_p)
22610 tail_call_site_note_count++;
22614 call_arg_locations = NULL;
22615 call_arg_loc_last = NULL;
22616 if (tail_call_site_count >= 0
22617 && tail_call_site_count == tail_call_site_note_count
22618 && (!dwarf_strict || dwarf_version >= 5))
22620 if (call_site_count >= 0
22621 && call_site_count == call_site_note_count)
22622 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22623 else
22624 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22626 call_site_count = -1;
22627 tail_call_site_count = -1;
22630 /* Mark used types after we have created DIEs for the functions scopes. */
22631 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22634 /* Returns a hash value for X (which really is a die_struct). */
22636 hashval_t
22637 block_die_hasher::hash (die_struct *d)
22639 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22642 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22643 as decl_id and die_parent of die_struct Y. */
22645 bool
22646 block_die_hasher::equal (die_struct *x, die_struct *y)
22648 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22651 /* Return TRUE if DECL, which may have been previously generated as
22652 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22653 true if decl (or its origin) is either an extern declaration or a
22654 class/namespace scoped declaration.
22656 The declare_in_namespace support causes us to get two DIEs for one
22657 variable, both of which are declarations. We want to avoid
22658 considering one to be a specification, so we must test for
22659 DECLARATION and DW_AT_declaration. */
22660 static inline bool
22661 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22663 return (old_die && TREE_STATIC (decl) && !declaration
22664 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22667 /* Return true if DECL is a local static. */
22669 static inline bool
22670 local_function_static (tree decl)
22672 gcc_assert (VAR_P (decl));
22673 return TREE_STATIC (decl)
22674 && DECL_CONTEXT (decl)
22675 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22678 /* Generate a DIE to represent a declared data object.
22679 Either DECL or ORIGIN must be non-null. */
22681 static void
22682 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22684 HOST_WIDE_INT off = 0;
22685 tree com_decl;
22686 tree decl_or_origin = decl ? decl : origin;
22687 tree ultimate_origin;
22688 dw_die_ref var_die;
22689 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22690 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22691 || class_or_namespace_scope_p (context_die));
22692 bool specialization_p = false;
22693 bool no_linkage_name = false;
22695 /* While C++ inline static data members have definitions inside of the
22696 class, force the first DIE to be a declaration, then let gen_member_die
22697 reparent it to the class context and call gen_variable_die again
22698 to create the outside of the class DIE for the definition. */
22699 if (!declaration
22700 && old_die == NULL
22701 && decl
22702 && DECL_CONTEXT (decl)
22703 && TYPE_P (DECL_CONTEXT (decl))
22704 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22706 declaration = true;
22707 if (dwarf_version < 5)
22708 no_linkage_name = true;
22711 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22712 if (decl || ultimate_origin)
22713 origin = ultimate_origin;
22714 com_decl = fortran_common (decl_or_origin, &off);
22716 /* Symbol in common gets emitted as a child of the common block, in the form
22717 of a data member. */
22718 if (com_decl)
22720 dw_die_ref com_die;
22721 dw_loc_list_ref loc = NULL;
22722 die_node com_die_arg;
22724 var_die = lookup_decl_die (decl_or_origin);
22725 if (var_die)
22727 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22729 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22730 if (loc)
22732 if (off)
22734 /* Optimize the common case. */
22735 if (single_element_loc_list_p (loc)
22736 && loc->expr->dw_loc_opc == DW_OP_addr
22737 && loc->expr->dw_loc_next == NULL
22738 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22739 == SYMBOL_REF)
22741 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22742 loc->expr->dw_loc_oprnd1.v.val_addr
22743 = plus_constant (GET_MODE (x), x , off);
22745 else
22746 loc_list_plus_const (loc, off);
22748 add_AT_location_description (var_die, DW_AT_location, loc);
22749 remove_AT (var_die, DW_AT_declaration);
22752 return;
22755 if (common_block_die_table == NULL)
22756 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22758 com_die_arg.decl_id = DECL_UID (com_decl);
22759 com_die_arg.die_parent = context_die;
22760 com_die = common_block_die_table->find (&com_die_arg);
22761 if (! early_dwarf)
22762 loc = loc_list_from_tree (com_decl, 2, NULL);
22763 if (com_die == NULL)
22765 const char *cnam
22766 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22767 die_node **slot;
22769 com_die = new_die (DW_TAG_common_block, context_die, decl);
22770 add_name_and_src_coords_attributes (com_die, com_decl);
22771 if (loc)
22773 add_AT_location_description (com_die, DW_AT_location, loc);
22774 /* Avoid sharing the same loc descriptor between
22775 DW_TAG_common_block and DW_TAG_variable. */
22776 loc = loc_list_from_tree (com_decl, 2, NULL);
22778 else if (DECL_EXTERNAL (decl_or_origin))
22779 add_AT_flag (com_die, DW_AT_declaration, 1);
22780 if (want_pubnames ())
22781 add_pubname_string (cnam, com_die); /* ??? needed? */
22782 com_die->decl_id = DECL_UID (com_decl);
22783 slot = common_block_die_table->find_slot (com_die, INSERT);
22784 *slot = com_die;
22786 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22788 add_AT_location_description (com_die, DW_AT_location, loc);
22789 loc = loc_list_from_tree (com_decl, 2, NULL);
22790 remove_AT (com_die, DW_AT_declaration);
22792 var_die = new_die (DW_TAG_variable, com_die, decl);
22793 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22794 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22795 decl_quals (decl_or_origin), false,
22796 context_die);
22797 add_alignment_attribute (var_die, decl);
22798 add_AT_flag (var_die, DW_AT_external, 1);
22799 if (loc)
22801 if (off)
22803 /* Optimize the common case. */
22804 if (single_element_loc_list_p (loc)
22805 && loc->expr->dw_loc_opc == DW_OP_addr
22806 && loc->expr->dw_loc_next == NULL
22807 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22809 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22810 loc->expr->dw_loc_oprnd1.v.val_addr
22811 = plus_constant (GET_MODE (x), x, off);
22813 else
22814 loc_list_plus_const (loc, off);
22816 add_AT_location_description (var_die, DW_AT_location, loc);
22818 else if (DECL_EXTERNAL (decl_or_origin))
22819 add_AT_flag (var_die, DW_AT_declaration, 1);
22820 if (decl)
22821 equate_decl_number_to_die (decl, var_die);
22822 return;
22825 if (old_die)
22827 if (declaration)
22829 /* A declaration that has been previously dumped, needs no
22830 further annotations, since it doesn't need location on
22831 the second pass. */
22832 return;
22834 else if (decl_will_get_specification_p (old_die, decl, declaration)
22835 && !get_AT (old_die, DW_AT_specification))
22837 /* Fall-thru so we can make a new variable die along with a
22838 DW_AT_specification. */
22840 else if (origin && old_die->die_parent != context_die)
22842 /* If we will be creating an inlined instance, we need a
22843 new DIE that will get annotated with
22844 DW_AT_abstract_origin. Clear things so we can get a
22845 new DIE. */
22846 gcc_assert (!DECL_ABSTRACT_P (decl));
22847 old_die = NULL;
22849 else
22851 /* If a DIE was dumped early, it still needs location info.
22852 Skip to where we fill the location bits. */
22853 var_die = old_die;
22855 /* ??? In LTRANS we cannot annotate early created variably
22856 modified type DIEs without copying them and adjusting all
22857 references to them. Thus we dumped them again, also add a
22858 reference to them. */
22859 tree type = TREE_TYPE (decl_or_origin);
22860 if (in_lto_p
22861 && variably_modified_type_p
22862 (type, decl_function_context (decl_or_origin)))
22864 if (decl_by_reference_p (decl_or_origin))
22865 add_type_attribute (var_die, TREE_TYPE (type),
22866 TYPE_UNQUALIFIED, false, context_die);
22867 else
22868 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22869 false, context_die);
22872 goto gen_variable_die_location;
22876 /* For static data members, the declaration in the class is supposed
22877 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22878 also in DWARF2; the specification should still be DW_TAG_variable
22879 referencing the DW_TAG_member DIE. */
22880 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22881 var_die = new_die (DW_TAG_member, context_die, decl);
22882 else
22883 var_die = new_die (DW_TAG_variable, context_die, decl);
22885 if (origin != NULL)
22886 add_abstract_origin_attribute (var_die, origin);
22888 /* Loop unrolling can create multiple blocks that refer to the same
22889 static variable, so we must test for the DW_AT_declaration flag.
22891 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22892 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22893 sharing them.
22895 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22896 else if (decl_will_get_specification_p (old_die, decl, declaration))
22898 /* This is a definition of a C++ class level static. */
22899 add_AT_specification (var_die, old_die);
22900 specialization_p = true;
22901 if (DECL_NAME (decl))
22903 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22904 struct dwarf_file_data * file_index = lookup_filename (s.file);
22906 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22907 add_AT_file (var_die, DW_AT_decl_file, file_index);
22909 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22910 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22912 if (debug_column_info
22913 && s.column
22914 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22915 != (unsigned) s.column))
22916 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22918 if (old_die->die_tag == DW_TAG_member)
22919 add_linkage_name (var_die, decl);
22922 else
22923 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22925 if ((origin == NULL && !specialization_p)
22926 || (origin != NULL
22927 && !DECL_ABSTRACT_P (decl_or_origin)
22928 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22929 decl_function_context
22930 (decl_or_origin))))
22932 tree type = TREE_TYPE (decl_or_origin);
22934 if (decl_by_reference_p (decl_or_origin))
22935 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22936 context_die);
22937 else
22938 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22939 context_die);
22942 if (origin == NULL && !specialization_p)
22944 if (TREE_PUBLIC (decl))
22945 add_AT_flag (var_die, DW_AT_external, 1);
22947 if (DECL_ARTIFICIAL (decl))
22948 add_AT_flag (var_die, DW_AT_artificial, 1);
22950 add_alignment_attribute (var_die, decl);
22952 add_accessibility_attribute (var_die, decl);
22955 if (declaration)
22956 add_AT_flag (var_die, DW_AT_declaration, 1);
22958 if (decl && (DECL_ABSTRACT_P (decl)
22959 || !old_die || is_declaration_die (old_die)))
22960 equate_decl_number_to_die (decl, var_die);
22962 gen_variable_die_location:
22963 if (! declaration
22964 && (! DECL_ABSTRACT_P (decl_or_origin)
22965 /* Local static vars are shared between all clones/inlines,
22966 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22967 already set. */
22968 || (VAR_P (decl_or_origin)
22969 && TREE_STATIC (decl_or_origin)
22970 && DECL_RTL_SET_P (decl_or_origin))))
22972 if (early_dwarf)
22973 add_pubname (decl_or_origin, var_die);
22974 else
22975 add_location_or_const_value_attribute (var_die, decl_or_origin,
22976 decl == NULL);
22978 else
22979 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22981 if ((dwarf_version >= 4 || !dwarf_strict)
22982 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22983 DW_AT_const_expr) == 1
22984 && !get_AT (var_die, DW_AT_const_expr)
22985 && !specialization_p)
22986 add_AT_flag (var_die, DW_AT_const_expr, 1);
22988 if (!dwarf_strict)
22990 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22991 DW_AT_inline);
22992 if (inl != -1
22993 && !get_AT (var_die, DW_AT_inline)
22994 && !specialization_p)
22995 add_AT_unsigned (var_die, DW_AT_inline, inl);
22999 /* Generate a DIE to represent a named constant. */
23001 static void
23002 gen_const_die (tree decl, dw_die_ref context_die)
23004 dw_die_ref const_die;
23005 tree type = TREE_TYPE (decl);
23007 const_die = lookup_decl_die (decl);
23008 if (const_die)
23009 return;
23011 const_die = new_die (DW_TAG_constant, context_die, decl);
23012 equate_decl_number_to_die (decl, const_die);
23013 add_name_and_src_coords_attributes (const_die, decl);
23014 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23015 if (TREE_PUBLIC (decl))
23016 add_AT_flag (const_die, DW_AT_external, 1);
23017 if (DECL_ARTIFICIAL (decl))
23018 add_AT_flag (const_die, DW_AT_artificial, 1);
23019 tree_add_const_value_attribute_for_decl (const_die, decl);
23022 /* Generate a DIE to represent a label identifier. */
23024 static void
23025 gen_label_die (tree decl, dw_die_ref context_die)
23027 tree origin = decl_ultimate_origin (decl);
23028 dw_die_ref lbl_die = lookup_decl_die (decl);
23029 rtx insn;
23030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23032 if (!lbl_die)
23034 lbl_die = new_die (DW_TAG_label, context_die, decl);
23035 equate_decl_number_to_die (decl, lbl_die);
23037 if (origin != NULL)
23038 add_abstract_origin_attribute (lbl_die, origin);
23039 else
23040 add_name_and_src_coords_attributes (lbl_die, decl);
23043 if (DECL_ABSTRACT_P (decl))
23044 equate_decl_number_to_die (decl, lbl_die);
23045 else if (! early_dwarf)
23047 insn = DECL_RTL_IF_SET (decl);
23049 /* Deleted labels are programmer specified labels which have been
23050 eliminated because of various optimizations. We still emit them
23051 here so that it is possible to put breakpoints on them. */
23052 if (insn
23053 && (LABEL_P (insn)
23054 || ((NOTE_P (insn)
23055 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23057 /* When optimization is enabled (via -O) some parts of the compiler
23058 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23059 represent source-level labels which were explicitly declared by
23060 the user. This really shouldn't be happening though, so catch
23061 it if it ever does happen. */
23062 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23064 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23065 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23067 else if (insn
23068 && NOTE_P (insn)
23069 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23070 && CODE_LABEL_NUMBER (insn) != -1)
23072 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23073 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23078 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23079 attributes to the DIE for a block STMT, to describe where the inlined
23080 function was called from. This is similar to add_src_coords_attributes. */
23082 static inline void
23083 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23085 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23087 if (dwarf_version >= 3 || !dwarf_strict)
23089 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23090 add_AT_unsigned (die, DW_AT_call_line, s.line);
23091 if (debug_column_info && s.column)
23092 add_AT_unsigned (die, DW_AT_call_column, s.column);
23097 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23098 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23100 static inline void
23101 add_high_low_attributes (tree stmt, dw_die_ref die)
23103 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23105 if (BLOCK_FRAGMENT_CHAIN (stmt)
23106 && (dwarf_version >= 3 || !dwarf_strict))
23108 tree chain, superblock = NULL_TREE;
23109 dw_die_ref pdie;
23110 dw_attr_node *attr = NULL;
23112 if (inlined_function_outer_scope_p (stmt))
23114 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23115 BLOCK_NUMBER (stmt));
23116 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23119 /* Optimize duplicate .debug_ranges lists or even tails of
23120 lists. If this BLOCK has same ranges as its supercontext,
23121 lookup DW_AT_ranges attribute in the supercontext (and
23122 recursively so), verify that the ranges_table contains the
23123 right values and use it instead of adding a new .debug_range. */
23124 for (chain = stmt, pdie = die;
23125 BLOCK_SAME_RANGE (chain);
23126 chain = BLOCK_SUPERCONTEXT (chain))
23128 dw_attr_node *new_attr;
23130 pdie = pdie->die_parent;
23131 if (pdie == NULL)
23132 break;
23133 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23134 break;
23135 new_attr = get_AT (pdie, DW_AT_ranges);
23136 if (new_attr == NULL
23137 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23138 break;
23139 attr = new_attr;
23140 superblock = BLOCK_SUPERCONTEXT (chain);
23142 if (attr != NULL
23143 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23144 == BLOCK_NUMBER (superblock))
23145 && BLOCK_FRAGMENT_CHAIN (superblock))
23147 unsigned long off = attr->dw_attr_val.v.val_offset;
23148 unsigned long supercnt = 0, thiscnt = 0;
23149 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23150 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23152 ++supercnt;
23153 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23154 == BLOCK_NUMBER (chain));
23156 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23157 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23158 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23159 ++thiscnt;
23160 gcc_assert (supercnt >= thiscnt);
23161 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23162 false);
23163 note_rnglist_head (off + supercnt - thiscnt);
23164 return;
23167 unsigned int offset = add_ranges (stmt, true);
23168 add_AT_range_list (die, DW_AT_ranges, offset, false);
23169 note_rnglist_head (offset);
23171 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23172 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23175 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23176 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23177 chain = BLOCK_FRAGMENT_CHAIN (chain);
23179 while (chain);
23180 add_ranges (NULL);
23182 else
23184 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23185 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23186 BLOCK_NUMBER (stmt));
23187 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23188 BLOCK_NUMBER (stmt));
23189 add_AT_low_high_pc (die, label, label_high, false);
23193 /* Generate a DIE for a lexical block. */
23195 static void
23196 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23198 dw_die_ref old_die = BLOCK_DIE (stmt);
23199 dw_die_ref stmt_die = NULL;
23200 if (!old_die)
23202 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23203 BLOCK_DIE (stmt) = stmt_die;
23206 if (BLOCK_ABSTRACT (stmt))
23208 if (old_die)
23210 /* This must have been generated early and it won't even
23211 need location information since it's a DW_AT_inline
23212 function. */
23213 if (flag_checking)
23214 for (dw_die_ref c = context_die; c; c = c->die_parent)
23215 if (c->die_tag == DW_TAG_inlined_subroutine
23216 || c->die_tag == DW_TAG_subprogram)
23218 gcc_assert (get_AT (c, DW_AT_inline));
23219 break;
23221 return;
23224 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23226 /* If this is an inlined instance, create a new lexical die for
23227 anything below to attach DW_AT_abstract_origin to. */
23228 if (old_die)
23230 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23231 BLOCK_DIE (stmt) = stmt_die;
23232 old_die = NULL;
23235 tree origin = block_ultimate_origin (stmt);
23236 if (origin != NULL_TREE && origin != stmt)
23237 add_abstract_origin_attribute (stmt_die, origin);
23240 if (old_die)
23241 stmt_die = old_die;
23243 /* A non abstract block whose blocks have already been reordered
23244 should have the instruction range for this block. If so, set the
23245 high/low attributes. */
23246 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23248 gcc_assert (stmt_die);
23249 add_high_low_attributes (stmt, stmt_die);
23252 decls_for_scope (stmt, stmt_die);
23255 /* Generate a DIE for an inlined subprogram. */
23257 static void
23258 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23260 tree decl;
23262 /* The instance of function that is effectively being inlined shall not
23263 be abstract. */
23264 gcc_assert (! BLOCK_ABSTRACT (stmt));
23266 decl = block_ultimate_origin (stmt);
23268 /* Make sure any inlined functions are known to be inlineable. */
23269 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23270 || cgraph_function_possibly_inlined_p (decl));
23272 if (! BLOCK_ABSTRACT (stmt))
23274 dw_die_ref subr_die
23275 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23277 if (call_arg_locations)
23278 BLOCK_DIE (stmt) = subr_die;
23279 add_abstract_origin_attribute (subr_die, decl);
23280 if (TREE_ASM_WRITTEN (stmt))
23281 add_high_low_attributes (stmt, subr_die);
23282 add_call_src_coords_attributes (stmt, subr_die);
23284 decls_for_scope (stmt, subr_die);
23288 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23289 the comment for VLR_CONTEXT. */
23291 static void
23292 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23294 dw_die_ref decl_die;
23296 if (TREE_TYPE (decl) == error_mark_node)
23297 return;
23299 decl_die = new_die (DW_TAG_member, context_die, decl);
23300 add_name_and_src_coords_attributes (decl_die, decl);
23301 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23302 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23303 context_die);
23305 if (DECL_BIT_FIELD_TYPE (decl))
23307 add_byte_size_attribute (decl_die, decl);
23308 add_bit_size_attribute (decl_die, decl);
23309 add_bit_offset_attribute (decl_die, decl, ctx);
23312 add_alignment_attribute (decl_die, decl);
23314 /* If we have a variant part offset, then we are supposed to process a member
23315 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23316 trees. */
23317 gcc_assert (ctx->variant_part_offset == NULL_TREE
23318 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23319 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23320 add_data_member_location_attribute (decl_die, decl, ctx);
23322 if (DECL_ARTIFICIAL (decl))
23323 add_AT_flag (decl_die, DW_AT_artificial, 1);
23325 add_accessibility_attribute (decl_die, decl);
23327 /* Equate decl number to die, so that we can look up this decl later on. */
23328 equate_decl_number_to_die (decl, decl_die);
23331 /* Generate a DIE for a pointer to a member type. TYPE can be an
23332 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23333 pointer to member function. */
23335 static void
23336 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23338 if (lookup_type_die (type))
23339 return;
23341 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23342 scope_die_for (type, context_die), type);
23344 equate_type_number_to_die (type, ptr_die);
23345 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23346 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23347 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23348 context_die);
23349 add_alignment_attribute (ptr_die, type);
23351 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23352 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23354 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23355 add_AT_loc (ptr_die, DW_AT_use_location, op);
23359 static char *producer_string;
23361 /* Return a heap allocated producer string including command line options
23362 if -grecord-gcc-switches. */
23364 static char *
23365 gen_producer_string (void)
23367 size_t j;
23368 auto_vec<const char *> switches;
23369 const char *language_string = lang_hooks.name;
23370 char *producer, *tail;
23371 const char *p;
23372 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23373 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23375 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23376 switch (save_decoded_options[j].opt_index)
23378 case OPT_o:
23379 case OPT_d:
23380 case OPT_dumpbase:
23381 case OPT_dumpdir:
23382 case OPT_auxbase:
23383 case OPT_auxbase_strip:
23384 case OPT_quiet:
23385 case OPT_version:
23386 case OPT_v:
23387 case OPT_w:
23388 case OPT_L:
23389 case OPT_D:
23390 case OPT_I:
23391 case OPT_U:
23392 case OPT_SPECIAL_unknown:
23393 case OPT_SPECIAL_ignore:
23394 case OPT_SPECIAL_program_name:
23395 case OPT_SPECIAL_input_file:
23396 case OPT_grecord_gcc_switches:
23397 case OPT__output_pch_:
23398 case OPT_fdiagnostics_show_location_:
23399 case OPT_fdiagnostics_show_option:
23400 case OPT_fdiagnostics_show_caret:
23401 case OPT_fdiagnostics_color_:
23402 case OPT_fverbose_asm:
23403 case OPT____:
23404 case OPT__sysroot_:
23405 case OPT_nostdinc:
23406 case OPT_nostdinc__:
23407 case OPT_fpreprocessed:
23408 case OPT_fltrans_output_list_:
23409 case OPT_fresolution_:
23410 case OPT_fdebug_prefix_map_:
23411 case OPT_fcompare_debug:
23412 /* Ignore these. */
23413 continue;
23414 default:
23415 if (cl_options[save_decoded_options[j].opt_index].flags
23416 & CL_NO_DWARF_RECORD)
23417 continue;
23418 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23419 == '-');
23420 switch (save_decoded_options[j].canonical_option[0][1])
23422 case 'M':
23423 case 'i':
23424 case 'W':
23425 continue;
23426 case 'f':
23427 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23428 "dump", 4) == 0)
23429 continue;
23430 break;
23431 default:
23432 break;
23434 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23435 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23436 break;
23439 producer = XNEWVEC (char, plen + 1 + len + 1);
23440 tail = producer;
23441 sprintf (tail, "%s %s", language_string, version_string);
23442 tail += plen;
23444 FOR_EACH_VEC_ELT (switches, j, p)
23446 len = strlen (p);
23447 *tail = ' ';
23448 memcpy (tail + 1, p, len);
23449 tail += len + 1;
23452 *tail = '\0';
23453 return producer;
23456 /* Given a C and/or C++ language/version string return the "highest".
23457 C++ is assumed to be "higher" than C in this case. Used for merging
23458 LTO translation unit languages. */
23459 static const char *
23460 highest_c_language (const char *lang1, const char *lang2)
23462 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23463 return "GNU C++17";
23464 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23465 return "GNU C++14";
23466 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23467 return "GNU C++11";
23468 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23469 return "GNU C++98";
23471 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23472 return "GNU C17";
23473 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23474 return "GNU C11";
23475 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23476 return "GNU C99";
23477 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23478 return "GNU C89";
23480 gcc_unreachable ();
23484 /* Generate the DIE for the compilation unit. */
23486 static dw_die_ref
23487 gen_compile_unit_die (const char *filename)
23489 dw_die_ref die;
23490 const char *language_string = lang_hooks.name;
23491 int language;
23493 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23495 if (filename)
23497 add_name_attribute (die, filename);
23498 /* Don't add cwd for <built-in>. */
23499 if (filename[0] != '<')
23500 add_comp_dir_attribute (die);
23503 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23505 /* If our producer is LTO try to figure out a common language to use
23506 from the global list of translation units. */
23507 if (strcmp (language_string, "GNU GIMPLE") == 0)
23509 unsigned i;
23510 tree t;
23511 const char *common_lang = NULL;
23513 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23515 if (!TRANSLATION_UNIT_LANGUAGE (t))
23516 continue;
23517 if (!common_lang)
23518 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23519 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23521 else if (strncmp (common_lang, "GNU C", 5) == 0
23522 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23523 /* Mixing C and C++ is ok, use C++ in that case. */
23524 common_lang = highest_c_language (common_lang,
23525 TRANSLATION_UNIT_LANGUAGE (t));
23526 else
23528 /* Fall back to C. */
23529 common_lang = NULL;
23530 break;
23534 if (common_lang)
23535 language_string = common_lang;
23538 language = DW_LANG_C;
23539 if (strncmp (language_string, "GNU C", 5) == 0
23540 && ISDIGIT (language_string[5]))
23542 language = DW_LANG_C89;
23543 if (dwarf_version >= 3 || !dwarf_strict)
23545 if (strcmp (language_string, "GNU C89") != 0)
23546 language = DW_LANG_C99;
23548 if (dwarf_version >= 5 /* || !dwarf_strict */)
23549 if (strcmp (language_string, "GNU C11") == 0
23550 || strcmp (language_string, "GNU C17") == 0)
23551 language = DW_LANG_C11;
23554 else if (strncmp (language_string, "GNU C++", 7) == 0)
23556 language = DW_LANG_C_plus_plus;
23557 if (dwarf_version >= 5 /* || !dwarf_strict */)
23559 if (strcmp (language_string, "GNU C++11") == 0)
23560 language = DW_LANG_C_plus_plus_11;
23561 else if (strcmp (language_string, "GNU C++14") == 0)
23562 language = DW_LANG_C_plus_plus_14;
23563 else if (strcmp (language_string, "GNU C++17") == 0)
23564 /* For now. */
23565 language = DW_LANG_C_plus_plus_14;
23568 else if (strcmp (language_string, "GNU F77") == 0)
23569 language = DW_LANG_Fortran77;
23570 else if (dwarf_version >= 3 || !dwarf_strict)
23572 if (strcmp (language_string, "GNU Ada") == 0)
23573 language = DW_LANG_Ada95;
23574 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23576 language = DW_LANG_Fortran95;
23577 if (dwarf_version >= 5 /* || !dwarf_strict */)
23579 if (strcmp (language_string, "GNU Fortran2003") == 0)
23580 language = DW_LANG_Fortran03;
23581 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23582 language = DW_LANG_Fortran08;
23585 else if (strcmp (language_string, "GNU Objective-C") == 0)
23586 language = DW_LANG_ObjC;
23587 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23588 language = DW_LANG_ObjC_plus_plus;
23589 else if (dwarf_version >= 5 || !dwarf_strict)
23591 if (strcmp (language_string, "GNU Go") == 0)
23592 language = DW_LANG_Go;
23595 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23596 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23597 language = DW_LANG_Fortran90;
23599 add_AT_unsigned (die, DW_AT_language, language);
23601 switch (language)
23603 case DW_LANG_Fortran77:
23604 case DW_LANG_Fortran90:
23605 case DW_LANG_Fortran95:
23606 case DW_LANG_Fortran03:
23607 case DW_LANG_Fortran08:
23608 /* Fortran has case insensitive identifiers and the front-end
23609 lowercases everything. */
23610 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23611 break;
23612 default:
23613 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23614 break;
23616 return die;
23619 /* Generate the DIE for a base class. */
23621 static void
23622 gen_inheritance_die (tree binfo, tree access, tree type,
23623 dw_die_ref context_die)
23625 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23626 struct vlr_context ctx = { type, NULL };
23628 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23629 context_die);
23630 add_data_member_location_attribute (die, binfo, &ctx);
23632 if (BINFO_VIRTUAL_P (binfo))
23633 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23635 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23636 children, otherwise the default is DW_ACCESS_public. In DWARF2
23637 the default has always been DW_ACCESS_private. */
23638 if (access == access_public_node)
23640 if (dwarf_version == 2
23641 || context_die->die_tag == DW_TAG_class_type)
23642 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23644 else if (access == access_protected_node)
23645 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23646 else if (dwarf_version > 2
23647 && context_die->die_tag != DW_TAG_class_type)
23648 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23651 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23652 structure. */
23653 static bool
23654 is_variant_part (tree decl)
23656 return (TREE_CODE (decl) == FIELD_DECL
23657 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23660 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23661 return the FIELD_DECL. Return NULL_TREE otherwise. */
23663 static tree
23664 analyze_discr_in_predicate (tree operand, tree struct_type)
23666 bool continue_stripping = true;
23667 while (continue_stripping)
23668 switch (TREE_CODE (operand))
23670 CASE_CONVERT:
23671 operand = TREE_OPERAND (operand, 0);
23672 break;
23673 default:
23674 continue_stripping = false;
23675 break;
23678 /* Match field access to members of struct_type only. */
23679 if (TREE_CODE (operand) == COMPONENT_REF
23680 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23681 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23682 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23683 return TREE_OPERAND (operand, 1);
23684 else
23685 return NULL_TREE;
23688 /* Check that SRC is a constant integer that can be represented as a native
23689 integer constant (either signed or unsigned). If so, store it into DEST and
23690 return true. Return false otherwise. */
23692 static bool
23693 get_discr_value (tree src, dw_discr_value *dest)
23695 tree discr_type = TREE_TYPE (src);
23697 if (lang_hooks.types.get_debug_type)
23699 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23700 if (debug_type != NULL)
23701 discr_type = debug_type;
23704 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23705 return false;
23707 /* Signedness can vary between the original type and the debug type. This
23708 can happen for character types in Ada for instance: the character type
23709 used for code generation can be signed, to be compatible with the C one,
23710 but from a debugger point of view, it must be unsigned. */
23711 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23712 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23714 if (is_orig_unsigned != is_debug_unsigned)
23715 src = fold_convert (discr_type, src);
23717 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23718 return false;
23720 dest->pos = is_debug_unsigned;
23721 if (is_debug_unsigned)
23722 dest->v.uval = tree_to_uhwi (src);
23723 else
23724 dest->v.sval = tree_to_shwi (src);
23726 return true;
23729 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23730 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23731 store NULL_TREE in DISCR_DECL. Otherwise:
23733 - store the discriminant field in STRUCT_TYPE that controls the variant
23734 part to *DISCR_DECL
23736 - put in *DISCR_LISTS_P an array where for each variant, the item
23737 represents the corresponding matching list of discriminant values.
23739 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23740 the above array.
23742 Note that when the array is allocated (i.e. when the analysis is
23743 successful), it is up to the caller to free the array. */
23745 static void
23746 analyze_variants_discr (tree variant_part_decl,
23747 tree struct_type,
23748 tree *discr_decl,
23749 dw_discr_list_ref **discr_lists_p,
23750 unsigned *discr_lists_length)
23752 tree variant_part_type = TREE_TYPE (variant_part_decl);
23753 tree variant;
23754 dw_discr_list_ref *discr_lists;
23755 unsigned i;
23757 /* Compute how many variants there are in this variant part. */
23758 *discr_lists_length = 0;
23759 for (variant = TYPE_FIELDS (variant_part_type);
23760 variant != NULL_TREE;
23761 variant = DECL_CHAIN (variant))
23762 ++*discr_lists_length;
23764 *discr_decl = NULL_TREE;
23765 *discr_lists_p
23766 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23767 sizeof (**discr_lists_p));
23768 discr_lists = *discr_lists_p;
23770 /* And then analyze all variants to extract discriminant information for all
23771 of them. This analysis is conservative: as soon as we detect something we
23772 do not support, abort everything and pretend we found nothing. */
23773 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23774 variant != NULL_TREE;
23775 variant = DECL_CHAIN (variant), ++i)
23777 tree match_expr = DECL_QUALIFIER (variant);
23779 /* Now, try to analyze the predicate and deduce a discriminant for
23780 it. */
23781 if (match_expr == boolean_true_node)
23782 /* Typically happens for the default variant: it matches all cases that
23783 previous variants rejected. Don't output any matching value for
23784 this one. */
23785 continue;
23787 /* The following loop tries to iterate over each discriminant
23788 possibility: single values or ranges. */
23789 while (match_expr != NULL_TREE)
23791 tree next_round_match_expr;
23792 tree candidate_discr = NULL_TREE;
23793 dw_discr_list_ref new_node = NULL;
23795 /* Possibilities are matched one after the other by nested
23796 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23797 continue with the rest at next iteration. */
23798 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23800 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23801 match_expr = TREE_OPERAND (match_expr, 1);
23803 else
23804 next_round_match_expr = NULL_TREE;
23806 if (match_expr == boolean_false_node)
23807 /* This sub-expression matches nothing: just wait for the next
23808 one. */
23811 else if (TREE_CODE (match_expr) == EQ_EXPR)
23813 /* We are matching: <discr_field> == <integer_cst>
23814 This sub-expression matches a single value. */
23815 tree integer_cst = TREE_OPERAND (match_expr, 1);
23817 candidate_discr
23818 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23819 struct_type);
23821 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23822 if (!get_discr_value (integer_cst,
23823 &new_node->dw_discr_lower_bound))
23824 goto abort;
23825 new_node->dw_discr_range = false;
23828 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23830 /* We are matching:
23831 <discr_field> > <integer_cst>
23832 && <discr_field> < <integer_cst>.
23833 This sub-expression matches the range of values between the
23834 two matched integer constants. Note that comparisons can be
23835 inclusive or exclusive. */
23836 tree candidate_discr_1, candidate_discr_2;
23837 tree lower_cst, upper_cst;
23838 bool lower_cst_included, upper_cst_included;
23839 tree lower_op = TREE_OPERAND (match_expr, 0);
23840 tree upper_op = TREE_OPERAND (match_expr, 1);
23842 /* When the comparison is exclusive, the integer constant is not
23843 the discriminant range bound we are looking for: we will have
23844 to increment or decrement it. */
23845 if (TREE_CODE (lower_op) == GE_EXPR)
23846 lower_cst_included = true;
23847 else if (TREE_CODE (lower_op) == GT_EXPR)
23848 lower_cst_included = false;
23849 else
23850 goto abort;
23852 if (TREE_CODE (upper_op) == LE_EXPR)
23853 upper_cst_included = true;
23854 else if (TREE_CODE (upper_op) == LT_EXPR)
23855 upper_cst_included = false;
23856 else
23857 goto abort;
23859 /* Extract the discriminant from the first operand and check it
23860 is consistant with the same analysis in the second
23861 operand. */
23862 candidate_discr_1
23863 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23864 struct_type);
23865 candidate_discr_2
23866 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23867 struct_type);
23868 if (candidate_discr_1 == candidate_discr_2)
23869 candidate_discr = candidate_discr_1;
23870 else
23871 goto abort;
23873 /* Extract bounds from both. */
23874 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23875 lower_cst = TREE_OPERAND (lower_op, 1);
23876 upper_cst = TREE_OPERAND (upper_op, 1);
23878 if (!lower_cst_included)
23879 lower_cst
23880 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23881 build_int_cst (TREE_TYPE (lower_cst), 1));
23882 if (!upper_cst_included)
23883 upper_cst
23884 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23885 build_int_cst (TREE_TYPE (upper_cst), 1));
23887 if (!get_discr_value (lower_cst,
23888 &new_node->dw_discr_lower_bound)
23889 || !get_discr_value (upper_cst,
23890 &new_node->dw_discr_upper_bound))
23891 goto abort;
23893 new_node->dw_discr_range = true;
23896 else
23897 /* Unsupported sub-expression: we cannot determine the set of
23898 matching discriminant values. Abort everything. */
23899 goto abort;
23901 /* If the discriminant info is not consistant with what we saw so
23902 far, consider the analysis failed and abort everything. */
23903 if (candidate_discr == NULL_TREE
23904 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23905 goto abort;
23906 else
23907 *discr_decl = candidate_discr;
23909 if (new_node != NULL)
23911 new_node->dw_discr_next = discr_lists[i];
23912 discr_lists[i] = new_node;
23914 match_expr = next_round_match_expr;
23918 /* If we reach this point, we could match everything we were interested
23919 in. */
23920 return;
23922 abort:
23923 /* Clean all data structure and return no result. */
23924 free (*discr_lists_p);
23925 *discr_lists_p = NULL;
23926 *discr_decl = NULL_TREE;
23929 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23930 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23931 under CONTEXT_DIE.
23933 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23934 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23935 this type, which are record types, represent the available variants and each
23936 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23937 values are inferred from these attributes.
23939 In trees, the offsets for the fields inside these sub-records are relative
23940 to the variant part itself, whereas the corresponding DIEs should have
23941 offset attributes that are relative to the embedding record base address.
23942 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23943 must be an expression that computes the offset of the variant part to
23944 describe in DWARF. */
23946 static void
23947 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23948 dw_die_ref context_die)
23950 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23951 tree variant_part_offset = vlr_ctx->variant_part_offset;
23952 struct loc_descr_context ctx = {
23953 vlr_ctx->struct_type, /* context_type */
23954 NULL_TREE, /* base_decl */
23955 NULL, /* dpi */
23956 false, /* placeholder_arg */
23957 false /* placeholder_seen */
23960 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23961 NULL_TREE if there is no such field. */
23962 tree discr_decl = NULL_TREE;
23963 dw_discr_list_ref *discr_lists;
23964 unsigned discr_lists_length = 0;
23965 unsigned i;
23967 dw_die_ref dwarf_proc_die = NULL;
23968 dw_die_ref variant_part_die
23969 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23971 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23973 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23974 &discr_decl, &discr_lists, &discr_lists_length);
23976 if (discr_decl != NULL_TREE)
23978 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23980 if (discr_die)
23981 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23982 else
23983 /* We have no DIE for the discriminant, so just discard all
23984 discrimimant information in the output. */
23985 discr_decl = NULL_TREE;
23988 /* If the offset for this variant part is more complex than a constant,
23989 create a DWARF procedure for it so that we will not have to generate DWARF
23990 expressions for it for each member. */
23991 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23992 && (dwarf_version >= 3 || !dwarf_strict))
23994 const tree dwarf_proc_fndecl
23995 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23996 build_function_type (TREE_TYPE (variant_part_offset),
23997 NULL_TREE));
23998 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23999 const dw_loc_descr_ref dwarf_proc_body
24000 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24002 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24003 dwarf_proc_fndecl, context_die);
24004 if (dwarf_proc_die != NULL)
24005 variant_part_offset = dwarf_proc_call;
24008 /* Output DIEs for all variants. */
24009 i = 0;
24010 for (tree variant = TYPE_FIELDS (variant_part_type);
24011 variant != NULL_TREE;
24012 variant = DECL_CHAIN (variant), ++i)
24014 tree variant_type = TREE_TYPE (variant);
24015 dw_die_ref variant_die;
24017 /* All variants (i.e. members of a variant part) are supposed to be
24018 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24019 under these records. */
24020 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24022 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24023 equate_decl_number_to_die (variant, variant_die);
24025 /* Output discriminant values this variant matches, if any. */
24026 if (discr_decl == NULL || discr_lists[i] == NULL)
24027 /* In the case we have discriminant information at all, this is
24028 probably the default variant: as the standard says, don't
24029 output any discriminant value/list attribute. */
24031 else if (discr_lists[i]->dw_discr_next == NULL
24032 && !discr_lists[i]->dw_discr_range)
24033 /* If there is only one accepted value, don't bother outputting a
24034 list. */
24035 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24036 else
24037 add_discr_list (variant_die, discr_lists[i]);
24039 for (tree member = TYPE_FIELDS (variant_type);
24040 member != NULL_TREE;
24041 member = DECL_CHAIN (member))
24043 struct vlr_context vlr_sub_ctx = {
24044 vlr_ctx->struct_type, /* struct_type */
24045 NULL /* variant_part_offset */
24047 if (is_variant_part (member))
24049 /* All offsets for fields inside variant parts are relative to
24050 the top-level embedding RECORD_TYPE's base address. On the
24051 other hand, offsets in GCC's types are relative to the
24052 nested-most variant part. So we have to sum offsets each time
24053 we recurse. */
24055 vlr_sub_ctx.variant_part_offset
24056 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24057 variant_part_offset, byte_position (member));
24058 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24060 else
24062 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24063 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24068 free (discr_lists);
24071 /* Generate a DIE for a class member. */
24073 static void
24074 gen_member_die (tree type, dw_die_ref context_die)
24076 tree member;
24077 tree binfo = TYPE_BINFO (type);
24079 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24081 /* If this is not an incomplete type, output descriptions of each of its
24082 members. Note that as we output the DIEs necessary to represent the
24083 members of this record or union type, we will also be trying to output
24084 DIEs to represent the *types* of those members. However the `type'
24085 function (above) will specifically avoid generating type DIEs for member
24086 types *within* the list of member DIEs for this (containing) type except
24087 for those types (of members) which are explicitly marked as also being
24088 members of this (containing) type themselves. The g++ front- end can
24089 force any given type to be treated as a member of some other (containing)
24090 type by setting the TYPE_CONTEXT of the given (member) type to point to
24091 the TREE node representing the appropriate (containing) type. */
24093 /* First output info about the base classes. */
24094 if (binfo)
24096 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24097 int i;
24098 tree base;
24100 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24101 gen_inheritance_die (base,
24102 (accesses ? (*accesses)[i] : access_public_node),
24103 type,
24104 context_die);
24107 /* Now output info about the data members and type members. */
24108 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24110 struct vlr_context vlr_ctx = { type, NULL_TREE };
24111 bool static_inline_p
24112 = (TREE_STATIC (member)
24113 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24114 != -1));
24116 /* Ignore clones. */
24117 if (DECL_ABSTRACT_ORIGIN (member))
24118 continue;
24120 /* If we thought we were generating minimal debug info for TYPE
24121 and then changed our minds, some of the member declarations
24122 may have already been defined. Don't define them again, but
24123 do put them in the right order. */
24125 if (dw_die_ref child = lookup_decl_die (member))
24127 /* Handle inline static data members, which only have in-class
24128 declarations. */
24129 dw_die_ref ref = NULL;
24130 if (child->die_tag == DW_TAG_variable
24131 && child->die_parent == comp_unit_die ())
24133 ref = get_AT_ref (child, DW_AT_specification);
24134 /* For C++17 inline static data members followed by redundant
24135 out of class redeclaration, we might get here with
24136 child being the DIE created for the out of class
24137 redeclaration and with its DW_AT_specification being
24138 the DIE created for in-class definition. We want to
24139 reparent the latter, and don't want to create another
24140 DIE with DW_AT_specification in that case, because
24141 we already have one. */
24142 if (ref
24143 && static_inline_p
24144 && ref->die_tag == DW_TAG_variable
24145 && ref->die_parent == comp_unit_die ()
24146 && get_AT (ref, DW_AT_specification) == NULL)
24148 child = ref;
24149 ref = NULL;
24150 static_inline_p = false;
24154 if (child->die_tag == DW_TAG_variable
24155 && child->die_parent == comp_unit_die ()
24156 && ref == NULL)
24158 reparent_child (child, context_die);
24159 if (dwarf_version < 5)
24160 child->die_tag = DW_TAG_member;
24162 else
24163 splice_child_die (context_die, child);
24166 /* Do not generate standard DWARF for variant parts if we are generating
24167 the corresponding GNAT encodings: DIEs generated for both would
24168 conflict in our mappings. */
24169 else if (is_variant_part (member)
24170 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24172 vlr_ctx.variant_part_offset = byte_position (member);
24173 gen_variant_part (member, &vlr_ctx, context_die);
24175 else
24177 vlr_ctx.variant_part_offset = NULL_TREE;
24178 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24181 /* For C++ inline static data members emit immediately a DW_TAG_variable
24182 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24183 DW_AT_specification. */
24184 if (static_inline_p)
24186 int old_extern = DECL_EXTERNAL (member);
24187 DECL_EXTERNAL (member) = 0;
24188 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24189 DECL_EXTERNAL (member) = old_extern;
24194 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24195 is set, we pretend that the type was never defined, so we only get the
24196 member DIEs needed by later specification DIEs. */
24198 static void
24199 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24200 enum debug_info_usage usage)
24202 if (TREE_ASM_WRITTEN (type))
24204 /* Fill in the bound of variable-length fields in late dwarf if
24205 still incomplete. */
24206 if (!early_dwarf && variably_modified_type_p (type, NULL))
24207 for (tree member = TYPE_FIELDS (type);
24208 member;
24209 member = DECL_CHAIN (member))
24210 fill_variable_array_bounds (TREE_TYPE (member));
24211 return;
24214 dw_die_ref type_die = lookup_type_die (type);
24215 dw_die_ref scope_die = 0;
24216 int nested = 0;
24217 int complete = (TYPE_SIZE (type)
24218 && (! TYPE_STUB_DECL (type)
24219 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24220 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24221 complete = complete && should_emit_struct_debug (type, usage);
24223 if (type_die && ! complete)
24224 return;
24226 if (TYPE_CONTEXT (type) != NULL_TREE
24227 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24228 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24229 nested = 1;
24231 scope_die = scope_die_for (type, context_die);
24233 /* Generate child dies for template paramaters. */
24234 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24235 schedule_generic_params_dies_gen (type);
24237 if (! type_die || (nested && is_cu_die (scope_die)))
24238 /* First occurrence of type or toplevel definition of nested class. */
24240 dw_die_ref old_die = type_die;
24242 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24243 ? record_type_tag (type) : DW_TAG_union_type,
24244 scope_die, type);
24245 equate_type_number_to_die (type, type_die);
24246 if (old_die)
24247 add_AT_specification (type_die, old_die);
24248 else
24249 add_name_attribute (type_die, type_tag (type));
24251 else
24252 remove_AT (type_die, DW_AT_declaration);
24254 /* If this type has been completed, then give it a byte_size attribute and
24255 then give a list of members. */
24256 if (complete && !ns_decl)
24258 /* Prevent infinite recursion in cases where the type of some member of
24259 this type is expressed in terms of this type itself. */
24260 TREE_ASM_WRITTEN (type) = 1;
24261 add_byte_size_attribute (type_die, type);
24262 add_alignment_attribute (type_die, type);
24263 if (TYPE_STUB_DECL (type) != NULL_TREE)
24265 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24266 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24269 /* If the first reference to this type was as the return type of an
24270 inline function, then it may not have a parent. Fix this now. */
24271 if (type_die->die_parent == NULL)
24272 add_child_die (scope_die, type_die);
24274 push_decl_scope (type);
24275 gen_member_die (type, type_die);
24276 pop_decl_scope ();
24278 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24279 if (TYPE_ARTIFICIAL (type))
24280 add_AT_flag (type_die, DW_AT_artificial, 1);
24282 /* GNU extension: Record what type our vtable lives in. */
24283 if (TYPE_VFIELD (type))
24285 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24287 gen_type_die (vtype, context_die);
24288 add_AT_die_ref (type_die, DW_AT_containing_type,
24289 lookup_type_die (vtype));
24292 else
24294 add_AT_flag (type_die, DW_AT_declaration, 1);
24296 /* We don't need to do this for function-local types. */
24297 if (TYPE_STUB_DECL (type)
24298 && ! decl_function_context (TYPE_STUB_DECL (type)))
24299 vec_safe_push (incomplete_types, type);
24302 if (get_AT (type_die, DW_AT_name))
24303 add_pubtype (type, type_die);
24306 /* Generate a DIE for a subroutine _type_. */
24308 static void
24309 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24311 tree return_type = TREE_TYPE (type);
24312 dw_die_ref subr_die
24313 = new_die (DW_TAG_subroutine_type,
24314 scope_die_for (type, context_die), type);
24316 equate_type_number_to_die (type, subr_die);
24317 add_prototyped_attribute (subr_die, type);
24318 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24319 context_die);
24320 add_alignment_attribute (subr_die, type);
24321 gen_formal_types_die (type, subr_die);
24323 if (get_AT (subr_die, DW_AT_name))
24324 add_pubtype (type, subr_die);
24325 if ((dwarf_version >= 5 || !dwarf_strict)
24326 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24327 add_AT_flag (subr_die, DW_AT_reference, 1);
24328 if ((dwarf_version >= 5 || !dwarf_strict)
24329 && lang_hooks.types.type_dwarf_attribute (type,
24330 DW_AT_rvalue_reference) != -1)
24331 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24334 /* Generate a DIE for a type definition. */
24336 static void
24337 gen_typedef_die (tree decl, dw_die_ref context_die)
24339 dw_die_ref type_die;
24340 tree type;
24342 if (TREE_ASM_WRITTEN (decl))
24344 if (DECL_ORIGINAL_TYPE (decl))
24345 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24346 return;
24349 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24350 checks in process_scope_var and modified_type_die), this should be called
24351 only for original types. */
24352 gcc_assert (decl_ultimate_origin (decl) == NULL
24353 || decl_ultimate_origin (decl) == decl);
24355 TREE_ASM_WRITTEN (decl) = 1;
24356 type_die = new_die (DW_TAG_typedef, context_die, decl);
24358 add_name_and_src_coords_attributes (type_die, decl);
24359 if (DECL_ORIGINAL_TYPE (decl))
24361 type = DECL_ORIGINAL_TYPE (decl);
24362 if (type == error_mark_node)
24363 return;
24365 gcc_assert (type != TREE_TYPE (decl));
24366 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24368 else
24370 type = TREE_TYPE (decl);
24371 if (type == error_mark_node)
24372 return;
24374 if (is_naming_typedef_decl (TYPE_NAME (type)))
24376 /* Here, we are in the case of decl being a typedef naming
24377 an anonymous type, e.g:
24378 typedef struct {...} foo;
24379 In that case TREE_TYPE (decl) is not a typedef variant
24380 type and TYPE_NAME of the anonymous type is set to the
24381 TYPE_DECL of the typedef. This construct is emitted by
24382 the C++ FE.
24384 TYPE is the anonymous struct named by the typedef
24385 DECL. As we need the DW_AT_type attribute of the
24386 DW_TAG_typedef to point to the DIE of TYPE, let's
24387 generate that DIE right away. add_type_attribute
24388 called below will then pick (via lookup_type_die) that
24389 anonymous struct DIE. */
24390 if (!TREE_ASM_WRITTEN (type))
24391 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24393 /* This is a GNU Extension. We are adding a
24394 DW_AT_linkage_name attribute to the DIE of the
24395 anonymous struct TYPE. The value of that attribute
24396 is the name of the typedef decl naming the anonymous
24397 struct. This greatly eases the work of consumers of
24398 this debug info. */
24399 add_linkage_name_raw (lookup_type_die (type), decl);
24403 add_type_attribute (type_die, type, decl_quals (decl), false,
24404 context_die);
24406 if (is_naming_typedef_decl (decl))
24407 /* We want that all subsequent calls to lookup_type_die with
24408 TYPE in argument yield the DW_TAG_typedef we have just
24409 created. */
24410 equate_type_number_to_die (type, type_die);
24412 add_alignment_attribute (type_die, TREE_TYPE (decl));
24414 add_accessibility_attribute (type_die, decl);
24416 if (DECL_ABSTRACT_P (decl))
24417 equate_decl_number_to_die (decl, type_die);
24419 if (get_AT (type_die, DW_AT_name))
24420 add_pubtype (decl, type_die);
24423 /* Generate a DIE for a struct, class, enum or union type. */
24425 static void
24426 gen_tagged_type_die (tree type,
24427 dw_die_ref context_die,
24428 enum debug_info_usage usage)
24430 int need_pop;
24432 if (type == NULL_TREE
24433 || !is_tagged_type (type))
24434 return;
24436 if (TREE_ASM_WRITTEN (type))
24437 need_pop = 0;
24438 /* If this is a nested type whose containing class hasn't been written
24439 out yet, writing it out will cover this one, too. This does not apply
24440 to instantiations of member class templates; they need to be added to
24441 the containing class as they are generated. FIXME: This hurts the
24442 idea of combining type decls from multiple TUs, since we can't predict
24443 what set of template instantiations we'll get. */
24444 else if (TYPE_CONTEXT (type)
24445 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24446 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24448 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24450 if (TREE_ASM_WRITTEN (type))
24451 return;
24453 /* If that failed, attach ourselves to the stub. */
24454 push_decl_scope (TYPE_CONTEXT (type));
24455 context_die = lookup_type_die (TYPE_CONTEXT (type));
24456 need_pop = 1;
24458 else if (TYPE_CONTEXT (type) != NULL_TREE
24459 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24461 /* If this type is local to a function that hasn't been written
24462 out yet, use a NULL context for now; it will be fixed up in
24463 decls_for_scope. */
24464 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24465 /* A declaration DIE doesn't count; nested types need to go in the
24466 specification. */
24467 if (context_die && is_declaration_die (context_die))
24468 context_die = NULL;
24469 need_pop = 0;
24471 else
24473 context_die = declare_in_namespace (type, context_die);
24474 need_pop = 0;
24477 if (TREE_CODE (type) == ENUMERAL_TYPE)
24479 /* This might have been written out by the call to
24480 declare_in_namespace. */
24481 if (!TREE_ASM_WRITTEN (type))
24482 gen_enumeration_type_die (type, context_die);
24484 else
24485 gen_struct_or_union_type_die (type, context_die, usage);
24487 if (need_pop)
24488 pop_decl_scope ();
24490 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24491 it up if it is ever completed. gen_*_type_die will set it for us
24492 when appropriate. */
24495 /* Generate a type description DIE. */
24497 static void
24498 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24499 enum debug_info_usage usage)
24501 struct array_descr_info info;
24503 if (type == NULL_TREE || type == error_mark_node)
24504 return;
24506 if (flag_checking && type)
24507 verify_type (type);
24509 if (TYPE_NAME (type) != NULL_TREE
24510 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24511 && is_redundant_typedef (TYPE_NAME (type))
24512 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24513 /* The DECL of this type is a typedef we don't want to emit debug
24514 info for but we want debug info for its underlying typedef.
24515 This can happen for e.g, the injected-class-name of a C++
24516 type. */
24517 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24519 /* If TYPE is a typedef type variant, let's generate debug info
24520 for the parent typedef which TYPE is a type of. */
24521 if (typedef_variant_p (type))
24523 if (TREE_ASM_WRITTEN (type))
24524 return;
24526 tree name = TYPE_NAME (type);
24527 tree origin = decl_ultimate_origin (name);
24528 if (origin != NULL && origin != name)
24530 gen_decl_die (origin, NULL, NULL, context_die);
24531 return;
24534 /* Prevent broken recursion; we can't hand off to the same type. */
24535 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24537 /* Give typedefs the right scope. */
24538 context_die = scope_die_for (type, context_die);
24540 TREE_ASM_WRITTEN (type) = 1;
24542 gen_decl_die (name, NULL, NULL, context_die);
24543 return;
24546 /* If type is an anonymous tagged type named by a typedef, let's
24547 generate debug info for the typedef. */
24548 if (is_naming_typedef_decl (TYPE_NAME (type)))
24550 /* Use the DIE of the containing namespace as the parent DIE of
24551 the type description DIE we want to generate. */
24552 if (DECL_CONTEXT (TYPE_NAME (type))
24553 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24554 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24556 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24557 return;
24560 if (lang_hooks.types.get_debug_type)
24562 tree debug_type = lang_hooks.types.get_debug_type (type);
24564 if (debug_type != NULL_TREE && debug_type != type)
24566 gen_type_die_with_usage (debug_type, context_die, usage);
24567 return;
24571 /* We are going to output a DIE to represent the unqualified version
24572 of this type (i.e. without any const or volatile qualifiers) so
24573 get the main variant (i.e. the unqualified version) of this type
24574 now. (Vectors and arrays are special because the debugging info is in the
24575 cloned type itself. Similarly function/method types can contain extra
24576 ref-qualification). */
24577 if (TREE_CODE (type) == FUNCTION_TYPE
24578 || TREE_CODE (type) == METHOD_TYPE)
24580 /* For function/method types, can't use type_main_variant here,
24581 because that can have different ref-qualifiers for C++,
24582 but try to canonicalize. */
24583 tree main = TYPE_MAIN_VARIANT (type);
24584 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24585 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24586 && check_base_type (t, main)
24587 && check_lang_type (t, type))
24589 type = t;
24590 break;
24593 else if (TREE_CODE (type) != VECTOR_TYPE
24594 && TREE_CODE (type) != ARRAY_TYPE)
24595 type = type_main_variant (type);
24597 /* If this is an array type with hidden descriptor, handle it first. */
24598 if (!TREE_ASM_WRITTEN (type)
24599 && lang_hooks.types.get_array_descr_info)
24601 memset (&info, 0, sizeof (info));
24602 if (lang_hooks.types.get_array_descr_info (type, &info))
24604 /* Fortran sometimes emits array types with no dimension. */
24605 gcc_assert (info.ndimensions >= 0
24606 && (info.ndimensions
24607 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24608 gen_descr_array_type_die (type, &info, context_die);
24609 TREE_ASM_WRITTEN (type) = 1;
24610 return;
24614 if (TREE_ASM_WRITTEN (type))
24616 /* Variable-length types may be incomplete even if
24617 TREE_ASM_WRITTEN. For such types, fall through to
24618 gen_array_type_die() and possibly fill in
24619 DW_AT_{upper,lower}_bound attributes. */
24620 if ((TREE_CODE (type) != ARRAY_TYPE
24621 && TREE_CODE (type) != RECORD_TYPE
24622 && TREE_CODE (type) != UNION_TYPE
24623 && TREE_CODE (type) != QUAL_UNION_TYPE)
24624 || !variably_modified_type_p (type, NULL))
24625 return;
24628 switch (TREE_CODE (type))
24630 case ERROR_MARK:
24631 break;
24633 case POINTER_TYPE:
24634 case REFERENCE_TYPE:
24635 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24636 ensures that the gen_type_die recursion will terminate even if the
24637 type is recursive. Recursive types are possible in Ada. */
24638 /* ??? We could perhaps do this for all types before the switch
24639 statement. */
24640 TREE_ASM_WRITTEN (type) = 1;
24642 /* For these types, all that is required is that we output a DIE (or a
24643 set of DIEs) to represent the "basis" type. */
24644 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24645 DINFO_USAGE_IND_USE);
24646 break;
24648 case OFFSET_TYPE:
24649 /* This code is used for C++ pointer-to-data-member types.
24650 Output a description of the relevant class type. */
24651 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24652 DINFO_USAGE_IND_USE);
24654 /* Output a description of the type of the object pointed to. */
24655 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24656 DINFO_USAGE_IND_USE);
24658 /* Now output a DIE to represent this pointer-to-data-member type
24659 itself. */
24660 gen_ptr_to_mbr_type_die (type, context_die);
24661 break;
24663 case FUNCTION_TYPE:
24664 /* Force out return type (in case it wasn't forced out already). */
24665 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24666 DINFO_USAGE_DIR_USE);
24667 gen_subroutine_type_die (type, context_die);
24668 break;
24670 case METHOD_TYPE:
24671 /* Force out return type (in case it wasn't forced out already). */
24672 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24673 DINFO_USAGE_DIR_USE);
24674 gen_subroutine_type_die (type, context_die);
24675 break;
24677 case ARRAY_TYPE:
24678 case VECTOR_TYPE:
24679 gen_array_type_die (type, context_die);
24680 break;
24682 case ENUMERAL_TYPE:
24683 case RECORD_TYPE:
24684 case UNION_TYPE:
24685 case QUAL_UNION_TYPE:
24686 gen_tagged_type_die (type, context_die, usage);
24687 return;
24689 case VOID_TYPE:
24690 case INTEGER_TYPE:
24691 case REAL_TYPE:
24692 case FIXED_POINT_TYPE:
24693 case COMPLEX_TYPE:
24694 case BOOLEAN_TYPE:
24695 case POINTER_BOUNDS_TYPE:
24696 /* No DIEs needed for fundamental types. */
24697 break;
24699 case NULLPTR_TYPE:
24700 case LANG_TYPE:
24701 /* Just use DW_TAG_unspecified_type. */
24703 dw_die_ref type_die = lookup_type_die (type);
24704 if (type_die == NULL)
24706 tree name = TYPE_IDENTIFIER (type);
24707 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24708 type);
24709 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24710 equate_type_number_to_die (type, type_die);
24713 break;
24715 default:
24716 if (is_cxx_auto (type))
24718 tree name = TYPE_IDENTIFIER (type);
24719 dw_die_ref *die = (name == get_identifier ("auto")
24720 ? &auto_die : &decltype_auto_die);
24721 if (!*die)
24723 *die = new_die (DW_TAG_unspecified_type,
24724 comp_unit_die (), NULL_TREE);
24725 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24727 equate_type_number_to_die (type, *die);
24728 break;
24730 gcc_unreachable ();
24733 TREE_ASM_WRITTEN (type) = 1;
24736 static void
24737 gen_type_die (tree type, dw_die_ref context_die)
24739 if (type != error_mark_node)
24741 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24742 if (flag_checking)
24744 dw_die_ref die = lookup_type_die (type);
24745 if (die)
24746 check_die (die);
24751 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24752 things which are local to the given block. */
24754 static void
24755 gen_block_die (tree stmt, dw_die_ref context_die)
24757 int must_output_die = 0;
24758 bool inlined_func;
24760 /* Ignore blocks that are NULL. */
24761 if (stmt == NULL_TREE)
24762 return;
24764 inlined_func = inlined_function_outer_scope_p (stmt);
24766 /* If the block is one fragment of a non-contiguous block, do not
24767 process the variables, since they will have been done by the
24768 origin block. Do process subblocks. */
24769 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24771 tree sub;
24773 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24774 gen_block_die (sub, context_die);
24776 return;
24779 /* Determine if we need to output any Dwarf DIEs at all to represent this
24780 block. */
24781 if (inlined_func)
24782 /* The outer scopes for inlinings *must* always be represented. We
24783 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24784 must_output_die = 1;
24785 else
24787 /* Determine if this block directly contains any "significant"
24788 local declarations which we will need to output DIEs for. */
24789 if (debug_info_level > DINFO_LEVEL_TERSE)
24790 /* We are not in terse mode so *any* local declaration counts
24791 as being a "significant" one. */
24792 must_output_die = ((BLOCK_VARS (stmt) != NULL
24793 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24794 && (TREE_USED (stmt)
24795 || TREE_ASM_WRITTEN (stmt)
24796 || BLOCK_ABSTRACT (stmt)));
24797 else if ((TREE_USED (stmt)
24798 || TREE_ASM_WRITTEN (stmt)
24799 || BLOCK_ABSTRACT (stmt))
24800 && !dwarf2out_ignore_block (stmt))
24801 must_output_die = 1;
24804 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24805 DIE for any block which contains no significant local declarations at
24806 all. Rather, in such cases we just call `decls_for_scope' so that any
24807 needed Dwarf info for any sub-blocks will get properly generated. Note
24808 that in terse mode, our definition of what constitutes a "significant"
24809 local declaration gets restricted to include only inlined function
24810 instances and local (nested) function definitions. */
24811 if (must_output_die)
24813 if (inlined_func)
24815 /* If STMT block is abstract, that means we have been called
24816 indirectly from dwarf2out_abstract_function.
24817 That function rightfully marks the descendent blocks (of
24818 the abstract function it is dealing with) as being abstract,
24819 precisely to prevent us from emitting any
24820 DW_TAG_inlined_subroutine DIE as a descendent
24821 of an abstract function instance. So in that case, we should
24822 not call gen_inlined_subroutine_die.
24824 Later though, when cgraph asks dwarf2out to emit info
24825 for the concrete instance of the function decl into which
24826 the concrete instance of STMT got inlined, the later will lead
24827 to the generation of a DW_TAG_inlined_subroutine DIE. */
24828 if (! BLOCK_ABSTRACT (stmt))
24829 gen_inlined_subroutine_die (stmt, context_die);
24831 else
24832 gen_lexical_block_die (stmt, context_die);
24834 else
24835 decls_for_scope (stmt, context_die);
24838 /* Process variable DECL (or variable with origin ORIGIN) within
24839 block STMT and add it to CONTEXT_DIE. */
24840 static void
24841 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24843 dw_die_ref die;
24844 tree decl_or_origin = decl ? decl : origin;
24846 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24847 die = lookup_decl_die (decl_or_origin);
24848 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24850 if (TYPE_DECL_IS_STUB (decl_or_origin))
24851 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24852 else
24853 die = lookup_decl_die (decl_or_origin);
24854 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24855 if (! die && ! early_dwarf)
24856 return;
24858 else
24859 die = NULL;
24861 /* Avoid creating DIEs for local typedefs and concrete static variables that
24862 will only be pruned later. */
24863 if ((origin || decl_ultimate_origin (decl))
24864 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24865 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24867 origin = decl_ultimate_origin (decl_or_origin);
24868 if (decl && VAR_P (decl) && die != NULL)
24870 die = lookup_decl_die (origin);
24871 if (die != NULL)
24872 equate_decl_number_to_die (decl, die);
24874 return;
24877 if (die != NULL && die->die_parent == NULL)
24878 add_child_die (context_die, die);
24879 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24881 if (early_dwarf)
24882 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24883 stmt, context_die);
24885 else
24887 if (decl && DECL_P (decl))
24889 die = lookup_decl_die (decl);
24891 /* Early created DIEs do not have a parent as the decls refer
24892 to the function as DECL_CONTEXT rather than the BLOCK. */
24893 if (die && die->die_parent == NULL)
24895 gcc_assert (in_lto_p);
24896 add_child_die (context_die, die);
24900 gen_decl_die (decl, origin, NULL, context_die);
24904 /* Generate all of the decls declared within a given scope and (recursively)
24905 all of its sub-blocks. */
24907 static void
24908 decls_for_scope (tree stmt, dw_die_ref context_die)
24910 tree decl;
24911 unsigned int i;
24912 tree subblocks;
24914 /* Ignore NULL blocks. */
24915 if (stmt == NULL_TREE)
24916 return;
24918 /* Output the DIEs to represent all of the data objects and typedefs
24919 declared directly within this block but not within any nested
24920 sub-blocks. Also, nested function and tag DIEs have been
24921 generated with a parent of NULL; fix that up now. We don't
24922 have to do this if we're at -g1. */
24923 if (debug_info_level > DINFO_LEVEL_TERSE)
24925 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24926 process_scope_var (stmt, decl, NULL_TREE, context_die);
24927 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24928 origin - avoid doing this twice as we have no good way to see
24929 if we've done it once already. */
24930 if (! early_dwarf)
24931 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24933 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24934 if (decl == current_function_decl)
24935 /* Ignore declarations of the current function, while they
24936 are declarations, gen_subprogram_die would treat them
24937 as definitions again, because they are equal to
24938 current_function_decl and endlessly recurse. */;
24939 else if (TREE_CODE (decl) == FUNCTION_DECL)
24940 process_scope_var (stmt, decl, NULL_TREE, context_die);
24941 else
24942 process_scope_var (stmt, NULL_TREE, decl, context_die);
24946 /* Even if we're at -g1, we need to process the subblocks in order to get
24947 inlined call information. */
24949 /* Output the DIEs to represent all sub-blocks (and the items declared
24950 therein) of this block. */
24951 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24952 subblocks != NULL;
24953 subblocks = BLOCK_CHAIN (subblocks))
24954 gen_block_die (subblocks, context_die);
24957 /* Is this a typedef we can avoid emitting? */
24959 bool
24960 is_redundant_typedef (const_tree decl)
24962 if (TYPE_DECL_IS_STUB (decl))
24963 return true;
24965 if (DECL_ARTIFICIAL (decl)
24966 && DECL_CONTEXT (decl)
24967 && is_tagged_type (DECL_CONTEXT (decl))
24968 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24969 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24970 /* Also ignore the artificial member typedef for the class name. */
24971 return true;
24973 return false;
24976 /* Return TRUE if TYPE is a typedef that names a type for linkage
24977 purposes. This kind of typedefs is produced by the C++ FE for
24978 constructs like:
24980 typedef struct {...} foo;
24982 In that case, there is no typedef variant type produced for foo.
24983 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24984 struct type. */
24986 static bool
24987 is_naming_typedef_decl (const_tree decl)
24989 if (decl == NULL_TREE
24990 || TREE_CODE (decl) != TYPE_DECL
24991 || DECL_NAMELESS (decl)
24992 || !is_tagged_type (TREE_TYPE (decl))
24993 || DECL_IS_BUILTIN (decl)
24994 || is_redundant_typedef (decl)
24995 /* It looks like Ada produces TYPE_DECLs that are very similar
24996 to C++ naming typedefs but that have different
24997 semantics. Let's be specific to c++ for now. */
24998 || !is_cxx (decl))
24999 return FALSE;
25001 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25002 && TYPE_NAME (TREE_TYPE (decl)) == decl
25003 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25004 != TYPE_NAME (TREE_TYPE (decl))));
25007 /* Looks up the DIE for a context. */
25009 static inline dw_die_ref
25010 lookup_context_die (tree context)
25012 if (context)
25014 /* Find die that represents this context. */
25015 if (TYPE_P (context))
25017 context = TYPE_MAIN_VARIANT (context);
25018 dw_die_ref ctx = lookup_type_die (context);
25019 if (!ctx)
25020 return NULL;
25021 return strip_naming_typedef (context, ctx);
25023 else
25024 return lookup_decl_die (context);
25026 return comp_unit_die ();
25029 /* Returns the DIE for a context. */
25031 static inline dw_die_ref
25032 get_context_die (tree context)
25034 if (context)
25036 /* Find die that represents this context. */
25037 if (TYPE_P (context))
25039 context = TYPE_MAIN_VARIANT (context);
25040 return strip_naming_typedef (context, force_type_die (context));
25042 else
25043 return force_decl_die (context);
25045 return comp_unit_die ();
25048 /* Returns the DIE for decl. A DIE will always be returned. */
25050 static dw_die_ref
25051 force_decl_die (tree decl)
25053 dw_die_ref decl_die;
25054 unsigned saved_external_flag;
25055 tree save_fn = NULL_TREE;
25056 decl_die = lookup_decl_die (decl);
25057 if (!decl_die)
25059 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25061 decl_die = lookup_decl_die (decl);
25062 if (decl_die)
25063 return decl_die;
25065 switch (TREE_CODE (decl))
25067 case FUNCTION_DECL:
25068 /* Clear current_function_decl, so that gen_subprogram_die thinks
25069 that this is a declaration. At this point, we just want to force
25070 declaration die. */
25071 save_fn = current_function_decl;
25072 current_function_decl = NULL_TREE;
25073 gen_subprogram_die (decl, context_die);
25074 current_function_decl = save_fn;
25075 break;
25077 case VAR_DECL:
25078 /* Set external flag to force declaration die. Restore it after
25079 gen_decl_die() call. */
25080 saved_external_flag = DECL_EXTERNAL (decl);
25081 DECL_EXTERNAL (decl) = 1;
25082 gen_decl_die (decl, NULL, NULL, context_die);
25083 DECL_EXTERNAL (decl) = saved_external_flag;
25084 break;
25086 case NAMESPACE_DECL:
25087 if (dwarf_version >= 3 || !dwarf_strict)
25088 dwarf2out_decl (decl);
25089 else
25090 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25091 decl_die = comp_unit_die ();
25092 break;
25094 case TRANSLATION_UNIT_DECL:
25095 decl_die = comp_unit_die ();
25096 break;
25098 default:
25099 gcc_unreachable ();
25102 /* We should be able to find the DIE now. */
25103 if (!decl_die)
25104 decl_die = lookup_decl_die (decl);
25105 gcc_assert (decl_die);
25108 return decl_die;
25111 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25112 always returned. */
25114 static dw_die_ref
25115 force_type_die (tree type)
25117 dw_die_ref type_die;
25119 type_die = lookup_type_die (type);
25120 if (!type_die)
25122 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25124 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25125 false, context_die);
25126 gcc_assert (type_die);
25128 return type_die;
25131 /* Force out any required namespaces to be able to output DECL,
25132 and return the new context_die for it, if it's changed. */
25134 static dw_die_ref
25135 setup_namespace_context (tree thing, dw_die_ref context_die)
25137 tree context = (DECL_P (thing)
25138 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25139 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25140 /* Force out the namespace. */
25141 context_die = force_decl_die (context);
25143 return context_die;
25146 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25147 type) within its namespace, if appropriate.
25149 For compatibility with older debuggers, namespace DIEs only contain
25150 declarations; all definitions are emitted at CU scope, with
25151 DW_AT_specification pointing to the declaration (like with class
25152 members). */
25154 static dw_die_ref
25155 declare_in_namespace (tree thing, dw_die_ref context_die)
25157 dw_die_ref ns_context;
25159 if (debug_info_level <= DINFO_LEVEL_TERSE)
25160 return context_die;
25162 /* External declarations in the local scope only need to be emitted
25163 once, not once in the namespace and once in the scope.
25165 This avoids declaring the `extern' below in the
25166 namespace DIE as well as in the innermost scope:
25168 namespace S
25170 int i=5;
25171 int foo()
25173 int i=8;
25174 extern int i;
25175 return i;
25179 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25180 return context_die;
25182 /* If this decl is from an inlined function, then don't try to emit it in its
25183 namespace, as we will get confused. It would have already been emitted
25184 when the abstract instance of the inline function was emitted anyways. */
25185 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25186 return context_die;
25188 ns_context = setup_namespace_context (thing, context_die);
25190 if (ns_context != context_die)
25192 if (is_fortran ())
25193 return ns_context;
25194 if (DECL_P (thing))
25195 gen_decl_die (thing, NULL, NULL, ns_context);
25196 else
25197 gen_type_die (thing, ns_context);
25199 return context_die;
25202 /* Generate a DIE for a namespace or namespace alias. */
25204 static void
25205 gen_namespace_die (tree decl, dw_die_ref context_die)
25207 dw_die_ref namespace_die;
25209 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25210 they are an alias of. */
25211 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25213 /* Output a real namespace or module. */
25214 context_die = setup_namespace_context (decl, comp_unit_die ());
25215 namespace_die = new_die (is_fortran ()
25216 ? DW_TAG_module : DW_TAG_namespace,
25217 context_die, decl);
25218 /* For Fortran modules defined in different CU don't add src coords. */
25219 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25221 const char *name = dwarf2_name (decl, 0);
25222 if (name)
25223 add_name_attribute (namespace_die, name);
25225 else
25226 add_name_and_src_coords_attributes (namespace_die, decl);
25227 if (DECL_EXTERNAL (decl))
25228 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25229 equate_decl_number_to_die (decl, namespace_die);
25231 else
25233 /* Output a namespace alias. */
25235 /* Force out the namespace we are an alias of, if necessary. */
25236 dw_die_ref origin_die
25237 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25239 if (DECL_FILE_SCOPE_P (decl)
25240 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25241 context_die = setup_namespace_context (decl, comp_unit_die ());
25242 /* Now create the namespace alias DIE. */
25243 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25244 add_name_and_src_coords_attributes (namespace_die, decl);
25245 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25246 equate_decl_number_to_die (decl, namespace_die);
25248 if ((dwarf_version >= 5 || !dwarf_strict)
25249 && lang_hooks.decls.decl_dwarf_attribute (decl,
25250 DW_AT_export_symbols) == 1)
25251 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25253 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25254 if (want_pubnames ())
25255 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25258 /* Generate Dwarf debug information for a decl described by DECL.
25259 The return value is currently only meaningful for PARM_DECLs,
25260 for all other decls it returns NULL.
25262 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25263 It can be NULL otherwise. */
25265 static dw_die_ref
25266 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25267 dw_die_ref context_die)
25269 tree decl_or_origin = decl ? decl : origin;
25270 tree class_origin = NULL, ultimate_origin;
25272 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25273 return NULL;
25275 /* Ignore pointer bounds decls. */
25276 if (DECL_P (decl_or_origin)
25277 && TREE_TYPE (decl_or_origin)
25278 && POINTER_BOUNDS_P (decl_or_origin))
25279 return NULL;
25281 switch (TREE_CODE (decl_or_origin))
25283 case ERROR_MARK:
25284 break;
25286 case CONST_DECL:
25287 if (!is_fortran () && !is_ada ())
25289 /* The individual enumerators of an enum type get output when we output
25290 the Dwarf representation of the relevant enum type itself. */
25291 break;
25294 /* Emit its type. */
25295 gen_type_die (TREE_TYPE (decl), context_die);
25297 /* And its containing namespace. */
25298 context_die = declare_in_namespace (decl, context_die);
25300 gen_const_die (decl, context_die);
25301 break;
25303 case FUNCTION_DECL:
25304 #if 0
25305 /* FIXME */
25306 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25307 on local redeclarations of global functions. That seems broken. */
25308 if (current_function_decl != decl)
25309 /* This is only a declaration. */;
25310 #endif
25312 /* We should have abstract copies already and should not generate
25313 stray type DIEs in late LTO dumping. */
25314 if (! early_dwarf)
25317 /* If we're emitting a clone, emit info for the abstract instance. */
25318 else if (origin || DECL_ORIGIN (decl) != decl)
25319 dwarf2out_abstract_function (origin
25320 ? DECL_ORIGIN (origin)
25321 : DECL_ABSTRACT_ORIGIN (decl));
25323 /* If we're emitting a possibly inlined function emit it as
25324 abstract instance. */
25325 else if (cgraph_function_possibly_inlined_p (decl)
25326 && ! DECL_ABSTRACT_P (decl)
25327 && ! class_or_namespace_scope_p (context_die)
25328 /* dwarf2out_abstract_function won't emit a die if this is just
25329 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25330 that case, because that works only if we have a die. */
25331 && DECL_INITIAL (decl) != NULL_TREE)
25332 dwarf2out_abstract_function (decl);
25334 /* Otherwise we're emitting the primary DIE for this decl. */
25335 else if (debug_info_level > DINFO_LEVEL_TERSE)
25337 /* Before we describe the FUNCTION_DECL itself, make sure that we
25338 have its containing type. */
25339 if (!origin)
25340 origin = decl_class_context (decl);
25341 if (origin != NULL_TREE)
25342 gen_type_die (origin, context_die);
25344 /* And its return type. */
25345 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25347 /* And its virtual context. */
25348 if (DECL_VINDEX (decl) != NULL_TREE)
25349 gen_type_die (DECL_CONTEXT (decl), context_die);
25351 /* Make sure we have a member DIE for decl. */
25352 if (origin != NULL_TREE)
25353 gen_type_die_for_member (origin, decl, context_die);
25355 /* And its containing namespace. */
25356 context_die = declare_in_namespace (decl, context_die);
25359 /* Now output a DIE to represent the function itself. */
25360 if (decl)
25361 gen_subprogram_die (decl, context_die);
25362 break;
25364 case TYPE_DECL:
25365 /* If we are in terse mode, don't generate any DIEs to represent any
25366 actual typedefs. */
25367 if (debug_info_level <= DINFO_LEVEL_TERSE)
25368 break;
25370 /* In the special case of a TYPE_DECL node representing the declaration
25371 of some type tag, if the given TYPE_DECL is marked as having been
25372 instantiated from some other (original) TYPE_DECL node (e.g. one which
25373 was generated within the original definition of an inline function) we
25374 used to generate a special (abbreviated) DW_TAG_structure_type,
25375 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25376 should be actually referencing those DIEs, as variable DIEs with that
25377 type would be emitted already in the abstract origin, so it was always
25378 removed during unused type prunning. Don't add anything in this
25379 case. */
25380 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25381 break;
25383 if (is_redundant_typedef (decl))
25384 gen_type_die (TREE_TYPE (decl), context_die);
25385 else
25386 /* Output a DIE to represent the typedef itself. */
25387 gen_typedef_die (decl, context_die);
25388 break;
25390 case LABEL_DECL:
25391 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25392 gen_label_die (decl, context_die);
25393 break;
25395 case VAR_DECL:
25396 case RESULT_DECL:
25397 /* If we are in terse mode, don't generate any DIEs to represent any
25398 variable declarations or definitions. */
25399 if (debug_info_level <= DINFO_LEVEL_TERSE)
25400 break;
25402 /* Avoid generating stray type DIEs during late dwarf dumping.
25403 All types have been dumped early. */
25404 if (early_dwarf
25405 /* ??? But in LTRANS we cannot annotate early created variably
25406 modified type DIEs without copying them and adjusting all
25407 references to them. Dump them again as happens for inlining
25408 which copies both the decl and the types. */
25409 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25410 in VLA bound information for example. */
25411 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25412 current_function_decl)))
25414 /* Output any DIEs that are needed to specify the type of this data
25415 object. */
25416 if (decl_by_reference_p (decl_or_origin))
25417 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25418 else
25419 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25422 if (early_dwarf)
25424 /* And its containing type. */
25425 class_origin = decl_class_context (decl_or_origin);
25426 if (class_origin != NULL_TREE)
25427 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25429 /* And its containing namespace. */
25430 context_die = declare_in_namespace (decl_or_origin, context_die);
25433 /* Now output the DIE to represent the data object itself. This gets
25434 complicated because of the possibility that the VAR_DECL really
25435 represents an inlined instance of a formal parameter for an inline
25436 function. */
25437 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25438 if (ultimate_origin != NULL_TREE
25439 && TREE_CODE (ultimate_origin) == PARM_DECL)
25440 gen_formal_parameter_die (decl, origin,
25441 true /* Emit name attribute. */,
25442 context_die);
25443 else
25444 gen_variable_die (decl, origin, context_die);
25445 break;
25447 case FIELD_DECL:
25448 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25449 /* Ignore the nameless fields that are used to skip bits but handle C++
25450 anonymous unions and structs. */
25451 if (DECL_NAME (decl) != NULL_TREE
25452 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25453 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25455 gen_type_die (member_declared_type (decl), context_die);
25456 gen_field_die (decl, ctx, context_die);
25458 break;
25460 case PARM_DECL:
25461 /* Avoid generating stray type DIEs during late dwarf dumping.
25462 All types have been dumped early. */
25463 if (early_dwarf
25464 /* ??? But in LTRANS we cannot annotate early created variably
25465 modified type DIEs without copying them and adjusting all
25466 references to them. Dump them again as happens for inlining
25467 which copies both the decl and the types. */
25468 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25469 in VLA bound information for example. */
25470 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25471 current_function_decl)))
25473 if (DECL_BY_REFERENCE (decl_or_origin))
25474 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25475 else
25476 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25478 return gen_formal_parameter_die (decl, origin,
25479 true /* Emit name attribute. */,
25480 context_die);
25482 case NAMESPACE_DECL:
25483 if (dwarf_version >= 3 || !dwarf_strict)
25484 gen_namespace_die (decl, context_die);
25485 break;
25487 case IMPORTED_DECL:
25488 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25489 DECL_CONTEXT (decl), context_die);
25490 break;
25492 case NAMELIST_DECL:
25493 gen_namelist_decl (DECL_NAME (decl), context_die,
25494 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25495 break;
25497 default:
25498 /* Probably some frontend-internal decl. Assume we don't care. */
25499 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25500 break;
25503 return NULL;
25506 /* Output initial debug information for global DECL. Called at the
25507 end of the parsing process.
25509 This is the initial debug generation process. As such, the DIEs
25510 generated may be incomplete. A later debug generation pass
25511 (dwarf2out_late_global_decl) will augment the information generated
25512 in this pass (e.g., with complete location info). */
25514 static void
25515 dwarf2out_early_global_decl (tree decl)
25517 set_early_dwarf s;
25519 /* gen_decl_die() will set DECL_ABSTRACT because
25520 cgraph_function_possibly_inlined_p() returns true. This is in
25521 turn will cause DW_AT_inline attributes to be set.
25523 This happens because at early dwarf generation, there is no
25524 cgraph information, causing cgraph_function_possibly_inlined_p()
25525 to return true. Trick cgraph_function_possibly_inlined_p()
25526 while we generate dwarf early. */
25527 bool save = symtab->global_info_ready;
25528 symtab->global_info_ready = true;
25530 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25531 other DECLs and they can point to template types or other things
25532 that dwarf2out can't handle when done via dwarf2out_decl. */
25533 if (TREE_CODE (decl) != TYPE_DECL
25534 && TREE_CODE (decl) != PARM_DECL)
25536 if (TREE_CODE (decl) == FUNCTION_DECL)
25538 tree save_fndecl = current_function_decl;
25540 /* For nested functions, make sure we have DIEs for the parents first
25541 so that all nested DIEs are generated at the proper scope in the
25542 first shot. */
25543 tree context = decl_function_context (decl);
25544 if (context != NULL)
25546 dw_die_ref context_die = lookup_decl_die (context);
25547 current_function_decl = context;
25549 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25550 enough so that it lands in its own context. This avoids type
25551 pruning issues later on. */
25552 if (context_die == NULL || is_declaration_die (context_die))
25553 dwarf2out_decl (context);
25556 /* Emit an abstract origin of a function first. This happens
25557 with C++ constructor clones for example and makes
25558 dwarf2out_abstract_function happy which requires the early
25559 DIE of the abstract instance to be present. */
25560 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25561 dw_die_ref origin_die;
25562 if (origin != NULL
25563 /* Do not emit the DIE multiple times but make sure to
25564 process it fully here in case we just saw a declaration. */
25565 && ((origin_die = lookup_decl_die (origin)) == NULL
25566 || is_declaration_die (origin_die)))
25568 current_function_decl = origin;
25569 dwarf2out_decl (origin);
25572 /* Emit the DIE for decl but avoid doing that multiple times. */
25573 dw_die_ref old_die;
25574 if ((old_die = lookup_decl_die (decl)) == NULL
25575 || is_declaration_die (old_die))
25577 current_function_decl = decl;
25578 dwarf2out_decl (decl);
25581 current_function_decl = save_fndecl;
25583 else
25584 dwarf2out_decl (decl);
25586 symtab->global_info_ready = save;
25589 /* Output debug information for global decl DECL. Called from
25590 toplev.c after compilation proper has finished. */
25592 static void
25593 dwarf2out_late_global_decl (tree decl)
25595 /* Fill-in any location information we were unable to determine
25596 on the first pass. */
25597 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25599 dw_die_ref die = lookup_decl_die (decl);
25601 /* We may have to generate early debug late for LTO in case debug
25602 was not enabled at compile-time or the target doesn't support
25603 the LTO early debug scheme. */
25604 if (! die && in_lto_p)
25606 dwarf2out_decl (decl);
25607 die = lookup_decl_die (decl);
25610 if (die)
25612 /* We get called via the symtab code invoking late_global_decl
25613 for symbols that are optimized out. Do not add locations
25614 for those, except if they have a DECL_VALUE_EXPR, in which case
25615 they are relevant for debuggers. */
25616 varpool_node *node = varpool_node::get (decl);
25617 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25618 tree_add_const_value_attribute_for_decl (die, decl);
25619 else
25620 add_location_or_const_value_attribute (die, decl, false);
25625 /* Output debug information for type decl DECL. Called from toplev.c
25626 and from language front ends (to record built-in types). */
25627 static void
25628 dwarf2out_type_decl (tree decl, int local)
25630 if (!local)
25632 set_early_dwarf s;
25633 dwarf2out_decl (decl);
25637 /* Output debug information for imported module or decl DECL.
25638 NAME is non-NULL name in the lexical block if the decl has been renamed.
25639 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25640 that DECL belongs to.
25641 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25642 static void
25643 dwarf2out_imported_module_or_decl_1 (tree decl,
25644 tree name,
25645 tree lexical_block,
25646 dw_die_ref lexical_block_die)
25648 expanded_location xloc;
25649 dw_die_ref imported_die = NULL;
25650 dw_die_ref at_import_die;
25652 if (TREE_CODE (decl) == IMPORTED_DECL)
25654 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25655 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25656 gcc_assert (decl);
25658 else
25659 xloc = expand_location (input_location);
25661 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25663 at_import_die = force_type_die (TREE_TYPE (decl));
25664 /* For namespace N { typedef void T; } using N::T; base_type_die
25665 returns NULL, but DW_TAG_imported_declaration requires
25666 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25667 if (!at_import_die)
25669 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25670 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25671 at_import_die = lookup_type_die (TREE_TYPE (decl));
25672 gcc_assert (at_import_die);
25675 else
25677 at_import_die = lookup_decl_die (decl);
25678 if (!at_import_die)
25680 /* If we're trying to avoid duplicate debug info, we may not have
25681 emitted the member decl for this field. Emit it now. */
25682 if (TREE_CODE (decl) == FIELD_DECL)
25684 tree type = DECL_CONTEXT (decl);
25686 if (TYPE_CONTEXT (type)
25687 && TYPE_P (TYPE_CONTEXT (type))
25688 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25689 DINFO_USAGE_DIR_USE))
25690 return;
25691 gen_type_die_for_member (type, decl,
25692 get_context_die (TYPE_CONTEXT (type)));
25694 if (TREE_CODE (decl) == NAMELIST_DECL)
25695 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25696 get_context_die (DECL_CONTEXT (decl)),
25697 NULL_TREE);
25698 else
25699 at_import_die = force_decl_die (decl);
25703 if (TREE_CODE (decl) == NAMESPACE_DECL)
25705 if (dwarf_version >= 3 || !dwarf_strict)
25706 imported_die = new_die (DW_TAG_imported_module,
25707 lexical_block_die,
25708 lexical_block);
25709 else
25710 return;
25712 else
25713 imported_die = new_die (DW_TAG_imported_declaration,
25714 lexical_block_die,
25715 lexical_block);
25717 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25718 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25719 if (debug_column_info && xloc.column)
25720 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25721 if (name)
25722 add_AT_string (imported_die, DW_AT_name,
25723 IDENTIFIER_POINTER (name));
25724 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25727 /* Output debug information for imported module or decl DECL.
25728 NAME is non-NULL name in context if the decl has been renamed.
25729 CHILD is true if decl is one of the renamed decls as part of
25730 importing whole module.
25731 IMPLICIT is set if this hook is called for an implicit import
25732 such as inline namespace. */
25734 static void
25735 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25736 bool child, bool implicit)
25738 /* dw_die_ref at_import_die; */
25739 dw_die_ref scope_die;
25741 if (debug_info_level <= DINFO_LEVEL_TERSE)
25742 return;
25744 gcc_assert (decl);
25746 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25747 should be enough, for DWARF4 and older even if we emit as extension
25748 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25749 for the benefit of consumers unaware of DW_AT_export_symbols. */
25750 if (implicit
25751 && dwarf_version >= 5
25752 && lang_hooks.decls.decl_dwarf_attribute (decl,
25753 DW_AT_export_symbols) == 1)
25754 return;
25756 set_early_dwarf s;
25758 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25759 We need decl DIE for reference and scope die. First, get DIE for the decl
25760 itself. */
25762 /* Get the scope die for decl context. Use comp_unit_die for global module
25763 or decl. If die is not found for non globals, force new die. */
25764 if (context
25765 && TYPE_P (context)
25766 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25767 return;
25769 scope_die = get_context_die (context);
25771 if (child)
25773 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25774 there is nothing we can do, here. */
25775 if (dwarf_version < 3 && dwarf_strict)
25776 return;
25778 gcc_assert (scope_die->die_child);
25779 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25780 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25781 scope_die = scope_die->die_child;
25784 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25785 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25788 /* Output debug information for namelists. */
25790 static dw_die_ref
25791 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25793 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25794 tree value;
25795 unsigned i;
25797 if (debug_info_level <= DINFO_LEVEL_TERSE)
25798 return NULL;
25800 gcc_assert (scope_die != NULL);
25801 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25802 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25804 /* If there are no item_decls, we have a nondefining namelist, e.g.
25805 with USE association; hence, set DW_AT_declaration. */
25806 if (item_decls == NULL_TREE)
25808 add_AT_flag (nml_die, DW_AT_declaration, 1);
25809 return nml_die;
25812 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25814 nml_item_ref_die = lookup_decl_die (value);
25815 if (!nml_item_ref_die)
25816 nml_item_ref_die = force_decl_die (value);
25818 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25819 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25821 return nml_die;
25825 /* Write the debugging output for DECL and return the DIE. */
25827 static void
25828 dwarf2out_decl (tree decl)
25830 dw_die_ref context_die = comp_unit_die ();
25832 switch (TREE_CODE (decl))
25834 case ERROR_MARK:
25835 return;
25837 case FUNCTION_DECL:
25838 /* If we're a nested function, initially use a parent of NULL; if we're
25839 a plain function, this will be fixed up in decls_for_scope. If
25840 we're a method, it will be ignored, since we already have a DIE. */
25841 if (decl_function_context (decl)
25842 /* But if we're in terse mode, we don't care about scope. */
25843 && debug_info_level > DINFO_LEVEL_TERSE)
25844 context_die = NULL;
25845 break;
25847 case VAR_DECL:
25848 /* For local statics lookup proper context die. */
25849 if (local_function_static (decl))
25850 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25852 /* If we are in terse mode, don't generate any DIEs to represent any
25853 variable declarations or definitions. */
25854 if (debug_info_level <= DINFO_LEVEL_TERSE)
25855 return;
25856 break;
25858 case CONST_DECL:
25859 if (debug_info_level <= DINFO_LEVEL_TERSE)
25860 return;
25861 if (!is_fortran () && !is_ada ())
25862 return;
25863 if (TREE_STATIC (decl) && decl_function_context (decl))
25864 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25865 break;
25867 case NAMESPACE_DECL:
25868 case IMPORTED_DECL:
25869 if (debug_info_level <= DINFO_LEVEL_TERSE)
25870 return;
25871 if (lookup_decl_die (decl) != NULL)
25872 return;
25873 break;
25875 case TYPE_DECL:
25876 /* Don't emit stubs for types unless they are needed by other DIEs. */
25877 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25878 return;
25880 /* Don't bother trying to generate any DIEs to represent any of the
25881 normal built-in types for the language we are compiling. */
25882 if (DECL_IS_BUILTIN (decl))
25883 return;
25885 /* If we are in terse mode, don't generate any DIEs for types. */
25886 if (debug_info_level <= DINFO_LEVEL_TERSE)
25887 return;
25889 /* If we're a function-scope tag, initially use a parent of NULL;
25890 this will be fixed up in decls_for_scope. */
25891 if (decl_function_context (decl))
25892 context_die = NULL;
25894 break;
25896 case NAMELIST_DECL:
25897 break;
25899 default:
25900 return;
25903 gen_decl_die (decl, NULL, NULL, context_die);
25905 if (flag_checking)
25907 dw_die_ref die = lookup_decl_die (decl);
25908 if (die)
25909 check_die (die);
25913 /* Write the debugging output for DECL. */
25915 static void
25916 dwarf2out_function_decl (tree decl)
25918 dwarf2out_decl (decl);
25919 call_arg_locations = NULL;
25920 call_arg_loc_last = NULL;
25921 call_site_count = -1;
25922 tail_call_site_count = -1;
25923 decl_loc_table->empty ();
25924 cached_dw_loc_list_table->empty ();
25927 /* Output a marker (i.e. a label) for the beginning of the generated code for
25928 a lexical block. */
25930 static void
25931 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25932 unsigned int blocknum)
25934 switch_to_section (current_function_section ());
25935 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25938 /* Output a marker (i.e. a label) for the end of the generated code for a
25939 lexical block. */
25941 static void
25942 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25944 switch_to_section (current_function_section ());
25945 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25948 /* Returns nonzero if it is appropriate not to emit any debugging
25949 information for BLOCK, because it doesn't contain any instructions.
25951 Don't allow this for blocks with nested functions or local classes
25952 as we would end up with orphans, and in the presence of scheduling
25953 we may end up calling them anyway. */
25955 static bool
25956 dwarf2out_ignore_block (const_tree block)
25958 tree decl;
25959 unsigned int i;
25961 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25962 if (TREE_CODE (decl) == FUNCTION_DECL
25963 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25964 return 0;
25965 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25967 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25968 if (TREE_CODE (decl) == FUNCTION_DECL
25969 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25970 return 0;
25973 return 1;
25976 /* Hash table routines for file_hash. */
25978 bool
25979 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25981 return filename_cmp (p1->filename, p2) == 0;
25984 hashval_t
25985 dwarf_file_hasher::hash (dwarf_file_data *p)
25987 return htab_hash_string (p->filename);
25990 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25991 dwarf2out.c) and return its "index". The index of each (known) filename is
25992 just a unique number which is associated with only that one filename. We
25993 need such numbers for the sake of generating labels (in the .debug_sfnames
25994 section) and references to those files numbers (in the .debug_srcinfo
25995 and .debug_macinfo sections). If the filename given as an argument is not
25996 found in our current list, add it to the list and assign it the next
25997 available unique index number. */
25999 static struct dwarf_file_data *
26000 lookup_filename (const char *file_name)
26002 struct dwarf_file_data * created;
26004 if (!file_name)
26005 return NULL;
26007 dwarf_file_data **slot
26008 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26009 INSERT);
26010 if (*slot)
26011 return *slot;
26013 created = ggc_alloc<dwarf_file_data> ();
26014 created->filename = file_name;
26015 created->emitted_number = 0;
26016 *slot = created;
26017 return created;
26020 /* If the assembler will construct the file table, then translate the compiler
26021 internal file table number into the assembler file table number, and emit
26022 a .file directive if we haven't already emitted one yet. The file table
26023 numbers are different because we prune debug info for unused variables and
26024 types, which may include filenames. */
26026 static int
26027 maybe_emit_file (struct dwarf_file_data * fd)
26029 if (! fd->emitted_number)
26031 if (last_emitted_file)
26032 fd->emitted_number = last_emitted_file->emitted_number + 1;
26033 else
26034 fd->emitted_number = 1;
26035 last_emitted_file = fd;
26037 if (DWARF2_ASM_LINE_DEBUG_INFO)
26039 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26040 output_quoted_string (asm_out_file,
26041 remap_debug_filename (fd->filename));
26042 fputc ('\n', asm_out_file);
26046 return fd->emitted_number;
26049 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26050 That generation should happen after function debug info has been
26051 generated. The value of the attribute is the constant value of ARG. */
26053 static void
26054 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26056 die_arg_entry entry;
26058 if (!die || !arg)
26059 return;
26061 gcc_assert (early_dwarf);
26063 if (!tmpl_value_parm_die_table)
26064 vec_alloc (tmpl_value_parm_die_table, 32);
26066 entry.die = die;
26067 entry.arg = arg;
26068 vec_safe_push (tmpl_value_parm_die_table, entry);
26071 /* Return TRUE if T is an instance of generic type, FALSE
26072 otherwise. */
26074 static bool
26075 generic_type_p (tree t)
26077 if (t == NULL_TREE || !TYPE_P (t))
26078 return false;
26079 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26082 /* Schedule the generation of the generic parameter dies for the
26083 instance of generic type T. The proper generation itself is later
26084 done by gen_scheduled_generic_parms_dies. */
26086 static void
26087 schedule_generic_params_dies_gen (tree t)
26089 if (!generic_type_p (t))
26090 return;
26092 gcc_assert (early_dwarf);
26094 if (!generic_type_instances)
26095 vec_alloc (generic_type_instances, 256);
26097 vec_safe_push (generic_type_instances, t);
26100 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26101 by append_entry_to_tmpl_value_parm_die_table. This function must
26102 be called after function DIEs have been generated. */
26104 static void
26105 gen_remaining_tmpl_value_param_die_attribute (void)
26107 if (tmpl_value_parm_die_table)
26109 unsigned i, j;
26110 die_arg_entry *e;
26112 /* We do this in two phases - first get the cases we can
26113 handle during early-finish, preserving those we cannot
26114 (containing symbolic constants where we don't yet know
26115 whether we are going to output the referenced symbols).
26116 For those we try again at late-finish. */
26117 j = 0;
26118 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26120 if (!e->die->removed
26121 && !tree_add_const_value_attribute (e->die, e->arg))
26123 dw_loc_descr_ref loc = NULL;
26124 if (! early_dwarf
26125 && (dwarf_version >= 5 || !dwarf_strict))
26126 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26127 if (loc)
26128 add_AT_loc (e->die, DW_AT_location, loc);
26129 else
26130 (*tmpl_value_parm_die_table)[j++] = *e;
26133 tmpl_value_parm_die_table->truncate (j);
26137 /* Generate generic parameters DIEs for instances of generic types
26138 that have been previously scheduled by
26139 schedule_generic_params_dies_gen. This function must be called
26140 after all the types of the CU have been laid out. */
26142 static void
26143 gen_scheduled_generic_parms_dies (void)
26145 unsigned i;
26146 tree t;
26148 if (!generic_type_instances)
26149 return;
26151 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26152 if (COMPLETE_TYPE_P (t))
26153 gen_generic_params_dies (t);
26155 generic_type_instances = NULL;
26159 /* Replace DW_AT_name for the decl with name. */
26161 static void
26162 dwarf2out_set_name (tree decl, tree name)
26164 dw_die_ref die;
26165 dw_attr_node *attr;
26166 const char *dname;
26168 die = TYPE_SYMTAB_DIE (decl);
26169 if (!die)
26170 return;
26172 dname = dwarf2_name (name, 0);
26173 if (!dname)
26174 return;
26176 attr = get_AT (die, DW_AT_name);
26177 if (attr)
26179 struct indirect_string_node *node;
26181 node = find_AT_string (dname);
26182 /* replace the string. */
26183 attr->dw_attr_val.v.val_str = node;
26186 else
26187 add_name_attribute (die, dname);
26190 /* True if before or during processing of the first function being emitted. */
26191 static bool in_first_function_p = true;
26192 /* True if loc_note during dwarf2out_var_location call might still be
26193 before first real instruction at address equal to .Ltext0. */
26194 static bool maybe_at_text_label_p = true;
26195 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26196 static unsigned int first_loclabel_num_not_at_text_label;
26198 /* Called by the final INSN scan whenever we see a var location. We
26199 use it to drop labels in the right places, and throw the location in
26200 our lookup table. */
26202 static void
26203 dwarf2out_var_location (rtx_insn *loc_note)
26205 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26206 struct var_loc_node *newloc;
26207 rtx_insn *next_real, *next_note;
26208 rtx_insn *call_insn = NULL;
26209 static const char *last_label;
26210 static const char *last_postcall_label;
26211 static bool last_in_cold_section_p;
26212 static rtx_insn *expected_next_loc_note;
26213 tree decl;
26214 bool var_loc_p;
26216 if (!NOTE_P (loc_note))
26218 if (CALL_P (loc_note))
26220 call_site_count++;
26221 if (SIBLING_CALL_P (loc_note))
26222 tail_call_site_count++;
26223 if (optimize == 0 && !flag_var_tracking)
26225 /* When the var-tracking pass is not running, there is no note
26226 for indirect calls whose target is compile-time known. In this
26227 case, process such calls specifically so that we generate call
26228 sites for them anyway. */
26229 rtx x = PATTERN (loc_note);
26230 if (GET_CODE (x) == PARALLEL)
26231 x = XVECEXP (x, 0, 0);
26232 if (GET_CODE (x) == SET)
26233 x = SET_SRC (x);
26234 if (GET_CODE (x) == CALL)
26235 x = XEXP (x, 0);
26236 if (!MEM_P (x)
26237 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26238 || !SYMBOL_REF_DECL (XEXP (x, 0))
26239 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26240 != FUNCTION_DECL))
26242 call_insn = loc_note;
26243 loc_note = NULL;
26244 var_loc_p = false;
26246 next_real = next_real_insn (call_insn);
26247 next_note = NULL;
26248 cached_next_real_insn = NULL;
26249 goto create_label;
26253 return;
26256 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26257 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26258 return;
26260 /* Optimize processing a large consecutive sequence of location
26261 notes so we don't spend too much time in next_real_insn. If the
26262 next insn is another location note, remember the next_real_insn
26263 calculation for next time. */
26264 next_real = cached_next_real_insn;
26265 if (next_real)
26267 if (expected_next_loc_note != loc_note)
26268 next_real = NULL;
26271 next_note = NEXT_INSN (loc_note);
26272 if (! next_note
26273 || next_note->deleted ()
26274 || ! NOTE_P (next_note)
26275 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26276 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26277 next_note = NULL;
26279 if (! next_real)
26280 next_real = next_real_insn (loc_note);
26282 if (next_note)
26284 expected_next_loc_note = next_note;
26285 cached_next_real_insn = next_real;
26287 else
26288 cached_next_real_insn = NULL;
26290 /* If there are no instructions which would be affected by this note,
26291 don't do anything. */
26292 if (var_loc_p
26293 && next_real == NULL_RTX
26294 && !NOTE_DURING_CALL_P (loc_note))
26295 return;
26297 create_label:
26299 if (next_real == NULL_RTX)
26300 next_real = get_last_insn ();
26302 /* If there were any real insns between note we processed last time
26303 and this note (or if it is the first note), clear
26304 last_{,postcall_}label so that they are not reused this time. */
26305 if (last_var_location_insn == NULL_RTX
26306 || last_var_location_insn != next_real
26307 || last_in_cold_section_p != in_cold_section_p)
26309 last_label = NULL;
26310 last_postcall_label = NULL;
26313 if (var_loc_p)
26315 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26316 newloc = add_var_loc_to_decl (decl, loc_note,
26317 NOTE_DURING_CALL_P (loc_note)
26318 ? last_postcall_label : last_label);
26319 if (newloc == NULL)
26320 return;
26322 else
26324 decl = NULL_TREE;
26325 newloc = NULL;
26328 /* If there were no real insns between note we processed last time
26329 and this note, use the label we emitted last time. Otherwise
26330 create a new label and emit it. */
26331 if (last_label == NULL)
26333 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26334 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26335 loclabel_num++;
26336 last_label = ggc_strdup (loclabel);
26337 /* See if loclabel might be equal to .Ltext0. If yes,
26338 bump first_loclabel_num_not_at_text_label. */
26339 if (!have_multiple_function_sections
26340 && in_first_function_p
26341 && maybe_at_text_label_p)
26343 static rtx_insn *last_start;
26344 rtx_insn *insn;
26345 for (insn = loc_note; insn; insn = previous_insn (insn))
26346 if (insn == last_start)
26347 break;
26348 else if (!NONDEBUG_INSN_P (insn))
26349 continue;
26350 else
26352 rtx body = PATTERN (insn);
26353 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26354 continue;
26355 /* Inline asm could occupy zero bytes. */
26356 else if (GET_CODE (body) == ASM_INPUT
26357 || asm_noperands (body) >= 0)
26358 continue;
26359 #ifdef HAVE_attr_length
26360 else if (get_attr_min_length (insn) == 0)
26361 continue;
26362 #endif
26363 else
26365 /* Assume insn has non-zero length. */
26366 maybe_at_text_label_p = false;
26367 break;
26370 if (maybe_at_text_label_p)
26372 last_start = loc_note;
26373 first_loclabel_num_not_at_text_label = loclabel_num;
26378 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26379 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26381 if (!var_loc_p)
26383 struct call_arg_loc_node *ca_loc
26384 = ggc_cleared_alloc<call_arg_loc_node> ();
26385 rtx_insn *prev
26386 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26388 ca_loc->call_arg_loc_note = loc_note;
26389 ca_loc->next = NULL;
26390 ca_loc->label = last_label;
26391 gcc_assert (prev
26392 && (CALL_P (prev)
26393 || (NONJUMP_INSN_P (prev)
26394 && GET_CODE (PATTERN (prev)) == SEQUENCE
26395 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26396 if (!CALL_P (prev))
26397 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26398 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26400 /* Look for a SYMBOL_REF in the "prev" instruction. */
26401 rtx x = get_call_rtx_from (PATTERN (prev));
26402 if (x)
26404 /* Try to get the call symbol, if any. */
26405 if (MEM_P (XEXP (x, 0)))
26406 x = XEXP (x, 0);
26407 /* First, look for a memory access to a symbol_ref. */
26408 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26409 && SYMBOL_REF_DECL (XEXP (x, 0))
26410 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26411 ca_loc->symbol_ref = XEXP (x, 0);
26412 /* Otherwise, look at a compile-time known user-level function
26413 declaration. */
26414 else if (MEM_P (x)
26415 && MEM_EXPR (x)
26416 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26417 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26420 ca_loc->block = insn_scope (prev);
26421 if (call_arg_locations)
26422 call_arg_loc_last->next = ca_loc;
26423 else
26424 call_arg_locations = ca_loc;
26425 call_arg_loc_last = ca_loc;
26427 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26428 newloc->label = last_label;
26429 else
26431 if (!last_postcall_label)
26433 sprintf (loclabel, "%s-1", last_label);
26434 last_postcall_label = ggc_strdup (loclabel);
26436 newloc->label = last_postcall_label;
26439 last_var_location_insn = next_real;
26440 last_in_cold_section_p = in_cold_section_p;
26443 /* Called from finalize_size_functions for size functions so that their body
26444 can be encoded in the debug info to describe the layout of variable-length
26445 structures. */
26447 static void
26448 dwarf2out_size_function (tree decl)
26450 function_to_dwarf_procedure (decl);
26453 /* Note in one location list that text section has changed. */
26456 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26458 var_loc_list *list = *slot;
26459 if (list->first)
26460 list->last_before_switch
26461 = list->last->next ? list->last->next : list->last;
26462 return 1;
26465 /* Note in all location lists that text section has changed. */
26467 static void
26468 var_location_switch_text_section (void)
26470 if (decl_loc_table == NULL)
26471 return;
26473 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26476 /* Create a new line number table. */
26478 static dw_line_info_table *
26479 new_line_info_table (void)
26481 dw_line_info_table *table;
26483 table = ggc_cleared_alloc<dw_line_info_table> ();
26484 table->file_num = 1;
26485 table->line_num = 1;
26486 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26488 return table;
26491 /* Lookup the "current" table into which we emit line info, so
26492 that we don't have to do it for every source line. */
26494 static void
26495 set_cur_line_info_table (section *sec)
26497 dw_line_info_table *table;
26499 if (sec == text_section)
26500 table = text_section_line_info;
26501 else if (sec == cold_text_section)
26503 table = cold_text_section_line_info;
26504 if (!table)
26506 cold_text_section_line_info = table = new_line_info_table ();
26507 table->end_label = cold_end_label;
26510 else
26512 const char *end_label;
26514 if (crtl->has_bb_partition)
26516 if (in_cold_section_p)
26517 end_label = crtl->subsections.cold_section_end_label;
26518 else
26519 end_label = crtl->subsections.hot_section_end_label;
26521 else
26523 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26524 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26525 current_function_funcdef_no);
26526 end_label = ggc_strdup (label);
26529 table = new_line_info_table ();
26530 table->end_label = end_label;
26532 vec_safe_push (separate_line_info, table);
26535 if (DWARF2_ASM_LINE_DEBUG_INFO)
26536 table->is_stmt = (cur_line_info_table
26537 ? cur_line_info_table->is_stmt
26538 : DWARF_LINE_DEFAULT_IS_STMT_START);
26539 cur_line_info_table = table;
26543 /* We need to reset the locations at the beginning of each
26544 function. We can't do this in the end_function hook, because the
26545 declarations that use the locations won't have been output when
26546 that hook is called. Also compute have_multiple_function_sections here. */
26548 static void
26549 dwarf2out_begin_function (tree fun)
26551 section *sec = function_section (fun);
26553 if (sec != text_section)
26554 have_multiple_function_sections = true;
26556 if (crtl->has_bb_partition && !cold_text_section)
26558 gcc_assert (current_function_decl == fun);
26559 cold_text_section = unlikely_text_section ();
26560 switch_to_section (cold_text_section);
26561 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26562 switch_to_section (sec);
26565 dwarf2out_note_section_used ();
26566 call_site_count = 0;
26567 tail_call_site_count = 0;
26569 set_cur_line_info_table (sec);
26572 /* Helper function of dwarf2out_end_function, called only after emitting
26573 the very first function into assembly. Check if some .debug_loc range
26574 might end with a .LVL* label that could be equal to .Ltext0.
26575 In that case we must force using absolute addresses in .debug_loc ranges,
26576 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26577 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26578 list terminator.
26579 Set have_multiple_function_sections to true in that case and
26580 terminate htab traversal. */
26583 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26585 var_loc_list *entry = *slot;
26586 struct var_loc_node *node;
26588 node = entry->first;
26589 if (node && node->next && node->next->label)
26591 unsigned int i;
26592 const char *label = node->next->label;
26593 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26595 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26597 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26598 if (strcmp (label, loclabel) == 0)
26600 have_multiple_function_sections = true;
26601 return 0;
26605 return 1;
26608 /* Hook called after emitting a function into assembly.
26609 This does something only for the very first function emitted. */
26611 static void
26612 dwarf2out_end_function (unsigned int)
26614 if (in_first_function_p
26615 && !have_multiple_function_sections
26616 && first_loclabel_num_not_at_text_label
26617 && decl_loc_table)
26618 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26619 in_first_function_p = false;
26620 maybe_at_text_label_p = false;
26623 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26624 front-ends register a translation unit even before dwarf2out_init is
26625 called. */
26626 static tree main_translation_unit = NULL_TREE;
26628 /* Hook called by front-ends after they built their main translation unit.
26629 Associate comp_unit_die to UNIT. */
26631 static void
26632 dwarf2out_register_main_translation_unit (tree unit)
26634 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26635 && main_translation_unit == NULL_TREE);
26636 main_translation_unit = unit;
26637 /* If dwarf2out_init has not been called yet, it will perform the association
26638 itself looking at main_translation_unit. */
26639 if (decl_die_table != NULL)
26640 equate_decl_number_to_die (unit, comp_unit_die ());
26643 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26645 static void
26646 push_dw_line_info_entry (dw_line_info_table *table,
26647 enum dw_line_info_opcode opcode, unsigned int val)
26649 dw_line_info_entry e;
26650 e.opcode = opcode;
26651 e.val = val;
26652 vec_safe_push (table->entries, e);
26655 /* Output a label to mark the beginning of a source code line entry
26656 and record information relating to this source line, in
26657 'line_info_table' for later output of the .debug_line section. */
26658 /* ??? The discriminator parameter ought to be unsigned. */
26660 static void
26661 dwarf2out_source_line (unsigned int line, unsigned int column,
26662 const char *filename,
26663 int discriminator, bool is_stmt)
26665 unsigned int file_num;
26666 dw_line_info_table *table;
26668 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26669 return;
26671 /* The discriminator column was added in dwarf4. Simplify the below
26672 by simply removing it if we're not supposed to output it. */
26673 if (dwarf_version < 4 && dwarf_strict)
26674 discriminator = 0;
26676 if (!debug_column_info)
26677 column = 0;
26679 table = cur_line_info_table;
26680 file_num = maybe_emit_file (lookup_filename (filename));
26682 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26683 the debugger has used the second (possibly duplicate) line number
26684 at the beginning of the function to mark the end of the prologue.
26685 We could eliminate any other duplicates within the function. For
26686 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26687 that second line number entry. */
26688 /* Recall that this end-of-prologue indication is *not* the same thing
26689 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26690 to which the hook corresponds, follows the last insn that was
26691 emitted by gen_prologue. What we need is to precede the first insn
26692 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26693 insn that corresponds to something the user wrote. These may be
26694 very different locations once scheduling is enabled. */
26696 if (0 && file_num == table->file_num
26697 && line == table->line_num
26698 && column == table->column_num
26699 && discriminator == table->discrim_num
26700 && is_stmt == table->is_stmt)
26701 return;
26703 switch_to_section (current_function_section ());
26705 /* If requested, emit something human-readable. */
26706 if (flag_debug_asm)
26708 if (debug_column_info)
26709 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26710 filename, line, column);
26711 else
26712 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26713 filename, line);
26716 if (DWARF2_ASM_LINE_DEBUG_INFO)
26718 /* Emit the .loc directive understood by GNU as. */
26719 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26720 file_num, line, is_stmt, discriminator */
26721 fputs ("\t.loc ", asm_out_file);
26722 fprint_ul (asm_out_file, file_num);
26723 putc (' ', asm_out_file);
26724 fprint_ul (asm_out_file, line);
26725 putc (' ', asm_out_file);
26726 fprint_ul (asm_out_file, column);
26728 if (is_stmt != table->is_stmt)
26730 fputs (" is_stmt ", asm_out_file);
26731 putc (is_stmt ? '1' : '0', asm_out_file);
26733 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26735 gcc_assert (discriminator > 0);
26736 fputs (" discriminator ", asm_out_file);
26737 fprint_ul (asm_out_file, (unsigned long) discriminator);
26739 putc ('\n', asm_out_file);
26741 else
26743 unsigned int label_num = ++line_info_label_num;
26745 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26747 push_dw_line_info_entry (table, LI_set_address, label_num);
26748 if (file_num != table->file_num)
26749 push_dw_line_info_entry (table, LI_set_file, file_num);
26750 if (discriminator != table->discrim_num)
26751 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26752 if (is_stmt != table->is_stmt)
26753 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26754 push_dw_line_info_entry (table, LI_set_line, line);
26755 if (debug_column_info)
26756 push_dw_line_info_entry (table, LI_set_column, column);
26759 table->file_num = file_num;
26760 table->line_num = line;
26761 table->column_num = column;
26762 table->discrim_num = discriminator;
26763 table->is_stmt = is_stmt;
26764 table->in_use = true;
26767 /* Record the beginning of a new source file. */
26769 static void
26770 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26772 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26774 macinfo_entry e;
26775 e.code = DW_MACINFO_start_file;
26776 e.lineno = lineno;
26777 e.info = ggc_strdup (filename);
26778 vec_safe_push (macinfo_table, e);
26782 /* Record the end of a source file. */
26784 static void
26785 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26787 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26789 macinfo_entry e;
26790 e.code = DW_MACINFO_end_file;
26791 e.lineno = lineno;
26792 e.info = NULL;
26793 vec_safe_push (macinfo_table, e);
26797 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26798 the tail part of the directive line, i.e. the part which is past the
26799 initial whitespace, #, whitespace, directive-name, whitespace part. */
26801 static void
26802 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26803 const char *buffer ATTRIBUTE_UNUSED)
26805 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26807 macinfo_entry e;
26808 /* Insert a dummy first entry to be able to optimize the whole
26809 predefined macro block using DW_MACRO_import. */
26810 if (macinfo_table->is_empty () && lineno <= 1)
26812 e.code = 0;
26813 e.lineno = 0;
26814 e.info = NULL;
26815 vec_safe_push (macinfo_table, e);
26817 e.code = DW_MACINFO_define;
26818 e.lineno = lineno;
26819 e.info = ggc_strdup (buffer);
26820 vec_safe_push (macinfo_table, e);
26824 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26825 the tail part of the directive line, i.e. the part which is past the
26826 initial whitespace, #, whitespace, directive-name, whitespace part. */
26828 static void
26829 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26830 const char *buffer ATTRIBUTE_UNUSED)
26832 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26834 macinfo_entry e;
26835 /* Insert a dummy first entry to be able to optimize the whole
26836 predefined macro block using DW_MACRO_import. */
26837 if (macinfo_table->is_empty () && lineno <= 1)
26839 e.code = 0;
26840 e.lineno = 0;
26841 e.info = NULL;
26842 vec_safe_push (macinfo_table, e);
26844 e.code = DW_MACINFO_undef;
26845 e.lineno = lineno;
26846 e.info = ggc_strdup (buffer);
26847 vec_safe_push (macinfo_table, e);
26851 /* Helpers to manipulate hash table of CUs. */
26853 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26855 static inline hashval_t hash (const macinfo_entry *);
26856 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26859 inline hashval_t
26860 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26862 return htab_hash_string (entry->info);
26865 inline bool
26866 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26867 const macinfo_entry *entry2)
26869 return !strcmp (entry1->info, entry2->info);
26872 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26874 /* Output a single .debug_macinfo entry. */
26876 static void
26877 output_macinfo_op (macinfo_entry *ref)
26879 int file_num;
26880 size_t len;
26881 struct indirect_string_node *node;
26882 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26883 struct dwarf_file_data *fd;
26885 switch (ref->code)
26887 case DW_MACINFO_start_file:
26888 fd = lookup_filename (ref->info);
26889 file_num = maybe_emit_file (fd);
26890 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26891 dw2_asm_output_data_uleb128 (ref->lineno,
26892 "Included from line number %lu",
26893 (unsigned long) ref->lineno);
26894 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26895 break;
26896 case DW_MACINFO_end_file:
26897 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26898 break;
26899 case DW_MACINFO_define:
26900 case DW_MACINFO_undef:
26901 len = strlen (ref->info) + 1;
26902 if (!dwarf_strict
26903 && len > DWARF_OFFSET_SIZE
26904 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26905 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26907 ref->code = ref->code == DW_MACINFO_define
26908 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26909 output_macinfo_op (ref);
26910 return;
26912 dw2_asm_output_data (1, ref->code,
26913 ref->code == DW_MACINFO_define
26914 ? "Define macro" : "Undefine macro");
26915 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26916 (unsigned long) ref->lineno);
26917 dw2_asm_output_nstring (ref->info, -1, "The macro");
26918 break;
26919 case DW_MACRO_define_strp:
26920 case DW_MACRO_undef_strp:
26921 node = find_AT_string (ref->info);
26922 gcc_assert (node
26923 && (node->form == DW_FORM_strp
26924 || node->form == DW_FORM_GNU_str_index));
26925 dw2_asm_output_data (1, ref->code,
26926 ref->code == DW_MACRO_define_strp
26927 ? "Define macro strp"
26928 : "Undefine macro strp");
26929 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26930 (unsigned long) ref->lineno);
26931 if (node->form == DW_FORM_strp)
26932 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26933 debug_str_section, "The macro: \"%s\"",
26934 ref->info);
26935 else
26936 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26937 ref->info);
26938 break;
26939 case DW_MACRO_import:
26940 dw2_asm_output_data (1, ref->code, "Import");
26941 ASM_GENERATE_INTERNAL_LABEL (label,
26942 DEBUG_MACRO_SECTION_LABEL,
26943 ref->lineno + macinfo_label_base);
26944 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26945 break;
26946 default:
26947 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26948 ASM_COMMENT_START, (unsigned long) ref->code);
26949 break;
26953 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26954 other compilation unit .debug_macinfo sections. IDX is the first
26955 index of a define/undef, return the number of ops that should be
26956 emitted in a comdat .debug_macinfo section and emit
26957 a DW_MACRO_import entry referencing it.
26958 If the define/undef entry should be emitted normally, return 0. */
26960 static unsigned
26961 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26962 macinfo_hash_type **macinfo_htab)
26964 macinfo_entry *first, *second, *cur, *inc;
26965 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26966 unsigned char checksum[16];
26967 struct md5_ctx ctx;
26968 char *grp_name, *tail;
26969 const char *base;
26970 unsigned int i, count, encoded_filename_len, linebuf_len;
26971 macinfo_entry **slot;
26973 first = &(*macinfo_table)[idx];
26974 second = &(*macinfo_table)[idx + 1];
26976 /* Optimize only if there are at least two consecutive define/undef ops,
26977 and either all of them are before first DW_MACINFO_start_file
26978 with lineno {0,1} (i.e. predefined macro block), or all of them are
26979 in some included header file. */
26980 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26981 return 0;
26982 if (vec_safe_is_empty (files))
26984 if (first->lineno > 1 || second->lineno > 1)
26985 return 0;
26987 else if (first->lineno == 0)
26988 return 0;
26990 /* Find the last define/undef entry that can be grouped together
26991 with first and at the same time compute md5 checksum of their
26992 codes, linenumbers and strings. */
26993 md5_init_ctx (&ctx);
26994 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26995 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26996 break;
26997 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26998 break;
26999 else
27001 unsigned char code = cur->code;
27002 md5_process_bytes (&code, 1, &ctx);
27003 checksum_uleb128 (cur->lineno, &ctx);
27004 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27006 md5_finish_ctx (&ctx, checksum);
27007 count = i - idx;
27009 /* From the containing include filename (if any) pick up just
27010 usable characters from its basename. */
27011 if (vec_safe_is_empty (files))
27012 base = "";
27013 else
27014 base = lbasename (files->last ().info);
27015 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27016 if (ISIDNUM (base[i]) || base[i] == '.')
27017 encoded_filename_len++;
27018 /* Count . at the end. */
27019 if (encoded_filename_len)
27020 encoded_filename_len++;
27022 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27023 linebuf_len = strlen (linebuf);
27025 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27026 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27027 + 16 * 2 + 1);
27028 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27029 tail = grp_name + 4;
27030 if (encoded_filename_len)
27032 for (i = 0; base[i]; i++)
27033 if (ISIDNUM (base[i]) || base[i] == '.')
27034 *tail++ = base[i];
27035 *tail++ = '.';
27037 memcpy (tail, linebuf, linebuf_len);
27038 tail += linebuf_len;
27039 *tail++ = '.';
27040 for (i = 0; i < 16; i++)
27041 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27043 /* Construct a macinfo_entry for DW_MACRO_import
27044 in the empty vector entry before the first define/undef. */
27045 inc = &(*macinfo_table)[idx - 1];
27046 inc->code = DW_MACRO_import;
27047 inc->lineno = 0;
27048 inc->info = ggc_strdup (grp_name);
27049 if (!*macinfo_htab)
27050 *macinfo_htab = new macinfo_hash_type (10);
27051 /* Avoid emitting duplicates. */
27052 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27053 if (*slot != NULL)
27055 inc->code = 0;
27056 inc->info = NULL;
27057 /* If such an entry has been used before, just emit
27058 a DW_MACRO_import op. */
27059 inc = *slot;
27060 output_macinfo_op (inc);
27061 /* And clear all macinfo_entry in the range to avoid emitting them
27062 in the second pass. */
27063 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27065 cur->code = 0;
27066 cur->info = NULL;
27069 else
27071 *slot = inc;
27072 inc->lineno = (*macinfo_htab)->elements ();
27073 output_macinfo_op (inc);
27075 return count;
27078 /* Save any strings needed by the macinfo table in the debug str
27079 table. All strings must be collected into the table by the time
27080 index_string is called. */
27082 static void
27083 save_macinfo_strings (void)
27085 unsigned len;
27086 unsigned i;
27087 macinfo_entry *ref;
27089 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27091 switch (ref->code)
27093 /* Match the logic in output_macinfo_op to decide on
27094 indirect strings. */
27095 case DW_MACINFO_define:
27096 case DW_MACINFO_undef:
27097 len = strlen (ref->info) + 1;
27098 if (!dwarf_strict
27099 && len > DWARF_OFFSET_SIZE
27100 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27101 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27102 set_indirect_string (find_AT_string (ref->info));
27103 break;
27104 case DW_MACRO_define_strp:
27105 case DW_MACRO_undef_strp:
27106 set_indirect_string (find_AT_string (ref->info));
27107 break;
27108 default:
27109 break;
27114 /* Output macinfo section(s). */
27116 static void
27117 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27119 unsigned i;
27120 unsigned long length = vec_safe_length (macinfo_table);
27121 macinfo_entry *ref;
27122 vec<macinfo_entry, va_gc> *files = NULL;
27123 macinfo_hash_type *macinfo_htab = NULL;
27124 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27126 if (! length)
27127 return;
27129 /* output_macinfo* uses these interchangeably. */
27130 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27131 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27132 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27133 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27135 /* AIX Assembler inserts the length, so adjust the reference to match the
27136 offset expected by debuggers. */
27137 strcpy (dl_section_ref, debug_line_label);
27138 if (XCOFF_DEBUGGING_INFO)
27139 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27141 /* For .debug_macro emit the section header. */
27142 if (!dwarf_strict || dwarf_version >= 5)
27144 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27145 "DWARF macro version number");
27146 if (DWARF_OFFSET_SIZE == 8)
27147 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27148 else
27149 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27150 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27151 debug_line_section, NULL);
27154 /* In the first loop, it emits the primary .debug_macinfo section
27155 and after each emitted op the macinfo_entry is cleared.
27156 If a longer range of define/undef ops can be optimized using
27157 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27158 the vector before the first define/undef in the range and the
27159 whole range of define/undef ops is not emitted and kept. */
27160 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27162 switch (ref->code)
27164 case DW_MACINFO_start_file:
27165 vec_safe_push (files, *ref);
27166 break;
27167 case DW_MACINFO_end_file:
27168 if (!vec_safe_is_empty (files))
27169 files->pop ();
27170 break;
27171 case DW_MACINFO_define:
27172 case DW_MACINFO_undef:
27173 if ((!dwarf_strict || dwarf_version >= 5)
27174 && HAVE_COMDAT_GROUP
27175 && vec_safe_length (files) != 1
27176 && i > 0
27177 && i + 1 < length
27178 && (*macinfo_table)[i - 1].code == 0)
27180 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27181 if (count)
27183 i += count - 1;
27184 continue;
27187 break;
27188 case 0:
27189 /* A dummy entry may be inserted at the beginning to be able
27190 to optimize the whole block of predefined macros. */
27191 if (i == 0)
27192 continue;
27193 default:
27194 break;
27196 output_macinfo_op (ref);
27197 ref->info = NULL;
27198 ref->code = 0;
27201 if (!macinfo_htab)
27202 return;
27204 /* Save the number of transparent includes so we can adjust the
27205 label number for the fat LTO object DWARF. */
27206 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27208 delete macinfo_htab;
27209 macinfo_htab = NULL;
27211 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27212 terminate the current chain and switch to a new comdat .debug_macinfo
27213 section and emit the define/undef entries within it. */
27214 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27215 switch (ref->code)
27217 case 0:
27218 continue;
27219 case DW_MACRO_import:
27221 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27222 tree comdat_key = get_identifier (ref->info);
27223 /* Terminate the previous .debug_macinfo section. */
27224 dw2_asm_output_data (1, 0, "End compilation unit");
27225 targetm.asm_out.named_section (debug_macinfo_section_name,
27226 SECTION_DEBUG
27227 | SECTION_LINKONCE
27228 | (early_lto_debug
27229 ? SECTION_EXCLUDE : 0),
27230 comdat_key);
27231 ASM_GENERATE_INTERNAL_LABEL (label,
27232 DEBUG_MACRO_SECTION_LABEL,
27233 ref->lineno + macinfo_label_base);
27234 ASM_OUTPUT_LABEL (asm_out_file, label);
27235 ref->code = 0;
27236 ref->info = NULL;
27237 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27238 "DWARF macro version number");
27239 if (DWARF_OFFSET_SIZE == 8)
27240 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27241 else
27242 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27244 break;
27245 case DW_MACINFO_define:
27246 case DW_MACINFO_undef:
27247 output_macinfo_op (ref);
27248 ref->code = 0;
27249 ref->info = NULL;
27250 break;
27251 default:
27252 gcc_unreachable ();
27255 macinfo_label_base += macinfo_label_base_adj;
27258 /* Initialize the various sections and labels for dwarf output and prefix
27259 them with PREFIX if non-NULL. */
27261 static void
27262 init_sections_and_labels (bool early_lto_debug)
27264 /* As we may get called multiple times have a generation count for
27265 labels. */
27266 static unsigned generation = 0;
27268 if (early_lto_debug)
27270 if (!dwarf_split_debug_info)
27272 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27273 SECTION_DEBUG | SECTION_EXCLUDE,
27274 NULL);
27275 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27276 SECTION_DEBUG | SECTION_EXCLUDE,
27277 NULL);
27278 debug_macinfo_section_name
27279 = ((dwarf_strict && dwarf_version < 5)
27280 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27281 debug_macinfo_section = get_section (debug_macinfo_section_name,
27282 SECTION_DEBUG
27283 | SECTION_EXCLUDE, NULL);
27284 /* For macro info we have to refer to a debug_line section, so
27285 similar to split-dwarf emit a skeleton one for early debug. */
27286 debug_skeleton_line_section
27287 = get_section (DEBUG_LTO_LINE_SECTION,
27288 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27289 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27290 DEBUG_SKELETON_LINE_SECTION_LABEL,
27291 generation);
27293 else
27295 /* ??? Which of the following do we need early? */
27296 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27297 SECTION_DEBUG | SECTION_EXCLUDE,
27298 NULL);
27299 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27300 SECTION_DEBUG | SECTION_EXCLUDE,
27301 NULL);
27302 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27303 SECTION_DEBUG
27304 | SECTION_EXCLUDE, NULL);
27305 debug_skeleton_abbrev_section
27306 = get_section (DEBUG_LTO_ABBREV_SECTION,
27307 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27308 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27309 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27310 generation);
27312 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27313 stay in the main .o, but the skeleton_line goes into the split
27314 off dwo. */
27315 debug_skeleton_line_section
27316 = get_section (DEBUG_LTO_LINE_SECTION,
27317 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27318 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27319 DEBUG_SKELETON_LINE_SECTION_LABEL,
27320 generation);
27321 debug_str_offsets_section
27322 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27323 SECTION_DEBUG | SECTION_EXCLUDE,
27324 NULL);
27325 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27326 DEBUG_SKELETON_INFO_SECTION_LABEL,
27327 generation);
27328 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27329 DEBUG_STR_DWO_SECTION_FLAGS,
27330 NULL);
27331 debug_macinfo_section_name
27332 = ((dwarf_strict && dwarf_version < 5)
27333 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27334 debug_macinfo_section = get_section (debug_macinfo_section_name,
27335 SECTION_DEBUG | SECTION_EXCLUDE,
27336 NULL);
27338 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27339 DEBUG_STR_SECTION_FLAGS
27340 | SECTION_EXCLUDE, NULL);
27341 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27342 debug_line_str_section
27343 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27344 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27346 else
27348 if (!dwarf_split_debug_info)
27350 debug_info_section = get_section (DEBUG_INFO_SECTION,
27351 SECTION_DEBUG, NULL);
27352 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27353 SECTION_DEBUG, NULL);
27354 debug_loc_section = get_section (dwarf_version >= 5
27355 ? DEBUG_LOCLISTS_SECTION
27356 : DEBUG_LOC_SECTION,
27357 SECTION_DEBUG, NULL);
27358 debug_macinfo_section_name
27359 = ((dwarf_strict && dwarf_version < 5)
27360 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27361 debug_macinfo_section = get_section (debug_macinfo_section_name,
27362 SECTION_DEBUG, NULL);
27364 else
27366 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27367 SECTION_DEBUG | SECTION_EXCLUDE,
27368 NULL);
27369 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27370 SECTION_DEBUG | SECTION_EXCLUDE,
27371 NULL);
27372 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27373 SECTION_DEBUG, NULL);
27374 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27375 SECTION_DEBUG, NULL);
27376 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27377 SECTION_DEBUG, NULL);
27378 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27379 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27380 generation);
27382 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27383 stay in the main .o, but the skeleton_line goes into the
27384 split off dwo. */
27385 debug_skeleton_line_section
27386 = get_section (DEBUG_DWO_LINE_SECTION,
27387 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27388 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27389 DEBUG_SKELETON_LINE_SECTION_LABEL,
27390 generation);
27391 debug_str_offsets_section
27392 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27393 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27394 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27395 DEBUG_SKELETON_INFO_SECTION_LABEL,
27396 generation);
27397 debug_loc_section = get_section (dwarf_version >= 5
27398 ? DEBUG_DWO_LOCLISTS_SECTION
27399 : DEBUG_DWO_LOC_SECTION,
27400 SECTION_DEBUG | SECTION_EXCLUDE,
27401 NULL);
27402 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27403 DEBUG_STR_DWO_SECTION_FLAGS,
27404 NULL);
27405 debug_macinfo_section_name
27406 = ((dwarf_strict && dwarf_version < 5)
27407 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27408 debug_macinfo_section = get_section (debug_macinfo_section_name,
27409 SECTION_DEBUG | SECTION_EXCLUDE,
27410 NULL);
27412 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27413 SECTION_DEBUG, NULL);
27414 debug_line_section = get_section (DEBUG_LINE_SECTION,
27415 SECTION_DEBUG, NULL);
27416 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27417 SECTION_DEBUG, NULL);
27418 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27419 SECTION_DEBUG, NULL);
27420 debug_str_section = get_section (DEBUG_STR_SECTION,
27421 DEBUG_STR_SECTION_FLAGS, NULL);
27422 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27423 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27424 DEBUG_STR_SECTION_FLAGS, NULL);
27425 debug_ranges_section = get_section (dwarf_version >= 5
27426 ? DEBUG_RNGLISTS_SECTION
27427 : DEBUG_RANGES_SECTION,
27428 SECTION_DEBUG, NULL);
27429 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27430 SECTION_DEBUG, NULL);
27433 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27434 DEBUG_ABBREV_SECTION_LABEL, generation);
27435 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27436 DEBUG_INFO_SECTION_LABEL, generation);
27437 info_section_emitted = false;
27438 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27439 DEBUG_LINE_SECTION_LABEL, generation);
27440 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27441 DEBUG_RANGES_SECTION_LABEL, generation);
27442 if (dwarf_version >= 5 && dwarf_split_debug_info)
27443 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27444 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27445 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27446 DEBUG_ADDR_SECTION_LABEL, generation);
27447 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27448 (dwarf_strict && dwarf_version < 5)
27449 ? DEBUG_MACINFO_SECTION_LABEL
27450 : DEBUG_MACRO_SECTION_LABEL, generation);
27451 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27452 generation);
27454 ++generation;
27457 /* Set up for Dwarf output at the start of compilation. */
27459 static void
27460 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27462 /* Allocate the file_table. */
27463 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27465 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27466 /* Allocate the decl_die_table. */
27467 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27469 /* Allocate the decl_loc_table. */
27470 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27472 /* Allocate the cached_dw_loc_list_table. */
27473 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27475 /* Allocate the initial hunk of the decl_scope_table. */
27476 vec_alloc (decl_scope_table, 256);
27478 /* Allocate the initial hunk of the abbrev_die_table. */
27479 vec_alloc (abbrev_die_table, 256);
27480 /* Zero-th entry is allocated, but unused. */
27481 abbrev_die_table->quick_push (NULL);
27483 /* Allocate the dwarf_proc_stack_usage_map. */
27484 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27486 /* Allocate the pubtypes and pubnames vectors. */
27487 vec_alloc (pubname_table, 32);
27488 vec_alloc (pubtype_table, 32);
27490 vec_alloc (incomplete_types, 64);
27492 vec_alloc (used_rtx_array, 32);
27494 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27495 vec_alloc (macinfo_table, 64);
27496 #endif
27498 /* If front-ends already registered a main translation unit but we were not
27499 ready to perform the association, do this now. */
27500 if (main_translation_unit != NULL_TREE)
27501 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27504 /* Called before compile () starts outputtting functions, variables
27505 and toplevel asms into assembly. */
27507 static void
27508 dwarf2out_assembly_start (void)
27510 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27511 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27512 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27513 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27514 COLD_TEXT_SECTION_LABEL, 0);
27515 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27517 switch_to_section (text_section);
27518 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27519 #endif
27521 /* Make sure the line number table for .text always exists. */
27522 text_section_line_info = new_line_info_table ();
27523 text_section_line_info->end_label = text_end_label;
27525 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27526 cur_line_info_table = text_section_line_info;
27527 #endif
27529 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27530 && dwarf2out_do_cfi_asm ()
27531 && !dwarf2out_do_eh_frame ())
27532 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27535 /* A helper function for dwarf2out_finish called through
27536 htab_traverse. Assign a string its index. All strings must be
27537 collected into the table by the time index_string is called,
27538 because the indexing code relies on htab_traverse to traverse nodes
27539 in the same order for each run. */
27542 index_string (indirect_string_node **h, unsigned int *index)
27544 indirect_string_node *node = *h;
27546 find_string_form (node);
27547 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27549 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27550 node->index = *index;
27551 *index += 1;
27553 return 1;
27556 /* A helper function for output_indirect_strings called through
27557 htab_traverse. Output the offset to a string and update the
27558 current offset. */
27561 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27563 indirect_string_node *node = *h;
27565 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27567 /* Assert that this node has been assigned an index. */
27568 gcc_assert (node->index != NO_INDEX_ASSIGNED
27569 && node->index != NOT_INDEXED);
27570 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27571 "indexed string 0x%x: %s", node->index, node->str);
27572 *offset += strlen (node->str) + 1;
27574 return 1;
27577 /* A helper function for dwarf2out_finish called through
27578 htab_traverse. Output the indexed string. */
27581 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27583 struct indirect_string_node *node = *h;
27585 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27587 /* Assert that the strings are output in the same order as their
27588 indexes were assigned. */
27589 gcc_assert (*cur_idx == node->index);
27590 assemble_string (node->str, strlen (node->str) + 1);
27591 *cur_idx += 1;
27593 return 1;
27596 /* A helper function for dwarf2out_finish called through
27597 htab_traverse. Emit one queued .debug_str string. */
27600 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27602 struct indirect_string_node *node = *h;
27604 node->form = find_string_form (node);
27605 if (node->form == form && node->refcount > 0)
27607 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27608 assemble_string (node->str, strlen (node->str) + 1);
27611 return 1;
27614 /* Output the indexed string table. */
27616 static void
27617 output_indirect_strings (void)
27619 switch_to_section (debug_str_section);
27620 if (!dwarf_split_debug_info)
27621 debug_str_hash->traverse<enum dwarf_form,
27622 output_indirect_string> (DW_FORM_strp);
27623 else
27625 unsigned int offset = 0;
27626 unsigned int cur_idx = 0;
27628 skeleton_debug_str_hash->traverse<enum dwarf_form,
27629 output_indirect_string> (DW_FORM_strp);
27631 switch_to_section (debug_str_offsets_section);
27632 debug_str_hash->traverse_noresize
27633 <unsigned int *, output_index_string_offset> (&offset);
27634 switch_to_section (debug_str_dwo_section);
27635 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27636 (&cur_idx);
27640 /* Callback for htab_traverse to assign an index to an entry in the
27641 table, and to write that entry to the .debug_addr section. */
27644 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27646 addr_table_entry *entry = *slot;
27648 if (entry->refcount == 0)
27650 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27651 || entry->index == NOT_INDEXED);
27652 return 1;
27655 gcc_assert (entry->index == *cur_index);
27656 (*cur_index)++;
27658 switch (entry->kind)
27660 case ate_kind_rtx:
27661 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27662 "0x%x", entry->index);
27663 break;
27664 case ate_kind_rtx_dtprel:
27665 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27666 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27667 DWARF2_ADDR_SIZE,
27668 entry->addr.rtl);
27669 fputc ('\n', asm_out_file);
27670 break;
27671 case ate_kind_label:
27672 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27673 "0x%x", entry->index);
27674 break;
27675 default:
27676 gcc_unreachable ();
27678 return 1;
27681 /* Produce the .debug_addr section. */
27683 static void
27684 output_addr_table (void)
27686 unsigned int index = 0;
27687 if (addr_index_table == NULL || addr_index_table->size () == 0)
27688 return;
27690 switch_to_section (debug_addr_section);
27691 addr_index_table
27692 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27695 #if ENABLE_ASSERT_CHECKING
27696 /* Verify that all marks are clear. */
27698 static void
27699 verify_marks_clear (dw_die_ref die)
27701 dw_die_ref c;
27703 gcc_assert (! die->die_mark);
27704 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27706 #endif /* ENABLE_ASSERT_CHECKING */
27708 /* Clear the marks for a die and its children.
27709 Be cool if the mark isn't set. */
27711 static void
27712 prune_unmark_dies (dw_die_ref die)
27714 dw_die_ref c;
27716 if (die->die_mark)
27717 die->die_mark = 0;
27718 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27721 /* Given LOC that is referenced by a DIE we're marking as used, find all
27722 referenced DWARF procedures it references and mark them as used. */
27724 static void
27725 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27727 for (; loc != NULL; loc = loc->dw_loc_next)
27728 switch (loc->dw_loc_opc)
27730 case DW_OP_implicit_pointer:
27731 case DW_OP_convert:
27732 case DW_OP_reinterpret:
27733 case DW_OP_GNU_implicit_pointer:
27734 case DW_OP_GNU_convert:
27735 case DW_OP_GNU_reinterpret:
27736 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27737 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27738 break;
27739 case DW_OP_GNU_variable_value:
27740 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27742 dw_die_ref ref
27743 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27744 if (ref == NULL)
27745 break;
27746 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27747 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27748 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27750 /* FALLTHRU */
27751 case DW_OP_call2:
27752 case DW_OP_call4:
27753 case DW_OP_call_ref:
27754 case DW_OP_const_type:
27755 case DW_OP_GNU_const_type:
27756 case DW_OP_GNU_parameter_ref:
27757 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27758 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27759 break;
27760 case DW_OP_regval_type:
27761 case DW_OP_deref_type:
27762 case DW_OP_GNU_regval_type:
27763 case DW_OP_GNU_deref_type:
27764 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27765 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27766 break;
27767 case DW_OP_entry_value:
27768 case DW_OP_GNU_entry_value:
27769 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27770 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27771 break;
27772 default:
27773 break;
27777 /* Given DIE that we're marking as used, find any other dies
27778 it references as attributes and mark them as used. */
27780 static void
27781 prune_unused_types_walk_attribs (dw_die_ref die)
27783 dw_attr_node *a;
27784 unsigned ix;
27786 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27788 switch (AT_class (a))
27790 /* Make sure DWARF procedures referenced by location descriptions will
27791 get emitted. */
27792 case dw_val_class_loc:
27793 prune_unused_types_walk_loc_descr (AT_loc (a));
27794 break;
27795 case dw_val_class_loc_list:
27796 for (dw_loc_list_ref list = AT_loc_list (a);
27797 list != NULL;
27798 list = list->dw_loc_next)
27799 prune_unused_types_walk_loc_descr (list->expr);
27800 break;
27802 case dw_val_class_die_ref:
27803 /* A reference to another DIE.
27804 Make sure that it will get emitted.
27805 If it was broken out into a comdat group, don't follow it. */
27806 if (! AT_ref (a)->comdat_type_p
27807 || a->dw_attr == DW_AT_specification)
27808 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27809 break;
27811 case dw_val_class_str:
27812 /* Set the string's refcount to 0 so that prune_unused_types_mark
27813 accounts properly for it. */
27814 a->dw_attr_val.v.val_str->refcount = 0;
27815 break;
27817 default:
27818 break;
27823 /* Mark the generic parameters and arguments children DIEs of DIE. */
27825 static void
27826 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27828 dw_die_ref c;
27830 if (die == NULL || die->die_child == NULL)
27831 return;
27832 c = die->die_child;
27835 if (is_template_parameter (c))
27836 prune_unused_types_mark (c, 1);
27837 c = c->die_sib;
27838 } while (c && c != die->die_child);
27841 /* Mark DIE as being used. If DOKIDS is true, then walk down
27842 to DIE's children. */
27844 static void
27845 prune_unused_types_mark (dw_die_ref die, int dokids)
27847 dw_die_ref c;
27849 if (die->die_mark == 0)
27851 /* We haven't done this node yet. Mark it as used. */
27852 die->die_mark = 1;
27853 /* If this is the DIE of a generic type instantiation,
27854 mark the children DIEs that describe its generic parms and
27855 args. */
27856 prune_unused_types_mark_generic_parms_dies (die);
27858 /* We also have to mark its parents as used.
27859 (But we don't want to mark our parent's kids due to this,
27860 unless it is a class.) */
27861 if (die->die_parent)
27862 prune_unused_types_mark (die->die_parent,
27863 class_scope_p (die->die_parent));
27865 /* Mark any referenced nodes. */
27866 prune_unused_types_walk_attribs (die);
27868 /* If this node is a specification,
27869 also mark the definition, if it exists. */
27870 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27871 prune_unused_types_mark (die->die_definition, 1);
27874 if (dokids && die->die_mark != 2)
27876 /* We need to walk the children, but haven't done so yet.
27877 Remember that we've walked the kids. */
27878 die->die_mark = 2;
27880 /* If this is an array type, we need to make sure our
27881 kids get marked, even if they're types. If we're
27882 breaking out types into comdat sections, do this
27883 for all type definitions. */
27884 if (die->die_tag == DW_TAG_array_type
27885 || (use_debug_types
27886 && is_type_die (die) && ! is_declaration_die (die)))
27887 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27888 else
27889 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27893 /* For local classes, look if any static member functions were emitted
27894 and if so, mark them. */
27896 static void
27897 prune_unused_types_walk_local_classes (dw_die_ref die)
27899 dw_die_ref c;
27901 if (die->die_mark == 2)
27902 return;
27904 switch (die->die_tag)
27906 case DW_TAG_structure_type:
27907 case DW_TAG_union_type:
27908 case DW_TAG_class_type:
27909 break;
27911 case DW_TAG_subprogram:
27912 if (!get_AT_flag (die, DW_AT_declaration)
27913 || die->die_definition != NULL)
27914 prune_unused_types_mark (die, 1);
27915 return;
27917 default:
27918 return;
27921 /* Mark children. */
27922 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27925 /* Walk the tree DIE and mark types that we actually use. */
27927 static void
27928 prune_unused_types_walk (dw_die_ref die)
27930 dw_die_ref c;
27932 /* Don't do anything if this node is already marked and
27933 children have been marked as well. */
27934 if (die->die_mark == 2)
27935 return;
27937 switch (die->die_tag)
27939 case DW_TAG_structure_type:
27940 case DW_TAG_union_type:
27941 case DW_TAG_class_type:
27942 if (die->die_perennial_p)
27943 break;
27945 for (c = die->die_parent; c; c = c->die_parent)
27946 if (c->die_tag == DW_TAG_subprogram)
27947 break;
27949 /* Finding used static member functions inside of classes
27950 is needed just for local classes, because for other classes
27951 static member function DIEs with DW_AT_specification
27952 are emitted outside of the DW_TAG_*_type. If we ever change
27953 it, we'd need to call this even for non-local classes. */
27954 if (c)
27955 prune_unused_types_walk_local_classes (die);
27957 /* It's a type node --- don't mark it. */
27958 return;
27960 case DW_TAG_const_type:
27961 case DW_TAG_packed_type:
27962 case DW_TAG_pointer_type:
27963 case DW_TAG_reference_type:
27964 case DW_TAG_rvalue_reference_type:
27965 case DW_TAG_volatile_type:
27966 case DW_TAG_typedef:
27967 case DW_TAG_array_type:
27968 case DW_TAG_interface_type:
27969 case DW_TAG_friend:
27970 case DW_TAG_enumeration_type:
27971 case DW_TAG_subroutine_type:
27972 case DW_TAG_string_type:
27973 case DW_TAG_set_type:
27974 case DW_TAG_subrange_type:
27975 case DW_TAG_ptr_to_member_type:
27976 case DW_TAG_file_type:
27977 /* Type nodes are useful only when other DIEs reference them --- don't
27978 mark them. */
27979 /* FALLTHROUGH */
27981 case DW_TAG_dwarf_procedure:
27982 /* Likewise for DWARF procedures. */
27984 if (die->die_perennial_p)
27985 break;
27987 return;
27989 default:
27990 /* Mark everything else. */
27991 break;
27994 if (die->die_mark == 0)
27996 die->die_mark = 1;
27998 /* Now, mark any dies referenced from here. */
27999 prune_unused_types_walk_attribs (die);
28002 die->die_mark = 2;
28004 /* Mark children. */
28005 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28008 /* Increment the string counts on strings referred to from DIE's
28009 attributes. */
28011 static void
28012 prune_unused_types_update_strings (dw_die_ref die)
28014 dw_attr_node *a;
28015 unsigned ix;
28017 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28018 if (AT_class (a) == dw_val_class_str)
28020 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28021 s->refcount++;
28022 /* Avoid unnecessarily putting strings that are used less than
28023 twice in the hash table. */
28024 if (s->refcount
28025 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28027 indirect_string_node **slot
28028 = debug_str_hash->find_slot_with_hash (s->str,
28029 htab_hash_string (s->str),
28030 INSERT);
28031 gcc_assert (*slot == NULL);
28032 *slot = s;
28037 /* Mark DIE and its children as removed. */
28039 static void
28040 mark_removed (dw_die_ref die)
28042 dw_die_ref c;
28043 die->removed = true;
28044 FOR_EACH_CHILD (die, c, mark_removed (c));
28047 /* Remove from the tree DIE any dies that aren't marked. */
28049 static void
28050 prune_unused_types_prune (dw_die_ref die)
28052 dw_die_ref c;
28054 gcc_assert (die->die_mark);
28055 prune_unused_types_update_strings (die);
28057 if (! die->die_child)
28058 return;
28060 c = die->die_child;
28061 do {
28062 dw_die_ref prev = c, next;
28063 for (c = c->die_sib; ! c->die_mark; c = next)
28064 if (c == die->die_child)
28066 /* No marked children between 'prev' and the end of the list. */
28067 if (prev == c)
28068 /* No marked children at all. */
28069 die->die_child = NULL;
28070 else
28072 prev->die_sib = c->die_sib;
28073 die->die_child = prev;
28075 c->die_sib = NULL;
28076 mark_removed (c);
28077 return;
28079 else
28081 next = c->die_sib;
28082 c->die_sib = NULL;
28083 mark_removed (c);
28086 if (c != prev->die_sib)
28087 prev->die_sib = c;
28088 prune_unused_types_prune (c);
28089 } while (c != die->die_child);
28092 /* Remove dies representing declarations that we never use. */
28094 static void
28095 prune_unused_types (void)
28097 unsigned int i;
28098 limbo_die_node *node;
28099 comdat_type_node *ctnode;
28100 pubname_entry *pub;
28101 dw_die_ref base_type;
28103 #if ENABLE_ASSERT_CHECKING
28104 /* All the marks should already be clear. */
28105 verify_marks_clear (comp_unit_die ());
28106 for (node = limbo_die_list; node; node = node->next)
28107 verify_marks_clear (node->die);
28108 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28109 verify_marks_clear (ctnode->root_die);
28110 #endif /* ENABLE_ASSERT_CHECKING */
28112 /* Mark types that are used in global variables. */
28113 premark_types_used_by_global_vars ();
28115 /* Set the mark on nodes that are actually used. */
28116 prune_unused_types_walk (comp_unit_die ());
28117 for (node = limbo_die_list; node; node = node->next)
28118 prune_unused_types_walk (node->die);
28119 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28121 prune_unused_types_walk (ctnode->root_die);
28122 prune_unused_types_mark (ctnode->type_die, 1);
28125 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28126 are unusual in that they are pubnames that are the children of pubtypes.
28127 They should only be marked via their parent DW_TAG_enumeration_type die,
28128 not as roots in themselves. */
28129 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28130 if (pub->die->die_tag != DW_TAG_enumerator)
28131 prune_unused_types_mark (pub->die, 1);
28132 for (i = 0; base_types.iterate (i, &base_type); i++)
28133 prune_unused_types_mark (base_type, 1);
28135 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28136 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28137 callees). */
28138 cgraph_node *cnode;
28139 FOR_EACH_FUNCTION (cnode)
28140 if (cnode->referred_to_p (false))
28142 dw_die_ref die = lookup_decl_die (cnode->decl);
28143 if (die == NULL || die->die_mark)
28144 continue;
28145 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28146 if (e->caller != cnode
28147 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28149 prune_unused_types_mark (die, 1);
28150 break;
28154 if (debug_str_hash)
28155 debug_str_hash->empty ();
28156 if (skeleton_debug_str_hash)
28157 skeleton_debug_str_hash->empty ();
28158 prune_unused_types_prune (comp_unit_die ());
28159 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28161 node = *pnode;
28162 if (!node->die->die_mark)
28163 *pnode = node->next;
28164 else
28166 prune_unused_types_prune (node->die);
28167 pnode = &node->next;
28170 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28171 prune_unused_types_prune (ctnode->root_die);
28173 /* Leave the marks clear. */
28174 prune_unmark_dies (comp_unit_die ());
28175 for (node = limbo_die_list; node; node = node->next)
28176 prune_unmark_dies (node->die);
28177 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28178 prune_unmark_dies (ctnode->root_die);
28181 /* Helpers to manipulate hash table of comdat type units. */
28183 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28185 static inline hashval_t hash (const comdat_type_node *);
28186 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28189 inline hashval_t
28190 comdat_type_hasher::hash (const comdat_type_node *type_node)
28192 hashval_t h;
28193 memcpy (&h, type_node->signature, sizeof (h));
28194 return h;
28197 inline bool
28198 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28199 const comdat_type_node *type_node_2)
28201 return (! memcmp (type_node_1->signature, type_node_2->signature,
28202 DWARF_TYPE_SIGNATURE_SIZE));
28205 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28206 to the location it would have been added, should we know its
28207 DECL_ASSEMBLER_NAME when we added other attributes. This will
28208 probably improve compactness of debug info, removing equivalent
28209 abbrevs, and hide any differences caused by deferring the
28210 computation of the assembler name, triggered by e.g. PCH. */
28212 static inline void
28213 move_linkage_attr (dw_die_ref die)
28215 unsigned ix = vec_safe_length (die->die_attr);
28216 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28218 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28219 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28221 while (--ix > 0)
28223 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28225 if (prev->dw_attr == DW_AT_decl_line
28226 || prev->dw_attr == DW_AT_decl_column
28227 || prev->dw_attr == DW_AT_name)
28228 break;
28231 if (ix != vec_safe_length (die->die_attr) - 1)
28233 die->die_attr->pop ();
28234 die->die_attr->quick_insert (ix, linkage);
28238 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28239 referenced from typed stack ops and count how often they are used. */
28241 static void
28242 mark_base_types (dw_loc_descr_ref loc)
28244 dw_die_ref base_type = NULL;
28246 for (; loc; loc = loc->dw_loc_next)
28248 switch (loc->dw_loc_opc)
28250 case DW_OP_regval_type:
28251 case DW_OP_deref_type:
28252 case DW_OP_GNU_regval_type:
28253 case DW_OP_GNU_deref_type:
28254 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28255 break;
28256 case DW_OP_convert:
28257 case DW_OP_reinterpret:
28258 case DW_OP_GNU_convert:
28259 case DW_OP_GNU_reinterpret:
28260 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28261 continue;
28262 /* FALLTHRU */
28263 case DW_OP_const_type:
28264 case DW_OP_GNU_const_type:
28265 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28266 break;
28267 case DW_OP_entry_value:
28268 case DW_OP_GNU_entry_value:
28269 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28270 continue;
28271 default:
28272 continue;
28274 gcc_assert (base_type->die_parent == comp_unit_die ());
28275 if (base_type->die_mark)
28276 base_type->die_mark++;
28277 else
28279 base_types.safe_push (base_type);
28280 base_type->die_mark = 1;
28285 /* Comparison function for sorting marked base types. */
28287 static int
28288 base_type_cmp (const void *x, const void *y)
28290 dw_die_ref dx = *(const dw_die_ref *) x;
28291 dw_die_ref dy = *(const dw_die_ref *) y;
28292 unsigned int byte_size1, byte_size2;
28293 unsigned int encoding1, encoding2;
28294 unsigned int align1, align2;
28295 if (dx->die_mark > dy->die_mark)
28296 return -1;
28297 if (dx->die_mark < dy->die_mark)
28298 return 1;
28299 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28300 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28301 if (byte_size1 < byte_size2)
28302 return 1;
28303 if (byte_size1 > byte_size2)
28304 return -1;
28305 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28306 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28307 if (encoding1 < encoding2)
28308 return 1;
28309 if (encoding1 > encoding2)
28310 return -1;
28311 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28312 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28313 if (align1 < align2)
28314 return 1;
28315 if (align1 > align2)
28316 return -1;
28317 return 0;
28320 /* Move base types marked by mark_base_types as early as possible
28321 in the CU, sorted by decreasing usage count both to make the
28322 uleb128 references as small as possible and to make sure they
28323 will have die_offset already computed by calc_die_sizes when
28324 sizes of typed stack loc ops is computed. */
28326 static void
28327 move_marked_base_types (void)
28329 unsigned int i;
28330 dw_die_ref base_type, die, c;
28332 if (base_types.is_empty ())
28333 return;
28335 /* Sort by decreasing usage count, they will be added again in that
28336 order later on. */
28337 base_types.qsort (base_type_cmp);
28338 die = comp_unit_die ();
28339 c = die->die_child;
28342 dw_die_ref prev = c;
28343 c = c->die_sib;
28344 while (c->die_mark)
28346 remove_child_with_prev (c, prev);
28347 /* As base types got marked, there must be at least
28348 one node other than DW_TAG_base_type. */
28349 gcc_assert (die->die_child != NULL);
28350 c = prev->die_sib;
28353 while (c != die->die_child);
28354 gcc_assert (die->die_child);
28355 c = die->die_child;
28356 for (i = 0; base_types.iterate (i, &base_type); i++)
28358 base_type->die_mark = 0;
28359 base_type->die_sib = c->die_sib;
28360 c->die_sib = base_type;
28361 c = base_type;
28365 /* Helper function for resolve_addr, attempt to resolve
28366 one CONST_STRING, return true if successful. Similarly verify that
28367 SYMBOL_REFs refer to variables emitted in the current CU. */
28369 static bool
28370 resolve_one_addr (rtx *addr)
28372 rtx rtl = *addr;
28374 if (GET_CODE (rtl) == CONST_STRING)
28376 size_t len = strlen (XSTR (rtl, 0)) + 1;
28377 tree t = build_string (len, XSTR (rtl, 0));
28378 tree tlen = size_int (len - 1);
28379 TREE_TYPE (t)
28380 = build_array_type (char_type_node, build_index_type (tlen));
28381 rtl = lookup_constant_def (t);
28382 if (!rtl || !MEM_P (rtl))
28383 return false;
28384 rtl = XEXP (rtl, 0);
28385 if (GET_CODE (rtl) == SYMBOL_REF
28386 && SYMBOL_REF_DECL (rtl)
28387 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28388 return false;
28389 vec_safe_push (used_rtx_array, rtl);
28390 *addr = rtl;
28391 return true;
28394 if (GET_CODE (rtl) == SYMBOL_REF
28395 && SYMBOL_REF_DECL (rtl))
28397 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28399 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28400 return false;
28402 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28403 return false;
28406 if (GET_CODE (rtl) == CONST)
28408 subrtx_ptr_iterator::array_type array;
28409 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28410 if (!resolve_one_addr (*iter))
28411 return false;
28414 return true;
28417 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28418 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28419 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28421 static rtx
28422 string_cst_pool_decl (tree t)
28424 rtx rtl = output_constant_def (t, 1);
28425 unsigned char *array;
28426 dw_loc_descr_ref l;
28427 tree decl;
28428 size_t len;
28429 dw_die_ref ref;
28431 if (!rtl || !MEM_P (rtl))
28432 return NULL_RTX;
28433 rtl = XEXP (rtl, 0);
28434 if (GET_CODE (rtl) != SYMBOL_REF
28435 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28436 return NULL_RTX;
28438 decl = SYMBOL_REF_DECL (rtl);
28439 if (!lookup_decl_die (decl))
28441 len = TREE_STRING_LENGTH (t);
28442 vec_safe_push (used_rtx_array, rtl);
28443 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28444 array = ggc_vec_alloc<unsigned char> (len);
28445 memcpy (array, TREE_STRING_POINTER (t), len);
28446 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28447 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28448 l->dw_loc_oprnd2.v.val_vec.length = len;
28449 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28450 l->dw_loc_oprnd2.v.val_vec.array = array;
28451 add_AT_loc (ref, DW_AT_location, l);
28452 equate_decl_number_to_die (decl, ref);
28454 return rtl;
28457 /* Helper function of resolve_addr_in_expr. LOC is
28458 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28459 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28460 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28461 with DW_OP_implicit_pointer if possible
28462 and return true, if unsuccessful, return false. */
28464 static bool
28465 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28467 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28468 HOST_WIDE_INT offset = 0;
28469 dw_die_ref ref = NULL;
28470 tree decl;
28472 if (GET_CODE (rtl) == CONST
28473 && GET_CODE (XEXP (rtl, 0)) == PLUS
28474 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28476 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28477 rtl = XEXP (XEXP (rtl, 0), 0);
28479 if (GET_CODE (rtl) == CONST_STRING)
28481 size_t len = strlen (XSTR (rtl, 0)) + 1;
28482 tree t = build_string (len, XSTR (rtl, 0));
28483 tree tlen = size_int (len - 1);
28485 TREE_TYPE (t)
28486 = build_array_type (char_type_node, build_index_type (tlen));
28487 rtl = string_cst_pool_decl (t);
28488 if (!rtl)
28489 return false;
28491 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28493 decl = SYMBOL_REF_DECL (rtl);
28494 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28496 ref = lookup_decl_die (decl);
28497 if (ref && (get_AT (ref, DW_AT_location)
28498 || get_AT (ref, DW_AT_const_value)))
28500 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28501 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28502 loc->dw_loc_oprnd1.val_entry = NULL;
28503 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28504 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28505 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28506 loc->dw_loc_oprnd2.v.val_int = offset;
28507 return true;
28511 return false;
28514 /* Helper function for resolve_addr, handle one location
28515 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28516 the location list couldn't be resolved. */
28518 static bool
28519 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28521 dw_loc_descr_ref keep = NULL;
28522 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28523 switch (loc->dw_loc_opc)
28525 case DW_OP_addr:
28526 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28528 if ((prev == NULL
28529 || prev->dw_loc_opc == DW_OP_piece
28530 || prev->dw_loc_opc == DW_OP_bit_piece)
28531 && loc->dw_loc_next
28532 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28533 && (!dwarf_strict || dwarf_version >= 5)
28534 && optimize_one_addr_into_implicit_ptr (loc))
28535 break;
28536 return false;
28538 break;
28539 case DW_OP_GNU_addr_index:
28540 case DW_OP_GNU_const_index:
28541 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28542 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28544 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28545 if (!resolve_one_addr (&rtl))
28546 return false;
28547 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28548 loc->dw_loc_oprnd1.val_entry
28549 = add_addr_table_entry (rtl, ate_kind_rtx);
28551 break;
28552 case DW_OP_const4u:
28553 case DW_OP_const8u:
28554 if (loc->dtprel
28555 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28556 return false;
28557 break;
28558 case DW_OP_plus_uconst:
28559 if (size_of_loc_descr (loc)
28560 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28562 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28564 dw_loc_descr_ref repl
28565 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28566 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28567 add_loc_descr (&repl, loc->dw_loc_next);
28568 *loc = *repl;
28570 break;
28571 case DW_OP_implicit_value:
28572 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28573 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28574 return false;
28575 break;
28576 case DW_OP_implicit_pointer:
28577 case DW_OP_GNU_implicit_pointer:
28578 case DW_OP_GNU_parameter_ref:
28579 case DW_OP_GNU_variable_value:
28580 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28582 dw_die_ref ref
28583 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28584 if (ref == NULL)
28585 return false;
28586 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28587 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28588 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28590 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28592 if (prev == NULL
28593 && loc->dw_loc_next == NULL
28594 && AT_class (a) == dw_val_class_loc)
28595 switch (a->dw_attr)
28597 /* Following attributes allow both exprloc and reference,
28598 so if the whole expression is DW_OP_GNU_variable_value
28599 alone we could transform it into reference. */
28600 case DW_AT_byte_size:
28601 case DW_AT_bit_size:
28602 case DW_AT_lower_bound:
28603 case DW_AT_upper_bound:
28604 case DW_AT_bit_stride:
28605 case DW_AT_count:
28606 case DW_AT_allocated:
28607 case DW_AT_associated:
28608 case DW_AT_byte_stride:
28609 a->dw_attr_val.val_class = dw_val_class_die_ref;
28610 a->dw_attr_val.val_entry = NULL;
28611 a->dw_attr_val.v.val_die_ref.die
28612 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28613 a->dw_attr_val.v.val_die_ref.external = 0;
28614 return true;
28615 default:
28616 break;
28618 if (dwarf_strict)
28619 return false;
28621 break;
28622 case DW_OP_const_type:
28623 case DW_OP_regval_type:
28624 case DW_OP_deref_type:
28625 case DW_OP_convert:
28626 case DW_OP_reinterpret:
28627 case DW_OP_GNU_const_type:
28628 case DW_OP_GNU_regval_type:
28629 case DW_OP_GNU_deref_type:
28630 case DW_OP_GNU_convert:
28631 case DW_OP_GNU_reinterpret:
28632 while (loc->dw_loc_next
28633 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28634 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28636 dw_die_ref base1, base2;
28637 unsigned enc1, enc2, size1, size2;
28638 if (loc->dw_loc_opc == DW_OP_regval_type
28639 || loc->dw_loc_opc == DW_OP_deref_type
28640 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28641 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28642 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28643 else if (loc->dw_loc_oprnd1.val_class
28644 == dw_val_class_unsigned_const)
28645 break;
28646 else
28647 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28648 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28649 == dw_val_class_unsigned_const)
28650 break;
28651 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28652 gcc_assert (base1->die_tag == DW_TAG_base_type
28653 && base2->die_tag == DW_TAG_base_type);
28654 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28655 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28656 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28657 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28658 if (size1 == size2
28659 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28660 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28661 && loc != keep)
28662 || enc1 == enc2))
28664 /* Optimize away next DW_OP_convert after
28665 adjusting LOC's base type die reference. */
28666 if (loc->dw_loc_opc == DW_OP_regval_type
28667 || loc->dw_loc_opc == DW_OP_deref_type
28668 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28669 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28670 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28671 else
28672 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28673 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28674 continue;
28676 /* Don't change integer DW_OP_convert after e.g. floating
28677 point typed stack entry. */
28678 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28679 keep = loc->dw_loc_next;
28680 break;
28682 break;
28683 default:
28684 break;
28686 return true;
28689 /* Helper function of resolve_addr. DIE had DW_AT_location of
28690 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28691 and DW_OP_addr couldn't be resolved. resolve_addr has already
28692 removed the DW_AT_location attribute. This function attempts to
28693 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28694 to it or DW_AT_const_value attribute, if possible. */
28696 static void
28697 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28699 if (!VAR_P (decl)
28700 || lookup_decl_die (decl) != die
28701 || DECL_EXTERNAL (decl)
28702 || !TREE_STATIC (decl)
28703 || DECL_INITIAL (decl) == NULL_TREE
28704 || DECL_P (DECL_INITIAL (decl))
28705 || get_AT (die, DW_AT_const_value))
28706 return;
28708 tree init = DECL_INITIAL (decl);
28709 HOST_WIDE_INT offset = 0;
28710 /* For variables that have been optimized away and thus
28711 don't have a memory location, see if we can emit
28712 DW_AT_const_value instead. */
28713 if (tree_add_const_value_attribute (die, init))
28714 return;
28715 if (dwarf_strict && dwarf_version < 5)
28716 return;
28717 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28718 and ADDR_EXPR refers to a decl that has DW_AT_location or
28719 DW_AT_const_value (but isn't addressable, otherwise
28720 resolving the original DW_OP_addr wouldn't fail), see if
28721 we can add DW_OP_implicit_pointer. */
28722 STRIP_NOPS (init);
28723 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28724 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28726 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28727 init = TREE_OPERAND (init, 0);
28728 STRIP_NOPS (init);
28730 if (TREE_CODE (init) != ADDR_EXPR)
28731 return;
28732 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28733 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28734 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28735 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28736 && TREE_OPERAND (init, 0) != decl))
28738 dw_die_ref ref;
28739 dw_loc_descr_ref l;
28741 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28743 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28744 if (!rtl)
28745 return;
28746 decl = SYMBOL_REF_DECL (rtl);
28748 else
28749 decl = TREE_OPERAND (init, 0);
28750 ref = lookup_decl_die (decl);
28751 if (ref == NULL
28752 || (!get_AT (ref, DW_AT_location)
28753 && !get_AT (ref, DW_AT_const_value)))
28754 return;
28755 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28756 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28757 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28758 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28759 add_AT_loc (die, DW_AT_location, l);
28763 /* Return NULL if l is a DWARF expression, or first op that is not
28764 valid DWARF expression. */
28766 static dw_loc_descr_ref
28767 non_dwarf_expression (dw_loc_descr_ref l)
28769 while (l)
28771 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28772 return l;
28773 switch (l->dw_loc_opc)
28775 case DW_OP_regx:
28776 case DW_OP_implicit_value:
28777 case DW_OP_stack_value:
28778 case DW_OP_implicit_pointer:
28779 case DW_OP_GNU_implicit_pointer:
28780 case DW_OP_GNU_parameter_ref:
28781 case DW_OP_piece:
28782 case DW_OP_bit_piece:
28783 return l;
28784 default:
28785 break;
28787 l = l->dw_loc_next;
28789 return NULL;
28792 /* Return adjusted copy of EXPR:
28793 If it is empty DWARF expression, return it.
28794 If it is valid non-empty DWARF expression,
28795 return copy of EXPR with DW_OP_deref appended to it.
28796 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28797 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28798 If it is DWARF expression followed by DW_OP_stack_value, return
28799 copy of the DWARF expression without anything appended.
28800 Otherwise, return NULL. */
28802 static dw_loc_descr_ref
28803 copy_deref_exprloc (dw_loc_descr_ref expr)
28805 dw_loc_descr_ref tail = NULL;
28807 if (expr == NULL)
28808 return NULL;
28810 dw_loc_descr_ref l = non_dwarf_expression (expr);
28811 if (l && l->dw_loc_next)
28812 return NULL;
28814 if (l)
28816 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28817 tail = new_loc_descr ((enum dwarf_location_atom)
28818 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28819 0, 0);
28820 else
28821 switch (l->dw_loc_opc)
28823 case DW_OP_regx:
28824 tail = new_loc_descr (DW_OP_bregx,
28825 l->dw_loc_oprnd1.v.val_unsigned, 0);
28826 break;
28827 case DW_OP_stack_value:
28828 break;
28829 default:
28830 return NULL;
28833 else
28834 tail = new_loc_descr (DW_OP_deref, 0, 0);
28836 dw_loc_descr_ref ret = NULL, *p = &ret;
28837 while (expr != l)
28839 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28840 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28841 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28842 p = &(*p)->dw_loc_next;
28843 expr = expr->dw_loc_next;
28845 *p = tail;
28846 return ret;
28849 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28850 reference to a variable or argument, adjust it if needed and return:
28851 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28852 attribute if present should be removed
28853 0 keep the attribute perhaps with minor modifications, no need to rescan
28854 1 if the attribute has been successfully adjusted. */
28856 static int
28857 optimize_string_length (dw_attr_node *a)
28859 dw_loc_descr_ref l = AT_loc (a), lv;
28860 dw_die_ref die;
28861 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28863 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28864 die = lookup_decl_die (decl);
28865 if (die)
28867 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28868 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28869 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28871 else
28872 return -1;
28874 else
28875 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28877 /* DWARF5 allows reference class, so we can then reference the DIE.
28878 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28879 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28881 a->dw_attr_val.val_class = dw_val_class_die_ref;
28882 a->dw_attr_val.val_entry = NULL;
28883 a->dw_attr_val.v.val_die_ref.die = die;
28884 a->dw_attr_val.v.val_die_ref.external = 0;
28885 return 0;
28888 dw_attr_node *av = get_AT (die, DW_AT_location);
28889 dw_loc_list_ref d;
28890 bool non_dwarf_expr = false;
28892 if (av == NULL)
28893 return dwarf_strict ? -1 : 0;
28894 switch (AT_class (av))
28896 case dw_val_class_loc_list:
28897 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28898 if (d->expr && non_dwarf_expression (d->expr))
28899 non_dwarf_expr = true;
28900 break;
28901 case dw_val_class_loc:
28902 lv = AT_loc (av);
28903 if (lv == NULL)
28904 return dwarf_strict ? -1 : 0;
28905 if (non_dwarf_expression (lv))
28906 non_dwarf_expr = true;
28907 break;
28908 default:
28909 return dwarf_strict ? -1 : 0;
28912 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28913 into DW_OP_call4 or DW_OP_GNU_variable_value into
28914 DW_OP_call4 DW_OP_deref, do so. */
28915 if (!non_dwarf_expr
28916 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28918 l->dw_loc_opc = DW_OP_call4;
28919 if (l->dw_loc_next)
28920 l->dw_loc_next = NULL;
28921 else
28922 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28923 return 0;
28926 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28927 copy over the DW_AT_location attribute from die to a. */
28928 if (l->dw_loc_next != NULL)
28930 a->dw_attr_val = av->dw_attr_val;
28931 return 1;
28934 dw_loc_list_ref list, *p;
28935 switch (AT_class (av))
28937 case dw_val_class_loc_list:
28938 p = &list;
28939 list = NULL;
28940 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28942 lv = copy_deref_exprloc (d->expr);
28943 if (lv)
28945 *p = new_loc_list (lv, d->begin, d->end, d->section);
28946 p = &(*p)->dw_loc_next;
28948 else if (!dwarf_strict && d->expr)
28949 return 0;
28951 if (list == NULL)
28952 return dwarf_strict ? -1 : 0;
28953 a->dw_attr_val.val_class = dw_val_class_loc_list;
28954 gen_llsym (list);
28955 *AT_loc_list_ptr (a) = list;
28956 return 1;
28957 case dw_val_class_loc:
28958 lv = copy_deref_exprloc (AT_loc (av));
28959 if (lv == NULL)
28960 return dwarf_strict ? -1 : 0;
28961 a->dw_attr_val.v.val_loc = lv;
28962 return 1;
28963 default:
28964 gcc_unreachable ();
28968 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28969 an address in .rodata section if the string literal is emitted there,
28970 or remove the containing location list or replace DW_AT_const_value
28971 with DW_AT_location and empty location expression, if it isn't found
28972 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28973 to something that has been emitted in the current CU. */
28975 static void
28976 resolve_addr (dw_die_ref die)
28978 dw_die_ref c;
28979 dw_attr_node *a;
28980 dw_loc_list_ref *curr, *start, loc;
28981 unsigned ix;
28982 bool remove_AT_byte_size = false;
28984 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28985 switch (AT_class (a))
28987 case dw_val_class_loc_list:
28988 start = curr = AT_loc_list_ptr (a);
28989 loc = *curr;
28990 gcc_assert (loc);
28991 /* The same list can be referenced more than once. See if we have
28992 already recorded the result from a previous pass. */
28993 if (loc->replaced)
28994 *curr = loc->dw_loc_next;
28995 else if (!loc->resolved_addr)
28997 /* As things stand, we do not expect or allow one die to
28998 reference a suffix of another die's location list chain.
28999 References must be identical or completely separate.
29000 There is therefore no need to cache the result of this
29001 pass on any list other than the first; doing so
29002 would lead to unnecessary writes. */
29003 while (*curr)
29005 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29006 if (!resolve_addr_in_expr (a, (*curr)->expr))
29008 dw_loc_list_ref next = (*curr)->dw_loc_next;
29009 dw_loc_descr_ref l = (*curr)->expr;
29011 if (next && (*curr)->ll_symbol)
29013 gcc_assert (!next->ll_symbol);
29014 next->ll_symbol = (*curr)->ll_symbol;
29016 if (dwarf_split_debug_info)
29017 remove_loc_list_addr_table_entries (l);
29018 *curr = next;
29020 else
29022 mark_base_types ((*curr)->expr);
29023 curr = &(*curr)->dw_loc_next;
29026 if (loc == *start)
29027 loc->resolved_addr = 1;
29028 else
29030 loc->replaced = 1;
29031 loc->dw_loc_next = *start;
29034 if (!*start)
29036 remove_AT (die, a->dw_attr);
29037 ix--;
29039 break;
29040 case dw_val_class_loc:
29042 dw_loc_descr_ref l = AT_loc (a);
29043 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29044 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29045 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29046 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29047 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29048 with DW_FORM_ref referencing the same DIE as
29049 DW_OP_GNU_variable_value used to reference. */
29050 if (a->dw_attr == DW_AT_string_length
29051 && l
29052 && l->dw_loc_opc == DW_OP_GNU_variable_value
29053 && (l->dw_loc_next == NULL
29054 || (l->dw_loc_next->dw_loc_next == NULL
29055 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29057 switch (optimize_string_length (a))
29059 case -1:
29060 remove_AT (die, a->dw_attr);
29061 ix--;
29062 /* If we drop DW_AT_string_length, we need to drop also
29063 DW_AT_{string_length_,}byte_size. */
29064 remove_AT_byte_size = true;
29065 continue;
29066 default:
29067 break;
29068 case 1:
29069 /* Even if we keep the optimized DW_AT_string_length,
29070 it might have changed AT_class, so process it again. */
29071 ix--;
29072 continue;
29075 /* For -gdwarf-2 don't attempt to optimize
29076 DW_AT_data_member_location containing
29077 DW_OP_plus_uconst - older consumers might
29078 rely on it being that op instead of a more complex,
29079 but shorter, location description. */
29080 if ((dwarf_version > 2
29081 || a->dw_attr != DW_AT_data_member_location
29082 || l == NULL
29083 || l->dw_loc_opc != DW_OP_plus_uconst
29084 || l->dw_loc_next != NULL)
29085 && !resolve_addr_in_expr (a, l))
29087 if (dwarf_split_debug_info)
29088 remove_loc_list_addr_table_entries (l);
29089 if (l != NULL
29090 && l->dw_loc_next == NULL
29091 && l->dw_loc_opc == DW_OP_addr
29092 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29093 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29094 && a->dw_attr == DW_AT_location)
29096 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29097 remove_AT (die, a->dw_attr);
29098 ix--;
29099 optimize_location_into_implicit_ptr (die, decl);
29100 break;
29102 if (a->dw_attr == DW_AT_string_length)
29103 /* If we drop DW_AT_string_length, we need to drop also
29104 DW_AT_{string_length_,}byte_size. */
29105 remove_AT_byte_size = true;
29106 remove_AT (die, a->dw_attr);
29107 ix--;
29109 else
29110 mark_base_types (l);
29112 break;
29113 case dw_val_class_addr:
29114 if (a->dw_attr == DW_AT_const_value
29115 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29117 if (AT_index (a) != NOT_INDEXED)
29118 remove_addr_table_entry (a->dw_attr_val.val_entry);
29119 remove_AT (die, a->dw_attr);
29120 ix--;
29122 if ((die->die_tag == DW_TAG_call_site
29123 && a->dw_attr == DW_AT_call_origin)
29124 || (die->die_tag == DW_TAG_GNU_call_site
29125 && a->dw_attr == DW_AT_abstract_origin))
29127 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29128 dw_die_ref tdie = lookup_decl_die (tdecl);
29129 dw_die_ref cdie;
29130 if (tdie == NULL
29131 && DECL_EXTERNAL (tdecl)
29132 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29133 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29135 dw_die_ref pdie = cdie;
29136 /* Make sure we don't add these DIEs into type units.
29137 We could emit skeleton DIEs for context (namespaces,
29138 outer structs/classes) and a skeleton DIE for the
29139 innermost context with DW_AT_signature pointing to the
29140 type unit. See PR78835. */
29141 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29142 pdie = pdie->die_parent;
29143 if (pdie == NULL)
29145 /* Creating a full DIE for tdecl is overly expensive and
29146 at this point even wrong when in the LTO phase
29147 as it can end up generating new type DIEs we didn't
29148 output and thus optimize_external_refs will crash. */
29149 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29150 add_AT_flag (tdie, DW_AT_external, 1);
29151 add_AT_flag (tdie, DW_AT_declaration, 1);
29152 add_linkage_attr (tdie, tdecl);
29153 add_name_and_src_coords_attributes (tdie, tdecl, true);
29154 equate_decl_number_to_die (tdecl, tdie);
29157 if (tdie)
29159 a->dw_attr_val.val_class = dw_val_class_die_ref;
29160 a->dw_attr_val.v.val_die_ref.die = tdie;
29161 a->dw_attr_val.v.val_die_ref.external = 0;
29163 else
29165 if (AT_index (a) != NOT_INDEXED)
29166 remove_addr_table_entry (a->dw_attr_val.val_entry);
29167 remove_AT (die, a->dw_attr);
29168 ix--;
29171 break;
29172 default:
29173 break;
29176 if (remove_AT_byte_size)
29177 remove_AT (die, dwarf_version >= 5
29178 ? DW_AT_string_length_byte_size
29179 : DW_AT_byte_size);
29181 FOR_EACH_CHILD (die, c, resolve_addr (c));
29184 /* Helper routines for optimize_location_lists.
29185 This pass tries to share identical local lists in .debug_loc
29186 section. */
29188 /* Iteratively hash operands of LOC opcode into HSTATE. */
29190 static void
29191 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29193 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29194 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29196 switch (loc->dw_loc_opc)
29198 case DW_OP_const4u:
29199 case DW_OP_const8u:
29200 if (loc->dtprel)
29201 goto hash_addr;
29202 /* FALLTHRU */
29203 case DW_OP_const1u:
29204 case DW_OP_const1s:
29205 case DW_OP_const2u:
29206 case DW_OP_const2s:
29207 case DW_OP_const4s:
29208 case DW_OP_const8s:
29209 case DW_OP_constu:
29210 case DW_OP_consts:
29211 case DW_OP_pick:
29212 case DW_OP_plus_uconst:
29213 case DW_OP_breg0:
29214 case DW_OP_breg1:
29215 case DW_OP_breg2:
29216 case DW_OP_breg3:
29217 case DW_OP_breg4:
29218 case DW_OP_breg5:
29219 case DW_OP_breg6:
29220 case DW_OP_breg7:
29221 case DW_OP_breg8:
29222 case DW_OP_breg9:
29223 case DW_OP_breg10:
29224 case DW_OP_breg11:
29225 case DW_OP_breg12:
29226 case DW_OP_breg13:
29227 case DW_OP_breg14:
29228 case DW_OP_breg15:
29229 case DW_OP_breg16:
29230 case DW_OP_breg17:
29231 case DW_OP_breg18:
29232 case DW_OP_breg19:
29233 case DW_OP_breg20:
29234 case DW_OP_breg21:
29235 case DW_OP_breg22:
29236 case DW_OP_breg23:
29237 case DW_OP_breg24:
29238 case DW_OP_breg25:
29239 case DW_OP_breg26:
29240 case DW_OP_breg27:
29241 case DW_OP_breg28:
29242 case DW_OP_breg29:
29243 case DW_OP_breg30:
29244 case DW_OP_breg31:
29245 case DW_OP_regx:
29246 case DW_OP_fbreg:
29247 case DW_OP_piece:
29248 case DW_OP_deref_size:
29249 case DW_OP_xderef_size:
29250 hstate.add_object (val1->v.val_int);
29251 break;
29252 case DW_OP_skip:
29253 case DW_OP_bra:
29255 int offset;
29257 gcc_assert (val1->val_class == dw_val_class_loc);
29258 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29259 hstate.add_object (offset);
29261 break;
29262 case DW_OP_implicit_value:
29263 hstate.add_object (val1->v.val_unsigned);
29264 switch (val2->val_class)
29266 case dw_val_class_const:
29267 hstate.add_object (val2->v.val_int);
29268 break;
29269 case dw_val_class_vec:
29271 unsigned int elt_size = val2->v.val_vec.elt_size;
29272 unsigned int len = val2->v.val_vec.length;
29274 hstate.add_int (elt_size);
29275 hstate.add_int (len);
29276 hstate.add (val2->v.val_vec.array, len * elt_size);
29278 break;
29279 case dw_val_class_const_double:
29280 hstate.add_object (val2->v.val_double.low);
29281 hstate.add_object (val2->v.val_double.high);
29282 break;
29283 case dw_val_class_wide_int:
29284 hstate.add (val2->v.val_wide->get_val (),
29285 get_full_len (*val2->v.val_wide)
29286 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29287 break;
29288 case dw_val_class_addr:
29289 inchash::add_rtx (val2->v.val_addr, hstate);
29290 break;
29291 default:
29292 gcc_unreachable ();
29294 break;
29295 case DW_OP_bregx:
29296 case DW_OP_bit_piece:
29297 hstate.add_object (val1->v.val_int);
29298 hstate.add_object (val2->v.val_int);
29299 break;
29300 case DW_OP_addr:
29301 hash_addr:
29302 if (loc->dtprel)
29304 unsigned char dtprel = 0xd1;
29305 hstate.add_object (dtprel);
29307 inchash::add_rtx (val1->v.val_addr, hstate);
29308 break;
29309 case DW_OP_GNU_addr_index:
29310 case DW_OP_GNU_const_index:
29312 if (loc->dtprel)
29314 unsigned char dtprel = 0xd1;
29315 hstate.add_object (dtprel);
29317 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29319 break;
29320 case DW_OP_implicit_pointer:
29321 case DW_OP_GNU_implicit_pointer:
29322 hstate.add_int (val2->v.val_int);
29323 break;
29324 case DW_OP_entry_value:
29325 case DW_OP_GNU_entry_value:
29326 hstate.add_object (val1->v.val_loc);
29327 break;
29328 case DW_OP_regval_type:
29329 case DW_OP_deref_type:
29330 case DW_OP_GNU_regval_type:
29331 case DW_OP_GNU_deref_type:
29333 unsigned int byte_size
29334 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29335 unsigned int encoding
29336 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29337 hstate.add_object (val1->v.val_int);
29338 hstate.add_object (byte_size);
29339 hstate.add_object (encoding);
29341 break;
29342 case DW_OP_convert:
29343 case DW_OP_reinterpret:
29344 case DW_OP_GNU_convert:
29345 case DW_OP_GNU_reinterpret:
29346 if (val1->val_class == dw_val_class_unsigned_const)
29348 hstate.add_object (val1->v.val_unsigned);
29349 break;
29351 /* FALLTHRU */
29352 case DW_OP_const_type:
29353 case DW_OP_GNU_const_type:
29355 unsigned int byte_size
29356 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29357 unsigned int encoding
29358 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29359 hstate.add_object (byte_size);
29360 hstate.add_object (encoding);
29361 if (loc->dw_loc_opc != DW_OP_const_type
29362 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29363 break;
29364 hstate.add_object (val2->val_class);
29365 switch (val2->val_class)
29367 case dw_val_class_const:
29368 hstate.add_object (val2->v.val_int);
29369 break;
29370 case dw_val_class_vec:
29372 unsigned int elt_size = val2->v.val_vec.elt_size;
29373 unsigned int len = val2->v.val_vec.length;
29375 hstate.add_object (elt_size);
29376 hstate.add_object (len);
29377 hstate.add (val2->v.val_vec.array, len * elt_size);
29379 break;
29380 case dw_val_class_const_double:
29381 hstate.add_object (val2->v.val_double.low);
29382 hstate.add_object (val2->v.val_double.high);
29383 break;
29384 case dw_val_class_wide_int:
29385 hstate.add (val2->v.val_wide->get_val (),
29386 get_full_len (*val2->v.val_wide)
29387 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29388 break;
29389 default:
29390 gcc_unreachable ();
29393 break;
29395 default:
29396 /* Other codes have no operands. */
29397 break;
29401 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29403 static inline void
29404 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29406 dw_loc_descr_ref l;
29407 bool sizes_computed = false;
29408 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29409 size_of_locs (loc);
29411 for (l = loc; l != NULL; l = l->dw_loc_next)
29413 enum dwarf_location_atom opc = l->dw_loc_opc;
29414 hstate.add_object (opc);
29415 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29417 size_of_locs (loc);
29418 sizes_computed = true;
29420 hash_loc_operands (l, hstate);
29424 /* Compute hash of the whole location list LIST_HEAD. */
29426 static inline void
29427 hash_loc_list (dw_loc_list_ref list_head)
29429 dw_loc_list_ref curr = list_head;
29430 inchash::hash hstate;
29432 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29434 hstate.add (curr->begin, strlen (curr->begin) + 1);
29435 hstate.add (curr->end, strlen (curr->end) + 1);
29436 if (curr->section)
29437 hstate.add (curr->section, strlen (curr->section) + 1);
29438 hash_locs (curr->expr, hstate);
29440 list_head->hash = hstate.end ();
29443 /* Return true if X and Y opcodes have the same operands. */
29445 static inline bool
29446 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29448 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29449 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29450 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29451 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29453 switch (x->dw_loc_opc)
29455 case DW_OP_const4u:
29456 case DW_OP_const8u:
29457 if (x->dtprel)
29458 goto hash_addr;
29459 /* FALLTHRU */
29460 case DW_OP_const1u:
29461 case DW_OP_const1s:
29462 case DW_OP_const2u:
29463 case DW_OP_const2s:
29464 case DW_OP_const4s:
29465 case DW_OP_const8s:
29466 case DW_OP_constu:
29467 case DW_OP_consts:
29468 case DW_OP_pick:
29469 case DW_OP_plus_uconst:
29470 case DW_OP_breg0:
29471 case DW_OP_breg1:
29472 case DW_OP_breg2:
29473 case DW_OP_breg3:
29474 case DW_OP_breg4:
29475 case DW_OP_breg5:
29476 case DW_OP_breg6:
29477 case DW_OP_breg7:
29478 case DW_OP_breg8:
29479 case DW_OP_breg9:
29480 case DW_OP_breg10:
29481 case DW_OP_breg11:
29482 case DW_OP_breg12:
29483 case DW_OP_breg13:
29484 case DW_OP_breg14:
29485 case DW_OP_breg15:
29486 case DW_OP_breg16:
29487 case DW_OP_breg17:
29488 case DW_OP_breg18:
29489 case DW_OP_breg19:
29490 case DW_OP_breg20:
29491 case DW_OP_breg21:
29492 case DW_OP_breg22:
29493 case DW_OP_breg23:
29494 case DW_OP_breg24:
29495 case DW_OP_breg25:
29496 case DW_OP_breg26:
29497 case DW_OP_breg27:
29498 case DW_OP_breg28:
29499 case DW_OP_breg29:
29500 case DW_OP_breg30:
29501 case DW_OP_breg31:
29502 case DW_OP_regx:
29503 case DW_OP_fbreg:
29504 case DW_OP_piece:
29505 case DW_OP_deref_size:
29506 case DW_OP_xderef_size:
29507 return valx1->v.val_int == valy1->v.val_int;
29508 case DW_OP_skip:
29509 case DW_OP_bra:
29510 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29511 can cause irrelevant differences in dw_loc_addr. */
29512 gcc_assert (valx1->val_class == dw_val_class_loc
29513 && valy1->val_class == dw_val_class_loc
29514 && (dwarf_split_debug_info
29515 || x->dw_loc_addr == y->dw_loc_addr));
29516 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29517 case DW_OP_implicit_value:
29518 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29519 || valx2->val_class != valy2->val_class)
29520 return false;
29521 switch (valx2->val_class)
29523 case dw_val_class_const:
29524 return valx2->v.val_int == valy2->v.val_int;
29525 case dw_val_class_vec:
29526 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29527 && valx2->v.val_vec.length == valy2->v.val_vec.length
29528 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29529 valx2->v.val_vec.elt_size
29530 * valx2->v.val_vec.length) == 0;
29531 case dw_val_class_const_double:
29532 return valx2->v.val_double.low == valy2->v.val_double.low
29533 && valx2->v.val_double.high == valy2->v.val_double.high;
29534 case dw_val_class_wide_int:
29535 return *valx2->v.val_wide == *valy2->v.val_wide;
29536 case dw_val_class_addr:
29537 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29538 default:
29539 gcc_unreachable ();
29541 case DW_OP_bregx:
29542 case DW_OP_bit_piece:
29543 return valx1->v.val_int == valy1->v.val_int
29544 && valx2->v.val_int == valy2->v.val_int;
29545 case DW_OP_addr:
29546 hash_addr:
29547 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29548 case DW_OP_GNU_addr_index:
29549 case DW_OP_GNU_const_index:
29551 rtx ax1 = valx1->val_entry->addr.rtl;
29552 rtx ay1 = valy1->val_entry->addr.rtl;
29553 return rtx_equal_p (ax1, ay1);
29555 case DW_OP_implicit_pointer:
29556 case DW_OP_GNU_implicit_pointer:
29557 return valx1->val_class == dw_val_class_die_ref
29558 && valx1->val_class == valy1->val_class
29559 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29560 && valx2->v.val_int == valy2->v.val_int;
29561 case DW_OP_entry_value:
29562 case DW_OP_GNU_entry_value:
29563 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29564 case DW_OP_const_type:
29565 case DW_OP_GNU_const_type:
29566 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29567 || valx2->val_class != valy2->val_class)
29568 return false;
29569 switch (valx2->val_class)
29571 case dw_val_class_const:
29572 return valx2->v.val_int == valy2->v.val_int;
29573 case dw_val_class_vec:
29574 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29575 && valx2->v.val_vec.length == valy2->v.val_vec.length
29576 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29577 valx2->v.val_vec.elt_size
29578 * valx2->v.val_vec.length) == 0;
29579 case dw_val_class_const_double:
29580 return valx2->v.val_double.low == valy2->v.val_double.low
29581 && valx2->v.val_double.high == valy2->v.val_double.high;
29582 case dw_val_class_wide_int:
29583 return *valx2->v.val_wide == *valy2->v.val_wide;
29584 default:
29585 gcc_unreachable ();
29587 case DW_OP_regval_type:
29588 case DW_OP_deref_type:
29589 case DW_OP_GNU_regval_type:
29590 case DW_OP_GNU_deref_type:
29591 return valx1->v.val_int == valy1->v.val_int
29592 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29593 case DW_OP_convert:
29594 case DW_OP_reinterpret:
29595 case DW_OP_GNU_convert:
29596 case DW_OP_GNU_reinterpret:
29597 if (valx1->val_class != valy1->val_class)
29598 return false;
29599 if (valx1->val_class == dw_val_class_unsigned_const)
29600 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29601 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29602 case DW_OP_GNU_parameter_ref:
29603 return valx1->val_class == dw_val_class_die_ref
29604 && valx1->val_class == valy1->val_class
29605 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29606 default:
29607 /* Other codes have no operands. */
29608 return true;
29612 /* Return true if DWARF location expressions X and Y are the same. */
29614 static inline bool
29615 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29617 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29618 if (x->dw_loc_opc != y->dw_loc_opc
29619 || x->dtprel != y->dtprel
29620 || !compare_loc_operands (x, y))
29621 break;
29622 return x == NULL && y == NULL;
29625 /* Hashtable helpers. */
29627 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29629 static inline hashval_t hash (const dw_loc_list_struct *);
29630 static inline bool equal (const dw_loc_list_struct *,
29631 const dw_loc_list_struct *);
29634 /* Return precomputed hash of location list X. */
29636 inline hashval_t
29637 loc_list_hasher::hash (const dw_loc_list_struct *x)
29639 return x->hash;
29642 /* Return true if location lists A and B are the same. */
29644 inline bool
29645 loc_list_hasher::equal (const dw_loc_list_struct *a,
29646 const dw_loc_list_struct *b)
29648 if (a == b)
29649 return 1;
29650 if (a->hash != b->hash)
29651 return 0;
29652 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29653 if (strcmp (a->begin, b->begin) != 0
29654 || strcmp (a->end, b->end) != 0
29655 || (a->section == NULL) != (b->section == NULL)
29656 || (a->section && strcmp (a->section, b->section) != 0)
29657 || !compare_locs (a->expr, b->expr))
29658 break;
29659 return a == NULL && b == NULL;
29662 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29665 /* Recursively optimize location lists referenced from DIE
29666 children and share them whenever possible. */
29668 static void
29669 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29671 dw_die_ref c;
29672 dw_attr_node *a;
29673 unsigned ix;
29674 dw_loc_list_struct **slot;
29676 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29677 if (AT_class (a) == dw_val_class_loc_list)
29679 dw_loc_list_ref list = AT_loc_list (a);
29680 /* TODO: perform some optimizations here, before hashing
29681 it and storing into the hash table. */
29682 hash_loc_list (list);
29683 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29684 if (*slot == NULL)
29685 *slot = list;
29686 else
29687 a->dw_attr_val.v.val_loc_list = *slot;
29690 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29694 /* Recursively assign each location list a unique index into the debug_addr
29695 section. */
29697 static void
29698 index_location_lists (dw_die_ref die)
29700 dw_die_ref c;
29701 dw_attr_node *a;
29702 unsigned ix;
29704 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29705 if (AT_class (a) == dw_val_class_loc_list)
29707 dw_loc_list_ref list = AT_loc_list (a);
29708 dw_loc_list_ref curr;
29709 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29711 /* Don't index an entry that has already been indexed
29712 or won't be output. */
29713 if (curr->begin_entry != NULL
29714 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29715 continue;
29717 curr->begin_entry
29718 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29722 FOR_EACH_CHILD (die, c, index_location_lists (c));
29725 /* Optimize location lists referenced from DIE
29726 children and share them whenever possible. */
29728 static void
29729 optimize_location_lists (dw_die_ref die)
29731 loc_list_hash_type htab (500);
29732 optimize_location_lists_1 (die, &htab);
29735 /* Traverse the limbo die list, and add parent/child links. The only
29736 dies without parents that should be here are concrete instances of
29737 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29738 For concrete instances, we can get the parent die from the abstract
29739 instance. */
29741 static void
29742 flush_limbo_die_list (void)
29744 limbo_die_node *node;
29746 /* get_context_die calls force_decl_die, which can put new DIEs on the
29747 limbo list in LTO mode when nested functions are put in a different
29748 partition than that of their parent function. */
29749 while ((node = limbo_die_list))
29751 dw_die_ref die = node->die;
29752 limbo_die_list = node->next;
29754 if (die->die_parent == NULL)
29756 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29758 if (origin && origin->die_parent)
29759 add_child_die (origin->die_parent, die);
29760 else if (is_cu_die (die))
29762 else if (seen_error ())
29763 /* It's OK to be confused by errors in the input. */
29764 add_child_die (comp_unit_die (), die);
29765 else
29767 /* In certain situations, the lexical block containing a
29768 nested function can be optimized away, which results
29769 in the nested function die being orphaned. Likewise
29770 with the return type of that nested function. Force
29771 this to be a child of the containing function.
29773 It may happen that even the containing function got fully
29774 inlined and optimized out. In that case we are lost and
29775 assign the empty child. This should not be big issue as
29776 the function is likely unreachable too. */
29777 gcc_assert (node->created_for);
29779 if (DECL_P (node->created_for))
29780 origin = get_context_die (DECL_CONTEXT (node->created_for));
29781 else if (TYPE_P (node->created_for))
29782 origin = scope_die_for (node->created_for, comp_unit_die ());
29783 else
29784 origin = comp_unit_die ();
29786 add_child_die (origin, die);
29792 /* Reset DIEs so we can output them again. */
29794 static void
29795 reset_dies (dw_die_ref die)
29797 dw_die_ref c;
29799 /* Remove stuff we re-generate. */
29800 die->die_mark = 0;
29801 die->die_offset = 0;
29802 die->die_abbrev = 0;
29803 remove_AT (die, DW_AT_sibling);
29805 FOR_EACH_CHILD (die, c, reset_dies (c));
29808 /* Output stuff that dwarf requires at the end of every file,
29809 and generate the DWARF-2 debugging info. */
29811 static void
29812 dwarf2out_finish (const char *)
29814 comdat_type_node *ctnode;
29815 dw_die_ref main_comp_unit_die;
29816 unsigned char checksum[16];
29817 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29819 /* Flush out any latecomers to the limbo party. */
29820 flush_limbo_die_list ();
29822 if (flag_checking)
29824 verify_die (comp_unit_die ());
29825 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29826 verify_die (node->die);
29829 /* We shouldn't have any symbols with delayed asm names for
29830 DIEs generated after early finish. */
29831 gcc_assert (deferred_asm_name == NULL);
29833 gen_remaining_tmpl_value_param_die_attribute ();
29835 if (flag_generate_lto || flag_generate_offload)
29837 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29839 /* Prune stuff so that dwarf2out_finish runs successfully
29840 for the fat part of the object. */
29841 reset_dies (comp_unit_die ());
29842 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29843 reset_dies (node->die);
29845 hash_table<comdat_type_hasher> comdat_type_table (100);
29846 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29848 comdat_type_node **slot
29849 = comdat_type_table.find_slot (ctnode, INSERT);
29851 /* Don't reset types twice. */
29852 if (*slot != HTAB_EMPTY_ENTRY)
29853 continue;
29855 /* Add a pointer to the line table for the main compilation unit
29856 so that the debugger can make sense of DW_AT_decl_file
29857 attributes. */
29858 if (debug_info_level >= DINFO_LEVEL_TERSE)
29859 reset_dies (ctnode->root_die);
29861 *slot = ctnode;
29864 /* Reset die CU symbol so we don't output it twice. */
29865 comp_unit_die ()->die_id.die_symbol = NULL;
29867 /* Remove DW_AT_macro from the early output. */
29868 if (have_macinfo)
29869 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29871 /* Remove indirect string decisions. */
29872 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29875 #if ENABLE_ASSERT_CHECKING
29877 dw_die_ref die = comp_unit_die (), c;
29878 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29880 #endif
29881 resolve_addr (comp_unit_die ());
29882 move_marked_base_types ();
29884 /* Initialize sections and labels used for actual assembler output. */
29885 init_sections_and_labels (false);
29887 /* Traverse the DIE's and add sibling attributes to those DIE's that
29888 have children. */
29889 add_sibling_attributes (comp_unit_die ());
29890 limbo_die_node *node;
29891 for (node = cu_die_list; node; node = node->next)
29892 add_sibling_attributes (node->die);
29893 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29894 add_sibling_attributes (ctnode->root_die);
29896 /* When splitting DWARF info, we put some attributes in the
29897 skeleton compile_unit DIE that remains in the .o, while
29898 most attributes go in the DWO compile_unit_die. */
29899 if (dwarf_split_debug_info)
29901 limbo_die_node *cu;
29902 main_comp_unit_die = gen_compile_unit_die (NULL);
29903 if (dwarf_version >= 5)
29904 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29905 cu = limbo_die_list;
29906 gcc_assert (cu->die == main_comp_unit_die);
29907 limbo_die_list = limbo_die_list->next;
29908 cu->next = cu_die_list;
29909 cu_die_list = cu;
29911 else
29912 main_comp_unit_die = comp_unit_die ();
29914 /* Output a terminator label for the .text section. */
29915 switch_to_section (text_section);
29916 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29917 if (cold_text_section)
29919 switch_to_section (cold_text_section);
29920 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29923 /* We can only use the low/high_pc attributes if all of the code was
29924 in .text. */
29925 if (!have_multiple_function_sections
29926 || (dwarf_version < 3 && dwarf_strict))
29928 /* Don't add if the CU has no associated code. */
29929 if (text_section_used)
29930 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29931 text_end_label, true);
29933 else
29935 unsigned fde_idx;
29936 dw_fde_ref fde;
29937 bool range_list_added = false;
29939 if (text_section_used)
29940 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29941 text_end_label, &range_list_added, true);
29942 if (cold_text_section_used)
29943 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29944 cold_end_label, &range_list_added, true);
29946 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29948 if (DECL_IGNORED_P (fde->decl))
29949 continue;
29950 if (!fde->in_std_section)
29951 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29952 fde->dw_fde_end, &range_list_added,
29953 true);
29954 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29955 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29956 fde->dw_fde_second_end, &range_list_added,
29957 true);
29960 if (range_list_added)
29962 /* We need to give .debug_loc and .debug_ranges an appropriate
29963 "base address". Use zero so that these addresses become
29964 absolute. Historically, we've emitted the unexpected
29965 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29966 Emit both to give time for other tools to adapt. */
29967 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29968 if (! dwarf_strict && dwarf_version < 4)
29969 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29971 add_ranges (NULL);
29975 /* AIX Assembler inserts the length, so adjust the reference to match the
29976 offset expected by debuggers. */
29977 strcpy (dl_section_ref, debug_line_section_label);
29978 if (XCOFF_DEBUGGING_INFO)
29979 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29981 if (debug_info_level >= DINFO_LEVEL_TERSE)
29982 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29983 dl_section_ref);
29985 if (have_macinfo)
29986 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29987 macinfo_section_label);
29989 if (dwarf_split_debug_info)
29991 if (have_location_lists)
29993 if (dwarf_version >= 5)
29994 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29995 loc_section_label);
29996 /* optimize_location_lists calculates the size of the lists,
29997 so index them first, and assign indices to the entries.
29998 Although optimize_location_lists will remove entries from
29999 the table, it only does so for duplicates, and therefore
30000 only reduces ref_counts to 1. */
30001 index_location_lists (comp_unit_die ());
30004 if (addr_index_table != NULL)
30006 unsigned int index = 0;
30007 addr_index_table
30008 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30009 (&index);
30013 loc_list_idx = 0;
30014 if (have_location_lists)
30016 optimize_location_lists (comp_unit_die ());
30017 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30018 if (dwarf_version >= 5 && dwarf_split_debug_info)
30019 assign_location_list_indexes (comp_unit_die ());
30022 save_macinfo_strings ();
30024 if (dwarf_split_debug_info)
30026 unsigned int index = 0;
30028 /* Add attributes common to skeleton compile_units and
30029 type_units. Because these attributes include strings, it
30030 must be done before freezing the string table. Top-level
30031 skeleton die attrs are added when the skeleton type unit is
30032 created, so ensure it is created by this point. */
30033 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30034 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30037 /* Output all of the compilation units. We put the main one last so that
30038 the offsets are available to output_pubnames. */
30039 for (node = cu_die_list; node; node = node->next)
30040 output_comp_unit (node->die, 0, NULL);
30042 hash_table<comdat_type_hasher> comdat_type_table (100);
30043 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30045 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30047 /* Don't output duplicate types. */
30048 if (*slot != HTAB_EMPTY_ENTRY)
30049 continue;
30051 /* Add a pointer to the line table for the main compilation unit
30052 so that the debugger can make sense of DW_AT_decl_file
30053 attributes. */
30054 if (debug_info_level >= DINFO_LEVEL_TERSE)
30055 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30056 (!dwarf_split_debug_info
30057 ? dl_section_ref
30058 : debug_skeleton_line_section_label));
30060 output_comdat_type_unit (ctnode);
30061 *slot = ctnode;
30064 if (dwarf_split_debug_info)
30066 int mark;
30067 struct md5_ctx ctx;
30069 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30070 index_rnglists ();
30072 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30073 md5_init_ctx (&ctx);
30074 mark = 0;
30075 die_checksum (comp_unit_die (), &ctx, &mark);
30076 unmark_all_dies (comp_unit_die ());
30077 md5_finish_ctx (&ctx, checksum);
30079 if (dwarf_version < 5)
30081 /* Use the first 8 bytes of the checksum as the dwo_id,
30082 and add it to both comp-unit DIEs. */
30083 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30084 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30087 /* Add the base offset of the ranges table to the skeleton
30088 comp-unit DIE. */
30089 if (!vec_safe_is_empty (ranges_table))
30091 if (dwarf_version >= 5)
30092 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30093 ranges_base_label);
30094 else
30095 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30096 ranges_section_label);
30099 switch_to_section (debug_addr_section);
30100 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30101 output_addr_table ();
30104 /* Output the main compilation unit if non-empty or if .debug_macinfo
30105 or .debug_macro will be emitted. */
30106 output_comp_unit (comp_unit_die (), have_macinfo,
30107 dwarf_split_debug_info ? checksum : NULL);
30109 if (dwarf_split_debug_info && info_section_emitted)
30110 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30112 /* Output the abbreviation table. */
30113 if (vec_safe_length (abbrev_die_table) != 1)
30115 switch_to_section (debug_abbrev_section);
30116 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30117 output_abbrev_section ();
30120 /* Output location list section if necessary. */
30121 if (have_location_lists)
30123 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30124 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30125 /* Output the location lists info. */
30126 switch_to_section (debug_loc_section);
30127 if (dwarf_version >= 5)
30129 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30130 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30131 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30132 dw2_asm_output_data (4, 0xffffffff,
30133 "Initial length escape value indicating "
30134 "64-bit DWARF extension");
30135 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30136 "Length of Location Lists");
30137 ASM_OUTPUT_LABEL (asm_out_file, l1);
30138 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30139 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30140 dw2_asm_output_data (1, 0, "Segment Size");
30141 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30142 "Offset Entry Count");
30144 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30145 if (dwarf_version >= 5 && dwarf_split_debug_info)
30147 unsigned int save_loc_list_idx = loc_list_idx;
30148 loc_list_idx = 0;
30149 output_loclists_offsets (comp_unit_die ());
30150 gcc_assert (save_loc_list_idx == loc_list_idx);
30152 output_location_lists (comp_unit_die ());
30153 if (dwarf_version >= 5)
30154 ASM_OUTPUT_LABEL (asm_out_file, l2);
30157 output_pubtables ();
30159 /* Output the address range information if a CU (.debug_info section)
30160 was emitted. We output an empty table even if we had no functions
30161 to put in it. This because the consumer has no way to tell the
30162 difference between an empty table that we omitted and failure to
30163 generate a table that would have contained data. */
30164 if (info_section_emitted)
30166 switch_to_section (debug_aranges_section);
30167 output_aranges ();
30170 /* Output ranges section if necessary. */
30171 if (!vec_safe_is_empty (ranges_table))
30173 if (dwarf_version >= 5)
30174 output_rnglists ();
30175 else
30176 output_ranges ();
30179 /* Have to end the macro section. */
30180 if (have_macinfo)
30182 switch_to_section (debug_macinfo_section);
30183 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30184 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30185 : debug_skeleton_line_section_label, false);
30186 dw2_asm_output_data (1, 0, "End compilation unit");
30189 /* Output the source line correspondence table. We must do this
30190 even if there is no line information. Otherwise, on an empty
30191 translation unit, we will generate a present, but empty,
30192 .debug_info section. IRIX 6.5 `nm' will then complain when
30193 examining the file. This is done late so that any filenames
30194 used by the debug_info section are marked as 'used'. */
30195 switch_to_section (debug_line_section);
30196 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30197 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30198 output_line_info (false);
30200 if (dwarf_split_debug_info && info_section_emitted)
30202 switch_to_section (debug_skeleton_line_section);
30203 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30204 output_line_info (true);
30207 /* If we emitted any indirect strings, output the string table too. */
30208 if (debug_str_hash || skeleton_debug_str_hash)
30209 output_indirect_strings ();
30210 if (debug_line_str_hash)
30212 switch_to_section (debug_line_str_section);
30213 const enum dwarf_form form = DW_FORM_line_strp;
30214 debug_line_str_hash->traverse<enum dwarf_form,
30215 output_indirect_string> (form);
30219 /* Returns a hash value for X (which really is a variable_value_struct). */
30221 inline hashval_t
30222 variable_value_hasher::hash (variable_value_struct *x)
30224 return (hashval_t) x->decl_id;
30227 /* Return nonzero if decl_id of variable_value_struct X is the same as
30228 UID of decl Y. */
30230 inline bool
30231 variable_value_hasher::equal (variable_value_struct *x, tree y)
30233 return x->decl_id == DECL_UID (y);
30236 /* Helper function for resolve_variable_value, handle
30237 DW_OP_GNU_variable_value in one location expression.
30238 Return true if exprloc has been changed into loclist. */
30240 static bool
30241 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30243 dw_loc_descr_ref next;
30244 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30246 next = loc->dw_loc_next;
30247 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30248 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30249 continue;
30251 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30252 if (DECL_CONTEXT (decl) != current_function_decl)
30253 continue;
30255 dw_die_ref ref = lookup_decl_die (decl);
30256 if (ref)
30258 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30259 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30260 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30261 continue;
30263 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30264 if (l == NULL)
30265 continue;
30266 if (l->dw_loc_next)
30268 if (AT_class (a) != dw_val_class_loc)
30269 continue;
30270 switch (a->dw_attr)
30272 /* Following attributes allow both exprloc and loclist
30273 classes, so we can change them into a loclist. */
30274 case DW_AT_location:
30275 case DW_AT_string_length:
30276 case DW_AT_return_addr:
30277 case DW_AT_data_member_location:
30278 case DW_AT_frame_base:
30279 case DW_AT_segment:
30280 case DW_AT_static_link:
30281 case DW_AT_use_location:
30282 case DW_AT_vtable_elem_location:
30283 if (prev)
30285 prev->dw_loc_next = NULL;
30286 prepend_loc_descr_to_each (l, AT_loc (a));
30288 if (next)
30289 add_loc_descr_to_each (l, next);
30290 a->dw_attr_val.val_class = dw_val_class_loc_list;
30291 a->dw_attr_val.val_entry = NULL;
30292 a->dw_attr_val.v.val_loc_list = l;
30293 have_location_lists = true;
30294 return true;
30295 /* Following attributes allow both exprloc and reference,
30296 so if the whole expression is DW_OP_GNU_variable_value alone
30297 we could transform it into reference. */
30298 case DW_AT_byte_size:
30299 case DW_AT_bit_size:
30300 case DW_AT_lower_bound:
30301 case DW_AT_upper_bound:
30302 case DW_AT_bit_stride:
30303 case DW_AT_count:
30304 case DW_AT_allocated:
30305 case DW_AT_associated:
30306 case DW_AT_byte_stride:
30307 if (prev == NULL && next == NULL)
30308 break;
30309 /* FALLTHRU */
30310 default:
30311 if (dwarf_strict)
30312 continue;
30313 break;
30315 /* Create DW_TAG_variable that we can refer to. */
30316 gen_decl_die (decl, NULL_TREE, NULL,
30317 lookup_decl_die (current_function_decl));
30318 ref = lookup_decl_die (decl);
30319 if (ref)
30321 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30322 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30323 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30325 continue;
30327 if (prev)
30329 prev->dw_loc_next = l->expr;
30330 add_loc_descr (&prev->dw_loc_next, next);
30331 free_loc_descr (loc, NULL);
30332 next = prev->dw_loc_next;
30334 else
30336 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30337 add_loc_descr (&loc, next);
30338 next = loc;
30340 loc = prev;
30342 return false;
30345 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30347 static void
30348 resolve_variable_value (dw_die_ref die)
30350 dw_attr_node *a;
30351 dw_loc_list_ref loc;
30352 unsigned ix;
30354 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30355 switch (AT_class (a))
30357 case dw_val_class_loc:
30358 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30359 break;
30360 /* FALLTHRU */
30361 case dw_val_class_loc_list:
30362 loc = AT_loc_list (a);
30363 gcc_assert (loc);
30364 for (; loc; loc = loc->dw_loc_next)
30365 resolve_variable_value_in_expr (a, loc->expr);
30366 break;
30367 default:
30368 break;
30372 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30373 temporaries in the current function. */
30375 static void
30376 resolve_variable_values (void)
30378 if (!variable_value_hash || !current_function_decl)
30379 return;
30381 struct variable_value_struct *node
30382 = variable_value_hash->find_with_hash (current_function_decl,
30383 DECL_UID (current_function_decl));
30385 if (node == NULL)
30386 return;
30388 unsigned int i;
30389 dw_die_ref die;
30390 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30391 resolve_variable_value (die);
30394 /* Helper function for note_variable_value, handle one location
30395 expression. */
30397 static void
30398 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30400 for (; loc; loc = loc->dw_loc_next)
30401 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30402 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30404 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30405 dw_die_ref ref = lookup_decl_die (decl);
30406 if (! ref && (flag_generate_lto || flag_generate_offload))
30408 /* ??? This is somewhat a hack because we do not create DIEs
30409 for variables not in BLOCK trees early but when generating
30410 early LTO output we need the dw_val_class_decl_ref to be
30411 fully resolved. For fat LTO objects we'd also like to
30412 undo this after LTO dwarf output. */
30413 gcc_assert (DECL_CONTEXT (decl));
30414 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30415 gcc_assert (ctx != NULL);
30416 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30417 ref = lookup_decl_die (decl);
30418 gcc_assert (ref != NULL);
30420 if (ref)
30422 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30423 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30424 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30425 continue;
30427 if (VAR_P (decl)
30428 && DECL_CONTEXT (decl)
30429 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30430 && lookup_decl_die (DECL_CONTEXT (decl)))
30432 if (!variable_value_hash)
30433 variable_value_hash
30434 = hash_table<variable_value_hasher>::create_ggc (10);
30436 tree fndecl = DECL_CONTEXT (decl);
30437 struct variable_value_struct *node;
30438 struct variable_value_struct **slot
30439 = variable_value_hash->find_slot_with_hash (fndecl,
30440 DECL_UID (fndecl),
30441 INSERT);
30442 if (*slot == NULL)
30444 node = ggc_cleared_alloc<variable_value_struct> ();
30445 node->decl_id = DECL_UID (fndecl);
30446 *slot = node;
30448 else
30449 node = *slot;
30451 vec_safe_push (node->dies, die);
30456 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30457 with dw_val_class_decl_ref operand. */
30459 static void
30460 note_variable_value (dw_die_ref die)
30462 dw_die_ref c;
30463 dw_attr_node *a;
30464 dw_loc_list_ref loc;
30465 unsigned ix;
30467 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30468 switch (AT_class (a))
30470 case dw_val_class_loc_list:
30471 loc = AT_loc_list (a);
30472 gcc_assert (loc);
30473 if (!loc->noted_variable_value)
30475 loc->noted_variable_value = 1;
30476 for (; loc; loc = loc->dw_loc_next)
30477 note_variable_value_in_expr (die, loc->expr);
30479 break;
30480 case dw_val_class_loc:
30481 note_variable_value_in_expr (die, AT_loc (a));
30482 break;
30483 default:
30484 break;
30487 /* Mark children. */
30488 FOR_EACH_CHILD (die, c, note_variable_value (c));
30491 /* Perform any cleanups needed after the early debug generation pass
30492 has run. */
30494 static void
30495 dwarf2out_early_finish (const char *filename)
30497 set_early_dwarf s;
30499 /* PCH might result in DW_AT_producer string being restored from the
30500 header compilation, so always fill it with empty string initially
30501 and overwrite only here. */
30502 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30503 producer_string = gen_producer_string ();
30504 producer->dw_attr_val.v.val_str->refcount--;
30505 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30507 /* Add the name for the main input file now. We delayed this from
30508 dwarf2out_init to avoid complications with PCH. */
30509 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30510 add_comp_dir_attribute (comp_unit_die ());
30512 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30513 DW_AT_comp_dir into .debug_line_str section. */
30514 if (!DWARF2_ASM_LINE_DEBUG_INFO
30515 && dwarf_version >= 5
30516 && DWARF5_USE_DEBUG_LINE_STR)
30518 for (int i = 0; i < 2; i++)
30520 dw_attr_node *a = get_AT (comp_unit_die (),
30521 i ? DW_AT_comp_dir : DW_AT_name);
30522 if (a == NULL
30523 || AT_class (a) != dw_val_class_str
30524 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30525 continue;
30527 if (! debug_line_str_hash)
30528 debug_line_str_hash
30529 = hash_table<indirect_string_hasher>::create_ggc (10);
30531 struct indirect_string_node *node
30532 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30533 set_indirect_string (node);
30534 node->form = DW_FORM_line_strp;
30535 a->dw_attr_val.v.val_str->refcount--;
30536 a->dw_attr_val.v.val_str = node;
30540 /* With LTO early dwarf was really finished at compile-time, so make
30541 sure to adjust the phase after annotating the LTRANS CU DIE. */
30542 if (in_lto_p)
30544 early_dwarf_finished = true;
30545 return;
30548 /* Walk through the list of incomplete types again, trying once more to
30549 emit full debugging info for them. */
30550 retry_incomplete_types ();
30552 /* The point here is to flush out the limbo list so that it is empty
30553 and we don't need to stream it for LTO. */
30554 flush_limbo_die_list ();
30556 gen_scheduled_generic_parms_dies ();
30557 gen_remaining_tmpl_value_param_die_attribute ();
30559 /* Add DW_AT_linkage_name for all deferred DIEs. */
30560 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30562 tree decl = node->created_for;
30563 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30564 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30565 ended up in deferred_asm_name before we knew it was
30566 constant and never written to disk. */
30567 && DECL_ASSEMBLER_NAME (decl))
30569 add_linkage_attr (node->die, decl);
30570 move_linkage_attr (node->die);
30573 deferred_asm_name = NULL;
30575 if (flag_eliminate_unused_debug_types)
30576 prune_unused_types ();
30578 /* Generate separate COMDAT sections for type DIEs. */
30579 if (use_debug_types)
30581 break_out_comdat_types (comp_unit_die ());
30583 /* Each new type_unit DIE was added to the limbo die list when created.
30584 Since these have all been added to comdat_type_list, clear the
30585 limbo die list. */
30586 limbo_die_list = NULL;
30588 /* For each new comdat type unit, copy declarations for incomplete
30589 types to make the new unit self-contained (i.e., no direct
30590 references to the main compile unit). */
30591 for (comdat_type_node *ctnode = comdat_type_list;
30592 ctnode != NULL; ctnode = ctnode->next)
30593 copy_decls_for_unworthy_types (ctnode->root_die);
30594 copy_decls_for_unworthy_types (comp_unit_die ());
30596 /* In the process of copying declarations from one unit to another,
30597 we may have left some declarations behind that are no longer
30598 referenced. Prune them. */
30599 prune_unused_types ();
30602 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30603 with dw_val_class_decl_ref operand. */
30604 note_variable_value (comp_unit_die ());
30605 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30606 note_variable_value (node->die);
30607 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30608 ctnode = ctnode->next)
30609 note_variable_value (ctnode->root_die);
30610 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30611 note_variable_value (node->die);
30613 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30614 both the main_cu and all skeleton TUs. Making this call unconditional
30615 would end up either adding a second copy of the AT_pubnames attribute, or
30616 requiring a special case in add_top_level_skeleton_die_attrs. */
30617 if (!dwarf_split_debug_info)
30618 add_AT_pubnames (comp_unit_die ());
30620 /* The early debug phase is now finished. */
30621 early_dwarf_finished = true;
30623 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30624 if (!flag_generate_lto && !flag_generate_offload)
30625 return;
30627 /* Now as we are going to output for LTO initialize sections and labels
30628 to the LTO variants. We don't need a random-seed postfix as other
30629 LTO sections as linking the LTO debug sections into one in a partial
30630 link is fine. */
30631 init_sections_and_labels (true);
30633 /* The output below is modeled after dwarf2out_finish with all
30634 location related output removed and some LTO specific changes.
30635 Some refactoring might make both smaller and easier to match up. */
30637 /* Traverse the DIE's and add add sibling attributes to those DIE's
30638 that have children. */
30639 add_sibling_attributes (comp_unit_die ());
30640 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30641 add_sibling_attributes (node->die);
30642 for (comdat_type_node *ctnode = comdat_type_list;
30643 ctnode != NULL; ctnode = ctnode->next)
30644 add_sibling_attributes (ctnode->root_die);
30646 if (have_macinfo)
30647 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30648 macinfo_section_label);
30650 save_macinfo_strings ();
30652 /* Output all of the compilation units. We put the main one last so that
30653 the offsets are available to output_pubnames. */
30654 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30655 output_comp_unit (node->die, 0, NULL);
30657 hash_table<comdat_type_hasher> comdat_type_table (100);
30658 for (comdat_type_node *ctnode = comdat_type_list;
30659 ctnode != NULL; ctnode = ctnode->next)
30661 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30663 /* Don't output duplicate types. */
30664 if (*slot != HTAB_EMPTY_ENTRY)
30665 continue;
30667 /* Add a pointer to the line table for the main compilation unit
30668 so that the debugger can make sense of DW_AT_decl_file
30669 attributes. */
30670 if (debug_info_level >= DINFO_LEVEL_TERSE)
30671 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30672 (!dwarf_split_debug_info
30673 ? debug_line_section_label
30674 : debug_skeleton_line_section_label));
30676 output_comdat_type_unit (ctnode);
30677 *slot = ctnode;
30680 /* Stick a unique symbol to the main debuginfo section. */
30681 compute_comp_unit_symbol (comp_unit_die ());
30683 /* Output the main compilation unit. We always need it if only for
30684 the CU symbol. */
30685 output_comp_unit (comp_unit_die (), true, NULL);
30687 /* Output the abbreviation table. */
30688 if (vec_safe_length (abbrev_die_table) != 1)
30690 switch_to_section (debug_abbrev_section);
30691 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30692 output_abbrev_section ();
30695 /* Have to end the macro section. */
30696 if (have_macinfo)
30698 /* We have to save macinfo state if we need to output it again
30699 for the FAT part of the object. */
30700 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30701 if (flag_fat_lto_objects)
30702 macinfo_table = macinfo_table->copy ();
30704 switch_to_section (debug_macinfo_section);
30705 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30706 output_macinfo (debug_skeleton_line_section_label, true);
30707 dw2_asm_output_data (1, 0, "End compilation unit");
30709 /* Emit a skeleton debug_line section. */
30710 switch_to_section (debug_skeleton_line_section);
30711 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30712 output_line_info (true);
30714 if (flag_fat_lto_objects)
30716 vec_free (macinfo_table);
30717 macinfo_table = saved_macinfo_table;
30722 /* If we emitted any indirect strings, output the string table too. */
30723 if (debug_str_hash || skeleton_debug_str_hash)
30724 output_indirect_strings ();
30726 /* Switch back to the text section. */
30727 switch_to_section (text_section);
30730 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30731 within the same process. For use by toplev::finalize. */
30733 void
30734 dwarf2out_c_finalize (void)
30736 last_var_location_insn = NULL;
30737 cached_next_real_insn = NULL;
30738 used_rtx_array = NULL;
30739 incomplete_types = NULL;
30740 decl_scope_table = NULL;
30741 debug_info_section = NULL;
30742 debug_skeleton_info_section = NULL;
30743 debug_abbrev_section = NULL;
30744 debug_skeleton_abbrev_section = NULL;
30745 debug_aranges_section = NULL;
30746 debug_addr_section = NULL;
30747 debug_macinfo_section = NULL;
30748 debug_line_section = NULL;
30749 debug_skeleton_line_section = NULL;
30750 debug_loc_section = NULL;
30751 debug_pubnames_section = NULL;
30752 debug_pubtypes_section = NULL;
30753 debug_str_section = NULL;
30754 debug_line_str_section = NULL;
30755 debug_str_dwo_section = NULL;
30756 debug_str_offsets_section = NULL;
30757 debug_ranges_section = NULL;
30758 debug_frame_section = NULL;
30759 fde_vec = NULL;
30760 debug_str_hash = NULL;
30761 debug_line_str_hash = NULL;
30762 skeleton_debug_str_hash = NULL;
30763 dw2_string_counter = 0;
30764 have_multiple_function_sections = false;
30765 text_section_used = false;
30766 cold_text_section_used = false;
30767 cold_text_section = NULL;
30768 current_unit_personality = NULL;
30770 early_dwarf = false;
30771 early_dwarf_finished = false;
30773 next_die_offset = 0;
30774 single_comp_unit_die = NULL;
30775 comdat_type_list = NULL;
30776 limbo_die_list = NULL;
30777 file_table = NULL;
30778 decl_die_table = NULL;
30779 common_block_die_table = NULL;
30780 decl_loc_table = NULL;
30781 call_arg_locations = NULL;
30782 call_arg_loc_last = NULL;
30783 call_site_count = -1;
30784 tail_call_site_count = -1;
30785 cached_dw_loc_list_table = NULL;
30786 abbrev_die_table = NULL;
30787 delete dwarf_proc_stack_usage_map;
30788 dwarf_proc_stack_usage_map = NULL;
30789 line_info_label_num = 0;
30790 cur_line_info_table = NULL;
30791 text_section_line_info = NULL;
30792 cold_text_section_line_info = NULL;
30793 separate_line_info = NULL;
30794 info_section_emitted = false;
30795 pubname_table = NULL;
30796 pubtype_table = NULL;
30797 macinfo_table = NULL;
30798 ranges_table = NULL;
30799 ranges_by_label = NULL;
30800 rnglist_idx = 0;
30801 have_location_lists = false;
30802 loclabel_num = 0;
30803 poc_label_num = 0;
30804 last_emitted_file = NULL;
30805 label_num = 0;
30806 tmpl_value_parm_die_table = NULL;
30807 generic_type_instances = NULL;
30808 frame_pointer_fb_offset = 0;
30809 frame_pointer_fb_offset_valid = false;
30810 base_types.release ();
30811 XDELETEVEC (producer_string);
30812 producer_string = NULL;
30815 #include "gt-dwarf2out.h"