PR ada/66205
[official-gcc.git] / gcc / dwarf2out.c
blobae3d962526c24f3ebe2170970de5844875865292
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 /* Return TRUE if the language is C++. */
5107 static inline bool
5108 is_cxx (void)
5110 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5112 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5113 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5116 /* Return TRUE if DECL was created by the C++ frontend. */
5118 static bool
5119 is_cxx (const_tree decl)
5121 if (in_lto_p)
5123 const_tree context = get_ultimate_context (decl);
5124 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5125 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5127 return is_cxx ();
5130 /* Return TRUE if the language is Fortran. */
5132 static inline bool
5133 is_fortran (void)
5135 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5137 return (lang == DW_LANG_Fortran77
5138 || lang == DW_LANG_Fortran90
5139 || lang == DW_LANG_Fortran95
5140 || lang == DW_LANG_Fortran03
5141 || lang == DW_LANG_Fortran08);
5144 static inline bool
5145 is_fortran (const_tree decl)
5147 if (in_lto_p)
5149 const_tree context = get_ultimate_context (decl);
5150 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5151 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5152 "GNU Fortran", 11) == 0
5153 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5154 "GNU F77") == 0);
5156 return is_fortran ();
5159 /* Return TRUE if the language is Ada. */
5161 static inline bool
5162 is_ada (void)
5164 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5166 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5169 /* Remove the specified attribute if present. Return TRUE if removal
5170 was successful. */
5172 static bool
5173 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5175 dw_attr_node *a;
5176 unsigned ix;
5178 if (! die)
5179 return false;
5181 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5182 if (a->dw_attr == attr_kind)
5184 if (AT_class (a) == dw_val_class_str)
5185 if (a->dw_attr_val.v.val_str->refcount)
5186 a->dw_attr_val.v.val_str->refcount--;
5188 /* vec::ordered_remove should help reduce the number of abbrevs
5189 that are needed. */
5190 die->die_attr->ordered_remove (ix);
5191 return true;
5193 return false;
5196 /* Remove CHILD from its parent. PREV must have the property that
5197 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5199 static void
5200 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5202 gcc_assert (child->die_parent == prev->die_parent);
5203 gcc_assert (prev->die_sib == child);
5204 if (prev == child)
5206 gcc_assert (child->die_parent->die_child == child);
5207 prev = NULL;
5209 else
5210 prev->die_sib = child->die_sib;
5211 if (child->die_parent->die_child == child)
5212 child->die_parent->die_child = prev;
5213 child->die_sib = NULL;
5216 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5217 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5219 static void
5220 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5222 dw_die_ref parent = old_child->die_parent;
5224 gcc_assert (parent == prev->die_parent);
5225 gcc_assert (prev->die_sib == old_child);
5227 new_child->die_parent = parent;
5228 if (prev == old_child)
5230 gcc_assert (parent->die_child == old_child);
5231 new_child->die_sib = new_child;
5233 else
5235 prev->die_sib = new_child;
5236 new_child->die_sib = old_child->die_sib;
5238 if (old_child->die_parent->die_child == old_child)
5239 old_child->die_parent->die_child = new_child;
5240 old_child->die_sib = NULL;
5243 /* Move all children from OLD_PARENT to NEW_PARENT. */
5245 static void
5246 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5248 dw_die_ref c;
5249 new_parent->die_child = old_parent->die_child;
5250 old_parent->die_child = NULL;
5251 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5254 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5255 matches TAG. */
5257 static void
5258 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5260 dw_die_ref c;
5262 c = die->die_child;
5263 if (c) do {
5264 dw_die_ref prev = c;
5265 c = c->die_sib;
5266 while (c->die_tag == tag)
5268 remove_child_with_prev (c, prev);
5269 c->die_parent = NULL;
5270 /* Might have removed every child. */
5271 if (die->die_child == NULL)
5272 return;
5273 c = prev->die_sib;
5275 } while (c != die->die_child);
5278 /* Add a CHILD_DIE as the last child of DIE. */
5280 static void
5281 add_child_die (dw_die_ref die, dw_die_ref child_die)
5283 /* FIXME this should probably be an assert. */
5284 if (! die || ! child_die)
5285 return;
5286 gcc_assert (die != child_die);
5288 child_die->die_parent = die;
5289 if (die->die_child)
5291 child_die->die_sib = die->die_child->die_sib;
5292 die->die_child->die_sib = child_die;
5294 else
5295 child_die->die_sib = child_die;
5296 die->die_child = child_die;
5299 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5301 static void
5302 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5303 dw_die_ref after_die)
5305 gcc_assert (die
5306 && child_die
5307 && after_die
5308 && die->die_child
5309 && die != child_die);
5311 child_die->die_parent = die;
5312 child_die->die_sib = after_die->die_sib;
5313 after_die->die_sib = child_die;
5314 if (die->die_child == after_die)
5315 die->die_child = child_die;
5318 /* Unassociate CHILD from its parent, and make its parent be
5319 NEW_PARENT. */
5321 static void
5322 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5324 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5325 if (p->die_sib == child)
5327 remove_child_with_prev (child, p);
5328 break;
5330 add_child_die (new_parent, child);
5333 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5334 is the specification, to the end of PARENT's list of children.
5335 This is done by removing and re-adding it. */
5337 static void
5338 splice_child_die (dw_die_ref parent, dw_die_ref child)
5340 /* We want the declaration DIE from inside the class, not the
5341 specification DIE at toplevel. */
5342 if (child->die_parent != parent)
5344 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5346 if (tmp)
5347 child = tmp;
5350 gcc_assert (child->die_parent == parent
5351 || (child->die_parent
5352 == get_AT_ref (parent, DW_AT_specification)));
5354 reparent_child (child, parent);
5357 /* Create and return a new die with TAG_VALUE as tag. */
5359 static inline dw_die_ref
5360 new_die_raw (enum dwarf_tag tag_value)
5362 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5363 die->die_tag = tag_value;
5364 return die;
5367 /* Create and return a new die with a parent of PARENT_DIE. If
5368 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5369 associated tree T must be supplied to determine parenthood
5370 later. */
5372 static inline dw_die_ref
5373 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5375 dw_die_ref die = new_die_raw (tag_value);
5377 if (parent_die != NULL)
5378 add_child_die (parent_die, die);
5379 else
5381 limbo_die_node *limbo_node;
5383 /* No DIEs created after early dwarf should end up in limbo,
5384 because the limbo list should not persist past LTO
5385 streaming. */
5386 if (tag_value != DW_TAG_compile_unit
5387 /* These are allowed because they're generated while
5388 breaking out COMDAT units late. */
5389 && tag_value != DW_TAG_type_unit
5390 && tag_value != DW_TAG_skeleton_unit
5391 && !early_dwarf
5392 /* Allow nested functions to live in limbo because they will
5393 only temporarily live there, as decls_for_scope will fix
5394 them up. */
5395 && (TREE_CODE (t) != FUNCTION_DECL
5396 || !decl_function_context (t))
5397 /* Same as nested functions above but for types. Types that
5398 are local to a function will be fixed in
5399 decls_for_scope. */
5400 && (!RECORD_OR_UNION_TYPE_P (t)
5401 || !TYPE_CONTEXT (t)
5402 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5403 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5404 especially in the ltrans stage, but once we implement LTO
5405 dwarf streaming, we should remove this exception. */
5406 && !in_lto_p)
5408 fprintf (stderr, "symbol ended up in limbo too late:");
5409 debug_generic_stmt (t);
5410 gcc_unreachable ();
5413 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5414 limbo_node->die = die;
5415 limbo_node->created_for = t;
5416 limbo_node->next = limbo_die_list;
5417 limbo_die_list = limbo_node;
5420 return die;
5423 /* Return the DIE associated with the given type specifier. */
5425 static inline dw_die_ref
5426 lookup_type_die (tree type)
5428 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5429 if (die && die->removed)
5431 TYPE_SYMTAB_DIE (type) = NULL;
5432 return NULL;
5434 return die;
5437 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5438 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5439 anonymous type instead the one of the naming typedef. */
5441 static inline dw_die_ref
5442 strip_naming_typedef (tree type, dw_die_ref type_die)
5444 if (type
5445 && TREE_CODE (type) == RECORD_TYPE
5446 && type_die
5447 && type_die->die_tag == DW_TAG_typedef
5448 && is_naming_typedef_decl (TYPE_NAME (type)))
5449 type_die = get_AT_ref (type_die, DW_AT_type);
5450 return type_die;
5453 /* Like lookup_type_die, but if type is an anonymous type named by a
5454 typedef[1], return the DIE of the anonymous type instead the one of
5455 the naming typedef. This is because in gen_typedef_die, we did
5456 equate the anonymous struct named by the typedef with the DIE of
5457 the naming typedef. So by default, lookup_type_die on an anonymous
5458 struct yields the DIE of the naming typedef.
5460 [1]: Read the comment of is_naming_typedef_decl to learn about what
5461 a naming typedef is. */
5463 static inline dw_die_ref
5464 lookup_type_die_strip_naming_typedef (tree type)
5466 dw_die_ref die = lookup_type_die (type);
5467 return strip_naming_typedef (type, die);
5470 /* Equate a DIE to a given type specifier. */
5472 static inline void
5473 equate_type_number_to_die (tree type, dw_die_ref type_die)
5475 TYPE_SYMTAB_DIE (type) = type_die;
5478 /* Returns a hash value for X (which really is a die_struct). */
5480 inline hashval_t
5481 decl_die_hasher::hash (die_node *x)
5483 return (hashval_t) x->decl_id;
5486 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5488 inline bool
5489 decl_die_hasher::equal (die_node *x, tree y)
5491 return (x->decl_id == DECL_UID (y));
5494 /* Return the DIE associated with a given declaration. */
5496 static inline dw_die_ref
5497 lookup_decl_die (tree decl)
5499 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5500 NO_INSERT);
5501 if (!die)
5502 return NULL;
5503 if ((*die)->removed)
5505 decl_die_table->clear_slot (die);
5506 return NULL;
5508 return *die;
5512 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5513 style reference. Return true if we found one refering to a DIE for
5514 DECL, otherwise return false. */
5516 static bool
5517 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5518 unsigned HOST_WIDE_INT *off)
5520 dw_die_ref die;
5522 if (flag_wpa && !decl_die_table)
5523 return false;
5525 if (TREE_CODE (decl) == BLOCK)
5526 die = BLOCK_DIE (decl);
5527 else
5528 die = lookup_decl_die (decl);
5529 if (!die)
5530 return false;
5532 /* During WPA stage we currently use DIEs to store the
5533 decl <-> label + offset map. That's quite inefficient but it
5534 works for now. */
5535 if (flag_wpa)
5537 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5538 if (!ref)
5540 gcc_assert (die == comp_unit_die ());
5541 return false;
5543 *off = ref->die_offset;
5544 *sym = ref->die_id.die_symbol;
5545 return true;
5548 /* Similar to get_ref_die_offset_label, but using the "correct"
5549 label. */
5550 *off = die->die_offset;
5551 while (die->die_parent)
5552 die = die->die_parent;
5553 /* For the containing CU DIE we compute a die_symbol in
5554 compute_comp_unit_symbol. */
5555 gcc_assert (die->die_tag == DW_TAG_compile_unit
5556 && die->die_id.die_symbol != NULL);
5557 *sym = die->die_id.die_symbol;
5558 return true;
5561 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5563 static void
5564 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5565 const char *symbol, HOST_WIDE_INT offset)
5567 /* Create a fake DIE that contains the reference. Don't use
5568 new_die because we don't want to end up in the limbo list. */
5569 dw_die_ref ref = new_die_raw (die->die_tag);
5570 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5571 ref->die_offset = offset;
5572 ref->with_offset = 1;
5573 add_AT_die_ref (die, attr_kind, ref);
5576 /* Create a DIE for DECL if required and add a reference to a DIE
5577 at SYMBOL + OFFSET which contains attributes dumped early. */
5579 static void
5580 dwarf2out_register_external_die (tree decl, const char *sym,
5581 unsigned HOST_WIDE_INT off)
5583 if (debug_info_level == DINFO_LEVEL_NONE)
5584 return;
5586 if (flag_wpa && !decl_die_table)
5587 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5589 dw_die_ref die
5590 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5591 gcc_assert (!die);
5593 tree ctx;
5594 dw_die_ref parent = NULL;
5595 /* Need to lookup a DIE for the decls context - the containing
5596 function or translation unit. */
5597 if (TREE_CODE (decl) == BLOCK)
5599 ctx = BLOCK_SUPERCONTEXT (decl);
5600 /* ??? We do not output DIEs for all scopes thus skip as
5601 many DIEs as needed. */
5602 while (TREE_CODE (ctx) == BLOCK
5603 && !BLOCK_DIE (ctx))
5604 ctx = BLOCK_SUPERCONTEXT (ctx);
5606 else
5607 ctx = DECL_CONTEXT (decl);
5608 while (ctx && TYPE_P (ctx))
5609 ctx = TYPE_CONTEXT (ctx);
5610 if (ctx)
5612 if (TREE_CODE (ctx) == BLOCK)
5613 parent = BLOCK_DIE (ctx);
5614 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5615 /* Keep the 1:1 association during WPA. */
5616 && !flag_wpa)
5617 /* Otherwise all late annotations go to the main CU which
5618 imports the original CUs. */
5619 parent = comp_unit_die ();
5620 else if (TREE_CODE (ctx) == FUNCTION_DECL
5621 && TREE_CODE (decl) != PARM_DECL
5622 && TREE_CODE (decl) != BLOCK)
5623 /* Leave function local entities parent determination to when
5624 we process scope vars. */
5626 else
5627 parent = lookup_decl_die (ctx);
5629 else
5630 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5631 Handle this case gracefully by globalizing stuff. */
5632 parent = comp_unit_die ();
5633 /* Create a DIE "stub". */
5634 switch (TREE_CODE (decl))
5636 case TRANSLATION_UNIT_DECL:
5637 if (! flag_wpa)
5639 die = comp_unit_die ();
5640 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5641 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5642 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5643 to create a DIE for the original CUs. */
5644 return;
5646 /* Keep the 1:1 association during WPA. */
5647 die = new_die (DW_TAG_compile_unit, NULL, decl);
5648 break;
5649 case NAMESPACE_DECL:
5650 if (is_fortran (decl))
5651 die = new_die (DW_TAG_module, parent, decl);
5652 else
5653 die = new_die (DW_TAG_namespace, parent, decl);
5654 break;
5655 case FUNCTION_DECL:
5656 die = new_die (DW_TAG_subprogram, parent, decl);
5657 break;
5658 case VAR_DECL:
5659 die = new_die (DW_TAG_variable, parent, decl);
5660 break;
5661 case RESULT_DECL:
5662 die = new_die (DW_TAG_variable, parent, decl);
5663 break;
5664 case PARM_DECL:
5665 die = new_die (DW_TAG_formal_parameter, parent, decl);
5666 break;
5667 case CONST_DECL:
5668 die = new_die (DW_TAG_constant, parent, decl);
5669 break;
5670 case LABEL_DECL:
5671 die = new_die (DW_TAG_label, parent, decl);
5672 break;
5673 case BLOCK:
5674 die = new_die (DW_TAG_lexical_block, parent, decl);
5675 break;
5676 default:
5677 gcc_unreachable ();
5679 if (TREE_CODE (decl) == BLOCK)
5680 BLOCK_DIE (decl) = die;
5681 else
5682 equate_decl_number_to_die (decl, die);
5684 /* Add a reference to the DIE providing early debug at $sym + off. */
5685 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5688 /* Returns a hash value for X (which really is a var_loc_list). */
5690 inline hashval_t
5691 decl_loc_hasher::hash (var_loc_list *x)
5693 return (hashval_t) x->decl_id;
5696 /* Return nonzero if decl_id of var_loc_list X is the same as
5697 UID of decl *Y. */
5699 inline bool
5700 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5702 return (x->decl_id == DECL_UID (y));
5705 /* Return the var_loc list associated with a given declaration. */
5707 static inline var_loc_list *
5708 lookup_decl_loc (const_tree decl)
5710 if (!decl_loc_table)
5711 return NULL;
5712 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5715 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5717 inline hashval_t
5718 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5720 return (hashval_t) x->decl_id;
5723 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5724 UID of decl *Y. */
5726 inline bool
5727 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5729 return (x->decl_id == DECL_UID (y));
5732 /* Equate a DIE to a particular declaration. */
5734 static void
5735 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5737 unsigned int decl_id = DECL_UID (decl);
5739 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5740 decl_die->decl_id = decl_id;
5743 /* Return how many bits covers PIECE EXPR_LIST. */
5745 static HOST_WIDE_INT
5746 decl_piece_bitsize (rtx piece)
5748 int ret = (int) GET_MODE (piece);
5749 if (ret)
5750 return ret;
5751 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5752 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5753 return INTVAL (XEXP (XEXP (piece, 0), 0));
5756 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5758 static rtx *
5759 decl_piece_varloc_ptr (rtx piece)
5761 if ((int) GET_MODE (piece))
5762 return &XEXP (piece, 0);
5763 else
5764 return &XEXP (XEXP (piece, 0), 1);
5767 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5768 Next is the chain of following piece nodes. */
5770 static rtx_expr_list *
5771 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5773 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5774 return alloc_EXPR_LIST (bitsize, loc_note, next);
5775 else
5776 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5777 GEN_INT (bitsize),
5778 loc_note), next);
5781 /* Return rtx that should be stored into loc field for
5782 LOC_NOTE and BITPOS/BITSIZE. */
5784 static rtx
5785 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5786 HOST_WIDE_INT bitsize)
5788 if (bitsize != -1)
5790 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5791 if (bitpos != 0)
5792 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5794 return loc_note;
5797 /* This function either modifies location piece list *DEST in
5798 place (if SRC and INNER is NULL), or copies location piece list
5799 *SRC to *DEST while modifying it. Location BITPOS is modified
5800 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5801 not copied and if needed some padding around it is added.
5802 When modifying in place, DEST should point to EXPR_LIST where
5803 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5804 to the start of the whole list and INNER points to the EXPR_LIST
5805 where earlier pieces cover PIECE_BITPOS bits. */
5807 static void
5808 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5809 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5810 HOST_WIDE_INT bitsize, rtx loc_note)
5812 HOST_WIDE_INT diff;
5813 bool copy = inner != NULL;
5815 if (copy)
5817 /* First copy all nodes preceding the current bitpos. */
5818 while (src != inner)
5820 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5821 decl_piece_bitsize (*src), NULL_RTX);
5822 dest = &XEXP (*dest, 1);
5823 src = &XEXP (*src, 1);
5826 /* Add padding if needed. */
5827 if (bitpos != piece_bitpos)
5829 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5830 copy ? NULL_RTX : *dest);
5831 dest = &XEXP (*dest, 1);
5833 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5835 gcc_assert (!copy);
5836 /* A piece with correct bitpos and bitsize already exist,
5837 just update the location for it and return. */
5838 *decl_piece_varloc_ptr (*dest) = loc_note;
5839 return;
5841 /* Add the piece that changed. */
5842 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5843 dest = &XEXP (*dest, 1);
5844 /* Skip over pieces that overlap it. */
5845 diff = bitpos - piece_bitpos + bitsize;
5846 if (!copy)
5847 src = dest;
5848 while (diff > 0 && *src)
5850 rtx piece = *src;
5851 diff -= decl_piece_bitsize (piece);
5852 if (copy)
5853 src = &XEXP (piece, 1);
5854 else
5856 *src = XEXP (piece, 1);
5857 free_EXPR_LIST_node (piece);
5860 /* Add padding if needed. */
5861 if (diff < 0 && *src)
5863 if (!copy)
5864 dest = src;
5865 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5866 dest = &XEXP (*dest, 1);
5868 if (!copy)
5869 return;
5870 /* Finally copy all nodes following it. */
5871 while (*src)
5873 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5874 decl_piece_bitsize (*src), NULL_RTX);
5875 dest = &XEXP (*dest, 1);
5876 src = &XEXP (*src, 1);
5880 /* Add a variable location node to the linked list for DECL. */
5882 static struct var_loc_node *
5883 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5885 unsigned int decl_id;
5886 var_loc_list *temp;
5887 struct var_loc_node *loc = NULL;
5888 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5890 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5892 tree realdecl = DECL_DEBUG_EXPR (decl);
5893 if (handled_component_p (realdecl)
5894 || (TREE_CODE (realdecl) == MEM_REF
5895 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5897 HOST_WIDE_INT maxsize;
5898 bool reverse;
5899 tree innerdecl
5900 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5901 &reverse);
5902 if (!DECL_P (innerdecl)
5903 || DECL_IGNORED_P (innerdecl)
5904 || TREE_STATIC (innerdecl)
5905 || bitsize <= 0
5906 || bitpos + bitsize > 256
5907 || bitsize != maxsize)
5908 return NULL;
5909 decl = innerdecl;
5913 decl_id = DECL_UID (decl);
5914 var_loc_list **slot
5915 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5916 if (*slot == NULL)
5918 temp = ggc_cleared_alloc<var_loc_list> ();
5919 temp->decl_id = decl_id;
5920 *slot = temp;
5922 else
5923 temp = *slot;
5925 /* For PARM_DECLs try to keep around the original incoming value,
5926 even if that means we'll emit a zero-range .debug_loc entry. */
5927 if (temp->last
5928 && temp->first == temp->last
5929 && TREE_CODE (decl) == PARM_DECL
5930 && NOTE_P (temp->first->loc)
5931 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5932 && DECL_INCOMING_RTL (decl)
5933 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5934 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5935 == GET_CODE (DECL_INCOMING_RTL (decl))
5936 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5937 && (bitsize != -1
5938 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5939 NOTE_VAR_LOCATION_LOC (loc_note))
5940 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5941 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5943 loc = ggc_cleared_alloc<var_loc_node> ();
5944 temp->first->next = loc;
5945 temp->last = loc;
5946 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5948 else if (temp->last)
5950 struct var_loc_node *last = temp->last, *unused = NULL;
5951 rtx *piece_loc = NULL, last_loc_note;
5952 HOST_WIDE_INT piece_bitpos = 0;
5953 if (last->next)
5955 last = last->next;
5956 gcc_assert (last->next == NULL);
5958 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5960 piece_loc = &last->loc;
5963 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5964 if (piece_bitpos + cur_bitsize > bitpos)
5965 break;
5966 piece_bitpos += cur_bitsize;
5967 piece_loc = &XEXP (*piece_loc, 1);
5969 while (*piece_loc);
5971 /* TEMP->LAST here is either pointer to the last but one or
5972 last element in the chained list, LAST is pointer to the
5973 last element. */
5974 if (label && strcmp (last->label, label) == 0)
5976 /* For SRA optimized variables if there weren't any real
5977 insns since last note, just modify the last node. */
5978 if (piece_loc != NULL)
5980 adjust_piece_list (piece_loc, NULL, NULL,
5981 bitpos, piece_bitpos, bitsize, loc_note);
5982 return NULL;
5984 /* If the last note doesn't cover any instructions, remove it. */
5985 if (temp->last != last)
5987 temp->last->next = NULL;
5988 unused = last;
5989 last = temp->last;
5990 gcc_assert (strcmp (last->label, label) != 0);
5992 else
5994 gcc_assert (temp->first == temp->last
5995 || (temp->first->next == temp->last
5996 && TREE_CODE (decl) == PARM_DECL));
5997 memset (temp->last, '\0', sizeof (*temp->last));
5998 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5999 return temp->last;
6002 if (bitsize == -1 && NOTE_P (last->loc))
6003 last_loc_note = last->loc;
6004 else if (piece_loc != NULL
6005 && *piece_loc != NULL_RTX
6006 && piece_bitpos == bitpos
6007 && decl_piece_bitsize (*piece_loc) == bitsize)
6008 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6009 else
6010 last_loc_note = NULL_RTX;
6011 /* If the current location is the same as the end of the list,
6012 and either both or neither of the locations is uninitialized,
6013 we have nothing to do. */
6014 if (last_loc_note == NULL_RTX
6015 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6016 NOTE_VAR_LOCATION_LOC (loc_note)))
6017 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6018 != NOTE_VAR_LOCATION_STATUS (loc_note))
6019 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6020 == VAR_INIT_STATUS_UNINITIALIZED)
6021 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6022 == VAR_INIT_STATUS_UNINITIALIZED))))
6024 /* Add LOC to the end of list and update LAST. If the last
6025 element of the list has been removed above, reuse its
6026 memory for the new node, otherwise allocate a new one. */
6027 if (unused)
6029 loc = unused;
6030 memset (loc, '\0', sizeof (*loc));
6032 else
6033 loc = ggc_cleared_alloc<var_loc_node> ();
6034 if (bitsize == -1 || piece_loc == NULL)
6035 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6036 else
6037 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6038 bitpos, piece_bitpos, bitsize, loc_note);
6039 last->next = loc;
6040 /* Ensure TEMP->LAST will point either to the new last but one
6041 element of the chain, or to the last element in it. */
6042 if (last != temp->last)
6043 temp->last = last;
6045 else if (unused)
6046 ggc_free (unused);
6048 else
6050 loc = ggc_cleared_alloc<var_loc_node> ();
6051 temp->first = loc;
6052 temp->last = loc;
6053 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6055 return loc;
6058 /* Keep track of the number of spaces used to indent the
6059 output of the debugging routines that print the structure of
6060 the DIE internal representation. */
6061 static int print_indent;
6063 /* Indent the line the number of spaces given by print_indent. */
6065 static inline void
6066 print_spaces (FILE *outfile)
6068 fprintf (outfile, "%*s", print_indent, "");
6071 /* Print a type signature in hex. */
6073 static inline void
6074 print_signature (FILE *outfile, char *sig)
6076 int i;
6078 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6079 fprintf (outfile, "%02x", sig[i] & 0xff);
6082 static inline void
6083 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6085 if (discr_value->pos)
6086 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6087 else
6088 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6091 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6093 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6094 RECURSE, output location descriptor operations. */
6096 static void
6097 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6099 switch (val->val_class)
6101 case dw_val_class_addr:
6102 fprintf (outfile, "address");
6103 break;
6104 case dw_val_class_offset:
6105 fprintf (outfile, "offset");
6106 break;
6107 case dw_val_class_loc:
6108 fprintf (outfile, "location descriptor");
6109 if (val->v.val_loc == NULL)
6110 fprintf (outfile, " -> <null>\n");
6111 else if (recurse)
6113 fprintf (outfile, ":\n");
6114 print_indent += 4;
6115 print_loc_descr (val->v.val_loc, outfile);
6116 print_indent -= 4;
6118 else
6119 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6120 break;
6121 case dw_val_class_loc_list:
6122 fprintf (outfile, "location list -> label:%s",
6123 val->v.val_loc_list->ll_symbol);
6124 break;
6125 case dw_val_class_range_list:
6126 fprintf (outfile, "range list");
6127 break;
6128 case dw_val_class_const:
6129 case dw_val_class_const_implicit:
6130 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6131 break;
6132 case dw_val_class_unsigned_const:
6133 case dw_val_class_unsigned_const_implicit:
6134 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6135 break;
6136 case dw_val_class_const_double:
6137 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6138 HOST_WIDE_INT_PRINT_UNSIGNED")",
6139 val->v.val_double.high,
6140 val->v.val_double.low);
6141 break;
6142 case dw_val_class_wide_int:
6144 int i = val->v.val_wide->get_len ();
6145 fprintf (outfile, "constant (");
6146 gcc_assert (i > 0);
6147 if (val->v.val_wide->elt (i - 1) == 0)
6148 fprintf (outfile, "0x");
6149 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6150 val->v.val_wide->elt (--i));
6151 while (--i >= 0)
6152 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6153 val->v.val_wide->elt (i));
6154 fprintf (outfile, ")");
6155 break;
6157 case dw_val_class_vec:
6158 fprintf (outfile, "floating-point or vector constant");
6159 break;
6160 case dw_val_class_flag:
6161 fprintf (outfile, "%u", val->v.val_flag);
6162 break;
6163 case dw_val_class_die_ref:
6164 if (val->v.val_die_ref.die != NULL)
6166 dw_die_ref die = val->v.val_die_ref.die;
6168 if (die->comdat_type_p)
6170 fprintf (outfile, "die -> signature: ");
6171 print_signature (outfile,
6172 die->die_id.die_type_node->signature);
6174 else if (die->die_id.die_symbol)
6176 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6177 if (die->with_offset)
6178 fprintf (outfile, " + %ld", die->die_offset);
6180 else
6181 fprintf (outfile, "die -> %ld", die->die_offset);
6182 fprintf (outfile, " (%p)", (void *) die);
6184 else
6185 fprintf (outfile, "die -> <null>");
6186 break;
6187 case dw_val_class_vms_delta:
6188 fprintf (outfile, "delta: @slotcount(%s-%s)",
6189 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6190 break;
6191 case dw_val_class_lbl_id:
6192 case dw_val_class_lineptr:
6193 case dw_val_class_macptr:
6194 case dw_val_class_loclistsptr:
6195 case dw_val_class_high_pc:
6196 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6197 break;
6198 case dw_val_class_str:
6199 if (val->v.val_str->str != NULL)
6200 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6201 else
6202 fprintf (outfile, "<null>");
6203 break;
6204 case dw_val_class_file:
6205 case dw_val_class_file_implicit:
6206 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6207 val->v.val_file->emitted_number);
6208 break;
6209 case dw_val_class_data8:
6211 int i;
6213 for (i = 0; i < 8; i++)
6214 fprintf (outfile, "%02x", val->v.val_data8[i]);
6215 break;
6217 case dw_val_class_discr_value:
6218 print_discr_value (outfile, &val->v.val_discr_value);
6219 break;
6220 case dw_val_class_discr_list:
6221 for (dw_discr_list_ref node = val->v.val_discr_list;
6222 node != NULL;
6223 node = node->dw_discr_next)
6225 if (node->dw_discr_range)
6227 fprintf (outfile, " .. ");
6228 print_discr_value (outfile, &node->dw_discr_lower_bound);
6229 print_discr_value (outfile, &node->dw_discr_upper_bound);
6231 else
6232 print_discr_value (outfile, &node->dw_discr_lower_bound);
6234 if (node->dw_discr_next != NULL)
6235 fprintf (outfile, " | ");
6237 default:
6238 break;
6242 /* Likewise, for a DIE attribute. */
6244 static void
6245 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6247 print_dw_val (&a->dw_attr_val, recurse, outfile);
6251 /* Print the list of operands in the LOC location description to OUTFILE. This
6252 routine is a debugging aid only. */
6254 static void
6255 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6257 dw_loc_descr_ref l = loc;
6259 if (loc == NULL)
6261 print_spaces (outfile);
6262 fprintf (outfile, "<null>\n");
6263 return;
6266 for (l = loc; l != NULL; l = l->dw_loc_next)
6268 print_spaces (outfile);
6269 fprintf (outfile, "(%p) %s",
6270 (void *) l,
6271 dwarf_stack_op_name (l->dw_loc_opc));
6272 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6274 fprintf (outfile, " ");
6275 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6277 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6279 fprintf (outfile, ", ");
6280 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6282 fprintf (outfile, "\n");
6286 /* Print the information associated with a given DIE, and its children.
6287 This routine is a debugging aid only. */
6289 static void
6290 print_die (dw_die_ref die, FILE *outfile)
6292 dw_attr_node *a;
6293 dw_die_ref c;
6294 unsigned ix;
6296 print_spaces (outfile);
6297 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6298 die->die_offset, dwarf_tag_name (die->die_tag),
6299 (void*) die);
6300 print_spaces (outfile);
6301 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6302 fprintf (outfile, " offset: %ld", die->die_offset);
6303 fprintf (outfile, " mark: %d\n", die->die_mark);
6305 if (die->comdat_type_p)
6307 print_spaces (outfile);
6308 fprintf (outfile, " signature: ");
6309 print_signature (outfile, die->die_id.die_type_node->signature);
6310 fprintf (outfile, "\n");
6313 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6315 print_spaces (outfile);
6316 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6318 print_attribute (a, true, outfile);
6319 fprintf (outfile, "\n");
6322 if (die->die_child != NULL)
6324 print_indent += 4;
6325 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6326 print_indent -= 4;
6328 if (print_indent == 0)
6329 fprintf (outfile, "\n");
6332 /* Print the list of operations in the LOC location description. */
6334 DEBUG_FUNCTION void
6335 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6337 print_loc_descr (loc, stderr);
6340 /* Print the information collected for a given DIE. */
6342 DEBUG_FUNCTION void
6343 debug_dwarf_die (dw_die_ref die)
6345 print_die (die, stderr);
6348 DEBUG_FUNCTION void
6349 debug (die_struct &ref)
6351 print_die (&ref, stderr);
6354 DEBUG_FUNCTION void
6355 debug (die_struct *ptr)
6357 if (ptr)
6358 debug (*ptr);
6359 else
6360 fprintf (stderr, "<nil>\n");
6364 /* Print all DWARF information collected for the compilation unit.
6365 This routine is a debugging aid only. */
6367 DEBUG_FUNCTION void
6368 debug_dwarf (void)
6370 print_indent = 0;
6371 print_die (comp_unit_die (), stderr);
6374 /* Verify the DIE tree structure. */
6376 DEBUG_FUNCTION void
6377 verify_die (dw_die_ref die)
6379 gcc_assert (!die->die_mark);
6380 if (die->die_parent == NULL
6381 && die->die_sib == NULL)
6382 return;
6383 /* Verify the die_sib list is cyclic. */
6384 dw_die_ref x = die;
6387 x->die_mark = 1;
6388 x = x->die_sib;
6390 while (x && !x->die_mark);
6391 gcc_assert (x == die);
6392 x = die;
6395 /* Verify all dies have the same parent. */
6396 gcc_assert (x->die_parent == die->die_parent);
6397 if (x->die_child)
6399 /* Verify the child has the proper parent and recurse. */
6400 gcc_assert (x->die_child->die_parent == x);
6401 verify_die (x->die_child);
6403 x->die_mark = 0;
6404 x = x->die_sib;
6406 while (x && x->die_mark);
6409 /* Sanity checks on DIEs. */
6411 static void
6412 check_die (dw_die_ref die)
6414 unsigned ix;
6415 dw_attr_node *a;
6416 bool inline_found = false;
6417 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6418 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6419 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6421 switch (a->dw_attr)
6423 case DW_AT_inline:
6424 if (a->dw_attr_val.v.val_unsigned)
6425 inline_found = true;
6426 break;
6427 case DW_AT_location:
6428 ++n_location;
6429 break;
6430 case DW_AT_low_pc:
6431 ++n_low_pc;
6432 break;
6433 case DW_AT_high_pc:
6434 ++n_high_pc;
6435 break;
6436 case DW_AT_artificial:
6437 ++n_artificial;
6438 break;
6439 case DW_AT_decl_column:
6440 ++n_decl_column;
6441 break;
6442 case DW_AT_decl_line:
6443 ++n_decl_line;
6444 break;
6445 case DW_AT_decl_file:
6446 ++n_decl_file;
6447 break;
6448 default:
6449 break;
6452 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6453 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6455 fprintf (stderr, "Duplicate attributes in DIE:\n");
6456 debug_dwarf_die (die);
6457 gcc_unreachable ();
6459 if (inline_found)
6461 /* A debugging information entry that is a member of an abstract
6462 instance tree [that has DW_AT_inline] should not contain any
6463 attributes which describe aspects of the subroutine which vary
6464 between distinct inlined expansions or distinct out-of-line
6465 expansions. */
6466 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6467 gcc_assert (a->dw_attr != DW_AT_low_pc
6468 && a->dw_attr != DW_AT_high_pc
6469 && a->dw_attr != DW_AT_location
6470 && a->dw_attr != DW_AT_frame_base
6471 && a->dw_attr != DW_AT_call_all_calls
6472 && a->dw_attr != DW_AT_GNU_all_call_sites);
6476 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6477 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6478 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6480 /* Calculate the checksum of a location expression. */
6482 static inline void
6483 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6485 int tem;
6486 inchash::hash hstate;
6487 hashval_t hash;
6489 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6490 CHECKSUM (tem);
6491 hash_loc_operands (loc, hstate);
6492 hash = hstate.end();
6493 CHECKSUM (hash);
6496 /* Calculate the checksum of an attribute. */
6498 static void
6499 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6501 dw_loc_descr_ref loc;
6502 rtx r;
6504 CHECKSUM (at->dw_attr);
6506 /* We don't care that this was compiled with a different compiler
6507 snapshot; if the output is the same, that's what matters. */
6508 if (at->dw_attr == DW_AT_producer)
6509 return;
6511 switch (AT_class (at))
6513 case dw_val_class_const:
6514 case dw_val_class_const_implicit:
6515 CHECKSUM (at->dw_attr_val.v.val_int);
6516 break;
6517 case dw_val_class_unsigned_const:
6518 case dw_val_class_unsigned_const_implicit:
6519 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6520 break;
6521 case dw_val_class_const_double:
6522 CHECKSUM (at->dw_attr_val.v.val_double);
6523 break;
6524 case dw_val_class_wide_int:
6525 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6526 get_full_len (*at->dw_attr_val.v.val_wide)
6527 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6528 break;
6529 case dw_val_class_vec:
6530 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6531 (at->dw_attr_val.v.val_vec.length
6532 * at->dw_attr_val.v.val_vec.elt_size));
6533 break;
6534 case dw_val_class_flag:
6535 CHECKSUM (at->dw_attr_val.v.val_flag);
6536 break;
6537 case dw_val_class_str:
6538 CHECKSUM_STRING (AT_string (at));
6539 break;
6541 case dw_val_class_addr:
6542 r = AT_addr (at);
6543 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6544 CHECKSUM_STRING (XSTR (r, 0));
6545 break;
6547 case dw_val_class_offset:
6548 CHECKSUM (at->dw_attr_val.v.val_offset);
6549 break;
6551 case dw_val_class_loc:
6552 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6553 loc_checksum (loc, ctx);
6554 break;
6556 case dw_val_class_die_ref:
6557 die_checksum (AT_ref (at), ctx, mark);
6558 break;
6560 case dw_val_class_fde_ref:
6561 case dw_val_class_vms_delta:
6562 case dw_val_class_lbl_id:
6563 case dw_val_class_lineptr:
6564 case dw_val_class_macptr:
6565 case dw_val_class_loclistsptr:
6566 case dw_val_class_high_pc:
6567 break;
6569 case dw_val_class_file:
6570 case dw_val_class_file_implicit:
6571 CHECKSUM_STRING (AT_file (at)->filename);
6572 break;
6574 case dw_val_class_data8:
6575 CHECKSUM (at->dw_attr_val.v.val_data8);
6576 break;
6578 default:
6579 break;
6583 /* Calculate the checksum of a DIE. */
6585 static void
6586 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6588 dw_die_ref c;
6589 dw_attr_node *a;
6590 unsigned ix;
6592 /* To avoid infinite recursion. */
6593 if (die->die_mark)
6595 CHECKSUM (die->die_mark);
6596 return;
6598 die->die_mark = ++(*mark);
6600 CHECKSUM (die->die_tag);
6602 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6603 attr_checksum (a, ctx, mark);
6605 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6608 #undef CHECKSUM
6609 #undef CHECKSUM_BLOCK
6610 #undef CHECKSUM_STRING
6612 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6613 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6614 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6615 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6616 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6617 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6618 #define CHECKSUM_ATTR(FOO) \
6619 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6621 /* Calculate the checksum of a number in signed LEB128 format. */
6623 static void
6624 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6626 unsigned char byte;
6627 bool more;
6629 while (1)
6631 byte = (value & 0x7f);
6632 value >>= 7;
6633 more = !((value == 0 && (byte & 0x40) == 0)
6634 || (value == -1 && (byte & 0x40) != 0));
6635 if (more)
6636 byte |= 0x80;
6637 CHECKSUM (byte);
6638 if (!more)
6639 break;
6643 /* Calculate the checksum of a number in unsigned LEB128 format. */
6645 static void
6646 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6648 while (1)
6650 unsigned char byte = (value & 0x7f);
6651 value >>= 7;
6652 if (value != 0)
6653 /* More bytes to follow. */
6654 byte |= 0x80;
6655 CHECKSUM (byte);
6656 if (value == 0)
6657 break;
6661 /* Checksum the context of the DIE. This adds the names of any
6662 surrounding namespaces or structures to the checksum. */
6664 static void
6665 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6667 const char *name;
6668 dw_die_ref spec;
6669 int tag = die->die_tag;
6671 if (tag != DW_TAG_namespace
6672 && tag != DW_TAG_structure_type
6673 && tag != DW_TAG_class_type)
6674 return;
6676 name = get_AT_string (die, DW_AT_name);
6678 spec = get_AT_ref (die, DW_AT_specification);
6679 if (spec != NULL)
6680 die = spec;
6682 if (die->die_parent != NULL)
6683 checksum_die_context (die->die_parent, ctx);
6685 CHECKSUM_ULEB128 ('C');
6686 CHECKSUM_ULEB128 (tag);
6687 if (name != NULL)
6688 CHECKSUM_STRING (name);
6691 /* Calculate the checksum of a location expression. */
6693 static inline void
6694 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6696 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6697 were emitted as a DW_FORM_sdata instead of a location expression. */
6698 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6700 CHECKSUM_ULEB128 (DW_FORM_sdata);
6701 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6702 return;
6705 /* Otherwise, just checksum the raw location expression. */
6706 while (loc != NULL)
6708 inchash::hash hstate;
6709 hashval_t hash;
6711 CHECKSUM_ULEB128 (loc->dtprel);
6712 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6713 hash_loc_operands (loc, hstate);
6714 hash = hstate.end ();
6715 CHECKSUM (hash);
6716 loc = loc->dw_loc_next;
6720 /* Calculate the checksum of an attribute. */
6722 static void
6723 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6724 struct md5_ctx *ctx, int *mark)
6726 dw_loc_descr_ref loc;
6727 rtx r;
6729 if (AT_class (at) == dw_val_class_die_ref)
6731 dw_die_ref target_die = AT_ref (at);
6733 /* For pointer and reference types, we checksum only the (qualified)
6734 name of the target type (if there is a name). For friend entries,
6735 we checksum only the (qualified) name of the target type or function.
6736 This allows the checksum to remain the same whether the target type
6737 is complete or not. */
6738 if ((at->dw_attr == DW_AT_type
6739 && (tag == DW_TAG_pointer_type
6740 || tag == DW_TAG_reference_type
6741 || tag == DW_TAG_rvalue_reference_type
6742 || tag == DW_TAG_ptr_to_member_type))
6743 || (at->dw_attr == DW_AT_friend
6744 && tag == DW_TAG_friend))
6746 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6748 if (name_attr != NULL)
6750 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6752 if (decl == NULL)
6753 decl = target_die;
6754 CHECKSUM_ULEB128 ('N');
6755 CHECKSUM_ULEB128 (at->dw_attr);
6756 if (decl->die_parent != NULL)
6757 checksum_die_context (decl->die_parent, ctx);
6758 CHECKSUM_ULEB128 ('E');
6759 CHECKSUM_STRING (AT_string (name_attr));
6760 return;
6764 /* For all other references to another DIE, we check to see if the
6765 target DIE has already been visited. If it has, we emit a
6766 backward reference; if not, we descend recursively. */
6767 if (target_die->die_mark > 0)
6769 CHECKSUM_ULEB128 ('R');
6770 CHECKSUM_ULEB128 (at->dw_attr);
6771 CHECKSUM_ULEB128 (target_die->die_mark);
6773 else
6775 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6777 if (decl == NULL)
6778 decl = target_die;
6779 target_die->die_mark = ++(*mark);
6780 CHECKSUM_ULEB128 ('T');
6781 CHECKSUM_ULEB128 (at->dw_attr);
6782 if (decl->die_parent != NULL)
6783 checksum_die_context (decl->die_parent, ctx);
6784 die_checksum_ordered (target_die, ctx, mark);
6786 return;
6789 CHECKSUM_ULEB128 ('A');
6790 CHECKSUM_ULEB128 (at->dw_attr);
6792 switch (AT_class (at))
6794 case dw_val_class_const:
6795 case dw_val_class_const_implicit:
6796 CHECKSUM_ULEB128 (DW_FORM_sdata);
6797 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6798 break;
6800 case dw_val_class_unsigned_const:
6801 case dw_val_class_unsigned_const_implicit:
6802 CHECKSUM_ULEB128 (DW_FORM_sdata);
6803 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6804 break;
6806 case dw_val_class_const_double:
6807 CHECKSUM_ULEB128 (DW_FORM_block);
6808 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6809 CHECKSUM (at->dw_attr_val.v.val_double);
6810 break;
6812 case dw_val_class_wide_int:
6813 CHECKSUM_ULEB128 (DW_FORM_block);
6814 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6815 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6816 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6817 get_full_len (*at->dw_attr_val.v.val_wide)
6818 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6819 break;
6821 case dw_val_class_vec:
6822 CHECKSUM_ULEB128 (DW_FORM_block);
6823 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6824 * at->dw_attr_val.v.val_vec.elt_size);
6825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6826 (at->dw_attr_val.v.val_vec.length
6827 * at->dw_attr_val.v.val_vec.elt_size));
6828 break;
6830 case dw_val_class_flag:
6831 CHECKSUM_ULEB128 (DW_FORM_flag);
6832 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6833 break;
6835 case dw_val_class_str:
6836 CHECKSUM_ULEB128 (DW_FORM_string);
6837 CHECKSUM_STRING (AT_string (at));
6838 break;
6840 case dw_val_class_addr:
6841 r = AT_addr (at);
6842 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6843 CHECKSUM_ULEB128 (DW_FORM_string);
6844 CHECKSUM_STRING (XSTR (r, 0));
6845 break;
6847 case dw_val_class_offset:
6848 CHECKSUM_ULEB128 (DW_FORM_sdata);
6849 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6850 break;
6852 case dw_val_class_loc:
6853 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6854 loc_checksum_ordered (loc, ctx);
6855 break;
6857 case dw_val_class_fde_ref:
6858 case dw_val_class_lbl_id:
6859 case dw_val_class_lineptr:
6860 case dw_val_class_macptr:
6861 case dw_val_class_loclistsptr:
6862 case dw_val_class_high_pc:
6863 break;
6865 case dw_val_class_file:
6866 case dw_val_class_file_implicit:
6867 CHECKSUM_ULEB128 (DW_FORM_string);
6868 CHECKSUM_STRING (AT_file (at)->filename);
6869 break;
6871 case dw_val_class_data8:
6872 CHECKSUM (at->dw_attr_val.v.val_data8);
6873 break;
6875 default:
6876 break;
6880 struct checksum_attributes
6882 dw_attr_node *at_name;
6883 dw_attr_node *at_type;
6884 dw_attr_node *at_friend;
6885 dw_attr_node *at_accessibility;
6886 dw_attr_node *at_address_class;
6887 dw_attr_node *at_alignment;
6888 dw_attr_node *at_allocated;
6889 dw_attr_node *at_artificial;
6890 dw_attr_node *at_associated;
6891 dw_attr_node *at_binary_scale;
6892 dw_attr_node *at_bit_offset;
6893 dw_attr_node *at_bit_size;
6894 dw_attr_node *at_bit_stride;
6895 dw_attr_node *at_byte_size;
6896 dw_attr_node *at_byte_stride;
6897 dw_attr_node *at_const_value;
6898 dw_attr_node *at_containing_type;
6899 dw_attr_node *at_count;
6900 dw_attr_node *at_data_location;
6901 dw_attr_node *at_data_member_location;
6902 dw_attr_node *at_decimal_scale;
6903 dw_attr_node *at_decimal_sign;
6904 dw_attr_node *at_default_value;
6905 dw_attr_node *at_digit_count;
6906 dw_attr_node *at_discr;
6907 dw_attr_node *at_discr_list;
6908 dw_attr_node *at_discr_value;
6909 dw_attr_node *at_encoding;
6910 dw_attr_node *at_endianity;
6911 dw_attr_node *at_explicit;
6912 dw_attr_node *at_is_optional;
6913 dw_attr_node *at_location;
6914 dw_attr_node *at_lower_bound;
6915 dw_attr_node *at_mutable;
6916 dw_attr_node *at_ordering;
6917 dw_attr_node *at_picture_string;
6918 dw_attr_node *at_prototyped;
6919 dw_attr_node *at_small;
6920 dw_attr_node *at_segment;
6921 dw_attr_node *at_string_length;
6922 dw_attr_node *at_string_length_bit_size;
6923 dw_attr_node *at_string_length_byte_size;
6924 dw_attr_node *at_threads_scaled;
6925 dw_attr_node *at_upper_bound;
6926 dw_attr_node *at_use_location;
6927 dw_attr_node *at_use_UTF8;
6928 dw_attr_node *at_variable_parameter;
6929 dw_attr_node *at_virtuality;
6930 dw_attr_node *at_visibility;
6931 dw_attr_node *at_vtable_elem_location;
6934 /* Collect the attributes that we will want to use for the checksum. */
6936 static void
6937 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6939 dw_attr_node *a;
6940 unsigned ix;
6942 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6944 switch (a->dw_attr)
6946 case DW_AT_name:
6947 attrs->at_name = a;
6948 break;
6949 case DW_AT_type:
6950 attrs->at_type = a;
6951 break;
6952 case DW_AT_friend:
6953 attrs->at_friend = a;
6954 break;
6955 case DW_AT_accessibility:
6956 attrs->at_accessibility = a;
6957 break;
6958 case DW_AT_address_class:
6959 attrs->at_address_class = a;
6960 break;
6961 case DW_AT_alignment:
6962 attrs->at_alignment = a;
6963 break;
6964 case DW_AT_allocated:
6965 attrs->at_allocated = a;
6966 break;
6967 case DW_AT_artificial:
6968 attrs->at_artificial = a;
6969 break;
6970 case DW_AT_associated:
6971 attrs->at_associated = a;
6972 break;
6973 case DW_AT_binary_scale:
6974 attrs->at_binary_scale = a;
6975 break;
6976 case DW_AT_bit_offset:
6977 attrs->at_bit_offset = a;
6978 break;
6979 case DW_AT_bit_size:
6980 attrs->at_bit_size = a;
6981 break;
6982 case DW_AT_bit_stride:
6983 attrs->at_bit_stride = a;
6984 break;
6985 case DW_AT_byte_size:
6986 attrs->at_byte_size = a;
6987 break;
6988 case DW_AT_byte_stride:
6989 attrs->at_byte_stride = a;
6990 break;
6991 case DW_AT_const_value:
6992 attrs->at_const_value = a;
6993 break;
6994 case DW_AT_containing_type:
6995 attrs->at_containing_type = a;
6996 break;
6997 case DW_AT_count:
6998 attrs->at_count = a;
6999 break;
7000 case DW_AT_data_location:
7001 attrs->at_data_location = a;
7002 break;
7003 case DW_AT_data_member_location:
7004 attrs->at_data_member_location = a;
7005 break;
7006 case DW_AT_decimal_scale:
7007 attrs->at_decimal_scale = a;
7008 break;
7009 case DW_AT_decimal_sign:
7010 attrs->at_decimal_sign = a;
7011 break;
7012 case DW_AT_default_value:
7013 attrs->at_default_value = a;
7014 break;
7015 case DW_AT_digit_count:
7016 attrs->at_digit_count = a;
7017 break;
7018 case DW_AT_discr:
7019 attrs->at_discr = a;
7020 break;
7021 case DW_AT_discr_list:
7022 attrs->at_discr_list = a;
7023 break;
7024 case DW_AT_discr_value:
7025 attrs->at_discr_value = a;
7026 break;
7027 case DW_AT_encoding:
7028 attrs->at_encoding = a;
7029 break;
7030 case DW_AT_endianity:
7031 attrs->at_endianity = a;
7032 break;
7033 case DW_AT_explicit:
7034 attrs->at_explicit = a;
7035 break;
7036 case DW_AT_is_optional:
7037 attrs->at_is_optional = a;
7038 break;
7039 case DW_AT_location:
7040 attrs->at_location = a;
7041 break;
7042 case DW_AT_lower_bound:
7043 attrs->at_lower_bound = a;
7044 break;
7045 case DW_AT_mutable:
7046 attrs->at_mutable = a;
7047 break;
7048 case DW_AT_ordering:
7049 attrs->at_ordering = a;
7050 break;
7051 case DW_AT_picture_string:
7052 attrs->at_picture_string = a;
7053 break;
7054 case DW_AT_prototyped:
7055 attrs->at_prototyped = a;
7056 break;
7057 case DW_AT_small:
7058 attrs->at_small = a;
7059 break;
7060 case DW_AT_segment:
7061 attrs->at_segment = a;
7062 break;
7063 case DW_AT_string_length:
7064 attrs->at_string_length = a;
7065 break;
7066 case DW_AT_string_length_bit_size:
7067 attrs->at_string_length_bit_size = a;
7068 break;
7069 case DW_AT_string_length_byte_size:
7070 attrs->at_string_length_byte_size = a;
7071 break;
7072 case DW_AT_threads_scaled:
7073 attrs->at_threads_scaled = a;
7074 break;
7075 case DW_AT_upper_bound:
7076 attrs->at_upper_bound = a;
7077 break;
7078 case DW_AT_use_location:
7079 attrs->at_use_location = a;
7080 break;
7081 case DW_AT_use_UTF8:
7082 attrs->at_use_UTF8 = a;
7083 break;
7084 case DW_AT_variable_parameter:
7085 attrs->at_variable_parameter = a;
7086 break;
7087 case DW_AT_virtuality:
7088 attrs->at_virtuality = a;
7089 break;
7090 case DW_AT_visibility:
7091 attrs->at_visibility = a;
7092 break;
7093 case DW_AT_vtable_elem_location:
7094 attrs->at_vtable_elem_location = a;
7095 break;
7096 default:
7097 break;
7102 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7104 static void
7105 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7107 dw_die_ref c;
7108 dw_die_ref decl;
7109 struct checksum_attributes attrs;
7111 CHECKSUM_ULEB128 ('D');
7112 CHECKSUM_ULEB128 (die->die_tag);
7114 memset (&attrs, 0, sizeof (attrs));
7116 decl = get_AT_ref (die, DW_AT_specification);
7117 if (decl != NULL)
7118 collect_checksum_attributes (&attrs, decl);
7119 collect_checksum_attributes (&attrs, die);
7121 CHECKSUM_ATTR (attrs.at_name);
7122 CHECKSUM_ATTR (attrs.at_accessibility);
7123 CHECKSUM_ATTR (attrs.at_address_class);
7124 CHECKSUM_ATTR (attrs.at_allocated);
7125 CHECKSUM_ATTR (attrs.at_artificial);
7126 CHECKSUM_ATTR (attrs.at_associated);
7127 CHECKSUM_ATTR (attrs.at_binary_scale);
7128 CHECKSUM_ATTR (attrs.at_bit_offset);
7129 CHECKSUM_ATTR (attrs.at_bit_size);
7130 CHECKSUM_ATTR (attrs.at_bit_stride);
7131 CHECKSUM_ATTR (attrs.at_byte_size);
7132 CHECKSUM_ATTR (attrs.at_byte_stride);
7133 CHECKSUM_ATTR (attrs.at_const_value);
7134 CHECKSUM_ATTR (attrs.at_containing_type);
7135 CHECKSUM_ATTR (attrs.at_count);
7136 CHECKSUM_ATTR (attrs.at_data_location);
7137 CHECKSUM_ATTR (attrs.at_data_member_location);
7138 CHECKSUM_ATTR (attrs.at_decimal_scale);
7139 CHECKSUM_ATTR (attrs.at_decimal_sign);
7140 CHECKSUM_ATTR (attrs.at_default_value);
7141 CHECKSUM_ATTR (attrs.at_digit_count);
7142 CHECKSUM_ATTR (attrs.at_discr);
7143 CHECKSUM_ATTR (attrs.at_discr_list);
7144 CHECKSUM_ATTR (attrs.at_discr_value);
7145 CHECKSUM_ATTR (attrs.at_encoding);
7146 CHECKSUM_ATTR (attrs.at_endianity);
7147 CHECKSUM_ATTR (attrs.at_explicit);
7148 CHECKSUM_ATTR (attrs.at_is_optional);
7149 CHECKSUM_ATTR (attrs.at_location);
7150 CHECKSUM_ATTR (attrs.at_lower_bound);
7151 CHECKSUM_ATTR (attrs.at_mutable);
7152 CHECKSUM_ATTR (attrs.at_ordering);
7153 CHECKSUM_ATTR (attrs.at_picture_string);
7154 CHECKSUM_ATTR (attrs.at_prototyped);
7155 CHECKSUM_ATTR (attrs.at_small);
7156 CHECKSUM_ATTR (attrs.at_segment);
7157 CHECKSUM_ATTR (attrs.at_string_length);
7158 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7159 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7160 CHECKSUM_ATTR (attrs.at_threads_scaled);
7161 CHECKSUM_ATTR (attrs.at_upper_bound);
7162 CHECKSUM_ATTR (attrs.at_use_location);
7163 CHECKSUM_ATTR (attrs.at_use_UTF8);
7164 CHECKSUM_ATTR (attrs.at_variable_parameter);
7165 CHECKSUM_ATTR (attrs.at_virtuality);
7166 CHECKSUM_ATTR (attrs.at_visibility);
7167 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7168 CHECKSUM_ATTR (attrs.at_type);
7169 CHECKSUM_ATTR (attrs.at_friend);
7170 CHECKSUM_ATTR (attrs.at_alignment);
7172 /* Checksum the child DIEs. */
7173 c = die->die_child;
7174 if (c) do {
7175 dw_attr_node *name_attr;
7177 c = c->die_sib;
7178 name_attr = get_AT (c, DW_AT_name);
7179 if (is_template_instantiation (c))
7181 /* Ignore instantiations of member type and function templates. */
7183 else if (name_attr != NULL
7184 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7186 /* Use a shallow checksum for named nested types and member
7187 functions. */
7188 CHECKSUM_ULEB128 ('S');
7189 CHECKSUM_ULEB128 (c->die_tag);
7190 CHECKSUM_STRING (AT_string (name_attr));
7192 else
7194 /* Use a deep checksum for other children. */
7195 /* Mark this DIE so it gets processed when unmarking. */
7196 if (c->die_mark == 0)
7197 c->die_mark = -1;
7198 die_checksum_ordered (c, ctx, mark);
7200 } while (c != die->die_child);
7202 CHECKSUM_ULEB128 (0);
7205 /* Add a type name and tag to a hash. */
7206 static void
7207 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7209 CHECKSUM_ULEB128 (tag);
7210 CHECKSUM_STRING (name);
7213 #undef CHECKSUM
7214 #undef CHECKSUM_STRING
7215 #undef CHECKSUM_ATTR
7216 #undef CHECKSUM_LEB128
7217 #undef CHECKSUM_ULEB128
7219 /* Generate the type signature for DIE. This is computed by generating an
7220 MD5 checksum over the DIE's tag, its relevant attributes, and its
7221 children. Attributes that are references to other DIEs are processed
7222 by recursion, using the MARK field to prevent infinite recursion.
7223 If the DIE is nested inside a namespace or another type, we also
7224 need to include that context in the signature. The lower 64 bits
7225 of the resulting MD5 checksum comprise the signature. */
7227 static void
7228 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7230 int mark;
7231 const char *name;
7232 unsigned char checksum[16];
7233 struct md5_ctx ctx;
7234 dw_die_ref decl;
7235 dw_die_ref parent;
7237 name = get_AT_string (die, DW_AT_name);
7238 decl = get_AT_ref (die, DW_AT_specification);
7239 parent = get_die_parent (die);
7241 /* First, compute a signature for just the type name (and its surrounding
7242 context, if any. This is stored in the type unit DIE for link-time
7243 ODR (one-definition rule) checking. */
7245 if (is_cxx () && name != NULL)
7247 md5_init_ctx (&ctx);
7249 /* Checksum the names of surrounding namespaces and structures. */
7250 if (parent != NULL)
7251 checksum_die_context (parent, &ctx);
7253 /* Checksum the current DIE. */
7254 die_odr_checksum (die->die_tag, name, &ctx);
7255 md5_finish_ctx (&ctx, checksum);
7257 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7260 /* Next, compute the complete type signature. */
7262 md5_init_ctx (&ctx);
7263 mark = 1;
7264 die->die_mark = mark;
7266 /* Checksum the names of surrounding namespaces and structures. */
7267 if (parent != NULL)
7268 checksum_die_context (parent, &ctx);
7270 /* Checksum the DIE and its children. */
7271 die_checksum_ordered (die, &ctx, &mark);
7272 unmark_all_dies (die);
7273 md5_finish_ctx (&ctx, checksum);
7275 /* Store the signature in the type node and link the type DIE and the
7276 type node together. */
7277 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7278 DWARF_TYPE_SIGNATURE_SIZE);
7279 die->comdat_type_p = true;
7280 die->die_id.die_type_node = type_node;
7281 type_node->type_die = die;
7283 /* If the DIE is a specification, link its declaration to the type node
7284 as well. */
7285 if (decl != NULL)
7287 decl->comdat_type_p = true;
7288 decl->die_id.die_type_node = type_node;
7292 /* Do the location expressions look same? */
7293 static inline int
7294 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7296 return loc1->dw_loc_opc == loc2->dw_loc_opc
7297 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7298 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7301 /* Do the values look the same? */
7302 static int
7303 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7305 dw_loc_descr_ref loc1, loc2;
7306 rtx r1, r2;
7308 if (v1->val_class != v2->val_class)
7309 return 0;
7311 switch (v1->val_class)
7313 case dw_val_class_const:
7314 case dw_val_class_const_implicit:
7315 return v1->v.val_int == v2->v.val_int;
7316 case dw_val_class_unsigned_const:
7317 case dw_val_class_unsigned_const_implicit:
7318 return v1->v.val_unsigned == v2->v.val_unsigned;
7319 case dw_val_class_const_double:
7320 return v1->v.val_double.high == v2->v.val_double.high
7321 && v1->v.val_double.low == v2->v.val_double.low;
7322 case dw_val_class_wide_int:
7323 return *v1->v.val_wide == *v2->v.val_wide;
7324 case dw_val_class_vec:
7325 if (v1->v.val_vec.length != v2->v.val_vec.length
7326 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7327 return 0;
7328 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7329 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7330 return 0;
7331 return 1;
7332 case dw_val_class_flag:
7333 return v1->v.val_flag == v2->v.val_flag;
7334 case dw_val_class_str:
7335 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7337 case dw_val_class_addr:
7338 r1 = v1->v.val_addr;
7339 r2 = v2->v.val_addr;
7340 if (GET_CODE (r1) != GET_CODE (r2))
7341 return 0;
7342 return !rtx_equal_p (r1, r2);
7344 case dw_val_class_offset:
7345 return v1->v.val_offset == v2->v.val_offset;
7347 case dw_val_class_loc:
7348 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7349 loc1 && loc2;
7350 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7351 if (!same_loc_p (loc1, loc2, mark))
7352 return 0;
7353 return !loc1 && !loc2;
7355 case dw_val_class_die_ref:
7356 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7358 case dw_val_class_fde_ref:
7359 case dw_val_class_vms_delta:
7360 case dw_val_class_lbl_id:
7361 case dw_val_class_lineptr:
7362 case dw_val_class_macptr:
7363 case dw_val_class_loclistsptr:
7364 case dw_val_class_high_pc:
7365 return 1;
7367 case dw_val_class_file:
7368 case dw_val_class_file_implicit:
7369 return v1->v.val_file == v2->v.val_file;
7371 case dw_val_class_data8:
7372 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7374 default:
7375 return 1;
7379 /* Do the attributes look the same? */
7381 static int
7382 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7384 if (at1->dw_attr != at2->dw_attr)
7385 return 0;
7387 /* We don't care that this was compiled with a different compiler
7388 snapshot; if the output is the same, that's what matters. */
7389 if (at1->dw_attr == DW_AT_producer)
7390 return 1;
7392 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7395 /* Do the dies look the same? */
7397 static int
7398 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7400 dw_die_ref c1, c2;
7401 dw_attr_node *a1;
7402 unsigned ix;
7404 /* To avoid infinite recursion. */
7405 if (die1->die_mark)
7406 return die1->die_mark == die2->die_mark;
7407 die1->die_mark = die2->die_mark = ++(*mark);
7409 if (die1->die_tag != die2->die_tag)
7410 return 0;
7412 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7413 return 0;
7415 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7416 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7417 return 0;
7419 c1 = die1->die_child;
7420 c2 = die2->die_child;
7421 if (! c1)
7423 if (c2)
7424 return 0;
7426 else
7427 for (;;)
7429 if (!same_die_p (c1, c2, mark))
7430 return 0;
7431 c1 = c1->die_sib;
7432 c2 = c2->die_sib;
7433 if (c1 == die1->die_child)
7435 if (c2 == die2->die_child)
7436 break;
7437 else
7438 return 0;
7442 return 1;
7445 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7446 children, and set die_symbol. */
7448 static void
7449 compute_comp_unit_symbol (dw_die_ref unit_die)
7451 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7452 const char *base = die_name ? lbasename (die_name) : "anonymous";
7453 char *name = XALLOCAVEC (char, strlen (base) + 64);
7454 char *p;
7455 int i, mark;
7456 unsigned char checksum[16];
7457 struct md5_ctx ctx;
7459 /* Compute the checksum of the DIE, then append part of it as hex digits to
7460 the name filename of the unit. */
7462 md5_init_ctx (&ctx);
7463 mark = 0;
7464 die_checksum (unit_die, &ctx, &mark);
7465 unmark_all_dies (unit_die);
7466 md5_finish_ctx (&ctx, checksum);
7468 /* When we this for comp_unit_die () we have a DW_AT_name that might
7469 not start with a letter but with anything valid for filenames and
7470 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7471 character is not a letter. */
7472 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7473 clean_symbol_name (name);
7475 p = name + strlen (name);
7476 for (i = 0; i < 4; i++)
7478 sprintf (p, "%.2x", checksum[i]);
7479 p += 2;
7482 unit_die->die_id.die_symbol = xstrdup (name);
7485 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7487 static int
7488 is_type_die (dw_die_ref die)
7490 switch (die->die_tag)
7492 case DW_TAG_array_type:
7493 case DW_TAG_class_type:
7494 case DW_TAG_interface_type:
7495 case DW_TAG_enumeration_type:
7496 case DW_TAG_pointer_type:
7497 case DW_TAG_reference_type:
7498 case DW_TAG_rvalue_reference_type:
7499 case DW_TAG_string_type:
7500 case DW_TAG_structure_type:
7501 case DW_TAG_subroutine_type:
7502 case DW_TAG_union_type:
7503 case DW_TAG_ptr_to_member_type:
7504 case DW_TAG_set_type:
7505 case DW_TAG_subrange_type:
7506 case DW_TAG_base_type:
7507 case DW_TAG_const_type:
7508 case DW_TAG_file_type:
7509 case DW_TAG_packed_type:
7510 case DW_TAG_volatile_type:
7511 case DW_TAG_typedef:
7512 return 1;
7513 default:
7514 return 0;
7518 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7519 Basically, we want to choose the bits that are likely to be shared between
7520 compilations (types) and leave out the bits that are specific to individual
7521 compilations (functions). */
7523 static int
7524 is_comdat_die (dw_die_ref c)
7526 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7527 we do for stabs. The advantage is a greater likelihood of sharing between
7528 objects that don't include headers in the same order (and therefore would
7529 put the base types in a different comdat). jason 8/28/00 */
7531 if (c->die_tag == DW_TAG_base_type)
7532 return 0;
7534 if (c->die_tag == DW_TAG_pointer_type
7535 || c->die_tag == DW_TAG_reference_type
7536 || c->die_tag == DW_TAG_rvalue_reference_type
7537 || c->die_tag == DW_TAG_const_type
7538 || c->die_tag == DW_TAG_volatile_type)
7540 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7542 return t ? is_comdat_die (t) : 0;
7545 return is_type_die (c);
7548 /* Returns true iff C is a compile-unit DIE. */
7550 static inline bool
7551 is_cu_die (dw_die_ref c)
7553 return c && (c->die_tag == DW_TAG_compile_unit
7554 || c->die_tag == DW_TAG_skeleton_unit);
7557 /* Returns true iff C is a unit DIE of some sort. */
7559 static inline bool
7560 is_unit_die (dw_die_ref c)
7562 return c && (c->die_tag == DW_TAG_compile_unit
7563 || c->die_tag == DW_TAG_partial_unit
7564 || c->die_tag == DW_TAG_type_unit
7565 || c->die_tag == DW_TAG_skeleton_unit);
7568 /* Returns true iff C is a namespace DIE. */
7570 static inline bool
7571 is_namespace_die (dw_die_ref c)
7573 return c && c->die_tag == DW_TAG_namespace;
7576 /* Returns true iff C is a class or structure DIE. */
7578 static inline bool
7579 is_class_die (dw_die_ref c)
7581 return c && (c->die_tag == DW_TAG_class_type
7582 || c->die_tag == DW_TAG_structure_type);
7585 /* Return non-zero if this DIE is a template parameter. */
7587 static inline bool
7588 is_template_parameter (dw_die_ref die)
7590 switch (die->die_tag)
7592 case DW_TAG_template_type_param:
7593 case DW_TAG_template_value_param:
7594 case DW_TAG_GNU_template_template_param:
7595 case DW_TAG_GNU_template_parameter_pack:
7596 return true;
7597 default:
7598 return false;
7602 /* Return non-zero if this DIE represents a template instantiation. */
7604 static inline bool
7605 is_template_instantiation (dw_die_ref die)
7607 dw_die_ref c;
7609 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7610 return false;
7611 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7612 return false;
7615 static char *
7616 gen_internal_sym (const char *prefix)
7618 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7620 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7621 return xstrdup (buf);
7624 /* Return non-zero if this DIE is a declaration. */
7626 static int
7627 is_declaration_die (dw_die_ref die)
7629 dw_attr_node *a;
7630 unsigned ix;
7632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7633 if (a->dw_attr == DW_AT_declaration)
7634 return 1;
7636 return 0;
7639 /* Return non-zero if this DIE is nested inside a subprogram. */
7641 static int
7642 is_nested_in_subprogram (dw_die_ref die)
7644 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7646 if (decl == NULL)
7647 decl = die;
7648 return local_scope_p (decl);
7651 /* Return non-zero if this DIE contains a defining declaration of a
7652 subprogram. */
7654 static int
7655 contains_subprogram_definition (dw_die_ref die)
7657 dw_die_ref c;
7659 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7660 return 1;
7661 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7662 return 0;
7665 /* Return non-zero if this is a type DIE that should be moved to a
7666 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7667 unit type. */
7669 static int
7670 should_move_die_to_comdat (dw_die_ref die)
7672 switch (die->die_tag)
7674 case DW_TAG_class_type:
7675 case DW_TAG_structure_type:
7676 case DW_TAG_enumeration_type:
7677 case DW_TAG_union_type:
7678 /* Don't move declarations, inlined instances, types nested in a
7679 subprogram, or types that contain subprogram definitions. */
7680 if (is_declaration_die (die)
7681 || get_AT (die, DW_AT_abstract_origin)
7682 || is_nested_in_subprogram (die)
7683 || contains_subprogram_definition (die))
7684 return 0;
7685 return 1;
7686 case DW_TAG_array_type:
7687 case DW_TAG_interface_type:
7688 case DW_TAG_pointer_type:
7689 case DW_TAG_reference_type:
7690 case DW_TAG_rvalue_reference_type:
7691 case DW_TAG_string_type:
7692 case DW_TAG_subroutine_type:
7693 case DW_TAG_ptr_to_member_type:
7694 case DW_TAG_set_type:
7695 case DW_TAG_subrange_type:
7696 case DW_TAG_base_type:
7697 case DW_TAG_const_type:
7698 case DW_TAG_file_type:
7699 case DW_TAG_packed_type:
7700 case DW_TAG_volatile_type:
7701 case DW_TAG_typedef:
7702 default:
7703 return 0;
7707 /* Make a clone of DIE. */
7709 static dw_die_ref
7710 clone_die (dw_die_ref die)
7712 dw_die_ref clone = new_die_raw (die->die_tag);
7713 dw_attr_node *a;
7714 unsigned ix;
7716 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7717 add_dwarf_attr (clone, a);
7719 return clone;
7722 /* Make a clone of the tree rooted at DIE. */
7724 static dw_die_ref
7725 clone_tree (dw_die_ref die)
7727 dw_die_ref c;
7728 dw_die_ref clone = clone_die (die);
7730 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7732 return clone;
7735 /* Make a clone of DIE as a declaration. */
7737 static dw_die_ref
7738 clone_as_declaration (dw_die_ref die)
7740 dw_die_ref clone;
7741 dw_die_ref decl;
7742 dw_attr_node *a;
7743 unsigned ix;
7745 /* If the DIE is already a declaration, just clone it. */
7746 if (is_declaration_die (die))
7747 return clone_die (die);
7749 /* If the DIE is a specification, just clone its declaration DIE. */
7750 decl = get_AT_ref (die, DW_AT_specification);
7751 if (decl != NULL)
7753 clone = clone_die (decl);
7754 if (die->comdat_type_p)
7755 add_AT_die_ref (clone, DW_AT_signature, die);
7756 return clone;
7759 clone = new_die_raw (die->die_tag);
7761 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7763 /* We don't want to copy over all attributes.
7764 For example we don't want DW_AT_byte_size because otherwise we will no
7765 longer have a declaration and GDB will treat it as a definition. */
7767 switch (a->dw_attr)
7769 case DW_AT_abstract_origin:
7770 case DW_AT_artificial:
7771 case DW_AT_containing_type:
7772 case DW_AT_external:
7773 case DW_AT_name:
7774 case DW_AT_type:
7775 case DW_AT_virtuality:
7776 case DW_AT_linkage_name:
7777 case DW_AT_MIPS_linkage_name:
7778 add_dwarf_attr (clone, a);
7779 break;
7780 case DW_AT_byte_size:
7781 case DW_AT_alignment:
7782 default:
7783 break;
7787 if (die->comdat_type_p)
7788 add_AT_die_ref (clone, DW_AT_signature, die);
7790 add_AT_flag (clone, DW_AT_declaration, 1);
7791 return clone;
7795 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7797 struct decl_table_entry
7799 dw_die_ref orig;
7800 dw_die_ref copy;
7803 /* Helpers to manipulate hash table of copied declarations. */
7805 /* Hashtable helpers. */
7807 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7809 typedef die_struct *compare_type;
7810 static inline hashval_t hash (const decl_table_entry *);
7811 static inline bool equal (const decl_table_entry *, const die_struct *);
7814 inline hashval_t
7815 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7817 return htab_hash_pointer (entry->orig);
7820 inline bool
7821 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7822 const die_struct *entry2)
7824 return entry1->orig == entry2;
7827 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7829 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7830 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7831 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7832 to check if the ancestor has already been copied into UNIT. */
7834 static dw_die_ref
7835 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7836 decl_hash_type *decl_table)
7838 dw_die_ref parent = die->die_parent;
7839 dw_die_ref new_parent = unit;
7840 dw_die_ref copy;
7841 decl_table_entry **slot = NULL;
7842 struct decl_table_entry *entry = NULL;
7844 if (decl_table)
7846 /* Check if the entry has already been copied to UNIT. */
7847 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7848 INSERT);
7849 if (*slot != HTAB_EMPTY_ENTRY)
7851 entry = *slot;
7852 return entry->copy;
7855 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7856 entry = XCNEW (struct decl_table_entry);
7857 entry->orig = die;
7858 entry->copy = NULL;
7859 *slot = entry;
7862 if (parent != NULL)
7864 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7865 if (spec != NULL)
7866 parent = spec;
7867 if (!is_unit_die (parent))
7868 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7871 copy = clone_as_declaration (die);
7872 add_child_die (new_parent, copy);
7874 if (decl_table)
7876 /* Record the pointer to the copy. */
7877 entry->copy = copy;
7880 return copy;
7882 /* Copy the declaration context to the new type unit DIE. This includes
7883 any surrounding namespace or type declarations. If the DIE has an
7884 AT_specification attribute, it also includes attributes and children
7885 attached to the specification, and returns a pointer to the original
7886 parent of the declaration DIE. Returns NULL otherwise. */
7888 static dw_die_ref
7889 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7891 dw_die_ref decl;
7892 dw_die_ref new_decl;
7893 dw_die_ref orig_parent = NULL;
7895 decl = get_AT_ref (die, DW_AT_specification);
7896 if (decl == NULL)
7897 decl = die;
7898 else
7900 unsigned ix;
7901 dw_die_ref c;
7902 dw_attr_node *a;
7904 /* The original DIE will be changed to a declaration, and must
7905 be moved to be a child of the original declaration DIE. */
7906 orig_parent = decl->die_parent;
7908 /* Copy the type node pointer from the new DIE to the original
7909 declaration DIE so we can forward references later. */
7910 decl->comdat_type_p = true;
7911 decl->die_id.die_type_node = die->die_id.die_type_node;
7913 remove_AT (die, DW_AT_specification);
7915 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7917 if (a->dw_attr != DW_AT_name
7918 && a->dw_attr != DW_AT_declaration
7919 && a->dw_attr != DW_AT_external)
7920 add_dwarf_attr (die, a);
7923 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7926 if (decl->die_parent != NULL
7927 && !is_unit_die (decl->die_parent))
7929 new_decl = copy_ancestor_tree (unit, decl, NULL);
7930 if (new_decl != NULL)
7932 remove_AT (new_decl, DW_AT_signature);
7933 add_AT_specification (die, new_decl);
7937 return orig_parent;
7940 /* Generate the skeleton ancestor tree for the given NODE, then clone
7941 the DIE and add the clone into the tree. */
7943 static void
7944 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7946 if (node->new_die != NULL)
7947 return;
7949 node->new_die = clone_as_declaration (node->old_die);
7951 if (node->parent != NULL)
7953 generate_skeleton_ancestor_tree (node->parent);
7954 add_child_die (node->parent->new_die, node->new_die);
7958 /* Generate a skeleton tree of DIEs containing any declarations that are
7959 found in the original tree. We traverse the tree looking for declaration
7960 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7962 static void
7963 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7965 skeleton_chain_node node;
7966 dw_die_ref c;
7967 dw_die_ref first;
7968 dw_die_ref prev = NULL;
7969 dw_die_ref next = NULL;
7971 node.parent = parent;
7973 first = c = parent->old_die->die_child;
7974 if (c)
7975 next = c->die_sib;
7976 if (c) do {
7977 if (prev == NULL || prev->die_sib == c)
7978 prev = c;
7979 c = next;
7980 next = (c == first ? NULL : c->die_sib);
7981 node.old_die = c;
7982 node.new_die = NULL;
7983 if (is_declaration_die (c))
7985 if (is_template_instantiation (c))
7987 /* Instantiated templates do not need to be cloned into the
7988 type unit. Just move the DIE and its children back to
7989 the skeleton tree (in the main CU). */
7990 remove_child_with_prev (c, prev);
7991 add_child_die (parent->new_die, c);
7992 c = prev;
7994 else if (c->comdat_type_p)
7996 /* This is the skeleton of earlier break_out_comdat_types
7997 type. Clone the existing DIE, but keep the children
7998 under the original (which is in the main CU). */
7999 dw_die_ref clone = clone_die (c);
8001 replace_child (c, clone, prev);
8002 generate_skeleton_ancestor_tree (parent);
8003 add_child_die (parent->new_die, c);
8004 c = clone;
8005 continue;
8007 else
8009 /* Clone the existing DIE, move the original to the skeleton
8010 tree (which is in the main CU), and put the clone, with
8011 all the original's children, where the original came from
8012 (which is about to be moved to the type unit). */
8013 dw_die_ref clone = clone_die (c);
8014 move_all_children (c, clone);
8016 /* If the original has a DW_AT_object_pointer attribute,
8017 it would now point to a child DIE just moved to the
8018 cloned tree, so we need to remove that attribute from
8019 the original. */
8020 remove_AT (c, DW_AT_object_pointer);
8022 replace_child (c, clone, prev);
8023 generate_skeleton_ancestor_tree (parent);
8024 add_child_die (parent->new_die, c);
8025 node.old_die = clone;
8026 node.new_die = c;
8027 c = clone;
8030 generate_skeleton_bottom_up (&node);
8031 } while (next != NULL);
8034 /* Wrapper function for generate_skeleton_bottom_up. */
8036 static dw_die_ref
8037 generate_skeleton (dw_die_ref die)
8039 skeleton_chain_node node;
8041 node.old_die = die;
8042 node.new_die = NULL;
8043 node.parent = NULL;
8045 /* If this type definition is nested inside another type,
8046 and is not an instantiation of a template, always leave
8047 at least a declaration in its place. */
8048 if (die->die_parent != NULL
8049 && is_type_die (die->die_parent)
8050 && !is_template_instantiation (die))
8051 node.new_die = clone_as_declaration (die);
8053 generate_skeleton_bottom_up (&node);
8054 return node.new_die;
8057 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8058 declaration. The original DIE is moved to a new compile unit so that
8059 existing references to it follow it to the new location. If any of the
8060 original DIE's descendants is a declaration, we need to replace the
8061 original DIE with a skeleton tree and move the declarations back into the
8062 skeleton tree. */
8064 static dw_die_ref
8065 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8066 dw_die_ref prev)
8068 dw_die_ref skeleton, orig_parent;
8070 /* Copy the declaration context to the type unit DIE. If the returned
8071 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8072 that DIE. */
8073 orig_parent = copy_declaration_context (unit, child);
8075 skeleton = generate_skeleton (child);
8076 if (skeleton == NULL)
8077 remove_child_with_prev (child, prev);
8078 else
8080 skeleton->comdat_type_p = true;
8081 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8083 /* If the original DIE was a specification, we need to put
8084 the skeleton under the parent DIE of the declaration.
8085 This leaves the original declaration in the tree, but
8086 it will be pruned later since there are no longer any
8087 references to it. */
8088 if (orig_parent != NULL)
8090 remove_child_with_prev (child, prev);
8091 add_child_die (orig_parent, skeleton);
8093 else
8094 replace_child (child, skeleton, prev);
8097 return skeleton;
8100 static void
8101 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8102 comdat_type_node *type_node,
8103 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8105 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8106 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8107 DWARF procedure references in the DW_AT_location attribute. */
8109 static dw_die_ref
8110 copy_dwarf_procedure (dw_die_ref die,
8111 comdat_type_node *type_node,
8112 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8114 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8116 /* DWARF procedures are not supposed to have children... */
8117 gcc_assert (die->die_child == NULL);
8119 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8120 gcc_assert (vec_safe_length (die->die_attr) == 1
8121 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8123 /* Do not copy more than once DWARF procedures. */
8124 bool existed;
8125 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8126 if (existed)
8127 return die_copy;
8129 die_copy = clone_die (die);
8130 add_child_die (type_node->root_die, die_copy);
8131 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8132 return die_copy;
8135 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8136 procedures in DIE's attributes. */
8138 static void
8139 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8140 comdat_type_node *type_node,
8141 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8143 dw_attr_node *a;
8144 unsigned i;
8146 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8148 dw_loc_descr_ref loc;
8150 if (a->dw_attr_val.val_class != dw_val_class_loc)
8151 continue;
8153 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8155 switch (loc->dw_loc_opc)
8157 case DW_OP_call2:
8158 case DW_OP_call4:
8159 case DW_OP_call_ref:
8160 gcc_assert (loc->dw_loc_oprnd1.val_class
8161 == dw_val_class_die_ref);
8162 loc->dw_loc_oprnd1.v.val_die_ref.die
8163 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8164 type_node,
8165 copied_dwarf_procs);
8167 default:
8168 break;
8174 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8175 rewrite references to point to the copies.
8177 References are looked for in DIE's attributes and recursively in all its
8178 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8179 mapping from old DWARF procedures to their copy. It is used not to copy
8180 twice the same DWARF procedure under TYPE_NODE. */
8182 static void
8183 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8184 comdat_type_node *type_node,
8185 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8187 dw_die_ref c;
8189 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8190 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8191 type_node,
8192 copied_dwarf_procs));
8195 /* Traverse the DIE and set up additional .debug_types or .debug_info
8196 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8197 section. */
8199 static void
8200 break_out_comdat_types (dw_die_ref die)
8202 dw_die_ref c;
8203 dw_die_ref first;
8204 dw_die_ref prev = NULL;
8205 dw_die_ref next = NULL;
8206 dw_die_ref unit = NULL;
8208 first = c = die->die_child;
8209 if (c)
8210 next = c->die_sib;
8211 if (c) do {
8212 if (prev == NULL || prev->die_sib == c)
8213 prev = c;
8214 c = next;
8215 next = (c == first ? NULL : c->die_sib);
8216 if (should_move_die_to_comdat (c))
8218 dw_die_ref replacement;
8219 comdat_type_node *type_node;
8221 /* Break out nested types into their own type units. */
8222 break_out_comdat_types (c);
8224 /* Create a new type unit DIE as the root for the new tree, and
8225 add it to the list of comdat types. */
8226 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8227 add_AT_unsigned (unit, DW_AT_language,
8228 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8229 type_node = ggc_cleared_alloc<comdat_type_node> ();
8230 type_node->root_die = unit;
8231 type_node->next = comdat_type_list;
8232 comdat_type_list = type_node;
8234 /* Generate the type signature. */
8235 generate_type_signature (c, type_node);
8237 /* Copy the declaration context, attributes, and children of the
8238 declaration into the new type unit DIE, then remove this DIE
8239 from the main CU (or replace it with a skeleton if necessary). */
8240 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8241 type_node->skeleton_die = replacement;
8243 /* Add the DIE to the new compunit. */
8244 add_child_die (unit, c);
8246 /* Types can reference DWARF procedures for type size or data location
8247 expressions. Calls in DWARF expressions cannot target procedures
8248 that are not in the same section. So we must copy DWARF procedures
8249 along with this type and then rewrite references to them. */
8250 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8251 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8253 if (replacement != NULL)
8254 c = replacement;
8256 else if (c->die_tag == DW_TAG_namespace
8257 || c->die_tag == DW_TAG_class_type
8258 || c->die_tag == DW_TAG_structure_type
8259 || c->die_tag == DW_TAG_union_type)
8261 /* Look for nested types that can be broken out. */
8262 break_out_comdat_types (c);
8264 } while (next != NULL);
8267 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8268 Enter all the cloned children into the hash table decl_table. */
8270 static dw_die_ref
8271 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8273 dw_die_ref c;
8274 dw_die_ref clone;
8275 struct decl_table_entry *entry;
8276 decl_table_entry **slot;
8278 if (die->die_tag == DW_TAG_subprogram)
8279 clone = clone_as_declaration (die);
8280 else
8281 clone = clone_die (die);
8283 slot = decl_table->find_slot_with_hash (die,
8284 htab_hash_pointer (die), INSERT);
8286 /* Assert that DIE isn't in the hash table yet. If it would be there
8287 before, the ancestors would be necessarily there as well, therefore
8288 clone_tree_partial wouldn't be called. */
8289 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8291 entry = XCNEW (struct decl_table_entry);
8292 entry->orig = die;
8293 entry->copy = clone;
8294 *slot = entry;
8296 if (die->die_tag != DW_TAG_subprogram)
8297 FOR_EACH_CHILD (die, c,
8298 add_child_die (clone, clone_tree_partial (c, decl_table)));
8300 return clone;
8303 /* Walk the DIE and its children, looking for references to incomplete
8304 or trivial types that are unmarked (i.e., that are not in the current
8305 type_unit). */
8307 static void
8308 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8310 dw_die_ref c;
8311 dw_attr_node *a;
8312 unsigned ix;
8314 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8316 if (AT_class (a) == dw_val_class_die_ref)
8318 dw_die_ref targ = AT_ref (a);
8319 decl_table_entry **slot;
8320 struct decl_table_entry *entry;
8322 if (targ->die_mark != 0 || targ->comdat_type_p)
8323 continue;
8325 slot = decl_table->find_slot_with_hash (targ,
8326 htab_hash_pointer (targ),
8327 INSERT);
8329 if (*slot != HTAB_EMPTY_ENTRY)
8331 /* TARG has already been copied, so we just need to
8332 modify the reference to point to the copy. */
8333 entry = *slot;
8334 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8336 else
8338 dw_die_ref parent = unit;
8339 dw_die_ref copy = clone_die (targ);
8341 /* Record in DECL_TABLE that TARG has been copied.
8342 Need to do this now, before the recursive call,
8343 because DECL_TABLE may be expanded and SLOT
8344 would no longer be a valid pointer. */
8345 entry = XCNEW (struct decl_table_entry);
8346 entry->orig = targ;
8347 entry->copy = copy;
8348 *slot = entry;
8350 /* If TARG is not a declaration DIE, we need to copy its
8351 children. */
8352 if (!is_declaration_die (targ))
8354 FOR_EACH_CHILD (
8355 targ, c,
8356 add_child_die (copy,
8357 clone_tree_partial (c, decl_table)));
8360 /* Make sure the cloned tree is marked as part of the
8361 type unit. */
8362 mark_dies (copy);
8364 /* If TARG has surrounding context, copy its ancestor tree
8365 into the new type unit. */
8366 if (targ->die_parent != NULL
8367 && !is_unit_die (targ->die_parent))
8368 parent = copy_ancestor_tree (unit, targ->die_parent,
8369 decl_table);
8371 add_child_die (parent, copy);
8372 a->dw_attr_val.v.val_die_ref.die = copy;
8374 /* Make sure the newly-copied DIE is walked. If it was
8375 installed in a previously-added context, it won't
8376 get visited otherwise. */
8377 if (parent != unit)
8379 /* Find the highest point of the newly-added tree,
8380 mark each node along the way, and walk from there. */
8381 parent->die_mark = 1;
8382 while (parent->die_parent
8383 && parent->die_parent->die_mark == 0)
8385 parent = parent->die_parent;
8386 parent->die_mark = 1;
8388 copy_decls_walk (unit, parent, decl_table);
8394 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8397 /* Copy declarations for "unworthy" types into the new comdat section.
8398 Incomplete types, modified types, and certain other types aren't broken
8399 out into comdat sections of their own, so they don't have a signature,
8400 and we need to copy the declaration into the same section so that we
8401 don't have an external reference. */
8403 static void
8404 copy_decls_for_unworthy_types (dw_die_ref unit)
8406 mark_dies (unit);
8407 decl_hash_type decl_table (10);
8408 copy_decls_walk (unit, unit, &decl_table);
8409 unmark_dies (unit);
8412 /* Traverse the DIE and add a sibling attribute if it may have the
8413 effect of speeding up access to siblings. To save some space,
8414 avoid generating sibling attributes for DIE's without children. */
8416 static void
8417 add_sibling_attributes (dw_die_ref die)
8419 dw_die_ref c;
8421 if (! die->die_child)
8422 return;
8424 if (die->die_parent && die != die->die_parent->die_child)
8425 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8427 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8430 /* Output all location lists for the DIE and its children. */
8432 static void
8433 output_location_lists (dw_die_ref die)
8435 dw_die_ref c;
8436 dw_attr_node *a;
8437 unsigned ix;
8439 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8440 if (AT_class (a) == dw_val_class_loc_list)
8441 output_loc_list (AT_loc_list (a));
8443 FOR_EACH_CHILD (die, c, output_location_lists (c));
8446 /* During assign_location_list_indexes and output_loclists_offset the
8447 current index, after it the number of assigned indexes (i.e. how
8448 large the .debug_loclists* offset table should be). */
8449 static unsigned int loc_list_idx;
8451 /* Output all location list offsets for the DIE and its children. */
8453 static void
8454 output_loclists_offsets (dw_die_ref die)
8456 dw_die_ref c;
8457 dw_attr_node *a;
8458 unsigned ix;
8460 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8461 if (AT_class (a) == dw_val_class_loc_list)
8463 dw_loc_list_ref l = AT_loc_list (a);
8464 if (l->offset_emitted)
8465 continue;
8466 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8467 loc_section_label, NULL);
8468 gcc_assert (l->hash == loc_list_idx);
8469 loc_list_idx++;
8470 l->offset_emitted = true;
8473 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8476 /* Recursively set indexes of location lists. */
8478 static void
8479 assign_location_list_indexes (dw_die_ref die)
8481 dw_die_ref c;
8482 dw_attr_node *a;
8483 unsigned ix;
8485 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8486 if (AT_class (a) == dw_val_class_loc_list)
8488 dw_loc_list_ref list = AT_loc_list (a);
8489 if (!list->num_assigned)
8491 list->num_assigned = true;
8492 list->hash = loc_list_idx++;
8496 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8499 /* We want to limit the number of external references, because they are
8500 larger than local references: a relocation takes multiple words, and
8501 even a sig8 reference is always eight bytes, whereas a local reference
8502 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8503 So if we encounter multiple external references to the same type DIE, we
8504 make a local typedef stub for it and redirect all references there.
8506 This is the element of the hash table for keeping track of these
8507 references. */
8509 struct external_ref
8511 dw_die_ref type;
8512 dw_die_ref stub;
8513 unsigned n_refs;
8516 /* Hashtable helpers. */
8518 struct external_ref_hasher : free_ptr_hash <external_ref>
8520 static inline hashval_t hash (const external_ref *);
8521 static inline bool equal (const external_ref *, const external_ref *);
8524 inline hashval_t
8525 external_ref_hasher::hash (const external_ref *r)
8527 dw_die_ref die = r->type;
8528 hashval_t h = 0;
8530 /* We can't use the address of the DIE for hashing, because
8531 that will make the order of the stub DIEs non-deterministic. */
8532 if (! die->comdat_type_p)
8533 /* We have a symbol; use it to compute a hash. */
8534 h = htab_hash_string (die->die_id.die_symbol);
8535 else
8537 /* We have a type signature; use a subset of the bits as the hash.
8538 The 8-byte signature is at least as large as hashval_t. */
8539 comdat_type_node *type_node = die->die_id.die_type_node;
8540 memcpy (&h, type_node->signature, sizeof (h));
8542 return h;
8545 inline bool
8546 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8548 return r1->type == r2->type;
8551 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8553 /* Return a pointer to the external_ref for references to DIE. */
8555 static struct external_ref *
8556 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8558 struct external_ref ref, *ref_p;
8559 external_ref **slot;
8561 ref.type = die;
8562 slot = map->find_slot (&ref, INSERT);
8563 if (*slot != HTAB_EMPTY_ENTRY)
8564 return *slot;
8566 ref_p = XCNEW (struct external_ref);
8567 ref_p->type = die;
8568 *slot = ref_p;
8569 return ref_p;
8572 /* Subroutine of optimize_external_refs, below.
8574 If we see a type skeleton, record it as our stub. If we see external
8575 references, remember how many we've seen. */
8577 static void
8578 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8580 dw_die_ref c;
8581 dw_attr_node *a;
8582 unsigned ix;
8583 struct external_ref *ref_p;
8585 if (is_type_die (die)
8586 && (c = get_AT_ref (die, DW_AT_signature)))
8588 /* This is a local skeleton; use it for local references. */
8589 ref_p = lookup_external_ref (map, c);
8590 ref_p->stub = die;
8593 /* Scan the DIE references, and remember any that refer to DIEs from
8594 other CUs (i.e. those which are not marked). */
8595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8596 if (AT_class (a) == dw_val_class_die_ref
8597 && (c = AT_ref (a))->die_mark == 0
8598 && is_type_die (c))
8600 ref_p = lookup_external_ref (map, c);
8601 ref_p->n_refs++;
8604 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8607 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8608 points to an external_ref, DATA is the CU we're processing. If we don't
8609 already have a local stub, and we have multiple refs, build a stub. */
8612 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8614 struct external_ref *ref_p = *slot;
8616 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8618 /* We have multiple references to this type, so build a small stub.
8619 Both of these forms are a bit dodgy from the perspective of the
8620 DWARF standard, since technically they should have names. */
8621 dw_die_ref cu = data;
8622 dw_die_ref type = ref_p->type;
8623 dw_die_ref stub = NULL;
8625 if (type->comdat_type_p)
8627 /* If we refer to this type via sig8, use AT_signature. */
8628 stub = new_die (type->die_tag, cu, NULL_TREE);
8629 add_AT_die_ref (stub, DW_AT_signature, type);
8631 else
8633 /* Otherwise, use a typedef with no name. */
8634 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8635 add_AT_die_ref (stub, DW_AT_type, type);
8638 stub->die_mark++;
8639 ref_p->stub = stub;
8641 return 1;
8644 /* DIE is a unit; look through all the DIE references to see if there are
8645 any external references to types, and if so, create local stubs for
8646 them which will be applied in build_abbrev_table. This is useful because
8647 references to local DIEs are smaller. */
8649 static external_ref_hash_type *
8650 optimize_external_refs (dw_die_ref die)
8652 external_ref_hash_type *map = new external_ref_hash_type (10);
8653 optimize_external_refs_1 (die, map);
8654 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8655 return map;
8658 /* The following 3 variables are temporaries that are computed only during the
8659 build_abbrev_table call and used and released during the following
8660 optimize_abbrev_table call. */
8662 /* First abbrev_id that can be optimized based on usage. */
8663 static unsigned int abbrev_opt_start;
8665 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8666 abbrev_id smaller than this, because they must be already sized
8667 during build_abbrev_table). */
8668 static unsigned int abbrev_opt_base_type_end;
8670 /* Vector of usage counts during build_abbrev_table. Indexed by
8671 abbrev_id - abbrev_opt_start. */
8672 static vec<unsigned int> abbrev_usage_count;
8674 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8675 static vec<dw_die_ref> sorted_abbrev_dies;
8677 /* The format of each DIE (and its attribute value pairs) is encoded in an
8678 abbreviation table. This routine builds the abbreviation table and assigns
8679 a unique abbreviation id for each abbreviation entry. The children of each
8680 die are visited recursively. */
8682 static void
8683 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8685 unsigned int abbrev_id = 0;
8686 dw_die_ref c;
8687 dw_attr_node *a;
8688 unsigned ix;
8689 dw_die_ref abbrev;
8691 /* Scan the DIE references, and replace any that refer to
8692 DIEs from other CUs (i.e. those which are not marked) with
8693 the local stubs we built in optimize_external_refs. */
8694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8695 if (AT_class (a) == dw_val_class_die_ref
8696 && (c = AT_ref (a))->die_mark == 0)
8698 struct external_ref *ref_p;
8699 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8701 ref_p = lookup_external_ref (extern_map, c);
8702 if (ref_p->stub && ref_p->stub != die)
8703 change_AT_die_ref (a, ref_p->stub);
8704 else
8705 /* We aren't changing this reference, so mark it external. */
8706 set_AT_ref_external (a, 1);
8709 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8711 dw_attr_node *die_a, *abbrev_a;
8712 unsigned ix;
8713 bool ok = true;
8715 if (abbrev_id == 0)
8716 continue;
8717 if (abbrev->die_tag != die->die_tag)
8718 continue;
8719 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8720 continue;
8722 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8723 continue;
8725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8727 abbrev_a = &(*abbrev->die_attr)[ix];
8728 if ((abbrev_a->dw_attr != die_a->dw_attr)
8729 || (value_format (abbrev_a) != value_format (die_a)))
8731 ok = false;
8732 break;
8735 if (ok)
8736 break;
8739 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8741 vec_safe_push (abbrev_die_table, die);
8742 if (abbrev_opt_start)
8743 abbrev_usage_count.safe_push (0);
8745 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8747 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8748 sorted_abbrev_dies.safe_push (die);
8751 die->die_abbrev = abbrev_id;
8752 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8755 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8756 by die_abbrev's usage count, from the most commonly used
8757 abbreviation to the least. */
8759 static int
8760 die_abbrev_cmp (const void *p1, const void *p2)
8762 dw_die_ref die1 = *(const dw_die_ref *) p1;
8763 dw_die_ref die2 = *(const dw_die_ref *) p2;
8765 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8766 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8768 if (die1->die_abbrev >= abbrev_opt_base_type_end
8769 && die2->die_abbrev >= abbrev_opt_base_type_end)
8771 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8772 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8773 return -1;
8774 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8775 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8776 return 1;
8779 /* Stabilize the sort. */
8780 if (die1->die_abbrev < die2->die_abbrev)
8781 return -1;
8782 if (die1->die_abbrev > die2->die_abbrev)
8783 return 1;
8785 return 0;
8788 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8789 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8790 into dw_val_class_const_implicit or
8791 dw_val_class_unsigned_const_implicit. */
8793 static void
8794 optimize_implicit_const (unsigned int first_id, unsigned int end,
8795 vec<bool> &implicit_consts)
8797 /* It never makes sense if there is just one DIE using the abbreviation. */
8798 if (end < first_id + 2)
8799 return;
8801 dw_attr_node *a;
8802 unsigned ix, i;
8803 dw_die_ref die = sorted_abbrev_dies[first_id];
8804 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8805 if (implicit_consts[ix])
8807 enum dw_val_class new_class = dw_val_class_none;
8808 switch (AT_class (a))
8810 case dw_val_class_unsigned_const:
8811 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8812 continue;
8814 /* The .debug_abbrev section will grow by
8815 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8816 in all the DIEs using that abbreviation. */
8817 if (constant_size (AT_unsigned (a)) * (end - first_id)
8818 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8819 continue;
8821 new_class = dw_val_class_unsigned_const_implicit;
8822 break;
8824 case dw_val_class_const:
8825 new_class = dw_val_class_const_implicit;
8826 break;
8828 case dw_val_class_file:
8829 new_class = dw_val_class_file_implicit;
8830 break;
8832 default:
8833 continue;
8835 for (i = first_id; i < end; i++)
8836 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8837 = new_class;
8841 /* Attempt to optimize abbreviation table from abbrev_opt_start
8842 abbreviation above. */
8844 static void
8845 optimize_abbrev_table (void)
8847 if (abbrev_opt_start
8848 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8849 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8851 auto_vec<bool, 32> implicit_consts;
8852 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8854 unsigned int abbrev_id = abbrev_opt_start - 1;
8855 unsigned int first_id = ~0U;
8856 unsigned int last_abbrev_id = 0;
8857 unsigned int i;
8858 dw_die_ref die;
8859 if (abbrev_opt_base_type_end > abbrev_opt_start)
8860 abbrev_id = abbrev_opt_base_type_end - 1;
8861 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8862 most commonly used abbreviations come first. */
8863 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8865 dw_attr_node *a;
8866 unsigned ix;
8868 /* If calc_base_type_die_sizes has been called, the CU and
8869 base types after it can't be optimized, because we've already
8870 calculated their DIE offsets. We've sorted them first. */
8871 if (die->die_abbrev < abbrev_opt_base_type_end)
8872 continue;
8873 if (die->die_abbrev != last_abbrev_id)
8875 last_abbrev_id = die->die_abbrev;
8876 if (dwarf_version >= 5 && first_id != ~0U)
8877 optimize_implicit_const (first_id, i, implicit_consts);
8878 abbrev_id++;
8879 (*abbrev_die_table)[abbrev_id] = die;
8880 if (dwarf_version >= 5)
8882 first_id = i;
8883 implicit_consts.truncate (0);
8885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8886 switch (AT_class (a))
8888 case dw_val_class_const:
8889 case dw_val_class_unsigned_const:
8890 case dw_val_class_file:
8891 implicit_consts.safe_push (true);
8892 break;
8893 default:
8894 implicit_consts.safe_push (false);
8895 break;
8899 else if (dwarf_version >= 5)
8901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8902 if (!implicit_consts[ix])
8903 continue;
8904 else
8906 dw_attr_node *other_a
8907 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8908 if (!dw_val_equal_p (&a->dw_attr_val,
8909 &other_a->dw_attr_val))
8910 implicit_consts[ix] = false;
8913 die->die_abbrev = abbrev_id;
8915 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8916 if (dwarf_version >= 5 && first_id != ~0U)
8917 optimize_implicit_const (first_id, i, implicit_consts);
8920 abbrev_opt_start = 0;
8921 abbrev_opt_base_type_end = 0;
8922 abbrev_usage_count.release ();
8923 sorted_abbrev_dies.release ();
8926 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8928 static int
8929 constant_size (unsigned HOST_WIDE_INT value)
8931 int log;
8933 if (value == 0)
8934 log = 0;
8935 else
8936 log = floor_log2 (value);
8938 log = log / 8;
8939 log = 1 << (floor_log2 (log) + 1);
8941 return log;
8944 /* Return the size of a DIE as it is represented in the
8945 .debug_info section. */
8947 static unsigned long
8948 size_of_die (dw_die_ref die)
8950 unsigned long size = 0;
8951 dw_attr_node *a;
8952 unsigned ix;
8953 enum dwarf_form form;
8955 size += size_of_uleb128 (die->die_abbrev);
8956 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8958 switch (AT_class (a))
8960 case dw_val_class_addr:
8961 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8963 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8964 size += size_of_uleb128 (AT_index (a));
8966 else
8967 size += DWARF2_ADDR_SIZE;
8968 break;
8969 case dw_val_class_offset:
8970 size += DWARF_OFFSET_SIZE;
8971 break;
8972 case dw_val_class_loc:
8974 unsigned long lsize = size_of_locs (AT_loc (a));
8976 /* Block length. */
8977 if (dwarf_version >= 4)
8978 size += size_of_uleb128 (lsize);
8979 else
8980 size += constant_size (lsize);
8981 size += lsize;
8983 break;
8984 case dw_val_class_loc_list:
8985 if (dwarf_split_debug_info && dwarf_version >= 5)
8987 gcc_assert (AT_loc_list (a)->num_assigned);
8988 size += size_of_uleb128 (AT_loc_list (a)->hash);
8990 else
8991 size += DWARF_OFFSET_SIZE;
8992 break;
8993 case dw_val_class_range_list:
8994 if (value_format (a) == DW_FORM_rnglistx)
8996 gcc_assert (rnglist_idx);
8997 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8998 size += size_of_uleb128 (r->idx);
9000 else
9001 size += DWARF_OFFSET_SIZE;
9002 break;
9003 case dw_val_class_const:
9004 size += size_of_sleb128 (AT_int (a));
9005 break;
9006 case dw_val_class_unsigned_const:
9008 int csize = constant_size (AT_unsigned (a));
9009 if (dwarf_version == 3
9010 && a->dw_attr == DW_AT_data_member_location
9011 && csize >= 4)
9012 size += size_of_uleb128 (AT_unsigned (a));
9013 else
9014 size += csize;
9016 break;
9017 case dw_val_class_const_implicit:
9018 case dw_val_class_unsigned_const_implicit:
9019 case dw_val_class_file_implicit:
9020 /* These occupy no size in the DIE, just an extra sleb128 in
9021 .debug_abbrev. */
9022 break;
9023 case dw_val_class_const_double:
9024 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9025 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9026 size++; /* block */
9027 break;
9028 case dw_val_class_wide_int:
9029 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9030 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9031 if (get_full_len (*a->dw_attr_val.v.val_wide)
9032 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9033 size++; /* block */
9034 break;
9035 case dw_val_class_vec:
9036 size += constant_size (a->dw_attr_val.v.val_vec.length
9037 * a->dw_attr_val.v.val_vec.elt_size)
9038 + a->dw_attr_val.v.val_vec.length
9039 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9040 break;
9041 case dw_val_class_flag:
9042 if (dwarf_version >= 4)
9043 /* Currently all add_AT_flag calls pass in 1 as last argument,
9044 so DW_FORM_flag_present can be used. If that ever changes,
9045 we'll need to use DW_FORM_flag and have some optimization
9046 in build_abbrev_table that will change those to
9047 DW_FORM_flag_present if it is set to 1 in all DIEs using
9048 the same abbrev entry. */
9049 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9050 else
9051 size += 1;
9052 break;
9053 case dw_val_class_die_ref:
9054 if (AT_ref_external (a))
9056 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9057 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9058 is sized by target address length, whereas in DWARF3
9059 it's always sized as an offset. */
9060 if (use_debug_types)
9061 size += DWARF_TYPE_SIGNATURE_SIZE;
9062 else if (dwarf_version == 2)
9063 size += DWARF2_ADDR_SIZE;
9064 else
9065 size += DWARF_OFFSET_SIZE;
9067 else
9068 size += DWARF_OFFSET_SIZE;
9069 break;
9070 case dw_val_class_fde_ref:
9071 size += DWARF_OFFSET_SIZE;
9072 break;
9073 case dw_val_class_lbl_id:
9074 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9076 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9077 size += size_of_uleb128 (AT_index (a));
9079 else
9080 size += DWARF2_ADDR_SIZE;
9081 break;
9082 case dw_val_class_lineptr:
9083 case dw_val_class_macptr:
9084 case dw_val_class_loclistsptr:
9085 size += DWARF_OFFSET_SIZE;
9086 break;
9087 case dw_val_class_str:
9088 form = AT_string_form (a);
9089 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9090 size += DWARF_OFFSET_SIZE;
9091 else if (form == DW_FORM_GNU_str_index)
9092 size += size_of_uleb128 (AT_index (a));
9093 else
9094 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9095 break;
9096 case dw_val_class_file:
9097 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9098 break;
9099 case dw_val_class_data8:
9100 size += 8;
9101 break;
9102 case dw_val_class_vms_delta:
9103 size += DWARF_OFFSET_SIZE;
9104 break;
9105 case dw_val_class_high_pc:
9106 size += DWARF2_ADDR_SIZE;
9107 break;
9108 case dw_val_class_discr_value:
9109 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9110 break;
9111 case dw_val_class_discr_list:
9113 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9115 /* This is a block, so we have the block length and then its
9116 data. */
9117 size += constant_size (block_size) + block_size;
9119 break;
9120 default:
9121 gcc_unreachable ();
9125 return size;
9128 /* Size the debugging information associated with a given DIE. Visits the
9129 DIE's children recursively. Updates the global variable next_die_offset, on
9130 each time through. Uses the current value of next_die_offset to update the
9131 die_offset field in each DIE. */
9133 static void
9134 calc_die_sizes (dw_die_ref die)
9136 dw_die_ref c;
9138 gcc_assert (die->die_offset == 0
9139 || (unsigned long int) die->die_offset == next_die_offset);
9140 die->die_offset = next_die_offset;
9141 next_die_offset += size_of_die (die);
9143 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9145 if (die->die_child != NULL)
9146 /* Count the null byte used to terminate sibling lists. */
9147 next_die_offset += 1;
9150 /* Size just the base type children at the start of the CU.
9151 This is needed because build_abbrev needs to size locs
9152 and sizing of type based stack ops needs to know die_offset
9153 values for the base types. */
9155 static void
9156 calc_base_type_die_sizes (void)
9158 unsigned long die_offset = (dwarf_split_debug_info
9159 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9160 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9161 unsigned int i;
9162 dw_die_ref base_type;
9163 #if ENABLE_ASSERT_CHECKING
9164 dw_die_ref prev = comp_unit_die ()->die_child;
9165 #endif
9167 die_offset += size_of_die (comp_unit_die ());
9168 for (i = 0; base_types.iterate (i, &base_type); i++)
9170 #if ENABLE_ASSERT_CHECKING
9171 gcc_assert (base_type->die_offset == 0
9172 && prev->die_sib == base_type
9173 && base_type->die_child == NULL
9174 && base_type->die_abbrev);
9175 prev = base_type;
9176 #endif
9177 if (abbrev_opt_start
9178 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9179 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9180 base_type->die_offset = die_offset;
9181 die_offset += size_of_die (base_type);
9185 /* Set the marks for a die and its children. We do this so
9186 that we know whether or not a reference needs to use FORM_ref_addr; only
9187 DIEs in the same CU will be marked. We used to clear out the offset
9188 and use that as the flag, but ran into ordering problems. */
9190 static void
9191 mark_dies (dw_die_ref die)
9193 dw_die_ref c;
9195 gcc_assert (!die->die_mark);
9197 die->die_mark = 1;
9198 FOR_EACH_CHILD (die, c, mark_dies (c));
9201 /* Clear the marks for a die and its children. */
9203 static void
9204 unmark_dies (dw_die_ref die)
9206 dw_die_ref c;
9208 if (! use_debug_types)
9209 gcc_assert (die->die_mark);
9211 die->die_mark = 0;
9212 FOR_EACH_CHILD (die, c, unmark_dies (c));
9215 /* Clear the marks for a die, its children and referred dies. */
9217 static void
9218 unmark_all_dies (dw_die_ref die)
9220 dw_die_ref c;
9221 dw_attr_node *a;
9222 unsigned ix;
9224 if (!die->die_mark)
9225 return;
9226 die->die_mark = 0;
9228 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9230 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9231 if (AT_class (a) == dw_val_class_die_ref)
9232 unmark_all_dies (AT_ref (a));
9235 /* Calculate if the entry should appear in the final output file. It may be
9236 from a pruned a type. */
9238 static bool
9239 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9241 /* By limiting gnu pubnames to definitions only, gold can generate a
9242 gdb index without entries for declarations, which don't include
9243 enough information to be useful. */
9244 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9245 return false;
9247 if (table == pubname_table)
9249 /* Enumerator names are part of the pubname table, but the
9250 parent DW_TAG_enumeration_type die may have been pruned.
9251 Don't output them if that is the case. */
9252 if (p->die->die_tag == DW_TAG_enumerator &&
9253 (p->die->die_parent == NULL
9254 || !p->die->die_parent->die_perennial_p))
9255 return false;
9257 /* Everything else in the pubname table is included. */
9258 return true;
9261 /* The pubtypes table shouldn't include types that have been
9262 pruned. */
9263 return (p->die->die_offset != 0
9264 || !flag_eliminate_unused_debug_types);
9267 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9268 generated for the compilation unit. */
9270 static unsigned long
9271 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9273 unsigned long size;
9274 unsigned i;
9275 pubname_entry *p;
9276 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9278 size = DWARF_PUBNAMES_HEADER_SIZE;
9279 FOR_EACH_VEC_ELT (*names, i, p)
9280 if (include_pubname_in_output (names, p))
9281 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9283 size += DWARF_OFFSET_SIZE;
9284 return size;
9287 /* Return the size of the information in the .debug_aranges section. */
9289 static unsigned long
9290 size_of_aranges (void)
9292 unsigned long size;
9294 size = DWARF_ARANGES_HEADER_SIZE;
9296 /* Count the address/length pair for this compilation unit. */
9297 if (text_section_used)
9298 size += 2 * DWARF2_ADDR_SIZE;
9299 if (cold_text_section_used)
9300 size += 2 * DWARF2_ADDR_SIZE;
9301 if (have_multiple_function_sections)
9303 unsigned fde_idx;
9304 dw_fde_ref fde;
9306 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9308 if (DECL_IGNORED_P (fde->decl))
9309 continue;
9310 if (!fde->in_std_section)
9311 size += 2 * DWARF2_ADDR_SIZE;
9312 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9313 size += 2 * DWARF2_ADDR_SIZE;
9317 /* Count the two zero words used to terminated the address range table. */
9318 size += 2 * DWARF2_ADDR_SIZE;
9319 return size;
9322 /* Select the encoding of an attribute value. */
9324 static enum dwarf_form
9325 value_format (dw_attr_node *a)
9327 switch (AT_class (a))
9329 case dw_val_class_addr:
9330 /* Only very few attributes allow DW_FORM_addr. */
9331 switch (a->dw_attr)
9333 case DW_AT_low_pc:
9334 case DW_AT_high_pc:
9335 case DW_AT_entry_pc:
9336 case DW_AT_trampoline:
9337 return (AT_index (a) == NOT_INDEXED
9338 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9339 default:
9340 break;
9342 switch (DWARF2_ADDR_SIZE)
9344 case 1:
9345 return DW_FORM_data1;
9346 case 2:
9347 return DW_FORM_data2;
9348 case 4:
9349 return DW_FORM_data4;
9350 case 8:
9351 return DW_FORM_data8;
9352 default:
9353 gcc_unreachable ();
9355 case dw_val_class_loc_list:
9356 if (dwarf_split_debug_info
9357 && dwarf_version >= 5
9358 && AT_loc_list (a)->num_assigned)
9359 return DW_FORM_loclistx;
9360 /* FALLTHRU */
9361 case dw_val_class_range_list:
9362 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9363 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9364 care about sizes of .debug* sections in shared libraries and
9365 executables and don't take into account relocations that affect just
9366 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9367 table in the .debug_rnglists section. */
9368 if (dwarf_split_debug_info
9369 && dwarf_version >= 5
9370 && AT_class (a) == dw_val_class_range_list
9371 && rnglist_idx
9372 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9373 return DW_FORM_rnglistx;
9374 if (dwarf_version >= 4)
9375 return DW_FORM_sec_offset;
9376 /* FALLTHRU */
9377 case dw_val_class_vms_delta:
9378 case dw_val_class_offset:
9379 switch (DWARF_OFFSET_SIZE)
9381 case 4:
9382 return DW_FORM_data4;
9383 case 8:
9384 return DW_FORM_data8;
9385 default:
9386 gcc_unreachable ();
9388 case dw_val_class_loc:
9389 if (dwarf_version >= 4)
9390 return DW_FORM_exprloc;
9391 switch (constant_size (size_of_locs (AT_loc (a))))
9393 case 1:
9394 return DW_FORM_block1;
9395 case 2:
9396 return DW_FORM_block2;
9397 case 4:
9398 return DW_FORM_block4;
9399 default:
9400 gcc_unreachable ();
9402 case dw_val_class_const:
9403 return DW_FORM_sdata;
9404 case dw_val_class_unsigned_const:
9405 switch (constant_size (AT_unsigned (a)))
9407 case 1:
9408 return DW_FORM_data1;
9409 case 2:
9410 return DW_FORM_data2;
9411 case 4:
9412 /* In DWARF3 DW_AT_data_member_location with
9413 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9414 constant, so we need to use DW_FORM_udata if we need
9415 a large constant. */
9416 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9417 return DW_FORM_udata;
9418 return DW_FORM_data4;
9419 case 8:
9420 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9421 return DW_FORM_udata;
9422 return DW_FORM_data8;
9423 default:
9424 gcc_unreachable ();
9426 case dw_val_class_const_implicit:
9427 case dw_val_class_unsigned_const_implicit:
9428 case dw_val_class_file_implicit:
9429 return DW_FORM_implicit_const;
9430 case dw_val_class_const_double:
9431 switch (HOST_BITS_PER_WIDE_INT)
9433 case 8:
9434 return DW_FORM_data2;
9435 case 16:
9436 return DW_FORM_data4;
9437 case 32:
9438 return DW_FORM_data8;
9439 case 64:
9440 if (dwarf_version >= 5)
9441 return DW_FORM_data16;
9442 /* FALLTHRU */
9443 default:
9444 return DW_FORM_block1;
9446 case dw_val_class_wide_int:
9447 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9449 case 8:
9450 return DW_FORM_data1;
9451 case 16:
9452 return DW_FORM_data2;
9453 case 32:
9454 return DW_FORM_data4;
9455 case 64:
9456 return DW_FORM_data8;
9457 case 128:
9458 if (dwarf_version >= 5)
9459 return DW_FORM_data16;
9460 /* FALLTHRU */
9461 default:
9462 return DW_FORM_block1;
9464 case dw_val_class_vec:
9465 switch (constant_size (a->dw_attr_val.v.val_vec.length
9466 * a->dw_attr_val.v.val_vec.elt_size))
9468 case 1:
9469 return DW_FORM_block1;
9470 case 2:
9471 return DW_FORM_block2;
9472 case 4:
9473 return DW_FORM_block4;
9474 default:
9475 gcc_unreachable ();
9477 case dw_val_class_flag:
9478 if (dwarf_version >= 4)
9480 /* Currently all add_AT_flag calls pass in 1 as last argument,
9481 so DW_FORM_flag_present can be used. If that ever changes,
9482 we'll need to use DW_FORM_flag and have some optimization
9483 in build_abbrev_table that will change those to
9484 DW_FORM_flag_present if it is set to 1 in all DIEs using
9485 the same abbrev entry. */
9486 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9487 return DW_FORM_flag_present;
9489 return DW_FORM_flag;
9490 case dw_val_class_die_ref:
9491 if (AT_ref_external (a))
9492 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9493 else
9494 return DW_FORM_ref;
9495 case dw_val_class_fde_ref:
9496 return DW_FORM_data;
9497 case dw_val_class_lbl_id:
9498 return (AT_index (a) == NOT_INDEXED
9499 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9500 case dw_val_class_lineptr:
9501 case dw_val_class_macptr:
9502 case dw_val_class_loclistsptr:
9503 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9504 case dw_val_class_str:
9505 return AT_string_form (a);
9506 case dw_val_class_file:
9507 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9509 case 1:
9510 return DW_FORM_data1;
9511 case 2:
9512 return DW_FORM_data2;
9513 case 4:
9514 return DW_FORM_data4;
9515 default:
9516 gcc_unreachable ();
9519 case dw_val_class_data8:
9520 return DW_FORM_data8;
9522 case dw_val_class_high_pc:
9523 switch (DWARF2_ADDR_SIZE)
9525 case 1:
9526 return DW_FORM_data1;
9527 case 2:
9528 return DW_FORM_data2;
9529 case 4:
9530 return DW_FORM_data4;
9531 case 8:
9532 return DW_FORM_data8;
9533 default:
9534 gcc_unreachable ();
9537 case dw_val_class_discr_value:
9538 return (a->dw_attr_val.v.val_discr_value.pos
9539 ? DW_FORM_udata
9540 : DW_FORM_sdata);
9541 case dw_val_class_discr_list:
9542 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9544 case 1:
9545 return DW_FORM_block1;
9546 case 2:
9547 return DW_FORM_block2;
9548 case 4:
9549 return DW_FORM_block4;
9550 default:
9551 gcc_unreachable ();
9554 default:
9555 gcc_unreachable ();
9559 /* Output the encoding of an attribute value. */
9561 static void
9562 output_value_format (dw_attr_node *a)
9564 enum dwarf_form form = value_format (a);
9566 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9569 /* Given a die and id, produce the appropriate abbreviations. */
9571 static void
9572 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9574 unsigned ix;
9575 dw_attr_node *a_attr;
9577 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9578 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9579 dwarf_tag_name (abbrev->die_tag));
9581 if (abbrev->die_child != NULL)
9582 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9583 else
9584 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9586 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9588 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9589 dwarf_attr_name (a_attr->dw_attr));
9590 output_value_format (a_attr);
9591 if (value_format (a_attr) == DW_FORM_implicit_const)
9593 if (AT_class (a_attr) == dw_val_class_file_implicit)
9595 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9596 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9597 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9599 else
9600 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9604 dw2_asm_output_data (1, 0, NULL);
9605 dw2_asm_output_data (1, 0, NULL);
9609 /* Output the .debug_abbrev section which defines the DIE abbreviation
9610 table. */
9612 static void
9613 output_abbrev_section (void)
9615 unsigned int abbrev_id;
9616 dw_die_ref abbrev;
9618 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9619 if (abbrev_id != 0)
9620 output_die_abbrevs (abbrev_id, abbrev);
9622 /* Terminate the table. */
9623 dw2_asm_output_data (1, 0, NULL);
9626 /* Return a new location list, given the begin and end range, and the
9627 expression. */
9629 static inline dw_loc_list_ref
9630 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9631 const char *section)
9633 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9635 retlist->begin = begin;
9636 retlist->begin_entry = NULL;
9637 retlist->end = end;
9638 retlist->expr = expr;
9639 retlist->section = section;
9641 return retlist;
9644 /* Generate a new internal symbol for this location list node, if it
9645 hasn't got one yet. */
9647 static inline void
9648 gen_llsym (dw_loc_list_ref list)
9650 gcc_assert (!list->ll_symbol);
9651 list->ll_symbol = gen_internal_sym ("LLST");
9654 /* Output the location list given to us. */
9656 static void
9657 output_loc_list (dw_loc_list_ref list_head)
9659 if (list_head->emitted)
9660 return;
9661 list_head->emitted = true;
9663 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9665 dw_loc_list_ref curr = list_head;
9666 const char *last_section = NULL;
9667 const char *base_label = NULL;
9669 /* Walk the location list, and output each range + expression. */
9670 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9672 unsigned long size;
9673 /* Don't output an entry that starts and ends at the same address. */
9674 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9675 continue;
9676 size = size_of_locs (curr->expr);
9677 /* If the expression is too large, drop it on the floor. We could
9678 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9679 in the expression, but >= 64KB expressions for a single value
9680 in a single range are unlikely very useful. */
9681 if (dwarf_version < 5 && size > 0xffff)
9682 continue;
9683 if (dwarf_version >= 5)
9685 if (dwarf_split_debug_info)
9687 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9688 uleb128 index into .debug_addr and uleb128 length. */
9689 dw2_asm_output_data (1, DW_LLE_startx_length,
9690 "DW_LLE_startx_length (%s)",
9691 list_head->ll_symbol);
9692 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9693 "Location list range start index "
9694 "(%s)", curr->begin);
9695 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9696 For that case we probably need to emit DW_LLE_startx_endx,
9697 but we'd need 2 .debug_addr entries rather than just one. */
9698 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9699 "Location list length (%s)",
9700 list_head->ll_symbol);
9702 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9704 /* If all code is in .text section, the base address is
9705 already provided by the CU attributes. Use
9706 DW_LLE_offset_pair where both addresses are uleb128 encoded
9707 offsets against that base. */
9708 dw2_asm_output_data (1, DW_LLE_offset_pair,
9709 "DW_LLE_offset_pair (%s)",
9710 list_head->ll_symbol);
9711 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9712 "Location list begin address (%s)",
9713 list_head->ll_symbol);
9714 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9715 "Location list end address (%s)",
9716 list_head->ll_symbol);
9718 else if (HAVE_AS_LEB128)
9720 /* Otherwise, find out how many consecutive entries could share
9721 the same base entry. If just one, emit DW_LLE_start_length,
9722 otherwise emit DW_LLE_base_address for the base address
9723 followed by a series of DW_LLE_offset_pair. */
9724 if (last_section == NULL || curr->section != last_section)
9726 dw_loc_list_ref curr2;
9727 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9728 curr2 = curr2->dw_loc_next)
9730 if (strcmp (curr2->begin, curr2->end) == 0
9731 && !curr2->force)
9732 continue;
9733 break;
9735 if (curr2 == NULL || curr->section != curr2->section)
9736 last_section = NULL;
9737 else
9739 last_section = curr->section;
9740 base_label = curr->begin;
9741 dw2_asm_output_data (1, DW_LLE_base_address,
9742 "DW_LLE_base_address (%s)",
9743 list_head->ll_symbol);
9744 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9745 "Base address (%s)",
9746 list_head->ll_symbol);
9749 /* Only one entry with the same base address. Use
9750 DW_LLE_start_length with absolute address and uleb128
9751 length. */
9752 if (last_section == NULL)
9754 dw2_asm_output_data (1, DW_LLE_start_length,
9755 "DW_LLE_start_length (%s)",
9756 list_head->ll_symbol);
9757 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9758 "Location list begin address (%s)",
9759 list_head->ll_symbol);
9760 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9761 "Location list length "
9762 "(%s)", list_head->ll_symbol);
9764 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9765 DW_LLE_base_address. */
9766 else
9768 dw2_asm_output_data (1, DW_LLE_offset_pair,
9769 "DW_LLE_offset_pair (%s)",
9770 list_head->ll_symbol);
9771 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9772 "Location list begin address "
9773 "(%s)", list_head->ll_symbol);
9774 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9775 "Location list end address "
9776 "(%s)", list_head->ll_symbol);
9779 /* The assembler does not support .uleb128 directive. Emit
9780 DW_LLE_start_end with a pair of absolute addresses. */
9781 else
9783 dw2_asm_output_data (1, DW_LLE_start_end,
9784 "DW_LLE_start_end (%s)",
9785 list_head->ll_symbol);
9786 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9787 "Location list begin address (%s)",
9788 list_head->ll_symbol);
9789 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9790 "Location list end address (%s)",
9791 list_head->ll_symbol);
9794 else if (dwarf_split_debug_info)
9796 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9797 and 4 byte length. */
9798 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9799 "Location list start/length entry (%s)",
9800 list_head->ll_symbol);
9801 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9802 "Location list range start index (%s)",
9803 curr->begin);
9804 /* The length field is 4 bytes. If we ever need to support
9805 an 8-byte length, we can add a new DW_LLE code or fall back
9806 to DW_LLE_GNU_start_end_entry. */
9807 dw2_asm_output_delta (4, curr->end, curr->begin,
9808 "Location list range length (%s)",
9809 list_head->ll_symbol);
9811 else if (!have_multiple_function_sections)
9813 /* Pair of relative addresses against start of text section. */
9814 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9815 "Location list begin address (%s)",
9816 list_head->ll_symbol);
9817 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9818 "Location list end address (%s)",
9819 list_head->ll_symbol);
9821 else
9823 /* Pair of absolute addresses. */
9824 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9825 "Location list begin address (%s)",
9826 list_head->ll_symbol);
9827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9828 "Location list end address (%s)",
9829 list_head->ll_symbol);
9832 /* Output the block length for this list of location operations. */
9833 if (dwarf_version >= 5)
9834 dw2_asm_output_data_uleb128 (size, "Location expression size");
9835 else
9837 gcc_assert (size <= 0xffff);
9838 dw2_asm_output_data (2, size, "Location expression size");
9841 output_loc_sequence (curr->expr, -1);
9844 /* And finally list termination. */
9845 if (dwarf_version >= 5)
9846 dw2_asm_output_data (1, DW_LLE_end_of_list,
9847 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9848 else if (dwarf_split_debug_info)
9849 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9850 "Location list terminator (%s)",
9851 list_head->ll_symbol);
9852 else
9854 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9855 "Location list terminator begin (%s)",
9856 list_head->ll_symbol);
9857 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9858 "Location list terminator end (%s)",
9859 list_head->ll_symbol);
9863 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9864 section. Emit a relocated reference if val_entry is NULL, otherwise,
9865 emit an indirect reference. */
9867 static void
9868 output_range_list_offset (dw_attr_node *a)
9870 const char *name = dwarf_attr_name (a->dw_attr);
9872 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9874 if (dwarf_version >= 5)
9876 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9878 debug_ranges_section, "%s", name);
9880 else
9882 char *p = strchr (ranges_section_label, '\0');
9883 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9884 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9885 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9886 debug_ranges_section, "%s", name);
9887 *p = '\0';
9890 else if (dwarf_version >= 5)
9892 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9893 gcc_assert (rnglist_idx);
9894 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9896 else
9897 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9898 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9899 "%s (offset from %s)", name, ranges_section_label);
9902 /* Output the offset into the debug_loc section. */
9904 static void
9905 output_loc_list_offset (dw_attr_node *a)
9907 char *sym = AT_loc_list (a)->ll_symbol;
9909 gcc_assert (sym);
9910 if (!dwarf_split_debug_info)
9911 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9912 "%s", dwarf_attr_name (a->dw_attr));
9913 else if (dwarf_version >= 5)
9915 gcc_assert (AT_loc_list (a)->num_assigned);
9916 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9917 dwarf_attr_name (a->dw_attr),
9918 sym);
9920 else
9921 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9922 "%s", dwarf_attr_name (a->dw_attr));
9925 /* Output an attribute's index or value appropriately. */
9927 static void
9928 output_attr_index_or_value (dw_attr_node *a)
9930 const char *name = dwarf_attr_name (a->dw_attr);
9932 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9934 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9935 return;
9937 switch (AT_class (a))
9939 case dw_val_class_addr:
9940 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9941 break;
9942 case dw_val_class_high_pc:
9943 case dw_val_class_lbl_id:
9944 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9945 break;
9946 default:
9947 gcc_unreachable ();
9951 /* Output a type signature. */
9953 static inline void
9954 output_signature (const char *sig, const char *name)
9956 int i;
9958 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9959 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9962 /* Output a discriminant value. */
9964 static inline void
9965 output_discr_value (dw_discr_value *discr_value, const char *name)
9967 if (discr_value->pos)
9968 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9969 else
9970 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9973 /* Output the DIE and its attributes. Called recursively to generate
9974 the definitions of each child DIE. */
9976 static void
9977 output_die (dw_die_ref die)
9979 dw_attr_node *a;
9980 dw_die_ref c;
9981 unsigned long size;
9982 unsigned ix;
9984 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9985 (unsigned long)die->die_offset,
9986 dwarf_tag_name (die->die_tag));
9988 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9990 const char *name = dwarf_attr_name (a->dw_attr);
9992 switch (AT_class (a))
9994 case dw_val_class_addr:
9995 output_attr_index_or_value (a);
9996 break;
9998 case dw_val_class_offset:
9999 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10000 "%s", name);
10001 break;
10003 case dw_val_class_range_list:
10004 output_range_list_offset (a);
10005 break;
10007 case dw_val_class_loc:
10008 size = size_of_locs (AT_loc (a));
10010 /* Output the block length for this list of location operations. */
10011 if (dwarf_version >= 4)
10012 dw2_asm_output_data_uleb128 (size, "%s", name);
10013 else
10014 dw2_asm_output_data (constant_size (size), size, "%s", name);
10016 output_loc_sequence (AT_loc (a), -1);
10017 break;
10019 case dw_val_class_const:
10020 /* ??? It would be slightly more efficient to use a scheme like is
10021 used for unsigned constants below, but gdb 4.x does not sign
10022 extend. Gdb 5.x does sign extend. */
10023 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10024 break;
10026 case dw_val_class_unsigned_const:
10028 int csize = constant_size (AT_unsigned (a));
10029 if (dwarf_version == 3
10030 && a->dw_attr == DW_AT_data_member_location
10031 && csize >= 4)
10032 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10033 else
10034 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10036 break;
10038 case dw_val_class_const_implicit:
10039 if (flag_debug_asm)
10040 fprintf (asm_out_file, "\t\t\t%s %s ("
10041 HOST_WIDE_INT_PRINT_DEC ")\n",
10042 ASM_COMMENT_START, name, AT_int (a));
10043 break;
10045 case dw_val_class_unsigned_const_implicit:
10046 if (flag_debug_asm)
10047 fprintf (asm_out_file, "\t\t\t%s %s ("
10048 HOST_WIDE_INT_PRINT_HEX ")\n",
10049 ASM_COMMENT_START, name, AT_unsigned (a));
10050 break;
10052 case dw_val_class_const_double:
10054 unsigned HOST_WIDE_INT first, second;
10056 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10057 dw2_asm_output_data (1,
10058 HOST_BITS_PER_DOUBLE_INT
10059 / HOST_BITS_PER_CHAR,
10060 NULL);
10062 if (WORDS_BIG_ENDIAN)
10064 first = a->dw_attr_val.v.val_double.high;
10065 second = a->dw_attr_val.v.val_double.low;
10067 else
10069 first = a->dw_attr_val.v.val_double.low;
10070 second = a->dw_attr_val.v.val_double.high;
10073 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10074 first, "%s", name);
10075 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10076 second, NULL);
10078 break;
10080 case dw_val_class_wide_int:
10082 int i;
10083 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10084 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10085 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10086 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10087 * l, NULL);
10089 if (WORDS_BIG_ENDIAN)
10090 for (i = len - 1; i >= 0; --i)
10092 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10093 "%s", name);
10094 name = "";
10096 else
10097 for (i = 0; i < len; ++i)
10099 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10100 "%s", name);
10101 name = "";
10104 break;
10106 case dw_val_class_vec:
10108 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10109 unsigned int len = a->dw_attr_val.v.val_vec.length;
10110 unsigned int i;
10111 unsigned char *p;
10113 dw2_asm_output_data (constant_size (len * elt_size),
10114 len * elt_size, "%s", name);
10115 if (elt_size > sizeof (HOST_WIDE_INT))
10117 elt_size /= 2;
10118 len *= 2;
10120 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10121 i < len;
10122 i++, p += elt_size)
10123 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10124 "fp or vector constant word %u", i);
10125 break;
10128 case dw_val_class_flag:
10129 if (dwarf_version >= 4)
10131 /* Currently all add_AT_flag calls pass in 1 as last argument,
10132 so DW_FORM_flag_present can be used. If that ever changes,
10133 we'll need to use DW_FORM_flag and have some optimization
10134 in build_abbrev_table that will change those to
10135 DW_FORM_flag_present if it is set to 1 in all DIEs using
10136 the same abbrev entry. */
10137 gcc_assert (AT_flag (a) == 1);
10138 if (flag_debug_asm)
10139 fprintf (asm_out_file, "\t\t\t%s %s\n",
10140 ASM_COMMENT_START, name);
10141 break;
10143 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10144 break;
10146 case dw_val_class_loc_list:
10147 output_loc_list_offset (a);
10148 break;
10150 case dw_val_class_die_ref:
10151 if (AT_ref_external (a))
10153 if (AT_ref (a)->comdat_type_p)
10155 comdat_type_node *type_node
10156 = AT_ref (a)->die_id.die_type_node;
10158 gcc_assert (type_node);
10159 output_signature (type_node->signature, name);
10161 else
10163 const char *sym = AT_ref (a)->die_id.die_symbol;
10164 int size;
10166 gcc_assert (sym);
10167 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10168 length, whereas in DWARF3 it's always sized as an
10169 offset. */
10170 if (dwarf_version == 2)
10171 size = DWARF2_ADDR_SIZE;
10172 else
10173 size = DWARF_OFFSET_SIZE;
10174 /* ??? We cannot unconditionally output die_offset if
10175 non-zero - others might create references to those
10176 DIEs via symbols.
10177 And we do not clear its DIE offset after outputting it
10178 (and the label refers to the actual DIEs, not the
10179 DWARF CU unit header which is when using label + offset
10180 would be the correct thing to do).
10181 ??? This is the reason for the with_offset flag. */
10182 if (AT_ref (a)->with_offset)
10183 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10184 debug_info_section, "%s", name);
10185 else
10186 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10187 name);
10190 else
10192 gcc_assert (AT_ref (a)->die_offset);
10193 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10194 "%s", name);
10196 break;
10198 case dw_val_class_fde_ref:
10200 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10202 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10203 a->dw_attr_val.v.val_fde_index * 2);
10204 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10205 "%s", name);
10207 break;
10209 case dw_val_class_vms_delta:
10210 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10211 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10212 AT_vms_delta2 (a), AT_vms_delta1 (a),
10213 "%s", name);
10214 #else
10215 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10216 AT_vms_delta2 (a), AT_vms_delta1 (a),
10217 "%s", name);
10218 #endif
10219 break;
10221 case dw_val_class_lbl_id:
10222 output_attr_index_or_value (a);
10223 break;
10225 case dw_val_class_lineptr:
10226 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10227 debug_line_section, "%s", name);
10228 break;
10230 case dw_val_class_macptr:
10231 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10232 debug_macinfo_section, "%s", name);
10233 break;
10235 case dw_val_class_loclistsptr:
10236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10237 debug_loc_section, "%s", name);
10238 break;
10240 case dw_val_class_str:
10241 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10242 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10243 a->dw_attr_val.v.val_str->label,
10244 debug_str_section,
10245 "%s: \"%s\"", name, AT_string (a));
10246 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10247 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10248 a->dw_attr_val.v.val_str->label,
10249 debug_line_str_section,
10250 "%s: \"%s\"", name, AT_string (a));
10251 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10252 dw2_asm_output_data_uleb128 (AT_index (a),
10253 "%s: \"%s\"", name, AT_string (a));
10254 else
10255 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10256 break;
10258 case dw_val_class_file:
10260 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10262 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10263 a->dw_attr_val.v.val_file->filename);
10264 break;
10267 case dw_val_class_file_implicit:
10268 if (flag_debug_asm)
10269 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10270 ASM_COMMENT_START, name,
10271 maybe_emit_file (a->dw_attr_val.v.val_file),
10272 a->dw_attr_val.v.val_file->filename);
10273 break;
10275 case dw_val_class_data8:
10277 int i;
10279 for (i = 0; i < 8; i++)
10280 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10281 i == 0 ? "%s" : NULL, name);
10282 break;
10285 case dw_val_class_high_pc:
10286 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10287 get_AT_low_pc (die), "DW_AT_high_pc");
10288 break;
10290 case dw_val_class_discr_value:
10291 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10292 break;
10294 case dw_val_class_discr_list:
10296 dw_discr_list_ref list = AT_discr_list (a);
10297 const int size = size_of_discr_list (list);
10299 /* This is a block, so output its length first. */
10300 dw2_asm_output_data (constant_size (size), size,
10301 "%s: block size", name);
10303 for (; list != NULL; list = list->dw_discr_next)
10305 /* One byte for the discriminant value descriptor, and then as
10306 many LEB128 numbers as required. */
10307 if (list->dw_discr_range)
10308 dw2_asm_output_data (1, DW_DSC_range,
10309 "%s: DW_DSC_range", name);
10310 else
10311 dw2_asm_output_data (1, DW_DSC_label,
10312 "%s: DW_DSC_label", name);
10314 output_discr_value (&list->dw_discr_lower_bound, name);
10315 if (list->dw_discr_range)
10316 output_discr_value (&list->dw_discr_upper_bound, name);
10318 break;
10321 default:
10322 gcc_unreachable ();
10326 FOR_EACH_CHILD (die, c, output_die (c));
10328 /* Add null byte to terminate sibling list. */
10329 if (die->die_child != NULL)
10330 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10331 (unsigned long) die->die_offset);
10334 /* Output the compilation unit that appears at the beginning of the
10335 .debug_info section, and precedes the DIE descriptions. */
10337 static void
10338 output_compilation_unit_header (enum dwarf_unit_type ut)
10340 if (!XCOFF_DEBUGGING_INFO)
10342 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10343 dw2_asm_output_data (4, 0xffffffff,
10344 "Initial length escape value indicating 64-bit DWARF extension");
10345 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10346 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10347 "Length of Compilation Unit Info");
10350 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10351 if (dwarf_version >= 5)
10353 const char *name;
10354 switch (ut)
10356 case DW_UT_compile: name = "DW_UT_compile"; break;
10357 case DW_UT_type: name = "DW_UT_type"; break;
10358 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10359 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10360 default: gcc_unreachable ();
10362 dw2_asm_output_data (1, ut, "%s", name);
10363 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10365 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10366 debug_abbrev_section,
10367 "Offset Into Abbrev. Section");
10368 if (dwarf_version < 5)
10369 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10372 /* Output the compilation unit DIE and its children. */
10374 static void
10375 output_comp_unit (dw_die_ref die, int output_if_empty,
10376 const unsigned char *dwo_id)
10378 const char *secname, *oldsym;
10379 char *tmp;
10381 /* Unless we are outputting main CU, we may throw away empty ones. */
10382 if (!output_if_empty && die->die_child == NULL)
10383 return;
10385 /* Even if there are no children of this DIE, we must output the information
10386 about the compilation unit. Otherwise, on an empty translation unit, we
10387 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10388 will then complain when examining the file. First mark all the DIEs in
10389 this CU so we know which get local refs. */
10390 mark_dies (die);
10392 external_ref_hash_type *extern_map = optimize_external_refs (die);
10394 /* For now, optimize only the main CU, in order to optimize the rest
10395 we'd need to see all of them earlier. Leave the rest for post-linking
10396 tools like DWZ. */
10397 if (die == comp_unit_die ())
10398 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10400 build_abbrev_table (die, extern_map);
10402 optimize_abbrev_table ();
10404 delete extern_map;
10406 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10407 next_die_offset = (dwo_id
10408 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10409 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10410 calc_die_sizes (die);
10412 oldsym = die->die_id.die_symbol;
10413 if (oldsym && die->comdat_type_p)
10415 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10417 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10418 secname = tmp;
10419 die->die_id.die_symbol = NULL;
10420 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10422 else
10424 switch_to_section (debug_info_section);
10425 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10426 info_section_emitted = true;
10429 /* For LTO cross unit DIE refs we want a symbol on the start of the
10430 debuginfo section, not on the CU DIE. */
10431 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10433 /* ??? No way to get visibility assembled without a decl. */
10434 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10435 get_identifier (oldsym), char_type_node);
10436 TREE_PUBLIC (decl) = true;
10437 TREE_STATIC (decl) = true;
10438 DECL_ARTIFICIAL (decl) = true;
10439 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10440 DECL_VISIBILITY_SPECIFIED (decl) = true;
10441 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10442 #ifdef ASM_WEAKEN_LABEL
10443 /* We prefer a .weak because that handles duplicates from duplicate
10444 archive members in a graceful way. */
10445 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10446 #else
10447 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10448 #endif
10449 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10452 /* Output debugging information. */
10453 output_compilation_unit_header (dwo_id
10454 ? DW_UT_split_compile : DW_UT_compile);
10455 if (dwarf_version >= 5)
10457 if (dwo_id != NULL)
10458 for (int i = 0; i < 8; i++)
10459 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10461 output_die (die);
10463 /* Leave the marks on the main CU, so we can check them in
10464 output_pubnames. */
10465 if (oldsym)
10467 unmark_dies (die);
10468 die->die_id.die_symbol = oldsym;
10472 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10473 and .debug_pubtypes. This is configured per-target, but can be
10474 overridden by the -gpubnames or -gno-pubnames options. */
10476 static inline bool
10477 want_pubnames (void)
10479 if (debug_info_level <= DINFO_LEVEL_TERSE)
10480 return false;
10481 if (debug_generate_pub_sections != -1)
10482 return debug_generate_pub_sections;
10483 return targetm.want_debug_pub_sections;
10486 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10488 static void
10489 add_AT_pubnames (dw_die_ref die)
10491 if (want_pubnames ())
10492 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10495 /* Add a string attribute value to a skeleton DIE. */
10497 static inline void
10498 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10499 const char *str)
10501 dw_attr_node attr;
10502 struct indirect_string_node *node;
10504 if (! skeleton_debug_str_hash)
10505 skeleton_debug_str_hash
10506 = hash_table<indirect_string_hasher>::create_ggc (10);
10508 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10509 find_string_form (node);
10510 if (node->form == DW_FORM_GNU_str_index)
10511 node->form = DW_FORM_strp;
10513 attr.dw_attr = attr_kind;
10514 attr.dw_attr_val.val_class = dw_val_class_str;
10515 attr.dw_attr_val.val_entry = NULL;
10516 attr.dw_attr_val.v.val_str = node;
10517 add_dwarf_attr (die, &attr);
10520 /* Helper function to generate top-level dies for skeleton debug_info and
10521 debug_types. */
10523 static void
10524 add_top_level_skeleton_die_attrs (dw_die_ref die)
10526 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10527 const char *comp_dir = comp_dir_string ();
10529 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10530 if (comp_dir != NULL)
10531 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10532 add_AT_pubnames (die);
10533 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10536 /* Output skeleton debug sections that point to the dwo file. */
10538 static void
10539 output_skeleton_debug_sections (dw_die_ref comp_unit,
10540 const unsigned char *dwo_id)
10542 /* These attributes will be found in the full debug_info section. */
10543 remove_AT (comp_unit, DW_AT_producer);
10544 remove_AT (comp_unit, DW_AT_language);
10546 switch_to_section (debug_skeleton_info_section);
10547 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10549 /* Produce the skeleton compilation-unit header. This one differs enough from
10550 a normal CU header that it's better not to call output_compilation_unit
10551 header. */
10552 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10553 dw2_asm_output_data (4, 0xffffffff,
10554 "Initial length escape value indicating 64-bit "
10555 "DWARF extension");
10557 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10558 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10559 - DWARF_INITIAL_LENGTH_SIZE
10560 + size_of_die (comp_unit),
10561 "Length of Compilation Unit Info");
10562 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10563 if (dwarf_version >= 5)
10565 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10566 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10568 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10569 debug_skeleton_abbrev_section,
10570 "Offset Into Abbrev. Section");
10571 if (dwarf_version < 5)
10572 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10573 else
10574 for (int i = 0; i < 8; i++)
10575 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10577 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10578 output_die (comp_unit);
10580 /* Build the skeleton debug_abbrev section. */
10581 switch_to_section (debug_skeleton_abbrev_section);
10582 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10584 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10586 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10589 /* Output a comdat type unit DIE and its children. */
10591 static void
10592 output_comdat_type_unit (comdat_type_node *node)
10594 const char *secname;
10595 char *tmp;
10596 int i;
10597 #if defined (OBJECT_FORMAT_ELF)
10598 tree comdat_key;
10599 #endif
10601 /* First mark all the DIEs in this CU so we know which get local refs. */
10602 mark_dies (node->root_die);
10604 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10606 build_abbrev_table (node->root_die, extern_map);
10608 delete extern_map;
10609 extern_map = NULL;
10611 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10612 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10613 calc_die_sizes (node->root_die);
10615 #if defined (OBJECT_FORMAT_ELF)
10616 if (dwarf_version >= 5)
10618 if (!dwarf_split_debug_info)
10619 secname = ".debug_info";
10620 else
10621 secname = ".debug_info.dwo";
10623 else if (!dwarf_split_debug_info)
10624 secname = ".debug_types";
10625 else
10626 secname = ".debug_types.dwo";
10628 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10629 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10630 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10631 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10632 comdat_key = get_identifier (tmp);
10633 targetm.asm_out.named_section (secname,
10634 SECTION_DEBUG | SECTION_LINKONCE,
10635 comdat_key);
10636 #else
10637 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10638 sprintf (tmp, (dwarf_version >= 5
10639 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10640 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10641 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10642 secname = tmp;
10643 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10644 #endif
10646 /* Output debugging information. */
10647 output_compilation_unit_header (dwarf_split_debug_info
10648 ? DW_UT_split_type : DW_UT_type);
10649 output_signature (node->signature, "Type Signature");
10650 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10651 "Offset to Type DIE");
10652 output_die (node->root_die);
10654 unmark_dies (node->root_die);
10657 /* Return the DWARF2/3 pubname associated with a decl. */
10659 static const char *
10660 dwarf2_name (tree decl, int scope)
10662 if (DECL_NAMELESS (decl))
10663 return NULL;
10664 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10667 /* Add a new entry to .debug_pubnames if appropriate. */
10669 static void
10670 add_pubname_string (const char *str, dw_die_ref die)
10672 pubname_entry e;
10674 e.die = die;
10675 e.name = xstrdup (str);
10676 vec_safe_push (pubname_table, e);
10679 static void
10680 add_pubname (tree decl, dw_die_ref die)
10682 if (!want_pubnames ())
10683 return;
10685 /* Don't add items to the table when we expect that the consumer will have
10686 just read the enclosing die. For example, if the consumer is looking at a
10687 class_member, it will either be inside the class already, or will have just
10688 looked up the class to find the member. Either way, searching the class is
10689 faster than searching the index. */
10690 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10691 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10693 const char *name = dwarf2_name (decl, 1);
10695 if (name)
10696 add_pubname_string (name, die);
10700 /* Add an enumerator to the pubnames section. */
10702 static void
10703 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10705 pubname_entry e;
10707 gcc_assert (scope_name);
10708 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10709 e.die = die;
10710 vec_safe_push (pubname_table, e);
10713 /* Add a new entry to .debug_pubtypes if appropriate. */
10715 static void
10716 add_pubtype (tree decl, dw_die_ref die)
10718 pubname_entry e;
10720 if (!want_pubnames ())
10721 return;
10723 if ((TREE_PUBLIC (decl)
10724 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10725 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10727 tree scope = NULL;
10728 const char *scope_name = "";
10729 const char *sep = is_cxx () ? "::" : ".";
10730 const char *name;
10732 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10733 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10735 scope_name = lang_hooks.dwarf_name (scope, 1);
10736 if (scope_name != NULL && scope_name[0] != '\0')
10737 scope_name = concat (scope_name, sep, NULL);
10738 else
10739 scope_name = "";
10742 if (TYPE_P (decl))
10743 name = type_tag (decl);
10744 else
10745 name = lang_hooks.dwarf_name (decl, 1);
10747 /* If we don't have a name for the type, there's no point in adding
10748 it to the table. */
10749 if (name != NULL && name[0] != '\0')
10751 e.die = die;
10752 e.name = concat (scope_name, name, NULL);
10753 vec_safe_push (pubtype_table, e);
10756 /* Although it might be more consistent to add the pubinfo for the
10757 enumerators as their dies are created, they should only be added if the
10758 enum type meets the criteria above. So rather than re-check the parent
10759 enum type whenever an enumerator die is created, just output them all
10760 here. This isn't protected by the name conditional because anonymous
10761 enums don't have names. */
10762 if (die->die_tag == DW_TAG_enumeration_type)
10764 dw_die_ref c;
10766 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10771 /* Output a single entry in the pubnames table. */
10773 static void
10774 output_pubname (dw_offset die_offset, pubname_entry *entry)
10776 dw_die_ref die = entry->die;
10777 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10779 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10781 if (debug_generate_pub_sections == 2)
10783 /* This logic follows gdb's method for determining the value of the flag
10784 byte. */
10785 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10786 switch (die->die_tag)
10788 case DW_TAG_typedef:
10789 case DW_TAG_base_type:
10790 case DW_TAG_subrange_type:
10791 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10792 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10793 break;
10794 case DW_TAG_enumerator:
10795 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10796 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10797 if (!is_cxx ())
10798 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10799 break;
10800 case DW_TAG_subprogram:
10801 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10802 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10803 if (!is_ada ())
10804 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10805 break;
10806 case DW_TAG_constant:
10807 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10808 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10809 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10810 break;
10811 case DW_TAG_variable:
10812 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10813 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10814 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10815 break;
10816 case DW_TAG_namespace:
10817 case DW_TAG_imported_declaration:
10818 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10819 break;
10820 case DW_TAG_class_type:
10821 case DW_TAG_interface_type:
10822 case DW_TAG_structure_type:
10823 case DW_TAG_union_type:
10824 case DW_TAG_enumeration_type:
10825 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10826 if (!is_cxx ())
10827 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10828 break;
10829 default:
10830 /* An unusual tag. Leave the flag-byte empty. */
10831 break;
10833 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10834 "GDB-index flags");
10837 dw2_asm_output_nstring (entry->name, -1, "external name");
10841 /* Output the public names table used to speed up access to externally
10842 visible names; or the public types table used to find type definitions. */
10844 static void
10845 output_pubnames (vec<pubname_entry, va_gc> *names)
10847 unsigned i;
10848 unsigned long pubnames_length = size_of_pubnames (names);
10849 pubname_entry *pub;
10851 if (!XCOFF_DEBUGGING_INFO)
10853 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10854 dw2_asm_output_data (4, 0xffffffff,
10855 "Initial length escape value indicating 64-bit DWARF extension");
10856 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10857 "Pub Info Length");
10860 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10861 dw2_asm_output_data (2, 2, "DWARF Version");
10863 if (dwarf_split_debug_info)
10864 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10865 debug_skeleton_info_section,
10866 "Offset of Compilation Unit Info");
10867 else
10868 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10869 debug_info_section,
10870 "Offset of Compilation Unit Info");
10871 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10872 "Compilation Unit Length");
10874 FOR_EACH_VEC_ELT (*names, i, pub)
10876 if (include_pubname_in_output (names, pub))
10878 dw_offset die_offset = pub->die->die_offset;
10880 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10881 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10882 gcc_assert (pub->die->die_mark);
10884 /* If we're putting types in their own .debug_types sections,
10885 the .debug_pubtypes table will still point to the compile
10886 unit (not the type unit), so we want to use the offset of
10887 the skeleton DIE (if there is one). */
10888 if (pub->die->comdat_type_p && names == pubtype_table)
10890 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10892 if (type_node != NULL)
10893 die_offset = (type_node->skeleton_die != NULL
10894 ? type_node->skeleton_die->die_offset
10895 : comp_unit_die ()->die_offset);
10898 output_pubname (die_offset, pub);
10902 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10905 /* Output public names and types tables if necessary. */
10907 static void
10908 output_pubtables (void)
10910 if (!want_pubnames () || !info_section_emitted)
10911 return;
10913 switch_to_section (debug_pubnames_section);
10914 output_pubnames (pubname_table);
10915 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10916 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10917 simply won't look for the section. */
10918 switch_to_section (debug_pubtypes_section);
10919 output_pubnames (pubtype_table);
10923 /* Output the information that goes into the .debug_aranges table.
10924 Namely, define the beginning and ending address range of the
10925 text section generated for this compilation unit. */
10927 static void
10928 output_aranges (void)
10930 unsigned i;
10931 unsigned long aranges_length = size_of_aranges ();
10933 if (!XCOFF_DEBUGGING_INFO)
10935 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10936 dw2_asm_output_data (4, 0xffffffff,
10937 "Initial length escape value indicating 64-bit DWARF extension");
10938 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10939 "Length of Address Ranges Info");
10942 /* Version number for aranges is still 2, even up to DWARF5. */
10943 dw2_asm_output_data (2, 2, "DWARF Version");
10944 if (dwarf_split_debug_info)
10945 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10946 debug_skeleton_info_section,
10947 "Offset of Compilation Unit Info");
10948 else
10949 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10950 debug_info_section,
10951 "Offset of Compilation Unit Info");
10952 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10953 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10955 /* We need to align to twice the pointer size here. */
10956 if (DWARF_ARANGES_PAD_SIZE)
10958 /* Pad using a 2 byte words so that padding is correct for any
10959 pointer size. */
10960 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10961 2 * DWARF2_ADDR_SIZE);
10962 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10963 dw2_asm_output_data (2, 0, NULL);
10966 /* It is necessary not to output these entries if the sections were
10967 not used; if the sections were not used, the length will be 0 and
10968 the address may end up as 0 if the section is discarded by ld
10969 --gc-sections, leaving an invalid (0, 0) entry that can be
10970 confused with the terminator. */
10971 if (text_section_used)
10973 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10974 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10975 text_section_label, "Length");
10977 if (cold_text_section_used)
10979 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10980 "Address");
10981 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10982 cold_text_section_label, "Length");
10985 if (have_multiple_function_sections)
10987 unsigned fde_idx;
10988 dw_fde_ref fde;
10990 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10992 if (DECL_IGNORED_P (fde->decl))
10993 continue;
10994 if (!fde->in_std_section)
10996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10997 "Address");
10998 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10999 fde->dw_fde_begin, "Length");
11001 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11003 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11004 "Address");
11005 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11006 fde->dw_fde_second_begin, "Length");
11011 /* Output the terminator words. */
11012 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11013 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11016 /* Add a new entry to .debug_ranges. Return its index into
11017 ranges_table vector. */
11019 static unsigned int
11020 add_ranges_num (int num, bool maybe_new_sec)
11022 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11023 vec_safe_push (ranges_table, r);
11024 return vec_safe_length (ranges_table) - 1;
11027 /* Add a new entry to .debug_ranges corresponding to a block, or a
11028 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11029 this entry might be in a different section from previous range. */
11031 static unsigned int
11032 add_ranges (const_tree block, bool maybe_new_sec)
11034 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11037 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11038 chain, or middle entry of a chain that will be directly referred to. */
11040 static void
11041 note_rnglist_head (unsigned int offset)
11043 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11044 return;
11045 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11048 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11049 When using dwarf_split_debug_info, address attributes in dies destined
11050 for the final executable should be direct references--setting the
11051 parameter force_direct ensures this behavior. */
11053 static void
11054 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11055 bool *added, bool force_direct)
11057 unsigned int in_use = vec_safe_length (ranges_by_label);
11058 unsigned int offset;
11059 dw_ranges_by_label rbl = { begin, end };
11060 vec_safe_push (ranges_by_label, rbl);
11061 offset = add_ranges_num (-(int)in_use - 1, true);
11062 if (!*added)
11064 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11065 *added = true;
11066 note_rnglist_head (offset);
11070 /* Emit .debug_ranges section. */
11072 static void
11073 output_ranges (void)
11075 unsigned i;
11076 static const char *const start_fmt = "Offset %#x";
11077 const char *fmt = start_fmt;
11078 dw_ranges *r;
11080 switch_to_section (debug_ranges_section);
11081 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11082 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11084 int block_num = r->num;
11086 if (block_num > 0)
11088 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11089 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11091 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11092 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11094 /* If all code is in the text section, then the compilation
11095 unit base address defaults to DW_AT_low_pc, which is the
11096 base of the text section. */
11097 if (!have_multiple_function_sections)
11099 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11100 text_section_label,
11101 fmt, i * 2 * DWARF2_ADDR_SIZE);
11102 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11103 text_section_label, NULL);
11106 /* Otherwise, the compilation unit base address is zero,
11107 which allows us to use absolute addresses, and not worry
11108 about whether the target supports cross-section
11109 arithmetic. */
11110 else
11112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11113 fmt, i * 2 * DWARF2_ADDR_SIZE);
11114 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11117 fmt = NULL;
11120 /* Negative block_num stands for an index into ranges_by_label. */
11121 else if (block_num < 0)
11123 int lab_idx = - block_num - 1;
11125 if (!have_multiple_function_sections)
11127 gcc_unreachable ();
11128 #if 0
11129 /* If we ever use add_ranges_by_labels () for a single
11130 function section, all we have to do is to take out
11131 the #if 0 above. */
11132 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11133 (*ranges_by_label)[lab_idx].begin,
11134 text_section_label,
11135 fmt, i * 2 * DWARF2_ADDR_SIZE);
11136 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11137 (*ranges_by_label)[lab_idx].end,
11138 text_section_label, NULL);
11139 #endif
11141 else
11143 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11144 (*ranges_by_label)[lab_idx].begin,
11145 fmt, i * 2 * DWARF2_ADDR_SIZE);
11146 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11147 (*ranges_by_label)[lab_idx].end,
11148 NULL);
11151 else
11153 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11154 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11155 fmt = start_fmt;
11160 /* Non-zero if .debug_line_str should be used for .debug_line section
11161 strings or strings that are likely shareable with those. */
11162 #define DWARF5_USE_DEBUG_LINE_STR \
11163 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11164 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11165 /* FIXME: there is no .debug_line_str.dwo section, \
11166 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11167 && !dwarf_split_debug_info)
11169 /* Assign .debug_rnglists indexes. */
11171 static void
11172 index_rnglists (void)
11174 unsigned i;
11175 dw_ranges *r;
11177 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11178 if (r->label)
11179 r->idx = rnglist_idx++;
11182 /* Emit .debug_rnglists section. */
11184 static void
11185 output_rnglists (unsigned generation)
11187 unsigned i;
11188 dw_ranges *r;
11189 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11190 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11191 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11193 switch_to_section (debug_ranges_section);
11194 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11195 /* There are up to 4 unique ranges labels per generation.
11196 See also init_sections_and_labels. */
11197 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11198 2 + generation * 4);
11199 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11200 3 + generation * 4);
11201 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11202 dw2_asm_output_data (4, 0xffffffff,
11203 "Initial length escape value indicating "
11204 "64-bit DWARF extension");
11205 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11206 "Length of Range Lists");
11207 ASM_OUTPUT_LABEL (asm_out_file, l1);
11208 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11209 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11210 dw2_asm_output_data (1, 0, "Segment Size");
11211 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11212 about relocation sizes and primarily care about the size of .debug*
11213 sections in linked shared libraries and executables, then
11214 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11215 into it are usually larger than just DW_FORM_sec_offset offsets
11216 into the .debug_rnglists section. */
11217 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11218 "Offset Entry Count");
11219 if (dwarf_split_debug_info)
11221 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11222 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11223 if (r->label)
11224 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11225 ranges_base_label, NULL);
11228 const char *lab = "";
11229 unsigned int len = vec_safe_length (ranges_table);
11230 const char *base = NULL;
11231 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11233 int block_num = r->num;
11235 if (r->label)
11237 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11238 lab = r->label;
11240 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11241 base = NULL;
11242 if (block_num > 0)
11244 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11245 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11247 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11248 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11250 if (HAVE_AS_LEB128)
11252 /* If all code is in the text section, then the compilation
11253 unit base address defaults to DW_AT_low_pc, which is the
11254 base of the text section. */
11255 if (!have_multiple_function_sections)
11257 dw2_asm_output_data (1, DW_RLE_offset_pair,
11258 "DW_RLE_offset_pair (%s)", lab);
11259 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11260 "Range begin address (%s)", lab);
11261 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11262 "Range end address (%s)", lab);
11263 continue;
11265 if (base == NULL)
11267 dw_ranges *r2 = NULL;
11268 if (i < len - 1)
11269 r2 = &(*ranges_table)[i + 1];
11270 if (r2
11271 && r2->num != 0
11272 && r2->label == NULL
11273 && !r2->maybe_new_sec)
11275 dw2_asm_output_data (1, DW_RLE_base_address,
11276 "DW_RLE_base_address (%s)", lab);
11277 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11278 "Base address (%s)", lab);
11279 strcpy (basebuf, blabel);
11280 base = basebuf;
11283 if (base)
11285 dw2_asm_output_data (1, DW_RLE_offset_pair,
11286 "DW_RLE_offset_pair (%s)", lab);
11287 dw2_asm_output_delta_uleb128 (blabel, base,
11288 "Range begin address (%s)", lab);
11289 dw2_asm_output_delta_uleb128 (elabel, base,
11290 "Range end address (%s)", lab);
11291 continue;
11293 dw2_asm_output_data (1, DW_RLE_start_length,
11294 "DW_RLE_start_length (%s)", lab);
11295 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11296 "Range begin address (%s)", lab);
11297 dw2_asm_output_delta_uleb128 (elabel, blabel,
11298 "Range length (%s)", lab);
11300 else
11302 dw2_asm_output_data (1, DW_RLE_start_end,
11303 "DW_RLE_start_end (%s)", lab);
11304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11305 "Range begin address (%s)", lab);
11306 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11307 "Range end address (%s)", lab);
11311 /* Negative block_num stands for an index into ranges_by_label. */
11312 else if (block_num < 0)
11314 int lab_idx = - block_num - 1;
11315 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11316 const char *elabel = (*ranges_by_label)[lab_idx].end;
11318 if (!have_multiple_function_sections)
11319 gcc_unreachable ();
11320 if (HAVE_AS_LEB128)
11322 dw2_asm_output_data (1, DW_RLE_start_length,
11323 "DW_RLE_start_length (%s)", lab);
11324 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11325 "Range begin address (%s)", lab);
11326 dw2_asm_output_delta_uleb128 (elabel, blabel,
11327 "Range length (%s)", lab);
11329 else
11331 dw2_asm_output_data (1, DW_RLE_start_end,
11332 "DW_RLE_start_end (%s)", lab);
11333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11334 "Range begin address (%s)", lab);
11335 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11336 "Range end address (%s)", lab);
11339 else
11340 dw2_asm_output_data (1, DW_RLE_end_of_list,
11341 "DW_RLE_end_of_list (%s)", lab);
11343 ASM_OUTPUT_LABEL (asm_out_file, l2);
11346 /* Data structure containing information about input files. */
11347 struct file_info
11349 const char *path; /* Complete file name. */
11350 const char *fname; /* File name part. */
11351 int length; /* Length of entire string. */
11352 struct dwarf_file_data * file_idx; /* Index in input file table. */
11353 int dir_idx; /* Index in directory table. */
11356 /* Data structure containing information about directories with source
11357 files. */
11358 struct dir_info
11360 const char *path; /* Path including directory name. */
11361 int length; /* Path length. */
11362 int prefix; /* Index of directory entry which is a prefix. */
11363 int count; /* Number of files in this directory. */
11364 int dir_idx; /* Index of directory used as base. */
11367 /* Callback function for file_info comparison. We sort by looking at
11368 the directories in the path. */
11370 static int
11371 file_info_cmp (const void *p1, const void *p2)
11373 const struct file_info *const s1 = (const struct file_info *) p1;
11374 const struct file_info *const s2 = (const struct file_info *) p2;
11375 const unsigned char *cp1;
11376 const unsigned char *cp2;
11378 /* Take care of file names without directories. We need to make sure that
11379 we return consistent values to qsort since some will get confused if
11380 we return the same value when identical operands are passed in opposite
11381 orders. So if neither has a directory, return 0 and otherwise return
11382 1 or -1 depending on which one has the directory. */
11383 if ((s1->path == s1->fname || s2->path == s2->fname))
11384 return (s2->path == s2->fname) - (s1->path == s1->fname);
11386 cp1 = (const unsigned char *) s1->path;
11387 cp2 = (const unsigned char *) s2->path;
11389 while (1)
11391 ++cp1;
11392 ++cp2;
11393 /* Reached the end of the first path? If so, handle like above. */
11394 if ((cp1 == (const unsigned char *) s1->fname)
11395 || (cp2 == (const unsigned char *) s2->fname))
11396 return ((cp2 == (const unsigned char *) s2->fname)
11397 - (cp1 == (const unsigned char *) s1->fname));
11399 /* Character of current path component the same? */
11400 else if (*cp1 != *cp2)
11401 return *cp1 - *cp2;
11405 struct file_name_acquire_data
11407 struct file_info *files;
11408 int used_files;
11409 int max_files;
11412 /* Traversal function for the hash table. */
11415 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11417 struct dwarf_file_data *d = *slot;
11418 struct file_info *fi;
11419 const char *f;
11421 gcc_assert (fnad->max_files >= d->emitted_number);
11423 if (! d->emitted_number)
11424 return 1;
11426 gcc_assert (fnad->max_files != fnad->used_files);
11428 fi = fnad->files + fnad->used_files++;
11430 /* Skip all leading "./". */
11431 f = d->filename;
11432 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11433 f += 2;
11435 /* Create a new array entry. */
11436 fi->path = f;
11437 fi->length = strlen (f);
11438 fi->file_idx = d;
11440 /* Search for the file name part. */
11441 f = strrchr (f, DIR_SEPARATOR);
11442 #if defined (DIR_SEPARATOR_2)
11444 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11446 if (g != NULL)
11448 if (f == NULL || f < g)
11449 f = g;
11452 #endif
11454 fi->fname = f == NULL ? fi->path : f + 1;
11455 return 1;
11458 /* Helper function for output_file_names. Emit a FORM encoded
11459 string STR, with assembly comment start ENTRY_KIND and
11460 index IDX */
11462 static void
11463 output_line_string (enum dwarf_form form, const char *str,
11464 const char *entry_kind, unsigned int idx)
11466 switch (form)
11468 case DW_FORM_string:
11469 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11470 break;
11471 case DW_FORM_line_strp:
11472 if (!debug_line_str_hash)
11473 debug_line_str_hash
11474 = hash_table<indirect_string_hasher>::create_ggc (10);
11476 struct indirect_string_node *node;
11477 node = find_AT_string_in_table (str, debug_line_str_hash);
11478 set_indirect_string (node);
11479 node->form = form;
11480 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11481 debug_line_str_section, "%s: %#x: \"%s\"",
11482 entry_kind, 0, node->str);
11483 break;
11484 default:
11485 gcc_unreachable ();
11489 /* Output the directory table and the file name table. We try to minimize
11490 the total amount of memory needed. A heuristic is used to avoid large
11491 slowdowns with many input files. */
11493 static void
11494 output_file_names (void)
11496 struct file_name_acquire_data fnad;
11497 int numfiles;
11498 struct file_info *files;
11499 struct dir_info *dirs;
11500 int *saved;
11501 int *savehere;
11502 int *backmap;
11503 int ndirs;
11504 int idx_offset;
11505 int i;
11507 if (!last_emitted_file)
11509 if (dwarf_version >= 5)
11511 dw2_asm_output_data (1, 0, "Directory entry format count");
11512 dw2_asm_output_data_uleb128 (0, "Directories count");
11513 dw2_asm_output_data (1, 0, "File name entry format count");
11514 dw2_asm_output_data_uleb128 (0, "File names count");
11516 else
11518 dw2_asm_output_data (1, 0, "End directory table");
11519 dw2_asm_output_data (1, 0, "End file name table");
11521 return;
11524 numfiles = last_emitted_file->emitted_number;
11526 /* Allocate the various arrays we need. */
11527 files = XALLOCAVEC (struct file_info, numfiles);
11528 dirs = XALLOCAVEC (struct dir_info, numfiles);
11530 fnad.files = files;
11531 fnad.used_files = 0;
11532 fnad.max_files = numfiles;
11533 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11534 gcc_assert (fnad.used_files == fnad.max_files);
11536 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11538 /* Find all the different directories used. */
11539 dirs[0].path = files[0].path;
11540 dirs[0].length = files[0].fname - files[0].path;
11541 dirs[0].prefix = -1;
11542 dirs[0].count = 1;
11543 dirs[0].dir_idx = 0;
11544 files[0].dir_idx = 0;
11545 ndirs = 1;
11547 for (i = 1; i < numfiles; i++)
11548 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11549 && memcmp (dirs[ndirs - 1].path, files[i].path,
11550 dirs[ndirs - 1].length) == 0)
11552 /* Same directory as last entry. */
11553 files[i].dir_idx = ndirs - 1;
11554 ++dirs[ndirs - 1].count;
11556 else
11558 int j;
11560 /* This is a new directory. */
11561 dirs[ndirs].path = files[i].path;
11562 dirs[ndirs].length = files[i].fname - files[i].path;
11563 dirs[ndirs].count = 1;
11564 dirs[ndirs].dir_idx = ndirs;
11565 files[i].dir_idx = ndirs;
11567 /* Search for a prefix. */
11568 dirs[ndirs].prefix = -1;
11569 for (j = 0; j < ndirs; j++)
11570 if (dirs[j].length < dirs[ndirs].length
11571 && dirs[j].length > 1
11572 && (dirs[ndirs].prefix == -1
11573 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11574 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11575 dirs[ndirs].prefix = j;
11577 ++ndirs;
11580 /* Now to the actual work. We have to find a subset of the directories which
11581 allow expressing the file name using references to the directory table
11582 with the least amount of characters. We do not do an exhaustive search
11583 where we would have to check out every combination of every single
11584 possible prefix. Instead we use a heuristic which provides nearly optimal
11585 results in most cases and never is much off. */
11586 saved = XALLOCAVEC (int, ndirs);
11587 savehere = XALLOCAVEC (int, ndirs);
11589 memset (saved, '\0', ndirs * sizeof (saved[0]));
11590 for (i = 0; i < ndirs; i++)
11592 int j;
11593 int total;
11595 /* We can always save some space for the current directory. But this
11596 does not mean it will be enough to justify adding the directory. */
11597 savehere[i] = dirs[i].length;
11598 total = (savehere[i] - saved[i]) * dirs[i].count;
11600 for (j = i + 1; j < ndirs; j++)
11602 savehere[j] = 0;
11603 if (saved[j] < dirs[i].length)
11605 /* Determine whether the dirs[i] path is a prefix of the
11606 dirs[j] path. */
11607 int k;
11609 k = dirs[j].prefix;
11610 while (k != -1 && k != (int) i)
11611 k = dirs[k].prefix;
11613 if (k == (int) i)
11615 /* Yes it is. We can possibly save some memory by
11616 writing the filenames in dirs[j] relative to
11617 dirs[i]. */
11618 savehere[j] = dirs[i].length;
11619 total += (savehere[j] - saved[j]) * dirs[j].count;
11624 /* Check whether we can save enough to justify adding the dirs[i]
11625 directory. */
11626 if (total > dirs[i].length + 1)
11628 /* It's worthwhile adding. */
11629 for (j = i; j < ndirs; j++)
11630 if (savehere[j] > 0)
11632 /* Remember how much we saved for this directory so far. */
11633 saved[j] = savehere[j];
11635 /* Remember the prefix directory. */
11636 dirs[j].dir_idx = i;
11641 /* Emit the directory name table. */
11642 idx_offset = dirs[0].length > 0 ? 1 : 0;
11643 enum dwarf_form str_form = DW_FORM_string;
11644 enum dwarf_form idx_form = DW_FORM_udata;
11645 if (dwarf_version >= 5)
11647 const char *comp_dir = comp_dir_string ();
11648 if (comp_dir == NULL)
11649 comp_dir = "";
11650 dw2_asm_output_data (1, 1, "Directory entry format count");
11651 if (DWARF5_USE_DEBUG_LINE_STR)
11652 str_form = DW_FORM_line_strp;
11653 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11654 dw2_asm_output_data_uleb128 (str_form, "%s",
11655 get_DW_FORM_name (str_form));
11656 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11657 if (str_form == DW_FORM_string)
11659 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11660 for (i = 1 - idx_offset; i < ndirs; i++)
11661 dw2_asm_output_nstring (dirs[i].path,
11662 dirs[i].length
11663 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11664 "Directory Entry: %#x", i + idx_offset);
11666 else
11668 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11669 for (i = 1 - idx_offset; i < ndirs; i++)
11671 const char *str
11672 = ggc_alloc_string (dirs[i].path,
11673 dirs[i].length
11674 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11675 output_line_string (str_form, str, "Directory Entry",
11676 (unsigned) i + idx_offset);
11680 else
11682 for (i = 1 - idx_offset; i < ndirs; i++)
11683 dw2_asm_output_nstring (dirs[i].path,
11684 dirs[i].length
11685 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11686 "Directory Entry: %#x", i + idx_offset);
11688 dw2_asm_output_data (1, 0, "End directory table");
11691 /* We have to emit them in the order of emitted_number since that's
11692 used in the debug info generation. To do this efficiently we
11693 generate a back-mapping of the indices first. */
11694 backmap = XALLOCAVEC (int, numfiles);
11695 for (i = 0; i < numfiles; i++)
11696 backmap[files[i].file_idx->emitted_number - 1] = i;
11698 if (dwarf_version >= 5)
11700 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11701 if (filename0 == NULL)
11702 filename0 = "";
11703 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11704 DW_FORM_data2. Choose one based on the number of directories
11705 and how much space would they occupy in each encoding.
11706 If we have at most 256 directories, all indexes fit into
11707 a single byte, so DW_FORM_data1 is most compact (if there
11708 are at most 128 directories, DW_FORM_udata would be as
11709 compact as that, but not shorter and slower to decode). */
11710 if (ndirs + idx_offset <= 256)
11711 idx_form = DW_FORM_data1;
11712 /* If there are more than 65536 directories, we have to use
11713 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11714 Otherwise, compute what space would occupy if all the indexes
11715 used DW_FORM_udata - sum - and compare that to how large would
11716 be DW_FORM_data2 encoding, and pick the more efficient one. */
11717 else if (ndirs + idx_offset <= 65536)
11719 unsigned HOST_WIDE_INT sum = 1;
11720 for (i = 0; i < numfiles; i++)
11722 int file_idx = backmap[i];
11723 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11724 sum += size_of_uleb128 (dir_idx);
11726 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11727 idx_form = DW_FORM_data2;
11729 #ifdef VMS_DEBUGGING_INFO
11730 dw2_asm_output_data (1, 4, "File name entry format count");
11731 #else
11732 dw2_asm_output_data (1, 2, "File name entry format count");
11733 #endif
11734 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11735 dw2_asm_output_data_uleb128 (str_form, "%s",
11736 get_DW_FORM_name (str_form));
11737 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11738 "DW_LNCT_directory_index");
11739 dw2_asm_output_data_uleb128 (idx_form, "%s",
11740 get_DW_FORM_name (idx_form));
11741 #ifdef VMS_DEBUGGING_INFO
11742 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11743 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11744 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11745 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11746 #endif
11747 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11749 output_line_string (str_form, filename0, "File Entry", 0);
11751 /* Include directory index. */
11752 if (idx_form != DW_FORM_udata)
11753 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11754 0, NULL);
11755 else
11756 dw2_asm_output_data_uleb128 (0, NULL);
11758 #ifdef VMS_DEBUGGING_INFO
11759 dw2_asm_output_data_uleb128 (0, NULL);
11760 dw2_asm_output_data_uleb128 (0, NULL);
11761 #endif
11764 /* Now write all the file names. */
11765 for (i = 0; i < numfiles; i++)
11767 int file_idx = backmap[i];
11768 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11770 #ifdef VMS_DEBUGGING_INFO
11771 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11773 /* Setting these fields can lead to debugger miscomparisons,
11774 but VMS Debug requires them to be set correctly. */
11776 int ver;
11777 long long cdt;
11778 long siz;
11779 int maxfilelen = (strlen (files[file_idx].path)
11780 + dirs[dir_idx].length
11781 + MAX_VMS_VERSION_LEN + 1);
11782 char *filebuf = XALLOCAVEC (char, maxfilelen);
11784 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11785 snprintf (filebuf, maxfilelen, "%s;%d",
11786 files[file_idx].path + dirs[dir_idx].length, ver);
11788 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11790 /* Include directory index. */
11791 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11792 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11793 dir_idx + idx_offset, NULL);
11794 else
11795 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11797 /* Modification time. */
11798 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11799 &cdt, 0, 0, 0) == 0)
11800 ? cdt : 0, NULL);
11802 /* File length in bytes. */
11803 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11804 0, &siz, 0, 0) == 0)
11805 ? siz : 0, NULL);
11806 #else
11807 output_line_string (str_form,
11808 files[file_idx].path + dirs[dir_idx].length,
11809 "File Entry", (unsigned) i + 1);
11811 /* Include directory index. */
11812 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11813 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11814 dir_idx + idx_offset, NULL);
11815 else
11816 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11818 if (dwarf_version >= 5)
11819 continue;
11821 /* Modification time. */
11822 dw2_asm_output_data_uleb128 (0, NULL);
11824 /* File length in bytes. */
11825 dw2_asm_output_data_uleb128 (0, NULL);
11826 #endif /* VMS_DEBUGGING_INFO */
11829 if (dwarf_version < 5)
11830 dw2_asm_output_data (1, 0, "End file name table");
11834 /* Output one line number table into the .debug_line section. */
11836 static void
11837 output_one_line_info_table (dw_line_info_table *table)
11839 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11840 unsigned int current_line = 1;
11841 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11842 dw_line_info_entry *ent;
11843 size_t i;
11845 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11847 switch (ent->opcode)
11849 case LI_set_address:
11850 /* ??? Unfortunately, we have little choice here currently, and
11851 must always use the most general form. GCC does not know the
11852 address delta itself, so we can't use DW_LNS_advance_pc. Many
11853 ports do have length attributes which will give an upper bound
11854 on the address range. We could perhaps use length attributes
11855 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11856 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11858 /* This can handle any delta. This takes
11859 4+DWARF2_ADDR_SIZE bytes. */
11860 dw2_asm_output_data (1, 0, "set address %s", line_label);
11861 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11862 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11863 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11864 break;
11866 case LI_set_line:
11867 if (ent->val == current_line)
11869 /* We still need to start a new row, so output a copy insn. */
11870 dw2_asm_output_data (1, DW_LNS_copy,
11871 "copy line %u", current_line);
11873 else
11875 int line_offset = ent->val - current_line;
11876 int line_delta = line_offset - DWARF_LINE_BASE;
11878 current_line = ent->val;
11879 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11881 /* This can handle deltas from -10 to 234, using the current
11882 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11883 This takes 1 byte. */
11884 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11885 "line %u", current_line);
11887 else
11889 /* This can handle any delta. This takes at least 4 bytes,
11890 depending on the value being encoded. */
11891 dw2_asm_output_data (1, DW_LNS_advance_line,
11892 "advance to line %u", current_line);
11893 dw2_asm_output_data_sleb128 (line_offset, NULL);
11894 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11897 break;
11899 case LI_set_file:
11900 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11901 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11902 break;
11904 case LI_set_column:
11905 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11906 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11907 break;
11909 case LI_negate_stmt:
11910 current_is_stmt = !current_is_stmt;
11911 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11912 "is_stmt %d", current_is_stmt);
11913 break;
11915 case LI_set_prologue_end:
11916 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11917 "set prologue end");
11918 break;
11920 case LI_set_epilogue_begin:
11921 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11922 "set epilogue begin");
11923 break;
11925 case LI_set_discriminator:
11926 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11927 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11928 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11929 dw2_asm_output_data_uleb128 (ent->val, NULL);
11930 break;
11934 /* Emit debug info for the address of the end of the table. */
11935 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11936 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11937 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11938 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11940 dw2_asm_output_data (1, 0, "end sequence");
11941 dw2_asm_output_data_uleb128 (1, NULL);
11942 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11945 /* Output the source line number correspondence information. This
11946 information goes into the .debug_line section. */
11948 static void
11949 output_line_info (bool prologue_only)
11951 static unsigned int generation;
11952 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11953 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11954 bool saw_one = false;
11955 int opc;
11957 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11958 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11959 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11960 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11962 if (!XCOFF_DEBUGGING_INFO)
11964 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11965 dw2_asm_output_data (4, 0xffffffff,
11966 "Initial length escape value indicating 64-bit DWARF extension");
11967 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11968 "Length of Source Line Info");
11971 ASM_OUTPUT_LABEL (asm_out_file, l1);
11973 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11974 if (dwarf_version >= 5)
11976 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11977 dw2_asm_output_data (1, 0, "Segment Size");
11979 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11980 ASM_OUTPUT_LABEL (asm_out_file, p1);
11982 /* Define the architecture-dependent minimum instruction length (in bytes).
11983 In this implementation of DWARF, this field is used for information
11984 purposes only. Since GCC generates assembly language, we have no
11985 a priori knowledge of how many instruction bytes are generated for each
11986 source line, and therefore can use only the DW_LNE_set_address and
11987 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11988 this as '1', which is "correct enough" for all architectures,
11989 and don't let the target override. */
11990 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11992 if (dwarf_version >= 4)
11993 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
11994 "Maximum Operations Per Instruction");
11995 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11996 "Default is_stmt_start flag");
11997 dw2_asm_output_data (1, DWARF_LINE_BASE,
11998 "Line Base Value (Special Opcodes)");
11999 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12000 "Line Range Value (Special Opcodes)");
12001 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12002 "Special Opcode Base");
12004 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12006 int n_op_args;
12007 switch (opc)
12009 case DW_LNS_advance_pc:
12010 case DW_LNS_advance_line:
12011 case DW_LNS_set_file:
12012 case DW_LNS_set_column:
12013 case DW_LNS_fixed_advance_pc:
12014 case DW_LNS_set_isa:
12015 n_op_args = 1;
12016 break;
12017 default:
12018 n_op_args = 0;
12019 break;
12022 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12023 opc, n_op_args);
12026 /* Write out the information about the files we use. */
12027 output_file_names ();
12028 ASM_OUTPUT_LABEL (asm_out_file, p2);
12029 if (prologue_only)
12031 /* Output the marker for the end of the line number info. */
12032 ASM_OUTPUT_LABEL (asm_out_file, l2);
12033 return;
12036 if (separate_line_info)
12038 dw_line_info_table *table;
12039 size_t i;
12041 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12042 if (table->in_use)
12044 output_one_line_info_table (table);
12045 saw_one = true;
12048 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12050 output_one_line_info_table (cold_text_section_line_info);
12051 saw_one = true;
12054 /* ??? Some Darwin linkers crash on a .debug_line section with no
12055 sequences. Further, merely a DW_LNE_end_sequence entry is not
12056 sufficient -- the address column must also be initialized.
12057 Make sure to output at least one set_address/end_sequence pair,
12058 choosing .text since that section is always present. */
12059 if (text_section_line_info->in_use || !saw_one)
12060 output_one_line_info_table (text_section_line_info);
12062 /* Output the marker for the end of the line number info. */
12063 ASM_OUTPUT_LABEL (asm_out_file, l2);
12066 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12068 static inline bool
12069 need_endianity_attribute_p (bool reverse)
12071 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12074 /* Given a pointer to a tree node for some base type, return a pointer to
12075 a DIE that describes the given type. REVERSE is true if the type is
12076 to be interpreted in the reverse storage order wrt the target order.
12078 This routine must only be called for GCC type nodes that correspond to
12079 Dwarf base (fundamental) types. */
12081 static dw_die_ref
12082 base_type_die (tree type, bool reverse)
12084 dw_die_ref base_type_result;
12085 enum dwarf_type encoding;
12086 bool fpt_used = false;
12087 struct fixed_point_type_info fpt_info;
12088 tree type_bias = NULL_TREE;
12090 /* If this is a subtype that should not be emitted as a subrange type,
12091 use the base type. See subrange_type_for_debug_p. */
12092 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12093 type = TREE_TYPE (type);
12095 switch (TREE_CODE (type))
12097 case INTEGER_TYPE:
12098 if ((dwarf_version >= 4 || !dwarf_strict)
12099 && TYPE_NAME (type)
12100 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12101 && DECL_IS_BUILTIN (TYPE_NAME (type))
12102 && DECL_NAME (TYPE_NAME (type)))
12104 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12105 if (strcmp (name, "char16_t") == 0
12106 || strcmp (name, "char32_t") == 0)
12108 encoding = DW_ATE_UTF;
12109 break;
12112 if ((dwarf_version >= 3 || !dwarf_strict)
12113 && lang_hooks.types.get_fixed_point_type_info)
12115 memset (&fpt_info, 0, sizeof (fpt_info));
12116 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12118 fpt_used = true;
12119 encoding = ((TYPE_UNSIGNED (type))
12120 ? DW_ATE_unsigned_fixed
12121 : DW_ATE_signed_fixed);
12122 break;
12125 if (TYPE_STRING_FLAG (type))
12127 if (TYPE_UNSIGNED (type))
12128 encoding = DW_ATE_unsigned_char;
12129 else
12130 encoding = DW_ATE_signed_char;
12132 else if (TYPE_UNSIGNED (type))
12133 encoding = DW_ATE_unsigned;
12134 else
12135 encoding = DW_ATE_signed;
12137 if (!dwarf_strict
12138 && lang_hooks.types.get_type_bias)
12139 type_bias = lang_hooks.types.get_type_bias (type);
12140 break;
12142 case REAL_TYPE:
12143 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12145 if (dwarf_version >= 3 || !dwarf_strict)
12146 encoding = DW_ATE_decimal_float;
12147 else
12148 encoding = DW_ATE_lo_user;
12150 else
12151 encoding = DW_ATE_float;
12152 break;
12154 case FIXED_POINT_TYPE:
12155 if (!(dwarf_version >= 3 || !dwarf_strict))
12156 encoding = DW_ATE_lo_user;
12157 else if (TYPE_UNSIGNED (type))
12158 encoding = DW_ATE_unsigned_fixed;
12159 else
12160 encoding = DW_ATE_signed_fixed;
12161 break;
12163 /* Dwarf2 doesn't know anything about complex ints, so use
12164 a user defined type for it. */
12165 case COMPLEX_TYPE:
12166 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12167 encoding = DW_ATE_complex_float;
12168 else
12169 encoding = DW_ATE_lo_user;
12170 break;
12172 case BOOLEAN_TYPE:
12173 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12174 encoding = DW_ATE_boolean;
12175 break;
12177 default:
12178 /* No other TREE_CODEs are Dwarf fundamental types. */
12179 gcc_unreachable ();
12182 base_type_result = new_die_raw (DW_TAG_base_type);
12184 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12185 int_size_in_bytes (type));
12186 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12188 if (need_endianity_attribute_p (reverse))
12189 add_AT_unsigned (base_type_result, DW_AT_endianity,
12190 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12192 add_alignment_attribute (base_type_result, type);
12194 if (fpt_used)
12196 switch (fpt_info.scale_factor_kind)
12198 case fixed_point_scale_factor_binary:
12199 add_AT_int (base_type_result, DW_AT_binary_scale,
12200 fpt_info.scale_factor.binary);
12201 break;
12203 case fixed_point_scale_factor_decimal:
12204 add_AT_int (base_type_result, DW_AT_decimal_scale,
12205 fpt_info.scale_factor.decimal);
12206 break;
12208 case fixed_point_scale_factor_arbitrary:
12209 /* Arbitrary scale factors cannot be described in standard DWARF,
12210 yet. */
12211 if (!dwarf_strict)
12213 /* Describe the scale factor as a rational constant. */
12214 const dw_die_ref scale_factor
12215 = new_die (DW_TAG_constant, comp_unit_die (), type);
12217 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12218 fpt_info.scale_factor.arbitrary.numerator);
12219 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12220 fpt_info.scale_factor.arbitrary.denominator);
12222 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12224 break;
12226 default:
12227 gcc_unreachable ();
12231 if (type_bias)
12232 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12233 dw_scalar_form_constant
12234 | dw_scalar_form_exprloc
12235 | dw_scalar_form_reference,
12236 NULL);
12238 return base_type_result;
12241 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12242 named 'auto' in its type: return true for it, false otherwise. */
12244 static inline bool
12245 is_cxx_auto (tree type)
12247 if (is_cxx ())
12249 tree name = TYPE_IDENTIFIER (type);
12250 if (name == get_identifier ("auto")
12251 || name == get_identifier ("decltype(auto)"))
12252 return true;
12254 return false;
12257 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12258 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12260 static inline int
12261 is_base_type (tree type)
12263 switch (TREE_CODE (type))
12265 case INTEGER_TYPE:
12266 case REAL_TYPE:
12267 case FIXED_POINT_TYPE:
12268 case COMPLEX_TYPE:
12269 case BOOLEAN_TYPE:
12270 case POINTER_BOUNDS_TYPE:
12271 return 1;
12273 case VOID_TYPE:
12274 case ARRAY_TYPE:
12275 case RECORD_TYPE:
12276 case UNION_TYPE:
12277 case QUAL_UNION_TYPE:
12278 case ENUMERAL_TYPE:
12279 case FUNCTION_TYPE:
12280 case METHOD_TYPE:
12281 case POINTER_TYPE:
12282 case REFERENCE_TYPE:
12283 case NULLPTR_TYPE:
12284 case OFFSET_TYPE:
12285 case LANG_TYPE:
12286 case VECTOR_TYPE:
12287 return 0;
12289 default:
12290 if (is_cxx_auto (type))
12291 return 0;
12292 gcc_unreachable ();
12295 return 0;
12298 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12299 node, return the size in bits for the type if it is a constant, or else
12300 return the alignment for the type if the type's size is not constant, or
12301 else return BITS_PER_WORD if the type actually turns out to be an
12302 ERROR_MARK node. */
12304 static inline unsigned HOST_WIDE_INT
12305 simple_type_size_in_bits (const_tree type)
12307 if (TREE_CODE (type) == ERROR_MARK)
12308 return BITS_PER_WORD;
12309 else if (TYPE_SIZE (type) == NULL_TREE)
12310 return 0;
12311 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12312 return tree_to_uhwi (TYPE_SIZE (type));
12313 else
12314 return TYPE_ALIGN (type);
12317 /* Similarly, but return an offset_int instead of UHWI. */
12319 static inline offset_int
12320 offset_int_type_size_in_bits (const_tree type)
12322 if (TREE_CODE (type) == ERROR_MARK)
12323 return BITS_PER_WORD;
12324 else if (TYPE_SIZE (type) == NULL_TREE)
12325 return 0;
12326 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12327 return wi::to_offset (TYPE_SIZE (type));
12328 else
12329 return TYPE_ALIGN (type);
12332 /* Given a pointer to a tree node for a subrange type, return a pointer
12333 to a DIE that describes the given type. */
12335 static dw_die_ref
12336 subrange_type_die (tree type, tree low, tree high, tree bias,
12337 dw_die_ref context_die)
12339 dw_die_ref subrange_die;
12340 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12342 if (context_die == NULL)
12343 context_die = comp_unit_die ();
12345 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12347 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12349 /* The size of the subrange type and its base type do not match,
12350 so we need to generate a size attribute for the subrange type. */
12351 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12354 add_alignment_attribute (subrange_die, type);
12356 if (low)
12357 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12358 if (high)
12359 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12360 if (bias && !dwarf_strict)
12361 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12362 dw_scalar_form_constant
12363 | dw_scalar_form_exprloc
12364 | dw_scalar_form_reference,
12365 NULL);
12367 return subrange_die;
12370 /* Returns the (const and/or volatile) cv_qualifiers associated with
12371 the decl node. This will normally be augmented with the
12372 cv_qualifiers of the underlying type in add_type_attribute. */
12374 static int
12375 decl_quals (const_tree decl)
12377 return ((TREE_READONLY (decl)
12378 /* The C++ front-end correctly marks reference-typed
12379 variables as readonly, but from a language (and debug
12380 info) standpoint they are not const-qualified. */
12381 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12382 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12383 | (TREE_THIS_VOLATILE (decl)
12384 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12387 /* Determine the TYPE whose qualifiers match the largest strict subset
12388 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12389 qualifiers outside QUAL_MASK. */
12391 static int
12392 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12394 tree t;
12395 int best_rank = 0, best_qual = 0, max_rank;
12397 type_quals &= qual_mask;
12398 max_rank = popcount_hwi (type_quals) - 1;
12400 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12401 t = TYPE_NEXT_VARIANT (t))
12403 int q = TYPE_QUALS (t) & qual_mask;
12405 if ((q & type_quals) == q && q != type_quals
12406 && check_base_type (t, type))
12408 int rank = popcount_hwi (q);
12410 if (rank > best_rank)
12412 best_rank = rank;
12413 best_qual = q;
12418 return best_qual;
12421 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12422 static const dwarf_qual_info_t dwarf_qual_info[] =
12424 { TYPE_QUAL_CONST, DW_TAG_const_type },
12425 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12426 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12427 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12429 static const unsigned int dwarf_qual_info_size
12430 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12432 /* If DIE is a qualified DIE of some base DIE with the same parent,
12433 return the base DIE, otherwise return NULL. Set MASK to the
12434 qualifiers added compared to the returned DIE. */
12436 static dw_die_ref
12437 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12439 unsigned int i;
12440 for (i = 0; i < dwarf_qual_info_size; i++)
12441 if (die->die_tag == dwarf_qual_info[i].t)
12442 break;
12443 if (i == dwarf_qual_info_size)
12444 return NULL;
12445 if (vec_safe_length (die->die_attr) != 1)
12446 return NULL;
12447 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12448 if (type == NULL || type->die_parent != die->die_parent)
12449 return NULL;
12450 *mask |= dwarf_qual_info[i].q;
12451 if (depth)
12453 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12454 if (ret)
12455 return ret;
12457 return type;
12460 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12461 entry that chains the modifiers specified by CV_QUALS in front of the
12462 given type. REVERSE is true if the type is to be interpreted in the
12463 reverse storage order wrt the target order. */
12465 static dw_die_ref
12466 modified_type_die (tree type, int cv_quals, bool reverse,
12467 dw_die_ref context_die)
12469 enum tree_code code = TREE_CODE (type);
12470 dw_die_ref mod_type_die;
12471 dw_die_ref sub_die = NULL;
12472 tree item_type = NULL;
12473 tree qualified_type;
12474 tree name, low, high;
12475 dw_die_ref mod_scope;
12476 /* Only these cv-qualifiers are currently handled. */
12477 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12478 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12479 ENCODE_QUAL_ADDR_SPACE(~0U));
12480 const bool reverse_base_type
12481 = need_endianity_attribute_p (reverse) && is_base_type (type);
12483 if (code == ERROR_MARK)
12484 return NULL;
12486 if (lang_hooks.types.get_debug_type)
12488 tree debug_type = lang_hooks.types.get_debug_type (type);
12490 if (debug_type != NULL_TREE && debug_type != type)
12491 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12494 cv_quals &= cv_qual_mask;
12496 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12497 tag modifier (and not an attribute) old consumers won't be able
12498 to handle it. */
12499 if (dwarf_version < 3)
12500 cv_quals &= ~TYPE_QUAL_RESTRICT;
12502 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12503 if (dwarf_version < 5)
12504 cv_quals &= ~TYPE_QUAL_ATOMIC;
12506 /* See if we already have the appropriately qualified variant of
12507 this type. */
12508 qualified_type = get_qualified_type (type, cv_quals);
12510 if (qualified_type == sizetype)
12512 /* Try not to expose the internal sizetype type's name. */
12513 if (TYPE_NAME (qualified_type)
12514 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12516 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12518 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12519 && (TYPE_PRECISION (t)
12520 == TYPE_PRECISION (qualified_type))
12521 && (TYPE_UNSIGNED (t)
12522 == TYPE_UNSIGNED (qualified_type)));
12523 qualified_type = t;
12525 else if (qualified_type == sizetype
12526 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12527 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12528 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12529 qualified_type = size_type_node;
12532 /* If we do, then we can just use its DIE, if it exists. */
12533 if (qualified_type)
12535 mod_type_die = lookup_type_die (qualified_type);
12537 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12538 dealt with specially: the DIE with the attribute, if it exists, is
12539 placed immediately after the regular DIE for the same base type. */
12540 if (mod_type_die
12541 && (!reverse_base_type
12542 || ((mod_type_die = mod_type_die->die_sib) != NULL
12543 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12544 return mod_type_die;
12547 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12549 /* Handle C typedef types. */
12550 if (name
12551 && TREE_CODE (name) == TYPE_DECL
12552 && DECL_ORIGINAL_TYPE (name)
12553 && !DECL_ARTIFICIAL (name))
12555 tree dtype = TREE_TYPE (name);
12557 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12558 if (qualified_type == dtype && !reverse_base_type)
12560 tree origin = decl_ultimate_origin (name);
12562 /* Typedef variants that have an abstract origin don't get their own
12563 type DIE (see gen_typedef_die), so fall back on the ultimate
12564 abstract origin instead. */
12565 if (origin != NULL && origin != name)
12566 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12567 context_die);
12569 /* For a named type, use the typedef. */
12570 gen_type_die (qualified_type, context_die);
12571 return lookup_type_die (qualified_type);
12573 else
12575 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12576 dquals &= cv_qual_mask;
12577 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12578 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12579 /* cv-unqualified version of named type. Just use
12580 the unnamed type to which it refers. */
12581 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12582 reverse, context_die);
12583 /* Else cv-qualified version of named type; fall through. */
12587 mod_scope = scope_die_for (type, context_die);
12589 if (cv_quals)
12591 int sub_quals = 0, first_quals = 0;
12592 unsigned i;
12593 dw_die_ref first = NULL, last = NULL;
12595 /* Determine a lesser qualified type that most closely matches
12596 this one. Then generate DW_TAG_* entries for the remaining
12597 qualifiers. */
12598 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12599 cv_qual_mask);
12600 if (sub_quals && use_debug_types)
12602 bool needed = false;
12603 /* If emitting type units, make sure the order of qualifiers
12604 is canonical. Thus, start from unqualified type if
12605 an earlier qualifier is missing in sub_quals, but some later
12606 one is present there. */
12607 for (i = 0; i < dwarf_qual_info_size; i++)
12608 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12609 needed = true;
12610 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12612 sub_quals = 0;
12613 break;
12616 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12617 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12619 /* As not all intermediate qualified DIEs have corresponding
12620 tree types, ensure that qualified DIEs in the same scope
12621 as their DW_AT_type are emitted after their DW_AT_type,
12622 only with other qualified DIEs for the same type possibly
12623 in between them. Determine the range of such qualified
12624 DIEs now (first being the base type, last being corresponding
12625 last qualified DIE for it). */
12626 unsigned int count = 0;
12627 first = qualified_die_p (mod_type_die, &first_quals,
12628 dwarf_qual_info_size);
12629 if (first == NULL)
12630 first = mod_type_die;
12631 gcc_assert ((first_quals & ~sub_quals) == 0);
12632 for (count = 0, last = first;
12633 count < (1U << dwarf_qual_info_size);
12634 count++, last = last->die_sib)
12636 int quals = 0;
12637 if (last == mod_scope->die_child)
12638 break;
12639 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12640 != first)
12641 break;
12645 for (i = 0; i < dwarf_qual_info_size; i++)
12646 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12648 dw_die_ref d;
12649 if (first && first != last)
12651 for (d = first->die_sib; ; d = d->die_sib)
12653 int quals = 0;
12654 qualified_die_p (d, &quals, dwarf_qual_info_size);
12655 if (quals == (first_quals | dwarf_qual_info[i].q))
12656 break;
12657 if (d == last)
12659 d = NULL;
12660 break;
12663 if (d)
12665 mod_type_die = d;
12666 continue;
12669 if (first)
12671 d = new_die_raw (dwarf_qual_info[i].t);
12672 add_child_die_after (mod_scope, d, last);
12673 last = d;
12675 else
12676 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12677 if (mod_type_die)
12678 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12679 mod_type_die = d;
12680 first_quals |= dwarf_qual_info[i].q;
12683 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12685 dwarf_tag tag = DW_TAG_pointer_type;
12686 if (code == REFERENCE_TYPE)
12688 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12689 tag = DW_TAG_rvalue_reference_type;
12690 else
12691 tag = DW_TAG_reference_type;
12693 mod_type_die = new_die (tag, mod_scope, type);
12695 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12696 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12697 add_alignment_attribute (mod_type_die, type);
12698 item_type = TREE_TYPE (type);
12700 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12701 if (!ADDR_SPACE_GENERIC_P (as))
12703 int action = targetm.addr_space.debug (as);
12704 if (action >= 0)
12706 /* Positive values indicate an address_class. */
12707 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12709 else
12711 /* Negative values indicate an (inverted) segment base reg. */
12712 dw_loc_descr_ref d
12713 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12714 add_AT_loc (mod_type_die, DW_AT_segment, d);
12718 else if (code == INTEGER_TYPE
12719 && TREE_TYPE (type) != NULL_TREE
12720 && subrange_type_for_debug_p (type, &low, &high))
12722 tree bias = NULL_TREE;
12723 if (lang_hooks.types.get_type_bias)
12724 bias = lang_hooks.types.get_type_bias (type);
12725 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12726 item_type = TREE_TYPE (type);
12728 else if (is_base_type (type))
12730 mod_type_die = base_type_die (type, reverse);
12732 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12733 if (reverse_base_type)
12735 dw_die_ref after_die
12736 = modified_type_die (type, cv_quals, false, context_die);
12737 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12739 else
12740 add_child_die (comp_unit_die (), mod_type_die);
12742 add_pubtype (type, mod_type_die);
12744 else
12746 gen_type_die (type, context_die);
12748 /* We have to get the type_main_variant here (and pass that to the
12749 `lookup_type_die' routine) because the ..._TYPE node we have
12750 might simply be a *copy* of some original type node (where the
12751 copy was created to help us keep track of typedef names) and
12752 that copy might have a different TYPE_UID from the original
12753 ..._TYPE node. */
12754 if (TREE_CODE (type) == FUNCTION_TYPE
12755 || TREE_CODE (type) == METHOD_TYPE)
12757 /* For function/method types, can't just use type_main_variant here,
12758 because that can have different ref-qualifiers for C++,
12759 but try to canonicalize. */
12760 tree main = TYPE_MAIN_VARIANT (type);
12761 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12762 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12763 && check_base_type (t, main)
12764 && check_lang_type (t, type))
12765 return lookup_type_die (t);
12766 return lookup_type_die (type);
12768 else if (TREE_CODE (type) != VECTOR_TYPE
12769 && TREE_CODE (type) != ARRAY_TYPE)
12770 return lookup_type_die (type_main_variant (type));
12771 else
12772 /* Vectors have the debugging information in the type,
12773 not the main variant. */
12774 return lookup_type_die (type);
12777 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12778 don't output a DW_TAG_typedef, since there isn't one in the
12779 user's program; just attach a DW_AT_name to the type.
12780 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12781 if the base type already has the same name. */
12782 if (name
12783 && ((TREE_CODE (name) != TYPE_DECL
12784 && (qualified_type == TYPE_MAIN_VARIANT (type)
12785 || (cv_quals == TYPE_UNQUALIFIED)))
12786 || (TREE_CODE (name) == TYPE_DECL
12787 && TREE_TYPE (name) == qualified_type
12788 && DECL_NAME (name))))
12790 if (TREE_CODE (name) == TYPE_DECL)
12791 /* Could just call add_name_and_src_coords_attributes here,
12792 but since this is a builtin type it doesn't have any
12793 useful source coordinates anyway. */
12794 name = DECL_NAME (name);
12795 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12797 /* This probably indicates a bug. */
12798 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12800 name = TYPE_IDENTIFIER (type);
12801 add_name_attribute (mod_type_die,
12802 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12805 if (qualified_type && !reverse_base_type)
12806 equate_type_number_to_die (qualified_type, mod_type_die);
12808 if (item_type)
12809 /* We must do this after the equate_type_number_to_die call, in case
12810 this is a recursive type. This ensures that the modified_type_die
12811 recursion will terminate even if the type is recursive. Recursive
12812 types are possible in Ada. */
12813 sub_die = modified_type_die (item_type,
12814 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12815 reverse,
12816 context_die);
12818 if (sub_die != NULL)
12819 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12821 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12822 if (TYPE_ARTIFICIAL (type))
12823 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12825 return mod_type_die;
12828 /* Generate DIEs for the generic parameters of T.
12829 T must be either a generic type or a generic function.
12830 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12832 static void
12833 gen_generic_params_dies (tree t)
12835 tree parms, args;
12836 int parms_num, i;
12837 dw_die_ref die = NULL;
12838 int non_default;
12840 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12841 return;
12843 if (TYPE_P (t))
12844 die = lookup_type_die (t);
12845 else if (DECL_P (t))
12846 die = lookup_decl_die (t);
12848 gcc_assert (die);
12850 parms = lang_hooks.get_innermost_generic_parms (t);
12851 if (!parms)
12852 /* T has no generic parameter. It means T is neither a generic type
12853 or function. End of story. */
12854 return;
12856 parms_num = TREE_VEC_LENGTH (parms);
12857 args = lang_hooks.get_innermost_generic_args (t);
12858 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12859 non_default = int_cst_value (TREE_CHAIN (args));
12860 else
12861 non_default = TREE_VEC_LENGTH (args);
12862 for (i = 0; i < parms_num; i++)
12864 tree parm, arg, arg_pack_elems;
12865 dw_die_ref parm_die;
12867 parm = TREE_VEC_ELT (parms, i);
12868 arg = TREE_VEC_ELT (args, i);
12869 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12870 gcc_assert (parm && TREE_VALUE (parm) && arg);
12872 if (parm && TREE_VALUE (parm) && arg)
12874 /* If PARM represents a template parameter pack,
12875 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12876 by DW_TAG_template_*_parameter DIEs for the argument
12877 pack elements of ARG. Note that ARG would then be
12878 an argument pack. */
12879 if (arg_pack_elems)
12880 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12881 arg_pack_elems,
12882 die);
12883 else
12884 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12885 true /* emit name */, die);
12886 if (i >= non_default)
12887 add_AT_flag (parm_die, DW_AT_default_value, 1);
12892 /* Create and return a DIE for PARM which should be
12893 the representation of a generic type parameter.
12894 For instance, in the C++ front end, PARM would be a template parameter.
12895 ARG is the argument to PARM.
12896 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12897 name of the PARM.
12898 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12899 as a child node. */
12901 static dw_die_ref
12902 generic_parameter_die (tree parm, tree arg,
12903 bool emit_name_p,
12904 dw_die_ref parent_die)
12906 dw_die_ref tmpl_die = NULL;
12907 const char *name = NULL;
12909 if (!parm || !DECL_NAME (parm) || !arg)
12910 return NULL;
12912 /* We support non-type generic parameters and arguments,
12913 type generic parameters and arguments, as well as
12914 generic generic parameters (a.k.a. template template parameters in C++)
12915 and arguments. */
12916 if (TREE_CODE (parm) == PARM_DECL)
12917 /* PARM is a nontype generic parameter */
12918 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12919 else if (TREE_CODE (parm) == TYPE_DECL)
12920 /* PARM is a type generic parameter. */
12921 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12922 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12923 /* PARM is a generic generic parameter.
12924 Its DIE is a GNU extension. It shall have a
12925 DW_AT_name attribute to represent the name of the template template
12926 parameter, and a DW_AT_GNU_template_name attribute to represent the
12927 name of the template template argument. */
12928 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12929 parent_die, parm);
12930 else
12931 gcc_unreachable ();
12933 if (tmpl_die)
12935 tree tmpl_type;
12937 /* If PARM is a generic parameter pack, it means we are
12938 emitting debug info for a template argument pack element.
12939 In other terms, ARG is a template argument pack element.
12940 In that case, we don't emit any DW_AT_name attribute for
12941 the die. */
12942 if (emit_name_p)
12944 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12945 gcc_assert (name);
12946 add_AT_string (tmpl_die, DW_AT_name, name);
12949 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12951 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12952 TMPL_DIE should have a child DW_AT_type attribute that is set
12953 to the type of the argument to PARM, which is ARG.
12954 If PARM is a type generic parameter, TMPL_DIE should have a
12955 child DW_AT_type that is set to ARG. */
12956 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12957 add_type_attribute (tmpl_die, tmpl_type,
12958 (TREE_THIS_VOLATILE (tmpl_type)
12959 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12960 false, parent_die);
12962 else
12964 /* So TMPL_DIE is a DIE representing a
12965 a generic generic template parameter, a.k.a template template
12966 parameter in C++ and arg is a template. */
12968 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12969 to the name of the argument. */
12970 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12971 if (name)
12972 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12975 if (TREE_CODE (parm) == PARM_DECL)
12976 /* So PARM is a non-type generic parameter.
12977 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12978 attribute of TMPL_DIE which value represents the value
12979 of ARG.
12980 We must be careful here:
12981 The value of ARG might reference some function decls.
12982 We might currently be emitting debug info for a generic
12983 type and types are emitted before function decls, we don't
12984 know if the function decls referenced by ARG will actually be
12985 emitted after cgraph computations.
12986 So must defer the generation of the DW_AT_const_value to
12987 after cgraph is ready. */
12988 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12991 return tmpl_die;
12994 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12995 PARM_PACK must be a template parameter pack. The returned DIE
12996 will be child DIE of PARENT_DIE. */
12998 static dw_die_ref
12999 template_parameter_pack_die (tree parm_pack,
13000 tree parm_pack_args,
13001 dw_die_ref parent_die)
13003 dw_die_ref die;
13004 int j;
13006 gcc_assert (parent_die && parm_pack);
13008 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13009 add_name_and_src_coords_attributes (die, parm_pack);
13010 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13011 generic_parameter_die (parm_pack,
13012 TREE_VEC_ELT (parm_pack_args, j),
13013 false /* Don't emit DW_AT_name */,
13014 die);
13015 return die;
13018 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13019 an enumerated type. */
13021 static inline int
13022 type_is_enum (const_tree type)
13024 return TREE_CODE (type) == ENUMERAL_TYPE;
13027 /* Return the DBX register number described by a given RTL node. */
13029 static unsigned int
13030 dbx_reg_number (const_rtx rtl)
13032 unsigned regno = REGNO (rtl);
13034 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13036 #ifdef LEAF_REG_REMAP
13037 if (crtl->uses_only_leaf_regs)
13039 int leaf_reg = LEAF_REG_REMAP (regno);
13040 if (leaf_reg != -1)
13041 regno = (unsigned) leaf_reg;
13043 #endif
13045 regno = DBX_REGISTER_NUMBER (regno);
13046 gcc_assert (regno != INVALID_REGNUM);
13047 return regno;
13050 /* Optionally add a DW_OP_piece term to a location description expression.
13051 DW_OP_piece is only added if the location description expression already
13052 doesn't end with DW_OP_piece. */
13054 static void
13055 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13057 dw_loc_descr_ref loc;
13059 if (*list_head != NULL)
13061 /* Find the end of the chain. */
13062 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13065 if (loc->dw_loc_opc != DW_OP_piece)
13066 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13070 /* Return a location descriptor that designates a machine register or
13071 zero if there is none. */
13073 static dw_loc_descr_ref
13074 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13076 rtx regs;
13078 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13079 return 0;
13081 /* We only use "frame base" when we're sure we're talking about the
13082 post-prologue local stack frame. We do this by *not* running
13083 register elimination until this point, and recognizing the special
13084 argument pointer and soft frame pointer rtx's.
13085 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13086 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13087 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13089 dw_loc_descr_ref result = NULL;
13091 if (dwarf_version >= 4 || !dwarf_strict)
13093 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13094 initialized);
13095 if (result)
13096 add_loc_descr (&result,
13097 new_loc_descr (DW_OP_stack_value, 0, 0));
13099 return result;
13102 regs = targetm.dwarf_register_span (rtl);
13104 if (REG_NREGS (rtl) > 1 || regs)
13105 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13106 else
13108 unsigned int dbx_regnum = dbx_reg_number (rtl);
13109 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13110 return 0;
13111 return one_reg_loc_descriptor (dbx_regnum, initialized);
13115 /* Return a location descriptor that designates a machine register for
13116 a given hard register number. */
13118 static dw_loc_descr_ref
13119 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13121 dw_loc_descr_ref reg_loc_descr;
13123 if (regno <= 31)
13124 reg_loc_descr
13125 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13126 else
13127 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13129 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13130 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13132 return reg_loc_descr;
13135 /* Given an RTL of a register, return a location descriptor that
13136 designates a value that spans more than one register. */
13138 static dw_loc_descr_ref
13139 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13140 enum var_init_status initialized)
13142 int size, i;
13143 dw_loc_descr_ref loc_result = NULL;
13145 /* Simple, contiguous registers. */
13146 if (regs == NULL_RTX)
13148 unsigned reg = REGNO (rtl);
13149 int nregs;
13151 #ifdef LEAF_REG_REMAP
13152 if (crtl->uses_only_leaf_regs)
13154 int leaf_reg = LEAF_REG_REMAP (reg);
13155 if (leaf_reg != -1)
13156 reg = (unsigned) leaf_reg;
13158 #endif
13160 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13161 nregs = REG_NREGS (rtl);
13163 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13165 loc_result = NULL;
13166 while (nregs--)
13168 dw_loc_descr_ref t;
13170 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13171 VAR_INIT_STATUS_INITIALIZED);
13172 add_loc_descr (&loc_result, t);
13173 add_loc_descr_op_piece (&loc_result, size);
13174 ++reg;
13176 return loc_result;
13179 /* Now onto stupid register sets in non contiguous locations. */
13181 gcc_assert (GET_CODE (regs) == PARALLEL);
13183 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13184 loc_result = NULL;
13186 for (i = 0; i < XVECLEN (regs, 0); ++i)
13188 dw_loc_descr_ref t;
13190 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13191 VAR_INIT_STATUS_INITIALIZED);
13192 add_loc_descr (&loc_result, t);
13193 add_loc_descr_op_piece (&loc_result, size);
13196 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13197 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13198 return loc_result;
13201 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13203 /* Return a location descriptor that designates a constant i,
13204 as a compound operation from constant (i >> shift), constant shift
13205 and DW_OP_shl. */
13207 static dw_loc_descr_ref
13208 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13210 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13211 add_loc_descr (&ret, int_loc_descriptor (shift));
13212 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13213 return ret;
13216 /* Return a location descriptor that designates a constant. */
13218 static dw_loc_descr_ref
13219 int_loc_descriptor (HOST_WIDE_INT i)
13221 enum dwarf_location_atom op;
13223 /* Pick the smallest representation of a constant, rather than just
13224 defaulting to the LEB encoding. */
13225 if (i >= 0)
13227 int clz = clz_hwi (i);
13228 int ctz = ctz_hwi (i);
13229 if (i <= 31)
13230 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13231 else if (i <= 0xff)
13232 op = DW_OP_const1u;
13233 else if (i <= 0xffff)
13234 op = DW_OP_const2u;
13235 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13236 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13237 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13238 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13239 while DW_OP_const4u is 5 bytes. */
13240 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13241 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13242 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13243 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13244 while DW_OP_const4u is 5 bytes. */
13245 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13247 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13248 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13249 <= 4)
13251 /* As i >= 2**31, the double cast above will yield a negative number.
13252 Since wrapping is defined in DWARF expressions we can output big
13253 positive integers as small negative ones, regardless of the size
13254 of host wide ints.
13256 Here, since the evaluator will handle 32-bit values and since i >=
13257 2**31, we know it's going to be interpreted as a negative literal:
13258 store it this way if we can do better than 5 bytes this way. */
13259 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13261 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13262 op = DW_OP_const4u;
13264 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13265 least 6 bytes: see if we can do better before falling back to it. */
13266 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13267 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13268 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13269 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13270 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13271 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13272 >= HOST_BITS_PER_WIDE_INT)
13273 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13274 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13275 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13276 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13277 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13278 && size_of_uleb128 (i) > 6)
13279 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13280 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13281 else
13282 op = DW_OP_constu;
13284 else
13286 if (i >= -0x80)
13287 op = DW_OP_const1s;
13288 else if (i >= -0x8000)
13289 op = DW_OP_const2s;
13290 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13292 if (size_of_int_loc_descriptor (i) < 5)
13294 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13295 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13296 return ret;
13298 op = DW_OP_const4s;
13300 else
13302 if (size_of_int_loc_descriptor (i)
13303 < (unsigned long) 1 + size_of_sleb128 (i))
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_consts;
13313 return new_loc_descr (op, i, 0);
13316 /* Likewise, for unsigned constants. */
13318 static dw_loc_descr_ref
13319 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13321 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13322 const unsigned HOST_WIDE_INT max_uint
13323 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13325 /* If possible, use the clever signed constants handling. */
13326 if (i <= max_int)
13327 return int_loc_descriptor ((HOST_WIDE_INT) i);
13329 /* Here, we are left with positive numbers that cannot be represented as
13330 HOST_WIDE_INT, i.e.:
13331 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13333 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13334 whereas may be better to output a negative integer: thanks to integer
13335 wrapping, we know that:
13336 x = x - 2 ** DWARF2_ADDR_SIZE
13337 = x - 2 * (max (HOST_WIDE_INT) + 1)
13338 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13339 small negative integers. Let's try that in cases it will clearly improve
13340 the encoding: there is no gain turning DW_OP_const4u into
13341 DW_OP_const4s. */
13342 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13343 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13344 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13346 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13348 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13349 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13350 const HOST_WIDE_INT second_shift
13351 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13353 /* So we finally have:
13354 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13355 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13356 return int_loc_descriptor (second_shift);
13359 /* Last chance: fallback to a simple constant operation. */
13360 return new_loc_descr
13361 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13362 ? DW_OP_const4u
13363 : DW_OP_const8u,
13364 i, 0);
13367 /* Generate and return a location description that computes the unsigned
13368 comparison of the two stack top entries (a OP b where b is the top-most
13369 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13370 LE_EXPR, GT_EXPR or GE_EXPR. */
13372 static dw_loc_descr_ref
13373 uint_comparison_loc_list (enum tree_code kind)
13375 enum dwarf_location_atom op, flip_op;
13376 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13378 switch (kind)
13380 case LT_EXPR:
13381 op = DW_OP_lt;
13382 break;
13383 case LE_EXPR:
13384 op = DW_OP_le;
13385 break;
13386 case GT_EXPR:
13387 op = DW_OP_gt;
13388 break;
13389 case GE_EXPR:
13390 op = DW_OP_ge;
13391 break;
13392 default:
13393 gcc_unreachable ();
13396 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13397 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13399 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13400 possible to perform unsigned comparisons: we just have to distinguish
13401 three cases:
13403 1. when a and b have the same sign (as signed integers); then we should
13404 return: a OP(signed) b;
13406 2. when a is a negative signed integer while b is a positive one, then a
13407 is a greater unsigned integer than b; likewise when a and b's roles
13408 are flipped.
13410 So first, compare the sign of the two operands. */
13411 ret = new_loc_descr (DW_OP_over, 0, 0);
13412 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13413 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13414 /* If they have different signs (i.e. they have different sign bits), then
13415 the stack top value has now the sign bit set and thus it's smaller than
13416 zero. */
13417 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13418 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13419 add_loc_descr (&ret, bra_node);
13421 /* We are in case 1. At this point, we know both operands have the same
13422 sign, to it's safe to use the built-in signed comparison. */
13423 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13424 add_loc_descr (&ret, jmp_node);
13426 /* We are in case 2. Here, we know both operands do not have the same sign,
13427 so we have to flip the signed comparison. */
13428 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13429 tmp = new_loc_descr (flip_op, 0, 0);
13430 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13431 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13432 add_loc_descr (&ret, tmp);
13434 /* This dummy operation is necessary to make the two branches join. */
13435 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13436 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13437 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13438 add_loc_descr (&ret, tmp);
13440 return ret;
13443 /* Likewise, but takes the location description lists (might be destructive on
13444 them). Return NULL if either is NULL or if concatenation fails. */
13446 static dw_loc_list_ref
13447 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13448 enum tree_code kind)
13450 if (left == NULL || right == NULL)
13451 return NULL;
13453 add_loc_list (&left, right);
13454 if (left == NULL)
13455 return NULL;
13457 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13458 return left;
13461 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13462 without actually allocating it. */
13464 static unsigned long
13465 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13467 return size_of_int_loc_descriptor (i >> shift)
13468 + size_of_int_loc_descriptor (shift)
13469 + 1;
13472 /* Return size_of_locs (int_loc_descriptor (i)) without
13473 actually allocating it. */
13475 static unsigned long
13476 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13478 unsigned long s;
13480 if (i >= 0)
13482 int clz, ctz;
13483 if (i <= 31)
13484 return 1;
13485 else if (i <= 0xff)
13486 return 2;
13487 else if (i <= 0xffff)
13488 return 3;
13489 clz = clz_hwi (i);
13490 ctz = ctz_hwi (i);
13491 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13492 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13493 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13494 - clz - 5);
13495 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13496 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13497 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13498 - clz - 8);
13499 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13500 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13501 <= 4)
13502 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13503 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13504 return 5;
13505 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13506 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13507 && clz + 8 + 255 >= 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 (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13511 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13512 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13513 - clz - 16);
13514 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13515 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13516 && s > 6)
13517 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13518 - clz - 32);
13519 else
13520 return 1 + s;
13522 else
13524 if (i >= -0x80)
13525 return 2;
13526 else if (i >= -0x8000)
13527 return 3;
13528 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13530 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13532 s = size_of_int_loc_descriptor (-i) + 1;
13533 if (s < 5)
13534 return s;
13536 return 5;
13538 else
13540 unsigned long r = 1 + size_of_sleb128 (i);
13541 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13543 s = size_of_int_loc_descriptor (-i) + 1;
13544 if (s < r)
13545 return s;
13547 return r;
13552 /* Return loc description representing "address" of integer value.
13553 This can appear only as toplevel expression. */
13555 static dw_loc_descr_ref
13556 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13558 int litsize;
13559 dw_loc_descr_ref loc_result = NULL;
13561 if (!(dwarf_version >= 4 || !dwarf_strict))
13562 return NULL;
13564 litsize = size_of_int_loc_descriptor (i);
13565 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13566 is more compact. For DW_OP_stack_value we need:
13567 litsize + 1 (DW_OP_stack_value)
13568 and for DW_OP_implicit_value:
13569 1 (DW_OP_implicit_value) + 1 (length) + size. */
13570 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13572 loc_result = int_loc_descriptor (i);
13573 add_loc_descr (&loc_result,
13574 new_loc_descr (DW_OP_stack_value, 0, 0));
13575 return loc_result;
13578 loc_result = new_loc_descr (DW_OP_implicit_value,
13579 size, 0);
13580 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13581 loc_result->dw_loc_oprnd2.v.val_int = i;
13582 return loc_result;
13585 /* Return a location descriptor that designates a base+offset location. */
13587 static dw_loc_descr_ref
13588 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13589 enum var_init_status initialized)
13591 unsigned int regno;
13592 dw_loc_descr_ref result;
13593 dw_fde_ref fde = cfun->fde;
13595 /* We only use "frame base" when we're sure we're talking about the
13596 post-prologue local stack frame. We do this by *not* running
13597 register elimination until this point, and recognizing the special
13598 argument pointer and soft frame pointer rtx's. */
13599 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13601 rtx elim = (ira_use_lra_p
13602 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13603 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13605 if (elim != reg)
13607 if (GET_CODE (elim) == PLUS)
13609 offset += INTVAL (XEXP (elim, 1));
13610 elim = XEXP (elim, 0);
13612 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13613 && (elim == hard_frame_pointer_rtx
13614 || elim == stack_pointer_rtx))
13615 || elim == (frame_pointer_needed
13616 ? hard_frame_pointer_rtx
13617 : stack_pointer_rtx));
13619 /* If drap register is used to align stack, use frame
13620 pointer + offset to access stack variables. If stack
13621 is aligned without drap, use stack pointer + offset to
13622 access stack variables. */
13623 if (crtl->stack_realign_tried
13624 && reg == frame_pointer_rtx)
13626 int base_reg
13627 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13628 ? HARD_FRAME_POINTER_REGNUM
13629 : REGNO (elim));
13630 return new_reg_loc_descr (base_reg, offset);
13633 gcc_assert (frame_pointer_fb_offset_valid);
13634 offset += frame_pointer_fb_offset;
13635 return new_loc_descr (DW_OP_fbreg, offset, 0);
13639 regno = REGNO (reg);
13640 #ifdef LEAF_REG_REMAP
13641 if (crtl->uses_only_leaf_regs)
13643 int leaf_reg = LEAF_REG_REMAP (regno);
13644 if (leaf_reg != -1)
13645 regno = (unsigned) leaf_reg;
13647 #endif
13648 regno = DWARF_FRAME_REGNUM (regno);
13650 if (!optimize && fde
13651 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13653 /* Use cfa+offset to represent the location of arguments passed
13654 on the stack when drap is used to align stack.
13655 Only do this when not optimizing, for optimized code var-tracking
13656 is supposed to track where the arguments live and the register
13657 used as vdrap or drap in some spot might be used for something
13658 else in other part of the routine. */
13659 return new_loc_descr (DW_OP_fbreg, offset, 0);
13662 if (regno <= 31)
13663 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13664 offset, 0);
13665 else
13666 result = new_loc_descr (DW_OP_bregx, regno, offset);
13668 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13669 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13671 return result;
13674 /* Return true if this RTL expression describes a base+offset calculation. */
13676 static inline int
13677 is_based_loc (const_rtx rtl)
13679 return (GET_CODE (rtl) == PLUS
13680 && ((REG_P (XEXP (rtl, 0))
13681 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13682 && CONST_INT_P (XEXP (rtl, 1)))));
13685 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13686 failed. */
13688 static dw_loc_descr_ref
13689 tls_mem_loc_descriptor (rtx mem)
13691 tree base;
13692 dw_loc_descr_ref loc_result;
13694 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13695 return NULL;
13697 base = get_base_address (MEM_EXPR (mem));
13698 if (base == NULL
13699 || !VAR_P (base)
13700 || !DECL_THREAD_LOCAL_P (base))
13701 return NULL;
13703 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13704 if (loc_result == NULL)
13705 return NULL;
13707 if (MEM_OFFSET (mem))
13708 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13710 return loc_result;
13713 /* Output debug info about reason why we failed to expand expression as dwarf
13714 expression. */
13716 static void
13717 expansion_failed (tree expr, rtx rtl, char const *reason)
13719 if (dump_file && (dump_flags & TDF_DETAILS))
13721 fprintf (dump_file, "Failed to expand as dwarf: ");
13722 if (expr)
13723 print_generic_expr (dump_file, expr, dump_flags);
13724 if (rtl)
13726 fprintf (dump_file, "\n");
13727 print_rtl (dump_file, rtl);
13729 fprintf (dump_file, "\nReason: %s\n", reason);
13733 /* Helper function for const_ok_for_output. */
13735 static bool
13736 const_ok_for_output_1 (rtx rtl)
13738 if (targetm.const_not_ok_for_debug_p (rtl))
13740 if (GET_CODE (rtl) != UNSPEC)
13742 expansion_failed (NULL_TREE, rtl,
13743 "Expression rejected for debug by the backend.\n");
13744 return false;
13747 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13748 the target hook doesn't explicitly allow it in debug info, assume
13749 we can't express it in the debug info. */
13750 /* Don't complain about TLS UNSPECs, those are just too hard to
13751 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13752 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13753 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13754 if (flag_checking
13755 && (XVECLEN (rtl, 0) == 0
13756 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13757 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13758 inform (current_function_decl
13759 ? DECL_SOURCE_LOCATION (current_function_decl)
13760 : UNKNOWN_LOCATION,
13761 #if NUM_UNSPEC_VALUES > 0
13762 "non-delegitimized UNSPEC %s (%d) found in variable location",
13763 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13764 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13765 XINT (rtl, 1));
13766 #else
13767 "non-delegitimized UNSPEC %d found in variable location",
13768 XINT (rtl, 1));
13769 #endif
13770 expansion_failed (NULL_TREE, rtl,
13771 "UNSPEC hasn't been delegitimized.\n");
13772 return false;
13775 /* FIXME: Refer to PR60655. It is possible for simplification
13776 of rtl expressions in var tracking to produce such expressions.
13777 We should really identify / validate expressions
13778 enclosed in CONST that can be handled by assemblers on various
13779 targets and only handle legitimate cases here. */
13780 switch (GET_CODE (rtl))
13782 case SYMBOL_REF:
13783 break;
13784 case NOT:
13785 case NEG:
13786 return false;
13787 default:
13788 return true;
13791 if (CONSTANT_POOL_ADDRESS_P (rtl))
13793 bool marked;
13794 get_pool_constant_mark (rtl, &marked);
13795 /* If all references to this pool constant were optimized away,
13796 it was not output and thus we can't represent it. */
13797 if (!marked)
13799 expansion_failed (NULL_TREE, rtl,
13800 "Constant was removed from constant pool.\n");
13801 return false;
13805 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13806 return false;
13808 /* Avoid references to external symbols in debug info, on several targets
13809 the linker might even refuse to link when linking a shared library,
13810 and in many other cases the relocations for .debug_info/.debug_loc are
13811 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13812 to be defined within the same shared library or executable are fine. */
13813 if (SYMBOL_REF_EXTERNAL_P (rtl))
13815 tree decl = SYMBOL_REF_DECL (rtl);
13817 if (decl == NULL || !targetm.binds_local_p (decl))
13819 expansion_failed (NULL_TREE, rtl,
13820 "Symbol not defined in current TU.\n");
13821 return false;
13825 return true;
13828 /* Return true if constant RTL can be emitted in DW_OP_addr or
13829 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13830 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13832 static bool
13833 const_ok_for_output (rtx rtl)
13835 if (GET_CODE (rtl) == SYMBOL_REF)
13836 return const_ok_for_output_1 (rtl);
13838 if (GET_CODE (rtl) == CONST)
13840 subrtx_var_iterator::array_type array;
13841 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13842 if (!const_ok_for_output_1 (*iter))
13843 return false;
13844 return true;
13847 return true;
13850 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13851 if possible, NULL otherwise. */
13853 static dw_die_ref
13854 base_type_for_mode (machine_mode mode, bool unsignedp)
13856 dw_die_ref type_die;
13857 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13859 if (type == NULL)
13860 return NULL;
13861 switch (TREE_CODE (type))
13863 case INTEGER_TYPE:
13864 case REAL_TYPE:
13865 break;
13866 default:
13867 return NULL;
13869 type_die = lookup_type_die (type);
13870 if (!type_die)
13871 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13872 comp_unit_die ());
13873 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13874 return NULL;
13875 return type_die;
13878 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13879 type matching MODE, or, if MODE is narrower than or as wide as
13880 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13881 possible. */
13883 static dw_loc_descr_ref
13884 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13886 machine_mode outer_mode = mode;
13887 dw_die_ref type_die;
13888 dw_loc_descr_ref cvt;
13890 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13892 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13893 return op;
13895 type_die = base_type_for_mode (outer_mode, 1);
13896 if (type_die == NULL)
13897 return NULL;
13898 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13899 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13900 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13901 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13902 add_loc_descr (&op, cvt);
13903 return op;
13906 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13908 static dw_loc_descr_ref
13909 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13910 dw_loc_descr_ref op1)
13912 dw_loc_descr_ref ret = op0;
13913 add_loc_descr (&ret, op1);
13914 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13915 if (STORE_FLAG_VALUE != 1)
13917 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13918 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13920 return ret;
13923 /* Subroutine of scompare_loc_descriptor for the case in which we're
13924 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13925 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13927 static dw_loc_descr_ref
13928 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13929 scalar_int_mode op_mode,
13930 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13932 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13933 dw_loc_descr_ref cvt;
13935 if (type_die == NULL)
13936 return NULL;
13937 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13938 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13939 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13940 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13941 add_loc_descr (&op0, cvt);
13942 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13943 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13944 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13945 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13946 add_loc_descr (&op1, cvt);
13947 return compare_loc_descriptor (op, op0, op1);
13950 /* Subroutine of scompare_loc_descriptor for the case in which we're
13951 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13952 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13954 static dw_loc_descr_ref
13955 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13956 scalar_int_mode op_mode,
13957 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13959 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13960 /* For eq/ne, if the operands are known to be zero-extended,
13961 there is no need to do the fancy shifting up. */
13962 if (op == DW_OP_eq || op == DW_OP_ne)
13964 dw_loc_descr_ref last0, last1;
13965 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13967 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13969 /* deref_size zero extends, and for constants we can check
13970 whether they are zero extended or not. */
13971 if (((last0->dw_loc_opc == DW_OP_deref_size
13972 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13973 || (CONST_INT_P (XEXP (rtl, 0))
13974 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13975 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13976 && ((last1->dw_loc_opc == DW_OP_deref_size
13977 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13978 || (CONST_INT_P (XEXP (rtl, 1))
13979 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13980 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13981 return compare_loc_descriptor (op, op0, op1);
13983 /* EQ/NE comparison against constant in narrower type than
13984 DWARF2_ADDR_SIZE can be performed either as
13985 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13986 DW_OP_{eq,ne}
13988 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13989 DW_OP_{eq,ne}. Pick whatever is shorter. */
13990 if (CONST_INT_P (XEXP (rtl, 1))
13991 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13992 && (size_of_int_loc_descriptor (shift) + 1
13993 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
13994 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
13995 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
13996 & GET_MODE_MASK (op_mode))))
13998 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
13999 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14000 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14001 & GET_MODE_MASK (op_mode));
14002 return compare_loc_descriptor (op, op0, op1);
14005 add_loc_descr (&op0, int_loc_descriptor (shift));
14006 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14007 if (CONST_INT_P (XEXP (rtl, 1)))
14008 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14009 else
14011 add_loc_descr (&op1, int_loc_descriptor (shift));
14012 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14014 return compare_loc_descriptor (op, op0, op1);
14017 /* Return location descriptor for unsigned comparison OP RTL. */
14019 static dw_loc_descr_ref
14020 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14021 machine_mode mem_mode)
14023 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14024 dw_loc_descr_ref op0, op1;
14026 if (op_mode == VOIDmode)
14027 op_mode = GET_MODE (XEXP (rtl, 1));
14028 if (op_mode == VOIDmode)
14029 return NULL;
14031 scalar_int_mode int_op_mode;
14032 if (dwarf_strict
14033 && dwarf_version < 5
14034 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14035 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14036 return NULL;
14038 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14039 VAR_INIT_STATUS_INITIALIZED);
14040 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14041 VAR_INIT_STATUS_INITIALIZED);
14043 if (op0 == NULL || op1 == NULL)
14044 return NULL;
14046 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14048 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14049 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14051 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14052 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14054 return compare_loc_descriptor (op, op0, op1);
14057 /* Return location descriptor for unsigned comparison OP RTL. */
14059 static dw_loc_descr_ref
14060 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14061 machine_mode mem_mode)
14063 dw_loc_descr_ref op0, op1;
14065 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14066 if (test_op_mode == VOIDmode)
14067 test_op_mode = GET_MODE (XEXP (rtl, 1));
14069 scalar_int_mode op_mode;
14070 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14071 return NULL;
14073 if (dwarf_strict
14074 && dwarf_version < 5
14075 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14076 return NULL;
14078 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14079 VAR_INIT_STATUS_INITIALIZED);
14080 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14081 VAR_INIT_STATUS_INITIALIZED);
14083 if (op0 == NULL || op1 == NULL)
14084 return NULL;
14086 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14088 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14089 dw_loc_descr_ref last0, last1;
14090 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14092 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14094 if (CONST_INT_P (XEXP (rtl, 0)))
14095 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14096 /* deref_size zero extends, so no need to mask it again. */
14097 else if (last0->dw_loc_opc != DW_OP_deref_size
14098 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14100 add_loc_descr (&op0, int_loc_descriptor (mask));
14101 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14103 if (CONST_INT_P (XEXP (rtl, 1)))
14104 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14105 /* deref_size zero extends, so no need to mask it again. */
14106 else if (last1->dw_loc_opc != DW_OP_deref_size
14107 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14109 add_loc_descr (&op1, int_loc_descriptor (mask));
14110 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14113 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14115 HOST_WIDE_INT bias = 1;
14116 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14117 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14118 if (CONST_INT_P (XEXP (rtl, 1)))
14119 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14120 + INTVAL (XEXP (rtl, 1)));
14121 else
14122 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14123 bias, 0));
14125 return compare_loc_descriptor (op, op0, op1);
14128 /* Return location descriptor for {U,S}{MIN,MAX}. */
14130 static dw_loc_descr_ref
14131 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14132 machine_mode mem_mode)
14134 enum dwarf_location_atom op;
14135 dw_loc_descr_ref op0, op1, ret;
14136 dw_loc_descr_ref bra_node, drop_node;
14138 scalar_int_mode int_mode;
14139 if (dwarf_strict
14140 && dwarf_version < 5
14141 && (!is_a <scalar_int_mode> (mode, &int_mode)
14142 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14143 return NULL;
14145 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14146 VAR_INIT_STATUS_INITIALIZED);
14147 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14148 VAR_INIT_STATUS_INITIALIZED);
14150 if (op0 == NULL || op1 == NULL)
14151 return NULL;
14153 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14154 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14155 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14156 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14158 /* Checked by the caller. */
14159 int_mode = as_a <scalar_int_mode> (mode);
14160 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14162 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14163 add_loc_descr (&op0, int_loc_descriptor (mask));
14164 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14165 add_loc_descr (&op1, int_loc_descriptor (mask));
14166 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14168 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14170 HOST_WIDE_INT bias = 1;
14171 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14172 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14173 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14176 else if (is_a <scalar_int_mode> (mode, &int_mode)
14177 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14179 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14180 add_loc_descr (&op0, int_loc_descriptor (shift));
14181 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14182 add_loc_descr (&op1, int_loc_descriptor (shift));
14183 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14185 else if (is_a <scalar_int_mode> (mode, &int_mode)
14186 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14188 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14189 dw_loc_descr_ref cvt;
14190 if (type_die == NULL)
14191 return NULL;
14192 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14193 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14194 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14195 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14196 add_loc_descr (&op0, cvt);
14197 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14198 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14199 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14200 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14201 add_loc_descr (&op1, cvt);
14204 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14205 op = DW_OP_lt;
14206 else
14207 op = DW_OP_gt;
14208 ret = op0;
14209 add_loc_descr (&ret, op1);
14210 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14211 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14212 add_loc_descr (&ret, bra_node);
14213 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14214 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14215 add_loc_descr (&ret, drop_node);
14216 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14217 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14218 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14219 && is_a <scalar_int_mode> (mode, &int_mode)
14220 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14221 ret = convert_descriptor_to_mode (int_mode, ret);
14222 return ret;
14225 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14226 but after converting arguments to type_die, afterwards
14227 convert back to unsigned. */
14229 static dw_loc_descr_ref
14230 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14231 scalar_int_mode mode, machine_mode mem_mode)
14233 dw_loc_descr_ref cvt, op0, op1;
14235 if (type_die == NULL)
14236 return NULL;
14237 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14238 VAR_INIT_STATUS_INITIALIZED);
14239 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14240 VAR_INIT_STATUS_INITIALIZED);
14241 if (op0 == NULL || op1 == NULL)
14242 return NULL;
14243 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14244 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14245 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14246 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14247 add_loc_descr (&op0, cvt);
14248 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14249 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14250 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14251 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14252 add_loc_descr (&op1, cvt);
14253 add_loc_descr (&op0, op1);
14254 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14255 return convert_descriptor_to_mode (mode, op0);
14258 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14259 const0 is DW_OP_lit0 or corresponding typed constant,
14260 const1 is DW_OP_lit1 or corresponding typed constant
14261 and constMSB is constant with just the MSB bit set
14262 for the mode):
14263 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14264 L1: const0 DW_OP_swap
14265 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14266 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14267 L3: DW_OP_drop
14268 L4: DW_OP_nop
14270 CTZ is similar:
14271 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14272 L1: const0 DW_OP_swap
14273 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14274 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14275 L3: DW_OP_drop
14276 L4: DW_OP_nop
14278 FFS is similar:
14279 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14280 L1: const1 DW_OP_swap
14281 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14282 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14283 L3: DW_OP_drop
14284 L4: DW_OP_nop */
14286 static dw_loc_descr_ref
14287 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14288 machine_mode mem_mode)
14290 dw_loc_descr_ref op0, ret, tmp;
14291 HOST_WIDE_INT valv;
14292 dw_loc_descr_ref l1jump, l1label;
14293 dw_loc_descr_ref l2jump, l2label;
14294 dw_loc_descr_ref l3jump, l3label;
14295 dw_loc_descr_ref l4jump, l4label;
14296 rtx msb;
14298 if (GET_MODE (XEXP (rtl, 0)) != mode)
14299 return NULL;
14301 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14302 VAR_INIT_STATUS_INITIALIZED);
14303 if (op0 == NULL)
14304 return NULL;
14305 ret = op0;
14306 if (GET_CODE (rtl) == CLZ)
14308 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14309 valv = GET_MODE_BITSIZE (mode);
14311 else if (GET_CODE (rtl) == FFS)
14312 valv = 0;
14313 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14314 valv = GET_MODE_BITSIZE (mode);
14315 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14316 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14317 add_loc_descr (&ret, l1jump);
14318 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14319 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14320 VAR_INIT_STATUS_INITIALIZED);
14321 if (tmp == NULL)
14322 return NULL;
14323 add_loc_descr (&ret, tmp);
14324 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14325 add_loc_descr (&ret, l4jump);
14326 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14327 ? const1_rtx : const0_rtx,
14328 mode, mem_mode,
14329 VAR_INIT_STATUS_INITIALIZED);
14330 if (l1label == NULL)
14331 return NULL;
14332 add_loc_descr (&ret, l1label);
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14334 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14335 add_loc_descr (&ret, l2label);
14336 if (GET_CODE (rtl) != CLZ)
14337 msb = const1_rtx;
14338 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14339 msb = GEN_INT (HOST_WIDE_INT_1U
14340 << (GET_MODE_BITSIZE (mode) - 1));
14341 else
14342 msb = immed_wide_int_const
14343 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14344 GET_MODE_PRECISION (mode)), mode);
14345 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14346 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14347 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14348 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14349 else
14350 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14351 VAR_INIT_STATUS_INITIALIZED);
14352 if (tmp == NULL)
14353 return NULL;
14354 add_loc_descr (&ret, tmp);
14355 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14356 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14357 add_loc_descr (&ret, l3jump);
14358 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14359 VAR_INIT_STATUS_INITIALIZED);
14360 if (tmp == NULL)
14361 return NULL;
14362 add_loc_descr (&ret, tmp);
14363 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14364 ? DW_OP_shl : DW_OP_shr, 0, 0));
14365 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14366 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14367 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14368 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14369 add_loc_descr (&ret, l2jump);
14370 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14371 add_loc_descr (&ret, l3label);
14372 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14373 add_loc_descr (&ret, l4label);
14374 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14375 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14376 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14377 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14378 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14379 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14380 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14381 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14382 return ret;
14385 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14386 const1 is DW_OP_lit1 or corresponding typed constant):
14387 const0 DW_OP_swap
14388 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14389 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14390 L2: DW_OP_drop
14392 PARITY is similar:
14393 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14394 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14395 L2: DW_OP_drop */
14397 static dw_loc_descr_ref
14398 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14399 machine_mode mem_mode)
14401 dw_loc_descr_ref op0, ret, tmp;
14402 dw_loc_descr_ref l1jump, l1label;
14403 dw_loc_descr_ref l2jump, l2label;
14405 if (GET_MODE (XEXP (rtl, 0)) != mode)
14406 return NULL;
14408 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14409 VAR_INIT_STATUS_INITIALIZED);
14410 if (op0 == NULL)
14411 return NULL;
14412 ret = op0;
14413 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14414 VAR_INIT_STATUS_INITIALIZED);
14415 if (tmp == NULL)
14416 return NULL;
14417 add_loc_descr (&ret, tmp);
14418 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14419 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14420 add_loc_descr (&ret, l1label);
14421 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14422 add_loc_descr (&ret, l2jump);
14423 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14425 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14426 VAR_INIT_STATUS_INITIALIZED);
14427 if (tmp == NULL)
14428 return NULL;
14429 add_loc_descr (&ret, tmp);
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14431 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14432 ? DW_OP_plus : DW_OP_xor, 0, 0));
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14434 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14435 VAR_INIT_STATUS_INITIALIZED);
14436 add_loc_descr (&ret, tmp);
14437 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14438 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14439 add_loc_descr (&ret, l1jump);
14440 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14441 add_loc_descr (&ret, l2label);
14442 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14443 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14444 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14445 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14446 return ret;
14449 /* BSWAP (constS is initial shift count, either 56 or 24):
14450 constS const0
14451 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14452 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14453 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14454 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14455 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14457 static dw_loc_descr_ref
14458 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14459 machine_mode mem_mode)
14461 dw_loc_descr_ref op0, ret, tmp;
14462 dw_loc_descr_ref l1jump, l1label;
14463 dw_loc_descr_ref l2jump, l2label;
14465 if (BITS_PER_UNIT != 8
14466 || (GET_MODE_BITSIZE (mode) != 32
14467 && GET_MODE_BITSIZE (mode) != 64))
14468 return NULL;
14470 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14471 VAR_INIT_STATUS_INITIALIZED);
14472 if (op0 == NULL)
14473 return NULL;
14475 ret = op0;
14476 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14477 mode, mem_mode,
14478 VAR_INIT_STATUS_INITIALIZED);
14479 if (tmp == NULL)
14480 return NULL;
14481 add_loc_descr (&ret, tmp);
14482 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14483 VAR_INIT_STATUS_INITIALIZED);
14484 if (tmp == NULL)
14485 return NULL;
14486 add_loc_descr (&ret, tmp);
14487 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14488 add_loc_descr (&ret, l1label);
14489 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14490 mode, mem_mode,
14491 VAR_INIT_STATUS_INITIALIZED);
14492 add_loc_descr (&ret, tmp);
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14494 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14495 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14496 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14497 VAR_INIT_STATUS_INITIALIZED);
14498 if (tmp == NULL)
14499 return NULL;
14500 add_loc_descr (&ret, tmp);
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14502 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14505 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14507 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14508 VAR_INIT_STATUS_INITIALIZED);
14509 add_loc_descr (&ret, tmp);
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14511 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14512 add_loc_descr (&ret, l2jump);
14513 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14514 VAR_INIT_STATUS_INITIALIZED);
14515 add_loc_descr (&ret, tmp);
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14517 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14518 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14519 add_loc_descr (&ret, l1jump);
14520 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14521 add_loc_descr (&ret, l2label);
14522 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14524 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14525 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14526 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14527 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14528 return ret;
14531 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14532 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14533 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14534 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14536 ROTATERT is similar:
14537 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14538 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14539 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14541 static dw_loc_descr_ref
14542 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14543 machine_mode mem_mode)
14545 rtx rtlop1 = XEXP (rtl, 1);
14546 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14547 int i;
14549 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14550 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14551 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14552 VAR_INIT_STATUS_INITIALIZED);
14553 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14554 VAR_INIT_STATUS_INITIALIZED);
14555 if (op0 == NULL || op1 == NULL)
14556 return NULL;
14557 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14558 for (i = 0; i < 2; i++)
14560 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14561 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14562 mode, mem_mode,
14563 VAR_INIT_STATUS_INITIALIZED);
14564 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14565 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14566 ? DW_OP_const4u
14567 : HOST_BITS_PER_WIDE_INT == 64
14568 ? DW_OP_const8u : DW_OP_constu,
14569 GET_MODE_MASK (mode), 0);
14570 else
14571 mask[i] = NULL;
14572 if (mask[i] == NULL)
14573 return NULL;
14574 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14576 ret = op0;
14577 add_loc_descr (&ret, op1);
14578 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14580 if (GET_CODE (rtl) == ROTATERT)
14582 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14583 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14584 GET_MODE_BITSIZE (mode), 0));
14586 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14587 if (mask[0] != NULL)
14588 add_loc_descr (&ret, mask[0]);
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14590 if (mask[1] != NULL)
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14593 add_loc_descr (&ret, mask[1]);
14594 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14596 if (GET_CODE (rtl) == ROTATE)
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14599 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14600 GET_MODE_BITSIZE (mode), 0));
14602 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14603 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14604 return ret;
14607 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14608 for DEBUG_PARAMETER_REF RTL. */
14610 static dw_loc_descr_ref
14611 parameter_ref_descriptor (rtx rtl)
14613 dw_loc_descr_ref ret;
14614 dw_die_ref ref;
14616 if (dwarf_strict)
14617 return NULL;
14618 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14619 /* With LTO during LTRANS we get the late DIE that refers to the early
14620 DIE, thus we add another indirection here. This seems to confuse
14621 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14622 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14623 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14624 if (ref)
14626 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14627 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14628 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14630 else
14632 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14633 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14635 return ret;
14638 /* The following routine converts the RTL for a variable or parameter
14639 (resident in memory) into an equivalent Dwarf representation of a
14640 mechanism for getting the address of that same variable onto the top of a
14641 hypothetical "address evaluation" stack.
14643 When creating memory location descriptors, we are effectively transforming
14644 the RTL for a memory-resident object into its Dwarf postfix expression
14645 equivalent. This routine recursively descends an RTL tree, turning
14646 it into Dwarf postfix code as it goes.
14648 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14650 MEM_MODE is the mode of the memory reference, needed to handle some
14651 autoincrement addressing modes.
14653 Return 0 if we can't represent the location. */
14655 dw_loc_descr_ref
14656 mem_loc_descriptor (rtx rtl, machine_mode mode,
14657 machine_mode mem_mode,
14658 enum var_init_status initialized)
14660 dw_loc_descr_ref mem_loc_result = NULL;
14661 enum dwarf_location_atom op;
14662 dw_loc_descr_ref op0, op1;
14663 rtx inner = NULL_RTX;
14665 if (mode == VOIDmode)
14666 mode = GET_MODE (rtl);
14668 /* Note that for a dynamically sized array, the location we will generate a
14669 description of here will be the lowest numbered location which is
14670 actually within the array. That's *not* necessarily the same as the
14671 zeroth element of the array. */
14673 rtl = targetm.delegitimize_address (rtl);
14675 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14676 return NULL;
14678 scalar_int_mode int_mode, inner_mode, op1_mode;
14679 switch (GET_CODE (rtl))
14681 case POST_INC:
14682 case POST_DEC:
14683 case POST_MODIFY:
14684 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14686 case SUBREG:
14687 /* The case of a subreg may arise when we have a local (register)
14688 variable or a formal (register) parameter which doesn't quite fill
14689 up an entire register. For now, just assume that it is
14690 legitimate to make the Dwarf info refer to the whole register which
14691 contains the given subreg. */
14692 if (!subreg_lowpart_p (rtl))
14693 break;
14694 inner = SUBREG_REG (rtl);
14695 /* FALLTHRU */
14696 case TRUNCATE:
14697 if (inner == NULL_RTX)
14698 inner = XEXP (rtl, 0);
14699 if (is_a <scalar_int_mode> (mode, &int_mode)
14700 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14701 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14702 #ifdef POINTERS_EXTEND_UNSIGNED
14703 || (int_mode == Pmode && mem_mode != VOIDmode)
14704 #endif
14706 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14708 mem_loc_result = mem_loc_descriptor (inner,
14709 inner_mode,
14710 mem_mode, initialized);
14711 break;
14713 if (dwarf_strict && dwarf_version < 5)
14714 break;
14715 if (is_a <scalar_int_mode> (mode, &int_mode)
14716 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14717 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14718 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14720 dw_die_ref type_die;
14721 dw_loc_descr_ref cvt;
14723 mem_loc_result = mem_loc_descriptor (inner,
14724 GET_MODE (inner),
14725 mem_mode, initialized);
14726 if (mem_loc_result == NULL)
14727 break;
14728 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14729 if (type_die == NULL)
14731 mem_loc_result = NULL;
14732 break;
14734 if (GET_MODE_SIZE (mode)
14735 != GET_MODE_SIZE (GET_MODE (inner)))
14736 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14737 else
14738 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14739 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14740 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14741 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14742 add_loc_descr (&mem_loc_result, cvt);
14743 if (is_a <scalar_int_mode> (mode, &int_mode)
14744 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14746 /* Convert it to untyped afterwards. */
14747 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14748 add_loc_descr (&mem_loc_result, cvt);
14751 break;
14753 case REG:
14754 if (!is_a <scalar_int_mode> (mode, &int_mode)
14755 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14756 && rtl != arg_pointer_rtx
14757 && rtl != frame_pointer_rtx
14758 #ifdef POINTERS_EXTEND_UNSIGNED
14759 && (int_mode != Pmode || mem_mode == VOIDmode)
14760 #endif
14763 dw_die_ref type_die;
14764 unsigned int dbx_regnum;
14766 if (dwarf_strict && dwarf_version < 5)
14767 break;
14768 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14769 break;
14770 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14771 if (type_die == NULL)
14772 break;
14774 dbx_regnum = dbx_reg_number (rtl);
14775 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14776 break;
14777 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14778 dbx_regnum, 0);
14779 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14780 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14781 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14782 break;
14784 /* Whenever a register number forms a part of the description of the
14785 method for calculating the (dynamic) address of a memory resident
14786 object, DWARF rules require the register number be referred to as
14787 a "base register". This distinction is not based in any way upon
14788 what category of register the hardware believes the given register
14789 belongs to. This is strictly DWARF terminology we're dealing with
14790 here. Note that in cases where the location of a memory-resident
14791 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14792 OP_CONST (0)) the actual DWARF location descriptor that we generate
14793 may just be OP_BASEREG (basereg). This may look deceptively like
14794 the object in question was allocated to a register (rather than in
14795 memory) so DWARF consumers need to be aware of the subtle
14796 distinction between OP_REG and OP_BASEREG. */
14797 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14798 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14799 else if (stack_realign_drap
14800 && crtl->drap_reg
14801 && crtl->args.internal_arg_pointer == rtl
14802 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14804 /* If RTL is internal_arg_pointer, which has been optimized
14805 out, use DRAP instead. */
14806 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14807 VAR_INIT_STATUS_INITIALIZED);
14809 break;
14811 case SIGN_EXTEND:
14812 case ZERO_EXTEND:
14813 if (!is_a <scalar_int_mode> (mode, &int_mode)
14814 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14815 break;
14816 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14817 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14818 if (op0 == 0)
14819 break;
14820 else if (GET_CODE (rtl) == ZERO_EXTEND
14821 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14822 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14823 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14824 to expand zero extend as two shifts instead of
14825 masking. */
14826 && GET_MODE_SIZE (inner_mode) <= 4)
14828 mem_loc_result = op0;
14829 add_loc_descr (&mem_loc_result,
14830 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14831 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14833 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14835 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14836 shift *= BITS_PER_UNIT;
14837 if (GET_CODE (rtl) == SIGN_EXTEND)
14838 op = DW_OP_shra;
14839 else
14840 op = DW_OP_shr;
14841 mem_loc_result = op0;
14842 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14843 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14844 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14845 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14847 else if (!dwarf_strict || dwarf_version >= 5)
14849 dw_die_ref type_die1, type_die2;
14850 dw_loc_descr_ref cvt;
14852 type_die1 = base_type_for_mode (inner_mode,
14853 GET_CODE (rtl) == ZERO_EXTEND);
14854 if (type_die1 == NULL)
14855 break;
14856 type_die2 = base_type_for_mode (int_mode, 1);
14857 if (type_die2 == NULL)
14858 break;
14859 mem_loc_result = op0;
14860 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14861 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14862 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14863 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14864 add_loc_descr (&mem_loc_result, cvt);
14865 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14866 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14867 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14868 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14869 add_loc_descr (&mem_loc_result, cvt);
14871 break;
14873 case MEM:
14875 rtx new_rtl = avoid_constant_pool_reference (rtl);
14876 if (new_rtl != rtl)
14878 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14879 initialized);
14880 if (mem_loc_result != NULL)
14881 return mem_loc_result;
14884 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14885 get_address_mode (rtl), mode,
14886 VAR_INIT_STATUS_INITIALIZED);
14887 if (mem_loc_result == NULL)
14888 mem_loc_result = tls_mem_loc_descriptor (rtl);
14889 if (mem_loc_result != NULL)
14891 if (!is_a <scalar_int_mode> (mode, &int_mode)
14892 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14894 dw_die_ref type_die;
14895 dw_loc_descr_ref deref;
14897 if (dwarf_strict && dwarf_version < 5)
14898 return NULL;
14899 type_die
14900 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14901 if (type_die == NULL)
14902 return NULL;
14903 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14904 GET_MODE_SIZE (mode), 0);
14905 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14906 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14907 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14908 add_loc_descr (&mem_loc_result, deref);
14910 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14911 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14912 else
14913 add_loc_descr (&mem_loc_result,
14914 new_loc_descr (DW_OP_deref_size,
14915 GET_MODE_SIZE (int_mode), 0));
14917 break;
14919 case LO_SUM:
14920 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14922 case LABEL_REF:
14923 /* Some ports can transform a symbol ref into a label ref, because
14924 the symbol ref is too far away and has to be dumped into a constant
14925 pool. */
14926 case CONST:
14927 case SYMBOL_REF:
14928 if (!is_a <scalar_int_mode> (mode, &int_mode)
14929 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14930 #ifdef POINTERS_EXTEND_UNSIGNED
14931 && (int_mode != Pmode || mem_mode == VOIDmode)
14932 #endif
14934 break;
14935 if (GET_CODE (rtl) == SYMBOL_REF
14936 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14938 dw_loc_descr_ref temp;
14940 /* If this is not defined, we have no way to emit the data. */
14941 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14942 break;
14944 temp = new_addr_loc_descr (rtl, dtprel_true);
14946 /* We check for DWARF 5 here because gdb did not implement
14947 DW_OP_form_tls_address until after 7.12. */
14948 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14949 ? DW_OP_form_tls_address
14950 : DW_OP_GNU_push_tls_address),
14951 0, 0);
14952 add_loc_descr (&mem_loc_result, temp);
14954 break;
14957 if (!const_ok_for_output (rtl))
14959 if (GET_CODE (rtl) == CONST)
14960 switch (GET_CODE (XEXP (rtl, 0)))
14962 case NOT:
14963 op = DW_OP_not;
14964 goto try_const_unop;
14965 case NEG:
14966 op = DW_OP_neg;
14967 goto try_const_unop;
14968 try_const_unop:
14969 rtx arg;
14970 arg = XEXP (XEXP (rtl, 0), 0);
14971 if (!CONSTANT_P (arg))
14972 arg = gen_rtx_CONST (int_mode, arg);
14973 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14974 initialized);
14975 if (op0)
14977 mem_loc_result = op0;
14978 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14980 break;
14981 default:
14982 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14983 mem_mode, initialized);
14984 break;
14986 break;
14989 symref:
14990 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14991 vec_safe_push (used_rtx_array, rtl);
14992 break;
14994 case CONCAT:
14995 case CONCATN:
14996 case VAR_LOCATION:
14997 case DEBUG_IMPLICIT_PTR:
14998 expansion_failed (NULL_TREE, rtl,
14999 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15000 return 0;
15002 case ENTRY_VALUE:
15003 if (dwarf_strict && dwarf_version < 5)
15004 return NULL;
15005 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15007 if (!is_a <scalar_int_mode> (mode, &int_mode)
15008 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15009 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15010 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15011 else
15013 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15014 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15015 return NULL;
15016 op0 = one_reg_loc_descriptor (dbx_regnum,
15017 VAR_INIT_STATUS_INITIALIZED);
15020 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15021 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15023 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15024 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15025 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15026 return NULL;
15028 else
15029 gcc_unreachable ();
15030 if (op0 == NULL)
15031 return NULL;
15032 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15033 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15034 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15035 break;
15037 case DEBUG_PARAMETER_REF:
15038 mem_loc_result = parameter_ref_descriptor (rtl);
15039 break;
15041 case PRE_MODIFY:
15042 /* Extract the PLUS expression nested inside and fall into
15043 PLUS code below. */
15044 rtl = XEXP (rtl, 1);
15045 goto plus;
15047 case PRE_INC:
15048 case PRE_DEC:
15049 /* Turn these into a PLUS expression and fall into the PLUS code
15050 below. */
15051 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15052 gen_int_mode (GET_CODE (rtl) == PRE_INC
15053 ? GET_MODE_UNIT_SIZE (mem_mode)
15054 : -GET_MODE_UNIT_SIZE (mem_mode),
15055 mode));
15057 /* fall through */
15059 case PLUS:
15060 plus:
15061 if (is_based_loc (rtl)
15062 && is_a <scalar_int_mode> (mode, &int_mode)
15063 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15064 || XEXP (rtl, 0) == arg_pointer_rtx
15065 || XEXP (rtl, 0) == frame_pointer_rtx))
15066 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15067 INTVAL (XEXP (rtl, 1)),
15068 VAR_INIT_STATUS_INITIALIZED);
15069 else
15071 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15072 VAR_INIT_STATUS_INITIALIZED);
15073 if (mem_loc_result == 0)
15074 break;
15076 if (CONST_INT_P (XEXP (rtl, 1))
15077 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15078 <= DWARF2_ADDR_SIZE))
15079 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15080 else
15082 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15083 VAR_INIT_STATUS_INITIALIZED);
15084 if (op1 == 0)
15085 return NULL;
15086 add_loc_descr (&mem_loc_result, op1);
15087 add_loc_descr (&mem_loc_result,
15088 new_loc_descr (DW_OP_plus, 0, 0));
15091 break;
15093 /* If a pseudo-reg is optimized away, it is possible for it to
15094 be replaced with a MEM containing a multiply or shift. */
15095 case MINUS:
15096 op = DW_OP_minus;
15097 goto do_binop;
15099 case MULT:
15100 op = DW_OP_mul;
15101 goto do_binop;
15103 case DIV:
15104 if ((!dwarf_strict || dwarf_version >= 5)
15105 && is_a <scalar_int_mode> (mode, &int_mode)
15106 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15108 mem_loc_result = typed_binop (DW_OP_div, rtl,
15109 base_type_for_mode (mode, 0),
15110 int_mode, mem_mode);
15111 break;
15113 op = DW_OP_div;
15114 goto do_binop;
15116 case UMOD:
15117 op = DW_OP_mod;
15118 goto do_binop;
15120 case ASHIFT:
15121 op = DW_OP_shl;
15122 goto do_shift;
15124 case ASHIFTRT:
15125 op = DW_OP_shra;
15126 goto do_shift;
15128 case LSHIFTRT:
15129 op = DW_OP_shr;
15130 goto do_shift;
15132 do_shift:
15133 if (!is_a <scalar_int_mode> (mode, &int_mode))
15134 break;
15135 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15136 VAR_INIT_STATUS_INITIALIZED);
15138 rtx rtlop1 = XEXP (rtl, 1);
15139 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15140 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15141 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15142 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15143 VAR_INIT_STATUS_INITIALIZED);
15146 if (op0 == 0 || op1 == 0)
15147 break;
15149 mem_loc_result = op0;
15150 add_loc_descr (&mem_loc_result, op1);
15151 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15152 break;
15154 case AND:
15155 op = DW_OP_and;
15156 goto do_binop;
15158 case IOR:
15159 op = DW_OP_or;
15160 goto do_binop;
15162 case XOR:
15163 op = DW_OP_xor;
15164 goto do_binop;
15166 do_binop:
15167 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15168 VAR_INIT_STATUS_INITIALIZED);
15169 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15170 VAR_INIT_STATUS_INITIALIZED);
15172 if (op0 == 0 || op1 == 0)
15173 break;
15175 mem_loc_result = op0;
15176 add_loc_descr (&mem_loc_result, op1);
15177 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15178 break;
15180 case MOD:
15181 if ((!dwarf_strict || dwarf_version >= 5)
15182 && is_a <scalar_int_mode> (mode, &int_mode)
15183 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15185 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15186 base_type_for_mode (mode, 0),
15187 int_mode, mem_mode);
15188 break;
15191 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15192 VAR_INIT_STATUS_INITIALIZED);
15193 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15194 VAR_INIT_STATUS_INITIALIZED);
15196 if (op0 == 0 || op1 == 0)
15197 break;
15199 mem_loc_result = op0;
15200 add_loc_descr (&mem_loc_result, op1);
15201 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15202 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15203 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15204 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15205 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15206 break;
15208 case UDIV:
15209 if ((!dwarf_strict || dwarf_version >= 5)
15210 && is_a <scalar_int_mode> (mode, &int_mode))
15212 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15214 op = DW_OP_div;
15215 goto do_binop;
15217 mem_loc_result = typed_binop (DW_OP_div, rtl,
15218 base_type_for_mode (int_mode, 1),
15219 int_mode, mem_mode);
15221 break;
15223 case NOT:
15224 op = DW_OP_not;
15225 goto do_unop;
15227 case ABS:
15228 op = DW_OP_abs;
15229 goto do_unop;
15231 case NEG:
15232 op = DW_OP_neg;
15233 goto do_unop;
15235 do_unop:
15236 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15237 VAR_INIT_STATUS_INITIALIZED);
15239 if (op0 == 0)
15240 break;
15242 mem_loc_result = op0;
15243 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15244 break;
15246 case CONST_INT:
15247 if (!is_a <scalar_int_mode> (mode, &int_mode)
15248 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15249 #ifdef POINTERS_EXTEND_UNSIGNED
15250 || (int_mode == Pmode
15251 && mem_mode != VOIDmode
15252 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15253 #endif
15256 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15257 break;
15259 if ((!dwarf_strict || dwarf_version >= 5)
15260 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15261 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15263 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15264 scalar_int_mode amode;
15265 if (type_die == NULL)
15266 return NULL;
15267 if (INTVAL (rtl) >= 0
15268 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15269 .exists (&amode))
15270 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15271 /* const DW_OP_convert <XXX> vs.
15272 DW_OP_const_type <XXX, 1, const>. */
15273 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15274 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15276 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15277 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15278 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15279 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15280 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15281 add_loc_descr (&mem_loc_result, op0);
15282 return mem_loc_result;
15284 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15285 INTVAL (rtl));
15286 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15287 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15288 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15289 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15290 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15291 else
15293 mem_loc_result->dw_loc_oprnd2.val_class
15294 = dw_val_class_const_double;
15295 mem_loc_result->dw_loc_oprnd2.v.val_double
15296 = double_int::from_shwi (INTVAL (rtl));
15299 break;
15301 case CONST_DOUBLE:
15302 if (!dwarf_strict || dwarf_version >= 5)
15304 dw_die_ref type_die;
15306 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15307 CONST_DOUBLE rtx could represent either a large integer
15308 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15309 the value is always a floating point constant.
15311 When it is an integer, a CONST_DOUBLE is used whenever
15312 the constant requires 2 HWIs to be adequately represented.
15313 We output CONST_DOUBLEs as blocks. */
15314 if (mode == VOIDmode
15315 || (GET_MODE (rtl) == VOIDmode
15316 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15317 break;
15318 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15319 if (type_die == NULL)
15320 return NULL;
15321 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15322 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15323 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15324 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15325 #if TARGET_SUPPORTS_WIDE_INT == 0
15326 if (!SCALAR_FLOAT_MODE_P (mode))
15328 mem_loc_result->dw_loc_oprnd2.val_class
15329 = dw_val_class_const_double;
15330 mem_loc_result->dw_loc_oprnd2.v.val_double
15331 = rtx_to_double_int (rtl);
15333 else
15334 #endif
15336 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15337 unsigned int length = GET_MODE_SIZE (float_mode);
15338 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15340 insert_float (rtl, array);
15341 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15342 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15343 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15344 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15347 break;
15349 case CONST_WIDE_INT:
15350 if (!dwarf_strict || dwarf_version >= 5)
15352 dw_die_ref type_die;
15354 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15355 if (type_die == NULL)
15356 return NULL;
15357 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15358 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15359 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15360 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15361 mem_loc_result->dw_loc_oprnd2.val_class
15362 = dw_val_class_wide_int;
15363 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15364 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15366 break;
15368 case EQ:
15369 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15370 break;
15372 case GE:
15373 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15374 break;
15376 case GT:
15377 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15378 break;
15380 case LE:
15381 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15382 break;
15384 case LT:
15385 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15386 break;
15388 case NE:
15389 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15390 break;
15392 case GEU:
15393 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15394 break;
15396 case GTU:
15397 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15398 break;
15400 case LEU:
15401 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15402 break;
15404 case LTU:
15405 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15406 break;
15408 case UMIN:
15409 case UMAX:
15410 if (!SCALAR_INT_MODE_P (mode))
15411 break;
15412 /* FALLTHRU */
15413 case SMIN:
15414 case SMAX:
15415 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15416 break;
15418 case ZERO_EXTRACT:
15419 case SIGN_EXTRACT:
15420 if (CONST_INT_P (XEXP (rtl, 1))
15421 && CONST_INT_P (XEXP (rtl, 2))
15422 && is_a <scalar_int_mode> (mode, &int_mode)
15423 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15424 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15425 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15426 && ((unsigned) INTVAL (XEXP (rtl, 1))
15427 + (unsigned) INTVAL (XEXP (rtl, 2))
15428 <= GET_MODE_BITSIZE (int_mode)))
15430 int shift, size;
15431 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15432 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15433 if (op0 == 0)
15434 break;
15435 if (GET_CODE (rtl) == SIGN_EXTRACT)
15436 op = DW_OP_shra;
15437 else
15438 op = DW_OP_shr;
15439 mem_loc_result = op0;
15440 size = INTVAL (XEXP (rtl, 1));
15441 shift = INTVAL (XEXP (rtl, 2));
15442 if (BITS_BIG_ENDIAN)
15443 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15444 if (shift + size != (int) DWARF2_ADDR_SIZE)
15446 add_loc_descr (&mem_loc_result,
15447 int_loc_descriptor (DWARF2_ADDR_SIZE
15448 - shift - size));
15449 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15451 if (size != (int) DWARF2_ADDR_SIZE)
15453 add_loc_descr (&mem_loc_result,
15454 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15455 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15458 break;
15460 case IF_THEN_ELSE:
15462 dw_loc_descr_ref op2, bra_node, drop_node;
15463 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15464 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15465 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15466 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15467 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15468 VAR_INIT_STATUS_INITIALIZED);
15469 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15470 VAR_INIT_STATUS_INITIALIZED);
15471 if (op0 == NULL || op1 == NULL || op2 == NULL)
15472 break;
15474 mem_loc_result = op1;
15475 add_loc_descr (&mem_loc_result, op2);
15476 add_loc_descr (&mem_loc_result, op0);
15477 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15478 add_loc_descr (&mem_loc_result, bra_node);
15479 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15480 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15481 add_loc_descr (&mem_loc_result, drop_node);
15482 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15483 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15485 break;
15487 case FLOAT_EXTEND:
15488 case FLOAT_TRUNCATE:
15489 case FLOAT:
15490 case UNSIGNED_FLOAT:
15491 case FIX:
15492 case UNSIGNED_FIX:
15493 if (!dwarf_strict || dwarf_version >= 5)
15495 dw_die_ref type_die;
15496 dw_loc_descr_ref cvt;
15498 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15499 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15500 if (op0 == NULL)
15501 break;
15502 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15503 && (GET_CODE (rtl) == FLOAT
15504 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15506 type_die = base_type_for_mode (int_mode,
15507 GET_CODE (rtl) == UNSIGNED_FLOAT);
15508 if (type_die == NULL)
15509 break;
15510 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15511 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15512 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15513 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15514 add_loc_descr (&op0, cvt);
15516 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15517 if (type_die == NULL)
15518 break;
15519 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15520 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15521 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15522 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15523 add_loc_descr (&op0, cvt);
15524 if (is_a <scalar_int_mode> (mode, &int_mode)
15525 && (GET_CODE (rtl) == FIX
15526 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15528 op0 = convert_descriptor_to_mode (int_mode, op0);
15529 if (op0 == NULL)
15530 break;
15532 mem_loc_result = op0;
15534 break;
15536 case CLZ:
15537 case CTZ:
15538 case FFS:
15539 if (is_a <scalar_int_mode> (mode, &int_mode))
15540 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15541 break;
15543 case POPCOUNT:
15544 case PARITY:
15545 if (is_a <scalar_int_mode> (mode, &int_mode))
15546 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15547 break;
15549 case BSWAP:
15550 if (is_a <scalar_int_mode> (mode, &int_mode))
15551 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15552 break;
15554 case ROTATE:
15555 case ROTATERT:
15556 if (is_a <scalar_int_mode> (mode, &int_mode))
15557 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15558 break;
15560 case COMPARE:
15561 /* In theory, we could implement the above. */
15562 /* DWARF cannot represent the unsigned compare operations
15563 natively. */
15564 case SS_MULT:
15565 case US_MULT:
15566 case SS_DIV:
15567 case US_DIV:
15568 case SS_PLUS:
15569 case US_PLUS:
15570 case SS_MINUS:
15571 case US_MINUS:
15572 case SS_NEG:
15573 case US_NEG:
15574 case SS_ABS:
15575 case SS_ASHIFT:
15576 case US_ASHIFT:
15577 case SS_TRUNCATE:
15578 case US_TRUNCATE:
15579 case UNORDERED:
15580 case ORDERED:
15581 case UNEQ:
15582 case UNGE:
15583 case UNGT:
15584 case UNLE:
15585 case UNLT:
15586 case LTGT:
15587 case FRACT_CONVERT:
15588 case UNSIGNED_FRACT_CONVERT:
15589 case SAT_FRACT:
15590 case UNSIGNED_SAT_FRACT:
15591 case SQRT:
15592 case ASM_OPERANDS:
15593 case VEC_MERGE:
15594 case VEC_SELECT:
15595 case VEC_CONCAT:
15596 case VEC_DUPLICATE:
15597 case VEC_SERIES:
15598 case UNSPEC:
15599 case HIGH:
15600 case FMA:
15601 case STRICT_LOW_PART:
15602 case CONST_VECTOR:
15603 case CONST_FIXED:
15604 case CLRSB:
15605 case CLOBBER:
15606 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15607 can't express it in the debug info. This can happen e.g. with some
15608 TLS UNSPECs. */
15609 break;
15611 case CONST_STRING:
15612 resolve_one_addr (&rtl);
15613 goto symref;
15615 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15616 the expression. An UNSPEC rtx represents a raw DWARF operation,
15617 new_loc_descr is called for it to build the operation directly.
15618 Otherwise mem_loc_descriptor is called recursively. */
15619 case PARALLEL:
15621 int index = 0;
15622 dw_loc_descr_ref exp_result = NULL;
15624 for (; index < XVECLEN (rtl, 0); index++)
15626 rtx elem = XVECEXP (rtl, 0, index);
15627 if (GET_CODE (elem) == UNSPEC)
15629 /* Each DWARF operation UNSPEC contain two operands, if
15630 one operand is not used for the operation, const0_rtx is
15631 passed. */
15632 gcc_assert (XVECLEN (elem, 0) == 2);
15634 HOST_WIDE_INT dw_op = XINT (elem, 1);
15635 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15636 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15637 exp_result
15638 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15639 oprnd2);
15641 else
15642 exp_result
15643 = mem_loc_descriptor (elem, mode, mem_mode,
15644 VAR_INIT_STATUS_INITIALIZED);
15646 if (!mem_loc_result)
15647 mem_loc_result = exp_result;
15648 else
15649 add_loc_descr (&mem_loc_result, exp_result);
15652 break;
15655 default:
15656 if (flag_checking)
15658 print_rtl (stderr, rtl);
15659 gcc_unreachable ();
15661 break;
15664 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15665 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15667 return mem_loc_result;
15670 /* Return a descriptor that describes the concatenation of two locations.
15671 This is typically a complex variable. */
15673 static dw_loc_descr_ref
15674 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15676 dw_loc_descr_ref cc_loc_result = NULL;
15677 dw_loc_descr_ref x0_ref
15678 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15679 dw_loc_descr_ref x1_ref
15680 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15682 if (x0_ref == 0 || x1_ref == 0)
15683 return 0;
15685 cc_loc_result = x0_ref;
15686 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15688 add_loc_descr (&cc_loc_result, x1_ref);
15689 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15691 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15692 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15694 return cc_loc_result;
15697 /* Return a descriptor that describes the concatenation of N
15698 locations. */
15700 static dw_loc_descr_ref
15701 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15703 unsigned int i;
15704 dw_loc_descr_ref cc_loc_result = NULL;
15705 unsigned int n = XVECLEN (concatn, 0);
15707 for (i = 0; i < n; ++i)
15709 dw_loc_descr_ref ref;
15710 rtx x = XVECEXP (concatn, 0, i);
15712 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15713 if (ref == NULL)
15714 return NULL;
15716 add_loc_descr (&cc_loc_result, ref);
15717 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15720 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15721 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15723 return cc_loc_result;
15726 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15727 for DEBUG_IMPLICIT_PTR RTL. */
15729 static dw_loc_descr_ref
15730 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15732 dw_loc_descr_ref ret;
15733 dw_die_ref ref;
15735 if (dwarf_strict && dwarf_version < 5)
15736 return NULL;
15737 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15738 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15739 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15740 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15741 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15742 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15743 if (ref)
15745 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15746 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15747 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15749 else
15751 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15752 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15754 return ret;
15757 /* Output a proper Dwarf location descriptor for a variable or parameter
15758 which is either allocated in a register or in a memory location. For a
15759 register, we just generate an OP_REG and the register number. For a
15760 memory location we provide a Dwarf postfix expression describing how to
15761 generate the (dynamic) address of the object onto the address stack.
15763 MODE is mode of the decl if this loc_descriptor is going to be used in
15764 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15765 allowed, VOIDmode otherwise.
15767 If we don't know how to describe it, return 0. */
15769 static dw_loc_descr_ref
15770 loc_descriptor (rtx rtl, machine_mode mode,
15771 enum var_init_status initialized)
15773 dw_loc_descr_ref loc_result = NULL;
15774 scalar_int_mode int_mode;
15776 switch (GET_CODE (rtl))
15778 case SUBREG:
15779 /* The case of a subreg may arise when we have a local (register)
15780 variable or a formal (register) parameter which doesn't quite fill
15781 up an entire register. For now, just assume that it is
15782 legitimate to make the Dwarf info refer to the whole register which
15783 contains the given subreg. */
15784 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15785 loc_result = loc_descriptor (SUBREG_REG (rtl),
15786 GET_MODE (SUBREG_REG (rtl)), initialized);
15787 else
15788 goto do_default;
15789 break;
15791 case REG:
15792 loc_result = reg_loc_descriptor (rtl, initialized);
15793 break;
15795 case MEM:
15796 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15797 GET_MODE (rtl), initialized);
15798 if (loc_result == NULL)
15799 loc_result = tls_mem_loc_descriptor (rtl);
15800 if (loc_result == NULL)
15802 rtx new_rtl = avoid_constant_pool_reference (rtl);
15803 if (new_rtl != rtl)
15804 loc_result = loc_descriptor (new_rtl, mode, initialized);
15806 break;
15808 case CONCAT:
15809 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15810 initialized);
15811 break;
15813 case CONCATN:
15814 loc_result = concatn_loc_descriptor (rtl, initialized);
15815 break;
15817 case VAR_LOCATION:
15818 /* Single part. */
15819 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15821 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15822 if (GET_CODE (loc) == EXPR_LIST)
15823 loc = XEXP (loc, 0);
15824 loc_result = loc_descriptor (loc, mode, initialized);
15825 break;
15828 rtl = XEXP (rtl, 1);
15829 /* FALLTHRU */
15831 case PARALLEL:
15833 rtvec par_elems = XVEC (rtl, 0);
15834 int num_elem = GET_NUM_ELEM (par_elems);
15835 machine_mode mode;
15836 int i;
15838 /* Create the first one, so we have something to add to. */
15839 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15840 VOIDmode, initialized);
15841 if (loc_result == NULL)
15842 return NULL;
15843 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15844 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15845 for (i = 1; i < num_elem; i++)
15847 dw_loc_descr_ref temp;
15849 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15850 VOIDmode, initialized);
15851 if (temp == NULL)
15852 return NULL;
15853 add_loc_descr (&loc_result, temp);
15854 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15855 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15858 break;
15860 case CONST_INT:
15861 if (mode != VOIDmode && mode != BLKmode)
15863 int_mode = as_a <scalar_int_mode> (mode);
15864 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15865 INTVAL (rtl));
15867 break;
15869 case CONST_DOUBLE:
15870 if (mode == VOIDmode)
15871 mode = GET_MODE (rtl);
15873 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15875 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15877 /* Note that a CONST_DOUBLE rtx could represent either an integer
15878 or a floating-point constant. A CONST_DOUBLE is used whenever
15879 the constant requires more than one word in order to be
15880 adequately represented. We output CONST_DOUBLEs as blocks. */
15881 scalar_mode smode = as_a <scalar_mode> (mode);
15882 loc_result = new_loc_descr (DW_OP_implicit_value,
15883 GET_MODE_SIZE (smode), 0);
15884 #if TARGET_SUPPORTS_WIDE_INT == 0
15885 if (!SCALAR_FLOAT_MODE_P (smode))
15887 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15888 loc_result->dw_loc_oprnd2.v.val_double
15889 = rtx_to_double_int (rtl);
15891 else
15892 #endif
15894 unsigned int length = GET_MODE_SIZE (smode);
15895 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15897 insert_float (rtl, array);
15898 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15899 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15900 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15901 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15904 break;
15906 case CONST_WIDE_INT:
15907 if (mode == VOIDmode)
15908 mode = GET_MODE (rtl);
15910 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15912 int_mode = as_a <scalar_int_mode> (mode);
15913 loc_result = new_loc_descr (DW_OP_implicit_value,
15914 GET_MODE_SIZE (int_mode), 0);
15915 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15916 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15917 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15919 break;
15921 case CONST_VECTOR:
15922 if (mode == VOIDmode)
15923 mode = GET_MODE (rtl);
15925 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15927 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15928 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15929 unsigned char *array
15930 = ggc_vec_alloc<unsigned char> (length * elt_size);
15931 unsigned int i;
15932 unsigned char *p;
15933 machine_mode imode = GET_MODE_INNER (mode);
15935 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15936 switch (GET_MODE_CLASS (mode))
15938 case MODE_VECTOR_INT:
15939 for (i = 0, p = array; i < length; i++, p += elt_size)
15941 rtx elt = CONST_VECTOR_ELT (rtl, i);
15942 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15944 break;
15946 case MODE_VECTOR_FLOAT:
15947 for (i = 0, p = array; i < length; i++, p += elt_size)
15949 rtx elt = CONST_VECTOR_ELT (rtl, i);
15950 insert_float (elt, p);
15952 break;
15954 default:
15955 gcc_unreachable ();
15958 loc_result = new_loc_descr (DW_OP_implicit_value,
15959 length * elt_size, 0);
15960 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15961 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15962 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15963 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15965 break;
15967 case CONST:
15968 if (mode == VOIDmode
15969 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15970 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15971 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15973 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15974 break;
15976 /* FALLTHROUGH */
15977 case SYMBOL_REF:
15978 if (!const_ok_for_output (rtl))
15979 break;
15980 /* FALLTHROUGH */
15981 case LABEL_REF:
15982 if (is_a <scalar_int_mode> (mode, &int_mode)
15983 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15984 && (dwarf_version >= 4 || !dwarf_strict))
15986 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15987 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15988 vec_safe_push (used_rtx_array, rtl);
15990 break;
15992 case DEBUG_IMPLICIT_PTR:
15993 loc_result = implicit_ptr_descriptor (rtl, 0);
15994 break;
15996 case PLUS:
15997 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15998 && CONST_INT_P (XEXP (rtl, 1)))
16000 loc_result
16001 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16002 break;
16004 /* FALLTHRU */
16005 do_default:
16006 default:
16007 if ((is_a <scalar_int_mode> (mode, &int_mode)
16008 && GET_MODE (rtl) == int_mode
16009 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16010 && dwarf_version >= 4)
16011 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16013 /* Value expression. */
16014 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16015 if (loc_result)
16016 add_loc_descr (&loc_result,
16017 new_loc_descr (DW_OP_stack_value, 0, 0));
16019 break;
16022 return loc_result;
16025 /* We need to figure out what section we should use as the base for the
16026 address ranges where a given location is valid.
16027 1. If this particular DECL has a section associated with it, use that.
16028 2. If this function has a section associated with it, use that.
16029 3. Otherwise, use the text section.
16030 XXX: If you split a variable across multiple sections, we won't notice. */
16032 static const char *
16033 secname_for_decl (const_tree decl)
16035 const char *secname;
16037 if (VAR_OR_FUNCTION_DECL_P (decl)
16038 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16039 && DECL_SECTION_NAME (decl))
16040 secname = DECL_SECTION_NAME (decl);
16041 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16042 secname = DECL_SECTION_NAME (current_function_decl);
16043 else if (cfun && in_cold_section_p)
16044 secname = crtl->subsections.cold_section_label;
16045 else
16046 secname = text_section_label;
16048 return secname;
16051 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16053 static bool
16054 decl_by_reference_p (tree decl)
16056 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16057 || VAR_P (decl))
16058 && DECL_BY_REFERENCE (decl));
16061 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16062 for VARLOC. */
16064 static dw_loc_descr_ref
16065 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16066 enum var_init_status initialized)
16068 int have_address = 0;
16069 dw_loc_descr_ref descr;
16070 machine_mode mode;
16072 if (want_address != 2)
16074 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16075 /* Single part. */
16076 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16078 varloc = PAT_VAR_LOCATION_LOC (varloc);
16079 if (GET_CODE (varloc) == EXPR_LIST)
16080 varloc = XEXP (varloc, 0);
16081 mode = GET_MODE (varloc);
16082 if (MEM_P (varloc))
16084 rtx addr = XEXP (varloc, 0);
16085 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16086 mode, initialized);
16087 if (descr)
16088 have_address = 1;
16089 else
16091 rtx x = avoid_constant_pool_reference (varloc);
16092 if (x != varloc)
16093 descr = mem_loc_descriptor (x, mode, VOIDmode,
16094 initialized);
16097 else
16098 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16100 else
16101 return 0;
16103 else
16105 if (GET_CODE (varloc) == VAR_LOCATION)
16106 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16107 else
16108 mode = DECL_MODE (loc);
16109 descr = loc_descriptor (varloc, mode, initialized);
16110 have_address = 1;
16113 if (!descr)
16114 return 0;
16116 if (want_address == 2 && !have_address
16117 && (dwarf_version >= 4 || !dwarf_strict))
16119 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16121 expansion_failed (loc, NULL_RTX,
16122 "DWARF address size mismatch");
16123 return 0;
16125 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16126 have_address = 1;
16128 /* Show if we can't fill the request for an address. */
16129 if (want_address && !have_address)
16131 expansion_failed (loc, NULL_RTX,
16132 "Want address and only have value");
16133 return 0;
16136 /* If we've got an address and don't want one, dereference. */
16137 if (!want_address && have_address)
16139 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16140 enum dwarf_location_atom op;
16142 if (size > DWARF2_ADDR_SIZE || size == -1)
16144 expansion_failed (loc, NULL_RTX,
16145 "DWARF address size mismatch");
16146 return 0;
16148 else if (size == DWARF2_ADDR_SIZE)
16149 op = DW_OP_deref;
16150 else
16151 op = DW_OP_deref_size;
16153 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16156 return descr;
16159 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16160 if it is not possible. */
16162 static dw_loc_descr_ref
16163 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16165 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16166 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16167 else if (dwarf_version >= 3 || !dwarf_strict)
16168 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16169 else
16170 return NULL;
16173 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16174 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16176 static dw_loc_descr_ref
16177 dw_sra_loc_expr (tree decl, rtx loc)
16179 rtx p;
16180 unsigned HOST_WIDE_INT padsize = 0;
16181 dw_loc_descr_ref descr, *descr_tail;
16182 unsigned HOST_WIDE_INT decl_size;
16183 rtx varloc;
16184 enum var_init_status initialized;
16186 if (DECL_SIZE (decl) == NULL
16187 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16188 return NULL;
16190 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16191 descr = NULL;
16192 descr_tail = &descr;
16194 for (p = loc; p; p = XEXP (p, 1))
16196 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16197 rtx loc_note = *decl_piece_varloc_ptr (p);
16198 dw_loc_descr_ref cur_descr;
16199 dw_loc_descr_ref *tail, last = NULL;
16200 unsigned HOST_WIDE_INT opsize = 0;
16202 if (loc_note == NULL_RTX
16203 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16205 padsize += bitsize;
16206 continue;
16208 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16209 varloc = NOTE_VAR_LOCATION (loc_note);
16210 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16211 if (cur_descr == NULL)
16213 padsize += bitsize;
16214 continue;
16217 /* Check that cur_descr either doesn't use
16218 DW_OP_*piece operations, or their sum is equal
16219 to bitsize. Otherwise we can't embed it. */
16220 for (tail = &cur_descr; *tail != NULL;
16221 tail = &(*tail)->dw_loc_next)
16222 if ((*tail)->dw_loc_opc == DW_OP_piece)
16224 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16225 * BITS_PER_UNIT;
16226 last = *tail;
16228 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16230 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16231 last = *tail;
16234 if (last != NULL && opsize != bitsize)
16236 padsize += bitsize;
16237 /* Discard the current piece of the descriptor and release any
16238 addr_table entries it uses. */
16239 remove_loc_list_addr_table_entries (cur_descr);
16240 continue;
16243 /* If there is a hole, add DW_OP_*piece after empty DWARF
16244 expression, which means that those bits are optimized out. */
16245 if (padsize)
16247 if (padsize > decl_size)
16249 remove_loc_list_addr_table_entries (cur_descr);
16250 goto discard_descr;
16252 decl_size -= padsize;
16253 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16254 if (*descr_tail == NULL)
16256 remove_loc_list_addr_table_entries (cur_descr);
16257 goto discard_descr;
16259 descr_tail = &(*descr_tail)->dw_loc_next;
16260 padsize = 0;
16262 *descr_tail = cur_descr;
16263 descr_tail = tail;
16264 if (bitsize > decl_size)
16265 goto discard_descr;
16266 decl_size -= bitsize;
16267 if (last == NULL)
16269 HOST_WIDE_INT offset = 0;
16270 if (GET_CODE (varloc) == VAR_LOCATION
16271 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16273 varloc = PAT_VAR_LOCATION_LOC (varloc);
16274 if (GET_CODE (varloc) == EXPR_LIST)
16275 varloc = XEXP (varloc, 0);
16279 if (GET_CODE (varloc) == CONST
16280 || GET_CODE (varloc) == SIGN_EXTEND
16281 || GET_CODE (varloc) == ZERO_EXTEND)
16282 varloc = XEXP (varloc, 0);
16283 else if (GET_CODE (varloc) == SUBREG)
16284 varloc = SUBREG_REG (varloc);
16285 else
16286 break;
16288 while (1);
16289 /* DW_OP_bit_size offset should be zero for register
16290 or implicit location descriptions and empty location
16291 descriptions, but for memory addresses needs big endian
16292 adjustment. */
16293 if (MEM_P (varloc))
16295 unsigned HOST_WIDE_INT memsize
16296 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16297 if (memsize != bitsize)
16299 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16300 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16301 goto discard_descr;
16302 if (memsize < bitsize)
16303 goto discard_descr;
16304 if (BITS_BIG_ENDIAN)
16305 offset = memsize - bitsize;
16309 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16310 if (*descr_tail == NULL)
16311 goto discard_descr;
16312 descr_tail = &(*descr_tail)->dw_loc_next;
16316 /* If there were any non-empty expressions, add padding till the end of
16317 the decl. */
16318 if (descr != NULL && decl_size != 0)
16320 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16321 if (*descr_tail == NULL)
16322 goto discard_descr;
16324 return descr;
16326 discard_descr:
16327 /* Discard the descriptor and release any addr_table entries it uses. */
16328 remove_loc_list_addr_table_entries (descr);
16329 return NULL;
16332 /* Return the dwarf representation of the location list LOC_LIST of
16333 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16334 function. */
16336 static dw_loc_list_ref
16337 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16339 const char *endname, *secname;
16340 rtx varloc;
16341 enum var_init_status initialized;
16342 struct var_loc_node *node;
16343 dw_loc_descr_ref descr;
16344 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16345 dw_loc_list_ref list = NULL;
16346 dw_loc_list_ref *listp = &list;
16348 /* Now that we know what section we are using for a base,
16349 actually construct the list of locations.
16350 The first location information is what is passed to the
16351 function that creates the location list, and the remaining
16352 locations just get added on to that list.
16353 Note that we only know the start address for a location
16354 (IE location changes), so to build the range, we use
16355 the range [current location start, next location start].
16356 This means we have to special case the last node, and generate
16357 a range of [last location start, end of function label]. */
16359 if (cfun && crtl->has_bb_partition)
16361 bool save_in_cold_section_p = in_cold_section_p;
16362 in_cold_section_p = first_function_block_is_cold;
16363 if (loc_list->last_before_switch == NULL)
16364 in_cold_section_p = !in_cold_section_p;
16365 secname = secname_for_decl (decl);
16366 in_cold_section_p = save_in_cold_section_p;
16368 else
16369 secname = secname_for_decl (decl);
16371 for (node = loc_list->first; node; node = node->next)
16373 bool range_across_switch = false;
16374 if (GET_CODE (node->loc) == EXPR_LIST
16375 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16377 if (GET_CODE (node->loc) == EXPR_LIST)
16379 descr = NULL;
16380 /* This requires DW_OP_{,bit_}piece, which is not usable
16381 inside DWARF expressions. */
16382 if (want_address == 2)
16383 descr = dw_sra_loc_expr (decl, node->loc);
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 /* If section switch happens in between node->label
16394 and node->next->label (or end of function) and
16395 we can't emit it as a single entry list,
16396 emit two ranges, first one ending at the end
16397 of first partition and second one starting at the
16398 beginning of second partition. */
16399 if (node == loc_list->last_before_switch
16400 && (node != loc_list->first || loc_list->first->next)
16401 && current_function_decl)
16403 endname = cfun->fde->dw_fde_end;
16404 range_across_switch = true;
16406 /* The variable has a location between NODE->LABEL and
16407 NODE->NEXT->LABEL. */
16408 else if (node->next)
16409 endname = node->next->label;
16410 /* If the variable has a location at the last label
16411 it keeps its location until the end of function. */
16412 else if (!current_function_decl)
16413 endname = text_end_label;
16414 else
16416 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16417 current_function_funcdef_no);
16418 endname = ggc_strdup (label_id);
16421 *listp = new_loc_list (descr, node->label, endname, secname);
16422 if (TREE_CODE (decl) == PARM_DECL
16423 && node == loc_list->first
16424 && NOTE_P (node->loc)
16425 && strcmp (node->label, endname) == 0)
16426 (*listp)->force = true;
16427 listp = &(*listp)->dw_loc_next;
16431 if (cfun
16432 && crtl->has_bb_partition
16433 && node == loc_list->last_before_switch)
16435 bool save_in_cold_section_p = in_cold_section_p;
16436 in_cold_section_p = !first_function_block_is_cold;
16437 secname = secname_for_decl (decl);
16438 in_cold_section_p = save_in_cold_section_p;
16441 if (range_across_switch)
16443 if (GET_CODE (node->loc) == EXPR_LIST)
16444 descr = dw_sra_loc_expr (decl, node->loc);
16445 else
16447 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16448 varloc = NOTE_VAR_LOCATION (node->loc);
16449 descr = dw_loc_list_1 (decl, varloc, want_address,
16450 initialized);
16452 gcc_assert (descr);
16453 /* The variable has a location between NODE->LABEL and
16454 NODE->NEXT->LABEL. */
16455 if (node->next)
16456 endname = node->next->label;
16457 else
16458 endname = cfun->fde->dw_fde_second_end;
16459 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16460 endname, secname);
16461 listp = &(*listp)->dw_loc_next;
16465 /* Try to avoid the overhead of a location list emitting a location
16466 expression instead, but only if we didn't have more than one
16467 location entry in the first place. If some entries were not
16468 representable, we don't want to pretend a single entry that was
16469 applies to the entire scope in which the variable is
16470 available. */
16471 if (list && loc_list->first->next)
16472 gen_llsym (list);
16474 return list;
16477 /* Return if the loc_list has only single element and thus can be represented
16478 as location description. */
16480 static bool
16481 single_element_loc_list_p (dw_loc_list_ref list)
16483 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16484 return !list->ll_symbol;
16487 /* Duplicate a single element of location list. */
16489 static inline dw_loc_descr_ref
16490 copy_loc_descr (dw_loc_descr_ref ref)
16492 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16493 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16494 return copy;
16497 /* To each location in list LIST append loc descr REF. */
16499 static void
16500 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16502 dw_loc_descr_ref copy;
16503 add_loc_descr (&list->expr, ref);
16504 list = list->dw_loc_next;
16505 while (list)
16507 copy = copy_loc_descr (ref);
16508 add_loc_descr (&list->expr, copy);
16509 while (copy->dw_loc_next)
16510 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16511 list = list->dw_loc_next;
16515 /* To each location in list LIST prepend loc descr REF. */
16517 static void
16518 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16520 dw_loc_descr_ref copy;
16521 dw_loc_descr_ref ref_end = list->expr;
16522 add_loc_descr (&ref, list->expr);
16523 list->expr = ref;
16524 list = list->dw_loc_next;
16525 while (list)
16527 dw_loc_descr_ref end = list->expr;
16528 list->expr = copy = copy_loc_descr (ref);
16529 while (copy->dw_loc_next != ref_end)
16530 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16531 copy->dw_loc_next = end;
16532 list = list->dw_loc_next;
16536 /* Given two lists RET and LIST
16537 produce location list that is result of adding expression in LIST
16538 to expression in RET on each position in program.
16539 Might be destructive on both RET and LIST.
16541 TODO: We handle only simple cases of RET or LIST having at most one
16542 element. General case would involve sorting the lists in program order
16543 and merging them that will need some additional work.
16544 Adding that will improve quality of debug info especially for SRA-ed
16545 structures. */
16547 static void
16548 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16550 if (!list)
16551 return;
16552 if (!*ret)
16554 *ret = list;
16555 return;
16557 if (!list->dw_loc_next)
16559 add_loc_descr_to_each (*ret, list->expr);
16560 return;
16562 if (!(*ret)->dw_loc_next)
16564 prepend_loc_descr_to_each (list, (*ret)->expr);
16565 *ret = list;
16566 return;
16568 expansion_failed (NULL_TREE, NULL_RTX,
16569 "Don't know how to merge two non-trivial"
16570 " location lists.\n");
16571 *ret = NULL;
16572 return;
16575 /* LOC is constant expression. Try a luck, look it up in constant
16576 pool and return its loc_descr of its address. */
16578 static dw_loc_descr_ref
16579 cst_pool_loc_descr (tree loc)
16581 /* Get an RTL for this, if something has been emitted. */
16582 rtx rtl = lookup_constant_def (loc);
16584 if (!rtl || !MEM_P (rtl))
16586 gcc_assert (!rtl);
16587 return 0;
16589 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16591 /* TODO: We might get more coverage if we was actually delaying expansion
16592 of all expressions till end of compilation when constant pools are fully
16593 populated. */
16594 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16596 expansion_failed (loc, NULL_RTX,
16597 "CST value in contant pool but not marked.");
16598 return 0;
16600 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16601 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16604 /* Return dw_loc_list representing address of addr_expr LOC
16605 by looking for inner INDIRECT_REF expression and turning
16606 it into simple arithmetics.
16608 See loc_list_from_tree for the meaning of CONTEXT. */
16610 static dw_loc_list_ref
16611 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16612 loc_descr_context *context)
16614 tree obj, offset;
16615 HOST_WIDE_INT bitsize, bitpos, bytepos;
16616 machine_mode mode;
16617 int unsignedp, reversep, volatilep = 0;
16618 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16620 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16621 &bitsize, &bitpos, &offset, &mode,
16622 &unsignedp, &reversep, &volatilep);
16623 STRIP_NOPS (obj);
16624 if (bitpos % BITS_PER_UNIT)
16626 expansion_failed (loc, NULL_RTX, "bitfield access");
16627 return 0;
16629 if (!INDIRECT_REF_P (obj))
16631 expansion_failed (obj,
16632 NULL_RTX, "no indirect ref in inner refrence");
16633 return 0;
16635 if (!offset && !bitpos)
16636 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16637 context);
16638 else if (toplev
16639 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16640 && (dwarf_version >= 4 || !dwarf_strict))
16642 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16643 if (!list_ret)
16644 return 0;
16645 if (offset)
16647 /* Variable offset. */
16648 list_ret1 = loc_list_from_tree (offset, 0, context);
16649 if (list_ret1 == 0)
16650 return 0;
16651 add_loc_list (&list_ret, list_ret1);
16652 if (!list_ret)
16653 return 0;
16654 add_loc_descr_to_each (list_ret,
16655 new_loc_descr (DW_OP_plus, 0, 0));
16657 bytepos = bitpos / BITS_PER_UNIT;
16658 if (bytepos > 0)
16659 add_loc_descr_to_each (list_ret,
16660 new_loc_descr (DW_OP_plus_uconst,
16661 bytepos, 0));
16662 else if (bytepos < 0)
16663 loc_list_plus_const (list_ret, bytepos);
16664 add_loc_descr_to_each (list_ret,
16665 new_loc_descr (DW_OP_stack_value, 0, 0));
16667 return list_ret;
16670 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16671 all operations from LOC are nops, move to the last one. Insert in NOPS all
16672 operations that are skipped. */
16674 static void
16675 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16676 hash_set<dw_loc_descr_ref> &nops)
16678 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16680 nops.add (loc);
16681 loc = loc->dw_loc_next;
16685 /* Helper for loc_descr_without_nops: free the location description operation
16686 P. */
16688 bool
16689 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16691 ggc_free (loc);
16692 return true;
16695 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16696 finishes LOC. */
16698 static void
16699 loc_descr_without_nops (dw_loc_descr_ref &loc)
16701 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16702 return;
16704 /* Set of all DW_OP_nop operations we remove. */
16705 hash_set<dw_loc_descr_ref> nops;
16707 /* First, strip all prefix NOP operations in order to keep the head of the
16708 operations list. */
16709 loc_descr_to_next_no_nop (loc, nops);
16711 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16713 /* For control flow operations: strip "prefix" nops in destination
16714 labels. */
16715 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16716 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16717 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16718 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16720 /* Do the same for the operations that follow, then move to the next
16721 iteration. */
16722 if (cur->dw_loc_next != NULL)
16723 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16724 cur = cur->dw_loc_next;
16727 nops.traverse<void *, free_loc_descr> (NULL);
16731 struct dwarf_procedure_info;
16733 /* Helper structure for location descriptions generation. */
16734 struct loc_descr_context
16736 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16737 NULL_TREE if DW_OP_push_object_address in invalid for this location
16738 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16739 tree context_type;
16740 /* The ..._DECL node that should be translated as a
16741 DW_OP_push_object_address operation. */
16742 tree base_decl;
16743 /* Information about the DWARF procedure we are currently generating. NULL if
16744 we are not generating a DWARF procedure. */
16745 struct dwarf_procedure_info *dpi;
16746 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16747 by consumer. Used for DW_TAG_generic_subrange attributes. */
16748 bool placeholder_arg;
16749 /* True if PLACEHOLDER_EXPR has been seen. */
16750 bool placeholder_seen;
16753 /* DWARF procedures generation
16755 DWARF expressions (aka. location descriptions) are used to encode variable
16756 things such as sizes or offsets. Such computations can have redundant parts
16757 that can be factorized in order to reduce the size of the output debug
16758 information. This is the whole point of DWARF procedures.
16760 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16761 already factorized into functions ("size functions") in order to handle very
16762 big and complex types. Such functions are quite simple: they have integral
16763 arguments, they return an integral result and their body contains only a
16764 return statement with arithmetic expressions. This is the only kind of
16765 function we are interested in translating into DWARF procedures, here.
16767 DWARF expressions and DWARF procedure are executed using a stack, so we have
16768 to define some calling convention for them to interact. Let's say that:
16770 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16771 all arguments in reverse order (right-to-left) so that when the DWARF
16772 procedure execution starts, the first argument is the top of the stack.
16774 - Then, when returning, the DWARF procedure must have consumed all arguments
16775 on the stack, must have pushed the result and touched nothing else.
16777 - Each integral argument and the result are integral types can be hold in a
16778 single stack slot.
16780 - We call "frame offset" the number of stack slots that are "under DWARF
16781 procedure control": it includes the arguments slots, the temporaries and
16782 the result slot. Thus, it is equal to the number of arguments when the
16783 procedure execution starts and must be equal to one (the result) when it
16784 returns. */
16786 /* Helper structure used when generating operations for a DWARF procedure. */
16787 struct dwarf_procedure_info
16789 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16790 currently translated. */
16791 tree fndecl;
16792 /* The number of arguments FNDECL takes. */
16793 unsigned args_count;
16796 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16797 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16798 equate it to this DIE. */
16800 static dw_die_ref
16801 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16802 dw_die_ref parent_die)
16804 dw_die_ref dwarf_proc_die;
16806 if ((dwarf_version < 3 && dwarf_strict)
16807 || location == NULL)
16808 return NULL;
16810 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16811 if (fndecl)
16812 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16813 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16814 return dwarf_proc_die;
16817 /* Return whether TYPE is a supported type as a DWARF procedure argument
16818 type or return type (we handle only scalar types and pointer types that
16819 aren't wider than the DWARF expression evaluation stack. */
16821 static bool
16822 is_handled_procedure_type (tree type)
16824 return ((INTEGRAL_TYPE_P (type)
16825 || TREE_CODE (type) == OFFSET_TYPE
16826 || TREE_CODE (type) == POINTER_TYPE)
16827 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16830 /* Helper for resolve_args_picking: do the same but stop when coming across
16831 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16832 offset *before* evaluating the corresponding operation. */
16834 static bool
16835 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16836 struct dwarf_procedure_info *dpi,
16837 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16839 /* The "frame_offset" identifier is already used to name a macro... */
16840 unsigned frame_offset_ = initial_frame_offset;
16841 dw_loc_descr_ref l;
16843 for (l = loc; l != NULL;)
16845 bool existed;
16846 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16848 /* If we already met this node, there is nothing to compute anymore. */
16849 if (existed)
16851 /* Make sure that the stack size is consistent wherever the execution
16852 flow comes from. */
16853 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16854 break;
16856 l_frame_offset = frame_offset_;
16858 /* If needed, relocate the picking offset with respect to the frame
16859 offset. */
16860 if (l->frame_offset_rel)
16862 unsigned HOST_WIDE_INT off;
16863 switch (l->dw_loc_opc)
16865 case DW_OP_pick:
16866 off = l->dw_loc_oprnd1.v.val_unsigned;
16867 break;
16868 case DW_OP_dup:
16869 off = 0;
16870 break;
16871 case DW_OP_over:
16872 off = 1;
16873 break;
16874 default:
16875 gcc_unreachable ();
16877 /* frame_offset_ is the size of the current stack frame, including
16878 incoming arguments. Besides, the arguments are pushed
16879 right-to-left. Thus, in order to access the Nth argument from
16880 this operation node, the picking has to skip temporaries *plus*
16881 one stack slot per argument (0 for the first one, 1 for the second
16882 one, etc.).
16884 The targetted argument number (N) is already set as the operand,
16885 and the number of temporaries can be computed with:
16886 frame_offsets_ - dpi->args_count */
16887 off += frame_offset_ - dpi->args_count;
16889 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16890 if (off > 255)
16891 return false;
16893 if (off == 0)
16895 l->dw_loc_opc = DW_OP_dup;
16896 l->dw_loc_oprnd1.v.val_unsigned = 0;
16898 else if (off == 1)
16900 l->dw_loc_opc = DW_OP_over;
16901 l->dw_loc_oprnd1.v.val_unsigned = 0;
16903 else
16905 l->dw_loc_opc = DW_OP_pick;
16906 l->dw_loc_oprnd1.v.val_unsigned = off;
16910 /* Update frame_offset according to the effect the current operation has
16911 on the stack. */
16912 switch (l->dw_loc_opc)
16914 case DW_OP_deref:
16915 case DW_OP_swap:
16916 case DW_OP_rot:
16917 case DW_OP_abs:
16918 case DW_OP_neg:
16919 case DW_OP_not:
16920 case DW_OP_plus_uconst:
16921 case DW_OP_skip:
16922 case DW_OP_reg0:
16923 case DW_OP_reg1:
16924 case DW_OP_reg2:
16925 case DW_OP_reg3:
16926 case DW_OP_reg4:
16927 case DW_OP_reg5:
16928 case DW_OP_reg6:
16929 case DW_OP_reg7:
16930 case DW_OP_reg8:
16931 case DW_OP_reg9:
16932 case DW_OP_reg10:
16933 case DW_OP_reg11:
16934 case DW_OP_reg12:
16935 case DW_OP_reg13:
16936 case DW_OP_reg14:
16937 case DW_OP_reg15:
16938 case DW_OP_reg16:
16939 case DW_OP_reg17:
16940 case DW_OP_reg18:
16941 case DW_OP_reg19:
16942 case DW_OP_reg20:
16943 case DW_OP_reg21:
16944 case DW_OP_reg22:
16945 case DW_OP_reg23:
16946 case DW_OP_reg24:
16947 case DW_OP_reg25:
16948 case DW_OP_reg26:
16949 case DW_OP_reg27:
16950 case DW_OP_reg28:
16951 case DW_OP_reg29:
16952 case DW_OP_reg30:
16953 case DW_OP_reg31:
16954 case DW_OP_bregx:
16955 case DW_OP_piece:
16956 case DW_OP_deref_size:
16957 case DW_OP_nop:
16958 case DW_OP_bit_piece:
16959 case DW_OP_implicit_value:
16960 case DW_OP_stack_value:
16961 break;
16963 case DW_OP_addr:
16964 case DW_OP_const1u:
16965 case DW_OP_const1s:
16966 case DW_OP_const2u:
16967 case DW_OP_const2s:
16968 case DW_OP_const4u:
16969 case DW_OP_const4s:
16970 case DW_OP_const8u:
16971 case DW_OP_const8s:
16972 case DW_OP_constu:
16973 case DW_OP_consts:
16974 case DW_OP_dup:
16975 case DW_OP_over:
16976 case DW_OP_pick:
16977 case DW_OP_lit0:
16978 case DW_OP_lit1:
16979 case DW_OP_lit2:
16980 case DW_OP_lit3:
16981 case DW_OP_lit4:
16982 case DW_OP_lit5:
16983 case DW_OP_lit6:
16984 case DW_OP_lit7:
16985 case DW_OP_lit8:
16986 case DW_OP_lit9:
16987 case DW_OP_lit10:
16988 case DW_OP_lit11:
16989 case DW_OP_lit12:
16990 case DW_OP_lit13:
16991 case DW_OP_lit14:
16992 case DW_OP_lit15:
16993 case DW_OP_lit16:
16994 case DW_OP_lit17:
16995 case DW_OP_lit18:
16996 case DW_OP_lit19:
16997 case DW_OP_lit20:
16998 case DW_OP_lit21:
16999 case DW_OP_lit22:
17000 case DW_OP_lit23:
17001 case DW_OP_lit24:
17002 case DW_OP_lit25:
17003 case DW_OP_lit26:
17004 case DW_OP_lit27:
17005 case DW_OP_lit28:
17006 case DW_OP_lit29:
17007 case DW_OP_lit30:
17008 case DW_OP_lit31:
17009 case DW_OP_breg0:
17010 case DW_OP_breg1:
17011 case DW_OP_breg2:
17012 case DW_OP_breg3:
17013 case DW_OP_breg4:
17014 case DW_OP_breg5:
17015 case DW_OP_breg6:
17016 case DW_OP_breg7:
17017 case DW_OP_breg8:
17018 case DW_OP_breg9:
17019 case DW_OP_breg10:
17020 case DW_OP_breg11:
17021 case DW_OP_breg12:
17022 case DW_OP_breg13:
17023 case DW_OP_breg14:
17024 case DW_OP_breg15:
17025 case DW_OP_breg16:
17026 case DW_OP_breg17:
17027 case DW_OP_breg18:
17028 case DW_OP_breg19:
17029 case DW_OP_breg20:
17030 case DW_OP_breg21:
17031 case DW_OP_breg22:
17032 case DW_OP_breg23:
17033 case DW_OP_breg24:
17034 case DW_OP_breg25:
17035 case DW_OP_breg26:
17036 case DW_OP_breg27:
17037 case DW_OP_breg28:
17038 case DW_OP_breg29:
17039 case DW_OP_breg30:
17040 case DW_OP_breg31:
17041 case DW_OP_fbreg:
17042 case DW_OP_push_object_address:
17043 case DW_OP_call_frame_cfa:
17044 case DW_OP_GNU_variable_value:
17045 ++frame_offset_;
17046 break;
17048 case DW_OP_drop:
17049 case DW_OP_xderef:
17050 case DW_OP_and:
17051 case DW_OP_div:
17052 case DW_OP_minus:
17053 case DW_OP_mod:
17054 case DW_OP_mul:
17055 case DW_OP_or:
17056 case DW_OP_plus:
17057 case DW_OP_shl:
17058 case DW_OP_shr:
17059 case DW_OP_shra:
17060 case DW_OP_xor:
17061 case DW_OP_bra:
17062 case DW_OP_eq:
17063 case DW_OP_ge:
17064 case DW_OP_gt:
17065 case DW_OP_le:
17066 case DW_OP_lt:
17067 case DW_OP_ne:
17068 case DW_OP_regx:
17069 case DW_OP_xderef_size:
17070 --frame_offset_;
17071 break;
17073 case DW_OP_call2:
17074 case DW_OP_call4:
17075 case DW_OP_call_ref:
17077 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17078 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17080 if (stack_usage == NULL)
17081 return false;
17082 frame_offset_ += *stack_usage;
17083 break;
17086 case DW_OP_implicit_pointer:
17087 case DW_OP_entry_value:
17088 case DW_OP_const_type:
17089 case DW_OP_regval_type:
17090 case DW_OP_deref_type:
17091 case DW_OP_convert:
17092 case DW_OP_reinterpret:
17093 case DW_OP_form_tls_address:
17094 case DW_OP_GNU_push_tls_address:
17095 case DW_OP_GNU_uninit:
17096 case DW_OP_GNU_encoded_addr:
17097 case DW_OP_GNU_implicit_pointer:
17098 case DW_OP_GNU_entry_value:
17099 case DW_OP_GNU_const_type:
17100 case DW_OP_GNU_regval_type:
17101 case DW_OP_GNU_deref_type:
17102 case DW_OP_GNU_convert:
17103 case DW_OP_GNU_reinterpret:
17104 case DW_OP_GNU_parameter_ref:
17105 /* loc_list_from_tree will probably not output these operations for
17106 size functions, so assume they will not appear here. */
17107 /* Fall through... */
17109 default:
17110 gcc_unreachable ();
17113 /* Now, follow the control flow (except subroutine calls). */
17114 switch (l->dw_loc_opc)
17116 case DW_OP_bra:
17117 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17118 frame_offsets))
17119 return false;
17120 /* Fall through. */
17122 case DW_OP_skip:
17123 l = l->dw_loc_oprnd1.v.val_loc;
17124 break;
17126 case DW_OP_stack_value:
17127 return true;
17129 default:
17130 l = l->dw_loc_next;
17131 break;
17135 return true;
17138 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17139 operations) in order to resolve the operand of DW_OP_pick operations that
17140 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17141 offset *before* LOC is executed. Return if all relocations were
17142 successful. */
17144 static bool
17145 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17146 struct dwarf_procedure_info *dpi)
17148 /* Associate to all visited operations the frame offset *before* evaluating
17149 this operation. */
17150 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17152 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17153 frame_offsets);
17156 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17157 Return NULL if it is not possible. */
17159 static dw_die_ref
17160 function_to_dwarf_procedure (tree fndecl)
17162 struct loc_descr_context ctx;
17163 struct dwarf_procedure_info dpi;
17164 dw_die_ref dwarf_proc_die;
17165 tree tree_body = DECL_SAVED_TREE (fndecl);
17166 dw_loc_descr_ref loc_body, epilogue;
17168 tree cursor;
17169 unsigned i;
17171 /* Do not generate multiple DWARF procedures for the same function
17172 declaration. */
17173 dwarf_proc_die = lookup_decl_die (fndecl);
17174 if (dwarf_proc_die != NULL)
17175 return dwarf_proc_die;
17177 /* DWARF procedures are available starting with the DWARFv3 standard. */
17178 if (dwarf_version < 3 && dwarf_strict)
17179 return NULL;
17181 /* We handle only functions for which we still have a body, that return a
17182 supported type and that takes arguments with supported types. Note that
17183 there is no point translating functions that return nothing. */
17184 if (tree_body == NULL_TREE
17185 || DECL_RESULT (fndecl) == NULL_TREE
17186 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17187 return NULL;
17189 for (cursor = DECL_ARGUMENTS (fndecl);
17190 cursor != NULL_TREE;
17191 cursor = TREE_CHAIN (cursor))
17192 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17193 return NULL;
17195 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17196 if (TREE_CODE (tree_body) != RETURN_EXPR)
17197 return NULL;
17198 tree_body = TREE_OPERAND (tree_body, 0);
17199 if (TREE_CODE (tree_body) != MODIFY_EXPR
17200 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17201 return NULL;
17202 tree_body = TREE_OPERAND (tree_body, 1);
17204 /* Try to translate the body expression itself. Note that this will probably
17205 cause an infinite recursion if its call graph has a cycle. This is very
17206 unlikely for size functions, however, so don't bother with such things at
17207 the moment. */
17208 ctx.context_type = NULL_TREE;
17209 ctx.base_decl = NULL_TREE;
17210 ctx.dpi = &dpi;
17211 ctx.placeholder_arg = false;
17212 ctx.placeholder_seen = false;
17213 dpi.fndecl = fndecl;
17214 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17215 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17216 if (!loc_body)
17217 return NULL;
17219 /* After evaluating all operands in "loc_body", we should still have on the
17220 stack all arguments plus the desired function result (top of the stack).
17221 Generate code in order to keep only the result in our stack frame. */
17222 epilogue = NULL;
17223 for (i = 0; i < dpi.args_count; ++i)
17225 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17226 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17227 op_couple->dw_loc_next->dw_loc_next = epilogue;
17228 epilogue = op_couple;
17230 add_loc_descr (&loc_body, epilogue);
17231 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17232 return NULL;
17234 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17235 because they are considered useful. Now there is an epilogue, they are
17236 not anymore, so give it another try. */
17237 loc_descr_without_nops (loc_body);
17239 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17240 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17241 though, given that size functions do not come from source, so they should
17242 not have a dedicated DW_TAG_subprogram DIE. */
17243 dwarf_proc_die
17244 = new_dwarf_proc_die (loc_body, fndecl,
17245 get_context_die (DECL_CONTEXT (fndecl)));
17247 /* The called DWARF procedure consumes one stack slot per argument and
17248 returns one stack slot. */
17249 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17251 return dwarf_proc_die;
17255 /* Generate Dwarf location list representing LOC.
17256 If WANT_ADDRESS is false, expression computing LOC will be computed
17257 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17258 if WANT_ADDRESS is 2, expression computing address useable in location
17259 will be returned (i.e. DW_OP_reg can be used
17260 to refer to register values).
17262 CONTEXT provides information to customize the location descriptions
17263 generation. Its context_type field specifies what type is implicitly
17264 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17265 will not be generated.
17267 Its DPI field determines whether we are generating a DWARF expression for a
17268 DWARF procedure, so PARM_DECL references are processed specifically.
17270 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17271 and dpi fields were null. */
17273 static dw_loc_list_ref
17274 loc_list_from_tree_1 (tree loc, int want_address,
17275 struct loc_descr_context *context)
17277 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17278 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17279 int have_address = 0;
17280 enum dwarf_location_atom op;
17282 /* ??? Most of the time we do not take proper care for sign/zero
17283 extending the values properly. Hopefully this won't be a real
17284 problem... */
17286 if (context != NULL
17287 && context->base_decl == loc
17288 && want_address == 0)
17290 if (dwarf_version >= 3 || !dwarf_strict)
17291 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17292 NULL, NULL, NULL);
17293 else
17294 return NULL;
17297 switch (TREE_CODE (loc))
17299 case ERROR_MARK:
17300 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17301 return 0;
17303 case PLACEHOLDER_EXPR:
17304 /* This case involves extracting fields from an object to determine the
17305 position of other fields. It is supposed to appear only as the first
17306 operand of COMPONENT_REF nodes and to reference precisely the type
17307 that the context allows. */
17308 if (context != NULL
17309 && TREE_TYPE (loc) == context->context_type
17310 && want_address >= 1)
17312 if (dwarf_version >= 3 || !dwarf_strict)
17314 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17315 have_address = 1;
17316 break;
17318 else
17319 return NULL;
17321 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17322 the single argument passed by consumer. */
17323 else if (context != NULL
17324 && context->placeholder_arg
17325 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17326 && want_address == 0)
17328 ret = new_loc_descr (DW_OP_pick, 0, 0);
17329 ret->frame_offset_rel = 1;
17330 context->placeholder_seen = true;
17331 break;
17333 else
17334 expansion_failed (loc, NULL_RTX,
17335 "PLACEHOLDER_EXPR for an unexpected type");
17336 break;
17338 case CALL_EXPR:
17340 const int nargs = call_expr_nargs (loc);
17341 tree callee = get_callee_fndecl (loc);
17342 int i;
17343 dw_die_ref dwarf_proc;
17345 if (callee == NULL_TREE)
17346 goto call_expansion_failed;
17348 /* We handle only functions that return an integer. */
17349 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17350 goto call_expansion_failed;
17352 dwarf_proc = function_to_dwarf_procedure (callee);
17353 if (dwarf_proc == NULL)
17354 goto call_expansion_failed;
17356 /* Evaluate arguments right-to-left so that the first argument will
17357 be the top-most one on the stack. */
17358 for (i = nargs - 1; i >= 0; --i)
17360 dw_loc_descr_ref loc_descr
17361 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17362 context);
17364 if (loc_descr == NULL)
17365 goto call_expansion_failed;
17367 add_loc_descr (&ret, loc_descr);
17370 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17371 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17372 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17373 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17374 add_loc_descr (&ret, ret1);
17375 break;
17377 call_expansion_failed:
17378 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17379 /* There are no opcodes for these operations. */
17380 return 0;
17383 case PREINCREMENT_EXPR:
17384 case PREDECREMENT_EXPR:
17385 case POSTINCREMENT_EXPR:
17386 case POSTDECREMENT_EXPR:
17387 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17388 /* There are no opcodes for these operations. */
17389 return 0;
17391 case ADDR_EXPR:
17392 /* If we already want an address, see if there is INDIRECT_REF inside
17393 e.g. for &this->field. */
17394 if (want_address)
17396 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17397 (loc, want_address == 2, context);
17398 if (list_ret)
17399 have_address = 1;
17400 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17401 && (ret = cst_pool_loc_descr (loc)))
17402 have_address = 1;
17404 /* Otherwise, process the argument and look for the address. */
17405 if (!list_ret && !ret)
17406 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17407 else
17409 if (want_address)
17410 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17411 return NULL;
17413 break;
17415 case VAR_DECL:
17416 if (DECL_THREAD_LOCAL_P (loc))
17418 rtx rtl;
17419 enum dwarf_location_atom tls_op;
17420 enum dtprel_bool dtprel = dtprel_false;
17422 if (targetm.have_tls)
17424 /* If this is not defined, we have no way to emit the
17425 data. */
17426 if (!targetm.asm_out.output_dwarf_dtprel)
17427 return 0;
17429 /* The way DW_OP_GNU_push_tls_address is specified, we
17430 can only look up addresses of objects in the current
17431 module. We used DW_OP_addr as first op, but that's
17432 wrong, because DW_OP_addr is relocated by the debug
17433 info consumer, while DW_OP_GNU_push_tls_address
17434 operand shouldn't be. */
17435 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17436 return 0;
17437 dtprel = dtprel_true;
17438 /* We check for DWARF 5 here because gdb did not implement
17439 DW_OP_form_tls_address until after 7.12. */
17440 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17441 : DW_OP_GNU_push_tls_address);
17443 else
17445 if (!targetm.emutls.debug_form_tls_address
17446 || !(dwarf_version >= 3 || !dwarf_strict))
17447 return 0;
17448 /* We stuffed the control variable into the DECL_VALUE_EXPR
17449 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17450 no longer appear in gimple code. We used the control
17451 variable in specific so that we could pick it up here. */
17452 loc = DECL_VALUE_EXPR (loc);
17453 tls_op = DW_OP_form_tls_address;
17456 rtl = rtl_for_decl_location (loc);
17457 if (rtl == NULL_RTX)
17458 return 0;
17460 if (!MEM_P (rtl))
17461 return 0;
17462 rtl = XEXP (rtl, 0);
17463 if (! CONSTANT_P (rtl))
17464 return 0;
17466 ret = new_addr_loc_descr (rtl, dtprel);
17467 ret1 = new_loc_descr (tls_op, 0, 0);
17468 add_loc_descr (&ret, ret1);
17470 have_address = 1;
17471 break;
17473 /* FALLTHRU */
17475 case PARM_DECL:
17476 if (context != NULL && context->dpi != NULL
17477 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17479 /* We are generating code for a DWARF procedure and we want to access
17480 one of its arguments: find the appropriate argument offset and let
17481 the resolve_args_picking pass compute the offset that complies
17482 with the stack frame size. */
17483 unsigned i = 0;
17484 tree cursor;
17486 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17487 cursor != NULL_TREE && cursor != loc;
17488 cursor = TREE_CHAIN (cursor), ++i)
17490 /* If we are translating a DWARF procedure, all referenced parameters
17491 must belong to the current function. */
17492 gcc_assert (cursor != NULL_TREE);
17494 ret = new_loc_descr (DW_OP_pick, i, 0);
17495 ret->frame_offset_rel = 1;
17496 break;
17498 /* FALLTHRU */
17500 case RESULT_DECL:
17501 if (DECL_HAS_VALUE_EXPR_P (loc))
17502 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17503 want_address, context);
17504 /* FALLTHRU */
17506 case FUNCTION_DECL:
17508 rtx rtl;
17509 var_loc_list *loc_list = lookup_decl_loc (loc);
17511 if (loc_list && loc_list->first)
17513 list_ret = dw_loc_list (loc_list, loc, want_address);
17514 have_address = want_address != 0;
17515 break;
17517 rtl = rtl_for_decl_location (loc);
17518 if (rtl == NULL_RTX)
17520 if (TREE_CODE (loc) != FUNCTION_DECL
17521 && early_dwarf
17522 && current_function_decl
17523 && want_address != 1
17524 && ! DECL_IGNORED_P (loc)
17525 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17526 || POINTER_TYPE_P (TREE_TYPE (loc)))
17527 && DECL_CONTEXT (loc) == current_function_decl
17528 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17529 <= DWARF2_ADDR_SIZE))
17531 dw_die_ref ref = lookup_decl_die (loc);
17532 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17533 if (ref)
17535 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17536 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17537 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17539 else
17541 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17542 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17544 break;
17546 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17547 return 0;
17549 else if (CONST_INT_P (rtl))
17551 HOST_WIDE_INT val = INTVAL (rtl);
17552 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17553 val &= GET_MODE_MASK (DECL_MODE (loc));
17554 ret = int_loc_descriptor (val);
17556 else if (GET_CODE (rtl) == CONST_STRING)
17558 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17559 return 0;
17561 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17562 ret = new_addr_loc_descr (rtl, dtprel_false);
17563 else
17565 machine_mode mode, mem_mode;
17567 /* Certain constructs can only be represented at top-level. */
17568 if (want_address == 2)
17570 ret = loc_descriptor (rtl, VOIDmode,
17571 VAR_INIT_STATUS_INITIALIZED);
17572 have_address = 1;
17574 else
17576 mode = GET_MODE (rtl);
17577 mem_mode = VOIDmode;
17578 if (MEM_P (rtl))
17580 mem_mode = mode;
17581 mode = get_address_mode (rtl);
17582 rtl = XEXP (rtl, 0);
17583 have_address = 1;
17585 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17586 VAR_INIT_STATUS_INITIALIZED);
17588 if (!ret)
17589 expansion_failed (loc, rtl,
17590 "failed to produce loc descriptor for rtl");
17593 break;
17595 case MEM_REF:
17596 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17598 have_address = 1;
17599 goto do_plus;
17601 /* Fallthru. */
17602 case INDIRECT_REF:
17603 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17604 have_address = 1;
17605 break;
17607 case TARGET_MEM_REF:
17608 case SSA_NAME:
17609 case DEBUG_EXPR_DECL:
17610 return NULL;
17612 case COMPOUND_EXPR:
17613 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17614 context);
17616 CASE_CONVERT:
17617 case VIEW_CONVERT_EXPR:
17618 case SAVE_EXPR:
17619 case MODIFY_EXPR:
17620 case NON_LVALUE_EXPR:
17621 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17622 context);
17624 case COMPONENT_REF:
17625 case BIT_FIELD_REF:
17626 case ARRAY_REF:
17627 case ARRAY_RANGE_REF:
17628 case REALPART_EXPR:
17629 case IMAGPART_EXPR:
17631 tree obj, offset;
17632 HOST_WIDE_INT bitsize, bitpos, bytepos;
17633 machine_mode mode;
17634 int unsignedp, reversep, volatilep = 0;
17636 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17637 &unsignedp, &reversep, &volatilep);
17639 gcc_assert (obj != loc);
17641 list_ret = loc_list_from_tree_1 (obj,
17642 want_address == 2
17643 && !bitpos && !offset ? 2 : 1,
17644 context);
17645 /* TODO: We can extract value of the small expression via shifting even
17646 for nonzero bitpos. */
17647 if (list_ret == 0)
17648 return 0;
17649 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17651 expansion_failed (loc, NULL_RTX,
17652 "bitfield access");
17653 return 0;
17656 if (offset != NULL_TREE)
17658 /* Variable offset. */
17659 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17660 if (list_ret1 == 0)
17661 return 0;
17662 add_loc_list (&list_ret, list_ret1);
17663 if (!list_ret)
17664 return 0;
17665 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17668 bytepos = bitpos / BITS_PER_UNIT;
17669 if (bytepos > 0)
17670 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17671 else if (bytepos < 0)
17672 loc_list_plus_const (list_ret, bytepos);
17674 have_address = 1;
17675 break;
17678 case INTEGER_CST:
17679 if ((want_address || !tree_fits_shwi_p (loc))
17680 && (ret = cst_pool_loc_descr (loc)))
17681 have_address = 1;
17682 else if (want_address == 2
17683 && tree_fits_shwi_p (loc)
17684 && (ret = address_of_int_loc_descriptor
17685 (int_size_in_bytes (TREE_TYPE (loc)),
17686 tree_to_shwi (loc))))
17687 have_address = 1;
17688 else if (tree_fits_shwi_p (loc))
17689 ret = int_loc_descriptor (tree_to_shwi (loc));
17690 else if (tree_fits_uhwi_p (loc))
17691 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17692 else
17694 expansion_failed (loc, NULL_RTX,
17695 "Integer operand is not host integer");
17696 return 0;
17698 break;
17700 case CONSTRUCTOR:
17701 case REAL_CST:
17702 case STRING_CST:
17703 case COMPLEX_CST:
17704 if ((ret = cst_pool_loc_descr (loc)))
17705 have_address = 1;
17706 else if (TREE_CODE (loc) == CONSTRUCTOR)
17708 tree type = TREE_TYPE (loc);
17709 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17710 unsigned HOST_WIDE_INT offset = 0;
17711 unsigned HOST_WIDE_INT cnt;
17712 constructor_elt *ce;
17714 if (TREE_CODE (type) == RECORD_TYPE)
17716 /* This is very limited, but it's enough to output
17717 pointers to member functions, as long as the
17718 referenced function is defined in the current
17719 translation unit. */
17720 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17722 tree val = ce->value;
17724 tree field = ce->index;
17726 if (val)
17727 STRIP_NOPS (val);
17729 if (!field || DECL_BIT_FIELD (field))
17731 expansion_failed (loc, NULL_RTX,
17732 "bitfield in record type constructor");
17733 size = offset = (unsigned HOST_WIDE_INT)-1;
17734 ret = NULL;
17735 break;
17738 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17739 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17740 gcc_assert (pos + fieldsize <= size);
17741 if (pos < offset)
17743 expansion_failed (loc, NULL_RTX,
17744 "out-of-order fields in record constructor");
17745 size = offset = (unsigned HOST_WIDE_INT)-1;
17746 ret = NULL;
17747 break;
17749 if (pos > offset)
17751 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17752 add_loc_descr (&ret, ret1);
17753 offset = pos;
17755 if (val && fieldsize != 0)
17757 ret1 = loc_descriptor_from_tree (val, want_address, context);
17758 if (!ret1)
17760 expansion_failed (loc, NULL_RTX,
17761 "unsupported expression in field");
17762 size = offset = (unsigned HOST_WIDE_INT)-1;
17763 ret = NULL;
17764 break;
17766 add_loc_descr (&ret, ret1);
17768 if (fieldsize)
17770 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17771 add_loc_descr (&ret, ret1);
17772 offset = pos + fieldsize;
17776 if (offset != size)
17778 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17779 add_loc_descr (&ret, ret1);
17780 offset = size;
17783 have_address = !!want_address;
17785 else
17786 expansion_failed (loc, NULL_RTX,
17787 "constructor of non-record type");
17789 else
17790 /* We can construct small constants here using int_loc_descriptor. */
17791 expansion_failed (loc, NULL_RTX,
17792 "constructor or constant not in constant pool");
17793 break;
17795 case TRUTH_AND_EXPR:
17796 case TRUTH_ANDIF_EXPR:
17797 case BIT_AND_EXPR:
17798 op = DW_OP_and;
17799 goto do_binop;
17801 case TRUTH_XOR_EXPR:
17802 case BIT_XOR_EXPR:
17803 op = DW_OP_xor;
17804 goto do_binop;
17806 case TRUTH_OR_EXPR:
17807 case TRUTH_ORIF_EXPR:
17808 case BIT_IOR_EXPR:
17809 op = DW_OP_or;
17810 goto do_binop;
17812 case FLOOR_DIV_EXPR:
17813 case CEIL_DIV_EXPR:
17814 case ROUND_DIV_EXPR:
17815 case TRUNC_DIV_EXPR:
17816 case EXACT_DIV_EXPR:
17817 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17818 return 0;
17819 op = DW_OP_div;
17820 goto do_binop;
17822 case MINUS_EXPR:
17823 op = DW_OP_minus;
17824 goto do_binop;
17826 case FLOOR_MOD_EXPR:
17827 case CEIL_MOD_EXPR:
17828 case ROUND_MOD_EXPR:
17829 case TRUNC_MOD_EXPR:
17830 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17832 op = DW_OP_mod;
17833 goto do_binop;
17835 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17836 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17837 if (list_ret == 0 || list_ret1 == 0)
17838 return 0;
17840 add_loc_list (&list_ret, list_ret1);
17841 if (list_ret == 0)
17842 return 0;
17843 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17844 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17845 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17846 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17847 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17848 break;
17850 case MULT_EXPR:
17851 op = DW_OP_mul;
17852 goto do_binop;
17854 case LSHIFT_EXPR:
17855 op = DW_OP_shl;
17856 goto do_binop;
17858 case RSHIFT_EXPR:
17859 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17860 goto do_binop;
17862 case POINTER_PLUS_EXPR:
17863 case PLUS_EXPR:
17864 do_plus:
17865 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17867 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17868 smarter to encode their opposite. The DW_OP_plus_uconst operation
17869 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17870 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17871 bytes, Y being the size of the operation that pushes the opposite
17872 of the addend. So let's choose the smallest representation. */
17873 const tree tree_addend = TREE_OPERAND (loc, 1);
17874 offset_int wi_addend;
17875 HOST_WIDE_INT shwi_addend;
17876 dw_loc_descr_ref loc_naddend;
17878 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17879 if (list_ret == 0)
17880 return 0;
17882 /* Try to get the literal to push. It is the opposite of the addend,
17883 so as we rely on wrapping during DWARF evaluation, first decode
17884 the literal as a "DWARF-sized" signed number. */
17885 wi_addend = wi::to_offset (tree_addend);
17886 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17887 shwi_addend = wi_addend.to_shwi ();
17888 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17889 ? int_loc_descriptor (-shwi_addend)
17890 : NULL;
17892 if (loc_naddend != NULL
17893 && ((unsigned) size_of_uleb128 (shwi_addend)
17894 > size_of_loc_descr (loc_naddend)))
17896 add_loc_descr_to_each (list_ret, loc_naddend);
17897 add_loc_descr_to_each (list_ret,
17898 new_loc_descr (DW_OP_minus, 0, 0));
17900 else
17902 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17904 loc_naddend = loc_cur;
17905 loc_cur = loc_cur->dw_loc_next;
17906 ggc_free (loc_naddend);
17908 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17910 break;
17913 op = DW_OP_plus;
17914 goto do_binop;
17916 case LE_EXPR:
17917 op = DW_OP_le;
17918 goto do_comp_binop;
17920 case GE_EXPR:
17921 op = DW_OP_ge;
17922 goto do_comp_binop;
17924 case LT_EXPR:
17925 op = DW_OP_lt;
17926 goto do_comp_binop;
17928 case GT_EXPR:
17929 op = DW_OP_gt;
17930 goto do_comp_binop;
17932 do_comp_binop:
17933 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17935 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17936 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17937 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17938 TREE_CODE (loc));
17939 break;
17941 else
17942 goto do_binop;
17944 case EQ_EXPR:
17945 op = DW_OP_eq;
17946 goto do_binop;
17948 case NE_EXPR:
17949 op = DW_OP_ne;
17950 goto do_binop;
17952 do_binop:
17953 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17954 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17955 if (list_ret == 0 || list_ret1 == 0)
17956 return 0;
17958 add_loc_list (&list_ret, list_ret1);
17959 if (list_ret == 0)
17960 return 0;
17961 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17962 break;
17964 case TRUTH_NOT_EXPR:
17965 case BIT_NOT_EXPR:
17966 op = DW_OP_not;
17967 goto do_unop;
17969 case ABS_EXPR:
17970 op = DW_OP_abs;
17971 goto do_unop;
17973 case NEGATE_EXPR:
17974 op = DW_OP_neg;
17975 goto do_unop;
17977 do_unop:
17978 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17979 if (list_ret == 0)
17980 return 0;
17982 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17983 break;
17985 case MIN_EXPR:
17986 case MAX_EXPR:
17988 const enum tree_code code =
17989 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17991 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17992 build2 (code, integer_type_node,
17993 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17994 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17997 /* fall through */
17999 case COND_EXPR:
18001 dw_loc_descr_ref lhs
18002 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18003 dw_loc_list_ref rhs
18004 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18005 dw_loc_descr_ref bra_node, jump_node, tmp;
18007 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18008 if (list_ret == 0 || lhs == 0 || rhs == 0)
18009 return 0;
18011 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18012 add_loc_descr_to_each (list_ret, bra_node);
18014 add_loc_list (&list_ret, rhs);
18015 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18016 add_loc_descr_to_each (list_ret, jump_node);
18018 add_loc_descr_to_each (list_ret, lhs);
18019 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18020 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18022 /* ??? Need a node to point the skip at. Use a nop. */
18023 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18024 add_loc_descr_to_each (list_ret, tmp);
18025 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18026 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18028 break;
18030 case FIX_TRUNC_EXPR:
18031 return 0;
18033 default:
18034 /* Leave front-end specific codes as simply unknown. This comes
18035 up, for instance, with the C STMT_EXPR. */
18036 if ((unsigned int) TREE_CODE (loc)
18037 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18039 expansion_failed (loc, NULL_RTX,
18040 "language specific tree node");
18041 return 0;
18044 /* Otherwise this is a generic code; we should just lists all of
18045 these explicitly. We forgot one. */
18046 if (flag_checking)
18047 gcc_unreachable ();
18049 /* In a release build, we want to degrade gracefully: better to
18050 generate incomplete debugging information than to crash. */
18051 return NULL;
18054 if (!ret && !list_ret)
18055 return 0;
18057 if (want_address == 2 && !have_address
18058 && (dwarf_version >= 4 || !dwarf_strict))
18060 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18062 expansion_failed (loc, NULL_RTX,
18063 "DWARF address size mismatch");
18064 return 0;
18066 if (ret)
18067 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18068 else
18069 add_loc_descr_to_each (list_ret,
18070 new_loc_descr (DW_OP_stack_value, 0, 0));
18071 have_address = 1;
18073 /* Show if we can't fill the request for an address. */
18074 if (want_address && !have_address)
18076 expansion_failed (loc, NULL_RTX,
18077 "Want address and only have value");
18078 return 0;
18081 gcc_assert (!ret || !list_ret);
18083 /* If we've got an address and don't want one, dereference. */
18084 if (!want_address && have_address)
18086 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18088 if (size > DWARF2_ADDR_SIZE || size == -1)
18090 expansion_failed (loc, NULL_RTX,
18091 "DWARF address size mismatch");
18092 return 0;
18094 else if (size == DWARF2_ADDR_SIZE)
18095 op = DW_OP_deref;
18096 else
18097 op = DW_OP_deref_size;
18099 if (ret)
18100 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18101 else
18102 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18104 if (ret)
18105 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18107 return list_ret;
18110 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18111 expressions. */
18113 static dw_loc_list_ref
18114 loc_list_from_tree (tree loc, int want_address,
18115 struct loc_descr_context *context)
18117 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18119 for (dw_loc_list_ref loc_cur = result;
18120 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18121 loc_descr_without_nops (loc_cur->expr);
18122 return result;
18125 /* Same as above but return only single location expression. */
18126 static dw_loc_descr_ref
18127 loc_descriptor_from_tree (tree loc, int want_address,
18128 struct loc_descr_context *context)
18130 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18131 if (!ret)
18132 return NULL;
18133 if (ret->dw_loc_next)
18135 expansion_failed (loc, NULL_RTX,
18136 "Location list where only loc descriptor needed");
18137 return NULL;
18139 return ret->expr;
18142 /* Given a value, round it up to the lowest multiple of `boundary'
18143 which is not less than the value itself. */
18145 static inline HOST_WIDE_INT
18146 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18148 return (((value + boundary - 1) / boundary) * boundary);
18151 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18152 pointer to the declared type for the relevant field variable, or return
18153 `integer_type_node' if the given node turns out to be an
18154 ERROR_MARK node. */
18156 static inline tree
18157 field_type (const_tree decl)
18159 tree type;
18161 if (TREE_CODE (decl) == ERROR_MARK)
18162 return integer_type_node;
18164 type = DECL_BIT_FIELD_TYPE (decl);
18165 if (type == NULL_TREE)
18166 type = TREE_TYPE (decl);
18168 return type;
18171 /* Given a pointer to a tree node, return the alignment in bits for
18172 it, or else return BITS_PER_WORD if the node actually turns out to
18173 be an ERROR_MARK node. */
18175 static inline unsigned
18176 simple_type_align_in_bits (const_tree type)
18178 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18181 static inline unsigned
18182 simple_decl_align_in_bits (const_tree decl)
18184 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18187 /* Return the result of rounding T up to ALIGN. */
18189 static inline offset_int
18190 round_up_to_align (const offset_int &t, unsigned int align)
18192 return wi::udiv_trunc (t + align - 1, align) * align;
18195 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18196 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18197 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18198 if we fail to return the size in one of these two forms. */
18200 static dw_loc_descr_ref
18201 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18203 tree tree_size;
18204 struct loc_descr_context ctx;
18206 /* Return a constant integer in priority, if possible. */
18207 *cst_size = int_size_in_bytes (type);
18208 if (*cst_size != -1)
18209 return NULL;
18211 ctx.context_type = const_cast<tree> (type);
18212 ctx.base_decl = NULL_TREE;
18213 ctx.dpi = NULL;
18214 ctx.placeholder_arg = false;
18215 ctx.placeholder_seen = false;
18217 type = TYPE_MAIN_VARIANT (type);
18218 tree_size = TYPE_SIZE_UNIT (type);
18219 return ((tree_size != NULL_TREE)
18220 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18221 : NULL);
18224 /* Helper structure for RECORD_TYPE processing. */
18225 struct vlr_context
18227 /* Root RECORD_TYPE. It is needed to generate data member location
18228 descriptions in variable-length records (VLR), but also to cope with
18229 variants, which are composed of nested structures multiplexed with
18230 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18231 function processing a FIELD_DECL, it is required to be non null. */
18232 tree struct_type;
18233 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18234 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18235 this variant part as part of the root record (in storage units). For
18236 regular records, it must be NULL_TREE. */
18237 tree variant_part_offset;
18240 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18241 addressed byte of the "containing object" for the given FIELD_DECL. If
18242 possible, return a native constant through CST_OFFSET (in which case NULL is
18243 returned); otherwise return a DWARF expression that computes the offset.
18245 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18246 that offset is, either because the argument turns out to be a pointer to an
18247 ERROR_MARK node, or because the offset expression is too complex for us.
18249 CTX is required: see the comment for VLR_CONTEXT. */
18251 static dw_loc_descr_ref
18252 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18253 HOST_WIDE_INT *cst_offset)
18255 tree tree_result;
18256 dw_loc_list_ref loc_result;
18258 *cst_offset = 0;
18260 if (TREE_CODE (decl) == ERROR_MARK)
18261 return NULL;
18262 else
18263 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18265 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18266 case. */
18267 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18268 return NULL;
18270 #ifdef PCC_BITFIELD_TYPE_MATTERS
18271 /* We used to handle only constant offsets in all cases. Now, we handle
18272 properly dynamic byte offsets only when PCC bitfield type doesn't
18273 matter. */
18274 if (PCC_BITFIELD_TYPE_MATTERS
18275 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18277 offset_int object_offset_in_bits;
18278 offset_int object_offset_in_bytes;
18279 offset_int bitpos_int;
18280 tree type;
18281 tree field_size_tree;
18282 offset_int deepest_bitpos;
18283 offset_int field_size_in_bits;
18284 unsigned int type_align_in_bits;
18285 unsigned int decl_align_in_bits;
18286 offset_int type_size_in_bits;
18288 bitpos_int = wi::to_offset (bit_position (decl));
18289 type = field_type (decl);
18290 type_size_in_bits = offset_int_type_size_in_bits (type);
18291 type_align_in_bits = simple_type_align_in_bits (type);
18293 field_size_tree = DECL_SIZE (decl);
18295 /* The size could be unspecified if there was an error, or for
18296 a flexible array member. */
18297 if (!field_size_tree)
18298 field_size_tree = bitsize_zero_node;
18300 /* If the size of the field is not constant, use the type size. */
18301 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18302 field_size_in_bits = wi::to_offset (field_size_tree);
18303 else
18304 field_size_in_bits = type_size_in_bits;
18306 decl_align_in_bits = simple_decl_align_in_bits (decl);
18308 /* The GCC front-end doesn't make any attempt to keep track of the
18309 starting bit offset (relative to the start of the containing
18310 structure type) of the hypothetical "containing object" for a
18311 bit-field. Thus, when computing the byte offset value for the
18312 start of the "containing object" of a bit-field, we must deduce
18313 this information on our own. This can be rather tricky to do in
18314 some cases. For example, handling the following structure type
18315 definition when compiling for an i386/i486 target (which only
18316 aligns long long's to 32-bit boundaries) can be very tricky:
18318 struct S { int field1; long long field2:31; };
18320 Fortunately, there is a simple rule-of-thumb which can be used
18321 in such cases. When compiling for an i386/i486, GCC will
18322 allocate 8 bytes for the structure shown above. It decides to
18323 do this based upon one simple rule for bit-field allocation.
18324 GCC allocates each "containing object" for each bit-field at
18325 the first (i.e. lowest addressed) legitimate alignment boundary
18326 (based upon the required minimum alignment for the declared
18327 type of the field) which it can possibly use, subject to the
18328 condition that there is still enough available space remaining
18329 in the containing object (when allocated at the selected point)
18330 to fully accommodate all of the bits of the bit-field itself.
18332 This simple rule makes it obvious why GCC allocates 8 bytes for
18333 each object of the structure type shown above. When looking
18334 for a place to allocate the "containing object" for `field2',
18335 the compiler simply tries to allocate a 64-bit "containing
18336 object" at each successive 32-bit boundary (starting at zero)
18337 until it finds a place to allocate that 64- bit field such that
18338 at least 31 contiguous (and previously unallocated) bits remain
18339 within that selected 64 bit field. (As it turns out, for the
18340 example above, the compiler finds it is OK to allocate the
18341 "containing object" 64-bit field at bit-offset zero within the
18342 structure type.)
18344 Here we attempt to work backwards from the limited set of facts
18345 we're given, and we try to deduce from those facts, where GCC
18346 must have believed that the containing object started (within
18347 the structure type). The value we deduce is then used (by the
18348 callers of this routine) to generate DW_AT_location and
18349 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18350 the case of DW_AT_location, regular fields as well). */
18352 /* Figure out the bit-distance from the start of the structure to
18353 the "deepest" bit of the bit-field. */
18354 deepest_bitpos = bitpos_int + field_size_in_bits;
18356 /* This is the tricky part. Use some fancy footwork to deduce
18357 where the lowest addressed bit of the containing object must
18358 be. */
18359 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18361 /* Round up to type_align by default. This works best for
18362 bitfields. */
18363 object_offset_in_bits
18364 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18366 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18368 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18370 /* Round up to decl_align instead. */
18371 object_offset_in_bits
18372 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18375 object_offset_in_bytes
18376 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18377 if (ctx->variant_part_offset == NULL_TREE)
18379 *cst_offset = object_offset_in_bytes.to_shwi ();
18380 return NULL;
18382 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18384 else
18385 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18386 tree_result = byte_position (decl);
18388 if (ctx->variant_part_offset != NULL_TREE)
18389 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18390 ctx->variant_part_offset, tree_result);
18392 /* If the byte offset is a constant, it's simplier to handle a native
18393 constant rather than a DWARF expression. */
18394 if (TREE_CODE (tree_result) == INTEGER_CST)
18396 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18397 return NULL;
18399 struct loc_descr_context loc_ctx = {
18400 ctx->struct_type, /* context_type */
18401 NULL_TREE, /* base_decl */
18402 NULL, /* dpi */
18403 false, /* placeholder_arg */
18404 false /* placeholder_seen */
18406 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18408 /* We want a DWARF expression: abort if we only have a location list with
18409 multiple elements. */
18410 if (!loc_result || !single_element_loc_list_p (loc_result))
18411 return NULL;
18412 else
18413 return loc_result->expr;
18416 /* The following routines define various Dwarf attributes and any data
18417 associated with them. */
18419 /* Add a location description attribute value to a DIE.
18421 This emits location attributes suitable for whole variables and
18422 whole parameters. Note that the location attributes for struct fields are
18423 generated by the routine `data_member_location_attribute' below. */
18425 static inline void
18426 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18427 dw_loc_list_ref descr)
18429 if (descr == 0)
18430 return;
18431 if (single_element_loc_list_p (descr))
18432 add_AT_loc (die, attr_kind, descr->expr);
18433 else
18434 add_AT_loc_list (die, attr_kind, descr);
18437 /* Add DW_AT_accessibility attribute to DIE if needed. */
18439 static void
18440 add_accessibility_attribute (dw_die_ref die, tree decl)
18442 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18443 children, otherwise the default is DW_ACCESS_public. In DWARF2
18444 the default has always been DW_ACCESS_public. */
18445 if (TREE_PROTECTED (decl))
18446 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18447 else if (TREE_PRIVATE (decl))
18449 if (dwarf_version == 2
18450 || die->die_parent == NULL
18451 || die->die_parent->die_tag != DW_TAG_class_type)
18452 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18454 else if (dwarf_version > 2
18455 && die->die_parent
18456 && die->die_parent->die_tag == DW_TAG_class_type)
18457 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18460 /* Attach the specialized form of location attribute used for data members of
18461 struct and union types. In the special case of a FIELD_DECL node which
18462 represents a bit-field, the "offset" part of this special location
18463 descriptor must indicate the distance in bytes from the lowest-addressed
18464 byte of the containing struct or union type to the lowest-addressed byte of
18465 the "containing object" for the bit-field. (See the `field_byte_offset'
18466 function above).
18468 For any given bit-field, the "containing object" is a hypothetical object
18469 (of some integral or enum type) within which the given bit-field lives. The
18470 type of this hypothetical "containing object" is always the same as the
18471 declared type of the individual bit-field itself (for GCC anyway... the
18472 DWARF spec doesn't actually mandate this). Note that it is the size (in
18473 bytes) of the hypothetical "containing object" which will be given in the
18474 DW_AT_byte_size attribute for this bit-field. (See the
18475 `byte_size_attribute' function below.) It is also used when calculating the
18476 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18477 function below.)
18479 CTX is required: see the comment for VLR_CONTEXT. */
18481 static void
18482 add_data_member_location_attribute (dw_die_ref die,
18483 tree decl,
18484 struct vlr_context *ctx)
18486 HOST_WIDE_INT offset;
18487 dw_loc_descr_ref loc_descr = 0;
18489 if (TREE_CODE (decl) == TREE_BINFO)
18491 /* We're working on the TAG_inheritance for a base class. */
18492 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18494 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18495 aren't at a fixed offset from all (sub)objects of the same
18496 type. We need to extract the appropriate offset from our
18497 vtable. The following dwarf expression means
18499 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18501 This is specific to the V3 ABI, of course. */
18503 dw_loc_descr_ref tmp;
18505 /* Make a copy of the object address. */
18506 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18507 add_loc_descr (&loc_descr, tmp);
18509 /* Extract the vtable address. */
18510 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18511 add_loc_descr (&loc_descr, tmp);
18513 /* Calculate the address of the offset. */
18514 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18515 gcc_assert (offset < 0);
18517 tmp = int_loc_descriptor (-offset);
18518 add_loc_descr (&loc_descr, tmp);
18519 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18520 add_loc_descr (&loc_descr, tmp);
18522 /* Extract the offset. */
18523 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18524 add_loc_descr (&loc_descr, tmp);
18526 /* Add it to the object address. */
18527 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18528 add_loc_descr (&loc_descr, tmp);
18530 else
18531 offset = tree_to_shwi (BINFO_OFFSET (decl));
18533 else
18535 loc_descr = field_byte_offset (decl, ctx, &offset);
18537 /* If loc_descr is available then we know the field offset is dynamic.
18538 However, GDB does not handle dynamic field offsets very well at the
18539 moment. */
18540 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18542 loc_descr = NULL;
18543 offset = 0;
18546 /* Data member location evalutation starts with the base address on the
18547 stack. Compute the field offset and add it to this base address. */
18548 else if (loc_descr != NULL)
18549 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18552 if (! loc_descr)
18554 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18555 e.g. GDB only added support to it in November 2016. For DWARF5
18556 we need newer debug info consumers anyway. We might change this
18557 to dwarf_version >= 4 once most consumers catched up. */
18558 if (dwarf_version >= 5
18559 && TREE_CODE (decl) == FIELD_DECL
18560 && DECL_BIT_FIELD_TYPE (decl))
18562 tree off = bit_position (decl);
18563 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18565 remove_AT (die, DW_AT_byte_size);
18566 remove_AT (die, DW_AT_bit_offset);
18567 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18568 return;
18571 if (dwarf_version > 2)
18573 /* Don't need to output a location expression, just the constant. */
18574 if (offset < 0)
18575 add_AT_int (die, DW_AT_data_member_location, offset);
18576 else
18577 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18578 return;
18580 else
18582 enum dwarf_location_atom op;
18584 /* The DWARF2 standard says that we should assume that the structure
18585 address is already on the stack, so we can specify a structure
18586 field address by using DW_OP_plus_uconst. */
18587 op = DW_OP_plus_uconst;
18588 loc_descr = new_loc_descr (op, offset, 0);
18592 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18595 /* Writes integer values to dw_vec_const array. */
18597 static void
18598 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18600 while (size != 0)
18602 *dest++ = val & 0xff;
18603 val >>= 8;
18604 --size;
18608 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18610 static HOST_WIDE_INT
18611 extract_int (const unsigned char *src, unsigned int size)
18613 HOST_WIDE_INT val = 0;
18615 src += size;
18616 while (size != 0)
18618 val <<= 8;
18619 val |= *--src & 0xff;
18620 --size;
18622 return val;
18625 /* Writes wide_int values to dw_vec_const array. */
18627 static void
18628 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18630 int i;
18632 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18634 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18635 return;
18638 /* We'd have to extend this code to support odd sizes. */
18639 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18641 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18643 if (WORDS_BIG_ENDIAN)
18644 for (i = n - 1; i >= 0; i--)
18646 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18647 dest += sizeof (HOST_WIDE_INT);
18649 else
18650 for (i = 0; i < n; i++)
18652 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18653 dest += sizeof (HOST_WIDE_INT);
18657 /* Writes floating point values to dw_vec_const array. */
18659 static void
18660 insert_float (const_rtx rtl, unsigned char *array)
18662 long val[4];
18663 int i;
18664 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18666 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18668 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18669 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18671 insert_int (val[i], 4, array);
18672 array += 4;
18676 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18677 does not have a "location" either in memory or in a register. These
18678 things can arise in GNU C when a constant is passed as an actual parameter
18679 to an inlined function. They can also arise in C++ where declared
18680 constants do not necessarily get memory "homes". */
18682 static bool
18683 add_const_value_attribute (dw_die_ref die, rtx rtl)
18685 switch (GET_CODE (rtl))
18687 case CONST_INT:
18689 HOST_WIDE_INT val = INTVAL (rtl);
18691 if (val < 0)
18692 add_AT_int (die, DW_AT_const_value, val);
18693 else
18694 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18696 return true;
18698 case CONST_WIDE_INT:
18700 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18701 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18702 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18703 wide_int w = wi::zext (w1, prec);
18704 add_AT_wide (die, DW_AT_const_value, w);
18706 return true;
18708 case CONST_DOUBLE:
18709 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18710 floating-point constant. A CONST_DOUBLE is used whenever the
18711 constant requires more than one word in order to be adequately
18712 represented. */
18713 if (TARGET_SUPPORTS_WIDE_INT == 0
18714 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18715 add_AT_double (die, DW_AT_const_value,
18716 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18717 else
18719 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18720 unsigned int length = GET_MODE_SIZE (mode);
18721 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18723 insert_float (rtl, array);
18724 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18726 return true;
18728 case CONST_VECTOR:
18730 machine_mode mode = GET_MODE (rtl);
18731 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18732 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18733 unsigned char *array
18734 = ggc_vec_alloc<unsigned char> (length * elt_size);
18735 unsigned int i;
18736 unsigned char *p;
18737 machine_mode imode = GET_MODE_INNER (mode);
18739 switch (GET_MODE_CLASS (mode))
18741 case MODE_VECTOR_INT:
18742 for (i = 0, p = array; i < length; i++, p += elt_size)
18744 rtx elt = CONST_VECTOR_ELT (rtl, i);
18745 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18747 break;
18749 case MODE_VECTOR_FLOAT:
18750 for (i = 0, p = array; i < length; i++, p += elt_size)
18752 rtx elt = CONST_VECTOR_ELT (rtl, i);
18753 insert_float (elt, p);
18755 break;
18757 default:
18758 gcc_unreachable ();
18761 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18763 return true;
18765 case CONST_STRING:
18766 if (dwarf_version >= 4 || !dwarf_strict)
18768 dw_loc_descr_ref loc_result;
18769 resolve_one_addr (&rtl);
18770 rtl_addr:
18771 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18772 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18773 add_AT_loc (die, DW_AT_location, loc_result);
18774 vec_safe_push (used_rtx_array, rtl);
18775 return true;
18777 return false;
18779 case CONST:
18780 if (CONSTANT_P (XEXP (rtl, 0)))
18781 return add_const_value_attribute (die, XEXP (rtl, 0));
18782 /* FALLTHROUGH */
18783 case SYMBOL_REF:
18784 if (!const_ok_for_output (rtl))
18785 return false;
18786 /* FALLTHROUGH */
18787 case LABEL_REF:
18788 if (dwarf_version >= 4 || !dwarf_strict)
18789 goto rtl_addr;
18790 return false;
18792 case PLUS:
18793 /* In cases where an inlined instance of an inline function is passed
18794 the address of an `auto' variable (which is local to the caller) we
18795 can get a situation where the DECL_RTL of the artificial local
18796 variable (for the inlining) which acts as a stand-in for the
18797 corresponding formal parameter (of the inline function) will look
18798 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18799 exactly a compile-time constant expression, but it isn't the address
18800 of the (artificial) local variable either. Rather, it represents the
18801 *value* which the artificial local variable always has during its
18802 lifetime. We currently have no way to represent such quasi-constant
18803 values in Dwarf, so for now we just punt and generate nothing. */
18804 return false;
18806 case HIGH:
18807 case CONST_FIXED:
18808 return false;
18810 case MEM:
18811 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18812 && MEM_READONLY_P (rtl)
18813 && GET_MODE (rtl) == BLKmode)
18815 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18816 return true;
18818 return false;
18820 default:
18821 /* No other kinds of rtx should be possible here. */
18822 gcc_unreachable ();
18824 return false;
18827 /* Determine whether the evaluation of EXPR references any variables
18828 or functions which aren't otherwise used (and therefore may not be
18829 output). */
18830 static tree
18831 reference_to_unused (tree * tp, int * walk_subtrees,
18832 void * data ATTRIBUTE_UNUSED)
18834 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18835 *walk_subtrees = 0;
18837 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18838 && ! TREE_ASM_WRITTEN (*tp))
18839 return *tp;
18840 /* ??? The C++ FE emits debug information for using decls, so
18841 putting gcc_unreachable here falls over. See PR31899. For now
18842 be conservative. */
18843 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18844 return *tp;
18845 else if (VAR_P (*tp))
18847 varpool_node *node = varpool_node::get (*tp);
18848 if (!node || !node->definition)
18849 return *tp;
18851 else if (TREE_CODE (*tp) == FUNCTION_DECL
18852 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18854 /* The call graph machinery must have finished analyzing,
18855 optimizing and gimplifying the CU by now.
18856 So if *TP has no call graph node associated
18857 to it, it means *TP will not be emitted. */
18858 if (!cgraph_node::get (*tp))
18859 return *tp;
18861 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18862 return *tp;
18864 return NULL_TREE;
18867 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18868 for use in a later add_const_value_attribute call. */
18870 static rtx
18871 rtl_for_decl_init (tree init, tree type)
18873 rtx rtl = NULL_RTX;
18875 STRIP_NOPS (init);
18877 /* If a variable is initialized with a string constant without embedded
18878 zeros, build CONST_STRING. */
18879 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18881 tree enttype = TREE_TYPE (type);
18882 tree domain = TYPE_DOMAIN (type);
18883 scalar_int_mode mode;
18885 if (is_int_mode (TYPE_MODE (enttype), &mode)
18886 && GET_MODE_SIZE (mode) == 1
18887 && domain
18888 && integer_zerop (TYPE_MIN_VALUE (domain))
18889 && compare_tree_int (TYPE_MAX_VALUE (domain),
18890 TREE_STRING_LENGTH (init) - 1) == 0
18891 && ((size_t) TREE_STRING_LENGTH (init)
18892 == strlen (TREE_STRING_POINTER (init)) + 1))
18894 rtl = gen_rtx_CONST_STRING (VOIDmode,
18895 ggc_strdup (TREE_STRING_POINTER (init)));
18896 rtl = gen_rtx_MEM (BLKmode, rtl);
18897 MEM_READONLY_P (rtl) = 1;
18900 /* Other aggregates, and complex values, could be represented using
18901 CONCAT: FIXME! */
18902 else if (AGGREGATE_TYPE_P (type)
18903 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18904 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18905 || TREE_CODE (type) == COMPLEX_TYPE)
18907 /* Vectors only work if their mode is supported by the target.
18908 FIXME: generic vectors ought to work too. */
18909 else if (TREE_CODE (type) == VECTOR_TYPE
18910 && !VECTOR_MODE_P (TYPE_MODE (type)))
18912 /* If the initializer is something that we know will expand into an
18913 immediate RTL constant, expand it now. We must be careful not to
18914 reference variables which won't be output. */
18915 else if (initializer_constant_valid_p (init, type)
18916 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18918 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18919 possible. */
18920 if (TREE_CODE (type) == VECTOR_TYPE)
18921 switch (TREE_CODE (init))
18923 case VECTOR_CST:
18924 break;
18925 case CONSTRUCTOR:
18926 if (TREE_CONSTANT (init))
18928 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18929 bool constant_p = true;
18930 tree value;
18931 unsigned HOST_WIDE_INT ix;
18933 /* Even when ctor is constant, it might contain non-*_CST
18934 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18935 belong into VECTOR_CST nodes. */
18936 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18937 if (!CONSTANT_CLASS_P (value))
18939 constant_p = false;
18940 break;
18943 if (constant_p)
18945 init = build_vector_from_ctor (type, elts);
18946 break;
18949 /* FALLTHRU */
18951 default:
18952 return NULL;
18955 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18957 /* If expand_expr returns a MEM, it wasn't immediate. */
18958 gcc_assert (!rtl || !MEM_P (rtl));
18961 return rtl;
18964 /* Generate RTL for the variable DECL to represent its location. */
18966 static rtx
18967 rtl_for_decl_location (tree decl)
18969 rtx rtl;
18971 /* Here we have to decide where we are going to say the parameter "lives"
18972 (as far as the debugger is concerned). We only have a couple of
18973 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18975 DECL_RTL normally indicates where the parameter lives during most of the
18976 activation of the function. If optimization is enabled however, this
18977 could be either NULL or else a pseudo-reg. Both of those cases indicate
18978 that the parameter doesn't really live anywhere (as far as the code
18979 generation parts of GCC are concerned) during most of the function's
18980 activation. That will happen (for example) if the parameter is never
18981 referenced within the function.
18983 We could just generate a location descriptor here for all non-NULL
18984 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18985 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18986 where DECL_RTL is NULL or is a pseudo-reg.
18988 Note however that we can only get away with using DECL_INCOMING_RTL as
18989 a backup substitute for DECL_RTL in certain limited cases. In cases
18990 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18991 we can be sure that the parameter was passed using the same type as it is
18992 declared to have within the function, and that its DECL_INCOMING_RTL
18993 points us to a place where a value of that type is passed.
18995 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18996 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18997 because in these cases DECL_INCOMING_RTL points us to a value of some
18998 type which is *different* from the type of the parameter itself. Thus,
18999 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19000 such cases, the debugger would end up (for example) trying to fetch a
19001 `float' from a place which actually contains the first part of a
19002 `double'. That would lead to really incorrect and confusing
19003 output at debug-time.
19005 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19006 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19007 are a couple of exceptions however. On little-endian machines we can
19008 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19009 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19010 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19011 when (on a little-endian machine) a non-prototyped function has a
19012 parameter declared to be of type `short' or `char'. In such cases,
19013 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19014 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19015 passed `int' value. If the debugger then uses that address to fetch
19016 a `short' or a `char' (on a little-endian machine) the result will be
19017 the correct data, so we allow for such exceptional cases below.
19019 Note that our goal here is to describe the place where the given formal
19020 parameter lives during most of the function's activation (i.e. between the
19021 end of the prologue and the start of the epilogue). We'll do that as best
19022 as we can. Note however that if the given formal parameter is modified
19023 sometime during the execution of the function, then a stack backtrace (at
19024 debug-time) will show the function as having been called with the *new*
19025 value rather than the value which was originally passed in. This happens
19026 rarely enough that it is not a major problem, but it *is* a problem, and
19027 I'd like to fix it.
19029 A future version of dwarf2out.c may generate two additional attributes for
19030 any given DW_TAG_formal_parameter DIE which will describe the "passed
19031 type" and the "passed location" for the given formal parameter in addition
19032 to the attributes we now generate to indicate the "declared type" and the
19033 "active location" for each parameter. This additional set of attributes
19034 could be used by debuggers for stack backtraces. Separately, note that
19035 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19036 This happens (for example) for inlined-instances of inline function formal
19037 parameters which are never referenced. This really shouldn't be
19038 happening. All PARM_DECL nodes should get valid non-NULL
19039 DECL_INCOMING_RTL values. FIXME. */
19041 /* Use DECL_RTL as the "location" unless we find something better. */
19042 rtl = DECL_RTL_IF_SET (decl);
19044 /* When generating abstract instances, ignore everything except
19045 constants, symbols living in memory, and symbols living in
19046 fixed registers. */
19047 if (! reload_completed)
19049 if (rtl
19050 && (CONSTANT_P (rtl)
19051 || (MEM_P (rtl)
19052 && CONSTANT_P (XEXP (rtl, 0)))
19053 || (REG_P (rtl)
19054 && VAR_P (decl)
19055 && TREE_STATIC (decl))))
19057 rtl = targetm.delegitimize_address (rtl);
19058 return rtl;
19060 rtl = NULL_RTX;
19062 else if (TREE_CODE (decl) == PARM_DECL)
19064 if (rtl == NULL_RTX
19065 || is_pseudo_reg (rtl)
19066 || (MEM_P (rtl)
19067 && is_pseudo_reg (XEXP (rtl, 0))
19068 && DECL_INCOMING_RTL (decl)
19069 && MEM_P (DECL_INCOMING_RTL (decl))
19070 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19072 tree declared_type = TREE_TYPE (decl);
19073 tree passed_type = DECL_ARG_TYPE (decl);
19074 machine_mode dmode = TYPE_MODE (declared_type);
19075 machine_mode pmode = TYPE_MODE (passed_type);
19077 /* This decl represents a formal parameter which was optimized out.
19078 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19079 all cases where (rtl == NULL_RTX) just below. */
19080 if (dmode == pmode)
19081 rtl = DECL_INCOMING_RTL (decl);
19082 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19083 && SCALAR_INT_MODE_P (dmode)
19084 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19085 && DECL_INCOMING_RTL (decl))
19087 rtx inc = DECL_INCOMING_RTL (decl);
19088 if (REG_P (inc))
19089 rtl = inc;
19090 else if (MEM_P (inc))
19092 if (BYTES_BIG_ENDIAN)
19093 rtl = adjust_address_nv (inc, dmode,
19094 GET_MODE_SIZE (pmode)
19095 - GET_MODE_SIZE (dmode));
19096 else
19097 rtl = inc;
19102 /* If the parm was passed in registers, but lives on the stack, then
19103 make a big endian correction if the mode of the type of the
19104 parameter is not the same as the mode of the rtl. */
19105 /* ??? This is the same series of checks that are made in dbxout.c before
19106 we reach the big endian correction code there. It isn't clear if all
19107 of these checks are necessary here, but keeping them all is the safe
19108 thing to do. */
19109 else if (MEM_P (rtl)
19110 && XEXP (rtl, 0) != const0_rtx
19111 && ! CONSTANT_P (XEXP (rtl, 0))
19112 /* Not passed in memory. */
19113 && !MEM_P (DECL_INCOMING_RTL (decl))
19114 /* Not passed by invisible reference. */
19115 && (!REG_P (XEXP (rtl, 0))
19116 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19117 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19118 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19119 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19120 #endif
19122 /* Big endian correction check. */
19123 && BYTES_BIG_ENDIAN
19124 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19125 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19126 < UNITS_PER_WORD))
19128 machine_mode addr_mode = get_address_mode (rtl);
19129 int offset = (UNITS_PER_WORD
19130 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19132 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19133 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19136 else if (VAR_P (decl)
19137 && rtl
19138 && MEM_P (rtl)
19139 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19141 machine_mode addr_mode = get_address_mode (rtl);
19142 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19143 GET_MODE (rtl));
19145 /* If a variable is declared "register" yet is smaller than
19146 a register, then if we store the variable to memory, it
19147 looks like we're storing a register-sized value, when in
19148 fact we are not. We need to adjust the offset of the
19149 storage location to reflect the actual value's bytes,
19150 else gdb will not be able to display it. */
19151 if (offset != 0)
19152 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19153 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19156 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19157 and will have been substituted directly into all expressions that use it.
19158 C does not have such a concept, but C++ and other languages do. */
19159 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19160 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19162 if (rtl)
19163 rtl = targetm.delegitimize_address (rtl);
19165 /* If we don't look past the constant pool, we risk emitting a
19166 reference to a constant pool entry that isn't referenced from
19167 code, and thus is not emitted. */
19168 if (rtl)
19169 rtl = avoid_constant_pool_reference (rtl);
19171 /* Try harder to get a rtl. If this symbol ends up not being emitted
19172 in the current CU, resolve_addr will remove the expression referencing
19173 it. */
19174 if (rtl == NULL_RTX
19175 && VAR_P (decl)
19176 && !DECL_EXTERNAL (decl)
19177 && TREE_STATIC (decl)
19178 && DECL_NAME (decl)
19179 && !DECL_HARD_REGISTER (decl)
19180 && DECL_MODE (decl) != VOIDmode)
19182 rtl = make_decl_rtl_for_debug (decl);
19183 if (!MEM_P (rtl)
19184 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19185 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19186 rtl = NULL_RTX;
19189 return rtl;
19192 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19193 returned. If so, the decl for the COMMON block is returned, and the
19194 value is the offset into the common block for the symbol. */
19196 static tree
19197 fortran_common (tree decl, HOST_WIDE_INT *value)
19199 tree val_expr, cvar;
19200 machine_mode mode;
19201 HOST_WIDE_INT bitsize, bitpos;
19202 tree offset;
19203 int unsignedp, reversep, volatilep = 0;
19205 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19206 it does not have a value (the offset into the common area), or if it
19207 is thread local (as opposed to global) then it isn't common, and shouldn't
19208 be handled as such. */
19209 if (!VAR_P (decl)
19210 || !TREE_STATIC (decl)
19211 || !DECL_HAS_VALUE_EXPR_P (decl)
19212 || !is_fortran ())
19213 return NULL_TREE;
19215 val_expr = DECL_VALUE_EXPR (decl);
19216 if (TREE_CODE (val_expr) != COMPONENT_REF)
19217 return NULL_TREE;
19219 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19220 &unsignedp, &reversep, &volatilep);
19222 if (cvar == NULL_TREE
19223 || !VAR_P (cvar)
19224 || DECL_ARTIFICIAL (cvar)
19225 || !TREE_PUBLIC (cvar))
19226 return NULL_TREE;
19228 *value = 0;
19229 if (offset != NULL)
19231 if (!tree_fits_shwi_p (offset))
19232 return NULL_TREE;
19233 *value = tree_to_shwi (offset);
19235 if (bitpos != 0)
19236 *value += bitpos / BITS_PER_UNIT;
19238 return cvar;
19241 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19242 data attribute for a variable or a parameter. We generate the
19243 DW_AT_const_value attribute only in those cases where the given variable
19244 or parameter does not have a true "location" either in memory or in a
19245 register. This can happen (for example) when a constant is passed as an
19246 actual argument in a call to an inline function. (It's possible that
19247 these things can crop up in other ways also.) Note that one type of
19248 constant value which can be passed into an inlined function is a constant
19249 pointer. This can happen for example if an actual argument in an inlined
19250 function call evaluates to a compile-time constant address.
19252 CACHE_P is true if it is worth caching the location list for DECL,
19253 so that future calls can reuse it rather than regenerate it from scratch.
19254 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19255 since we will need to refer to them each time the function is inlined. */
19257 static bool
19258 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19260 rtx rtl;
19261 dw_loc_list_ref list;
19262 var_loc_list *loc_list;
19263 cached_dw_loc_list *cache;
19265 if (early_dwarf)
19266 return false;
19268 if (TREE_CODE (decl) == ERROR_MARK)
19269 return false;
19271 if (get_AT (die, DW_AT_location)
19272 || get_AT (die, DW_AT_const_value))
19273 return true;
19275 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19276 || TREE_CODE (decl) == RESULT_DECL);
19278 /* Try to get some constant RTL for this decl, and use that as the value of
19279 the location. */
19281 rtl = rtl_for_decl_location (decl);
19282 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19283 && add_const_value_attribute (die, rtl))
19284 return true;
19286 /* See if we have single element location list that is equivalent to
19287 a constant value. That way we are better to use add_const_value_attribute
19288 rather than expanding constant value equivalent. */
19289 loc_list = lookup_decl_loc (decl);
19290 if (loc_list
19291 && loc_list->first
19292 && loc_list->first->next == NULL
19293 && NOTE_P (loc_list->first->loc)
19294 && NOTE_VAR_LOCATION (loc_list->first->loc)
19295 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19297 struct var_loc_node *node;
19299 node = loc_list->first;
19300 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19301 if (GET_CODE (rtl) == EXPR_LIST)
19302 rtl = XEXP (rtl, 0);
19303 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19304 && add_const_value_attribute (die, rtl))
19305 return true;
19307 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19308 list several times. See if we've already cached the contents. */
19309 list = NULL;
19310 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19311 cache_p = false;
19312 if (cache_p)
19314 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19315 if (cache)
19316 list = cache->loc_list;
19318 if (list == NULL)
19320 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19321 NULL);
19322 /* It is usually worth caching this result if the decl is from
19323 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19324 if (cache_p && list && list->dw_loc_next)
19326 cached_dw_loc_list **slot
19327 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19328 DECL_UID (decl),
19329 INSERT);
19330 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19331 cache->decl_id = DECL_UID (decl);
19332 cache->loc_list = list;
19333 *slot = cache;
19336 if (list)
19338 add_AT_location_description (die, DW_AT_location, list);
19339 return true;
19341 /* None of that worked, so it must not really have a location;
19342 try adding a constant value attribute from the DECL_INITIAL. */
19343 return tree_add_const_value_attribute_for_decl (die, decl);
19346 /* Helper function for tree_add_const_value_attribute. Natively encode
19347 initializer INIT into an array. Return true if successful. */
19349 static bool
19350 native_encode_initializer (tree init, unsigned char *array, int size)
19352 tree type;
19354 if (init == NULL_TREE)
19355 return false;
19357 STRIP_NOPS (init);
19358 switch (TREE_CODE (init))
19360 case STRING_CST:
19361 type = TREE_TYPE (init);
19362 if (TREE_CODE (type) == ARRAY_TYPE)
19364 tree enttype = TREE_TYPE (type);
19365 scalar_int_mode mode;
19367 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19368 || GET_MODE_SIZE (mode) != 1)
19369 return false;
19370 if (int_size_in_bytes (type) != size)
19371 return false;
19372 if (size > TREE_STRING_LENGTH (init))
19374 memcpy (array, TREE_STRING_POINTER (init),
19375 TREE_STRING_LENGTH (init));
19376 memset (array + TREE_STRING_LENGTH (init),
19377 '\0', size - TREE_STRING_LENGTH (init));
19379 else
19380 memcpy (array, TREE_STRING_POINTER (init), size);
19381 return true;
19383 return false;
19384 case CONSTRUCTOR:
19385 type = TREE_TYPE (init);
19386 if (int_size_in_bytes (type) != size)
19387 return false;
19388 if (TREE_CODE (type) == ARRAY_TYPE)
19390 HOST_WIDE_INT min_index;
19391 unsigned HOST_WIDE_INT cnt;
19392 int curpos = 0, fieldsize;
19393 constructor_elt *ce;
19395 if (TYPE_DOMAIN (type) == NULL_TREE
19396 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19397 return false;
19399 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19400 if (fieldsize <= 0)
19401 return false;
19403 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19404 memset (array, '\0', size);
19405 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19407 tree val = ce->value;
19408 tree index = ce->index;
19409 int pos = curpos;
19410 if (index && TREE_CODE (index) == RANGE_EXPR)
19411 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19412 * fieldsize;
19413 else if (index)
19414 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19416 if (val)
19418 STRIP_NOPS (val);
19419 if (!native_encode_initializer (val, array + pos, fieldsize))
19420 return false;
19422 curpos = pos + fieldsize;
19423 if (index && TREE_CODE (index) == RANGE_EXPR)
19425 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19426 - tree_to_shwi (TREE_OPERAND (index, 0));
19427 while (count-- > 0)
19429 if (val)
19430 memcpy (array + curpos, array + pos, fieldsize);
19431 curpos += fieldsize;
19434 gcc_assert (curpos <= size);
19436 return true;
19438 else if (TREE_CODE (type) == RECORD_TYPE
19439 || TREE_CODE (type) == UNION_TYPE)
19441 tree field = NULL_TREE;
19442 unsigned HOST_WIDE_INT cnt;
19443 constructor_elt *ce;
19445 if (int_size_in_bytes (type) != size)
19446 return false;
19448 if (TREE_CODE (type) == RECORD_TYPE)
19449 field = TYPE_FIELDS (type);
19451 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19453 tree val = ce->value;
19454 int pos, fieldsize;
19456 if (ce->index != 0)
19457 field = ce->index;
19459 if (val)
19460 STRIP_NOPS (val);
19462 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19463 return false;
19465 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19466 && TYPE_DOMAIN (TREE_TYPE (field))
19467 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19468 return false;
19469 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19470 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19471 return false;
19472 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19473 pos = int_byte_position (field);
19474 gcc_assert (pos + fieldsize <= size);
19475 if (val && fieldsize != 0
19476 && !native_encode_initializer (val, array + pos, fieldsize))
19477 return false;
19479 return true;
19481 return false;
19482 case VIEW_CONVERT_EXPR:
19483 case NON_LVALUE_EXPR:
19484 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19485 default:
19486 return native_encode_expr (init, array, size) == size;
19490 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19491 attribute is the const value T. */
19493 static bool
19494 tree_add_const_value_attribute (dw_die_ref die, tree t)
19496 tree init;
19497 tree type = TREE_TYPE (t);
19498 rtx rtl;
19500 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19501 return false;
19503 init = t;
19504 gcc_assert (!DECL_P (init));
19506 if (TREE_CODE (init) == INTEGER_CST)
19508 if (tree_fits_uhwi_p (init))
19510 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19511 return true;
19513 if (tree_fits_shwi_p (init))
19515 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19516 return true;
19519 if (! early_dwarf)
19521 rtl = rtl_for_decl_init (init, type);
19522 if (rtl)
19523 return add_const_value_attribute (die, rtl);
19525 /* If the host and target are sane, try harder. */
19526 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19527 && initializer_constant_valid_p (init, type))
19529 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19530 if (size > 0 && (int) size == size)
19532 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19534 if (native_encode_initializer (init, array, size))
19536 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19537 return true;
19539 ggc_free (array);
19542 return false;
19545 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19546 attribute is the const value of T, where T is an integral constant
19547 variable with static storage duration
19548 (so it can't be a PARM_DECL or a RESULT_DECL). */
19550 static bool
19551 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19554 if (!decl
19555 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19556 || (VAR_P (decl) && !TREE_STATIC (decl)))
19557 return false;
19559 if (TREE_READONLY (decl)
19560 && ! TREE_THIS_VOLATILE (decl)
19561 && DECL_INITIAL (decl))
19562 /* OK */;
19563 else
19564 return false;
19566 /* Don't add DW_AT_const_value if abstract origin already has one. */
19567 if (get_AT (var_die, DW_AT_const_value))
19568 return false;
19570 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19573 /* Convert the CFI instructions for the current function into a
19574 location list. This is used for DW_AT_frame_base when we targeting
19575 a dwarf2 consumer that does not support the dwarf3
19576 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19577 expressions. */
19579 static dw_loc_list_ref
19580 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19582 int ix;
19583 dw_fde_ref fde;
19584 dw_loc_list_ref list, *list_tail;
19585 dw_cfi_ref cfi;
19586 dw_cfa_location last_cfa, next_cfa;
19587 const char *start_label, *last_label, *section;
19588 dw_cfa_location remember;
19590 fde = cfun->fde;
19591 gcc_assert (fde != NULL);
19593 section = secname_for_decl (current_function_decl);
19594 list_tail = &list;
19595 list = NULL;
19597 memset (&next_cfa, 0, sizeof (next_cfa));
19598 next_cfa.reg = INVALID_REGNUM;
19599 remember = next_cfa;
19601 start_label = fde->dw_fde_begin;
19603 /* ??? Bald assumption that the CIE opcode list does not contain
19604 advance opcodes. */
19605 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19606 lookup_cfa_1 (cfi, &next_cfa, &remember);
19608 last_cfa = next_cfa;
19609 last_label = start_label;
19611 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19613 /* If the first partition contained no CFI adjustments, the
19614 CIE opcodes apply to the whole first partition. */
19615 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19616 fde->dw_fde_begin, fde->dw_fde_end, section);
19617 list_tail =&(*list_tail)->dw_loc_next;
19618 start_label = last_label = fde->dw_fde_second_begin;
19621 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19623 switch (cfi->dw_cfi_opc)
19625 case DW_CFA_set_loc:
19626 case DW_CFA_advance_loc1:
19627 case DW_CFA_advance_loc2:
19628 case DW_CFA_advance_loc4:
19629 if (!cfa_equal_p (&last_cfa, &next_cfa))
19631 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19632 start_label, last_label, section);
19634 list_tail = &(*list_tail)->dw_loc_next;
19635 last_cfa = next_cfa;
19636 start_label = last_label;
19638 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19639 break;
19641 case DW_CFA_advance_loc:
19642 /* The encoding is complex enough that we should never emit this. */
19643 gcc_unreachable ();
19645 default:
19646 lookup_cfa_1 (cfi, &next_cfa, &remember);
19647 break;
19649 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19651 if (!cfa_equal_p (&last_cfa, &next_cfa))
19653 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19654 start_label, last_label, section);
19656 list_tail = &(*list_tail)->dw_loc_next;
19657 last_cfa = next_cfa;
19658 start_label = last_label;
19660 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19661 start_label, fde->dw_fde_end, section);
19662 list_tail = &(*list_tail)->dw_loc_next;
19663 start_label = last_label = fde->dw_fde_second_begin;
19667 if (!cfa_equal_p (&last_cfa, &next_cfa))
19669 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19670 start_label, last_label, section);
19671 list_tail = &(*list_tail)->dw_loc_next;
19672 start_label = last_label;
19675 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19676 start_label,
19677 fde->dw_fde_second_begin
19678 ? fde->dw_fde_second_end : fde->dw_fde_end,
19679 section);
19681 if (list && list->dw_loc_next)
19682 gen_llsym (list);
19684 return list;
19687 /* Compute a displacement from the "steady-state frame pointer" to the
19688 frame base (often the same as the CFA), and store it in
19689 frame_pointer_fb_offset. OFFSET is added to the displacement
19690 before the latter is negated. */
19692 static void
19693 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19695 rtx reg, elim;
19697 #ifdef FRAME_POINTER_CFA_OFFSET
19698 reg = frame_pointer_rtx;
19699 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19700 #else
19701 reg = arg_pointer_rtx;
19702 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19703 #endif
19705 elim = (ira_use_lra_p
19706 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19707 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19708 if (GET_CODE (elim) == PLUS)
19710 offset += INTVAL (XEXP (elim, 1));
19711 elim = XEXP (elim, 0);
19714 frame_pointer_fb_offset = -offset;
19716 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19717 in which to eliminate. This is because it's stack pointer isn't
19718 directly accessible as a register within the ISA. To work around
19719 this, assume that while we cannot provide a proper value for
19720 frame_pointer_fb_offset, we won't need one either. */
19721 frame_pointer_fb_offset_valid
19722 = ((SUPPORTS_STACK_ALIGNMENT
19723 && (elim == hard_frame_pointer_rtx
19724 || elim == stack_pointer_rtx))
19725 || elim == (frame_pointer_needed
19726 ? hard_frame_pointer_rtx
19727 : stack_pointer_rtx));
19730 /* Generate a DW_AT_name attribute given some string value to be included as
19731 the value of the attribute. */
19733 static void
19734 add_name_attribute (dw_die_ref die, const char *name_string)
19736 if (name_string != NULL && *name_string != 0)
19738 if (demangle_name_func)
19739 name_string = (*demangle_name_func) (name_string);
19741 add_AT_string (die, DW_AT_name, name_string);
19745 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19746 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19747 of TYPE accordingly.
19749 ??? This is a temporary measure until after we're able to generate
19750 regular DWARF for the complex Ada type system. */
19752 static void
19753 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19754 dw_die_ref context_die)
19756 tree dtype;
19757 dw_die_ref dtype_die;
19759 if (!lang_hooks.types.descriptive_type)
19760 return;
19762 dtype = lang_hooks.types.descriptive_type (type);
19763 if (!dtype)
19764 return;
19766 dtype_die = lookup_type_die (dtype);
19767 if (!dtype_die)
19769 gen_type_die (dtype, context_die);
19770 dtype_die = lookup_type_die (dtype);
19771 gcc_assert (dtype_die);
19774 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19777 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19779 static const char *
19780 comp_dir_string (void)
19782 const char *wd;
19783 char *wd1;
19784 static const char *cached_wd = NULL;
19786 if (cached_wd != NULL)
19787 return cached_wd;
19789 wd = get_src_pwd ();
19790 if (wd == NULL)
19791 return NULL;
19793 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19795 int wdlen;
19797 wdlen = strlen (wd);
19798 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19799 strcpy (wd1, wd);
19800 wd1 [wdlen] = DIR_SEPARATOR;
19801 wd1 [wdlen + 1] = 0;
19802 wd = wd1;
19805 cached_wd = remap_debug_filename (wd);
19806 return cached_wd;
19809 /* Generate a DW_AT_comp_dir attribute for DIE. */
19811 static void
19812 add_comp_dir_attribute (dw_die_ref die)
19814 const char * wd = comp_dir_string ();
19815 if (wd != NULL)
19816 add_AT_string (die, DW_AT_comp_dir, wd);
19819 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19820 pointer computation, ...), output a representation for that bound according
19821 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19822 loc_list_from_tree for the meaning of CONTEXT. */
19824 static void
19825 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19826 int forms, struct loc_descr_context *context)
19828 dw_die_ref context_die, decl_die;
19829 dw_loc_list_ref list;
19830 bool strip_conversions = true;
19831 bool placeholder_seen = false;
19833 while (strip_conversions)
19834 switch (TREE_CODE (value))
19836 case ERROR_MARK:
19837 case SAVE_EXPR:
19838 return;
19840 CASE_CONVERT:
19841 case VIEW_CONVERT_EXPR:
19842 value = TREE_OPERAND (value, 0);
19843 break;
19845 default:
19846 strip_conversions = false;
19847 break;
19850 /* If possible and permitted, output the attribute as a constant. */
19851 if ((forms & dw_scalar_form_constant) != 0
19852 && TREE_CODE (value) == INTEGER_CST)
19854 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19856 /* If HOST_WIDE_INT is big enough then represent the bound as
19857 a constant value. We need to choose a form based on
19858 whether the type is signed or unsigned. We cannot just
19859 call add_AT_unsigned if the value itself is positive
19860 (add_AT_unsigned might add the unsigned value encoded as
19861 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19862 bounds type and then sign extend any unsigned values found
19863 for signed types. This is needed only for
19864 DW_AT_{lower,upper}_bound, since for most other attributes,
19865 consumers will treat DW_FORM_data[1248] as unsigned values,
19866 regardless of the underlying type. */
19867 if (prec <= HOST_BITS_PER_WIDE_INT
19868 || tree_fits_uhwi_p (value))
19870 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19871 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19872 else
19873 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19875 else
19876 /* Otherwise represent the bound as an unsigned value with
19877 the precision of its type. The precision and signedness
19878 of the type will be necessary to re-interpret it
19879 unambiguously. */
19880 add_AT_wide (die, attr, wi::to_wide (value));
19881 return;
19884 /* Otherwise, if it's possible and permitted too, output a reference to
19885 another DIE. */
19886 if ((forms & dw_scalar_form_reference) != 0)
19888 tree decl = NULL_TREE;
19890 /* Some type attributes reference an outer type. For instance, the upper
19891 bound of an array may reference an embedding record (this happens in
19892 Ada). */
19893 if (TREE_CODE (value) == COMPONENT_REF
19894 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19895 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19896 decl = TREE_OPERAND (value, 1);
19898 else if (VAR_P (value)
19899 || TREE_CODE (value) == PARM_DECL
19900 || TREE_CODE (value) == RESULT_DECL)
19901 decl = value;
19903 if (decl != NULL_TREE)
19905 dw_die_ref decl_die = lookup_decl_die (decl);
19907 /* ??? Can this happen, or should the variable have been bound
19908 first? Probably it can, since I imagine that we try to create
19909 the types of parameters in the order in which they exist in
19910 the list, and won't have created a forward reference to a
19911 later parameter. */
19912 if (decl_die != NULL)
19914 add_AT_die_ref (die, attr, decl_die);
19915 return;
19920 /* Last chance: try to create a stack operation procedure to evaluate the
19921 value. Do nothing if even that is not possible or permitted. */
19922 if ((forms & dw_scalar_form_exprloc) == 0)
19923 return;
19925 list = loc_list_from_tree (value, 2, context);
19926 if (context && context->placeholder_arg)
19928 placeholder_seen = context->placeholder_seen;
19929 context->placeholder_seen = false;
19931 if (list == NULL || single_element_loc_list_p (list))
19933 /* If this attribute is not a reference nor constant, it is
19934 a DWARF expression rather than location description. For that
19935 loc_list_from_tree (value, 0, &context) is needed. */
19936 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19937 if (list2 && single_element_loc_list_p (list2))
19939 if (placeholder_seen)
19941 struct dwarf_procedure_info dpi;
19942 dpi.fndecl = NULL_TREE;
19943 dpi.args_count = 1;
19944 if (!resolve_args_picking (list2->expr, 1, &dpi))
19945 return;
19947 add_AT_loc (die, attr, list2->expr);
19948 return;
19952 /* If that failed to give a single element location list, fall back to
19953 outputting this as a reference... still if permitted. */
19954 if (list == NULL
19955 || (forms & dw_scalar_form_reference) == 0
19956 || placeholder_seen)
19957 return;
19959 if (current_function_decl == 0)
19960 context_die = comp_unit_die ();
19961 else
19962 context_die = lookup_decl_die (current_function_decl);
19964 decl_die = new_die (DW_TAG_variable, context_die, value);
19965 add_AT_flag (decl_die, DW_AT_artificial, 1);
19966 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19967 context_die);
19968 add_AT_location_description (decl_die, DW_AT_location, list);
19969 add_AT_die_ref (die, attr, decl_die);
19972 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19973 default. */
19975 static int
19976 lower_bound_default (void)
19978 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19980 case DW_LANG_C:
19981 case DW_LANG_C89:
19982 case DW_LANG_C99:
19983 case DW_LANG_C11:
19984 case DW_LANG_C_plus_plus:
19985 case DW_LANG_C_plus_plus_11:
19986 case DW_LANG_C_plus_plus_14:
19987 case DW_LANG_ObjC:
19988 case DW_LANG_ObjC_plus_plus:
19989 return 0;
19990 case DW_LANG_Fortran77:
19991 case DW_LANG_Fortran90:
19992 case DW_LANG_Fortran95:
19993 case DW_LANG_Fortran03:
19994 case DW_LANG_Fortran08:
19995 return 1;
19996 case DW_LANG_UPC:
19997 case DW_LANG_D:
19998 case DW_LANG_Python:
19999 return dwarf_version >= 4 ? 0 : -1;
20000 case DW_LANG_Ada95:
20001 case DW_LANG_Ada83:
20002 case DW_LANG_Cobol74:
20003 case DW_LANG_Cobol85:
20004 case DW_LANG_Modula2:
20005 case DW_LANG_PLI:
20006 return dwarf_version >= 4 ? 1 : -1;
20007 default:
20008 return -1;
20012 /* Given a tree node describing an array bound (either lower or upper) output
20013 a representation for that bound. */
20015 static void
20016 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20017 tree bound, struct loc_descr_context *context)
20019 int dflt;
20021 while (1)
20022 switch (TREE_CODE (bound))
20024 /* Strip all conversions. */
20025 CASE_CONVERT:
20026 case VIEW_CONVERT_EXPR:
20027 bound = TREE_OPERAND (bound, 0);
20028 break;
20030 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20031 are even omitted when they are the default. */
20032 case INTEGER_CST:
20033 /* If the value for this bound is the default one, we can even omit the
20034 attribute. */
20035 if (bound_attr == DW_AT_lower_bound
20036 && tree_fits_shwi_p (bound)
20037 && (dflt = lower_bound_default ()) != -1
20038 && tree_to_shwi (bound) == dflt)
20039 return;
20041 /* FALLTHRU */
20043 default:
20044 /* Because of the complex interaction there can be with other GNAT
20045 encodings, GDB isn't ready yet to handle proper DWARF description
20046 for self-referencial subrange bounds: let GNAT encodings do the
20047 magic in such a case. */
20048 if (is_ada ()
20049 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20050 && contains_placeholder_p (bound))
20051 return;
20053 add_scalar_info (subrange_die, bound_attr, bound,
20054 dw_scalar_form_constant
20055 | dw_scalar_form_exprloc
20056 | dw_scalar_form_reference,
20057 context);
20058 return;
20062 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20063 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20064 Note that the block of subscript information for an array type also
20065 includes information about the element type of the given array type.
20067 This function reuses previously set type and bound information if
20068 available. */
20070 static void
20071 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20073 unsigned dimension_number;
20074 tree lower, upper;
20075 dw_die_ref child = type_die->die_child;
20077 for (dimension_number = 0;
20078 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20079 type = TREE_TYPE (type), dimension_number++)
20081 tree domain = TYPE_DOMAIN (type);
20083 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20084 break;
20086 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20087 and (in GNU C only) variable bounds. Handle all three forms
20088 here. */
20090 /* Find and reuse a previously generated DW_TAG_subrange_type if
20091 available.
20093 For multi-dimensional arrays, as we iterate through the
20094 various dimensions in the enclosing for loop above, we also
20095 iterate through the DIE children and pick at each
20096 DW_TAG_subrange_type previously generated (if available).
20097 Each child DW_TAG_subrange_type DIE describes the range of
20098 the current dimension. At this point we should have as many
20099 DW_TAG_subrange_type's as we have dimensions in the
20100 array. */
20101 dw_die_ref subrange_die = NULL;
20102 if (child)
20103 while (1)
20105 child = child->die_sib;
20106 if (child->die_tag == DW_TAG_subrange_type)
20107 subrange_die = child;
20108 if (child == type_die->die_child)
20110 /* If we wrapped around, stop looking next time. */
20111 child = NULL;
20112 break;
20114 if (child->die_tag == DW_TAG_subrange_type)
20115 break;
20117 if (!subrange_die)
20118 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20120 if (domain)
20122 /* We have an array type with specified bounds. */
20123 lower = TYPE_MIN_VALUE (domain);
20124 upper = TYPE_MAX_VALUE (domain);
20126 /* Define the index type. */
20127 if (TREE_TYPE (domain)
20128 && !get_AT (subrange_die, DW_AT_type))
20130 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20131 TREE_TYPE field. We can't emit debug info for this
20132 because it is an unnamed integral type. */
20133 if (TREE_CODE (domain) == INTEGER_TYPE
20134 && TYPE_NAME (domain) == NULL_TREE
20135 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20136 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20138 else
20139 add_type_attribute (subrange_die, TREE_TYPE (domain),
20140 TYPE_UNQUALIFIED, false, type_die);
20143 /* ??? If upper is NULL, the array has unspecified length,
20144 but it does have a lower bound. This happens with Fortran
20145 dimension arr(N:*)
20146 Since the debugger is definitely going to need to know N
20147 to produce useful results, go ahead and output the lower
20148 bound solo, and hope the debugger can cope. */
20150 if (!get_AT (subrange_die, DW_AT_lower_bound))
20151 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20152 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20153 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20156 /* Otherwise we have an array type with an unspecified length. The
20157 DWARF-2 spec does not say how to handle this; let's just leave out the
20158 bounds. */
20162 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20164 static void
20165 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20167 dw_die_ref decl_die;
20168 HOST_WIDE_INT size;
20169 dw_loc_descr_ref size_expr = NULL;
20171 switch (TREE_CODE (tree_node))
20173 case ERROR_MARK:
20174 size = 0;
20175 break;
20176 case ENUMERAL_TYPE:
20177 case RECORD_TYPE:
20178 case UNION_TYPE:
20179 case QUAL_UNION_TYPE:
20180 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20181 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20183 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20184 return;
20186 size_expr = type_byte_size (tree_node, &size);
20187 break;
20188 case FIELD_DECL:
20189 /* For a data member of a struct or union, the DW_AT_byte_size is
20190 generally given as the number of bytes normally allocated for an
20191 object of the *declared* type of the member itself. This is true
20192 even for bit-fields. */
20193 size = int_size_in_bytes (field_type (tree_node));
20194 break;
20195 default:
20196 gcc_unreachable ();
20199 /* Support for dynamically-sized objects was introduced by DWARFv3.
20200 At the moment, GDB does not handle variable byte sizes very well,
20201 though. */
20202 if ((dwarf_version >= 3 || !dwarf_strict)
20203 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20204 && size_expr != NULL)
20205 add_AT_loc (die, DW_AT_byte_size, size_expr);
20207 /* Note that `size' might be -1 when we get to this point. If it is, that
20208 indicates that the byte size of the entity in question is variable and
20209 that we could not generate a DWARF expression that computes it. */
20210 if (size >= 0)
20211 add_AT_unsigned (die, DW_AT_byte_size, size);
20214 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20215 alignment. */
20217 static void
20218 add_alignment_attribute (dw_die_ref die, tree tree_node)
20220 if (dwarf_version < 5 && dwarf_strict)
20221 return;
20223 unsigned align;
20225 if (DECL_P (tree_node))
20227 if (!DECL_USER_ALIGN (tree_node))
20228 return;
20230 align = DECL_ALIGN_UNIT (tree_node);
20232 else if (TYPE_P (tree_node))
20234 if (!TYPE_USER_ALIGN (tree_node))
20235 return;
20237 align = TYPE_ALIGN_UNIT (tree_node);
20239 else
20240 gcc_unreachable ();
20242 add_AT_unsigned (die, DW_AT_alignment, align);
20245 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20246 which specifies the distance in bits from the highest order bit of the
20247 "containing object" for the bit-field to the highest order bit of the
20248 bit-field itself.
20250 For any given bit-field, the "containing object" is a hypothetical object
20251 (of some integral or enum type) within which the given bit-field lives. The
20252 type of this hypothetical "containing object" is always the same as the
20253 declared type of the individual bit-field itself. The determination of the
20254 exact location of the "containing object" for a bit-field is rather
20255 complicated. It's handled by the `field_byte_offset' function (above).
20257 CTX is required: see the comment for VLR_CONTEXT.
20259 Note that it is the size (in bytes) of the hypothetical "containing object"
20260 which will be given in the DW_AT_byte_size attribute for this bit-field.
20261 (See `byte_size_attribute' above). */
20263 static inline void
20264 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20266 HOST_WIDE_INT object_offset_in_bytes;
20267 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20268 HOST_WIDE_INT bitpos_int;
20269 HOST_WIDE_INT highest_order_object_bit_offset;
20270 HOST_WIDE_INT highest_order_field_bit_offset;
20271 HOST_WIDE_INT bit_offset;
20273 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20275 /* Must be a field and a bit field. */
20276 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20278 /* We can't yet handle bit-fields whose offsets are variable, so if we
20279 encounter such things, just return without generating any attribute
20280 whatsoever. Likewise for variable or too large size. */
20281 if (! tree_fits_shwi_p (bit_position (decl))
20282 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20283 return;
20285 bitpos_int = int_bit_position (decl);
20287 /* Note that the bit offset is always the distance (in bits) from the
20288 highest-order bit of the "containing object" to the highest-order bit of
20289 the bit-field itself. Since the "high-order end" of any object or field
20290 is different on big-endian and little-endian machines, the computation
20291 below must take account of these differences. */
20292 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20293 highest_order_field_bit_offset = bitpos_int;
20295 if (! BYTES_BIG_ENDIAN)
20297 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20298 highest_order_object_bit_offset +=
20299 simple_type_size_in_bits (original_type);
20302 bit_offset
20303 = (! BYTES_BIG_ENDIAN
20304 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20305 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20307 if (bit_offset < 0)
20308 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20309 else
20310 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20313 /* For a FIELD_DECL node which represents a bit field, output an attribute
20314 which specifies the length in bits of the given field. */
20316 static inline void
20317 add_bit_size_attribute (dw_die_ref die, tree decl)
20319 /* Must be a field and a bit field. */
20320 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20321 && DECL_BIT_FIELD_TYPE (decl));
20323 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20324 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20327 /* If the compiled language is ANSI C, then add a 'prototyped'
20328 attribute, if arg types are given for the parameters of a function. */
20330 static inline void
20331 add_prototyped_attribute (dw_die_ref die, tree func_type)
20333 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20335 case DW_LANG_C:
20336 case DW_LANG_C89:
20337 case DW_LANG_C99:
20338 case DW_LANG_C11:
20339 case DW_LANG_ObjC:
20340 if (prototype_p (func_type))
20341 add_AT_flag (die, DW_AT_prototyped, 1);
20342 break;
20343 default:
20344 break;
20348 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20349 by looking in the type declaration, the object declaration equate table or
20350 the block mapping. */
20352 static inline dw_die_ref
20353 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20355 dw_die_ref origin_die = NULL;
20357 if (DECL_P (origin))
20359 dw_die_ref c;
20360 origin_die = lookup_decl_die (origin);
20361 /* "Unwrap" the decls DIE which we put in the imported unit context.
20362 We are looking for the abstract copy here. */
20363 if (in_lto_p
20364 && origin_die
20365 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20366 /* ??? Identify this better. */
20367 && c->with_offset)
20368 origin_die = c;
20370 else if (TYPE_P (origin))
20371 origin_die = lookup_type_die (origin);
20372 else if (TREE_CODE (origin) == BLOCK)
20373 origin_die = BLOCK_DIE (origin);
20375 /* XXX: Functions that are never lowered don't always have correct block
20376 trees (in the case of java, they simply have no block tree, in some other
20377 languages). For these functions, there is nothing we can really do to
20378 output correct debug info for inlined functions in all cases. Rather
20379 than die, we'll just produce deficient debug info now, in that we will
20380 have variables without a proper abstract origin. In the future, when all
20381 functions are lowered, we should re-add a gcc_assert (origin_die)
20382 here. */
20384 if (origin_die)
20385 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20386 return origin_die;
20389 /* We do not currently support the pure_virtual attribute. */
20391 static inline void
20392 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20394 if (DECL_VINDEX (func_decl))
20396 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20398 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20399 add_AT_loc (die, DW_AT_vtable_elem_location,
20400 new_loc_descr (DW_OP_constu,
20401 tree_to_shwi (DECL_VINDEX (func_decl)),
20402 0));
20404 /* GNU extension: Record what type this method came from originally. */
20405 if (debug_info_level > DINFO_LEVEL_TERSE
20406 && DECL_CONTEXT (func_decl))
20407 add_AT_die_ref (die, DW_AT_containing_type,
20408 lookup_type_die (DECL_CONTEXT (func_decl)));
20412 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20413 given decl. This used to be a vendor extension until after DWARF 4
20414 standardized it. */
20416 static void
20417 add_linkage_attr (dw_die_ref die, tree decl)
20419 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20421 /* Mimic what assemble_name_raw does with a leading '*'. */
20422 if (name[0] == '*')
20423 name = &name[1];
20425 if (dwarf_version >= 4)
20426 add_AT_string (die, DW_AT_linkage_name, name);
20427 else
20428 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20431 /* Add source coordinate attributes for the given decl. */
20433 static void
20434 add_src_coords_attributes (dw_die_ref die, tree decl)
20436 expanded_location s;
20438 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20439 return;
20440 s = expand_location (DECL_SOURCE_LOCATION (decl));
20441 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20442 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20443 if (debug_column_info && s.column)
20444 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20447 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20449 static void
20450 add_linkage_name_raw (dw_die_ref die, tree decl)
20452 /* Defer until we have an assembler name set. */
20453 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20455 limbo_die_node *asm_name;
20457 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20458 asm_name->die = die;
20459 asm_name->created_for = decl;
20460 asm_name->next = deferred_asm_name;
20461 deferred_asm_name = asm_name;
20463 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20464 add_linkage_attr (die, decl);
20467 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20469 static void
20470 add_linkage_name (dw_die_ref die, tree decl)
20472 if (debug_info_level > DINFO_LEVEL_NONE
20473 && VAR_OR_FUNCTION_DECL_P (decl)
20474 && TREE_PUBLIC (decl)
20475 && !(VAR_P (decl) && DECL_REGISTER (decl))
20476 && die->die_tag != DW_TAG_member)
20477 add_linkage_name_raw (die, decl);
20480 /* Add a DW_AT_name attribute and source coordinate attribute for the
20481 given decl, but only if it actually has a name. */
20483 static void
20484 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20485 bool no_linkage_name)
20487 tree decl_name;
20489 decl_name = DECL_NAME (decl);
20490 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20492 const char *name = dwarf2_name (decl, 0);
20493 if (name)
20494 add_name_attribute (die, name);
20495 if (! DECL_ARTIFICIAL (decl))
20496 add_src_coords_attributes (die, decl);
20498 if (!no_linkage_name)
20499 add_linkage_name (die, decl);
20502 #ifdef VMS_DEBUGGING_INFO
20503 /* Get the function's name, as described by its RTL. This may be different
20504 from the DECL_NAME name used in the source file. */
20505 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20507 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20508 XEXP (DECL_RTL (decl), 0), false);
20509 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20511 #endif /* VMS_DEBUGGING_INFO */
20514 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20516 static void
20517 add_discr_value (dw_die_ref die, dw_discr_value *value)
20519 dw_attr_node attr;
20521 attr.dw_attr = DW_AT_discr_value;
20522 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20523 attr.dw_attr_val.val_entry = NULL;
20524 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20525 if (value->pos)
20526 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20527 else
20528 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20529 add_dwarf_attr (die, &attr);
20532 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20534 static void
20535 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20537 dw_attr_node attr;
20539 attr.dw_attr = DW_AT_discr_list;
20540 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20541 attr.dw_attr_val.val_entry = NULL;
20542 attr.dw_attr_val.v.val_discr_list = discr_list;
20543 add_dwarf_attr (die, &attr);
20546 static inline dw_discr_list_ref
20547 AT_discr_list (dw_attr_node *attr)
20549 return attr->dw_attr_val.v.val_discr_list;
20552 #ifdef VMS_DEBUGGING_INFO
20553 /* Output the debug main pointer die for VMS */
20555 void
20556 dwarf2out_vms_debug_main_pointer (void)
20558 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20559 dw_die_ref die;
20561 /* Allocate the VMS debug main subprogram die. */
20562 die = new_die_raw (DW_TAG_subprogram);
20563 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20564 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20565 current_function_funcdef_no);
20566 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20568 /* Make it the first child of comp_unit_die (). */
20569 die->die_parent = comp_unit_die ();
20570 if (comp_unit_die ()->die_child)
20572 die->die_sib = comp_unit_die ()->die_child->die_sib;
20573 comp_unit_die ()->die_child->die_sib = die;
20575 else
20577 die->die_sib = die;
20578 comp_unit_die ()->die_child = die;
20581 #endif /* VMS_DEBUGGING_INFO */
20583 /* Push a new declaration scope. */
20585 static void
20586 push_decl_scope (tree scope)
20588 vec_safe_push (decl_scope_table, scope);
20591 /* Pop a declaration scope. */
20593 static inline void
20594 pop_decl_scope (void)
20596 decl_scope_table->pop ();
20599 /* walk_tree helper function for uses_local_type, below. */
20601 static tree
20602 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20604 if (!TYPE_P (*tp))
20605 *walk_subtrees = 0;
20606 else
20608 tree name = TYPE_NAME (*tp);
20609 if (name && DECL_P (name) && decl_function_context (name))
20610 return *tp;
20612 return NULL_TREE;
20615 /* If TYPE involves a function-local type (including a local typedef to a
20616 non-local type), returns that type; otherwise returns NULL_TREE. */
20618 static tree
20619 uses_local_type (tree type)
20621 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20622 return used;
20625 /* Return the DIE for the scope that immediately contains this type.
20626 Non-named types that do not involve a function-local type get global
20627 scope. Named types nested in namespaces or other types get their
20628 containing scope. All other types (i.e. function-local named types) get
20629 the current active scope. */
20631 static dw_die_ref
20632 scope_die_for (tree t, dw_die_ref context_die)
20634 dw_die_ref scope_die = NULL;
20635 tree containing_scope;
20637 /* Non-types always go in the current scope. */
20638 gcc_assert (TYPE_P (t));
20640 /* Use the scope of the typedef, rather than the scope of the type
20641 it refers to. */
20642 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20643 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20644 else
20645 containing_scope = TYPE_CONTEXT (t);
20647 /* Use the containing namespace if there is one. */
20648 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20650 if (context_die == lookup_decl_die (containing_scope))
20651 /* OK */;
20652 else if (debug_info_level > DINFO_LEVEL_TERSE)
20653 context_die = get_context_die (containing_scope);
20654 else
20655 containing_scope = NULL_TREE;
20658 /* Ignore function type "scopes" from the C frontend. They mean that
20659 a tagged type is local to a parmlist of a function declarator, but
20660 that isn't useful to DWARF. */
20661 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20662 containing_scope = NULL_TREE;
20664 if (SCOPE_FILE_SCOPE_P (containing_scope))
20666 /* If T uses a local type keep it local as well, to avoid references
20667 to function-local DIEs from outside the function. */
20668 if (current_function_decl && uses_local_type (t))
20669 scope_die = context_die;
20670 else
20671 scope_die = comp_unit_die ();
20673 else if (TYPE_P (containing_scope))
20675 /* For types, we can just look up the appropriate DIE. */
20676 if (debug_info_level > DINFO_LEVEL_TERSE)
20677 scope_die = get_context_die (containing_scope);
20678 else
20680 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20681 if (scope_die == NULL)
20682 scope_die = comp_unit_die ();
20685 else
20686 scope_die = context_die;
20688 return scope_die;
20691 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20693 static inline int
20694 local_scope_p (dw_die_ref context_die)
20696 for (; context_die; context_die = context_die->die_parent)
20697 if (context_die->die_tag == DW_TAG_inlined_subroutine
20698 || context_die->die_tag == DW_TAG_subprogram)
20699 return 1;
20701 return 0;
20704 /* Returns nonzero if CONTEXT_DIE is a class. */
20706 static inline int
20707 class_scope_p (dw_die_ref context_die)
20709 return (context_die
20710 && (context_die->die_tag == DW_TAG_structure_type
20711 || context_die->die_tag == DW_TAG_class_type
20712 || context_die->die_tag == DW_TAG_interface_type
20713 || context_die->die_tag == DW_TAG_union_type));
20716 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20717 whether or not to treat a DIE in this context as a declaration. */
20719 static inline int
20720 class_or_namespace_scope_p (dw_die_ref context_die)
20722 return (class_scope_p (context_die)
20723 || (context_die && context_die->die_tag == DW_TAG_namespace));
20726 /* Many forms of DIEs require a "type description" attribute. This
20727 routine locates the proper "type descriptor" die for the type given
20728 by 'type' plus any additional qualifiers given by 'cv_quals', and
20729 adds a DW_AT_type attribute below the given die. */
20731 static void
20732 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20733 bool reverse, dw_die_ref context_die)
20735 enum tree_code code = TREE_CODE (type);
20736 dw_die_ref type_die = NULL;
20738 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20739 or fixed-point type, use the inner type. This is because we have no
20740 support for unnamed types in base_type_die. This can happen if this is
20741 an Ada subrange type. Correct solution is emit a subrange type die. */
20742 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20743 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20744 type = TREE_TYPE (type), code = TREE_CODE (type);
20746 if (code == ERROR_MARK
20747 /* Handle a special case. For functions whose return type is void, we
20748 generate *no* type attribute. (Note that no object may have type
20749 `void', so this only applies to function return types). */
20750 || code == VOID_TYPE)
20751 return;
20753 type_die = modified_type_die (type,
20754 cv_quals | TYPE_QUALS (type),
20755 reverse,
20756 context_die);
20758 if (type_die != NULL)
20759 add_AT_die_ref (object_die, DW_AT_type, type_die);
20762 /* Given an object die, add the calling convention attribute for the
20763 function call type. */
20764 static void
20765 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20767 enum dwarf_calling_convention value = DW_CC_normal;
20769 value = ((enum dwarf_calling_convention)
20770 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20772 if (is_fortran ()
20773 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20775 /* DWARF 2 doesn't provide a way to identify a program's source-level
20776 entry point. DW_AT_calling_convention attributes are only meant
20777 to describe functions' calling conventions. However, lacking a
20778 better way to signal the Fortran main program, we used this for
20779 a long time, following existing custom. Now, DWARF 4 has
20780 DW_AT_main_subprogram, which we add below, but some tools still
20781 rely on the old way, which we thus keep. */
20782 value = DW_CC_program;
20784 if (dwarf_version >= 4 || !dwarf_strict)
20785 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20788 /* Only add the attribute if the backend requests it, and
20789 is not DW_CC_normal. */
20790 if (value && (value != DW_CC_normal))
20791 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20794 /* Given a tree pointer to a struct, class, union, or enum type node, return
20795 a pointer to the (string) tag name for the given type, or zero if the type
20796 was declared without a tag. */
20798 static const char *
20799 type_tag (const_tree type)
20801 const char *name = 0;
20803 if (TYPE_NAME (type) != 0)
20805 tree t = 0;
20807 /* Find the IDENTIFIER_NODE for the type name. */
20808 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20809 && !TYPE_NAMELESS (type))
20810 t = TYPE_NAME (type);
20812 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20813 a TYPE_DECL node, regardless of whether or not a `typedef' was
20814 involved. */
20815 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20816 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20818 /* We want to be extra verbose. Don't call dwarf_name if
20819 DECL_NAME isn't set. The default hook for decl_printable_name
20820 doesn't like that, and in this context it's correct to return
20821 0, instead of "<anonymous>" or the like. */
20822 if (DECL_NAME (TYPE_NAME (type))
20823 && !DECL_NAMELESS (TYPE_NAME (type)))
20824 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20827 /* Now get the name as a string, or invent one. */
20828 if (!name && t != 0)
20829 name = IDENTIFIER_POINTER (t);
20832 return (name == 0 || *name == '\0') ? 0 : name;
20835 /* Return the type associated with a data member, make a special check
20836 for bit field types. */
20838 static inline tree
20839 member_declared_type (const_tree member)
20841 return (DECL_BIT_FIELD_TYPE (member)
20842 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20845 /* Get the decl's label, as described by its RTL. This may be different
20846 from the DECL_NAME name used in the source file. */
20848 #if 0
20849 static const char *
20850 decl_start_label (tree decl)
20852 rtx x;
20853 const char *fnname;
20855 x = DECL_RTL (decl);
20856 gcc_assert (MEM_P (x));
20858 x = XEXP (x, 0);
20859 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20861 fnname = XSTR (x, 0);
20862 return fnname;
20864 #endif
20866 /* For variable-length arrays that have been previously generated, but
20867 may be incomplete due to missing subscript info, fill the subscript
20868 info. Return TRUE if this is one of those cases. */
20869 static bool
20870 fill_variable_array_bounds (tree type)
20872 if (TREE_ASM_WRITTEN (type)
20873 && TREE_CODE (type) == ARRAY_TYPE
20874 && variably_modified_type_p (type, NULL))
20876 dw_die_ref array_die = lookup_type_die (type);
20877 if (!array_die)
20878 return false;
20879 add_subscript_info (array_die, type, !is_ada ());
20880 return true;
20882 return false;
20885 /* These routines generate the internal representation of the DIE's for
20886 the compilation unit. Debugging information is collected by walking
20887 the declaration trees passed in from dwarf2out_decl(). */
20889 static void
20890 gen_array_type_die (tree type, dw_die_ref context_die)
20892 dw_die_ref array_die;
20894 /* GNU compilers represent multidimensional array types as sequences of one
20895 dimensional array types whose element types are themselves array types.
20896 We sometimes squish that down to a single array_type DIE with multiple
20897 subscripts in the Dwarf debugging info. The draft Dwarf specification
20898 say that we are allowed to do this kind of compression in C, because
20899 there is no difference between an array of arrays and a multidimensional
20900 array. We don't do this for Ada to remain as close as possible to the
20901 actual representation, which is especially important against the language
20902 flexibilty wrt arrays of variable size. */
20904 bool collapse_nested_arrays = !is_ada ();
20906 if (fill_variable_array_bounds (type))
20907 return;
20909 dw_die_ref scope_die = scope_die_for (type, context_die);
20910 tree element_type;
20912 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20913 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20914 if (TYPE_STRING_FLAG (type)
20915 && TREE_CODE (type) == ARRAY_TYPE
20916 && is_fortran ()
20917 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20919 HOST_WIDE_INT size;
20921 array_die = new_die (DW_TAG_string_type, scope_die, type);
20922 add_name_attribute (array_die, type_tag (type));
20923 equate_type_number_to_die (type, array_die);
20924 size = int_size_in_bytes (type);
20925 if (size >= 0)
20926 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20927 /* ??? We can't annotate types late, but for LTO we may not
20928 generate a location early either (gfortran.dg/save_6.f90). */
20929 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20930 && TYPE_DOMAIN (type) != NULL_TREE
20931 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20933 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20934 tree rszdecl = szdecl;
20936 size = int_size_in_bytes (TREE_TYPE (szdecl));
20937 if (!DECL_P (szdecl))
20939 if (TREE_CODE (szdecl) == INDIRECT_REF
20940 && DECL_P (TREE_OPERAND (szdecl, 0)))
20942 rszdecl = TREE_OPERAND (szdecl, 0);
20943 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20944 != DWARF2_ADDR_SIZE)
20945 size = 0;
20947 else
20948 size = 0;
20950 if (size > 0)
20952 dw_loc_list_ref loc
20953 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20954 NULL);
20955 if (loc)
20957 add_AT_location_description (array_die, DW_AT_string_length,
20958 loc);
20959 if (size != DWARF2_ADDR_SIZE)
20960 add_AT_unsigned (array_die, dwarf_version >= 5
20961 ? DW_AT_string_length_byte_size
20962 : DW_AT_byte_size, size);
20966 return;
20969 array_die = new_die (DW_TAG_array_type, scope_die, type);
20970 add_name_attribute (array_die, type_tag (type));
20971 equate_type_number_to_die (type, array_die);
20973 if (TREE_CODE (type) == VECTOR_TYPE)
20974 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20976 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20977 if (is_fortran ()
20978 && TREE_CODE (type) == ARRAY_TYPE
20979 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20980 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20981 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20983 #if 0
20984 /* We default the array ordering. Debuggers will probably do the right
20985 things even if DW_AT_ordering is not present. It's not even an issue
20986 until we start to get into multidimensional arrays anyway. If a debugger
20987 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20988 then we'll have to put the DW_AT_ordering attribute back in. (But if
20989 and when we find out that we need to put these in, we will only do so
20990 for multidimensional arrays. */
20991 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20992 #endif
20994 if (TREE_CODE (type) == VECTOR_TYPE)
20996 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20997 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20998 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20999 add_bound_info (subrange_die, DW_AT_upper_bound,
21000 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21002 else
21003 add_subscript_info (array_die, type, collapse_nested_arrays);
21005 /* Add representation of the type of the elements of this array type and
21006 emit the corresponding DIE if we haven't done it already. */
21007 element_type = TREE_TYPE (type);
21008 if (collapse_nested_arrays)
21009 while (TREE_CODE (element_type) == ARRAY_TYPE)
21011 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21012 break;
21013 element_type = TREE_TYPE (element_type);
21016 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21017 TREE_CODE (type) == ARRAY_TYPE
21018 && TYPE_REVERSE_STORAGE_ORDER (type),
21019 context_die);
21021 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21022 if (TYPE_ARTIFICIAL (type))
21023 add_AT_flag (array_die, DW_AT_artificial, 1);
21025 if (get_AT (array_die, DW_AT_name))
21026 add_pubtype (type, array_die);
21028 add_alignment_attribute (array_die, type);
21031 /* This routine generates DIE for array with hidden descriptor, details
21032 are filled into *info by a langhook. */
21034 static void
21035 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21036 dw_die_ref context_die)
21038 const dw_die_ref scope_die = scope_die_for (type, context_die);
21039 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21040 struct loc_descr_context context = { type, info->base_decl, NULL,
21041 false, false };
21042 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21043 int dim;
21045 add_name_attribute (array_die, type_tag (type));
21046 equate_type_number_to_die (type, array_die);
21048 if (info->ndimensions > 1)
21049 switch (info->ordering)
21051 case array_descr_ordering_row_major:
21052 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21053 break;
21054 case array_descr_ordering_column_major:
21055 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21056 break;
21057 default:
21058 break;
21061 if (dwarf_version >= 3 || !dwarf_strict)
21063 if (info->data_location)
21064 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21065 dw_scalar_form_exprloc, &context);
21066 if (info->associated)
21067 add_scalar_info (array_die, DW_AT_associated, info->associated,
21068 dw_scalar_form_constant
21069 | dw_scalar_form_exprloc
21070 | dw_scalar_form_reference, &context);
21071 if (info->allocated)
21072 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21073 dw_scalar_form_constant
21074 | dw_scalar_form_exprloc
21075 | dw_scalar_form_reference, &context);
21076 if (info->stride)
21078 const enum dwarf_attribute attr
21079 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21080 const int forms
21081 = (info->stride_in_bits)
21082 ? dw_scalar_form_constant
21083 : (dw_scalar_form_constant
21084 | dw_scalar_form_exprloc
21085 | dw_scalar_form_reference);
21087 add_scalar_info (array_die, attr, info->stride, forms, &context);
21090 if (dwarf_version >= 5)
21092 if (info->rank)
21094 add_scalar_info (array_die, DW_AT_rank, info->rank,
21095 dw_scalar_form_constant
21096 | dw_scalar_form_exprloc, &context);
21097 subrange_tag = DW_TAG_generic_subrange;
21098 context.placeholder_arg = true;
21102 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21104 for (dim = 0; dim < info->ndimensions; dim++)
21106 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21108 if (info->dimen[dim].bounds_type)
21109 add_type_attribute (subrange_die,
21110 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21111 false, context_die);
21112 if (info->dimen[dim].lower_bound)
21113 add_bound_info (subrange_die, DW_AT_lower_bound,
21114 info->dimen[dim].lower_bound, &context);
21115 if (info->dimen[dim].upper_bound)
21116 add_bound_info (subrange_die, DW_AT_upper_bound,
21117 info->dimen[dim].upper_bound, &context);
21118 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21119 add_scalar_info (subrange_die, DW_AT_byte_stride,
21120 info->dimen[dim].stride,
21121 dw_scalar_form_constant
21122 | dw_scalar_form_exprloc
21123 | dw_scalar_form_reference,
21124 &context);
21127 gen_type_die (info->element_type, context_die);
21128 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21129 TREE_CODE (type) == ARRAY_TYPE
21130 && TYPE_REVERSE_STORAGE_ORDER (type),
21131 context_die);
21133 if (get_AT (array_die, DW_AT_name))
21134 add_pubtype (type, array_die);
21136 add_alignment_attribute (array_die, type);
21139 #if 0
21140 static void
21141 gen_entry_point_die (tree decl, dw_die_ref context_die)
21143 tree origin = decl_ultimate_origin (decl);
21144 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21146 if (origin != NULL)
21147 add_abstract_origin_attribute (decl_die, origin);
21148 else
21150 add_name_and_src_coords_attributes (decl_die, decl);
21151 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21152 TYPE_UNQUALIFIED, false, context_die);
21155 if (DECL_ABSTRACT_P (decl))
21156 equate_decl_number_to_die (decl, decl_die);
21157 else
21158 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21160 #endif
21162 /* Walk through the list of incomplete types again, trying once more to
21163 emit full debugging info for them. */
21165 static void
21166 retry_incomplete_types (void)
21168 set_early_dwarf s;
21169 int i;
21171 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21172 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21173 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21174 vec_safe_truncate (incomplete_types, 0);
21177 /* Determine what tag to use for a record type. */
21179 static enum dwarf_tag
21180 record_type_tag (tree type)
21182 if (! lang_hooks.types.classify_record)
21183 return DW_TAG_structure_type;
21185 switch (lang_hooks.types.classify_record (type))
21187 case RECORD_IS_STRUCT:
21188 return DW_TAG_structure_type;
21190 case RECORD_IS_CLASS:
21191 return DW_TAG_class_type;
21193 case RECORD_IS_INTERFACE:
21194 if (dwarf_version >= 3 || !dwarf_strict)
21195 return DW_TAG_interface_type;
21196 return DW_TAG_structure_type;
21198 default:
21199 gcc_unreachable ();
21203 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21204 include all of the information about the enumeration values also. Each
21205 enumerated type name/value is listed as a child of the enumerated type
21206 DIE. */
21208 static dw_die_ref
21209 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21211 dw_die_ref type_die = lookup_type_die (type);
21213 if (type_die == NULL)
21215 type_die = new_die (DW_TAG_enumeration_type,
21216 scope_die_for (type, context_die), type);
21217 equate_type_number_to_die (type, type_die);
21218 add_name_attribute (type_die, type_tag (type));
21219 if (dwarf_version >= 4 || !dwarf_strict)
21221 if (ENUM_IS_SCOPED (type))
21222 add_AT_flag (type_die, DW_AT_enum_class, 1);
21223 if (ENUM_IS_OPAQUE (type))
21224 add_AT_flag (type_die, DW_AT_declaration, 1);
21226 if (!dwarf_strict)
21227 add_AT_unsigned (type_die, DW_AT_encoding,
21228 TYPE_UNSIGNED (type)
21229 ? DW_ATE_unsigned
21230 : DW_ATE_signed);
21232 else if (! TYPE_SIZE (type))
21233 return type_die;
21234 else
21235 remove_AT (type_die, DW_AT_declaration);
21237 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21238 given enum type is incomplete, do not generate the DW_AT_byte_size
21239 attribute or the DW_AT_element_list attribute. */
21240 if (TYPE_SIZE (type))
21242 tree link;
21244 TREE_ASM_WRITTEN (type) = 1;
21245 add_byte_size_attribute (type_die, type);
21246 add_alignment_attribute (type_die, type);
21247 if (dwarf_version >= 3 || !dwarf_strict)
21249 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21250 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21251 context_die);
21253 if (TYPE_STUB_DECL (type) != NULL_TREE)
21255 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21256 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21259 /* If the first reference to this type was as the return type of an
21260 inline function, then it may not have a parent. Fix this now. */
21261 if (type_die->die_parent == NULL)
21262 add_child_die (scope_die_for (type, context_die), type_die);
21264 for (link = TYPE_VALUES (type);
21265 link != NULL; link = TREE_CHAIN (link))
21267 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21268 tree value = TREE_VALUE (link);
21270 add_name_attribute (enum_die,
21271 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21273 if (TREE_CODE (value) == CONST_DECL)
21274 value = DECL_INITIAL (value);
21276 if (simple_type_size_in_bits (TREE_TYPE (value))
21277 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21279 /* For constant forms created by add_AT_unsigned DWARF
21280 consumers (GDB, elfutils, etc.) always zero extend
21281 the value. Only when the actual value is negative
21282 do we need to use add_AT_int to generate a constant
21283 form that can represent negative values. */
21284 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21285 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21286 add_AT_unsigned (enum_die, DW_AT_const_value,
21287 (unsigned HOST_WIDE_INT) val);
21288 else
21289 add_AT_int (enum_die, DW_AT_const_value, val);
21291 else
21292 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21293 that here. TODO: This should be re-worked to use correct
21294 signed/unsigned double tags for all cases. */
21295 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21298 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21299 if (TYPE_ARTIFICIAL (type))
21300 add_AT_flag (type_die, DW_AT_artificial, 1);
21302 else
21303 add_AT_flag (type_die, DW_AT_declaration, 1);
21305 add_pubtype (type, type_die);
21307 return type_die;
21310 /* Generate a DIE to represent either a real live formal parameter decl or to
21311 represent just the type of some formal parameter position in some function
21312 type.
21314 Note that this routine is a bit unusual because its argument may be a
21315 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21316 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21317 node. If it's the former then this function is being called to output a
21318 DIE to represent a formal parameter object (or some inlining thereof). If
21319 it's the latter, then this function is only being called to output a
21320 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21321 argument type of some subprogram type.
21322 If EMIT_NAME_P is true, name and source coordinate attributes
21323 are emitted. */
21325 static dw_die_ref
21326 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21327 dw_die_ref context_die)
21329 tree node_or_origin = node ? node : origin;
21330 tree ultimate_origin;
21331 dw_die_ref parm_die = NULL;
21333 if (DECL_P (node_or_origin))
21335 parm_die = lookup_decl_die (node);
21337 /* If the contexts differ, we may not be talking about the same
21338 thing.
21339 ??? When in LTO the DIE parent is the "abstract" copy and the
21340 context_die is the specification "copy". But this whole block
21341 should eventually be no longer needed. */
21342 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21344 if (!DECL_ABSTRACT_P (node))
21346 /* This can happen when creating an inlined instance, in
21347 which case we need to create a new DIE that will get
21348 annotated with DW_AT_abstract_origin. */
21349 parm_die = NULL;
21351 else
21352 gcc_unreachable ();
21355 if (parm_die && parm_die->die_parent == NULL)
21357 /* Check that parm_die already has the right attributes that
21358 we would have added below. If any attributes are
21359 missing, fall through to add them. */
21360 if (! DECL_ABSTRACT_P (node_or_origin)
21361 && !get_AT (parm_die, DW_AT_location)
21362 && !get_AT (parm_die, DW_AT_const_value))
21363 /* We are missing location info, and are about to add it. */
21365 else
21367 add_child_die (context_die, parm_die);
21368 return parm_die;
21373 /* If we have a previously generated DIE, use it, unless this is an
21374 concrete instance (origin != NULL), in which case we need a new
21375 DIE with a corresponding DW_AT_abstract_origin. */
21376 bool reusing_die;
21377 if (parm_die && origin == NULL)
21378 reusing_die = true;
21379 else
21381 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21382 reusing_die = false;
21385 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21387 case tcc_declaration:
21388 ultimate_origin = decl_ultimate_origin (node_or_origin);
21389 if (node || ultimate_origin)
21390 origin = ultimate_origin;
21392 if (reusing_die)
21393 goto add_location;
21395 if (origin != NULL)
21396 add_abstract_origin_attribute (parm_die, origin);
21397 else if (emit_name_p)
21398 add_name_and_src_coords_attributes (parm_die, node);
21399 if (origin == NULL
21400 || (! DECL_ABSTRACT_P (node_or_origin)
21401 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21402 decl_function_context
21403 (node_or_origin))))
21405 tree type = TREE_TYPE (node_or_origin);
21406 if (decl_by_reference_p (node_or_origin))
21407 add_type_attribute (parm_die, TREE_TYPE (type),
21408 TYPE_UNQUALIFIED,
21409 false, context_die);
21410 else
21411 add_type_attribute (parm_die, type,
21412 decl_quals (node_or_origin),
21413 false, context_die);
21415 if (origin == NULL && DECL_ARTIFICIAL (node))
21416 add_AT_flag (parm_die, DW_AT_artificial, 1);
21417 add_location:
21418 if (node && node != origin)
21419 equate_decl_number_to_die (node, parm_die);
21420 if (! DECL_ABSTRACT_P (node_or_origin))
21421 add_location_or_const_value_attribute (parm_die, node_or_origin,
21422 node == NULL);
21424 break;
21426 case tcc_type:
21427 /* We were called with some kind of a ..._TYPE node. */
21428 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21429 context_die);
21430 break;
21432 default:
21433 gcc_unreachable ();
21436 return parm_die;
21439 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21440 children DW_TAG_formal_parameter DIEs representing the arguments of the
21441 parameter pack.
21443 PARM_PACK must be a function parameter pack.
21444 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21445 must point to the subsequent arguments of the function PACK_ARG belongs to.
21446 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21447 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21448 following the last one for which a DIE was generated. */
21450 static dw_die_ref
21451 gen_formal_parameter_pack_die (tree parm_pack,
21452 tree pack_arg,
21453 dw_die_ref subr_die,
21454 tree *next_arg)
21456 tree arg;
21457 dw_die_ref parm_pack_die;
21459 gcc_assert (parm_pack
21460 && lang_hooks.function_parameter_pack_p (parm_pack)
21461 && subr_die);
21463 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21464 add_src_coords_attributes (parm_pack_die, parm_pack);
21466 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21468 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21469 parm_pack))
21470 break;
21471 gen_formal_parameter_die (arg, NULL,
21472 false /* Don't emit name attribute. */,
21473 parm_pack_die);
21475 if (next_arg)
21476 *next_arg = arg;
21477 return parm_pack_die;
21480 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21481 at the end of an (ANSI prototyped) formal parameters list. */
21483 static void
21484 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21486 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21489 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21490 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21491 parameters as specified in some function type specification (except for
21492 those which appear as part of a function *definition*). */
21494 static void
21495 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21497 tree link;
21498 tree formal_type = NULL;
21499 tree first_parm_type;
21500 tree arg;
21502 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21504 arg = DECL_ARGUMENTS (function_or_method_type);
21505 function_or_method_type = TREE_TYPE (function_or_method_type);
21507 else
21508 arg = NULL_TREE;
21510 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21512 /* Make our first pass over the list of formal parameter types and output a
21513 DW_TAG_formal_parameter DIE for each one. */
21514 for (link = first_parm_type; link; )
21516 dw_die_ref parm_die;
21518 formal_type = TREE_VALUE (link);
21519 if (formal_type == void_type_node)
21520 break;
21522 /* Output a (nameless) DIE to represent the formal parameter itself. */
21523 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21525 parm_die = gen_formal_parameter_die (formal_type, NULL,
21526 true /* Emit name attribute. */,
21527 context_die);
21528 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21529 && link == first_parm_type)
21531 add_AT_flag (parm_die, DW_AT_artificial, 1);
21532 if (dwarf_version >= 3 || !dwarf_strict)
21533 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21535 else if (arg && DECL_ARTIFICIAL (arg))
21536 add_AT_flag (parm_die, DW_AT_artificial, 1);
21539 link = TREE_CHAIN (link);
21540 if (arg)
21541 arg = DECL_CHAIN (arg);
21544 /* If this function type has an ellipsis, add a
21545 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21546 if (formal_type != void_type_node)
21547 gen_unspecified_parameters_die (function_or_method_type, context_die);
21549 /* Make our second (and final) pass over the list of formal parameter types
21550 and output DIEs to represent those types (as necessary). */
21551 for (link = TYPE_ARG_TYPES (function_or_method_type);
21552 link && TREE_VALUE (link);
21553 link = TREE_CHAIN (link))
21554 gen_type_die (TREE_VALUE (link), context_die);
21557 /* We want to generate the DIE for TYPE so that we can generate the
21558 die for MEMBER, which has been defined; we will need to refer back
21559 to the member declaration nested within TYPE. If we're trying to
21560 generate minimal debug info for TYPE, processing TYPE won't do the
21561 trick; we need to attach the member declaration by hand. */
21563 static void
21564 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21566 gen_type_die (type, context_die);
21568 /* If we're trying to avoid duplicate debug info, we may not have
21569 emitted the member decl for this function. Emit it now. */
21570 if (TYPE_STUB_DECL (type)
21571 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21572 && ! lookup_decl_die (member))
21574 dw_die_ref type_die;
21575 gcc_assert (!decl_ultimate_origin (member));
21577 push_decl_scope (type);
21578 type_die = lookup_type_die_strip_naming_typedef (type);
21579 if (TREE_CODE (member) == FUNCTION_DECL)
21580 gen_subprogram_die (member, type_die);
21581 else if (TREE_CODE (member) == FIELD_DECL)
21583 /* Ignore the nameless fields that are used to skip bits but handle
21584 C++ anonymous unions and structs. */
21585 if (DECL_NAME (member) != NULL_TREE
21586 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21587 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21589 struct vlr_context vlr_ctx = {
21590 DECL_CONTEXT (member), /* struct_type */
21591 NULL_TREE /* variant_part_offset */
21593 gen_type_die (member_declared_type (member), type_die);
21594 gen_field_die (member, &vlr_ctx, type_die);
21597 else
21598 gen_variable_die (member, NULL_TREE, type_die);
21600 pop_decl_scope ();
21604 /* Forward declare these functions, because they are mutually recursive
21605 with their set_block_* pairing functions. */
21606 static void set_decl_origin_self (tree);
21608 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21609 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21610 that it points to the node itself, thus indicating that the node is its
21611 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21612 the given node is NULL, recursively descend the decl/block tree which
21613 it is the root of, and for each other ..._DECL or BLOCK node contained
21614 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21615 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21616 values to point to themselves. */
21618 static void
21619 set_block_origin_self (tree stmt)
21621 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21623 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21626 tree local_decl;
21628 for (local_decl = BLOCK_VARS (stmt);
21629 local_decl != NULL_TREE;
21630 local_decl = DECL_CHAIN (local_decl))
21631 /* Do not recurse on nested functions since the inlining status
21632 of parent and child can be different as per the DWARF spec. */
21633 if (TREE_CODE (local_decl) != FUNCTION_DECL
21634 && !DECL_EXTERNAL (local_decl))
21635 set_decl_origin_self (local_decl);
21639 tree subblock;
21641 for (subblock = BLOCK_SUBBLOCKS (stmt);
21642 subblock != NULL_TREE;
21643 subblock = BLOCK_CHAIN (subblock))
21644 set_block_origin_self (subblock); /* Recurse. */
21649 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21650 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21651 node to so that it points to the node itself, thus indicating that the
21652 node represents its own (abstract) origin. Additionally, if the
21653 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21654 the decl/block tree of which the given node is the root of, and for
21655 each other ..._DECL or BLOCK node contained therein whose
21656 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21657 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21658 point to themselves. */
21660 static void
21661 set_decl_origin_self (tree decl)
21663 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21665 DECL_ABSTRACT_ORIGIN (decl) = decl;
21666 if (TREE_CODE (decl) == FUNCTION_DECL)
21668 tree arg;
21670 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21671 DECL_ABSTRACT_ORIGIN (arg) = arg;
21672 if (DECL_INITIAL (decl) != NULL_TREE
21673 && DECL_INITIAL (decl) != error_mark_node)
21674 set_block_origin_self (DECL_INITIAL (decl));
21679 /* Mark the early DIE for DECL as the abstract instance. */
21681 static void
21682 dwarf2out_abstract_function (tree decl)
21684 dw_die_ref old_die;
21686 /* Make sure we have the actual abstract inline, not a clone. */
21687 decl = DECL_ORIGIN (decl);
21689 if (DECL_IGNORED_P (decl))
21690 return;
21692 old_die = lookup_decl_die (decl);
21693 /* With early debug we always have an old DIE unless we are in LTO
21694 and the user did not compile but only link with debug. */
21695 if (in_lto_p && ! old_die)
21696 return;
21697 gcc_assert (old_die != NULL);
21698 if (get_AT (old_die, DW_AT_inline)
21699 || get_AT (old_die, DW_AT_abstract_origin))
21700 /* We've already generated the abstract instance. */
21701 return;
21703 /* Go ahead and put DW_AT_inline on the DIE. */
21704 if (DECL_DECLARED_INLINE_P (decl))
21706 if (cgraph_function_possibly_inlined_p (decl))
21707 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21708 else
21709 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21711 else
21713 if (cgraph_function_possibly_inlined_p (decl))
21714 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21715 else
21716 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21719 if (DECL_DECLARED_INLINE_P (decl)
21720 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21721 add_AT_flag (old_die, DW_AT_artificial, 1);
21723 set_decl_origin_self (decl);
21726 /* Helper function of premark_used_types() which gets called through
21727 htab_traverse.
21729 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21730 marked as unused by prune_unused_types. */
21732 bool
21733 premark_used_types_helper (tree const &type, void *)
21735 dw_die_ref die;
21737 die = lookup_type_die (type);
21738 if (die != NULL)
21739 die->die_perennial_p = 1;
21740 return true;
21743 /* Helper function of premark_types_used_by_global_vars which gets called
21744 through htab_traverse.
21746 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21747 marked as unused by prune_unused_types. The DIE of the type is marked
21748 only if the global variable using the type will actually be emitted. */
21751 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21752 void *)
21754 struct types_used_by_vars_entry *entry;
21755 dw_die_ref die;
21757 entry = (struct types_used_by_vars_entry *) *slot;
21758 gcc_assert (entry->type != NULL
21759 && entry->var_decl != NULL);
21760 die = lookup_type_die (entry->type);
21761 if (die)
21763 /* Ask cgraph if the global variable really is to be emitted.
21764 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21765 varpool_node *node = varpool_node::get (entry->var_decl);
21766 if (node && node->definition)
21768 die->die_perennial_p = 1;
21769 /* Keep the parent DIEs as well. */
21770 while ((die = die->die_parent) && die->die_perennial_p == 0)
21771 die->die_perennial_p = 1;
21774 return 1;
21777 /* Mark all members of used_types_hash as perennial. */
21779 static void
21780 premark_used_types (struct function *fun)
21782 if (fun && fun->used_types_hash)
21783 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21786 /* Mark all members of types_used_by_vars_entry as perennial. */
21788 static void
21789 premark_types_used_by_global_vars (void)
21791 if (types_used_by_vars_hash)
21792 types_used_by_vars_hash
21793 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21796 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21797 for CA_LOC call arg loc node. */
21799 static dw_die_ref
21800 gen_call_site_die (tree decl, dw_die_ref subr_die,
21801 struct call_arg_loc_node *ca_loc)
21803 dw_die_ref stmt_die = NULL, die;
21804 tree block = ca_loc->block;
21806 while (block
21807 && block != DECL_INITIAL (decl)
21808 && TREE_CODE (block) == BLOCK)
21810 stmt_die = BLOCK_DIE (block);
21811 if (stmt_die)
21812 break;
21813 block = BLOCK_SUPERCONTEXT (block);
21815 if (stmt_die == NULL)
21816 stmt_die = subr_die;
21817 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21818 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21819 if (ca_loc->tail_call_p)
21820 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21821 if (ca_loc->symbol_ref)
21823 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21824 if (tdie)
21825 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21826 else
21827 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21828 false);
21830 return die;
21833 /* Generate a DIE to represent a declared function (either file-scope or
21834 block-local). */
21836 static void
21837 gen_subprogram_die (tree decl, dw_die_ref context_die)
21839 tree origin = decl_ultimate_origin (decl);
21840 dw_die_ref subr_die;
21841 dw_die_ref old_die = lookup_decl_die (decl);
21843 /* This function gets called multiple times for different stages of
21844 the debug process. For example, for func() in this code:
21846 namespace S
21848 void func() { ... }
21851 ...we get called 4 times. Twice in early debug and twice in
21852 late debug:
21854 Early debug
21855 -----------
21857 1. Once while generating func() within the namespace. This is
21858 the declaration. The declaration bit below is set, as the
21859 context is the namespace.
21861 A new DIE will be generated with DW_AT_declaration set.
21863 2. Once for func() itself. This is the specification. The
21864 declaration bit below is clear as the context is the CU.
21866 We will use the cached DIE from (1) to create a new DIE with
21867 DW_AT_specification pointing to the declaration in (1).
21869 Late debug via rest_of_handle_final()
21870 -------------------------------------
21872 3. Once generating func() within the namespace. This is also the
21873 declaration, as in (1), but this time we will early exit below
21874 as we have a cached DIE and a declaration needs no additional
21875 annotations (no locations), as the source declaration line
21876 info is enough.
21878 4. Once for func() itself. As in (2), this is the specification,
21879 but this time we will re-use the cached DIE, and just annotate
21880 it with the location information that should now be available.
21882 For something without namespaces, but with abstract instances, we
21883 are also called a multiple times:
21885 class Base
21887 public:
21888 Base (); // constructor declaration (1)
21891 Base::Base () { } // constructor specification (2)
21893 Early debug
21894 -----------
21896 1. Once for the Base() constructor by virtue of it being a
21897 member of the Base class. This is done via
21898 rest_of_type_compilation.
21900 This is a declaration, so a new DIE will be created with
21901 DW_AT_declaration.
21903 2. Once for the Base() constructor definition, but this time
21904 while generating the abstract instance of the base
21905 constructor (__base_ctor) which is being generated via early
21906 debug of reachable functions.
21908 Even though we have a cached version of the declaration (1),
21909 we will create a DW_AT_specification of the declaration DIE
21910 in (1).
21912 3. Once for the __base_ctor itself, but this time, we generate
21913 an DW_AT_abstract_origin version of the DW_AT_specification in
21914 (2).
21916 Late debug via rest_of_handle_final
21917 -----------------------------------
21919 4. One final time for the __base_ctor (which will have a cached
21920 DIE with DW_AT_abstract_origin created in (3). This time,
21921 we will just annotate the location information now
21922 available.
21924 int declaration = (current_function_decl != decl
21925 || class_or_namespace_scope_p (context_die));
21927 /* Now that the C++ front end lazily declares artificial member fns, we
21928 might need to retrofit the declaration into its class. */
21929 if (!declaration && !origin && !old_die
21930 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21931 && !class_or_namespace_scope_p (context_die)
21932 && debug_info_level > DINFO_LEVEL_TERSE)
21933 old_die = force_decl_die (decl);
21935 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21936 if (origin != NULL)
21938 gcc_assert (!declaration || local_scope_p (context_die));
21940 /* Fixup die_parent for the abstract instance of a nested
21941 inline function. */
21942 if (old_die && old_die->die_parent == NULL)
21943 add_child_die (context_die, old_die);
21945 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21947 /* If we have a DW_AT_abstract_origin we have a working
21948 cached version. */
21949 subr_die = old_die;
21951 else
21953 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21954 add_abstract_origin_attribute (subr_die, origin);
21955 /* This is where the actual code for a cloned function is.
21956 Let's emit linkage name attribute for it. This helps
21957 debuggers to e.g, set breakpoints into
21958 constructors/destructors when the user asks "break
21959 K::K". */
21960 add_linkage_name (subr_die, decl);
21963 /* A cached copy, possibly from early dwarf generation. Reuse as
21964 much as possible. */
21965 else if (old_die)
21967 /* A declaration that has been previously dumped needs no
21968 additional information. */
21969 if (declaration)
21970 return;
21972 if (!get_AT_flag (old_die, DW_AT_declaration)
21973 /* We can have a normal definition following an inline one in the
21974 case of redefinition of GNU C extern inlines.
21975 It seems reasonable to use AT_specification in this case. */
21976 && !get_AT (old_die, DW_AT_inline))
21978 /* Detect and ignore this case, where we are trying to output
21979 something we have already output. */
21980 if (get_AT (old_die, DW_AT_low_pc)
21981 || get_AT (old_die, DW_AT_ranges))
21982 return;
21984 /* If we have no location information, this must be a
21985 partially generated DIE from early dwarf generation.
21986 Fall through and generate it. */
21989 /* If the definition comes from the same place as the declaration,
21990 maybe use the old DIE. We always want the DIE for this function
21991 that has the *_pc attributes to be under comp_unit_die so the
21992 debugger can find it. We also need to do this for abstract
21993 instances of inlines, since the spec requires the out-of-line copy
21994 to have the same parent. For local class methods, this doesn't
21995 apply; we just use the old DIE. */
21996 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21997 struct dwarf_file_data * file_index = lookup_filename (s.file);
21998 if ((is_cu_die (old_die->die_parent)
21999 /* This condition fixes the inconsistency/ICE with the
22000 following Fortran test (or some derivative thereof) while
22001 building libgfortran:
22003 module some_m
22004 contains
22005 logical function funky (FLAG)
22006 funky = .true.
22007 end function
22008 end module
22010 || (old_die->die_parent
22011 && old_die->die_parent->die_tag == DW_TAG_module)
22012 || context_die == NULL)
22013 && (DECL_ARTIFICIAL (decl)
22014 /* The location attributes may be in the abstract origin
22015 which in the case of LTO might be not available to
22016 look at. */
22017 || get_AT (old_die, DW_AT_abstract_origin)
22018 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22019 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22020 == (unsigned) s.line)
22021 && (!debug_column_info
22022 || s.column == 0
22023 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22024 == (unsigned) s.column)))))
22026 subr_die = old_die;
22028 /* Clear out the declaration attribute, but leave the
22029 parameters so they can be augmented with location
22030 information later. Unless this was a declaration, in
22031 which case, wipe out the nameless parameters and recreate
22032 them further down. */
22033 if (remove_AT (subr_die, DW_AT_declaration))
22036 remove_AT (subr_die, DW_AT_object_pointer);
22037 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22040 /* Make a specification pointing to the previously built
22041 declaration. */
22042 else
22044 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22045 add_AT_specification (subr_die, old_die);
22046 add_pubname (decl, subr_die);
22047 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22048 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22049 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22050 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22051 if (debug_column_info
22052 && s.column
22053 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22054 != (unsigned) s.column))
22055 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22057 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22058 emit the real type on the definition die. */
22059 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22061 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22062 if (die == auto_die || die == decltype_auto_die)
22063 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22064 TYPE_UNQUALIFIED, false, context_die);
22067 /* When we process the method declaration, we haven't seen
22068 the out-of-class defaulted definition yet, so we have to
22069 recheck now. */
22070 if ((dwarf_version >= 5 || ! dwarf_strict)
22071 && !get_AT (subr_die, DW_AT_defaulted))
22073 int defaulted
22074 = lang_hooks.decls.decl_dwarf_attribute (decl,
22075 DW_AT_defaulted);
22076 if (defaulted != -1)
22078 /* Other values must have been handled before. */
22079 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22080 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22085 /* Create a fresh DIE for anything else. */
22086 else
22088 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22090 if (TREE_PUBLIC (decl))
22091 add_AT_flag (subr_die, DW_AT_external, 1);
22093 add_name_and_src_coords_attributes (subr_die, decl);
22094 add_pubname (decl, subr_die);
22095 if (debug_info_level > DINFO_LEVEL_TERSE)
22097 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22098 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22099 TYPE_UNQUALIFIED, false, context_die);
22102 add_pure_or_virtual_attribute (subr_die, decl);
22103 if (DECL_ARTIFICIAL (decl))
22104 add_AT_flag (subr_die, DW_AT_artificial, 1);
22106 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22107 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22109 add_alignment_attribute (subr_die, decl);
22111 add_accessibility_attribute (subr_die, decl);
22114 /* Unless we have an existing non-declaration DIE, equate the new
22115 DIE. */
22116 if (!old_die || is_declaration_die (old_die))
22117 equate_decl_number_to_die (decl, subr_die);
22119 if (declaration)
22121 if (!old_die || !get_AT (old_die, DW_AT_inline))
22123 add_AT_flag (subr_die, DW_AT_declaration, 1);
22125 /* If this is an explicit function declaration then generate
22126 a DW_AT_explicit attribute. */
22127 if ((dwarf_version >= 3 || !dwarf_strict)
22128 && lang_hooks.decls.decl_dwarf_attribute (decl,
22129 DW_AT_explicit) == 1)
22130 add_AT_flag (subr_die, DW_AT_explicit, 1);
22132 /* If this is a C++11 deleted special function member then generate
22133 a DW_AT_deleted attribute. */
22134 if ((dwarf_version >= 5 || !dwarf_strict)
22135 && lang_hooks.decls.decl_dwarf_attribute (decl,
22136 DW_AT_deleted) == 1)
22137 add_AT_flag (subr_die, DW_AT_deleted, 1);
22139 /* If this is a C++11 defaulted special function member then
22140 generate a DW_AT_defaulted attribute. */
22141 if (dwarf_version >= 5 || !dwarf_strict)
22143 int defaulted
22144 = lang_hooks.decls.decl_dwarf_attribute (decl,
22145 DW_AT_defaulted);
22146 if (defaulted != -1)
22147 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22150 /* If this is a C++11 non-static member function with & ref-qualifier
22151 then generate a DW_AT_reference attribute. */
22152 if ((dwarf_version >= 5 || !dwarf_strict)
22153 && lang_hooks.decls.decl_dwarf_attribute (decl,
22154 DW_AT_reference) == 1)
22155 add_AT_flag (subr_die, DW_AT_reference, 1);
22157 /* If this is a C++11 non-static member function with &&
22158 ref-qualifier then generate a DW_AT_reference attribute. */
22159 if ((dwarf_version >= 5 || !dwarf_strict)
22160 && lang_hooks.decls.decl_dwarf_attribute (decl,
22161 DW_AT_rvalue_reference)
22162 == 1)
22163 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22166 /* For non DECL_EXTERNALs, if range information is available, fill
22167 the DIE with it. */
22168 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22170 HOST_WIDE_INT cfa_fb_offset;
22172 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22174 if (!crtl->has_bb_partition)
22176 dw_fde_ref fde = fun->fde;
22177 if (fde->dw_fde_begin)
22179 /* We have already generated the labels. */
22180 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22181 fde->dw_fde_end, false);
22183 else
22185 /* Create start/end labels and add the range. */
22186 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22187 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22188 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22189 current_function_funcdef_no);
22190 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22191 current_function_funcdef_no);
22192 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22193 false);
22196 #if VMS_DEBUGGING_INFO
22197 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22198 Section 2.3 Prologue and Epilogue Attributes:
22199 When a breakpoint is set on entry to a function, it is generally
22200 desirable for execution to be suspended, not on the very first
22201 instruction of the function, but rather at a point after the
22202 function's frame has been set up, after any language defined local
22203 declaration processing has been completed, and before execution of
22204 the first statement of the function begins. Debuggers generally
22205 cannot properly determine where this point is. Similarly for a
22206 breakpoint set on exit from a function. The prologue and epilogue
22207 attributes allow a compiler to communicate the location(s) to use. */
22210 if (fde->dw_fde_vms_end_prologue)
22211 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22212 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22214 if (fde->dw_fde_vms_begin_epilogue)
22215 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22216 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22218 #endif
22221 else
22223 /* Generate pubnames entries for the split function code ranges. */
22224 dw_fde_ref fde = fun->fde;
22226 if (fde->dw_fde_second_begin)
22228 if (dwarf_version >= 3 || !dwarf_strict)
22230 /* We should use ranges for non-contiguous code section
22231 addresses. Use the actual code range for the initial
22232 section, since the HOT/COLD labels might precede an
22233 alignment offset. */
22234 bool range_list_added = false;
22235 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22236 fde->dw_fde_end, &range_list_added,
22237 false);
22238 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22239 fde->dw_fde_second_end,
22240 &range_list_added, false);
22241 if (range_list_added)
22242 add_ranges (NULL);
22244 else
22246 /* There is no real support in DW2 for this .. so we make
22247 a work-around. First, emit the pub name for the segment
22248 containing the function label. Then make and emit a
22249 simplified subprogram DIE for the second segment with the
22250 name pre-fixed by __hot/cold_sect_of_. We use the same
22251 linkage name for the second die so that gdb will find both
22252 sections when given "b foo". */
22253 const char *name = NULL;
22254 tree decl_name = DECL_NAME (decl);
22255 dw_die_ref seg_die;
22257 /* Do the 'primary' section. */
22258 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22259 fde->dw_fde_end, false);
22261 /* Build a minimal DIE for the secondary section. */
22262 seg_die = new_die (DW_TAG_subprogram,
22263 subr_die->die_parent, decl);
22265 if (TREE_PUBLIC (decl))
22266 add_AT_flag (seg_die, DW_AT_external, 1);
22268 if (decl_name != NULL
22269 && IDENTIFIER_POINTER (decl_name) != NULL)
22271 name = dwarf2_name (decl, 1);
22272 if (! DECL_ARTIFICIAL (decl))
22273 add_src_coords_attributes (seg_die, decl);
22275 add_linkage_name (seg_die, decl);
22277 gcc_assert (name != NULL);
22278 add_pure_or_virtual_attribute (seg_die, decl);
22279 if (DECL_ARTIFICIAL (decl))
22280 add_AT_flag (seg_die, DW_AT_artificial, 1);
22282 name = concat ("__second_sect_of_", name, NULL);
22283 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22284 fde->dw_fde_second_end, false);
22285 add_name_attribute (seg_die, name);
22286 if (want_pubnames ())
22287 add_pubname_string (name, seg_die);
22290 else
22291 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22292 false);
22295 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22297 /* We define the "frame base" as the function's CFA. This is more
22298 convenient for several reasons: (1) It's stable across the prologue
22299 and epilogue, which makes it better than just a frame pointer,
22300 (2) With dwarf3, there exists a one-byte encoding that allows us
22301 to reference the .debug_frame data by proxy, but failing that,
22302 (3) We can at least reuse the code inspection and interpretation
22303 code that determines the CFA position at various points in the
22304 function. */
22305 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22307 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22308 add_AT_loc (subr_die, DW_AT_frame_base, op);
22310 else
22312 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22313 if (list->dw_loc_next)
22314 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22315 else
22316 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22319 /* Compute a displacement from the "steady-state frame pointer" to
22320 the CFA. The former is what all stack slots and argument slots
22321 will reference in the rtl; the latter is what we've told the
22322 debugger about. We'll need to adjust all frame_base references
22323 by this displacement. */
22324 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22326 if (fun->static_chain_decl)
22328 /* DWARF requires here a location expression that computes the
22329 address of the enclosing subprogram's frame base. The machinery
22330 in tree-nested.c is supposed to store this specific address in the
22331 last field of the FRAME record. */
22332 const tree frame_type
22333 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22334 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22336 tree fb_expr
22337 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22338 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22339 fb_expr, fb_decl, NULL_TREE);
22341 add_AT_location_description (subr_die, DW_AT_static_link,
22342 loc_list_from_tree (fb_expr, 0, NULL));
22345 resolve_variable_values ();
22348 /* Generate child dies for template paramaters. */
22349 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22350 gen_generic_params_dies (decl);
22352 /* Now output descriptions of the arguments for this function. This gets
22353 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22354 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22355 `...' at the end of the formal parameter list. In order to find out if
22356 there was a trailing ellipsis or not, we must instead look at the type
22357 associated with the FUNCTION_DECL. This will be a node of type
22358 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22359 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22360 an ellipsis at the end. */
22362 /* In the case where we are describing a mere function declaration, all we
22363 need to do here (and all we *can* do here) is to describe the *types* of
22364 its formal parameters. */
22365 if (debug_info_level <= DINFO_LEVEL_TERSE)
22367 else if (declaration)
22368 gen_formal_types_die (decl, subr_die);
22369 else
22371 /* Generate DIEs to represent all known formal parameters. */
22372 tree parm = DECL_ARGUMENTS (decl);
22373 tree generic_decl = early_dwarf
22374 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22375 tree generic_decl_parm = generic_decl
22376 ? DECL_ARGUMENTS (generic_decl)
22377 : NULL;
22379 /* Now we want to walk the list of parameters of the function and
22380 emit their relevant DIEs.
22382 We consider the case of DECL being an instance of a generic function
22383 as well as it being a normal function.
22385 If DECL is an instance of a generic function we walk the
22386 parameters of the generic function declaration _and_ the parameters of
22387 DECL itself. This is useful because we want to emit specific DIEs for
22388 function parameter packs and those are declared as part of the
22389 generic function declaration. In that particular case,
22390 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22391 That DIE has children DIEs representing the set of arguments
22392 of the pack. Note that the set of pack arguments can be empty.
22393 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22394 children DIE.
22396 Otherwise, we just consider the parameters of DECL. */
22397 while (generic_decl_parm || parm)
22399 if (generic_decl_parm
22400 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22401 gen_formal_parameter_pack_die (generic_decl_parm,
22402 parm, subr_die,
22403 &parm);
22404 else if (parm && !POINTER_BOUNDS_P (parm))
22406 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22408 if (early_dwarf
22409 && parm == DECL_ARGUMENTS (decl)
22410 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22411 && parm_die
22412 && (dwarf_version >= 3 || !dwarf_strict))
22413 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22415 parm = DECL_CHAIN (parm);
22417 else if (parm)
22418 parm = DECL_CHAIN (parm);
22420 if (generic_decl_parm)
22421 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22424 /* Decide whether we need an unspecified_parameters DIE at the end.
22425 There are 2 more cases to do this for: 1) the ansi ... declaration -
22426 this is detectable when the end of the arg list is not a
22427 void_type_node 2) an unprototyped function declaration (not a
22428 definition). This just means that we have no info about the
22429 parameters at all. */
22430 if (early_dwarf)
22432 if (prototype_p (TREE_TYPE (decl)))
22434 /* This is the prototyped case, check for.... */
22435 if (stdarg_p (TREE_TYPE (decl)))
22436 gen_unspecified_parameters_die (decl, subr_die);
22438 else if (DECL_INITIAL (decl) == NULL_TREE)
22439 gen_unspecified_parameters_die (decl, subr_die);
22443 if (subr_die != old_die)
22444 /* Add the calling convention attribute if requested. */
22445 add_calling_convention_attribute (subr_die, decl);
22447 /* Output Dwarf info for all of the stuff within the body of the function
22448 (if it has one - it may be just a declaration).
22450 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22451 a function. This BLOCK actually represents the outermost binding contour
22452 for the function, i.e. the contour in which the function's formal
22453 parameters and labels get declared. Curiously, it appears that the front
22454 end doesn't actually put the PARM_DECL nodes for the current function onto
22455 the BLOCK_VARS list for this outer scope, but are strung off of the
22456 DECL_ARGUMENTS list for the function instead.
22458 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22459 the LABEL_DECL nodes for the function however, and we output DWARF info
22460 for those in decls_for_scope. Just within the `outer_scope' there will be
22461 a BLOCK node representing the function's outermost pair of curly braces,
22462 and any blocks used for the base and member initializers of a C++
22463 constructor function. */
22464 tree outer_scope = DECL_INITIAL (decl);
22465 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22467 int call_site_note_count = 0;
22468 int tail_call_site_note_count = 0;
22470 /* Emit a DW_TAG_variable DIE for a named return value. */
22471 if (DECL_NAME (DECL_RESULT (decl)))
22472 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22474 /* The first time through decls_for_scope we will generate the
22475 DIEs for the locals. The second time, we fill in the
22476 location info. */
22477 decls_for_scope (outer_scope, subr_die);
22479 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22481 struct call_arg_loc_node *ca_loc;
22482 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22484 dw_die_ref die = NULL;
22485 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22486 rtx arg, next_arg;
22488 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22489 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22490 : NULL_RTX);
22491 arg; arg = next_arg)
22493 dw_loc_descr_ref reg, val;
22494 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22495 dw_die_ref cdie, tdie = NULL;
22497 next_arg = XEXP (arg, 1);
22498 if (REG_P (XEXP (XEXP (arg, 0), 0))
22499 && next_arg
22500 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22501 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22502 && REGNO (XEXP (XEXP (arg, 0), 0))
22503 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22504 next_arg = XEXP (next_arg, 1);
22505 if (mode == VOIDmode)
22507 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22508 if (mode == VOIDmode)
22509 mode = GET_MODE (XEXP (arg, 0));
22511 if (mode == VOIDmode || mode == BLKmode)
22512 continue;
22513 /* Get dynamic information about call target only if we
22514 have no static information: we cannot generate both
22515 DW_AT_call_origin and DW_AT_call_target
22516 attributes. */
22517 if (ca_loc->symbol_ref == NULL_RTX)
22519 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22521 tloc = XEXP (XEXP (arg, 0), 1);
22522 continue;
22524 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22525 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22527 tlocc = XEXP (XEXP (arg, 0), 1);
22528 continue;
22531 reg = NULL;
22532 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22533 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22534 VAR_INIT_STATUS_INITIALIZED);
22535 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22537 rtx mem = XEXP (XEXP (arg, 0), 0);
22538 reg = mem_loc_descriptor (XEXP (mem, 0),
22539 get_address_mode (mem),
22540 GET_MODE (mem),
22541 VAR_INIT_STATUS_INITIALIZED);
22543 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22544 == DEBUG_PARAMETER_REF)
22546 tree tdecl
22547 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22548 tdie = lookup_decl_die (tdecl);
22549 if (tdie == NULL)
22550 continue;
22552 else
22553 continue;
22554 if (reg == NULL
22555 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22556 != DEBUG_PARAMETER_REF)
22557 continue;
22558 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22559 VOIDmode,
22560 VAR_INIT_STATUS_INITIALIZED);
22561 if (val == NULL)
22562 continue;
22563 if (die == NULL)
22564 die = gen_call_site_die (decl, subr_die, ca_loc);
22565 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22566 NULL_TREE);
22567 if (reg != NULL)
22568 add_AT_loc (cdie, DW_AT_location, reg);
22569 else if (tdie != NULL)
22570 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22571 tdie);
22572 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22573 if (next_arg != XEXP (arg, 1))
22575 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22576 if (mode == VOIDmode)
22577 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22578 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22579 0), 1),
22580 mode, VOIDmode,
22581 VAR_INIT_STATUS_INITIALIZED);
22582 if (val != NULL)
22583 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22584 val);
22587 if (die == NULL
22588 && (ca_loc->symbol_ref || tloc))
22589 die = gen_call_site_die (decl, subr_die, ca_loc);
22590 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22592 dw_loc_descr_ref tval = NULL;
22594 if (tloc != NULL_RTX)
22595 tval = mem_loc_descriptor (tloc,
22596 GET_MODE (tloc) == VOIDmode
22597 ? Pmode : GET_MODE (tloc),
22598 VOIDmode,
22599 VAR_INIT_STATUS_INITIALIZED);
22600 if (tval)
22601 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22602 else if (tlocc != NULL_RTX)
22604 tval = mem_loc_descriptor (tlocc,
22605 GET_MODE (tlocc) == VOIDmode
22606 ? Pmode : GET_MODE (tlocc),
22607 VOIDmode,
22608 VAR_INIT_STATUS_INITIALIZED);
22609 if (tval)
22610 add_AT_loc (die,
22611 dwarf_AT (DW_AT_call_target_clobbered),
22612 tval);
22615 if (die != NULL)
22617 call_site_note_count++;
22618 if (ca_loc->tail_call_p)
22619 tail_call_site_note_count++;
22623 call_arg_locations = NULL;
22624 call_arg_loc_last = NULL;
22625 if (tail_call_site_count >= 0
22626 && tail_call_site_count == tail_call_site_note_count
22627 && (!dwarf_strict || dwarf_version >= 5))
22629 if (call_site_count >= 0
22630 && call_site_count == call_site_note_count)
22631 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22632 else
22633 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22635 call_site_count = -1;
22636 tail_call_site_count = -1;
22639 /* Mark used types after we have created DIEs for the functions scopes. */
22640 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22643 /* Returns a hash value for X (which really is a die_struct). */
22645 hashval_t
22646 block_die_hasher::hash (die_struct *d)
22648 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22651 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22652 as decl_id and die_parent of die_struct Y. */
22654 bool
22655 block_die_hasher::equal (die_struct *x, die_struct *y)
22657 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22660 /* Return TRUE if DECL, which may have been previously generated as
22661 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22662 true if decl (or its origin) is either an extern declaration or a
22663 class/namespace scoped declaration.
22665 The declare_in_namespace support causes us to get two DIEs for one
22666 variable, both of which are declarations. We want to avoid
22667 considering one to be a specification, so we must test for
22668 DECLARATION and DW_AT_declaration. */
22669 static inline bool
22670 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22672 return (old_die && TREE_STATIC (decl) && !declaration
22673 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22676 /* Return true if DECL is a local static. */
22678 static inline bool
22679 local_function_static (tree decl)
22681 gcc_assert (VAR_P (decl));
22682 return TREE_STATIC (decl)
22683 && DECL_CONTEXT (decl)
22684 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22687 /* Generate a DIE to represent a declared data object.
22688 Either DECL or ORIGIN must be non-null. */
22690 static void
22691 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22693 HOST_WIDE_INT off = 0;
22694 tree com_decl;
22695 tree decl_or_origin = decl ? decl : origin;
22696 tree ultimate_origin;
22697 dw_die_ref var_die;
22698 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22699 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22700 || class_or_namespace_scope_p (context_die));
22701 bool specialization_p = false;
22702 bool no_linkage_name = false;
22704 /* While C++ inline static data members have definitions inside of the
22705 class, force the first DIE to be a declaration, then let gen_member_die
22706 reparent it to the class context and call gen_variable_die again
22707 to create the outside of the class DIE for the definition. */
22708 if (!declaration
22709 && old_die == NULL
22710 && decl
22711 && DECL_CONTEXT (decl)
22712 && TYPE_P (DECL_CONTEXT (decl))
22713 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22715 declaration = true;
22716 if (dwarf_version < 5)
22717 no_linkage_name = true;
22720 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22721 if (decl || ultimate_origin)
22722 origin = ultimate_origin;
22723 com_decl = fortran_common (decl_or_origin, &off);
22725 /* Symbol in common gets emitted as a child of the common block, in the form
22726 of a data member. */
22727 if (com_decl)
22729 dw_die_ref com_die;
22730 dw_loc_list_ref loc = NULL;
22731 die_node com_die_arg;
22733 var_die = lookup_decl_die (decl_or_origin);
22734 if (var_die)
22736 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22738 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22739 if (loc)
22741 if (off)
22743 /* Optimize the common case. */
22744 if (single_element_loc_list_p (loc)
22745 && loc->expr->dw_loc_opc == DW_OP_addr
22746 && loc->expr->dw_loc_next == NULL
22747 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22748 == SYMBOL_REF)
22750 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22751 loc->expr->dw_loc_oprnd1.v.val_addr
22752 = plus_constant (GET_MODE (x), x , off);
22754 else
22755 loc_list_plus_const (loc, off);
22757 add_AT_location_description (var_die, DW_AT_location, loc);
22758 remove_AT (var_die, DW_AT_declaration);
22761 return;
22764 if (common_block_die_table == NULL)
22765 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22767 com_die_arg.decl_id = DECL_UID (com_decl);
22768 com_die_arg.die_parent = context_die;
22769 com_die = common_block_die_table->find (&com_die_arg);
22770 if (! early_dwarf)
22771 loc = loc_list_from_tree (com_decl, 2, NULL);
22772 if (com_die == NULL)
22774 const char *cnam
22775 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22776 die_node **slot;
22778 com_die = new_die (DW_TAG_common_block, context_die, decl);
22779 add_name_and_src_coords_attributes (com_die, com_decl);
22780 if (loc)
22782 add_AT_location_description (com_die, DW_AT_location, loc);
22783 /* Avoid sharing the same loc descriptor between
22784 DW_TAG_common_block and DW_TAG_variable. */
22785 loc = loc_list_from_tree (com_decl, 2, NULL);
22787 else if (DECL_EXTERNAL (decl_or_origin))
22788 add_AT_flag (com_die, DW_AT_declaration, 1);
22789 if (want_pubnames ())
22790 add_pubname_string (cnam, com_die); /* ??? needed? */
22791 com_die->decl_id = DECL_UID (com_decl);
22792 slot = common_block_die_table->find_slot (com_die, INSERT);
22793 *slot = com_die;
22795 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22797 add_AT_location_description (com_die, DW_AT_location, loc);
22798 loc = loc_list_from_tree (com_decl, 2, NULL);
22799 remove_AT (com_die, DW_AT_declaration);
22801 var_die = new_die (DW_TAG_variable, com_die, decl);
22802 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22803 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22804 decl_quals (decl_or_origin), false,
22805 context_die);
22806 add_alignment_attribute (var_die, decl);
22807 add_AT_flag (var_die, DW_AT_external, 1);
22808 if (loc)
22810 if (off)
22812 /* Optimize the common case. */
22813 if (single_element_loc_list_p (loc)
22814 && loc->expr->dw_loc_opc == DW_OP_addr
22815 && loc->expr->dw_loc_next == NULL
22816 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22818 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22819 loc->expr->dw_loc_oprnd1.v.val_addr
22820 = plus_constant (GET_MODE (x), x, off);
22822 else
22823 loc_list_plus_const (loc, off);
22825 add_AT_location_description (var_die, DW_AT_location, loc);
22827 else if (DECL_EXTERNAL (decl_or_origin))
22828 add_AT_flag (var_die, DW_AT_declaration, 1);
22829 if (decl)
22830 equate_decl_number_to_die (decl, var_die);
22831 return;
22834 if (old_die)
22836 if (declaration)
22838 /* A declaration that has been previously dumped, needs no
22839 further annotations, since it doesn't need location on
22840 the second pass. */
22841 return;
22843 else if (decl_will_get_specification_p (old_die, decl, declaration)
22844 && !get_AT (old_die, DW_AT_specification))
22846 /* Fall-thru so we can make a new variable die along with a
22847 DW_AT_specification. */
22849 else if (origin && old_die->die_parent != context_die)
22851 /* If we will be creating an inlined instance, we need a
22852 new DIE that will get annotated with
22853 DW_AT_abstract_origin. Clear things so we can get a
22854 new DIE. */
22855 gcc_assert (!DECL_ABSTRACT_P (decl));
22856 old_die = NULL;
22858 else
22860 /* If a DIE was dumped early, it still needs location info.
22861 Skip to where we fill the location bits. */
22862 var_die = old_die;
22864 /* ??? In LTRANS we cannot annotate early created variably
22865 modified type DIEs without copying them and adjusting all
22866 references to them. Thus we dumped them again, also add a
22867 reference to them. */
22868 tree type = TREE_TYPE (decl_or_origin);
22869 if (in_lto_p
22870 && variably_modified_type_p
22871 (type, decl_function_context (decl_or_origin)))
22873 if (decl_by_reference_p (decl_or_origin))
22874 add_type_attribute (var_die, TREE_TYPE (type),
22875 TYPE_UNQUALIFIED, false, context_die);
22876 else
22877 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22878 false, context_die);
22881 goto gen_variable_die_location;
22885 /* For static data members, the declaration in the class is supposed
22886 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22887 also in DWARF2; the specification should still be DW_TAG_variable
22888 referencing the DW_TAG_member DIE. */
22889 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22890 var_die = new_die (DW_TAG_member, context_die, decl);
22891 else
22892 var_die = new_die (DW_TAG_variable, context_die, decl);
22894 if (origin != NULL)
22895 add_abstract_origin_attribute (var_die, origin);
22897 /* Loop unrolling can create multiple blocks that refer to the same
22898 static variable, so we must test for the DW_AT_declaration flag.
22900 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22901 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22902 sharing them.
22904 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22905 else if (decl_will_get_specification_p (old_die, decl, declaration))
22907 /* This is a definition of a C++ class level static. */
22908 add_AT_specification (var_die, old_die);
22909 specialization_p = true;
22910 if (DECL_NAME (decl))
22912 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22913 struct dwarf_file_data * file_index = lookup_filename (s.file);
22915 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22916 add_AT_file (var_die, DW_AT_decl_file, file_index);
22918 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22919 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22921 if (debug_column_info
22922 && s.column
22923 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22924 != (unsigned) s.column))
22925 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22927 if (old_die->die_tag == DW_TAG_member)
22928 add_linkage_name (var_die, decl);
22931 else
22932 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22934 if ((origin == NULL && !specialization_p)
22935 || (origin != NULL
22936 && !DECL_ABSTRACT_P (decl_or_origin)
22937 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22938 decl_function_context
22939 (decl_or_origin))))
22941 tree type = TREE_TYPE (decl_or_origin);
22943 if (decl_by_reference_p (decl_or_origin))
22944 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22945 context_die);
22946 else
22947 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22948 context_die);
22951 if (origin == NULL && !specialization_p)
22953 if (TREE_PUBLIC (decl))
22954 add_AT_flag (var_die, DW_AT_external, 1);
22956 if (DECL_ARTIFICIAL (decl))
22957 add_AT_flag (var_die, DW_AT_artificial, 1);
22959 add_alignment_attribute (var_die, decl);
22961 add_accessibility_attribute (var_die, decl);
22964 if (declaration)
22965 add_AT_flag (var_die, DW_AT_declaration, 1);
22967 if (decl && (DECL_ABSTRACT_P (decl)
22968 || !old_die || is_declaration_die (old_die)))
22969 equate_decl_number_to_die (decl, var_die);
22971 gen_variable_die_location:
22972 if (! declaration
22973 && (! DECL_ABSTRACT_P (decl_or_origin)
22974 /* Local static vars are shared between all clones/inlines,
22975 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22976 already set. */
22977 || (VAR_P (decl_or_origin)
22978 && TREE_STATIC (decl_or_origin)
22979 && DECL_RTL_SET_P (decl_or_origin))))
22981 if (early_dwarf)
22982 add_pubname (decl_or_origin, var_die);
22983 else
22984 add_location_or_const_value_attribute (var_die, decl_or_origin,
22985 decl == NULL);
22987 else
22988 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22990 if ((dwarf_version >= 4 || !dwarf_strict)
22991 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22992 DW_AT_const_expr) == 1
22993 && !get_AT (var_die, DW_AT_const_expr)
22994 && !specialization_p)
22995 add_AT_flag (var_die, DW_AT_const_expr, 1);
22997 if (!dwarf_strict)
22999 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23000 DW_AT_inline);
23001 if (inl != -1
23002 && !get_AT (var_die, DW_AT_inline)
23003 && !specialization_p)
23004 add_AT_unsigned (var_die, DW_AT_inline, inl);
23008 /* Generate a DIE to represent a named constant. */
23010 static void
23011 gen_const_die (tree decl, dw_die_ref context_die)
23013 dw_die_ref const_die;
23014 tree type = TREE_TYPE (decl);
23016 const_die = lookup_decl_die (decl);
23017 if (const_die)
23018 return;
23020 const_die = new_die (DW_TAG_constant, context_die, decl);
23021 equate_decl_number_to_die (decl, const_die);
23022 add_name_and_src_coords_attributes (const_die, decl);
23023 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23024 if (TREE_PUBLIC (decl))
23025 add_AT_flag (const_die, DW_AT_external, 1);
23026 if (DECL_ARTIFICIAL (decl))
23027 add_AT_flag (const_die, DW_AT_artificial, 1);
23028 tree_add_const_value_attribute_for_decl (const_die, decl);
23031 /* Generate a DIE to represent a label identifier. */
23033 static void
23034 gen_label_die (tree decl, dw_die_ref context_die)
23036 tree origin = decl_ultimate_origin (decl);
23037 dw_die_ref lbl_die = lookup_decl_die (decl);
23038 rtx insn;
23039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23041 if (!lbl_die)
23043 lbl_die = new_die (DW_TAG_label, context_die, decl);
23044 equate_decl_number_to_die (decl, lbl_die);
23046 if (origin != NULL)
23047 add_abstract_origin_attribute (lbl_die, origin);
23048 else
23049 add_name_and_src_coords_attributes (lbl_die, decl);
23052 if (DECL_ABSTRACT_P (decl))
23053 equate_decl_number_to_die (decl, lbl_die);
23054 else if (! early_dwarf)
23056 insn = DECL_RTL_IF_SET (decl);
23058 /* Deleted labels are programmer specified labels which have been
23059 eliminated because of various optimizations. We still emit them
23060 here so that it is possible to put breakpoints on them. */
23061 if (insn
23062 && (LABEL_P (insn)
23063 || ((NOTE_P (insn)
23064 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23066 /* When optimization is enabled (via -O) some parts of the compiler
23067 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23068 represent source-level labels which were explicitly declared by
23069 the user. This really shouldn't be happening though, so catch
23070 it if it ever does happen. */
23071 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23073 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23074 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23076 else if (insn
23077 && NOTE_P (insn)
23078 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23079 && CODE_LABEL_NUMBER (insn) != -1)
23081 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23082 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23087 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23088 attributes to the DIE for a block STMT, to describe where the inlined
23089 function was called from. This is similar to add_src_coords_attributes. */
23091 static inline void
23092 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23094 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23096 if (dwarf_version >= 3 || !dwarf_strict)
23098 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23099 add_AT_unsigned (die, DW_AT_call_line, s.line);
23100 if (debug_column_info && s.column)
23101 add_AT_unsigned (die, DW_AT_call_column, s.column);
23106 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23107 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23109 static inline void
23110 add_high_low_attributes (tree stmt, dw_die_ref die)
23112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23114 if (BLOCK_FRAGMENT_CHAIN (stmt)
23115 && (dwarf_version >= 3 || !dwarf_strict))
23117 tree chain, superblock = NULL_TREE;
23118 dw_die_ref pdie;
23119 dw_attr_node *attr = NULL;
23121 if (inlined_function_outer_scope_p (stmt))
23123 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23124 BLOCK_NUMBER (stmt));
23125 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23128 /* Optimize duplicate .debug_ranges lists or even tails of
23129 lists. If this BLOCK has same ranges as its supercontext,
23130 lookup DW_AT_ranges attribute in the supercontext (and
23131 recursively so), verify that the ranges_table contains the
23132 right values and use it instead of adding a new .debug_range. */
23133 for (chain = stmt, pdie = die;
23134 BLOCK_SAME_RANGE (chain);
23135 chain = BLOCK_SUPERCONTEXT (chain))
23137 dw_attr_node *new_attr;
23139 pdie = pdie->die_parent;
23140 if (pdie == NULL)
23141 break;
23142 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23143 break;
23144 new_attr = get_AT (pdie, DW_AT_ranges);
23145 if (new_attr == NULL
23146 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23147 break;
23148 attr = new_attr;
23149 superblock = BLOCK_SUPERCONTEXT (chain);
23151 if (attr != NULL
23152 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23153 == BLOCK_NUMBER (superblock))
23154 && BLOCK_FRAGMENT_CHAIN (superblock))
23156 unsigned long off = attr->dw_attr_val.v.val_offset;
23157 unsigned long supercnt = 0, thiscnt = 0;
23158 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23159 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23161 ++supercnt;
23162 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23163 == BLOCK_NUMBER (chain));
23165 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23166 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23167 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23168 ++thiscnt;
23169 gcc_assert (supercnt >= thiscnt);
23170 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23171 false);
23172 note_rnglist_head (off + supercnt - thiscnt);
23173 return;
23176 unsigned int offset = add_ranges (stmt, true);
23177 add_AT_range_list (die, DW_AT_ranges, offset, false);
23178 note_rnglist_head (offset);
23180 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23181 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23184 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23185 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23186 chain = BLOCK_FRAGMENT_CHAIN (chain);
23188 while (chain);
23189 add_ranges (NULL);
23191 else
23193 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23194 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23195 BLOCK_NUMBER (stmt));
23196 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23197 BLOCK_NUMBER (stmt));
23198 add_AT_low_high_pc (die, label, label_high, false);
23202 /* Generate a DIE for a lexical block. */
23204 static void
23205 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23207 dw_die_ref old_die = BLOCK_DIE (stmt);
23208 dw_die_ref stmt_die = NULL;
23209 if (!old_die)
23211 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23212 BLOCK_DIE (stmt) = stmt_die;
23215 if (BLOCK_ABSTRACT (stmt))
23217 if (old_die)
23219 /* This must have been generated early and it won't even
23220 need location information since it's a DW_AT_inline
23221 function. */
23222 if (flag_checking)
23223 for (dw_die_ref c = context_die; c; c = c->die_parent)
23224 if (c->die_tag == DW_TAG_inlined_subroutine
23225 || c->die_tag == DW_TAG_subprogram)
23227 gcc_assert (get_AT (c, DW_AT_inline));
23228 break;
23230 return;
23233 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23235 /* If this is an inlined instance, create a new lexical die for
23236 anything below to attach DW_AT_abstract_origin to. */
23237 if (old_die)
23239 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23240 BLOCK_DIE (stmt) = stmt_die;
23241 old_die = NULL;
23244 tree origin = block_ultimate_origin (stmt);
23245 if (origin != NULL_TREE && origin != stmt)
23246 add_abstract_origin_attribute (stmt_die, origin);
23249 if (old_die)
23250 stmt_die = old_die;
23252 /* A non abstract block whose blocks have already been reordered
23253 should have the instruction range for this block. If so, set the
23254 high/low attributes. */
23255 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23257 gcc_assert (stmt_die);
23258 add_high_low_attributes (stmt, stmt_die);
23261 decls_for_scope (stmt, stmt_die);
23264 /* Generate a DIE for an inlined subprogram. */
23266 static void
23267 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23269 tree decl;
23271 /* The instance of function that is effectively being inlined shall not
23272 be abstract. */
23273 gcc_assert (! BLOCK_ABSTRACT (stmt));
23275 decl = block_ultimate_origin (stmt);
23277 /* Make sure any inlined functions are known to be inlineable. */
23278 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23279 || cgraph_function_possibly_inlined_p (decl));
23281 if (! BLOCK_ABSTRACT (stmt))
23283 dw_die_ref subr_die
23284 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23286 if (call_arg_locations)
23287 BLOCK_DIE (stmt) = subr_die;
23288 add_abstract_origin_attribute (subr_die, decl);
23289 if (TREE_ASM_WRITTEN (stmt))
23290 add_high_low_attributes (stmt, subr_die);
23291 add_call_src_coords_attributes (stmt, subr_die);
23293 decls_for_scope (stmt, subr_die);
23297 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23298 the comment for VLR_CONTEXT. */
23300 static void
23301 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23303 dw_die_ref decl_die;
23305 if (TREE_TYPE (decl) == error_mark_node)
23306 return;
23308 decl_die = new_die (DW_TAG_member, context_die, decl);
23309 add_name_and_src_coords_attributes (decl_die, decl);
23310 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23311 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23312 context_die);
23314 if (DECL_BIT_FIELD_TYPE (decl))
23316 add_byte_size_attribute (decl_die, decl);
23317 add_bit_size_attribute (decl_die, decl);
23318 add_bit_offset_attribute (decl_die, decl, ctx);
23321 add_alignment_attribute (decl_die, decl);
23323 /* If we have a variant part offset, then we are supposed to process a member
23324 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23325 trees. */
23326 gcc_assert (ctx->variant_part_offset == NULL_TREE
23327 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23328 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23329 add_data_member_location_attribute (decl_die, decl, ctx);
23331 if (DECL_ARTIFICIAL (decl))
23332 add_AT_flag (decl_die, DW_AT_artificial, 1);
23334 add_accessibility_attribute (decl_die, decl);
23336 /* Equate decl number to die, so that we can look up this decl later on. */
23337 equate_decl_number_to_die (decl, decl_die);
23340 /* Generate a DIE for a pointer to a member type. TYPE can be an
23341 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23342 pointer to member function. */
23344 static void
23345 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23347 if (lookup_type_die (type))
23348 return;
23350 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23351 scope_die_for (type, context_die), type);
23353 equate_type_number_to_die (type, ptr_die);
23354 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23355 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23356 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23357 context_die);
23358 add_alignment_attribute (ptr_die, type);
23360 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23361 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23363 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23364 add_AT_loc (ptr_die, DW_AT_use_location, op);
23368 static char *producer_string;
23370 /* Return a heap allocated producer string including command line options
23371 if -grecord-gcc-switches. */
23373 static char *
23374 gen_producer_string (void)
23376 size_t j;
23377 auto_vec<const char *> switches;
23378 const char *language_string = lang_hooks.name;
23379 char *producer, *tail;
23380 const char *p;
23381 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23382 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23384 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23385 switch (save_decoded_options[j].opt_index)
23387 case OPT_o:
23388 case OPT_d:
23389 case OPT_dumpbase:
23390 case OPT_dumpdir:
23391 case OPT_auxbase:
23392 case OPT_auxbase_strip:
23393 case OPT_quiet:
23394 case OPT_version:
23395 case OPT_v:
23396 case OPT_w:
23397 case OPT_L:
23398 case OPT_D:
23399 case OPT_I:
23400 case OPT_U:
23401 case OPT_SPECIAL_unknown:
23402 case OPT_SPECIAL_ignore:
23403 case OPT_SPECIAL_program_name:
23404 case OPT_SPECIAL_input_file:
23405 case OPT_grecord_gcc_switches:
23406 case OPT__output_pch_:
23407 case OPT_fdiagnostics_show_location_:
23408 case OPT_fdiagnostics_show_option:
23409 case OPT_fdiagnostics_show_caret:
23410 case OPT_fdiagnostics_color_:
23411 case OPT_fverbose_asm:
23412 case OPT____:
23413 case OPT__sysroot_:
23414 case OPT_nostdinc:
23415 case OPT_nostdinc__:
23416 case OPT_fpreprocessed:
23417 case OPT_fltrans_output_list_:
23418 case OPT_fresolution_:
23419 case OPT_fdebug_prefix_map_:
23420 case OPT_fcompare_debug:
23421 /* Ignore these. */
23422 continue;
23423 default:
23424 if (cl_options[save_decoded_options[j].opt_index].flags
23425 & CL_NO_DWARF_RECORD)
23426 continue;
23427 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23428 == '-');
23429 switch (save_decoded_options[j].canonical_option[0][1])
23431 case 'M':
23432 case 'i':
23433 case 'W':
23434 continue;
23435 case 'f':
23436 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23437 "dump", 4) == 0)
23438 continue;
23439 break;
23440 default:
23441 break;
23443 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23444 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23445 break;
23448 producer = XNEWVEC (char, plen + 1 + len + 1);
23449 tail = producer;
23450 sprintf (tail, "%s %s", language_string, version_string);
23451 tail += plen;
23453 FOR_EACH_VEC_ELT (switches, j, p)
23455 len = strlen (p);
23456 *tail = ' ';
23457 memcpy (tail + 1, p, len);
23458 tail += len + 1;
23461 *tail = '\0';
23462 return producer;
23465 /* Given a C and/or C++ language/version string return the "highest".
23466 C++ is assumed to be "higher" than C in this case. Used for merging
23467 LTO translation unit languages. */
23468 static const char *
23469 highest_c_language (const char *lang1, const char *lang2)
23471 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23472 return "GNU C++17";
23473 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23474 return "GNU C++14";
23475 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23476 return "GNU C++11";
23477 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23478 return "GNU C++98";
23480 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23481 return "GNU C17";
23482 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23483 return "GNU C11";
23484 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23485 return "GNU C99";
23486 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23487 return "GNU C89";
23489 gcc_unreachable ();
23493 /* Generate the DIE for the compilation unit. */
23495 static dw_die_ref
23496 gen_compile_unit_die (const char *filename)
23498 dw_die_ref die;
23499 const char *language_string = lang_hooks.name;
23500 int language;
23502 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23504 if (filename)
23506 add_name_attribute (die, filename);
23507 /* Don't add cwd for <built-in>. */
23508 if (filename[0] != '<')
23509 add_comp_dir_attribute (die);
23512 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23514 /* If our producer is LTO try to figure out a common language to use
23515 from the global list of translation units. */
23516 if (strcmp (language_string, "GNU GIMPLE") == 0)
23518 unsigned i;
23519 tree t;
23520 const char *common_lang = NULL;
23522 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23524 if (!TRANSLATION_UNIT_LANGUAGE (t))
23525 continue;
23526 if (!common_lang)
23527 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23528 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23530 else if (strncmp (common_lang, "GNU C", 5) == 0
23531 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23532 /* Mixing C and C++ is ok, use C++ in that case. */
23533 common_lang = highest_c_language (common_lang,
23534 TRANSLATION_UNIT_LANGUAGE (t));
23535 else
23537 /* Fall back to C. */
23538 common_lang = NULL;
23539 break;
23543 if (common_lang)
23544 language_string = common_lang;
23547 language = DW_LANG_C;
23548 if (strncmp (language_string, "GNU C", 5) == 0
23549 && ISDIGIT (language_string[5]))
23551 language = DW_LANG_C89;
23552 if (dwarf_version >= 3 || !dwarf_strict)
23554 if (strcmp (language_string, "GNU C89") != 0)
23555 language = DW_LANG_C99;
23557 if (dwarf_version >= 5 /* || !dwarf_strict */)
23558 if (strcmp (language_string, "GNU C11") == 0
23559 || strcmp (language_string, "GNU C17") == 0)
23560 language = DW_LANG_C11;
23563 else if (strncmp (language_string, "GNU C++", 7) == 0)
23565 language = DW_LANG_C_plus_plus;
23566 if (dwarf_version >= 5 /* || !dwarf_strict */)
23568 if (strcmp (language_string, "GNU C++11") == 0)
23569 language = DW_LANG_C_plus_plus_11;
23570 else if (strcmp (language_string, "GNU C++14") == 0)
23571 language = DW_LANG_C_plus_plus_14;
23572 else if (strcmp (language_string, "GNU C++17") == 0)
23573 /* For now. */
23574 language = DW_LANG_C_plus_plus_14;
23577 else if (strcmp (language_string, "GNU F77") == 0)
23578 language = DW_LANG_Fortran77;
23579 else if (dwarf_version >= 3 || !dwarf_strict)
23581 if (strcmp (language_string, "GNU Ada") == 0)
23582 language = DW_LANG_Ada95;
23583 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23585 language = DW_LANG_Fortran95;
23586 if (dwarf_version >= 5 /* || !dwarf_strict */)
23588 if (strcmp (language_string, "GNU Fortran2003") == 0)
23589 language = DW_LANG_Fortran03;
23590 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23591 language = DW_LANG_Fortran08;
23594 else if (strcmp (language_string, "GNU Objective-C") == 0)
23595 language = DW_LANG_ObjC;
23596 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23597 language = DW_LANG_ObjC_plus_plus;
23598 else if (dwarf_version >= 5 || !dwarf_strict)
23600 if (strcmp (language_string, "GNU Go") == 0)
23601 language = DW_LANG_Go;
23604 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23605 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23606 language = DW_LANG_Fortran90;
23608 add_AT_unsigned (die, DW_AT_language, language);
23610 switch (language)
23612 case DW_LANG_Fortran77:
23613 case DW_LANG_Fortran90:
23614 case DW_LANG_Fortran95:
23615 case DW_LANG_Fortran03:
23616 case DW_LANG_Fortran08:
23617 /* Fortran has case insensitive identifiers and the front-end
23618 lowercases everything. */
23619 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23620 break;
23621 default:
23622 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23623 break;
23625 return die;
23628 /* Generate the DIE for a base class. */
23630 static void
23631 gen_inheritance_die (tree binfo, tree access, tree type,
23632 dw_die_ref context_die)
23634 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23635 struct vlr_context ctx = { type, NULL };
23637 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23638 context_die);
23639 add_data_member_location_attribute (die, binfo, &ctx);
23641 if (BINFO_VIRTUAL_P (binfo))
23642 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23644 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23645 children, otherwise the default is DW_ACCESS_public. In DWARF2
23646 the default has always been DW_ACCESS_private. */
23647 if (access == access_public_node)
23649 if (dwarf_version == 2
23650 || context_die->die_tag == DW_TAG_class_type)
23651 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23653 else if (access == access_protected_node)
23654 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23655 else if (dwarf_version > 2
23656 && context_die->die_tag != DW_TAG_class_type)
23657 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23660 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23661 structure. */
23662 static bool
23663 is_variant_part (tree decl)
23665 return (TREE_CODE (decl) == FIELD_DECL
23666 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23669 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23670 return the FIELD_DECL. Return NULL_TREE otherwise. */
23672 static tree
23673 analyze_discr_in_predicate (tree operand, tree struct_type)
23675 bool continue_stripping = true;
23676 while (continue_stripping)
23677 switch (TREE_CODE (operand))
23679 CASE_CONVERT:
23680 operand = TREE_OPERAND (operand, 0);
23681 break;
23682 default:
23683 continue_stripping = false;
23684 break;
23687 /* Match field access to members of struct_type only. */
23688 if (TREE_CODE (operand) == COMPONENT_REF
23689 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23690 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23691 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23692 return TREE_OPERAND (operand, 1);
23693 else
23694 return NULL_TREE;
23697 /* Check that SRC is a constant integer that can be represented as a native
23698 integer constant (either signed or unsigned). If so, store it into DEST and
23699 return true. Return false otherwise. */
23701 static bool
23702 get_discr_value (tree src, dw_discr_value *dest)
23704 tree discr_type = TREE_TYPE (src);
23706 if (lang_hooks.types.get_debug_type)
23708 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23709 if (debug_type != NULL)
23710 discr_type = debug_type;
23713 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23714 return false;
23716 /* Signedness can vary between the original type and the debug type. This
23717 can happen for character types in Ada for instance: the character type
23718 used for code generation can be signed, to be compatible with the C one,
23719 but from a debugger point of view, it must be unsigned. */
23720 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23721 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23723 if (is_orig_unsigned != is_debug_unsigned)
23724 src = fold_convert (discr_type, src);
23726 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23727 return false;
23729 dest->pos = is_debug_unsigned;
23730 if (is_debug_unsigned)
23731 dest->v.uval = tree_to_uhwi (src);
23732 else
23733 dest->v.sval = tree_to_shwi (src);
23735 return true;
23738 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23739 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23740 store NULL_TREE in DISCR_DECL. Otherwise:
23742 - store the discriminant field in STRUCT_TYPE that controls the variant
23743 part to *DISCR_DECL
23745 - put in *DISCR_LISTS_P an array where for each variant, the item
23746 represents the corresponding matching list of discriminant values.
23748 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23749 the above array.
23751 Note that when the array is allocated (i.e. when the analysis is
23752 successful), it is up to the caller to free the array. */
23754 static void
23755 analyze_variants_discr (tree variant_part_decl,
23756 tree struct_type,
23757 tree *discr_decl,
23758 dw_discr_list_ref **discr_lists_p,
23759 unsigned *discr_lists_length)
23761 tree variant_part_type = TREE_TYPE (variant_part_decl);
23762 tree variant;
23763 dw_discr_list_ref *discr_lists;
23764 unsigned i;
23766 /* Compute how many variants there are in this variant part. */
23767 *discr_lists_length = 0;
23768 for (variant = TYPE_FIELDS (variant_part_type);
23769 variant != NULL_TREE;
23770 variant = DECL_CHAIN (variant))
23771 ++*discr_lists_length;
23773 *discr_decl = NULL_TREE;
23774 *discr_lists_p
23775 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23776 sizeof (**discr_lists_p));
23777 discr_lists = *discr_lists_p;
23779 /* And then analyze all variants to extract discriminant information for all
23780 of them. This analysis is conservative: as soon as we detect something we
23781 do not support, abort everything and pretend we found nothing. */
23782 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23783 variant != NULL_TREE;
23784 variant = DECL_CHAIN (variant), ++i)
23786 tree match_expr = DECL_QUALIFIER (variant);
23788 /* Now, try to analyze the predicate and deduce a discriminant for
23789 it. */
23790 if (match_expr == boolean_true_node)
23791 /* Typically happens for the default variant: it matches all cases that
23792 previous variants rejected. Don't output any matching value for
23793 this one. */
23794 continue;
23796 /* The following loop tries to iterate over each discriminant
23797 possibility: single values or ranges. */
23798 while (match_expr != NULL_TREE)
23800 tree next_round_match_expr;
23801 tree candidate_discr = NULL_TREE;
23802 dw_discr_list_ref new_node = NULL;
23804 /* Possibilities are matched one after the other by nested
23805 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23806 continue with the rest at next iteration. */
23807 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23809 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23810 match_expr = TREE_OPERAND (match_expr, 1);
23812 else
23813 next_round_match_expr = NULL_TREE;
23815 if (match_expr == boolean_false_node)
23816 /* This sub-expression matches nothing: just wait for the next
23817 one. */
23820 else if (TREE_CODE (match_expr) == EQ_EXPR)
23822 /* We are matching: <discr_field> == <integer_cst>
23823 This sub-expression matches a single value. */
23824 tree integer_cst = TREE_OPERAND (match_expr, 1);
23826 candidate_discr
23827 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23828 struct_type);
23830 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23831 if (!get_discr_value (integer_cst,
23832 &new_node->dw_discr_lower_bound))
23833 goto abort;
23834 new_node->dw_discr_range = false;
23837 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23839 /* We are matching:
23840 <discr_field> > <integer_cst>
23841 && <discr_field> < <integer_cst>.
23842 This sub-expression matches the range of values between the
23843 two matched integer constants. Note that comparisons can be
23844 inclusive or exclusive. */
23845 tree candidate_discr_1, candidate_discr_2;
23846 tree lower_cst, upper_cst;
23847 bool lower_cst_included, upper_cst_included;
23848 tree lower_op = TREE_OPERAND (match_expr, 0);
23849 tree upper_op = TREE_OPERAND (match_expr, 1);
23851 /* When the comparison is exclusive, the integer constant is not
23852 the discriminant range bound we are looking for: we will have
23853 to increment or decrement it. */
23854 if (TREE_CODE (lower_op) == GE_EXPR)
23855 lower_cst_included = true;
23856 else if (TREE_CODE (lower_op) == GT_EXPR)
23857 lower_cst_included = false;
23858 else
23859 goto abort;
23861 if (TREE_CODE (upper_op) == LE_EXPR)
23862 upper_cst_included = true;
23863 else if (TREE_CODE (upper_op) == LT_EXPR)
23864 upper_cst_included = false;
23865 else
23866 goto abort;
23868 /* Extract the discriminant from the first operand and check it
23869 is consistant with the same analysis in the second
23870 operand. */
23871 candidate_discr_1
23872 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23873 struct_type);
23874 candidate_discr_2
23875 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23876 struct_type);
23877 if (candidate_discr_1 == candidate_discr_2)
23878 candidate_discr = candidate_discr_1;
23879 else
23880 goto abort;
23882 /* Extract bounds from both. */
23883 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23884 lower_cst = TREE_OPERAND (lower_op, 1);
23885 upper_cst = TREE_OPERAND (upper_op, 1);
23887 if (!lower_cst_included)
23888 lower_cst
23889 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23890 build_int_cst (TREE_TYPE (lower_cst), 1));
23891 if (!upper_cst_included)
23892 upper_cst
23893 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23894 build_int_cst (TREE_TYPE (upper_cst), 1));
23896 if (!get_discr_value (lower_cst,
23897 &new_node->dw_discr_lower_bound)
23898 || !get_discr_value (upper_cst,
23899 &new_node->dw_discr_upper_bound))
23900 goto abort;
23902 new_node->dw_discr_range = true;
23905 else
23906 /* Unsupported sub-expression: we cannot determine the set of
23907 matching discriminant values. Abort everything. */
23908 goto abort;
23910 /* If the discriminant info is not consistant with what we saw so
23911 far, consider the analysis failed and abort everything. */
23912 if (candidate_discr == NULL_TREE
23913 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23914 goto abort;
23915 else
23916 *discr_decl = candidate_discr;
23918 if (new_node != NULL)
23920 new_node->dw_discr_next = discr_lists[i];
23921 discr_lists[i] = new_node;
23923 match_expr = next_round_match_expr;
23927 /* If we reach this point, we could match everything we were interested
23928 in. */
23929 return;
23931 abort:
23932 /* Clean all data structure and return no result. */
23933 free (*discr_lists_p);
23934 *discr_lists_p = NULL;
23935 *discr_decl = NULL_TREE;
23938 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23939 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23940 under CONTEXT_DIE.
23942 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23943 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23944 this type, which are record types, represent the available variants and each
23945 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23946 values are inferred from these attributes.
23948 In trees, the offsets for the fields inside these sub-records are relative
23949 to the variant part itself, whereas the corresponding DIEs should have
23950 offset attributes that are relative to the embedding record base address.
23951 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23952 must be an expression that computes the offset of the variant part to
23953 describe in DWARF. */
23955 static void
23956 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23957 dw_die_ref context_die)
23959 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23960 tree variant_part_offset = vlr_ctx->variant_part_offset;
23961 struct loc_descr_context ctx = {
23962 vlr_ctx->struct_type, /* context_type */
23963 NULL_TREE, /* base_decl */
23964 NULL, /* dpi */
23965 false, /* placeholder_arg */
23966 false /* placeholder_seen */
23969 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23970 NULL_TREE if there is no such field. */
23971 tree discr_decl = NULL_TREE;
23972 dw_discr_list_ref *discr_lists;
23973 unsigned discr_lists_length = 0;
23974 unsigned i;
23976 dw_die_ref dwarf_proc_die = NULL;
23977 dw_die_ref variant_part_die
23978 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23980 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23982 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23983 &discr_decl, &discr_lists, &discr_lists_length);
23985 if (discr_decl != NULL_TREE)
23987 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23989 if (discr_die)
23990 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23991 else
23992 /* We have no DIE for the discriminant, so just discard all
23993 discrimimant information in the output. */
23994 discr_decl = NULL_TREE;
23997 /* If the offset for this variant part is more complex than a constant,
23998 create a DWARF procedure for it so that we will not have to generate DWARF
23999 expressions for it for each member. */
24000 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24001 && (dwarf_version >= 3 || !dwarf_strict))
24003 const tree dwarf_proc_fndecl
24004 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24005 build_function_type (TREE_TYPE (variant_part_offset),
24006 NULL_TREE));
24007 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24008 const dw_loc_descr_ref dwarf_proc_body
24009 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24011 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24012 dwarf_proc_fndecl, context_die);
24013 if (dwarf_proc_die != NULL)
24014 variant_part_offset = dwarf_proc_call;
24017 /* Output DIEs for all variants. */
24018 i = 0;
24019 for (tree variant = TYPE_FIELDS (variant_part_type);
24020 variant != NULL_TREE;
24021 variant = DECL_CHAIN (variant), ++i)
24023 tree variant_type = TREE_TYPE (variant);
24024 dw_die_ref variant_die;
24026 /* All variants (i.e. members of a variant part) are supposed to be
24027 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24028 under these records. */
24029 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24031 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24032 equate_decl_number_to_die (variant, variant_die);
24034 /* Output discriminant values this variant matches, if any. */
24035 if (discr_decl == NULL || discr_lists[i] == NULL)
24036 /* In the case we have discriminant information at all, this is
24037 probably the default variant: as the standard says, don't
24038 output any discriminant value/list attribute. */
24040 else if (discr_lists[i]->dw_discr_next == NULL
24041 && !discr_lists[i]->dw_discr_range)
24042 /* If there is only one accepted value, don't bother outputting a
24043 list. */
24044 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24045 else
24046 add_discr_list (variant_die, discr_lists[i]);
24048 for (tree member = TYPE_FIELDS (variant_type);
24049 member != NULL_TREE;
24050 member = DECL_CHAIN (member))
24052 struct vlr_context vlr_sub_ctx = {
24053 vlr_ctx->struct_type, /* struct_type */
24054 NULL /* variant_part_offset */
24056 if (is_variant_part (member))
24058 /* All offsets for fields inside variant parts are relative to
24059 the top-level embedding RECORD_TYPE's base address. On the
24060 other hand, offsets in GCC's types are relative to the
24061 nested-most variant part. So we have to sum offsets each time
24062 we recurse. */
24064 vlr_sub_ctx.variant_part_offset
24065 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24066 variant_part_offset, byte_position (member));
24067 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24069 else
24071 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24072 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24077 free (discr_lists);
24080 /* Generate a DIE for a class member. */
24082 static void
24083 gen_member_die (tree type, dw_die_ref context_die)
24085 tree member;
24086 tree binfo = TYPE_BINFO (type);
24088 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24090 /* If this is not an incomplete type, output descriptions of each of its
24091 members. Note that as we output the DIEs necessary to represent the
24092 members of this record or union type, we will also be trying to output
24093 DIEs to represent the *types* of those members. However the `type'
24094 function (above) will specifically avoid generating type DIEs for member
24095 types *within* the list of member DIEs for this (containing) type except
24096 for those types (of members) which are explicitly marked as also being
24097 members of this (containing) type themselves. The g++ front- end can
24098 force any given type to be treated as a member of some other (containing)
24099 type by setting the TYPE_CONTEXT of the given (member) type to point to
24100 the TREE node representing the appropriate (containing) type. */
24102 /* First output info about the base classes. */
24103 if (binfo)
24105 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24106 int i;
24107 tree base;
24109 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24110 gen_inheritance_die (base,
24111 (accesses ? (*accesses)[i] : access_public_node),
24112 type,
24113 context_die);
24116 /* Now output info about the data members and type members. */
24117 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24119 struct vlr_context vlr_ctx = { type, NULL_TREE };
24120 bool static_inline_p
24121 = (TREE_STATIC (member)
24122 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24123 != -1));
24125 /* Ignore clones. */
24126 if (DECL_ABSTRACT_ORIGIN (member))
24127 continue;
24129 /* If we thought we were generating minimal debug info for TYPE
24130 and then changed our minds, some of the member declarations
24131 may have already been defined. Don't define them again, but
24132 do put them in the right order. */
24134 if (dw_die_ref child = lookup_decl_die (member))
24136 /* Handle inline static data members, which only have in-class
24137 declarations. */
24138 dw_die_ref ref = NULL;
24139 if (child->die_tag == DW_TAG_variable
24140 && child->die_parent == comp_unit_die ())
24142 ref = get_AT_ref (child, DW_AT_specification);
24143 /* For C++17 inline static data members followed by redundant
24144 out of class redeclaration, we might get here with
24145 child being the DIE created for the out of class
24146 redeclaration and with its DW_AT_specification being
24147 the DIE created for in-class definition. We want to
24148 reparent the latter, and don't want to create another
24149 DIE with DW_AT_specification in that case, because
24150 we already have one. */
24151 if (ref
24152 && static_inline_p
24153 && ref->die_tag == DW_TAG_variable
24154 && ref->die_parent == comp_unit_die ()
24155 && get_AT (ref, DW_AT_specification) == NULL)
24157 child = ref;
24158 ref = NULL;
24159 static_inline_p = false;
24163 if (child->die_tag == DW_TAG_variable
24164 && child->die_parent == comp_unit_die ()
24165 && ref == NULL)
24167 reparent_child (child, context_die);
24168 if (dwarf_version < 5)
24169 child->die_tag = DW_TAG_member;
24171 else
24172 splice_child_die (context_die, child);
24175 /* Do not generate standard DWARF for variant parts if we are generating
24176 the corresponding GNAT encodings: DIEs generated for both would
24177 conflict in our mappings. */
24178 else if (is_variant_part (member)
24179 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24181 vlr_ctx.variant_part_offset = byte_position (member);
24182 gen_variant_part (member, &vlr_ctx, context_die);
24184 else
24186 vlr_ctx.variant_part_offset = NULL_TREE;
24187 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24190 /* For C++ inline static data members emit immediately a DW_TAG_variable
24191 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24192 DW_AT_specification. */
24193 if (static_inline_p)
24195 int old_extern = DECL_EXTERNAL (member);
24196 DECL_EXTERNAL (member) = 0;
24197 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24198 DECL_EXTERNAL (member) = old_extern;
24203 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24204 is set, we pretend that the type was never defined, so we only get the
24205 member DIEs needed by later specification DIEs. */
24207 static void
24208 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24209 enum debug_info_usage usage)
24211 if (TREE_ASM_WRITTEN (type))
24213 /* Fill in the bound of variable-length fields in late dwarf if
24214 still incomplete. */
24215 if (!early_dwarf && variably_modified_type_p (type, NULL))
24216 for (tree member = TYPE_FIELDS (type);
24217 member;
24218 member = DECL_CHAIN (member))
24219 fill_variable_array_bounds (TREE_TYPE (member));
24220 return;
24223 dw_die_ref type_die = lookup_type_die (type);
24224 dw_die_ref scope_die = 0;
24225 int nested = 0;
24226 int complete = (TYPE_SIZE (type)
24227 && (! TYPE_STUB_DECL (type)
24228 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24229 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24230 complete = complete && should_emit_struct_debug (type, usage);
24232 if (type_die && ! complete)
24233 return;
24235 if (TYPE_CONTEXT (type) != NULL_TREE
24236 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24237 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24238 nested = 1;
24240 scope_die = scope_die_for (type, context_die);
24242 /* Generate child dies for template paramaters. */
24243 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24244 schedule_generic_params_dies_gen (type);
24246 if (! type_die || (nested && is_cu_die (scope_die)))
24247 /* First occurrence of type or toplevel definition of nested class. */
24249 dw_die_ref old_die = type_die;
24251 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24252 ? record_type_tag (type) : DW_TAG_union_type,
24253 scope_die, type);
24254 equate_type_number_to_die (type, type_die);
24255 if (old_die)
24256 add_AT_specification (type_die, old_die);
24257 else
24258 add_name_attribute (type_die, type_tag (type));
24260 else
24261 remove_AT (type_die, DW_AT_declaration);
24263 /* If this type has been completed, then give it a byte_size attribute and
24264 then give a list of members. */
24265 if (complete && !ns_decl)
24267 /* Prevent infinite recursion in cases where the type of some member of
24268 this type is expressed in terms of this type itself. */
24269 TREE_ASM_WRITTEN (type) = 1;
24270 add_byte_size_attribute (type_die, type);
24271 add_alignment_attribute (type_die, type);
24272 if (TYPE_STUB_DECL (type) != NULL_TREE)
24274 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24275 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24278 /* If the first reference to this type was as the return type of an
24279 inline function, then it may not have a parent. Fix this now. */
24280 if (type_die->die_parent == NULL)
24281 add_child_die (scope_die, type_die);
24283 push_decl_scope (type);
24284 gen_member_die (type, type_die);
24285 pop_decl_scope ();
24287 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24288 if (TYPE_ARTIFICIAL (type))
24289 add_AT_flag (type_die, DW_AT_artificial, 1);
24291 /* GNU extension: Record what type our vtable lives in. */
24292 if (TYPE_VFIELD (type))
24294 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24296 gen_type_die (vtype, context_die);
24297 add_AT_die_ref (type_die, DW_AT_containing_type,
24298 lookup_type_die (vtype));
24301 else
24303 add_AT_flag (type_die, DW_AT_declaration, 1);
24305 /* We don't need to do this for function-local types. */
24306 if (TYPE_STUB_DECL (type)
24307 && ! decl_function_context (TYPE_STUB_DECL (type)))
24308 vec_safe_push (incomplete_types, type);
24311 if (get_AT (type_die, DW_AT_name))
24312 add_pubtype (type, type_die);
24315 /* Generate a DIE for a subroutine _type_. */
24317 static void
24318 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24320 tree return_type = TREE_TYPE (type);
24321 dw_die_ref subr_die
24322 = new_die (DW_TAG_subroutine_type,
24323 scope_die_for (type, context_die), type);
24325 equate_type_number_to_die (type, subr_die);
24326 add_prototyped_attribute (subr_die, type);
24327 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24328 context_die);
24329 add_alignment_attribute (subr_die, type);
24330 gen_formal_types_die (type, subr_die);
24332 if (get_AT (subr_die, DW_AT_name))
24333 add_pubtype (type, subr_die);
24334 if ((dwarf_version >= 5 || !dwarf_strict)
24335 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24336 add_AT_flag (subr_die, DW_AT_reference, 1);
24337 if ((dwarf_version >= 5 || !dwarf_strict)
24338 && lang_hooks.types.type_dwarf_attribute (type,
24339 DW_AT_rvalue_reference) != -1)
24340 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24343 /* Generate a DIE for a type definition. */
24345 static void
24346 gen_typedef_die (tree decl, dw_die_ref context_die)
24348 dw_die_ref type_die;
24349 tree type;
24351 if (TREE_ASM_WRITTEN (decl))
24353 if (DECL_ORIGINAL_TYPE (decl))
24354 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24355 return;
24358 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24359 checks in process_scope_var and modified_type_die), this should be called
24360 only for original types. */
24361 gcc_assert (decl_ultimate_origin (decl) == NULL
24362 || decl_ultimate_origin (decl) == decl);
24364 TREE_ASM_WRITTEN (decl) = 1;
24365 type_die = new_die (DW_TAG_typedef, context_die, decl);
24367 add_name_and_src_coords_attributes (type_die, decl);
24368 if (DECL_ORIGINAL_TYPE (decl))
24370 type = DECL_ORIGINAL_TYPE (decl);
24371 if (type == error_mark_node)
24372 return;
24374 gcc_assert (type != TREE_TYPE (decl));
24375 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24377 else
24379 type = TREE_TYPE (decl);
24380 if (type == error_mark_node)
24381 return;
24383 if (is_naming_typedef_decl (TYPE_NAME (type)))
24385 /* Here, we are in the case of decl being a typedef naming
24386 an anonymous type, e.g:
24387 typedef struct {...} foo;
24388 In that case TREE_TYPE (decl) is not a typedef variant
24389 type and TYPE_NAME of the anonymous type is set to the
24390 TYPE_DECL of the typedef. This construct is emitted by
24391 the C++ FE.
24393 TYPE is the anonymous struct named by the typedef
24394 DECL. As we need the DW_AT_type attribute of the
24395 DW_TAG_typedef to point to the DIE of TYPE, let's
24396 generate that DIE right away. add_type_attribute
24397 called below will then pick (via lookup_type_die) that
24398 anonymous struct DIE. */
24399 if (!TREE_ASM_WRITTEN (type))
24400 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24402 /* This is a GNU Extension. We are adding a
24403 DW_AT_linkage_name attribute to the DIE of the
24404 anonymous struct TYPE. The value of that attribute
24405 is the name of the typedef decl naming the anonymous
24406 struct. This greatly eases the work of consumers of
24407 this debug info. */
24408 add_linkage_name_raw (lookup_type_die (type), decl);
24412 add_type_attribute (type_die, type, decl_quals (decl), false,
24413 context_die);
24415 if (is_naming_typedef_decl (decl))
24416 /* We want that all subsequent calls to lookup_type_die with
24417 TYPE in argument yield the DW_TAG_typedef we have just
24418 created. */
24419 equate_type_number_to_die (type, type_die);
24421 add_alignment_attribute (type_die, TREE_TYPE (decl));
24423 add_accessibility_attribute (type_die, decl);
24425 if (DECL_ABSTRACT_P (decl))
24426 equate_decl_number_to_die (decl, type_die);
24428 if (get_AT (type_die, DW_AT_name))
24429 add_pubtype (decl, type_die);
24432 /* Generate a DIE for a struct, class, enum or union type. */
24434 static void
24435 gen_tagged_type_die (tree type,
24436 dw_die_ref context_die,
24437 enum debug_info_usage usage)
24439 int need_pop;
24441 if (type == NULL_TREE
24442 || !is_tagged_type (type))
24443 return;
24445 if (TREE_ASM_WRITTEN (type))
24446 need_pop = 0;
24447 /* If this is a nested type whose containing class hasn't been written
24448 out yet, writing it out will cover this one, too. This does not apply
24449 to instantiations of member class templates; they need to be added to
24450 the containing class as they are generated. FIXME: This hurts the
24451 idea of combining type decls from multiple TUs, since we can't predict
24452 what set of template instantiations we'll get. */
24453 else if (TYPE_CONTEXT (type)
24454 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24455 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24457 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24459 if (TREE_ASM_WRITTEN (type))
24460 return;
24462 /* If that failed, attach ourselves to the stub. */
24463 push_decl_scope (TYPE_CONTEXT (type));
24464 context_die = lookup_type_die (TYPE_CONTEXT (type));
24465 need_pop = 1;
24467 else if (TYPE_CONTEXT (type) != NULL_TREE
24468 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24470 /* If this type is local to a function that hasn't been written
24471 out yet, use a NULL context for now; it will be fixed up in
24472 decls_for_scope. */
24473 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24474 /* A declaration DIE doesn't count; nested types need to go in the
24475 specification. */
24476 if (context_die && is_declaration_die (context_die))
24477 context_die = NULL;
24478 need_pop = 0;
24480 else
24482 context_die = declare_in_namespace (type, context_die);
24483 need_pop = 0;
24486 if (TREE_CODE (type) == ENUMERAL_TYPE)
24488 /* This might have been written out by the call to
24489 declare_in_namespace. */
24490 if (!TREE_ASM_WRITTEN (type))
24491 gen_enumeration_type_die (type, context_die);
24493 else
24494 gen_struct_or_union_type_die (type, context_die, usage);
24496 if (need_pop)
24497 pop_decl_scope ();
24499 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24500 it up if it is ever completed. gen_*_type_die will set it for us
24501 when appropriate. */
24504 /* Generate a type description DIE. */
24506 static void
24507 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24508 enum debug_info_usage usage)
24510 struct array_descr_info info;
24512 if (type == NULL_TREE || type == error_mark_node)
24513 return;
24515 if (flag_checking && type)
24516 verify_type (type);
24518 if (TYPE_NAME (type) != NULL_TREE
24519 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24520 && is_redundant_typedef (TYPE_NAME (type))
24521 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24522 /* The DECL of this type is a typedef we don't want to emit debug
24523 info for but we want debug info for its underlying typedef.
24524 This can happen for e.g, the injected-class-name of a C++
24525 type. */
24526 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24528 /* If TYPE is a typedef type variant, let's generate debug info
24529 for the parent typedef which TYPE is a type of. */
24530 if (typedef_variant_p (type))
24532 if (TREE_ASM_WRITTEN (type))
24533 return;
24535 tree name = TYPE_NAME (type);
24536 tree origin = decl_ultimate_origin (name);
24537 if (origin != NULL && origin != name)
24539 gen_decl_die (origin, NULL, NULL, context_die);
24540 return;
24543 /* Prevent broken recursion; we can't hand off to the same type. */
24544 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24546 /* Give typedefs the right scope. */
24547 context_die = scope_die_for (type, context_die);
24549 TREE_ASM_WRITTEN (type) = 1;
24551 gen_decl_die (name, NULL, NULL, context_die);
24552 return;
24555 /* If type is an anonymous tagged type named by a typedef, let's
24556 generate debug info for the typedef. */
24557 if (is_naming_typedef_decl (TYPE_NAME (type)))
24559 /* Use the DIE of the containing namespace as the parent DIE of
24560 the type description DIE we want to generate. */
24561 if (DECL_CONTEXT (TYPE_NAME (type))
24562 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24563 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24565 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24566 return;
24569 if (lang_hooks.types.get_debug_type)
24571 tree debug_type = lang_hooks.types.get_debug_type (type);
24573 if (debug_type != NULL_TREE && debug_type != type)
24575 gen_type_die_with_usage (debug_type, context_die, usage);
24576 return;
24580 /* We are going to output a DIE to represent the unqualified version
24581 of this type (i.e. without any const or volatile qualifiers) so
24582 get the main variant (i.e. the unqualified version) of this type
24583 now. (Vectors and arrays are special because the debugging info is in the
24584 cloned type itself. Similarly function/method types can contain extra
24585 ref-qualification). */
24586 if (TREE_CODE (type) == FUNCTION_TYPE
24587 || TREE_CODE (type) == METHOD_TYPE)
24589 /* For function/method types, can't use type_main_variant here,
24590 because that can have different ref-qualifiers for C++,
24591 but try to canonicalize. */
24592 tree main = TYPE_MAIN_VARIANT (type);
24593 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24594 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24595 && check_base_type (t, main)
24596 && check_lang_type (t, type))
24598 type = t;
24599 break;
24602 else if (TREE_CODE (type) != VECTOR_TYPE
24603 && TREE_CODE (type) != ARRAY_TYPE)
24604 type = type_main_variant (type);
24606 /* If this is an array type with hidden descriptor, handle it first. */
24607 if (!TREE_ASM_WRITTEN (type)
24608 && lang_hooks.types.get_array_descr_info)
24610 memset (&info, 0, sizeof (info));
24611 if (lang_hooks.types.get_array_descr_info (type, &info))
24613 /* Fortran sometimes emits array types with no dimension. */
24614 gcc_assert (info.ndimensions >= 0
24615 && (info.ndimensions
24616 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24617 gen_descr_array_type_die (type, &info, context_die);
24618 TREE_ASM_WRITTEN (type) = 1;
24619 return;
24623 if (TREE_ASM_WRITTEN (type))
24625 /* Variable-length types may be incomplete even if
24626 TREE_ASM_WRITTEN. For such types, fall through to
24627 gen_array_type_die() and possibly fill in
24628 DW_AT_{upper,lower}_bound attributes. */
24629 if ((TREE_CODE (type) != ARRAY_TYPE
24630 && TREE_CODE (type) != RECORD_TYPE
24631 && TREE_CODE (type) != UNION_TYPE
24632 && TREE_CODE (type) != QUAL_UNION_TYPE)
24633 || !variably_modified_type_p (type, NULL))
24634 return;
24637 switch (TREE_CODE (type))
24639 case ERROR_MARK:
24640 break;
24642 case POINTER_TYPE:
24643 case REFERENCE_TYPE:
24644 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24645 ensures that the gen_type_die recursion will terminate even if the
24646 type is recursive. Recursive types are possible in Ada. */
24647 /* ??? We could perhaps do this for all types before the switch
24648 statement. */
24649 TREE_ASM_WRITTEN (type) = 1;
24651 /* For these types, all that is required is that we output a DIE (or a
24652 set of DIEs) to represent the "basis" type. */
24653 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24654 DINFO_USAGE_IND_USE);
24655 break;
24657 case OFFSET_TYPE:
24658 /* This code is used for C++ pointer-to-data-member types.
24659 Output a description of the relevant class type. */
24660 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24661 DINFO_USAGE_IND_USE);
24663 /* Output a description of the type of the object pointed to. */
24664 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24665 DINFO_USAGE_IND_USE);
24667 /* Now output a DIE to represent this pointer-to-data-member type
24668 itself. */
24669 gen_ptr_to_mbr_type_die (type, context_die);
24670 break;
24672 case FUNCTION_TYPE:
24673 /* Force out return type (in case it wasn't forced out already). */
24674 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24675 DINFO_USAGE_DIR_USE);
24676 gen_subroutine_type_die (type, context_die);
24677 break;
24679 case METHOD_TYPE:
24680 /* Force out return type (in case it wasn't forced out already). */
24681 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24682 DINFO_USAGE_DIR_USE);
24683 gen_subroutine_type_die (type, context_die);
24684 break;
24686 case ARRAY_TYPE:
24687 case VECTOR_TYPE:
24688 gen_array_type_die (type, context_die);
24689 break;
24691 case ENUMERAL_TYPE:
24692 case RECORD_TYPE:
24693 case UNION_TYPE:
24694 case QUAL_UNION_TYPE:
24695 gen_tagged_type_die (type, context_die, usage);
24696 return;
24698 case VOID_TYPE:
24699 case INTEGER_TYPE:
24700 case REAL_TYPE:
24701 case FIXED_POINT_TYPE:
24702 case COMPLEX_TYPE:
24703 case BOOLEAN_TYPE:
24704 case POINTER_BOUNDS_TYPE:
24705 /* No DIEs needed for fundamental types. */
24706 break;
24708 case NULLPTR_TYPE:
24709 case LANG_TYPE:
24710 /* Just use DW_TAG_unspecified_type. */
24712 dw_die_ref type_die = lookup_type_die (type);
24713 if (type_die == NULL)
24715 tree name = TYPE_IDENTIFIER (type);
24716 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24717 type);
24718 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24719 equate_type_number_to_die (type, type_die);
24722 break;
24724 default:
24725 if (is_cxx_auto (type))
24727 tree name = TYPE_IDENTIFIER (type);
24728 dw_die_ref *die = (name == get_identifier ("auto")
24729 ? &auto_die : &decltype_auto_die);
24730 if (!*die)
24732 *die = new_die (DW_TAG_unspecified_type,
24733 comp_unit_die (), NULL_TREE);
24734 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24736 equate_type_number_to_die (type, *die);
24737 break;
24739 gcc_unreachable ();
24742 TREE_ASM_WRITTEN (type) = 1;
24745 static void
24746 gen_type_die (tree type, dw_die_ref context_die)
24748 if (type != error_mark_node)
24750 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24751 if (flag_checking)
24753 dw_die_ref die = lookup_type_die (type);
24754 if (die)
24755 check_die (die);
24760 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24761 things which are local to the given block. */
24763 static void
24764 gen_block_die (tree stmt, dw_die_ref context_die)
24766 int must_output_die = 0;
24767 bool inlined_func;
24769 /* Ignore blocks that are NULL. */
24770 if (stmt == NULL_TREE)
24771 return;
24773 inlined_func = inlined_function_outer_scope_p (stmt);
24775 /* If the block is one fragment of a non-contiguous block, do not
24776 process the variables, since they will have been done by the
24777 origin block. Do process subblocks. */
24778 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24780 tree sub;
24782 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24783 gen_block_die (sub, context_die);
24785 return;
24788 /* Determine if we need to output any Dwarf DIEs at all to represent this
24789 block. */
24790 if (inlined_func)
24791 /* The outer scopes for inlinings *must* always be represented. We
24792 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24793 must_output_die = 1;
24794 else
24796 /* Determine if this block directly contains any "significant"
24797 local declarations which we will need to output DIEs for. */
24798 if (debug_info_level > DINFO_LEVEL_TERSE)
24799 /* We are not in terse mode so *any* local declaration counts
24800 as being a "significant" one. */
24801 must_output_die = ((BLOCK_VARS (stmt) != NULL
24802 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24803 && (TREE_USED (stmt)
24804 || TREE_ASM_WRITTEN (stmt)
24805 || BLOCK_ABSTRACT (stmt)));
24806 else if ((TREE_USED (stmt)
24807 || TREE_ASM_WRITTEN (stmt)
24808 || BLOCK_ABSTRACT (stmt))
24809 && !dwarf2out_ignore_block (stmt))
24810 must_output_die = 1;
24813 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24814 DIE for any block which contains no significant local declarations at
24815 all. Rather, in such cases we just call `decls_for_scope' so that any
24816 needed Dwarf info for any sub-blocks will get properly generated. Note
24817 that in terse mode, our definition of what constitutes a "significant"
24818 local declaration gets restricted to include only inlined function
24819 instances and local (nested) function definitions. */
24820 if (must_output_die)
24822 if (inlined_func)
24824 /* If STMT block is abstract, that means we have been called
24825 indirectly from dwarf2out_abstract_function.
24826 That function rightfully marks the descendent blocks (of
24827 the abstract function it is dealing with) as being abstract,
24828 precisely to prevent us from emitting any
24829 DW_TAG_inlined_subroutine DIE as a descendent
24830 of an abstract function instance. So in that case, we should
24831 not call gen_inlined_subroutine_die.
24833 Later though, when cgraph asks dwarf2out to emit info
24834 for the concrete instance of the function decl into which
24835 the concrete instance of STMT got inlined, the later will lead
24836 to the generation of a DW_TAG_inlined_subroutine DIE. */
24837 if (! BLOCK_ABSTRACT (stmt))
24838 gen_inlined_subroutine_die (stmt, context_die);
24840 else
24841 gen_lexical_block_die (stmt, context_die);
24843 else
24844 decls_for_scope (stmt, context_die);
24847 /* Process variable DECL (or variable with origin ORIGIN) within
24848 block STMT and add it to CONTEXT_DIE. */
24849 static void
24850 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24852 dw_die_ref die;
24853 tree decl_or_origin = decl ? decl : origin;
24855 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24856 die = lookup_decl_die (decl_or_origin);
24857 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24859 if (TYPE_DECL_IS_STUB (decl_or_origin))
24860 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24861 else
24862 die = lookup_decl_die (decl_or_origin);
24863 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24864 if (! die && ! early_dwarf)
24865 return;
24867 else
24868 die = NULL;
24870 /* Avoid creating DIEs for local typedefs and concrete static variables that
24871 will only be pruned later. */
24872 if ((origin || decl_ultimate_origin (decl))
24873 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24874 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24876 origin = decl_ultimate_origin (decl_or_origin);
24877 if (decl && VAR_P (decl) && die != NULL)
24879 die = lookup_decl_die (origin);
24880 if (die != NULL)
24881 equate_decl_number_to_die (decl, die);
24883 return;
24886 if (die != NULL && die->die_parent == NULL)
24887 add_child_die (context_die, die);
24888 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24890 if (early_dwarf)
24891 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24892 stmt, context_die);
24894 else
24896 if (decl && DECL_P (decl))
24898 die = lookup_decl_die (decl);
24900 /* Early created DIEs do not have a parent as the decls refer
24901 to the function as DECL_CONTEXT rather than the BLOCK. */
24902 if (die && die->die_parent == NULL)
24904 gcc_assert (in_lto_p);
24905 add_child_die (context_die, die);
24909 gen_decl_die (decl, origin, NULL, context_die);
24913 /* Generate all of the decls declared within a given scope and (recursively)
24914 all of its sub-blocks. */
24916 static void
24917 decls_for_scope (tree stmt, dw_die_ref context_die)
24919 tree decl;
24920 unsigned int i;
24921 tree subblocks;
24923 /* Ignore NULL blocks. */
24924 if (stmt == NULL_TREE)
24925 return;
24927 /* Output the DIEs to represent all of the data objects and typedefs
24928 declared directly within this block but not within any nested
24929 sub-blocks. Also, nested function and tag DIEs have been
24930 generated with a parent of NULL; fix that up now. We don't
24931 have to do this if we're at -g1. */
24932 if (debug_info_level > DINFO_LEVEL_TERSE)
24934 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24935 process_scope_var (stmt, decl, NULL_TREE, context_die);
24936 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24937 origin - avoid doing this twice as we have no good way to see
24938 if we've done it once already. */
24939 if (! early_dwarf)
24940 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24942 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24943 if (decl == current_function_decl)
24944 /* Ignore declarations of the current function, while they
24945 are declarations, gen_subprogram_die would treat them
24946 as definitions again, because they are equal to
24947 current_function_decl and endlessly recurse. */;
24948 else if (TREE_CODE (decl) == FUNCTION_DECL)
24949 process_scope_var (stmt, decl, NULL_TREE, context_die);
24950 else
24951 process_scope_var (stmt, NULL_TREE, decl, context_die);
24955 /* Even if we're at -g1, we need to process the subblocks in order to get
24956 inlined call information. */
24958 /* Output the DIEs to represent all sub-blocks (and the items declared
24959 therein) of this block. */
24960 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24961 subblocks != NULL;
24962 subblocks = BLOCK_CHAIN (subblocks))
24963 gen_block_die (subblocks, context_die);
24966 /* Is this a typedef we can avoid emitting? */
24968 bool
24969 is_redundant_typedef (const_tree decl)
24971 if (TYPE_DECL_IS_STUB (decl))
24972 return true;
24974 if (DECL_ARTIFICIAL (decl)
24975 && DECL_CONTEXT (decl)
24976 && is_tagged_type (DECL_CONTEXT (decl))
24977 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24978 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24979 /* Also ignore the artificial member typedef for the class name. */
24980 return true;
24982 return false;
24985 /* Return TRUE if TYPE is a typedef that names a type for linkage
24986 purposes. This kind of typedefs is produced by the C++ FE for
24987 constructs like:
24989 typedef struct {...} foo;
24991 In that case, there is no typedef variant type produced for foo.
24992 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24993 struct type. */
24995 static bool
24996 is_naming_typedef_decl (const_tree decl)
24998 if (decl == NULL_TREE
24999 || TREE_CODE (decl) != TYPE_DECL
25000 || DECL_NAMELESS (decl)
25001 || !is_tagged_type (TREE_TYPE (decl))
25002 || DECL_IS_BUILTIN (decl)
25003 || is_redundant_typedef (decl)
25004 /* It looks like Ada produces TYPE_DECLs that are very similar
25005 to C++ naming typedefs but that have different
25006 semantics. Let's be specific to c++ for now. */
25007 || !is_cxx (decl))
25008 return FALSE;
25010 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25011 && TYPE_NAME (TREE_TYPE (decl)) == decl
25012 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25013 != TYPE_NAME (TREE_TYPE (decl))));
25016 /* Looks up the DIE for a context. */
25018 static inline dw_die_ref
25019 lookup_context_die (tree context)
25021 if (context)
25023 /* Find die that represents this context. */
25024 if (TYPE_P (context))
25026 context = TYPE_MAIN_VARIANT (context);
25027 dw_die_ref ctx = lookup_type_die (context);
25028 if (!ctx)
25029 return NULL;
25030 return strip_naming_typedef (context, ctx);
25032 else
25033 return lookup_decl_die (context);
25035 return comp_unit_die ();
25038 /* Returns the DIE for a context. */
25040 static inline dw_die_ref
25041 get_context_die (tree context)
25043 if (context)
25045 /* Find die that represents this context. */
25046 if (TYPE_P (context))
25048 context = TYPE_MAIN_VARIANT (context);
25049 return strip_naming_typedef (context, force_type_die (context));
25051 else
25052 return force_decl_die (context);
25054 return comp_unit_die ();
25057 /* Returns the DIE for decl. A DIE will always be returned. */
25059 static dw_die_ref
25060 force_decl_die (tree decl)
25062 dw_die_ref decl_die;
25063 unsigned saved_external_flag;
25064 tree save_fn = NULL_TREE;
25065 decl_die = lookup_decl_die (decl);
25066 if (!decl_die)
25068 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25070 decl_die = lookup_decl_die (decl);
25071 if (decl_die)
25072 return decl_die;
25074 switch (TREE_CODE (decl))
25076 case FUNCTION_DECL:
25077 /* Clear current_function_decl, so that gen_subprogram_die thinks
25078 that this is a declaration. At this point, we just want to force
25079 declaration die. */
25080 save_fn = current_function_decl;
25081 current_function_decl = NULL_TREE;
25082 gen_subprogram_die (decl, context_die);
25083 current_function_decl = save_fn;
25084 break;
25086 case VAR_DECL:
25087 /* Set external flag to force declaration die. Restore it after
25088 gen_decl_die() call. */
25089 saved_external_flag = DECL_EXTERNAL (decl);
25090 DECL_EXTERNAL (decl) = 1;
25091 gen_decl_die (decl, NULL, NULL, context_die);
25092 DECL_EXTERNAL (decl) = saved_external_flag;
25093 break;
25095 case NAMESPACE_DECL:
25096 if (dwarf_version >= 3 || !dwarf_strict)
25097 dwarf2out_decl (decl);
25098 else
25099 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25100 decl_die = comp_unit_die ();
25101 break;
25103 case TRANSLATION_UNIT_DECL:
25104 decl_die = comp_unit_die ();
25105 break;
25107 default:
25108 gcc_unreachable ();
25111 /* We should be able to find the DIE now. */
25112 if (!decl_die)
25113 decl_die = lookup_decl_die (decl);
25114 gcc_assert (decl_die);
25117 return decl_die;
25120 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25121 always returned. */
25123 static dw_die_ref
25124 force_type_die (tree type)
25126 dw_die_ref type_die;
25128 type_die = lookup_type_die (type);
25129 if (!type_die)
25131 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25133 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25134 false, context_die);
25135 gcc_assert (type_die);
25137 return type_die;
25140 /* Force out any required namespaces to be able to output DECL,
25141 and return the new context_die for it, if it's changed. */
25143 static dw_die_ref
25144 setup_namespace_context (tree thing, dw_die_ref context_die)
25146 tree context = (DECL_P (thing)
25147 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25148 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25149 /* Force out the namespace. */
25150 context_die = force_decl_die (context);
25152 return context_die;
25155 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25156 type) within its namespace, if appropriate.
25158 For compatibility with older debuggers, namespace DIEs only contain
25159 declarations; all definitions are emitted at CU scope, with
25160 DW_AT_specification pointing to the declaration (like with class
25161 members). */
25163 static dw_die_ref
25164 declare_in_namespace (tree thing, dw_die_ref context_die)
25166 dw_die_ref ns_context;
25168 if (debug_info_level <= DINFO_LEVEL_TERSE)
25169 return context_die;
25171 /* External declarations in the local scope only need to be emitted
25172 once, not once in the namespace and once in the scope.
25174 This avoids declaring the `extern' below in the
25175 namespace DIE as well as in the innermost scope:
25177 namespace S
25179 int i=5;
25180 int foo()
25182 int i=8;
25183 extern int i;
25184 return i;
25188 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25189 return context_die;
25191 /* If this decl is from an inlined function, then don't try to emit it in its
25192 namespace, as we will get confused. It would have already been emitted
25193 when the abstract instance of the inline function was emitted anyways. */
25194 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25195 return context_die;
25197 ns_context = setup_namespace_context (thing, context_die);
25199 if (ns_context != context_die)
25201 if (is_fortran ())
25202 return ns_context;
25203 if (DECL_P (thing))
25204 gen_decl_die (thing, NULL, NULL, ns_context);
25205 else
25206 gen_type_die (thing, ns_context);
25208 return context_die;
25211 /* Generate a DIE for a namespace or namespace alias. */
25213 static void
25214 gen_namespace_die (tree decl, dw_die_ref context_die)
25216 dw_die_ref namespace_die;
25218 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25219 they are an alias of. */
25220 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25222 /* Output a real namespace or module. */
25223 context_die = setup_namespace_context (decl, comp_unit_die ());
25224 namespace_die = new_die (is_fortran ()
25225 ? DW_TAG_module : DW_TAG_namespace,
25226 context_die, decl);
25227 /* For Fortran modules defined in different CU don't add src coords. */
25228 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25230 const char *name = dwarf2_name (decl, 0);
25231 if (name)
25232 add_name_attribute (namespace_die, name);
25234 else
25235 add_name_and_src_coords_attributes (namespace_die, decl);
25236 if (DECL_EXTERNAL (decl))
25237 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25238 equate_decl_number_to_die (decl, namespace_die);
25240 else
25242 /* Output a namespace alias. */
25244 /* Force out the namespace we are an alias of, if necessary. */
25245 dw_die_ref origin_die
25246 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25248 if (DECL_FILE_SCOPE_P (decl)
25249 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25250 context_die = setup_namespace_context (decl, comp_unit_die ());
25251 /* Now create the namespace alias DIE. */
25252 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25253 add_name_and_src_coords_attributes (namespace_die, decl);
25254 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25255 equate_decl_number_to_die (decl, namespace_die);
25257 if ((dwarf_version >= 5 || !dwarf_strict)
25258 && lang_hooks.decls.decl_dwarf_attribute (decl,
25259 DW_AT_export_symbols) == 1)
25260 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25262 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25263 if (want_pubnames ())
25264 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25267 /* Generate Dwarf debug information for a decl described by DECL.
25268 The return value is currently only meaningful for PARM_DECLs,
25269 for all other decls it returns NULL.
25271 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25272 It can be NULL otherwise. */
25274 static dw_die_ref
25275 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25276 dw_die_ref context_die)
25278 tree decl_or_origin = decl ? decl : origin;
25279 tree class_origin = NULL, ultimate_origin;
25281 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25282 return NULL;
25284 /* Ignore pointer bounds decls. */
25285 if (DECL_P (decl_or_origin)
25286 && TREE_TYPE (decl_or_origin)
25287 && POINTER_BOUNDS_P (decl_or_origin))
25288 return NULL;
25290 switch (TREE_CODE (decl_or_origin))
25292 case ERROR_MARK:
25293 break;
25295 case CONST_DECL:
25296 if (!is_fortran () && !is_ada ())
25298 /* The individual enumerators of an enum type get output when we output
25299 the Dwarf representation of the relevant enum type itself. */
25300 break;
25303 /* Emit its type. */
25304 gen_type_die (TREE_TYPE (decl), context_die);
25306 /* And its containing namespace. */
25307 context_die = declare_in_namespace (decl, context_die);
25309 gen_const_die (decl, context_die);
25310 break;
25312 case FUNCTION_DECL:
25313 #if 0
25314 /* FIXME */
25315 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25316 on local redeclarations of global functions. That seems broken. */
25317 if (current_function_decl != decl)
25318 /* This is only a declaration. */;
25319 #endif
25321 /* We should have abstract copies already and should not generate
25322 stray type DIEs in late LTO dumping. */
25323 if (! early_dwarf)
25326 /* If we're emitting a clone, emit info for the abstract instance. */
25327 else if (origin || DECL_ORIGIN (decl) != decl)
25328 dwarf2out_abstract_function (origin
25329 ? DECL_ORIGIN (origin)
25330 : DECL_ABSTRACT_ORIGIN (decl));
25332 /* If we're emitting a possibly inlined function emit it as
25333 abstract instance. */
25334 else if (cgraph_function_possibly_inlined_p (decl)
25335 && ! DECL_ABSTRACT_P (decl)
25336 && ! class_or_namespace_scope_p (context_die)
25337 /* dwarf2out_abstract_function won't emit a die if this is just
25338 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25339 that case, because that works only if we have a die. */
25340 && DECL_INITIAL (decl) != NULL_TREE)
25341 dwarf2out_abstract_function (decl);
25343 /* Otherwise we're emitting the primary DIE for this decl. */
25344 else if (debug_info_level > DINFO_LEVEL_TERSE)
25346 /* Before we describe the FUNCTION_DECL itself, make sure that we
25347 have its containing type. */
25348 if (!origin)
25349 origin = decl_class_context (decl);
25350 if (origin != NULL_TREE)
25351 gen_type_die (origin, context_die);
25353 /* And its return type. */
25354 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25356 /* And its virtual context. */
25357 if (DECL_VINDEX (decl) != NULL_TREE)
25358 gen_type_die (DECL_CONTEXT (decl), context_die);
25360 /* Make sure we have a member DIE for decl. */
25361 if (origin != NULL_TREE)
25362 gen_type_die_for_member (origin, decl, context_die);
25364 /* And its containing namespace. */
25365 context_die = declare_in_namespace (decl, context_die);
25368 /* Now output a DIE to represent the function itself. */
25369 if (decl)
25370 gen_subprogram_die (decl, context_die);
25371 break;
25373 case TYPE_DECL:
25374 /* If we are in terse mode, don't generate any DIEs to represent any
25375 actual typedefs. */
25376 if (debug_info_level <= DINFO_LEVEL_TERSE)
25377 break;
25379 /* In the special case of a TYPE_DECL node representing the declaration
25380 of some type tag, if the given TYPE_DECL is marked as having been
25381 instantiated from some other (original) TYPE_DECL node (e.g. one which
25382 was generated within the original definition of an inline function) we
25383 used to generate a special (abbreviated) DW_TAG_structure_type,
25384 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25385 should be actually referencing those DIEs, as variable DIEs with that
25386 type would be emitted already in the abstract origin, so it was always
25387 removed during unused type prunning. Don't add anything in this
25388 case. */
25389 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25390 break;
25392 if (is_redundant_typedef (decl))
25393 gen_type_die (TREE_TYPE (decl), context_die);
25394 else
25395 /* Output a DIE to represent the typedef itself. */
25396 gen_typedef_die (decl, context_die);
25397 break;
25399 case LABEL_DECL:
25400 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25401 gen_label_die (decl, context_die);
25402 break;
25404 case VAR_DECL:
25405 case RESULT_DECL:
25406 /* If we are in terse mode, don't generate any DIEs to represent any
25407 variable declarations or definitions. */
25408 if (debug_info_level <= DINFO_LEVEL_TERSE)
25409 break;
25411 /* Avoid generating stray type DIEs during late dwarf dumping.
25412 All types have been dumped early. */
25413 if (early_dwarf
25414 /* ??? But in LTRANS we cannot annotate early created variably
25415 modified type DIEs without copying them and adjusting all
25416 references to them. Dump them again as happens for inlining
25417 which copies both the decl and the types. */
25418 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25419 in VLA bound information for example. */
25420 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25421 current_function_decl)))
25423 /* Output any DIEs that are needed to specify the type of this data
25424 object. */
25425 if (decl_by_reference_p (decl_or_origin))
25426 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25427 else
25428 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25431 if (early_dwarf)
25433 /* And its containing type. */
25434 class_origin = decl_class_context (decl_or_origin);
25435 if (class_origin != NULL_TREE)
25436 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25438 /* And its containing namespace. */
25439 context_die = declare_in_namespace (decl_or_origin, context_die);
25442 /* Now output the DIE to represent the data object itself. This gets
25443 complicated because of the possibility that the VAR_DECL really
25444 represents an inlined instance of a formal parameter for an inline
25445 function. */
25446 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25447 if (ultimate_origin != NULL_TREE
25448 && TREE_CODE (ultimate_origin) == PARM_DECL)
25449 gen_formal_parameter_die (decl, origin,
25450 true /* Emit name attribute. */,
25451 context_die);
25452 else
25453 gen_variable_die (decl, origin, context_die);
25454 break;
25456 case FIELD_DECL:
25457 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25458 /* Ignore the nameless fields that are used to skip bits but handle C++
25459 anonymous unions and structs. */
25460 if (DECL_NAME (decl) != NULL_TREE
25461 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25462 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25464 gen_type_die (member_declared_type (decl), context_die);
25465 gen_field_die (decl, ctx, context_die);
25467 break;
25469 case PARM_DECL:
25470 /* Avoid generating stray type DIEs during late dwarf dumping.
25471 All types have been dumped early. */
25472 if (early_dwarf
25473 /* ??? But in LTRANS we cannot annotate early created variably
25474 modified type DIEs without copying them and adjusting all
25475 references to them. Dump them again as happens for inlining
25476 which copies both the decl and the types. */
25477 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25478 in VLA bound information for example. */
25479 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25480 current_function_decl)))
25482 if (DECL_BY_REFERENCE (decl_or_origin))
25483 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25484 else
25485 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25487 return gen_formal_parameter_die (decl, origin,
25488 true /* Emit name attribute. */,
25489 context_die);
25491 case NAMESPACE_DECL:
25492 if (dwarf_version >= 3 || !dwarf_strict)
25493 gen_namespace_die (decl, context_die);
25494 break;
25496 case IMPORTED_DECL:
25497 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25498 DECL_CONTEXT (decl), context_die);
25499 break;
25501 case NAMELIST_DECL:
25502 gen_namelist_decl (DECL_NAME (decl), context_die,
25503 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25504 break;
25506 default:
25507 /* Probably some frontend-internal decl. Assume we don't care. */
25508 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25509 break;
25512 return NULL;
25515 /* Output initial debug information for global DECL. Called at the
25516 end of the parsing process.
25518 This is the initial debug generation process. As such, the DIEs
25519 generated may be incomplete. A later debug generation pass
25520 (dwarf2out_late_global_decl) will augment the information generated
25521 in this pass (e.g., with complete location info). */
25523 static void
25524 dwarf2out_early_global_decl (tree decl)
25526 set_early_dwarf s;
25528 /* gen_decl_die() will set DECL_ABSTRACT because
25529 cgraph_function_possibly_inlined_p() returns true. This is in
25530 turn will cause DW_AT_inline attributes to be set.
25532 This happens because at early dwarf generation, there is no
25533 cgraph information, causing cgraph_function_possibly_inlined_p()
25534 to return true. Trick cgraph_function_possibly_inlined_p()
25535 while we generate dwarf early. */
25536 bool save = symtab->global_info_ready;
25537 symtab->global_info_ready = true;
25539 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25540 other DECLs and they can point to template types or other things
25541 that dwarf2out can't handle when done via dwarf2out_decl. */
25542 if (TREE_CODE (decl) != TYPE_DECL
25543 && TREE_CODE (decl) != PARM_DECL)
25545 if (TREE_CODE (decl) == FUNCTION_DECL)
25547 tree save_fndecl = current_function_decl;
25549 /* For nested functions, make sure we have DIEs for the parents first
25550 so that all nested DIEs are generated at the proper scope in the
25551 first shot. */
25552 tree context = decl_function_context (decl);
25553 if (context != NULL)
25555 dw_die_ref context_die = lookup_decl_die (context);
25556 current_function_decl = context;
25558 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25559 enough so that it lands in its own context. This avoids type
25560 pruning issues later on. */
25561 if (context_die == NULL || is_declaration_die (context_die))
25562 dwarf2out_decl (context);
25565 /* Emit an abstract origin of a function first. This happens
25566 with C++ constructor clones for example and makes
25567 dwarf2out_abstract_function happy which requires the early
25568 DIE of the abstract instance to be present. */
25569 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25570 dw_die_ref origin_die;
25571 if (origin != NULL
25572 /* Do not emit the DIE multiple times but make sure to
25573 process it fully here in case we just saw a declaration. */
25574 && ((origin_die = lookup_decl_die (origin)) == NULL
25575 || is_declaration_die (origin_die)))
25577 current_function_decl = origin;
25578 dwarf2out_decl (origin);
25581 /* Emit the DIE for decl but avoid doing that multiple times. */
25582 dw_die_ref old_die;
25583 if ((old_die = lookup_decl_die (decl)) == NULL
25584 || is_declaration_die (old_die))
25586 current_function_decl = decl;
25587 dwarf2out_decl (decl);
25590 current_function_decl = save_fndecl;
25592 else
25593 dwarf2out_decl (decl);
25595 symtab->global_info_ready = save;
25598 /* Output debug information for global decl DECL. Called from
25599 toplev.c after compilation proper has finished. */
25601 static void
25602 dwarf2out_late_global_decl (tree decl)
25604 /* Fill-in any location information we were unable to determine
25605 on the first pass. */
25606 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25608 dw_die_ref die = lookup_decl_die (decl);
25610 /* We may have to generate early debug late for LTO in case debug
25611 was not enabled at compile-time or the target doesn't support
25612 the LTO early debug scheme. */
25613 if (! die && in_lto_p)
25615 dwarf2out_decl (decl);
25616 die = lookup_decl_die (decl);
25619 if (die)
25621 /* We get called via the symtab code invoking late_global_decl
25622 for symbols that are optimized out. Do not add locations
25623 for those, except if they have a DECL_VALUE_EXPR, in which case
25624 they are relevant for debuggers. */
25625 varpool_node *node = varpool_node::get (decl);
25626 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25627 tree_add_const_value_attribute_for_decl (die, decl);
25628 else
25629 add_location_or_const_value_attribute (die, decl, false);
25634 /* Output debug information for type decl DECL. Called from toplev.c
25635 and from language front ends (to record built-in types). */
25636 static void
25637 dwarf2out_type_decl (tree decl, int local)
25639 if (!local)
25641 set_early_dwarf s;
25642 dwarf2out_decl (decl);
25646 /* Output debug information for imported module or decl DECL.
25647 NAME is non-NULL name in the lexical block if the decl has been renamed.
25648 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25649 that DECL belongs to.
25650 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25651 static void
25652 dwarf2out_imported_module_or_decl_1 (tree decl,
25653 tree name,
25654 tree lexical_block,
25655 dw_die_ref lexical_block_die)
25657 expanded_location xloc;
25658 dw_die_ref imported_die = NULL;
25659 dw_die_ref at_import_die;
25661 if (TREE_CODE (decl) == IMPORTED_DECL)
25663 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25664 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25665 gcc_assert (decl);
25667 else
25668 xloc = expand_location (input_location);
25670 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25672 at_import_die = force_type_die (TREE_TYPE (decl));
25673 /* For namespace N { typedef void T; } using N::T; base_type_die
25674 returns NULL, but DW_TAG_imported_declaration requires
25675 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25676 if (!at_import_die)
25678 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25679 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25680 at_import_die = lookup_type_die (TREE_TYPE (decl));
25681 gcc_assert (at_import_die);
25684 else
25686 at_import_die = lookup_decl_die (decl);
25687 if (!at_import_die)
25689 /* If we're trying to avoid duplicate debug info, we may not have
25690 emitted the member decl for this field. Emit it now. */
25691 if (TREE_CODE (decl) == FIELD_DECL)
25693 tree type = DECL_CONTEXT (decl);
25695 if (TYPE_CONTEXT (type)
25696 && TYPE_P (TYPE_CONTEXT (type))
25697 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25698 DINFO_USAGE_DIR_USE))
25699 return;
25700 gen_type_die_for_member (type, decl,
25701 get_context_die (TYPE_CONTEXT (type)));
25703 if (TREE_CODE (decl) == NAMELIST_DECL)
25704 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25705 get_context_die (DECL_CONTEXT (decl)),
25706 NULL_TREE);
25707 else
25708 at_import_die = force_decl_die (decl);
25712 if (TREE_CODE (decl) == NAMESPACE_DECL)
25714 if (dwarf_version >= 3 || !dwarf_strict)
25715 imported_die = new_die (DW_TAG_imported_module,
25716 lexical_block_die,
25717 lexical_block);
25718 else
25719 return;
25721 else
25722 imported_die = new_die (DW_TAG_imported_declaration,
25723 lexical_block_die,
25724 lexical_block);
25726 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25727 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25728 if (debug_column_info && xloc.column)
25729 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25730 if (name)
25731 add_AT_string (imported_die, DW_AT_name,
25732 IDENTIFIER_POINTER (name));
25733 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25736 /* Output debug information for imported module or decl DECL.
25737 NAME is non-NULL name in context if the decl has been renamed.
25738 CHILD is true if decl is one of the renamed decls as part of
25739 importing whole module.
25740 IMPLICIT is set if this hook is called for an implicit import
25741 such as inline namespace. */
25743 static void
25744 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25745 bool child, bool implicit)
25747 /* dw_die_ref at_import_die; */
25748 dw_die_ref scope_die;
25750 if (debug_info_level <= DINFO_LEVEL_TERSE)
25751 return;
25753 gcc_assert (decl);
25755 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25756 should be enough, for DWARF4 and older even if we emit as extension
25757 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25758 for the benefit of consumers unaware of DW_AT_export_symbols. */
25759 if (implicit
25760 && dwarf_version >= 5
25761 && lang_hooks.decls.decl_dwarf_attribute (decl,
25762 DW_AT_export_symbols) == 1)
25763 return;
25765 set_early_dwarf s;
25767 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25768 We need decl DIE for reference and scope die. First, get DIE for the decl
25769 itself. */
25771 /* Get the scope die for decl context. Use comp_unit_die for global module
25772 or decl. If die is not found for non globals, force new die. */
25773 if (context
25774 && TYPE_P (context)
25775 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25776 return;
25778 scope_die = get_context_die (context);
25780 if (child)
25782 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25783 there is nothing we can do, here. */
25784 if (dwarf_version < 3 && dwarf_strict)
25785 return;
25787 gcc_assert (scope_die->die_child);
25788 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25789 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25790 scope_die = scope_die->die_child;
25793 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25794 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25797 /* Output debug information for namelists. */
25799 static dw_die_ref
25800 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25802 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25803 tree value;
25804 unsigned i;
25806 if (debug_info_level <= DINFO_LEVEL_TERSE)
25807 return NULL;
25809 gcc_assert (scope_die != NULL);
25810 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25811 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25813 /* If there are no item_decls, we have a nondefining namelist, e.g.
25814 with USE association; hence, set DW_AT_declaration. */
25815 if (item_decls == NULL_TREE)
25817 add_AT_flag (nml_die, DW_AT_declaration, 1);
25818 return nml_die;
25821 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25823 nml_item_ref_die = lookup_decl_die (value);
25824 if (!nml_item_ref_die)
25825 nml_item_ref_die = force_decl_die (value);
25827 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25828 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25830 return nml_die;
25834 /* Write the debugging output for DECL and return the DIE. */
25836 static void
25837 dwarf2out_decl (tree decl)
25839 dw_die_ref context_die = comp_unit_die ();
25841 switch (TREE_CODE (decl))
25843 case ERROR_MARK:
25844 return;
25846 case FUNCTION_DECL:
25847 /* If we're a nested function, initially use a parent of NULL; if we're
25848 a plain function, this will be fixed up in decls_for_scope. If
25849 we're a method, it will be ignored, since we already have a DIE. */
25850 if (decl_function_context (decl)
25851 /* But if we're in terse mode, we don't care about scope. */
25852 && debug_info_level > DINFO_LEVEL_TERSE)
25853 context_die = NULL;
25854 break;
25856 case VAR_DECL:
25857 /* For local statics lookup proper context die. */
25858 if (local_function_static (decl))
25859 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25861 /* If we are in terse mode, don't generate any DIEs to represent any
25862 variable declarations or definitions. */
25863 if (debug_info_level <= DINFO_LEVEL_TERSE)
25864 return;
25865 break;
25867 case CONST_DECL:
25868 if (debug_info_level <= DINFO_LEVEL_TERSE)
25869 return;
25870 if (!is_fortran () && !is_ada ())
25871 return;
25872 if (TREE_STATIC (decl) && decl_function_context (decl))
25873 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25874 break;
25876 case NAMESPACE_DECL:
25877 case IMPORTED_DECL:
25878 if (debug_info_level <= DINFO_LEVEL_TERSE)
25879 return;
25880 if (lookup_decl_die (decl) != NULL)
25881 return;
25882 break;
25884 case TYPE_DECL:
25885 /* Don't emit stubs for types unless they are needed by other DIEs. */
25886 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25887 return;
25889 /* Don't bother trying to generate any DIEs to represent any of the
25890 normal built-in types for the language we are compiling. */
25891 if (DECL_IS_BUILTIN (decl))
25892 return;
25894 /* If we are in terse mode, don't generate any DIEs for types. */
25895 if (debug_info_level <= DINFO_LEVEL_TERSE)
25896 return;
25898 /* If we're a function-scope tag, initially use a parent of NULL;
25899 this will be fixed up in decls_for_scope. */
25900 if (decl_function_context (decl))
25901 context_die = NULL;
25903 break;
25905 case NAMELIST_DECL:
25906 break;
25908 default:
25909 return;
25912 gen_decl_die (decl, NULL, NULL, context_die);
25914 if (flag_checking)
25916 dw_die_ref die = lookup_decl_die (decl);
25917 if (die)
25918 check_die (die);
25922 /* Write the debugging output for DECL. */
25924 static void
25925 dwarf2out_function_decl (tree decl)
25927 dwarf2out_decl (decl);
25928 call_arg_locations = NULL;
25929 call_arg_loc_last = NULL;
25930 call_site_count = -1;
25931 tail_call_site_count = -1;
25932 decl_loc_table->empty ();
25933 cached_dw_loc_list_table->empty ();
25936 /* Output a marker (i.e. a label) for the beginning of the generated code for
25937 a lexical block. */
25939 static void
25940 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25941 unsigned int blocknum)
25943 switch_to_section (current_function_section ());
25944 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25947 /* Output a marker (i.e. a label) for the end of the generated code for a
25948 lexical block. */
25950 static void
25951 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25953 switch_to_section (current_function_section ());
25954 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25957 /* Returns nonzero if it is appropriate not to emit any debugging
25958 information for BLOCK, because it doesn't contain any instructions.
25960 Don't allow this for blocks with nested functions or local classes
25961 as we would end up with orphans, and in the presence of scheduling
25962 we may end up calling them anyway. */
25964 static bool
25965 dwarf2out_ignore_block (const_tree block)
25967 tree decl;
25968 unsigned int i;
25970 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25971 if (TREE_CODE (decl) == FUNCTION_DECL
25972 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25973 return 0;
25974 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25976 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25977 if (TREE_CODE (decl) == FUNCTION_DECL
25978 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25979 return 0;
25982 return 1;
25985 /* Hash table routines for file_hash. */
25987 bool
25988 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25990 return filename_cmp (p1->filename, p2) == 0;
25993 hashval_t
25994 dwarf_file_hasher::hash (dwarf_file_data *p)
25996 return htab_hash_string (p->filename);
25999 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26000 dwarf2out.c) and return its "index". The index of each (known) filename is
26001 just a unique number which is associated with only that one filename. We
26002 need such numbers for the sake of generating labels (in the .debug_sfnames
26003 section) and references to those files numbers (in the .debug_srcinfo
26004 and .debug_macinfo sections). If the filename given as an argument is not
26005 found in our current list, add it to the list and assign it the next
26006 available unique index number. */
26008 static struct dwarf_file_data *
26009 lookup_filename (const char *file_name)
26011 struct dwarf_file_data * created;
26013 if (!file_name)
26014 return NULL;
26016 dwarf_file_data **slot
26017 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26018 INSERT);
26019 if (*slot)
26020 return *slot;
26022 created = ggc_alloc<dwarf_file_data> ();
26023 created->filename = file_name;
26024 created->emitted_number = 0;
26025 *slot = created;
26026 return created;
26029 /* If the assembler will construct the file table, then translate the compiler
26030 internal file table number into the assembler file table number, and emit
26031 a .file directive if we haven't already emitted one yet. The file table
26032 numbers are different because we prune debug info for unused variables and
26033 types, which may include filenames. */
26035 static int
26036 maybe_emit_file (struct dwarf_file_data * fd)
26038 if (! fd->emitted_number)
26040 if (last_emitted_file)
26041 fd->emitted_number = last_emitted_file->emitted_number + 1;
26042 else
26043 fd->emitted_number = 1;
26044 last_emitted_file = fd;
26046 if (DWARF2_ASM_LINE_DEBUG_INFO)
26048 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26049 output_quoted_string (asm_out_file,
26050 remap_debug_filename (fd->filename));
26051 fputc ('\n', asm_out_file);
26055 return fd->emitted_number;
26058 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26059 That generation should happen after function debug info has been
26060 generated. The value of the attribute is the constant value of ARG. */
26062 static void
26063 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26065 die_arg_entry entry;
26067 if (!die || !arg)
26068 return;
26070 gcc_assert (early_dwarf);
26072 if (!tmpl_value_parm_die_table)
26073 vec_alloc (tmpl_value_parm_die_table, 32);
26075 entry.die = die;
26076 entry.arg = arg;
26077 vec_safe_push (tmpl_value_parm_die_table, entry);
26080 /* Return TRUE if T is an instance of generic type, FALSE
26081 otherwise. */
26083 static bool
26084 generic_type_p (tree t)
26086 if (t == NULL_TREE || !TYPE_P (t))
26087 return false;
26088 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26091 /* Schedule the generation of the generic parameter dies for the
26092 instance of generic type T. The proper generation itself is later
26093 done by gen_scheduled_generic_parms_dies. */
26095 static void
26096 schedule_generic_params_dies_gen (tree t)
26098 if (!generic_type_p (t))
26099 return;
26101 gcc_assert (early_dwarf);
26103 if (!generic_type_instances)
26104 vec_alloc (generic_type_instances, 256);
26106 vec_safe_push (generic_type_instances, t);
26109 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26110 by append_entry_to_tmpl_value_parm_die_table. This function must
26111 be called after function DIEs have been generated. */
26113 static void
26114 gen_remaining_tmpl_value_param_die_attribute (void)
26116 if (tmpl_value_parm_die_table)
26118 unsigned i, j;
26119 die_arg_entry *e;
26121 /* We do this in two phases - first get the cases we can
26122 handle during early-finish, preserving those we cannot
26123 (containing symbolic constants where we don't yet know
26124 whether we are going to output the referenced symbols).
26125 For those we try again at late-finish. */
26126 j = 0;
26127 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26129 if (!e->die->removed
26130 && !tree_add_const_value_attribute (e->die, e->arg))
26132 dw_loc_descr_ref loc = NULL;
26133 if (! early_dwarf
26134 && (dwarf_version >= 5 || !dwarf_strict))
26135 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26136 if (loc)
26137 add_AT_loc (e->die, DW_AT_location, loc);
26138 else
26139 (*tmpl_value_parm_die_table)[j++] = *e;
26142 tmpl_value_parm_die_table->truncate (j);
26146 /* Generate generic parameters DIEs for instances of generic types
26147 that have been previously scheduled by
26148 schedule_generic_params_dies_gen. This function must be called
26149 after all the types of the CU have been laid out. */
26151 static void
26152 gen_scheduled_generic_parms_dies (void)
26154 unsigned i;
26155 tree t;
26157 if (!generic_type_instances)
26158 return;
26160 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26161 if (COMPLETE_TYPE_P (t))
26162 gen_generic_params_dies (t);
26164 generic_type_instances = NULL;
26168 /* Replace DW_AT_name for the decl with name. */
26170 static void
26171 dwarf2out_set_name (tree decl, tree name)
26173 dw_die_ref die;
26174 dw_attr_node *attr;
26175 const char *dname;
26177 die = TYPE_SYMTAB_DIE (decl);
26178 if (!die)
26179 return;
26181 dname = dwarf2_name (name, 0);
26182 if (!dname)
26183 return;
26185 attr = get_AT (die, DW_AT_name);
26186 if (attr)
26188 struct indirect_string_node *node;
26190 node = find_AT_string (dname);
26191 /* replace the string. */
26192 attr->dw_attr_val.v.val_str = node;
26195 else
26196 add_name_attribute (die, dname);
26199 /* True if before or during processing of the first function being emitted. */
26200 static bool in_first_function_p = true;
26201 /* True if loc_note during dwarf2out_var_location call might still be
26202 before first real instruction at address equal to .Ltext0. */
26203 static bool maybe_at_text_label_p = true;
26204 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26205 static unsigned int first_loclabel_num_not_at_text_label;
26207 /* Called by the final INSN scan whenever we see a var location. We
26208 use it to drop labels in the right places, and throw the location in
26209 our lookup table. */
26211 static void
26212 dwarf2out_var_location (rtx_insn *loc_note)
26214 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26215 struct var_loc_node *newloc;
26216 rtx_insn *next_real, *next_note;
26217 rtx_insn *call_insn = NULL;
26218 static const char *last_label;
26219 static const char *last_postcall_label;
26220 static bool last_in_cold_section_p;
26221 static rtx_insn *expected_next_loc_note;
26222 tree decl;
26223 bool var_loc_p;
26225 if (!NOTE_P (loc_note))
26227 if (CALL_P (loc_note))
26229 call_site_count++;
26230 if (SIBLING_CALL_P (loc_note))
26231 tail_call_site_count++;
26232 if (optimize == 0 && !flag_var_tracking)
26234 /* When the var-tracking pass is not running, there is no note
26235 for indirect calls whose target is compile-time known. In this
26236 case, process such calls specifically so that we generate call
26237 sites for them anyway. */
26238 rtx x = PATTERN (loc_note);
26239 if (GET_CODE (x) == PARALLEL)
26240 x = XVECEXP (x, 0, 0);
26241 if (GET_CODE (x) == SET)
26242 x = SET_SRC (x);
26243 if (GET_CODE (x) == CALL)
26244 x = XEXP (x, 0);
26245 if (!MEM_P (x)
26246 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26247 || !SYMBOL_REF_DECL (XEXP (x, 0))
26248 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26249 != FUNCTION_DECL))
26251 call_insn = loc_note;
26252 loc_note = NULL;
26253 var_loc_p = false;
26255 next_real = next_real_insn (call_insn);
26256 next_note = NULL;
26257 cached_next_real_insn = NULL;
26258 goto create_label;
26262 return;
26265 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26266 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26267 return;
26269 /* Optimize processing a large consecutive sequence of location
26270 notes so we don't spend too much time in next_real_insn. If the
26271 next insn is another location note, remember the next_real_insn
26272 calculation for next time. */
26273 next_real = cached_next_real_insn;
26274 if (next_real)
26276 if (expected_next_loc_note != loc_note)
26277 next_real = NULL;
26280 next_note = NEXT_INSN (loc_note);
26281 if (! next_note
26282 || next_note->deleted ()
26283 || ! NOTE_P (next_note)
26284 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26285 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26286 next_note = NULL;
26288 if (! next_real)
26289 next_real = next_real_insn (loc_note);
26291 if (next_note)
26293 expected_next_loc_note = next_note;
26294 cached_next_real_insn = next_real;
26296 else
26297 cached_next_real_insn = NULL;
26299 /* If there are no instructions which would be affected by this note,
26300 don't do anything. */
26301 if (var_loc_p
26302 && next_real == NULL_RTX
26303 && !NOTE_DURING_CALL_P (loc_note))
26304 return;
26306 create_label:
26308 if (next_real == NULL_RTX)
26309 next_real = get_last_insn ();
26311 /* If there were any real insns between note we processed last time
26312 and this note (or if it is the first note), clear
26313 last_{,postcall_}label so that they are not reused this time. */
26314 if (last_var_location_insn == NULL_RTX
26315 || last_var_location_insn != next_real
26316 || last_in_cold_section_p != in_cold_section_p)
26318 last_label = NULL;
26319 last_postcall_label = NULL;
26322 if (var_loc_p)
26324 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26325 newloc = add_var_loc_to_decl (decl, loc_note,
26326 NOTE_DURING_CALL_P (loc_note)
26327 ? last_postcall_label : last_label);
26328 if (newloc == NULL)
26329 return;
26331 else
26333 decl = NULL_TREE;
26334 newloc = NULL;
26337 /* If there were no real insns between note we processed last time
26338 and this note, use the label we emitted last time. Otherwise
26339 create a new label and emit it. */
26340 if (last_label == NULL)
26342 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26343 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26344 loclabel_num++;
26345 last_label = ggc_strdup (loclabel);
26346 /* See if loclabel might be equal to .Ltext0. If yes,
26347 bump first_loclabel_num_not_at_text_label. */
26348 if (!have_multiple_function_sections
26349 && in_first_function_p
26350 && maybe_at_text_label_p)
26352 static rtx_insn *last_start;
26353 rtx_insn *insn;
26354 for (insn = loc_note; insn; insn = previous_insn (insn))
26355 if (insn == last_start)
26356 break;
26357 else if (!NONDEBUG_INSN_P (insn))
26358 continue;
26359 else
26361 rtx body = PATTERN (insn);
26362 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26363 continue;
26364 /* Inline asm could occupy zero bytes. */
26365 else if (GET_CODE (body) == ASM_INPUT
26366 || asm_noperands (body) >= 0)
26367 continue;
26368 #ifdef HAVE_attr_length
26369 else if (get_attr_min_length (insn) == 0)
26370 continue;
26371 #endif
26372 else
26374 /* Assume insn has non-zero length. */
26375 maybe_at_text_label_p = false;
26376 break;
26379 if (maybe_at_text_label_p)
26381 last_start = loc_note;
26382 first_loclabel_num_not_at_text_label = loclabel_num;
26387 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26388 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26390 if (!var_loc_p)
26392 struct call_arg_loc_node *ca_loc
26393 = ggc_cleared_alloc<call_arg_loc_node> ();
26394 rtx_insn *prev
26395 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26397 ca_loc->call_arg_loc_note = loc_note;
26398 ca_loc->next = NULL;
26399 ca_loc->label = last_label;
26400 gcc_assert (prev
26401 && (CALL_P (prev)
26402 || (NONJUMP_INSN_P (prev)
26403 && GET_CODE (PATTERN (prev)) == SEQUENCE
26404 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26405 if (!CALL_P (prev))
26406 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26407 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26409 /* Look for a SYMBOL_REF in the "prev" instruction. */
26410 rtx x = get_call_rtx_from (PATTERN (prev));
26411 if (x)
26413 /* Try to get the call symbol, if any. */
26414 if (MEM_P (XEXP (x, 0)))
26415 x = XEXP (x, 0);
26416 /* First, look for a memory access to a symbol_ref. */
26417 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26418 && SYMBOL_REF_DECL (XEXP (x, 0))
26419 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26420 ca_loc->symbol_ref = XEXP (x, 0);
26421 /* Otherwise, look at a compile-time known user-level function
26422 declaration. */
26423 else if (MEM_P (x)
26424 && MEM_EXPR (x)
26425 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26426 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26429 ca_loc->block = insn_scope (prev);
26430 if (call_arg_locations)
26431 call_arg_loc_last->next = ca_loc;
26432 else
26433 call_arg_locations = ca_loc;
26434 call_arg_loc_last = ca_loc;
26436 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26437 newloc->label = last_label;
26438 else
26440 if (!last_postcall_label)
26442 sprintf (loclabel, "%s-1", last_label);
26443 last_postcall_label = ggc_strdup (loclabel);
26445 newloc->label = last_postcall_label;
26448 last_var_location_insn = next_real;
26449 last_in_cold_section_p = in_cold_section_p;
26452 /* Called from finalize_size_functions for size functions so that their body
26453 can be encoded in the debug info to describe the layout of variable-length
26454 structures. */
26456 static void
26457 dwarf2out_size_function (tree decl)
26459 function_to_dwarf_procedure (decl);
26462 /* Note in one location list that text section has changed. */
26465 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26467 var_loc_list *list = *slot;
26468 if (list->first)
26469 list->last_before_switch
26470 = list->last->next ? list->last->next : list->last;
26471 return 1;
26474 /* Note in all location lists that text section has changed. */
26476 static void
26477 var_location_switch_text_section (void)
26479 if (decl_loc_table == NULL)
26480 return;
26482 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26485 /* Create a new line number table. */
26487 static dw_line_info_table *
26488 new_line_info_table (void)
26490 dw_line_info_table *table;
26492 table = ggc_cleared_alloc<dw_line_info_table> ();
26493 table->file_num = 1;
26494 table->line_num = 1;
26495 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26497 return table;
26500 /* Lookup the "current" table into which we emit line info, so
26501 that we don't have to do it for every source line. */
26503 static void
26504 set_cur_line_info_table (section *sec)
26506 dw_line_info_table *table;
26508 if (sec == text_section)
26509 table = text_section_line_info;
26510 else if (sec == cold_text_section)
26512 table = cold_text_section_line_info;
26513 if (!table)
26515 cold_text_section_line_info = table = new_line_info_table ();
26516 table->end_label = cold_end_label;
26519 else
26521 const char *end_label;
26523 if (crtl->has_bb_partition)
26525 if (in_cold_section_p)
26526 end_label = crtl->subsections.cold_section_end_label;
26527 else
26528 end_label = crtl->subsections.hot_section_end_label;
26530 else
26532 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26533 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26534 current_function_funcdef_no);
26535 end_label = ggc_strdup (label);
26538 table = new_line_info_table ();
26539 table->end_label = end_label;
26541 vec_safe_push (separate_line_info, table);
26544 if (DWARF2_ASM_LINE_DEBUG_INFO)
26545 table->is_stmt = (cur_line_info_table
26546 ? cur_line_info_table->is_stmt
26547 : DWARF_LINE_DEFAULT_IS_STMT_START);
26548 cur_line_info_table = table;
26552 /* We need to reset the locations at the beginning of each
26553 function. We can't do this in the end_function hook, because the
26554 declarations that use the locations won't have been output when
26555 that hook is called. Also compute have_multiple_function_sections here. */
26557 static void
26558 dwarf2out_begin_function (tree fun)
26560 section *sec = function_section (fun);
26562 if (sec != text_section)
26563 have_multiple_function_sections = true;
26565 if (crtl->has_bb_partition && !cold_text_section)
26567 gcc_assert (current_function_decl == fun);
26568 cold_text_section = unlikely_text_section ();
26569 switch_to_section (cold_text_section);
26570 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26571 switch_to_section (sec);
26574 dwarf2out_note_section_used ();
26575 call_site_count = 0;
26576 tail_call_site_count = 0;
26578 set_cur_line_info_table (sec);
26581 /* Helper function of dwarf2out_end_function, called only after emitting
26582 the very first function into assembly. Check if some .debug_loc range
26583 might end with a .LVL* label that could be equal to .Ltext0.
26584 In that case we must force using absolute addresses in .debug_loc ranges,
26585 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26586 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26587 list terminator.
26588 Set have_multiple_function_sections to true in that case and
26589 terminate htab traversal. */
26592 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26594 var_loc_list *entry = *slot;
26595 struct var_loc_node *node;
26597 node = entry->first;
26598 if (node && node->next && node->next->label)
26600 unsigned int i;
26601 const char *label = node->next->label;
26602 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26604 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26606 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26607 if (strcmp (label, loclabel) == 0)
26609 have_multiple_function_sections = true;
26610 return 0;
26614 return 1;
26617 /* Hook called after emitting a function into assembly.
26618 This does something only for the very first function emitted. */
26620 static void
26621 dwarf2out_end_function (unsigned int)
26623 if (in_first_function_p
26624 && !have_multiple_function_sections
26625 && first_loclabel_num_not_at_text_label
26626 && decl_loc_table)
26627 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26628 in_first_function_p = false;
26629 maybe_at_text_label_p = false;
26632 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26633 front-ends register a translation unit even before dwarf2out_init is
26634 called. */
26635 static tree main_translation_unit = NULL_TREE;
26637 /* Hook called by front-ends after they built their main translation unit.
26638 Associate comp_unit_die to UNIT. */
26640 static void
26641 dwarf2out_register_main_translation_unit (tree unit)
26643 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26644 && main_translation_unit == NULL_TREE);
26645 main_translation_unit = unit;
26646 /* If dwarf2out_init has not been called yet, it will perform the association
26647 itself looking at main_translation_unit. */
26648 if (decl_die_table != NULL)
26649 equate_decl_number_to_die (unit, comp_unit_die ());
26652 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26654 static void
26655 push_dw_line_info_entry (dw_line_info_table *table,
26656 enum dw_line_info_opcode opcode, unsigned int val)
26658 dw_line_info_entry e;
26659 e.opcode = opcode;
26660 e.val = val;
26661 vec_safe_push (table->entries, e);
26664 /* Output a label to mark the beginning of a source code line entry
26665 and record information relating to this source line, in
26666 'line_info_table' for later output of the .debug_line section. */
26667 /* ??? The discriminator parameter ought to be unsigned. */
26669 static void
26670 dwarf2out_source_line (unsigned int line, unsigned int column,
26671 const char *filename,
26672 int discriminator, bool is_stmt)
26674 unsigned int file_num;
26675 dw_line_info_table *table;
26677 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26678 return;
26680 /* The discriminator column was added in dwarf4. Simplify the below
26681 by simply removing it if we're not supposed to output it. */
26682 if (dwarf_version < 4 && dwarf_strict)
26683 discriminator = 0;
26685 if (!debug_column_info)
26686 column = 0;
26688 table = cur_line_info_table;
26689 file_num = maybe_emit_file (lookup_filename (filename));
26691 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26692 the debugger has used the second (possibly duplicate) line number
26693 at the beginning of the function to mark the end of the prologue.
26694 We could eliminate any other duplicates within the function. For
26695 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26696 that second line number entry. */
26697 /* Recall that this end-of-prologue indication is *not* the same thing
26698 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26699 to which the hook corresponds, follows the last insn that was
26700 emitted by gen_prologue. What we need is to precede the first insn
26701 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26702 insn that corresponds to something the user wrote. These may be
26703 very different locations once scheduling is enabled. */
26705 if (0 && file_num == table->file_num
26706 && line == table->line_num
26707 && column == table->column_num
26708 && discriminator == table->discrim_num
26709 && is_stmt == table->is_stmt)
26710 return;
26712 switch_to_section (current_function_section ());
26714 /* If requested, emit something human-readable. */
26715 if (flag_debug_asm)
26717 if (debug_column_info)
26718 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26719 filename, line, column);
26720 else
26721 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26722 filename, line);
26725 if (DWARF2_ASM_LINE_DEBUG_INFO)
26727 /* Emit the .loc directive understood by GNU as. */
26728 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26729 file_num, line, is_stmt, discriminator */
26730 fputs ("\t.loc ", asm_out_file);
26731 fprint_ul (asm_out_file, file_num);
26732 putc (' ', asm_out_file);
26733 fprint_ul (asm_out_file, line);
26734 putc (' ', asm_out_file);
26735 fprint_ul (asm_out_file, column);
26737 if (is_stmt != table->is_stmt)
26739 fputs (" is_stmt ", asm_out_file);
26740 putc (is_stmt ? '1' : '0', asm_out_file);
26742 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26744 gcc_assert (discriminator > 0);
26745 fputs (" discriminator ", asm_out_file);
26746 fprint_ul (asm_out_file, (unsigned long) discriminator);
26748 putc ('\n', asm_out_file);
26750 else
26752 unsigned int label_num = ++line_info_label_num;
26754 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26756 push_dw_line_info_entry (table, LI_set_address, label_num);
26757 if (file_num != table->file_num)
26758 push_dw_line_info_entry (table, LI_set_file, file_num);
26759 if (discriminator != table->discrim_num)
26760 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26761 if (is_stmt != table->is_stmt)
26762 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26763 push_dw_line_info_entry (table, LI_set_line, line);
26764 if (debug_column_info)
26765 push_dw_line_info_entry (table, LI_set_column, column);
26768 table->file_num = file_num;
26769 table->line_num = line;
26770 table->column_num = column;
26771 table->discrim_num = discriminator;
26772 table->is_stmt = is_stmt;
26773 table->in_use = true;
26776 /* Record the beginning of a new source file. */
26778 static void
26779 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26781 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26783 macinfo_entry e;
26784 e.code = DW_MACINFO_start_file;
26785 e.lineno = lineno;
26786 e.info = ggc_strdup (filename);
26787 vec_safe_push (macinfo_table, e);
26791 /* Record the end of a source file. */
26793 static void
26794 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26796 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26798 macinfo_entry e;
26799 e.code = DW_MACINFO_end_file;
26800 e.lineno = lineno;
26801 e.info = NULL;
26802 vec_safe_push (macinfo_table, e);
26806 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26807 the tail part of the directive line, i.e. the part which is past the
26808 initial whitespace, #, whitespace, directive-name, whitespace part. */
26810 static void
26811 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26812 const char *buffer ATTRIBUTE_UNUSED)
26814 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26816 macinfo_entry e;
26817 /* Insert a dummy first entry to be able to optimize the whole
26818 predefined macro block using DW_MACRO_import. */
26819 if (macinfo_table->is_empty () && lineno <= 1)
26821 e.code = 0;
26822 e.lineno = 0;
26823 e.info = NULL;
26824 vec_safe_push (macinfo_table, e);
26826 e.code = DW_MACINFO_define;
26827 e.lineno = lineno;
26828 e.info = ggc_strdup (buffer);
26829 vec_safe_push (macinfo_table, e);
26833 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26834 the tail part of the directive line, i.e. the part which is past the
26835 initial whitespace, #, whitespace, directive-name, whitespace part. */
26837 static void
26838 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26839 const char *buffer ATTRIBUTE_UNUSED)
26841 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26843 macinfo_entry e;
26844 /* Insert a dummy first entry to be able to optimize the whole
26845 predefined macro block using DW_MACRO_import. */
26846 if (macinfo_table->is_empty () && lineno <= 1)
26848 e.code = 0;
26849 e.lineno = 0;
26850 e.info = NULL;
26851 vec_safe_push (macinfo_table, e);
26853 e.code = DW_MACINFO_undef;
26854 e.lineno = lineno;
26855 e.info = ggc_strdup (buffer);
26856 vec_safe_push (macinfo_table, e);
26860 /* Helpers to manipulate hash table of CUs. */
26862 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26864 static inline hashval_t hash (const macinfo_entry *);
26865 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26868 inline hashval_t
26869 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26871 return htab_hash_string (entry->info);
26874 inline bool
26875 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26876 const macinfo_entry *entry2)
26878 return !strcmp (entry1->info, entry2->info);
26881 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26883 /* Output a single .debug_macinfo entry. */
26885 static void
26886 output_macinfo_op (macinfo_entry *ref)
26888 int file_num;
26889 size_t len;
26890 struct indirect_string_node *node;
26891 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26892 struct dwarf_file_data *fd;
26894 switch (ref->code)
26896 case DW_MACINFO_start_file:
26897 fd = lookup_filename (ref->info);
26898 file_num = maybe_emit_file (fd);
26899 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26900 dw2_asm_output_data_uleb128 (ref->lineno,
26901 "Included from line number %lu",
26902 (unsigned long) ref->lineno);
26903 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26904 break;
26905 case DW_MACINFO_end_file:
26906 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26907 break;
26908 case DW_MACINFO_define:
26909 case DW_MACINFO_undef:
26910 len = strlen (ref->info) + 1;
26911 if (!dwarf_strict
26912 && len > DWARF_OFFSET_SIZE
26913 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26914 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26916 ref->code = ref->code == DW_MACINFO_define
26917 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26918 output_macinfo_op (ref);
26919 return;
26921 dw2_asm_output_data (1, ref->code,
26922 ref->code == DW_MACINFO_define
26923 ? "Define macro" : "Undefine macro");
26924 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26925 (unsigned long) ref->lineno);
26926 dw2_asm_output_nstring (ref->info, -1, "The macro");
26927 break;
26928 case DW_MACRO_define_strp:
26929 case DW_MACRO_undef_strp:
26930 node = find_AT_string (ref->info);
26931 gcc_assert (node
26932 && (node->form == DW_FORM_strp
26933 || node->form == DW_FORM_GNU_str_index));
26934 dw2_asm_output_data (1, ref->code,
26935 ref->code == DW_MACRO_define_strp
26936 ? "Define macro strp"
26937 : "Undefine macro strp");
26938 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26939 (unsigned long) ref->lineno);
26940 if (node->form == DW_FORM_strp)
26941 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26942 debug_str_section, "The macro: \"%s\"",
26943 ref->info);
26944 else
26945 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26946 ref->info);
26947 break;
26948 case DW_MACRO_import:
26949 dw2_asm_output_data (1, ref->code, "Import");
26950 ASM_GENERATE_INTERNAL_LABEL (label,
26951 DEBUG_MACRO_SECTION_LABEL,
26952 ref->lineno + macinfo_label_base);
26953 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26954 break;
26955 default:
26956 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26957 ASM_COMMENT_START, (unsigned long) ref->code);
26958 break;
26962 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26963 other compilation unit .debug_macinfo sections. IDX is the first
26964 index of a define/undef, return the number of ops that should be
26965 emitted in a comdat .debug_macinfo section and emit
26966 a DW_MACRO_import entry referencing it.
26967 If the define/undef entry should be emitted normally, return 0. */
26969 static unsigned
26970 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26971 macinfo_hash_type **macinfo_htab)
26973 macinfo_entry *first, *second, *cur, *inc;
26974 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26975 unsigned char checksum[16];
26976 struct md5_ctx ctx;
26977 char *grp_name, *tail;
26978 const char *base;
26979 unsigned int i, count, encoded_filename_len, linebuf_len;
26980 macinfo_entry **slot;
26982 first = &(*macinfo_table)[idx];
26983 second = &(*macinfo_table)[idx + 1];
26985 /* Optimize only if there are at least two consecutive define/undef ops,
26986 and either all of them are before first DW_MACINFO_start_file
26987 with lineno {0,1} (i.e. predefined macro block), or all of them are
26988 in some included header file. */
26989 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26990 return 0;
26991 if (vec_safe_is_empty (files))
26993 if (first->lineno > 1 || second->lineno > 1)
26994 return 0;
26996 else if (first->lineno == 0)
26997 return 0;
26999 /* Find the last define/undef entry that can be grouped together
27000 with first and at the same time compute md5 checksum of their
27001 codes, linenumbers and strings. */
27002 md5_init_ctx (&ctx);
27003 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27004 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27005 break;
27006 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27007 break;
27008 else
27010 unsigned char code = cur->code;
27011 md5_process_bytes (&code, 1, &ctx);
27012 checksum_uleb128 (cur->lineno, &ctx);
27013 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27015 md5_finish_ctx (&ctx, checksum);
27016 count = i - idx;
27018 /* From the containing include filename (if any) pick up just
27019 usable characters from its basename. */
27020 if (vec_safe_is_empty (files))
27021 base = "";
27022 else
27023 base = lbasename (files->last ().info);
27024 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27025 if (ISIDNUM (base[i]) || base[i] == '.')
27026 encoded_filename_len++;
27027 /* Count . at the end. */
27028 if (encoded_filename_len)
27029 encoded_filename_len++;
27031 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27032 linebuf_len = strlen (linebuf);
27034 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27035 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27036 + 16 * 2 + 1);
27037 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27038 tail = grp_name + 4;
27039 if (encoded_filename_len)
27041 for (i = 0; base[i]; i++)
27042 if (ISIDNUM (base[i]) || base[i] == '.')
27043 *tail++ = base[i];
27044 *tail++ = '.';
27046 memcpy (tail, linebuf, linebuf_len);
27047 tail += linebuf_len;
27048 *tail++ = '.';
27049 for (i = 0; i < 16; i++)
27050 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27052 /* Construct a macinfo_entry for DW_MACRO_import
27053 in the empty vector entry before the first define/undef. */
27054 inc = &(*macinfo_table)[idx - 1];
27055 inc->code = DW_MACRO_import;
27056 inc->lineno = 0;
27057 inc->info = ggc_strdup (grp_name);
27058 if (!*macinfo_htab)
27059 *macinfo_htab = new macinfo_hash_type (10);
27060 /* Avoid emitting duplicates. */
27061 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27062 if (*slot != NULL)
27064 inc->code = 0;
27065 inc->info = NULL;
27066 /* If such an entry has been used before, just emit
27067 a DW_MACRO_import op. */
27068 inc = *slot;
27069 output_macinfo_op (inc);
27070 /* And clear all macinfo_entry in the range to avoid emitting them
27071 in the second pass. */
27072 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27074 cur->code = 0;
27075 cur->info = NULL;
27078 else
27080 *slot = inc;
27081 inc->lineno = (*macinfo_htab)->elements ();
27082 output_macinfo_op (inc);
27084 return count;
27087 /* Save any strings needed by the macinfo table in the debug str
27088 table. All strings must be collected into the table by the time
27089 index_string is called. */
27091 static void
27092 save_macinfo_strings (void)
27094 unsigned len;
27095 unsigned i;
27096 macinfo_entry *ref;
27098 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27100 switch (ref->code)
27102 /* Match the logic in output_macinfo_op to decide on
27103 indirect strings. */
27104 case DW_MACINFO_define:
27105 case DW_MACINFO_undef:
27106 len = strlen (ref->info) + 1;
27107 if (!dwarf_strict
27108 && len > DWARF_OFFSET_SIZE
27109 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27110 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27111 set_indirect_string (find_AT_string (ref->info));
27112 break;
27113 case DW_MACRO_define_strp:
27114 case DW_MACRO_undef_strp:
27115 set_indirect_string (find_AT_string (ref->info));
27116 break;
27117 default:
27118 break;
27123 /* Output macinfo section(s). */
27125 static void
27126 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27128 unsigned i;
27129 unsigned long length = vec_safe_length (macinfo_table);
27130 macinfo_entry *ref;
27131 vec<macinfo_entry, va_gc> *files = NULL;
27132 macinfo_hash_type *macinfo_htab = NULL;
27133 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27135 if (! length)
27136 return;
27138 /* output_macinfo* uses these interchangeably. */
27139 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27140 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27141 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27142 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27144 /* AIX Assembler inserts the length, so adjust the reference to match the
27145 offset expected by debuggers. */
27146 strcpy (dl_section_ref, debug_line_label);
27147 if (XCOFF_DEBUGGING_INFO)
27148 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27150 /* For .debug_macro emit the section header. */
27151 if (!dwarf_strict || dwarf_version >= 5)
27153 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27154 "DWARF macro version number");
27155 if (DWARF_OFFSET_SIZE == 8)
27156 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27157 else
27158 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27159 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27160 debug_line_section, NULL);
27163 /* In the first loop, it emits the primary .debug_macinfo section
27164 and after each emitted op the macinfo_entry is cleared.
27165 If a longer range of define/undef ops can be optimized using
27166 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27167 the vector before the first define/undef in the range and the
27168 whole range of define/undef ops is not emitted and kept. */
27169 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27171 switch (ref->code)
27173 case DW_MACINFO_start_file:
27174 vec_safe_push (files, *ref);
27175 break;
27176 case DW_MACINFO_end_file:
27177 if (!vec_safe_is_empty (files))
27178 files->pop ();
27179 break;
27180 case DW_MACINFO_define:
27181 case DW_MACINFO_undef:
27182 if ((!dwarf_strict || dwarf_version >= 5)
27183 && HAVE_COMDAT_GROUP
27184 && vec_safe_length (files) != 1
27185 && i > 0
27186 && i + 1 < length
27187 && (*macinfo_table)[i - 1].code == 0)
27189 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27190 if (count)
27192 i += count - 1;
27193 continue;
27196 break;
27197 case 0:
27198 /* A dummy entry may be inserted at the beginning to be able
27199 to optimize the whole block of predefined macros. */
27200 if (i == 0)
27201 continue;
27202 default:
27203 break;
27205 output_macinfo_op (ref);
27206 ref->info = NULL;
27207 ref->code = 0;
27210 if (!macinfo_htab)
27211 return;
27213 /* Save the number of transparent includes so we can adjust the
27214 label number for the fat LTO object DWARF. */
27215 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27217 delete macinfo_htab;
27218 macinfo_htab = NULL;
27220 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27221 terminate the current chain and switch to a new comdat .debug_macinfo
27222 section and emit the define/undef entries within it. */
27223 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27224 switch (ref->code)
27226 case 0:
27227 continue;
27228 case DW_MACRO_import:
27230 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27231 tree comdat_key = get_identifier (ref->info);
27232 /* Terminate the previous .debug_macinfo section. */
27233 dw2_asm_output_data (1, 0, "End compilation unit");
27234 targetm.asm_out.named_section (debug_macinfo_section_name,
27235 SECTION_DEBUG
27236 | SECTION_LINKONCE
27237 | (early_lto_debug
27238 ? SECTION_EXCLUDE : 0),
27239 comdat_key);
27240 ASM_GENERATE_INTERNAL_LABEL (label,
27241 DEBUG_MACRO_SECTION_LABEL,
27242 ref->lineno + macinfo_label_base);
27243 ASM_OUTPUT_LABEL (asm_out_file, label);
27244 ref->code = 0;
27245 ref->info = NULL;
27246 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27247 "DWARF macro version number");
27248 if (DWARF_OFFSET_SIZE == 8)
27249 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27250 else
27251 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27253 break;
27254 case DW_MACINFO_define:
27255 case DW_MACINFO_undef:
27256 output_macinfo_op (ref);
27257 ref->code = 0;
27258 ref->info = NULL;
27259 break;
27260 default:
27261 gcc_unreachable ();
27264 macinfo_label_base += macinfo_label_base_adj;
27267 /* Initialize the various sections and labels for dwarf output and prefix
27268 them with PREFIX if non-NULL. Returns the generation (zero based
27269 number of times function was called). */
27271 static unsigned
27272 init_sections_and_labels (bool early_lto_debug)
27274 /* As we may get called multiple times have a generation count for
27275 labels. */
27276 static unsigned generation = 0;
27278 if (early_lto_debug)
27280 if (!dwarf_split_debug_info)
27282 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27283 SECTION_DEBUG | SECTION_EXCLUDE,
27284 NULL);
27285 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27286 SECTION_DEBUG | SECTION_EXCLUDE,
27287 NULL);
27288 debug_macinfo_section_name
27289 = ((dwarf_strict && dwarf_version < 5)
27290 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27291 debug_macinfo_section = get_section (debug_macinfo_section_name,
27292 SECTION_DEBUG
27293 | SECTION_EXCLUDE, NULL);
27294 /* For macro info we have to refer to a debug_line section, so
27295 similar to split-dwarf emit a skeleton one for early debug. */
27296 debug_skeleton_line_section
27297 = get_section (DEBUG_LTO_LINE_SECTION,
27298 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27299 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27300 DEBUG_SKELETON_LINE_SECTION_LABEL,
27301 generation);
27303 else
27305 /* ??? Which of the following do we need early? */
27306 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27307 SECTION_DEBUG | SECTION_EXCLUDE,
27308 NULL);
27309 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27310 SECTION_DEBUG | SECTION_EXCLUDE,
27311 NULL);
27312 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27313 SECTION_DEBUG
27314 | SECTION_EXCLUDE, NULL);
27315 debug_skeleton_abbrev_section
27316 = get_section (DEBUG_LTO_ABBREV_SECTION,
27317 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27318 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27319 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27320 generation);
27322 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27323 stay in the main .o, but the skeleton_line goes into the split
27324 off dwo. */
27325 debug_skeleton_line_section
27326 = get_section (DEBUG_LTO_LINE_SECTION,
27327 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27328 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27329 DEBUG_SKELETON_LINE_SECTION_LABEL,
27330 generation);
27331 debug_str_offsets_section
27332 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27333 SECTION_DEBUG | SECTION_EXCLUDE,
27334 NULL);
27335 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27336 DEBUG_SKELETON_INFO_SECTION_LABEL,
27337 generation);
27338 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27339 DEBUG_STR_DWO_SECTION_FLAGS,
27340 NULL);
27341 debug_macinfo_section_name
27342 = ((dwarf_strict && dwarf_version < 5)
27343 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27344 debug_macinfo_section = get_section (debug_macinfo_section_name,
27345 SECTION_DEBUG | SECTION_EXCLUDE,
27346 NULL);
27348 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27349 DEBUG_STR_SECTION_FLAGS
27350 | SECTION_EXCLUDE, NULL);
27351 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27352 debug_line_str_section
27353 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27354 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27356 else
27358 if (!dwarf_split_debug_info)
27360 debug_info_section = get_section (DEBUG_INFO_SECTION,
27361 SECTION_DEBUG, NULL);
27362 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27363 SECTION_DEBUG, NULL);
27364 debug_loc_section = get_section (dwarf_version >= 5
27365 ? DEBUG_LOCLISTS_SECTION
27366 : DEBUG_LOC_SECTION,
27367 SECTION_DEBUG, NULL);
27368 debug_macinfo_section_name
27369 = ((dwarf_strict && dwarf_version < 5)
27370 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27371 debug_macinfo_section = get_section (debug_macinfo_section_name,
27372 SECTION_DEBUG, NULL);
27374 else
27376 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27377 SECTION_DEBUG | SECTION_EXCLUDE,
27378 NULL);
27379 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27380 SECTION_DEBUG | SECTION_EXCLUDE,
27381 NULL);
27382 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27383 SECTION_DEBUG, NULL);
27384 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27385 SECTION_DEBUG, NULL);
27386 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27387 SECTION_DEBUG, NULL);
27388 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27389 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27390 generation);
27392 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27393 stay in the main .o, but the skeleton_line goes into the
27394 split off dwo. */
27395 debug_skeleton_line_section
27396 = get_section (DEBUG_DWO_LINE_SECTION,
27397 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27398 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27399 DEBUG_SKELETON_LINE_SECTION_LABEL,
27400 generation);
27401 debug_str_offsets_section
27402 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27403 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27404 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27405 DEBUG_SKELETON_INFO_SECTION_LABEL,
27406 generation);
27407 debug_loc_section = get_section (dwarf_version >= 5
27408 ? DEBUG_DWO_LOCLISTS_SECTION
27409 : DEBUG_DWO_LOC_SECTION,
27410 SECTION_DEBUG | SECTION_EXCLUDE,
27411 NULL);
27412 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27413 DEBUG_STR_DWO_SECTION_FLAGS,
27414 NULL);
27415 debug_macinfo_section_name
27416 = ((dwarf_strict && dwarf_version < 5)
27417 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27418 debug_macinfo_section = get_section (debug_macinfo_section_name,
27419 SECTION_DEBUG | SECTION_EXCLUDE,
27420 NULL);
27422 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27423 SECTION_DEBUG, NULL);
27424 debug_line_section = get_section (DEBUG_LINE_SECTION,
27425 SECTION_DEBUG, NULL);
27426 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27427 SECTION_DEBUG, NULL);
27428 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27429 SECTION_DEBUG, NULL);
27430 debug_str_section = get_section (DEBUG_STR_SECTION,
27431 DEBUG_STR_SECTION_FLAGS, NULL);
27432 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27433 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27434 DEBUG_STR_SECTION_FLAGS, NULL);
27435 debug_ranges_section = get_section (dwarf_version >= 5
27436 ? DEBUG_RNGLISTS_SECTION
27437 : DEBUG_RANGES_SECTION,
27438 SECTION_DEBUG, NULL);
27439 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27440 SECTION_DEBUG, NULL);
27443 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27444 DEBUG_ABBREV_SECTION_LABEL, generation);
27445 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27446 DEBUG_INFO_SECTION_LABEL, generation);
27447 info_section_emitted = false;
27448 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27449 DEBUG_LINE_SECTION_LABEL, generation);
27450 /* There are up to 4 unique ranges labels per generation.
27451 See also output_rnglists. */
27452 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27453 DEBUG_RANGES_SECTION_LABEL, generation * 4);
27454 if (dwarf_version >= 5 && dwarf_split_debug_info)
27455 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27456 DEBUG_RANGES_SECTION_LABEL,
27457 1 + generation * 4);
27458 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27459 DEBUG_ADDR_SECTION_LABEL, generation);
27460 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27461 (dwarf_strict && dwarf_version < 5)
27462 ? DEBUG_MACINFO_SECTION_LABEL
27463 : DEBUG_MACRO_SECTION_LABEL, generation);
27464 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27465 generation);
27467 ++generation;
27468 return generation - 1;
27471 /* Set up for Dwarf output at the start of compilation. */
27473 static void
27474 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27476 /* Allocate the file_table. */
27477 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27479 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27480 /* Allocate the decl_die_table. */
27481 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27483 /* Allocate the decl_loc_table. */
27484 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27486 /* Allocate the cached_dw_loc_list_table. */
27487 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27489 /* Allocate the initial hunk of the decl_scope_table. */
27490 vec_alloc (decl_scope_table, 256);
27492 /* Allocate the initial hunk of the abbrev_die_table. */
27493 vec_alloc (abbrev_die_table, 256);
27494 /* Zero-th entry is allocated, but unused. */
27495 abbrev_die_table->quick_push (NULL);
27497 /* Allocate the dwarf_proc_stack_usage_map. */
27498 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27500 /* Allocate the pubtypes and pubnames vectors. */
27501 vec_alloc (pubname_table, 32);
27502 vec_alloc (pubtype_table, 32);
27504 vec_alloc (incomplete_types, 64);
27506 vec_alloc (used_rtx_array, 32);
27508 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27509 vec_alloc (macinfo_table, 64);
27510 #endif
27512 /* If front-ends already registered a main translation unit but we were not
27513 ready to perform the association, do this now. */
27514 if (main_translation_unit != NULL_TREE)
27515 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27518 /* Called before compile () starts outputtting functions, variables
27519 and toplevel asms into assembly. */
27521 static void
27522 dwarf2out_assembly_start (void)
27524 if (text_section_line_info)
27525 return;
27527 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27528 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27529 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27530 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27531 COLD_TEXT_SECTION_LABEL, 0);
27532 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27534 switch_to_section (text_section);
27535 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27536 #endif
27538 /* Make sure the line number table for .text always exists. */
27539 text_section_line_info = new_line_info_table ();
27540 text_section_line_info->end_label = text_end_label;
27542 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27543 cur_line_info_table = text_section_line_info;
27544 #endif
27546 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27547 && dwarf2out_do_cfi_asm ()
27548 && !dwarf2out_do_eh_frame ())
27549 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27552 /* A helper function for dwarf2out_finish called through
27553 htab_traverse. Assign a string its index. All strings must be
27554 collected into the table by the time index_string is called,
27555 because the indexing code relies on htab_traverse to traverse nodes
27556 in the same order for each run. */
27559 index_string (indirect_string_node **h, unsigned int *index)
27561 indirect_string_node *node = *h;
27563 find_string_form (node);
27564 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27566 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27567 node->index = *index;
27568 *index += 1;
27570 return 1;
27573 /* A helper function for output_indirect_strings called through
27574 htab_traverse. Output the offset to a string and update the
27575 current offset. */
27578 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27580 indirect_string_node *node = *h;
27582 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27584 /* Assert that this node has been assigned an index. */
27585 gcc_assert (node->index != NO_INDEX_ASSIGNED
27586 && node->index != NOT_INDEXED);
27587 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27588 "indexed string 0x%x: %s", node->index, node->str);
27589 *offset += strlen (node->str) + 1;
27591 return 1;
27594 /* A helper function for dwarf2out_finish called through
27595 htab_traverse. Output the indexed string. */
27598 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27600 struct indirect_string_node *node = *h;
27602 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27604 /* Assert that the strings are output in the same order as their
27605 indexes were assigned. */
27606 gcc_assert (*cur_idx == node->index);
27607 assemble_string (node->str, strlen (node->str) + 1);
27608 *cur_idx += 1;
27610 return 1;
27613 /* A helper function for dwarf2out_finish called through
27614 htab_traverse. Emit one queued .debug_str string. */
27617 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27619 struct indirect_string_node *node = *h;
27621 node->form = find_string_form (node);
27622 if (node->form == form && node->refcount > 0)
27624 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27625 assemble_string (node->str, strlen (node->str) + 1);
27628 return 1;
27631 /* Output the indexed string table. */
27633 static void
27634 output_indirect_strings (void)
27636 switch_to_section (debug_str_section);
27637 if (!dwarf_split_debug_info)
27638 debug_str_hash->traverse<enum dwarf_form,
27639 output_indirect_string> (DW_FORM_strp);
27640 else
27642 unsigned int offset = 0;
27643 unsigned int cur_idx = 0;
27645 skeleton_debug_str_hash->traverse<enum dwarf_form,
27646 output_indirect_string> (DW_FORM_strp);
27648 switch_to_section (debug_str_offsets_section);
27649 debug_str_hash->traverse_noresize
27650 <unsigned int *, output_index_string_offset> (&offset);
27651 switch_to_section (debug_str_dwo_section);
27652 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27653 (&cur_idx);
27657 /* Callback for htab_traverse to assign an index to an entry in the
27658 table, and to write that entry to the .debug_addr section. */
27661 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27663 addr_table_entry *entry = *slot;
27665 if (entry->refcount == 0)
27667 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27668 || entry->index == NOT_INDEXED);
27669 return 1;
27672 gcc_assert (entry->index == *cur_index);
27673 (*cur_index)++;
27675 switch (entry->kind)
27677 case ate_kind_rtx:
27678 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27679 "0x%x", entry->index);
27680 break;
27681 case ate_kind_rtx_dtprel:
27682 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27683 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27684 DWARF2_ADDR_SIZE,
27685 entry->addr.rtl);
27686 fputc ('\n', asm_out_file);
27687 break;
27688 case ate_kind_label:
27689 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27690 "0x%x", entry->index);
27691 break;
27692 default:
27693 gcc_unreachable ();
27695 return 1;
27698 /* Produce the .debug_addr section. */
27700 static void
27701 output_addr_table (void)
27703 unsigned int index = 0;
27704 if (addr_index_table == NULL || addr_index_table->size () == 0)
27705 return;
27707 switch_to_section (debug_addr_section);
27708 addr_index_table
27709 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27712 #if ENABLE_ASSERT_CHECKING
27713 /* Verify that all marks are clear. */
27715 static void
27716 verify_marks_clear (dw_die_ref die)
27718 dw_die_ref c;
27720 gcc_assert (! die->die_mark);
27721 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27723 #endif /* ENABLE_ASSERT_CHECKING */
27725 /* Clear the marks for a die and its children.
27726 Be cool if the mark isn't set. */
27728 static void
27729 prune_unmark_dies (dw_die_ref die)
27731 dw_die_ref c;
27733 if (die->die_mark)
27734 die->die_mark = 0;
27735 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27738 /* Given LOC that is referenced by a DIE we're marking as used, find all
27739 referenced DWARF procedures it references and mark them as used. */
27741 static void
27742 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27744 for (; loc != NULL; loc = loc->dw_loc_next)
27745 switch (loc->dw_loc_opc)
27747 case DW_OP_implicit_pointer:
27748 case DW_OP_convert:
27749 case DW_OP_reinterpret:
27750 case DW_OP_GNU_implicit_pointer:
27751 case DW_OP_GNU_convert:
27752 case DW_OP_GNU_reinterpret:
27753 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27754 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27755 break;
27756 case DW_OP_GNU_variable_value:
27757 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27759 dw_die_ref ref
27760 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27761 if (ref == NULL)
27762 break;
27763 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27764 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27765 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27767 /* FALLTHRU */
27768 case DW_OP_call2:
27769 case DW_OP_call4:
27770 case DW_OP_call_ref:
27771 case DW_OP_const_type:
27772 case DW_OP_GNU_const_type:
27773 case DW_OP_GNU_parameter_ref:
27774 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27775 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27776 break;
27777 case DW_OP_regval_type:
27778 case DW_OP_deref_type:
27779 case DW_OP_GNU_regval_type:
27780 case DW_OP_GNU_deref_type:
27781 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27782 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27783 break;
27784 case DW_OP_entry_value:
27785 case DW_OP_GNU_entry_value:
27786 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27787 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27788 break;
27789 default:
27790 break;
27794 /* Given DIE that we're marking as used, find any other dies
27795 it references as attributes and mark them as used. */
27797 static void
27798 prune_unused_types_walk_attribs (dw_die_ref die)
27800 dw_attr_node *a;
27801 unsigned ix;
27803 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27805 switch (AT_class (a))
27807 /* Make sure DWARF procedures referenced by location descriptions will
27808 get emitted. */
27809 case dw_val_class_loc:
27810 prune_unused_types_walk_loc_descr (AT_loc (a));
27811 break;
27812 case dw_val_class_loc_list:
27813 for (dw_loc_list_ref list = AT_loc_list (a);
27814 list != NULL;
27815 list = list->dw_loc_next)
27816 prune_unused_types_walk_loc_descr (list->expr);
27817 break;
27819 case dw_val_class_die_ref:
27820 /* A reference to another DIE.
27821 Make sure that it will get emitted.
27822 If it was broken out into a comdat group, don't follow it. */
27823 if (! AT_ref (a)->comdat_type_p
27824 || a->dw_attr == DW_AT_specification)
27825 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27826 break;
27828 case dw_val_class_str:
27829 /* Set the string's refcount to 0 so that prune_unused_types_mark
27830 accounts properly for it. */
27831 a->dw_attr_val.v.val_str->refcount = 0;
27832 break;
27834 default:
27835 break;
27840 /* Mark the generic parameters and arguments children DIEs of DIE. */
27842 static void
27843 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27845 dw_die_ref c;
27847 if (die == NULL || die->die_child == NULL)
27848 return;
27849 c = die->die_child;
27852 if (is_template_parameter (c))
27853 prune_unused_types_mark (c, 1);
27854 c = c->die_sib;
27855 } while (c && c != die->die_child);
27858 /* Mark DIE as being used. If DOKIDS is true, then walk down
27859 to DIE's children. */
27861 static void
27862 prune_unused_types_mark (dw_die_ref die, int dokids)
27864 dw_die_ref c;
27866 if (die->die_mark == 0)
27868 /* We haven't done this node yet. Mark it as used. */
27869 die->die_mark = 1;
27870 /* If this is the DIE of a generic type instantiation,
27871 mark the children DIEs that describe its generic parms and
27872 args. */
27873 prune_unused_types_mark_generic_parms_dies (die);
27875 /* We also have to mark its parents as used.
27876 (But we don't want to mark our parent's kids due to this,
27877 unless it is a class.) */
27878 if (die->die_parent)
27879 prune_unused_types_mark (die->die_parent,
27880 class_scope_p (die->die_parent));
27882 /* Mark any referenced nodes. */
27883 prune_unused_types_walk_attribs (die);
27885 /* If this node is a specification,
27886 also mark the definition, if it exists. */
27887 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27888 prune_unused_types_mark (die->die_definition, 1);
27891 if (dokids && die->die_mark != 2)
27893 /* We need to walk the children, but haven't done so yet.
27894 Remember that we've walked the kids. */
27895 die->die_mark = 2;
27897 /* If this is an array type, we need to make sure our
27898 kids get marked, even if they're types. If we're
27899 breaking out types into comdat sections, do this
27900 for all type definitions. */
27901 if (die->die_tag == DW_TAG_array_type
27902 || (use_debug_types
27903 && is_type_die (die) && ! is_declaration_die (die)))
27904 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27905 else
27906 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27910 /* For local classes, look if any static member functions were emitted
27911 and if so, mark them. */
27913 static void
27914 prune_unused_types_walk_local_classes (dw_die_ref die)
27916 dw_die_ref c;
27918 if (die->die_mark == 2)
27919 return;
27921 switch (die->die_tag)
27923 case DW_TAG_structure_type:
27924 case DW_TAG_union_type:
27925 case DW_TAG_class_type:
27926 break;
27928 case DW_TAG_subprogram:
27929 if (!get_AT_flag (die, DW_AT_declaration)
27930 || die->die_definition != NULL)
27931 prune_unused_types_mark (die, 1);
27932 return;
27934 default:
27935 return;
27938 /* Mark children. */
27939 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27942 /* Walk the tree DIE and mark types that we actually use. */
27944 static void
27945 prune_unused_types_walk (dw_die_ref die)
27947 dw_die_ref c;
27949 /* Don't do anything if this node is already marked and
27950 children have been marked as well. */
27951 if (die->die_mark == 2)
27952 return;
27954 switch (die->die_tag)
27956 case DW_TAG_structure_type:
27957 case DW_TAG_union_type:
27958 case DW_TAG_class_type:
27959 if (die->die_perennial_p)
27960 break;
27962 for (c = die->die_parent; c; c = c->die_parent)
27963 if (c->die_tag == DW_TAG_subprogram)
27964 break;
27966 /* Finding used static member functions inside of classes
27967 is needed just for local classes, because for other classes
27968 static member function DIEs with DW_AT_specification
27969 are emitted outside of the DW_TAG_*_type. If we ever change
27970 it, we'd need to call this even for non-local classes. */
27971 if (c)
27972 prune_unused_types_walk_local_classes (die);
27974 /* It's a type node --- don't mark it. */
27975 return;
27977 case DW_TAG_const_type:
27978 case DW_TAG_packed_type:
27979 case DW_TAG_pointer_type:
27980 case DW_TAG_reference_type:
27981 case DW_TAG_rvalue_reference_type:
27982 case DW_TAG_volatile_type:
27983 case DW_TAG_typedef:
27984 case DW_TAG_array_type:
27985 case DW_TAG_interface_type:
27986 case DW_TAG_friend:
27987 case DW_TAG_enumeration_type:
27988 case DW_TAG_subroutine_type:
27989 case DW_TAG_string_type:
27990 case DW_TAG_set_type:
27991 case DW_TAG_subrange_type:
27992 case DW_TAG_ptr_to_member_type:
27993 case DW_TAG_file_type:
27994 /* Type nodes are useful only when other DIEs reference them --- don't
27995 mark them. */
27996 /* FALLTHROUGH */
27998 case DW_TAG_dwarf_procedure:
27999 /* Likewise for DWARF procedures. */
28001 if (die->die_perennial_p)
28002 break;
28004 return;
28006 default:
28007 /* Mark everything else. */
28008 break;
28011 if (die->die_mark == 0)
28013 die->die_mark = 1;
28015 /* Now, mark any dies referenced from here. */
28016 prune_unused_types_walk_attribs (die);
28019 die->die_mark = 2;
28021 /* Mark children. */
28022 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28025 /* Increment the string counts on strings referred to from DIE's
28026 attributes. */
28028 static void
28029 prune_unused_types_update_strings (dw_die_ref die)
28031 dw_attr_node *a;
28032 unsigned ix;
28034 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28035 if (AT_class (a) == dw_val_class_str)
28037 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28038 s->refcount++;
28039 /* Avoid unnecessarily putting strings that are used less than
28040 twice in the hash table. */
28041 if (s->refcount
28042 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28044 indirect_string_node **slot
28045 = debug_str_hash->find_slot_with_hash (s->str,
28046 htab_hash_string (s->str),
28047 INSERT);
28048 gcc_assert (*slot == NULL);
28049 *slot = s;
28054 /* Mark DIE and its children as removed. */
28056 static void
28057 mark_removed (dw_die_ref die)
28059 dw_die_ref c;
28060 die->removed = true;
28061 FOR_EACH_CHILD (die, c, mark_removed (c));
28064 /* Remove from the tree DIE any dies that aren't marked. */
28066 static void
28067 prune_unused_types_prune (dw_die_ref die)
28069 dw_die_ref c;
28071 gcc_assert (die->die_mark);
28072 prune_unused_types_update_strings (die);
28074 if (! die->die_child)
28075 return;
28077 c = die->die_child;
28078 do {
28079 dw_die_ref prev = c, next;
28080 for (c = c->die_sib; ! c->die_mark; c = next)
28081 if (c == die->die_child)
28083 /* No marked children between 'prev' and the end of the list. */
28084 if (prev == c)
28085 /* No marked children at all. */
28086 die->die_child = NULL;
28087 else
28089 prev->die_sib = c->die_sib;
28090 die->die_child = prev;
28092 c->die_sib = NULL;
28093 mark_removed (c);
28094 return;
28096 else
28098 next = c->die_sib;
28099 c->die_sib = NULL;
28100 mark_removed (c);
28103 if (c != prev->die_sib)
28104 prev->die_sib = c;
28105 prune_unused_types_prune (c);
28106 } while (c != die->die_child);
28109 /* Remove dies representing declarations that we never use. */
28111 static void
28112 prune_unused_types (void)
28114 unsigned int i;
28115 limbo_die_node *node;
28116 comdat_type_node *ctnode;
28117 pubname_entry *pub;
28118 dw_die_ref base_type;
28120 #if ENABLE_ASSERT_CHECKING
28121 /* All the marks should already be clear. */
28122 verify_marks_clear (comp_unit_die ());
28123 for (node = limbo_die_list; node; node = node->next)
28124 verify_marks_clear (node->die);
28125 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28126 verify_marks_clear (ctnode->root_die);
28127 #endif /* ENABLE_ASSERT_CHECKING */
28129 /* Mark types that are used in global variables. */
28130 premark_types_used_by_global_vars ();
28132 /* Set the mark on nodes that are actually used. */
28133 prune_unused_types_walk (comp_unit_die ());
28134 for (node = limbo_die_list; node; node = node->next)
28135 prune_unused_types_walk (node->die);
28136 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28138 prune_unused_types_walk (ctnode->root_die);
28139 prune_unused_types_mark (ctnode->type_die, 1);
28142 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28143 are unusual in that they are pubnames that are the children of pubtypes.
28144 They should only be marked via their parent DW_TAG_enumeration_type die,
28145 not as roots in themselves. */
28146 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28147 if (pub->die->die_tag != DW_TAG_enumerator)
28148 prune_unused_types_mark (pub->die, 1);
28149 for (i = 0; base_types.iterate (i, &base_type); i++)
28150 prune_unused_types_mark (base_type, 1);
28152 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28153 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28154 callees). */
28155 cgraph_node *cnode;
28156 FOR_EACH_FUNCTION (cnode)
28157 if (cnode->referred_to_p (false))
28159 dw_die_ref die = lookup_decl_die (cnode->decl);
28160 if (die == NULL || die->die_mark)
28161 continue;
28162 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28163 if (e->caller != cnode
28164 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28166 prune_unused_types_mark (die, 1);
28167 break;
28171 if (debug_str_hash)
28172 debug_str_hash->empty ();
28173 if (skeleton_debug_str_hash)
28174 skeleton_debug_str_hash->empty ();
28175 prune_unused_types_prune (comp_unit_die ());
28176 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28178 node = *pnode;
28179 if (!node->die->die_mark)
28180 *pnode = node->next;
28181 else
28183 prune_unused_types_prune (node->die);
28184 pnode = &node->next;
28187 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28188 prune_unused_types_prune (ctnode->root_die);
28190 /* Leave the marks clear. */
28191 prune_unmark_dies (comp_unit_die ());
28192 for (node = limbo_die_list; node; node = node->next)
28193 prune_unmark_dies (node->die);
28194 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28195 prune_unmark_dies (ctnode->root_die);
28198 /* Helpers to manipulate hash table of comdat type units. */
28200 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28202 static inline hashval_t hash (const comdat_type_node *);
28203 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28206 inline hashval_t
28207 comdat_type_hasher::hash (const comdat_type_node *type_node)
28209 hashval_t h;
28210 memcpy (&h, type_node->signature, sizeof (h));
28211 return h;
28214 inline bool
28215 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28216 const comdat_type_node *type_node_2)
28218 return (! memcmp (type_node_1->signature, type_node_2->signature,
28219 DWARF_TYPE_SIGNATURE_SIZE));
28222 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28223 to the location it would have been added, should we know its
28224 DECL_ASSEMBLER_NAME when we added other attributes. This will
28225 probably improve compactness of debug info, removing equivalent
28226 abbrevs, and hide any differences caused by deferring the
28227 computation of the assembler name, triggered by e.g. PCH. */
28229 static inline void
28230 move_linkage_attr (dw_die_ref die)
28232 unsigned ix = vec_safe_length (die->die_attr);
28233 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28235 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28236 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28238 while (--ix > 0)
28240 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28242 if (prev->dw_attr == DW_AT_decl_line
28243 || prev->dw_attr == DW_AT_decl_column
28244 || prev->dw_attr == DW_AT_name)
28245 break;
28248 if (ix != vec_safe_length (die->die_attr) - 1)
28250 die->die_attr->pop ();
28251 die->die_attr->quick_insert (ix, linkage);
28255 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28256 referenced from typed stack ops and count how often they are used. */
28258 static void
28259 mark_base_types (dw_loc_descr_ref loc)
28261 dw_die_ref base_type = NULL;
28263 for (; loc; loc = loc->dw_loc_next)
28265 switch (loc->dw_loc_opc)
28267 case DW_OP_regval_type:
28268 case DW_OP_deref_type:
28269 case DW_OP_GNU_regval_type:
28270 case DW_OP_GNU_deref_type:
28271 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28272 break;
28273 case DW_OP_convert:
28274 case DW_OP_reinterpret:
28275 case DW_OP_GNU_convert:
28276 case DW_OP_GNU_reinterpret:
28277 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28278 continue;
28279 /* FALLTHRU */
28280 case DW_OP_const_type:
28281 case DW_OP_GNU_const_type:
28282 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28283 break;
28284 case DW_OP_entry_value:
28285 case DW_OP_GNU_entry_value:
28286 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28287 continue;
28288 default:
28289 continue;
28291 gcc_assert (base_type->die_parent == comp_unit_die ());
28292 if (base_type->die_mark)
28293 base_type->die_mark++;
28294 else
28296 base_types.safe_push (base_type);
28297 base_type->die_mark = 1;
28302 /* Comparison function for sorting marked base types. */
28304 static int
28305 base_type_cmp (const void *x, const void *y)
28307 dw_die_ref dx = *(const dw_die_ref *) x;
28308 dw_die_ref dy = *(const dw_die_ref *) y;
28309 unsigned int byte_size1, byte_size2;
28310 unsigned int encoding1, encoding2;
28311 unsigned int align1, align2;
28312 if (dx->die_mark > dy->die_mark)
28313 return -1;
28314 if (dx->die_mark < dy->die_mark)
28315 return 1;
28316 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28317 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28318 if (byte_size1 < byte_size2)
28319 return 1;
28320 if (byte_size1 > byte_size2)
28321 return -1;
28322 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28323 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28324 if (encoding1 < encoding2)
28325 return 1;
28326 if (encoding1 > encoding2)
28327 return -1;
28328 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28329 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28330 if (align1 < align2)
28331 return 1;
28332 if (align1 > align2)
28333 return -1;
28334 return 0;
28337 /* Move base types marked by mark_base_types as early as possible
28338 in the CU, sorted by decreasing usage count both to make the
28339 uleb128 references as small as possible and to make sure they
28340 will have die_offset already computed by calc_die_sizes when
28341 sizes of typed stack loc ops is computed. */
28343 static void
28344 move_marked_base_types (void)
28346 unsigned int i;
28347 dw_die_ref base_type, die, c;
28349 if (base_types.is_empty ())
28350 return;
28352 /* Sort by decreasing usage count, they will be added again in that
28353 order later on. */
28354 base_types.qsort (base_type_cmp);
28355 die = comp_unit_die ();
28356 c = die->die_child;
28359 dw_die_ref prev = c;
28360 c = c->die_sib;
28361 while (c->die_mark)
28363 remove_child_with_prev (c, prev);
28364 /* As base types got marked, there must be at least
28365 one node other than DW_TAG_base_type. */
28366 gcc_assert (die->die_child != NULL);
28367 c = prev->die_sib;
28370 while (c != die->die_child);
28371 gcc_assert (die->die_child);
28372 c = die->die_child;
28373 for (i = 0; base_types.iterate (i, &base_type); i++)
28375 base_type->die_mark = 0;
28376 base_type->die_sib = c->die_sib;
28377 c->die_sib = base_type;
28378 c = base_type;
28382 /* Helper function for resolve_addr, attempt to resolve
28383 one CONST_STRING, return true if successful. Similarly verify that
28384 SYMBOL_REFs refer to variables emitted in the current CU. */
28386 static bool
28387 resolve_one_addr (rtx *addr)
28389 rtx rtl = *addr;
28391 if (GET_CODE (rtl) == CONST_STRING)
28393 size_t len = strlen (XSTR (rtl, 0)) + 1;
28394 tree t = build_string (len, XSTR (rtl, 0));
28395 tree tlen = size_int (len - 1);
28396 TREE_TYPE (t)
28397 = build_array_type (char_type_node, build_index_type (tlen));
28398 rtl = lookup_constant_def (t);
28399 if (!rtl || !MEM_P (rtl))
28400 return false;
28401 rtl = XEXP (rtl, 0);
28402 if (GET_CODE (rtl) == SYMBOL_REF
28403 && SYMBOL_REF_DECL (rtl)
28404 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28405 return false;
28406 vec_safe_push (used_rtx_array, rtl);
28407 *addr = rtl;
28408 return true;
28411 if (GET_CODE (rtl) == SYMBOL_REF
28412 && SYMBOL_REF_DECL (rtl))
28414 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28416 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28417 return false;
28419 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28420 return false;
28423 if (GET_CODE (rtl) == CONST)
28425 subrtx_ptr_iterator::array_type array;
28426 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28427 if (!resolve_one_addr (*iter))
28428 return false;
28431 return true;
28434 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28435 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28436 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28438 static rtx
28439 string_cst_pool_decl (tree t)
28441 rtx rtl = output_constant_def (t, 1);
28442 unsigned char *array;
28443 dw_loc_descr_ref l;
28444 tree decl;
28445 size_t len;
28446 dw_die_ref ref;
28448 if (!rtl || !MEM_P (rtl))
28449 return NULL_RTX;
28450 rtl = XEXP (rtl, 0);
28451 if (GET_CODE (rtl) != SYMBOL_REF
28452 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28453 return NULL_RTX;
28455 decl = SYMBOL_REF_DECL (rtl);
28456 if (!lookup_decl_die (decl))
28458 len = TREE_STRING_LENGTH (t);
28459 vec_safe_push (used_rtx_array, rtl);
28460 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28461 array = ggc_vec_alloc<unsigned char> (len);
28462 memcpy (array, TREE_STRING_POINTER (t), len);
28463 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28464 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28465 l->dw_loc_oprnd2.v.val_vec.length = len;
28466 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28467 l->dw_loc_oprnd2.v.val_vec.array = array;
28468 add_AT_loc (ref, DW_AT_location, l);
28469 equate_decl_number_to_die (decl, ref);
28471 return rtl;
28474 /* Helper function of resolve_addr_in_expr. LOC is
28475 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28476 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28477 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28478 with DW_OP_implicit_pointer if possible
28479 and return true, if unsuccessful, return false. */
28481 static bool
28482 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28484 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28485 HOST_WIDE_INT offset = 0;
28486 dw_die_ref ref = NULL;
28487 tree decl;
28489 if (GET_CODE (rtl) == CONST
28490 && GET_CODE (XEXP (rtl, 0)) == PLUS
28491 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28493 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28494 rtl = XEXP (XEXP (rtl, 0), 0);
28496 if (GET_CODE (rtl) == CONST_STRING)
28498 size_t len = strlen (XSTR (rtl, 0)) + 1;
28499 tree t = build_string (len, XSTR (rtl, 0));
28500 tree tlen = size_int (len - 1);
28502 TREE_TYPE (t)
28503 = build_array_type (char_type_node, build_index_type (tlen));
28504 rtl = string_cst_pool_decl (t);
28505 if (!rtl)
28506 return false;
28508 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28510 decl = SYMBOL_REF_DECL (rtl);
28511 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28513 ref = lookup_decl_die (decl);
28514 if (ref && (get_AT (ref, DW_AT_location)
28515 || get_AT (ref, DW_AT_const_value)))
28517 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28518 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28519 loc->dw_loc_oprnd1.val_entry = NULL;
28520 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28521 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28522 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28523 loc->dw_loc_oprnd2.v.val_int = offset;
28524 return true;
28528 return false;
28531 /* Helper function for resolve_addr, handle one location
28532 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28533 the location list couldn't be resolved. */
28535 static bool
28536 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28538 dw_loc_descr_ref keep = NULL;
28539 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28540 switch (loc->dw_loc_opc)
28542 case DW_OP_addr:
28543 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28545 if ((prev == NULL
28546 || prev->dw_loc_opc == DW_OP_piece
28547 || prev->dw_loc_opc == DW_OP_bit_piece)
28548 && loc->dw_loc_next
28549 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28550 && (!dwarf_strict || dwarf_version >= 5)
28551 && optimize_one_addr_into_implicit_ptr (loc))
28552 break;
28553 return false;
28555 break;
28556 case DW_OP_GNU_addr_index:
28557 case DW_OP_GNU_const_index:
28558 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28559 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28561 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28562 if (!resolve_one_addr (&rtl))
28563 return false;
28564 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28565 loc->dw_loc_oprnd1.val_entry
28566 = add_addr_table_entry (rtl, ate_kind_rtx);
28568 break;
28569 case DW_OP_const4u:
28570 case DW_OP_const8u:
28571 if (loc->dtprel
28572 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28573 return false;
28574 break;
28575 case DW_OP_plus_uconst:
28576 if (size_of_loc_descr (loc)
28577 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28579 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28581 dw_loc_descr_ref repl
28582 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28583 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28584 add_loc_descr (&repl, loc->dw_loc_next);
28585 *loc = *repl;
28587 break;
28588 case DW_OP_implicit_value:
28589 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28590 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28591 return false;
28592 break;
28593 case DW_OP_implicit_pointer:
28594 case DW_OP_GNU_implicit_pointer:
28595 case DW_OP_GNU_parameter_ref:
28596 case DW_OP_GNU_variable_value:
28597 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28599 dw_die_ref ref
28600 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28601 if (ref == NULL)
28602 return false;
28603 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28604 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28605 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28607 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28609 if (prev == NULL
28610 && loc->dw_loc_next == NULL
28611 && AT_class (a) == dw_val_class_loc)
28612 switch (a->dw_attr)
28614 /* Following attributes allow both exprloc and reference,
28615 so if the whole expression is DW_OP_GNU_variable_value
28616 alone we could transform it into reference. */
28617 case DW_AT_byte_size:
28618 case DW_AT_bit_size:
28619 case DW_AT_lower_bound:
28620 case DW_AT_upper_bound:
28621 case DW_AT_bit_stride:
28622 case DW_AT_count:
28623 case DW_AT_allocated:
28624 case DW_AT_associated:
28625 case DW_AT_byte_stride:
28626 a->dw_attr_val.val_class = dw_val_class_die_ref;
28627 a->dw_attr_val.val_entry = NULL;
28628 a->dw_attr_val.v.val_die_ref.die
28629 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28630 a->dw_attr_val.v.val_die_ref.external = 0;
28631 return true;
28632 default:
28633 break;
28635 if (dwarf_strict)
28636 return false;
28638 break;
28639 case DW_OP_const_type:
28640 case DW_OP_regval_type:
28641 case DW_OP_deref_type:
28642 case DW_OP_convert:
28643 case DW_OP_reinterpret:
28644 case DW_OP_GNU_const_type:
28645 case DW_OP_GNU_regval_type:
28646 case DW_OP_GNU_deref_type:
28647 case DW_OP_GNU_convert:
28648 case DW_OP_GNU_reinterpret:
28649 while (loc->dw_loc_next
28650 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28651 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28653 dw_die_ref base1, base2;
28654 unsigned enc1, enc2, size1, size2;
28655 if (loc->dw_loc_opc == DW_OP_regval_type
28656 || loc->dw_loc_opc == DW_OP_deref_type
28657 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28658 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28659 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28660 else if (loc->dw_loc_oprnd1.val_class
28661 == dw_val_class_unsigned_const)
28662 break;
28663 else
28664 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28665 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28666 == dw_val_class_unsigned_const)
28667 break;
28668 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28669 gcc_assert (base1->die_tag == DW_TAG_base_type
28670 && base2->die_tag == DW_TAG_base_type);
28671 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28672 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28673 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28674 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28675 if (size1 == size2
28676 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28677 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28678 && loc != keep)
28679 || enc1 == enc2))
28681 /* Optimize away next DW_OP_convert after
28682 adjusting LOC's base type die reference. */
28683 if (loc->dw_loc_opc == DW_OP_regval_type
28684 || loc->dw_loc_opc == DW_OP_deref_type
28685 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28686 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28687 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28688 else
28689 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28690 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28691 continue;
28693 /* Don't change integer DW_OP_convert after e.g. floating
28694 point typed stack entry. */
28695 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28696 keep = loc->dw_loc_next;
28697 break;
28699 break;
28700 default:
28701 break;
28703 return true;
28706 /* Helper function of resolve_addr. DIE had DW_AT_location of
28707 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28708 and DW_OP_addr couldn't be resolved. resolve_addr has already
28709 removed the DW_AT_location attribute. This function attempts to
28710 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28711 to it or DW_AT_const_value attribute, if possible. */
28713 static void
28714 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28716 if (!VAR_P (decl)
28717 || lookup_decl_die (decl) != die
28718 || DECL_EXTERNAL (decl)
28719 || !TREE_STATIC (decl)
28720 || DECL_INITIAL (decl) == NULL_TREE
28721 || DECL_P (DECL_INITIAL (decl))
28722 || get_AT (die, DW_AT_const_value))
28723 return;
28725 tree init = DECL_INITIAL (decl);
28726 HOST_WIDE_INT offset = 0;
28727 /* For variables that have been optimized away and thus
28728 don't have a memory location, see if we can emit
28729 DW_AT_const_value instead. */
28730 if (tree_add_const_value_attribute (die, init))
28731 return;
28732 if (dwarf_strict && dwarf_version < 5)
28733 return;
28734 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28735 and ADDR_EXPR refers to a decl that has DW_AT_location or
28736 DW_AT_const_value (but isn't addressable, otherwise
28737 resolving the original DW_OP_addr wouldn't fail), see if
28738 we can add DW_OP_implicit_pointer. */
28739 STRIP_NOPS (init);
28740 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28741 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28743 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28744 init = TREE_OPERAND (init, 0);
28745 STRIP_NOPS (init);
28747 if (TREE_CODE (init) != ADDR_EXPR)
28748 return;
28749 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28750 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28751 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28752 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28753 && TREE_OPERAND (init, 0) != decl))
28755 dw_die_ref ref;
28756 dw_loc_descr_ref l;
28758 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28760 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28761 if (!rtl)
28762 return;
28763 decl = SYMBOL_REF_DECL (rtl);
28765 else
28766 decl = TREE_OPERAND (init, 0);
28767 ref = lookup_decl_die (decl);
28768 if (ref == NULL
28769 || (!get_AT (ref, DW_AT_location)
28770 && !get_AT (ref, DW_AT_const_value)))
28771 return;
28772 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28773 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28774 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28775 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28776 add_AT_loc (die, DW_AT_location, l);
28780 /* Return NULL if l is a DWARF expression, or first op that is not
28781 valid DWARF expression. */
28783 static dw_loc_descr_ref
28784 non_dwarf_expression (dw_loc_descr_ref l)
28786 while (l)
28788 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28789 return l;
28790 switch (l->dw_loc_opc)
28792 case DW_OP_regx:
28793 case DW_OP_implicit_value:
28794 case DW_OP_stack_value:
28795 case DW_OP_implicit_pointer:
28796 case DW_OP_GNU_implicit_pointer:
28797 case DW_OP_GNU_parameter_ref:
28798 case DW_OP_piece:
28799 case DW_OP_bit_piece:
28800 return l;
28801 default:
28802 break;
28804 l = l->dw_loc_next;
28806 return NULL;
28809 /* Return adjusted copy of EXPR:
28810 If it is empty DWARF expression, return it.
28811 If it is valid non-empty DWARF expression,
28812 return copy of EXPR with DW_OP_deref appended to it.
28813 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28814 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28815 If it is DWARF expression followed by DW_OP_stack_value, return
28816 copy of the DWARF expression without anything appended.
28817 Otherwise, return NULL. */
28819 static dw_loc_descr_ref
28820 copy_deref_exprloc (dw_loc_descr_ref expr)
28822 dw_loc_descr_ref tail = NULL;
28824 if (expr == NULL)
28825 return NULL;
28827 dw_loc_descr_ref l = non_dwarf_expression (expr);
28828 if (l && l->dw_loc_next)
28829 return NULL;
28831 if (l)
28833 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28834 tail = new_loc_descr ((enum dwarf_location_atom)
28835 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28836 0, 0);
28837 else
28838 switch (l->dw_loc_opc)
28840 case DW_OP_regx:
28841 tail = new_loc_descr (DW_OP_bregx,
28842 l->dw_loc_oprnd1.v.val_unsigned, 0);
28843 break;
28844 case DW_OP_stack_value:
28845 break;
28846 default:
28847 return NULL;
28850 else
28851 tail = new_loc_descr (DW_OP_deref, 0, 0);
28853 dw_loc_descr_ref ret = NULL, *p = &ret;
28854 while (expr != l)
28856 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28857 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28858 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28859 p = &(*p)->dw_loc_next;
28860 expr = expr->dw_loc_next;
28862 *p = tail;
28863 return ret;
28866 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28867 reference to a variable or argument, adjust it if needed and return:
28868 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28869 attribute if present should be removed
28870 0 keep the attribute perhaps with minor modifications, no need to rescan
28871 1 if the attribute has been successfully adjusted. */
28873 static int
28874 optimize_string_length (dw_attr_node *a)
28876 dw_loc_descr_ref l = AT_loc (a), lv;
28877 dw_die_ref die;
28878 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28880 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28881 die = lookup_decl_die (decl);
28882 if (die)
28884 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28885 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28886 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28888 else
28889 return -1;
28891 else
28892 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28894 /* DWARF5 allows reference class, so we can then reference the DIE.
28895 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28896 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28898 a->dw_attr_val.val_class = dw_val_class_die_ref;
28899 a->dw_attr_val.val_entry = NULL;
28900 a->dw_attr_val.v.val_die_ref.die = die;
28901 a->dw_attr_val.v.val_die_ref.external = 0;
28902 return 0;
28905 dw_attr_node *av = get_AT (die, DW_AT_location);
28906 dw_loc_list_ref d;
28907 bool non_dwarf_expr = false;
28909 if (av == NULL)
28910 return dwarf_strict ? -1 : 0;
28911 switch (AT_class (av))
28913 case dw_val_class_loc_list:
28914 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28915 if (d->expr && non_dwarf_expression (d->expr))
28916 non_dwarf_expr = true;
28917 break;
28918 case dw_val_class_loc:
28919 lv = AT_loc (av);
28920 if (lv == NULL)
28921 return dwarf_strict ? -1 : 0;
28922 if (non_dwarf_expression (lv))
28923 non_dwarf_expr = true;
28924 break;
28925 default:
28926 return dwarf_strict ? -1 : 0;
28929 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28930 into DW_OP_call4 or DW_OP_GNU_variable_value into
28931 DW_OP_call4 DW_OP_deref, do so. */
28932 if (!non_dwarf_expr
28933 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28935 l->dw_loc_opc = DW_OP_call4;
28936 if (l->dw_loc_next)
28937 l->dw_loc_next = NULL;
28938 else
28939 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28940 return 0;
28943 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28944 copy over the DW_AT_location attribute from die to a. */
28945 if (l->dw_loc_next != NULL)
28947 a->dw_attr_val = av->dw_attr_val;
28948 return 1;
28951 dw_loc_list_ref list, *p;
28952 switch (AT_class (av))
28954 case dw_val_class_loc_list:
28955 p = &list;
28956 list = NULL;
28957 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28959 lv = copy_deref_exprloc (d->expr);
28960 if (lv)
28962 *p = new_loc_list (lv, d->begin, d->end, d->section);
28963 p = &(*p)->dw_loc_next;
28965 else if (!dwarf_strict && d->expr)
28966 return 0;
28968 if (list == NULL)
28969 return dwarf_strict ? -1 : 0;
28970 a->dw_attr_val.val_class = dw_val_class_loc_list;
28971 gen_llsym (list);
28972 *AT_loc_list_ptr (a) = list;
28973 return 1;
28974 case dw_val_class_loc:
28975 lv = copy_deref_exprloc (AT_loc (av));
28976 if (lv == NULL)
28977 return dwarf_strict ? -1 : 0;
28978 a->dw_attr_val.v.val_loc = lv;
28979 return 1;
28980 default:
28981 gcc_unreachable ();
28985 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28986 an address in .rodata section if the string literal is emitted there,
28987 or remove the containing location list or replace DW_AT_const_value
28988 with DW_AT_location and empty location expression, if it isn't found
28989 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28990 to something that has been emitted in the current CU. */
28992 static void
28993 resolve_addr (dw_die_ref die)
28995 dw_die_ref c;
28996 dw_attr_node *a;
28997 dw_loc_list_ref *curr, *start, loc;
28998 unsigned ix;
28999 bool remove_AT_byte_size = false;
29001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29002 switch (AT_class (a))
29004 case dw_val_class_loc_list:
29005 start = curr = AT_loc_list_ptr (a);
29006 loc = *curr;
29007 gcc_assert (loc);
29008 /* The same list can be referenced more than once. See if we have
29009 already recorded the result from a previous pass. */
29010 if (loc->replaced)
29011 *curr = loc->dw_loc_next;
29012 else if (!loc->resolved_addr)
29014 /* As things stand, we do not expect or allow one die to
29015 reference a suffix of another die's location list chain.
29016 References must be identical or completely separate.
29017 There is therefore no need to cache the result of this
29018 pass on any list other than the first; doing so
29019 would lead to unnecessary writes. */
29020 while (*curr)
29022 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29023 if (!resolve_addr_in_expr (a, (*curr)->expr))
29025 dw_loc_list_ref next = (*curr)->dw_loc_next;
29026 dw_loc_descr_ref l = (*curr)->expr;
29028 if (next && (*curr)->ll_symbol)
29030 gcc_assert (!next->ll_symbol);
29031 next->ll_symbol = (*curr)->ll_symbol;
29033 if (dwarf_split_debug_info)
29034 remove_loc_list_addr_table_entries (l);
29035 *curr = next;
29037 else
29039 mark_base_types ((*curr)->expr);
29040 curr = &(*curr)->dw_loc_next;
29043 if (loc == *start)
29044 loc->resolved_addr = 1;
29045 else
29047 loc->replaced = 1;
29048 loc->dw_loc_next = *start;
29051 if (!*start)
29053 remove_AT (die, a->dw_attr);
29054 ix--;
29056 break;
29057 case dw_val_class_loc:
29059 dw_loc_descr_ref l = AT_loc (a);
29060 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29061 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29062 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29063 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29064 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29065 with DW_FORM_ref referencing the same DIE as
29066 DW_OP_GNU_variable_value used to reference. */
29067 if (a->dw_attr == DW_AT_string_length
29068 && l
29069 && l->dw_loc_opc == DW_OP_GNU_variable_value
29070 && (l->dw_loc_next == NULL
29071 || (l->dw_loc_next->dw_loc_next == NULL
29072 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29074 switch (optimize_string_length (a))
29076 case -1:
29077 remove_AT (die, a->dw_attr);
29078 ix--;
29079 /* If we drop DW_AT_string_length, we need to drop also
29080 DW_AT_{string_length_,}byte_size. */
29081 remove_AT_byte_size = true;
29082 continue;
29083 default:
29084 break;
29085 case 1:
29086 /* Even if we keep the optimized DW_AT_string_length,
29087 it might have changed AT_class, so process it again. */
29088 ix--;
29089 continue;
29092 /* For -gdwarf-2 don't attempt to optimize
29093 DW_AT_data_member_location containing
29094 DW_OP_plus_uconst - older consumers might
29095 rely on it being that op instead of a more complex,
29096 but shorter, location description. */
29097 if ((dwarf_version > 2
29098 || a->dw_attr != DW_AT_data_member_location
29099 || l == NULL
29100 || l->dw_loc_opc != DW_OP_plus_uconst
29101 || l->dw_loc_next != NULL)
29102 && !resolve_addr_in_expr (a, l))
29104 if (dwarf_split_debug_info)
29105 remove_loc_list_addr_table_entries (l);
29106 if (l != NULL
29107 && l->dw_loc_next == NULL
29108 && l->dw_loc_opc == DW_OP_addr
29109 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29110 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29111 && a->dw_attr == DW_AT_location)
29113 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29114 remove_AT (die, a->dw_attr);
29115 ix--;
29116 optimize_location_into_implicit_ptr (die, decl);
29117 break;
29119 if (a->dw_attr == DW_AT_string_length)
29120 /* If we drop DW_AT_string_length, we need to drop also
29121 DW_AT_{string_length_,}byte_size. */
29122 remove_AT_byte_size = true;
29123 remove_AT (die, a->dw_attr);
29124 ix--;
29126 else
29127 mark_base_types (l);
29129 break;
29130 case dw_val_class_addr:
29131 if (a->dw_attr == DW_AT_const_value
29132 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29134 if (AT_index (a) != NOT_INDEXED)
29135 remove_addr_table_entry (a->dw_attr_val.val_entry);
29136 remove_AT (die, a->dw_attr);
29137 ix--;
29139 if ((die->die_tag == DW_TAG_call_site
29140 && a->dw_attr == DW_AT_call_origin)
29141 || (die->die_tag == DW_TAG_GNU_call_site
29142 && a->dw_attr == DW_AT_abstract_origin))
29144 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29145 dw_die_ref tdie = lookup_decl_die (tdecl);
29146 dw_die_ref cdie;
29147 if (tdie == NULL
29148 && DECL_EXTERNAL (tdecl)
29149 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29150 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29152 dw_die_ref pdie = cdie;
29153 /* Make sure we don't add these DIEs into type units.
29154 We could emit skeleton DIEs for context (namespaces,
29155 outer structs/classes) and a skeleton DIE for the
29156 innermost context with DW_AT_signature pointing to the
29157 type unit. See PR78835. */
29158 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29159 pdie = pdie->die_parent;
29160 if (pdie == NULL)
29162 /* Creating a full DIE for tdecl is overly expensive and
29163 at this point even wrong when in the LTO phase
29164 as it can end up generating new type DIEs we didn't
29165 output and thus optimize_external_refs will crash. */
29166 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29167 add_AT_flag (tdie, DW_AT_external, 1);
29168 add_AT_flag (tdie, DW_AT_declaration, 1);
29169 add_linkage_attr (tdie, tdecl);
29170 add_name_and_src_coords_attributes (tdie, tdecl, true);
29171 equate_decl_number_to_die (tdecl, tdie);
29174 if (tdie)
29176 a->dw_attr_val.val_class = dw_val_class_die_ref;
29177 a->dw_attr_val.v.val_die_ref.die = tdie;
29178 a->dw_attr_val.v.val_die_ref.external = 0;
29180 else
29182 if (AT_index (a) != NOT_INDEXED)
29183 remove_addr_table_entry (a->dw_attr_val.val_entry);
29184 remove_AT (die, a->dw_attr);
29185 ix--;
29188 break;
29189 default:
29190 break;
29193 if (remove_AT_byte_size)
29194 remove_AT (die, dwarf_version >= 5
29195 ? DW_AT_string_length_byte_size
29196 : DW_AT_byte_size);
29198 FOR_EACH_CHILD (die, c, resolve_addr (c));
29201 /* Helper routines for optimize_location_lists.
29202 This pass tries to share identical local lists in .debug_loc
29203 section. */
29205 /* Iteratively hash operands of LOC opcode into HSTATE. */
29207 static void
29208 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29210 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29211 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29213 switch (loc->dw_loc_opc)
29215 case DW_OP_const4u:
29216 case DW_OP_const8u:
29217 if (loc->dtprel)
29218 goto hash_addr;
29219 /* FALLTHRU */
29220 case DW_OP_const1u:
29221 case DW_OP_const1s:
29222 case DW_OP_const2u:
29223 case DW_OP_const2s:
29224 case DW_OP_const4s:
29225 case DW_OP_const8s:
29226 case DW_OP_constu:
29227 case DW_OP_consts:
29228 case DW_OP_pick:
29229 case DW_OP_plus_uconst:
29230 case DW_OP_breg0:
29231 case DW_OP_breg1:
29232 case DW_OP_breg2:
29233 case DW_OP_breg3:
29234 case DW_OP_breg4:
29235 case DW_OP_breg5:
29236 case DW_OP_breg6:
29237 case DW_OP_breg7:
29238 case DW_OP_breg8:
29239 case DW_OP_breg9:
29240 case DW_OP_breg10:
29241 case DW_OP_breg11:
29242 case DW_OP_breg12:
29243 case DW_OP_breg13:
29244 case DW_OP_breg14:
29245 case DW_OP_breg15:
29246 case DW_OP_breg16:
29247 case DW_OP_breg17:
29248 case DW_OP_breg18:
29249 case DW_OP_breg19:
29250 case DW_OP_breg20:
29251 case DW_OP_breg21:
29252 case DW_OP_breg22:
29253 case DW_OP_breg23:
29254 case DW_OP_breg24:
29255 case DW_OP_breg25:
29256 case DW_OP_breg26:
29257 case DW_OP_breg27:
29258 case DW_OP_breg28:
29259 case DW_OP_breg29:
29260 case DW_OP_breg30:
29261 case DW_OP_breg31:
29262 case DW_OP_regx:
29263 case DW_OP_fbreg:
29264 case DW_OP_piece:
29265 case DW_OP_deref_size:
29266 case DW_OP_xderef_size:
29267 hstate.add_object (val1->v.val_int);
29268 break;
29269 case DW_OP_skip:
29270 case DW_OP_bra:
29272 int offset;
29274 gcc_assert (val1->val_class == dw_val_class_loc);
29275 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29276 hstate.add_object (offset);
29278 break;
29279 case DW_OP_implicit_value:
29280 hstate.add_object (val1->v.val_unsigned);
29281 switch (val2->val_class)
29283 case dw_val_class_const:
29284 hstate.add_object (val2->v.val_int);
29285 break;
29286 case dw_val_class_vec:
29288 unsigned int elt_size = val2->v.val_vec.elt_size;
29289 unsigned int len = val2->v.val_vec.length;
29291 hstate.add_int (elt_size);
29292 hstate.add_int (len);
29293 hstate.add (val2->v.val_vec.array, len * elt_size);
29295 break;
29296 case dw_val_class_const_double:
29297 hstate.add_object (val2->v.val_double.low);
29298 hstate.add_object (val2->v.val_double.high);
29299 break;
29300 case dw_val_class_wide_int:
29301 hstate.add (val2->v.val_wide->get_val (),
29302 get_full_len (*val2->v.val_wide)
29303 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29304 break;
29305 case dw_val_class_addr:
29306 inchash::add_rtx (val2->v.val_addr, hstate);
29307 break;
29308 default:
29309 gcc_unreachable ();
29311 break;
29312 case DW_OP_bregx:
29313 case DW_OP_bit_piece:
29314 hstate.add_object (val1->v.val_int);
29315 hstate.add_object (val2->v.val_int);
29316 break;
29317 case DW_OP_addr:
29318 hash_addr:
29319 if (loc->dtprel)
29321 unsigned char dtprel = 0xd1;
29322 hstate.add_object (dtprel);
29324 inchash::add_rtx (val1->v.val_addr, hstate);
29325 break;
29326 case DW_OP_GNU_addr_index:
29327 case DW_OP_GNU_const_index:
29329 if (loc->dtprel)
29331 unsigned char dtprel = 0xd1;
29332 hstate.add_object (dtprel);
29334 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29336 break;
29337 case DW_OP_implicit_pointer:
29338 case DW_OP_GNU_implicit_pointer:
29339 hstate.add_int (val2->v.val_int);
29340 break;
29341 case DW_OP_entry_value:
29342 case DW_OP_GNU_entry_value:
29343 hstate.add_object (val1->v.val_loc);
29344 break;
29345 case DW_OP_regval_type:
29346 case DW_OP_deref_type:
29347 case DW_OP_GNU_regval_type:
29348 case DW_OP_GNU_deref_type:
29350 unsigned int byte_size
29351 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29352 unsigned int encoding
29353 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29354 hstate.add_object (val1->v.val_int);
29355 hstate.add_object (byte_size);
29356 hstate.add_object (encoding);
29358 break;
29359 case DW_OP_convert:
29360 case DW_OP_reinterpret:
29361 case DW_OP_GNU_convert:
29362 case DW_OP_GNU_reinterpret:
29363 if (val1->val_class == dw_val_class_unsigned_const)
29365 hstate.add_object (val1->v.val_unsigned);
29366 break;
29368 /* FALLTHRU */
29369 case DW_OP_const_type:
29370 case DW_OP_GNU_const_type:
29372 unsigned int byte_size
29373 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29374 unsigned int encoding
29375 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29376 hstate.add_object (byte_size);
29377 hstate.add_object (encoding);
29378 if (loc->dw_loc_opc != DW_OP_const_type
29379 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29380 break;
29381 hstate.add_object (val2->val_class);
29382 switch (val2->val_class)
29384 case dw_val_class_const:
29385 hstate.add_object (val2->v.val_int);
29386 break;
29387 case dw_val_class_vec:
29389 unsigned int elt_size = val2->v.val_vec.elt_size;
29390 unsigned int len = val2->v.val_vec.length;
29392 hstate.add_object (elt_size);
29393 hstate.add_object (len);
29394 hstate.add (val2->v.val_vec.array, len * elt_size);
29396 break;
29397 case dw_val_class_const_double:
29398 hstate.add_object (val2->v.val_double.low);
29399 hstate.add_object (val2->v.val_double.high);
29400 break;
29401 case dw_val_class_wide_int:
29402 hstate.add (val2->v.val_wide->get_val (),
29403 get_full_len (*val2->v.val_wide)
29404 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29405 break;
29406 default:
29407 gcc_unreachable ();
29410 break;
29412 default:
29413 /* Other codes have no operands. */
29414 break;
29418 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29420 static inline void
29421 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29423 dw_loc_descr_ref l;
29424 bool sizes_computed = false;
29425 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29426 size_of_locs (loc);
29428 for (l = loc; l != NULL; l = l->dw_loc_next)
29430 enum dwarf_location_atom opc = l->dw_loc_opc;
29431 hstate.add_object (opc);
29432 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29434 size_of_locs (loc);
29435 sizes_computed = true;
29437 hash_loc_operands (l, hstate);
29441 /* Compute hash of the whole location list LIST_HEAD. */
29443 static inline void
29444 hash_loc_list (dw_loc_list_ref list_head)
29446 dw_loc_list_ref curr = list_head;
29447 inchash::hash hstate;
29449 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29451 hstate.add (curr->begin, strlen (curr->begin) + 1);
29452 hstate.add (curr->end, strlen (curr->end) + 1);
29453 if (curr->section)
29454 hstate.add (curr->section, strlen (curr->section) + 1);
29455 hash_locs (curr->expr, hstate);
29457 list_head->hash = hstate.end ();
29460 /* Return true if X and Y opcodes have the same operands. */
29462 static inline bool
29463 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29465 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29466 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29467 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29468 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29470 switch (x->dw_loc_opc)
29472 case DW_OP_const4u:
29473 case DW_OP_const8u:
29474 if (x->dtprel)
29475 goto hash_addr;
29476 /* FALLTHRU */
29477 case DW_OP_const1u:
29478 case DW_OP_const1s:
29479 case DW_OP_const2u:
29480 case DW_OP_const2s:
29481 case DW_OP_const4s:
29482 case DW_OP_const8s:
29483 case DW_OP_constu:
29484 case DW_OP_consts:
29485 case DW_OP_pick:
29486 case DW_OP_plus_uconst:
29487 case DW_OP_breg0:
29488 case DW_OP_breg1:
29489 case DW_OP_breg2:
29490 case DW_OP_breg3:
29491 case DW_OP_breg4:
29492 case DW_OP_breg5:
29493 case DW_OP_breg6:
29494 case DW_OP_breg7:
29495 case DW_OP_breg8:
29496 case DW_OP_breg9:
29497 case DW_OP_breg10:
29498 case DW_OP_breg11:
29499 case DW_OP_breg12:
29500 case DW_OP_breg13:
29501 case DW_OP_breg14:
29502 case DW_OP_breg15:
29503 case DW_OP_breg16:
29504 case DW_OP_breg17:
29505 case DW_OP_breg18:
29506 case DW_OP_breg19:
29507 case DW_OP_breg20:
29508 case DW_OP_breg21:
29509 case DW_OP_breg22:
29510 case DW_OP_breg23:
29511 case DW_OP_breg24:
29512 case DW_OP_breg25:
29513 case DW_OP_breg26:
29514 case DW_OP_breg27:
29515 case DW_OP_breg28:
29516 case DW_OP_breg29:
29517 case DW_OP_breg30:
29518 case DW_OP_breg31:
29519 case DW_OP_regx:
29520 case DW_OP_fbreg:
29521 case DW_OP_piece:
29522 case DW_OP_deref_size:
29523 case DW_OP_xderef_size:
29524 return valx1->v.val_int == valy1->v.val_int;
29525 case DW_OP_skip:
29526 case DW_OP_bra:
29527 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29528 can cause irrelevant differences in dw_loc_addr. */
29529 gcc_assert (valx1->val_class == dw_val_class_loc
29530 && valy1->val_class == dw_val_class_loc
29531 && (dwarf_split_debug_info
29532 || x->dw_loc_addr == y->dw_loc_addr));
29533 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29534 case DW_OP_implicit_value:
29535 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29536 || valx2->val_class != valy2->val_class)
29537 return false;
29538 switch (valx2->val_class)
29540 case dw_val_class_const:
29541 return valx2->v.val_int == valy2->v.val_int;
29542 case dw_val_class_vec:
29543 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29544 && valx2->v.val_vec.length == valy2->v.val_vec.length
29545 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29546 valx2->v.val_vec.elt_size
29547 * valx2->v.val_vec.length) == 0;
29548 case dw_val_class_const_double:
29549 return valx2->v.val_double.low == valy2->v.val_double.low
29550 && valx2->v.val_double.high == valy2->v.val_double.high;
29551 case dw_val_class_wide_int:
29552 return *valx2->v.val_wide == *valy2->v.val_wide;
29553 case dw_val_class_addr:
29554 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29555 default:
29556 gcc_unreachable ();
29558 case DW_OP_bregx:
29559 case DW_OP_bit_piece:
29560 return valx1->v.val_int == valy1->v.val_int
29561 && valx2->v.val_int == valy2->v.val_int;
29562 case DW_OP_addr:
29563 hash_addr:
29564 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29565 case DW_OP_GNU_addr_index:
29566 case DW_OP_GNU_const_index:
29568 rtx ax1 = valx1->val_entry->addr.rtl;
29569 rtx ay1 = valy1->val_entry->addr.rtl;
29570 return rtx_equal_p (ax1, ay1);
29572 case DW_OP_implicit_pointer:
29573 case DW_OP_GNU_implicit_pointer:
29574 return valx1->val_class == dw_val_class_die_ref
29575 && valx1->val_class == valy1->val_class
29576 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29577 && valx2->v.val_int == valy2->v.val_int;
29578 case DW_OP_entry_value:
29579 case DW_OP_GNU_entry_value:
29580 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29581 case DW_OP_const_type:
29582 case DW_OP_GNU_const_type:
29583 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29584 || valx2->val_class != valy2->val_class)
29585 return false;
29586 switch (valx2->val_class)
29588 case dw_val_class_const:
29589 return valx2->v.val_int == valy2->v.val_int;
29590 case dw_val_class_vec:
29591 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29592 && valx2->v.val_vec.length == valy2->v.val_vec.length
29593 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29594 valx2->v.val_vec.elt_size
29595 * valx2->v.val_vec.length) == 0;
29596 case dw_val_class_const_double:
29597 return valx2->v.val_double.low == valy2->v.val_double.low
29598 && valx2->v.val_double.high == valy2->v.val_double.high;
29599 case dw_val_class_wide_int:
29600 return *valx2->v.val_wide == *valy2->v.val_wide;
29601 default:
29602 gcc_unreachable ();
29604 case DW_OP_regval_type:
29605 case DW_OP_deref_type:
29606 case DW_OP_GNU_regval_type:
29607 case DW_OP_GNU_deref_type:
29608 return valx1->v.val_int == valy1->v.val_int
29609 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29610 case DW_OP_convert:
29611 case DW_OP_reinterpret:
29612 case DW_OP_GNU_convert:
29613 case DW_OP_GNU_reinterpret:
29614 if (valx1->val_class != valy1->val_class)
29615 return false;
29616 if (valx1->val_class == dw_val_class_unsigned_const)
29617 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29618 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29619 case DW_OP_GNU_parameter_ref:
29620 return valx1->val_class == dw_val_class_die_ref
29621 && valx1->val_class == valy1->val_class
29622 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29623 default:
29624 /* Other codes have no operands. */
29625 return true;
29629 /* Return true if DWARF location expressions X and Y are the same. */
29631 static inline bool
29632 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29634 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29635 if (x->dw_loc_opc != y->dw_loc_opc
29636 || x->dtprel != y->dtprel
29637 || !compare_loc_operands (x, y))
29638 break;
29639 return x == NULL && y == NULL;
29642 /* Hashtable helpers. */
29644 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29646 static inline hashval_t hash (const dw_loc_list_struct *);
29647 static inline bool equal (const dw_loc_list_struct *,
29648 const dw_loc_list_struct *);
29651 /* Return precomputed hash of location list X. */
29653 inline hashval_t
29654 loc_list_hasher::hash (const dw_loc_list_struct *x)
29656 return x->hash;
29659 /* Return true if location lists A and B are the same. */
29661 inline bool
29662 loc_list_hasher::equal (const dw_loc_list_struct *a,
29663 const dw_loc_list_struct *b)
29665 if (a == b)
29666 return 1;
29667 if (a->hash != b->hash)
29668 return 0;
29669 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29670 if (strcmp (a->begin, b->begin) != 0
29671 || strcmp (a->end, b->end) != 0
29672 || (a->section == NULL) != (b->section == NULL)
29673 || (a->section && strcmp (a->section, b->section) != 0)
29674 || !compare_locs (a->expr, b->expr))
29675 break;
29676 return a == NULL && b == NULL;
29679 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29682 /* Recursively optimize location lists referenced from DIE
29683 children and share them whenever possible. */
29685 static void
29686 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29688 dw_die_ref c;
29689 dw_attr_node *a;
29690 unsigned ix;
29691 dw_loc_list_struct **slot;
29693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29694 if (AT_class (a) == dw_val_class_loc_list)
29696 dw_loc_list_ref list = AT_loc_list (a);
29697 /* TODO: perform some optimizations here, before hashing
29698 it and storing into the hash table. */
29699 hash_loc_list (list);
29700 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29701 if (*slot == NULL)
29702 *slot = list;
29703 else
29704 a->dw_attr_val.v.val_loc_list = *slot;
29707 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29711 /* Recursively assign each location list a unique index into the debug_addr
29712 section. */
29714 static void
29715 index_location_lists (dw_die_ref die)
29717 dw_die_ref c;
29718 dw_attr_node *a;
29719 unsigned ix;
29721 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29722 if (AT_class (a) == dw_val_class_loc_list)
29724 dw_loc_list_ref list = AT_loc_list (a);
29725 dw_loc_list_ref curr;
29726 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29728 /* Don't index an entry that has already been indexed
29729 or won't be output. */
29730 if (curr->begin_entry != NULL
29731 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29732 continue;
29734 curr->begin_entry
29735 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29739 FOR_EACH_CHILD (die, c, index_location_lists (c));
29742 /* Optimize location lists referenced from DIE
29743 children and share them whenever possible. */
29745 static void
29746 optimize_location_lists (dw_die_ref die)
29748 loc_list_hash_type htab (500);
29749 optimize_location_lists_1 (die, &htab);
29752 /* Traverse the limbo die list, and add parent/child links. The only
29753 dies without parents that should be here are concrete instances of
29754 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29755 For concrete instances, we can get the parent die from the abstract
29756 instance. */
29758 static void
29759 flush_limbo_die_list (void)
29761 limbo_die_node *node;
29763 /* get_context_die calls force_decl_die, which can put new DIEs on the
29764 limbo list in LTO mode when nested functions are put in a different
29765 partition than that of their parent function. */
29766 while ((node = limbo_die_list))
29768 dw_die_ref die = node->die;
29769 limbo_die_list = node->next;
29771 if (die->die_parent == NULL)
29773 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29775 if (origin && origin->die_parent)
29776 add_child_die (origin->die_parent, die);
29777 else if (is_cu_die (die))
29779 else if (seen_error ())
29780 /* It's OK to be confused by errors in the input. */
29781 add_child_die (comp_unit_die (), die);
29782 else
29784 /* In certain situations, the lexical block containing a
29785 nested function can be optimized away, which results
29786 in the nested function die being orphaned. Likewise
29787 with the return type of that nested function. Force
29788 this to be a child of the containing function.
29790 It may happen that even the containing function got fully
29791 inlined and optimized out. In that case we are lost and
29792 assign the empty child. This should not be big issue as
29793 the function is likely unreachable too. */
29794 gcc_assert (node->created_for);
29796 if (DECL_P (node->created_for))
29797 origin = get_context_die (DECL_CONTEXT (node->created_for));
29798 else if (TYPE_P (node->created_for))
29799 origin = scope_die_for (node->created_for, comp_unit_die ());
29800 else
29801 origin = comp_unit_die ();
29803 add_child_die (origin, die);
29809 /* Reset DIEs so we can output them again. */
29811 static void
29812 reset_dies (dw_die_ref die)
29814 dw_die_ref c;
29816 /* Remove stuff we re-generate. */
29817 die->die_mark = 0;
29818 die->die_offset = 0;
29819 die->die_abbrev = 0;
29820 remove_AT (die, DW_AT_sibling);
29822 FOR_EACH_CHILD (die, c, reset_dies (c));
29825 /* Output stuff that dwarf requires at the end of every file,
29826 and generate the DWARF-2 debugging info. */
29828 static void
29829 dwarf2out_finish (const char *)
29831 comdat_type_node *ctnode;
29832 dw_die_ref main_comp_unit_die;
29833 unsigned char checksum[16];
29834 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29836 /* Flush out any latecomers to the limbo party. */
29837 flush_limbo_die_list ();
29839 if (flag_checking)
29841 verify_die (comp_unit_die ());
29842 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29843 verify_die (node->die);
29846 /* We shouldn't have any symbols with delayed asm names for
29847 DIEs generated after early finish. */
29848 gcc_assert (deferred_asm_name == NULL);
29850 gen_remaining_tmpl_value_param_die_attribute ();
29852 if (flag_generate_lto || flag_generate_offload)
29854 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29856 /* Prune stuff so that dwarf2out_finish runs successfully
29857 for the fat part of the object. */
29858 reset_dies (comp_unit_die ());
29859 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29860 reset_dies (node->die);
29862 hash_table<comdat_type_hasher> comdat_type_table (100);
29863 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29865 comdat_type_node **slot
29866 = comdat_type_table.find_slot (ctnode, INSERT);
29868 /* Don't reset types twice. */
29869 if (*slot != HTAB_EMPTY_ENTRY)
29870 continue;
29872 /* Add a pointer to the line table for the main compilation unit
29873 so that the debugger can make sense of DW_AT_decl_file
29874 attributes. */
29875 if (debug_info_level >= DINFO_LEVEL_TERSE)
29876 reset_dies (ctnode->root_die);
29878 *slot = ctnode;
29881 /* Reset die CU symbol so we don't output it twice. */
29882 comp_unit_die ()->die_id.die_symbol = NULL;
29884 /* Remove DW_AT_macro from the early output. */
29885 if (have_macinfo)
29886 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29888 /* Remove indirect string decisions. */
29889 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29892 #if ENABLE_ASSERT_CHECKING
29894 dw_die_ref die = comp_unit_die (), c;
29895 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29897 #endif
29898 resolve_addr (comp_unit_die ());
29899 move_marked_base_types ();
29901 /* Initialize sections and labels used for actual assembler output. */
29902 unsigned generation = init_sections_and_labels (false);
29904 /* Traverse the DIE's and add sibling attributes to those DIE's that
29905 have children. */
29906 add_sibling_attributes (comp_unit_die ());
29907 limbo_die_node *node;
29908 for (node = cu_die_list; node; node = node->next)
29909 add_sibling_attributes (node->die);
29910 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29911 add_sibling_attributes (ctnode->root_die);
29913 /* When splitting DWARF info, we put some attributes in the
29914 skeleton compile_unit DIE that remains in the .o, while
29915 most attributes go in the DWO compile_unit_die. */
29916 if (dwarf_split_debug_info)
29918 limbo_die_node *cu;
29919 main_comp_unit_die = gen_compile_unit_die (NULL);
29920 if (dwarf_version >= 5)
29921 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29922 cu = limbo_die_list;
29923 gcc_assert (cu->die == main_comp_unit_die);
29924 limbo_die_list = limbo_die_list->next;
29925 cu->next = cu_die_list;
29926 cu_die_list = cu;
29928 else
29929 main_comp_unit_die = comp_unit_die ();
29931 /* Output a terminator label for the .text section. */
29932 switch_to_section (text_section);
29933 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29934 if (cold_text_section)
29936 switch_to_section (cold_text_section);
29937 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29940 /* We can only use the low/high_pc attributes if all of the code was
29941 in .text. */
29942 if (!have_multiple_function_sections
29943 || (dwarf_version < 3 && dwarf_strict))
29945 /* Don't add if the CU has no associated code. */
29946 if (text_section_used)
29947 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29948 text_end_label, true);
29950 else
29952 unsigned fde_idx;
29953 dw_fde_ref fde;
29954 bool range_list_added = false;
29956 if (text_section_used)
29957 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29958 text_end_label, &range_list_added, true);
29959 if (cold_text_section_used)
29960 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29961 cold_end_label, &range_list_added, true);
29963 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29965 if (DECL_IGNORED_P (fde->decl))
29966 continue;
29967 if (!fde->in_std_section)
29968 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29969 fde->dw_fde_end, &range_list_added,
29970 true);
29971 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29972 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29973 fde->dw_fde_second_end, &range_list_added,
29974 true);
29977 if (range_list_added)
29979 /* We need to give .debug_loc and .debug_ranges an appropriate
29980 "base address". Use zero so that these addresses become
29981 absolute. Historically, we've emitted the unexpected
29982 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29983 Emit both to give time for other tools to adapt. */
29984 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29985 if (! dwarf_strict && dwarf_version < 4)
29986 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29988 add_ranges (NULL);
29992 /* AIX Assembler inserts the length, so adjust the reference to match the
29993 offset expected by debuggers. */
29994 strcpy (dl_section_ref, debug_line_section_label);
29995 if (XCOFF_DEBUGGING_INFO)
29996 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29998 if (debug_info_level >= DINFO_LEVEL_TERSE)
29999 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30000 dl_section_ref);
30002 if (have_macinfo)
30003 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30004 macinfo_section_label);
30006 if (dwarf_split_debug_info)
30008 if (have_location_lists)
30010 if (dwarf_version >= 5)
30011 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30012 loc_section_label);
30013 /* optimize_location_lists calculates the size of the lists,
30014 so index them first, and assign indices to the entries.
30015 Although optimize_location_lists will remove entries from
30016 the table, it only does so for duplicates, and therefore
30017 only reduces ref_counts to 1. */
30018 index_location_lists (comp_unit_die ());
30021 if (addr_index_table != NULL)
30023 unsigned int index = 0;
30024 addr_index_table
30025 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30026 (&index);
30030 loc_list_idx = 0;
30031 if (have_location_lists)
30033 optimize_location_lists (comp_unit_die ());
30034 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30035 if (dwarf_version >= 5 && dwarf_split_debug_info)
30036 assign_location_list_indexes (comp_unit_die ());
30039 save_macinfo_strings ();
30041 if (dwarf_split_debug_info)
30043 unsigned int index = 0;
30045 /* Add attributes common to skeleton compile_units and
30046 type_units. Because these attributes include strings, it
30047 must be done before freezing the string table. Top-level
30048 skeleton die attrs are added when the skeleton type unit is
30049 created, so ensure it is created by this point. */
30050 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30051 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30054 /* Output all of the compilation units. We put the main one last so that
30055 the offsets are available to output_pubnames. */
30056 for (node = cu_die_list; node; node = node->next)
30057 output_comp_unit (node->die, 0, NULL);
30059 hash_table<comdat_type_hasher> comdat_type_table (100);
30060 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30062 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30064 /* Don't output duplicate types. */
30065 if (*slot != HTAB_EMPTY_ENTRY)
30066 continue;
30068 /* Add a pointer to the line table for the main compilation unit
30069 so that the debugger can make sense of DW_AT_decl_file
30070 attributes. */
30071 if (debug_info_level >= DINFO_LEVEL_TERSE)
30072 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30073 (!dwarf_split_debug_info
30074 ? dl_section_ref
30075 : debug_skeleton_line_section_label));
30077 output_comdat_type_unit (ctnode);
30078 *slot = ctnode;
30081 if (dwarf_split_debug_info)
30083 int mark;
30084 struct md5_ctx ctx;
30086 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30087 index_rnglists ();
30089 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30090 md5_init_ctx (&ctx);
30091 mark = 0;
30092 die_checksum (comp_unit_die (), &ctx, &mark);
30093 unmark_all_dies (comp_unit_die ());
30094 md5_finish_ctx (&ctx, checksum);
30096 if (dwarf_version < 5)
30098 /* Use the first 8 bytes of the checksum as the dwo_id,
30099 and add it to both comp-unit DIEs. */
30100 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30101 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30104 /* Add the base offset of the ranges table to the skeleton
30105 comp-unit DIE. */
30106 if (!vec_safe_is_empty (ranges_table))
30108 if (dwarf_version >= 5)
30109 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30110 ranges_base_label);
30111 else
30112 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30113 ranges_section_label);
30116 switch_to_section (debug_addr_section);
30117 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30118 output_addr_table ();
30121 /* Output the main compilation unit if non-empty or if .debug_macinfo
30122 or .debug_macro will be emitted. */
30123 output_comp_unit (comp_unit_die (), have_macinfo,
30124 dwarf_split_debug_info ? checksum : NULL);
30126 if (dwarf_split_debug_info && info_section_emitted)
30127 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30129 /* Output the abbreviation table. */
30130 if (vec_safe_length (abbrev_die_table) != 1)
30132 switch_to_section (debug_abbrev_section);
30133 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30134 output_abbrev_section ();
30137 /* Output location list section if necessary. */
30138 if (have_location_lists)
30140 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30141 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30142 /* Output the location lists info. */
30143 switch_to_section (debug_loc_section);
30144 if (dwarf_version >= 5)
30146 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30147 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30148 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30149 dw2_asm_output_data (4, 0xffffffff,
30150 "Initial length escape value indicating "
30151 "64-bit DWARF extension");
30152 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30153 "Length of Location Lists");
30154 ASM_OUTPUT_LABEL (asm_out_file, l1);
30155 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30156 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30157 dw2_asm_output_data (1, 0, "Segment Size");
30158 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30159 "Offset Entry Count");
30161 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30162 if (dwarf_version >= 5 && dwarf_split_debug_info)
30164 unsigned int save_loc_list_idx = loc_list_idx;
30165 loc_list_idx = 0;
30166 output_loclists_offsets (comp_unit_die ());
30167 gcc_assert (save_loc_list_idx == loc_list_idx);
30169 output_location_lists (comp_unit_die ());
30170 if (dwarf_version >= 5)
30171 ASM_OUTPUT_LABEL (asm_out_file, l2);
30174 output_pubtables ();
30176 /* Output the address range information if a CU (.debug_info section)
30177 was emitted. We output an empty table even if we had no functions
30178 to put in it. This because the consumer has no way to tell the
30179 difference between an empty table that we omitted and failure to
30180 generate a table that would have contained data. */
30181 if (info_section_emitted)
30183 switch_to_section (debug_aranges_section);
30184 output_aranges ();
30187 /* Output ranges section if necessary. */
30188 if (!vec_safe_is_empty (ranges_table))
30190 if (dwarf_version >= 5)
30191 output_rnglists (generation);
30192 else
30193 output_ranges ();
30196 /* Have to end the macro section. */
30197 if (have_macinfo)
30199 switch_to_section (debug_macinfo_section);
30200 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30201 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30202 : debug_skeleton_line_section_label, false);
30203 dw2_asm_output_data (1, 0, "End compilation unit");
30206 /* Output the source line correspondence table. We must do this
30207 even if there is no line information. Otherwise, on an empty
30208 translation unit, we will generate a present, but empty,
30209 .debug_info section. IRIX 6.5 `nm' will then complain when
30210 examining the file. This is done late so that any filenames
30211 used by the debug_info section are marked as 'used'. */
30212 switch_to_section (debug_line_section);
30213 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30214 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30215 output_line_info (false);
30217 if (dwarf_split_debug_info && info_section_emitted)
30219 switch_to_section (debug_skeleton_line_section);
30220 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30221 output_line_info (true);
30224 /* If we emitted any indirect strings, output the string table too. */
30225 if (debug_str_hash || skeleton_debug_str_hash)
30226 output_indirect_strings ();
30227 if (debug_line_str_hash)
30229 switch_to_section (debug_line_str_section);
30230 const enum dwarf_form form = DW_FORM_line_strp;
30231 debug_line_str_hash->traverse<enum dwarf_form,
30232 output_indirect_string> (form);
30236 /* Returns a hash value for X (which really is a variable_value_struct). */
30238 inline hashval_t
30239 variable_value_hasher::hash (variable_value_struct *x)
30241 return (hashval_t) x->decl_id;
30244 /* Return nonzero if decl_id of variable_value_struct X is the same as
30245 UID of decl Y. */
30247 inline bool
30248 variable_value_hasher::equal (variable_value_struct *x, tree y)
30250 return x->decl_id == DECL_UID (y);
30253 /* Helper function for resolve_variable_value, handle
30254 DW_OP_GNU_variable_value in one location expression.
30255 Return true if exprloc has been changed into loclist. */
30257 static bool
30258 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30260 dw_loc_descr_ref next;
30261 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30263 next = loc->dw_loc_next;
30264 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30265 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30266 continue;
30268 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30269 if (DECL_CONTEXT (decl) != current_function_decl)
30270 continue;
30272 dw_die_ref ref = lookup_decl_die (decl);
30273 if (ref)
30275 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30276 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30277 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30278 continue;
30280 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30281 if (l == NULL)
30282 continue;
30283 if (l->dw_loc_next)
30285 if (AT_class (a) != dw_val_class_loc)
30286 continue;
30287 switch (a->dw_attr)
30289 /* Following attributes allow both exprloc and loclist
30290 classes, so we can change them into a loclist. */
30291 case DW_AT_location:
30292 case DW_AT_string_length:
30293 case DW_AT_return_addr:
30294 case DW_AT_data_member_location:
30295 case DW_AT_frame_base:
30296 case DW_AT_segment:
30297 case DW_AT_static_link:
30298 case DW_AT_use_location:
30299 case DW_AT_vtable_elem_location:
30300 if (prev)
30302 prev->dw_loc_next = NULL;
30303 prepend_loc_descr_to_each (l, AT_loc (a));
30305 if (next)
30306 add_loc_descr_to_each (l, next);
30307 a->dw_attr_val.val_class = dw_val_class_loc_list;
30308 a->dw_attr_val.val_entry = NULL;
30309 a->dw_attr_val.v.val_loc_list = l;
30310 have_location_lists = true;
30311 return true;
30312 /* Following attributes allow both exprloc and reference,
30313 so if the whole expression is DW_OP_GNU_variable_value alone
30314 we could transform it into reference. */
30315 case DW_AT_byte_size:
30316 case DW_AT_bit_size:
30317 case DW_AT_lower_bound:
30318 case DW_AT_upper_bound:
30319 case DW_AT_bit_stride:
30320 case DW_AT_count:
30321 case DW_AT_allocated:
30322 case DW_AT_associated:
30323 case DW_AT_byte_stride:
30324 if (prev == NULL && next == NULL)
30325 break;
30326 /* FALLTHRU */
30327 default:
30328 if (dwarf_strict)
30329 continue;
30330 break;
30332 /* Create DW_TAG_variable that we can refer to. */
30333 gen_decl_die (decl, NULL_TREE, NULL,
30334 lookup_decl_die (current_function_decl));
30335 ref = lookup_decl_die (decl);
30336 if (ref)
30338 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30339 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30340 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30342 continue;
30344 if (prev)
30346 prev->dw_loc_next = l->expr;
30347 add_loc_descr (&prev->dw_loc_next, next);
30348 free_loc_descr (loc, NULL);
30349 next = prev->dw_loc_next;
30351 else
30353 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30354 add_loc_descr (&loc, next);
30355 next = loc;
30357 loc = prev;
30359 return false;
30362 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30364 static void
30365 resolve_variable_value (dw_die_ref die)
30367 dw_attr_node *a;
30368 dw_loc_list_ref loc;
30369 unsigned ix;
30371 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30372 switch (AT_class (a))
30374 case dw_val_class_loc:
30375 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30376 break;
30377 /* FALLTHRU */
30378 case dw_val_class_loc_list:
30379 loc = AT_loc_list (a);
30380 gcc_assert (loc);
30381 for (; loc; loc = loc->dw_loc_next)
30382 resolve_variable_value_in_expr (a, loc->expr);
30383 break;
30384 default:
30385 break;
30389 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30390 temporaries in the current function. */
30392 static void
30393 resolve_variable_values (void)
30395 if (!variable_value_hash || !current_function_decl)
30396 return;
30398 struct variable_value_struct *node
30399 = variable_value_hash->find_with_hash (current_function_decl,
30400 DECL_UID (current_function_decl));
30402 if (node == NULL)
30403 return;
30405 unsigned int i;
30406 dw_die_ref die;
30407 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30408 resolve_variable_value (die);
30411 /* Helper function for note_variable_value, handle one location
30412 expression. */
30414 static void
30415 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30417 for (; loc; loc = loc->dw_loc_next)
30418 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30419 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30421 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30422 dw_die_ref ref = lookup_decl_die (decl);
30423 if (! ref && (flag_generate_lto || flag_generate_offload))
30425 /* ??? This is somewhat a hack because we do not create DIEs
30426 for variables not in BLOCK trees early but when generating
30427 early LTO output we need the dw_val_class_decl_ref to be
30428 fully resolved. For fat LTO objects we'd also like to
30429 undo this after LTO dwarf output. */
30430 gcc_assert (DECL_CONTEXT (decl));
30431 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30432 gcc_assert (ctx != NULL);
30433 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30434 ref = lookup_decl_die (decl);
30435 gcc_assert (ref != NULL);
30437 if (ref)
30439 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30440 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30441 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30442 continue;
30444 if (VAR_P (decl)
30445 && DECL_CONTEXT (decl)
30446 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30447 && lookup_decl_die (DECL_CONTEXT (decl)))
30449 if (!variable_value_hash)
30450 variable_value_hash
30451 = hash_table<variable_value_hasher>::create_ggc (10);
30453 tree fndecl = DECL_CONTEXT (decl);
30454 struct variable_value_struct *node;
30455 struct variable_value_struct **slot
30456 = variable_value_hash->find_slot_with_hash (fndecl,
30457 DECL_UID (fndecl),
30458 INSERT);
30459 if (*slot == NULL)
30461 node = ggc_cleared_alloc<variable_value_struct> ();
30462 node->decl_id = DECL_UID (fndecl);
30463 *slot = node;
30465 else
30466 node = *slot;
30468 vec_safe_push (node->dies, die);
30473 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30474 with dw_val_class_decl_ref operand. */
30476 static void
30477 note_variable_value (dw_die_ref die)
30479 dw_die_ref c;
30480 dw_attr_node *a;
30481 dw_loc_list_ref loc;
30482 unsigned ix;
30484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30485 switch (AT_class (a))
30487 case dw_val_class_loc_list:
30488 loc = AT_loc_list (a);
30489 gcc_assert (loc);
30490 if (!loc->noted_variable_value)
30492 loc->noted_variable_value = 1;
30493 for (; loc; loc = loc->dw_loc_next)
30494 note_variable_value_in_expr (die, loc->expr);
30496 break;
30497 case dw_val_class_loc:
30498 note_variable_value_in_expr (die, AT_loc (a));
30499 break;
30500 default:
30501 break;
30504 /* Mark children. */
30505 FOR_EACH_CHILD (die, c, note_variable_value (c));
30508 /* Perform any cleanups needed after the early debug generation pass
30509 has run. */
30511 static void
30512 dwarf2out_early_finish (const char *filename)
30514 set_early_dwarf s;
30516 /* PCH might result in DW_AT_producer string being restored from the
30517 header compilation, so always fill it with empty string initially
30518 and overwrite only here. */
30519 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30520 producer_string = gen_producer_string ();
30521 producer->dw_attr_val.v.val_str->refcount--;
30522 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30524 /* Add the name for the main input file now. We delayed this from
30525 dwarf2out_init to avoid complications with PCH. */
30526 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30527 add_comp_dir_attribute (comp_unit_die ());
30529 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30530 DW_AT_comp_dir into .debug_line_str section. */
30531 if (!DWARF2_ASM_LINE_DEBUG_INFO
30532 && dwarf_version >= 5
30533 && DWARF5_USE_DEBUG_LINE_STR)
30535 for (int i = 0; i < 2; i++)
30537 dw_attr_node *a = get_AT (comp_unit_die (),
30538 i ? DW_AT_comp_dir : DW_AT_name);
30539 if (a == NULL
30540 || AT_class (a) != dw_val_class_str
30541 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30542 continue;
30544 if (! debug_line_str_hash)
30545 debug_line_str_hash
30546 = hash_table<indirect_string_hasher>::create_ggc (10);
30548 struct indirect_string_node *node
30549 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30550 set_indirect_string (node);
30551 node->form = DW_FORM_line_strp;
30552 a->dw_attr_val.v.val_str->refcount--;
30553 a->dw_attr_val.v.val_str = node;
30557 /* With LTO early dwarf was really finished at compile-time, so make
30558 sure to adjust the phase after annotating the LTRANS CU DIE. */
30559 if (in_lto_p)
30561 early_dwarf_finished = true;
30562 return;
30565 /* Walk through the list of incomplete types again, trying once more to
30566 emit full debugging info for them. */
30567 retry_incomplete_types ();
30569 /* The point here is to flush out the limbo list so that it is empty
30570 and we don't need to stream it for LTO. */
30571 flush_limbo_die_list ();
30573 gen_scheduled_generic_parms_dies ();
30574 gen_remaining_tmpl_value_param_die_attribute ();
30576 /* Add DW_AT_linkage_name for all deferred DIEs. */
30577 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30579 tree decl = node->created_for;
30580 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30581 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30582 ended up in deferred_asm_name before we knew it was
30583 constant and never written to disk. */
30584 && DECL_ASSEMBLER_NAME (decl))
30586 add_linkage_attr (node->die, decl);
30587 move_linkage_attr (node->die);
30590 deferred_asm_name = NULL;
30592 if (flag_eliminate_unused_debug_types)
30593 prune_unused_types ();
30595 /* Generate separate COMDAT sections for type DIEs. */
30596 if (use_debug_types)
30598 break_out_comdat_types (comp_unit_die ());
30600 /* Each new type_unit DIE was added to the limbo die list when created.
30601 Since these have all been added to comdat_type_list, clear the
30602 limbo die list. */
30603 limbo_die_list = NULL;
30605 /* For each new comdat type unit, copy declarations for incomplete
30606 types to make the new unit self-contained (i.e., no direct
30607 references to the main compile unit). */
30608 for (comdat_type_node *ctnode = comdat_type_list;
30609 ctnode != NULL; ctnode = ctnode->next)
30610 copy_decls_for_unworthy_types (ctnode->root_die);
30611 copy_decls_for_unworthy_types (comp_unit_die ());
30613 /* In the process of copying declarations from one unit to another,
30614 we may have left some declarations behind that are no longer
30615 referenced. Prune them. */
30616 prune_unused_types ();
30619 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30620 with dw_val_class_decl_ref operand. */
30621 note_variable_value (comp_unit_die ());
30622 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30623 note_variable_value (node->die);
30624 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30625 ctnode = ctnode->next)
30626 note_variable_value (ctnode->root_die);
30627 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30628 note_variable_value (node->die);
30630 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30631 both the main_cu and all skeleton TUs. Making this call unconditional
30632 would end up either adding a second copy of the AT_pubnames attribute, or
30633 requiring a special case in add_top_level_skeleton_die_attrs. */
30634 if (!dwarf_split_debug_info)
30635 add_AT_pubnames (comp_unit_die ());
30637 /* The early debug phase is now finished. */
30638 early_dwarf_finished = true;
30640 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30641 if (!flag_generate_lto && !flag_generate_offload)
30642 return;
30644 /* Now as we are going to output for LTO initialize sections and labels
30645 to the LTO variants. We don't need a random-seed postfix as other
30646 LTO sections as linking the LTO debug sections into one in a partial
30647 link is fine. */
30648 init_sections_and_labels (true);
30650 /* The output below is modeled after dwarf2out_finish with all
30651 location related output removed and some LTO specific changes.
30652 Some refactoring might make both smaller and easier to match up. */
30654 /* Traverse the DIE's and add add sibling attributes to those DIE's
30655 that have children. */
30656 add_sibling_attributes (comp_unit_die ());
30657 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30658 add_sibling_attributes (node->die);
30659 for (comdat_type_node *ctnode = comdat_type_list;
30660 ctnode != NULL; ctnode = ctnode->next)
30661 add_sibling_attributes (ctnode->root_die);
30663 if (have_macinfo)
30664 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30665 macinfo_section_label);
30667 save_macinfo_strings ();
30669 /* Output all of the compilation units. We put the main one last so that
30670 the offsets are available to output_pubnames. */
30671 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30672 output_comp_unit (node->die, 0, NULL);
30674 hash_table<comdat_type_hasher> comdat_type_table (100);
30675 for (comdat_type_node *ctnode = comdat_type_list;
30676 ctnode != NULL; ctnode = ctnode->next)
30678 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30680 /* Don't output duplicate types. */
30681 if (*slot != HTAB_EMPTY_ENTRY)
30682 continue;
30684 /* Add a pointer to the line table for the main compilation unit
30685 so that the debugger can make sense of DW_AT_decl_file
30686 attributes. */
30687 if (debug_info_level >= DINFO_LEVEL_TERSE)
30688 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30689 (!dwarf_split_debug_info
30690 ? debug_line_section_label
30691 : debug_skeleton_line_section_label));
30693 output_comdat_type_unit (ctnode);
30694 *slot = ctnode;
30697 /* Stick a unique symbol to the main debuginfo section. */
30698 compute_comp_unit_symbol (comp_unit_die ());
30700 /* Output the main compilation unit. We always need it if only for
30701 the CU symbol. */
30702 output_comp_unit (comp_unit_die (), true, NULL);
30704 /* Output the abbreviation table. */
30705 if (vec_safe_length (abbrev_die_table) != 1)
30707 switch_to_section (debug_abbrev_section);
30708 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30709 output_abbrev_section ();
30712 /* Have to end the macro section. */
30713 if (have_macinfo)
30715 /* We have to save macinfo state if we need to output it again
30716 for the FAT part of the object. */
30717 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30718 if (flag_fat_lto_objects)
30719 macinfo_table = macinfo_table->copy ();
30721 switch_to_section (debug_macinfo_section);
30722 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30723 output_macinfo (debug_skeleton_line_section_label, true);
30724 dw2_asm_output_data (1, 0, "End compilation unit");
30726 /* Emit a skeleton debug_line section. */
30727 switch_to_section (debug_skeleton_line_section);
30728 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30729 output_line_info (true);
30731 if (flag_fat_lto_objects)
30733 vec_free (macinfo_table);
30734 macinfo_table = saved_macinfo_table;
30739 /* If we emitted any indirect strings, output the string table too. */
30740 if (debug_str_hash || skeleton_debug_str_hash)
30741 output_indirect_strings ();
30743 /* Switch back to the text section. */
30744 switch_to_section (text_section);
30747 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30748 within the same process. For use by toplev::finalize. */
30750 void
30751 dwarf2out_c_finalize (void)
30753 last_var_location_insn = NULL;
30754 cached_next_real_insn = NULL;
30755 used_rtx_array = NULL;
30756 incomplete_types = NULL;
30757 decl_scope_table = NULL;
30758 debug_info_section = NULL;
30759 debug_skeleton_info_section = NULL;
30760 debug_abbrev_section = NULL;
30761 debug_skeleton_abbrev_section = NULL;
30762 debug_aranges_section = NULL;
30763 debug_addr_section = NULL;
30764 debug_macinfo_section = NULL;
30765 debug_line_section = NULL;
30766 debug_skeleton_line_section = NULL;
30767 debug_loc_section = NULL;
30768 debug_pubnames_section = NULL;
30769 debug_pubtypes_section = NULL;
30770 debug_str_section = NULL;
30771 debug_line_str_section = NULL;
30772 debug_str_dwo_section = NULL;
30773 debug_str_offsets_section = NULL;
30774 debug_ranges_section = NULL;
30775 debug_frame_section = NULL;
30776 fde_vec = NULL;
30777 debug_str_hash = NULL;
30778 debug_line_str_hash = NULL;
30779 skeleton_debug_str_hash = NULL;
30780 dw2_string_counter = 0;
30781 have_multiple_function_sections = false;
30782 text_section_used = false;
30783 cold_text_section_used = false;
30784 cold_text_section = NULL;
30785 current_unit_personality = NULL;
30787 early_dwarf = false;
30788 early_dwarf_finished = false;
30790 next_die_offset = 0;
30791 single_comp_unit_die = NULL;
30792 comdat_type_list = NULL;
30793 limbo_die_list = NULL;
30794 file_table = NULL;
30795 decl_die_table = NULL;
30796 common_block_die_table = NULL;
30797 decl_loc_table = NULL;
30798 call_arg_locations = NULL;
30799 call_arg_loc_last = NULL;
30800 call_site_count = -1;
30801 tail_call_site_count = -1;
30802 cached_dw_loc_list_table = NULL;
30803 abbrev_die_table = NULL;
30804 delete dwarf_proc_stack_usage_map;
30805 dwarf_proc_stack_usage_map = NULL;
30806 line_info_label_num = 0;
30807 cur_line_info_table = NULL;
30808 text_section_line_info = NULL;
30809 cold_text_section_line_info = NULL;
30810 separate_line_info = NULL;
30811 info_section_emitted = false;
30812 pubname_table = NULL;
30813 pubtype_table = NULL;
30814 macinfo_table = NULL;
30815 ranges_table = NULL;
30816 ranges_by_label = NULL;
30817 rnglist_idx = 0;
30818 have_location_lists = false;
30819 loclabel_num = 0;
30820 poc_label_num = 0;
30821 last_emitted_file = NULL;
30822 label_num = 0;
30823 tmpl_value_parm_die_table = NULL;
30824 generic_type_instances = NULL;
30825 frame_pointer_fb_offset = 0;
30826 frame_pointer_fb_offset_valid = false;
30827 base_types.release ();
30828 XDELETEVEC (producer_string);
30829 producer_string = NULL;
30832 #include "gt-dwarf2out.h"