PR middle-end/84095 - false-positive -Wrestrict warnings for memcpy within array
[official-gcc.git] / gcc / dwarf2out.c
blob5e88c7bacf063b896479649917f09ec45fb032f3
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
106 #ifndef XCOFF_DEBUGGING_INFO
107 #define XCOFF_DEBUGGING_INFO 0
108 #endif
110 #ifndef HAVE_XCOFF_DWARF_EXTRAS
111 #define HAVE_XCOFF_DWARF_EXTRAS 0
112 #endif
114 #ifdef VMS_DEBUGGING_INFO
115 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117 /* Define this macro to be a nonzero value if the directory specifications
118 which are output in the debug info should end with a separator. */
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
120 /* Define this macro to evaluate to a nonzero value if GCC should refrain
121 from generating indirect strings in DWARF2 debug information, for instance
122 if your target is stuck with an old version of GDB that is unable to
123 process them properly or uses VMS Debug. */
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
125 #else
126 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
128 #endif
130 /* ??? Poison these here until it can be done generically. They've been
131 totally replaced in this file; make sure it stays that way. */
132 #undef DWARF2_UNWIND_INFO
133 #undef DWARF2_FRAME_INFO
134 #if (GCC_VERSION >= 3000)
135 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
136 #endif
138 /* The size of the target's pointer type. */
139 #ifndef PTR_SIZE
140 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
141 #endif
143 /* Array of RTXes referenced by the debugging information, which therefore
144 must be kept around forever. */
145 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147 /* A pointer to the base of a list of incomplete types which might be
148 completed at some later time. incomplete_types_list needs to be a
149 vec<tree, va_gc> *because we want to tell the garbage collector about
150 it. */
151 static GTY(()) vec<tree, va_gc> *incomplete_types;
153 /* A pointer to the base of a table of references to declaration
154 scopes. This table is a display which tracks the nesting
155 of declaration scopes at the current scope and containing
156 scopes. This table is used to find the proper place to
157 define type declaration DIE's. */
158 static GTY(()) vec<tree, va_gc> *decl_scope_table;
160 /* Pointers to various DWARF2 sections. */
161 static GTY(()) section *debug_info_section;
162 static GTY(()) section *debug_skeleton_info_section;
163 static GTY(()) section *debug_abbrev_section;
164 static GTY(()) section *debug_skeleton_abbrev_section;
165 static GTY(()) section *debug_aranges_section;
166 static GTY(()) section *debug_addr_section;
167 static GTY(()) section *debug_macinfo_section;
168 static const char *debug_macinfo_section_name;
169 static unsigned macinfo_label_base = 1;
170 static GTY(()) section *debug_line_section;
171 static GTY(()) section *debug_skeleton_line_section;
172 static GTY(()) section *debug_loc_section;
173 static GTY(()) section *debug_pubnames_section;
174 static GTY(()) section *debug_pubtypes_section;
175 static GTY(()) section *debug_str_section;
176 static GTY(()) section *debug_line_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
182 /* Maximum size (in bytes) of an artificially generated label. */
183 #define MAX_ARTIFICIAL_LABEL_BYTES 40
185 /* According to the (draft) DWARF 3 specification, the initial length
186 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
187 bytes are 0xffffffff, followed by the length stored in the next 8
188 bytes.
190 However, the SGI/MIPS ABI uses an initial length which is equal to
191 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
197 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
198 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
199 #endif
201 /* Round SIZE up to the nearest BOUNDARY. */
202 #define DWARF_ROUND(SIZE,BOUNDARY) \
203 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
205 /* CIE identifier. */
206 #if HOST_BITS_PER_WIDE_INT >= 64
207 #define DWARF_CIE_ID \
208 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
209 #else
210 #define DWARF_CIE_ID DW_CIE_ID
211 #endif
214 /* A vector for a table that contains frame description
215 information for each routine. */
216 #define NOT_INDEXED (-1U)
217 #define NO_INDEX_ASSIGNED (-2U)
219 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
221 struct GTY((for_user)) indirect_string_node {
222 const char *str;
223 unsigned int refcount;
224 enum dwarf_form form;
225 char *label;
226 unsigned int index;
229 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
231 typedef const char *compare_type;
233 static hashval_t hash (indirect_string_node *);
234 static bool equal (indirect_string_node *, const char *);
237 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
239 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
241 /* With split_debug_info, both the comp_dir and dwo_name go in the
242 main object file, rather than the dwo, similar to the force_direct
243 parameter elsewhere but with additional complications:
245 1) The string is needed in both the main object file and the dwo.
246 That is, the comp_dir and dwo_name will appear in both places.
248 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
249 DW_FORM_line_strp or DW_FORM_GNU_str_index.
251 3) GCC chooses the form to use late, depending on the size and
252 reference count.
254 Rather than forcing the all debug string handling functions and
255 callers to deal with these complications, simply use a separate,
256 special-cased string table for any attribute that should go in the
257 main object file. This limits the complexity to just the places
258 that need it. */
260 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
262 static GTY(()) int dw2_string_counter;
264 /* True if the compilation unit places functions in more than one section. */
265 static GTY(()) bool have_multiple_function_sections = false;
267 /* Whether the default text and cold text sections have been used at all. */
268 static GTY(()) bool text_section_used = false;
269 static GTY(()) bool cold_text_section_used = false;
271 /* The default cold text section. */
272 static GTY(()) section *cold_text_section;
274 /* The DIE for C++14 'auto' in a function return type. */
275 static GTY(()) dw_die_ref auto_die;
277 /* The DIE for C++14 'decltype(auto)' in a function return type. */
278 static GTY(()) dw_die_ref decltype_auto_die;
280 /* Forward declarations for functions defined in this file. */
282 static void output_call_frame_info (int);
283 static void dwarf2out_note_section_used (void);
285 /* Personality decl of current unit. Used only when assembler does not support
286 personality CFI. */
287 static GTY(()) rtx current_unit_personality;
289 /* Whether an eh_frame section is required. */
290 static GTY(()) bool do_eh_frame = false;
292 /* .debug_rnglists next index. */
293 static unsigned int rnglist_idx;
295 /* Data and reference forms for relocatable data. */
296 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
297 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
299 #ifndef DEBUG_FRAME_SECTION
300 #define DEBUG_FRAME_SECTION ".debug_frame"
301 #endif
303 #ifndef FUNC_BEGIN_LABEL
304 #define FUNC_BEGIN_LABEL "LFB"
305 #endif
307 #ifndef FUNC_END_LABEL
308 #define FUNC_END_LABEL "LFE"
309 #endif
311 #ifndef PROLOGUE_END_LABEL
312 #define PROLOGUE_END_LABEL "LPE"
313 #endif
315 #ifndef EPILOGUE_BEGIN_LABEL
316 #define EPILOGUE_BEGIN_LABEL "LEB"
317 #endif
319 #ifndef FRAME_BEGIN_LABEL
320 #define FRAME_BEGIN_LABEL "Lframe"
321 #endif
322 #define CIE_AFTER_SIZE_LABEL "LSCIE"
323 #define CIE_END_LABEL "LECIE"
324 #define FDE_LABEL "LSFDE"
325 #define FDE_AFTER_SIZE_LABEL "LASFDE"
326 #define FDE_END_LABEL "LEFDE"
327 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
328 #define LINE_NUMBER_END_LABEL "LELT"
329 #define LN_PROLOG_AS_LABEL "LASLTP"
330 #define LN_PROLOG_END_LABEL "LELTP"
331 #define DIE_LABEL_PREFIX "DW"
333 /* Match the base name of a file to the base name of a compilation unit. */
335 static int
336 matches_main_base (const char *path)
338 /* Cache the last query. */
339 static const char *last_path = NULL;
340 static int last_match = 0;
341 if (path != last_path)
343 const char *base;
344 int length = base_of_path (path, &base);
345 last_path = path;
346 last_match = (length == main_input_baselength
347 && memcmp (base, main_input_basename, length) == 0);
349 return last_match;
352 #ifdef DEBUG_DEBUG_STRUCT
354 static int
355 dump_struct_debug (tree type, enum debug_info_usage usage,
356 enum debug_struct_file criterion, int generic,
357 int matches, int result)
359 /* Find the type name. */
360 tree type_decl = TYPE_STUB_DECL (type);
361 tree t = type_decl;
362 const char *name = 0;
363 if (TREE_CODE (t) == TYPE_DECL)
364 t = DECL_NAME (t);
365 if (t)
366 name = IDENTIFIER_POINTER (t);
368 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
369 criterion,
370 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
371 matches ? "bas" : "hdr",
372 generic ? "gen" : "ord",
373 usage == DINFO_USAGE_DFN ? ";" :
374 usage == DINFO_USAGE_DIR_USE ? "." : "*",
375 result,
376 (void*) type_decl, name);
377 return result;
379 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
380 dump_struct_debug (type, usage, criterion, generic, matches, result)
382 #else
384 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
385 (result)
387 #endif
389 /* Get the number of HOST_WIDE_INTs needed to represent the precision
390 of the number. Some constants have a large uniform precision, so
391 we get the precision needed for the actual value of the number. */
393 static unsigned int
394 get_full_len (const wide_int &op)
396 int prec = wi::min_precision (op, UNSIGNED);
397 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
398 / HOST_BITS_PER_WIDE_INT);
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
404 enum debug_struct_file criterion;
405 tree type_decl;
406 bool generic = lang_hooks.types.generic_p (type);
408 if (generic)
409 criterion = debug_struct_generic[usage];
410 else
411 criterion = debug_struct_ordinary[usage];
413 if (criterion == DINFO_STRUCT_FILE_NONE)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415 if (criterion == DINFO_STRUCT_FILE_ANY)
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
420 if (type_decl != NULL)
422 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
429 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
432 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
433 switch to the data section instead, and write out a synthetic start label
434 for collect2 the first time around. */
436 static void
437 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
439 if (eh_frame_section == 0)
441 int flags;
443 if (EH_TABLES_CAN_BE_READ_ONLY)
445 int fde_encoding;
446 int per_encoding;
447 int lsda_encoding;
449 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
450 /*global=*/0);
451 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
452 /*global=*/1);
453 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
454 /*global=*/0);
455 flags = ((! flag_pic
456 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
457 && (fde_encoding & 0x70) != DW_EH_PE_aligned
458 && (per_encoding & 0x70) != DW_EH_PE_absptr
459 && (per_encoding & 0x70) != DW_EH_PE_aligned
460 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
461 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
462 ? 0 : SECTION_WRITE);
464 else
465 flags = SECTION_WRITE;
467 #ifdef EH_FRAME_SECTION_NAME
468 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
469 #else
470 eh_frame_section = ((flags == SECTION_WRITE)
471 ? data_section : readonly_data_section);
472 #endif /* EH_FRAME_SECTION_NAME */
475 switch_to_section (eh_frame_section);
477 #ifdef EH_FRAME_THROUGH_COLLECT2
478 /* We have no special eh_frame section. Emit special labels to guide
479 collect2. */
480 if (!back)
482 tree label = get_file_function_name ("F");
483 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
484 targetm.asm_out.globalize_label (asm_out_file,
485 IDENTIFIER_POINTER (label));
486 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
488 #endif
491 /* Switch [BACK] to the eh or debug frame table section, depending on
492 FOR_EH. */
494 static void
495 switch_to_frame_table_section (int for_eh, bool back)
497 if (for_eh)
498 switch_to_eh_frame_section (back);
499 else
501 if (!debug_frame_section)
502 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
503 SECTION_DEBUG, NULL);
504 switch_to_section (debug_frame_section);
508 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
510 enum dw_cfi_oprnd_type
511 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
513 switch (cfi)
515 case DW_CFA_nop:
516 case DW_CFA_GNU_window_save:
517 case DW_CFA_remember_state:
518 case DW_CFA_restore_state:
519 return dw_cfi_oprnd_unused;
521 case DW_CFA_set_loc:
522 case DW_CFA_advance_loc1:
523 case DW_CFA_advance_loc2:
524 case DW_CFA_advance_loc4:
525 case DW_CFA_MIPS_advance_loc8:
526 return dw_cfi_oprnd_addr;
528 case DW_CFA_offset:
529 case DW_CFA_offset_extended:
530 case DW_CFA_def_cfa:
531 case DW_CFA_offset_extended_sf:
532 case DW_CFA_def_cfa_sf:
533 case DW_CFA_restore:
534 case DW_CFA_restore_extended:
535 case DW_CFA_undefined:
536 case DW_CFA_same_value:
537 case DW_CFA_def_cfa_register:
538 case DW_CFA_register:
539 case DW_CFA_expression:
540 case DW_CFA_val_expression:
541 return dw_cfi_oprnd_reg_num;
543 case DW_CFA_def_cfa_offset:
544 case DW_CFA_GNU_args_size:
545 case DW_CFA_def_cfa_offset_sf:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_def_cfa_expression:
549 return dw_cfi_oprnd_loc;
551 default:
552 gcc_unreachable ();
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 switch (cfi)
563 case DW_CFA_def_cfa:
564 case DW_CFA_def_cfa_sf:
565 case DW_CFA_offset:
566 case DW_CFA_offset_extended_sf:
567 case DW_CFA_offset_extended:
568 return dw_cfi_oprnd_offset;
570 case DW_CFA_register:
571 return dw_cfi_oprnd_reg_num;
573 case DW_CFA_expression:
574 case DW_CFA_val_expression:
575 return dw_cfi_oprnd_loc;
577 case DW_CFA_def_cfa_expression:
578 return dw_cfi_oprnd_cfa_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (!XCOFF_DEBUGGING_INFO || for_eh)
604 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
605 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
606 " indicating 64-bit DWARF extension");
607 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
608 "FDE Length");
610 ASM_OUTPUT_LABEL (asm_out_file, l1);
612 if (for_eh)
613 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
614 else
615 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
616 debug_frame_section, "FDE CIE offset");
618 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
619 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
621 if (for_eh)
623 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
624 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
625 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
626 "FDE initial location");
627 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
628 end, begin, "FDE address range");
630 else
632 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
633 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
636 if (augmentation[0])
638 if (any_lsda_needed)
640 int size = size_of_encoded_value (lsda_encoding);
642 if (lsda_encoding == DW_EH_PE_aligned)
644 int offset = ( 4 /* Length */
645 + 4 /* CIE offset */
646 + 2 * size_of_encoded_value (fde_encoding)
647 + 1 /* Augmentation size */ );
648 int pad = -offset & (PTR_SIZE - 1);
650 size += pad;
651 gcc_assert (size_of_uleb128 (size) == 1);
654 dw2_asm_output_data_uleb128 (size, "Augmentation size");
656 if (fde->uses_eh_lsda)
658 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
659 fde->funcdef_number);
660 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
661 gen_rtx_SYMBOL_REF (Pmode, l1),
662 false,
663 "Language Specific Data Area");
665 else
667 if (lsda_encoding == DW_EH_PE_aligned)
668 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
669 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
670 "Language Specific Data Area (none)");
673 else
674 dw2_asm_output_data_uleb128 (0, "Augmentation size");
677 /* Loop through the Call Frame Instructions associated with this FDE. */
678 fde->dw_fde_current_label = begin;
680 size_t from, until, i;
682 from = 0;
683 until = vec_safe_length (fde->dw_fde_cfi);
685 if (fde->dw_fde_second_begin == NULL)
687 else if (!second)
688 until = fde->dw_fde_switch_cfi_index;
689 else
690 from = fde->dw_fde_switch_cfi_index;
692 for (i = from; i < until; i++)
693 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
696 /* If we are to emit a ref/link from function bodies to their frame tables,
697 do it now. This is typically performed to make sure that tables
698 associated with functions are dragged with them and not discarded in
699 garbage collecting links. We need to do this on a per function basis to
700 cope with -ffunction-sections. */
702 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
703 /* Switch to the function section, emit the ref to the tables, and
704 switch *back* into the table section. */
705 switch_to_section (function_section (fde->decl));
706 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
707 switch_to_frame_table_section (for_eh, true);
708 #endif
710 /* Pad the FDE out to an address sized boundary. */
711 ASM_OUTPUT_ALIGN (asm_out_file,
712 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
713 ASM_OUTPUT_LABEL (asm_out_file, l2);
715 j += 2;
718 /* Return true if frame description entry FDE is needed for EH. */
720 static bool
721 fde_needed_for_eh_p (dw_fde_ref fde)
723 if (flag_asynchronous_unwind_tables)
724 return true;
726 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
727 return true;
729 if (fde->uses_eh_lsda)
730 return true;
732 /* If exceptions are enabled, we have collected nothrow info. */
733 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
734 return false;
736 return true;
739 /* Output the call frame information used to record information
740 that relates to calculating the frame pointer, and records the
741 location of saved registers. */
743 static void
744 output_call_frame_info (int for_eh)
746 unsigned int i;
747 dw_fde_ref fde;
748 dw_cfi_ref cfi;
749 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
750 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
751 bool any_lsda_needed = false;
752 char augmentation[6];
753 int augmentation_size;
754 int fde_encoding = DW_EH_PE_absptr;
755 int per_encoding = DW_EH_PE_absptr;
756 int lsda_encoding = DW_EH_PE_absptr;
757 int return_reg;
758 rtx personality = NULL;
759 int dw_cie_version;
761 /* Don't emit a CIE if there won't be any FDEs. */
762 if (!fde_vec)
763 return;
765 /* Nothing to do if the assembler's doing it all. */
766 if (dwarf2out_do_cfi_asm ())
767 return;
769 /* If we don't have any functions we'll want to unwind out of, don't emit
770 any EH unwind information. If we make FDEs linkonce, we may have to
771 emit an empty label for an FDE that wouldn't otherwise be emitted. We
772 want to avoid having an FDE kept around when the function it refers to
773 is discarded. Example where this matters: a primary function template
774 in C++ requires EH information, an explicit specialization doesn't. */
775 if (for_eh)
777 bool any_eh_needed = false;
779 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
781 if (fde->uses_eh_lsda)
782 any_eh_needed = any_lsda_needed = true;
783 else if (fde_needed_for_eh_p (fde))
784 any_eh_needed = true;
785 else if (TARGET_USES_WEAK_UNWIND_INFO)
786 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
789 if (!any_eh_needed)
790 return;
793 /* We're going to be generating comments, so turn on app. */
794 if (flag_debug_asm)
795 app_enable ();
797 /* Switch to the proper frame section, first time. */
798 switch_to_frame_table_section (for_eh, false);
800 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
801 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
803 /* Output the CIE. */
804 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
805 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
806 if (!XCOFF_DEBUGGING_INFO || for_eh)
808 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
809 dw2_asm_output_data (4, 0xffffffff,
810 "Initial length escape value indicating 64-bit DWARF extension");
811 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
812 "Length of Common Information Entry");
814 ASM_OUTPUT_LABEL (asm_out_file, l1);
816 /* Now that the CIE pointer is PC-relative for EH,
817 use 0 to identify the CIE. */
818 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
819 (for_eh ? 0 : DWARF_CIE_ID),
820 "CIE Identifier Tag");
822 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
823 use CIE version 1, unless that would produce incorrect results
824 due to overflowing the return register column. */
825 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
826 dw_cie_version = 1;
827 if (return_reg >= 256 || dwarf_version > 2)
828 dw_cie_version = 3;
829 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
831 augmentation[0] = 0;
832 augmentation_size = 0;
834 personality = current_unit_personality;
835 if (for_eh)
837 char *p;
839 /* Augmentation:
840 z Indicates that a uleb128 is present to size the
841 augmentation section.
842 L Indicates the encoding (and thus presence) of
843 an LSDA pointer in the FDE augmentation.
844 R Indicates a non-default pointer encoding for
845 FDE code pointers.
846 P Indicates the presence of an encoding + language
847 personality routine in the CIE augmentation. */
849 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
850 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
851 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
853 p = augmentation + 1;
854 if (personality)
856 *p++ = 'P';
857 augmentation_size += 1 + size_of_encoded_value (per_encoding);
858 assemble_external_libcall (personality);
860 if (any_lsda_needed)
862 *p++ = 'L';
863 augmentation_size += 1;
865 if (fde_encoding != DW_EH_PE_absptr)
867 *p++ = 'R';
868 augmentation_size += 1;
870 if (p > augmentation + 1)
872 augmentation[0] = 'z';
873 *p = '\0';
876 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
877 if (personality && per_encoding == DW_EH_PE_aligned)
879 int offset = ( 4 /* Length */
880 + 4 /* CIE Id */
881 + 1 /* CIE version */
882 + strlen (augmentation) + 1 /* Augmentation */
883 + size_of_uleb128 (1) /* Code alignment */
884 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
885 + 1 /* RA column */
886 + 1 /* Augmentation size */
887 + 1 /* Personality encoding */ );
888 int pad = -offset & (PTR_SIZE - 1);
890 augmentation_size += pad;
892 /* Augmentations should be small, so there's scarce need to
893 iterate for a solution. Die if we exceed one uleb128 byte. */
894 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
898 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
899 if (dw_cie_version >= 4)
901 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
902 dw2_asm_output_data (1, 0, "CIE Segment Size");
904 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
905 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
906 "CIE Data Alignment Factor");
908 if (dw_cie_version == 1)
909 dw2_asm_output_data (1, return_reg, "CIE RA Column");
910 else
911 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
913 if (augmentation[0])
915 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
916 if (personality)
918 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
919 eh_data_format_name (per_encoding));
920 dw2_asm_output_encoded_addr_rtx (per_encoding,
921 personality,
922 true, NULL);
925 if (any_lsda_needed)
926 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
927 eh_data_format_name (lsda_encoding));
929 if (fde_encoding != DW_EH_PE_absptr)
930 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
931 eh_data_format_name (fde_encoding));
934 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
935 output_cfi (cfi, NULL, for_eh);
937 /* Pad the CIE out to an address sized boundary. */
938 ASM_OUTPUT_ALIGN (asm_out_file,
939 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
940 ASM_OUTPUT_LABEL (asm_out_file, l2);
942 /* Loop through all of the FDE's. */
943 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
945 unsigned int k;
947 /* Don't emit EH unwind info for leaf functions that don't need it. */
948 if (for_eh && !fde_needed_for_eh_p (fde))
949 continue;
951 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
952 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
953 augmentation, any_lsda_needed, lsda_encoding);
956 if (for_eh && targetm.terminate_dw2_eh_frame_info)
957 dw2_asm_output_data (4, 0, "End of Table");
959 /* Turn off app to make assembly quicker. */
960 if (flag_debug_asm)
961 app_disable ();
964 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
966 static void
967 dwarf2out_do_cfi_startproc (bool second)
969 int enc;
970 rtx ref;
972 fprintf (asm_out_file, "\t.cfi_startproc\n");
974 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
975 eh unwinders. */
976 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
977 return;
979 rtx personality = get_personality_function (current_function_decl);
981 if (personality)
983 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
984 ref = personality;
986 /* ??? The GAS support isn't entirely consistent. We have to
987 handle indirect support ourselves, but PC-relative is done
988 in the assembler. Further, the assembler can't handle any
989 of the weirder relocation types. */
990 if (enc & DW_EH_PE_indirect)
991 ref = dw2_force_const_mem (ref, true);
993 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
994 output_addr_const (asm_out_file, ref);
995 fputc ('\n', asm_out_file);
998 if (crtl->uses_eh_lsda)
1000 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1002 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1003 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1004 current_function_funcdef_no);
1005 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1006 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008 if (enc & DW_EH_PE_indirect)
1009 ref = dw2_force_const_mem (ref, true);
1011 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1012 output_addr_const (asm_out_file, ref);
1013 fputc ('\n', asm_out_file);
1017 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1018 this allocation may be done before pass_final. */
1020 dw_fde_ref
1021 dwarf2out_alloc_current_fde (void)
1023 dw_fde_ref fde;
1025 fde = ggc_cleared_alloc<dw_fde_node> ();
1026 fde->decl = current_function_decl;
1027 fde->funcdef_number = current_function_funcdef_no;
1028 fde->fde_index = vec_safe_length (fde_vec);
1029 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1030 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1031 fde->nothrow = crtl->nothrow;
1032 fde->drap_reg = INVALID_REGNUM;
1033 fde->vdrap_reg = INVALID_REGNUM;
1035 /* Record the FDE associated with this function. */
1036 cfun->fde = fde;
1037 vec_safe_push (fde_vec, fde);
1039 return fde;
1042 /* Output a marker (i.e. a label) for the beginning of a function, before
1043 the prologue. */
1045 void
1046 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1047 unsigned int column ATTRIBUTE_UNUSED,
1048 const char *file ATTRIBUTE_UNUSED)
1050 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 char * dup_label;
1052 dw_fde_ref fde;
1053 section *fnsec;
1054 bool do_frame;
1056 current_function_func_begin_label = NULL;
1058 do_frame = dwarf2out_do_frame ();
1060 /* ??? current_function_func_begin_label is also used by except.c for
1061 call-site information. We must emit this label if it might be used. */
1062 if (!do_frame
1063 && (!flag_exceptions
1064 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065 return;
1067 fnsec = function_section (current_function_decl);
1068 switch_to_section (fnsec);
1069 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 dup_label = xstrdup (label);
1074 current_function_func_begin_label = dup_label;
1076 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1077 if (!do_frame)
1078 return;
1080 /* Unlike the debug version, the EH version of frame unwind info is a per-
1081 function setting so we need to record whether we need it for the unit. */
1082 do_eh_frame |= dwarf2out_do_eh_frame ();
1084 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1085 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1086 would include pass_dwarf2_frame. If we've not created the FDE yet,
1087 do so now. */
1088 fde = cfun->fde;
1089 if (fde == NULL)
1090 fde = dwarf2out_alloc_current_fde ();
1092 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1093 fde->dw_fde_begin = dup_label;
1094 fde->dw_fde_current_label = dup_label;
1095 fde->in_std_section = (fnsec == text_section
1096 || (cold_text_section && fnsec == cold_text_section));
1098 /* We only want to output line number information for the genuine dwarf2
1099 prologue case, not the eh frame case. */
1100 #ifdef DWARF2_DEBUGGING_INFO
1101 if (file)
1102 dwarf2out_source_line (line, column, file, 0, true);
1103 #endif
1105 if (dwarf2out_do_cfi_asm ())
1106 dwarf2out_do_cfi_startproc (false);
1107 else
1109 rtx personality = get_personality_function (current_function_decl);
1110 if (!current_unit_personality)
1111 current_unit_personality = personality;
1113 /* We cannot keep a current personality per function as without CFI
1114 asm, at the point where we emit the CFI data, there is no current
1115 function anymore. */
1116 if (personality && current_unit_personality != personality)
1117 sorry ("multiple EH personalities are supported only with assemblers "
1118 "supporting .cfi_personality directive");
1122 /* Output a marker (i.e. a label) for the end of the generated code
1123 for a function prologue. This gets called *after* the prologue code has
1124 been generated. */
1126 void
1127 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1128 const char *file ATTRIBUTE_UNUSED)
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 /* Output a label to mark the endpoint of the code generated for this
1133 function. */
1134 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1135 current_function_funcdef_no);
1136 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1137 current_function_funcdef_no);
1138 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1141 /* Output a marker (i.e. a label) for the beginning of the generated code
1142 for a function epilogue. This gets called *before* the prologue code has
1143 been generated. */
1145 void
1146 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1147 const char *file ATTRIBUTE_UNUSED)
1149 dw_fde_ref fde = cfun->fde;
1150 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1152 if (fde->dw_fde_vms_begin_epilogue)
1153 return;
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1160 current_function_funcdef_no);
1161 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the absolute end of the generated code
1165 for a function definition. This gets called *after* the epilogue code has
1166 been generated. */
1168 void
1169 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 last_var_location_insn = NULL;
1176 cached_next_real_insn = NULL;
1178 if (dwarf2out_do_cfi_asm ())
1179 fprintf (asm_out_file, "\t.cfi_endproc\n");
1181 /* Output a label to mark the endpoint of the code generated for this
1182 function. */
1183 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1184 current_function_funcdef_no);
1185 ASM_OUTPUT_LABEL (asm_out_file, label);
1186 fde = cfun->fde;
1187 gcc_assert (fde != NULL);
1188 if (fde->dw_fde_second_begin == NULL)
1189 fde->dw_fde_end = xstrdup (label);
1192 void
1193 dwarf2out_frame_finish (void)
1195 /* Output call frame information. */
1196 if (targetm.debug_unwind_info () == UI_DWARF2)
1197 output_call_frame_info (0);
1199 /* Output another copy for the unwinder. */
1200 if (do_eh_frame)
1201 output_call_frame_info (1);
1204 /* Note that the current function section is being used for code. */
1206 static void
1207 dwarf2out_note_section_used (void)
1209 section *sec = current_function_section ();
1210 if (sec == text_section)
1211 text_section_used = true;
1212 else if (sec == cold_text_section)
1213 cold_text_section_used = true;
1216 static void var_location_switch_text_section (void);
1217 static void set_cur_line_info_table (section *);
1219 void
1220 dwarf2out_switch_text_section (void)
1222 section *sect;
1223 dw_fde_ref fde = cfun->fde;
1225 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1227 if (!in_cold_section_p)
1229 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1230 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1231 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1233 else
1235 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1236 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1237 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1239 have_multiple_function_sections = true;
1241 /* There is no need to mark used sections when not debugging. */
1242 if (cold_text_section != NULL)
1243 dwarf2out_note_section_used ();
1245 if (dwarf2out_do_cfi_asm ())
1246 fprintf (asm_out_file, "\t.cfi_endproc\n");
1248 /* Now do the real section switch. */
1249 sect = current_function_section ();
1250 switch_to_section (sect);
1252 fde->second_in_std_section
1253 = (sect == text_section
1254 || (cold_text_section && sect == cold_text_section));
1256 if (dwarf2out_do_cfi_asm ())
1257 dwarf2out_do_cfi_startproc (true);
1259 var_location_switch_text_section ();
1261 if (cold_text_section != NULL)
1262 set_cur_line_info_table (sect);
1265 /* And now, the subset of the debugging information support code necessary
1266 for emitting location expressions. */
1268 /* Data about a single source file. */
1269 struct GTY((for_user)) dwarf_file_data {
1270 const char * filename;
1271 int emitted_number;
1274 /* Describe an entry into the .debug_addr section. */
1276 enum ate_kind {
1277 ate_kind_rtx,
1278 ate_kind_rtx_dtprel,
1279 ate_kind_label
1282 struct GTY((for_user)) addr_table_entry {
1283 enum ate_kind kind;
1284 unsigned int refcount;
1285 unsigned int index;
1286 union addr_table_entry_struct_union
1288 rtx GTY ((tag ("0"))) rtl;
1289 char * GTY ((tag ("1"))) label;
1291 GTY ((desc ("%1.kind"))) addr;
1294 typedef unsigned int var_loc_view;
1296 /* Location lists are ranges + location descriptions for that range,
1297 so you can track variables that are in different places over
1298 their entire life. */
1299 typedef struct GTY(()) dw_loc_list_struct {
1300 dw_loc_list_ref dw_loc_next;
1301 const char *begin; /* Label and addr_entry for start of range */
1302 addr_table_entry *begin_entry;
1303 const char *end; /* Label for end of range */
1304 char *ll_symbol; /* Label for beginning of location list.
1305 Only on head of list. */
1306 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1307 const char *section; /* Section this loclist is relative to */
1308 dw_loc_descr_ref expr;
1309 var_loc_view vbegin, vend;
1310 hashval_t hash;
1311 /* True if all addresses in this and subsequent lists are known to be
1312 resolved. */
1313 bool resolved_addr;
1314 /* True if this list has been replaced by dw_loc_next. */
1315 bool replaced;
1316 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1317 section. */
1318 unsigned char emitted : 1;
1319 /* True if hash field is index rather than hash value. */
1320 unsigned char num_assigned : 1;
1321 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1322 unsigned char offset_emitted : 1;
1323 /* True if note_variable_value_in_expr has been called on it. */
1324 unsigned char noted_variable_value : 1;
1325 /* True if the range should be emitted even if begin and end
1326 are the same. */
1327 bool force;
1328 } dw_loc_list_node;
1330 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1331 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1333 /* Convert a DWARF stack opcode into its string name. */
1335 static const char *
1336 dwarf_stack_op_name (unsigned int op)
1338 const char *name = get_DW_OP_name (op);
1340 if (name != NULL)
1341 return name;
1343 return "OP_<unknown>";
1346 /* Return TRUE iff we're to output location view lists as a separate
1347 attribute next to the location lists, as an extension compatible
1348 with DWARF 2 and above. */
1350 static inline bool
1351 dwarf2out_locviews_in_attribute ()
1353 return debug_variable_location_views == 1;
1356 /* Return TRUE iff we're to output location view lists as part of the
1357 location lists, as proposed for standardization after DWARF 5. */
1359 static inline bool
1360 dwarf2out_locviews_in_loclist ()
1362 #ifndef DW_LLE_view_pair
1363 return false;
1364 #else
1365 return debug_variable_location_views == -1;
1366 #endif
1369 /* Return a pointer to a newly allocated location description. Location
1370 descriptions are simple expression terms that can be strung
1371 together to form more complicated location (address) descriptions. */
1373 static inline dw_loc_descr_ref
1374 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1375 unsigned HOST_WIDE_INT oprnd2)
1377 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1379 descr->dw_loc_opc = op;
1380 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1381 descr->dw_loc_oprnd1.val_entry = NULL;
1382 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1383 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1384 descr->dw_loc_oprnd2.val_entry = NULL;
1385 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1387 return descr;
1390 /* Add a location description term to a location description expression. */
1392 static inline void
1393 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1395 dw_loc_descr_ref *d;
1397 /* Find the end of the chain. */
1398 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1401 *d = descr;
1404 /* Compare two location operands for exact equality. */
1406 static bool
1407 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1409 if (a->val_class != b->val_class)
1410 return false;
1411 switch (a->val_class)
1413 case dw_val_class_none:
1414 return true;
1415 case dw_val_class_addr:
1416 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1418 case dw_val_class_offset:
1419 case dw_val_class_unsigned_const:
1420 case dw_val_class_const:
1421 case dw_val_class_unsigned_const_implicit:
1422 case dw_val_class_const_implicit:
1423 case dw_val_class_range_list:
1424 /* These are all HOST_WIDE_INT, signed or unsigned. */
1425 return a->v.val_unsigned == b->v.val_unsigned;
1427 case dw_val_class_loc:
1428 return a->v.val_loc == b->v.val_loc;
1429 case dw_val_class_loc_list:
1430 return a->v.val_loc_list == b->v.val_loc_list;
1431 case dw_val_class_view_list:
1432 return a->v.val_view_list == b->v.val_view_list;
1433 case dw_val_class_die_ref:
1434 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1435 case dw_val_class_fde_ref:
1436 return a->v.val_fde_index == b->v.val_fde_index;
1437 case dw_val_class_lbl_id:
1438 case dw_val_class_lineptr:
1439 case dw_val_class_macptr:
1440 case dw_val_class_loclistsptr:
1441 case dw_val_class_high_pc:
1442 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1443 case dw_val_class_str:
1444 return a->v.val_str == b->v.val_str;
1445 case dw_val_class_flag:
1446 return a->v.val_flag == b->v.val_flag;
1447 case dw_val_class_file:
1448 case dw_val_class_file_implicit:
1449 return a->v.val_file == b->v.val_file;
1450 case dw_val_class_decl_ref:
1451 return a->v.val_decl_ref == b->v.val_decl_ref;
1453 case dw_val_class_const_double:
1454 return (a->v.val_double.high == b->v.val_double.high
1455 && a->v.val_double.low == b->v.val_double.low);
1457 case dw_val_class_wide_int:
1458 return *a->v.val_wide == *b->v.val_wide;
1460 case dw_val_class_vec:
1462 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1463 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1465 return (a_len == b_len
1466 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1469 case dw_val_class_data8:
1470 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1472 case dw_val_class_vms_delta:
1473 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1474 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1476 case dw_val_class_discr_value:
1477 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1478 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1479 case dw_val_class_discr_list:
1480 /* It makes no sense comparing two discriminant value lists. */
1481 return false;
1483 gcc_unreachable ();
1486 /* Compare two location atoms for exact equality. */
1488 static bool
1489 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1491 if (a->dw_loc_opc != b->dw_loc_opc)
1492 return false;
1494 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1495 address size, but since we always allocate cleared storage it
1496 should be zero for other types of locations. */
1497 if (a->dtprel != b->dtprel)
1498 return false;
1500 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1501 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1504 /* Compare two complete location expressions for exact equality. */
1506 bool
1507 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1509 while (1)
1511 if (a == b)
1512 return true;
1513 if (a == NULL || b == NULL)
1514 return false;
1515 if (!loc_descr_equal_p_1 (a, b))
1516 return false;
1518 a = a->dw_loc_next;
1519 b = b->dw_loc_next;
1524 /* Add a constant POLY_OFFSET to a location expression. */
1526 static void
1527 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1529 dw_loc_descr_ref loc;
1530 HOST_WIDE_INT *p;
1532 gcc_assert (*list_head != NULL);
1534 if (known_eq (poly_offset, 0))
1535 return;
1537 /* Find the end of the chain. */
1538 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1541 HOST_WIDE_INT offset;
1542 if (!poly_offset.is_constant (&offset))
1544 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1545 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1546 return;
1549 p = NULL;
1550 if (loc->dw_loc_opc == DW_OP_fbreg
1551 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1552 p = &loc->dw_loc_oprnd1.v.val_int;
1553 else if (loc->dw_loc_opc == DW_OP_bregx)
1554 p = &loc->dw_loc_oprnd2.v.val_int;
1556 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1557 offset. Don't optimize if an signed integer overflow would happen. */
1558 if (p != NULL
1559 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1560 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1561 *p += offset;
1563 else if (offset > 0)
1564 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1566 else
1568 loc->dw_loc_next
1569 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1570 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1574 /* Return a pointer to a newly allocated location description for
1575 REG and OFFSET. */
1577 static inline dw_loc_descr_ref
1578 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1580 HOST_WIDE_INT const_offset;
1581 if (offset.is_constant (&const_offset))
1583 if (reg <= 31)
1584 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1585 const_offset, 0);
1586 else
1587 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1589 else
1591 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1592 loc_descr_plus_const (&ret, offset);
1593 return ret;
1597 /* Add a constant OFFSET to a location list. */
1599 static void
1600 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1602 dw_loc_list_ref d;
1603 for (d = list_head; d != NULL; d = d->dw_loc_next)
1604 loc_descr_plus_const (&d->expr, offset);
1607 #define DWARF_REF_SIZE \
1608 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1610 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1611 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1612 DW_FORM_data16 with 128 bits. */
1613 #define DWARF_LARGEST_DATA_FORM_BITS \
1614 (dwarf_version >= 5 ? 128 : 64)
1616 /* Utility inline function for construction of ops that were GNU extension
1617 before DWARF 5. */
1618 static inline enum dwarf_location_atom
1619 dwarf_OP (enum dwarf_location_atom op)
1621 switch (op)
1623 case DW_OP_implicit_pointer:
1624 if (dwarf_version < 5)
1625 return DW_OP_GNU_implicit_pointer;
1626 break;
1628 case DW_OP_entry_value:
1629 if (dwarf_version < 5)
1630 return DW_OP_GNU_entry_value;
1631 break;
1633 case DW_OP_const_type:
1634 if (dwarf_version < 5)
1635 return DW_OP_GNU_const_type;
1636 break;
1638 case DW_OP_regval_type:
1639 if (dwarf_version < 5)
1640 return DW_OP_GNU_regval_type;
1641 break;
1643 case DW_OP_deref_type:
1644 if (dwarf_version < 5)
1645 return DW_OP_GNU_deref_type;
1646 break;
1648 case DW_OP_convert:
1649 if (dwarf_version < 5)
1650 return DW_OP_GNU_convert;
1651 break;
1653 case DW_OP_reinterpret:
1654 if (dwarf_version < 5)
1655 return DW_OP_GNU_reinterpret;
1656 break;
1658 default:
1659 break;
1661 return op;
1664 /* Similarly for attributes. */
1665 static inline enum dwarf_attribute
1666 dwarf_AT (enum dwarf_attribute at)
1668 switch (at)
1670 case DW_AT_call_return_pc:
1671 if (dwarf_version < 5)
1672 return DW_AT_low_pc;
1673 break;
1675 case DW_AT_call_tail_call:
1676 if (dwarf_version < 5)
1677 return DW_AT_GNU_tail_call;
1678 break;
1680 case DW_AT_call_origin:
1681 if (dwarf_version < 5)
1682 return DW_AT_abstract_origin;
1683 break;
1685 case DW_AT_call_target:
1686 if (dwarf_version < 5)
1687 return DW_AT_GNU_call_site_target;
1688 break;
1690 case DW_AT_call_target_clobbered:
1691 if (dwarf_version < 5)
1692 return DW_AT_GNU_call_site_target_clobbered;
1693 break;
1695 case DW_AT_call_parameter:
1696 if (dwarf_version < 5)
1697 return DW_AT_abstract_origin;
1698 break;
1700 case DW_AT_call_value:
1701 if (dwarf_version < 5)
1702 return DW_AT_GNU_call_site_value;
1703 break;
1705 case DW_AT_call_data_value:
1706 if (dwarf_version < 5)
1707 return DW_AT_GNU_call_site_data_value;
1708 break;
1710 case DW_AT_call_all_calls:
1711 if (dwarf_version < 5)
1712 return DW_AT_GNU_all_call_sites;
1713 break;
1715 case DW_AT_call_all_tail_calls:
1716 if (dwarf_version < 5)
1717 return DW_AT_GNU_all_tail_call_sites;
1718 break;
1720 case DW_AT_dwo_name:
1721 if (dwarf_version < 5)
1722 return DW_AT_GNU_dwo_name;
1723 break;
1725 default:
1726 break;
1728 return at;
1731 /* And similarly for tags. */
1732 static inline enum dwarf_tag
1733 dwarf_TAG (enum dwarf_tag tag)
1735 switch (tag)
1737 case DW_TAG_call_site:
1738 if (dwarf_version < 5)
1739 return DW_TAG_GNU_call_site;
1740 break;
1742 case DW_TAG_call_site_parameter:
1743 if (dwarf_version < 5)
1744 return DW_TAG_GNU_call_site_parameter;
1745 break;
1747 default:
1748 break;
1750 return tag;
1753 static unsigned long int get_base_type_offset (dw_die_ref);
1755 /* Return the size of a location descriptor. */
1757 static unsigned long
1758 size_of_loc_descr (dw_loc_descr_ref loc)
1760 unsigned long size = 1;
1762 switch (loc->dw_loc_opc)
1764 case DW_OP_addr:
1765 size += DWARF2_ADDR_SIZE;
1766 break;
1767 case DW_OP_GNU_addr_index:
1768 case DW_OP_GNU_const_index:
1769 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1770 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1771 break;
1772 case DW_OP_const1u:
1773 case DW_OP_const1s:
1774 size += 1;
1775 break;
1776 case DW_OP_const2u:
1777 case DW_OP_const2s:
1778 size += 2;
1779 break;
1780 case DW_OP_const4u:
1781 case DW_OP_const4s:
1782 size += 4;
1783 break;
1784 case DW_OP_const8u:
1785 case DW_OP_const8s:
1786 size += 8;
1787 break;
1788 case DW_OP_constu:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 break;
1791 case DW_OP_consts:
1792 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1793 break;
1794 case DW_OP_pick:
1795 size += 1;
1796 break;
1797 case DW_OP_plus_uconst:
1798 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1799 break;
1800 case DW_OP_skip:
1801 case DW_OP_bra:
1802 size += 2;
1803 break;
1804 case DW_OP_breg0:
1805 case DW_OP_breg1:
1806 case DW_OP_breg2:
1807 case DW_OP_breg3:
1808 case DW_OP_breg4:
1809 case DW_OP_breg5:
1810 case DW_OP_breg6:
1811 case DW_OP_breg7:
1812 case DW_OP_breg8:
1813 case DW_OP_breg9:
1814 case DW_OP_breg10:
1815 case DW_OP_breg11:
1816 case DW_OP_breg12:
1817 case DW_OP_breg13:
1818 case DW_OP_breg14:
1819 case DW_OP_breg15:
1820 case DW_OP_breg16:
1821 case DW_OP_breg17:
1822 case DW_OP_breg18:
1823 case DW_OP_breg19:
1824 case DW_OP_breg20:
1825 case DW_OP_breg21:
1826 case DW_OP_breg22:
1827 case DW_OP_breg23:
1828 case DW_OP_breg24:
1829 case DW_OP_breg25:
1830 case DW_OP_breg26:
1831 case DW_OP_breg27:
1832 case DW_OP_breg28:
1833 case DW_OP_breg29:
1834 case DW_OP_breg30:
1835 case DW_OP_breg31:
1836 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1837 break;
1838 case DW_OP_regx:
1839 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1840 break;
1841 case DW_OP_fbreg:
1842 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1843 break;
1844 case DW_OP_bregx:
1845 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1846 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1847 break;
1848 case DW_OP_piece:
1849 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1850 break;
1851 case DW_OP_bit_piece:
1852 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1854 break;
1855 case DW_OP_deref_size:
1856 case DW_OP_xderef_size:
1857 size += 1;
1858 break;
1859 case DW_OP_call2:
1860 size += 2;
1861 break;
1862 case DW_OP_call4:
1863 size += 4;
1864 break;
1865 case DW_OP_call_ref:
1866 case DW_OP_GNU_variable_value:
1867 size += DWARF_REF_SIZE;
1868 break;
1869 case DW_OP_implicit_value:
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1871 + loc->dw_loc_oprnd1.v.val_unsigned;
1872 break;
1873 case DW_OP_implicit_pointer:
1874 case DW_OP_GNU_implicit_pointer:
1875 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1876 break;
1877 case DW_OP_entry_value:
1878 case DW_OP_GNU_entry_value:
1880 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1881 size += size_of_uleb128 (op_size) + op_size;
1882 break;
1884 case DW_OP_const_type:
1885 case DW_OP_GNU_const_type:
1887 unsigned long o
1888 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1889 size += size_of_uleb128 (o) + 1;
1890 switch (loc->dw_loc_oprnd2.val_class)
1892 case dw_val_class_vec:
1893 size += loc->dw_loc_oprnd2.v.val_vec.length
1894 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1895 break;
1896 case dw_val_class_const:
1897 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1898 break;
1899 case dw_val_class_const_double:
1900 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1901 break;
1902 case dw_val_class_wide_int:
1903 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1904 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1905 break;
1906 default:
1907 gcc_unreachable ();
1909 break;
1911 case DW_OP_regval_type:
1912 case DW_OP_GNU_regval_type:
1914 unsigned long o
1915 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1916 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1917 + size_of_uleb128 (o);
1919 break;
1920 case DW_OP_deref_type:
1921 case DW_OP_GNU_deref_type:
1923 unsigned long o
1924 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1925 size += 1 + size_of_uleb128 (o);
1927 break;
1928 case DW_OP_convert:
1929 case DW_OP_reinterpret:
1930 case DW_OP_GNU_convert:
1931 case DW_OP_GNU_reinterpret:
1932 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1933 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1934 else
1936 unsigned long o
1937 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1938 size += size_of_uleb128 (o);
1940 break;
1941 case DW_OP_GNU_parameter_ref:
1942 size += 4;
1943 break;
1944 default:
1945 break;
1948 return size;
1951 /* Return the size of a series of location descriptors. */
1953 unsigned long
1954 size_of_locs (dw_loc_descr_ref loc)
1956 dw_loc_descr_ref l;
1957 unsigned long size;
1959 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1960 field, to avoid writing to a PCH file. */
1961 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1963 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1964 break;
1965 size += size_of_loc_descr (l);
1967 if (! l)
1968 return size;
1970 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1972 l->dw_loc_addr = size;
1973 size += size_of_loc_descr (l);
1976 return size;
1979 /* Return the size of the value in a DW_AT_discr_value attribute. */
1981 static int
1982 size_of_discr_value (dw_discr_value *discr_value)
1984 if (discr_value->pos)
1985 return size_of_uleb128 (discr_value->v.uval);
1986 else
1987 return size_of_sleb128 (discr_value->v.sval);
1990 /* Return the size of the value in a DW_AT_discr_list attribute. */
1992 static int
1993 size_of_discr_list (dw_discr_list_ref discr_list)
1995 int size = 0;
1997 for (dw_discr_list_ref list = discr_list;
1998 list != NULL;
1999 list = list->dw_discr_next)
2001 /* One byte for the discriminant value descriptor, and then one or two
2002 LEB128 numbers, depending on whether it's a single case label or a
2003 range label. */
2004 size += 1;
2005 size += size_of_discr_value (&list->dw_discr_lower_bound);
2006 if (list->dw_discr_range != 0)
2007 size += size_of_discr_value (&list->dw_discr_upper_bound);
2009 return size;
2012 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2013 static void get_ref_die_offset_label (char *, dw_die_ref);
2014 static unsigned long int get_ref_die_offset (dw_die_ref);
2016 /* Output location description stack opcode's operands (if any).
2017 The for_eh_or_skip parameter controls whether register numbers are
2018 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2019 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2020 info). This should be suppressed for the cases that have not been converted
2021 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2023 static void
2024 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2026 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2027 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2029 switch (loc->dw_loc_opc)
2031 #ifdef DWARF2_DEBUGGING_INFO
2032 case DW_OP_const2u:
2033 case DW_OP_const2s:
2034 dw2_asm_output_data (2, val1->v.val_int, NULL);
2035 break;
2036 case DW_OP_const4u:
2037 if (loc->dtprel)
2039 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2040 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2041 val1->v.val_addr);
2042 fputc ('\n', asm_out_file);
2043 break;
2045 /* FALLTHRU */
2046 case DW_OP_const4s:
2047 dw2_asm_output_data (4, val1->v.val_int, NULL);
2048 break;
2049 case DW_OP_const8u:
2050 if (loc->dtprel)
2052 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2053 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2054 val1->v.val_addr);
2055 fputc ('\n', asm_out_file);
2056 break;
2058 /* FALLTHRU */
2059 case DW_OP_const8s:
2060 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2061 dw2_asm_output_data (8, val1->v.val_int, NULL);
2062 break;
2063 case DW_OP_skip:
2064 case DW_OP_bra:
2066 int offset;
2068 gcc_assert (val1->val_class == dw_val_class_loc);
2069 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2071 dw2_asm_output_data (2, offset, NULL);
2073 break;
2074 case DW_OP_implicit_value:
2075 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2076 switch (val2->val_class)
2078 case dw_val_class_const:
2079 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2080 break;
2081 case dw_val_class_vec:
2083 unsigned int elt_size = val2->v.val_vec.elt_size;
2084 unsigned int len = val2->v.val_vec.length;
2085 unsigned int i;
2086 unsigned char *p;
2088 if (elt_size > sizeof (HOST_WIDE_INT))
2090 elt_size /= 2;
2091 len *= 2;
2093 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2094 i < len;
2095 i++, p += elt_size)
2096 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2097 "fp or vector constant word %u", i);
2099 break;
2100 case dw_val_class_const_double:
2102 unsigned HOST_WIDE_INT first, second;
2104 if (WORDS_BIG_ENDIAN)
2106 first = val2->v.val_double.high;
2107 second = val2->v.val_double.low;
2109 else
2111 first = val2->v.val_double.low;
2112 second = val2->v.val_double.high;
2114 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2115 first, NULL);
2116 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2117 second, NULL);
2119 break;
2120 case dw_val_class_wide_int:
2122 int i;
2123 int len = get_full_len (*val2->v.val_wide);
2124 if (WORDS_BIG_ENDIAN)
2125 for (i = len - 1; i >= 0; --i)
2126 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2127 val2->v.val_wide->elt (i), NULL);
2128 else
2129 for (i = 0; i < len; ++i)
2130 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2131 val2->v.val_wide->elt (i), NULL);
2133 break;
2134 case dw_val_class_addr:
2135 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2136 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2137 break;
2138 default:
2139 gcc_unreachable ();
2141 break;
2142 #else
2143 case DW_OP_const2u:
2144 case DW_OP_const2s:
2145 case DW_OP_const4u:
2146 case DW_OP_const4s:
2147 case DW_OP_const8u:
2148 case DW_OP_const8s:
2149 case DW_OP_skip:
2150 case DW_OP_bra:
2151 case DW_OP_implicit_value:
2152 /* We currently don't make any attempt to make sure these are
2153 aligned properly like we do for the main unwind info, so
2154 don't support emitting things larger than a byte if we're
2155 only doing unwinding. */
2156 gcc_unreachable ();
2157 #endif
2158 case DW_OP_const1u:
2159 case DW_OP_const1s:
2160 dw2_asm_output_data (1, val1->v.val_int, NULL);
2161 break;
2162 case DW_OP_constu:
2163 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2164 break;
2165 case DW_OP_consts:
2166 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2167 break;
2168 case DW_OP_pick:
2169 dw2_asm_output_data (1, val1->v.val_int, NULL);
2170 break;
2171 case DW_OP_plus_uconst:
2172 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2173 break;
2174 case DW_OP_breg0:
2175 case DW_OP_breg1:
2176 case DW_OP_breg2:
2177 case DW_OP_breg3:
2178 case DW_OP_breg4:
2179 case DW_OP_breg5:
2180 case DW_OP_breg6:
2181 case DW_OP_breg7:
2182 case DW_OP_breg8:
2183 case DW_OP_breg9:
2184 case DW_OP_breg10:
2185 case DW_OP_breg11:
2186 case DW_OP_breg12:
2187 case DW_OP_breg13:
2188 case DW_OP_breg14:
2189 case DW_OP_breg15:
2190 case DW_OP_breg16:
2191 case DW_OP_breg17:
2192 case DW_OP_breg18:
2193 case DW_OP_breg19:
2194 case DW_OP_breg20:
2195 case DW_OP_breg21:
2196 case DW_OP_breg22:
2197 case DW_OP_breg23:
2198 case DW_OP_breg24:
2199 case DW_OP_breg25:
2200 case DW_OP_breg26:
2201 case DW_OP_breg27:
2202 case DW_OP_breg28:
2203 case DW_OP_breg29:
2204 case DW_OP_breg30:
2205 case DW_OP_breg31:
2206 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2207 break;
2208 case DW_OP_regx:
2210 unsigned r = val1->v.val_unsigned;
2211 if (for_eh_or_skip >= 0)
2212 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2213 gcc_assert (size_of_uleb128 (r)
2214 == size_of_uleb128 (val1->v.val_unsigned));
2215 dw2_asm_output_data_uleb128 (r, NULL);
2217 break;
2218 case DW_OP_fbreg:
2219 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2220 break;
2221 case DW_OP_bregx:
2223 unsigned r = val1->v.val_unsigned;
2224 if (for_eh_or_skip >= 0)
2225 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2226 gcc_assert (size_of_uleb128 (r)
2227 == size_of_uleb128 (val1->v.val_unsigned));
2228 dw2_asm_output_data_uleb128 (r, NULL);
2229 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2231 break;
2232 case DW_OP_piece:
2233 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2234 break;
2235 case DW_OP_bit_piece:
2236 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2237 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2238 break;
2239 case DW_OP_deref_size:
2240 case DW_OP_xderef_size:
2241 dw2_asm_output_data (1, val1->v.val_int, NULL);
2242 break;
2244 case DW_OP_addr:
2245 if (loc->dtprel)
2247 if (targetm.asm_out.output_dwarf_dtprel)
2249 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2250 DWARF2_ADDR_SIZE,
2251 val1->v.val_addr);
2252 fputc ('\n', asm_out_file);
2254 else
2255 gcc_unreachable ();
2257 else
2259 #ifdef DWARF2_DEBUGGING_INFO
2260 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2261 #else
2262 gcc_unreachable ();
2263 #endif
2265 break;
2267 case DW_OP_GNU_addr_index:
2268 case DW_OP_GNU_const_index:
2269 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2270 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2271 "(index into .debug_addr)");
2272 break;
2274 case DW_OP_call2:
2275 case DW_OP_call4:
2277 unsigned long die_offset
2278 = get_ref_die_offset (val1->v.val_die_ref.die);
2279 /* Make sure the offset has been computed and that we can encode it as
2280 an operand. */
2281 gcc_assert (die_offset > 0
2282 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2283 ? 0xffff
2284 : 0xffffffff));
2285 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2286 die_offset, NULL);
2288 break;
2290 case DW_OP_call_ref:
2291 case DW_OP_GNU_variable_value:
2293 char label[MAX_ARTIFICIAL_LABEL_BYTES
2294 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2295 gcc_assert (val1->val_class == dw_val_class_die_ref);
2296 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2297 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2299 break;
2301 case DW_OP_implicit_pointer:
2302 case DW_OP_GNU_implicit_pointer:
2304 char label[MAX_ARTIFICIAL_LABEL_BYTES
2305 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2306 gcc_assert (val1->val_class == dw_val_class_die_ref);
2307 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2308 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2309 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2311 break;
2313 case DW_OP_entry_value:
2314 case DW_OP_GNU_entry_value:
2315 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2316 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2317 break;
2319 case DW_OP_const_type:
2320 case DW_OP_GNU_const_type:
2322 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2323 gcc_assert (o);
2324 dw2_asm_output_data_uleb128 (o, NULL);
2325 switch (val2->val_class)
2327 case dw_val_class_const:
2328 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2329 dw2_asm_output_data (1, l, NULL);
2330 dw2_asm_output_data (l, val2->v.val_int, NULL);
2331 break;
2332 case dw_val_class_vec:
2334 unsigned int elt_size = val2->v.val_vec.elt_size;
2335 unsigned int len = val2->v.val_vec.length;
2336 unsigned int i;
2337 unsigned char *p;
2339 l = len * elt_size;
2340 dw2_asm_output_data (1, l, NULL);
2341 if (elt_size > sizeof (HOST_WIDE_INT))
2343 elt_size /= 2;
2344 len *= 2;
2346 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2347 i < len;
2348 i++, p += elt_size)
2349 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2350 "fp or vector constant word %u", i);
2352 break;
2353 case dw_val_class_const_double:
2355 unsigned HOST_WIDE_INT first, second;
2356 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2358 dw2_asm_output_data (1, 2 * l, NULL);
2359 if (WORDS_BIG_ENDIAN)
2361 first = val2->v.val_double.high;
2362 second = val2->v.val_double.low;
2364 else
2366 first = val2->v.val_double.low;
2367 second = val2->v.val_double.high;
2369 dw2_asm_output_data (l, first, NULL);
2370 dw2_asm_output_data (l, second, NULL);
2372 break;
2373 case dw_val_class_wide_int:
2375 int i;
2376 int len = get_full_len (*val2->v.val_wide);
2377 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2379 dw2_asm_output_data (1, len * l, NULL);
2380 if (WORDS_BIG_ENDIAN)
2381 for (i = len - 1; i >= 0; --i)
2382 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2383 else
2384 for (i = 0; i < len; ++i)
2385 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2387 break;
2388 default:
2389 gcc_unreachable ();
2392 break;
2393 case DW_OP_regval_type:
2394 case DW_OP_GNU_regval_type:
2396 unsigned r = val1->v.val_unsigned;
2397 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2398 gcc_assert (o);
2399 if (for_eh_or_skip >= 0)
2401 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2402 gcc_assert (size_of_uleb128 (r)
2403 == size_of_uleb128 (val1->v.val_unsigned));
2405 dw2_asm_output_data_uleb128 (r, NULL);
2406 dw2_asm_output_data_uleb128 (o, NULL);
2408 break;
2409 case DW_OP_deref_type:
2410 case DW_OP_GNU_deref_type:
2412 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2413 gcc_assert (o);
2414 dw2_asm_output_data (1, val1->v.val_int, NULL);
2415 dw2_asm_output_data_uleb128 (o, NULL);
2417 break;
2418 case DW_OP_convert:
2419 case DW_OP_reinterpret:
2420 case DW_OP_GNU_convert:
2421 case DW_OP_GNU_reinterpret:
2422 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2423 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2424 else
2426 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2427 gcc_assert (o);
2428 dw2_asm_output_data_uleb128 (o, NULL);
2430 break;
2432 case DW_OP_GNU_parameter_ref:
2434 unsigned long o;
2435 gcc_assert (val1->val_class == dw_val_class_die_ref);
2436 o = get_ref_die_offset (val1->v.val_die_ref.die);
2437 dw2_asm_output_data (4, o, NULL);
2439 break;
2441 default:
2442 /* Other codes have no operands. */
2443 break;
2447 /* Output a sequence of location operations.
2448 The for_eh_or_skip parameter controls whether register numbers are
2449 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2450 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2451 info). This should be suppressed for the cases that have not been converted
2452 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2454 void
2455 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2457 for (; loc != NULL; loc = loc->dw_loc_next)
2459 enum dwarf_location_atom opc = loc->dw_loc_opc;
2460 /* Output the opcode. */
2461 if (for_eh_or_skip >= 0
2462 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2464 unsigned r = (opc - DW_OP_breg0);
2465 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2466 gcc_assert (r <= 31);
2467 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2469 else if (for_eh_or_skip >= 0
2470 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2472 unsigned r = (opc - DW_OP_reg0);
2473 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2474 gcc_assert (r <= 31);
2475 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2478 dw2_asm_output_data (1, opc,
2479 "%s", dwarf_stack_op_name (opc));
2481 /* Output the operand(s) (if any). */
2482 output_loc_operands (loc, for_eh_or_skip);
2486 /* Output location description stack opcode's operands (if any).
2487 The output is single bytes on a line, suitable for .cfi_escape. */
2489 static void
2490 output_loc_operands_raw (dw_loc_descr_ref loc)
2492 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2493 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2495 switch (loc->dw_loc_opc)
2497 case DW_OP_addr:
2498 case DW_OP_GNU_addr_index:
2499 case DW_OP_GNU_const_index:
2500 case DW_OP_implicit_value:
2501 /* We cannot output addresses in .cfi_escape, only bytes. */
2502 gcc_unreachable ();
2504 case DW_OP_const1u:
2505 case DW_OP_const1s:
2506 case DW_OP_pick:
2507 case DW_OP_deref_size:
2508 case DW_OP_xderef_size:
2509 fputc (',', asm_out_file);
2510 dw2_asm_output_data_raw (1, val1->v.val_int);
2511 break;
2513 case DW_OP_const2u:
2514 case DW_OP_const2s:
2515 fputc (',', asm_out_file);
2516 dw2_asm_output_data_raw (2, val1->v.val_int);
2517 break;
2519 case DW_OP_const4u:
2520 case DW_OP_const4s:
2521 fputc (',', asm_out_file);
2522 dw2_asm_output_data_raw (4, val1->v.val_int);
2523 break;
2525 case DW_OP_const8u:
2526 case DW_OP_const8s:
2527 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2528 fputc (',', asm_out_file);
2529 dw2_asm_output_data_raw (8, val1->v.val_int);
2530 break;
2532 case DW_OP_skip:
2533 case DW_OP_bra:
2535 int offset;
2537 gcc_assert (val1->val_class == dw_val_class_loc);
2538 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2540 fputc (',', asm_out_file);
2541 dw2_asm_output_data_raw (2, offset);
2543 break;
2545 case DW_OP_regx:
2547 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2548 gcc_assert (size_of_uleb128 (r)
2549 == size_of_uleb128 (val1->v.val_unsigned));
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_uleb128_raw (r);
2553 break;
2555 case DW_OP_constu:
2556 case DW_OP_plus_uconst:
2557 case DW_OP_piece:
2558 fputc (',', asm_out_file);
2559 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2560 break;
2562 case DW_OP_bit_piece:
2563 fputc (',', asm_out_file);
2564 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2565 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2566 break;
2568 case DW_OP_consts:
2569 case DW_OP_breg0:
2570 case DW_OP_breg1:
2571 case DW_OP_breg2:
2572 case DW_OP_breg3:
2573 case DW_OP_breg4:
2574 case DW_OP_breg5:
2575 case DW_OP_breg6:
2576 case DW_OP_breg7:
2577 case DW_OP_breg8:
2578 case DW_OP_breg9:
2579 case DW_OP_breg10:
2580 case DW_OP_breg11:
2581 case DW_OP_breg12:
2582 case DW_OP_breg13:
2583 case DW_OP_breg14:
2584 case DW_OP_breg15:
2585 case DW_OP_breg16:
2586 case DW_OP_breg17:
2587 case DW_OP_breg18:
2588 case DW_OP_breg19:
2589 case DW_OP_breg20:
2590 case DW_OP_breg21:
2591 case DW_OP_breg22:
2592 case DW_OP_breg23:
2593 case DW_OP_breg24:
2594 case DW_OP_breg25:
2595 case DW_OP_breg26:
2596 case DW_OP_breg27:
2597 case DW_OP_breg28:
2598 case DW_OP_breg29:
2599 case DW_OP_breg30:
2600 case DW_OP_breg31:
2601 case DW_OP_fbreg:
2602 fputc (',', asm_out_file);
2603 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2604 break;
2606 case DW_OP_bregx:
2608 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2609 gcc_assert (size_of_uleb128 (r)
2610 == size_of_uleb128 (val1->v.val_unsigned));
2611 fputc (',', asm_out_file);
2612 dw2_asm_output_data_uleb128_raw (r);
2613 fputc (',', asm_out_file);
2614 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2616 break;
2618 case DW_OP_implicit_pointer:
2619 case DW_OP_entry_value:
2620 case DW_OP_const_type:
2621 case DW_OP_regval_type:
2622 case DW_OP_deref_type:
2623 case DW_OP_convert:
2624 case DW_OP_reinterpret:
2625 case DW_OP_GNU_implicit_pointer:
2626 case DW_OP_GNU_entry_value:
2627 case DW_OP_GNU_const_type:
2628 case DW_OP_GNU_regval_type:
2629 case DW_OP_GNU_deref_type:
2630 case DW_OP_GNU_convert:
2631 case DW_OP_GNU_reinterpret:
2632 case DW_OP_GNU_parameter_ref:
2633 gcc_unreachable ();
2634 break;
2636 default:
2637 /* Other codes have no operands. */
2638 break;
2642 void
2643 output_loc_sequence_raw (dw_loc_descr_ref loc)
2645 while (1)
2647 enum dwarf_location_atom opc = loc->dw_loc_opc;
2648 /* Output the opcode. */
2649 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2651 unsigned r = (opc - DW_OP_breg0);
2652 r = DWARF2_FRAME_REG_OUT (r, 1);
2653 gcc_assert (r <= 31);
2654 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2656 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2658 unsigned r = (opc - DW_OP_reg0);
2659 r = DWARF2_FRAME_REG_OUT (r, 1);
2660 gcc_assert (r <= 31);
2661 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2663 /* Output the opcode. */
2664 fprintf (asm_out_file, "%#x", opc);
2665 output_loc_operands_raw (loc);
2667 if (!loc->dw_loc_next)
2668 break;
2669 loc = loc->dw_loc_next;
2671 fputc (',', asm_out_file);
2675 /* This function builds a dwarf location descriptor sequence from a
2676 dw_cfa_location, adding the given OFFSET to the result of the
2677 expression. */
2679 struct dw_loc_descr_node *
2680 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2682 struct dw_loc_descr_node *head, *tmp;
2684 offset += cfa->offset;
2686 if (cfa->indirect)
2688 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2689 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2690 head->dw_loc_oprnd1.val_entry = NULL;
2691 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2692 add_loc_descr (&head, tmp);
2693 loc_descr_plus_const (&head, offset);
2695 else
2696 head = new_reg_loc_descr (cfa->reg, offset);
2698 return head;
2701 /* This function builds a dwarf location descriptor sequence for
2702 the address at OFFSET from the CFA when stack is aligned to
2703 ALIGNMENT byte. */
2705 struct dw_loc_descr_node *
2706 build_cfa_aligned_loc (dw_cfa_location *cfa,
2707 poly_int64 offset, HOST_WIDE_INT alignment)
2709 struct dw_loc_descr_node *head;
2710 unsigned int dwarf_fp
2711 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2713 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2714 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2716 head = new_reg_loc_descr (dwarf_fp, 0);
2717 add_loc_descr (&head, int_loc_descriptor (alignment));
2718 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2719 loc_descr_plus_const (&head, offset);
2721 else
2722 head = new_reg_loc_descr (dwarf_fp, offset);
2723 return head;
2726 /* And now, the support for symbolic debugging information. */
2728 /* .debug_str support. */
2730 static void dwarf2out_init (const char *);
2731 static void dwarf2out_finish (const char *);
2732 static void dwarf2out_early_finish (const char *);
2733 static void dwarf2out_assembly_start (void);
2734 static void dwarf2out_define (unsigned int, const char *);
2735 static void dwarf2out_undef (unsigned int, const char *);
2736 static void dwarf2out_start_source_file (unsigned, const char *);
2737 static void dwarf2out_end_source_file (unsigned);
2738 static void dwarf2out_function_decl (tree);
2739 static void dwarf2out_begin_block (unsigned, unsigned);
2740 static void dwarf2out_end_block (unsigned, unsigned);
2741 static bool dwarf2out_ignore_block (const_tree);
2742 static void dwarf2out_early_global_decl (tree);
2743 static void dwarf2out_late_global_decl (tree);
2744 static void dwarf2out_type_decl (tree, int);
2745 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2746 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2747 dw_die_ref);
2748 static void dwarf2out_abstract_function (tree);
2749 static void dwarf2out_var_location (rtx_insn *);
2750 static void dwarf2out_inline_entry (tree);
2751 static void dwarf2out_size_function (tree);
2752 static void dwarf2out_begin_function (tree);
2753 static void dwarf2out_end_function (unsigned int);
2754 static void dwarf2out_register_main_translation_unit (tree unit);
2755 static void dwarf2out_set_name (tree, tree);
2756 static void dwarf2out_register_external_die (tree decl, const char *sym,
2757 unsigned HOST_WIDE_INT off);
2758 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2759 unsigned HOST_WIDE_INT *off);
2761 /* The debug hooks structure. */
2763 const struct gcc_debug_hooks dwarf2_debug_hooks =
2765 dwarf2out_init,
2766 dwarf2out_finish,
2767 dwarf2out_early_finish,
2768 dwarf2out_assembly_start,
2769 dwarf2out_define,
2770 dwarf2out_undef,
2771 dwarf2out_start_source_file,
2772 dwarf2out_end_source_file,
2773 dwarf2out_begin_block,
2774 dwarf2out_end_block,
2775 dwarf2out_ignore_block,
2776 dwarf2out_source_line,
2777 dwarf2out_begin_prologue,
2778 #if VMS_DEBUGGING_INFO
2779 dwarf2out_vms_end_prologue,
2780 dwarf2out_vms_begin_epilogue,
2781 #else
2782 debug_nothing_int_charstar,
2783 debug_nothing_int_charstar,
2784 #endif
2785 dwarf2out_end_epilogue,
2786 dwarf2out_begin_function,
2787 dwarf2out_end_function, /* end_function */
2788 dwarf2out_register_main_translation_unit,
2789 dwarf2out_function_decl, /* function_decl */
2790 dwarf2out_early_global_decl,
2791 dwarf2out_late_global_decl,
2792 dwarf2out_type_decl, /* type_decl */
2793 dwarf2out_imported_module_or_decl,
2794 dwarf2out_die_ref_for_decl,
2795 dwarf2out_register_external_die,
2796 debug_nothing_tree, /* deferred_inline_function */
2797 /* The DWARF 2 backend tries to reduce debugging bloat by not
2798 emitting the abstract description of inline functions until
2799 something tries to reference them. */
2800 dwarf2out_abstract_function, /* outlining_inline_function */
2801 debug_nothing_rtx_code_label, /* label */
2802 debug_nothing_int, /* handle_pch */
2803 dwarf2out_var_location,
2804 dwarf2out_inline_entry, /* inline_entry */
2805 dwarf2out_size_function, /* size_function */
2806 dwarf2out_switch_text_section,
2807 dwarf2out_set_name,
2808 1, /* start_end_main_source_file */
2809 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2812 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2814 dwarf2out_init,
2815 debug_nothing_charstar,
2816 debug_nothing_charstar,
2817 dwarf2out_assembly_start,
2818 debug_nothing_int_charstar,
2819 debug_nothing_int_charstar,
2820 debug_nothing_int_charstar,
2821 debug_nothing_int,
2822 debug_nothing_int_int, /* begin_block */
2823 debug_nothing_int_int, /* end_block */
2824 debug_true_const_tree, /* ignore_block */
2825 dwarf2out_source_line, /* source_line */
2826 debug_nothing_int_int_charstar, /* begin_prologue */
2827 debug_nothing_int_charstar, /* end_prologue */
2828 debug_nothing_int_charstar, /* begin_epilogue */
2829 debug_nothing_int_charstar, /* end_epilogue */
2830 debug_nothing_tree, /* begin_function */
2831 debug_nothing_int, /* end_function */
2832 debug_nothing_tree, /* register_main_translation_unit */
2833 debug_nothing_tree, /* function_decl */
2834 debug_nothing_tree, /* early_global_decl */
2835 debug_nothing_tree, /* late_global_decl */
2836 debug_nothing_tree_int, /* type_decl */
2837 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2838 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2839 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2840 debug_nothing_tree, /* deferred_inline_function */
2841 debug_nothing_tree, /* outlining_inline_function */
2842 debug_nothing_rtx_code_label, /* label */
2843 debug_nothing_int, /* handle_pch */
2844 debug_nothing_rtx_insn, /* var_location */
2845 debug_nothing_tree, /* inline_entry */
2846 debug_nothing_tree, /* size_function */
2847 debug_nothing_void, /* switch_text_section */
2848 debug_nothing_tree_tree, /* set_name */
2849 0, /* start_end_main_source_file */
2850 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2853 /* NOTE: In the comments in this file, many references are made to
2854 "Debugging Information Entries". This term is abbreviated as `DIE'
2855 throughout the remainder of this file. */
2857 /* An internal representation of the DWARF output is built, and then
2858 walked to generate the DWARF debugging info. The walk of the internal
2859 representation is done after the entire program has been compiled.
2860 The types below are used to describe the internal representation. */
2862 /* Whether to put type DIEs into their own section .debug_types instead
2863 of making them part of the .debug_info section. Only supported for
2864 Dwarf V4 or higher and the user didn't disable them through
2865 -fno-debug-types-section. It is more efficient to put them in a
2866 separate comdat sections since the linker will then be able to
2867 remove duplicates. But not all tools support .debug_types sections
2868 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2869 it is DW_UT_type unit type in .debug_info section. */
2871 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2873 /* Various DIE's use offsets relative to the beginning of the
2874 .debug_info section to refer to each other. */
2876 typedef long int dw_offset;
2878 struct comdat_type_node;
2880 /* The entries in the line_info table more-or-less mirror the opcodes
2881 that are used in the real dwarf line table. Arrays of these entries
2882 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2883 supported. */
2885 enum dw_line_info_opcode {
2886 /* Emit DW_LNE_set_address; the operand is the label index. */
2887 LI_set_address,
2889 /* Emit a row to the matrix with the given line. This may be done
2890 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2891 special opcodes. */
2892 LI_set_line,
2894 /* Emit a DW_LNS_set_file. */
2895 LI_set_file,
2897 /* Emit a DW_LNS_set_column. */
2898 LI_set_column,
2900 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2901 LI_negate_stmt,
2903 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2904 LI_set_prologue_end,
2905 LI_set_epilogue_begin,
2907 /* Emit a DW_LNE_set_discriminator. */
2908 LI_set_discriminator,
2910 /* Output a Fixed Advance PC; the target PC is the label index; the
2911 base PC is the previous LI_adv_address or LI_set_address entry.
2912 We only use this when emitting debug views without assembler
2913 support, at explicit user request. Ideally, we should only use
2914 it when the offset might be zero but we can't tell: it's the only
2915 way to maybe change the PC without resetting the view number. */
2916 LI_adv_address
2919 typedef struct GTY(()) dw_line_info_struct {
2920 enum dw_line_info_opcode opcode;
2921 unsigned int val;
2922 } dw_line_info_entry;
2925 struct GTY(()) dw_line_info_table {
2926 /* The label that marks the end of this section. */
2927 const char *end_label;
2929 /* The values for the last row of the matrix, as collected in the table.
2930 These are used to minimize the changes to the next row. */
2931 unsigned int file_num;
2932 unsigned int line_num;
2933 unsigned int column_num;
2934 int discrim_num;
2935 bool is_stmt;
2936 bool in_use;
2938 /* This denotes the NEXT view number.
2940 If it is 0, it is known that the NEXT view will be the first view
2941 at the given PC.
2943 If it is -1, we've advanced PC but we haven't emitted a line location yet,
2944 so we shouldn't use this view number.
2946 The meaning of other nonzero values depends on whether we're
2947 computing views internally or leaving it for the assembler to do
2948 so. If we're emitting them internally, view denotes the view
2949 number since the last known advance of PC. If we're leaving it
2950 for the assembler, it denotes the LVU label number that we're
2951 going to ask the assembler to assign. */
2952 var_loc_view view;
2954 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
2955 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0)
2957 vec<dw_line_info_entry, va_gc> *entries;
2960 /* If we're keep track of location views and their reset points, and
2961 INSN is a reset point (i.e., it necessarily advances the PC), mark
2962 the next view in TABLE as reset. */
2964 static void
2965 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
2967 if (!debug_internal_reset_location_views)
2968 return;
2970 /* Maybe turn (part of?) this test into a default target hook. */
2971 int reset = 0;
2973 if (targetm.reset_location_view)
2974 reset = targetm.reset_location_view (insn);
2976 if (reset)
2978 else if (JUMP_TABLE_DATA_P (insn))
2979 reset = 1;
2980 else if (GET_CODE (insn) == USE
2981 || GET_CODE (insn) == CLOBBER
2982 || GET_CODE (insn) == ASM_INPUT
2983 || asm_noperands (insn) >= 0)
2985 else if (get_attr_min_length (insn) > 0)
2986 reset = 1;
2988 if (reset > 0)
2989 RESET_NEXT_VIEW (table->view);
2992 /* Each DIE attribute has a field specifying the attribute kind,
2993 a link to the next attribute in the chain, and an attribute value.
2994 Attributes are typically linked below the DIE they modify. */
2996 typedef struct GTY(()) dw_attr_struct {
2997 enum dwarf_attribute dw_attr;
2998 dw_val_node dw_attr_val;
3000 dw_attr_node;
3003 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3004 The children of each node form a circular list linked by
3005 die_sib. die_child points to the node *before* the "first" child node. */
3007 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3008 union die_symbol_or_type_node
3010 const char * GTY ((tag ("0"))) die_symbol;
3011 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3013 GTY ((desc ("%0.comdat_type_p"))) die_id;
3014 vec<dw_attr_node, va_gc> *die_attr;
3015 dw_die_ref die_parent;
3016 dw_die_ref die_child;
3017 dw_die_ref die_sib;
3018 dw_die_ref die_definition; /* ref from a specification to its definition */
3019 dw_offset die_offset;
3020 unsigned long die_abbrev;
3021 int die_mark;
3022 unsigned int decl_id;
3023 enum dwarf_tag die_tag;
3024 /* Die is used and must not be pruned as unused. */
3025 BOOL_BITFIELD die_perennial_p : 1;
3026 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3027 /* For an external ref to die_symbol if die_offset contains an extra
3028 offset to that symbol. */
3029 BOOL_BITFIELD with_offset : 1;
3030 /* Whether this DIE was removed from the DIE tree, for example via
3031 prune_unused_types. We don't consider those present from the
3032 DIE lookup routines. */
3033 BOOL_BITFIELD removed : 1;
3034 /* Lots of spare bits. */
3036 die_node;
3038 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3039 static bool early_dwarf;
3040 static bool early_dwarf_finished;
3041 struct set_early_dwarf {
3042 bool saved;
3043 set_early_dwarf () : saved(early_dwarf)
3045 gcc_assert (! early_dwarf_finished);
3046 early_dwarf = true;
3048 ~set_early_dwarf () { early_dwarf = saved; }
3051 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3052 #define FOR_EACH_CHILD(die, c, expr) do { \
3053 c = die->die_child; \
3054 if (c) do { \
3055 c = c->die_sib; \
3056 expr; \
3057 } while (c != die->die_child); \
3058 } while (0)
3060 /* The pubname structure */
3062 typedef struct GTY(()) pubname_struct {
3063 dw_die_ref die;
3064 const char *name;
3066 pubname_entry;
3069 struct GTY(()) dw_ranges {
3070 const char *label;
3071 /* If this is positive, it's a block number, otherwise it's a
3072 bitwise-negated index into dw_ranges_by_label. */
3073 int num;
3074 /* Index for the range list for DW_FORM_rnglistx. */
3075 unsigned int idx : 31;
3076 /* True if this range might be possibly in a different section
3077 from previous entry. */
3078 unsigned int maybe_new_sec : 1;
3081 /* A structure to hold a macinfo entry. */
3083 typedef struct GTY(()) macinfo_struct {
3084 unsigned char code;
3085 unsigned HOST_WIDE_INT lineno;
3086 const char *info;
3088 macinfo_entry;
3091 struct GTY(()) dw_ranges_by_label {
3092 const char *begin;
3093 const char *end;
3096 /* The comdat type node structure. */
3097 struct GTY(()) comdat_type_node
3099 dw_die_ref root_die;
3100 dw_die_ref type_die;
3101 dw_die_ref skeleton_die;
3102 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3103 comdat_type_node *next;
3106 /* A list of DIEs for which we can't determine ancestry (parent_die
3107 field) just yet. Later in dwarf2out_finish we will fill in the
3108 missing bits. */
3109 typedef struct GTY(()) limbo_die_struct {
3110 dw_die_ref die;
3111 /* The tree for which this DIE was created. We use this to
3112 determine ancestry later. */
3113 tree created_for;
3114 struct limbo_die_struct *next;
3116 limbo_die_node;
3118 typedef struct skeleton_chain_struct
3120 dw_die_ref old_die;
3121 dw_die_ref new_die;
3122 struct skeleton_chain_struct *parent;
3124 skeleton_chain_node;
3126 /* Define a macro which returns nonzero for a TYPE_DECL which was
3127 implicitly generated for a type.
3129 Note that, unlike the C front-end (which generates a NULL named
3130 TYPE_DECL node for each complete tagged type, each array type,
3131 and each function type node created) the C++ front-end generates
3132 a _named_ TYPE_DECL node for each tagged type node created.
3133 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3134 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3135 front-end, but for each type, tagged or not. */
3137 #define TYPE_DECL_IS_STUB(decl) \
3138 (DECL_NAME (decl) == NULL_TREE \
3139 || (DECL_ARTIFICIAL (decl) \
3140 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3141 /* This is necessary for stub decls that \
3142 appear in nested inline functions. */ \
3143 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3144 && (decl_ultimate_origin (decl) \
3145 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3147 /* Information concerning the compilation unit's programming
3148 language, and compiler version. */
3150 /* Fixed size portion of the DWARF compilation unit header. */
3151 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3152 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3153 + (dwarf_version >= 5 ? 4 : 3))
3155 /* Fixed size portion of the DWARF comdat type unit header. */
3156 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3157 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3158 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3160 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3161 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3162 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3164 /* Fixed size portion of public names info. */
3165 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3167 /* Fixed size portion of the address range info. */
3168 #define DWARF_ARANGES_HEADER_SIZE \
3169 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3170 DWARF2_ADDR_SIZE * 2) \
3171 - DWARF_INITIAL_LENGTH_SIZE)
3173 /* Size of padding portion in the address range info. It must be
3174 aligned to twice the pointer size. */
3175 #define DWARF_ARANGES_PAD_SIZE \
3176 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3177 DWARF2_ADDR_SIZE * 2) \
3178 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3180 /* Use assembler line directives if available. */
3181 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3182 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3183 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3184 #else
3185 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3186 #endif
3187 #endif
3189 /* Use assembler views in line directives if available. */
3190 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3191 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3192 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3193 #else
3194 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3195 #endif
3196 #endif
3198 /* Return true if GCC configure detected assembler support for .loc. */
3200 bool
3201 dwarf2out_default_as_loc_support (void)
3203 return DWARF2_ASM_LINE_DEBUG_INFO;
3204 #if (GCC_VERSION >= 3000)
3205 # undef DWARF2_ASM_LINE_DEBUG_INFO
3206 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3207 #endif
3210 /* Return true if GCC configure detected assembler support for views
3211 in .loc directives. */
3213 bool
3214 dwarf2out_default_as_locview_support (void)
3216 return DWARF2_ASM_VIEW_DEBUG_INFO;
3217 #if (GCC_VERSION >= 3000)
3218 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3219 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3220 #endif
3223 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3224 view computation, and it refers to a view identifier for which we
3225 will not emit a label because it is known to map to a view number
3226 zero. We won't allocate the bitmap if we're not using assembler
3227 support for location views, but we have to make the variable
3228 visible for GGC and for code that will be optimized out for lack of
3229 support but that's still parsed and compiled. We could abstract it
3230 out with macros, but it's not worth it. */
3231 static GTY(()) bitmap zero_view_p;
3233 /* Evaluate to TRUE iff N is known to identify the first location view
3234 at its PC. When not using assembler location view computation,
3235 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3236 and views label numbers recorded in it are the ones known to be
3237 zero. */
3238 #define ZERO_VIEW_P(N) (zero_view_p \
3239 ? bitmap_bit_p (zero_view_p, (N)) \
3240 : (N) == 0)
3242 /* Return true iff we're to emit .loc directives for the assembler to
3243 generate line number sections.
3245 When we're not emitting views, all we need from the assembler is
3246 support for .loc directives.
3248 If we are emitting views, we can only use the assembler's .loc
3249 support if it also supports views.
3251 When the compiler is emitting the line number programs and
3252 computing view numbers itself, it resets view numbers at known PC
3253 changes and counts from that, and then it emits view numbers as
3254 literal constants in locviewlists. There are cases in which the
3255 compiler is not sure about PC changes, e.g. when extra alignment is
3256 requested for a label. In these cases, the compiler may not reset
3257 the view counter, and the potential PC advance in the line number
3258 program will use an opcode that does not reset the view counter
3259 even if the PC actually changes, so that compiler and debug info
3260 consumer can keep view numbers in sync.
3262 When the compiler defers view computation to the assembler, it
3263 emits symbolic view numbers in locviewlists, with the exception of
3264 views known to be zero (forced resets, or reset after
3265 compiler-visible PC changes): instead of emitting symbols for
3266 these, we emit literal zero and assert the assembler agrees with
3267 the compiler's assessment. We could use symbolic views everywhere,
3268 instead of special-casing zero views, but then we'd be unable to
3269 optimize out locviewlists that contain only zeros. */
3271 static bool
3272 output_asm_line_debug_info (void)
3274 return (dwarf2out_as_loc_support
3275 && (dwarf2out_as_locview_support
3276 || !debug_variable_location_views));
3279 /* Minimum line offset in a special line info. opcode.
3280 This value was chosen to give a reasonable range of values. */
3281 #define DWARF_LINE_BASE -10
3283 /* First special line opcode - leave room for the standard opcodes. */
3284 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3286 /* Range of line offsets in a special line info. opcode. */
3287 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3289 /* Flag that indicates the initial value of the is_stmt_start flag.
3290 In the present implementation, we do not mark any lines as
3291 the beginning of a source statement, because that information
3292 is not made available by the GCC front-end. */
3293 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3295 /* Maximum number of operations per instruction bundle. */
3296 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3297 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3298 #endif
3300 /* This location is used by calc_die_sizes() to keep track
3301 the offset of each DIE within the .debug_info section. */
3302 static unsigned long next_die_offset;
3304 /* Record the root of the DIE's built for the current compilation unit. */
3305 static GTY(()) dw_die_ref single_comp_unit_die;
3307 /* A list of type DIEs that have been separated into comdat sections. */
3308 static GTY(()) comdat_type_node *comdat_type_list;
3310 /* A list of CU DIEs that have been separated. */
3311 static GTY(()) limbo_die_node *cu_die_list;
3313 /* A list of DIEs with a NULL parent waiting to be relocated. */
3314 static GTY(()) limbo_die_node *limbo_die_list;
3316 /* A list of DIEs for which we may have to generate
3317 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3318 static GTY(()) limbo_die_node *deferred_asm_name;
3320 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3322 typedef const char *compare_type;
3324 static hashval_t hash (dwarf_file_data *);
3325 static bool equal (dwarf_file_data *, const char *);
3328 /* Filenames referenced by this compilation unit. */
3329 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3331 struct decl_die_hasher : ggc_ptr_hash<die_node>
3333 typedef tree compare_type;
3335 static hashval_t hash (die_node *);
3336 static bool equal (die_node *, tree);
3338 /* A hash table of references to DIE's that describe declarations.
3339 The key is a DECL_UID() which is a unique number identifying each decl. */
3340 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3342 struct GTY ((for_user)) variable_value_struct {
3343 unsigned int decl_id;
3344 vec<dw_die_ref, va_gc> *dies;
3347 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3349 typedef tree compare_type;
3351 static hashval_t hash (variable_value_struct *);
3352 static bool equal (variable_value_struct *, tree);
3354 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3355 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3356 DECL_CONTEXT of the referenced VAR_DECLs. */
3357 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3359 struct block_die_hasher : ggc_ptr_hash<die_struct>
3361 static hashval_t hash (die_struct *);
3362 static bool equal (die_struct *, die_struct *);
3365 /* A hash table of references to DIE's that describe COMMON blocks.
3366 The key is DECL_UID() ^ die_parent. */
3367 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3369 typedef struct GTY(()) die_arg_entry_struct {
3370 dw_die_ref die;
3371 tree arg;
3372 } die_arg_entry;
3375 /* Node of the variable location list. */
3376 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3377 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3378 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3379 in mode of the EXPR_LIST node and first EXPR_LIST operand
3380 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3381 location or NULL for padding. For larger bitsizes,
3382 mode is 0 and first operand is a CONCAT with bitsize
3383 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3384 NULL as second operand. */
3385 rtx GTY (()) loc;
3386 const char * GTY (()) label;
3387 struct var_loc_node * GTY (()) next;
3388 var_loc_view view;
3391 /* Variable location list. */
3392 struct GTY ((for_user)) var_loc_list_def {
3393 struct var_loc_node * GTY (()) first;
3395 /* Pointer to the last but one or last element of the
3396 chained list. If the list is empty, both first and
3397 last are NULL, if the list contains just one node
3398 or the last node certainly is not redundant, it points
3399 to the last node, otherwise points to the last but one.
3400 Do not mark it for GC because it is marked through the chain. */
3401 struct var_loc_node * GTY ((skip ("%h"))) last;
3403 /* Pointer to the last element before section switch,
3404 if NULL, either sections weren't switched or first
3405 is after section switch. */
3406 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3408 /* DECL_UID of the variable decl. */
3409 unsigned int decl_id;
3411 typedef struct var_loc_list_def var_loc_list;
3413 /* Call argument location list. */
3414 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3415 rtx GTY (()) call_arg_loc_note;
3416 const char * GTY (()) label;
3417 tree GTY (()) block;
3418 bool tail_call_p;
3419 rtx GTY (()) symbol_ref;
3420 struct call_arg_loc_node * GTY (()) next;
3424 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3426 typedef const_tree compare_type;
3428 static hashval_t hash (var_loc_list *);
3429 static bool equal (var_loc_list *, const_tree);
3432 /* Table of decl location linked lists. */
3433 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3435 /* Head and tail of call_arg_loc chain. */
3436 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3437 static struct call_arg_loc_node *call_arg_loc_last;
3439 /* Number of call sites in the current function. */
3440 static int call_site_count = -1;
3441 /* Number of tail call sites in the current function. */
3442 static int tail_call_site_count = -1;
3444 /* A cached location list. */
3445 struct GTY ((for_user)) cached_dw_loc_list_def {
3446 /* The DECL_UID of the decl that this entry describes. */
3447 unsigned int decl_id;
3449 /* The cached location list. */
3450 dw_loc_list_ref loc_list;
3452 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3454 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3457 typedef const_tree compare_type;
3459 static hashval_t hash (cached_dw_loc_list *);
3460 static bool equal (cached_dw_loc_list *, const_tree);
3463 /* Table of cached location lists. */
3464 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3466 /* A vector of references to DIE's that are uniquely identified by their tag,
3467 presence/absence of children DIE's, and list of attribute/value pairs. */
3468 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3470 /* A hash map to remember the stack usage for DWARF procedures. The value
3471 stored is the stack size difference between before the DWARF procedure
3472 invokation and after it returned. In other words, for a DWARF procedure
3473 that consumes N stack slots and that pushes M ones, this stores M - N. */
3474 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3476 /* A global counter for generating labels for line number data. */
3477 static unsigned int line_info_label_num;
3479 /* The current table to which we should emit line number information
3480 for the current function. This will be set up at the beginning of
3481 assembly for the function. */
3482 static GTY(()) dw_line_info_table *cur_line_info_table;
3484 /* The two default tables of line number info. */
3485 static GTY(()) dw_line_info_table *text_section_line_info;
3486 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3488 /* The set of all non-default tables of line number info. */
3489 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3491 /* A flag to tell pubnames/types export if there is an info section to
3492 refer to. */
3493 static bool info_section_emitted;
3495 /* A pointer to the base of a table that contains a list of publicly
3496 accessible names. */
3497 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3499 /* A pointer to the base of a table that contains a list of publicly
3500 accessible types. */
3501 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3503 /* A pointer to the base of a table that contains a list of macro
3504 defines/undefines (and file start/end markers). */
3505 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3507 /* True if .debug_macinfo or .debug_macros section is going to be
3508 emitted. */
3509 #define have_macinfo \
3510 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3511 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3512 && !macinfo_table->is_empty ())
3514 /* Vector of dies for which we should generate .debug_ranges info. */
3515 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3517 /* Vector of pairs of labels referenced in ranges_table. */
3518 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3520 /* Whether we have location lists that need outputting */
3521 static GTY(()) bool have_location_lists;
3523 /* Unique label counter. */
3524 static GTY(()) unsigned int loclabel_num;
3526 /* Unique label counter for point-of-call tables. */
3527 static GTY(()) unsigned int poc_label_num;
3529 /* The last file entry emitted by maybe_emit_file(). */
3530 static GTY(()) struct dwarf_file_data * last_emitted_file;
3532 /* Number of internal labels generated by gen_internal_sym(). */
3533 static GTY(()) int label_num;
3535 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3537 /* Instances of generic types for which we need to generate debug
3538 info that describe their generic parameters and arguments. That
3539 generation needs to happen once all types are properly laid out so
3540 we do it at the end of compilation. */
3541 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3543 /* Offset from the "steady-state frame pointer" to the frame base,
3544 within the current function. */
3545 static poly_int64 frame_pointer_fb_offset;
3546 static bool frame_pointer_fb_offset_valid;
3548 static vec<dw_die_ref> base_types;
3550 /* Flags to represent a set of attribute classes for attributes that represent
3551 a scalar value (bounds, pointers, ...). */
3552 enum dw_scalar_form
3554 dw_scalar_form_constant = 0x01,
3555 dw_scalar_form_exprloc = 0x02,
3556 dw_scalar_form_reference = 0x04
3559 /* Forward declarations for functions defined in this file. */
3561 static int is_pseudo_reg (const_rtx);
3562 static tree type_main_variant (tree);
3563 static int is_tagged_type (const_tree);
3564 static const char *dwarf_tag_name (unsigned);
3565 static const char *dwarf_attr_name (unsigned);
3566 static const char *dwarf_form_name (unsigned);
3567 static tree decl_ultimate_origin (const_tree);
3568 static tree decl_class_context (tree);
3569 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3570 static inline enum dw_val_class AT_class (dw_attr_node *);
3571 static inline unsigned int AT_index (dw_attr_node *);
3572 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3573 static inline unsigned AT_flag (dw_attr_node *);
3574 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3575 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3576 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3577 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3578 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3579 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3580 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3581 unsigned int, unsigned char *);
3582 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3583 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3584 static inline const char *AT_string (dw_attr_node *);
3585 static enum dwarf_form AT_string_form (dw_attr_node *);
3586 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3587 static void add_AT_specification (dw_die_ref, dw_die_ref);
3588 static inline dw_die_ref AT_ref (dw_attr_node *);
3589 static inline int AT_ref_external (dw_attr_node *);
3590 static inline void set_AT_ref_external (dw_attr_node *, int);
3591 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3592 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3593 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3594 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3595 dw_loc_list_ref);
3596 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3597 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3598 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3599 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3600 static void remove_addr_table_entry (addr_table_entry *);
3601 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3602 static inline rtx AT_addr (dw_attr_node *);
3603 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3604 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3605 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3606 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3607 const char *);
3608 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3609 unsigned HOST_WIDE_INT);
3610 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3611 unsigned long, bool);
3612 static inline const char *AT_lbl (dw_attr_node *);
3613 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3614 static const char *get_AT_low_pc (dw_die_ref);
3615 static const char *get_AT_hi_pc (dw_die_ref);
3616 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3617 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3618 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3619 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3620 static bool is_cxx (void);
3621 static bool is_cxx (const_tree);
3622 static bool is_fortran (void);
3623 static bool is_ada (void);
3624 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3625 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3626 static void add_child_die (dw_die_ref, dw_die_ref);
3627 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3628 static dw_die_ref lookup_type_die (tree);
3629 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3630 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3631 static void equate_type_number_to_die (tree, dw_die_ref);
3632 static dw_die_ref lookup_decl_die (tree);
3633 static var_loc_list *lookup_decl_loc (const_tree);
3634 static void equate_decl_number_to_die (tree, dw_die_ref);
3635 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3636 static void print_spaces (FILE *);
3637 static void print_die (dw_die_ref, FILE *);
3638 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3639 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3640 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3641 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3642 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3643 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3644 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3645 struct md5_ctx *, int *);
3646 struct checksum_attributes;
3647 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3648 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3649 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3650 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3651 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3652 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3653 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3654 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3655 static int is_type_die (dw_die_ref);
3656 static int is_comdat_die (dw_die_ref);
3657 static inline bool is_template_instantiation (dw_die_ref);
3658 static int is_declaration_die (dw_die_ref);
3659 static int should_move_die_to_comdat (dw_die_ref);
3660 static dw_die_ref clone_as_declaration (dw_die_ref);
3661 static dw_die_ref clone_die (dw_die_ref);
3662 static dw_die_ref clone_tree (dw_die_ref);
3663 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3664 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3665 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3666 static dw_die_ref generate_skeleton (dw_die_ref);
3667 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3668 dw_die_ref,
3669 dw_die_ref);
3670 static void break_out_comdat_types (dw_die_ref);
3671 static void copy_decls_for_unworthy_types (dw_die_ref);
3673 static void add_sibling_attributes (dw_die_ref);
3674 static void output_location_lists (dw_die_ref);
3675 static int constant_size (unsigned HOST_WIDE_INT);
3676 static unsigned long size_of_die (dw_die_ref);
3677 static void calc_die_sizes (dw_die_ref);
3678 static void calc_base_type_die_sizes (void);
3679 static void mark_dies (dw_die_ref);
3680 static void unmark_dies (dw_die_ref);
3681 static void unmark_all_dies (dw_die_ref);
3682 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3683 static unsigned long size_of_aranges (void);
3684 static enum dwarf_form value_format (dw_attr_node *);
3685 static void output_value_format (dw_attr_node *);
3686 static void output_abbrev_section (void);
3687 static void output_die_abbrevs (unsigned long, dw_die_ref);
3688 static void output_die (dw_die_ref);
3689 static void output_compilation_unit_header (enum dwarf_unit_type);
3690 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3691 static void output_comdat_type_unit (comdat_type_node *);
3692 static const char *dwarf2_name (tree, int);
3693 static void add_pubname (tree, dw_die_ref);
3694 static void add_enumerator_pubname (const char *, dw_die_ref);
3695 static void add_pubname_string (const char *, dw_die_ref);
3696 static void add_pubtype (tree, dw_die_ref);
3697 static void output_pubnames (vec<pubname_entry, va_gc> *);
3698 static void output_aranges (void);
3699 static unsigned int add_ranges (const_tree, bool = false);
3700 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3701 bool *, bool);
3702 static void output_ranges (void);
3703 static dw_line_info_table *new_line_info_table (void);
3704 static void output_line_info (bool);
3705 static void output_file_names (void);
3706 static dw_die_ref base_type_die (tree, bool);
3707 static int is_base_type (tree);
3708 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3709 static int decl_quals (const_tree);
3710 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3711 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3712 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3713 static int type_is_enum (const_tree);
3714 static unsigned int dbx_reg_number (const_rtx);
3715 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3716 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3717 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3718 enum var_init_status);
3719 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3720 enum var_init_status);
3721 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3722 enum var_init_status);
3723 static int is_based_loc (const_rtx);
3724 static bool resolve_one_addr (rtx *);
3725 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3726 enum var_init_status);
3727 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3728 enum var_init_status);
3729 struct loc_descr_context;
3730 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3731 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3732 static dw_loc_list_ref loc_list_from_tree (tree, int,
3733 struct loc_descr_context *);
3734 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3735 struct loc_descr_context *);
3736 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3737 static tree field_type (const_tree);
3738 static unsigned int simple_type_align_in_bits (const_tree);
3739 static unsigned int simple_decl_align_in_bits (const_tree);
3740 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3741 struct vlr_context;
3742 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3743 HOST_WIDE_INT *);
3744 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3745 dw_loc_list_ref);
3746 static void add_data_member_location_attribute (dw_die_ref, tree,
3747 struct vlr_context *);
3748 static bool add_const_value_attribute (dw_die_ref, rtx);
3749 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3750 static void insert_wide_int (const wide_int &, unsigned char *, int);
3751 static void insert_float (const_rtx, unsigned char *);
3752 static rtx rtl_for_decl_location (tree);
3753 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3754 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3755 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3756 static void add_name_attribute (dw_die_ref, const char *);
3757 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3758 static void add_comp_dir_attribute (dw_die_ref);
3759 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3760 struct loc_descr_context *);
3761 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3762 struct loc_descr_context *);
3763 static void add_subscript_info (dw_die_ref, tree, bool);
3764 static void add_byte_size_attribute (dw_die_ref, tree);
3765 static void add_alignment_attribute (dw_die_ref, tree);
3766 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3767 struct vlr_context *);
3768 static void add_bit_size_attribute (dw_die_ref, tree);
3769 static void add_prototyped_attribute (dw_die_ref, tree);
3770 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3771 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3772 static void add_src_coords_attributes (dw_die_ref, tree);
3773 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3774 static void add_discr_value (dw_die_ref, dw_discr_value *);
3775 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3776 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3777 static void push_decl_scope (tree);
3778 static void pop_decl_scope (void);
3779 static dw_die_ref scope_die_for (tree, dw_die_ref);
3780 static inline int local_scope_p (dw_die_ref);
3781 static inline int class_scope_p (dw_die_ref);
3782 static inline int class_or_namespace_scope_p (dw_die_ref);
3783 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3784 static void add_calling_convention_attribute (dw_die_ref, tree);
3785 static const char *type_tag (const_tree);
3786 static tree member_declared_type (const_tree);
3787 #if 0
3788 static const char *decl_start_label (tree);
3789 #endif
3790 static void gen_array_type_die (tree, dw_die_ref);
3791 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3792 #if 0
3793 static void gen_entry_point_die (tree, dw_die_ref);
3794 #endif
3795 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3796 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3797 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3798 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3799 static void gen_formal_types_die (tree, dw_die_ref);
3800 static void gen_subprogram_die (tree, dw_die_ref);
3801 static void gen_variable_die (tree, tree, dw_die_ref);
3802 static void gen_const_die (tree, dw_die_ref);
3803 static void gen_label_die (tree, dw_die_ref);
3804 static void gen_lexical_block_die (tree, dw_die_ref);
3805 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3806 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3807 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3808 static dw_die_ref gen_compile_unit_die (const char *);
3809 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3810 static void gen_member_die (tree, dw_die_ref);
3811 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3812 enum debug_info_usage);
3813 static void gen_subroutine_type_die (tree, dw_die_ref);
3814 static void gen_typedef_die (tree, dw_die_ref);
3815 static void gen_type_die (tree, dw_die_ref);
3816 static void gen_block_die (tree, dw_die_ref);
3817 static void decls_for_scope (tree, dw_die_ref);
3818 static bool is_naming_typedef_decl (const_tree);
3819 static inline dw_die_ref get_context_die (tree);
3820 static void gen_namespace_die (tree, dw_die_ref);
3821 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3822 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3823 static dw_die_ref force_decl_die (tree);
3824 static dw_die_ref force_type_die (tree);
3825 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3826 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3827 static struct dwarf_file_data * lookup_filename (const char *);
3828 static void retry_incomplete_types (void);
3829 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3830 static void gen_generic_params_dies (tree);
3831 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3832 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3833 static void splice_child_die (dw_die_ref, dw_die_ref);
3834 static int file_info_cmp (const void *, const void *);
3835 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3836 const char *, var_loc_view, const char *);
3837 static void output_loc_list (dw_loc_list_ref);
3838 static char *gen_internal_sym (const char *);
3839 static bool want_pubnames (void);
3841 static void prune_unmark_dies (dw_die_ref);
3842 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3843 static void prune_unused_types_mark (dw_die_ref, int);
3844 static void prune_unused_types_walk (dw_die_ref);
3845 static void prune_unused_types_walk_attribs (dw_die_ref);
3846 static void prune_unused_types_prune (dw_die_ref);
3847 static void prune_unused_types (void);
3848 static int maybe_emit_file (struct dwarf_file_data *fd);
3849 static inline const char *AT_vms_delta1 (dw_attr_node *);
3850 static inline const char *AT_vms_delta2 (dw_attr_node *);
3851 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3852 const char *, const char *);
3853 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3854 static void gen_remaining_tmpl_value_param_die_attribute (void);
3855 static bool generic_type_p (tree);
3856 static void schedule_generic_params_dies_gen (tree t);
3857 static void gen_scheduled_generic_parms_dies (void);
3858 static void resolve_variable_values (void);
3860 static const char *comp_dir_string (void);
3862 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3864 /* enum for tracking thread-local variables whose address is really an offset
3865 relative to the TLS pointer, which will need link-time relocation, but will
3866 not need relocation by the DWARF consumer. */
3868 enum dtprel_bool
3870 dtprel_false = 0,
3871 dtprel_true = 1
3874 /* Return the operator to use for an address of a variable. For dtprel_true, we
3875 use DW_OP_const*. For regular variables, which need both link-time
3876 relocation and consumer-level relocation (e.g., to account for shared objects
3877 loaded at a random address), we use DW_OP_addr*. */
3879 static inline enum dwarf_location_atom
3880 dw_addr_op (enum dtprel_bool dtprel)
3882 if (dtprel == dtprel_true)
3883 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3884 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3885 else
3886 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3889 /* Return a pointer to a newly allocated address location description. If
3890 dwarf_split_debug_info is true, then record the address with the appropriate
3891 relocation. */
3892 static inline dw_loc_descr_ref
3893 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3895 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3897 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3898 ref->dw_loc_oprnd1.v.val_addr = addr;
3899 ref->dtprel = dtprel;
3900 if (dwarf_split_debug_info)
3901 ref->dw_loc_oprnd1.val_entry
3902 = add_addr_table_entry (addr,
3903 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3904 else
3905 ref->dw_loc_oprnd1.val_entry = NULL;
3907 return ref;
3910 /* Section names used to hold DWARF debugging information. */
3912 #ifndef DEBUG_INFO_SECTION
3913 #define DEBUG_INFO_SECTION ".debug_info"
3914 #endif
3915 #ifndef DEBUG_DWO_INFO_SECTION
3916 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3917 #endif
3918 #ifndef DEBUG_LTO_INFO_SECTION
3919 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3920 #endif
3921 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3922 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3923 #endif
3924 #ifndef DEBUG_ABBREV_SECTION
3925 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3926 #endif
3927 #ifndef DEBUG_LTO_ABBREV_SECTION
3928 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3929 #endif
3930 #ifndef DEBUG_DWO_ABBREV_SECTION
3931 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3932 #endif
3933 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3934 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3935 #endif
3936 #ifndef DEBUG_ARANGES_SECTION
3937 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3938 #endif
3939 #ifndef DEBUG_ADDR_SECTION
3940 #define DEBUG_ADDR_SECTION ".debug_addr"
3941 #endif
3942 #ifndef DEBUG_MACINFO_SECTION
3943 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3944 #endif
3945 #ifndef DEBUG_LTO_MACINFO_SECTION
3946 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3947 #endif
3948 #ifndef DEBUG_DWO_MACINFO_SECTION
3949 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3950 #endif
3951 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3952 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3953 #endif
3954 #ifndef DEBUG_MACRO_SECTION
3955 #define DEBUG_MACRO_SECTION ".debug_macro"
3956 #endif
3957 #ifndef DEBUG_LTO_MACRO_SECTION
3958 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3959 #endif
3960 #ifndef DEBUG_DWO_MACRO_SECTION
3961 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3962 #endif
3963 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3964 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3965 #endif
3966 #ifndef DEBUG_LINE_SECTION
3967 #define DEBUG_LINE_SECTION ".debug_line"
3968 #endif
3969 #ifndef DEBUG_LTO_LINE_SECTION
3970 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3971 #endif
3972 #ifndef DEBUG_DWO_LINE_SECTION
3973 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3974 #endif
3975 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3976 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3977 #endif
3978 #ifndef DEBUG_LOC_SECTION
3979 #define DEBUG_LOC_SECTION ".debug_loc"
3980 #endif
3981 #ifndef DEBUG_DWO_LOC_SECTION
3982 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3983 #endif
3984 #ifndef DEBUG_LOCLISTS_SECTION
3985 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3986 #endif
3987 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3988 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3989 #endif
3990 #ifndef DEBUG_PUBNAMES_SECTION
3991 #define DEBUG_PUBNAMES_SECTION \
3992 ((debug_generate_pub_sections == 2) \
3993 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3994 #endif
3995 #ifndef DEBUG_PUBTYPES_SECTION
3996 #define DEBUG_PUBTYPES_SECTION \
3997 ((debug_generate_pub_sections == 2) \
3998 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3999 #endif
4000 #ifndef DEBUG_STR_OFFSETS_SECTION
4001 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4002 #endif
4003 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4004 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4005 #endif
4006 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4007 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4008 #endif
4009 #ifndef DEBUG_STR_SECTION
4010 #define DEBUG_STR_SECTION ".debug_str"
4011 #endif
4012 #ifndef DEBUG_LTO_STR_SECTION
4013 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4014 #endif
4015 #ifndef DEBUG_STR_DWO_SECTION
4016 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4017 #endif
4018 #ifndef DEBUG_LTO_STR_DWO_SECTION
4019 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4020 #endif
4021 #ifndef DEBUG_RANGES_SECTION
4022 #define DEBUG_RANGES_SECTION ".debug_ranges"
4023 #endif
4024 #ifndef DEBUG_RNGLISTS_SECTION
4025 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4026 #endif
4027 #ifndef DEBUG_LINE_STR_SECTION
4028 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4029 #endif
4030 #ifndef DEBUG_LTO_LINE_STR_SECTION
4031 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4032 #endif
4034 /* Standard ELF section names for compiled code and data. */
4035 #ifndef TEXT_SECTION_NAME
4036 #define TEXT_SECTION_NAME ".text"
4037 #endif
4039 /* Section flags for .debug_str section. */
4040 #define DEBUG_STR_SECTION_FLAGS \
4041 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4042 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4043 : SECTION_DEBUG)
4045 /* Section flags for .debug_str.dwo section. */
4046 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4048 /* Attribute used to refer to the macro section. */
4049 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4050 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4052 /* Labels we insert at beginning sections we can reference instead of
4053 the section names themselves. */
4055 #ifndef TEXT_SECTION_LABEL
4056 #define TEXT_SECTION_LABEL "Ltext"
4057 #endif
4058 #ifndef COLD_TEXT_SECTION_LABEL
4059 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4060 #endif
4061 #ifndef DEBUG_LINE_SECTION_LABEL
4062 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4063 #endif
4064 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4065 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4066 #endif
4067 #ifndef DEBUG_INFO_SECTION_LABEL
4068 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4069 #endif
4070 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4071 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4072 #endif
4073 #ifndef DEBUG_ABBREV_SECTION_LABEL
4074 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4075 #endif
4076 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4077 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4078 #endif
4079 #ifndef DEBUG_ADDR_SECTION_LABEL
4080 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4081 #endif
4082 #ifndef DEBUG_LOC_SECTION_LABEL
4083 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4084 #endif
4085 #ifndef DEBUG_RANGES_SECTION_LABEL
4086 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4087 #endif
4088 #ifndef DEBUG_MACINFO_SECTION_LABEL
4089 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4090 #endif
4091 #ifndef DEBUG_MACRO_SECTION_LABEL
4092 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4093 #endif
4094 #define SKELETON_COMP_DIE_ABBREV 1
4095 #define SKELETON_TYPE_DIE_ABBREV 2
4097 /* Definitions of defaults for formats and names of various special
4098 (artificial) labels which may be generated within this file (when the -g
4099 options is used and DWARF2_DEBUGGING_INFO is in effect.
4100 If necessary, these may be overridden from within the tm.h file, but
4101 typically, overriding these defaults is unnecessary. */
4103 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4104 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4105 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4106 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4107 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4108 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4109 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4110 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4111 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4112 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4113 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4114 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4115 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4116 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4117 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4119 #ifndef TEXT_END_LABEL
4120 #define TEXT_END_LABEL "Letext"
4121 #endif
4122 #ifndef COLD_END_LABEL
4123 #define COLD_END_LABEL "Letext_cold"
4124 #endif
4125 #ifndef BLOCK_BEGIN_LABEL
4126 #define BLOCK_BEGIN_LABEL "LBB"
4127 #endif
4128 #ifndef BLOCK_INLINE_ENTRY_LABEL
4129 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4130 #endif
4131 #ifndef BLOCK_END_LABEL
4132 #define BLOCK_END_LABEL "LBE"
4133 #endif
4134 #ifndef LINE_CODE_LABEL
4135 #define LINE_CODE_LABEL "LM"
4136 #endif
4139 /* Return the root of the DIE's built for the current compilation unit. */
4140 static dw_die_ref
4141 comp_unit_die (void)
4143 if (!single_comp_unit_die)
4144 single_comp_unit_die = gen_compile_unit_die (NULL);
4145 return single_comp_unit_die;
4148 /* We allow a language front-end to designate a function that is to be
4149 called to "demangle" any name before it is put into a DIE. */
4151 static const char *(*demangle_name_func) (const char *);
4153 void
4154 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4156 demangle_name_func = func;
4159 /* Test if rtl node points to a pseudo register. */
4161 static inline int
4162 is_pseudo_reg (const_rtx rtl)
4164 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4165 || (GET_CODE (rtl) == SUBREG
4166 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4169 /* Return a reference to a type, with its const and volatile qualifiers
4170 removed. */
4172 static inline tree
4173 type_main_variant (tree type)
4175 type = TYPE_MAIN_VARIANT (type);
4177 /* ??? There really should be only one main variant among any group of
4178 variants of a given type (and all of the MAIN_VARIANT values for all
4179 members of the group should point to that one type) but sometimes the C
4180 front-end messes this up for array types, so we work around that bug
4181 here. */
4182 if (TREE_CODE (type) == ARRAY_TYPE)
4183 while (type != TYPE_MAIN_VARIANT (type))
4184 type = TYPE_MAIN_VARIANT (type);
4186 return type;
4189 /* Return nonzero if the given type node represents a tagged type. */
4191 static inline int
4192 is_tagged_type (const_tree type)
4194 enum tree_code code = TREE_CODE (type);
4196 return (code == RECORD_TYPE || code == UNION_TYPE
4197 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4200 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4202 static void
4203 get_ref_die_offset_label (char *label, dw_die_ref ref)
4205 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4208 /* Return die_offset of a DIE reference to a base type. */
4210 static unsigned long int
4211 get_base_type_offset (dw_die_ref ref)
4213 if (ref->die_offset)
4214 return ref->die_offset;
4215 if (comp_unit_die ()->die_abbrev)
4217 calc_base_type_die_sizes ();
4218 gcc_assert (ref->die_offset);
4220 return ref->die_offset;
4223 /* Return die_offset of a DIE reference other than base type. */
4225 static unsigned long int
4226 get_ref_die_offset (dw_die_ref ref)
4228 gcc_assert (ref->die_offset);
4229 return ref->die_offset;
4232 /* Convert a DIE tag into its string name. */
4234 static const char *
4235 dwarf_tag_name (unsigned int tag)
4237 const char *name = get_DW_TAG_name (tag);
4239 if (name != NULL)
4240 return name;
4242 return "DW_TAG_<unknown>";
4245 /* Convert a DWARF attribute code into its string name. */
4247 static const char *
4248 dwarf_attr_name (unsigned int attr)
4250 const char *name;
4252 switch (attr)
4254 #if VMS_DEBUGGING_INFO
4255 case DW_AT_HP_prologue:
4256 return "DW_AT_HP_prologue";
4257 #else
4258 case DW_AT_MIPS_loop_unroll_factor:
4259 return "DW_AT_MIPS_loop_unroll_factor";
4260 #endif
4262 #if VMS_DEBUGGING_INFO
4263 case DW_AT_HP_epilogue:
4264 return "DW_AT_HP_epilogue";
4265 #else
4266 case DW_AT_MIPS_stride:
4267 return "DW_AT_MIPS_stride";
4268 #endif
4271 name = get_DW_AT_name (attr);
4273 if (name != NULL)
4274 return name;
4276 return "DW_AT_<unknown>";
4279 /* Convert a DWARF value form code into its string name. */
4281 static const char *
4282 dwarf_form_name (unsigned int form)
4284 const char *name = get_DW_FORM_name (form);
4286 if (name != NULL)
4287 return name;
4289 return "DW_FORM_<unknown>";
4292 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4293 instance of an inlined instance of a decl which is local to an inline
4294 function, so we have to trace all of the way back through the origin chain
4295 to find out what sort of node actually served as the original seed for the
4296 given block. */
4298 static tree
4299 decl_ultimate_origin (const_tree decl)
4301 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4302 return NULL_TREE;
4304 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4305 we're trying to output the abstract instance of this function. */
4306 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4307 return NULL_TREE;
4309 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4310 most distant ancestor, this should never happen. */
4311 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4313 return DECL_ABSTRACT_ORIGIN (decl);
4316 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4317 of a virtual function may refer to a base class, so we check the 'this'
4318 parameter. */
4320 static tree
4321 decl_class_context (tree decl)
4323 tree context = NULL_TREE;
4325 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4326 context = DECL_CONTEXT (decl);
4327 else
4328 context = TYPE_MAIN_VARIANT
4329 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4331 if (context && !TYPE_P (context))
4332 context = NULL_TREE;
4334 return context;
4337 /* Add an attribute/value pair to a DIE. */
4339 static inline void
4340 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4342 /* Maybe this should be an assert? */
4343 if (die == NULL)
4344 return;
4346 if (flag_checking)
4348 /* Check we do not add duplicate attrs. Can't use get_AT here
4349 because that recurses to the specification/abstract origin DIE. */
4350 dw_attr_node *a;
4351 unsigned ix;
4352 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4353 gcc_assert (a->dw_attr != attr->dw_attr);
4356 vec_safe_reserve (die->die_attr, 1);
4357 vec_safe_push (die->die_attr, *attr);
4360 static inline enum dw_val_class
4361 AT_class (dw_attr_node *a)
4363 return a->dw_attr_val.val_class;
4366 /* Return the index for any attribute that will be referenced with a
4367 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4368 are stored in dw_attr_val.v.val_str for reference counting
4369 pruning. */
4371 static inline unsigned int
4372 AT_index (dw_attr_node *a)
4374 if (AT_class (a) == dw_val_class_str)
4375 return a->dw_attr_val.v.val_str->index;
4376 else if (a->dw_attr_val.val_entry != NULL)
4377 return a->dw_attr_val.val_entry->index;
4378 return NOT_INDEXED;
4381 /* Add a flag value attribute to a DIE. */
4383 static inline void
4384 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4386 dw_attr_node attr;
4388 attr.dw_attr = attr_kind;
4389 attr.dw_attr_val.val_class = dw_val_class_flag;
4390 attr.dw_attr_val.val_entry = NULL;
4391 attr.dw_attr_val.v.val_flag = flag;
4392 add_dwarf_attr (die, &attr);
4395 static inline unsigned
4396 AT_flag (dw_attr_node *a)
4398 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4399 return a->dw_attr_val.v.val_flag;
4402 /* Add a signed integer attribute value to a DIE. */
4404 static inline void
4405 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4407 dw_attr_node attr;
4409 attr.dw_attr = attr_kind;
4410 attr.dw_attr_val.val_class = dw_val_class_const;
4411 attr.dw_attr_val.val_entry = NULL;
4412 attr.dw_attr_val.v.val_int = int_val;
4413 add_dwarf_attr (die, &attr);
4416 static inline HOST_WIDE_INT
4417 AT_int (dw_attr_node *a)
4419 gcc_assert (a && (AT_class (a) == dw_val_class_const
4420 || AT_class (a) == dw_val_class_const_implicit));
4421 return a->dw_attr_val.v.val_int;
4424 /* Add an unsigned integer attribute value to a DIE. */
4426 static inline void
4427 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4428 unsigned HOST_WIDE_INT unsigned_val)
4430 dw_attr_node attr;
4432 attr.dw_attr = attr_kind;
4433 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4434 attr.dw_attr_val.val_entry = NULL;
4435 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4436 add_dwarf_attr (die, &attr);
4439 static inline unsigned HOST_WIDE_INT
4440 AT_unsigned (dw_attr_node *a)
4442 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4443 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4444 return a->dw_attr_val.v.val_unsigned;
4447 /* Add an unsigned wide integer attribute value to a DIE. */
4449 static inline void
4450 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4451 const wide_int& w)
4453 dw_attr_node attr;
4455 attr.dw_attr = attr_kind;
4456 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4457 attr.dw_attr_val.val_entry = NULL;
4458 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4459 *attr.dw_attr_val.v.val_wide = w;
4460 add_dwarf_attr (die, &attr);
4463 /* Add an unsigned double integer attribute value to a DIE. */
4465 static inline void
4466 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4467 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4469 dw_attr_node attr;
4471 attr.dw_attr = attr_kind;
4472 attr.dw_attr_val.val_class = dw_val_class_const_double;
4473 attr.dw_attr_val.val_entry = NULL;
4474 attr.dw_attr_val.v.val_double.high = high;
4475 attr.dw_attr_val.v.val_double.low = low;
4476 add_dwarf_attr (die, &attr);
4479 /* Add a floating point attribute value to a DIE and return it. */
4481 static inline void
4482 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4483 unsigned int length, unsigned int elt_size, unsigned char *array)
4485 dw_attr_node attr;
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_vec;
4489 attr.dw_attr_val.val_entry = NULL;
4490 attr.dw_attr_val.v.val_vec.length = length;
4491 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4492 attr.dw_attr_val.v.val_vec.array = array;
4493 add_dwarf_attr (die, &attr);
4496 /* Add an 8-byte data attribute value to a DIE. */
4498 static inline void
4499 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4500 unsigned char data8[8])
4502 dw_attr_node attr;
4504 attr.dw_attr = attr_kind;
4505 attr.dw_attr_val.val_class = dw_val_class_data8;
4506 attr.dw_attr_val.val_entry = NULL;
4507 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4508 add_dwarf_attr (die, &attr);
4511 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4512 dwarf_split_debug_info, address attributes in dies destined for the
4513 final executable have force_direct set to avoid using indexed
4514 references. */
4516 static inline void
4517 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4518 bool force_direct)
4520 dw_attr_node attr;
4521 char * lbl_id;
4523 lbl_id = xstrdup (lbl_low);
4524 attr.dw_attr = DW_AT_low_pc;
4525 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4526 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4527 if (dwarf_split_debug_info && !force_direct)
4528 attr.dw_attr_val.val_entry
4529 = add_addr_table_entry (lbl_id, ate_kind_label);
4530 else
4531 attr.dw_attr_val.val_entry = NULL;
4532 add_dwarf_attr (die, &attr);
4534 attr.dw_attr = DW_AT_high_pc;
4535 if (dwarf_version < 4)
4536 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4537 else
4538 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4539 lbl_id = xstrdup (lbl_high);
4540 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4541 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4542 && dwarf_split_debug_info && !force_direct)
4543 attr.dw_attr_val.val_entry
4544 = add_addr_table_entry (lbl_id, ate_kind_label);
4545 else
4546 attr.dw_attr_val.val_entry = NULL;
4547 add_dwarf_attr (die, &attr);
4550 /* Hash and equality functions for debug_str_hash. */
4552 hashval_t
4553 indirect_string_hasher::hash (indirect_string_node *x)
4555 return htab_hash_string (x->str);
4558 bool
4559 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4561 return strcmp (x1->str, x2) == 0;
4564 /* Add STR to the given string hash table. */
4566 static struct indirect_string_node *
4567 find_AT_string_in_table (const char *str,
4568 hash_table<indirect_string_hasher> *table)
4570 struct indirect_string_node *node;
4572 indirect_string_node **slot
4573 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4574 if (*slot == NULL)
4576 node = ggc_cleared_alloc<indirect_string_node> ();
4577 node->str = ggc_strdup (str);
4578 *slot = node;
4580 else
4581 node = *slot;
4583 node->refcount++;
4584 return node;
4587 /* Add STR to the indirect string hash table. */
4589 static struct indirect_string_node *
4590 find_AT_string (const char *str)
4592 if (! debug_str_hash)
4593 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4595 return find_AT_string_in_table (str, debug_str_hash);
4598 /* Add a string attribute value to a DIE. */
4600 static inline void
4601 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4603 dw_attr_node attr;
4604 struct indirect_string_node *node;
4606 node = find_AT_string (str);
4608 attr.dw_attr = attr_kind;
4609 attr.dw_attr_val.val_class = dw_val_class_str;
4610 attr.dw_attr_val.val_entry = NULL;
4611 attr.dw_attr_val.v.val_str = node;
4612 add_dwarf_attr (die, &attr);
4615 static inline const char *
4616 AT_string (dw_attr_node *a)
4618 gcc_assert (a && AT_class (a) == dw_val_class_str);
4619 return a->dw_attr_val.v.val_str->str;
4622 /* Call this function directly to bypass AT_string_form's logic to put
4623 the string inline in the die. */
4625 static void
4626 set_indirect_string (struct indirect_string_node *node)
4628 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4629 /* Already indirect is a no op. */
4630 if (node->form == DW_FORM_strp
4631 || node->form == DW_FORM_line_strp
4632 || node->form == DW_FORM_GNU_str_index)
4634 gcc_assert (node->label);
4635 return;
4637 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4638 ++dw2_string_counter;
4639 node->label = xstrdup (label);
4641 if (!dwarf_split_debug_info)
4643 node->form = DW_FORM_strp;
4644 node->index = NOT_INDEXED;
4646 else
4648 node->form = DW_FORM_GNU_str_index;
4649 node->index = NO_INDEX_ASSIGNED;
4653 /* A helper function for dwarf2out_finish, called to reset indirect
4654 string decisions done for early LTO dwarf output before fat object
4655 dwarf output. */
4658 reset_indirect_string (indirect_string_node **h, void *)
4660 struct indirect_string_node *node = *h;
4661 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4663 free (node->label);
4664 node->label = NULL;
4665 node->form = (dwarf_form) 0;
4666 node->index = 0;
4668 return 1;
4671 /* Find out whether a string should be output inline in DIE
4672 or out-of-line in .debug_str section. */
4674 static enum dwarf_form
4675 find_string_form (struct indirect_string_node *node)
4677 unsigned int len;
4679 if (node->form)
4680 return node->form;
4682 len = strlen (node->str) + 1;
4684 /* If the string is shorter or equal to the size of the reference, it is
4685 always better to put it inline. */
4686 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4687 return node->form = DW_FORM_string;
4689 /* If we cannot expect the linker to merge strings in .debug_str
4690 section, only put it into .debug_str if it is worth even in this
4691 single module. */
4692 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4693 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4694 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4695 return node->form = DW_FORM_string;
4697 set_indirect_string (node);
4699 return node->form;
4702 /* Find out whether the string referenced from the attribute should be
4703 output inline in DIE or out-of-line in .debug_str section. */
4705 static enum dwarf_form
4706 AT_string_form (dw_attr_node *a)
4708 gcc_assert (a && AT_class (a) == dw_val_class_str);
4709 return find_string_form (a->dw_attr_val.v.val_str);
4712 /* Add a DIE reference attribute value to a DIE. */
4714 static inline void
4715 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4717 dw_attr_node attr;
4718 gcc_checking_assert (targ_die != NULL);
4720 /* With LTO we can end up trying to reference something we didn't create
4721 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4722 if (targ_die == NULL)
4723 return;
4725 attr.dw_attr = attr_kind;
4726 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4727 attr.dw_attr_val.val_entry = NULL;
4728 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4729 attr.dw_attr_val.v.val_die_ref.external = 0;
4730 add_dwarf_attr (die, &attr);
4733 /* Change DIE reference REF to point to NEW_DIE instead. */
4735 static inline void
4736 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4738 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4739 ref->dw_attr_val.v.val_die_ref.die = new_die;
4740 ref->dw_attr_val.v.val_die_ref.external = 0;
4743 /* Add an AT_specification attribute to a DIE, and also make the back
4744 pointer from the specification to the definition. */
4746 static inline void
4747 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4749 add_AT_die_ref (die, DW_AT_specification, targ_die);
4750 gcc_assert (!targ_die->die_definition);
4751 targ_die->die_definition = die;
4754 static inline dw_die_ref
4755 AT_ref (dw_attr_node *a)
4757 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4758 return a->dw_attr_val.v.val_die_ref.die;
4761 static inline int
4762 AT_ref_external (dw_attr_node *a)
4764 if (a && AT_class (a) == dw_val_class_die_ref)
4765 return a->dw_attr_val.v.val_die_ref.external;
4767 return 0;
4770 static inline void
4771 set_AT_ref_external (dw_attr_node *a, int i)
4773 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4774 a->dw_attr_val.v.val_die_ref.external = i;
4777 /* Add an FDE reference attribute value to a DIE. */
4779 static inline void
4780 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4782 dw_attr_node attr;
4784 attr.dw_attr = attr_kind;
4785 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4786 attr.dw_attr_val.val_entry = NULL;
4787 attr.dw_attr_val.v.val_fde_index = targ_fde;
4788 add_dwarf_attr (die, &attr);
4791 /* Add a location description attribute value to a DIE. */
4793 static inline void
4794 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4796 dw_attr_node attr;
4798 attr.dw_attr = attr_kind;
4799 attr.dw_attr_val.val_class = dw_val_class_loc;
4800 attr.dw_attr_val.val_entry = NULL;
4801 attr.dw_attr_val.v.val_loc = loc;
4802 add_dwarf_attr (die, &attr);
4805 static inline dw_loc_descr_ref
4806 AT_loc (dw_attr_node *a)
4808 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4809 return a->dw_attr_val.v.val_loc;
4812 static inline void
4813 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4815 dw_attr_node attr;
4817 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4818 return;
4820 attr.dw_attr = attr_kind;
4821 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4822 attr.dw_attr_val.val_entry = NULL;
4823 attr.dw_attr_val.v.val_loc_list = loc_list;
4824 add_dwarf_attr (die, &attr);
4825 have_location_lists = true;
4828 static inline dw_loc_list_ref
4829 AT_loc_list (dw_attr_node *a)
4831 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4832 return a->dw_attr_val.v.val_loc_list;
4835 /* Add a view list attribute to DIE. It must have a DW_AT_location
4836 attribute, because the view list complements the location list. */
4838 static inline void
4839 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4841 dw_attr_node attr;
4843 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4844 return;
4846 attr.dw_attr = attr_kind;
4847 attr.dw_attr_val.val_class = dw_val_class_view_list;
4848 attr.dw_attr_val.val_entry = NULL;
4849 attr.dw_attr_val.v.val_view_list = die;
4850 add_dwarf_attr (die, &attr);
4851 gcc_checking_assert (get_AT (die, DW_AT_location));
4852 gcc_assert (have_location_lists);
4855 /* Return a pointer to the location list referenced by the attribute.
4856 If the named attribute is a view list, look up the corresponding
4857 DW_AT_location attribute and return its location list. */
4859 static inline dw_loc_list_ref *
4860 AT_loc_list_ptr (dw_attr_node *a)
4862 gcc_assert (a);
4863 switch (AT_class (a))
4865 case dw_val_class_loc_list:
4866 return &a->dw_attr_val.v.val_loc_list;
4867 case dw_val_class_view_list:
4869 dw_attr_node *l;
4870 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4871 if (!l)
4872 return NULL;
4873 gcc_checking_assert (l + 1 == a);
4874 return AT_loc_list_ptr (l);
4876 default:
4877 gcc_unreachable ();
4881 /* Return the location attribute value associated with a view list
4882 attribute value. */
4884 static inline dw_val_node *
4885 view_list_to_loc_list_val_node (dw_val_node *val)
4887 gcc_assert (val->val_class == dw_val_class_view_list);
4888 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4889 if (!loc)
4890 return NULL;
4891 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4892 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4893 return &loc->dw_attr_val;
4896 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4898 static hashval_t hash (addr_table_entry *);
4899 static bool equal (addr_table_entry *, addr_table_entry *);
4902 /* Table of entries into the .debug_addr section. */
4904 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4906 /* Hash an address_table_entry. */
4908 hashval_t
4909 addr_hasher::hash (addr_table_entry *a)
4911 inchash::hash hstate;
4912 switch (a->kind)
4914 case ate_kind_rtx:
4915 hstate.add_int (0);
4916 break;
4917 case ate_kind_rtx_dtprel:
4918 hstate.add_int (1);
4919 break;
4920 case ate_kind_label:
4921 return htab_hash_string (a->addr.label);
4922 default:
4923 gcc_unreachable ();
4925 inchash::add_rtx (a->addr.rtl, hstate);
4926 return hstate.end ();
4929 /* Determine equality for two address_table_entries. */
4931 bool
4932 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4934 if (a1->kind != a2->kind)
4935 return 0;
4936 switch (a1->kind)
4938 case ate_kind_rtx:
4939 case ate_kind_rtx_dtprel:
4940 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4941 case ate_kind_label:
4942 return strcmp (a1->addr.label, a2->addr.label) == 0;
4943 default:
4944 gcc_unreachable ();
4948 /* Initialize an addr_table_entry. */
4950 void
4951 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4953 e->kind = kind;
4954 switch (kind)
4956 case ate_kind_rtx:
4957 case ate_kind_rtx_dtprel:
4958 e->addr.rtl = (rtx) addr;
4959 break;
4960 case ate_kind_label:
4961 e->addr.label = (char *) addr;
4962 break;
4964 e->refcount = 0;
4965 e->index = NO_INDEX_ASSIGNED;
4968 /* Add attr to the address table entry to the table. Defer setting an
4969 index until output time. */
4971 static addr_table_entry *
4972 add_addr_table_entry (void *addr, enum ate_kind kind)
4974 addr_table_entry *node;
4975 addr_table_entry finder;
4977 gcc_assert (dwarf_split_debug_info);
4978 if (! addr_index_table)
4979 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4980 init_addr_table_entry (&finder, kind, addr);
4981 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4983 if (*slot == HTAB_EMPTY_ENTRY)
4985 node = ggc_cleared_alloc<addr_table_entry> ();
4986 init_addr_table_entry (node, kind, addr);
4987 *slot = node;
4989 else
4990 node = *slot;
4992 node->refcount++;
4993 return node;
4996 /* Remove an entry from the addr table by decrementing its refcount.
4997 Strictly, decrementing the refcount would be enough, but the
4998 assertion that the entry is actually in the table has found
4999 bugs. */
5001 static void
5002 remove_addr_table_entry (addr_table_entry *entry)
5004 gcc_assert (dwarf_split_debug_info && addr_index_table);
5005 /* After an index is assigned, the table is frozen. */
5006 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5007 entry->refcount--;
5010 /* Given a location list, remove all addresses it refers to from the
5011 address_table. */
5013 static void
5014 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5016 for (; descr; descr = descr->dw_loc_next)
5017 if (descr->dw_loc_oprnd1.val_entry != NULL)
5019 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5020 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5024 /* A helper function for dwarf2out_finish called through
5025 htab_traverse. Assign an addr_table_entry its index. All entries
5026 must be collected into the table when this function is called,
5027 because the indexing code relies on htab_traverse to traverse nodes
5028 in the same order for each run. */
5031 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5033 addr_table_entry *node = *h;
5035 /* Don't index unreferenced nodes. */
5036 if (node->refcount == 0)
5037 return 1;
5039 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5040 node->index = *index;
5041 *index += 1;
5043 return 1;
5046 /* Add an address constant attribute value to a DIE. When using
5047 dwarf_split_debug_info, address attributes in dies destined for the
5048 final executable should be direct references--setting the parameter
5049 force_direct ensures this behavior. */
5051 static inline void
5052 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5053 bool force_direct)
5055 dw_attr_node attr;
5057 attr.dw_attr = attr_kind;
5058 attr.dw_attr_val.val_class = dw_val_class_addr;
5059 attr.dw_attr_val.v.val_addr = addr;
5060 if (dwarf_split_debug_info && !force_direct)
5061 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5062 else
5063 attr.dw_attr_val.val_entry = NULL;
5064 add_dwarf_attr (die, &attr);
5067 /* Get the RTX from to an address DIE attribute. */
5069 static inline rtx
5070 AT_addr (dw_attr_node *a)
5072 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5073 return a->dw_attr_val.v.val_addr;
5076 /* Add a file attribute value to a DIE. */
5078 static inline void
5079 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5080 struct dwarf_file_data *fd)
5082 dw_attr_node attr;
5084 attr.dw_attr = attr_kind;
5085 attr.dw_attr_val.val_class = dw_val_class_file;
5086 attr.dw_attr_val.val_entry = NULL;
5087 attr.dw_attr_val.v.val_file = fd;
5088 add_dwarf_attr (die, &attr);
5091 /* Get the dwarf_file_data from a file DIE attribute. */
5093 static inline struct dwarf_file_data *
5094 AT_file (dw_attr_node *a)
5096 gcc_assert (a && (AT_class (a) == dw_val_class_file
5097 || AT_class (a) == dw_val_class_file_implicit));
5098 return a->dw_attr_val.v.val_file;
5101 /* Add a vms delta attribute value to a DIE. */
5103 static inline void
5104 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5105 const char *lbl1, const char *lbl2)
5107 dw_attr_node attr;
5109 attr.dw_attr = attr_kind;
5110 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5111 attr.dw_attr_val.val_entry = NULL;
5112 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5113 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5114 add_dwarf_attr (die, &attr);
5117 /* Add a label identifier attribute value to a DIE. */
5119 static inline void
5120 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5121 const char *lbl_id)
5123 dw_attr_node attr;
5125 attr.dw_attr = attr_kind;
5126 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5127 attr.dw_attr_val.val_entry = NULL;
5128 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5129 if (dwarf_split_debug_info)
5130 attr.dw_attr_val.val_entry
5131 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5132 ate_kind_label);
5133 add_dwarf_attr (die, &attr);
5136 /* Add a section offset attribute value to a DIE, an offset into the
5137 debug_line section. */
5139 static inline void
5140 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5141 const char *label)
5143 dw_attr_node attr;
5145 attr.dw_attr = attr_kind;
5146 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5147 attr.dw_attr_val.val_entry = NULL;
5148 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5149 add_dwarf_attr (die, &attr);
5152 /* Add a section offset attribute value to a DIE, an offset into the
5153 debug_loclists section. */
5155 static inline void
5156 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5157 const char *label)
5159 dw_attr_node attr;
5161 attr.dw_attr = attr_kind;
5162 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
5163 attr.dw_attr_val.val_entry = NULL;
5164 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5165 add_dwarf_attr (die, &attr);
5168 /* Add a section offset attribute value to a DIE, an offset into the
5169 debug_macinfo section. */
5171 static inline void
5172 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5173 const char *label)
5175 dw_attr_node attr;
5177 attr.dw_attr = attr_kind;
5178 attr.dw_attr_val.val_class = dw_val_class_macptr;
5179 attr.dw_attr_val.val_entry = NULL;
5180 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5181 add_dwarf_attr (die, &attr);
5184 /* Add an offset attribute value to a DIE. */
5186 static inline void
5187 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5188 unsigned HOST_WIDE_INT offset)
5190 dw_attr_node attr;
5192 attr.dw_attr = attr_kind;
5193 attr.dw_attr_val.val_class = dw_val_class_offset;
5194 attr.dw_attr_val.val_entry = NULL;
5195 attr.dw_attr_val.v.val_offset = offset;
5196 add_dwarf_attr (die, &attr);
5199 /* Add a range_list attribute value to a DIE. When using
5200 dwarf_split_debug_info, address attributes in dies destined for the
5201 final executable should be direct references--setting the parameter
5202 force_direct ensures this behavior. */
5204 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5205 #define RELOCATED_OFFSET (NULL)
5207 static void
5208 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5209 long unsigned int offset, bool force_direct)
5211 dw_attr_node attr;
5213 attr.dw_attr = attr_kind;
5214 attr.dw_attr_val.val_class = dw_val_class_range_list;
5215 /* For the range_list attribute, use val_entry to store whether the
5216 offset should follow split-debug-info or normal semantics. This
5217 value is read in output_range_list_offset. */
5218 if (dwarf_split_debug_info && !force_direct)
5219 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5220 else
5221 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5222 attr.dw_attr_val.v.val_offset = offset;
5223 add_dwarf_attr (die, &attr);
5226 /* Return the start label of a delta attribute. */
5228 static inline const char *
5229 AT_vms_delta1 (dw_attr_node *a)
5231 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5232 return a->dw_attr_val.v.val_vms_delta.lbl1;
5235 /* Return the end label of a delta attribute. */
5237 static inline const char *
5238 AT_vms_delta2 (dw_attr_node *a)
5240 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5241 return a->dw_attr_val.v.val_vms_delta.lbl2;
5244 static inline const char *
5245 AT_lbl (dw_attr_node *a)
5247 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5248 || AT_class (a) == dw_val_class_lineptr
5249 || AT_class (a) == dw_val_class_macptr
5250 || AT_class (a) == dw_val_class_loclistsptr
5251 || AT_class (a) == dw_val_class_high_pc));
5252 return a->dw_attr_val.v.val_lbl_id;
5255 /* Get the attribute of type attr_kind. */
5257 static dw_attr_node *
5258 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5260 dw_attr_node *a;
5261 unsigned ix;
5262 dw_die_ref spec = NULL;
5264 if (! die)
5265 return NULL;
5267 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5268 if (a->dw_attr == attr_kind)
5269 return a;
5270 else if (a->dw_attr == DW_AT_specification
5271 || a->dw_attr == DW_AT_abstract_origin)
5272 spec = AT_ref (a);
5274 if (spec)
5275 return get_AT (spec, attr_kind);
5277 return NULL;
5280 /* Returns the parent of the declaration of DIE. */
5282 static dw_die_ref
5283 get_die_parent (dw_die_ref die)
5285 dw_die_ref t;
5287 if (!die)
5288 return NULL;
5290 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5291 || (t = get_AT_ref (die, DW_AT_specification)))
5292 die = t;
5294 return die->die_parent;
5297 /* Return the "low pc" attribute value, typically associated with a subprogram
5298 DIE. Return null if the "low pc" attribute is either not present, or if it
5299 cannot be represented as an assembler label identifier. */
5301 static inline const char *
5302 get_AT_low_pc (dw_die_ref die)
5304 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5306 return a ? AT_lbl (a) : NULL;
5309 /* Return the "high pc" attribute value, typically associated with a subprogram
5310 DIE. Return null if the "high pc" attribute is either not present, or if it
5311 cannot be represented as an assembler label identifier. */
5313 static inline const char *
5314 get_AT_hi_pc (dw_die_ref die)
5316 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5318 return a ? AT_lbl (a) : NULL;
5321 /* Return the value of the string attribute designated by ATTR_KIND, or
5322 NULL if it is not present. */
5324 static inline const char *
5325 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5327 dw_attr_node *a = get_AT (die, attr_kind);
5329 return a ? AT_string (a) : NULL;
5332 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5333 if it is not present. */
5335 static inline int
5336 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5338 dw_attr_node *a = get_AT (die, attr_kind);
5340 return a ? AT_flag (a) : 0;
5343 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5344 if it is not present. */
5346 static inline unsigned
5347 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5349 dw_attr_node *a = get_AT (die, attr_kind);
5351 return a ? AT_unsigned (a) : 0;
5354 static inline dw_die_ref
5355 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5357 dw_attr_node *a = get_AT (die, attr_kind);
5359 return a ? AT_ref (a) : NULL;
5362 static inline struct dwarf_file_data *
5363 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5365 dw_attr_node *a = get_AT (die, attr_kind);
5367 return a ? AT_file (a) : NULL;
5370 /* Return TRUE if the language is C++. */
5372 static inline bool
5373 is_cxx (void)
5375 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5377 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5378 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5381 /* Return TRUE if DECL was created by the C++ frontend. */
5383 static bool
5384 is_cxx (const_tree decl)
5386 if (in_lto_p)
5388 const_tree context = get_ultimate_context (decl);
5389 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5390 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5392 return is_cxx ();
5395 /* Return TRUE if the language is Fortran. */
5397 static inline bool
5398 is_fortran (void)
5400 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5402 return (lang == DW_LANG_Fortran77
5403 || lang == DW_LANG_Fortran90
5404 || lang == DW_LANG_Fortran95
5405 || lang == DW_LANG_Fortran03
5406 || lang == DW_LANG_Fortran08);
5409 static inline bool
5410 is_fortran (const_tree decl)
5412 if (in_lto_p)
5414 const_tree context = get_ultimate_context (decl);
5415 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5416 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5417 "GNU Fortran", 11) == 0
5418 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5419 "GNU F77") == 0);
5421 return is_fortran ();
5424 /* Return TRUE if the language is Ada. */
5426 static inline bool
5427 is_ada (void)
5429 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5431 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5434 /* Remove the specified attribute if present. Return TRUE if removal
5435 was successful. */
5437 static bool
5438 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5440 dw_attr_node *a;
5441 unsigned ix;
5443 if (! die)
5444 return false;
5446 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5447 if (a->dw_attr == attr_kind)
5449 if (AT_class (a) == dw_val_class_str)
5450 if (a->dw_attr_val.v.val_str->refcount)
5451 a->dw_attr_val.v.val_str->refcount--;
5453 /* vec::ordered_remove should help reduce the number of abbrevs
5454 that are needed. */
5455 die->die_attr->ordered_remove (ix);
5456 return true;
5458 return false;
5461 /* Remove CHILD from its parent. PREV must have the property that
5462 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5464 static void
5465 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5467 gcc_assert (child->die_parent == prev->die_parent);
5468 gcc_assert (prev->die_sib == child);
5469 if (prev == child)
5471 gcc_assert (child->die_parent->die_child == child);
5472 prev = NULL;
5474 else
5475 prev->die_sib = child->die_sib;
5476 if (child->die_parent->die_child == child)
5477 child->die_parent->die_child = prev;
5478 child->die_sib = NULL;
5481 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5482 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5484 static void
5485 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5487 dw_die_ref parent = old_child->die_parent;
5489 gcc_assert (parent == prev->die_parent);
5490 gcc_assert (prev->die_sib == old_child);
5492 new_child->die_parent = parent;
5493 if (prev == old_child)
5495 gcc_assert (parent->die_child == old_child);
5496 new_child->die_sib = new_child;
5498 else
5500 prev->die_sib = new_child;
5501 new_child->die_sib = old_child->die_sib;
5503 if (old_child->die_parent->die_child == old_child)
5504 old_child->die_parent->die_child = new_child;
5505 old_child->die_sib = NULL;
5508 /* Move all children from OLD_PARENT to NEW_PARENT. */
5510 static void
5511 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5513 dw_die_ref c;
5514 new_parent->die_child = old_parent->die_child;
5515 old_parent->die_child = NULL;
5516 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5519 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5520 matches TAG. */
5522 static void
5523 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5525 dw_die_ref c;
5527 c = die->die_child;
5528 if (c) do {
5529 dw_die_ref prev = c;
5530 c = c->die_sib;
5531 while (c->die_tag == tag)
5533 remove_child_with_prev (c, prev);
5534 c->die_parent = NULL;
5535 /* Might have removed every child. */
5536 if (die->die_child == NULL)
5537 return;
5538 c = prev->die_sib;
5540 } while (c != die->die_child);
5543 /* Add a CHILD_DIE as the last child of DIE. */
5545 static void
5546 add_child_die (dw_die_ref die, dw_die_ref child_die)
5548 /* FIXME this should probably be an assert. */
5549 if (! die || ! child_die)
5550 return;
5551 gcc_assert (die != child_die);
5553 child_die->die_parent = die;
5554 if (die->die_child)
5556 child_die->die_sib = die->die_child->die_sib;
5557 die->die_child->die_sib = child_die;
5559 else
5560 child_die->die_sib = child_die;
5561 die->die_child = child_die;
5564 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5566 static void
5567 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5568 dw_die_ref after_die)
5570 gcc_assert (die
5571 && child_die
5572 && after_die
5573 && die->die_child
5574 && die != child_die);
5576 child_die->die_parent = die;
5577 child_die->die_sib = after_die->die_sib;
5578 after_die->die_sib = child_die;
5579 if (die->die_child == after_die)
5580 die->die_child = child_die;
5583 /* Unassociate CHILD from its parent, and make its parent be
5584 NEW_PARENT. */
5586 static void
5587 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5589 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5590 if (p->die_sib == child)
5592 remove_child_with_prev (child, p);
5593 break;
5595 add_child_die (new_parent, child);
5598 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5599 is the specification, to the end of PARENT's list of children.
5600 This is done by removing and re-adding it. */
5602 static void
5603 splice_child_die (dw_die_ref parent, dw_die_ref child)
5605 /* We want the declaration DIE from inside the class, not the
5606 specification DIE at toplevel. */
5607 if (child->die_parent != parent)
5609 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5611 if (tmp)
5612 child = tmp;
5615 gcc_assert (child->die_parent == parent
5616 || (child->die_parent
5617 == get_AT_ref (parent, DW_AT_specification)));
5619 reparent_child (child, parent);
5622 /* Create and return a new die with TAG_VALUE as tag. */
5624 static inline dw_die_ref
5625 new_die_raw (enum dwarf_tag tag_value)
5627 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5628 die->die_tag = tag_value;
5629 return die;
5632 /* Create and return a new die with a parent of PARENT_DIE. If
5633 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5634 associated tree T must be supplied to determine parenthood
5635 later. */
5637 static inline dw_die_ref
5638 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5640 dw_die_ref die = new_die_raw (tag_value);
5642 if (parent_die != NULL)
5643 add_child_die (parent_die, die);
5644 else
5646 limbo_die_node *limbo_node;
5648 /* No DIEs created after early dwarf should end up in limbo,
5649 because the limbo list should not persist past LTO
5650 streaming. */
5651 if (tag_value != DW_TAG_compile_unit
5652 /* These are allowed because they're generated while
5653 breaking out COMDAT units late. */
5654 && tag_value != DW_TAG_type_unit
5655 && tag_value != DW_TAG_skeleton_unit
5656 && !early_dwarf
5657 /* Allow nested functions to live in limbo because they will
5658 only temporarily live there, as decls_for_scope will fix
5659 them up. */
5660 && (TREE_CODE (t) != FUNCTION_DECL
5661 || !decl_function_context (t))
5662 /* Same as nested functions above but for types. Types that
5663 are local to a function will be fixed in
5664 decls_for_scope. */
5665 && (!RECORD_OR_UNION_TYPE_P (t)
5666 || !TYPE_CONTEXT (t)
5667 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5668 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5669 especially in the ltrans stage, but once we implement LTO
5670 dwarf streaming, we should remove this exception. */
5671 && !in_lto_p)
5673 fprintf (stderr, "symbol ended up in limbo too late:");
5674 debug_generic_stmt (t);
5675 gcc_unreachable ();
5678 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5679 limbo_node->die = die;
5680 limbo_node->created_for = t;
5681 limbo_node->next = limbo_die_list;
5682 limbo_die_list = limbo_node;
5685 return die;
5688 /* Return the DIE associated with the given type specifier. */
5690 static inline dw_die_ref
5691 lookup_type_die (tree type)
5693 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5694 if (die && die->removed)
5696 TYPE_SYMTAB_DIE (type) = NULL;
5697 return NULL;
5699 return die;
5702 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5703 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5704 anonymous type instead the one of the naming typedef. */
5706 static inline dw_die_ref
5707 strip_naming_typedef (tree type, dw_die_ref type_die)
5709 if (type
5710 && TREE_CODE (type) == RECORD_TYPE
5711 && type_die
5712 && type_die->die_tag == DW_TAG_typedef
5713 && is_naming_typedef_decl (TYPE_NAME (type)))
5714 type_die = get_AT_ref (type_die, DW_AT_type);
5715 return type_die;
5718 /* Like lookup_type_die, but if type is an anonymous type named by a
5719 typedef[1], return the DIE of the anonymous type instead the one of
5720 the naming typedef. This is because in gen_typedef_die, we did
5721 equate the anonymous struct named by the typedef with the DIE of
5722 the naming typedef. So by default, lookup_type_die on an anonymous
5723 struct yields the DIE of the naming typedef.
5725 [1]: Read the comment of is_naming_typedef_decl to learn about what
5726 a naming typedef is. */
5728 static inline dw_die_ref
5729 lookup_type_die_strip_naming_typedef (tree type)
5731 dw_die_ref die = lookup_type_die (type);
5732 return strip_naming_typedef (type, die);
5735 /* Equate a DIE to a given type specifier. */
5737 static inline void
5738 equate_type_number_to_die (tree type, dw_die_ref type_die)
5740 TYPE_SYMTAB_DIE (type) = type_die;
5743 /* Returns a hash value for X (which really is a die_struct). */
5745 inline hashval_t
5746 decl_die_hasher::hash (die_node *x)
5748 return (hashval_t) x->decl_id;
5751 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5753 inline bool
5754 decl_die_hasher::equal (die_node *x, tree y)
5756 return (x->decl_id == DECL_UID (y));
5759 /* Return the DIE associated with a given declaration. */
5761 static inline dw_die_ref
5762 lookup_decl_die (tree decl)
5764 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5765 NO_INSERT);
5766 if (!die)
5767 return NULL;
5768 if ((*die)->removed)
5770 decl_die_table->clear_slot (die);
5771 return NULL;
5773 return *die;
5777 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5778 style reference. Return true if we found one refering to a DIE for
5779 DECL, otherwise return false. */
5781 static bool
5782 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5783 unsigned HOST_WIDE_INT *off)
5785 dw_die_ref die;
5787 if (flag_wpa && !decl_die_table)
5788 return false;
5790 if (TREE_CODE (decl) == BLOCK)
5791 die = BLOCK_DIE (decl);
5792 else
5793 die = lookup_decl_die (decl);
5794 if (!die)
5795 return false;
5797 /* During WPA stage we currently use DIEs to store the
5798 decl <-> label + offset map. That's quite inefficient but it
5799 works for now. */
5800 if (flag_wpa)
5802 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5803 if (!ref)
5805 gcc_assert (die == comp_unit_die ());
5806 return false;
5808 *off = ref->die_offset;
5809 *sym = ref->die_id.die_symbol;
5810 return true;
5813 /* Similar to get_ref_die_offset_label, but using the "correct"
5814 label. */
5815 *off = die->die_offset;
5816 while (die->die_parent)
5817 die = die->die_parent;
5818 /* For the containing CU DIE we compute a die_symbol in
5819 compute_comp_unit_symbol. */
5820 gcc_assert (die->die_tag == DW_TAG_compile_unit
5821 && die->die_id.die_symbol != NULL);
5822 *sym = die->die_id.die_symbol;
5823 return true;
5826 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5828 static void
5829 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5830 const char *symbol, HOST_WIDE_INT offset)
5832 /* Create a fake DIE that contains the reference. Don't use
5833 new_die because we don't want to end up in the limbo list. */
5834 dw_die_ref ref = new_die_raw (die->die_tag);
5835 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5836 ref->die_offset = offset;
5837 ref->with_offset = 1;
5838 add_AT_die_ref (die, attr_kind, ref);
5841 /* Create a DIE for DECL if required and add a reference to a DIE
5842 at SYMBOL + OFFSET which contains attributes dumped early. */
5844 static void
5845 dwarf2out_register_external_die (tree decl, const char *sym,
5846 unsigned HOST_WIDE_INT off)
5848 if (debug_info_level == DINFO_LEVEL_NONE)
5849 return;
5851 if (flag_wpa && !decl_die_table)
5852 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5854 dw_die_ref die
5855 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5856 gcc_assert (!die);
5858 tree ctx;
5859 dw_die_ref parent = NULL;
5860 /* Need to lookup a DIE for the decls context - the containing
5861 function or translation unit. */
5862 if (TREE_CODE (decl) == BLOCK)
5864 ctx = BLOCK_SUPERCONTEXT (decl);
5865 /* ??? We do not output DIEs for all scopes thus skip as
5866 many DIEs as needed. */
5867 while (TREE_CODE (ctx) == BLOCK
5868 && !BLOCK_DIE (ctx))
5869 ctx = BLOCK_SUPERCONTEXT (ctx);
5871 else
5872 ctx = DECL_CONTEXT (decl);
5873 while (ctx && TYPE_P (ctx))
5874 ctx = TYPE_CONTEXT (ctx);
5875 if (ctx)
5877 if (TREE_CODE (ctx) == BLOCK)
5878 parent = BLOCK_DIE (ctx);
5879 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5880 /* Keep the 1:1 association during WPA. */
5881 && !flag_wpa)
5882 /* Otherwise all late annotations go to the main CU which
5883 imports the original CUs. */
5884 parent = comp_unit_die ();
5885 else if (TREE_CODE (ctx) == FUNCTION_DECL
5886 && TREE_CODE (decl) != PARM_DECL
5887 && TREE_CODE (decl) != BLOCK)
5888 /* Leave function local entities parent determination to when
5889 we process scope vars. */
5891 else
5892 parent = lookup_decl_die (ctx);
5894 else
5895 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5896 Handle this case gracefully by globalizing stuff. */
5897 parent = comp_unit_die ();
5898 /* Create a DIE "stub". */
5899 switch (TREE_CODE (decl))
5901 case TRANSLATION_UNIT_DECL:
5902 if (! flag_wpa)
5904 die = comp_unit_die ();
5905 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5906 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5907 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5908 to create a DIE for the original CUs. */
5909 return;
5911 /* Keep the 1:1 association during WPA. */
5912 die = new_die (DW_TAG_compile_unit, NULL, decl);
5913 break;
5914 case NAMESPACE_DECL:
5915 if (is_fortran (decl))
5916 die = new_die (DW_TAG_module, parent, decl);
5917 else
5918 die = new_die (DW_TAG_namespace, parent, decl);
5919 break;
5920 case FUNCTION_DECL:
5921 die = new_die (DW_TAG_subprogram, parent, decl);
5922 break;
5923 case VAR_DECL:
5924 die = new_die (DW_TAG_variable, parent, decl);
5925 break;
5926 case RESULT_DECL:
5927 die = new_die (DW_TAG_variable, parent, decl);
5928 break;
5929 case PARM_DECL:
5930 die = new_die (DW_TAG_formal_parameter, parent, decl);
5931 break;
5932 case CONST_DECL:
5933 die = new_die (DW_TAG_constant, parent, decl);
5934 break;
5935 case LABEL_DECL:
5936 die = new_die (DW_TAG_label, parent, decl);
5937 break;
5938 case BLOCK:
5939 die = new_die (DW_TAG_lexical_block, parent, decl);
5940 break;
5941 default:
5942 gcc_unreachable ();
5944 if (TREE_CODE (decl) == BLOCK)
5945 BLOCK_DIE (decl) = die;
5946 else
5947 equate_decl_number_to_die (decl, die);
5949 /* Add a reference to the DIE providing early debug at $sym + off. */
5950 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5953 /* Returns a hash value for X (which really is a var_loc_list). */
5955 inline hashval_t
5956 decl_loc_hasher::hash (var_loc_list *x)
5958 return (hashval_t) x->decl_id;
5961 /* Return nonzero if decl_id of var_loc_list X is the same as
5962 UID of decl *Y. */
5964 inline bool
5965 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5967 return (x->decl_id == DECL_UID (y));
5970 /* Return the var_loc list associated with a given declaration. */
5972 static inline var_loc_list *
5973 lookup_decl_loc (const_tree decl)
5975 if (!decl_loc_table)
5976 return NULL;
5977 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5980 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5982 inline hashval_t
5983 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5985 return (hashval_t) x->decl_id;
5988 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5989 UID of decl *Y. */
5991 inline bool
5992 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5994 return (x->decl_id == DECL_UID (y));
5997 /* Equate a DIE to a particular declaration. */
5999 static void
6000 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6002 unsigned int decl_id = DECL_UID (decl);
6004 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6005 decl_die->decl_id = decl_id;
6008 /* Return how many bits covers PIECE EXPR_LIST. */
6010 static HOST_WIDE_INT
6011 decl_piece_bitsize (rtx piece)
6013 int ret = (int) GET_MODE (piece);
6014 if (ret)
6015 return ret;
6016 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6017 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6018 return INTVAL (XEXP (XEXP (piece, 0), 0));
6021 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6023 static rtx *
6024 decl_piece_varloc_ptr (rtx piece)
6026 if ((int) GET_MODE (piece))
6027 return &XEXP (piece, 0);
6028 else
6029 return &XEXP (XEXP (piece, 0), 1);
6032 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6033 Next is the chain of following piece nodes. */
6035 static rtx_expr_list *
6036 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6038 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6039 return alloc_EXPR_LIST (bitsize, loc_note, next);
6040 else
6041 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6042 GEN_INT (bitsize),
6043 loc_note), next);
6046 /* Return rtx that should be stored into loc field for
6047 LOC_NOTE and BITPOS/BITSIZE. */
6049 static rtx
6050 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6051 HOST_WIDE_INT bitsize)
6053 if (bitsize != -1)
6055 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6056 if (bitpos != 0)
6057 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6059 return loc_note;
6062 /* This function either modifies location piece list *DEST in
6063 place (if SRC and INNER is NULL), or copies location piece list
6064 *SRC to *DEST while modifying it. Location BITPOS is modified
6065 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6066 not copied and if needed some padding around it is added.
6067 When modifying in place, DEST should point to EXPR_LIST where
6068 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6069 to the start of the whole list and INNER points to the EXPR_LIST
6070 where earlier pieces cover PIECE_BITPOS bits. */
6072 static void
6073 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6074 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6075 HOST_WIDE_INT bitsize, rtx loc_note)
6077 HOST_WIDE_INT diff;
6078 bool copy = inner != NULL;
6080 if (copy)
6082 /* First copy all nodes preceding the current bitpos. */
6083 while (src != inner)
6085 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6086 decl_piece_bitsize (*src), NULL_RTX);
6087 dest = &XEXP (*dest, 1);
6088 src = &XEXP (*src, 1);
6091 /* Add padding if needed. */
6092 if (bitpos != piece_bitpos)
6094 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6095 copy ? NULL_RTX : *dest);
6096 dest = &XEXP (*dest, 1);
6098 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6100 gcc_assert (!copy);
6101 /* A piece with correct bitpos and bitsize already exist,
6102 just update the location for it and return. */
6103 *decl_piece_varloc_ptr (*dest) = loc_note;
6104 return;
6106 /* Add the piece that changed. */
6107 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6108 dest = &XEXP (*dest, 1);
6109 /* Skip over pieces that overlap it. */
6110 diff = bitpos - piece_bitpos + bitsize;
6111 if (!copy)
6112 src = dest;
6113 while (diff > 0 && *src)
6115 rtx piece = *src;
6116 diff -= decl_piece_bitsize (piece);
6117 if (copy)
6118 src = &XEXP (piece, 1);
6119 else
6121 *src = XEXP (piece, 1);
6122 free_EXPR_LIST_node (piece);
6125 /* Add padding if needed. */
6126 if (diff < 0 && *src)
6128 if (!copy)
6129 dest = src;
6130 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6131 dest = &XEXP (*dest, 1);
6133 if (!copy)
6134 return;
6135 /* Finally copy all nodes following it. */
6136 while (*src)
6138 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6139 decl_piece_bitsize (*src), NULL_RTX);
6140 dest = &XEXP (*dest, 1);
6141 src = &XEXP (*src, 1);
6145 /* Add a variable location node to the linked list for DECL. */
6147 static struct var_loc_node *
6148 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6150 unsigned int decl_id;
6151 var_loc_list *temp;
6152 struct var_loc_node *loc = NULL;
6153 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6155 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6157 tree realdecl = DECL_DEBUG_EXPR (decl);
6158 if (handled_component_p (realdecl)
6159 || (TREE_CODE (realdecl) == MEM_REF
6160 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6162 bool reverse;
6163 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6164 &bitsize, &reverse);
6165 if (!innerdecl
6166 || !DECL_P (innerdecl)
6167 || DECL_IGNORED_P (innerdecl)
6168 || TREE_STATIC (innerdecl)
6169 || bitsize == 0
6170 || bitpos + bitsize > 256)
6171 return NULL;
6172 decl = innerdecl;
6176 decl_id = DECL_UID (decl);
6177 var_loc_list **slot
6178 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6179 if (*slot == NULL)
6181 temp = ggc_cleared_alloc<var_loc_list> ();
6182 temp->decl_id = decl_id;
6183 *slot = temp;
6185 else
6186 temp = *slot;
6188 /* For PARM_DECLs try to keep around the original incoming value,
6189 even if that means we'll emit a zero-range .debug_loc entry. */
6190 if (temp->last
6191 && temp->first == temp->last
6192 && TREE_CODE (decl) == PARM_DECL
6193 && NOTE_P (temp->first->loc)
6194 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6195 && DECL_INCOMING_RTL (decl)
6196 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6197 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6198 == GET_CODE (DECL_INCOMING_RTL (decl))
6199 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6200 && (bitsize != -1
6201 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6202 NOTE_VAR_LOCATION_LOC (loc_note))
6203 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6204 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6206 loc = ggc_cleared_alloc<var_loc_node> ();
6207 temp->first->next = loc;
6208 temp->last = loc;
6209 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6211 else if (temp->last)
6213 struct var_loc_node *last = temp->last, *unused = NULL;
6214 rtx *piece_loc = NULL, last_loc_note;
6215 HOST_WIDE_INT piece_bitpos = 0;
6216 if (last->next)
6218 last = last->next;
6219 gcc_assert (last->next == NULL);
6221 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6223 piece_loc = &last->loc;
6226 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6227 if (piece_bitpos + cur_bitsize > bitpos)
6228 break;
6229 piece_bitpos += cur_bitsize;
6230 piece_loc = &XEXP (*piece_loc, 1);
6232 while (*piece_loc);
6234 /* TEMP->LAST here is either pointer to the last but one or
6235 last element in the chained list, LAST is pointer to the
6236 last element. */
6237 if (label && strcmp (last->label, label) == 0 && last->view == view)
6239 /* For SRA optimized variables if there weren't any real
6240 insns since last note, just modify the last node. */
6241 if (piece_loc != NULL)
6243 adjust_piece_list (piece_loc, NULL, NULL,
6244 bitpos, piece_bitpos, bitsize, loc_note);
6245 return NULL;
6247 /* If the last note doesn't cover any instructions, remove it. */
6248 if (temp->last != last)
6250 temp->last->next = NULL;
6251 unused = last;
6252 last = temp->last;
6253 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6255 else
6257 gcc_assert (temp->first == temp->last
6258 || (temp->first->next == temp->last
6259 && TREE_CODE (decl) == PARM_DECL));
6260 memset (temp->last, '\0', sizeof (*temp->last));
6261 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6262 return temp->last;
6265 if (bitsize == -1 && NOTE_P (last->loc))
6266 last_loc_note = last->loc;
6267 else if (piece_loc != NULL
6268 && *piece_loc != NULL_RTX
6269 && piece_bitpos == bitpos
6270 && decl_piece_bitsize (*piece_loc) == bitsize)
6271 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6272 else
6273 last_loc_note = NULL_RTX;
6274 /* If the current location is the same as the end of the list,
6275 and either both or neither of the locations is uninitialized,
6276 we have nothing to do. */
6277 if (last_loc_note == NULL_RTX
6278 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6279 NOTE_VAR_LOCATION_LOC (loc_note)))
6280 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6281 != NOTE_VAR_LOCATION_STATUS (loc_note))
6282 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6283 == VAR_INIT_STATUS_UNINITIALIZED)
6284 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6285 == VAR_INIT_STATUS_UNINITIALIZED))))
6287 /* Add LOC to the end of list and update LAST. If the last
6288 element of the list has been removed above, reuse its
6289 memory for the new node, otherwise allocate a new one. */
6290 if (unused)
6292 loc = unused;
6293 memset (loc, '\0', sizeof (*loc));
6295 else
6296 loc = ggc_cleared_alloc<var_loc_node> ();
6297 if (bitsize == -1 || piece_loc == NULL)
6298 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6299 else
6300 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6301 bitpos, piece_bitpos, bitsize, loc_note);
6302 last->next = loc;
6303 /* Ensure TEMP->LAST will point either to the new last but one
6304 element of the chain, or to the last element in it. */
6305 if (last != temp->last)
6306 temp->last = last;
6308 else if (unused)
6309 ggc_free (unused);
6311 else
6313 loc = ggc_cleared_alloc<var_loc_node> ();
6314 temp->first = loc;
6315 temp->last = loc;
6316 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6318 return loc;
6321 /* Keep track of the number of spaces used to indent the
6322 output of the debugging routines that print the structure of
6323 the DIE internal representation. */
6324 static int print_indent;
6326 /* Indent the line the number of spaces given by print_indent. */
6328 static inline void
6329 print_spaces (FILE *outfile)
6331 fprintf (outfile, "%*s", print_indent, "");
6334 /* Print a type signature in hex. */
6336 static inline void
6337 print_signature (FILE *outfile, char *sig)
6339 int i;
6341 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6342 fprintf (outfile, "%02x", sig[i] & 0xff);
6345 static inline void
6346 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6348 if (discr_value->pos)
6349 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6350 else
6351 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6354 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6356 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6357 RECURSE, output location descriptor operations. */
6359 static void
6360 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6362 switch (val->val_class)
6364 case dw_val_class_addr:
6365 fprintf (outfile, "address");
6366 break;
6367 case dw_val_class_offset:
6368 fprintf (outfile, "offset");
6369 break;
6370 case dw_val_class_loc:
6371 fprintf (outfile, "location descriptor");
6372 if (val->v.val_loc == NULL)
6373 fprintf (outfile, " -> <null>\n");
6374 else if (recurse)
6376 fprintf (outfile, ":\n");
6377 print_indent += 4;
6378 print_loc_descr (val->v.val_loc, outfile);
6379 print_indent -= 4;
6381 else
6382 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6383 break;
6384 case dw_val_class_loc_list:
6385 fprintf (outfile, "location list -> label:%s",
6386 val->v.val_loc_list->ll_symbol);
6387 break;
6388 case dw_val_class_view_list:
6389 val = view_list_to_loc_list_val_node (val);
6390 fprintf (outfile, "location list with views -> labels:%s and %s",
6391 val->v.val_loc_list->ll_symbol,
6392 val->v.val_loc_list->vl_symbol);
6393 break;
6394 case dw_val_class_range_list:
6395 fprintf (outfile, "range list");
6396 break;
6397 case dw_val_class_const:
6398 case dw_val_class_const_implicit:
6399 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6400 break;
6401 case dw_val_class_unsigned_const:
6402 case dw_val_class_unsigned_const_implicit:
6403 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6404 break;
6405 case dw_val_class_const_double:
6406 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6407 HOST_WIDE_INT_PRINT_UNSIGNED")",
6408 val->v.val_double.high,
6409 val->v.val_double.low);
6410 break;
6411 case dw_val_class_wide_int:
6413 int i = val->v.val_wide->get_len ();
6414 fprintf (outfile, "constant (");
6415 gcc_assert (i > 0);
6416 if (val->v.val_wide->elt (i - 1) == 0)
6417 fprintf (outfile, "0x");
6418 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6419 val->v.val_wide->elt (--i));
6420 while (--i >= 0)
6421 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6422 val->v.val_wide->elt (i));
6423 fprintf (outfile, ")");
6424 break;
6426 case dw_val_class_vec:
6427 fprintf (outfile, "floating-point or vector constant");
6428 break;
6429 case dw_val_class_flag:
6430 fprintf (outfile, "%u", val->v.val_flag);
6431 break;
6432 case dw_val_class_die_ref:
6433 if (val->v.val_die_ref.die != NULL)
6435 dw_die_ref die = val->v.val_die_ref.die;
6437 if (die->comdat_type_p)
6439 fprintf (outfile, "die -> signature: ");
6440 print_signature (outfile,
6441 die->die_id.die_type_node->signature);
6443 else if (die->die_id.die_symbol)
6445 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6446 if (die->with_offset)
6447 fprintf (outfile, " + %ld", die->die_offset);
6449 else
6450 fprintf (outfile, "die -> %ld", die->die_offset);
6451 fprintf (outfile, " (%p)", (void *) die);
6453 else
6454 fprintf (outfile, "die -> <null>");
6455 break;
6456 case dw_val_class_vms_delta:
6457 fprintf (outfile, "delta: @slotcount(%s-%s)",
6458 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6459 break;
6460 case dw_val_class_lbl_id:
6461 case dw_val_class_lineptr:
6462 case dw_val_class_macptr:
6463 case dw_val_class_loclistsptr:
6464 case dw_val_class_high_pc:
6465 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6466 break;
6467 case dw_val_class_str:
6468 if (val->v.val_str->str != NULL)
6469 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6470 else
6471 fprintf (outfile, "<null>");
6472 break;
6473 case dw_val_class_file:
6474 case dw_val_class_file_implicit:
6475 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6476 val->v.val_file->emitted_number);
6477 break;
6478 case dw_val_class_data8:
6480 int i;
6482 for (i = 0; i < 8; i++)
6483 fprintf (outfile, "%02x", val->v.val_data8[i]);
6484 break;
6486 case dw_val_class_discr_value:
6487 print_discr_value (outfile, &val->v.val_discr_value);
6488 break;
6489 case dw_val_class_discr_list:
6490 for (dw_discr_list_ref node = val->v.val_discr_list;
6491 node != NULL;
6492 node = node->dw_discr_next)
6494 if (node->dw_discr_range)
6496 fprintf (outfile, " .. ");
6497 print_discr_value (outfile, &node->dw_discr_lower_bound);
6498 print_discr_value (outfile, &node->dw_discr_upper_bound);
6500 else
6501 print_discr_value (outfile, &node->dw_discr_lower_bound);
6503 if (node->dw_discr_next != NULL)
6504 fprintf (outfile, " | ");
6506 default:
6507 break;
6511 /* Likewise, for a DIE attribute. */
6513 static void
6514 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6516 print_dw_val (&a->dw_attr_val, recurse, outfile);
6520 /* Print the list of operands in the LOC location description to OUTFILE. This
6521 routine is a debugging aid only. */
6523 static void
6524 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6526 dw_loc_descr_ref l = loc;
6528 if (loc == NULL)
6530 print_spaces (outfile);
6531 fprintf (outfile, "<null>\n");
6532 return;
6535 for (l = loc; l != NULL; l = l->dw_loc_next)
6537 print_spaces (outfile);
6538 fprintf (outfile, "(%p) %s",
6539 (void *) l,
6540 dwarf_stack_op_name (l->dw_loc_opc));
6541 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6543 fprintf (outfile, " ");
6544 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6546 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6548 fprintf (outfile, ", ");
6549 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6551 fprintf (outfile, "\n");
6555 /* Print the information associated with a given DIE, and its children.
6556 This routine is a debugging aid only. */
6558 static void
6559 print_die (dw_die_ref die, FILE *outfile)
6561 dw_attr_node *a;
6562 dw_die_ref c;
6563 unsigned ix;
6565 print_spaces (outfile);
6566 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6567 die->die_offset, dwarf_tag_name (die->die_tag),
6568 (void*) die);
6569 print_spaces (outfile);
6570 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6571 fprintf (outfile, " offset: %ld", die->die_offset);
6572 fprintf (outfile, " mark: %d\n", die->die_mark);
6574 if (die->comdat_type_p)
6576 print_spaces (outfile);
6577 fprintf (outfile, " signature: ");
6578 print_signature (outfile, die->die_id.die_type_node->signature);
6579 fprintf (outfile, "\n");
6582 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6584 print_spaces (outfile);
6585 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6587 print_attribute (a, true, outfile);
6588 fprintf (outfile, "\n");
6591 if (die->die_child != NULL)
6593 print_indent += 4;
6594 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6595 print_indent -= 4;
6597 if (print_indent == 0)
6598 fprintf (outfile, "\n");
6601 /* Print the list of operations in the LOC location description. */
6603 DEBUG_FUNCTION void
6604 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6606 print_loc_descr (loc, stderr);
6609 /* Print the information collected for a given DIE. */
6611 DEBUG_FUNCTION void
6612 debug_dwarf_die (dw_die_ref die)
6614 print_die (die, stderr);
6617 DEBUG_FUNCTION void
6618 debug (die_struct &ref)
6620 print_die (&ref, stderr);
6623 DEBUG_FUNCTION void
6624 debug (die_struct *ptr)
6626 if (ptr)
6627 debug (*ptr);
6628 else
6629 fprintf (stderr, "<nil>\n");
6633 /* Print all DWARF information collected for the compilation unit.
6634 This routine is a debugging aid only. */
6636 DEBUG_FUNCTION void
6637 debug_dwarf (void)
6639 print_indent = 0;
6640 print_die (comp_unit_die (), stderr);
6643 /* Verify the DIE tree structure. */
6645 DEBUG_FUNCTION void
6646 verify_die (dw_die_ref die)
6648 gcc_assert (!die->die_mark);
6649 if (die->die_parent == NULL
6650 && die->die_sib == NULL)
6651 return;
6652 /* Verify the die_sib list is cyclic. */
6653 dw_die_ref x = die;
6656 x->die_mark = 1;
6657 x = x->die_sib;
6659 while (x && !x->die_mark);
6660 gcc_assert (x == die);
6661 x = die;
6664 /* Verify all dies have the same parent. */
6665 gcc_assert (x->die_parent == die->die_parent);
6666 if (x->die_child)
6668 /* Verify the child has the proper parent and recurse. */
6669 gcc_assert (x->die_child->die_parent == x);
6670 verify_die (x->die_child);
6672 x->die_mark = 0;
6673 x = x->die_sib;
6675 while (x && x->die_mark);
6678 /* Sanity checks on DIEs. */
6680 static void
6681 check_die (dw_die_ref die)
6683 unsigned ix;
6684 dw_attr_node *a;
6685 bool inline_found = false;
6686 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6687 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6690 switch (a->dw_attr)
6692 case DW_AT_inline:
6693 if (a->dw_attr_val.v.val_unsigned)
6694 inline_found = true;
6695 break;
6696 case DW_AT_location:
6697 ++n_location;
6698 break;
6699 case DW_AT_low_pc:
6700 ++n_low_pc;
6701 break;
6702 case DW_AT_high_pc:
6703 ++n_high_pc;
6704 break;
6705 case DW_AT_artificial:
6706 ++n_artificial;
6707 break;
6708 case DW_AT_decl_column:
6709 ++n_decl_column;
6710 break;
6711 case DW_AT_decl_line:
6712 ++n_decl_line;
6713 break;
6714 case DW_AT_decl_file:
6715 ++n_decl_file;
6716 break;
6717 default:
6718 break;
6721 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6722 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6724 fprintf (stderr, "Duplicate attributes in DIE:\n");
6725 debug_dwarf_die (die);
6726 gcc_unreachable ();
6728 if (inline_found)
6730 /* A debugging information entry that is a member of an abstract
6731 instance tree [that has DW_AT_inline] should not contain any
6732 attributes which describe aspects of the subroutine which vary
6733 between distinct inlined expansions or distinct out-of-line
6734 expansions. */
6735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6736 gcc_assert (a->dw_attr != DW_AT_low_pc
6737 && a->dw_attr != DW_AT_high_pc
6738 && a->dw_attr != DW_AT_location
6739 && a->dw_attr != DW_AT_frame_base
6740 && a->dw_attr != DW_AT_call_all_calls
6741 && a->dw_attr != DW_AT_GNU_all_call_sites);
6745 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6746 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6747 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6749 /* Calculate the checksum of a location expression. */
6751 static inline void
6752 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6754 int tem;
6755 inchash::hash hstate;
6756 hashval_t hash;
6758 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6759 CHECKSUM (tem);
6760 hash_loc_operands (loc, hstate);
6761 hash = hstate.end();
6762 CHECKSUM (hash);
6765 /* Calculate the checksum of an attribute. */
6767 static void
6768 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6770 dw_loc_descr_ref loc;
6771 rtx r;
6773 CHECKSUM (at->dw_attr);
6775 /* We don't care that this was compiled with a different compiler
6776 snapshot; if the output is the same, that's what matters. */
6777 if (at->dw_attr == DW_AT_producer)
6778 return;
6780 switch (AT_class (at))
6782 case dw_val_class_const:
6783 case dw_val_class_const_implicit:
6784 CHECKSUM (at->dw_attr_val.v.val_int);
6785 break;
6786 case dw_val_class_unsigned_const:
6787 case dw_val_class_unsigned_const_implicit:
6788 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6789 break;
6790 case dw_val_class_const_double:
6791 CHECKSUM (at->dw_attr_val.v.val_double);
6792 break;
6793 case dw_val_class_wide_int:
6794 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6795 get_full_len (*at->dw_attr_val.v.val_wide)
6796 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6797 break;
6798 case dw_val_class_vec:
6799 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6800 (at->dw_attr_val.v.val_vec.length
6801 * at->dw_attr_val.v.val_vec.elt_size));
6802 break;
6803 case dw_val_class_flag:
6804 CHECKSUM (at->dw_attr_val.v.val_flag);
6805 break;
6806 case dw_val_class_str:
6807 CHECKSUM_STRING (AT_string (at));
6808 break;
6810 case dw_val_class_addr:
6811 r = AT_addr (at);
6812 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6813 CHECKSUM_STRING (XSTR (r, 0));
6814 break;
6816 case dw_val_class_offset:
6817 CHECKSUM (at->dw_attr_val.v.val_offset);
6818 break;
6820 case dw_val_class_loc:
6821 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6822 loc_checksum (loc, ctx);
6823 break;
6825 case dw_val_class_die_ref:
6826 die_checksum (AT_ref (at), ctx, mark);
6827 break;
6829 case dw_val_class_fde_ref:
6830 case dw_val_class_vms_delta:
6831 case dw_val_class_lbl_id:
6832 case dw_val_class_lineptr:
6833 case dw_val_class_macptr:
6834 case dw_val_class_loclistsptr:
6835 case dw_val_class_high_pc:
6836 break;
6838 case dw_val_class_file:
6839 case dw_val_class_file_implicit:
6840 CHECKSUM_STRING (AT_file (at)->filename);
6841 break;
6843 case dw_val_class_data8:
6844 CHECKSUM (at->dw_attr_val.v.val_data8);
6845 break;
6847 default:
6848 break;
6852 /* Calculate the checksum of a DIE. */
6854 static void
6855 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6857 dw_die_ref c;
6858 dw_attr_node *a;
6859 unsigned ix;
6861 /* To avoid infinite recursion. */
6862 if (die->die_mark)
6864 CHECKSUM (die->die_mark);
6865 return;
6867 die->die_mark = ++(*mark);
6869 CHECKSUM (die->die_tag);
6871 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6872 attr_checksum (a, ctx, mark);
6874 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6877 #undef CHECKSUM
6878 #undef CHECKSUM_BLOCK
6879 #undef CHECKSUM_STRING
6881 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6882 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6883 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6884 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6885 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6886 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6887 #define CHECKSUM_ATTR(FOO) \
6888 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6890 /* Calculate the checksum of a number in signed LEB128 format. */
6892 static void
6893 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6895 unsigned char byte;
6896 bool more;
6898 while (1)
6900 byte = (value & 0x7f);
6901 value >>= 7;
6902 more = !((value == 0 && (byte & 0x40) == 0)
6903 || (value == -1 && (byte & 0x40) != 0));
6904 if (more)
6905 byte |= 0x80;
6906 CHECKSUM (byte);
6907 if (!more)
6908 break;
6912 /* Calculate the checksum of a number in unsigned LEB128 format. */
6914 static void
6915 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6917 while (1)
6919 unsigned char byte = (value & 0x7f);
6920 value >>= 7;
6921 if (value != 0)
6922 /* More bytes to follow. */
6923 byte |= 0x80;
6924 CHECKSUM (byte);
6925 if (value == 0)
6926 break;
6930 /* Checksum the context of the DIE. This adds the names of any
6931 surrounding namespaces or structures to the checksum. */
6933 static void
6934 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6936 const char *name;
6937 dw_die_ref spec;
6938 int tag = die->die_tag;
6940 if (tag != DW_TAG_namespace
6941 && tag != DW_TAG_structure_type
6942 && tag != DW_TAG_class_type)
6943 return;
6945 name = get_AT_string (die, DW_AT_name);
6947 spec = get_AT_ref (die, DW_AT_specification);
6948 if (spec != NULL)
6949 die = spec;
6951 if (die->die_parent != NULL)
6952 checksum_die_context (die->die_parent, ctx);
6954 CHECKSUM_ULEB128 ('C');
6955 CHECKSUM_ULEB128 (tag);
6956 if (name != NULL)
6957 CHECKSUM_STRING (name);
6960 /* Calculate the checksum of a location expression. */
6962 static inline void
6963 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6965 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6966 were emitted as a DW_FORM_sdata instead of a location expression. */
6967 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6969 CHECKSUM_ULEB128 (DW_FORM_sdata);
6970 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6971 return;
6974 /* Otherwise, just checksum the raw location expression. */
6975 while (loc != NULL)
6977 inchash::hash hstate;
6978 hashval_t hash;
6980 CHECKSUM_ULEB128 (loc->dtprel);
6981 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6982 hash_loc_operands (loc, hstate);
6983 hash = hstate.end ();
6984 CHECKSUM (hash);
6985 loc = loc->dw_loc_next;
6989 /* Calculate the checksum of an attribute. */
6991 static void
6992 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6993 struct md5_ctx *ctx, int *mark)
6995 dw_loc_descr_ref loc;
6996 rtx r;
6998 if (AT_class (at) == dw_val_class_die_ref)
7000 dw_die_ref target_die = AT_ref (at);
7002 /* For pointer and reference types, we checksum only the (qualified)
7003 name of the target type (if there is a name). For friend entries,
7004 we checksum only the (qualified) name of the target type or function.
7005 This allows the checksum to remain the same whether the target type
7006 is complete or not. */
7007 if ((at->dw_attr == DW_AT_type
7008 && (tag == DW_TAG_pointer_type
7009 || tag == DW_TAG_reference_type
7010 || tag == DW_TAG_rvalue_reference_type
7011 || tag == DW_TAG_ptr_to_member_type))
7012 || (at->dw_attr == DW_AT_friend
7013 && tag == DW_TAG_friend))
7015 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7017 if (name_attr != NULL)
7019 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7021 if (decl == NULL)
7022 decl = target_die;
7023 CHECKSUM_ULEB128 ('N');
7024 CHECKSUM_ULEB128 (at->dw_attr);
7025 if (decl->die_parent != NULL)
7026 checksum_die_context (decl->die_parent, ctx);
7027 CHECKSUM_ULEB128 ('E');
7028 CHECKSUM_STRING (AT_string (name_attr));
7029 return;
7033 /* For all other references to another DIE, we check to see if the
7034 target DIE has already been visited. If it has, we emit a
7035 backward reference; if not, we descend recursively. */
7036 if (target_die->die_mark > 0)
7038 CHECKSUM_ULEB128 ('R');
7039 CHECKSUM_ULEB128 (at->dw_attr);
7040 CHECKSUM_ULEB128 (target_die->die_mark);
7042 else
7044 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7046 if (decl == NULL)
7047 decl = target_die;
7048 target_die->die_mark = ++(*mark);
7049 CHECKSUM_ULEB128 ('T');
7050 CHECKSUM_ULEB128 (at->dw_attr);
7051 if (decl->die_parent != NULL)
7052 checksum_die_context (decl->die_parent, ctx);
7053 die_checksum_ordered (target_die, ctx, mark);
7055 return;
7058 CHECKSUM_ULEB128 ('A');
7059 CHECKSUM_ULEB128 (at->dw_attr);
7061 switch (AT_class (at))
7063 case dw_val_class_const:
7064 case dw_val_class_const_implicit:
7065 CHECKSUM_ULEB128 (DW_FORM_sdata);
7066 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7067 break;
7069 case dw_val_class_unsigned_const:
7070 case dw_val_class_unsigned_const_implicit:
7071 CHECKSUM_ULEB128 (DW_FORM_sdata);
7072 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7073 break;
7075 case dw_val_class_const_double:
7076 CHECKSUM_ULEB128 (DW_FORM_block);
7077 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7078 CHECKSUM (at->dw_attr_val.v.val_double);
7079 break;
7081 case dw_val_class_wide_int:
7082 CHECKSUM_ULEB128 (DW_FORM_block);
7083 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7084 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7085 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7086 get_full_len (*at->dw_attr_val.v.val_wide)
7087 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7088 break;
7090 case dw_val_class_vec:
7091 CHECKSUM_ULEB128 (DW_FORM_block);
7092 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7093 * at->dw_attr_val.v.val_vec.elt_size);
7094 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7095 (at->dw_attr_val.v.val_vec.length
7096 * at->dw_attr_val.v.val_vec.elt_size));
7097 break;
7099 case dw_val_class_flag:
7100 CHECKSUM_ULEB128 (DW_FORM_flag);
7101 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7102 break;
7104 case dw_val_class_str:
7105 CHECKSUM_ULEB128 (DW_FORM_string);
7106 CHECKSUM_STRING (AT_string (at));
7107 break;
7109 case dw_val_class_addr:
7110 r = AT_addr (at);
7111 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7112 CHECKSUM_ULEB128 (DW_FORM_string);
7113 CHECKSUM_STRING (XSTR (r, 0));
7114 break;
7116 case dw_val_class_offset:
7117 CHECKSUM_ULEB128 (DW_FORM_sdata);
7118 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7119 break;
7121 case dw_val_class_loc:
7122 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7123 loc_checksum_ordered (loc, ctx);
7124 break;
7126 case dw_val_class_fde_ref:
7127 case dw_val_class_lbl_id:
7128 case dw_val_class_lineptr:
7129 case dw_val_class_macptr:
7130 case dw_val_class_loclistsptr:
7131 case dw_val_class_high_pc:
7132 break;
7134 case dw_val_class_file:
7135 case dw_val_class_file_implicit:
7136 CHECKSUM_ULEB128 (DW_FORM_string);
7137 CHECKSUM_STRING (AT_file (at)->filename);
7138 break;
7140 case dw_val_class_data8:
7141 CHECKSUM (at->dw_attr_val.v.val_data8);
7142 break;
7144 default:
7145 break;
7149 struct checksum_attributes
7151 dw_attr_node *at_name;
7152 dw_attr_node *at_type;
7153 dw_attr_node *at_friend;
7154 dw_attr_node *at_accessibility;
7155 dw_attr_node *at_address_class;
7156 dw_attr_node *at_alignment;
7157 dw_attr_node *at_allocated;
7158 dw_attr_node *at_artificial;
7159 dw_attr_node *at_associated;
7160 dw_attr_node *at_binary_scale;
7161 dw_attr_node *at_bit_offset;
7162 dw_attr_node *at_bit_size;
7163 dw_attr_node *at_bit_stride;
7164 dw_attr_node *at_byte_size;
7165 dw_attr_node *at_byte_stride;
7166 dw_attr_node *at_const_value;
7167 dw_attr_node *at_containing_type;
7168 dw_attr_node *at_count;
7169 dw_attr_node *at_data_location;
7170 dw_attr_node *at_data_member_location;
7171 dw_attr_node *at_decimal_scale;
7172 dw_attr_node *at_decimal_sign;
7173 dw_attr_node *at_default_value;
7174 dw_attr_node *at_digit_count;
7175 dw_attr_node *at_discr;
7176 dw_attr_node *at_discr_list;
7177 dw_attr_node *at_discr_value;
7178 dw_attr_node *at_encoding;
7179 dw_attr_node *at_endianity;
7180 dw_attr_node *at_explicit;
7181 dw_attr_node *at_is_optional;
7182 dw_attr_node *at_location;
7183 dw_attr_node *at_lower_bound;
7184 dw_attr_node *at_mutable;
7185 dw_attr_node *at_ordering;
7186 dw_attr_node *at_picture_string;
7187 dw_attr_node *at_prototyped;
7188 dw_attr_node *at_small;
7189 dw_attr_node *at_segment;
7190 dw_attr_node *at_string_length;
7191 dw_attr_node *at_string_length_bit_size;
7192 dw_attr_node *at_string_length_byte_size;
7193 dw_attr_node *at_threads_scaled;
7194 dw_attr_node *at_upper_bound;
7195 dw_attr_node *at_use_location;
7196 dw_attr_node *at_use_UTF8;
7197 dw_attr_node *at_variable_parameter;
7198 dw_attr_node *at_virtuality;
7199 dw_attr_node *at_visibility;
7200 dw_attr_node *at_vtable_elem_location;
7203 /* Collect the attributes that we will want to use for the checksum. */
7205 static void
7206 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7208 dw_attr_node *a;
7209 unsigned ix;
7211 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7213 switch (a->dw_attr)
7215 case DW_AT_name:
7216 attrs->at_name = a;
7217 break;
7218 case DW_AT_type:
7219 attrs->at_type = a;
7220 break;
7221 case DW_AT_friend:
7222 attrs->at_friend = a;
7223 break;
7224 case DW_AT_accessibility:
7225 attrs->at_accessibility = a;
7226 break;
7227 case DW_AT_address_class:
7228 attrs->at_address_class = a;
7229 break;
7230 case DW_AT_alignment:
7231 attrs->at_alignment = a;
7232 break;
7233 case DW_AT_allocated:
7234 attrs->at_allocated = a;
7235 break;
7236 case DW_AT_artificial:
7237 attrs->at_artificial = a;
7238 break;
7239 case DW_AT_associated:
7240 attrs->at_associated = a;
7241 break;
7242 case DW_AT_binary_scale:
7243 attrs->at_binary_scale = a;
7244 break;
7245 case DW_AT_bit_offset:
7246 attrs->at_bit_offset = a;
7247 break;
7248 case DW_AT_bit_size:
7249 attrs->at_bit_size = a;
7250 break;
7251 case DW_AT_bit_stride:
7252 attrs->at_bit_stride = a;
7253 break;
7254 case DW_AT_byte_size:
7255 attrs->at_byte_size = a;
7256 break;
7257 case DW_AT_byte_stride:
7258 attrs->at_byte_stride = a;
7259 break;
7260 case DW_AT_const_value:
7261 attrs->at_const_value = a;
7262 break;
7263 case DW_AT_containing_type:
7264 attrs->at_containing_type = a;
7265 break;
7266 case DW_AT_count:
7267 attrs->at_count = a;
7268 break;
7269 case DW_AT_data_location:
7270 attrs->at_data_location = a;
7271 break;
7272 case DW_AT_data_member_location:
7273 attrs->at_data_member_location = a;
7274 break;
7275 case DW_AT_decimal_scale:
7276 attrs->at_decimal_scale = a;
7277 break;
7278 case DW_AT_decimal_sign:
7279 attrs->at_decimal_sign = a;
7280 break;
7281 case DW_AT_default_value:
7282 attrs->at_default_value = a;
7283 break;
7284 case DW_AT_digit_count:
7285 attrs->at_digit_count = a;
7286 break;
7287 case DW_AT_discr:
7288 attrs->at_discr = a;
7289 break;
7290 case DW_AT_discr_list:
7291 attrs->at_discr_list = a;
7292 break;
7293 case DW_AT_discr_value:
7294 attrs->at_discr_value = a;
7295 break;
7296 case DW_AT_encoding:
7297 attrs->at_encoding = a;
7298 break;
7299 case DW_AT_endianity:
7300 attrs->at_endianity = a;
7301 break;
7302 case DW_AT_explicit:
7303 attrs->at_explicit = a;
7304 break;
7305 case DW_AT_is_optional:
7306 attrs->at_is_optional = a;
7307 break;
7308 case DW_AT_location:
7309 attrs->at_location = a;
7310 break;
7311 case DW_AT_lower_bound:
7312 attrs->at_lower_bound = a;
7313 break;
7314 case DW_AT_mutable:
7315 attrs->at_mutable = a;
7316 break;
7317 case DW_AT_ordering:
7318 attrs->at_ordering = a;
7319 break;
7320 case DW_AT_picture_string:
7321 attrs->at_picture_string = a;
7322 break;
7323 case DW_AT_prototyped:
7324 attrs->at_prototyped = a;
7325 break;
7326 case DW_AT_small:
7327 attrs->at_small = a;
7328 break;
7329 case DW_AT_segment:
7330 attrs->at_segment = a;
7331 break;
7332 case DW_AT_string_length:
7333 attrs->at_string_length = a;
7334 break;
7335 case DW_AT_string_length_bit_size:
7336 attrs->at_string_length_bit_size = a;
7337 break;
7338 case DW_AT_string_length_byte_size:
7339 attrs->at_string_length_byte_size = a;
7340 break;
7341 case DW_AT_threads_scaled:
7342 attrs->at_threads_scaled = a;
7343 break;
7344 case DW_AT_upper_bound:
7345 attrs->at_upper_bound = a;
7346 break;
7347 case DW_AT_use_location:
7348 attrs->at_use_location = a;
7349 break;
7350 case DW_AT_use_UTF8:
7351 attrs->at_use_UTF8 = a;
7352 break;
7353 case DW_AT_variable_parameter:
7354 attrs->at_variable_parameter = a;
7355 break;
7356 case DW_AT_virtuality:
7357 attrs->at_virtuality = a;
7358 break;
7359 case DW_AT_visibility:
7360 attrs->at_visibility = a;
7361 break;
7362 case DW_AT_vtable_elem_location:
7363 attrs->at_vtable_elem_location = a;
7364 break;
7365 default:
7366 break;
7371 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7373 static void
7374 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7376 dw_die_ref c;
7377 dw_die_ref decl;
7378 struct checksum_attributes attrs;
7380 CHECKSUM_ULEB128 ('D');
7381 CHECKSUM_ULEB128 (die->die_tag);
7383 memset (&attrs, 0, sizeof (attrs));
7385 decl = get_AT_ref (die, DW_AT_specification);
7386 if (decl != NULL)
7387 collect_checksum_attributes (&attrs, decl);
7388 collect_checksum_attributes (&attrs, die);
7390 CHECKSUM_ATTR (attrs.at_name);
7391 CHECKSUM_ATTR (attrs.at_accessibility);
7392 CHECKSUM_ATTR (attrs.at_address_class);
7393 CHECKSUM_ATTR (attrs.at_allocated);
7394 CHECKSUM_ATTR (attrs.at_artificial);
7395 CHECKSUM_ATTR (attrs.at_associated);
7396 CHECKSUM_ATTR (attrs.at_binary_scale);
7397 CHECKSUM_ATTR (attrs.at_bit_offset);
7398 CHECKSUM_ATTR (attrs.at_bit_size);
7399 CHECKSUM_ATTR (attrs.at_bit_stride);
7400 CHECKSUM_ATTR (attrs.at_byte_size);
7401 CHECKSUM_ATTR (attrs.at_byte_stride);
7402 CHECKSUM_ATTR (attrs.at_const_value);
7403 CHECKSUM_ATTR (attrs.at_containing_type);
7404 CHECKSUM_ATTR (attrs.at_count);
7405 CHECKSUM_ATTR (attrs.at_data_location);
7406 CHECKSUM_ATTR (attrs.at_data_member_location);
7407 CHECKSUM_ATTR (attrs.at_decimal_scale);
7408 CHECKSUM_ATTR (attrs.at_decimal_sign);
7409 CHECKSUM_ATTR (attrs.at_default_value);
7410 CHECKSUM_ATTR (attrs.at_digit_count);
7411 CHECKSUM_ATTR (attrs.at_discr);
7412 CHECKSUM_ATTR (attrs.at_discr_list);
7413 CHECKSUM_ATTR (attrs.at_discr_value);
7414 CHECKSUM_ATTR (attrs.at_encoding);
7415 CHECKSUM_ATTR (attrs.at_endianity);
7416 CHECKSUM_ATTR (attrs.at_explicit);
7417 CHECKSUM_ATTR (attrs.at_is_optional);
7418 CHECKSUM_ATTR (attrs.at_location);
7419 CHECKSUM_ATTR (attrs.at_lower_bound);
7420 CHECKSUM_ATTR (attrs.at_mutable);
7421 CHECKSUM_ATTR (attrs.at_ordering);
7422 CHECKSUM_ATTR (attrs.at_picture_string);
7423 CHECKSUM_ATTR (attrs.at_prototyped);
7424 CHECKSUM_ATTR (attrs.at_small);
7425 CHECKSUM_ATTR (attrs.at_segment);
7426 CHECKSUM_ATTR (attrs.at_string_length);
7427 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7428 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7429 CHECKSUM_ATTR (attrs.at_threads_scaled);
7430 CHECKSUM_ATTR (attrs.at_upper_bound);
7431 CHECKSUM_ATTR (attrs.at_use_location);
7432 CHECKSUM_ATTR (attrs.at_use_UTF8);
7433 CHECKSUM_ATTR (attrs.at_variable_parameter);
7434 CHECKSUM_ATTR (attrs.at_virtuality);
7435 CHECKSUM_ATTR (attrs.at_visibility);
7436 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7437 CHECKSUM_ATTR (attrs.at_type);
7438 CHECKSUM_ATTR (attrs.at_friend);
7439 CHECKSUM_ATTR (attrs.at_alignment);
7441 /* Checksum the child DIEs. */
7442 c = die->die_child;
7443 if (c) do {
7444 dw_attr_node *name_attr;
7446 c = c->die_sib;
7447 name_attr = get_AT (c, DW_AT_name);
7448 if (is_template_instantiation (c))
7450 /* Ignore instantiations of member type and function templates. */
7452 else if (name_attr != NULL
7453 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7455 /* Use a shallow checksum for named nested types and member
7456 functions. */
7457 CHECKSUM_ULEB128 ('S');
7458 CHECKSUM_ULEB128 (c->die_tag);
7459 CHECKSUM_STRING (AT_string (name_attr));
7461 else
7463 /* Use a deep checksum for other children. */
7464 /* Mark this DIE so it gets processed when unmarking. */
7465 if (c->die_mark == 0)
7466 c->die_mark = -1;
7467 die_checksum_ordered (c, ctx, mark);
7469 } while (c != die->die_child);
7471 CHECKSUM_ULEB128 (0);
7474 /* Add a type name and tag to a hash. */
7475 static void
7476 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7478 CHECKSUM_ULEB128 (tag);
7479 CHECKSUM_STRING (name);
7482 #undef CHECKSUM
7483 #undef CHECKSUM_STRING
7484 #undef CHECKSUM_ATTR
7485 #undef CHECKSUM_LEB128
7486 #undef CHECKSUM_ULEB128
7488 /* Generate the type signature for DIE. This is computed by generating an
7489 MD5 checksum over the DIE's tag, its relevant attributes, and its
7490 children. Attributes that are references to other DIEs are processed
7491 by recursion, using the MARK field to prevent infinite recursion.
7492 If the DIE is nested inside a namespace or another type, we also
7493 need to include that context in the signature. The lower 64 bits
7494 of the resulting MD5 checksum comprise the signature. */
7496 static void
7497 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7499 int mark;
7500 const char *name;
7501 unsigned char checksum[16];
7502 struct md5_ctx ctx;
7503 dw_die_ref decl;
7504 dw_die_ref parent;
7506 name = get_AT_string (die, DW_AT_name);
7507 decl = get_AT_ref (die, DW_AT_specification);
7508 parent = get_die_parent (die);
7510 /* First, compute a signature for just the type name (and its surrounding
7511 context, if any. This is stored in the type unit DIE for link-time
7512 ODR (one-definition rule) checking. */
7514 if (is_cxx () && name != NULL)
7516 md5_init_ctx (&ctx);
7518 /* Checksum the names of surrounding namespaces and structures. */
7519 if (parent != NULL)
7520 checksum_die_context (parent, &ctx);
7522 /* Checksum the current DIE. */
7523 die_odr_checksum (die->die_tag, name, &ctx);
7524 md5_finish_ctx (&ctx, checksum);
7526 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7529 /* Next, compute the complete type signature. */
7531 md5_init_ctx (&ctx);
7532 mark = 1;
7533 die->die_mark = mark;
7535 /* Checksum the names of surrounding namespaces and structures. */
7536 if (parent != NULL)
7537 checksum_die_context (parent, &ctx);
7539 /* Checksum the DIE and its children. */
7540 die_checksum_ordered (die, &ctx, &mark);
7541 unmark_all_dies (die);
7542 md5_finish_ctx (&ctx, checksum);
7544 /* Store the signature in the type node and link the type DIE and the
7545 type node together. */
7546 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7547 DWARF_TYPE_SIGNATURE_SIZE);
7548 die->comdat_type_p = true;
7549 die->die_id.die_type_node = type_node;
7550 type_node->type_die = die;
7552 /* If the DIE is a specification, link its declaration to the type node
7553 as well. */
7554 if (decl != NULL)
7556 decl->comdat_type_p = true;
7557 decl->die_id.die_type_node = type_node;
7561 /* Do the location expressions look same? */
7562 static inline int
7563 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7565 return loc1->dw_loc_opc == loc2->dw_loc_opc
7566 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7567 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7570 /* Do the values look the same? */
7571 static int
7572 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7574 dw_loc_descr_ref loc1, loc2;
7575 rtx r1, r2;
7577 if (v1->val_class != v2->val_class)
7578 return 0;
7580 switch (v1->val_class)
7582 case dw_val_class_const:
7583 case dw_val_class_const_implicit:
7584 return v1->v.val_int == v2->v.val_int;
7585 case dw_val_class_unsigned_const:
7586 case dw_val_class_unsigned_const_implicit:
7587 return v1->v.val_unsigned == v2->v.val_unsigned;
7588 case dw_val_class_const_double:
7589 return v1->v.val_double.high == v2->v.val_double.high
7590 && v1->v.val_double.low == v2->v.val_double.low;
7591 case dw_val_class_wide_int:
7592 return *v1->v.val_wide == *v2->v.val_wide;
7593 case dw_val_class_vec:
7594 if (v1->v.val_vec.length != v2->v.val_vec.length
7595 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7596 return 0;
7597 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7598 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7599 return 0;
7600 return 1;
7601 case dw_val_class_flag:
7602 return v1->v.val_flag == v2->v.val_flag;
7603 case dw_val_class_str:
7604 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7606 case dw_val_class_addr:
7607 r1 = v1->v.val_addr;
7608 r2 = v2->v.val_addr;
7609 if (GET_CODE (r1) != GET_CODE (r2))
7610 return 0;
7611 return !rtx_equal_p (r1, r2);
7613 case dw_val_class_offset:
7614 return v1->v.val_offset == v2->v.val_offset;
7616 case dw_val_class_loc:
7617 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7618 loc1 && loc2;
7619 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7620 if (!same_loc_p (loc1, loc2, mark))
7621 return 0;
7622 return !loc1 && !loc2;
7624 case dw_val_class_die_ref:
7625 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7627 case dw_val_class_fde_ref:
7628 case dw_val_class_vms_delta:
7629 case dw_val_class_lbl_id:
7630 case dw_val_class_lineptr:
7631 case dw_val_class_macptr:
7632 case dw_val_class_loclistsptr:
7633 case dw_val_class_high_pc:
7634 return 1;
7636 case dw_val_class_file:
7637 case dw_val_class_file_implicit:
7638 return v1->v.val_file == v2->v.val_file;
7640 case dw_val_class_data8:
7641 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7643 default:
7644 return 1;
7648 /* Do the attributes look the same? */
7650 static int
7651 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7653 if (at1->dw_attr != at2->dw_attr)
7654 return 0;
7656 /* We don't care that this was compiled with a different compiler
7657 snapshot; if the output is the same, that's what matters. */
7658 if (at1->dw_attr == DW_AT_producer)
7659 return 1;
7661 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7664 /* Do the dies look the same? */
7666 static int
7667 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7669 dw_die_ref c1, c2;
7670 dw_attr_node *a1;
7671 unsigned ix;
7673 /* To avoid infinite recursion. */
7674 if (die1->die_mark)
7675 return die1->die_mark == die2->die_mark;
7676 die1->die_mark = die2->die_mark = ++(*mark);
7678 if (die1->die_tag != die2->die_tag)
7679 return 0;
7681 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7682 return 0;
7684 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7685 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7686 return 0;
7688 c1 = die1->die_child;
7689 c2 = die2->die_child;
7690 if (! c1)
7692 if (c2)
7693 return 0;
7695 else
7696 for (;;)
7698 if (!same_die_p (c1, c2, mark))
7699 return 0;
7700 c1 = c1->die_sib;
7701 c2 = c2->die_sib;
7702 if (c1 == die1->die_child)
7704 if (c2 == die2->die_child)
7705 break;
7706 else
7707 return 0;
7711 return 1;
7714 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7715 children, and set die_symbol. */
7717 static void
7718 compute_comp_unit_symbol (dw_die_ref unit_die)
7720 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7721 const char *base = die_name ? lbasename (die_name) : "anonymous";
7722 char *name = XALLOCAVEC (char, strlen (base) + 64);
7723 char *p;
7724 int i, mark;
7725 unsigned char checksum[16];
7726 struct md5_ctx ctx;
7728 /* Compute the checksum of the DIE, then append part of it as hex digits to
7729 the name filename of the unit. */
7731 md5_init_ctx (&ctx);
7732 mark = 0;
7733 die_checksum (unit_die, &ctx, &mark);
7734 unmark_all_dies (unit_die);
7735 md5_finish_ctx (&ctx, checksum);
7737 /* When we this for comp_unit_die () we have a DW_AT_name that might
7738 not start with a letter but with anything valid for filenames and
7739 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7740 character is not a letter. */
7741 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7742 clean_symbol_name (name);
7744 p = name + strlen (name);
7745 for (i = 0; i < 4; i++)
7747 sprintf (p, "%.2x", checksum[i]);
7748 p += 2;
7751 unit_die->die_id.die_symbol = xstrdup (name);
7754 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7756 static int
7757 is_type_die (dw_die_ref die)
7759 switch (die->die_tag)
7761 case DW_TAG_array_type:
7762 case DW_TAG_class_type:
7763 case DW_TAG_interface_type:
7764 case DW_TAG_enumeration_type:
7765 case DW_TAG_pointer_type:
7766 case DW_TAG_reference_type:
7767 case DW_TAG_rvalue_reference_type:
7768 case DW_TAG_string_type:
7769 case DW_TAG_structure_type:
7770 case DW_TAG_subroutine_type:
7771 case DW_TAG_union_type:
7772 case DW_TAG_ptr_to_member_type:
7773 case DW_TAG_set_type:
7774 case DW_TAG_subrange_type:
7775 case DW_TAG_base_type:
7776 case DW_TAG_const_type:
7777 case DW_TAG_file_type:
7778 case DW_TAG_packed_type:
7779 case DW_TAG_volatile_type:
7780 case DW_TAG_typedef:
7781 return 1;
7782 default:
7783 return 0;
7787 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7788 Basically, we want to choose the bits that are likely to be shared between
7789 compilations (types) and leave out the bits that are specific to individual
7790 compilations (functions). */
7792 static int
7793 is_comdat_die (dw_die_ref c)
7795 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7796 we do for stabs. The advantage is a greater likelihood of sharing between
7797 objects that don't include headers in the same order (and therefore would
7798 put the base types in a different comdat). jason 8/28/00 */
7800 if (c->die_tag == DW_TAG_base_type)
7801 return 0;
7803 if (c->die_tag == DW_TAG_pointer_type
7804 || c->die_tag == DW_TAG_reference_type
7805 || c->die_tag == DW_TAG_rvalue_reference_type
7806 || c->die_tag == DW_TAG_const_type
7807 || c->die_tag == DW_TAG_volatile_type)
7809 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7811 return t ? is_comdat_die (t) : 0;
7814 return is_type_die (c);
7817 /* Returns true iff C is a compile-unit DIE. */
7819 static inline bool
7820 is_cu_die (dw_die_ref c)
7822 return c && (c->die_tag == DW_TAG_compile_unit
7823 || c->die_tag == DW_TAG_skeleton_unit);
7826 /* Returns true iff C is a unit DIE of some sort. */
7828 static inline bool
7829 is_unit_die (dw_die_ref c)
7831 return c && (c->die_tag == DW_TAG_compile_unit
7832 || c->die_tag == DW_TAG_partial_unit
7833 || c->die_tag == DW_TAG_type_unit
7834 || c->die_tag == DW_TAG_skeleton_unit);
7837 /* Returns true iff C is a namespace DIE. */
7839 static inline bool
7840 is_namespace_die (dw_die_ref c)
7842 return c && c->die_tag == DW_TAG_namespace;
7845 /* Returns true iff C is a class or structure DIE. */
7847 static inline bool
7848 is_class_die (dw_die_ref c)
7850 return c && (c->die_tag == DW_TAG_class_type
7851 || c->die_tag == DW_TAG_structure_type);
7854 /* Return non-zero if this DIE is a template parameter. */
7856 static inline bool
7857 is_template_parameter (dw_die_ref die)
7859 switch (die->die_tag)
7861 case DW_TAG_template_type_param:
7862 case DW_TAG_template_value_param:
7863 case DW_TAG_GNU_template_template_param:
7864 case DW_TAG_GNU_template_parameter_pack:
7865 return true;
7866 default:
7867 return false;
7871 /* Return non-zero if this DIE represents a template instantiation. */
7873 static inline bool
7874 is_template_instantiation (dw_die_ref die)
7876 dw_die_ref c;
7878 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7879 return false;
7880 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7881 return false;
7884 static char *
7885 gen_internal_sym (const char *prefix)
7887 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7889 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7890 return xstrdup (buf);
7893 /* Return non-zero if this DIE is a declaration. */
7895 static int
7896 is_declaration_die (dw_die_ref die)
7898 dw_attr_node *a;
7899 unsigned ix;
7901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7902 if (a->dw_attr == DW_AT_declaration)
7903 return 1;
7905 return 0;
7908 /* Return non-zero if this DIE is nested inside a subprogram. */
7910 static int
7911 is_nested_in_subprogram (dw_die_ref die)
7913 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7915 if (decl == NULL)
7916 decl = die;
7917 return local_scope_p (decl);
7920 /* Return non-zero if this DIE contains a defining declaration of a
7921 subprogram. */
7923 static int
7924 contains_subprogram_definition (dw_die_ref die)
7926 dw_die_ref c;
7928 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7929 return 1;
7930 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7931 return 0;
7934 /* Return non-zero if this is a type DIE that should be moved to a
7935 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7936 unit type. */
7938 static int
7939 should_move_die_to_comdat (dw_die_ref die)
7941 switch (die->die_tag)
7943 case DW_TAG_class_type:
7944 case DW_TAG_structure_type:
7945 case DW_TAG_enumeration_type:
7946 case DW_TAG_union_type:
7947 /* Don't move declarations, inlined instances, types nested in a
7948 subprogram, or types that contain subprogram definitions. */
7949 if (is_declaration_die (die)
7950 || get_AT (die, DW_AT_abstract_origin)
7951 || is_nested_in_subprogram (die)
7952 || contains_subprogram_definition (die))
7953 return 0;
7954 return 1;
7955 case DW_TAG_array_type:
7956 case DW_TAG_interface_type:
7957 case DW_TAG_pointer_type:
7958 case DW_TAG_reference_type:
7959 case DW_TAG_rvalue_reference_type:
7960 case DW_TAG_string_type:
7961 case DW_TAG_subroutine_type:
7962 case DW_TAG_ptr_to_member_type:
7963 case DW_TAG_set_type:
7964 case DW_TAG_subrange_type:
7965 case DW_TAG_base_type:
7966 case DW_TAG_const_type:
7967 case DW_TAG_file_type:
7968 case DW_TAG_packed_type:
7969 case DW_TAG_volatile_type:
7970 case DW_TAG_typedef:
7971 default:
7972 return 0;
7976 /* Make a clone of DIE. */
7978 static dw_die_ref
7979 clone_die (dw_die_ref die)
7981 dw_die_ref clone = new_die_raw (die->die_tag);
7982 dw_attr_node *a;
7983 unsigned ix;
7985 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7986 add_dwarf_attr (clone, a);
7988 return clone;
7991 /* Make a clone of the tree rooted at DIE. */
7993 static dw_die_ref
7994 clone_tree (dw_die_ref die)
7996 dw_die_ref c;
7997 dw_die_ref clone = clone_die (die);
7999 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8001 return clone;
8004 /* Make a clone of DIE as a declaration. */
8006 static dw_die_ref
8007 clone_as_declaration (dw_die_ref die)
8009 dw_die_ref clone;
8010 dw_die_ref decl;
8011 dw_attr_node *a;
8012 unsigned ix;
8014 /* If the DIE is already a declaration, just clone it. */
8015 if (is_declaration_die (die))
8016 return clone_die (die);
8018 /* If the DIE is a specification, just clone its declaration DIE. */
8019 decl = get_AT_ref (die, DW_AT_specification);
8020 if (decl != NULL)
8022 clone = clone_die (decl);
8023 if (die->comdat_type_p)
8024 add_AT_die_ref (clone, DW_AT_signature, die);
8025 return clone;
8028 clone = new_die_raw (die->die_tag);
8030 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8032 /* We don't want to copy over all attributes.
8033 For example we don't want DW_AT_byte_size because otherwise we will no
8034 longer have a declaration and GDB will treat it as a definition. */
8036 switch (a->dw_attr)
8038 case DW_AT_abstract_origin:
8039 case DW_AT_artificial:
8040 case DW_AT_containing_type:
8041 case DW_AT_external:
8042 case DW_AT_name:
8043 case DW_AT_type:
8044 case DW_AT_virtuality:
8045 case DW_AT_linkage_name:
8046 case DW_AT_MIPS_linkage_name:
8047 add_dwarf_attr (clone, a);
8048 break;
8049 case DW_AT_byte_size:
8050 case DW_AT_alignment:
8051 default:
8052 break;
8056 if (die->comdat_type_p)
8057 add_AT_die_ref (clone, DW_AT_signature, die);
8059 add_AT_flag (clone, DW_AT_declaration, 1);
8060 return clone;
8064 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8066 struct decl_table_entry
8068 dw_die_ref orig;
8069 dw_die_ref copy;
8072 /* Helpers to manipulate hash table of copied declarations. */
8074 /* Hashtable helpers. */
8076 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8078 typedef die_struct *compare_type;
8079 static inline hashval_t hash (const decl_table_entry *);
8080 static inline bool equal (const decl_table_entry *, const die_struct *);
8083 inline hashval_t
8084 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8086 return htab_hash_pointer (entry->orig);
8089 inline bool
8090 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8091 const die_struct *entry2)
8093 return entry1->orig == entry2;
8096 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8098 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8099 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8100 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8101 to check if the ancestor has already been copied into UNIT. */
8103 static dw_die_ref
8104 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8105 decl_hash_type *decl_table)
8107 dw_die_ref parent = die->die_parent;
8108 dw_die_ref new_parent = unit;
8109 dw_die_ref copy;
8110 decl_table_entry **slot = NULL;
8111 struct decl_table_entry *entry = NULL;
8113 if (decl_table)
8115 /* Check if the entry has already been copied to UNIT. */
8116 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8117 INSERT);
8118 if (*slot != HTAB_EMPTY_ENTRY)
8120 entry = *slot;
8121 return entry->copy;
8124 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8125 entry = XCNEW (struct decl_table_entry);
8126 entry->orig = die;
8127 entry->copy = NULL;
8128 *slot = entry;
8131 if (parent != NULL)
8133 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8134 if (spec != NULL)
8135 parent = spec;
8136 if (!is_unit_die (parent))
8137 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8140 copy = clone_as_declaration (die);
8141 add_child_die (new_parent, copy);
8143 if (decl_table)
8145 /* Record the pointer to the copy. */
8146 entry->copy = copy;
8149 return copy;
8151 /* Copy the declaration context to the new type unit DIE. This includes
8152 any surrounding namespace or type declarations. If the DIE has an
8153 AT_specification attribute, it also includes attributes and children
8154 attached to the specification, and returns a pointer to the original
8155 parent of the declaration DIE. Returns NULL otherwise. */
8157 static dw_die_ref
8158 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8160 dw_die_ref decl;
8161 dw_die_ref new_decl;
8162 dw_die_ref orig_parent = NULL;
8164 decl = get_AT_ref (die, DW_AT_specification);
8165 if (decl == NULL)
8166 decl = die;
8167 else
8169 unsigned ix;
8170 dw_die_ref c;
8171 dw_attr_node *a;
8173 /* The original DIE will be changed to a declaration, and must
8174 be moved to be a child of the original declaration DIE. */
8175 orig_parent = decl->die_parent;
8177 /* Copy the type node pointer from the new DIE to the original
8178 declaration DIE so we can forward references later. */
8179 decl->comdat_type_p = true;
8180 decl->die_id.die_type_node = die->die_id.die_type_node;
8182 remove_AT (die, DW_AT_specification);
8184 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8186 if (a->dw_attr != DW_AT_name
8187 && a->dw_attr != DW_AT_declaration
8188 && a->dw_attr != DW_AT_external)
8189 add_dwarf_attr (die, a);
8192 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8195 if (decl->die_parent != NULL
8196 && !is_unit_die (decl->die_parent))
8198 new_decl = copy_ancestor_tree (unit, decl, NULL);
8199 if (new_decl != NULL)
8201 remove_AT (new_decl, DW_AT_signature);
8202 add_AT_specification (die, new_decl);
8206 return orig_parent;
8209 /* Generate the skeleton ancestor tree for the given NODE, then clone
8210 the DIE and add the clone into the tree. */
8212 static void
8213 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8215 if (node->new_die != NULL)
8216 return;
8218 node->new_die = clone_as_declaration (node->old_die);
8220 if (node->parent != NULL)
8222 generate_skeleton_ancestor_tree (node->parent);
8223 add_child_die (node->parent->new_die, node->new_die);
8227 /* Generate a skeleton tree of DIEs containing any declarations that are
8228 found in the original tree. We traverse the tree looking for declaration
8229 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8231 static void
8232 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8234 skeleton_chain_node node;
8235 dw_die_ref c;
8236 dw_die_ref first;
8237 dw_die_ref prev = NULL;
8238 dw_die_ref next = NULL;
8240 node.parent = parent;
8242 first = c = parent->old_die->die_child;
8243 if (c)
8244 next = c->die_sib;
8245 if (c) do {
8246 if (prev == NULL || prev->die_sib == c)
8247 prev = c;
8248 c = next;
8249 next = (c == first ? NULL : c->die_sib);
8250 node.old_die = c;
8251 node.new_die = NULL;
8252 if (is_declaration_die (c))
8254 if (is_template_instantiation (c))
8256 /* Instantiated templates do not need to be cloned into the
8257 type unit. Just move the DIE and its children back to
8258 the skeleton tree (in the main CU). */
8259 remove_child_with_prev (c, prev);
8260 add_child_die (parent->new_die, c);
8261 c = prev;
8263 else if (c->comdat_type_p)
8265 /* This is the skeleton of earlier break_out_comdat_types
8266 type. Clone the existing DIE, but keep the children
8267 under the original (which is in the main CU). */
8268 dw_die_ref clone = clone_die (c);
8270 replace_child (c, clone, prev);
8271 generate_skeleton_ancestor_tree (parent);
8272 add_child_die (parent->new_die, c);
8273 c = clone;
8274 continue;
8276 else
8278 /* Clone the existing DIE, move the original to the skeleton
8279 tree (which is in the main CU), and put the clone, with
8280 all the original's children, where the original came from
8281 (which is about to be moved to the type unit). */
8282 dw_die_ref clone = clone_die (c);
8283 move_all_children (c, clone);
8285 /* If the original has a DW_AT_object_pointer attribute,
8286 it would now point to a child DIE just moved to the
8287 cloned tree, so we need to remove that attribute from
8288 the original. */
8289 remove_AT (c, DW_AT_object_pointer);
8291 replace_child (c, clone, prev);
8292 generate_skeleton_ancestor_tree (parent);
8293 add_child_die (parent->new_die, c);
8294 node.old_die = clone;
8295 node.new_die = c;
8296 c = clone;
8299 generate_skeleton_bottom_up (&node);
8300 } while (next != NULL);
8303 /* Wrapper function for generate_skeleton_bottom_up. */
8305 static dw_die_ref
8306 generate_skeleton (dw_die_ref die)
8308 skeleton_chain_node node;
8310 node.old_die = die;
8311 node.new_die = NULL;
8312 node.parent = NULL;
8314 /* If this type definition is nested inside another type,
8315 and is not an instantiation of a template, always leave
8316 at least a declaration in its place. */
8317 if (die->die_parent != NULL
8318 && is_type_die (die->die_parent)
8319 && !is_template_instantiation (die))
8320 node.new_die = clone_as_declaration (die);
8322 generate_skeleton_bottom_up (&node);
8323 return node.new_die;
8326 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8327 declaration. The original DIE is moved to a new compile unit so that
8328 existing references to it follow it to the new location. If any of the
8329 original DIE's descendants is a declaration, we need to replace the
8330 original DIE with a skeleton tree and move the declarations back into the
8331 skeleton tree. */
8333 static dw_die_ref
8334 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8335 dw_die_ref prev)
8337 dw_die_ref skeleton, orig_parent;
8339 /* Copy the declaration context to the type unit DIE. If the returned
8340 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8341 that DIE. */
8342 orig_parent = copy_declaration_context (unit, child);
8344 skeleton = generate_skeleton (child);
8345 if (skeleton == NULL)
8346 remove_child_with_prev (child, prev);
8347 else
8349 skeleton->comdat_type_p = true;
8350 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8352 /* If the original DIE was a specification, we need to put
8353 the skeleton under the parent DIE of the declaration.
8354 This leaves the original declaration in the tree, but
8355 it will be pruned later since there are no longer any
8356 references to it. */
8357 if (orig_parent != NULL)
8359 remove_child_with_prev (child, prev);
8360 add_child_die (orig_parent, skeleton);
8362 else
8363 replace_child (child, skeleton, prev);
8366 return skeleton;
8369 static void
8370 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8371 comdat_type_node *type_node,
8372 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8374 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8375 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8376 DWARF procedure references in the DW_AT_location attribute. */
8378 static dw_die_ref
8379 copy_dwarf_procedure (dw_die_ref die,
8380 comdat_type_node *type_node,
8381 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8383 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8385 /* DWARF procedures are not supposed to have children... */
8386 gcc_assert (die->die_child == NULL);
8388 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8389 gcc_assert (vec_safe_length (die->die_attr) == 1
8390 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8392 /* Do not copy more than once DWARF procedures. */
8393 bool existed;
8394 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8395 if (existed)
8396 return die_copy;
8398 die_copy = clone_die (die);
8399 add_child_die (type_node->root_die, die_copy);
8400 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8401 return die_copy;
8404 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8405 procedures in DIE's attributes. */
8407 static void
8408 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8409 comdat_type_node *type_node,
8410 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8412 dw_attr_node *a;
8413 unsigned i;
8415 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8417 dw_loc_descr_ref loc;
8419 if (a->dw_attr_val.val_class != dw_val_class_loc)
8420 continue;
8422 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8424 switch (loc->dw_loc_opc)
8426 case DW_OP_call2:
8427 case DW_OP_call4:
8428 case DW_OP_call_ref:
8429 gcc_assert (loc->dw_loc_oprnd1.val_class
8430 == dw_val_class_die_ref);
8431 loc->dw_loc_oprnd1.v.val_die_ref.die
8432 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8433 type_node,
8434 copied_dwarf_procs);
8436 default:
8437 break;
8443 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8444 rewrite references to point to the copies.
8446 References are looked for in DIE's attributes and recursively in all its
8447 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8448 mapping from old DWARF procedures to their copy. It is used not to copy
8449 twice the same DWARF procedure under TYPE_NODE. */
8451 static void
8452 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8453 comdat_type_node *type_node,
8454 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8456 dw_die_ref c;
8458 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8459 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8460 type_node,
8461 copied_dwarf_procs));
8464 /* Traverse the DIE and set up additional .debug_types or .debug_info
8465 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8466 section. */
8468 static void
8469 break_out_comdat_types (dw_die_ref die)
8471 dw_die_ref c;
8472 dw_die_ref first;
8473 dw_die_ref prev = NULL;
8474 dw_die_ref next = NULL;
8475 dw_die_ref unit = NULL;
8477 first = c = die->die_child;
8478 if (c)
8479 next = c->die_sib;
8480 if (c) do {
8481 if (prev == NULL || prev->die_sib == c)
8482 prev = c;
8483 c = next;
8484 next = (c == first ? NULL : c->die_sib);
8485 if (should_move_die_to_comdat (c))
8487 dw_die_ref replacement;
8488 comdat_type_node *type_node;
8490 /* Break out nested types into their own type units. */
8491 break_out_comdat_types (c);
8493 /* Create a new type unit DIE as the root for the new tree, and
8494 add it to the list of comdat types. */
8495 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8496 add_AT_unsigned (unit, DW_AT_language,
8497 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8498 type_node = ggc_cleared_alloc<comdat_type_node> ();
8499 type_node->root_die = unit;
8500 type_node->next = comdat_type_list;
8501 comdat_type_list = type_node;
8503 /* Generate the type signature. */
8504 generate_type_signature (c, type_node);
8506 /* Copy the declaration context, attributes, and children of the
8507 declaration into the new type unit DIE, then remove this DIE
8508 from the main CU (or replace it with a skeleton if necessary). */
8509 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8510 type_node->skeleton_die = replacement;
8512 /* Add the DIE to the new compunit. */
8513 add_child_die (unit, c);
8515 /* Types can reference DWARF procedures for type size or data location
8516 expressions. Calls in DWARF expressions cannot target procedures
8517 that are not in the same section. So we must copy DWARF procedures
8518 along with this type and then rewrite references to them. */
8519 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8520 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8522 if (replacement != NULL)
8523 c = replacement;
8525 else if (c->die_tag == DW_TAG_namespace
8526 || c->die_tag == DW_TAG_class_type
8527 || c->die_tag == DW_TAG_structure_type
8528 || c->die_tag == DW_TAG_union_type)
8530 /* Look for nested types that can be broken out. */
8531 break_out_comdat_types (c);
8533 } while (next != NULL);
8536 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8537 Enter all the cloned children into the hash table decl_table. */
8539 static dw_die_ref
8540 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8542 dw_die_ref c;
8543 dw_die_ref clone;
8544 struct decl_table_entry *entry;
8545 decl_table_entry **slot;
8547 if (die->die_tag == DW_TAG_subprogram)
8548 clone = clone_as_declaration (die);
8549 else
8550 clone = clone_die (die);
8552 slot = decl_table->find_slot_with_hash (die,
8553 htab_hash_pointer (die), INSERT);
8555 /* Assert that DIE isn't in the hash table yet. If it would be there
8556 before, the ancestors would be necessarily there as well, therefore
8557 clone_tree_partial wouldn't be called. */
8558 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8560 entry = XCNEW (struct decl_table_entry);
8561 entry->orig = die;
8562 entry->copy = clone;
8563 *slot = entry;
8565 if (die->die_tag != DW_TAG_subprogram)
8566 FOR_EACH_CHILD (die, c,
8567 add_child_die (clone, clone_tree_partial (c, decl_table)));
8569 return clone;
8572 /* Walk the DIE and its children, looking for references to incomplete
8573 or trivial types that are unmarked (i.e., that are not in the current
8574 type_unit). */
8576 static void
8577 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8579 dw_die_ref c;
8580 dw_attr_node *a;
8581 unsigned ix;
8583 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8585 if (AT_class (a) == dw_val_class_die_ref)
8587 dw_die_ref targ = AT_ref (a);
8588 decl_table_entry **slot;
8589 struct decl_table_entry *entry;
8591 if (targ->die_mark != 0 || targ->comdat_type_p)
8592 continue;
8594 slot = decl_table->find_slot_with_hash (targ,
8595 htab_hash_pointer (targ),
8596 INSERT);
8598 if (*slot != HTAB_EMPTY_ENTRY)
8600 /* TARG has already been copied, so we just need to
8601 modify the reference to point to the copy. */
8602 entry = *slot;
8603 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8605 else
8607 dw_die_ref parent = unit;
8608 dw_die_ref copy = clone_die (targ);
8610 /* Record in DECL_TABLE that TARG has been copied.
8611 Need to do this now, before the recursive call,
8612 because DECL_TABLE may be expanded and SLOT
8613 would no longer be a valid pointer. */
8614 entry = XCNEW (struct decl_table_entry);
8615 entry->orig = targ;
8616 entry->copy = copy;
8617 *slot = entry;
8619 /* If TARG is not a declaration DIE, we need to copy its
8620 children. */
8621 if (!is_declaration_die (targ))
8623 FOR_EACH_CHILD (
8624 targ, c,
8625 add_child_die (copy,
8626 clone_tree_partial (c, decl_table)));
8629 /* Make sure the cloned tree is marked as part of the
8630 type unit. */
8631 mark_dies (copy);
8633 /* If TARG has surrounding context, copy its ancestor tree
8634 into the new type unit. */
8635 if (targ->die_parent != NULL
8636 && !is_unit_die (targ->die_parent))
8637 parent = copy_ancestor_tree (unit, targ->die_parent,
8638 decl_table);
8640 add_child_die (parent, copy);
8641 a->dw_attr_val.v.val_die_ref.die = copy;
8643 /* Make sure the newly-copied DIE is walked. If it was
8644 installed in a previously-added context, it won't
8645 get visited otherwise. */
8646 if (parent != unit)
8648 /* Find the highest point of the newly-added tree,
8649 mark each node along the way, and walk from there. */
8650 parent->die_mark = 1;
8651 while (parent->die_parent
8652 && parent->die_parent->die_mark == 0)
8654 parent = parent->die_parent;
8655 parent->die_mark = 1;
8657 copy_decls_walk (unit, parent, decl_table);
8663 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8666 /* Copy declarations for "unworthy" types into the new comdat section.
8667 Incomplete types, modified types, and certain other types aren't broken
8668 out into comdat sections of their own, so they don't have a signature,
8669 and we need to copy the declaration into the same section so that we
8670 don't have an external reference. */
8672 static void
8673 copy_decls_for_unworthy_types (dw_die_ref unit)
8675 mark_dies (unit);
8676 decl_hash_type decl_table (10);
8677 copy_decls_walk (unit, unit, &decl_table);
8678 unmark_dies (unit);
8681 /* Traverse the DIE and add a sibling attribute if it may have the
8682 effect of speeding up access to siblings. To save some space,
8683 avoid generating sibling attributes for DIE's without children. */
8685 static void
8686 add_sibling_attributes (dw_die_ref die)
8688 dw_die_ref c;
8690 if (! die->die_child)
8691 return;
8693 if (die->die_parent && die != die->die_parent->die_child)
8694 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8696 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8699 /* Output all location lists for the DIE and its children. */
8701 static void
8702 output_location_lists (dw_die_ref die)
8704 dw_die_ref c;
8705 dw_attr_node *a;
8706 unsigned ix;
8708 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8709 if (AT_class (a) == dw_val_class_loc_list)
8710 output_loc_list (AT_loc_list (a));
8712 FOR_EACH_CHILD (die, c, output_location_lists (c));
8715 /* During assign_location_list_indexes and output_loclists_offset the
8716 current index, after it the number of assigned indexes (i.e. how
8717 large the .debug_loclists* offset table should be). */
8718 static unsigned int loc_list_idx;
8720 /* Output all location list offsets for the DIE and its children. */
8722 static void
8723 output_loclists_offsets (dw_die_ref die)
8725 dw_die_ref c;
8726 dw_attr_node *a;
8727 unsigned ix;
8729 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8730 if (AT_class (a) == dw_val_class_loc_list)
8732 dw_loc_list_ref l = AT_loc_list (a);
8733 if (l->offset_emitted)
8734 continue;
8735 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8736 loc_section_label, NULL);
8737 gcc_assert (l->hash == loc_list_idx);
8738 loc_list_idx++;
8739 l->offset_emitted = true;
8742 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8745 /* Recursively set indexes of location lists. */
8747 static void
8748 assign_location_list_indexes (dw_die_ref die)
8750 dw_die_ref c;
8751 dw_attr_node *a;
8752 unsigned ix;
8754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8755 if (AT_class (a) == dw_val_class_loc_list)
8757 dw_loc_list_ref list = AT_loc_list (a);
8758 if (!list->num_assigned)
8760 list->num_assigned = true;
8761 list->hash = loc_list_idx++;
8765 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8768 /* We want to limit the number of external references, because they are
8769 larger than local references: a relocation takes multiple words, and
8770 even a sig8 reference is always eight bytes, whereas a local reference
8771 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8772 So if we encounter multiple external references to the same type DIE, we
8773 make a local typedef stub for it and redirect all references there.
8775 This is the element of the hash table for keeping track of these
8776 references. */
8778 struct external_ref
8780 dw_die_ref type;
8781 dw_die_ref stub;
8782 unsigned n_refs;
8785 /* Hashtable helpers. */
8787 struct external_ref_hasher : free_ptr_hash <external_ref>
8789 static inline hashval_t hash (const external_ref *);
8790 static inline bool equal (const external_ref *, const external_ref *);
8793 inline hashval_t
8794 external_ref_hasher::hash (const external_ref *r)
8796 dw_die_ref die = r->type;
8797 hashval_t h = 0;
8799 /* We can't use the address of the DIE for hashing, because
8800 that will make the order of the stub DIEs non-deterministic. */
8801 if (! die->comdat_type_p)
8802 /* We have a symbol; use it to compute a hash. */
8803 h = htab_hash_string (die->die_id.die_symbol);
8804 else
8806 /* We have a type signature; use a subset of the bits as the hash.
8807 The 8-byte signature is at least as large as hashval_t. */
8808 comdat_type_node *type_node = die->die_id.die_type_node;
8809 memcpy (&h, type_node->signature, sizeof (h));
8811 return h;
8814 inline bool
8815 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8817 return r1->type == r2->type;
8820 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8822 /* Return a pointer to the external_ref for references to DIE. */
8824 static struct external_ref *
8825 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8827 struct external_ref ref, *ref_p;
8828 external_ref **slot;
8830 ref.type = die;
8831 slot = map->find_slot (&ref, INSERT);
8832 if (*slot != HTAB_EMPTY_ENTRY)
8833 return *slot;
8835 ref_p = XCNEW (struct external_ref);
8836 ref_p->type = die;
8837 *slot = ref_p;
8838 return ref_p;
8841 /* Subroutine of optimize_external_refs, below.
8843 If we see a type skeleton, record it as our stub. If we see external
8844 references, remember how many we've seen. */
8846 static void
8847 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8849 dw_die_ref c;
8850 dw_attr_node *a;
8851 unsigned ix;
8852 struct external_ref *ref_p;
8854 if (is_type_die (die)
8855 && (c = get_AT_ref (die, DW_AT_signature)))
8857 /* This is a local skeleton; use it for local references. */
8858 ref_p = lookup_external_ref (map, c);
8859 ref_p->stub = die;
8862 /* Scan the DIE references, and remember any that refer to DIEs from
8863 other CUs (i.e. those which are not marked). */
8864 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8865 if (AT_class (a) == dw_val_class_die_ref
8866 && (c = AT_ref (a))->die_mark == 0
8867 && is_type_die (c))
8869 ref_p = lookup_external_ref (map, c);
8870 ref_p->n_refs++;
8873 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8876 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8877 points to an external_ref, DATA is the CU we're processing. If we don't
8878 already have a local stub, and we have multiple refs, build a stub. */
8881 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8883 struct external_ref *ref_p = *slot;
8885 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8887 /* We have multiple references to this type, so build a small stub.
8888 Both of these forms are a bit dodgy from the perspective of the
8889 DWARF standard, since technically they should have names. */
8890 dw_die_ref cu = data;
8891 dw_die_ref type = ref_p->type;
8892 dw_die_ref stub = NULL;
8894 if (type->comdat_type_p)
8896 /* If we refer to this type via sig8, use AT_signature. */
8897 stub = new_die (type->die_tag, cu, NULL_TREE);
8898 add_AT_die_ref (stub, DW_AT_signature, type);
8900 else
8902 /* Otherwise, use a typedef with no name. */
8903 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8904 add_AT_die_ref (stub, DW_AT_type, type);
8907 stub->die_mark++;
8908 ref_p->stub = stub;
8910 return 1;
8913 /* DIE is a unit; look through all the DIE references to see if there are
8914 any external references to types, and if so, create local stubs for
8915 them which will be applied in build_abbrev_table. This is useful because
8916 references to local DIEs are smaller. */
8918 static external_ref_hash_type *
8919 optimize_external_refs (dw_die_ref die)
8921 external_ref_hash_type *map = new external_ref_hash_type (10);
8922 optimize_external_refs_1 (die, map);
8923 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8924 return map;
8927 /* The following 3 variables are temporaries that are computed only during the
8928 build_abbrev_table call and used and released during the following
8929 optimize_abbrev_table call. */
8931 /* First abbrev_id that can be optimized based on usage. */
8932 static unsigned int abbrev_opt_start;
8934 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8935 abbrev_id smaller than this, because they must be already sized
8936 during build_abbrev_table). */
8937 static unsigned int abbrev_opt_base_type_end;
8939 /* Vector of usage counts during build_abbrev_table. Indexed by
8940 abbrev_id - abbrev_opt_start. */
8941 static vec<unsigned int> abbrev_usage_count;
8943 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8944 static vec<dw_die_ref> sorted_abbrev_dies;
8946 /* The format of each DIE (and its attribute value pairs) is encoded in an
8947 abbreviation table. This routine builds the abbreviation table and assigns
8948 a unique abbreviation id for each abbreviation entry. The children of each
8949 die are visited recursively. */
8951 static void
8952 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8954 unsigned int abbrev_id = 0;
8955 dw_die_ref c;
8956 dw_attr_node *a;
8957 unsigned ix;
8958 dw_die_ref abbrev;
8960 /* Scan the DIE references, and replace any that refer to
8961 DIEs from other CUs (i.e. those which are not marked) with
8962 the local stubs we built in optimize_external_refs. */
8963 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8964 if (AT_class (a) == dw_val_class_die_ref
8965 && (c = AT_ref (a))->die_mark == 0)
8967 struct external_ref *ref_p;
8968 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8970 ref_p = lookup_external_ref (extern_map, c);
8971 if (ref_p->stub && ref_p->stub != die)
8972 change_AT_die_ref (a, ref_p->stub);
8973 else
8974 /* We aren't changing this reference, so mark it external. */
8975 set_AT_ref_external (a, 1);
8978 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8980 dw_attr_node *die_a, *abbrev_a;
8981 unsigned ix;
8982 bool ok = true;
8984 if (abbrev_id == 0)
8985 continue;
8986 if (abbrev->die_tag != die->die_tag)
8987 continue;
8988 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8989 continue;
8991 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8992 continue;
8994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8996 abbrev_a = &(*abbrev->die_attr)[ix];
8997 if ((abbrev_a->dw_attr != die_a->dw_attr)
8998 || (value_format (abbrev_a) != value_format (die_a)))
9000 ok = false;
9001 break;
9004 if (ok)
9005 break;
9008 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9010 vec_safe_push (abbrev_die_table, die);
9011 if (abbrev_opt_start)
9012 abbrev_usage_count.safe_push (0);
9014 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9016 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9017 sorted_abbrev_dies.safe_push (die);
9020 die->die_abbrev = abbrev_id;
9021 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9024 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9025 by die_abbrev's usage count, from the most commonly used
9026 abbreviation to the least. */
9028 static int
9029 die_abbrev_cmp (const void *p1, const void *p2)
9031 dw_die_ref die1 = *(const dw_die_ref *) p1;
9032 dw_die_ref die2 = *(const dw_die_ref *) p2;
9034 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9035 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9037 if (die1->die_abbrev >= abbrev_opt_base_type_end
9038 && die2->die_abbrev >= abbrev_opt_base_type_end)
9040 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9041 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9042 return -1;
9043 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9044 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9045 return 1;
9048 /* Stabilize the sort. */
9049 if (die1->die_abbrev < die2->die_abbrev)
9050 return -1;
9051 if (die1->die_abbrev > die2->die_abbrev)
9052 return 1;
9054 return 0;
9057 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9058 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9059 into dw_val_class_const_implicit or
9060 dw_val_class_unsigned_const_implicit. */
9062 static void
9063 optimize_implicit_const (unsigned int first_id, unsigned int end,
9064 vec<bool> &implicit_consts)
9066 /* It never makes sense if there is just one DIE using the abbreviation. */
9067 if (end < first_id + 2)
9068 return;
9070 dw_attr_node *a;
9071 unsigned ix, i;
9072 dw_die_ref die = sorted_abbrev_dies[first_id];
9073 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9074 if (implicit_consts[ix])
9076 enum dw_val_class new_class = dw_val_class_none;
9077 switch (AT_class (a))
9079 case dw_val_class_unsigned_const:
9080 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9081 continue;
9083 /* The .debug_abbrev section will grow by
9084 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9085 in all the DIEs using that abbreviation. */
9086 if (constant_size (AT_unsigned (a)) * (end - first_id)
9087 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9088 continue;
9090 new_class = dw_val_class_unsigned_const_implicit;
9091 break;
9093 case dw_val_class_const:
9094 new_class = dw_val_class_const_implicit;
9095 break;
9097 case dw_val_class_file:
9098 new_class = dw_val_class_file_implicit;
9099 break;
9101 default:
9102 continue;
9104 for (i = first_id; i < end; i++)
9105 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9106 = new_class;
9110 /* Attempt to optimize abbreviation table from abbrev_opt_start
9111 abbreviation above. */
9113 static void
9114 optimize_abbrev_table (void)
9116 if (abbrev_opt_start
9117 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9118 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9120 auto_vec<bool, 32> implicit_consts;
9121 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9123 unsigned int abbrev_id = abbrev_opt_start - 1;
9124 unsigned int first_id = ~0U;
9125 unsigned int last_abbrev_id = 0;
9126 unsigned int i;
9127 dw_die_ref die;
9128 if (abbrev_opt_base_type_end > abbrev_opt_start)
9129 abbrev_id = abbrev_opt_base_type_end - 1;
9130 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9131 most commonly used abbreviations come first. */
9132 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9134 dw_attr_node *a;
9135 unsigned ix;
9137 /* If calc_base_type_die_sizes has been called, the CU and
9138 base types after it can't be optimized, because we've already
9139 calculated their DIE offsets. We've sorted them first. */
9140 if (die->die_abbrev < abbrev_opt_base_type_end)
9141 continue;
9142 if (die->die_abbrev != last_abbrev_id)
9144 last_abbrev_id = die->die_abbrev;
9145 if (dwarf_version >= 5 && first_id != ~0U)
9146 optimize_implicit_const (first_id, i, implicit_consts);
9147 abbrev_id++;
9148 (*abbrev_die_table)[abbrev_id] = die;
9149 if (dwarf_version >= 5)
9151 first_id = i;
9152 implicit_consts.truncate (0);
9154 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9155 switch (AT_class (a))
9157 case dw_val_class_const:
9158 case dw_val_class_unsigned_const:
9159 case dw_val_class_file:
9160 implicit_consts.safe_push (true);
9161 break;
9162 default:
9163 implicit_consts.safe_push (false);
9164 break;
9168 else if (dwarf_version >= 5)
9170 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9171 if (!implicit_consts[ix])
9172 continue;
9173 else
9175 dw_attr_node *other_a
9176 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9177 if (!dw_val_equal_p (&a->dw_attr_val,
9178 &other_a->dw_attr_val))
9179 implicit_consts[ix] = false;
9182 die->die_abbrev = abbrev_id;
9184 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9185 if (dwarf_version >= 5 && first_id != ~0U)
9186 optimize_implicit_const (first_id, i, implicit_consts);
9189 abbrev_opt_start = 0;
9190 abbrev_opt_base_type_end = 0;
9191 abbrev_usage_count.release ();
9192 sorted_abbrev_dies.release ();
9195 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9197 static int
9198 constant_size (unsigned HOST_WIDE_INT value)
9200 int log;
9202 if (value == 0)
9203 log = 0;
9204 else
9205 log = floor_log2 (value);
9207 log = log / 8;
9208 log = 1 << (floor_log2 (log) + 1);
9210 return log;
9213 /* Return the size of a DIE as it is represented in the
9214 .debug_info section. */
9216 static unsigned long
9217 size_of_die (dw_die_ref die)
9219 unsigned long size = 0;
9220 dw_attr_node *a;
9221 unsigned ix;
9222 enum dwarf_form form;
9224 size += size_of_uleb128 (die->die_abbrev);
9225 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9227 switch (AT_class (a))
9229 case dw_val_class_addr:
9230 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9232 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9233 size += size_of_uleb128 (AT_index (a));
9235 else
9236 size += DWARF2_ADDR_SIZE;
9237 break;
9238 case dw_val_class_offset:
9239 size += DWARF_OFFSET_SIZE;
9240 break;
9241 case dw_val_class_loc:
9243 unsigned long lsize = size_of_locs (AT_loc (a));
9245 /* Block length. */
9246 if (dwarf_version >= 4)
9247 size += size_of_uleb128 (lsize);
9248 else
9249 size += constant_size (lsize);
9250 size += lsize;
9252 break;
9253 case dw_val_class_loc_list:
9254 case dw_val_class_view_list:
9255 if (dwarf_split_debug_info && dwarf_version >= 5)
9257 gcc_assert (AT_loc_list (a)->num_assigned);
9258 size += size_of_uleb128 (AT_loc_list (a)->hash);
9260 else
9261 size += DWARF_OFFSET_SIZE;
9262 break;
9263 case dw_val_class_range_list:
9264 if (value_format (a) == DW_FORM_rnglistx)
9266 gcc_assert (rnglist_idx);
9267 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9268 size += size_of_uleb128 (r->idx);
9270 else
9271 size += DWARF_OFFSET_SIZE;
9272 break;
9273 case dw_val_class_const:
9274 size += size_of_sleb128 (AT_int (a));
9275 break;
9276 case dw_val_class_unsigned_const:
9278 int csize = constant_size (AT_unsigned (a));
9279 if (dwarf_version == 3
9280 && a->dw_attr == DW_AT_data_member_location
9281 && csize >= 4)
9282 size += size_of_uleb128 (AT_unsigned (a));
9283 else
9284 size += csize;
9286 break;
9287 case dw_val_class_const_implicit:
9288 case dw_val_class_unsigned_const_implicit:
9289 case dw_val_class_file_implicit:
9290 /* These occupy no size in the DIE, just an extra sleb128 in
9291 .debug_abbrev. */
9292 break;
9293 case dw_val_class_const_double:
9294 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9295 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9296 size++; /* block */
9297 break;
9298 case dw_val_class_wide_int:
9299 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9300 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9301 if (get_full_len (*a->dw_attr_val.v.val_wide)
9302 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9303 size++; /* block */
9304 break;
9305 case dw_val_class_vec:
9306 size += constant_size (a->dw_attr_val.v.val_vec.length
9307 * a->dw_attr_val.v.val_vec.elt_size)
9308 + a->dw_attr_val.v.val_vec.length
9309 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9310 break;
9311 case dw_val_class_flag:
9312 if (dwarf_version >= 4)
9313 /* Currently all add_AT_flag calls pass in 1 as last argument,
9314 so DW_FORM_flag_present can be used. If that ever changes,
9315 we'll need to use DW_FORM_flag and have some optimization
9316 in build_abbrev_table that will change those to
9317 DW_FORM_flag_present if it is set to 1 in all DIEs using
9318 the same abbrev entry. */
9319 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9320 else
9321 size += 1;
9322 break;
9323 case dw_val_class_die_ref:
9324 if (AT_ref_external (a))
9326 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9327 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9328 is sized by target address length, whereas in DWARF3
9329 it's always sized as an offset. */
9330 if (use_debug_types)
9331 size += DWARF_TYPE_SIGNATURE_SIZE;
9332 else if (dwarf_version == 2)
9333 size += DWARF2_ADDR_SIZE;
9334 else
9335 size += DWARF_OFFSET_SIZE;
9337 else
9338 size += DWARF_OFFSET_SIZE;
9339 break;
9340 case dw_val_class_fde_ref:
9341 size += DWARF_OFFSET_SIZE;
9342 break;
9343 case dw_val_class_lbl_id:
9344 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9346 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9347 size += size_of_uleb128 (AT_index (a));
9349 else
9350 size += DWARF2_ADDR_SIZE;
9351 break;
9352 case dw_val_class_lineptr:
9353 case dw_val_class_macptr:
9354 case dw_val_class_loclistsptr:
9355 size += DWARF_OFFSET_SIZE;
9356 break;
9357 case dw_val_class_str:
9358 form = AT_string_form (a);
9359 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9360 size += DWARF_OFFSET_SIZE;
9361 else if (form == DW_FORM_GNU_str_index)
9362 size += size_of_uleb128 (AT_index (a));
9363 else
9364 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9365 break;
9366 case dw_val_class_file:
9367 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9368 break;
9369 case dw_val_class_data8:
9370 size += 8;
9371 break;
9372 case dw_val_class_vms_delta:
9373 size += DWARF_OFFSET_SIZE;
9374 break;
9375 case dw_val_class_high_pc:
9376 size += DWARF2_ADDR_SIZE;
9377 break;
9378 case dw_val_class_discr_value:
9379 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9380 break;
9381 case dw_val_class_discr_list:
9383 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9385 /* This is a block, so we have the block length and then its
9386 data. */
9387 size += constant_size (block_size) + block_size;
9389 break;
9390 default:
9391 gcc_unreachable ();
9395 return size;
9398 /* Size the debugging information associated with a given DIE. Visits the
9399 DIE's children recursively. Updates the global variable next_die_offset, on
9400 each time through. Uses the current value of next_die_offset to update the
9401 die_offset field in each DIE. */
9403 static void
9404 calc_die_sizes (dw_die_ref die)
9406 dw_die_ref c;
9408 gcc_assert (die->die_offset == 0
9409 || (unsigned long int) die->die_offset == next_die_offset);
9410 die->die_offset = next_die_offset;
9411 next_die_offset += size_of_die (die);
9413 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9415 if (die->die_child != NULL)
9416 /* Count the null byte used to terminate sibling lists. */
9417 next_die_offset += 1;
9420 /* Size just the base type children at the start of the CU.
9421 This is needed because build_abbrev needs to size locs
9422 and sizing of type based stack ops needs to know die_offset
9423 values for the base types. */
9425 static void
9426 calc_base_type_die_sizes (void)
9428 unsigned long die_offset = (dwarf_split_debug_info
9429 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9430 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9431 unsigned int i;
9432 dw_die_ref base_type;
9433 #if ENABLE_ASSERT_CHECKING
9434 dw_die_ref prev = comp_unit_die ()->die_child;
9435 #endif
9437 die_offset += size_of_die (comp_unit_die ());
9438 for (i = 0; base_types.iterate (i, &base_type); i++)
9440 #if ENABLE_ASSERT_CHECKING
9441 gcc_assert (base_type->die_offset == 0
9442 && prev->die_sib == base_type
9443 && base_type->die_child == NULL
9444 && base_type->die_abbrev);
9445 prev = base_type;
9446 #endif
9447 if (abbrev_opt_start
9448 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9449 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9450 base_type->die_offset = die_offset;
9451 die_offset += size_of_die (base_type);
9455 /* Set the marks for a die and its children. We do this so
9456 that we know whether or not a reference needs to use FORM_ref_addr; only
9457 DIEs in the same CU will be marked. We used to clear out the offset
9458 and use that as the flag, but ran into ordering problems. */
9460 static void
9461 mark_dies (dw_die_ref die)
9463 dw_die_ref c;
9465 gcc_assert (!die->die_mark);
9467 die->die_mark = 1;
9468 FOR_EACH_CHILD (die, c, mark_dies (c));
9471 /* Clear the marks for a die and its children. */
9473 static void
9474 unmark_dies (dw_die_ref die)
9476 dw_die_ref c;
9478 if (! use_debug_types)
9479 gcc_assert (die->die_mark);
9481 die->die_mark = 0;
9482 FOR_EACH_CHILD (die, c, unmark_dies (c));
9485 /* Clear the marks for a die, its children and referred dies. */
9487 static void
9488 unmark_all_dies (dw_die_ref die)
9490 dw_die_ref c;
9491 dw_attr_node *a;
9492 unsigned ix;
9494 if (!die->die_mark)
9495 return;
9496 die->die_mark = 0;
9498 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9500 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9501 if (AT_class (a) == dw_val_class_die_ref)
9502 unmark_all_dies (AT_ref (a));
9505 /* Calculate if the entry should appear in the final output file. It may be
9506 from a pruned a type. */
9508 static bool
9509 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9511 /* By limiting gnu pubnames to definitions only, gold can generate a
9512 gdb index without entries for declarations, which don't include
9513 enough information to be useful. */
9514 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9515 return false;
9517 if (table == pubname_table)
9519 /* Enumerator names are part of the pubname table, but the
9520 parent DW_TAG_enumeration_type die may have been pruned.
9521 Don't output them if that is the case. */
9522 if (p->die->die_tag == DW_TAG_enumerator &&
9523 (p->die->die_parent == NULL
9524 || !p->die->die_parent->die_perennial_p))
9525 return false;
9527 /* Everything else in the pubname table is included. */
9528 return true;
9531 /* The pubtypes table shouldn't include types that have been
9532 pruned. */
9533 return (p->die->die_offset != 0
9534 || !flag_eliminate_unused_debug_types);
9537 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9538 generated for the compilation unit. */
9540 static unsigned long
9541 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9543 unsigned long size;
9544 unsigned i;
9545 pubname_entry *p;
9546 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9548 size = DWARF_PUBNAMES_HEADER_SIZE;
9549 FOR_EACH_VEC_ELT (*names, i, p)
9550 if (include_pubname_in_output (names, p))
9551 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9553 size += DWARF_OFFSET_SIZE;
9554 return size;
9557 /* Return the size of the information in the .debug_aranges section. */
9559 static unsigned long
9560 size_of_aranges (void)
9562 unsigned long size;
9564 size = DWARF_ARANGES_HEADER_SIZE;
9566 /* Count the address/length pair for this compilation unit. */
9567 if (text_section_used)
9568 size += 2 * DWARF2_ADDR_SIZE;
9569 if (cold_text_section_used)
9570 size += 2 * DWARF2_ADDR_SIZE;
9571 if (have_multiple_function_sections)
9573 unsigned fde_idx;
9574 dw_fde_ref fde;
9576 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9578 if (DECL_IGNORED_P (fde->decl))
9579 continue;
9580 if (!fde->in_std_section)
9581 size += 2 * DWARF2_ADDR_SIZE;
9582 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9583 size += 2 * DWARF2_ADDR_SIZE;
9587 /* Count the two zero words used to terminated the address range table. */
9588 size += 2 * DWARF2_ADDR_SIZE;
9589 return size;
9592 /* Select the encoding of an attribute value. */
9594 static enum dwarf_form
9595 value_format (dw_attr_node *a)
9597 switch (AT_class (a))
9599 case dw_val_class_addr:
9600 /* Only very few attributes allow DW_FORM_addr. */
9601 switch (a->dw_attr)
9603 case DW_AT_low_pc:
9604 case DW_AT_high_pc:
9605 case DW_AT_entry_pc:
9606 case DW_AT_trampoline:
9607 return (AT_index (a) == NOT_INDEXED
9608 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9609 default:
9610 break;
9612 switch (DWARF2_ADDR_SIZE)
9614 case 1:
9615 return DW_FORM_data1;
9616 case 2:
9617 return DW_FORM_data2;
9618 case 4:
9619 return DW_FORM_data4;
9620 case 8:
9621 return DW_FORM_data8;
9622 default:
9623 gcc_unreachable ();
9625 case dw_val_class_loc_list:
9626 case dw_val_class_view_list:
9627 if (dwarf_split_debug_info
9628 && dwarf_version >= 5
9629 && AT_loc_list (a)->num_assigned)
9630 return DW_FORM_loclistx;
9631 /* FALLTHRU */
9632 case dw_val_class_range_list:
9633 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9634 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9635 care about sizes of .debug* sections in shared libraries and
9636 executables and don't take into account relocations that affect just
9637 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9638 table in the .debug_rnglists section. */
9639 if (dwarf_split_debug_info
9640 && dwarf_version >= 5
9641 && AT_class (a) == dw_val_class_range_list
9642 && rnglist_idx
9643 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9644 return DW_FORM_rnglistx;
9645 if (dwarf_version >= 4)
9646 return DW_FORM_sec_offset;
9647 /* FALLTHRU */
9648 case dw_val_class_vms_delta:
9649 case dw_val_class_offset:
9650 switch (DWARF_OFFSET_SIZE)
9652 case 4:
9653 return DW_FORM_data4;
9654 case 8:
9655 return DW_FORM_data8;
9656 default:
9657 gcc_unreachable ();
9659 case dw_val_class_loc:
9660 if (dwarf_version >= 4)
9661 return DW_FORM_exprloc;
9662 switch (constant_size (size_of_locs (AT_loc (a))))
9664 case 1:
9665 return DW_FORM_block1;
9666 case 2:
9667 return DW_FORM_block2;
9668 case 4:
9669 return DW_FORM_block4;
9670 default:
9671 gcc_unreachable ();
9673 case dw_val_class_const:
9674 return DW_FORM_sdata;
9675 case dw_val_class_unsigned_const:
9676 switch (constant_size (AT_unsigned (a)))
9678 case 1:
9679 return DW_FORM_data1;
9680 case 2:
9681 return DW_FORM_data2;
9682 case 4:
9683 /* In DWARF3 DW_AT_data_member_location with
9684 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9685 constant, so we need to use DW_FORM_udata if we need
9686 a large constant. */
9687 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9688 return DW_FORM_udata;
9689 return DW_FORM_data4;
9690 case 8:
9691 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9692 return DW_FORM_udata;
9693 return DW_FORM_data8;
9694 default:
9695 gcc_unreachable ();
9697 case dw_val_class_const_implicit:
9698 case dw_val_class_unsigned_const_implicit:
9699 case dw_val_class_file_implicit:
9700 return DW_FORM_implicit_const;
9701 case dw_val_class_const_double:
9702 switch (HOST_BITS_PER_WIDE_INT)
9704 case 8:
9705 return DW_FORM_data2;
9706 case 16:
9707 return DW_FORM_data4;
9708 case 32:
9709 return DW_FORM_data8;
9710 case 64:
9711 if (dwarf_version >= 5)
9712 return DW_FORM_data16;
9713 /* FALLTHRU */
9714 default:
9715 return DW_FORM_block1;
9717 case dw_val_class_wide_int:
9718 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9720 case 8:
9721 return DW_FORM_data1;
9722 case 16:
9723 return DW_FORM_data2;
9724 case 32:
9725 return DW_FORM_data4;
9726 case 64:
9727 return DW_FORM_data8;
9728 case 128:
9729 if (dwarf_version >= 5)
9730 return DW_FORM_data16;
9731 /* FALLTHRU */
9732 default:
9733 return DW_FORM_block1;
9735 case dw_val_class_vec:
9736 switch (constant_size (a->dw_attr_val.v.val_vec.length
9737 * a->dw_attr_val.v.val_vec.elt_size))
9739 case 1:
9740 return DW_FORM_block1;
9741 case 2:
9742 return DW_FORM_block2;
9743 case 4:
9744 return DW_FORM_block4;
9745 default:
9746 gcc_unreachable ();
9748 case dw_val_class_flag:
9749 if (dwarf_version >= 4)
9751 /* Currently all add_AT_flag calls pass in 1 as last argument,
9752 so DW_FORM_flag_present can be used. If that ever changes,
9753 we'll need to use DW_FORM_flag and have some optimization
9754 in build_abbrev_table that will change those to
9755 DW_FORM_flag_present if it is set to 1 in all DIEs using
9756 the same abbrev entry. */
9757 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9758 return DW_FORM_flag_present;
9760 return DW_FORM_flag;
9761 case dw_val_class_die_ref:
9762 if (AT_ref_external (a))
9763 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9764 else
9765 return DW_FORM_ref;
9766 case dw_val_class_fde_ref:
9767 return DW_FORM_data;
9768 case dw_val_class_lbl_id:
9769 return (AT_index (a) == NOT_INDEXED
9770 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9771 case dw_val_class_lineptr:
9772 case dw_val_class_macptr:
9773 case dw_val_class_loclistsptr:
9774 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9775 case dw_val_class_str:
9776 return AT_string_form (a);
9777 case dw_val_class_file:
9778 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9780 case 1:
9781 return DW_FORM_data1;
9782 case 2:
9783 return DW_FORM_data2;
9784 case 4:
9785 return DW_FORM_data4;
9786 default:
9787 gcc_unreachable ();
9790 case dw_val_class_data8:
9791 return DW_FORM_data8;
9793 case dw_val_class_high_pc:
9794 switch (DWARF2_ADDR_SIZE)
9796 case 1:
9797 return DW_FORM_data1;
9798 case 2:
9799 return DW_FORM_data2;
9800 case 4:
9801 return DW_FORM_data4;
9802 case 8:
9803 return DW_FORM_data8;
9804 default:
9805 gcc_unreachable ();
9808 case dw_val_class_discr_value:
9809 return (a->dw_attr_val.v.val_discr_value.pos
9810 ? DW_FORM_udata
9811 : DW_FORM_sdata);
9812 case dw_val_class_discr_list:
9813 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9815 case 1:
9816 return DW_FORM_block1;
9817 case 2:
9818 return DW_FORM_block2;
9819 case 4:
9820 return DW_FORM_block4;
9821 default:
9822 gcc_unreachable ();
9825 default:
9826 gcc_unreachable ();
9830 /* Output the encoding of an attribute value. */
9832 static void
9833 output_value_format (dw_attr_node *a)
9835 enum dwarf_form form = value_format (a);
9837 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9840 /* Given a die and id, produce the appropriate abbreviations. */
9842 static void
9843 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9845 unsigned ix;
9846 dw_attr_node *a_attr;
9848 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9849 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9850 dwarf_tag_name (abbrev->die_tag));
9852 if (abbrev->die_child != NULL)
9853 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9854 else
9855 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9857 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9859 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9860 dwarf_attr_name (a_attr->dw_attr));
9861 output_value_format (a_attr);
9862 if (value_format (a_attr) == DW_FORM_implicit_const)
9864 if (AT_class (a_attr) == dw_val_class_file_implicit)
9866 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9867 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9868 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9870 else
9871 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9875 dw2_asm_output_data (1, 0, NULL);
9876 dw2_asm_output_data (1, 0, NULL);
9880 /* Output the .debug_abbrev section which defines the DIE abbreviation
9881 table. */
9883 static void
9884 output_abbrev_section (void)
9886 unsigned int abbrev_id;
9887 dw_die_ref abbrev;
9889 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9890 if (abbrev_id != 0)
9891 output_die_abbrevs (abbrev_id, abbrev);
9893 /* Terminate the table. */
9894 dw2_asm_output_data (1, 0, NULL);
9897 /* Return a new location list, given the begin and end range, and the
9898 expression. */
9900 static inline dw_loc_list_ref
9901 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9902 const char *end, var_loc_view vend,
9903 const char *section)
9905 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9907 retlist->begin = begin;
9908 retlist->begin_entry = NULL;
9909 retlist->end = end;
9910 retlist->expr = expr;
9911 retlist->section = section;
9912 retlist->vbegin = vbegin;
9913 retlist->vend = vend;
9915 return retlist;
9918 /* Return true iff there's any nonzero view number in the loc list. */
9920 static bool
9921 loc_list_has_views (dw_loc_list_ref list)
9923 if (!debug_variable_location_views)
9924 return false;
9926 for (dw_loc_list_ref loc = list;
9927 loc != NULL; loc = loc->dw_loc_next)
9928 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
9929 return true;
9931 return false;
9934 /* Generate a new internal symbol for this location list node, if it
9935 hasn't got one yet. */
9937 static inline void
9938 gen_llsym (dw_loc_list_ref list)
9940 gcc_assert (!list->ll_symbol);
9941 list->ll_symbol = gen_internal_sym ("LLST");
9943 if (!loc_list_has_views (list))
9944 return;
9946 if (dwarf2out_locviews_in_attribute ())
9948 /* Use the same label_num for the view list. */
9949 label_num--;
9950 list->vl_symbol = gen_internal_sym ("LVUS");
9952 else
9953 list->vl_symbol = list->ll_symbol;
9956 /* Generate a symbol for the list, but only if we really want to emit
9957 it as a list. */
9959 static inline void
9960 maybe_gen_llsym (dw_loc_list_ref list)
9962 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
9963 return;
9965 gen_llsym (list);
9968 /* Determine whether or not to skip loc_list entry CURR. If we're not
9969 to skip it, and SIZEP is non-null, store the size of CURR->expr's
9970 representation in *SIZEP. */
9972 static bool
9973 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = 0)
9975 /* Don't output an entry that starts and ends at the same address. */
9976 if (strcmp (curr->begin, curr->end) == 0
9977 && curr->vbegin == curr->vend && !curr->force)
9978 return true;
9980 unsigned long size = size_of_locs (curr->expr);
9982 /* If the expression is too large, drop it on the floor. We could
9983 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9984 in the expression, but >= 64KB expressions for a single value
9985 in a single range are unlikely very useful. */
9986 if (dwarf_version < 5 && size > 0xffff)
9987 return true;
9989 if (sizep)
9990 *sizep = size;
9992 return false;
9995 /* Output a view pair loclist entry for CURR, if it requires one. */
9997 static void
9998 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10000 if (!dwarf2out_locviews_in_loclist ())
10001 return;
10003 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10004 return;
10006 #ifdef DW_LLE_view_pair
10007 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10009 if (dwarf2out_as_locview_support)
10011 if (ZERO_VIEW_P (curr->vbegin))
10012 dw2_asm_output_data_uleb128 (0, "Location view begin");
10013 else
10015 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10016 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10017 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10020 if (ZERO_VIEW_P (curr->vend))
10021 dw2_asm_output_data_uleb128 (0, "Location view end");
10022 else
10024 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10025 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10026 dw2_asm_output_symname_uleb128 (label, "Location view end");
10029 else
10031 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10032 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10034 #endif /* DW_LLE_view_pair */
10036 return;
10039 /* Output the location list given to us. */
10041 static void
10042 output_loc_list (dw_loc_list_ref list_head)
10044 int vcount = 0, lcount = 0;
10046 if (list_head->emitted)
10047 return;
10048 list_head->emitted = true;
10050 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10052 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10054 for (dw_loc_list_ref curr = list_head; curr != NULL;
10055 curr = curr->dw_loc_next)
10057 if (skip_loc_list_entry (curr))
10058 continue;
10060 vcount++;
10062 /* ?? dwarf_split_debug_info? */
10063 if (dwarf2out_as_locview_support)
10065 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10067 if (!ZERO_VIEW_P (curr->vbegin))
10069 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10070 dw2_asm_output_symname_uleb128 (label,
10071 "View list begin (%s)",
10072 list_head->vl_symbol);
10074 else
10075 dw2_asm_output_data_uleb128 (0,
10076 "View list begin (%s)",
10077 list_head->vl_symbol);
10079 if (!ZERO_VIEW_P (curr->vend))
10081 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10082 dw2_asm_output_symname_uleb128 (label,
10083 "View list end (%s)",
10084 list_head->vl_symbol);
10086 else
10087 dw2_asm_output_data_uleb128 (0,
10088 "View list end (%s)",
10089 list_head->vl_symbol);
10091 else
10093 dw2_asm_output_data_uleb128 (curr->vbegin,
10094 "View list begin (%s)",
10095 list_head->vl_symbol);
10096 dw2_asm_output_data_uleb128 (curr->vend,
10097 "View list end (%s)",
10098 list_head->vl_symbol);
10103 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10105 const char *last_section = NULL;
10106 const char *base_label = NULL;
10108 /* Walk the location list, and output each range + expression. */
10109 for (dw_loc_list_ref curr = list_head; curr != NULL;
10110 curr = curr->dw_loc_next)
10112 unsigned long size;
10114 /* Skip this entry? If we skip it here, we must skip it in the
10115 view list above as well. */
10116 if (skip_loc_list_entry (curr, &size))
10117 continue;
10119 lcount++;
10121 if (dwarf_version >= 5)
10123 if (dwarf_split_debug_info)
10125 dwarf2out_maybe_output_loclist_view_pair (curr);
10126 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10127 uleb128 index into .debug_addr and uleb128 length. */
10128 dw2_asm_output_data (1, DW_LLE_startx_length,
10129 "DW_LLE_startx_length (%s)",
10130 list_head->ll_symbol);
10131 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10132 "Location list range start index "
10133 "(%s)", curr->begin);
10134 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10135 For that case we probably need to emit DW_LLE_startx_endx,
10136 but we'd need 2 .debug_addr entries rather than just one. */
10137 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10138 "Location list length (%s)",
10139 list_head->ll_symbol);
10141 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10143 dwarf2out_maybe_output_loclist_view_pair (curr);
10144 /* If all code is in .text section, the base address is
10145 already provided by the CU attributes. Use
10146 DW_LLE_offset_pair where both addresses are uleb128 encoded
10147 offsets against that base. */
10148 dw2_asm_output_data (1, DW_LLE_offset_pair,
10149 "DW_LLE_offset_pair (%s)",
10150 list_head->ll_symbol);
10151 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10152 "Location list begin address (%s)",
10153 list_head->ll_symbol);
10154 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10155 "Location list end address (%s)",
10156 list_head->ll_symbol);
10158 else if (HAVE_AS_LEB128)
10160 /* Otherwise, find out how many consecutive entries could share
10161 the same base entry. If just one, emit DW_LLE_start_length,
10162 otherwise emit DW_LLE_base_address for the base address
10163 followed by a series of DW_LLE_offset_pair. */
10164 if (last_section == NULL || curr->section != last_section)
10166 dw_loc_list_ref curr2;
10167 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10168 curr2 = curr2->dw_loc_next)
10170 if (strcmp (curr2->begin, curr2->end) == 0
10171 && !curr2->force)
10172 continue;
10173 break;
10175 if (curr2 == NULL || curr->section != curr2->section)
10176 last_section = NULL;
10177 else
10179 last_section = curr->section;
10180 base_label = curr->begin;
10181 dw2_asm_output_data (1, DW_LLE_base_address,
10182 "DW_LLE_base_address (%s)",
10183 list_head->ll_symbol);
10184 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10185 "Base address (%s)",
10186 list_head->ll_symbol);
10189 /* Only one entry with the same base address. Use
10190 DW_LLE_start_length with absolute address and uleb128
10191 length. */
10192 if (last_section == NULL)
10194 dwarf2out_maybe_output_loclist_view_pair (curr);
10195 dw2_asm_output_data (1, DW_LLE_start_length,
10196 "DW_LLE_start_length (%s)",
10197 list_head->ll_symbol);
10198 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10199 "Location list begin address (%s)",
10200 list_head->ll_symbol);
10201 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10202 "Location list length "
10203 "(%s)", list_head->ll_symbol);
10205 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10206 DW_LLE_base_address. */
10207 else
10209 dwarf2out_maybe_output_loclist_view_pair (curr);
10210 dw2_asm_output_data (1, DW_LLE_offset_pair,
10211 "DW_LLE_offset_pair (%s)",
10212 list_head->ll_symbol);
10213 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10214 "Location list begin address "
10215 "(%s)", list_head->ll_symbol);
10216 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10217 "Location list end address "
10218 "(%s)", list_head->ll_symbol);
10221 /* The assembler does not support .uleb128 directive. Emit
10222 DW_LLE_start_end with a pair of absolute addresses. */
10223 else
10225 dwarf2out_maybe_output_loclist_view_pair (curr);
10226 dw2_asm_output_data (1, DW_LLE_start_end,
10227 "DW_LLE_start_end (%s)",
10228 list_head->ll_symbol);
10229 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10230 "Location list begin address (%s)",
10231 list_head->ll_symbol);
10232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10233 "Location list end address (%s)",
10234 list_head->ll_symbol);
10237 else if (dwarf_split_debug_info)
10239 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10240 and 4 byte length. */
10241 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10242 "Location list start/length entry (%s)",
10243 list_head->ll_symbol);
10244 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10245 "Location list range start index (%s)",
10246 curr->begin);
10247 /* The length field is 4 bytes. If we ever need to support
10248 an 8-byte length, we can add a new DW_LLE code or fall back
10249 to DW_LLE_GNU_start_end_entry. */
10250 dw2_asm_output_delta (4, curr->end, curr->begin,
10251 "Location list range length (%s)",
10252 list_head->ll_symbol);
10254 else if (!have_multiple_function_sections)
10256 /* Pair of relative addresses against start of text section. */
10257 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10258 "Location list begin address (%s)",
10259 list_head->ll_symbol);
10260 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10261 "Location list end address (%s)",
10262 list_head->ll_symbol);
10264 else
10266 /* Pair of absolute addresses. */
10267 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10268 "Location list begin address (%s)",
10269 list_head->ll_symbol);
10270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10271 "Location list end address (%s)",
10272 list_head->ll_symbol);
10275 /* Output the block length for this list of location operations. */
10276 if (dwarf_version >= 5)
10277 dw2_asm_output_data_uleb128 (size, "Location expression size");
10278 else
10280 gcc_assert (size <= 0xffff);
10281 dw2_asm_output_data (2, size, "Location expression size");
10284 output_loc_sequence (curr->expr, -1);
10287 /* And finally list termination. */
10288 if (dwarf_version >= 5)
10289 dw2_asm_output_data (1, DW_LLE_end_of_list,
10290 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10291 else if (dwarf_split_debug_info)
10292 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10293 "Location list terminator (%s)",
10294 list_head->ll_symbol);
10295 else
10297 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10298 "Location list terminator begin (%s)",
10299 list_head->ll_symbol);
10300 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10301 "Location list terminator end (%s)",
10302 list_head->ll_symbol);
10305 gcc_assert (!list_head->vl_symbol
10306 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10309 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10310 section. Emit a relocated reference if val_entry is NULL, otherwise,
10311 emit an indirect reference. */
10313 static void
10314 output_range_list_offset (dw_attr_node *a)
10316 const char *name = dwarf_attr_name (a->dw_attr);
10318 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10320 if (dwarf_version >= 5)
10322 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10323 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10324 debug_ranges_section, "%s", name);
10326 else
10328 char *p = strchr (ranges_section_label, '\0');
10329 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10330 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10331 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10332 debug_ranges_section, "%s", name);
10333 *p = '\0';
10336 else if (dwarf_version >= 5)
10338 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10339 gcc_assert (rnglist_idx);
10340 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10342 else
10343 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10344 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10345 "%s (offset from %s)", name, ranges_section_label);
10348 /* Output the offset into the debug_loc section. */
10350 static void
10351 output_loc_list_offset (dw_attr_node *a)
10353 char *sym = AT_loc_list (a)->ll_symbol;
10355 gcc_assert (sym);
10356 if (!dwarf_split_debug_info)
10357 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10358 "%s", dwarf_attr_name (a->dw_attr));
10359 else if (dwarf_version >= 5)
10361 gcc_assert (AT_loc_list (a)->num_assigned);
10362 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10363 dwarf_attr_name (a->dw_attr),
10364 sym);
10366 else
10367 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10368 "%s", dwarf_attr_name (a->dw_attr));
10371 /* Output the offset into the debug_loc section. */
10373 static void
10374 output_view_list_offset (dw_attr_node *a)
10376 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10378 gcc_assert (sym);
10379 if (dwarf_split_debug_info)
10380 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10381 "%s", dwarf_attr_name (a->dw_attr));
10382 else
10383 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10384 "%s", dwarf_attr_name (a->dw_attr));
10387 /* Output an attribute's index or value appropriately. */
10389 static void
10390 output_attr_index_or_value (dw_attr_node *a)
10392 const char *name = dwarf_attr_name (a->dw_attr);
10394 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10396 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10397 return;
10399 switch (AT_class (a))
10401 case dw_val_class_addr:
10402 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10403 break;
10404 case dw_val_class_high_pc:
10405 case dw_val_class_lbl_id:
10406 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10407 break;
10408 default:
10409 gcc_unreachable ();
10413 /* Output a type signature. */
10415 static inline void
10416 output_signature (const char *sig, const char *name)
10418 int i;
10420 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10421 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10424 /* Output a discriminant value. */
10426 static inline void
10427 output_discr_value (dw_discr_value *discr_value, const char *name)
10429 if (discr_value->pos)
10430 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10431 else
10432 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10435 /* Output the DIE and its attributes. Called recursively to generate
10436 the definitions of each child DIE. */
10438 static void
10439 output_die (dw_die_ref die)
10441 dw_attr_node *a;
10442 dw_die_ref c;
10443 unsigned long size;
10444 unsigned ix;
10446 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10447 (unsigned long)die->die_offset,
10448 dwarf_tag_name (die->die_tag));
10450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10452 const char *name = dwarf_attr_name (a->dw_attr);
10454 switch (AT_class (a))
10456 case dw_val_class_addr:
10457 output_attr_index_or_value (a);
10458 break;
10460 case dw_val_class_offset:
10461 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10462 "%s", name);
10463 break;
10465 case dw_val_class_range_list:
10466 output_range_list_offset (a);
10467 break;
10469 case dw_val_class_loc:
10470 size = size_of_locs (AT_loc (a));
10472 /* Output the block length for this list of location operations. */
10473 if (dwarf_version >= 4)
10474 dw2_asm_output_data_uleb128 (size, "%s", name);
10475 else
10476 dw2_asm_output_data (constant_size (size), size, "%s", name);
10478 output_loc_sequence (AT_loc (a), -1);
10479 break;
10481 case dw_val_class_const:
10482 /* ??? It would be slightly more efficient to use a scheme like is
10483 used for unsigned constants below, but gdb 4.x does not sign
10484 extend. Gdb 5.x does sign extend. */
10485 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10486 break;
10488 case dw_val_class_unsigned_const:
10490 int csize = constant_size (AT_unsigned (a));
10491 if (dwarf_version == 3
10492 && a->dw_attr == DW_AT_data_member_location
10493 && csize >= 4)
10494 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10495 else
10496 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10498 break;
10500 case dw_val_class_const_implicit:
10501 if (flag_debug_asm)
10502 fprintf (asm_out_file, "\t\t\t%s %s ("
10503 HOST_WIDE_INT_PRINT_DEC ")\n",
10504 ASM_COMMENT_START, name, AT_int (a));
10505 break;
10507 case dw_val_class_unsigned_const_implicit:
10508 if (flag_debug_asm)
10509 fprintf (asm_out_file, "\t\t\t%s %s ("
10510 HOST_WIDE_INT_PRINT_HEX ")\n",
10511 ASM_COMMENT_START, name, AT_unsigned (a));
10512 break;
10514 case dw_val_class_const_double:
10516 unsigned HOST_WIDE_INT first, second;
10518 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10519 dw2_asm_output_data (1,
10520 HOST_BITS_PER_DOUBLE_INT
10521 / HOST_BITS_PER_CHAR,
10522 NULL);
10524 if (WORDS_BIG_ENDIAN)
10526 first = a->dw_attr_val.v.val_double.high;
10527 second = a->dw_attr_val.v.val_double.low;
10529 else
10531 first = a->dw_attr_val.v.val_double.low;
10532 second = a->dw_attr_val.v.val_double.high;
10535 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10536 first, "%s", name);
10537 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10538 second, NULL);
10540 break;
10542 case dw_val_class_wide_int:
10544 int i;
10545 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10546 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10547 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10548 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10549 * l, NULL);
10551 if (WORDS_BIG_ENDIAN)
10552 for (i = len - 1; i >= 0; --i)
10554 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10555 "%s", name);
10556 name = "";
10558 else
10559 for (i = 0; i < len; ++i)
10561 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10562 "%s", name);
10563 name = "";
10566 break;
10568 case dw_val_class_vec:
10570 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10571 unsigned int len = a->dw_attr_val.v.val_vec.length;
10572 unsigned int i;
10573 unsigned char *p;
10575 dw2_asm_output_data (constant_size (len * elt_size),
10576 len * elt_size, "%s", name);
10577 if (elt_size > sizeof (HOST_WIDE_INT))
10579 elt_size /= 2;
10580 len *= 2;
10582 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10583 i < len;
10584 i++, p += elt_size)
10585 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10586 "fp or vector constant word %u", i);
10587 break;
10590 case dw_val_class_flag:
10591 if (dwarf_version >= 4)
10593 /* Currently all add_AT_flag calls pass in 1 as last argument,
10594 so DW_FORM_flag_present can be used. If that ever changes,
10595 we'll need to use DW_FORM_flag and have some optimization
10596 in build_abbrev_table that will change those to
10597 DW_FORM_flag_present if it is set to 1 in all DIEs using
10598 the same abbrev entry. */
10599 gcc_assert (AT_flag (a) == 1);
10600 if (flag_debug_asm)
10601 fprintf (asm_out_file, "\t\t\t%s %s\n",
10602 ASM_COMMENT_START, name);
10603 break;
10605 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10606 break;
10608 case dw_val_class_loc_list:
10609 output_loc_list_offset (a);
10610 break;
10612 case dw_val_class_view_list:
10613 output_view_list_offset (a);
10614 break;
10616 case dw_val_class_die_ref:
10617 if (AT_ref_external (a))
10619 if (AT_ref (a)->comdat_type_p)
10621 comdat_type_node *type_node
10622 = AT_ref (a)->die_id.die_type_node;
10624 gcc_assert (type_node);
10625 output_signature (type_node->signature, name);
10627 else
10629 const char *sym = AT_ref (a)->die_id.die_symbol;
10630 int size;
10632 gcc_assert (sym);
10633 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10634 length, whereas in DWARF3 it's always sized as an
10635 offset. */
10636 if (dwarf_version == 2)
10637 size = DWARF2_ADDR_SIZE;
10638 else
10639 size = DWARF_OFFSET_SIZE;
10640 /* ??? We cannot unconditionally output die_offset if
10641 non-zero - others might create references to those
10642 DIEs via symbols.
10643 And we do not clear its DIE offset after outputting it
10644 (and the label refers to the actual DIEs, not the
10645 DWARF CU unit header which is when using label + offset
10646 would be the correct thing to do).
10647 ??? This is the reason for the with_offset flag. */
10648 if (AT_ref (a)->with_offset)
10649 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10650 debug_info_section, "%s", name);
10651 else
10652 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10653 name);
10656 else
10658 gcc_assert (AT_ref (a)->die_offset);
10659 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10660 "%s", name);
10662 break;
10664 case dw_val_class_fde_ref:
10666 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10668 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10669 a->dw_attr_val.v.val_fde_index * 2);
10670 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10671 "%s", name);
10673 break;
10675 case dw_val_class_vms_delta:
10676 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10677 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10678 AT_vms_delta2 (a), AT_vms_delta1 (a),
10679 "%s", name);
10680 #else
10681 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10682 AT_vms_delta2 (a), AT_vms_delta1 (a),
10683 "%s", name);
10684 #endif
10685 break;
10687 case dw_val_class_lbl_id:
10688 output_attr_index_or_value (a);
10689 break;
10691 case dw_val_class_lineptr:
10692 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10693 debug_line_section, "%s", name);
10694 break;
10696 case dw_val_class_macptr:
10697 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10698 debug_macinfo_section, "%s", name);
10699 break;
10701 case dw_val_class_loclistsptr:
10702 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10703 debug_loc_section, "%s", name);
10704 break;
10706 case dw_val_class_str:
10707 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10708 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10709 a->dw_attr_val.v.val_str->label,
10710 debug_str_section,
10711 "%s: \"%s\"", name, AT_string (a));
10712 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10713 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10714 a->dw_attr_val.v.val_str->label,
10715 debug_line_str_section,
10716 "%s: \"%s\"", name, AT_string (a));
10717 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10718 dw2_asm_output_data_uleb128 (AT_index (a),
10719 "%s: \"%s\"", name, AT_string (a));
10720 else
10721 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10722 break;
10724 case dw_val_class_file:
10726 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10728 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10729 a->dw_attr_val.v.val_file->filename);
10730 break;
10733 case dw_val_class_file_implicit:
10734 if (flag_debug_asm)
10735 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10736 ASM_COMMENT_START, name,
10737 maybe_emit_file (a->dw_attr_val.v.val_file),
10738 a->dw_attr_val.v.val_file->filename);
10739 break;
10741 case dw_val_class_data8:
10743 int i;
10745 for (i = 0; i < 8; i++)
10746 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10747 i == 0 ? "%s" : NULL, name);
10748 break;
10751 case dw_val_class_high_pc:
10752 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10753 get_AT_low_pc (die), "DW_AT_high_pc");
10754 break;
10756 case dw_val_class_discr_value:
10757 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10758 break;
10760 case dw_val_class_discr_list:
10762 dw_discr_list_ref list = AT_discr_list (a);
10763 const int size = size_of_discr_list (list);
10765 /* This is a block, so output its length first. */
10766 dw2_asm_output_data (constant_size (size), size,
10767 "%s: block size", name);
10769 for (; list != NULL; list = list->dw_discr_next)
10771 /* One byte for the discriminant value descriptor, and then as
10772 many LEB128 numbers as required. */
10773 if (list->dw_discr_range)
10774 dw2_asm_output_data (1, DW_DSC_range,
10775 "%s: DW_DSC_range", name);
10776 else
10777 dw2_asm_output_data (1, DW_DSC_label,
10778 "%s: DW_DSC_label", name);
10780 output_discr_value (&list->dw_discr_lower_bound, name);
10781 if (list->dw_discr_range)
10782 output_discr_value (&list->dw_discr_upper_bound, name);
10784 break;
10787 default:
10788 gcc_unreachable ();
10792 FOR_EACH_CHILD (die, c, output_die (c));
10794 /* Add null byte to terminate sibling list. */
10795 if (die->die_child != NULL)
10796 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10797 (unsigned long) die->die_offset);
10800 /* Output the dwarf version number. */
10802 static void
10803 output_dwarf_version ()
10805 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10806 views in loclist. That will change eventually. */
10807 if (dwarf_version == 6)
10809 static bool once;
10810 if (!once)
10812 warning (0,
10813 "-gdwarf-6 is output as version 5 with incompatibilities");
10814 once = true;
10816 dw2_asm_output_data (2, 5, "DWARF version number");
10818 else
10819 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10822 /* Output the compilation unit that appears at the beginning of the
10823 .debug_info section, and precedes the DIE descriptions. */
10825 static void
10826 output_compilation_unit_header (enum dwarf_unit_type ut)
10828 if (!XCOFF_DEBUGGING_INFO)
10830 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10831 dw2_asm_output_data (4, 0xffffffff,
10832 "Initial length escape value indicating 64-bit DWARF extension");
10833 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10834 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10835 "Length of Compilation Unit Info");
10838 output_dwarf_version ();
10839 if (dwarf_version >= 5)
10841 const char *name;
10842 switch (ut)
10844 case DW_UT_compile: name = "DW_UT_compile"; break;
10845 case DW_UT_type: name = "DW_UT_type"; break;
10846 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10847 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10848 default: gcc_unreachable ();
10850 dw2_asm_output_data (1, ut, "%s", name);
10851 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10853 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10854 debug_abbrev_section,
10855 "Offset Into Abbrev. Section");
10856 if (dwarf_version < 5)
10857 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10860 /* Output the compilation unit DIE and its children. */
10862 static void
10863 output_comp_unit (dw_die_ref die, int output_if_empty,
10864 const unsigned char *dwo_id)
10866 const char *secname, *oldsym;
10867 char *tmp;
10869 /* Unless we are outputting main CU, we may throw away empty ones. */
10870 if (!output_if_empty && die->die_child == NULL)
10871 return;
10873 /* Even if there are no children of this DIE, we must output the information
10874 about the compilation unit. Otherwise, on an empty translation unit, we
10875 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10876 will then complain when examining the file. First mark all the DIEs in
10877 this CU so we know which get local refs. */
10878 mark_dies (die);
10880 external_ref_hash_type *extern_map = optimize_external_refs (die);
10882 /* For now, optimize only the main CU, in order to optimize the rest
10883 we'd need to see all of them earlier. Leave the rest for post-linking
10884 tools like DWZ. */
10885 if (die == comp_unit_die ())
10886 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10888 build_abbrev_table (die, extern_map);
10890 optimize_abbrev_table ();
10892 delete extern_map;
10894 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10895 next_die_offset = (dwo_id
10896 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10897 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10898 calc_die_sizes (die);
10900 oldsym = die->die_id.die_symbol;
10901 if (oldsym && die->comdat_type_p)
10903 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10905 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10906 secname = tmp;
10907 die->die_id.die_symbol = NULL;
10908 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10910 else
10912 switch_to_section (debug_info_section);
10913 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10914 info_section_emitted = true;
10917 /* For LTO cross unit DIE refs we want a symbol on the start of the
10918 debuginfo section, not on the CU DIE. */
10919 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10921 /* ??? No way to get visibility assembled without a decl. */
10922 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10923 get_identifier (oldsym), char_type_node);
10924 TREE_PUBLIC (decl) = true;
10925 TREE_STATIC (decl) = true;
10926 DECL_ARTIFICIAL (decl) = true;
10927 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10928 DECL_VISIBILITY_SPECIFIED (decl) = true;
10929 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10930 #ifdef ASM_WEAKEN_LABEL
10931 /* We prefer a .weak because that handles duplicates from duplicate
10932 archive members in a graceful way. */
10933 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10934 #else
10935 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10936 #endif
10937 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10940 /* Output debugging information. */
10941 output_compilation_unit_header (dwo_id
10942 ? DW_UT_split_compile : DW_UT_compile);
10943 if (dwarf_version >= 5)
10945 if (dwo_id != NULL)
10946 for (int i = 0; i < 8; i++)
10947 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10949 output_die (die);
10951 /* Leave the marks on the main CU, so we can check them in
10952 output_pubnames. */
10953 if (oldsym)
10955 unmark_dies (die);
10956 die->die_id.die_symbol = oldsym;
10960 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10961 and .debug_pubtypes. This is configured per-target, but can be
10962 overridden by the -gpubnames or -gno-pubnames options. */
10964 static inline bool
10965 want_pubnames (void)
10967 if (debug_info_level <= DINFO_LEVEL_TERSE)
10968 return false;
10969 if (debug_generate_pub_sections != -1)
10970 return debug_generate_pub_sections;
10971 return targetm.want_debug_pub_sections;
10974 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10976 static void
10977 add_AT_pubnames (dw_die_ref die)
10979 if (want_pubnames ())
10980 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10983 /* Add a string attribute value to a skeleton DIE. */
10985 static inline void
10986 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10987 const char *str)
10989 dw_attr_node attr;
10990 struct indirect_string_node *node;
10992 if (! skeleton_debug_str_hash)
10993 skeleton_debug_str_hash
10994 = hash_table<indirect_string_hasher>::create_ggc (10);
10996 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10997 find_string_form (node);
10998 if (node->form == DW_FORM_GNU_str_index)
10999 node->form = DW_FORM_strp;
11001 attr.dw_attr = attr_kind;
11002 attr.dw_attr_val.val_class = dw_val_class_str;
11003 attr.dw_attr_val.val_entry = NULL;
11004 attr.dw_attr_val.v.val_str = node;
11005 add_dwarf_attr (die, &attr);
11008 /* Helper function to generate top-level dies for skeleton debug_info and
11009 debug_types. */
11011 static void
11012 add_top_level_skeleton_die_attrs (dw_die_ref die)
11014 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11015 const char *comp_dir = comp_dir_string ();
11017 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11018 if (comp_dir != NULL)
11019 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11020 add_AT_pubnames (die);
11021 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
11024 /* Output skeleton debug sections that point to the dwo file. */
11026 static void
11027 output_skeleton_debug_sections (dw_die_ref comp_unit,
11028 const unsigned char *dwo_id)
11030 /* These attributes will be found in the full debug_info section. */
11031 remove_AT (comp_unit, DW_AT_producer);
11032 remove_AT (comp_unit, DW_AT_language);
11034 switch_to_section (debug_skeleton_info_section);
11035 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11037 /* Produce the skeleton compilation-unit header. This one differs enough from
11038 a normal CU header that it's better not to call output_compilation_unit
11039 header. */
11040 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11041 dw2_asm_output_data (4, 0xffffffff,
11042 "Initial length escape value indicating 64-bit "
11043 "DWARF extension");
11045 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11046 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11047 - DWARF_INITIAL_LENGTH_SIZE
11048 + size_of_die (comp_unit),
11049 "Length of Compilation Unit Info");
11050 output_dwarf_version ();
11051 if (dwarf_version >= 5)
11053 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11054 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11056 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11057 debug_skeleton_abbrev_section,
11058 "Offset Into Abbrev. Section");
11059 if (dwarf_version < 5)
11060 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11061 else
11062 for (int i = 0; i < 8; i++)
11063 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11065 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11066 output_die (comp_unit);
11068 /* Build the skeleton debug_abbrev section. */
11069 switch_to_section (debug_skeleton_abbrev_section);
11070 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11072 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11074 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11077 /* Output a comdat type unit DIE and its children. */
11079 static void
11080 output_comdat_type_unit (comdat_type_node *node)
11082 const char *secname;
11083 char *tmp;
11084 int i;
11085 #if defined (OBJECT_FORMAT_ELF)
11086 tree comdat_key;
11087 #endif
11089 /* First mark all the DIEs in this CU so we know which get local refs. */
11090 mark_dies (node->root_die);
11092 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11094 build_abbrev_table (node->root_die, extern_map);
11096 delete extern_map;
11097 extern_map = NULL;
11099 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11100 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11101 calc_die_sizes (node->root_die);
11103 #if defined (OBJECT_FORMAT_ELF)
11104 if (dwarf_version >= 5)
11106 if (!dwarf_split_debug_info)
11107 secname = ".debug_info";
11108 else
11109 secname = ".debug_info.dwo";
11111 else if (!dwarf_split_debug_info)
11112 secname = ".debug_types";
11113 else
11114 secname = ".debug_types.dwo";
11116 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11117 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11118 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11119 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11120 comdat_key = get_identifier (tmp);
11121 targetm.asm_out.named_section (secname,
11122 SECTION_DEBUG | SECTION_LINKONCE,
11123 comdat_key);
11124 #else
11125 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11126 sprintf (tmp, (dwarf_version >= 5
11127 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11128 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11129 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11130 secname = tmp;
11131 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11132 #endif
11134 /* Output debugging information. */
11135 output_compilation_unit_header (dwarf_split_debug_info
11136 ? DW_UT_split_type : DW_UT_type);
11137 output_signature (node->signature, "Type Signature");
11138 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11139 "Offset to Type DIE");
11140 output_die (node->root_die);
11142 unmark_dies (node->root_die);
11145 /* Return the DWARF2/3 pubname associated with a decl. */
11147 static const char *
11148 dwarf2_name (tree decl, int scope)
11150 if (DECL_NAMELESS (decl))
11151 return NULL;
11152 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11155 /* Add a new entry to .debug_pubnames if appropriate. */
11157 static void
11158 add_pubname_string (const char *str, dw_die_ref die)
11160 pubname_entry e;
11162 e.die = die;
11163 e.name = xstrdup (str);
11164 vec_safe_push (pubname_table, e);
11167 static void
11168 add_pubname (tree decl, dw_die_ref die)
11170 if (!want_pubnames ())
11171 return;
11173 /* Don't add items to the table when we expect that the consumer will have
11174 just read the enclosing die. For example, if the consumer is looking at a
11175 class_member, it will either be inside the class already, or will have just
11176 looked up the class to find the member. Either way, searching the class is
11177 faster than searching the index. */
11178 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11179 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11181 const char *name = dwarf2_name (decl, 1);
11183 if (name)
11184 add_pubname_string (name, die);
11188 /* Add an enumerator to the pubnames section. */
11190 static void
11191 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11193 pubname_entry e;
11195 gcc_assert (scope_name);
11196 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11197 e.die = die;
11198 vec_safe_push (pubname_table, e);
11201 /* Add a new entry to .debug_pubtypes if appropriate. */
11203 static void
11204 add_pubtype (tree decl, dw_die_ref die)
11206 pubname_entry e;
11208 if (!want_pubnames ())
11209 return;
11211 if ((TREE_PUBLIC (decl)
11212 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11213 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11215 tree scope = NULL;
11216 const char *scope_name = "";
11217 const char *sep = is_cxx () ? "::" : ".";
11218 const char *name;
11220 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11221 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11223 scope_name = lang_hooks.dwarf_name (scope, 1);
11224 if (scope_name != NULL && scope_name[0] != '\0')
11225 scope_name = concat (scope_name, sep, NULL);
11226 else
11227 scope_name = "";
11230 if (TYPE_P (decl))
11231 name = type_tag (decl);
11232 else
11233 name = lang_hooks.dwarf_name (decl, 1);
11235 /* If we don't have a name for the type, there's no point in adding
11236 it to the table. */
11237 if (name != NULL && name[0] != '\0')
11239 e.die = die;
11240 e.name = concat (scope_name, name, NULL);
11241 vec_safe_push (pubtype_table, e);
11244 /* Although it might be more consistent to add the pubinfo for the
11245 enumerators as their dies are created, they should only be added if the
11246 enum type meets the criteria above. So rather than re-check the parent
11247 enum type whenever an enumerator die is created, just output them all
11248 here. This isn't protected by the name conditional because anonymous
11249 enums don't have names. */
11250 if (die->die_tag == DW_TAG_enumeration_type)
11252 dw_die_ref c;
11254 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11259 /* Output a single entry in the pubnames table. */
11261 static void
11262 output_pubname (dw_offset die_offset, pubname_entry *entry)
11264 dw_die_ref die = entry->die;
11265 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11267 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11269 if (debug_generate_pub_sections == 2)
11271 /* This logic follows gdb's method for determining the value of the flag
11272 byte. */
11273 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11274 switch (die->die_tag)
11276 case DW_TAG_typedef:
11277 case DW_TAG_base_type:
11278 case DW_TAG_subrange_type:
11279 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11280 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11281 break;
11282 case DW_TAG_enumerator:
11283 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11284 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11285 if (!is_cxx ())
11286 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11287 break;
11288 case DW_TAG_subprogram:
11289 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11290 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11291 if (!is_ada ())
11292 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11293 break;
11294 case DW_TAG_constant:
11295 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11296 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11297 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11298 break;
11299 case DW_TAG_variable:
11300 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11301 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11302 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11303 break;
11304 case DW_TAG_namespace:
11305 case DW_TAG_imported_declaration:
11306 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11307 break;
11308 case DW_TAG_class_type:
11309 case DW_TAG_interface_type:
11310 case DW_TAG_structure_type:
11311 case DW_TAG_union_type:
11312 case DW_TAG_enumeration_type:
11313 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11314 if (!is_cxx ())
11315 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11316 break;
11317 default:
11318 /* An unusual tag. Leave the flag-byte empty. */
11319 break;
11321 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11322 "GDB-index flags");
11325 dw2_asm_output_nstring (entry->name, -1, "external name");
11329 /* Output the public names table used to speed up access to externally
11330 visible names; or the public types table used to find type definitions. */
11332 static void
11333 output_pubnames (vec<pubname_entry, va_gc> *names)
11335 unsigned i;
11336 unsigned long pubnames_length = size_of_pubnames (names);
11337 pubname_entry *pub;
11339 if (!XCOFF_DEBUGGING_INFO)
11341 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11342 dw2_asm_output_data (4, 0xffffffff,
11343 "Initial length escape value indicating 64-bit DWARF extension");
11344 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11345 "Pub Info Length");
11348 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11349 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11351 if (dwarf_split_debug_info)
11352 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11353 debug_skeleton_info_section,
11354 "Offset of Compilation Unit Info");
11355 else
11356 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11357 debug_info_section,
11358 "Offset of Compilation Unit Info");
11359 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11360 "Compilation Unit Length");
11362 FOR_EACH_VEC_ELT (*names, i, pub)
11364 if (include_pubname_in_output (names, pub))
11366 dw_offset die_offset = pub->die->die_offset;
11368 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11369 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11370 gcc_assert (pub->die->die_mark);
11372 /* If we're putting types in their own .debug_types sections,
11373 the .debug_pubtypes table will still point to the compile
11374 unit (not the type unit), so we want to use the offset of
11375 the skeleton DIE (if there is one). */
11376 if (pub->die->comdat_type_p && names == pubtype_table)
11378 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11380 if (type_node != NULL)
11381 die_offset = (type_node->skeleton_die != NULL
11382 ? type_node->skeleton_die->die_offset
11383 : comp_unit_die ()->die_offset);
11386 output_pubname (die_offset, pub);
11390 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11393 /* Output public names and types tables if necessary. */
11395 static void
11396 output_pubtables (void)
11398 if (!want_pubnames () || !info_section_emitted)
11399 return;
11401 switch_to_section (debug_pubnames_section);
11402 output_pubnames (pubname_table);
11403 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11404 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11405 simply won't look for the section. */
11406 switch_to_section (debug_pubtypes_section);
11407 output_pubnames (pubtype_table);
11411 /* Output the information that goes into the .debug_aranges table.
11412 Namely, define the beginning and ending address range of the
11413 text section generated for this compilation unit. */
11415 static void
11416 output_aranges (void)
11418 unsigned i;
11419 unsigned long aranges_length = size_of_aranges ();
11421 if (!XCOFF_DEBUGGING_INFO)
11423 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11424 dw2_asm_output_data (4, 0xffffffff,
11425 "Initial length escape value indicating 64-bit DWARF extension");
11426 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11427 "Length of Address Ranges Info");
11430 /* Version number for aranges is still 2, even up to DWARF5. */
11431 dw2_asm_output_data (2, 2, "DWARF aranges version");
11432 if (dwarf_split_debug_info)
11433 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11434 debug_skeleton_info_section,
11435 "Offset of Compilation Unit Info");
11436 else
11437 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11438 debug_info_section,
11439 "Offset of Compilation Unit Info");
11440 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11441 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11443 /* We need to align to twice the pointer size here. */
11444 if (DWARF_ARANGES_PAD_SIZE)
11446 /* Pad using a 2 byte words so that padding is correct for any
11447 pointer size. */
11448 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11449 2 * DWARF2_ADDR_SIZE);
11450 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11451 dw2_asm_output_data (2, 0, NULL);
11454 /* It is necessary not to output these entries if the sections were
11455 not used; if the sections were not used, the length will be 0 and
11456 the address may end up as 0 if the section is discarded by ld
11457 --gc-sections, leaving an invalid (0, 0) entry that can be
11458 confused with the terminator. */
11459 if (text_section_used)
11461 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11462 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11463 text_section_label, "Length");
11465 if (cold_text_section_used)
11467 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11468 "Address");
11469 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11470 cold_text_section_label, "Length");
11473 if (have_multiple_function_sections)
11475 unsigned fde_idx;
11476 dw_fde_ref fde;
11478 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11480 if (DECL_IGNORED_P (fde->decl))
11481 continue;
11482 if (!fde->in_std_section)
11484 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11485 "Address");
11486 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11487 fde->dw_fde_begin, "Length");
11489 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11491 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11492 "Address");
11493 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11494 fde->dw_fde_second_begin, "Length");
11499 /* Output the terminator words. */
11500 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11501 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11504 /* Add a new entry to .debug_ranges. Return its index into
11505 ranges_table vector. */
11507 static unsigned int
11508 add_ranges_num (int num, bool maybe_new_sec)
11510 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11511 vec_safe_push (ranges_table, r);
11512 return vec_safe_length (ranges_table) - 1;
11515 /* Add a new entry to .debug_ranges corresponding to a block, or a
11516 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11517 this entry might be in a different section from previous range. */
11519 static unsigned int
11520 add_ranges (const_tree block, bool maybe_new_sec)
11522 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11525 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11526 chain, or middle entry of a chain that will be directly referred to. */
11528 static void
11529 note_rnglist_head (unsigned int offset)
11531 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11532 return;
11533 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11536 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11537 When using dwarf_split_debug_info, address attributes in dies destined
11538 for the final executable should be direct references--setting the
11539 parameter force_direct ensures this behavior. */
11541 static void
11542 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11543 bool *added, bool force_direct)
11545 unsigned int in_use = vec_safe_length (ranges_by_label);
11546 unsigned int offset;
11547 dw_ranges_by_label rbl = { begin, end };
11548 vec_safe_push (ranges_by_label, rbl);
11549 offset = add_ranges_num (-(int)in_use - 1, true);
11550 if (!*added)
11552 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11553 *added = true;
11554 note_rnglist_head (offset);
11558 /* Emit .debug_ranges section. */
11560 static void
11561 output_ranges (void)
11563 unsigned i;
11564 static const char *const start_fmt = "Offset %#x";
11565 const char *fmt = start_fmt;
11566 dw_ranges *r;
11568 switch_to_section (debug_ranges_section);
11569 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11570 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11572 int block_num = r->num;
11574 if (block_num > 0)
11576 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11577 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11579 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11580 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11582 /* If all code is in the text section, then the compilation
11583 unit base address defaults to DW_AT_low_pc, which is the
11584 base of the text section. */
11585 if (!have_multiple_function_sections)
11587 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11588 text_section_label,
11589 fmt, i * 2 * DWARF2_ADDR_SIZE);
11590 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11591 text_section_label, NULL);
11594 /* Otherwise, the compilation unit base address is zero,
11595 which allows us to use absolute addresses, and not worry
11596 about whether the target supports cross-section
11597 arithmetic. */
11598 else
11600 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11601 fmt, i * 2 * DWARF2_ADDR_SIZE);
11602 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11605 fmt = NULL;
11608 /* Negative block_num stands for an index into ranges_by_label. */
11609 else if (block_num < 0)
11611 int lab_idx = - block_num - 1;
11613 if (!have_multiple_function_sections)
11615 gcc_unreachable ();
11616 #if 0
11617 /* If we ever use add_ranges_by_labels () for a single
11618 function section, all we have to do is to take out
11619 the #if 0 above. */
11620 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11621 (*ranges_by_label)[lab_idx].begin,
11622 text_section_label,
11623 fmt, i * 2 * DWARF2_ADDR_SIZE);
11624 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11625 (*ranges_by_label)[lab_idx].end,
11626 text_section_label, NULL);
11627 #endif
11629 else
11631 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11632 (*ranges_by_label)[lab_idx].begin,
11633 fmt, i * 2 * DWARF2_ADDR_SIZE);
11634 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11635 (*ranges_by_label)[lab_idx].end,
11636 NULL);
11639 else
11641 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11642 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11643 fmt = start_fmt;
11648 /* Non-zero if .debug_line_str should be used for .debug_line section
11649 strings or strings that are likely shareable with those. */
11650 #define DWARF5_USE_DEBUG_LINE_STR \
11651 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11652 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11653 /* FIXME: there is no .debug_line_str.dwo section, \
11654 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11655 && !dwarf_split_debug_info)
11657 /* Assign .debug_rnglists indexes. */
11659 static void
11660 index_rnglists (void)
11662 unsigned i;
11663 dw_ranges *r;
11665 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11666 if (r->label)
11667 r->idx = rnglist_idx++;
11670 /* Emit .debug_rnglists section. */
11672 static void
11673 output_rnglists (unsigned generation)
11675 unsigned i;
11676 dw_ranges *r;
11677 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11678 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11679 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11681 switch_to_section (debug_ranges_section);
11682 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11683 /* There are up to 4 unique ranges labels per generation.
11684 See also init_sections_and_labels. */
11685 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11686 2 + generation * 4);
11687 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11688 3 + generation * 4);
11689 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11690 dw2_asm_output_data (4, 0xffffffff,
11691 "Initial length escape value indicating "
11692 "64-bit DWARF extension");
11693 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11694 "Length of Range Lists");
11695 ASM_OUTPUT_LABEL (asm_out_file, l1);
11696 output_dwarf_version ();
11697 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11698 dw2_asm_output_data (1, 0, "Segment Size");
11699 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11700 about relocation sizes and primarily care about the size of .debug*
11701 sections in linked shared libraries and executables, then
11702 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11703 into it are usually larger than just DW_FORM_sec_offset offsets
11704 into the .debug_rnglists section. */
11705 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11706 "Offset Entry Count");
11707 if (dwarf_split_debug_info)
11709 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11710 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11711 if (r->label)
11712 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11713 ranges_base_label, NULL);
11716 const char *lab = "";
11717 unsigned int len = vec_safe_length (ranges_table);
11718 const char *base = NULL;
11719 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11721 int block_num = r->num;
11723 if (r->label)
11725 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11726 lab = r->label;
11728 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11729 base = NULL;
11730 if (block_num > 0)
11732 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11733 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11735 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11736 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11738 if (HAVE_AS_LEB128)
11740 /* If all code is in the text section, then the compilation
11741 unit base address defaults to DW_AT_low_pc, which is the
11742 base of the text section. */
11743 if (!have_multiple_function_sections)
11745 dw2_asm_output_data (1, DW_RLE_offset_pair,
11746 "DW_RLE_offset_pair (%s)", lab);
11747 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11748 "Range begin address (%s)", lab);
11749 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11750 "Range end address (%s)", lab);
11751 continue;
11753 if (base == NULL)
11755 dw_ranges *r2 = NULL;
11756 if (i < len - 1)
11757 r2 = &(*ranges_table)[i + 1];
11758 if (r2
11759 && r2->num != 0
11760 && r2->label == NULL
11761 && !r2->maybe_new_sec)
11763 dw2_asm_output_data (1, DW_RLE_base_address,
11764 "DW_RLE_base_address (%s)", lab);
11765 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11766 "Base address (%s)", lab);
11767 strcpy (basebuf, blabel);
11768 base = basebuf;
11771 if (base)
11773 dw2_asm_output_data (1, DW_RLE_offset_pair,
11774 "DW_RLE_offset_pair (%s)", lab);
11775 dw2_asm_output_delta_uleb128 (blabel, base,
11776 "Range begin address (%s)", lab);
11777 dw2_asm_output_delta_uleb128 (elabel, base,
11778 "Range end address (%s)", lab);
11779 continue;
11781 dw2_asm_output_data (1, DW_RLE_start_length,
11782 "DW_RLE_start_length (%s)", lab);
11783 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11784 "Range begin address (%s)", lab);
11785 dw2_asm_output_delta_uleb128 (elabel, blabel,
11786 "Range length (%s)", lab);
11788 else
11790 dw2_asm_output_data (1, DW_RLE_start_end,
11791 "DW_RLE_start_end (%s)", lab);
11792 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11793 "Range begin address (%s)", lab);
11794 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11795 "Range end address (%s)", lab);
11799 /* Negative block_num stands for an index into ranges_by_label. */
11800 else if (block_num < 0)
11802 int lab_idx = - block_num - 1;
11803 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11804 const char *elabel = (*ranges_by_label)[lab_idx].end;
11806 if (!have_multiple_function_sections)
11807 gcc_unreachable ();
11808 if (HAVE_AS_LEB128)
11810 dw2_asm_output_data (1, DW_RLE_start_length,
11811 "DW_RLE_start_length (%s)", lab);
11812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11813 "Range begin address (%s)", lab);
11814 dw2_asm_output_delta_uleb128 (elabel, blabel,
11815 "Range length (%s)", lab);
11817 else
11819 dw2_asm_output_data (1, DW_RLE_start_end,
11820 "DW_RLE_start_end (%s)", lab);
11821 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11822 "Range begin address (%s)", lab);
11823 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11824 "Range end address (%s)", lab);
11827 else
11828 dw2_asm_output_data (1, DW_RLE_end_of_list,
11829 "DW_RLE_end_of_list (%s)", lab);
11831 ASM_OUTPUT_LABEL (asm_out_file, l2);
11834 /* Data structure containing information about input files. */
11835 struct file_info
11837 const char *path; /* Complete file name. */
11838 const char *fname; /* File name part. */
11839 int length; /* Length of entire string. */
11840 struct dwarf_file_data * file_idx; /* Index in input file table. */
11841 int dir_idx; /* Index in directory table. */
11844 /* Data structure containing information about directories with source
11845 files. */
11846 struct dir_info
11848 const char *path; /* Path including directory name. */
11849 int length; /* Path length. */
11850 int prefix; /* Index of directory entry which is a prefix. */
11851 int count; /* Number of files in this directory. */
11852 int dir_idx; /* Index of directory used as base. */
11855 /* Callback function for file_info comparison. We sort by looking at
11856 the directories in the path. */
11858 static int
11859 file_info_cmp (const void *p1, const void *p2)
11861 const struct file_info *const s1 = (const struct file_info *) p1;
11862 const struct file_info *const s2 = (const struct file_info *) p2;
11863 const unsigned char *cp1;
11864 const unsigned char *cp2;
11866 /* Take care of file names without directories. We need to make sure that
11867 we return consistent values to qsort since some will get confused if
11868 we return the same value when identical operands are passed in opposite
11869 orders. So if neither has a directory, return 0 and otherwise return
11870 1 or -1 depending on which one has the directory. */
11871 if ((s1->path == s1->fname || s2->path == s2->fname))
11872 return (s2->path == s2->fname) - (s1->path == s1->fname);
11874 cp1 = (const unsigned char *) s1->path;
11875 cp2 = (const unsigned char *) s2->path;
11877 while (1)
11879 ++cp1;
11880 ++cp2;
11881 /* Reached the end of the first path? If so, handle like above. */
11882 if ((cp1 == (const unsigned char *) s1->fname)
11883 || (cp2 == (const unsigned char *) s2->fname))
11884 return ((cp2 == (const unsigned char *) s2->fname)
11885 - (cp1 == (const unsigned char *) s1->fname));
11887 /* Character of current path component the same? */
11888 else if (*cp1 != *cp2)
11889 return *cp1 - *cp2;
11893 struct file_name_acquire_data
11895 struct file_info *files;
11896 int used_files;
11897 int max_files;
11900 /* Traversal function for the hash table. */
11903 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11905 struct dwarf_file_data *d = *slot;
11906 struct file_info *fi;
11907 const char *f;
11909 gcc_assert (fnad->max_files >= d->emitted_number);
11911 if (! d->emitted_number)
11912 return 1;
11914 gcc_assert (fnad->max_files != fnad->used_files);
11916 fi = fnad->files + fnad->used_files++;
11918 /* Skip all leading "./". */
11919 f = d->filename;
11920 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11921 f += 2;
11923 /* Create a new array entry. */
11924 fi->path = f;
11925 fi->length = strlen (f);
11926 fi->file_idx = d;
11928 /* Search for the file name part. */
11929 f = strrchr (f, DIR_SEPARATOR);
11930 #if defined (DIR_SEPARATOR_2)
11932 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11934 if (g != NULL)
11936 if (f == NULL || f < g)
11937 f = g;
11940 #endif
11942 fi->fname = f == NULL ? fi->path : f + 1;
11943 return 1;
11946 /* Helper function for output_file_names. Emit a FORM encoded
11947 string STR, with assembly comment start ENTRY_KIND and
11948 index IDX */
11950 static void
11951 output_line_string (enum dwarf_form form, const char *str,
11952 const char *entry_kind, unsigned int idx)
11954 switch (form)
11956 case DW_FORM_string:
11957 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11958 break;
11959 case DW_FORM_line_strp:
11960 if (!debug_line_str_hash)
11961 debug_line_str_hash
11962 = hash_table<indirect_string_hasher>::create_ggc (10);
11964 struct indirect_string_node *node;
11965 node = find_AT_string_in_table (str, debug_line_str_hash);
11966 set_indirect_string (node);
11967 node->form = form;
11968 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11969 debug_line_str_section, "%s: %#x: \"%s\"",
11970 entry_kind, 0, node->str);
11971 break;
11972 default:
11973 gcc_unreachable ();
11977 /* Output the directory table and the file name table. We try to minimize
11978 the total amount of memory needed. A heuristic is used to avoid large
11979 slowdowns with many input files. */
11981 static void
11982 output_file_names (void)
11984 struct file_name_acquire_data fnad;
11985 int numfiles;
11986 struct file_info *files;
11987 struct dir_info *dirs;
11988 int *saved;
11989 int *savehere;
11990 int *backmap;
11991 int ndirs;
11992 int idx_offset;
11993 int i;
11995 if (!last_emitted_file)
11997 if (dwarf_version >= 5)
11999 dw2_asm_output_data (1, 0, "Directory entry format count");
12000 dw2_asm_output_data_uleb128 (0, "Directories count");
12001 dw2_asm_output_data (1, 0, "File name entry format count");
12002 dw2_asm_output_data_uleb128 (0, "File names count");
12004 else
12006 dw2_asm_output_data (1, 0, "End directory table");
12007 dw2_asm_output_data (1, 0, "End file name table");
12009 return;
12012 numfiles = last_emitted_file->emitted_number;
12014 /* Allocate the various arrays we need. */
12015 files = XALLOCAVEC (struct file_info, numfiles);
12016 dirs = XALLOCAVEC (struct dir_info, numfiles);
12018 fnad.files = files;
12019 fnad.used_files = 0;
12020 fnad.max_files = numfiles;
12021 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12022 gcc_assert (fnad.used_files == fnad.max_files);
12024 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12026 /* Find all the different directories used. */
12027 dirs[0].path = files[0].path;
12028 dirs[0].length = files[0].fname - files[0].path;
12029 dirs[0].prefix = -1;
12030 dirs[0].count = 1;
12031 dirs[0].dir_idx = 0;
12032 files[0].dir_idx = 0;
12033 ndirs = 1;
12035 for (i = 1; i < numfiles; i++)
12036 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12037 && memcmp (dirs[ndirs - 1].path, files[i].path,
12038 dirs[ndirs - 1].length) == 0)
12040 /* Same directory as last entry. */
12041 files[i].dir_idx = ndirs - 1;
12042 ++dirs[ndirs - 1].count;
12044 else
12046 int j;
12048 /* This is a new directory. */
12049 dirs[ndirs].path = files[i].path;
12050 dirs[ndirs].length = files[i].fname - files[i].path;
12051 dirs[ndirs].count = 1;
12052 dirs[ndirs].dir_idx = ndirs;
12053 files[i].dir_idx = ndirs;
12055 /* Search for a prefix. */
12056 dirs[ndirs].prefix = -1;
12057 for (j = 0; j < ndirs; j++)
12058 if (dirs[j].length < dirs[ndirs].length
12059 && dirs[j].length > 1
12060 && (dirs[ndirs].prefix == -1
12061 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12062 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12063 dirs[ndirs].prefix = j;
12065 ++ndirs;
12068 /* Now to the actual work. We have to find a subset of the directories which
12069 allow expressing the file name using references to the directory table
12070 with the least amount of characters. We do not do an exhaustive search
12071 where we would have to check out every combination of every single
12072 possible prefix. Instead we use a heuristic which provides nearly optimal
12073 results in most cases and never is much off. */
12074 saved = XALLOCAVEC (int, ndirs);
12075 savehere = XALLOCAVEC (int, ndirs);
12077 memset (saved, '\0', ndirs * sizeof (saved[0]));
12078 for (i = 0; i < ndirs; i++)
12080 int j;
12081 int total;
12083 /* We can always save some space for the current directory. But this
12084 does not mean it will be enough to justify adding the directory. */
12085 savehere[i] = dirs[i].length;
12086 total = (savehere[i] - saved[i]) * dirs[i].count;
12088 for (j = i + 1; j < ndirs; j++)
12090 savehere[j] = 0;
12091 if (saved[j] < dirs[i].length)
12093 /* Determine whether the dirs[i] path is a prefix of the
12094 dirs[j] path. */
12095 int k;
12097 k = dirs[j].prefix;
12098 while (k != -1 && k != (int) i)
12099 k = dirs[k].prefix;
12101 if (k == (int) i)
12103 /* Yes it is. We can possibly save some memory by
12104 writing the filenames in dirs[j] relative to
12105 dirs[i]. */
12106 savehere[j] = dirs[i].length;
12107 total += (savehere[j] - saved[j]) * dirs[j].count;
12112 /* Check whether we can save enough to justify adding the dirs[i]
12113 directory. */
12114 if (total > dirs[i].length + 1)
12116 /* It's worthwhile adding. */
12117 for (j = i; j < ndirs; j++)
12118 if (savehere[j] > 0)
12120 /* Remember how much we saved for this directory so far. */
12121 saved[j] = savehere[j];
12123 /* Remember the prefix directory. */
12124 dirs[j].dir_idx = i;
12129 /* Emit the directory name table. */
12130 idx_offset = dirs[0].length > 0 ? 1 : 0;
12131 enum dwarf_form str_form = DW_FORM_string;
12132 enum dwarf_form idx_form = DW_FORM_udata;
12133 if (dwarf_version >= 5)
12135 const char *comp_dir = comp_dir_string ();
12136 if (comp_dir == NULL)
12137 comp_dir = "";
12138 dw2_asm_output_data (1, 1, "Directory entry format count");
12139 if (DWARF5_USE_DEBUG_LINE_STR)
12140 str_form = DW_FORM_line_strp;
12141 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12142 dw2_asm_output_data_uleb128 (str_form, "%s",
12143 get_DW_FORM_name (str_form));
12144 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12145 if (str_form == DW_FORM_string)
12147 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12148 for (i = 1 - idx_offset; i < ndirs; i++)
12149 dw2_asm_output_nstring (dirs[i].path,
12150 dirs[i].length
12151 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12152 "Directory Entry: %#x", i + idx_offset);
12154 else
12156 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12157 for (i = 1 - idx_offset; i < ndirs; i++)
12159 const char *str
12160 = ggc_alloc_string (dirs[i].path,
12161 dirs[i].length
12162 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12163 output_line_string (str_form, str, "Directory Entry",
12164 (unsigned) i + idx_offset);
12168 else
12170 for (i = 1 - idx_offset; i < ndirs; i++)
12171 dw2_asm_output_nstring (dirs[i].path,
12172 dirs[i].length
12173 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12174 "Directory Entry: %#x", i + idx_offset);
12176 dw2_asm_output_data (1, 0, "End directory table");
12179 /* We have to emit them in the order of emitted_number since that's
12180 used in the debug info generation. To do this efficiently we
12181 generate a back-mapping of the indices first. */
12182 backmap = XALLOCAVEC (int, numfiles);
12183 for (i = 0; i < numfiles; i++)
12184 backmap[files[i].file_idx->emitted_number - 1] = i;
12186 if (dwarf_version >= 5)
12188 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12189 if (filename0 == NULL)
12190 filename0 = "";
12191 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12192 DW_FORM_data2. Choose one based on the number of directories
12193 and how much space would they occupy in each encoding.
12194 If we have at most 256 directories, all indexes fit into
12195 a single byte, so DW_FORM_data1 is most compact (if there
12196 are at most 128 directories, DW_FORM_udata would be as
12197 compact as that, but not shorter and slower to decode). */
12198 if (ndirs + idx_offset <= 256)
12199 idx_form = DW_FORM_data1;
12200 /* If there are more than 65536 directories, we have to use
12201 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12202 Otherwise, compute what space would occupy if all the indexes
12203 used DW_FORM_udata - sum - and compare that to how large would
12204 be DW_FORM_data2 encoding, and pick the more efficient one. */
12205 else if (ndirs + idx_offset <= 65536)
12207 unsigned HOST_WIDE_INT sum = 1;
12208 for (i = 0; i < numfiles; i++)
12210 int file_idx = backmap[i];
12211 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12212 sum += size_of_uleb128 (dir_idx);
12214 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12215 idx_form = DW_FORM_data2;
12217 #ifdef VMS_DEBUGGING_INFO
12218 dw2_asm_output_data (1, 4, "File name entry format count");
12219 #else
12220 dw2_asm_output_data (1, 2, "File name entry format count");
12221 #endif
12222 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12223 dw2_asm_output_data_uleb128 (str_form, "%s",
12224 get_DW_FORM_name (str_form));
12225 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12226 "DW_LNCT_directory_index");
12227 dw2_asm_output_data_uleb128 (idx_form, "%s",
12228 get_DW_FORM_name (idx_form));
12229 #ifdef VMS_DEBUGGING_INFO
12230 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12231 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12232 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12233 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12234 #endif
12235 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12237 output_line_string (str_form, filename0, "File Entry", 0);
12239 /* Include directory index. */
12240 if (idx_form != DW_FORM_udata)
12241 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12242 0, NULL);
12243 else
12244 dw2_asm_output_data_uleb128 (0, NULL);
12246 #ifdef VMS_DEBUGGING_INFO
12247 dw2_asm_output_data_uleb128 (0, NULL);
12248 dw2_asm_output_data_uleb128 (0, NULL);
12249 #endif
12252 /* Now write all the file names. */
12253 for (i = 0; i < numfiles; i++)
12255 int file_idx = backmap[i];
12256 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12258 #ifdef VMS_DEBUGGING_INFO
12259 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12261 /* Setting these fields can lead to debugger miscomparisons,
12262 but VMS Debug requires them to be set correctly. */
12264 int ver;
12265 long long cdt;
12266 long siz;
12267 int maxfilelen = (strlen (files[file_idx].path)
12268 + dirs[dir_idx].length
12269 + MAX_VMS_VERSION_LEN + 1);
12270 char *filebuf = XALLOCAVEC (char, maxfilelen);
12272 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12273 snprintf (filebuf, maxfilelen, "%s;%d",
12274 files[file_idx].path + dirs[dir_idx].length, ver);
12276 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12278 /* Include directory index. */
12279 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12280 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12281 dir_idx + idx_offset, NULL);
12282 else
12283 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12285 /* Modification time. */
12286 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12287 &cdt, 0, 0, 0) == 0)
12288 ? cdt : 0, NULL);
12290 /* File length in bytes. */
12291 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12292 0, &siz, 0, 0) == 0)
12293 ? siz : 0, NULL);
12294 #else
12295 output_line_string (str_form,
12296 files[file_idx].path + dirs[dir_idx].length,
12297 "File Entry", (unsigned) i + 1);
12299 /* Include directory index. */
12300 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12301 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12302 dir_idx + idx_offset, NULL);
12303 else
12304 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12306 if (dwarf_version >= 5)
12307 continue;
12309 /* Modification time. */
12310 dw2_asm_output_data_uleb128 (0, NULL);
12312 /* File length in bytes. */
12313 dw2_asm_output_data_uleb128 (0, NULL);
12314 #endif /* VMS_DEBUGGING_INFO */
12317 if (dwarf_version < 5)
12318 dw2_asm_output_data (1, 0, "End file name table");
12322 /* Output one line number table into the .debug_line section. */
12324 static void
12325 output_one_line_info_table (dw_line_info_table *table)
12327 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12328 unsigned int current_line = 1;
12329 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12330 dw_line_info_entry *ent, *prev_addr;
12331 size_t i;
12332 unsigned int view;
12334 view = 0;
12336 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12338 switch (ent->opcode)
12340 case LI_set_address:
12341 /* ??? Unfortunately, we have little choice here currently, and
12342 must always use the most general form. GCC does not know the
12343 address delta itself, so we can't use DW_LNS_advance_pc. Many
12344 ports do have length attributes which will give an upper bound
12345 on the address range. We could perhaps use length attributes
12346 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12347 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12349 view = 0;
12351 /* This can handle any delta. This takes
12352 4+DWARF2_ADDR_SIZE bytes. */
12353 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12354 debug_variable_location_views
12355 ? ", reset view to 0" : "");
12356 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12357 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12360 prev_addr = ent;
12361 break;
12363 case LI_adv_address:
12365 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12366 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12367 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12369 view++;
12371 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12372 dw2_asm_output_delta (2, line_label, prev_label,
12373 "from %s to %s", prev_label, line_label);
12375 prev_addr = ent;
12376 break;
12379 case LI_set_line:
12380 if (ent->val == current_line)
12382 /* We still need to start a new row, so output a copy insn. */
12383 dw2_asm_output_data (1, DW_LNS_copy,
12384 "copy line %u", current_line);
12386 else
12388 int line_offset = ent->val - current_line;
12389 int line_delta = line_offset - DWARF_LINE_BASE;
12391 current_line = ent->val;
12392 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12394 /* This can handle deltas from -10 to 234, using the current
12395 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12396 This takes 1 byte. */
12397 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12398 "line %u", current_line);
12400 else
12402 /* This can handle any delta. This takes at least 4 bytes,
12403 depending on the value being encoded. */
12404 dw2_asm_output_data (1, DW_LNS_advance_line,
12405 "advance to line %u", current_line);
12406 dw2_asm_output_data_sleb128 (line_offset, NULL);
12407 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12410 break;
12412 case LI_set_file:
12413 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12414 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12415 break;
12417 case LI_set_column:
12418 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12419 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12420 break;
12422 case LI_negate_stmt:
12423 current_is_stmt = !current_is_stmt;
12424 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12425 "is_stmt %d", current_is_stmt);
12426 break;
12428 case LI_set_prologue_end:
12429 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12430 "set prologue end");
12431 break;
12433 case LI_set_epilogue_begin:
12434 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12435 "set epilogue begin");
12436 break;
12438 case LI_set_discriminator:
12439 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12440 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12441 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12442 dw2_asm_output_data_uleb128 (ent->val, NULL);
12443 break;
12447 /* Emit debug info for the address of the end of the table. */
12448 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12449 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12450 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12451 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12453 dw2_asm_output_data (1, 0, "end sequence");
12454 dw2_asm_output_data_uleb128 (1, NULL);
12455 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12458 /* Output the source line number correspondence information. This
12459 information goes into the .debug_line section. */
12461 static void
12462 output_line_info (bool prologue_only)
12464 static unsigned int generation;
12465 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12466 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12467 bool saw_one = false;
12468 int opc;
12470 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12471 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12472 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12473 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12475 if (!XCOFF_DEBUGGING_INFO)
12477 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12478 dw2_asm_output_data (4, 0xffffffff,
12479 "Initial length escape value indicating 64-bit DWARF extension");
12480 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12481 "Length of Source Line Info");
12484 ASM_OUTPUT_LABEL (asm_out_file, l1);
12486 output_dwarf_version ();
12487 if (dwarf_version >= 5)
12489 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12490 dw2_asm_output_data (1, 0, "Segment Size");
12492 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12493 ASM_OUTPUT_LABEL (asm_out_file, p1);
12495 /* Define the architecture-dependent minimum instruction length (in bytes).
12496 In this implementation of DWARF, this field is used for information
12497 purposes only. Since GCC generates assembly language, we have no
12498 a priori knowledge of how many instruction bytes are generated for each
12499 source line, and therefore can use only the DW_LNE_set_address and
12500 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12501 this as '1', which is "correct enough" for all architectures,
12502 and don't let the target override. */
12503 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12505 if (dwarf_version >= 4)
12506 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12507 "Maximum Operations Per Instruction");
12508 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12509 "Default is_stmt_start flag");
12510 dw2_asm_output_data (1, DWARF_LINE_BASE,
12511 "Line Base Value (Special Opcodes)");
12512 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12513 "Line Range Value (Special Opcodes)");
12514 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12515 "Special Opcode Base");
12517 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12519 int n_op_args;
12520 switch (opc)
12522 case DW_LNS_advance_pc:
12523 case DW_LNS_advance_line:
12524 case DW_LNS_set_file:
12525 case DW_LNS_set_column:
12526 case DW_LNS_fixed_advance_pc:
12527 case DW_LNS_set_isa:
12528 n_op_args = 1;
12529 break;
12530 default:
12531 n_op_args = 0;
12532 break;
12535 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12536 opc, n_op_args);
12539 /* Write out the information about the files we use. */
12540 output_file_names ();
12541 ASM_OUTPUT_LABEL (asm_out_file, p2);
12542 if (prologue_only)
12544 /* Output the marker for the end of the line number info. */
12545 ASM_OUTPUT_LABEL (asm_out_file, l2);
12546 return;
12549 if (separate_line_info)
12551 dw_line_info_table *table;
12552 size_t i;
12554 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12555 if (table->in_use)
12557 output_one_line_info_table (table);
12558 saw_one = true;
12561 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12563 output_one_line_info_table (cold_text_section_line_info);
12564 saw_one = true;
12567 /* ??? Some Darwin linkers crash on a .debug_line section with no
12568 sequences. Further, merely a DW_LNE_end_sequence entry is not
12569 sufficient -- the address column must also be initialized.
12570 Make sure to output at least one set_address/end_sequence pair,
12571 choosing .text since that section is always present. */
12572 if (text_section_line_info->in_use || !saw_one)
12573 output_one_line_info_table (text_section_line_info);
12575 /* Output the marker for the end of the line number info. */
12576 ASM_OUTPUT_LABEL (asm_out_file, l2);
12579 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12581 static inline bool
12582 need_endianity_attribute_p (bool reverse)
12584 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12587 /* Given a pointer to a tree node for some base type, return a pointer to
12588 a DIE that describes the given type. REVERSE is true if the type is
12589 to be interpreted in the reverse storage order wrt the target order.
12591 This routine must only be called for GCC type nodes that correspond to
12592 Dwarf base (fundamental) types. */
12594 static dw_die_ref
12595 base_type_die (tree type, bool reverse)
12597 dw_die_ref base_type_result;
12598 enum dwarf_type encoding;
12599 bool fpt_used = false;
12600 struct fixed_point_type_info fpt_info;
12601 tree type_bias = NULL_TREE;
12603 /* If this is a subtype that should not be emitted as a subrange type,
12604 use the base type. See subrange_type_for_debug_p. */
12605 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12606 type = TREE_TYPE (type);
12608 switch (TREE_CODE (type))
12610 case INTEGER_TYPE:
12611 if ((dwarf_version >= 4 || !dwarf_strict)
12612 && TYPE_NAME (type)
12613 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12614 && DECL_IS_BUILTIN (TYPE_NAME (type))
12615 && DECL_NAME (TYPE_NAME (type)))
12617 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12618 if (strcmp (name, "char16_t") == 0
12619 || strcmp (name, "char32_t") == 0)
12621 encoding = DW_ATE_UTF;
12622 break;
12625 if ((dwarf_version >= 3 || !dwarf_strict)
12626 && lang_hooks.types.get_fixed_point_type_info)
12628 memset (&fpt_info, 0, sizeof (fpt_info));
12629 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12631 fpt_used = true;
12632 encoding = ((TYPE_UNSIGNED (type))
12633 ? DW_ATE_unsigned_fixed
12634 : DW_ATE_signed_fixed);
12635 break;
12638 if (TYPE_STRING_FLAG (type))
12640 if (TYPE_UNSIGNED (type))
12641 encoding = DW_ATE_unsigned_char;
12642 else
12643 encoding = DW_ATE_signed_char;
12645 else if (TYPE_UNSIGNED (type))
12646 encoding = DW_ATE_unsigned;
12647 else
12648 encoding = DW_ATE_signed;
12650 if (!dwarf_strict
12651 && lang_hooks.types.get_type_bias)
12652 type_bias = lang_hooks.types.get_type_bias (type);
12653 break;
12655 case REAL_TYPE:
12656 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12658 if (dwarf_version >= 3 || !dwarf_strict)
12659 encoding = DW_ATE_decimal_float;
12660 else
12661 encoding = DW_ATE_lo_user;
12663 else
12664 encoding = DW_ATE_float;
12665 break;
12667 case FIXED_POINT_TYPE:
12668 if (!(dwarf_version >= 3 || !dwarf_strict))
12669 encoding = DW_ATE_lo_user;
12670 else if (TYPE_UNSIGNED (type))
12671 encoding = DW_ATE_unsigned_fixed;
12672 else
12673 encoding = DW_ATE_signed_fixed;
12674 break;
12676 /* Dwarf2 doesn't know anything about complex ints, so use
12677 a user defined type for it. */
12678 case COMPLEX_TYPE:
12679 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12680 encoding = DW_ATE_complex_float;
12681 else
12682 encoding = DW_ATE_lo_user;
12683 break;
12685 case BOOLEAN_TYPE:
12686 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12687 encoding = DW_ATE_boolean;
12688 break;
12690 default:
12691 /* No other TREE_CODEs are Dwarf fundamental types. */
12692 gcc_unreachable ();
12695 base_type_result = new_die_raw (DW_TAG_base_type);
12697 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12698 int_size_in_bytes (type));
12699 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12701 if (need_endianity_attribute_p (reverse))
12702 add_AT_unsigned (base_type_result, DW_AT_endianity,
12703 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12705 add_alignment_attribute (base_type_result, type);
12707 if (fpt_used)
12709 switch (fpt_info.scale_factor_kind)
12711 case fixed_point_scale_factor_binary:
12712 add_AT_int (base_type_result, DW_AT_binary_scale,
12713 fpt_info.scale_factor.binary);
12714 break;
12716 case fixed_point_scale_factor_decimal:
12717 add_AT_int (base_type_result, DW_AT_decimal_scale,
12718 fpt_info.scale_factor.decimal);
12719 break;
12721 case fixed_point_scale_factor_arbitrary:
12722 /* Arbitrary scale factors cannot be described in standard DWARF,
12723 yet. */
12724 if (!dwarf_strict)
12726 /* Describe the scale factor as a rational constant. */
12727 const dw_die_ref scale_factor
12728 = new_die (DW_TAG_constant, comp_unit_die (), type);
12730 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12731 fpt_info.scale_factor.arbitrary.numerator);
12732 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12733 fpt_info.scale_factor.arbitrary.denominator);
12735 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12737 break;
12739 default:
12740 gcc_unreachable ();
12744 if (type_bias)
12745 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12746 dw_scalar_form_constant
12747 | dw_scalar_form_exprloc
12748 | dw_scalar_form_reference,
12749 NULL);
12751 return base_type_result;
12754 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12755 named 'auto' in its type: return true for it, false otherwise. */
12757 static inline bool
12758 is_cxx_auto (tree type)
12760 if (is_cxx ())
12762 tree name = TYPE_IDENTIFIER (type);
12763 if (name == get_identifier ("auto")
12764 || name == get_identifier ("decltype(auto)"))
12765 return true;
12767 return false;
12770 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12771 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12773 static inline int
12774 is_base_type (tree type)
12776 switch (TREE_CODE (type))
12778 case INTEGER_TYPE:
12779 case REAL_TYPE:
12780 case FIXED_POINT_TYPE:
12781 case COMPLEX_TYPE:
12782 case BOOLEAN_TYPE:
12783 case POINTER_BOUNDS_TYPE:
12784 return 1;
12786 case VOID_TYPE:
12787 case ARRAY_TYPE:
12788 case RECORD_TYPE:
12789 case UNION_TYPE:
12790 case QUAL_UNION_TYPE:
12791 case ENUMERAL_TYPE:
12792 case FUNCTION_TYPE:
12793 case METHOD_TYPE:
12794 case POINTER_TYPE:
12795 case REFERENCE_TYPE:
12796 case NULLPTR_TYPE:
12797 case OFFSET_TYPE:
12798 case LANG_TYPE:
12799 case VECTOR_TYPE:
12800 return 0;
12802 default:
12803 if (is_cxx_auto (type))
12804 return 0;
12805 gcc_unreachable ();
12808 return 0;
12811 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12812 node, return the size in bits for the type if it is a constant, or else
12813 return the alignment for the type if the type's size is not constant, or
12814 else return BITS_PER_WORD if the type actually turns out to be an
12815 ERROR_MARK node. */
12817 static inline unsigned HOST_WIDE_INT
12818 simple_type_size_in_bits (const_tree type)
12820 if (TREE_CODE (type) == ERROR_MARK)
12821 return BITS_PER_WORD;
12822 else if (TYPE_SIZE (type) == NULL_TREE)
12823 return 0;
12824 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12825 return tree_to_uhwi (TYPE_SIZE (type));
12826 else
12827 return TYPE_ALIGN (type);
12830 /* Similarly, but return an offset_int instead of UHWI. */
12832 static inline offset_int
12833 offset_int_type_size_in_bits (const_tree type)
12835 if (TREE_CODE (type) == ERROR_MARK)
12836 return BITS_PER_WORD;
12837 else if (TYPE_SIZE (type) == NULL_TREE)
12838 return 0;
12839 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12840 return wi::to_offset (TYPE_SIZE (type));
12841 else
12842 return TYPE_ALIGN (type);
12845 /* Given a pointer to a tree node for a subrange type, return a pointer
12846 to a DIE that describes the given type. */
12848 static dw_die_ref
12849 subrange_type_die (tree type, tree low, tree high, tree bias,
12850 dw_die_ref context_die)
12852 dw_die_ref subrange_die;
12853 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12855 if (context_die == NULL)
12856 context_die = comp_unit_die ();
12858 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12860 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12862 /* The size of the subrange type and its base type do not match,
12863 so we need to generate a size attribute for the subrange type. */
12864 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12867 add_alignment_attribute (subrange_die, type);
12869 if (low)
12870 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12871 if (high)
12872 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12873 if (bias && !dwarf_strict)
12874 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12875 dw_scalar_form_constant
12876 | dw_scalar_form_exprloc
12877 | dw_scalar_form_reference,
12878 NULL);
12880 return subrange_die;
12883 /* Returns the (const and/or volatile) cv_qualifiers associated with
12884 the decl node. This will normally be augmented with the
12885 cv_qualifiers of the underlying type in add_type_attribute. */
12887 static int
12888 decl_quals (const_tree decl)
12890 return ((TREE_READONLY (decl)
12891 /* The C++ front-end correctly marks reference-typed
12892 variables as readonly, but from a language (and debug
12893 info) standpoint they are not const-qualified. */
12894 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12895 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12896 | (TREE_THIS_VOLATILE (decl)
12897 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12900 /* Determine the TYPE whose qualifiers match the largest strict subset
12901 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12902 qualifiers outside QUAL_MASK. */
12904 static int
12905 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12907 tree t;
12908 int best_rank = 0, best_qual = 0, max_rank;
12910 type_quals &= qual_mask;
12911 max_rank = popcount_hwi (type_quals) - 1;
12913 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12914 t = TYPE_NEXT_VARIANT (t))
12916 int q = TYPE_QUALS (t) & qual_mask;
12918 if ((q & type_quals) == q && q != type_quals
12919 && check_base_type (t, type))
12921 int rank = popcount_hwi (q);
12923 if (rank > best_rank)
12925 best_rank = rank;
12926 best_qual = q;
12931 return best_qual;
12934 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12935 static const dwarf_qual_info_t dwarf_qual_info[] =
12937 { TYPE_QUAL_CONST, DW_TAG_const_type },
12938 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12939 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12940 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12942 static const unsigned int dwarf_qual_info_size
12943 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12945 /* If DIE is a qualified DIE of some base DIE with the same parent,
12946 return the base DIE, otherwise return NULL. Set MASK to the
12947 qualifiers added compared to the returned DIE. */
12949 static dw_die_ref
12950 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12952 unsigned int i;
12953 for (i = 0; i < dwarf_qual_info_size; i++)
12954 if (die->die_tag == dwarf_qual_info[i].t)
12955 break;
12956 if (i == dwarf_qual_info_size)
12957 return NULL;
12958 if (vec_safe_length (die->die_attr) != 1)
12959 return NULL;
12960 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12961 if (type == NULL || type->die_parent != die->die_parent)
12962 return NULL;
12963 *mask |= dwarf_qual_info[i].q;
12964 if (depth)
12966 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12967 if (ret)
12968 return ret;
12970 return type;
12973 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12974 entry that chains the modifiers specified by CV_QUALS in front of the
12975 given type. REVERSE is true if the type is to be interpreted in the
12976 reverse storage order wrt the target order. */
12978 static dw_die_ref
12979 modified_type_die (tree type, int cv_quals, bool reverse,
12980 dw_die_ref context_die)
12982 enum tree_code code = TREE_CODE (type);
12983 dw_die_ref mod_type_die;
12984 dw_die_ref sub_die = NULL;
12985 tree item_type = NULL;
12986 tree qualified_type;
12987 tree name, low, high;
12988 dw_die_ref mod_scope;
12989 /* Only these cv-qualifiers are currently handled. */
12990 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12991 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12992 ENCODE_QUAL_ADDR_SPACE(~0U));
12993 const bool reverse_base_type
12994 = need_endianity_attribute_p (reverse) && is_base_type (type);
12996 if (code == ERROR_MARK)
12997 return NULL;
12999 if (lang_hooks.types.get_debug_type)
13001 tree debug_type = lang_hooks.types.get_debug_type (type);
13003 if (debug_type != NULL_TREE && debug_type != type)
13004 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13007 cv_quals &= cv_qual_mask;
13009 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13010 tag modifier (and not an attribute) old consumers won't be able
13011 to handle it. */
13012 if (dwarf_version < 3)
13013 cv_quals &= ~TYPE_QUAL_RESTRICT;
13015 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13016 if (dwarf_version < 5)
13017 cv_quals &= ~TYPE_QUAL_ATOMIC;
13019 /* See if we already have the appropriately qualified variant of
13020 this type. */
13021 qualified_type = get_qualified_type (type, cv_quals);
13023 if (qualified_type == sizetype)
13025 /* Try not to expose the internal sizetype type's name. */
13026 if (TYPE_NAME (qualified_type)
13027 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13029 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13031 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13032 && (TYPE_PRECISION (t)
13033 == TYPE_PRECISION (qualified_type))
13034 && (TYPE_UNSIGNED (t)
13035 == TYPE_UNSIGNED (qualified_type)));
13036 qualified_type = t;
13038 else if (qualified_type == sizetype
13039 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13040 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13041 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13042 qualified_type = size_type_node;
13045 /* If we do, then we can just use its DIE, if it exists. */
13046 if (qualified_type)
13048 mod_type_die = lookup_type_die (qualified_type);
13050 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13051 dealt with specially: the DIE with the attribute, if it exists, is
13052 placed immediately after the regular DIE for the same base type. */
13053 if (mod_type_die
13054 && (!reverse_base_type
13055 || ((mod_type_die = mod_type_die->die_sib) != NULL
13056 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13057 return mod_type_die;
13060 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13062 /* Handle C typedef types. */
13063 if (name
13064 && TREE_CODE (name) == TYPE_DECL
13065 && DECL_ORIGINAL_TYPE (name)
13066 && !DECL_ARTIFICIAL (name))
13068 tree dtype = TREE_TYPE (name);
13070 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13071 if (qualified_type == dtype && !reverse_base_type)
13073 tree origin = decl_ultimate_origin (name);
13075 /* Typedef variants that have an abstract origin don't get their own
13076 type DIE (see gen_typedef_die), so fall back on the ultimate
13077 abstract origin instead. */
13078 if (origin != NULL && origin != name)
13079 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13080 context_die);
13082 /* For a named type, use the typedef. */
13083 gen_type_die (qualified_type, context_die);
13084 return lookup_type_die (qualified_type);
13086 else
13088 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13089 dquals &= cv_qual_mask;
13090 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13091 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13092 /* cv-unqualified version of named type. Just use
13093 the unnamed type to which it refers. */
13094 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13095 reverse, context_die);
13096 /* Else cv-qualified version of named type; fall through. */
13100 mod_scope = scope_die_for (type, context_die);
13102 if (cv_quals)
13104 int sub_quals = 0, first_quals = 0;
13105 unsigned i;
13106 dw_die_ref first = NULL, last = NULL;
13108 /* Determine a lesser qualified type that most closely matches
13109 this one. Then generate DW_TAG_* entries for the remaining
13110 qualifiers. */
13111 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13112 cv_qual_mask);
13113 if (sub_quals && use_debug_types)
13115 bool needed = false;
13116 /* If emitting type units, make sure the order of qualifiers
13117 is canonical. Thus, start from unqualified type if
13118 an earlier qualifier is missing in sub_quals, but some later
13119 one is present there. */
13120 for (i = 0; i < dwarf_qual_info_size; i++)
13121 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13122 needed = true;
13123 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13125 sub_quals = 0;
13126 break;
13129 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13130 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13132 /* As not all intermediate qualified DIEs have corresponding
13133 tree types, ensure that qualified DIEs in the same scope
13134 as their DW_AT_type are emitted after their DW_AT_type,
13135 only with other qualified DIEs for the same type possibly
13136 in between them. Determine the range of such qualified
13137 DIEs now (first being the base type, last being corresponding
13138 last qualified DIE for it). */
13139 unsigned int count = 0;
13140 first = qualified_die_p (mod_type_die, &first_quals,
13141 dwarf_qual_info_size);
13142 if (first == NULL)
13143 first = mod_type_die;
13144 gcc_assert ((first_quals & ~sub_quals) == 0);
13145 for (count = 0, last = first;
13146 count < (1U << dwarf_qual_info_size);
13147 count++, last = last->die_sib)
13149 int quals = 0;
13150 if (last == mod_scope->die_child)
13151 break;
13152 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13153 != first)
13154 break;
13158 for (i = 0; i < dwarf_qual_info_size; i++)
13159 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13161 dw_die_ref d;
13162 if (first && first != last)
13164 for (d = first->die_sib; ; d = d->die_sib)
13166 int quals = 0;
13167 qualified_die_p (d, &quals, dwarf_qual_info_size);
13168 if (quals == (first_quals | dwarf_qual_info[i].q))
13169 break;
13170 if (d == last)
13172 d = NULL;
13173 break;
13176 if (d)
13178 mod_type_die = d;
13179 continue;
13182 if (first)
13184 d = new_die_raw (dwarf_qual_info[i].t);
13185 add_child_die_after (mod_scope, d, last);
13186 last = d;
13188 else
13189 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13190 if (mod_type_die)
13191 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13192 mod_type_die = d;
13193 first_quals |= dwarf_qual_info[i].q;
13196 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13198 dwarf_tag tag = DW_TAG_pointer_type;
13199 if (code == REFERENCE_TYPE)
13201 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13202 tag = DW_TAG_rvalue_reference_type;
13203 else
13204 tag = DW_TAG_reference_type;
13206 mod_type_die = new_die (tag, mod_scope, type);
13208 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13209 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13210 add_alignment_attribute (mod_type_die, type);
13211 item_type = TREE_TYPE (type);
13213 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13214 if (!ADDR_SPACE_GENERIC_P (as))
13216 int action = targetm.addr_space.debug (as);
13217 if (action >= 0)
13219 /* Positive values indicate an address_class. */
13220 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13222 else
13224 /* Negative values indicate an (inverted) segment base reg. */
13225 dw_loc_descr_ref d
13226 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13227 add_AT_loc (mod_type_die, DW_AT_segment, d);
13231 else if (code == INTEGER_TYPE
13232 && TREE_TYPE (type) != NULL_TREE
13233 && subrange_type_for_debug_p (type, &low, &high))
13235 tree bias = NULL_TREE;
13236 if (lang_hooks.types.get_type_bias)
13237 bias = lang_hooks.types.get_type_bias (type);
13238 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13239 item_type = TREE_TYPE (type);
13241 else if (is_base_type (type))
13243 mod_type_die = base_type_die (type, reverse);
13245 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13246 if (reverse_base_type)
13248 dw_die_ref after_die
13249 = modified_type_die (type, cv_quals, false, context_die);
13250 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13252 else
13253 add_child_die (comp_unit_die (), mod_type_die);
13255 add_pubtype (type, mod_type_die);
13257 else
13259 gen_type_die (type, context_die);
13261 /* We have to get the type_main_variant here (and pass that to the
13262 `lookup_type_die' routine) because the ..._TYPE node we have
13263 might simply be a *copy* of some original type node (where the
13264 copy was created to help us keep track of typedef names) and
13265 that copy might have a different TYPE_UID from the original
13266 ..._TYPE node. */
13267 if (TREE_CODE (type) == FUNCTION_TYPE
13268 || TREE_CODE (type) == METHOD_TYPE)
13270 /* For function/method types, can't just use type_main_variant here,
13271 because that can have different ref-qualifiers for C++,
13272 but try to canonicalize. */
13273 tree main = TYPE_MAIN_VARIANT (type);
13274 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13275 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13276 && check_base_type (t, main)
13277 && check_lang_type (t, type))
13278 return lookup_type_die (t);
13279 return lookup_type_die (type);
13281 else if (TREE_CODE (type) != VECTOR_TYPE
13282 && TREE_CODE (type) != ARRAY_TYPE)
13283 return lookup_type_die (type_main_variant (type));
13284 else
13285 /* Vectors have the debugging information in the type,
13286 not the main variant. */
13287 return lookup_type_die (type);
13290 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13291 don't output a DW_TAG_typedef, since there isn't one in the
13292 user's program; just attach a DW_AT_name to the type.
13293 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13294 if the base type already has the same name. */
13295 if (name
13296 && ((TREE_CODE (name) != TYPE_DECL
13297 && (qualified_type == TYPE_MAIN_VARIANT (type)
13298 || (cv_quals == TYPE_UNQUALIFIED)))
13299 || (TREE_CODE (name) == TYPE_DECL
13300 && TREE_TYPE (name) == qualified_type
13301 && DECL_NAME (name))))
13303 if (TREE_CODE (name) == TYPE_DECL)
13304 /* Could just call add_name_and_src_coords_attributes here,
13305 but since this is a builtin type it doesn't have any
13306 useful source coordinates anyway. */
13307 name = DECL_NAME (name);
13308 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13310 /* This probably indicates a bug. */
13311 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13313 name = TYPE_IDENTIFIER (type);
13314 add_name_attribute (mod_type_die,
13315 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13318 if (qualified_type && !reverse_base_type)
13319 equate_type_number_to_die (qualified_type, mod_type_die);
13321 if (item_type)
13322 /* We must do this after the equate_type_number_to_die call, in case
13323 this is a recursive type. This ensures that the modified_type_die
13324 recursion will terminate even if the type is recursive. Recursive
13325 types are possible in Ada. */
13326 sub_die = modified_type_die (item_type,
13327 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13328 reverse,
13329 context_die);
13331 if (sub_die != NULL)
13332 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13334 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13335 if (TYPE_ARTIFICIAL (type))
13336 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13338 return mod_type_die;
13341 /* Generate DIEs for the generic parameters of T.
13342 T must be either a generic type or a generic function.
13343 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13345 static void
13346 gen_generic_params_dies (tree t)
13348 tree parms, args;
13349 int parms_num, i;
13350 dw_die_ref die = NULL;
13351 int non_default;
13353 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13354 return;
13356 if (TYPE_P (t))
13357 die = lookup_type_die (t);
13358 else if (DECL_P (t))
13359 die = lookup_decl_die (t);
13361 gcc_assert (die);
13363 parms = lang_hooks.get_innermost_generic_parms (t);
13364 if (!parms)
13365 /* T has no generic parameter. It means T is neither a generic type
13366 or function. End of story. */
13367 return;
13369 parms_num = TREE_VEC_LENGTH (parms);
13370 args = lang_hooks.get_innermost_generic_args (t);
13371 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13372 non_default = int_cst_value (TREE_CHAIN (args));
13373 else
13374 non_default = TREE_VEC_LENGTH (args);
13375 for (i = 0; i < parms_num; i++)
13377 tree parm, arg, arg_pack_elems;
13378 dw_die_ref parm_die;
13380 parm = TREE_VEC_ELT (parms, i);
13381 arg = TREE_VEC_ELT (args, i);
13382 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13383 gcc_assert (parm && TREE_VALUE (parm) && arg);
13385 if (parm && TREE_VALUE (parm) && arg)
13387 /* If PARM represents a template parameter pack,
13388 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13389 by DW_TAG_template_*_parameter DIEs for the argument
13390 pack elements of ARG. Note that ARG would then be
13391 an argument pack. */
13392 if (arg_pack_elems)
13393 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13394 arg_pack_elems,
13395 die);
13396 else
13397 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13398 true /* emit name */, die);
13399 if (i >= non_default)
13400 add_AT_flag (parm_die, DW_AT_default_value, 1);
13405 /* Create and return a DIE for PARM which should be
13406 the representation of a generic type parameter.
13407 For instance, in the C++ front end, PARM would be a template parameter.
13408 ARG is the argument to PARM.
13409 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13410 name of the PARM.
13411 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13412 as a child node. */
13414 static dw_die_ref
13415 generic_parameter_die (tree parm, tree arg,
13416 bool emit_name_p,
13417 dw_die_ref parent_die)
13419 dw_die_ref tmpl_die = NULL;
13420 const char *name = NULL;
13422 if (!parm || !DECL_NAME (parm) || !arg)
13423 return NULL;
13425 /* We support non-type generic parameters and arguments,
13426 type generic parameters and arguments, as well as
13427 generic generic parameters (a.k.a. template template parameters in C++)
13428 and arguments. */
13429 if (TREE_CODE (parm) == PARM_DECL)
13430 /* PARM is a nontype generic parameter */
13431 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13432 else if (TREE_CODE (parm) == TYPE_DECL)
13433 /* PARM is a type generic parameter. */
13434 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13435 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13436 /* PARM is a generic generic parameter.
13437 Its DIE is a GNU extension. It shall have a
13438 DW_AT_name attribute to represent the name of the template template
13439 parameter, and a DW_AT_GNU_template_name attribute to represent the
13440 name of the template template argument. */
13441 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13442 parent_die, parm);
13443 else
13444 gcc_unreachable ();
13446 if (tmpl_die)
13448 tree tmpl_type;
13450 /* If PARM is a generic parameter pack, it means we are
13451 emitting debug info for a template argument pack element.
13452 In other terms, ARG is a template argument pack element.
13453 In that case, we don't emit any DW_AT_name attribute for
13454 the die. */
13455 if (emit_name_p)
13457 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13458 gcc_assert (name);
13459 add_AT_string (tmpl_die, DW_AT_name, name);
13462 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13464 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13465 TMPL_DIE should have a child DW_AT_type attribute that is set
13466 to the type of the argument to PARM, which is ARG.
13467 If PARM is a type generic parameter, TMPL_DIE should have a
13468 child DW_AT_type that is set to ARG. */
13469 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13470 add_type_attribute (tmpl_die, tmpl_type,
13471 (TREE_THIS_VOLATILE (tmpl_type)
13472 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13473 false, parent_die);
13475 else
13477 /* So TMPL_DIE is a DIE representing a
13478 a generic generic template parameter, a.k.a template template
13479 parameter in C++ and arg is a template. */
13481 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13482 to the name of the argument. */
13483 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13484 if (name)
13485 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13488 if (TREE_CODE (parm) == PARM_DECL)
13489 /* So PARM is a non-type generic parameter.
13490 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13491 attribute of TMPL_DIE which value represents the value
13492 of ARG.
13493 We must be careful here:
13494 The value of ARG might reference some function decls.
13495 We might currently be emitting debug info for a generic
13496 type and types are emitted before function decls, we don't
13497 know if the function decls referenced by ARG will actually be
13498 emitted after cgraph computations.
13499 So must defer the generation of the DW_AT_const_value to
13500 after cgraph is ready. */
13501 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13504 return tmpl_die;
13507 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13508 PARM_PACK must be a template parameter pack. The returned DIE
13509 will be child DIE of PARENT_DIE. */
13511 static dw_die_ref
13512 template_parameter_pack_die (tree parm_pack,
13513 tree parm_pack_args,
13514 dw_die_ref parent_die)
13516 dw_die_ref die;
13517 int j;
13519 gcc_assert (parent_die && parm_pack);
13521 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13522 add_name_and_src_coords_attributes (die, parm_pack);
13523 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13524 generic_parameter_die (parm_pack,
13525 TREE_VEC_ELT (parm_pack_args, j),
13526 false /* Don't emit DW_AT_name */,
13527 die);
13528 return die;
13531 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13532 an enumerated type. */
13534 static inline int
13535 type_is_enum (const_tree type)
13537 return TREE_CODE (type) == ENUMERAL_TYPE;
13540 /* Return the DBX register number described by a given RTL node. */
13542 static unsigned int
13543 dbx_reg_number (const_rtx rtl)
13545 unsigned regno = REGNO (rtl);
13547 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13549 #ifdef LEAF_REG_REMAP
13550 if (crtl->uses_only_leaf_regs)
13552 int leaf_reg = LEAF_REG_REMAP (regno);
13553 if (leaf_reg != -1)
13554 regno = (unsigned) leaf_reg;
13556 #endif
13558 regno = DBX_REGISTER_NUMBER (regno);
13559 gcc_assert (regno != INVALID_REGNUM);
13560 return regno;
13563 /* Optionally add a DW_OP_piece term to a location description expression.
13564 DW_OP_piece is only added if the location description expression already
13565 doesn't end with DW_OP_piece. */
13567 static void
13568 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13570 dw_loc_descr_ref loc;
13572 if (*list_head != NULL)
13574 /* Find the end of the chain. */
13575 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13578 if (loc->dw_loc_opc != DW_OP_piece)
13579 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13583 /* Return a location descriptor that designates a machine register or
13584 zero if there is none. */
13586 static dw_loc_descr_ref
13587 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13589 rtx regs;
13591 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13592 return 0;
13594 /* We only use "frame base" when we're sure we're talking about the
13595 post-prologue local stack frame. We do this by *not* running
13596 register elimination until this point, and recognizing the special
13597 argument pointer and soft frame pointer rtx's.
13598 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13599 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13600 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13602 dw_loc_descr_ref result = NULL;
13604 if (dwarf_version >= 4 || !dwarf_strict)
13606 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13607 initialized);
13608 if (result)
13609 add_loc_descr (&result,
13610 new_loc_descr (DW_OP_stack_value, 0, 0));
13612 return result;
13615 regs = targetm.dwarf_register_span (rtl);
13617 if (REG_NREGS (rtl) > 1 || regs)
13618 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13619 else
13621 unsigned int dbx_regnum = dbx_reg_number (rtl);
13622 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13623 return 0;
13624 return one_reg_loc_descriptor (dbx_regnum, initialized);
13628 /* Return a location descriptor that designates a machine register for
13629 a given hard register number. */
13631 static dw_loc_descr_ref
13632 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13634 dw_loc_descr_ref reg_loc_descr;
13636 if (regno <= 31)
13637 reg_loc_descr
13638 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13639 else
13640 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13642 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13643 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13645 return reg_loc_descr;
13648 /* Given an RTL of a register, return a location descriptor that
13649 designates a value that spans more than one register. */
13651 static dw_loc_descr_ref
13652 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13653 enum var_init_status initialized)
13655 int size, i;
13656 dw_loc_descr_ref loc_result = NULL;
13658 /* Simple, contiguous registers. */
13659 if (regs == NULL_RTX)
13661 unsigned reg = REGNO (rtl);
13662 int nregs;
13664 #ifdef LEAF_REG_REMAP
13665 if (crtl->uses_only_leaf_regs)
13667 int leaf_reg = LEAF_REG_REMAP (reg);
13668 if (leaf_reg != -1)
13669 reg = (unsigned) leaf_reg;
13671 #endif
13673 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13674 nregs = REG_NREGS (rtl);
13676 /* At present we only track constant-sized pieces. */
13677 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13678 return NULL;
13679 size /= nregs;
13681 loc_result = NULL;
13682 while (nregs--)
13684 dw_loc_descr_ref t;
13686 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13687 VAR_INIT_STATUS_INITIALIZED);
13688 add_loc_descr (&loc_result, t);
13689 add_loc_descr_op_piece (&loc_result, size);
13690 ++reg;
13692 return loc_result;
13695 /* Now onto stupid register sets in non contiguous locations. */
13697 gcc_assert (GET_CODE (regs) == PARALLEL);
13699 /* At present we only track constant-sized pieces. */
13700 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13701 return NULL;
13702 loc_result = NULL;
13704 for (i = 0; i < XVECLEN (regs, 0); ++i)
13706 dw_loc_descr_ref t;
13708 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13709 VAR_INIT_STATUS_INITIALIZED);
13710 add_loc_descr (&loc_result, t);
13711 add_loc_descr_op_piece (&loc_result, size);
13714 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13715 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13716 return loc_result;
13719 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13721 /* Return a location descriptor that designates a constant i,
13722 as a compound operation from constant (i >> shift), constant shift
13723 and DW_OP_shl. */
13725 static dw_loc_descr_ref
13726 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13728 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13729 add_loc_descr (&ret, int_loc_descriptor (shift));
13730 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13731 return ret;
13734 /* Return a location descriptor that designates constant POLY_I. */
13736 static dw_loc_descr_ref
13737 int_loc_descriptor (poly_int64 poly_i)
13739 enum dwarf_location_atom op;
13741 HOST_WIDE_INT i;
13742 if (!poly_i.is_constant (&i))
13744 /* Create location descriptions for the non-constant part and
13745 add any constant offset at the end. */
13746 dw_loc_descr_ref ret = NULL;
13747 HOST_WIDE_INT constant = poly_i.coeffs[0];
13748 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13750 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13751 if (coeff != 0)
13753 dw_loc_descr_ref start = ret;
13754 unsigned int factor;
13755 int bias;
13756 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13757 (j, &factor, &bias);
13759 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13760 add COEFF * (REGNO / FACTOR) now and subtract
13761 COEFF * BIAS from the final constant part. */
13762 constant -= coeff * bias;
13763 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13764 if (coeff % factor == 0)
13765 coeff /= factor;
13766 else
13768 int amount = exact_log2 (factor);
13769 gcc_assert (amount >= 0);
13770 add_loc_descr (&ret, int_loc_descriptor (amount));
13771 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13773 if (coeff != 1)
13775 add_loc_descr (&ret, int_loc_descriptor (coeff));
13776 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13778 if (start)
13779 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13782 loc_descr_plus_const (&ret, constant);
13783 return ret;
13786 /* Pick the smallest representation of a constant, rather than just
13787 defaulting to the LEB encoding. */
13788 if (i >= 0)
13790 int clz = clz_hwi (i);
13791 int ctz = ctz_hwi (i);
13792 if (i <= 31)
13793 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13794 else if (i <= 0xff)
13795 op = DW_OP_const1u;
13796 else if (i <= 0xffff)
13797 op = DW_OP_const2u;
13798 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13799 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13800 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13801 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13802 while DW_OP_const4u is 5 bytes. */
13803 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13804 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13805 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13806 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13807 while DW_OP_const4u is 5 bytes. */
13808 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13810 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13811 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13812 <= 4)
13814 /* As i >= 2**31, the double cast above will yield a negative number.
13815 Since wrapping is defined in DWARF expressions we can output big
13816 positive integers as small negative ones, regardless of the size
13817 of host wide ints.
13819 Here, since the evaluator will handle 32-bit values and since i >=
13820 2**31, we know it's going to be interpreted as a negative literal:
13821 store it this way if we can do better than 5 bytes this way. */
13822 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13824 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13825 op = DW_OP_const4u;
13827 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13828 least 6 bytes: see if we can do better before falling back to it. */
13829 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13830 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13831 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13832 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13833 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13834 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13835 >= HOST_BITS_PER_WIDE_INT)
13836 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13837 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13838 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13839 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13840 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13841 && size_of_uleb128 (i) > 6)
13842 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13843 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13844 else
13845 op = DW_OP_constu;
13847 else
13849 if (i >= -0x80)
13850 op = DW_OP_const1s;
13851 else if (i >= -0x8000)
13852 op = DW_OP_const2s;
13853 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13855 if (size_of_int_loc_descriptor (i) < 5)
13857 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13858 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13859 return ret;
13861 op = DW_OP_const4s;
13863 else
13865 if (size_of_int_loc_descriptor (i)
13866 < (unsigned long) 1 + size_of_sleb128 (i))
13868 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13869 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13870 return ret;
13872 op = DW_OP_consts;
13876 return new_loc_descr (op, i, 0);
13879 /* Likewise, for unsigned constants. */
13881 static dw_loc_descr_ref
13882 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13884 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13885 const unsigned HOST_WIDE_INT max_uint
13886 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13888 /* If possible, use the clever signed constants handling. */
13889 if (i <= max_int)
13890 return int_loc_descriptor ((HOST_WIDE_INT) i);
13892 /* Here, we are left with positive numbers that cannot be represented as
13893 HOST_WIDE_INT, i.e.:
13894 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13896 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13897 whereas may be better to output a negative integer: thanks to integer
13898 wrapping, we know that:
13899 x = x - 2 ** DWARF2_ADDR_SIZE
13900 = x - 2 * (max (HOST_WIDE_INT) + 1)
13901 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13902 small negative integers. Let's try that in cases it will clearly improve
13903 the encoding: there is no gain turning DW_OP_const4u into
13904 DW_OP_const4s. */
13905 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13906 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13907 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13909 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13911 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13912 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13913 const HOST_WIDE_INT second_shift
13914 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13916 /* So we finally have:
13917 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13918 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13919 return int_loc_descriptor (second_shift);
13922 /* Last chance: fallback to a simple constant operation. */
13923 return new_loc_descr
13924 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13925 ? DW_OP_const4u
13926 : DW_OP_const8u,
13927 i, 0);
13930 /* Generate and return a location description that computes the unsigned
13931 comparison of the two stack top entries (a OP b where b is the top-most
13932 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13933 LE_EXPR, GT_EXPR or GE_EXPR. */
13935 static dw_loc_descr_ref
13936 uint_comparison_loc_list (enum tree_code kind)
13938 enum dwarf_location_atom op, flip_op;
13939 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13941 switch (kind)
13943 case LT_EXPR:
13944 op = DW_OP_lt;
13945 break;
13946 case LE_EXPR:
13947 op = DW_OP_le;
13948 break;
13949 case GT_EXPR:
13950 op = DW_OP_gt;
13951 break;
13952 case GE_EXPR:
13953 op = DW_OP_ge;
13954 break;
13955 default:
13956 gcc_unreachable ();
13959 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13960 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13962 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13963 possible to perform unsigned comparisons: we just have to distinguish
13964 three cases:
13966 1. when a and b have the same sign (as signed integers); then we should
13967 return: a OP(signed) b;
13969 2. when a is a negative signed integer while b is a positive one, then a
13970 is a greater unsigned integer than b; likewise when a and b's roles
13971 are flipped.
13973 So first, compare the sign of the two operands. */
13974 ret = new_loc_descr (DW_OP_over, 0, 0);
13975 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13976 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13977 /* If they have different signs (i.e. they have different sign bits), then
13978 the stack top value has now the sign bit set and thus it's smaller than
13979 zero. */
13980 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13981 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13982 add_loc_descr (&ret, bra_node);
13984 /* We are in case 1. At this point, we know both operands have the same
13985 sign, to it's safe to use the built-in signed comparison. */
13986 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13987 add_loc_descr (&ret, jmp_node);
13989 /* We are in case 2. Here, we know both operands do not have the same sign,
13990 so we have to flip the signed comparison. */
13991 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13992 tmp = new_loc_descr (flip_op, 0, 0);
13993 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13994 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13995 add_loc_descr (&ret, tmp);
13997 /* This dummy operation is necessary to make the two branches join. */
13998 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13999 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14000 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14001 add_loc_descr (&ret, tmp);
14003 return ret;
14006 /* Likewise, but takes the location description lists (might be destructive on
14007 them). Return NULL if either is NULL or if concatenation fails. */
14009 static dw_loc_list_ref
14010 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14011 enum tree_code kind)
14013 if (left == NULL || right == NULL)
14014 return NULL;
14016 add_loc_list (&left, right);
14017 if (left == NULL)
14018 return NULL;
14020 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14021 return left;
14024 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14025 without actually allocating it. */
14027 static unsigned long
14028 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14030 return size_of_int_loc_descriptor (i >> shift)
14031 + size_of_int_loc_descriptor (shift)
14032 + 1;
14035 /* Return size_of_locs (int_loc_descriptor (i)) without
14036 actually allocating it. */
14038 static unsigned long
14039 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14041 unsigned long s;
14043 if (i >= 0)
14045 int clz, ctz;
14046 if (i <= 31)
14047 return 1;
14048 else if (i <= 0xff)
14049 return 2;
14050 else if (i <= 0xffff)
14051 return 3;
14052 clz = clz_hwi (i);
14053 ctz = ctz_hwi (i);
14054 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14055 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14056 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14057 - clz - 5);
14058 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14059 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14060 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14061 - clz - 8);
14062 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14063 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14064 <= 4)
14065 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14066 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14067 return 5;
14068 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14069 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14070 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14071 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14072 - clz - 8);
14073 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14074 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14075 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14076 - clz - 16);
14077 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14078 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14079 && s > 6)
14080 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14081 - clz - 32);
14082 else
14083 return 1 + s;
14085 else
14087 if (i >= -0x80)
14088 return 2;
14089 else if (i >= -0x8000)
14090 return 3;
14091 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14093 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14095 s = size_of_int_loc_descriptor (-i) + 1;
14096 if (s < 5)
14097 return s;
14099 return 5;
14101 else
14103 unsigned long r = 1 + size_of_sleb128 (i);
14104 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14106 s = size_of_int_loc_descriptor (-i) + 1;
14107 if (s < r)
14108 return s;
14110 return r;
14115 /* Return loc description representing "address" of integer value.
14116 This can appear only as toplevel expression. */
14118 static dw_loc_descr_ref
14119 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14121 int litsize;
14122 dw_loc_descr_ref loc_result = NULL;
14124 if (!(dwarf_version >= 4 || !dwarf_strict))
14125 return NULL;
14127 litsize = size_of_int_loc_descriptor (i);
14128 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14129 is more compact. For DW_OP_stack_value we need:
14130 litsize + 1 (DW_OP_stack_value)
14131 and for DW_OP_implicit_value:
14132 1 (DW_OP_implicit_value) + 1 (length) + size. */
14133 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14135 loc_result = int_loc_descriptor (i);
14136 add_loc_descr (&loc_result,
14137 new_loc_descr (DW_OP_stack_value, 0, 0));
14138 return loc_result;
14141 loc_result = new_loc_descr (DW_OP_implicit_value,
14142 size, 0);
14143 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14144 loc_result->dw_loc_oprnd2.v.val_int = i;
14145 return loc_result;
14148 /* Return a location descriptor that designates a base+offset location. */
14150 static dw_loc_descr_ref
14151 based_loc_descr (rtx reg, poly_int64 offset,
14152 enum var_init_status initialized)
14154 unsigned int regno;
14155 dw_loc_descr_ref result;
14156 dw_fde_ref fde = cfun->fde;
14158 /* We only use "frame base" when we're sure we're talking about the
14159 post-prologue local stack frame. We do this by *not* running
14160 register elimination until this point, and recognizing the special
14161 argument pointer and soft frame pointer rtx's. */
14162 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14164 rtx elim = (ira_use_lra_p
14165 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14166 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14168 if (elim != reg)
14170 elim = strip_offset_and_add (elim, &offset);
14171 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14172 && (elim == hard_frame_pointer_rtx
14173 || elim == stack_pointer_rtx))
14174 || elim == (frame_pointer_needed
14175 ? hard_frame_pointer_rtx
14176 : stack_pointer_rtx));
14178 /* If drap register is used to align stack, use frame
14179 pointer + offset to access stack variables. If stack
14180 is aligned without drap, use stack pointer + offset to
14181 access stack variables. */
14182 if (crtl->stack_realign_tried
14183 && reg == frame_pointer_rtx)
14185 int base_reg
14186 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14187 ? HARD_FRAME_POINTER_REGNUM
14188 : REGNO (elim));
14189 return new_reg_loc_descr (base_reg, offset);
14192 gcc_assert (frame_pointer_fb_offset_valid);
14193 offset += frame_pointer_fb_offset;
14194 HOST_WIDE_INT const_offset;
14195 if (offset.is_constant (&const_offset))
14196 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14197 else
14199 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14200 loc_descr_plus_const (&ret, offset);
14201 return ret;
14206 regno = REGNO (reg);
14207 #ifdef LEAF_REG_REMAP
14208 if (crtl->uses_only_leaf_regs)
14210 int leaf_reg = LEAF_REG_REMAP (regno);
14211 if (leaf_reg != -1)
14212 regno = (unsigned) leaf_reg;
14214 #endif
14215 regno = DWARF_FRAME_REGNUM (regno);
14217 HOST_WIDE_INT const_offset;
14218 if (!optimize && fde
14219 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14220 && offset.is_constant (&const_offset))
14222 /* Use cfa+offset to represent the location of arguments passed
14223 on the stack when drap is used to align stack.
14224 Only do this when not optimizing, for optimized code var-tracking
14225 is supposed to track where the arguments live and the register
14226 used as vdrap or drap in some spot might be used for something
14227 else in other part of the routine. */
14228 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14231 result = new_reg_loc_descr (regno, offset);
14233 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14234 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14236 return result;
14239 /* Return true if this RTL expression describes a base+offset calculation. */
14241 static inline int
14242 is_based_loc (const_rtx rtl)
14244 return (GET_CODE (rtl) == PLUS
14245 && ((REG_P (XEXP (rtl, 0))
14246 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14247 && CONST_INT_P (XEXP (rtl, 1)))));
14250 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14251 failed. */
14253 static dw_loc_descr_ref
14254 tls_mem_loc_descriptor (rtx mem)
14256 tree base;
14257 dw_loc_descr_ref loc_result;
14259 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14260 return NULL;
14262 base = get_base_address (MEM_EXPR (mem));
14263 if (base == NULL
14264 || !VAR_P (base)
14265 || !DECL_THREAD_LOCAL_P (base))
14266 return NULL;
14268 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14269 if (loc_result == NULL)
14270 return NULL;
14272 if (maybe_ne (MEM_OFFSET (mem), 0))
14273 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14275 return loc_result;
14278 /* Output debug info about reason why we failed to expand expression as dwarf
14279 expression. */
14281 static void
14282 expansion_failed (tree expr, rtx rtl, char const *reason)
14284 if (dump_file && (dump_flags & TDF_DETAILS))
14286 fprintf (dump_file, "Failed to expand as dwarf: ");
14287 if (expr)
14288 print_generic_expr (dump_file, expr, dump_flags);
14289 if (rtl)
14291 fprintf (dump_file, "\n");
14292 print_rtl (dump_file, rtl);
14294 fprintf (dump_file, "\nReason: %s\n", reason);
14298 /* Helper function for const_ok_for_output. */
14300 static bool
14301 const_ok_for_output_1 (rtx rtl)
14303 if (targetm.const_not_ok_for_debug_p (rtl))
14305 if (GET_CODE (rtl) != UNSPEC)
14307 expansion_failed (NULL_TREE, rtl,
14308 "Expression rejected for debug by the backend.\n");
14309 return false;
14312 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14313 the target hook doesn't explicitly allow it in debug info, assume
14314 we can't express it in the debug info. */
14315 /* Don't complain about TLS UNSPECs, those are just too hard to
14316 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14317 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14318 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14319 if (flag_checking
14320 && (XVECLEN (rtl, 0) == 0
14321 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14322 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14323 inform (current_function_decl
14324 ? DECL_SOURCE_LOCATION (current_function_decl)
14325 : UNKNOWN_LOCATION,
14326 #if NUM_UNSPEC_VALUES > 0
14327 "non-delegitimized UNSPEC %s (%d) found in variable location",
14328 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14329 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14330 XINT (rtl, 1));
14331 #else
14332 "non-delegitimized UNSPEC %d found in variable location",
14333 XINT (rtl, 1));
14334 #endif
14335 expansion_failed (NULL_TREE, rtl,
14336 "UNSPEC hasn't been delegitimized.\n");
14337 return false;
14340 if (CONST_POLY_INT_P (rtl))
14341 return false;
14343 if (targetm.const_not_ok_for_debug_p (rtl))
14345 expansion_failed (NULL_TREE, rtl,
14346 "Expression rejected for debug by the backend.\n");
14347 return false;
14350 /* FIXME: Refer to PR60655. It is possible for simplification
14351 of rtl expressions in var tracking to produce such expressions.
14352 We should really identify / validate expressions
14353 enclosed in CONST that can be handled by assemblers on various
14354 targets and only handle legitimate cases here. */
14355 switch (GET_CODE (rtl))
14357 case SYMBOL_REF:
14358 break;
14359 case NOT:
14360 case NEG:
14361 return false;
14362 default:
14363 return true;
14366 if (CONSTANT_POOL_ADDRESS_P (rtl))
14368 bool marked;
14369 get_pool_constant_mark (rtl, &marked);
14370 /* If all references to this pool constant were optimized away,
14371 it was not output and thus we can't represent it. */
14372 if (!marked)
14374 expansion_failed (NULL_TREE, rtl,
14375 "Constant was removed from constant pool.\n");
14376 return false;
14380 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14381 return false;
14383 /* Avoid references to external symbols in debug info, on several targets
14384 the linker might even refuse to link when linking a shared library,
14385 and in many other cases the relocations for .debug_info/.debug_loc are
14386 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14387 to be defined within the same shared library or executable are fine. */
14388 if (SYMBOL_REF_EXTERNAL_P (rtl))
14390 tree decl = SYMBOL_REF_DECL (rtl);
14392 if (decl == NULL || !targetm.binds_local_p (decl))
14394 expansion_failed (NULL_TREE, rtl,
14395 "Symbol not defined in current TU.\n");
14396 return false;
14400 return true;
14403 /* Return true if constant RTL can be emitted in DW_OP_addr or
14404 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14405 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14407 static bool
14408 const_ok_for_output (rtx rtl)
14410 if (GET_CODE (rtl) == SYMBOL_REF)
14411 return const_ok_for_output_1 (rtl);
14413 if (GET_CODE (rtl) == CONST)
14415 subrtx_var_iterator::array_type array;
14416 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14417 if (!const_ok_for_output_1 (*iter))
14418 return false;
14419 return true;
14422 return true;
14425 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14426 if possible, NULL otherwise. */
14428 static dw_die_ref
14429 base_type_for_mode (machine_mode mode, bool unsignedp)
14431 dw_die_ref type_die;
14432 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14434 if (type == NULL)
14435 return NULL;
14436 switch (TREE_CODE (type))
14438 case INTEGER_TYPE:
14439 case REAL_TYPE:
14440 break;
14441 default:
14442 return NULL;
14444 type_die = lookup_type_die (type);
14445 if (!type_die)
14446 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14447 comp_unit_die ());
14448 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14449 return NULL;
14450 return type_die;
14453 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14454 type matching MODE, or, if MODE is narrower than or as wide as
14455 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14456 possible. */
14458 static dw_loc_descr_ref
14459 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14461 machine_mode outer_mode = mode;
14462 dw_die_ref type_die;
14463 dw_loc_descr_ref cvt;
14465 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14467 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14468 return op;
14470 type_die = base_type_for_mode (outer_mode, 1);
14471 if (type_die == NULL)
14472 return NULL;
14473 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14474 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14475 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14476 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14477 add_loc_descr (&op, cvt);
14478 return op;
14481 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14483 static dw_loc_descr_ref
14484 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14485 dw_loc_descr_ref op1)
14487 dw_loc_descr_ref ret = op0;
14488 add_loc_descr (&ret, op1);
14489 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14490 if (STORE_FLAG_VALUE != 1)
14492 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14495 return ret;
14498 /* Subroutine of scompare_loc_descriptor for the case in which we're
14499 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14500 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14502 static dw_loc_descr_ref
14503 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14504 scalar_int_mode op_mode,
14505 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14507 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14508 dw_loc_descr_ref cvt;
14510 if (type_die == NULL)
14511 return NULL;
14512 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14513 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14514 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14515 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14516 add_loc_descr (&op0, cvt);
14517 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14518 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14519 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14520 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14521 add_loc_descr (&op1, cvt);
14522 return compare_loc_descriptor (op, op0, op1);
14525 /* Subroutine of scompare_loc_descriptor for the case in which we're
14526 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14527 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14529 static dw_loc_descr_ref
14530 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14531 scalar_int_mode op_mode,
14532 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14534 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14535 /* For eq/ne, if the operands are known to be zero-extended,
14536 there is no need to do the fancy shifting up. */
14537 if (op == DW_OP_eq || op == DW_OP_ne)
14539 dw_loc_descr_ref last0, last1;
14540 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14542 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14544 /* deref_size zero extends, and for constants we can check
14545 whether they are zero extended or not. */
14546 if (((last0->dw_loc_opc == DW_OP_deref_size
14547 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14548 || (CONST_INT_P (XEXP (rtl, 0))
14549 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14550 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14551 && ((last1->dw_loc_opc == DW_OP_deref_size
14552 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14553 || (CONST_INT_P (XEXP (rtl, 1))
14554 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14555 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14556 return compare_loc_descriptor (op, op0, op1);
14558 /* EQ/NE comparison against constant in narrower type than
14559 DWARF2_ADDR_SIZE can be performed either as
14560 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14561 DW_OP_{eq,ne}
14563 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14564 DW_OP_{eq,ne}. Pick whatever is shorter. */
14565 if (CONST_INT_P (XEXP (rtl, 1))
14566 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14567 && (size_of_int_loc_descriptor (shift) + 1
14568 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14569 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14570 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14571 & GET_MODE_MASK (op_mode))))
14573 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14574 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14575 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14576 & GET_MODE_MASK (op_mode));
14577 return compare_loc_descriptor (op, op0, op1);
14580 add_loc_descr (&op0, int_loc_descriptor (shift));
14581 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14582 if (CONST_INT_P (XEXP (rtl, 1)))
14583 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14584 else
14586 add_loc_descr (&op1, int_loc_descriptor (shift));
14587 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14589 return compare_loc_descriptor (op, op0, op1);
14592 /* Return location descriptor for unsigned comparison OP RTL. */
14594 static dw_loc_descr_ref
14595 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14596 machine_mode mem_mode)
14598 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14599 dw_loc_descr_ref op0, op1;
14601 if (op_mode == VOIDmode)
14602 op_mode = GET_MODE (XEXP (rtl, 1));
14603 if (op_mode == VOIDmode)
14604 return NULL;
14606 scalar_int_mode int_op_mode;
14607 if (dwarf_strict
14608 && dwarf_version < 5
14609 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14610 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14611 return NULL;
14613 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14614 VAR_INIT_STATUS_INITIALIZED);
14615 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14616 VAR_INIT_STATUS_INITIALIZED);
14618 if (op0 == NULL || op1 == NULL)
14619 return NULL;
14621 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14623 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14624 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14626 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14627 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14629 return compare_loc_descriptor (op, op0, op1);
14632 /* Return location descriptor for unsigned comparison OP RTL. */
14634 static dw_loc_descr_ref
14635 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14636 machine_mode mem_mode)
14638 dw_loc_descr_ref op0, op1;
14640 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14641 if (test_op_mode == VOIDmode)
14642 test_op_mode = GET_MODE (XEXP (rtl, 1));
14644 scalar_int_mode op_mode;
14645 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14646 return NULL;
14648 if (dwarf_strict
14649 && dwarf_version < 5
14650 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14651 return NULL;
14653 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14654 VAR_INIT_STATUS_INITIALIZED);
14655 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14656 VAR_INIT_STATUS_INITIALIZED);
14658 if (op0 == NULL || op1 == NULL)
14659 return NULL;
14661 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14663 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14664 dw_loc_descr_ref last0, last1;
14665 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14667 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14669 if (CONST_INT_P (XEXP (rtl, 0)))
14670 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14671 /* deref_size zero extends, so no need to mask it again. */
14672 else if (last0->dw_loc_opc != DW_OP_deref_size
14673 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14675 add_loc_descr (&op0, int_loc_descriptor (mask));
14676 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14678 if (CONST_INT_P (XEXP (rtl, 1)))
14679 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14680 /* deref_size zero extends, so no need to mask it again. */
14681 else if (last1->dw_loc_opc != DW_OP_deref_size
14682 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14684 add_loc_descr (&op1, int_loc_descriptor (mask));
14685 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14688 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14690 HOST_WIDE_INT bias = 1;
14691 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14692 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14693 if (CONST_INT_P (XEXP (rtl, 1)))
14694 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14695 + INTVAL (XEXP (rtl, 1)));
14696 else
14697 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14698 bias, 0));
14700 return compare_loc_descriptor (op, op0, op1);
14703 /* Return location descriptor for {U,S}{MIN,MAX}. */
14705 static dw_loc_descr_ref
14706 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14707 machine_mode mem_mode)
14709 enum dwarf_location_atom op;
14710 dw_loc_descr_ref op0, op1, ret;
14711 dw_loc_descr_ref bra_node, drop_node;
14713 scalar_int_mode int_mode;
14714 if (dwarf_strict
14715 && dwarf_version < 5
14716 && (!is_a <scalar_int_mode> (mode, &int_mode)
14717 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14718 return NULL;
14720 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14721 VAR_INIT_STATUS_INITIALIZED);
14722 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14723 VAR_INIT_STATUS_INITIALIZED);
14725 if (op0 == NULL || op1 == NULL)
14726 return NULL;
14728 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14729 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14730 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14731 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14733 /* Checked by the caller. */
14734 int_mode = as_a <scalar_int_mode> (mode);
14735 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14737 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14738 add_loc_descr (&op0, int_loc_descriptor (mask));
14739 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14740 add_loc_descr (&op1, int_loc_descriptor (mask));
14741 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14743 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14745 HOST_WIDE_INT bias = 1;
14746 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14747 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14748 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14751 else if (is_a <scalar_int_mode> (mode, &int_mode)
14752 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14754 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14755 add_loc_descr (&op0, int_loc_descriptor (shift));
14756 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14757 add_loc_descr (&op1, int_loc_descriptor (shift));
14758 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14760 else if (is_a <scalar_int_mode> (mode, &int_mode)
14761 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14763 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14764 dw_loc_descr_ref cvt;
14765 if (type_die == NULL)
14766 return NULL;
14767 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14768 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14769 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14770 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14771 add_loc_descr (&op0, cvt);
14772 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14773 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14774 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14775 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14776 add_loc_descr (&op1, cvt);
14779 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14780 op = DW_OP_lt;
14781 else
14782 op = DW_OP_gt;
14783 ret = op0;
14784 add_loc_descr (&ret, op1);
14785 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14786 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14787 add_loc_descr (&ret, bra_node);
14788 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14789 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14790 add_loc_descr (&ret, drop_node);
14791 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14792 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14793 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14794 && is_a <scalar_int_mode> (mode, &int_mode)
14795 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14796 ret = convert_descriptor_to_mode (int_mode, ret);
14797 return ret;
14800 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14801 but after converting arguments to type_die, afterwards
14802 convert back to unsigned. */
14804 static dw_loc_descr_ref
14805 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14806 scalar_int_mode mode, machine_mode mem_mode)
14808 dw_loc_descr_ref cvt, op0, op1;
14810 if (type_die == NULL)
14811 return NULL;
14812 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14813 VAR_INIT_STATUS_INITIALIZED);
14814 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14815 VAR_INIT_STATUS_INITIALIZED);
14816 if (op0 == NULL || op1 == NULL)
14817 return NULL;
14818 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14819 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14820 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14821 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14822 add_loc_descr (&op0, cvt);
14823 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14824 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14825 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14826 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14827 add_loc_descr (&op1, cvt);
14828 add_loc_descr (&op0, op1);
14829 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14830 return convert_descriptor_to_mode (mode, op0);
14833 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14834 const0 is DW_OP_lit0 or corresponding typed constant,
14835 const1 is DW_OP_lit1 or corresponding typed constant
14836 and constMSB is constant with just the MSB bit set
14837 for the mode):
14838 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14839 L1: const0 DW_OP_swap
14840 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14841 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14842 L3: DW_OP_drop
14843 L4: DW_OP_nop
14845 CTZ is similar:
14846 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14847 L1: const0 DW_OP_swap
14848 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14849 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14850 L3: DW_OP_drop
14851 L4: DW_OP_nop
14853 FFS is similar:
14854 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14855 L1: const1 DW_OP_swap
14856 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14857 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14858 L3: DW_OP_drop
14859 L4: DW_OP_nop */
14861 static dw_loc_descr_ref
14862 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14863 machine_mode mem_mode)
14865 dw_loc_descr_ref op0, ret, tmp;
14866 HOST_WIDE_INT valv;
14867 dw_loc_descr_ref l1jump, l1label;
14868 dw_loc_descr_ref l2jump, l2label;
14869 dw_loc_descr_ref l3jump, l3label;
14870 dw_loc_descr_ref l4jump, l4label;
14871 rtx msb;
14873 if (GET_MODE (XEXP (rtl, 0)) != mode)
14874 return NULL;
14876 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14877 VAR_INIT_STATUS_INITIALIZED);
14878 if (op0 == NULL)
14879 return NULL;
14880 ret = op0;
14881 if (GET_CODE (rtl) == CLZ)
14883 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14884 valv = GET_MODE_BITSIZE (mode);
14886 else if (GET_CODE (rtl) == FFS)
14887 valv = 0;
14888 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14889 valv = GET_MODE_BITSIZE (mode);
14890 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14891 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14892 add_loc_descr (&ret, l1jump);
14893 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14894 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14895 VAR_INIT_STATUS_INITIALIZED);
14896 if (tmp == NULL)
14897 return NULL;
14898 add_loc_descr (&ret, tmp);
14899 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14900 add_loc_descr (&ret, l4jump);
14901 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14902 ? const1_rtx : const0_rtx,
14903 mode, mem_mode,
14904 VAR_INIT_STATUS_INITIALIZED);
14905 if (l1label == NULL)
14906 return NULL;
14907 add_loc_descr (&ret, l1label);
14908 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14909 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14910 add_loc_descr (&ret, l2label);
14911 if (GET_CODE (rtl) != CLZ)
14912 msb = const1_rtx;
14913 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14914 msb = GEN_INT (HOST_WIDE_INT_1U
14915 << (GET_MODE_BITSIZE (mode) - 1));
14916 else
14917 msb = immed_wide_int_const
14918 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14919 GET_MODE_PRECISION (mode)), mode);
14920 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14921 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14922 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14923 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14924 else
14925 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14926 VAR_INIT_STATUS_INITIALIZED);
14927 if (tmp == NULL)
14928 return NULL;
14929 add_loc_descr (&ret, tmp);
14930 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14931 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14932 add_loc_descr (&ret, l3jump);
14933 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14934 VAR_INIT_STATUS_INITIALIZED);
14935 if (tmp == NULL)
14936 return NULL;
14937 add_loc_descr (&ret, tmp);
14938 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14939 ? DW_OP_shl : DW_OP_shr, 0, 0));
14940 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14941 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14942 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14943 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14944 add_loc_descr (&ret, l2jump);
14945 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14946 add_loc_descr (&ret, l3label);
14947 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14948 add_loc_descr (&ret, l4label);
14949 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14950 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14951 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14952 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14953 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14954 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14955 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14956 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14957 return ret;
14960 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14961 const1 is DW_OP_lit1 or corresponding typed constant):
14962 const0 DW_OP_swap
14963 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14964 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14965 L2: DW_OP_drop
14967 PARITY is similar:
14968 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14969 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14970 L2: DW_OP_drop */
14972 static dw_loc_descr_ref
14973 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14974 machine_mode mem_mode)
14976 dw_loc_descr_ref op0, ret, tmp;
14977 dw_loc_descr_ref l1jump, l1label;
14978 dw_loc_descr_ref l2jump, l2label;
14980 if (GET_MODE (XEXP (rtl, 0)) != mode)
14981 return NULL;
14983 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14984 VAR_INIT_STATUS_INITIALIZED);
14985 if (op0 == NULL)
14986 return NULL;
14987 ret = op0;
14988 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14989 VAR_INIT_STATUS_INITIALIZED);
14990 if (tmp == NULL)
14991 return NULL;
14992 add_loc_descr (&ret, tmp);
14993 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14994 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14995 add_loc_descr (&ret, l1label);
14996 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14997 add_loc_descr (&ret, l2jump);
14998 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14999 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15000 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15001 VAR_INIT_STATUS_INITIALIZED);
15002 if (tmp == NULL)
15003 return NULL;
15004 add_loc_descr (&ret, tmp);
15005 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15006 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15007 ? DW_OP_plus : DW_OP_xor, 0, 0));
15008 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15009 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15010 VAR_INIT_STATUS_INITIALIZED);
15011 add_loc_descr (&ret, tmp);
15012 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15013 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15014 add_loc_descr (&ret, l1jump);
15015 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15016 add_loc_descr (&ret, l2label);
15017 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15018 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15019 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15020 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15021 return ret;
15024 /* BSWAP (constS is initial shift count, either 56 or 24):
15025 constS const0
15026 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15027 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15028 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15029 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15030 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15032 static dw_loc_descr_ref
15033 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15034 machine_mode mem_mode)
15036 dw_loc_descr_ref op0, ret, tmp;
15037 dw_loc_descr_ref l1jump, l1label;
15038 dw_loc_descr_ref l2jump, l2label;
15040 if (BITS_PER_UNIT != 8
15041 || (GET_MODE_BITSIZE (mode) != 32
15042 && GET_MODE_BITSIZE (mode) != 64))
15043 return NULL;
15045 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15046 VAR_INIT_STATUS_INITIALIZED);
15047 if (op0 == NULL)
15048 return NULL;
15050 ret = op0;
15051 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15052 mode, mem_mode,
15053 VAR_INIT_STATUS_INITIALIZED);
15054 if (tmp == NULL)
15055 return NULL;
15056 add_loc_descr (&ret, tmp);
15057 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15058 VAR_INIT_STATUS_INITIALIZED);
15059 if (tmp == NULL)
15060 return NULL;
15061 add_loc_descr (&ret, tmp);
15062 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15063 add_loc_descr (&ret, l1label);
15064 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15065 mode, mem_mode,
15066 VAR_INIT_STATUS_INITIALIZED);
15067 add_loc_descr (&ret, tmp);
15068 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15069 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15070 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15071 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15072 VAR_INIT_STATUS_INITIALIZED);
15073 if (tmp == NULL)
15074 return NULL;
15075 add_loc_descr (&ret, tmp);
15076 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15077 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15078 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15079 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15080 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15081 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15082 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15083 VAR_INIT_STATUS_INITIALIZED);
15084 add_loc_descr (&ret, tmp);
15085 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15086 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15087 add_loc_descr (&ret, l2jump);
15088 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15089 VAR_INIT_STATUS_INITIALIZED);
15090 add_loc_descr (&ret, tmp);
15091 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15092 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15093 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15094 add_loc_descr (&ret, l1jump);
15095 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15096 add_loc_descr (&ret, l2label);
15097 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15098 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15099 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15100 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15101 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15102 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15103 return ret;
15106 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15107 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15108 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15109 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15111 ROTATERT is similar:
15112 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15113 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15114 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15116 static dw_loc_descr_ref
15117 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15118 machine_mode mem_mode)
15120 rtx rtlop1 = XEXP (rtl, 1);
15121 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15122 int i;
15124 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15125 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15126 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15127 VAR_INIT_STATUS_INITIALIZED);
15128 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15129 VAR_INIT_STATUS_INITIALIZED);
15130 if (op0 == NULL || op1 == NULL)
15131 return NULL;
15132 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15133 for (i = 0; i < 2; i++)
15135 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15136 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15137 mode, mem_mode,
15138 VAR_INIT_STATUS_INITIALIZED);
15139 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15140 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15141 ? DW_OP_const4u
15142 : HOST_BITS_PER_WIDE_INT == 64
15143 ? DW_OP_const8u : DW_OP_constu,
15144 GET_MODE_MASK (mode), 0);
15145 else
15146 mask[i] = NULL;
15147 if (mask[i] == NULL)
15148 return NULL;
15149 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15151 ret = op0;
15152 add_loc_descr (&ret, op1);
15153 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15154 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15155 if (GET_CODE (rtl) == ROTATERT)
15157 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15158 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15159 GET_MODE_BITSIZE (mode), 0));
15161 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15162 if (mask[0] != NULL)
15163 add_loc_descr (&ret, mask[0]);
15164 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15165 if (mask[1] != NULL)
15167 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15168 add_loc_descr (&ret, mask[1]);
15169 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15171 if (GET_CODE (rtl) == ROTATE)
15173 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15174 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15175 GET_MODE_BITSIZE (mode), 0));
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15178 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15179 return ret;
15182 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15183 for DEBUG_PARAMETER_REF RTL. */
15185 static dw_loc_descr_ref
15186 parameter_ref_descriptor (rtx rtl)
15188 dw_loc_descr_ref ret;
15189 dw_die_ref ref;
15191 if (dwarf_strict)
15192 return NULL;
15193 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15194 /* With LTO during LTRANS we get the late DIE that refers to the early
15195 DIE, thus we add another indirection here. This seems to confuse
15196 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15197 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15198 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15199 if (ref)
15201 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15202 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15203 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15205 else
15207 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15208 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15210 return ret;
15213 /* The following routine converts the RTL for a variable or parameter
15214 (resident in memory) into an equivalent Dwarf representation of a
15215 mechanism for getting the address of that same variable onto the top of a
15216 hypothetical "address evaluation" stack.
15218 When creating memory location descriptors, we are effectively transforming
15219 the RTL for a memory-resident object into its Dwarf postfix expression
15220 equivalent. This routine recursively descends an RTL tree, turning
15221 it into Dwarf postfix code as it goes.
15223 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15225 MEM_MODE is the mode of the memory reference, needed to handle some
15226 autoincrement addressing modes.
15228 Return 0 if we can't represent the location. */
15230 dw_loc_descr_ref
15231 mem_loc_descriptor (rtx rtl, machine_mode mode,
15232 machine_mode mem_mode,
15233 enum var_init_status initialized)
15235 dw_loc_descr_ref mem_loc_result = NULL;
15236 enum dwarf_location_atom op;
15237 dw_loc_descr_ref op0, op1;
15238 rtx inner = NULL_RTX;
15239 poly_int64 offset;
15241 if (mode == VOIDmode)
15242 mode = GET_MODE (rtl);
15244 /* Note that for a dynamically sized array, the location we will generate a
15245 description of here will be the lowest numbered location which is
15246 actually within the array. That's *not* necessarily the same as the
15247 zeroth element of the array. */
15249 rtl = targetm.delegitimize_address (rtl);
15251 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15252 return NULL;
15254 scalar_int_mode int_mode, inner_mode, op1_mode;
15255 switch (GET_CODE (rtl))
15257 case POST_INC:
15258 case POST_DEC:
15259 case POST_MODIFY:
15260 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15262 case SUBREG:
15263 /* The case of a subreg may arise when we have a local (register)
15264 variable or a formal (register) parameter which doesn't quite fill
15265 up an entire register. For now, just assume that it is
15266 legitimate to make the Dwarf info refer to the whole register which
15267 contains the given subreg. */
15268 if (!subreg_lowpart_p (rtl))
15269 break;
15270 inner = SUBREG_REG (rtl);
15271 /* FALLTHRU */
15272 case TRUNCATE:
15273 if (inner == NULL_RTX)
15274 inner = XEXP (rtl, 0);
15275 if (is_a <scalar_int_mode> (mode, &int_mode)
15276 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15277 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15278 #ifdef POINTERS_EXTEND_UNSIGNED
15279 || (int_mode == Pmode && mem_mode != VOIDmode)
15280 #endif
15282 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15284 mem_loc_result = mem_loc_descriptor (inner,
15285 inner_mode,
15286 mem_mode, initialized);
15287 break;
15289 if (dwarf_strict && dwarf_version < 5)
15290 break;
15291 if (is_a <scalar_int_mode> (mode, &int_mode)
15292 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15293 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15294 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15296 dw_die_ref type_die;
15297 dw_loc_descr_ref cvt;
15299 mem_loc_result = mem_loc_descriptor (inner,
15300 GET_MODE (inner),
15301 mem_mode, initialized);
15302 if (mem_loc_result == NULL)
15303 break;
15304 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15305 if (type_die == NULL)
15307 mem_loc_result = NULL;
15308 break;
15310 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15311 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15312 else
15313 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15314 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15315 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15316 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15317 add_loc_descr (&mem_loc_result, cvt);
15318 if (is_a <scalar_int_mode> (mode, &int_mode)
15319 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15321 /* Convert it to untyped afterwards. */
15322 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15323 add_loc_descr (&mem_loc_result, cvt);
15326 break;
15328 case REG:
15329 if (!is_a <scalar_int_mode> (mode, &int_mode)
15330 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15331 && rtl != arg_pointer_rtx
15332 && rtl != frame_pointer_rtx
15333 #ifdef POINTERS_EXTEND_UNSIGNED
15334 && (int_mode != Pmode || mem_mode == VOIDmode)
15335 #endif
15338 dw_die_ref type_die;
15339 unsigned int dbx_regnum;
15341 if (dwarf_strict && dwarf_version < 5)
15342 break;
15343 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
15344 break;
15345 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15346 if (type_die == NULL)
15347 break;
15349 dbx_regnum = dbx_reg_number (rtl);
15350 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15351 break;
15352 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15353 dbx_regnum, 0);
15354 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15355 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15356 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15357 break;
15359 /* Whenever a register number forms a part of the description of the
15360 method for calculating the (dynamic) address of a memory resident
15361 object, DWARF rules require the register number be referred to as
15362 a "base register". This distinction is not based in any way upon
15363 what category of register the hardware believes the given register
15364 belongs to. This is strictly DWARF terminology we're dealing with
15365 here. Note that in cases where the location of a memory-resident
15366 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15367 OP_CONST (0)) the actual DWARF location descriptor that we generate
15368 may just be OP_BASEREG (basereg). This may look deceptively like
15369 the object in question was allocated to a register (rather than in
15370 memory) so DWARF consumers need to be aware of the subtle
15371 distinction between OP_REG and OP_BASEREG. */
15372 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15373 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15374 else if (stack_realign_drap
15375 && crtl->drap_reg
15376 && crtl->args.internal_arg_pointer == rtl
15377 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15379 /* If RTL is internal_arg_pointer, which has been optimized
15380 out, use DRAP instead. */
15381 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15382 VAR_INIT_STATUS_INITIALIZED);
15384 break;
15386 case SIGN_EXTEND:
15387 case ZERO_EXTEND:
15388 if (!is_a <scalar_int_mode> (mode, &int_mode)
15389 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15390 break;
15391 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15392 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15393 if (op0 == 0)
15394 break;
15395 else if (GET_CODE (rtl) == ZERO_EXTEND
15396 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15397 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15398 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15399 to expand zero extend as two shifts instead of
15400 masking. */
15401 && GET_MODE_SIZE (inner_mode) <= 4)
15403 mem_loc_result = op0;
15404 add_loc_descr (&mem_loc_result,
15405 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15406 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15408 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15410 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15411 shift *= BITS_PER_UNIT;
15412 if (GET_CODE (rtl) == SIGN_EXTEND)
15413 op = DW_OP_shra;
15414 else
15415 op = DW_OP_shr;
15416 mem_loc_result = op0;
15417 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15418 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15419 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15420 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15422 else if (!dwarf_strict || dwarf_version >= 5)
15424 dw_die_ref type_die1, type_die2;
15425 dw_loc_descr_ref cvt;
15427 type_die1 = base_type_for_mode (inner_mode,
15428 GET_CODE (rtl) == ZERO_EXTEND);
15429 if (type_die1 == NULL)
15430 break;
15431 type_die2 = base_type_for_mode (int_mode, 1);
15432 if (type_die2 == NULL)
15433 break;
15434 mem_loc_result = op0;
15435 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15436 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15437 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15438 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15439 add_loc_descr (&mem_loc_result, cvt);
15440 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15441 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15442 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15443 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15444 add_loc_descr (&mem_loc_result, cvt);
15446 break;
15448 case MEM:
15450 rtx new_rtl = avoid_constant_pool_reference (rtl);
15451 if (new_rtl != rtl)
15453 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15454 initialized);
15455 if (mem_loc_result != NULL)
15456 return mem_loc_result;
15459 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15460 get_address_mode (rtl), mode,
15461 VAR_INIT_STATUS_INITIALIZED);
15462 if (mem_loc_result == NULL)
15463 mem_loc_result = tls_mem_loc_descriptor (rtl);
15464 if (mem_loc_result != NULL)
15466 if (!is_a <scalar_int_mode> (mode, &int_mode)
15467 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15469 dw_die_ref type_die;
15470 dw_loc_descr_ref deref;
15471 HOST_WIDE_INT size;
15473 if (dwarf_strict && dwarf_version < 5)
15474 return NULL;
15475 if (!GET_MODE_SIZE (mode).is_constant (&size))
15476 return NULL;
15477 type_die
15478 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15479 if (type_die == NULL)
15480 return NULL;
15481 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15482 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15483 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15484 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15485 add_loc_descr (&mem_loc_result, deref);
15487 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15488 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15489 else
15490 add_loc_descr (&mem_loc_result,
15491 new_loc_descr (DW_OP_deref_size,
15492 GET_MODE_SIZE (int_mode), 0));
15494 break;
15496 case LO_SUM:
15497 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15499 case LABEL_REF:
15500 /* Some ports can transform a symbol ref into a label ref, because
15501 the symbol ref is too far away and has to be dumped into a constant
15502 pool. */
15503 case CONST:
15504 case SYMBOL_REF:
15505 if (!is_a <scalar_int_mode> (mode, &int_mode)
15506 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15507 #ifdef POINTERS_EXTEND_UNSIGNED
15508 && (int_mode != Pmode || mem_mode == VOIDmode)
15509 #endif
15511 break;
15512 if (GET_CODE (rtl) == SYMBOL_REF
15513 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15515 dw_loc_descr_ref temp;
15517 /* If this is not defined, we have no way to emit the data. */
15518 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15519 break;
15521 temp = new_addr_loc_descr (rtl, dtprel_true);
15523 /* We check for DWARF 5 here because gdb did not implement
15524 DW_OP_form_tls_address until after 7.12. */
15525 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15526 ? DW_OP_form_tls_address
15527 : DW_OP_GNU_push_tls_address),
15528 0, 0);
15529 add_loc_descr (&mem_loc_result, temp);
15531 break;
15534 if (!const_ok_for_output (rtl))
15536 if (GET_CODE (rtl) == CONST)
15537 switch (GET_CODE (XEXP (rtl, 0)))
15539 case NOT:
15540 op = DW_OP_not;
15541 goto try_const_unop;
15542 case NEG:
15543 op = DW_OP_neg;
15544 goto try_const_unop;
15545 try_const_unop:
15546 rtx arg;
15547 arg = XEXP (XEXP (rtl, 0), 0);
15548 if (!CONSTANT_P (arg))
15549 arg = gen_rtx_CONST (int_mode, arg);
15550 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15551 initialized);
15552 if (op0)
15554 mem_loc_result = op0;
15555 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15557 break;
15558 default:
15559 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15560 mem_mode, initialized);
15561 break;
15563 break;
15566 symref:
15567 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15568 vec_safe_push (used_rtx_array, rtl);
15569 break;
15571 case CONCAT:
15572 case CONCATN:
15573 case VAR_LOCATION:
15574 case DEBUG_IMPLICIT_PTR:
15575 expansion_failed (NULL_TREE, rtl,
15576 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15577 return 0;
15579 case ENTRY_VALUE:
15580 if (dwarf_strict && dwarf_version < 5)
15581 return NULL;
15582 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15584 if (!is_a <scalar_int_mode> (mode, &int_mode)
15585 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15586 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15587 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15588 else
15590 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15591 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15592 return NULL;
15593 op0 = one_reg_loc_descriptor (dbx_regnum,
15594 VAR_INIT_STATUS_INITIALIZED);
15597 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15598 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15600 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15601 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15602 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15603 return NULL;
15605 else
15606 gcc_unreachable ();
15607 if (op0 == NULL)
15608 return NULL;
15609 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15610 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15611 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15612 break;
15614 case DEBUG_PARAMETER_REF:
15615 mem_loc_result = parameter_ref_descriptor (rtl);
15616 break;
15618 case PRE_MODIFY:
15619 /* Extract the PLUS expression nested inside and fall into
15620 PLUS code below. */
15621 rtl = XEXP (rtl, 1);
15622 goto plus;
15624 case PRE_INC:
15625 case PRE_DEC:
15626 /* Turn these into a PLUS expression and fall into the PLUS code
15627 below. */
15628 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15629 gen_int_mode (GET_CODE (rtl) == PRE_INC
15630 ? GET_MODE_UNIT_SIZE (mem_mode)
15631 : -GET_MODE_UNIT_SIZE (mem_mode),
15632 mode));
15634 /* fall through */
15636 case PLUS:
15637 plus:
15638 if (is_based_loc (rtl)
15639 && is_a <scalar_int_mode> (mode, &int_mode)
15640 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15641 || XEXP (rtl, 0) == arg_pointer_rtx
15642 || XEXP (rtl, 0) == frame_pointer_rtx))
15643 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15644 INTVAL (XEXP (rtl, 1)),
15645 VAR_INIT_STATUS_INITIALIZED);
15646 else
15648 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15649 VAR_INIT_STATUS_INITIALIZED);
15650 if (mem_loc_result == 0)
15651 break;
15653 if (CONST_INT_P (XEXP (rtl, 1))
15654 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15655 <= DWARF2_ADDR_SIZE))
15656 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15657 else
15659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15660 VAR_INIT_STATUS_INITIALIZED);
15661 if (op1 == 0)
15662 return NULL;
15663 add_loc_descr (&mem_loc_result, op1);
15664 add_loc_descr (&mem_loc_result,
15665 new_loc_descr (DW_OP_plus, 0, 0));
15668 break;
15670 /* If a pseudo-reg is optimized away, it is possible for it to
15671 be replaced with a MEM containing a multiply or shift. */
15672 case MINUS:
15673 op = DW_OP_minus;
15674 goto do_binop;
15676 case MULT:
15677 op = DW_OP_mul;
15678 goto do_binop;
15680 case DIV:
15681 if ((!dwarf_strict || dwarf_version >= 5)
15682 && is_a <scalar_int_mode> (mode, &int_mode)
15683 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15685 mem_loc_result = typed_binop (DW_OP_div, rtl,
15686 base_type_for_mode (mode, 0),
15687 int_mode, mem_mode);
15688 break;
15690 op = DW_OP_div;
15691 goto do_binop;
15693 case UMOD:
15694 op = DW_OP_mod;
15695 goto do_binop;
15697 case ASHIFT:
15698 op = DW_OP_shl;
15699 goto do_shift;
15701 case ASHIFTRT:
15702 op = DW_OP_shra;
15703 goto do_shift;
15705 case LSHIFTRT:
15706 op = DW_OP_shr;
15707 goto do_shift;
15709 do_shift:
15710 if (!is_a <scalar_int_mode> (mode, &int_mode))
15711 break;
15712 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15713 VAR_INIT_STATUS_INITIALIZED);
15715 rtx rtlop1 = XEXP (rtl, 1);
15716 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15717 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15718 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15719 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15720 VAR_INIT_STATUS_INITIALIZED);
15723 if (op0 == 0 || op1 == 0)
15724 break;
15726 mem_loc_result = op0;
15727 add_loc_descr (&mem_loc_result, op1);
15728 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15729 break;
15731 case AND:
15732 op = DW_OP_and;
15733 goto do_binop;
15735 case IOR:
15736 op = DW_OP_or;
15737 goto do_binop;
15739 case XOR:
15740 op = DW_OP_xor;
15741 goto do_binop;
15743 do_binop:
15744 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15745 VAR_INIT_STATUS_INITIALIZED);
15746 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15747 VAR_INIT_STATUS_INITIALIZED);
15749 if (op0 == 0 || op1 == 0)
15750 break;
15752 mem_loc_result = op0;
15753 add_loc_descr (&mem_loc_result, op1);
15754 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15755 break;
15757 case MOD:
15758 if ((!dwarf_strict || dwarf_version >= 5)
15759 && is_a <scalar_int_mode> (mode, &int_mode)
15760 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15762 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15763 base_type_for_mode (mode, 0),
15764 int_mode, mem_mode);
15765 break;
15768 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15769 VAR_INIT_STATUS_INITIALIZED);
15770 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15771 VAR_INIT_STATUS_INITIALIZED);
15773 if (op0 == 0 || op1 == 0)
15774 break;
15776 mem_loc_result = op0;
15777 add_loc_descr (&mem_loc_result, op1);
15778 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15779 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15780 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15781 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15782 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15783 break;
15785 case UDIV:
15786 if ((!dwarf_strict || dwarf_version >= 5)
15787 && is_a <scalar_int_mode> (mode, &int_mode))
15789 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15791 op = DW_OP_div;
15792 goto do_binop;
15794 mem_loc_result = typed_binop (DW_OP_div, rtl,
15795 base_type_for_mode (int_mode, 1),
15796 int_mode, mem_mode);
15798 break;
15800 case NOT:
15801 op = DW_OP_not;
15802 goto do_unop;
15804 case ABS:
15805 op = DW_OP_abs;
15806 goto do_unop;
15808 case NEG:
15809 op = DW_OP_neg;
15810 goto do_unop;
15812 do_unop:
15813 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15814 VAR_INIT_STATUS_INITIALIZED);
15816 if (op0 == 0)
15817 break;
15819 mem_loc_result = op0;
15820 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15821 break;
15823 case CONST_INT:
15824 if (!is_a <scalar_int_mode> (mode, &int_mode)
15825 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15826 #ifdef POINTERS_EXTEND_UNSIGNED
15827 || (int_mode == Pmode
15828 && mem_mode != VOIDmode
15829 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15830 #endif
15833 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15834 break;
15836 if ((!dwarf_strict || dwarf_version >= 5)
15837 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15838 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15840 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15841 scalar_int_mode amode;
15842 if (type_die == NULL)
15843 return NULL;
15844 if (INTVAL (rtl) >= 0
15845 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15846 .exists (&amode))
15847 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15848 /* const DW_OP_convert <XXX> vs.
15849 DW_OP_const_type <XXX, 1, const>. */
15850 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15851 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15853 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15854 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15855 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15856 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15857 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15858 add_loc_descr (&mem_loc_result, op0);
15859 return mem_loc_result;
15861 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15862 INTVAL (rtl));
15863 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15864 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15865 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15866 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15867 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15868 else
15870 mem_loc_result->dw_loc_oprnd2.val_class
15871 = dw_val_class_const_double;
15872 mem_loc_result->dw_loc_oprnd2.v.val_double
15873 = double_int::from_shwi (INTVAL (rtl));
15876 break;
15878 case CONST_DOUBLE:
15879 if (!dwarf_strict || dwarf_version >= 5)
15881 dw_die_ref type_die;
15883 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15884 CONST_DOUBLE rtx could represent either a large integer
15885 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15886 the value is always a floating point constant.
15888 When it is an integer, a CONST_DOUBLE is used whenever
15889 the constant requires 2 HWIs to be adequately represented.
15890 We output CONST_DOUBLEs as blocks. */
15891 if (mode == VOIDmode
15892 || (GET_MODE (rtl) == VOIDmode
15893 && maybe_ne (GET_MODE_BITSIZE (mode),
15894 HOST_BITS_PER_DOUBLE_INT)))
15895 break;
15896 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15897 if (type_die == NULL)
15898 return NULL;
15899 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15900 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15901 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15902 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15903 #if TARGET_SUPPORTS_WIDE_INT == 0
15904 if (!SCALAR_FLOAT_MODE_P (mode))
15906 mem_loc_result->dw_loc_oprnd2.val_class
15907 = dw_val_class_const_double;
15908 mem_loc_result->dw_loc_oprnd2.v.val_double
15909 = rtx_to_double_int (rtl);
15911 else
15912 #endif
15914 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15915 unsigned int length = GET_MODE_SIZE (float_mode);
15916 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15918 insert_float (rtl, array);
15919 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15920 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15921 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15922 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15925 break;
15927 case CONST_WIDE_INT:
15928 if (!dwarf_strict || dwarf_version >= 5)
15930 dw_die_ref type_die;
15932 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15933 if (type_die == NULL)
15934 return NULL;
15935 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15936 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15937 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15938 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15939 mem_loc_result->dw_loc_oprnd2.val_class
15940 = dw_val_class_wide_int;
15941 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15942 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15944 break;
15946 case CONST_POLY_INT:
15947 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
15948 break;
15950 case EQ:
15951 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15952 break;
15954 case GE:
15955 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15956 break;
15958 case GT:
15959 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15960 break;
15962 case LE:
15963 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15964 break;
15966 case LT:
15967 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15968 break;
15970 case NE:
15971 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15972 break;
15974 case GEU:
15975 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15976 break;
15978 case GTU:
15979 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15980 break;
15982 case LEU:
15983 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15984 break;
15986 case LTU:
15987 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15988 break;
15990 case UMIN:
15991 case UMAX:
15992 if (!SCALAR_INT_MODE_P (mode))
15993 break;
15994 /* FALLTHRU */
15995 case SMIN:
15996 case SMAX:
15997 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15998 break;
16000 case ZERO_EXTRACT:
16001 case SIGN_EXTRACT:
16002 if (CONST_INT_P (XEXP (rtl, 1))
16003 && CONST_INT_P (XEXP (rtl, 2))
16004 && is_a <scalar_int_mode> (mode, &int_mode)
16005 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16006 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16007 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16008 && ((unsigned) INTVAL (XEXP (rtl, 1))
16009 + (unsigned) INTVAL (XEXP (rtl, 2))
16010 <= GET_MODE_BITSIZE (int_mode)))
16012 int shift, size;
16013 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16014 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16015 if (op0 == 0)
16016 break;
16017 if (GET_CODE (rtl) == SIGN_EXTRACT)
16018 op = DW_OP_shra;
16019 else
16020 op = DW_OP_shr;
16021 mem_loc_result = op0;
16022 size = INTVAL (XEXP (rtl, 1));
16023 shift = INTVAL (XEXP (rtl, 2));
16024 if (BITS_BIG_ENDIAN)
16025 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16026 if (shift + size != (int) DWARF2_ADDR_SIZE)
16028 add_loc_descr (&mem_loc_result,
16029 int_loc_descriptor (DWARF2_ADDR_SIZE
16030 - shift - size));
16031 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16033 if (size != (int) DWARF2_ADDR_SIZE)
16035 add_loc_descr (&mem_loc_result,
16036 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16037 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16040 break;
16042 case IF_THEN_ELSE:
16044 dw_loc_descr_ref op2, bra_node, drop_node;
16045 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16046 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16047 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16048 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16049 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16050 VAR_INIT_STATUS_INITIALIZED);
16051 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16052 VAR_INIT_STATUS_INITIALIZED);
16053 if (op0 == NULL || op1 == NULL || op2 == NULL)
16054 break;
16056 mem_loc_result = op1;
16057 add_loc_descr (&mem_loc_result, op2);
16058 add_loc_descr (&mem_loc_result, op0);
16059 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16060 add_loc_descr (&mem_loc_result, bra_node);
16061 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16062 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16063 add_loc_descr (&mem_loc_result, drop_node);
16064 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16065 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16067 break;
16069 case FLOAT_EXTEND:
16070 case FLOAT_TRUNCATE:
16071 case FLOAT:
16072 case UNSIGNED_FLOAT:
16073 case FIX:
16074 case UNSIGNED_FIX:
16075 if (!dwarf_strict || dwarf_version >= 5)
16077 dw_die_ref type_die;
16078 dw_loc_descr_ref cvt;
16080 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16081 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16082 if (op0 == NULL)
16083 break;
16084 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16085 && (GET_CODE (rtl) == FLOAT
16086 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16088 type_die = base_type_for_mode (int_mode,
16089 GET_CODE (rtl) == UNSIGNED_FLOAT);
16090 if (type_die == NULL)
16091 break;
16092 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16093 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16094 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16095 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16096 add_loc_descr (&op0, cvt);
16098 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16099 if (type_die == NULL)
16100 break;
16101 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16102 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16103 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16104 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16105 add_loc_descr (&op0, cvt);
16106 if (is_a <scalar_int_mode> (mode, &int_mode)
16107 && (GET_CODE (rtl) == FIX
16108 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16110 op0 = convert_descriptor_to_mode (int_mode, op0);
16111 if (op0 == NULL)
16112 break;
16114 mem_loc_result = op0;
16116 break;
16118 case CLZ:
16119 case CTZ:
16120 case FFS:
16121 if (is_a <scalar_int_mode> (mode, &int_mode))
16122 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16123 break;
16125 case POPCOUNT:
16126 case PARITY:
16127 if (is_a <scalar_int_mode> (mode, &int_mode))
16128 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16129 break;
16131 case BSWAP:
16132 if (is_a <scalar_int_mode> (mode, &int_mode))
16133 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16134 break;
16136 case ROTATE:
16137 case ROTATERT:
16138 if (is_a <scalar_int_mode> (mode, &int_mode))
16139 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16140 break;
16142 case COMPARE:
16143 /* In theory, we could implement the above. */
16144 /* DWARF cannot represent the unsigned compare operations
16145 natively. */
16146 case SS_MULT:
16147 case US_MULT:
16148 case SS_DIV:
16149 case US_DIV:
16150 case SS_PLUS:
16151 case US_PLUS:
16152 case SS_MINUS:
16153 case US_MINUS:
16154 case SS_NEG:
16155 case US_NEG:
16156 case SS_ABS:
16157 case SS_ASHIFT:
16158 case US_ASHIFT:
16159 case SS_TRUNCATE:
16160 case US_TRUNCATE:
16161 case UNORDERED:
16162 case ORDERED:
16163 case UNEQ:
16164 case UNGE:
16165 case UNGT:
16166 case UNLE:
16167 case UNLT:
16168 case LTGT:
16169 case FRACT_CONVERT:
16170 case UNSIGNED_FRACT_CONVERT:
16171 case SAT_FRACT:
16172 case UNSIGNED_SAT_FRACT:
16173 case SQRT:
16174 case ASM_OPERANDS:
16175 case VEC_MERGE:
16176 case VEC_SELECT:
16177 case VEC_CONCAT:
16178 case VEC_DUPLICATE:
16179 case VEC_SERIES:
16180 case UNSPEC:
16181 case HIGH:
16182 case FMA:
16183 case STRICT_LOW_PART:
16184 case CONST_VECTOR:
16185 case CONST_FIXED:
16186 case CLRSB:
16187 case CLOBBER:
16188 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16189 can't express it in the debug info. This can happen e.g. with some
16190 TLS UNSPECs. */
16191 break;
16193 case CONST_STRING:
16194 resolve_one_addr (&rtl);
16195 goto symref;
16197 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16198 the expression. An UNSPEC rtx represents a raw DWARF operation,
16199 new_loc_descr is called for it to build the operation directly.
16200 Otherwise mem_loc_descriptor is called recursively. */
16201 case PARALLEL:
16203 int index = 0;
16204 dw_loc_descr_ref exp_result = NULL;
16206 for (; index < XVECLEN (rtl, 0); index++)
16208 rtx elem = XVECEXP (rtl, 0, index);
16209 if (GET_CODE (elem) == UNSPEC)
16211 /* Each DWARF operation UNSPEC contain two operands, if
16212 one operand is not used for the operation, const0_rtx is
16213 passed. */
16214 gcc_assert (XVECLEN (elem, 0) == 2);
16216 HOST_WIDE_INT dw_op = XINT (elem, 1);
16217 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16218 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16219 exp_result
16220 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16221 oprnd2);
16223 else
16224 exp_result
16225 = mem_loc_descriptor (elem, mode, mem_mode,
16226 VAR_INIT_STATUS_INITIALIZED);
16228 if (!mem_loc_result)
16229 mem_loc_result = exp_result;
16230 else
16231 add_loc_descr (&mem_loc_result, exp_result);
16234 break;
16237 default:
16238 if (flag_checking)
16240 print_rtl (stderr, rtl);
16241 gcc_unreachable ();
16243 break;
16246 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16247 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16249 return mem_loc_result;
16252 /* Return a descriptor that describes the concatenation of two locations.
16253 This is typically a complex variable. */
16255 static dw_loc_descr_ref
16256 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16258 /* At present we only track constant-sized pieces. */
16259 unsigned int size0, size1;
16260 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16261 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16262 return 0;
16264 dw_loc_descr_ref cc_loc_result = NULL;
16265 dw_loc_descr_ref x0_ref
16266 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16267 dw_loc_descr_ref x1_ref
16268 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16270 if (x0_ref == 0 || x1_ref == 0)
16271 return 0;
16273 cc_loc_result = x0_ref;
16274 add_loc_descr_op_piece (&cc_loc_result, size0);
16276 add_loc_descr (&cc_loc_result, x1_ref);
16277 add_loc_descr_op_piece (&cc_loc_result, size1);
16279 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16280 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16282 return cc_loc_result;
16285 /* Return a descriptor that describes the concatenation of N
16286 locations. */
16288 static dw_loc_descr_ref
16289 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16291 unsigned int i;
16292 dw_loc_descr_ref cc_loc_result = NULL;
16293 unsigned int n = XVECLEN (concatn, 0);
16294 unsigned int size;
16296 for (i = 0; i < n; ++i)
16298 dw_loc_descr_ref ref;
16299 rtx x = XVECEXP (concatn, 0, i);
16301 /* At present we only track constant-sized pieces. */
16302 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16303 return NULL;
16305 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16306 if (ref == NULL)
16307 return NULL;
16309 add_loc_descr (&cc_loc_result, ref);
16310 add_loc_descr_op_piece (&cc_loc_result, size);
16313 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16314 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16316 return cc_loc_result;
16319 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16320 for DEBUG_IMPLICIT_PTR RTL. */
16322 static dw_loc_descr_ref
16323 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16325 dw_loc_descr_ref ret;
16326 dw_die_ref ref;
16328 if (dwarf_strict && dwarf_version < 5)
16329 return NULL;
16330 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16331 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16332 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16333 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16334 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16335 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16336 if (ref)
16338 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16339 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16340 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16342 else
16344 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16345 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16347 return ret;
16350 /* Output a proper Dwarf location descriptor for a variable or parameter
16351 which is either allocated in a register or in a memory location. For a
16352 register, we just generate an OP_REG and the register number. For a
16353 memory location we provide a Dwarf postfix expression describing how to
16354 generate the (dynamic) address of the object onto the address stack.
16356 MODE is mode of the decl if this loc_descriptor is going to be used in
16357 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16358 allowed, VOIDmode otherwise.
16360 If we don't know how to describe it, return 0. */
16362 static dw_loc_descr_ref
16363 loc_descriptor (rtx rtl, machine_mode mode,
16364 enum var_init_status initialized)
16366 dw_loc_descr_ref loc_result = NULL;
16367 scalar_int_mode int_mode;
16369 switch (GET_CODE (rtl))
16371 case SUBREG:
16372 /* The case of a subreg may arise when we have a local (register)
16373 variable or a formal (register) parameter which doesn't quite fill
16374 up an entire register. For now, just assume that it is
16375 legitimate to make the Dwarf info refer to the whole register which
16376 contains the given subreg. */
16377 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16378 loc_result = loc_descriptor (SUBREG_REG (rtl),
16379 GET_MODE (SUBREG_REG (rtl)), initialized);
16380 else
16381 goto do_default;
16382 break;
16384 case REG:
16385 loc_result = reg_loc_descriptor (rtl, initialized);
16386 break;
16388 case MEM:
16389 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16390 GET_MODE (rtl), initialized);
16391 if (loc_result == NULL)
16392 loc_result = tls_mem_loc_descriptor (rtl);
16393 if (loc_result == NULL)
16395 rtx new_rtl = avoid_constant_pool_reference (rtl);
16396 if (new_rtl != rtl)
16397 loc_result = loc_descriptor (new_rtl, mode, initialized);
16399 break;
16401 case CONCAT:
16402 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16403 initialized);
16404 break;
16406 case CONCATN:
16407 loc_result = concatn_loc_descriptor (rtl, initialized);
16408 break;
16410 case VAR_LOCATION:
16411 /* Single part. */
16412 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16414 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16415 if (GET_CODE (loc) == EXPR_LIST)
16416 loc = XEXP (loc, 0);
16417 loc_result = loc_descriptor (loc, mode, initialized);
16418 break;
16421 rtl = XEXP (rtl, 1);
16422 /* FALLTHRU */
16424 case PARALLEL:
16426 rtvec par_elems = XVEC (rtl, 0);
16427 int num_elem = GET_NUM_ELEM (par_elems);
16428 machine_mode mode;
16429 int i, size;
16431 /* Create the first one, so we have something to add to. */
16432 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16433 VOIDmode, initialized);
16434 if (loc_result == NULL)
16435 return NULL;
16436 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16437 /* At present we only track constant-sized pieces. */
16438 if (!GET_MODE_SIZE (mode).is_constant (&size))
16439 return NULL;
16440 add_loc_descr_op_piece (&loc_result, size);
16441 for (i = 1; i < num_elem; i++)
16443 dw_loc_descr_ref temp;
16445 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16446 VOIDmode, initialized);
16447 if (temp == NULL)
16448 return NULL;
16449 add_loc_descr (&loc_result, temp);
16450 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16451 /* At present we only track constant-sized pieces. */
16452 if (!GET_MODE_SIZE (mode).is_constant (&size))
16453 return NULL;
16454 add_loc_descr_op_piece (&loc_result, size);
16457 break;
16459 case CONST_INT:
16460 if (mode != VOIDmode && mode != BLKmode)
16462 int_mode = as_a <scalar_int_mode> (mode);
16463 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16464 INTVAL (rtl));
16466 break;
16468 case CONST_DOUBLE:
16469 if (mode == VOIDmode)
16470 mode = GET_MODE (rtl);
16472 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16474 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16476 /* Note that a CONST_DOUBLE rtx could represent either an integer
16477 or a floating-point constant. A CONST_DOUBLE is used whenever
16478 the constant requires more than one word in order to be
16479 adequately represented. We output CONST_DOUBLEs as blocks. */
16480 scalar_mode smode = as_a <scalar_mode> (mode);
16481 loc_result = new_loc_descr (DW_OP_implicit_value,
16482 GET_MODE_SIZE (smode), 0);
16483 #if TARGET_SUPPORTS_WIDE_INT == 0
16484 if (!SCALAR_FLOAT_MODE_P (smode))
16486 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16487 loc_result->dw_loc_oprnd2.v.val_double
16488 = rtx_to_double_int (rtl);
16490 else
16491 #endif
16493 unsigned int length = GET_MODE_SIZE (smode);
16494 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16496 insert_float (rtl, array);
16497 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16498 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16499 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16500 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16503 break;
16505 case CONST_WIDE_INT:
16506 if (mode == VOIDmode)
16507 mode = GET_MODE (rtl);
16509 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16511 int_mode = as_a <scalar_int_mode> (mode);
16512 loc_result = new_loc_descr (DW_OP_implicit_value,
16513 GET_MODE_SIZE (int_mode), 0);
16514 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16515 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16516 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16518 break;
16520 case CONST_VECTOR:
16521 if (mode == VOIDmode)
16522 mode = GET_MODE (rtl);
16524 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16526 unsigned int length;
16527 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16528 return NULL;
16530 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16531 unsigned char *array
16532 = ggc_vec_alloc<unsigned char> (length * elt_size);
16533 unsigned int i;
16534 unsigned char *p;
16535 machine_mode imode = GET_MODE_INNER (mode);
16537 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16538 switch (GET_MODE_CLASS (mode))
16540 case MODE_VECTOR_INT:
16541 for (i = 0, p = array; i < length; i++, p += elt_size)
16543 rtx elt = CONST_VECTOR_ELT (rtl, i);
16544 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16546 break;
16548 case MODE_VECTOR_FLOAT:
16549 for (i = 0, p = array; i < length; i++, p += elt_size)
16551 rtx elt = CONST_VECTOR_ELT (rtl, i);
16552 insert_float (elt, p);
16554 break;
16556 default:
16557 gcc_unreachable ();
16560 loc_result = new_loc_descr (DW_OP_implicit_value,
16561 length * elt_size, 0);
16562 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16563 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16564 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16565 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16567 break;
16569 case CONST:
16570 if (mode == VOIDmode
16571 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16572 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16573 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16575 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16576 break;
16578 /* FALLTHROUGH */
16579 case SYMBOL_REF:
16580 if (!const_ok_for_output (rtl))
16581 break;
16582 /* FALLTHROUGH */
16583 case LABEL_REF:
16584 if (is_a <scalar_int_mode> (mode, &int_mode)
16585 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16586 && (dwarf_version >= 4 || !dwarf_strict))
16588 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16589 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16590 vec_safe_push (used_rtx_array, rtl);
16592 break;
16594 case DEBUG_IMPLICIT_PTR:
16595 loc_result = implicit_ptr_descriptor (rtl, 0);
16596 break;
16598 case PLUS:
16599 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16600 && CONST_INT_P (XEXP (rtl, 1)))
16602 loc_result
16603 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16604 break;
16606 /* FALLTHRU */
16607 do_default:
16608 default:
16609 if ((is_a <scalar_int_mode> (mode, &int_mode)
16610 && GET_MODE (rtl) == int_mode
16611 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16612 && dwarf_version >= 4)
16613 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16615 /* Value expression. */
16616 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16617 if (loc_result)
16618 add_loc_descr (&loc_result,
16619 new_loc_descr (DW_OP_stack_value, 0, 0));
16621 break;
16624 return loc_result;
16627 /* We need to figure out what section we should use as the base for the
16628 address ranges where a given location is valid.
16629 1. If this particular DECL has a section associated with it, use that.
16630 2. If this function has a section associated with it, use that.
16631 3. Otherwise, use the text section.
16632 XXX: If you split a variable across multiple sections, we won't notice. */
16634 static const char *
16635 secname_for_decl (const_tree decl)
16637 const char *secname;
16639 if (VAR_OR_FUNCTION_DECL_P (decl)
16640 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16641 && DECL_SECTION_NAME (decl))
16642 secname = DECL_SECTION_NAME (decl);
16643 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16644 secname = DECL_SECTION_NAME (current_function_decl);
16645 else if (cfun && in_cold_section_p)
16646 secname = crtl->subsections.cold_section_label;
16647 else
16648 secname = text_section_label;
16650 return secname;
16653 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16655 static bool
16656 decl_by_reference_p (tree decl)
16658 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16659 || VAR_P (decl))
16660 && DECL_BY_REFERENCE (decl));
16663 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16664 for VARLOC. */
16666 static dw_loc_descr_ref
16667 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16668 enum var_init_status initialized)
16670 int have_address = 0;
16671 dw_loc_descr_ref descr;
16672 machine_mode mode;
16674 if (want_address != 2)
16676 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16677 /* Single part. */
16678 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16680 varloc = PAT_VAR_LOCATION_LOC (varloc);
16681 if (GET_CODE (varloc) == EXPR_LIST)
16682 varloc = XEXP (varloc, 0);
16683 mode = GET_MODE (varloc);
16684 if (MEM_P (varloc))
16686 rtx addr = XEXP (varloc, 0);
16687 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16688 mode, initialized);
16689 if (descr)
16690 have_address = 1;
16691 else
16693 rtx x = avoid_constant_pool_reference (varloc);
16694 if (x != varloc)
16695 descr = mem_loc_descriptor (x, mode, VOIDmode,
16696 initialized);
16699 else
16700 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16702 else
16703 return 0;
16705 else
16707 if (GET_CODE (varloc) == VAR_LOCATION)
16708 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16709 else
16710 mode = DECL_MODE (loc);
16711 descr = loc_descriptor (varloc, mode, initialized);
16712 have_address = 1;
16715 if (!descr)
16716 return 0;
16718 if (want_address == 2 && !have_address
16719 && (dwarf_version >= 4 || !dwarf_strict))
16721 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16723 expansion_failed (loc, NULL_RTX,
16724 "DWARF address size mismatch");
16725 return 0;
16727 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16728 have_address = 1;
16730 /* Show if we can't fill the request for an address. */
16731 if (want_address && !have_address)
16733 expansion_failed (loc, NULL_RTX,
16734 "Want address and only have value");
16735 return 0;
16738 /* If we've got an address and don't want one, dereference. */
16739 if (!want_address && have_address)
16741 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16742 enum dwarf_location_atom op;
16744 if (size > DWARF2_ADDR_SIZE || size == -1)
16746 expansion_failed (loc, NULL_RTX,
16747 "DWARF address size mismatch");
16748 return 0;
16750 else if (size == DWARF2_ADDR_SIZE)
16751 op = DW_OP_deref;
16752 else
16753 op = DW_OP_deref_size;
16755 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16758 return descr;
16761 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16762 if it is not possible. */
16764 static dw_loc_descr_ref
16765 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16767 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16768 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16769 else if (dwarf_version >= 3 || !dwarf_strict)
16770 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16771 else
16772 return NULL;
16775 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16776 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16778 static dw_loc_descr_ref
16779 dw_sra_loc_expr (tree decl, rtx loc)
16781 rtx p;
16782 unsigned HOST_WIDE_INT padsize = 0;
16783 dw_loc_descr_ref descr, *descr_tail;
16784 unsigned HOST_WIDE_INT decl_size;
16785 rtx varloc;
16786 enum var_init_status initialized;
16788 if (DECL_SIZE (decl) == NULL
16789 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16790 return NULL;
16792 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16793 descr = NULL;
16794 descr_tail = &descr;
16796 for (p = loc; p; p = XEXP (p, 1))
16798 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16799 rtx loc_note = *decl_piece_varloc_ptr (p);
16800 dw_loc_descr_ref cur_descr;
16801 dw_loc_descr_ref *tail, last = NULL;
16802 unsigned HOST_WIDE_INT opsize = 0;
16804 if (loc_note == NULL_RTX
16805 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16807 padsize += bitsize;
16808 continue;
16810 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16811 varloc = NOTE_VAR_LOCATION (loc_note);
16812 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16813 if (cur_descr == NULL)
16815 padsize += bitsize;
16816 continue;
16819 /* Check that cur_descr either doesn't use
16820 DW_OP_*piece operations, or their sum is equal
16821 to bitsize. Otherwise we can't embed it. */
16822 for (tail = &cur_descr; *tail != NULL;
16823 tail = &(*tail)->dw_loc_next)
16824 if ((*tail)->dw_loc_opc == DW_OP_piece)
16826 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16827 * BITS_PER_UNIT;
16828 last = *tail;
16830 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16832 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16833 last = *tail;
16836 if (last != NULL && opsize != bitsize)
16838 padsize += bitsize;
16839 /* Discard the current piece of the descriptor and release any
16840 addr_table entries it uses. */
16841 remove_loc_list_addr_table_entries (cur_descr);
16842 continue;
16845 /* If there is a hole, add DW_OP_*piece after empty DWARF
16846 expression, which means that those bits are optimized out. */
16847 if (padsize)
16849 if (padsize > decl_size)
16851 remove_loc_list_addr_table_entries (cur_descr);
16852 goto discard_descr;
16854 decl_size -= padsize;
16855 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16856 if (*descr_tail == NULL)
16858 remove_loc_list_addr_table_entries (cur_descr);
16859 goto discard_descr;
16861 descr_tail = &(*descr_tail)->dw_loc_next;
16862 padsize = 0;
16864 *descr_tail = cur_descr;
16865 descr_tail = tail;
16866 if (bitsize > decl_size)
16867 goto discard_descr;
16868 decl_size -= bitsize;
16869 if (last == NULL)
16871 HOST_WIDE_INT offset = 0;
16872 if (GET_CODE (varloc) == VAR_LOCATION
16873 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16875 varloc = PAT_VAR_LOCATION_LOC (varloc);
16876 if (GET_CODE (varloc) == EXPR_LIST)
16877 varloc = XEXP (varloc, 0);
16881 if (GET_CODE (varloc) == CONST
16882 || GET_CODE (varloc) == SIGN_EXTEND
16883 || GET_CODE (varloc) == ZERO_EXTEND)
16884 varloc = XEXP (varloc, 0);
16885 else if (GET_CODE (varloc) == SUBREG)
16886 varloc = SUBREG_REG (varloc);
16887 else
16888 break;
16890 while (1);
16891 /* DW_OP_bit_size offset should be zero for register
16892 or implicit location descriptions and empty location
16893 descriptions, but for memory addresses needs big endian
16894 adjustment. */
16895 if (MEM_P (varloc))
16897 unsigned HOST_WIDE_INT memsize;
16898 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16899 goto discard_descr;
16900 memsize *= BITS_PER_UNIT;
16901 if (memsize != bitsize)
16903 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16904 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16905 goto discard_descr;
16906 if (memsize < bitsize)
16907 goto discard_descr;
16908 if (BITS_BIG_ENDIAN)
16909 offset = memsize - bitsize;
16913 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16914 if (*descr_tail == NULL)
16915 goto discard_descr;
16916 descr_tail = &(*descr_tail)->dw_loc_next;
16920 /* If there were any non-empty expressions, add padding till the end of
16921 the decl. */
16922 if (descr != NULL && decl_size != 0)
16924 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16925 if (*descr_tail == NULL)
16926 goto discard_descr;
16928 return descr;
16930 discard_descr:
16931 /* Discard the descriptor and release any addr_table entries it uses. */
16932 remove_loc_list_addr_table_entries (descr);
16933 return NULL;
16936 /* Return the dwarf representation of the location list LOC_LIST of
16937 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16938 function. */
16940 static dw_loc_list_ref
16941 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16943 const char *endname, *secname;
16944 var_loc_view endview;
16945 rtx varloc;
16946 enum var_init_status initialized;
16947 struct var_loc_node *node;
16948 dw_loc_descr_ref descr;
16949 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16950 dw_loc_list_ref list = NULL;
16951 dw_loc_list_ref *listp = &list;
16953 /* Now that we know what section we are using for a base,
16954 actually construct the list of locations.
16955 The first location information is what is passed to the
16956 function that creates the location list, and the remaining
16957 locations just get added on to that list.
16958 Note that we only know the start address for a location
16959 (IE location changes), so to build the range, we use
16960 the range [current location start, next location start].
16961 This means we have to special case the last node, and generate
16962 a range of [last location start, end of function label]. */
16964 if (cfun && crtl->has_bb_partition)
16966 bool save_in_cold_section_p = in_cold_section_p;
16967 in_cold_section_p = first_function_block_is_cold;
16968 if (loc_list->last_before_switch == NULL)
16969 in_cold_section_p = !in_cold_section_p;
16970 secname = secname_for_decl (decl);
16971 in_cold_section_p = save_in_cold_section_p;
16973 else
16974 secname = secname_for_decl (decl);
16976 for (node = loc_list->first; node; node = node->next)
16978 bool range_across_switch = false;
16979 if (GET_CODE (node->loc) == EXPR_LIST
16980 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16982 if (GET_CODE (node->loc) == EXPR_LIST)
16984 descr = NULL;
16985 /* This requires DW_OP_{,bit_}piece, which is not usable
16986 inside DWARF expressions. */
16987 if (want_address == 2)
16988 descr = dw_sra_loc_expr (decl, node->loc);
16990 else
16992 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16993 varloc = NOTE_VAR_LOCATION (node->loc);
16994 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16996 if (descr)
16998 /* If section switch happens in between node->label
16999 and node->next->label (or end of function) and
17000 we can't emit it as a single entry list,
17001 emit two ranges, first one ending at the end
17002 of first partition and second one starting at the
17003 beginning of second partition. */
17004 if (node == loc_list->last_before_switch
17005 && (node != loc_list->first || loc_list->first->next)
17006 && current_function_decl)
17008 endname = cfun->fde->dw_fde_end;
17009 endview = 0;
17010 range_across_switch = true;
17012 /* The variable has a location between NODE->LABEL and
17013 NODE->NEXT->LABEL. */
17014 else if (node->next)
17015 endname = node->next->label, endview = node->next->view;
17016 /* If the variable has a location at the last label
17017 it keeps its location until the end of function. */
17018 else if (!current_function_decl)
17019 endname = text_end_label, endview = 0;
17020 else
17022 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17023 current_function_funcdef_no);
17024 endname = ggc_strdup (label_id);
17025 endview = 0;
17028 *listp = new_loc_list (descr, node->label, node->view,
17029 endname, endview, secname);
17030 if (TREE_CODE (decl) == PARM_DECL
17031 && node == loc_list->first
17032 && NOTE_P (node->loc)
17033 && strcmp (node->label, endname) == 0)
17034 (*listp)->force = true;
17035 listp = &(*listp)->dw_loc_next;
17039 if (cfun
17040 && crtl->has_bb_partition
17041 && node == loc_list->last_before_switch)
17043 bool save_in_cold_section_p = in_cold_section_p;
17044 in_cold_section_p = !first_function_block_is_cold;
17045 secname = secname_for_decl (decl);
17046 in_cold_section_p = save_in_cold_section_p;
17049 if (range_across_switch)
17051 if (GET_CODE (node->loc) == EXPR_LIST)
17052 descr = dw_sra_loc_expr (decl, node->loc);
17053 else
17055 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17056 varloc = NOTE_VAR_LOCATION (node->loc);
17057 descr = dw_loc_list_1 (decl, varloc, want_address,
17058 initialized);
17060 gcc_assert (descr);
17061 /* The variable has a location between NODE->LABEL and
17062 NODE->NEXT->LABEL. */
17063 if (node->next)
17064 endname = node->next->label, endview = node->next->view;
17065 else
17066 endname = cfun->fde->dw_fde_second_end, endview = 0;
17067 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17068 endname, endview, secname);
17069 listp = &(*listp)->dw_loc_next;
17073 /* Try to avoid the overhead of a location list emitting a location
17074 expression instead, but only if we didn't have more than one
17075 location entry in the first place. If some entries were not
17076 representable, we don't want to pretend a single entry that was
17077 applies to the entire scope in which the variable is
17078 available. */
17079 maybe_gen_llsym (list);
17081 return list;
17084 /* Return if the loc_list has only single element and thus can be represented
17085 as location description. */
17087 static bool
17088 single_element_loc_list_p (dw_loc_list_ref list)
17090 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17091 return !list->ll_symbol;
17094 /* Duplicate a single element of location list. */
17096 static inline dw_loc_descr_ref
17097 copy_loc_descr (dw_loc_descr_ref ref)
17099 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17100 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17101 return copy;
17104 /* To each location in list LIST append loc descr REF. */
17106 static void
17107 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17109 dw_loc_descr_ref copy;
17110 add_loc_descr (&list->expr, ref);
17111 list = list->dw_loc_next;
17112 while (list)
17114 copy = copy_loc_descr (ref);
17115 add_loc_descr (&list->expr, copy);
17116 while (copy->dw_loc_next)
17117 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17118 list = list->dw_loc_next;
17122 /* To each location in list LIST prepend loc descr REF. */
17124 static void
17125 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17127 dw_loc_descr_ref copy;
17128 dw_loc_descr_ref ref_end = list->expr;
17129 add_loc_descr (&ref, list->expr);
17130 list->expr = ref;
17131 list = list->dw_loc_next;
17132 while (list)
17134 dw_loc_descr_ref end = list->expr;
17135 list->expr = copy = copy_loc_descr (ref);
17136 while (copy->dw_loc_next != ref_end)
17137 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17138 copy->dw_loc_next = end;
17139 list = list->dw_loc_next;
17143 /* Given two lists RET and LIST
17144 produce location list that is result of adding expression in LIST
17145 to expression in RET on each position in program.
17146 Might be destructive on both RET and LIST.
17148 TODO: We handle only simple cases of RET or LIST having at most one
17149 element. General case would involve sorting the lists in program order
17150 and merging them that will need some additional work.
17151 Adding that will improve quality of debug info especially for SRA-ed
17152 structures. */
17154 static void
17155 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17157 if (!list)
17158 return;
17159 if (!*ret)
17161 *ret = list;
17162 return;
17164 if (!list->dw_loc_next)
17166 add_loc_descr_to_each (*ret, list->expr);
17167 return;
17169 if (!(*ret)->dw_loc_next)
17171 prepend_loc_descr_to_each (list, (*ret)->expr);
17172 *ret = list;
17173 return;
17175 expansion_failed (NULL_TREE, NULL_RTX,
17176 "Don't know how to merge two non-trivial"
17177 " location lists.\n");
17178 *ret = NULL;
17179 return;
17182 /* LOC is constant expression. Try a luck, look it up in constant
17183 pool and return its loc_descr of its address. */
17185 static dw_loc_descr_ref
17186 cst_pool_loc_descr (tree loc)
17188 /* Get an RTL for this, if something has been emitted. */
17189 rtx rtl = lookup_constant_def (loc);
17191 if (!rtl || !MEM_P (rtl))
17193 gcc_assert (!rtl);
17194 return 0;
17196 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17198 /* TODO: We might get more coverage if we was actually delaying expansion
17199 of all expressions till end of compilation when constant pools are fully
17200 populated. */
17201 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17203 expansion_failed (loc, NULL_RTX,
17204 "CST value in contant pool but not marked.");
17205 return 0;
17207 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17208 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17211 /* Return dw_loc_list representing address of addr_expr LOC
17212 by looking for inner INDIRECT_REF expression and turning
17213 it into simple arithmetics.
17215 See loc_list_from_tree for the meaning of CONTEXT. */
17217 static dw_loc_list_ref
17218 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17219 loc_descr_context *context)
17221 tree obj, offset;
17222 poly_int64 bitsize, bitpos, bytepos;
17223 machine_mode mode;
17224 int unsignedp, reversep, volatilep = 0;
17225 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17227 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17228 &bitsize, &bitpos, &offset, &mode,
17229 &unsignedp, &reversep, &volatilep);
17230 STRIP_NOPS (obj);
17231 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17233 expansion_failed (loc, NULL_RTX, "bitfield access");
17234 return 0;
17236 if (!INDIRECT_REF_P (obj))
17238 expansion_failed (obj,
17239 NULL_RTX, "no indirect ref in inner refrence");
17240 return 0;
17242 if (!offset && known_eq (bitpos, 0))
17243 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17244 context);
17245 else if (toplev
17246 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17247 && (dwarf_version >= 4 || !dwarf_strict))
17249 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17250 if (!list_ret)
17251 return 0;
17252 if (offset)
17254 /* Variable offset. */
17255 list_ret1 = loc_list_from_tree (offset, 0, context);
17256 if (list_ret1 == 0)
17257 return 0;
17258 add_loc_list (&list_ret, list_ret1);
17259 if (!list_ret)
17260 return 0;
17261 add_loc_descr_to_each (list_ret,
17262 new_loc_descr (DW_OP_plus, 0, 0));
17264 HOST_WIDE_INT value;
17265 if (bytepos.is_constant (&value) && value > 0)
17266 add_loc_descr_to_each (list_ret,
17267 new_loc_descr (DW_OP_plus_uconst, value, 0));
17268 else if (maybe_ne (bytepos, 0))
17269 loc_list_plus_const (list_ret, bytepos);
17270 add_loc_descr_to_each (list_ret,
17271 new_loc_descr (DW_OP_stack_value, 0, 0));
17273 return list_ret;
17276 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17277 all operations from LOC are nops, move to the last one. Insert in NOPS all
17278 operations that are skipped. */
17280 static void
17281 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17282 hash_set<dw_loc_descr_ref> &nops)
17284 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17286 nops.add (loc);
17287 loc = loc->dw_loc_next;
17291 /* Helper for loc_descr_without_nops: free the location description operation
17292 P. */
17294 bool
17295 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17297 ggc_free (loc);
17298 return true;
17301 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17302 finishes LOC. */
17304 static void
17305 loc_descr_without_nops (dw_loc_descr_ref &loc)
17307 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17308 return;
17310 /* Set of all DW_OP_nop operations we remove. */
17311 hash_set<dw_loc_descr_ref> nops;
17313 /* First, strip all prefix NOP operations in order to keep the head of the
17314 operations list. */
17315 loc_descr_to_next_no_nop (loc, nops);
17317 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17319 /* For control flow operations: strip "prefix" nops in destination
17320 labels. */
17321 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17322 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17323 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17324 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17326 /* Do the same for the operations that follow, then move to the next
17327 iteration. */
17328 if (cur->dw_loc_next != NULL)
17329 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17330 cur = cur->dw_loc_next;
17333 nops.traverse<void *, free_loc_descr> (NULL);
17337 struct dwarf_procedure_info;
17339 /* Helper structure for location descriptions generation. */
17340 struct loc_descr_context
17342 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17343 NULL_TREE if DW_OP_push_object_address in invalid for this location
17344 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17345 tree context_type;
17346 /* The ..._DECL node that should be translated as a
17347 DW_OP_push_object_address operation. */
17348 tree base_decl;
17349 /* Information about the DWARF procedure we are currently generating. NULL if
17350 we are not generating a DWARF procedure. */
17351 struct dwarf_procedure_info *dpi;
17352 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17353 by consumer. Used for DW_TAG_generic_subrange attributes. */
17354 bool placeholder_arg;
17355 /* True if PLACEHOLDER_EXPR has been seen. */
17356 bool placeholder_seen;
17359 /* DWARF procedures generation
17361 DWARF expressions (aka. location descriptions) are used to encode variable
17362 things such as sizes or offsets. Such computations can have redundant parts
17363 that can be factorized in order to reduce the size of the output debug
17364 information. This is the whole point of DWARF procedures.
17366 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17367 already factorized into functions ("size functions") in order to handle very
17368 big and complex types. Such functions are quite simple: they have integral
17369 arguments, they return an integral result and their body contains only a
17370 return statement with arithmetic expressions. This is the only kind of
17371 function we are interested in translating into DWARF procedures, here.
17373 DWARF expressions and DWARF procedure are executed using a stack, so we have
17374 to define some calling convention for them to interact. Let's say that:
17376 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17377 all arguments in reverse order (right-to-left) so that when the DWARF
17378 procedure execution starts, the first argument is the top of the stack.
17380 - Then, when returning, the DWARF procedure must have consumed all arguments
17381 on the stack, must have pushed the result and touched nothing else.
17383 - Each integral argument and the result are integral types can be hold in a
17384 single stack slot.
17386 - We call "frame offset" the number of stack slots that are "under DWARF
17387 procedure control": it includes the arguments slots, the temporaries and
17388 the result slot. Thus, it is equal to the number of arguments when the
17389 procedure execution starts and must be equal to one (the result) when it
17390 returns. */
17392 /* Helper structure used when generating operations for a DWARF procedure. */
17393 struct dwarf_procedure_info
17395 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17396 currently translated. */
17397 tree fndecl;
17398 /* The number of arguments FNDECL takes. */
17399 unsigned args_count;
17402 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17403 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17404 equate it to this DIE. */
17406 static dw_die_ref
17407 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17408 dw_die_ref parent_die)
17410 dw_die_ref dwarf_proc_die;
17412 if ((dwarf_version < 3 && dwarf_strict)
17413 || location == NULL)
17414 return NULL;
17416 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17417 if (fndecl)
17418 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17419 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17420 return dwarf_proc_die;
17423 /* Return whether TYPE is a supported type as a DWARF procedure argument
17424 type or return type (we handle only scalar types and pointer types that
17425 aren't wider than the DWARF expression evaluation stack. */
17427 static bool
17428 is_handled_procedure_type (tree type)
17430 return ((INTEGRAL_TYPE_P (type)
17431 || TREE_CODE (type) == OFFSET_TYPE
17432 || TREE_CODE (type) == POINTER_TYPE)
17433 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17436 /* Helper for resolve_args_picking: do the same but stop when coming across
17437 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17438 offset *before* evaluating the corresponding operation. */
17440 static bool
17441 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17442 struct dwarf_procedure_info *dpi,
17443 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17445 /* The "frame_offset" identifier is already used to name a macro... */
17446 unsigned frame_offset_ = initial_frame_offset;
17447 dw_loc_descr_ref l;
17449 for (l = loc; l != NULL;)
17451 bool existed;
17452 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17454 /* If we already met this node, there is nothing to compute anymore. */
17455 if (existed)
17457 /* Make sure that the stack size is consistent wherever the execution
17458 flow comes from. */
17459 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17460 break;
17462 l_frame_offset = frame_offset_;
17464 /* If needed, relocate the picking offset with respect to the frame
17465 offset. */
17466 if (l->frame_offset_rel)
17468 unsigned HOST_WIDE_INT off;
17469 switch (l->dw_loc_opc)
17471 case DW_OP_pick:
17472 off = l->dw_loc_oprnd1.v.val_unsigned;
17473 break;
17474 case DW_OP_dup:
17475 off = 0;
17476 break;
17477 case DW_OP_over:
17478 off = 1;
17479 break;
17480 default:
17481 gcc_unreachable ();
17483 /* frame_offset_ is the size of the current stack frame, including
17484 incoming arguments. Besides, the arguments are pushed
17485 right-to-left. Thus, in order to access the Nth argument from
17486 this operation node, the picking has to skip temporaries *plus*
17487 one stack slot per argument (0 for the first one, 1 for the second
17488 one, etc.).
17490 The targetted argument number (N) is already set as the operand,
17491 and the number of temporaries can be computed with:
17492 frame_offsets_ - dpi->args_count */
17493 off += frame_offset_ - dpi->args_count;
17495 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17496 if (off > 255)
17497 return false;
17499 if (off == 0)
17501 l->dw_loc_opc = DW_OP_dup;
17502 l->dw_loc_oprnd1.v.val_unsigned = 0;
17504 else if (off == 1)
17506 l->dw_loc_opc = DW_OP_over;
17507 l->dw_loc_oprnd1.v.val_unsigned = 0;
17509 else
17511 l->dw_loc_opc = DW_OP_pick;
17512 l->dw_loc_oprnd1.v.val_unsigned = off;
17516 /* Update frame_offset according to the effect the current operation has
17517 on the stack. */
17518 switch (l->dw_loc_opc)
17520 case DW_OP_deref:
17521 case DW_OP_swap:
17522 case DW_OP_rot:
17523 case DW_OP_abs:
17524 case DW_OP_neg:
17525 case DW_OP_not:
17526 case DW_OP_plus_uconst:
17527 case DW_OP_skip:
17528 case DW_OP_reg0:
17529 case DW_OP_reg1:
17530 case DW_OP_reg2:
17531 case DW_OP_reg3:
17532 case DW_OP_reg4:
17533 case DW_OP_reg5:
17534 case DW_OP_reg6:
17535 case DW_OP_reg7:
17536 case DW_OP_reg8:
17537 case DW_OP_reg9:
17538 case DW_OP_reg10:
17539 case DW_OP_reg11:
17540 case DW_OP_reg12:
17541 case DW_OP_reg13:
17542 case DW_OP_reg14:
17543 case DW_OP_reg15:
17544 case DW_OP_reg16:
17545 case DW_OP_reg17:
17546 case DW_OP_reg18:
17547 case DW_OP_reg19:
17548 case DW_OP_reg20:
17549 case DW_OP_reg21:
17550 case DW_OP_reg22:
17551 case DW_OP_reg23:
17552 case DW_OP_reg24:
17553 case DW_OP_reg25:
17554 case DW_OP_reg26:
17555 case DW_OP_reg27:
17556 case DW_OP_reg28:
17557 case DW_OP_reg29:
17558 case DW_OP_reg30:
17559 case DW_OP_reg31:
17560 case DW_OP_bregx:
17561 case DW_OP_piece:
17562 case DW_OP_deref_size:
17563 case DW_OP_nop:
17564 case DW_OP_bit_piece:
17565 case DW_OP_implicit_value:
17566 case DW_OP_stack_value:
17567 break;
17569 case DW_OP_addr:
17570 case DW_OP_const1u:
17571 case DW_OP_const1s:
17572 case DW_OP_const2u:
17573 case DW_OP_const2s:
17574 case DW_OP_const4u:
17575 case DW_OP_const4s:
17576 case DW_OP_const8u:
17577 case DW_OP_const8s:
17578 case DW_OP_constu:
17579 case DW_OP_consts:
17580 case DW_OP_dup:
17581 case DW_OP_over:
17582 case DW_OP_pick:
17583 case DW_OP_lit0:
17584 case DW_OP_lit1:
17585 case DW_OP_lit2:
17586 case DW_OP_lit3:
17587 case DW_OP_lit4:
17588 case DW_OP_lit5:
17589 case DW_OP_lit6:
17590 case DW_OP_lit7:
17591 case DW_OP_lit8:
17592 case DW_OP_lit9:
17593 case DW_OP_lit10:
17594 case DW_OP_lit11:
17595 case DW_OP_lit12:
17596 case DW_OP_lit13:
17597 case DW_OP_lit14:
17598 case DW_OP_lit15:
17599 case DW_OP_lit16:
17600 case DW_OP_lit17:
17601 case DW_OP_lit18:
17602 case DW_OP_lit19:
17603 case DW_OP_lit20:
17604 case DW_OP_lit21:
17605 case DW_OP_lit22:
17606 case DW_OP_lit23:
17607 case DW_OP_lit24:
17608 case DW_OP_lit25:
17609 case DW_OP_lit26:
17610 case DW_OP_lit27:
17611 case DW_OP_lit28:
17612 case DW_OP_lit29:
17613 case DW_OP_lit30:
17614 case DW_OP_lit31:
17615 case DW_OP_breg0:
17616 case DW_OP_breg1:
17617 case DW_OP_breg2:
17618 case DW_OP_breg3:
17619 case DW_OP_breg4:
17620 case DW_OP_breg5:
17621 case DW_OP_breg6:
17622 case DW_OP_breg7:
17623 case DW_OP_breg8:
17624 case DW_OP_breg9:
17625 case DW_OP_breg10:
17626 case DW_OP_breg11:
17627 case DW_OP_breg12:
17628 case DW_OP_breg13:
17629 case DW_OP_breg14:
17630 case DW_OP_breg15:
17631 case DW_OP_breg16:
17632 case DW_OP_breg17:
17633 case DW_OP_breg18:
17634 case DW_OP_breg19:
17635 case DW_OP_breg20:
17636 case DW_OP_breg21:
17637 case DW_OP_breg22:
17638 case DW_OP_breg23:
17639 case DW_OP_breg24:
17640 case DW_OP_breg25:
17641 case DW_OP_breg26:
17642 case DW_OP_breg27:
17643 case DW_OP_breg28:
17644 case DW_OP_breg29:
17645 case DW_OP_breg30:
17646 case DW_OP_breg31:
17647 case DW_OP_fbreg:
17648 case DW_OP_push_object_address:
17649 case DW_OP_call_frame_cfa:
17650 case DW_OP_GNU_variable_value:
17651 ++frame_offset_;
17652 break;
17654 case DW_OP_drop:
17655 case DW_OP_xderef:
17656 case DW_OP_and:
17657 case DW_OP_div:
17658 case DW_OP_minus:
17659 case DW_OP_mod:
17660 case DW_OP_mul:
17661 case DW_OP_or:
17662 case DW_OP_plus:
17663 case DW_OP_shl:
17664 case DW_OP_shr:
17665 case DW_OP_shra:
17666 case DW_OP_xor:
17667 case DW_OP_bra:
17668 case DW_OP_eq:
17669 case DW_OP_ge:
17670 case DW_OP_gt:
17671 case DW_OP_le:
17672 case DW_OP_lt:
17673 case DW_OP_ne:
17674 case DW_OP_regx:
17675 case DW_OP_xderef_size:
17676 --frame_offset_;
17677 break;
17679 case DW_OP_call2:
17680 case DW_OP_call4:
17681 case DW_OP_call_ref:
17683 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17684 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17686 if (stack_usage == NULL)
17687 return false;
17688 frame_offset_ += *stack_usage;
17689 break;
17692 case DW_OP_implicit_pointer:
17693 case DW_OP_entry_value:
17694 case DW_OP_const_type:
17695 case DW_OP_regval_type:
17696 case DW_OP_deref_type:
17697 case DW_OP_convert:
17698 case DW_OP_reinterpret:
17699 case DW_OP_form_tls_address:
17700 case DW_OP_GNU_push_tls_address:
17701 case DW_OP_GNU_uninit:
17702 case DW_OP_GNU_encoded_addr:
17703 case DW_OP_GNU_implicit_pointer:
17704 case DW_OP_GNU_entry_value:
17705 case DW_OP_GNU_const_type:
17706 case DW_OP_GNU_regval_type:
17707 case DW_OP_GNU_deref_type:
17708 case DW_OP_GNU_convert:
17709 case DW_OP_GNU_reinterpret:
17710 case DW_OP_GNU_parameter_ref:
17711 /* loc_list_from_tree will probably not output these operations for
17712 size functions, so assume they will not appear here. */
17713 /* Fall through... */
17715 default:
17716 gcc_unreachable ();
17719 /* Now, follow the control flow (except subroutine calls). */
17720 switch (l->dw_loc_opc)
17722 case DW_OP_bra:
17723 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17724 frame_offsets))
17725 return false;
17726 /* Fall through. */
17728 case DW_OP_skip:
17729 l = l->dw_loc_oprnd1.v.val_loc;
17730 break;
17732 case DW_OP_stack_value:
17733 return true;
17735 default:
17736 l = l->dw_loc_next;
17737 break;
17741 return true;
17744 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17745 operations) in order to resolve the operand of DW_OP_pick operations that
17746 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17747 offset *before* LOC is executed. Return if all relocations were
17748 successful. */
17750 static bool
17751 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17752 struct dwarf_procedure_info *dpi)
17754 /* Associate to all visited operations the frame offset *before* evaluating
17755 this operation. */
17756 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17758 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17759 frame_offsets);
17762 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17763 Return NULL if it is not possible. */
17765 static dw_die_ref
17766 function_to_dwarf_procedure (tree fndecl)
17768 struct loc_descr_context ctx;
17769 struct dwarf_procedure_info dpi;
17770 dw_die_ref dwarf_proc_die;
17771 tree tree_body = DECL_SAVED_TREE (fndecl);
17772 dw_loc_descr_ref loc_body, epilogue;
17774 tree cursor;
17775 unsigned i;
17777 /* Do not generate multiple DWARF procedures for the same function
17778 declaration. */
17779 dwarf_proc_die = lookup_decl_die (fndecl);
17780 if (dwarf_proc_die != NULL)
17781 return dwarf_proc_die;
17783 /* DWARF procedures are available starting with the DWARFv3 standard. */
17784 if (dwarf_version < 3 && dwarf_strict)
17785 return NULL;
17787 /* We handle only functions for which we still have a body, that return a
17788 supported type and that takes arguments with supported types. Note that
17789 there is no point translating functions that return nothing. */
17790 if (tree_body == NULL_TREE
17791 || DECL_RESULT (fndecl) == NULL_TREE
17792 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17793 return NULL;
17795 for (cursor = DECL_ARGUMENTS (fndecl);
17796 cursor != NULL_TREE;
17797 cursor = TREE_CHAIN (cursor))
17798 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17799 return NULL;
17801 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17802 if (TREE_CODE (tree_body) != RETURN_EXPR)
17803 return NULL;
17804 tree_body = TREE_OPERAND (tree_body, 0);
17805 if (TREE_CODE (tree_body) != MODIFY_EXPR
17806 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17807 return NULL;
17808 tree_body = TREE_OPERAND (tree_body, 1);
17810 /* Try to translate the body expression itself. Note that this will probably
17811 cause an infinite recursion if its call graph has a cycle. This is very
17812 unlikely for size functions, however, so don't bother with such things at
17813 the moment. */
17814 ctx.context_type = NULL_TREE;
17815 ctx.base_decl = NULL_TREE;
17816 ctx.dpi = &dpi;
17817 ctx.placeholder_arg = false;
17818 ctx.placeholder_seen = false;
17819 dpi.fndecl = fndecl;
17820 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17821 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17822 if (!loc_body)
17823 return NULL;
17825 /* After evaluating all operands in "loc_body", we should still have on the
17826 stack all arguments plus the desired function result (top of the stack).
17827 Generate code in order to keep only the result in our stack frame. */
17828 epilogue = NULL;
17829 for (i = 0; i < dpi.args_count; ++i)
17831 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17832 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17833 op_couple->dw_loc_next->dw_loc_next = epilogue;
17834 epilogue = op_couple;
17836 add_loc_descr (&loc_body, epilogue);
17837 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17838 return NULL;
17840 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17841 because they are considered useful. Now there is an epilogue, they are
17842 not anymore, so give it another try. */
17843 loc_descr_without_nops (loc_body);
17845 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17846 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17847 though, given that size functions do not come from source, so they should
17848 not have a dedicated DW_TAG_subprogram DIE. */
17849 dwarf_proc_die
17850 = new_dwarf_proc_die (loc_body, fndecl,
17851 get_context_die (DECL_CONTEXT (fndecl)));
17853 /* The called DWARF procedure consumes one stack slot per argument and
17854 returns one stack slot. */
17855 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17857 return dwarf_proc_die;
17861 /* Generate Dwarf location list representing LOC.
17862 If WANT_ADDRESS is false, expression computing LOC will be computed
17863 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17864 if WANT_ADDRESS is 2, expression computing address useable in location
17865 will be returned (i.e. DW_OP_reg can be used
17866 to refer to register values).
17868 CONTEXT provides information to customize the location descriptions
17869 generation. Its context_type field specifies what type is implicitly
17870 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17871 will not be generated.
17873 Its DPI field determines whether we are generating a DWARF expression for a
17874 DWARF procedure, so PARM_DECL references are processed specifically.
17876 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17877 and dpi fields were null. */
17879 static dw_loc_list_ref
17880 loc_list_from_tree_1 (tree loc, int want_address,
17881 struct loc_descr_context *context)
17883 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17884 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17885 int have_address = 0;
17886 enum dwarf_location_atom op;
17888 /* ??? Most of the time we do not take proper care for sign/zero
17889 extending the values properly. Hopefully this won't be a real
17890 problem... */
17892 if (context != NULL
17893 && context->base_decl == loc
17894 && want_address == 0)
17896 if (dwarf_version >= 3 || !dwarf_strict)
17897 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17898 NULL, 0, NULL, 0, NULL);
17899 else
17900 return NULL;
17903 switch (TREE_CODE (loc))
17905 case ERROR_MARK:
17906 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17907 return 0;
17909 case PLACEHOLDER_EXPR:
17910 /* This case involves extracting fields from an object to determine the
17911 position of other fields. It is supposed to appear only as the first
17912 operand of COMPONENT_REF nodes and to reference precisely the type
17913 that the context allows. */
17914 if (context != NULL
17915 && TREE_TYPE (loc) == context->context_type
17916 && want_address >= 1)
17918 if (dwarf_version >= 3 || !dwarf_strict)
17920 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17921 have_address = 1;
17922 break;
17924 else
17925 return NULL;
17927 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17928 the single argument passed by consumer. */
17929 else if (context != NULL
17930 && context->placeholder_arg
17931 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17932 && want_address == 0)
17934 ret = new_loc_descr (DW_OP_pick, 0, 0);
17935 ret->frame_offset_rel = 1;
17936 context->placeholder_seen = true;
17937 break;
17939 else
17940 expansion_failed (loc, NULL_RTX,
17941 "PLACEHOLDER_EXPR for an unexpected type");
17942 break;
17944 case CALL_EXPR:
17946 const int nargs = call_expr_nargs (loc);
17947 tree callee = get_callee_fndecl (loc);
17948 int i;
17949 dw_die_ref dwarf_proc;
17951 if (callee == NULL_TREE)
17952 goto call_expansion_failed;
17954 /* We handle only functions that return an integer. */
17955 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17956 goto call_expansion_failed;
17958 dwarf_proc = function_to_dwarf_procedure (callee);
17959 if (dwarf_proc == NULL)
17960 goto call_expansion_failed;
17962 /* Evaluate arguments right-to-left so that the first argument will
17963 be the top-most one on the stack. */
17964 for (i = nargs - 1; i >= 0; --i)
17966 dw_loc_descr_ref loc_descr
17967 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17968 context);
17970 if (loc_descr == NULL)
17971 goto call_expansion_failed;
17973 add_loc_descr (&ret, loc_descr);
17976 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17977 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17978 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17979 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17980 add_loc_descr (&ret, ret1);
17981 break;
17983 call_expansion_failed:
17984 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17985 /* There are no opcodes for these operations. */
17986 return 0;
17989 case PREINCREMENT_EXPR:
17990 case PREDECREMENT_EXPR:
17991 case POSTINCREMENT_EXPR:
17992 case POSTDECREMENT_EXPR:
17993 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17994 /* There are no opcodes for these operations. */
17995 return 0;
17997 case ADDR_EXPR:
17998 /* If we already want an address, see if there is INDIRECT_REF inside
17999 e.g. for &this->field. */
18000 if (want_address)
18002 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18003 (loc, want_address == 2, context);
18004 if (list_ret)
18005 have_address = 1;
18006 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18007 && (ret = cst_pool_loc_descr (loc)))
18008 have_address = 1;
18010 /* Otherwise, process the argument and look for the address. */
18011 if (!list_ret && !ret)
18012 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18013 else
18015 if (want_address)
18016 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18017 return NULL;
18019 break;
18021 case VAR_DECL:
18022 if (DECL_THREAD_LOCAL_P (loc))
18024 rtx rtl;
18025 enum dwarf_location_atom tls_op;
18026 enum dtprel_bool dtprel = dtprel_false;
18028 if (targetm.have_tls)
18030 /* If this is not defined, we have no way to emit the
18031 data. */
18032 if (!targetm.asm_out.output_dwarf_dtprel)
18033 return 0;
18035 /* The way DW_OP_GNU_push_tls_address is specified, we
18036 can only look up addresses of objects in the current
18037 module. We used DW_OP_addr as first op, but that's
18038 wrong, because DW_OP_addr is relocated by the debug
18039 info consumer, while DW_OP_GNU_push_tls_address
18040 operand shouldn't be. */
18041 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18042 return 0;
18043 dtprel = dtprel_true;
18044 /* We check for DWARF 5 here because gdb did not implement
18045 DW_OP_form_tls_address until after 7.12. */
18046 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18047 : DW_OP_GNU_push_tls_address);
18049 else
18051 if (!targetm.emutls.debug_form_tls_address
18052 || !(dwarf_version >= 3 || !dwarf_strict))
18053 return 0;
18054 /* We stuffed the control variable into the DECL_VALUE_EXPR
18055 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18056 no longer appear in gimple code. We used the control
18057 variable in specific so that we could pick it up here. */
18058 loc = DECL_VALUE_EXPR (loc);
18059 tls_op = DW_OP_form_tls_address;
18062 rtl = rtl_for_decl_location (loc);
18063 if (rtl == NULL_RTX)
18064 return 0;
18066 if (!MEM_P (rtl))
18067 return 0;
18068 rtl = XEXP (rtl, 0);
18069 if (! CONSTANT_P (rtl))
18070 return 0;
18072 ret = new_addr_loc_descr (rtl, dtprel);
18073 ret1 = new_loc_descr (tls_op, 0, 0);
18074 add_loc_descr (&ret, ret1);
18076 have_address = 1;
18077 break;
18079 /* FALLTHRU */
18081 case PARM_DECL:
18082 if (context != NULL && context->dpi != NULL
18083 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18085 /* We are generating code for a DWARF procedure and we want to access
18086 one of its arguments: find the appropriate argument offset and let
18087 the resolve_args_picking pass compute the offset that complies
18088 with the stack frame size. */
18089 unsigned i = 0;
18090 tree cursor;
18092 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18093 cursor != NULL_TREE && cursor != loc;
18094 cursor = TREE_CHAIN (cursor), ++i)
18096 /* If we are translating a DWARF procedure, all referenced parameters
18097 must belong to the current function. */
18098 gcc_assert (cursor != NULL_TREE);
18100 ret = new_loc_descr (DW_OP_pick, i, 0);
18101 ret->frame_offset_rel = 1;
18102 break;
18104 /* FALLTHRU */
18106 case RESULT_DECL:
18107 if (DECL_HAS_VALUE_EXPR_P (loc))
18108 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18109 want_address, context);
18110 /* FALLTHRU */
18112 case FUNCTION_DECL:
18114 rtx rtl;
18115 var_loc_list *loc_list = lookup_decl_loc (loc);
18117 if (loc_list && loc_list->first)
18119 list_ret = dw_loc_list (loc_list, loc, want_address);
18120 have_address = want_address != 0;
18121 break;
18123 rtl = rtl_for_decl_location (loc);
18124 if (rtl == NULL_RTX)
18126 if (TREE_CODE (loc) != FUNCTION_DECL
18127 && early_dwarf
18128 && current_function_decl
18129 && want_address != 1
18130 && ! DECL_IGNORED_P (loc)
18131 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18132 || POINTER_TYPE_P (TREE_TYPE (loc)))
18133 && DECL_CONTEXT (loc) == current_function_decl
18134 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18135 <= DWARF2_ADDR_SIZE))
18137 dw_die_ref ref = lookup_decl_die (loc);
18138 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18139 if (ref)
18141 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18142 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18143 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18145 else
18147 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18148 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18150 break;
18152 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18153 return 0;
18155 else if (CONST_INT_P (rtl))
18157 HOST_WIDE_INT val = INTVAL (rtl);
18158 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18159 val &= GET_MODE_MASK (DECL_MODE (loc));
18160 ret = int_loc_descriptor (val);
18162 else if (GET_CODE (rtl) == CONST_STRING)
18164 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18165 return 0;
18167 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18168 ret = new_addr_loc_descr (rtl, dtprel_false);
18169 else
18171 machine_mode mode, mem_mode;
18173 /* Certain constructs can only be represented at top-level. */
18174 if (want_address == 2)
18176 ret = loc_descriptor (rtl, VOIDmode,
18177 VAR_INIT_STATUS_INITIALIZED);
18178 have_address = 1;
18180 else
18182 mode = GET_MODE (rtl);
18183 mem_mode = VOIDmode;
18184 if (MEM_P (rtl))
18186 mem_mode = mode;
18187 mode = get_address_mode (rtl);
18188 rtl = XEXP (rtl, 0);
18189 have_address = 1;
18191 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18192 VAR_INIT_STATUS_INITIALIZED);
18194 if (!ret)
18195 expansion_failed (loc, rtl,
18196 "failed to produce loc descriptor for rtl");
18199 break;
18201 case MEM_REF:
18202 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18204 have_address = 1;
18205 goto do_plus;
18207 /* Fallthru. */
18208 case INDIRECT_REF:
18209 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18210 have_address = 1;
18211 break;
18213 case TARGET_MEM_REF:
18214 case SSA_NAME:
18215 case DEBUG_EXPR_DECL:
18216 return NULL;
18218 case COMPOUND_EXPR:
18219 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18220 context);
18222 CASE_CONVERT:
18223 case VIEW_CONVERT_EXPR:
18224 case SAVE_EXPR:
18225 case MODIFY_EXPR:
18226 case NON_LVALUE_EXPR:
18227 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18228 context);
18230 case COMPONENT_REF:
18231 case BIT_FIELD_REF:
18232 case ARRAY_REF:
18233 case ARRAY_RANGE_REF:
18234 case REALPART_EXPR:
18235 case IMAGPART_EXPR:
18237 tree obj, offset;
18238 poly_int64 bitsize, bitpos, bytepos;
18239 machine_mode mode;
18240 int unsignedp, reversep, volatilep = 0;
18242 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18243 &unsignedp, &reversep, &volatilep);
18245 gcc_assert (obj != loc);
18247 list_ret = loc_list_from_tree_1 (obj,
18248 want_address == 2
18249 && known_eq (bitpos, 0)
18250 && !offset ? 2 : 1,
18251 context);
18252 /* TODO: We can extract value of the small expression via shifting even
18253 for nonzero bitpos. */
18254 if (list_ret == 0)
18255 return 0;
18256 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18257 || !multiple_p (bitsize, BITS_PER_UNIT))
18259 expansion_failed (loc, NULL_RTX,
18260 "bitfield access");
18261 return 0;
18264 if (offset != NULL_TREE)
18266 /* Variable offset. */
18267 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18268 if (list_ret1 == 0)
18269 return 0;
18270 add_loc_list (&list_ret, list_ret1);
18271 if (!list_ret)
18272 return 0;
18273 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18276 HOST_WIDE_INT value;
18277 if (bytepos.is_constant (&value) && value > 0)
18278 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18279 value, 0));
18280 else if (maybe_ne (bytepos, 0))
18281 loc_list_plus_const (list_ret, bytepos);
18283 have_address = 1;
18284 break;
18287 case INTEGER_CST:
18288 if ((want_address || !tree_fits_shwi_p (loc))
18289 && (ret = cst_pool_loc_descr (loc)))
18290 have_address = 1;
18291 else if (want_address == 2
18292 && tree_fits_shwi_p (loc)
18293 && (ret = address_of_int_loc_descriptor
18294 (int_size_in_bytes (TREE_TYPE (loc)),
18295 tree_to_shwi (loc))))
18296 have_address = 1;
18297 else if (tree_fits_shwi_p (loc))
18298 ret = int_loc_descriptor (tree_to_shwi (loc));
18299 else if (tree_fits_uhwi_p (loc))
18300 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18301 else
18303 expansion_failed (loc, NULL_RTX,
18304 "Integer operand is not host integer");
18305 return 0;
18307 break;
18309 case CONSTRUCTOR:
18310 case REAL_CST:
18311 case STRING_CST:
18312 case COMPLEX_CST:
18313 if ((ret = cst_pool_loc_descr (loc)))
18314 have_address = 1;
18315 else if (TREE_CODE (loc) == CONSTRUCTOR)
18317 tree type = TREE_TYPE (loc);
18318 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18319 unsigned HOST_WIDE_INT offset = 0;
18320 unsigned HOST_WIDE_INT cnt;
18321 constructor_elt *ce;
18323 if (TREE_CODE (type) == RECORD_TYPE)
18325 /* This is very limited, but it's enough to output
18326 pointers to member functions, as long as the
18327 referenced function is defined in the current
18328 translation unit. */
18329 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18331 tree val = ce->value;
18333 tree field = ce->index;
18335 if (val)
18336 STRIP_NOPS (val);
18338 if (!field || DECL_BIT_FIELD (field))
18340 expansion_failed (loc, NULL_RTX,
18341 "bitfield in record type constructor");
18342 size = offset = (unsigned HOST_WIDE_INT)-1;
18343 ret = NULL;
18344 break;
18347 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18348 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18349 gcc_assert (pos + fieldsize <= size);
18350 if (pos < offset)
18352 expansion_failed (loc, NULL_RTX,
18353 "out-of-order fields in record constructor");
18354 size = offset = (unsigned HOST_WIDE_INT)-1;
18355 ret = NULL;
18356 break;
18358 if (pos > offset)
18360 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18361 add_loc_descr (&ret, ret1);
18362 offset = pos;
18364 if (val && fieldsize != 0)
18366 ret1 = loc_descriptor_from_tree (val, want_address, context);
18367 if (!ret1)
18369 expansion_failed (loc, NULL_RTX,
18370 "unsupported expression in field");
18371 size = offset = (unsigned HOST_WIDE_INT)-1;
18372 ret = NULL;
18373 break;
18375 add_loc_descr (&ret, ret1);
18377 if (fieldsize)
18379 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18380 add_loc_descr (&ret, ret1);
18381 offset = pos + fieldsize;
18385 if (offset != size)
18387 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18388 add_loc_descr (&ret, ret1);
18389 offset = size;
18392 have_address = !!want_address;
18394 else
18395 expansion_failed (loc, NULL_RTX,
18396 "constructor of non-record type");
18398 else
18399 /* We can construct small constants here using int_loc_descriptor. */
18400 expansion_failed (loc, NULL_RTX,
18401 "constructor or constant not in constant pool");
18402 break;
18404 case TRUTH_AND_EXPR:
18405 case TRUTH_ANDIF_EXPR:
18406 case BIT_AND_EXPR:
18407 op = DW_OP_and;
18408 goto do_binop;
18410 case TRUTH_XOR_EXPR:
18411 case BIT_XOR_EXPR:
18412 op = DW_OP_xor;
18413 goto do_binop;
18415 case TRUTH_OR_EXPR:
18416 case TRUTH_ORIF_EXPR:
18417 case BIT_IOR_EXPR:
18418 op = DW_OP_or;
18419 goto do_binop;
18421 case FLOOR_DIV_EXPR:
18422 case CEIL_DIV_EXPR:
18423 case ROUND_DIV_EXPR:
18424 case TRUNC_DIV_EXPR:
18425 case EXACT_DIV_EXPR:
18426 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18427 return 0;
18428 op = DW_OP_div;
18429 goto do_binop;
18431 case MINUS_EXPR:
18432 op = DW_OP_minus;
18433 goto do_binop;
18435 case FLOOR_MOD_EXPR:
18436 case CEIL_MOD_EXPR:
18437 case ROUND_MOD_EXPR:
18438 case TRUNC_MOD_EXPR:
18439 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18441 op = DW_OP_mod;
18442 goto do_binop;
18444 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18445 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18446 if (list_ret == 0 || list_ret1 == 0)
18447 return 0;
18449 add_loc_list (&list_ret, list_ret1);
18450 if (list_ret == 0)
18451 return 0;
18452 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18453 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18454 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18455 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18456 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18457 break;
18459 case MULT_EXPR:
18460 op = DW_OP_mul;
18461 goto do_binop;
18463 case LSHIFT_EXPR:
18464 op = DW_OP_shl;
18465 goto do_binop;
18467 case RSHIFT_EXPR:
18468 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18469 goto do_binop;
18471 case POINTER_PLUS_EXPR:
18472 case PLUS_EXPR:
18473 do_plus:
18474 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18476 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18477 smarter to encode their opposite. The DW_OP_plus_uconst operation
18478 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18479 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18480 bytes, Y being the size of the operation that pushes the opposite
18481 of the addend. So let's choose the smallest representation. */
18482 const tree tree_addend = TREE_OPERAND (loc, 1);
18483 offset_int wi_addend;
18484 HOST_WIDE_INT shwi_addend;
18485 dw_loc_descr_ref loc_naddend;
18487 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18488 if (list_ret == 0)
18489 return 0;
18491 /* Try to get the literal to push. It is the opposite of the addend,
18492 so as we rely on wrapping during DWARF evaluation, first decode
18493 the literal as a "DWARF-sized" signed number. */
18494 wi_addend = wi::to_offset (tree_addend);
18495 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18496 shwi_addend = wi_addend.to_shwi ();
18497 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18498 ? int_loc_descriptor (-shwi_addend)
18499 : NULL;
18501 if (loc_naddend != NULL
18502 && ((unsigned) size_of_uleb128 (shwi_addend)
18503 > size_of_loc_descr (loc_naddend)))
18505 add_loc_descr_to_each (list_ret, loc_naddend);
18506 add_loc_descr_to_each (list_ret,
18507 new_loc_descr (DW_OP_minus, 0, 0));
18509 else
18511 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18513 loc_naddend = loc_cur;
18514 loc_cur = loc_cur->dw_loc_next;
18515 ggc_free (loc_naddend);
18517 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18519 break;
18522 op = DW_OP_plus;
18523 goto do_binop;
18525 case LE_EXPR:
18526 op = DW_OP_le;
18527 goto do_comp_binop;
18529 case GE_EXPR:
18530 op = DW_OP_ge;
18531 goto do_comp_binop;
18533 case LT_EXPR:
18534 op = DW_OP_lt;
18535 goto do_comp_binop;
18537 case GT_EXPR:
18538 op = DW_OP_gt;
18539 goto do_comp_binop;
18541 do_comp_binop:
18542 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18544 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18545 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18546 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18547 TREE_CODE (loc));
18548 break;
18550 else
18551 goto do_binop;
18553 case EQ_EXPR:
18554 op = DW_OP_eq;
18555 goto do_binop;
18557 case NE_EXPR:
18558 op = DW_OP_ne;
18559 goto do_binop;
18561 do_binop:
18562 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18563 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18564 if (list_ret == 0 || list_ret1 == 0)
18565 return 0;
18567 add_loc_list (&list_ret, list_ret1);
18568 if (list_ret == 0)
18569 return 0;
18570 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18571 break;
18573 case TRUTH_NOT_EXPR:
18574 case BIT_NOT_EXPR:
18575 op = DW_OP_not;
18576 goto do_unop;
18578 case ABS_EXPR:
18579 op = DW_OP_abs;
18580 goto do_unop;
18582 case NEGATE_EXPR:
18583 op = DW_OP_neg;
18584 goto do_unop;
18586 do_unop:
18587 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18588 if (list_ret == 0)
18589 return 0;
18591 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18592 break;
18594 case MIN_EXPR:
18595 case MAX_EXPR:
18597 const enum tree_code code =
18598 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18600 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18601 build2 (code, integer_type_node,
18602 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18603 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18606 /* fall through */
18608 case COND_EXPR:
18610 dw_loc_descr_ref lhs
18611 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18612 dw_loc_list_ref rhs
18613 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18614 dw_loc_descr_ref bra_node, jump_node, tmp;
18616 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18617 if (list_ret == 0 || lhs == 0 || rhs == 0)
18618 return 0;
18620 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18621 add_loc_descr_to_each (list_ret, bra_node);
18623 add_loc_list (&list_ret, rhs);
18624 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18625 add_loc_descr_to_each (list_ret, jump_node);
18627 add_loc_descr_to_each (list_ret, lhs);
18628 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18629 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18631 /* ??? Need a node to point the skip at. Use a nop. */
18632 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18633 add_loc_descr_to_each (list_ret, tmp);
18634 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18635 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18637 break;
18639 case FIX_TRUNC_EXPR:
18640 return 0;
18642 default:
18643 /* Leave front-end specific codes as simply unknown. This comes
18644 up, for instance, with the C STMT_EXPR. */
18645 if ((unsigned int) TREE_CODE (loc)
18646 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18648 expansion_failed (loc, NULL_RTX,
18649 "language specific tree node");
18650 return 0;
18653 /* Otherwise this is a generic code; we should just lists all of
18654 these explicitly. We forgot one. */
18655 if (flag_checking)
18656 gcc_unreachable ();
18658 /* In a release build, we want to degrade gracefully: better to
18659 generate incomplete debugging information than to crash. */
18660 return NULL;
18663 if (!ret && !list_ret)
18664 return 0;
18666 if (want_address == 2 && !have_address
18667 && (dwarf_version >= 4 || !dwarf_strict))
18669 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18671 expansion_failed (loc, NULL_RTX,
18672 "DWARF address size mismatch");
18673 return 0;
18675 if (ret)
18676 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18677 else
18678 add_loc_descr_to_each (list_ret,
18679 new_loc_descr (DW_OP_stack_value, 0, 0));
18680 have_address = 1;
18682 /* Show if we can't fill the request for an address. */
18683 if (want_address && !have_address)
18685 expansion_failed (loc, NULL_RTX,
18686 "Want address and only have value");
18687 return 0;
18690 gcc_assert (!ret || !list_ret);
18692 /* If we've got an address and don't want one, dereference. */
18693 if (!want_address && have_address)
18695 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18697 if (size > DWARF2_ADDR_SIZE || size == -1)
18699 expansion_failed (loc, NULL_RTX,
18700 "DWARF address size mismatch");
18701 return 0;
18703 else if (size == DWARF2_ADDR_SIZE)
18704 op = DW_OP_deref;
18705 else
18706 op = DW_OP_deref_size;
18708 if (ret)
18709 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18710 else
18711 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18713 if (ret)
18714 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18716 return list_ret;
18719 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18720 expressions. */
18722 static dw_loc_list_ref
18723 loc_list_from_tree (tree loc, int want_address,
18724 struct loc_descr_context *context)
18726 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18728 for (dw_loc_list_ref loc_cur = result;
18729 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18730 loc_descr_without_nops (loc_cur->expr);
18731 return result;
18734 /* Same as above but return only single location expression. */
18735 static dw_loc_descr_ref
18736 loc_descriptor_from_tree (tree loc, int want_address,
18737 struct loc_descr_context *context)
18739 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18740 if (!ret)
18741 return NULL;
18742 if (ret->dw_loc_next)
18744 expansion_failed (loc, NULL_RTX,
18745 "Location list where only loc descriptor needed");
18746 return NULL;
18748 return ret->expr;
18751 /* Given a value, round it up to the lowest multiple of `boundary'
18752 which is not less than the value itself. */
18754 static inline HOST_WIDE_INT
18755 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18757 return (((value + boundary - 1) / boundary) * boundary);
18760 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18761 pointer to the declared type for the relevant field variable, or return
18762 `integer_type_node' if the given node turns out to be an
18763 ERROR_MARK node. */
18765 static inline tree
18766 field_type (const_tree decl)
18768 tree type;
18770 if (TREE_CODE (decl) == ERROR_MARK)
18771 return integer_type_node;
18773 type = DECL_BIT_FIELD_TYPE (decl);
18774 if (type == NULL_TREE)
18775 type = TREE_TYPE (decl);
18777 return type;
18780 /* Given a pointer to a tree node, return the alignment in bits for
18781 it, or else return BITS_PER_WORD if the node actually turns out to
18782 be an ERROR_MARK node. */
18784 static inline unsigned
18785 simple_type_align_in_bits (const_tree type)
18787 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18790 static inline unsigned
18791 simple_decl_align_in_bits (const_tree decl)
18793 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18796 /* Return the result of rounding T up to ALIGN. */
18798 static inline offset_int
18799 round_up_to_align (const offset_int &t, unsigned int align)
18801 return wi::udiv_trunc (t + align - 1, align) * align;
18804 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18805 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18806 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18807 if we fail to return the size in one of these two forms. */
18809 static dw_loc_descr_ref
18810 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18812 tree tree_size;
18813 struct loc_descr_context ctx;
18815 /* Return a constant integer in priority, if possible. */
18816 *cst_size = int_size_in_bytes (type);
18817 if (*cst_size != -1)
18818 return NULL;
18820 ctx.context_type = const_cast<tree> (type);
18821 ctx.base_decl = NULL_TREE;
18822 ctx.dpi = NULL;
18823 ctx.placeholder_arg = false;
18824 ctx.placeholder_seen = false;
18826 type = TYPE_MAIN_VARIANT (type);
18827 tree_size = TYPE_SIZE_UNIT (type);
18828 return ((tree_size != NULL_TREE)
18829 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18830 : NULL);
18833 /* Helper structure for RECORD_TYPE processing. */
18834 struct vlr_context
18836 /* Root RECORD_TYPE. It is needed to generate data member location
18837 descriptions in variable-length records (VLR), but also to cope with
18838 variants, which are composed of nested structures multiplexed with
18839 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18840 function processing a FIELD_DECL, it is required to be non null. */
18841 tree struct_type;
18842 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18843 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18844 this variant part as part of the root record (in storage units). For
18845 regular records, it must be NULL_TREE. */
18846 tree variant_part_offset;
18849 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18850 addressed byte of the "containing object" for the given FIELD_DECL. If
18851 possible, return a native constant through CST_OFFSET (in which case NULL is
18852 returned); otherwise return a DWARF expression that computes the offset.
18854 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18855 that offset is, either because the argument turns out to be a pointer to an
18856 ERROR_MARK node, or because the offset expression is too complex for us.
18858 CTX is required: see the comment for VLR_CONTEXT. */
18860 static dw_loc_descr_ref
18861 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18862 HOST_WIDE_INT *cst_offset)
18864 tree tree_result;
18865 dw_loc_list_ref loc_result;
18867 *cst_offset = 0;
18869 if (TREE_CODE (decl) == ERROR_MARK)
18870 return NULL;
18871 else
18872 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18874 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18875 case. */
18876 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18877 return NULL;
18879 #ifdef PCC_BITFIELD_TYPE_MATTERS
18880 /* We used to handle only constant offsets in all cases. Now, we handle
18881 properly dynamic byte offsets only when PCC bitfield type doesn't
18882 matter. */
18883 if (PCC_BITFIELD_TYPE_MATTERS
18884 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18886 offset_int object_offset_in_bits;
18887 offset_int object_offset_in_bytes;
18888 offset_int bitpos_int;
18889 tree type;
18890 tree field_size_tree;
18891 offset_int deepest_bitpos;
18892 offset_int field_size_in_bits;
18893 unsigned int type_align_in_bits;
18894 unsigned int decl_align_in_bits;
18895 offset_int type_size_in_bits;
18897 bitpos_int = wi::to_offset (bit_position (decl));
18898 type = field_type (decl);
18899 type_size_in_bits = offset_int_type_size_in_bits (type);
18900 type_align_in_bits = simple_type_align_in_bits (type);
18902 field_size_tree = DECL_SIZE (decl);
18904 /* The size could be unspecified if there was an error, or for
18905 a flexible array member. */
18906 if (!field_size_tree)
18907 field_size_tree = bitsize_zero_node;
18909 /* If the size of the field is not constant, use the type size. */
18910 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18911 field_size_in_bits = wi::to_offset (field_size_tree);
18912 else
18913 field_size_in_bits = type_size_in_bits;
18915 decl_align_in_bits = simple_decl_align_in_bits (decl);
18917 /* The GCC front-end doesn't make any attempt to keep track of the
18918 starting bit offset (relative to the start of the containing
18919 structure type) of the hypothetical "containing object" for a
18920 bit-field. Thus, when computing the byte offset value for the
18921 start of the "containing object" of a bit-field, we must deduce
18922 this information on our own. This can be rather tricky to do in
18923 some cases. For example, handling the following structure type
18924 definition when compiling for an i386/i486 target (which only
18925 aligns long long's to 32-bit boundaries) can be very tricky:
18927 struct S { int field1; long long field2:31; };
18929 Fortunately, there is a simple rule-of-thumb which can be used
18930 in such cases. When compiling for an i386/i486, GCC will
18931 allocate 8 bytes for the structure shown above. It decides to
18932 do this based upon one simple rule for bit-field allocation.
18933 GCC allocates each "containing object" for each bit-field at
18934 the first (i.e. lowest addressed) legitimate alignment boundary
18935 (based upon the required minimum alignment for the declared
18936 type of the field) which it can possibly use, subject to the
18937 condition that there is still enough available space remaining
18938 in the containing object (when allocated at the selected point)
18939 to fully accommodate all of the bits of the bit-field itself.
18941 This simple rule makes it obvious why GCC allocates 8 bytes for
18942 each object of the structure type shown above. When looking
18943 for a place to allocate the "containing object" for `field2',
18944 the compiler simply tries to allocate a 64-bit "containing
18945 object" at each successive 32-bit boundary (starting at zero)
18946 until it finds a place to allocate that 64- bit field such that
18947 at least 31 contiguous (and previously unallocated) bits remain
18948 within that selected 64 bit field. (As it turns out, for the
18949 example above, the compiler finds it is OK to allocate the
18950 "containing object" 64-bit field at bit-offset zero within the
18951 structure type.)
18953 Here we attempt to work backwards from the limited set of facts
18954 we're given, and we try to deduce from those facts, where GCC
18955 must have believed that the containing object started (within
18956 the structure type). The value we deduce is then used (by the
18957 callers of this routine) to generate DW_AT_location and
18958 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18959 the case of DW_AT_location, regular fields as well). */
18961 /* Figure out the bit-distance from the start of the structure to
18962 the "deepest" bit of the bit-field. */
18963 deepest_bitpos = bitpos_int + field_size_in_bits;
18965 /* This is the tricky part. Use some fancy footwork to deduce
18966 where the lowest addressed bit of the containing object must
18967 be. */
18968 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18970 /* Round up to type_align by default. This works best for
18971 bitfields. */
18972 object_offset_in_bits
18973 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18975 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18977 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18979 /* Round up to decl_align instead. */
18980 object_offset_in_bits
18981 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18984 object_offset_in_bytes
18985 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18986 if (ctx->variant_part_offset == NULL_TREE)
18988 *cst_offset = object_offset_in_bytes.to_shwi ();
18989 return NULL;
18991 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18993 else
18994 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18995 tree_result = byte_position (decl);
18997 if (ctx->variant_part_offset != NULL_TREE)
18998 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18999 ctx->variant_part_offset, tree_result);
19001 /* If the byte offset is a constant, it's simplier to handle a native
19002 constant rather than a DWARF expression. */
19003 if (TREE_CODE (tree_result) == INTEGER_CST)
19005 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19006 return NULL;
19008 struct loc_descr_context loc_ctx = {
19009 ctx->struct_type, /* context_type */
19010 NULL_TREE, /* base_decl */
19011 NULL, /* dpi */
19012 false, /* placeholder_arg */
19013 false /* placeholder_seen */
19015 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19017 /* We want a DWARF expression: abort if we only have a location list with
19018 multiple elements. */
19019 if (!loc_result || !single_element_loc_list_p (loc_result))
19020 return NULL;
19021 else
19022 return loc_result->expr;
19025 /* The following routines define various Dwarf attributes and any data
19026 associated with them. */
19028 /* Add a location description attribute value to a DIE.
19030 This emits location attributes suitable for whole variables and
19031 whole parameters. Note that the location attributes for struct fields are
19032 generated by the routine `data_member_location_attribute' below. */
19034 static inline void
19035 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19036 dw_loc_list_ref descr)
19038 bool check_no_locviews = true;
19039 if (descr == 0)
19040 return;
19041 if (single_element_loc_list_p (descr))
19042 add_AT_loc (die, attr_kind, descr->expr);
19043 else
19045 add_AT_loc_list (die, attr_kind, descr);
19046 gcc_assert (descr->ll_symbol);
19047 if (attr_kind == DW_AT_location && descr->vl_symbol
19048 && dwarf2out_locviews_in_attribute ())
19050 add_AT_view_list (die, DW_AT_GNU_locviews);
19051 check_no_locviews = false;
19055 if (check_no_locviews)
19056 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19059 /* Add DW_AT_accessibility attribute to DIE if needed. */
19061 static void
19062 add_accessibility_attribute (dw_die_ref die, tree decl)
19064 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19065 children, otherwise the default is DW_ACCESS_public. In DWARF2
19066 the default has always been DW_ACCESS_public. */
19067 if (TREE_PROTECTED (decl))
19068 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19069 else if (TREE_PRIVATE (decl))
19071 if (dwarf_version == 2
19072 || die->die_parent == NULL
19073 || die->die_parent->die_tag != DW_TAG_class_type)
19074 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19076 else if (dwarf_version > 2
19077 && die->die_parent
19078 && die->die_parent->die_tag == DW_TAG_class_type)
19079 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19082 /* Attach the specialized form of location attribute used for data members of
19083 struct and union types. In the special case of a FIELD_DECL node which
19084 represents a bit-field, the "offset" part of this special location
19085 descriptor must indicate the distance in bytes from the lowest-addressed
19086 byte of the containing struct or union type to the lowest-addressed byte of
19087 the "containing object" for the bit-field. (See the `field_byte_offset'
19088 function above).
19090 For any given bit-field, the "containing object" is a hypothetical object
19091 (of some integral or enum type) within which the given bit-field lives. The
19092 type of this hypothetical "containing object" is always the same as the
19093 declared type of the individual bit-field itself (for GCC anyway... the
19094 DWARF spec doesn't actually mandate this). Note that it is the size (in
19095 bytes) of the hypothetical "containing object" which will be given in the
19096 DW_AT_byte_size attribute for this bit-field. (See the
19097 `byte_size_attribute' function below.) It is also used when calculating the
19098 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19099 function below.)
19101 CTX is required: see the comment for VLR_CONTEXT. */
19103 static void
19104 add_data_member_location_attribute (dw_die_ref die,
19105 tree decl,
19106 struct vlr_context *ctx)
19108 HOST_WIDE_INT offset;
19109 dw_loc_descr_ref loc_descr = 0;
19111 if (TREE_CODE (decl) == TREE_BINFO)
19113 /* We're working on the TAG_inheritance for a base class. */
19114 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19116 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19117 aren't at a fixed offset from all (sub)objects of the same
19118 type. We need to extract the appropriate offset from our
19119 vtable. The following dwarf expression means
19121 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19123 This is specific to the V3 ABI, of course. */
19125 dw_loc_descr_ref tmp;
19127 /* Make a copy of the object address. */
19128 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19129 add_loc_descr (&loc_descr, tmp);
19131 /* Extract the vtable address. */
19132 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19133 add_loc_descr (&loc_descr, tmp);
19135 /* Calculate the address of the offset. */
19136 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19137 gcc_assert (offset < 0);
19139 tmp = int_loc_descriptor (-offset);
19140 add_loc_descr (&loc_descr, tmp);
19141 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19142 add_loc_descr (&loc_descr, tmp);
19144 /* Extract the offset. */
19145 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19146 add_loc_descr (&loc_descr, tmp);
19148 /* Add it to the object address. */
19149 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19150 add_loc_descr (&loc_descr, tmp);
19152 else
19153 offset = tree_to_shwi (BINFO_OFFSET (decl));
19155 else
19157 loc_descr = field_byte_offset (decl, ctx, &offset);
19159 /* If loc_descr is available then we know the field offset is dynamic.
19160 However, GDB does not handle dynamic field offsets very well at the
19161 moment. */
19162 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19164 loc_descr = NULL;
19165 offset = 0;
19168 /* Data member location evalutation starts with the base address on the
19169 stack. Compute the field offset and add it to this base address. */
19170 else if (loc_descr != NULL)
19171 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19174 if (! loc_descr)
19176 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19177 e.g. GDB only added support to it in November 2016. For DWARF5
19178 we need newer debug info consumers anyway. We might change this
19179 to dwarf_version >= 4 once most consumers catched up. */
19180 if (dwarf_version >= 5
19181 && TREE_CODE (decl) == FIELD_DECL
19182 && DECL_BIT_FIELD_TYPE (decl))
19184 tree off = bit_position (decl);
19185 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19187 remove_AT (die, DW_AT_byte_size);
19188 remove_AT (die, DW_AT_bit_offset);
19189 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19190 return;
19193 if (dwarf_version > 2)
19195 /* Don't need to output a location expression, just the constant. */
19196 if (offset < 0)
19197 add_AT_int (die, DW_AT_data_member_location, offset);
19198 else
19199 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19200 return;
19202 else
19204 enum dwarf_location_atom op;
19206 /* The DWARF2 standard says that we should assume that the structure
19207 address is already on the stack, so we can specify a structure
19208 field address by using DW_OP_plus_uconst. */
19209 op = DW_OP_plus_uconst;
19210 loc_descr = new_loc_descr (op, offset, 0);
19214 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19217 /* Writes integer values to dw_vec_const array. */
19219 static void
19220 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19222 while (size != 0)
19224 *dest++ = val & 0xff;
19225 val >>= 8;
19226 --size;
19230 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19232 static HOST_WIDE_INT
19233 extract_int (const unsigned char *src, unsigned int size)
19235 HOST_WIDE_INT val = 0;
19237 src += size;
19238 while (size != 0)
19240 val <<= 8;
19241 val |= *--src & 0xff;
19242 --size;
19244 return val;
19247 /* Writes wide_int values to dw_vec_const array. */
19249 static void
19250 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19252 int i;
19254 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19256 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19257 return;
19260 /* We'd have to extend this code to support odd sizes. */
19261 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19263 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19265 if (WORDS_BIG_ENDIAN)
19266 for (i = n - 1; i >= 0; i--)
19268 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19269 dest += sizeof (HOST_WIDE_INT);
19271 else
19272 for (i = 0; i < n; i++)
19274 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19275 dest += sizeof (HOST_WIDE_INT);
19279 /* Writes floating point values to dw_vec_const array. */
19281 static void
19282 insert_float (const_rtx rtl, unsigned char *array)
19284 long val[4];
19285 int i;
19286 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19288 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19290 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19291 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19293 insert_int (val[i], 4, array);
19294 array += 4;
19298 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19299 does not have a "location" either in memory or in a register. These
19300 things can arise in GNU C when a constant is passed as an actual parameter
19301 to an inlined function. They can also arise in C++ where declared
19302 constants do not necessarily get memory "homes". */
19304 static bool
19305 add_const_value_attribute (dw_die_ref die, rtx rtl)
19307 switch (GET_CODE (rtl))
19309 case CONST_INT:
19311 HOST_WIDE_INT val = INTVAL (rtl);
19313 if (val < 0)
19314 add_AT_int (die, DW_AT_const_value, val);
19315 else
19316 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19318 return true;
19320 case CONST_WIDE_INT:
19322 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19323 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19324 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19325 wide_int w = wi::zext (w1, prec);
19326 add_AT_wide (die, DW_AT_const_value, w);
19328 return true;
19330 case CONST_DOUBLE:
19331 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19332 floating-point constant. A CONST_DOUBLE is used whenever the
19333 constant requires more than one word in order to be adequately
19334 represented. */
19335 if (TARGET_SUPPORTS_WIDE_INT == 0
19336 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19337 add_AT_double (die, DW_AT_const_value,
19338 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19339 else
19341 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19342 unsigned int length = GET_MODE_SIZE (mode);
19343 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19345 insert_float (rtl, array);
19346 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19348 return true;
19350 case CONST_VECTOR:
19352 unsigned int length;
19353 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19354 return false;
19356 machine_mode mode = GET_MODE (rtl);
19357 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19358 unsigned char *array
19359 = ggc_vec_alloc<unsigned char> (length * elt_size);
19360 unsigned int i;
19361 unsigned char *p;
19362 machine_mode imode = GET_MODE_INNER (mode);
19364 switch (GET_MODE_CLASS (mode))
19366 case MODE_VECTOR_INT:
19367 for (i = 0, p = array; i < length; i++, p += elt_size)
19369 rtx elt = CONST_VECTOR_ELT (rtl, i);
19370 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19372 break;
19374 case MODE_VECTOR_FLOAT:
19375 for (i = 0, p = array; i < length; i++, p += elt_size)
19377 rtx elt = CONST_VECTOR_ELT (rtl, i);
19378 insert_float (elt, p);
19380 break;
19382 default:
19383 gcc_unreachable ();
19386 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19388 return true;
19390 case CONST_STRING:
19391 if (dwarf_version >= 4 || !dwarf_strict)
19393 dw_loc_descr_ref loc_result;
19394 resolve_one_addr (&rtl);
19395 rtl_addr:
19396 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19397 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19398 add_AT_loc (die, DW_AT_location, loc_result);
19399 vec_safe_push (used_rtx_array, rtl);
19400 return true;
19402 return false;
19404 case CONST:
19405 if (CONSTANT_P (XEXP (rtl, 0)))
19406 return add_const_value_attribute (die, XEXP (rtl, 0));
19407 /* FALLTHROUGH */
19408 case SYMBOL_REF:
19409 if (!const_ok_for_output (rtl))
19410 return false;
19411 /* FALLTHROUGH */
19412 case LABEL_REF:
19413 if (dwarf_version >= 4 || !dwarf_strict)
19414 goto rtl_addr;
19415 return false;
19417 case PLUS:
19418 /* In cases where an inlined instance of an inline function is passed
19419 the address of an `auto' variable (which is local to the caller) we
19420 can get a situation where the DECL_RTL of the artificial local
19421 variable (for the inlining) which acts as a stand-in for the
19422 corresponding formal parameter (of the inline function) will look
19423 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19424 exactly a compile-time constant expression, but it isn't the address
19425 of the (artificial) local variable either. Rather, it represents the
19426 *value* which the artificial local variable always has during its
19427 lifetime. We currently have no way to represent such quasi-constant
19428 values in Dwarf, so for now we just punt and generate nothing. */
19429 return false;
19431 case HIGH:
19432 case CONST_FIXED:
19433 return false;
19435 case MEM:
19436 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19437 && MEM_READONLY_P (rtl)
19438 && GET_MODE (rtl) == BLKmode)
19440 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19441 return true;
19443 return false;
19445 default:
19446 /* No other kinds of rtx should be possible here. */
19447 gcc_unreachable ();
19449 return false;
19452 /* Determine whether the evaluation of EXPR references any variables
19453 or functions which aren't otherwise used (and therefore may not be
19454 output). */
19455 static tree
19456 reference_to_unused (tree * tp, int * walk_subtrees,
19457 void * data ATTRIBUTE_UNUSED)
19459 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19460 *walk_subtrees = 0;
19462 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19463 && ! TREE_ASM_WRITTEN (*tp))
19464 return *tp;
19465 /* ??? The C++ FE emits debug information for using decls, so
19466 putting gcc_unreachable here falls over. See PR31899. For now
19467 be conservative. */
19468 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19469 return *tp;
19470 else if (VAR_P (*tp))
19472 varpool_node *node = varpool_node::get (*tp);
19473 if (!node || !node->definition)
19474 return *tp;
19476 else if (TREE_CODE (*tp) == FUNCTION_DECL
19477 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19479 /* The call graph machinery must have finished analyzing,
19480 optimizing and gimplifying the CU by now.
19481 So if *TP has no call graph node associated
19482 to it, it means *TP will not be emitted. */
19483 if (!cgraph_node::get (*tp))
19484 return *tp;
19486 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19487 return *tp;
19489 return NULL_TREE;
19492 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19493 for use in a later add_const_value_attribute call. */
19495 static rtx
19496 rtl_for_decl_init (tree init, tree type)
19498 rtx rtl = NULL_RTX;
19500 STRIP_NOPS (init);
19502 /* If a variable is initialized with a string constant without embedded
19503 zeros, build CONST_STRING. */
19504 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19506 tree enttype = TREE_TYPE (type);
19507 tree domain = TYPE_DOMAIN (type);
19508 scalar_int_mode mode;
19510 if (is_int_mode (TYPE_MODE (enttype), &mode)
19511 && GET_MODE_SIZE (mode) == 1
19512 && domain
19513 && integer_zerop (TYPE_MIN_VALUE (domain))
19514 && compare_tree_int (TYPE_MAX_VALUE (domain),
19515 TREE_STRING_LENGTH (init) - 1) == 0
19516 && ((size_t) TREE_STRING_LENGTH (init)
19517 == strlen (TREE_STRING_POINTER (init)) + 1))
19519 rtl = gen_rtx_CONST_STRING (VOIDmode,
19520 ggc_strdup (TREE_STRING_POINTER (init)));
19521 rtl = gen_rtx_MEM (BLKmode, rtl);
19522 MEM_READONLY_P (rtl) = 1;
19525 /* Other aggregates, and complex values, could be represented using
19526 CONCAT: FIXME! */
19527 else if (AGGREGATE_TYPE_P (type)
19528 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19529 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19530 || TREE_CODE (type) == COMPLEX_TYPE)
19532 /* Vectors only work if their mode is supported by the target.
19533 FIXME: generic vectors ought to work too. */
19534 else if (TREE_CODE (type) == VECTOR_TYPE
19535 && !VECTOR_MODE_P (TYPE_MODE (type)))
19537 /* If the initializer is something that we know will expand into an
19538 immediate RTL constant, expand it now. We must be careful not to
19539 reference variables which won't be output. */
19540 else if (initializer_constant_valid_p (init, type)
19541 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19543 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19544 possible. */
19545 if (TREE_CODE (type) == VECTOR_TYPE)
19546 switch (TREE_CODE (init))
19548 case VECTOR_CST:
19549 break;
19550 case CONSTRUCTOR:
19551 if (TREE_CONSTANT (init))
19553 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19554 bool constant_p = true;
19555 tree value;
19556 unsigned HOST_WIDE_INT ix;
19558 /* Even when ctor is constant, it might contain non-*_CST
19559 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19560 belong into VECTOR_CST nodes. */
19561 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19562 if (!CONSTANT_CLASS_P (value))
19564 constant_p = false;
19565 break;
19568 if (constant_p)
19570 init = build_vector_from_ctor (type, elts);
19571 break;
19574 /* FALLTHRU */
19576 default:
19577 return NULL;
19580 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19582 /* If expand_expr returns a MEM, it wasn't immediate. */
19583 gcc_assert (!rtl || !MEM_P (rtl));
19586 return rtl;
19589 /* Generate RTL for the variable DECL to represent its location. */
19591 static rtx
19592 rtl_for_decl_location (tree decl)
19594 rtx rtl;
19596 /* Here we have to decide where we are going to say the parameter "lives"
19597 (as far as the debugger is concerned). We only have a couple of
19598 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19600 DECL_RTL normally indicates where the parameter lives during most of the
19601 activation of the function. If optimization is enabled however, this
19602 could be either NULL or else a pseudo-reg. Both of those cases indicate
19603 that the parameter doesn't really live anywhere (as far as the code
19604 generation parts of GCC are concerned) during most of the function's
19605 activation. That will happen (for example) if the parameter is never
19606 referenced within the function.
19608 We could just generate a location descriptor here for all non-NULL
19609 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19610 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19611 where DECL_RTL is NULL or is a pseudo-reg.
19613 Note however that we can only get away with using DECL_INCOMING_RTL as
19614 a backup substitute for DECL_RTL in certain limited cases. In cases
19615 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19616 we can be sure that the parameter was passed using the same type as it is
19617 declared to have within the function, and that its DECL_INCOMING_RTL
19618 points us to a place where a value of that type is passed.
19620 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19621 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19622 because in these cases DECL_INCOMING_RTL points us to a value of some
19623 type which is *different* from the type of the parameter itself. Thus,
19624 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19625 such cases, the debugger would end up (for example) trying to fetch a
19626 `float' from a place which actually contains the first part of a
19627 `double'. That would lead to really incorrect and confusing
19628 output at debug-time.
19630 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19631 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19632 are a couple of exceptions however. On little-endian machines we can
19633 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19634 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19635 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19636 when (on a little-endian machine) a non-prototyped function has a
19637 parameter declared to be of type `short' or `char'. In such cases,
19638 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19639 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19640 passed `int' value. If the debugger then uses that address to fetch
19641 a `short' or a `char' (on a little-endian machine) the result will be
19642 the correct data, so we allow for such exceptional cases below.
19644 Note that our goal here is to describe the place where the given formal
19645 parameter lives during most of the function's activation (i.e. between the
19646 end of the prologue and the start of the epilogue). We'll do that as best
19647 as we can. Note however that if the given formal parameter is modified
19648 sometime during the execution of the function, then a stack backtrace (at
19649 debug-time) will show the function as having been called with the *new*
19650 value rather than the value which was originally passed in. This happens
19651 rarely enough that it is not a major problem, but it *is* a problem, and
19652 I'd like to fix it.
19654 A future version of dwarf2out.c may generate two additional attributes for
19655 any given DW_TAG_formal_parameter DIE which will describe the "passed
19656 type" and the "passed location" for the given formal parameter in addition
19657 to the attributes we now generate to indicate the "declared type" and the
19658 "active location" for each parameter. This additional set of attributes
19659 could be used by debuggers for stack backtraces. Separately, note that
19660 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19661 This happens (for example) for inlined-instances of inline function formal
19662 parameters which are never referenced. This really shouldn't be
19663 happening. All PARM_DECL nodes should get valid non-NULL
19664 DECL_INCOMING_RTL values. FIXME. */
19666 /* Use DECL_RTL as the "location" unless we find something better. */
19667 rtl = DECL_RTL_IF_SET (decl);
19669 /* When generating abstract instances, ignore everything except
19670 constants, symbols living in memory, and symbols living in
19671 fixed registers. */
19672 if (! reload_completed)
19674 if (rtl
19675 && (CONSTANT_P (rtl)
19676 || (MEM_P (rtl)
19677 && CONSTANT_P (XEXP (rtl, 0)))
19678 || (REG_P (rtl)
19679 && VAR_P (decl)
19680 && TREE_STATIC (decl))))
19682 rtl = targetm.delegitimize_address (rtl);
19683 return rtl;
19685 rtl = NULL_RTX;
19687 else if (TREE_CODE (decl) == PARM_DECL)
19689 if (rtl == NULL_RTX
19690 || is_pseudo_reg (rtl)
19691 || (MEM_P (rtl)
19692 && is_pseudo_reg (XEXP (rtl, 0))
19693 && DECL_INCOMING_RTL (decl)
19694 && MEM_P (DECL_INCOMING_RTL (decl))
19695 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19697 tree declared_type = TREE_TYPE (decl);
19698 tree passed_type = DECL_ARG_TYPE (decl);
19699 machine_mode dmode = TYPE_MODE (declared_type);
19700 machine_mode pmode = TYPE_MODE (passed_type);
19702 /* This decl represents a formal parameter which was optimized out.
19703 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19704 all cases where (rtl == NULL_RTX) just below. */
19705 if (dmode == pmode)
19706 rtl = DECL_INCOMING_RTL (decl);
19707 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19708 && SCALAR_INT_MODE_P (dmode)
19709 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19710 && DECL_INCOMING_RTL (decl))
19712 rtx inc = DECL_INCOMING_RTL (decl);
19713 if (REG_P (inc))
19714 rtl = inc;
19715 else if (MEM_P (inc))
19717 if (BYTES_BIG_ENDIAN)
19718 rtl = adjust_address_nv (inc, dmode,
19719 GET_MODE_SIZE (pmode)
19720 - GET_MODE_SIZE (dmode));
19721 else
19722 rtl = inc;
19727 /* If the parm was passed in registers, but lives on the stack, then
19728 make a big endian correction if the mode of the type of the
19729 parameter is not the same as the mode of the rtl. */
19730 /* ??? This is the same series of checks that are made in dbxout.c before
19731 we reach the big endian correction code there. It isn't clear if all
19732 of these checks are necessary here, but keeping them all is the safe
19733 thing to do. */
19734 else if (MEM_P (rtl)
19735 && XEXP (rtl, 0) != const0_rtx
19736 && ! CONSTANT_P (XEXP (rtl, 0))
19737 /* Not passed in memory. */
19738 && !MEM_P (DECL_INCOMING_RTL (decl))
19739 /* Not passed by invisible reference. */
19740 && (!REG_P (XEXP (rtl, 0))
19741 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19742 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19743 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19744 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19745 #endif
19747 /* Big endian correction check. */
19748 && BYTES_BIG_ENDIAN
19749 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19750 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19751 UNITS_PER_WORD))
19753 machine_mode addr_mode = get_address_mode (rtl);
19754 poly_int64 offset = (UNITS_PER_WORD
19755 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19757 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19758 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19761 else if (VAR_P (decl)
19762 && rtl
19763 && MEM_P (rtl)
19764 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19766 machine_mode addr_mode = get_address_mode (rtl);
19767 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19768 GET_MODE (rtl));
19770 /* If a variable is declared "register" yet is smaller than
19771 a register, then if we store the variable to memory, it
19772 looks like we're storing a register-sized value, when in
19773 fact we are not. We need to adjust the offset of the
19774 storage location to reflect the actual value's bytes,
19775 else gdb will not be able to display it. */
19776 if (maybe_ne (offset, 0))
19777 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19778 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19781 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19782 and will have been substituted directly into all expressions that use it.
19783 C does not have such a concept, but C++ and other languages do. */
19784 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19785 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19787 if (rtl)
19788 rtl = targetm.delegitimize_address (rtl);
19790 /* If we don't look past the constant pool, we risk emitting a
19791 reference to a constant pool entry that isn't referenced from
19792 code, and thus is not emitted. */
19793 if (rtl)
19794 rtl = avoid_constant_pool_reference (rtl);
19796 /* Try harder to get a rtl. If this symbol ends up not being emitted
19797 in the current CU, resolve_addr will remove the expression referencing
19798 it. */
19799 if (rtl == NULL_RTX
19800 && VAR_P (decl)
19801 && !DECL_EXTERNAL (decl)
19802 && TREE_STATIC (decl)
19803 && DECL_NAME (decl)
19804 && !DECL_HARD_REGISTER (decl)
19805 && DECL_MODE (decl) != VOIDmode)
19807 rtl = make_decl_rtl_for_debug (decl);
19808 if (!MEM_P (rtl)
19809 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19810 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19811 rtl = NULL_RTX;
19814 return rtl;
19817 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19818 returned. If so, the decl for the COMMON block is returned, and the
19819 value is the offset into the common block for the symbol. */
19821 static tree
19822 fortran_common (tree decl, HOST_WIDE_INT *value)
19824 tree val_expr, cvar;
19825 machine_mode mode;
19826 poly_int64 bitsize, bitpos;
19827 tree offset;
19828 HOST_WIDE_INT cbitpos;
19829 int unsignedp, reversep, volatilep = 0;
19831 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19832 it does not have a value (the offset into the common area), or if it
19833 is thread local (as opposed to global) then it isn't common, and shouldn't
19834 be handled as such. */
19835 if (!VAR_P (decl)
19836 || !TREE_STATIC (decl)
19837 || !DECL_HAS_VALUE_EXPR_P (decl)
19838 || !is_fortran ())
19839 return NULL_TREE;
19841 val_expr = DECL_VALUE_EXPR (decl);
19842 if (TREE_CODE (val_expr) != COMPONENT_REF)
19843 return NULL_TREE;
19845 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19846 &unsignedp, &reversep, &volatilep);
19848 if (cvar == NULL_TREE
19849 || !VAR_P (cvar)
19850 || DECL_ARTIFICIAL (cvar)
19851 || !TREE_PUBLIC (cvar)
19852 /* We don't expect to have to cope with variable offsets,
19853 since at present all static data must have a constant size. */
19854 || !bitpos.is_constant (&cbitpos))
19855 return NULL_TREE;
19857 *value = 0;
19858 if (offset != NULL)
19860 if (!tree_fits_shwi_p (offset))
19861 return NULL_TREE;
19862 *value = tree_to_shwi (offset);
19864 if (cbitpos != 0)
19865 *value += cbitpos / BITS_PER_UNIT;
19867 return cvar;
19870 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19871 data attribute for a variable or a parameter. We generate the
19872 DW_AT_const_value attribute only in those cases where the given variable
19873 or parameter does not have a true "location" either in memory or in a
19874 register. This can happen (for example) when a constant is passed as an
19875 actual argument in a call to an inline function. (It's possible that
19876 these things can crop up in other ways also.) Note that one type of
19877 constant value which can be passed into an inlined function is a constant
19878 pointer. This can happen for example if an actual argument in an inlined
19879 function call evaluates to a compile-time constant address.
19881 CACHE_P is true if it is worth caching the location list for DECL,
19882 so that future calls can reuse it rather than regenerate it from scratch.
19883 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19884 since we will need to refer to them each time the function is inlined. */
19886 static bool
19887 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19889 rtx rtl;
19890 dw_loc_list_ref list;
19891 var_loc_list *loc_list;
19892 cached_dw_loc_list *cache;
19894 if (early_dwarf)
19895 return false;
19897 if (TREE_CODE (decl) == ERROR_MARK)
19898 return false;
19900 if (get_AT (die, DW_AT_location)
19901 || get_AT (die, DW_AT_const_value))
19902 return true;
19904 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19905 || TREE_CODE (decl) == RESULT_DECL);
19907 /* Try to get some constant RTL for this decl, and use that as the value of
19908 the location. */
19910 rtl = rtl_for_decl_location (decl);
19911 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19912 && add_const_value_attribute (die, rtl))
19913 return true;
19915 /* See if we have single element location list that is equivalent to
19916 a constant value. That way we are better to use add_const_value_attribute
19917 rather than expanding constant value equivalent. */
19918 loc_list = lookup_decl_loc (decl);
19919 if (loc_list
19920 && loc_list->first
19921 && loc_list->first->next == NULL
19922 && NOTE_P (loc_list->first->loc)
19923 && NOTE_VAR_LOCATION (loc_list->first->loc)
19924 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19926 struct var_loc_node *node;
19928 node = loc_list->first;
19929 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19930 if (GET_CODE (rtl) == EXPR_LIST)
19931 rtl = XEXP (rtl, 0);
19932 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19933 && add_const_value_attribute (die, rtl))
19934 return true;
19936 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19937 list several times. See if we've already cached the contents. */
19938 list = NULL;
19939 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19940 cache_p = false;
19941 if (cache_p)
19943 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19944 if (cache)
19945 list = cache->loc_list;
19947 if (list == NULL)
19949 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19950 NULL);
19951 /* It is usually worth caching this result if the decl is from
19952 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19953 if (cache_p && list && list->dw_loc_next)
19955 cached_dw_loc_list **slot
19956 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19957 DECL_UID (decl),
19958 INSERT);
19959 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19960 cache->decl_id = DECL_UID (decl);
19961 cache->loc_list = list;
19962 *slot = cache;
19965 if (list)
19967 add_AT_location_description (die, DW_AT_location, list);
19968 return true;
19970 /* None of that worked, so it must not really have a location;
19971 try adding a constant value attribute from the DECL_INITIAL. */
19972 return tree_add_const_value_attribute_for_decl (die, decl);
19975 /* Helper function for tree_add_const_value_attribute. Natively encode
19976 initializer INIT into an array. Return true if successful. */
19978 static bool
19979 native_encode_initializer (tree init, unsigned char *array, int size)
19981 tree type;
19983 if (init == NULL_TREE)
19984 return false;
19986 STRIP_NOPS (init);
19987 switch (TREE_CODE (init))
19989 case STRING_CST:
19990 type = TREE_TYPE (init);
19991 if (TREE_CODE (type) == ARRAY_TYPE)
19993 tree enttype = TREE_TYPE (type);
19994 scalar_int_mode mode;
19996 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19997 || GET_MODE_SIZE (mode) != 1)
19998 return false;
19999 if (int_size_in_bytes (type) != size)
20000 return false;
20001 if (size > TREE_STRING_LENGTH (init))
20003 memcpy (array, TREE_STRING_POINTER (init),
20004 TREE_STRING_LENGTH (init));
20005 memset (array + TREE_STRING_LENGTH (init),
20006 '\0', size - TREE_STRING_LENGTH (init));
20008 else
20009 memcpy (array, TREE_STRING_POINTER (init), size);
20010 return true;
20012 return false;
20013 case CONSTRUCTOR:
20014 type = TREE_TYPE (init);
20015 if (int_size_in_bytes (type) != size)
20016 return false;
20017 if (TREE_CODE (type) == ARRAY_TYPE)
20019 HOST_WIDE_INT min_index;
20020 unsigned HOST_WIDE_INT cnt;
20021 int curpos = 0, fieldsize;
20022 constructor_elt *ce;
20024 if (TYPE_DOMAIN (type) == NULL_TREE
20025 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20026 return false;
20028 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20029 if (fieldsize <= 0)
20030 return false;
20032 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20033 memset (array, '\0', size);
20034 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20036 tree val = ce->value;
20037 tree index = ce->index;
20038 int pos = curpos;
20039 if (index && TREE_CODE (index) == RANGE_EXPR)
20040 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20041 * fieldsize;
20042 else if (index)
20043 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20045 if (val)
20047 STRIP_NOPS (val);
20048 if (!native_encode_initializer (val, array + pos, fieldsize))
20049 return false;
20051 curpos = pos + fieldsize;
20052 if (index && TREE_CODE (index) == RANGE_EXPR)
20054 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20055 - tree_to_shwi (TREE_OPERAND (index, 0));
20056 while (count-- > 0)
20058 if (val)
20059 memcpy (array + curpos, array + pos, fieldsize);
20060 curpos += fieldsize;
20063 gcc_assert (curpos <= size);
20065 return true;
20067 else if (TREE_CODE (type) == RECORD_TYPE
20068 || TREE_CODE (type) == UNION_TYPE)
20070 tree field = NULL_TREE;
20071 unsigned HOST_WIDE_INT cnt;
20072 constructor_elt *ce;
20074 if (int_size_in_bytes (type) != size)
20075 return false;
20077 if (TREE_CODE (type) == RECORD_TYPE)
20078 field = TYPE_FIELDS (type);
20080 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20082 tree val = ce->value;
20083 int pos, fieldsize;
20085 if (ce->index != 0)
20086 field = ce->index;
20088 if (val)
20089 STRIP_NOPS (val);
20091 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20092 return false;
20094 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20095 && TYPE_DOMAIN (TREE_TYPE (field))
20096 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20097 return false;
20098 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20099 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20100 return false;
20101 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20102 pos = int_byte_position (field);
20103 gcc_assert (pos + fieldsize <= size);
20104 if (val && fieldsize != 0
20105 && !native_encode_initializer (val, array + pos, fieldsize))
20106 return false;
20108 return true;
20110 return false;
20111 case VIEW_CONVERT_EXPR:
20112 case NON_LVALUE_EXPR:
20113 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20114 default:
20115 return native_encode_expr (init, array, size) == size;
20119 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20120 attribute is the const value T. */
20122 static bool
20123 tree_add_const_value_attribute (dw_die_ref die, tree t)
20125 tree init;
20126 tree type = TREE_TYPE (t);
20127 rtx rtl;
20129 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20130 return false;
20132 init = t;
20133 gcc_assert (!DECL_P (init));
20135 if (TREE_CODE (init) == INTEGER_CST)
20137 if (tree_fits_uhwi_p (init))
20139 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20140 return true;
20142 if (tree_fits_shwi_p (init))
20144 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20145 return true;
20148 if (! early_dwarf)
20150 rtl = rtl_for_decl_init (init, type);
20151 if (rtl)
20152 return add_const_value_attribute (die, rtl);
20154 /* If the host and target are sane, try harder. */
20155 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20156 && initializer_constant_valid_p (init, type))
20158 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20159 if (size > 0 && (int) size == size)
20161 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20163 if (native_encode_initializer (init, array, size))
20165 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20166 return true;
20168 ggc_free (array);
20171 return false;
20174 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20175 attribute is the const value of T, where T is an integral constant
20176 variable with static storage duration
20177 (so it can't be a PARM_DECL or a RESULT_DECL). */
20179 static bool
20180 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20183 if (!decl
20184 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20185 || (VAR_P (decl) && !TREE_STATIC (decl)))
20186 return false;
20188 if (TREE_READONLY (decl)
20189 && ! TREE_THIS_VOLATILE (decl)
20190 && DECL_INITIAL (decl))
20191 /* OK */;
20192 else
20193 return false;
20195 /* Don't add DW_AT_const_value if abstract origin already has one. */
20196 if (get_AT (var_die, DW_AT_const_value))
20197 return false;
20199 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20202 /* Convert the CFI instructions for the current function into a
20203 location list. This is used for DW_AT_frame_base when we targeting
20204 a dwarf2 consumer that does not support the dwarf3
20205 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20206 expressions. */
20208 static dw_loc_list_ref
20209 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20211 int ix;
20212 dw_fde_ref fde;
20213 dw_loc_list_ref list, *list_tail;
20214 dw_cfi_ref cfi;
20215 dw_cfa_location last_cfa, next_cfa;
20216 const char *start_label, *last_label, *section;
20217 dw_cfa_location remember;
20219 fde = cfun->fde;
20220 gcc_assert (fde != NULL);
20222 section = secname_for_decl (current_function_decl);
20223 list_tail = &list;
20224 list = NULL;
20226 memset (&next_cfa, 0, sizeof (next_cfa));
20227 next_cfa.reg = INVALID_REGNUM;
20228 remember = next_cfa;
20230 start_label = fde->dw_fde_begin;
20232 /* ??? Bald assumption that the CIE opcode list does not contain
20233 advance opcodes. */
20234 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20235 lookup_cfa_1 (cfi, &next_cfa, &remember);
20237 last_cfa = next_cfa;
20238 last_label = start_label;
20240 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20242 /* If the first partition contained no CFI adjustments, the
20243 CIE opcodes apply to the whole first partition. */
20244 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20245 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20246 list_tail =&(*list_tail)->dw_loc_next;
20247 start_label = last_label = fde->dw_fde_second_begin;
20250 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20252 switch (cfi->dw_cfi_opc)
20254 case DW_CFA_set_loc:
20255 case DW_CFA_advance_loc1:
20256 case DW_CFA_advance_loc2:
20257 case DW_CFA_advance_loc4:
20258 if (!cfa_equal_p (&last_cfa, &next_cfa))
20260 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20261 start_label, 0, last_label, 0, section);
20263 list_tail = &(*list_tail)->dw_loc_next;
20264 last_cfa = next_cfa;
20265 start_label = last_label;
20267 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20268 break;
20270 case DW_CFA_advance_loc:
20271 /* The encoding is complex enough that we should never emit this. */
20272 gcc_unreachable ();
20274 default:
20275 lookup_cfa_1 (cfi, &next_cfa, &remember);
20276 break;
20278 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20280 if (!cfa_equal_p (&last_cfa, &next_cfa))
20282 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20283 start_label, 0, last_label, 0, section);
20285 list_tail = &(*list_tail)->dw_loc_next;
20286 last_cfa = next_cfa;
20287 start_label = last_label;
20289 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20290 start_label, 0, fde->dw_fde_end, 0, section);
20291 list_tail = &(*list_tail)->dw_loc_next;
20292 start_label = last_label = fde->dw_fde_second_begin;
20296 if (!cfa_equal_p (&last_cfa, &next_cfa))
20298 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20299 start_label, 0, last_label, 0, section);
20300 list_tail = &(*list_tail)->dw_loc_next;
20301 start_label = last_label;
20304 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20305 start_label, 0,
20306 fde->dw_fde_second_begin
20307 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20308 section);
20310 maybe_gen_llsym (list);
20312 return list;
20315 /* Compute a displacement from the "steady-state frame pointer" to the
20316 frame base (often the same as the CFA), and store it in
20317 frame_pointer_fb_offset. OFFSET is added to the displacement
20318 before the latter is negated. */
20320 static void
20321 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20323 rtx reg, elim;
20325 #ifdef FRAME_POINTER_CFA_OFFSET
20326 reg = frame_pointer_rtx;
20327 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20328 #else
20329 reg = arg_pointer_rtx;
20330 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20331 #endif
20333 elim = (ira_use_lra_p
20334 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20335 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20336 elim = strip_offset_and_add (elim, &offset);
20338 frame_pointer_fb_offset = -offset;
20340 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20341 in which to eliminate. This is because it's stack pointer isn't
20342 directly accessible as a register within the ISA. To work around
20343 this, assume that while we cannot provide a proper value for
20344 frame_pointer_fb_offset, we won't need one either. */
20345 frame_pointer_fb_offset_valid
20346 = ((SUPPORTS_STACK_ALIGNMENT
20347 && (elim == hard_frame_pointer_rtx
20348 || elim == stack_pointer_rtx))
20349 || elim == (frame_pointer_needed
20350 ? hard_frame_pointer_rtx
20351 : stack_pointer_rtx));
20354 /* Generate a DW_AT_name attribute given some string value to be included as
20355 the value of the attribute. */
20357 static void
20358 add_name_attribute (dw_die_ref die, const char *name_string)
20360 if (name_string != NULL && *name_string != 0)
20362 if (demangle_name_func)
20363 name_string = (*demangle_name_func) (name_string);
20365 add_AT_string (die, DW_AT_name, name_string);
20369 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20370 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20371 of TYPE accordingly.
20373 ??? This is a temporary measure until after we're able to generate
20374 regular DWARF for the complex Ada type system. */
20376 static void
20377 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20378 dw_die_ref context_die)
20380 tree dtype;
20381 dw_die_ref dtype_die;
20383 if (!lang_hooks.types.descriptive_type)
20384 return;
20386 dtype = lang_hooks.types.descriptive_type (type);
20387 if (!dtype)
20388 return;
20390 dtype_die = lookup_type_die (dtype);
20391 if (!dtype_die)
20393 gen_type_die (dtype, context_die);
20394 dtype_die = lookup_type_die (dtype);
20395 gcc_assert (dtype_die);
20398 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20401 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20403 static const char *
20404 comp_dir_string (void)
20406 const char *wd;
20407 char *wd1;
20408 static const char *cached_wd = NULL;
20410 if (cached_wd != NULL)
20411 return cached_wd;
20413 wd = get_src_pwd ();
20414 if (wd == NULL)
20415 return NULL;
20417 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20419 int wdlen;
20421 wdlen = strlen (wd);
20422 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20423 strcpy (wd1, wd);
20424 wd1 [wdlen] = DIR_SEPARATOR;
20425 wd1 [wdlen + 1] = 0;
20426 wd = wd1;
20429 cached_wd = remap_debug_filename (wd);
20430 return cached_wd;
20433 /* Generate a DW_AT_comp_dir attribute for DIE. */
20435 static void
20436 add_comp_dir_attribute (dw_die_ref die)
20438 const char * wd = comp_dir_string ();
20439 if (wd != NULL)
20440 add_AT_string (die, DW_AT_comp_dir, wd);
20443 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20444 pointer computation, ...), output a representation for that bound according
20445 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20446 loc_list_from_tree for the meaning of CONTEXT. */
20448 static void
20449 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20450 int forms, struct loc_descr_context *context)
20452 dw_die_ref context_die, decl_die;
20453 dw_loc_list_ref list;
20454 bool strip_conversions = true;
20455 bool placeholder_seen = false;
20457 while (strip_conversions)
20458 switch (TREE_CODE (value))
20460 case ERROR_MARK:
20461 case SAVE_EXPR:
20462 return;
20464 CASE_CONVERT:
20465 case VIEW_CONVERT_EXPR:
20466 value = TREE_OPERAND (value, 0);
20467 break;
20469 default:
20470 strip_conversions = false;
20471 break;
20474 /* If possible and permitted, output the attribute as a constant. */
20475 if ((forms & dw_scalar_form_constant) != 0
20476 && TREE_CODE (value) == INTEGER_CST)
20478 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20480 /* If HOST_WIDE_INT is big enough then represent the bound as
20481 a constant value. We need to choose a form based on
20482 whether the type is signed or unsigned. We cannot just
20483 call add_AT_unsigned if the value itself is positive
20484 (add_AT_unsigned might add the unsigned value encoded as
20485 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20486 bounds type and then sign extend any unsigned values found
20487 for signed types. This is needed only for
20488 DW_AT_{lower,upper}_bound, since for most other attributes,
20489 consumers will treat DW_FORM_data[1248] as unsigned values,
20490 regardless of the underlying type. */
20491 if (prec <= HOST_BITS_PER_WIDE_INT
20492 || tree_fits_uhwi_p (value))
20494 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20495 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20496 else
20497 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20499 else
20500 /* Otherwise represent the bound as an unsigned value with
20501 the precision of its type. The precision and signedness
20502 of the type will be necessary to re-interpret it
20503 unambiguously. */
20504 add_AT_wide (die, attr, wi::to_wide (value));
20505 return;
20508 /* Otherwise, if it's possible and permitted too, output a reference to
20509 another DIE. */
20510 if ((forms & dw_scalar_form_reference) != 0)
20512 tree decl = NULL_TREE;
20514 /* Some type attributes reference an outer type. For instance, the upper
20515 bound of an array may reference an embedding record (this happens in
20516 Ada). */
20517 if (TREE_CODE (value) == COMPONENT_REF
20518 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20519 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20520 decl = TREE_OPERAND (value, 1);
20522 else if (VAR_P (value)
20523 || TREE_CODE (value) == PARM_DECL
20524 || TREE_CODE (value) == RESULT_DECL)
20525 decl = value;
20527 if (decl != NULL_TREE)
20529 dw_die_ref decl_die = lookup_decl_die (decl);
20531 /* ??? Can this happen, or should the variable have been bound
20532 first? Probably it can, since I imagine that we try to create
20533 the types of parameters in the order in which they exist in
20534 the list, and won't have created a forward reference to a
20535 later parameter. */
20536 if (decl_die != NULL)
20538 add_AT_die_ref (die, attr, decl_die);
20539 return;
20544 /* Last chance: try to create a stack operation procedure to evaluate the
20545 value. Do nothing if even that is not possible or permitted. */
20546 if ((forms & dw_scalar_form_exprloc) == 0)
20547 return;
20549 list = loc_list_from_tree (value, 2, context);
20550 if (context && context->placeholder_arg)
20552 placeholder_seen = context->placeholder_seen;
20553 context->placeholder_seen = false;
20555 if (list == NULL || single_element_loc_list_p (list))
20557 /* If this attribute is not a reference nor constant, it is
20558 a DWARF expression rather than location description. For that
20559 loc_list_from_tree (value, 0, &context) is needed. */
20560 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20561 if (list2 && single_element_loc_list_p (list2))
20563 if (placeholder_seen)
20565 struct dwarf_procedure_info dpi;
20566 dpi.fndecl = NULL_TREE;
20567 dpi.args_count = 1;
20568 if (!resolve_args_picking (list2->expr, 1, &dpi))
20569 return;
20571 add_AT_loc (die, attr, list2->expr);
20572 return;
20576 /* If that failed to give a single element location list, fall back to
20577 outputting this as a reference... still if permitted. */
20578 if (list == NULL
20579 || (forms & dw_scalar_form_reference) == 0
20580 || placeholder_seen)
20581 return;
20583 if (current_function_decl == 0)
20584 context_die = comp_unit_die ();
20585 else
20586 context_die = lookup_decl_die (current_function_decl);
20588 decl_die = new_die (DW_TAG_variable, context_die, value);
20589 add_AT_flag (decl_die, DW_AT_artificial, 1);
20590 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20591 context_die);
20592 add_AT_location_description (decl_die, DW_AT_location, list);
20593 add_AT_die_ref (die, attr, decl_die);
20596 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20597 default. */
20599 static int
20600 lower_bound_default (void)
20602 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20604 case DW_LANG_C:
20605 case DW_LANG_C89:
20606 case DW_LANG_C99:
20607 case DW_LANG_C11:
20608 case DW_LANG_C_plus_plus:
20609 case DW_LANG_C_plus_plus_11:
20610 case DW_LANG_C_plus_plus_14:
20611 case DW_LANG_ObjC:
20612 case DW_LANG_ObjC_plus_plus:
20613 return 0;
20614 case DW_LANG_Fortran77:
20615 case DW_LANG_Fortran90:
20616 case DW_LANG_Fortran95:
20617 case DW_LANG_Fortran03:
20618 case DW_LANG_Fortran08:
20619 return 1;
20620 case DW_LANG_UPC:
20621 case DW_LANG_D:
20622 case DW_LANG_Python:
20623 return dwarf_version >= 4 ? 0 : -1;
20624 case DW_LANG_Ada95:
20625 case DW_LANG_Ada83:
20626 case DW_LANG_Cobol74:
20627 case DW_LANG_Cobol85:
20628 case DW_LANG_Modula2:
20629 case DW_LANG_PLI:
20630 return dwarf_version >= 4 ? 1 : -1;
20631 default:
20632 return -1;
20636 /* Given a tree node describing an array bound (either lower or upper) output
20637 a representation for that bound. */
20639 static void
20640 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20641 tree bound, struct loc_descr_context *context)
20643 int dflt;
20645 while (1)
20646 switch (TREE_CODE (bound))
20648 /* Strip all conversions. */
20649 CASE_CONVERT:
20650 case VIEW_CONVERT_EXPR:
20651 bound = TREE_OPERAND (bound, 0);
20652 break;
20654 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20655 are even omitted when they are the default. */
20656 case INTEGER_CST:
20657 /* If the value for this bound is the default one, we can even omit the
20658 attribute. */
20659 if (bound_attr == DW_AT_lower_bound
20660 && tree_fits_shwi_p (bound)
20661 && (dflt = lower_bound_default ()) != -1
20662 && tree_to_shwi (bound) == dflt)
20663 return;
20665 /* FALLTHRU */
20667 default:
20668 /* Because of the complex interaction there can be with other GNAT
20669 encodings, GDB isn't ready yet to handle proper DWARF description
20670 for self-referencial subrange bounds: let GNAT encodings do the
20671 magic in such a case. */
20672 if (is_ada ()
20673 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20674 && contains_placeholder_p (bound))
20675 return;
20677 add_scalar_info (subrange_die, bound_attr, bound,
20678 dw_scalar_form_constant
20679 | dw_scalar_form_exprloc
20680 | dw_scalar_form_reference,
20681 context);
20682 return;
20686 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20687 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20688 Note that the block of subscript information for an array type also
20689 includes information about the element type of the given array type.
20691 This function reuses previously set type and bound information if
20692 available. */
20694 static void
20695 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20697 unsigned dimension_number;
20698 tree lower, upper;
20699 dw_die_ref child = type_die->die_child;
20701 for (dimension_number = 0;
20702 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20703 type = TREE_TYPE (type), dimension_number++)
20705 tree domain = TYPE_DOMAIN (type);
20707 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20708 break;
20710 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20711 and (in GNU C only) variable bounds. Handle all three forms
20712 here. */
20714 /* Find and reuse a previously generated DW_TAG_subrange_type if
20715 available.
20717 For multi-dimensional arrays, as we iterate through the
20718 various dimensions in the enclosing for loop above, we also
20719 iterate through the DIE children and pick at each
20720 DW_TAG_subrange_type previously generated (if available).
20721 Each child DW_TAG_subrange_type DIE describes the range of
20722 the current dimension. At this point we should have as many
20723 DW_TAG_subrange_type's as we have dimensions in the
20724 array. */
20725 dw_die_ref subrange_die = NULL;
20726 if (child)
20727 while (1)
20729 child = child->die_sib;
20730 if (child->die_tag == DW_TAG_subrange_type)
20731 subrange_die = child;
20732 if (child == type_die->die_child)
20734 /* If we wrapped around, stop looking next time. */
20735 child = NULL;
20736 break;
20738 if (child->die_tag == DW_TAG_subrange_type)
20739 break;
20741 if (!subrange_die)
20742 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20744 if (domain)
20746 /* We have an array type with specified bounds. */
20747 lower = TYPE_MIN_VALUE (domain);
20748 upper = TYPE_MAX_VALUE (domain);
20750 /* Define the index type. */
20751 if (TREE_TYPE (domain)
20752 && !get_AT (subrange_die, DW_AT_type))
20754 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20755 TREE_TYPE field. We can't emit debug info for this
20756 because it is an unnamed integral type. */
20757 if (TREE_CODE (domain) == INTEGER_TYPE
20758 && TYPE_NAME (domain) == NULL_TREE
20759 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20760 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20762 else
20763 add_type_attribute (subrange_die, TREE_TYPE (domain),
20764 TYPE_UNQUALIFIED, false, type_die);
20767 /* ??? If upper is NULL, the array has unspecified length,
20768 but it does have a lower bound. This happens with Fortran
20769 dimension arr(N:*)
20770 Since the debugger is definitely going to need to know N
20771 to produce useful results, go ahead and output the lower
20772 bound solo, and hope the debugger can cope. */
20774 if (!get_AT (subrange_die, DW_AT_lower_bound))
20775 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20776 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20777 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20780 /* Otherwise we have an array type with an unspecified length. The
20781 DWARF-2 spec does not say how to handle this; let's just leave out the
20782 bounds. */
20786 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20788 static void
20789 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20791 dw_die_ref decl_die;
20792 HOST_WIDE_INT size;
20793 dw_loc_descr_ref size_expr = NULL;
20795 switch (TREE_CODE (tree_node))
20797 case ERROR_MARK:
20798 size = 0;
20799 break;
20800 case ENUMERAL_TYPE:
20801 case RECORD_TYPE:
20802 case UNION_TYPE:
20803 case QUAL_UNION_TYPE:
20804 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20805 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20807 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20808 return;
20810 size_expr = type_byte_size (tree_node, &size);
20811 break;
20812 case FIELD_DECL:
20813 /* For a data member of a struct or union, the DW_AT_byte_size is
20814 generally given as the number of bytes normally allocated for an
20815 object of the *declared* type of the member itself. This is true
20816 even for bit-fields. */
20817 size = int_size_in_bytes (field_type (tree_node));
20818 break;
20819 default:
20820 gcc_unreachable ();
20823 /* Support for dynamically-sized objects was introduced by DWARFv3.
20824 At the moment, GDB does not handle variable byte sizes very well,
20825 though. */
20826 if ((dwarf_version >= 3 || !dwarf_strict)
20827 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20828 && size_expr != NULL)
20829 add_AT_loc (die, DW_AT_byte_size, size_expr);
20831 /* Note that `size' might be -1 when we get to this point. If it is, that
20832 indicates that the byte size of the entity in question is variable and
20833 that we could not generate a DWARF expression that computes it. */
20834 if (size >= 0)
20835 add_AT_unsigned (die, DW_AT_byte_size, size);
20838 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20839 alignment. */
20841 static void
20842 add_alignment_attribute (dw_die_ref die, tree tree_node)
20844 if (dwarf_version < 5 && dwarf_strict)
20845 return;
20847 unsigned align;
20849 if (DECL_P (tree_node))
20851 if (!DECL_USER_ALIGN (tree_node))
20852 return;
20854 align = DECL_ALIGN_UNIT (tree_node);
20856 else if (TYPE_P (tree_node))
20858 if (!TYPE_USER_ALIGN (tree_node))
20859 return;
20861 align = TYPE_ALIGN_UNIT (tree_node);
20863 else
20864 gcc_unreachable ();
20866 add_AT_unsigned (die, DW_AT_alignment, align);
20869 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20870 which specifies the distance in bits from the highest order bit of the
20871 "containing object" for the bit-field to the highest order bit of the
20872 bit-field itself.
20874 For any given bit-field, the "containing object" is a hypothetical object
20875 (of some integral or enum type) within which the given bit-field lives. The
20876 type of this hypothetical "containing object" is always the same as the
20877 declared type of the individual bit-field itself. The determination of the
20878 exact location of the "containing object" for a bit-field is rather
20879 complicated. It's handled by the `field_byte_offset' function (above).
20881 CTX is required: see the comment for VLR_CONTEXT.
20883 Note that it is the size (in bytes) of the hypothetical "containing object"
20884 which will be given in the DW_AT_byte_size attribute for this bit-field.
20885 (See `byte_size_attribute' above). */
20887 static inline void
20888 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20890 HOST_WIDE_INT object_offset_in_bytes;
20891 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20892 HOST_WIDE_INT bitpos_int;
20893 HOST_WIDE_INT highest_order_object_bit_offset;
20894 HOST_WIDE_INT highest_order_field_bit_offset;
20895 HOST_WIDE_INT bit_offset;
20897 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20899 /* Must be a field and a bit field. */
20900 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20902 /* We can't yet handle bit-fields whose offsets are variable, so if we
20903 encounter such things, just return without generating any attribute
20904 whatsoever. Likewise for variable or too large size. */
20905 if (! tree_fits_shwi_p (bit_position (decl))
20906 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20907 return;
20909 bitpos_int = int_bit_position (decl);
20911 /* Note that the bit offset is always the distance (in bits) from the
20912 highest-order bit of the "containing object" to the highest-order bit of
20913 the bit-field itself. Since the "high-order end" of any object or field
20914 is different on big-endian and little-endian machines, the computation
20915 below must take account of these differences. */
20916 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20917 highest_order_field_bit_offset = bitpos_int;
20919 if (! BYTES_BIG_ENDIAN)
20921 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20922 highest_order_object_bit_offset +=
20923 simple_type_size_in_bits (original_type);
20926 bit_offset
20927 = (! BYTES_BIG_ENDIAN
20928 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20929 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20931 if (bit_offset < 0)
20932 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20933 else
20934 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20937 /* For a FIELD_DECL node which represents a bit field, output an attribute
20938 which specifies the length in bits of the given field. */
20940 static inline void
20941 add_bit_size_attribute (dw_die_ref die, tree decl)
20943 /* Must be a field and a bit field. */
20944 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20945 && DECL_BIT_FIELD_TYPE (decl));
20947 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20948 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20951 /* If the compiled language is ANSI C, then add a 'prototyped'
20952 attribute, if arg types are given for the parameters of a function. */
20954 static inline void
20955 add_prototyped_attribute (dw_die_ref die, tree func_type)
20957 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20959 case DW_LANG_C:
20960 case DW_LANG_C89:
20961 case DW_LANG_C99:
20962 case DW_LANG_C11:
20963 case DW_LANG_ObjC:
20964 if (prototype_p (func_type))
20965 add_AT_flag (die, DW_AT_prototyped, 1);
20966 break;
20967 default:
20968 break;
20972 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20973 by looking in the type declaration, the object declaration equate table or
20974 the block mapping. */
20976 static inline dw_die_ref
20977 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20979 dw_die_ref origin_die = NULL;
20981 if (DECL_P (origin))
20983 dw_die_ref c;
20984 origin_die = lookup_decl_die (origin);
20985 /* "Unwrap" the decls DIE which we put in the imported unit context.
20986 We are looking for the abstract copy here. */
20987 if (in_lto_p
20988 && origin_die
20989 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20990 /* ??? Identify this better. */
20991 && c->with_offset)
20992 origin_die = c;
20994 else if (TYPE_P (origin))
20995 origin_die = lookup_type_die (origin);
20996 else if (TREE_CODE (origin) == BLOCK)
20997 origin_die = BLOCK_DIE (origin);
20999 /* XXX: Functions that are never lowered don't always have correct block
21000 trees (in the case of java, they simply have no block tree, in some other
21001 languages). For these functions, there is nothing we can really do to
21002 output correct debug info for inlined functions in all cases. Rather
21003 than die, we'll just produce deficient debug info now, in that we will
21004 have variables without a proper abstract origin. In the future, when all
21005 functions are lowered, we should re-add a gcc_assert (origin_die)
21006 here. */
21008 if (origin_die)
21009 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21010 return origin_die;
21013 /* We do not currently support the pure_virtual attribute. */
21015 static inline void
21016 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21018 if (DECL_VINDEX (func_decl))
21020 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21022 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21023 add_AT_loc (die, DW_AT_vtable_elem_location,
21024 new_loc_descr (DW_OP_constu,
21025 tree_to_shwi (DECL_VINDEX (func_decl)),
21026 0));
21028 /* GNU extension: Record what type this method came from originally. */
21029 if (debug_info_level > DINFO_LEVEL_TERSE
21030 && DECL_CONTEXT (func_decl))
21031 add_AT_die_ref (die, DW_AT_containing_type,
21032 lookup_type_die (DECL_CONTEXT (func_decl)));
21036 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21037 given decl. This used to be a vendor extension until after DWARF 4
21038 standardized it. */
21040 static void
21041 add_linkage_attr (dw_die_ref die, tree decl)
21043 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21045 /* Mimic what assemble_name_raw does with a leading '*'. */
21046 if (name[0] == '*')
21047 name = &name[1];
21049 if (dwarf_version >= 4)
21050 add_AT_string (die, DW_AT_linkage_name, name);
21051 else
21052 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21055 /* Add source coordinate attributes for the given decl. */
21057 static void
21058 add_src_coords_attributes (dw_die_ref die, tree decl)
21060 expanded_location s;
21062 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21063 return;
21064 s = expand_location (DECL_SOURCE_LOCATION (decl));
21065 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21066 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21067 if (debug_column_info && s.column)
21068 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21071 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21073 static void
21074 add_linkage_name_raw (dw_die_ref die, tree decl)
21076 /* Defer until we have an assembler name set. */
21077 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21079 limbo_die_node *asm_name;
21081 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21082 asm_name->die = die;
21083 asm_name->created_for = decl;
21084 asm_name->next = deferred_asm_name;
21085 deferred_asm_name = asm_name;
21087 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21088 add_linkage_attr (die, decl);
21091 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21093 static void
21094 add_linkage_name (dw_die_ref die, tree decl)
21096 if (debug_info_level > DINFO_LEVEL_NONE
21097 && VAR_OR_FUNCTION_DECL_P (decl)
21098 && TREE_PUBLIC (decl)
21099 && !(VAR_P (decl) && DECL_REGISTER (decl))
21100 && die->die_tag != DW_TAG_member)
21101 add_linkage_name_raw (die, decl);
21104 /* Add a DW_AT_name attribute and source coordinate attribute for the
21105 given decl, but only if it actually has a name. */
21107 static void
21108 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21109 bool no_linkage_name)
21111 tree decl_name;
21113 decl_name = DECL_NAME (decl);
21114 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21116 const char *name = dwarf2_name (decl, 0);
21117 if (name)
21118 add_name_attribute (die, name);
21119 if (! DECL_ARTIFICIAL (decl))
21120 add_src_coords_attributes (die, decl);
21122 if (!no_linkage_name)
21123 add_linkage_name (die, decl);
21126 #ifdef VMS_DEBUGGING_INFO
21127 /* Get the function's name, as described by its RTL. This may be different
21128 from the DECL_NAME name used in the source file. */
21129 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21131 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21132 XEXP (DECL_RTL (decl), 0), false);
21133 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21135 #endif /* VMS_DEBUGGING_INFO */
21138 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21140 static void
21141 add_discr_value (dw_die_ref die, dw_discr_value *value)
21143 dw_attr_node attr;
21145 attr.dw_attr = DW_AT_discr_value;
21146 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21147 attr.dw_attr_val.val_entry = NULL;
21148 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21149 if (value->pos)
21150 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21151 else
21152 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21153 add_dwarf_attr (die, &attr);
21156 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21158 static void
21159 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21161 dw_attr_node attr;
21163 attr.dw_attr = DW_AT_discr_list;
21164 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21165 attr.dw_attr_val.val_entry = NULL;
21166 attr.dw_attr_val.v.val_discr_list = discr_list;
21167 add_dwarf_attr (die, &attr);
21170 static inline dw_discr_list_ref
21171 AT_discr_list (dw_attr_node *attr)
21173 return attr->dw_attr_val.v.val_discr_list;
21176 #ifdef VMS_DEBUGGING_INFO
21177 /* Output the debug main pointer die for VMS */
21179 void
21180 dwarf2out_vms_debug_main_pointer (void)
21182 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21183 dw_die_ref die;
21185 /* Allocate the VMS debug main subprogram die. */
21186 die = new_die_raw (DW_TAG_subprogram);
21187 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21188 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21189 current_function_funcdef_no);
21190 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21192 /* Make it the first child of comp_unit_die (). */
21193 die->die_parent = comp_unit_die ();
21194 if (comp_unit_die ()->die_child)
21196 die->die_sib = comp_unit_die ()->die_child->die_sib;
21197 comp_unit_die ()->die_child->die_sib = die;
21199 else
21201 die->die_sib = die;
21202 comp_unit_die ()->die_child = die;
21205 #endif /* VMS_DEBUGGING_INFO */
21207 /* Push a new declaration scope. */
21209 static void
21210 push_decl_scope (tree scope)
21212 vec_safe_push (decl_scope_table, scope);
21215 /* Pop a declaration scope. */
21217 static inline void
21218 pop_decl_scope (void)
21220 decl_scope_table->pop ();
21223 /* walk_tree helper function for uses_local_type, below. */
21225 static tree
21226 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21228 if (!TYPE_P (*tp))
21229 *walk_subtrees = 0;
21230 else
21232 tree name = TYPE_NAME (*tp);
21233 if (name && DECL_P (name) && decl_function_context (name))
21234 return *tp;
21236 return NULL_TREE;
21239 /* If TYPE involves a function-local type (including a local typedef to a
21240 non-local type), returns that type; otherwise returns NULL_TREE. */
21242 static tree
21243 uses_local_type (tree type)
21245 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21246 return used;
21249 /* Return the DIE for the scope that immediately contains this type.
21250 Non-named types that do not involve a function-local type get global
21251 scope. Named types nested in namespaces or other types get their
21252 containing scope. All other types (i.e. function-local named types) get
21253 the current active scope. */
21255 static dw_die_ref
21256 scope_die_for (tree t, dw_die_ref context_die)
21258 dw_die_ref scope_die = NULL;
21259 tree containing_scope;
21261 /* Non-types always go in the current scope. */
21262 gcc_assert (TYPE_P (t));
21264 /* Use the scope of the typedef, rather than the scope of the type
21265 it refers to. */
21266 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21267 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21268 else
21269 containing_scope = TYPE_CONTEXT (t);
21271 /* Use the containing namespace if there is one. */
21272 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21274 if (context_die == lookup_decl_die (containing_scope))
21275 /* OK */;
21276 else if (debug_info_level > DINFO_LEVEL_TERSE)
21277 context_die = get_context_die (containing_scope);
21278 else
21279 containing_scope = NULL_TREE;
21282 /* Ignore function type "scopes" from the C frontend. They mean that
21283 a tagged type is local to a parmlist of a function declarator, but
21284 that isn't useful to DWARF. */
21285 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21286 containing_scope = NULL_TREE;
21288 if (SCOPE_FILE_SCOPE_P (containing_scope))
21290 /* If T uses a local type keep it local as well, to avoid references
21291 to function-local DIEs from outside the function. */
21292 if (current_function_decl && uses_local_type (t))
21293 scope_die = context_die;
21294 else
21295 scope_die = comp_unit_die ();
21297 else if (TYPE_P (containing_scope))
21299 /* For types, we can just look up the appropriate DIE. */
21300 if (debug_info_level > DINFO_LEVEL_TERSE)
21301 scope_die = get_context_die (containing_scope);
21302 else
21304 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21305 if (scope_die == NULL)
21306 scope_die = comp_unit_die ();
21309 else
21310 scope_die = context_die;
21312 return scope_die;
21315 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21317 static inline int
21318 local_scope_p (dw_die_ref context_die)
21320 for (; context_die; context_die = context_die->die_parent)
21321 if (context_die->die_tag == DW_TAG_inlined_subroutine
21322 || context_die->die_tag == DW_TAG_subprogram)
21323 return 1;
21325 return 0;
21328 /* Returns nonzero if CONTEXT_DIE is a class. */
21330 static inline int
21331 class_scope_p (dw_die_ref context_die)
21333 return (context_die
21334 && (context_die->die_tag == DW_TAG_structure_type
21335 || context_die->die_tag == DW_TAG_class_type
21336 || context_die->die_tag == DW_TAG_interface_type
21337 || context_die->die_tag == DW_TAG_union_type));
21340 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21341 whether or not to treat a DIE in this context as a declaration. */
21343 static inline int
21344 class_or_namespace_scope_p (dw_die_ref context_die)
21346 return (class_scope_p (context_die)
21347 || (context_die && context_die->die_tag == DW_TAG_namespace));
21350 /* Many forms of DIEs require a "type description" attribute. This
21351 routine locates the proper "type descriptor" die for the type given
21352 by 'type' plus any additional qualifiers given by 'cv_quals', and
21353 adds a DW_AT_type attribute below the given die. */
21355 static void
21356 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21357 bool reverse, dw_die_ref context_die)
21359 enum tree_code code = TREE_CODE (type);
21360 dw_die_ref type_die = NULL;
21362 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21363 or fixed-point type, use the inner type. This is because we have no
21364 support for unnamed types in base_type_die. This can happen if this is
21365 an Ada subrange type. Correct solution is emit a subrange type die. */
21366 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21367 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21368 type = TREE_TYPE (type), code = TREE_CODE (type);
21370 if (code == ERROR_MARK
21371 /* Handle a special case. For functions whose return type is void, we
21372 generate *no* type attribute. (Note that no object may have type
21373 `void', so this only applies to function return types). */
21374 || code == VOID_TYPE)
21375 return;
21377 type_die = modified_type_die (type,
21378 cv_quals | TYPE_QUALS (type),
21379 reverse,
21380 context_die);
21382 if (type_die != NULL)
21383 add_AT_die_ref (object_die, DW_AT_type, type_die);
21386 /* Given an object die, add the calling convention attribute for the
21387 function call type. */
21388 static void
21389 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21391 enum dwarf_calling_convention value = DW_CC_normal;
21393 value = ((enum dwarf_calling_convention)
21394 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21396 if (is_fortran ()
21397 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21399 /* DWARF 2 doesn't provide a way to identify a program's source-level
21400 entry point. DW_AT_calling_convention attributes are only meant
21401 to describe functions' calling conventions. However, lacking a
21402 better way to signal the Fortran main program, we used this for
21403 a long time, following existing custom. Now, DWARF 4 has
21404 DW_AT_main_subprogram, which we add below, but some tools still
21405 rely on the old way, which we thus keep. */
21406 value = DW_CC_program;
21408 if (dwarf_version >= 4 || !dwarf_strict)
21409 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21412 /* Only add the attribute if the backend requests it, and
21413 is not DW_CC_normal. */
21414 if (value && (value != DW_CC_normal))
21415 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21418 /* Given a tree pointer to a struct, class, union, or enum type node, return
21419 a pointer to the (string) tag name for the given type, or zero if the type
21420 was declared without a tag. */
21422 static const char *
21423 type_tag (const_tree type)
21425 const char *name = 0;
21427 if (TYPE_NAME (type) != 0)
21429 tree t = 0;
21431 /* Find the IDENTIFIER_NODE for the type name. */
21432 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21433 && !TYPE_NAMELESS (type))
21434 t = TYPE_NAME (type);
21436 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21437 a TYPE_DECL node, regardless of whether or not a `typedef' was
21438 involved. */
21439 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21440 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21442 /* We want to be extra verbose. Don't call dwarf_name if
21443 DECL_NAME isn't set. The default hook for decl_printable_name
21444 doesn't like that, and in this context it's correct to return
21445 0, instead of "<anonymous>" or the like. */
21446 if (DECL_NAME (TYPE_NAME (type))
21447 && !DECL_NAMELESS (TYPE_NAME (type)))
21448 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21451 /* Now get the name as a string, or invent one. */
21452 if (!name && t != 0)
21453 name = IDENTIFIER_POINTER (t);
21456 return (name == 0 || *name == '\0') ? 0 : name;
21459 /* Return the type associated with a data member, make a special check
21460 for bit field types. */
21462 static inline tree
21463 member_declared_type (const_tree member)
21465 return (DECL_BIT_FIELD_TYPE (member)
21466 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21469 /* Get the decl's label, as described by its RTL. This may be different
21470 from the DECL_NAME name used in the source file. */
21472 #if 0
21473 static const char *
21474 decl_start_label (tree decl)
21476 rtx x;
21477 const char *fnname;
21479 x = DECL_RTL (decl);
21480 gcc_assert (MEM_P (x));
21482 x = XEXP (x, 0);
21483 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21485 fnname = XSTR (x, 0);
21486 return fnname;
21488 #endif
21490 /* For variable-length arrays that have been previously generated, but
21491 may be incomplete due to missing subscript info, fill the subscript
21492 info. Return TRUE if this is one of those cases. */
21493 static bool
21494 fill_variable_array_bounds (tree type)
21496 if (TREE_ASM_WRITTEN (type)
21497 && TREE_CODE (type) == ARRAY_TYPE
21498 && variably_modified_type_p (type, NULL))
21500 dw_die_ref array_die = lookup_type_die (type);
21501 if (!array_die)
21502 return false;
21503 add_subscript_info (array_die, type, !is_ada ());
21504 return true;
21506 return false;
21509 /* These routines generate the internal representation of the DIE's for
21510 the compilation unit. Debugging information is collected by walking
21511 the declaration trees passed in from dwarf2out_decl(). */
21513 static void
21514 gen_array_type_die (tree type, dw_die_ref context_die)
21516 dw_die_ref array_die;
21518 /* GNU compilers represent multidimensional array types as sequences of one
21519 dimensional array types whose element types are themselves array types.
21520 We sometimes squish that down to a single array_type DIE with multiple
21521 subscripts in the Dwarf debugging info. The draft Dwarf specification
21522 say that we are allowed to do this kind of compression in C, because
21523 there is no difference between an array of arrays and a multidimensional
21524 array. We don't do this for Ada to remain as close as possible to the
21525 actual representation, which is especially important against the language
21526 flexibilty wrt arrays of variable size. */
21528 bool collapse_nested_arrays = !is_ada ();
21530 if (fill_variable_array_bounds (type))
21531 return;
21533 dw_die_ref scope_die = scope_die_for (type, context_die);
21534 tree element_type;
21536 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21537 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21538 if (TYPE_STRING_FLAG (type)
21539 && TREE_CODE (type) == ARRAY_TYPE
21540 && is_fortran ()
21541 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21543 HOST_WIDE_INT size;
21545 array_die = new_die (DW_TAG_string_type, scope_die, type);
21546 add_name_attribute (array_die, type_tag (type));
21547 equate_type_number_to_die (type, array_die);
21548 size = int_size_in_bytes (type);
21549 if (size >= 0)
21550 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21551 /* ??? We can't annotate types late, but for LTO we may not
21552 generate a location early either (gfortran.dg/save_6.f90). */
21553 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21554 && TYPE_DOMAIN (type) != NULL_TREE
21555 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21557 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21558 tree rszdecl = szdecl;
21560 size = int_size_in_bytes (TREE_TYPE (szdecl));
21561 if (!DECL_P (szdecl))
21563 if (TREE_CODE (szdecl) == INDIRECT_REF
21564 && DECL_P (TREE_OPERAND (szdecl, 0)))
21566 rszdecl = TREE_OPERAND (szdecl, 0);
21567 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21568 != DWARF2_ADDR_SIZE)
21569 size = 0;
21571 else
21572 size = 0;
21574 if (size > 0)
21576 dw_loc_list_ref loc
21577 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21578 NULL);
21579 if (loc)
21581 add_AT_location_description (array_die, DW_AT_string_length,
21582 loc);
21583 if (size != DWARF2_ADDR_SIZE)
21584 add_AT_unsigned (array_die, dwarf_version >= 5
21585 ? DW_AT_string_length_byte_size
21586 : DW_AT_byte_size, size);
21590 return;
21593 array_die = new_die (DW_TAG_array_type, scope_die, type);
21594 add_name_attribute (array_die, type_tag (type));
21595 equate_type_number_to_die (type, array_die);
21597 if (TREE_CODE (type) == VECTOR_TYPE)
21598 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21600 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21601 if (is_fortran ()
21602 && TREE_CODE (type) == ARRAY_TYPE
21603 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21604 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21605 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21607 #if 0
21608 /* We default the array ordering. Debuggers will probably do the right
21609 things even if DW_AT_ordering is not present. It's not even an issue
21610 until we start to get into multidimensional arrays anyway. If a debugger
21611 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21612 then we'll have to put the DW_AT_ordering attribute back in. (But if
21613 and when we find out that we need to put these in, we will only do so
21614 for multidimensional arrays. */
21615 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21616 #endif
21618 if (TREE_CODE (type) == VECTOR_TYPE)
21620 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21621 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21622 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21623 add_bound_info (subrange_die, DW_AT_upper_bound,
21624 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21626 else
21627 add_subscript_info (array_die, type, collapse_nested_arrays);
21629 /* Add representation of the type of the elements of this array type and
21630 emit the corresponding DIE if we haven't done it already. */
21631 element_type = TREE_TYPE (type);
21632 if (collapse_nested_arrays)
21633 while (TREE_CODE (element_type) == ARRAY_TYPE)
21635 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21636 break;
21637 element_type = TREE_TYPE (element_type);
21640 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21641 TREE_CODE (type) == ARRAY_TYPE
21642 && TYPE_REVERSE_STORAGE_ORDER (type),
21643 context_die);
21645 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21646 if (TYPE_ARTIFICIAL (type))
21647 add_AT_flag (array_die, DW_AT_artificial, 1);
21649 if (get_AT (array_die, DW_AT_name))
21650 add_pubtype (type, array_die);
21652 add_alignment_attribute (array_die, type);
21655 /* This routine generates DIE for array with hidden descriptor, details
21656 are filled into *info by a langhook. */
21658 static void
21659 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21660 dw_die_ref context_die)
21662 const dw_die_ref scope_die = scope_die_for (type, context_die);
21663 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21664 struct loc_descr_context context = { type, info->base_decl, NULL,
21665 false, false };
21666 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21667 int dim;
21669 add_name_attribute (array_die, type_tag (type));
21670 equate_type_number_to_die (type, array_die);
21672 if (info->ndimensions > 1)
21673 switch (info->ordering)
21675 case array_descr_ordering_row_major:
21676 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21677 break;
21678 case array_descr_ordering_column_major:
21679 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21680 break;
21681 default:
21682 break;
21685 if (dwarf_version >= 3 || !dwarf_strict)
21687 if (info->data_location)
21688 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21689 dw_scalar_form_exprloc, &context);
21690 if (info->associated)
21691 add_scalar_info (array_die, DW_AT_associated, info->associated,
21692 dw_scalar_form_constant
21693 | dw_scalar_form_exprloc
21694 | dw_scalar_form_reference, &context);
21695 if (info->allocated)
21696 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21697 dw_scalar_form_constant
21698 | dw_scalar_form_exprloc
21699 | dw_scalar_form_reference, &context);
21700 if (info->stride)
21702 const enum dwarf_attribute attr
21703 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21704 const int forms
21705 = (info->stride_in_bits)
21706 ? dw_scalar_form_constant
21707 : (dw_scalar_form_constant
21708 | dw_scalar_form_exprloc
21709 | dw_scalar_form_reference);
21711 add_scalar_info (array_die, attr, info->stride, forms, &context);
21714 if (dwarf_version >= 5)
21716 if (info->rank)
21718 add_scalar_info (array_die, DW_AT_rank, info->rank,
21719 dw_scalar_form_constant
21720 | dw_scalar_form_exprloc, &context);
21721 subrange_tag = DW_TAG_generic_subrange;
21722 context.placeholder_arg = true;
21726 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21728 for (dim = 0; dim < info->ndimensions; dim++)
21730 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21732 if (info->dimen[dim].bounds_type)
21733 add_type_attribute (subrange_die,
21734 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21735 false, context_die);
21736 if (info->dimen[dim].lower_bound)
21737 add_bound_info (subrange_die, DW_AT_lower_bound,
21738 info->dimen[dim].lower_bound, &context);
21739 if (info->dimen[dim].upper_bound)
21740 add_bound_info (subrange_die, DW_AT_upper_bound,
21741 info->dimen[dim].upper_bound, &context);
21742 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21743 add_scalar_info (subrange_die, DW_AT_byte_stride,
21744 info->dimen[dim].stride,
21745 dw_scalar_form_constant
21746 | dw_scalar_form_exprloc
21747 | dw_scalar_form_reference,
21748 &context);
21751 gen_type_die (info->element_type, context_die);
21752 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21753 TREE_CODE (type) == ARRAY_TYPE
21754 && TYPE_REVERSE_STORAGE_ORDER (type),
21755 context_die);
21757 if (get_AT (array_die, DW_AT_name))
21758 add_pubtype (type, array_die);
21760 add_alignment_attribute (array_die, type);
21763 #if 0
21764 static void
21765 gen_entry_point_die (tree decl, dw_die_ref context_die)
21767 tree origin = decl_ultimate_origin (decl);
21768 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21770 if (origin != NULL)
21771 add_abstract_origin_attribute (decl_die, origin);
21772 else
21774 add_name_and_src_coords_attributes (decl_die, decl);
21775 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21776 TYPE_UNQUALIFIED, false, context_die);
21779 if (DECL_ABSTRACT_P (decl))
21780 equate_decl_number_to_die (decl, decl_die);
21781 else
21782 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21784 #endif
21786 /* Walk through the list of incomplete types again, trying once more to
21787 emit full debugging info for them. */
21789 static void
21790 retry_incomplete_types (void)
21792 set_early_dwarf s;
21793 int i;
21795 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21796 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21797 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21798 vec_safe_truncate (incomplete_types, 0);
21801 /* Determine what tag to use for a record type. */
21803 static enum dwarf_tag
21804 record_type_tag (tree type)
21806 if (! lang_hooks.types.classify_record)
21807 return DW_TAG_structure_type;
21809 switch (lang_hooks.types.classify_record (type))
21811 case RECORD_IS_STRUCT:
21812 return DW_TAG_structure_type;
21814 case RECORD_IS_CLASS:
21815 return DW_TAG_class_type;
21817 case RECORD_IS_INTERFACE:
21818 if (dwarf_version >= 3 || !dwarf_strict)
21819 return DW_TAG_interface_type;
21820 return DW_TAG_structure_type;
21822 default:
21823 gcc_unreachable ();
21827 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21828 include all of the information about the enumeration values also. Each
21829 enumerated type name/value is listed as a child of the enumerated type
21830 DIE. */
21832 static dw_die_ref
21833 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21835 dw_die_ref type_die = lookup_type_die (type);
21837 if (type_die == NULL)
21839 type_die = new_die (DW_TAG_enumeration_type,
21840 scope_die_for (type, context_die), type);
21841 equate_type_number_to_die (type, type_die);
21842 add_name_attribute (type_die, type_tag (type));
21843 if (dwarf_version >= 4 || !dwarf_strict)
21845 if (ENUM_IS_SCOPED (type))
21846 add_AT_flag (type_die, DW_AT_enum_class, 1);
21847 if (ENUM_IS_OPAQUE (type))
21848 add_AT_flag (type_die, DW_AT_declaration, 1);
21850 if (!dwarf_strict)
21851 add_AT_unsigned (type_die, DW_AT_encoding,
21852 TYPE_UNSIGNED (type)
21853 ? DW_ATE_unsigned
21854 : DW_ATE_signed);
21856 else if (! TYPE_SIZE (type))
21857 return type_die;
21858 else
21859 remove_AT (type_die, DW_AT_declaration);
21861 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21862 given enum type is incomplete, do not generate the DW_AT_byte_size
21863 attribute or the DW_AT_element_list attribute. */
21864 if (TYPE_SIZE (type))
21866 tree link;
21868 TREE_ASM_WRITTEN (type) = 1;
21869 add_byte_size_attribute (type_die, type);
21870 add_alignment_attribute (type_die, type);
21871 if (dwarf_version >= 3 || !dwarf_strict)
21873 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21874 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21875 context_die);
21877 if (TYPE_STUB_DECL (type) != NULL_TREE)
21879 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21880 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21883 /* If the first reference to this type was as the return type of an
21884 inline function, then it may not have a parent. Fix this now. */
21885 if (type_die->die_parent == NULL)
21886 add_child_die (scope_die_for (type, context_die), type_die);
21888 for (link = TYPE_VALUES (type);
21889 link != NULL; link = TREE_CHAIN (link))
21891 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21892 tree value = TREE_VALUE (link);
21894 add_name_attribute (enum_die,
21895 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21897 if (TREE_CODE (value) == CONST_DECL)
21898 value = DECL_INITIAL (value);
21900 if (simple_type_size_in_bits (TREE_TYPE (value))
21901 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21903 /* For constant forms created by add_AT_unsigned DWARF
21904 consumers (GDB, elfutils, etc.) always zero extend
21905 the value. Only when the actual value is negative
21906 do we need to use add_AT_int to generate a constant
21907 form that can represent negative values. */
21908 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21909 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21910 add_AT_unsigned (enum_die, DW_AT_const_value,
21911 (unsigned HOST_WIDE_INT) val);
21912 else
21913 add_AT_int (enum_die, DW_AT_const_value, val);
21915 else
21916 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21917 that here. TODO: This should be re-worked to use correct
21918 signed/unsigned double tags for all cases. */
21919 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21922 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21923 if (TYPE_ARTIFICIAL (type))
21924 add_AT_flag (type_die, DW_AT_artificial, 1);
21926 else
21927 add_AT_flag (type_die, DW_AT_declaration, 1);
21929 add_pubtype (type, type_die);
21931 return type_die;
21934 /* Generate a DIE to represent either a real live formal parameter decl or to
21935 represent just the type of some formal parameter position in some function
21936 type.
21938 Note that this routine is a bit unusual because its argument may be a
21939 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21940 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21941 node. If it's the former then this function is being called to output a
21942 DIE to represent a formal parameter object (or some inlining thereof). If
21943 it's the latter, then this function is only being called to output a
21944 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21945 argument type of some subprogram type.
21946 If EMIT_NAME_P is true, name and source coordinate attributes
21947 are emitted. */
21949 static dw_die_ref
21950 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21951 dw_die_ref context_die)
21953 tree node_or_origin = node ? node : origin;
21954 tree ultimate_origin;
21955 dw_die_ref parm_die = NULL;
21957 if (DECL_P (node_or_origin))
21959 parm_die = lookup_decl_die (node);
21961 /* If the contexts differ, we may not be talking about the same
21962 thing.
21963 ??? When in LTO the DIE parent is the "abstract" copy and the
21964 context_die is the specification "copy". But this whole block
21965 should eventually be no longer needed. */
21966 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21968 if (!DECL_ABSTRACT_P (node))
21970 /* This can happen when creating an inlined instance, in
21971 which case we need to create a new DIE that will get
21972 annotated with DW_AT_abstract_origin. */
21973 parm_die = NULL;
21975 else
21976 gcc_unreachable ();
21979 if (parm_die && parm_die->die_parent == NULL)
21981 /* Check that parm_die already has the right attributes that
21982 we would have added below. If any attributes are
21983 missing, fall through to add them. */
21984 if (! DECL_ABSTRACT_P (node_or_origin)
21985 && !get_AT (parm_die, DW_AT_location)
21986 && !get_AT (parm_die, DW_AT_const_value))
21987 /* We are missing location info, and are about to add it. */
21989 else
21991 add_child_die (context_die, parm_die);
21992 return parm_die;
21997 /* If we have a previously generated DIE, use it, unless this is an
21998 concrete instance (origin != NULL), in which case we need a new
21999 DIE with a corresponding DW_AT_abstract_origin. */
22000 bool reusing_die;
22001 if (parm_die && origin == NULL)
22002 reusing_die = true;
22003 else
22005 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22006 reusing_die = false;
22009 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22011 case tcc_declaration:
22012 ultimate_origin = decl_ultimate_origin (node_or_origin);
22013 if (node || ultimate_origin)
22014 origin = ultimate_origin;
22016 if (reusing_die)
22017 goto add_location;
22019 if (origin != NULL)
22020 add_abstract_origin_attribute (parm_die, origin);
22021 else if (emit_name_p)
22022 add_name_and_src_coords_attributes (parm_die, node);
22023 if (origin == NULL
22024 || (! DECL_ABSTRACT_P (node_or_origin)
22025 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22026 decl_function_context
22027 (node_or_origin))))
22029 tree type = TREE_TYPE (node_or_origin);
22030 if (decl_by_reference_p (node_or_origin))
22031 add_type_attribute (parm_die, TREE_TYPE (type),
22032 TYPE_UNQUALIFIED,
22033 false, context_die);
22034 else
22035 add_type_attribute (parm_die, type,
22036 decl_quals (node_or_origin),
22037 false, context_die);
22039 if (origin == NULL && DECL_ARTIFICIAL (node))
22040 add_AT_flag (parm_die, DW_AT_artificial, 1);
22041 add_location:
22042 if (node && node != origin)
22043 equate_decl_number_to_die (node, parm_die);
22044 if (! DECL_ABSTRACT_P (node_or_origin))
22045 add_location_or_const_value_attribute (parm_die, node_or_origin,
22046 node == NULL);
22048 break;
22050 case tcc_type:
22051 /* We were called with some kind of a ..._TYPE node. */
22052 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22053 context_die);
22054 break;
22056 default:
22057 gcc_unreachable ();
22060 return parm_die;
22063 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22064 children DW_TAG_formal_parameter DIEs representing the arguments of the
22065 parameter pack.
22067 PARM_PACK must be a function parameter pack.
22068 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22069 must point to the subsequent arguments of the function PACK_ARG belongs to.
22070 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22071 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22072 following the last one for which a DIE was generated. */
22074 static dw_die_ref
22075 gen_formal_parameter_pack_die (tree parm_pack,
22076 tree pack_arg,
22077 dw_die_ref subr_die,
22078 tree *next_arg)
22080 tree arg;
22081 dw_die_ref parm_pack_die;
22083 gcc_assert (parm_pack
22084 && lang_hooks.function_parameter_pack_p (parm_pack)
22085 && subr_die);
22087 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22088 add_src_coords_attributes (parm_pack_die, parm_pack);
22090 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22092 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22093 parm_pack))
22094 break;
22095 gen_formal_parameter_die (arg, NULL,
22096 false /* Don't emit name attribute. */,
22097 parm_pack_die);
22099 if (next_arg)
22100 *next_arg = arg;
22101 return parm_pack_die;
22104 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22105 at the end of an (ANSI prototyped) formal parameters list. */
22107 static void
22108 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22110 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22113 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22114 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22115 parameters as specified in some function type specification (except for
22116 those which appear as part of a function *definition*). */
22118 static void
22119 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22121 tree link;
22122 tree formal_type = NULL;
22123 tree first_parm_type;
22124 tree arg;
22126 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22128 arg = DECL_ARGUMENTS (function_or_method_type);
22129 function_or_method_type = TREE_TYPE (function_or_method_type);
22131 else
22132 arg = NULL_TREE;
22134 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22136 /* Make our first pass over the list of formal parameter types and output a
22137 DW_TAG_formal_parameter DIE for each one. */
22138 for (link = first_parm_type; link; )
22140 dw_die_ref parm_die;
22142 formal_type = TREE_VALUE (link);
22143 if (formal_type == void_type_node)
22144 break;
22146 /* Output a (nameless) DIE to represent the formal parameter itself. */
22147 if (!POINTER_BOUNDS_TYPE_P (formal_type))
22149 parm_die = gen_formal_parameter_die (formal_type, NULL,
22150 true /* Emit name attribute. */,
22151 context_die);
22152 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22153 && link == first_parm_type)
22155 add_AT_flag (parm_die, DW_AT_artificial, 1);
22156 if (dwarf_version >= 3 || !dwarf_strict)
22157 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22159 else if (arg && DECL_ARTIFICIAL (arg))
22160 add_AT_flag (parm_die, DW_AT_artificial, 1);
22163 link = TREE_CHAIN (link);
22164 if (arg)
22165 arg = DECL_CHAIN (arg);
22168 /* If this function type has an ellipsis, add a
22169 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22170 if (formal_type != void_type_node)
22171 gen_unspecified_parameters_die (function_or_method_type, context_die);
22173 /* Make our second (and final) pass over the list of formal parameter types
22174 and output DIEs to represent those types (as necessary). */
22175 for (link = TYPE_ARG_TYPES (function_or_method_type);
22176 link && TREE_VALUE (link);
22177 link = TREE_CHAIN (link))
22178 gen_type_die (TREE_VALUE (link), context_die);
22181 /* We want to generate the DIE for TYPE so that we can generate the
22182 die for MEMBER, which has been defined; we will need to refer back
22183 to the member declaration nested within TYPE. If we're trying to
22184 generate minimal debug info for TYPE, processing TYPE won't do the
22185 trick; we need to attach the member declaration by hand. */
22187 static void
22188 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22190 gen_type_die (type, context_die);
22192 /* If we're trying to avoid duplicate debug info, we may not have
22193 emitted the member decl for this function. Emit it now. */
22194 if (TYPE_STUB_DECL (type)
22195 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22196 && ! lookup_decl_die (member))
22198 dw_die_ref type_die;
22199 gcc_assert (!decl_ultimate_origin (member));
22201 push_decl_scope (type);
22202 type_die = lookup_type_die_strip_naming_typedef (type);
22203 if (TREE_CODE (member) == FUNCTION_DECL)
22204 gen_subprogram_die (member, type_die);
22205 else if (TREE_CODE (member) == FIELD_DECL)
22207 /* Ignore the nameless fields that are used to skip bits but handle
22208 C++ anonymous unions and structs. */
22209 if (DECL_NAME (member) != NULL_TREE
22210 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22211 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22213 struct vlr_context vlr_ctx = {
22214 DECL_CONTEXT (member), /* struct_type */
22215 NULL_TREE /* variant_part_offset */
22217 gen_type_die (member_declared_type (member), type_die);
22218 gen_field_die (member, &vlr_ctx, type_die);
22221 else
22222 gen_variable_die (member, NULL_TREE, type_die);
22224 pop_decl_scope ();
22228 /* Forward declare these functions, because they are mutually recursive
22229 with their set_block_* pairing functions. */
22230 static void set_decl_origin_self (tree);
22232 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22233 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22234 that it points to the node itself, thus indicating that the node is its
22235 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22236 the given node is NULL, recursively descend the decl/block tree which
22237 it is the root of, and for each other ..._DECL or BLOCK node contained
22238 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22239 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22240 values to point to themselves. */
22242 static void
22243 set_block_origin_self (tree stmt)
22245 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22247 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22250 tree local_decl;
22252 for (local_decl = BLOCK_VARS (stmt);
22253 local_decl != NULL_TREE;
22254 local_decl = DECL_CHAIN (local_decl))
22255 /* Do not recurse on nested functions since the inlining status
22256 of parent and child can be different as per the DWARF spec. */
22257 if (TREE_CODE (local_decl) != FUNCTION_DECL
22258 && !DECL_EXTERNAL (local_decl))
22259 set_decl_origin_self (local_decl);
22263 tree subblock;
22265 for (subblock = BLOCK_SUBBLOCKS (stmt);
22266 subblock != NULL_TREE;
22267 subblock = BLOCK_CHAIN (subblock))
22268 set_block_origin_self (subblock); /* Recurse. */
22273 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22274 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22275 node to so that it points to the node itself, thus indicating that the
22276 node represents its own (abstract) origin. Additionally, if the
22277 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22278 the decl/block tree of which the given node is the root of, and for
22279 each other ..._DECL or BLOCK node contained therein whose
22280 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22281 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22282 point to themselves. */
22284 static void
22285 set_decl_origin_self (tree decl)
22287 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22289 DECL_ABSTRACT_ORIGIN (decl) = decl;
22290 if (TREE_CODE (decl) == FUNCTION_DECL)
22292 tree arg;
22294 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22295 DECL_ABSTRACT_ORIGIN (arg) = arg;
22296 if (DECL_INITIAL (decl) != NULL_TREE
22297 && DECL_INITIAL (decl) != error_mark_node)
22298 set_block_origin_self (DECL_INITIAL (decl));
22303 /* Mark the early DIE for DECL as the abstract instance. */
22305 static void
22306 dwarf2out_abstract_function (tree decl)
22308 dw_die_ref old_die;
22310 /* Make sure we have the actual abstract inline, not a clone. */
22311 decl = DECL_ORIGIN (decl);
22313 if (DECL_IGNORED_P (decl))
22314 return;
22316 old_die = lookup_decl_die (decl);
22317 /* With early debug we always have an old DIE unless we are in LTO
22318 and the user did not compile but only link with debug. */
22319 if (in_lto_p && ! old_die)
22320 return;
22321 gcc_assert (old_die != NULL);
22322 if (get_AT (old_die, DW_AT_inline)
22323 || get_AT (old_die, DW_AT_abstract_origin))
22324 /* We've already generated the abstract instance. */
22325 return;
22327 /* Go ahead and put DW_AT_inline on the DIE. */
22328 if (DECL_DECLARED_INLINE_P (decl))
22330 if (cgraph_function_possibly_inlined_p (decl))
22331 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22332 else
22333 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22335 else
22337 if (cgraph_function_possibly_inlined_p (decl))
22338 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22339 else
22340 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22343 if (DECL_DECLARED_INLINE_P (decl)
22344 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22345 add_AT_flag (old_die, DW_AT_artificial, 1);
22347 set_decl_origin_self (decl);
22350 /* Helper function of premark_used_types() which gets called through
22351 htab_traverse.
22353 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22354 marked as unused by prune_unused_types. */
22356 bool
22357 premark_used_types_helper (tree const &type, void *)
22359 dw_die_ref die;
22361 die = lookup_type_die (type);
22362 if (die != NULL)
22363 die->die_perennial_p = 1;
22364 return true;
22367 /* Helper function of premark_types_used_by_global_vars which gets called
22368 through htab_traverse.
22370 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22371 marked as unused by prune_unused_types. The DIE of the type is marked
22372 only if the global variable using the type will actually be emitted. */
22375 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22376 void *)
22378 struct types_used_by_vars_entry *entry;
22379 dw_die_ref die;
22381 entry = (struct types_used_by_vars_entry *) *slot;
22382 gcc_assert (entry->type != NULL
22383 && entry->var_decl != NULL);
22384 die = lookup_type_die (entry->type);
22385 if (die)
22387 /* Ask cgraph if the global variable really is to be emitted.
22388 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22389 varpool_node *node = varpool_node::get (entry->var_decl);
22390 if (node && node->definition)
22392 die->die_perennial_p = 1;
22393 /* Keep the parent DIEs as well. */
22394 while ((die = die->die_parent) && die->die_perennial_p == 0)
22395 die->die_perennial_p = 1;
22398 return 1;
22401 /* Mark all members of used_types_hash as perennial. */
22403 static void
22404 premark_used_types (struct function *fun)
22406 if (fun && fun->used_types_hash)
22407 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22410 /* Mark all members of types_used_by_vars_entry as perennial. */
22412 static void
22413 premark_types_used_by_global_vars (void)
22415 if (types_used_by_vars_hash)
22416 types_used_by_vars_hash
22417 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22420 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22421 for CA_LOC call arg loc node. */
22423 static dw_die_ref
22424 gen_call_site_die (tree decl, dw_die_ref subr_die,
22425 struct call_arg_loc_node *ca_loc)
22427 dw_die_ref stmt_die = NULL, die;
22428 tree block = ca_loc->block;
22430 while (block
22431 && block != DECL_INITIAL (decl)
22432 && TREE_CODE (block) == BLOCK)
22434 stmt_die = BLOCK_DIE (block);
22435 if (stmt_die)
22436 break;
22437 block = BLOCK_SUPERCONTEXT (block);
22439 if (stmt_die == NULL)
22440 stmt_die = subr_die;
22441 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22442 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22443 if (ca_loc->tail_call_p)
22444 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22445 if (ca_loc->symbol_ref)
22447 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22448 if (tdie)
22449 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22450 else
22451 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22452 false);
22454 return die;
22457 /* Generate a DIE to represent a declared function (either file-scope or
22458 block-local). */
22460 static void
22461 gen_subprogram_die (tree decl, dw_die_ref context_die)
22463 tree origin = decl_ultimate_origin (decl);
22464 dw_die_ref subr_die;
22465 dw_die_ref old_die = lookup_decl_die (decl);
22467 /* This function gets called multiple times for different stages of
22468 the debug process. For example, for func() in this code:
22470 namespace S
22472 void func() { ... }
22475 ...we get called 4 times. Twice in early debug and twice in
22476 late debug:
22478 Early debug
22479 -----------
22481 1. Once while generating func() within the namespace. This is
22482 the declaration. The declaration bit below is set, as the
22483 context is the namespace.
22485 A new DIE will be generated with DW_AT_declaration set.
22487 2. Once for func() itself. This is the specification. The
22488 declaration bit below is clear as the context is the CU.
22490 We will use the cached DIE from (1) to create a new DIE with
22491 DW_AT_specification pointing to the declaration in (1).
22493 Late debug via rest_of_handle_final()
22494 -------------------------------------
22496 3. Once generating func() within the namespace. This is also the
22497 declaration, as in (1), but this time we will early exit below
22498 as we have a cached DIE and a declaration needs no additional
22499 annotations (no locations), as the source declaration line
22500 info is enough.
22502 4. Once for func() itself. As in (2), this is the specification,
22503 but this time we will re-use the cached DIE, and just annotate
22504 it with the location information that should now be available.
22506 For something without namespaces, but with abstract instances, we
22507 are also called a multiple times:
22509 class Base
22511 public:
22512 Base (); // constructor declaration (1)
22515 Base::Base () { } // constructor specification (2)
22517 Early debug
22518 -----------
22520 1. Once for the Base() constructor by virtue of it being a
22521 member of the Base class. This is done via
22522 rest_of_type_compilation.
22524 This is a declaration, so a new DIE will be created with
22525 DW_AT_declaration.
22527 2. Once for the Base() constructor definition, but this time
22528 while generating the abstract instance of the base
22529 constructor (__base_ctor) which is being generated via early
22530 debug of reachable functions.
22532 Even though we have a cached version of the declaration (1),
22533 we will create a DW_AT_specification of the declaration DIE
22534 in (1).
22536 3. Once for the __base_ctor itself, but this time, we generate
22537 an DW_AT_abstract_origin version of the DW_AT_specification in
22538 (2).
22540 Late debug via rest_of_handle_final
22541 -----------------------------------
22543 4. One final time for the __base_ctor (which will have a cached
22544 DIE with DW_AT_abstract_origin created in (3). This time,
22545 we will just annotate the location information now
22546 available.
22548 int declaration = (current_function_decl != decl
22549 || class_or_namespace_scope_p (context_die));
22551 /* A declaration that has been previously dumped needs no
22552 additional information. */
22553 if (old_die && declaration)
22554 return;
22556 /* Now that the C++ front end lazily declares artificial member fns, we
22557 might need to retrofit the declaration into its class. */
22558 if (!declaration && !origin && !old_die
22559 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22560 && !class_or_namespace_scope_p (context_die)
22561 && debug_info_level > DINFO_LEVEL_TERSE)
22562 old_die = force_decl_die (decl);
22564 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22565 if (origin != NULL)
22567 gcc_assert (!declaration || local_scope_p (context_die));
22569 /* Fixup die_parent for the abstract instance of a nested
22570 inline function. */
22571 if (old_die && old_die->die_parent == NULL)
22572 add_child_die (context_die, old_die);
22574 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22576 /* If we have a DW_AT_abstract_origin we have a working
22577 cached version. */
22578 subr_die = old_die;
22580 else
22582 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22583 add_abstract_origin_attribute (subr_die, origin);
22584 /* This is where the actual code for a cloned function is.
22585 Let's emit linkage name attribute for it. This helps
22586 debuggers to e.g, set breakpoints into
22587 constructors/destructors when the user asks "break
22588 K::K". */
22589 add_linkage_name (subr_die, decl);
22592 /* A cached copy, possibly from early dwarf generation. Reuse as
22593 much as possible. */
22594 else if (old_die)
22596 if (!get_AT_flag (old_die, DW_AT_declaration)
22597 /* We can have a normal definition following an inline one in the
22598 case of redefinition of GNU C extern inlines.
22599 It seems reasonable to use AT_specification in this case. */
22600 && !get_AT (old_die, DW_AT_inline))
22602 /* Detect and ignore this case, where we are trying to output
22603 something we have already output. */
22604 if (get_AT (old_die, DW_AT_low_pc)
22605 || get_AT (old_die, DW_AT_ranges))
22606 return;
22608 /* If we have no location information, this must be a
22609 partially generated DIE from early dwarf generation.
22610 Fall through and generate it. */
22613 /* If the definition comes from the same place as the declaration,
22614 maybe use the old DIE. We always want the DIE for this function
22615 that has the *_pc attributes to be under comp_unit_die so the
22616 debugger can find it. We also need to do this for abstract
22617 instances of inlines, since the spec requires the out-of-line copy
22618 to have the same parent. For local class methods, this doesn't
22619 apply; we just use the old DIE. */
22620 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22621 struct dwarf_file_data * file_index = lookup_filename (s.file);
22622 if ((is_cu_die (old_die->die_parent)
22623 /* This condition fixes the inconsistency/ICE with the
22624 following Fortran test (or some derivative thereof) while
22625 building libgfortran:
22627 module some_m
22628 contains
22629 logical function funky (FLAG)
22630 funky = .true.
22631 end function
22632 end module
22634 || (old_die->die_parent
22635 && old_die->die_parent->die_tag == DW_TAG_module)
22636 || context_die == NULL)
22637 && (DECL_ARTIFICIAL (decl)
22638 /* The location attributes may be in the abstract origin
22639 which in the case of LTO might be not available to
22640 look at. */
22641 || get_AT (old_die, DW_AT_abstract_origin)
22642 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22643 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22644 == (unsigned) s.line)
22645 && (!debug_column_info
22646 || s.column == 0
22647 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22648 == (unsigned) s.column)))))
22650 subr_die = old_die;
22652 /* Clear out the declaration attribute, but leave the
22653 parameters so they can be augmented with location
22654 information later. Unless this was a declaration, in
22655 which case, wipe out the nameless parameters and recreate
22656 them further down. */
22657 if (remove_AT (subr_die, DW_AT_declaration))
22660 remove_AT (subr_die, DW_AT_object_pointer);
22661 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22664 /* Make a specification pointing to the previously built
22665 declaration. */
22666 else
22668 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22669 add_AT_specification (subr_die, old_die);
22670 add_pubname (decl, subr_die);
22671 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22672 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22673 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22674 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22675 if (debug_column_info
22676 && s.column
22677 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22678 != (unsigned) s.column))
22679 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22681 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22682 emit the real type on the definition die. */
22683 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22685 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22686 if (die == auto_die || die == decltype_auto_die)
22687 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22688 TYPE_UNQUALIFIED, false, context_die);
22691 /* When we process the method declaration, we haven't seen
22692 the out-of-class defaulted definition yet, so we have to
22693 recheck now. */
22694 if ((dwarf_version >= 5 || ! dwarf_strict)
22695 && !get_AT (subr_die, DW_AT_defaulted))
22697 int defaulted
22698 = lang_hooks.decls.decl_dwarf_attribute (decl,
22699 DW_AT_defaulted);
22700 if (defaulted != -1)
22702 /* Other values must have been handled before. */
22703 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22704 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22709 /* Create a fresh DIE for anything else. */
22710 else
22712 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22714 if (TREE_PUBLIC (decl))
22715 add_AT_flag (subr_die, DW_AT_external, 1);
22717 add_name_and_src_coords_attributes (subr_die, decl);
22718 add_pubname (decl, subr_die);
22719 if (debug_info_level > DINFO_LEVEL_TERSE)
22721 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22722 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22723 TYPE_UNQUALIFIED, false, context_die);
22726 add_pure_or_virtual_attribute (subr_die, decl);
22727 if (DECL_ARTIFICIAL (decl))
22728 add_AT_flag (subr_die, DW_AT_artificial, 1);
22730 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22731 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22733 add_alignment_attribute (subr_die, decl);
22735 add_accessibility_attribute (subr_die, decl);
22738 /* Unless we have an existing non-declaration DIE, equate the new
22739 DIE. */
22740 if (!old_die || is_declaration_die (old_die))
22741 equate_decl_number_to_die (decl, subr_die);
22743 if (declaration)
22745 if (!old_die || !get_AT (old_die, DW_AT_inline))
22747 add_AT_flag (subr_die, DW_AT_declaration, 1);
22749 /* If this is an explicit function declaration then generate
22750 a DW_AT_explicit attribute. */
22751 if ((dwarf_version >= 3 || !dwarf_strict)
22752 && lang_hooks.decls.decl_dwarf_attribute (decl,
22753 DW_AT_explicit) == 1)
22754 add_AT_flag (subr_die, DW_AT_explicit, 1);
22756 /* If this is a C++11 deleted special function member then generate
22757 a DW_AT_deleted attribute. */
22758 if ((dwarf_version >= 5 || !dwarf_strict)
22759 && lang_hooks.decls.decl_dwarf_attribute (decl,
22760 DW_AT_deleted) == 1)
22761 add_AT_flag (subr_die, DW_AT_deleted, 1);
22763 /* If this is a C++11 defaulted special function member then
22764 generate a DW_AT_defaulted attribute. */
22765 if (dwarf_version >= 5 || !dwarf_strict)
22767 int defaulted
22768 = lang_hooks.decls.decl_dwarf_attribute (decl,
22769 DW_AT_defaulted);
22770 if (defaulted != -1)
22771 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22774 /* If this is a C++11 non-static member function with & ref-qualifier
22775 then generate a DW_AT_reference attribute. */
22776 if ((dwarf_version >= 5 || !dwarf_strict)
22777 && lang_hooks.decls.decl_dwarf_attribute (decl,
22778 DW_AT_reference) == 1)
22779 add_AT_flag (subr_die, DW_AT_reference, 1);
22781 /* If this is a C++11 non-static member function with &&
22782 ref-qualifier then generate a DW_AT_reference attribute. */
22783 if ((dwarf_version >= 5 || !dwarf_strict)
22784 && lang_hooks.decls.decl_dwarf_attribute (decl,
22785 DW_AT_rvalue_reference)
22786 == 1)
22787 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22790 /* For non DECL_EXTERNALs, if range information is available, fill
22791 the DIE with it. */
22792 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22794 HOST_WIDE_INT cfa_fb_offset;
22796 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22798 if (!crtl->has_bb_partition)
22800 dw_fde_ref fde = fun->fde;
22801 if (fde->dw_fde_begin)
22803 /* We have already generated the labels. */
22804 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22805 fde->dw_fde_end, false);
22807 else
22809 /* Create start/end labels and add the range. */
22810 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22811 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22812 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22813 current_function_funcdef_no);
22814 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22815 current_function_funcdef_no);
22816 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22817 false);
22820 #if VMS_DEBUGGING_INFO
22821 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22822 Section 2.3 Prologue and Epilogue Attributes:
22823 When a breakpoint is set on entry to a function, it is generally
22824 desirable for execution to be suspended, not on the very first
22825 instruction of the function, but rather at a point after the
22826 function's frame has been set up, after any language defined local
22827 declaration processing has been completed, and before execution of
22828 the first statement of the function begins. Debuggers generally
22829 cannot properly determine where this point is. Similarly for a
22830 breakpoint set on exit from a function. The prologue and epilogue
22831 attributes allow a compiler to communicate the location(s) to use. */
22834 if (fde->dw_fde_vms_end_prologue)
22835 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22836 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22838 if (fde->dw_fde_vms_begin_epilogue)
22839 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22840 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22842 #endif
22845 else
22847 /* Generate pubnames entries for the split function code ranges. */
22848 dw_fde_ref fde = fun->fde;
22850 if (fde->dw_fde_second_begin)
22852 if (dwarf_version >= 3 || !dwarf_strict)
22854 /* We should use ranges for non-contiguous code section
22855 addresses. Use the actual code range for the initial
22856 section, since the HOT/COLD labels might precede an
22857 alignment offset. */
22858 bool range_list_added = false;
22859 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22860 fde->dw_fde_end, &range_list_added,
22861 false);
22862 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22863 fde->dw_fde_second_end,
22864 &range_list_added, false);
22865 if (range_list_added)
22866 add_ranges (NULL);
22868 else
22870 /* There is no real support in DW2 for this .. so we make
22871 a work-around. First, emit the pub name for the segment
22872 containing the function label. Then make and emit a
22873 simplified subprogram DIE for the second segment with the
22874 name pre-fixed by __hot/cold_sect_of_. We use the same
22875 linkage name for the second die so that gdb will find both
22876 sections when given "b foo". */
22877 const char *name = NULL;
22878 tree decl_name = DECL_NAME (decl);
22879 dw_die_ref seg_die;
22881 /* Do the 'primary' section. */
22882 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22883 fde->dw_fde_end, false);
22885 /* Build a minimal DIE for the secondary section. */
22886 seg_die = new_die (DW_TAG_subprogram,
22887 subr_die->die_parent, decl);
22889 if (TREE_PUBLIC (decl))
22890 add_AT_flag (seg_die, DW_AT_external, 1);
22892 if (decl_name != NULL
22893 && IDENTIFIER_POINTER (decl_name) != NULL)
22895 name = dwarf2_name (decl, 1);
22896 if (! DECL_ARTIFICIAL (decl))
22897 add_src_coords_attributes (seg_die, decl);
22899 add_linkage_name (seg_die, decl);
22901 gcc_assert (name != NULL);
22902 add_pure_or_virtual_attribute (seg_die, decl);
22903 if (DECL_ARTIFICIAL (decl))
22904 add_AT_flag (seg_die, DW_AT_artificial, 1);
22906 name = concat ("__second_sect_of_", name, NULL);
22907 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22908 fde->dw_fde_second_end, false);
22909 add_name_attribute (seg_die, name);
22910 if (want_pubnames ())
22911 add_pubname_string (name, seg_die);
22914 else
22915 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22916 false);
22919 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22921 /* We define the "frame base" as the function's CFA. This is more
22922 convenient for several reasons: (1) It's stable across the prologue
22923 and epilogue, which makes it better than just a frame pointer,
22924 (2) With dwarf3, there exists a one-byte encoding that allows us
22925 to reference the .debug_frame data by proxy, but failing that,
22926 (3) We can at least reuse the code inspection and interpretation
22927 code that determines the CFA position at various points in the
22928 function. */
22929 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22931 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22932 add_AT_loc (subr_die, DW_AT_frame_base, op);
22934 else
22936 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22937 if (list->dw_loc_next)
22938 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22939 else
22940 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22943 /* Compute a displacement from the "steady-state frame pointer" to
22944 the CFA. The former is what all stack slots and argument slots
22945 will reference in the rtl; the latter is what we've told the
22946 debugger about. We'll need to adjust all frame_base references
22947 by this displacement. */
22948 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22950 if (fun->static_chain_decl)
22952 /* DWARF requires here a location expression that computes the
22953 address of the enclosing subprogram's frame base. The machinery
22954 in tree-nested.c is supposed to store this specific address in the
22955 last field of the FRAME record. */
22956 const tree frame_type
22957 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22958 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22960 tree fb_expr
22961 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22962 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22963 fb_expr, fb_decl, NULL_TREE);
22965 add_AT_location_description (subr_die, DW_AT_static_link,
22966 loc_list_from_tree (fb_expr, 0, NULL));
22969 resolve_variable_values ();
22972 /* Generate child dies for template paramaters. */
22973 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22974 gen_generic_params_dies (decl);
22976 /* Now output descriptions of the arguments for this function. This gets
22977 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22978 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22979 `...' at the end of the formal parameter list. In order to find out if
22980 there was a trailing ellipsis or not, we must instead look at the type
22981 associated with the FUNCTION_DECL. This will be a node of type
22982 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22983 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22984 an ellipsis at the end. */
22986 /* In the case where we are describing a mere function declaration, all we
22987 need to do here (and all we *can* do here) is to describe the *types* of
22988 its formal parameters. */
22989 if (debug_info_level <= DINFO_LEVEL_TERSE)
22991 else if (declaration)
22992 gen_formal_types_die (decl, subr_die);
22993 else
22995 /* Generate DIEs to represent all known formal parameters. */
22996 tree parm = DECL_ARGUMENTS (decl);
22997 tree generic_decl = early_dwarf
22998 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22999 tree generic_decl_parm = generic_decl
23000 ? DECL_ARGUMENTS (generic_decl)
23001 : NULL;
23003 /* Now we want to walk the list of parameters of the function and
23004 emit their relevant DIEs.
23006 We consider the case of DECL being an instance of a generic function
23007 as well as it being a normal function.
23009 If DECL is an instance of a generic function we walk the
23010 parameters of the generic function declaration _and_ the parameters of
23011 DECL itself. This is useful because we want to emit specific DIEs for
23012 function parameter packs and those are declared as part of the
23013 generic function declaration. In that particular case,
23014 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23015 That DIE has children DIEs representing the set of arguments
23016 of the pack. Note that the set of pack arguments can be empty.
23017 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23018 children DIE.
23020 Otherwise, we just consider the parameters of DECL. */
23021 while (generic_decl_parm || parm)
23023 if (generic_decl_parm
23024 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23025 gen_formal_parameter_pack_die (generic_decl_parm,
23026 parm, subr_die,
23027 &parm);
23028 else if (parm && !POINTER_BOUNDS_P (parm))
23030 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23032 if (early_dwarf
23033 && parm == DECL_ARGUMENTS (decl)
23034 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23035 && parm_die
23036 && (dwarf_version >= 3 || !dwarf_strict))
23037 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23039 parm = DECL_CHAIN (parm);
23041 else if (parm)
23042 parm = DECL_CHAIN (parm);
23044 if (generic_decl_parm)
23045 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23048 /* Decide whether we need an unspecified_parameters DIE at the end.
23049 There are 2 more cases to do this for: 1) the ansi ... declaration -
23050 this is detectable when the end of the arg list is not a
23051 void_type_node 2) an unprototyped function declaration (not a
23052 definition). This just means that we have no info about the
23053 parameters at all. */
23054 if (early_dwarf)
23056 if (prototype_p (TREE_TYPE (decl)))
23058 /* This is the prototyped case, check for.... */
23059 if (stdarg_p (TREE_TYPE (decl)))
23060 gen_unspecified_parameters_die (decl, subr_die);
23062 else if (DECL_INITIAL (decl) == NULL_TREE)
23063 gen_unspecified_parameters_die (decl, subr_die);
23067 if (subr_die != old_die)
23068 /* Add the calling convention attribute if requested. */
23069 add_calling_convention_attribute (subr_die, decl);
23071 /* Output Dwarf info for all of the stuff within the body of the function
23072 (if it has one - it may be just a declaration).
23074 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23075 a function. This BLOCK actually represents the outermost binding contour
23076 for the function, i.e. the contour in which the function's formal
23077 parameters and labels get declared. Curiously, it appears that the front
23078 end doesn't actually put the PARM_DECL nodes for the current function onto
23079 the BLOCK_VARS list for this outer scope, but are strung off of the
23080 DECL_ARGUMENTS list for the function instead.
23082 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23083 the LABEL_DECL nodes for the function however, and we output DWARF info
23084 for those in decls_for_scope. Just within the `outer_scope' there will be
23085 a BLOCK node representing the function's outermost pair of curly braces,
23086 and any blocks used for the base and member initializers of a C++
23087 constructor function. */
23088 tree outer_scope = DECL_INITIAL (decl);
23089 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23091 int call_site_note_count = 0;
23092 int tail_call_site_note_count = 0;
23094 /* Emit a DW_TAG_variable DIE for a named return value. */
23095 if (DECL_NAME (DECL_RESULT (decl)))
23096 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23098 /* The first time through decls_for_scope we will generate the
23099 DIEs for the locals. The second time, we fill in the
23100 location info. */
23101 decls_for_scope (outer_scope, subr_die);
23103 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23105 struct call_arg_loc_node *ca_loc;
23106 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23108 dw_die_ref die = NULL;
23109 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23110 rtx arg, next_arg;
23112 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23113 ? XEXP (ca_loc->call_arg_loc_note, 0)
23114 : NULL_RTX);
23115 arg; arg = next_arg)
23117 dw_loc_descr_ref reg, val;
23118 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23119 dw_die_ref cdie, tdie = NULL;
23121 next_arg = XEXP (arg, 1);
23122 if (REG_P (XEXP (XEXP (arg, 0), 0))
23123 && next_arg
23124 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23125 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23126 && REGNO (XEXP (XEXP (arg, 0), 0))
23127 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23128 next_arg = XEXP (next_arg, 1);
23129 if (mode == VOIDmode)
23131 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23132 if (mode == VOIDmode)
23133 mode = GET_MODE (XEXP (arg, 0));
23135 if (mode == VOIDmode || mode == BLKmode)
23136 continue;
23137 /* Get dynamic information about call target only if we
23138 have no static information: we cannot generate both
23139 DW_AT_call_origin and DW_AT_call_target
23140 attributes. */
23141 if (ca_loc->symbol_ref == NULL_RTX)
23143 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23145 tloc = XEXP (XEXP (arg, 0), 1);
23146 continue;
23148 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23149 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23151 tlocc = XEXP (XEXP (arg, 0), 1);
23152 continue;
23155 reg = NULL;
23156 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23157 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23158 VAR_INIT_STATUS_INITIALIZED);
23159 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23161 rtx mem = XEXP (XEXP (arg, 0), 0);
23162 reg = mem_loc_descriptor (XEXP (mem, 0),
23163 get_address_mode (mem),
23164 GET_MODE (mem),
23165 VAR_INIT_STATUS_INITIALIZED);
23167 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23168 == DEBUG_PARAMETER_REF)
23170 tree tdecl
23171 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23172 tdie = lookup_decl_die (tdecl);
23173 if (tdie == NULL)
23174 continue;
23176 else
23177 continue;
23178 if (reg == NULL
23179 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23180 != DEBUG_PARAMETER_REF)
23181 continue;
23182 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23183 VOIDmode,
23184 VAR_INIT_STATUS_INITIALIZED);
23185 if (val == NULL)
23186 continue;
23187 if (die == NULL)
23188 die = gen_call_site_die (decl, subr_die, ca_loc);
23189 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23190 NULL_TREE);
23191 if (reg != NULL)
23192 add_AT_loc (cdie, DW_AT_location, reg);
23193 else if (tdie != NULL)
23194 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23195 tdie);
23196 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23197 if (next_arg != XEXP (arg, 1))
23199 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23200 if (mode == VOIDmode)
23201 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23202 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23203 0), 1),
23204 mode, VOIDmode,
23205 VAR_INIT_STATUS_INITIALIZED);
23206 if (val != NULL)
23207 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23208 val);
23211 if (die == NULL
23212 && (ca_loc->symbol_ref || tloc))
23213 die = gen_call_site_die (decl, subr_die, ca_loc);
23214 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23216 dw_loc_descr_ref tval = NULL;
23218 if (tloc != NULL_RTX)
23219 tval = mem_loc_descriptor (tloc,
23220 GET_MODE (tloc) == VOIDmode
23221 ? Pmode : GET_MODE (tloc),
23222 VOIDmode,
23223 VAR_INIT_STATUS_INITIALIZED);
23224 if (tval)
23225 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23226 else if (tlocc != NULL_RTX)
23228 tval = mem_loc_descriptor (tlocc,
23229 GET_MODE (tlocc) == VOIDmode
23230 ? Pmode : GET_MODE (tlocc),
23231 VOIDmode,
23232 VAR_INIT_STATUS_INITIALIZED);
23233 if (tval)
23234 add_AT_loc (die,
23235 dwarf_AT (DW_AT_call_target_clobbered),
23236 tval);
23239 if (die != NULL)
23241 call_site_note_count++;
23242 if (ca_loc->tail_call_p)
23243 tail_call_site_note_count++;
23247 call_arg_locations = NULL;
23248 call_arg_loc_last = NULL;
23249 if (tail_call_site_count >= 0
23250 && tail_call_site_count == tail_call_site_note_count
23251 && (!dwarf_strict || dwarf_version >= 5))
23253 if (call_site_count >= 0
23254 && call_site_count == call_site_note_count)
23255 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23256 else
23257 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23259 call_site_count = -1;
23260 tail_call_site_count = -1;
23263 /* Mark used types after we have created DIEs for the functions scopes. */
23264 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23267 /* Returns a hash value for X (which really is a die_struct). */
23269 hashval_t
23270 block_die_hasher::hash (die_struct *d)
23272 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23275 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23276 as decl_id and die_parent of die_struct Y. */
23278 bool
23279 block_die_hasher::equal (die_struct *x, die_struct *y)
23281 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23284 /* Hold information about markers for inlined entry points. */
23285 struct GTY ((for_user)) inline_entry_data
23287 /* The block that's the inlined_function_outer_scope for an inlined
23288 function. */
23289 tree block;
23291 /* The label at the inlined entry point. */
23292 const char *label_pfx;
23293 unsigned int label_num;
23295 /* The view number to be used as the inlined entry point. */
23296 var_loc_view view;
23299 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23301 typedef tree compare_type;
23302 static inline hashval_t hash (const inline_entry_data *);
23303 static inline bool equal (const inline_entry_data *, const_tree);
23306 /* Hash table routines for inline_entry_data. */
23308 inline hashval_t
23309 inline_entry_data_hasher::hash (const inline_entry_data *data)
23311 return htab_hash_pointer (data->block);
23314 inline bool
23315 inline_entry_data_hasher::equal (const inline_entry_data *data,
23316 const_tree block)
23318 return data->block == block;
23321 /* Inlined entry points pending DIE creation in this compilation unit. */
23323 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23326 /* Return TRUE if DECL, which may have been previously generated as
23327 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23328 true if decl (or its origin) is either an extern declaration or a
23329 class/namespace scoped declaration.
23331 The declare_in_namespace support causes us to get two DIEs for one
23332 variable, both of which are declarations. We want to avoid
23333 considering one to be a specification, so we must test for
23334 DECLARATION and DW_AT_declaration. */
23335 static inline bool
23336 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23338 return (old_die && TREE_STATIC (decl) && !declaration
23339 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23342 /* Return true if DECL is a local static. */
23344 static inline bool
23345 local_function_static (tree decl)
23347 gcc_assert (VAR_P (decl));
23348 return TREE_STATIC (decl)
23349 && DECL_CONTEXT (decl)
23350 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23353 /* Generate a DIE to represent a declared data object.
23354 Either DECL or ORIGIN must be non-null. */
23356 static void
23357 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23359 HOST_WIDE_INT off = 0;
23360 tree com_decl;
23361 tree decl_or_origin = decl ? decl : origin;
23362 tree ultimate_origin;
23363 dw_die_ref var_die;
23364 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23365 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23366 || class_or_namespace_scope_p (context_die));
23367 bool specialization_p = false;
23368 bool no_linkage_name = false;
23370 /* While C++ inline static data members have definitions inside of the
23371 class, force the first DIE to be a declaration, then let gen_member_die
23372 reparent it to the class context and call gen_variable_die again
23373 to create the outside of the class DIE for the definition. */
23374 if (!declaration
23375 && old_die == NULL
23376 && decl
23377 && DECL_CONTEXT (decl)
23378 && TYPE_P (DECL_CONTEXT (decl))
23379 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23381 declaration = true;
23382 if (dwarf_version < 5)
23383 no_linkage_name = true;
23386 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23387 if (decl || ultimate_origin)
23388 origin = ultimate_origin;
23389 com_decl = fortran_common (decl_or_origin, &off);
23391 /* Symbol in common gets emitted as a child of the common block, in the form
23392 of a data member. */
23393 if (com_decl)
23395 dw_die_ref com_die;
23396 dw_loc_list_ref loc = NULL;
23397 die_node com_die_arg;
23399 var_die = lookup_decl_die (decl_or_origin);
23400 if (var_die)
23402 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23404 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23405 if (loc)
23407 if (off)
23409 /* Optimize the common case. */
23410 if (single_element_loc_list_p (loc)
23411 && loc->expr->dw_loc_opc == DW_OP_addr
23412 && loc->expr->dw_loc_next == NULL
23413 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23414 == SYMBOL_REF)
23416 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23417 loc->expr->dw_loc_oprnd1.v.val_addr
23418 = plus_constant (GET_MODE (x), x , off);
23420 else
23421 loc_list_plus_const (loc, off);
23423 add_AT_location_description (var_die, DW_AT_location, loc);
23424 remove_AT (var_die, DW_AT_declaration);
23427 return;
23430 if (common_block_die_table == NULL)
23431 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23433 com_die_arg.decl_id = DECL_UID (com_decl);
23434 com_die_arg.die_parent = context_die;
23435 com_die = common_block_die_table->find (&com_die_arg);
23436 if (! early_dwarf)
23437 loc = loc_list_from_tree (com_decl, 2, NULL);
23438 if (com_die == NULL)
23440 const char *cnam
23441 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23442 die_node **slot;
23444 com_die = new_die (DW_TAG_common_block, context_die, decl);
23445 add_name_and_src_coords_attributes (com_die, com_decl);
23446 if (loc)
23448 add_AT_location_description (com_die, DW_AT_location, loc);
23449 /* Avoid sharing the same loc descriptor between
23450 DW_TAG_common_block and DW_TAG_variable. */
23451 loc = loc_list_from_tree (com_decl, 2, NULL);
23453 else if (DECL_EXTERNAL (decl_or_origin))
23454 add_AT_flag (com_die, DW_AT_declaration, 1);
23455 if (want_pubnames ())
23456 add_pubname_string (cnam, com_die); /* ??? needed? */
23457 com_die->decl_id = DECL_UID (com_decl);
23458 slot = common_block_die_table->find_slot (com_die, INSERT);
23459 *slot = com_die;
23461 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23463 add_AT_location_description (com_die, DW_AT_location, loc);
23464 loc = loc_list_from_tree (com_decl, 2, NULL);
23465 remove_AT (com_die, DW_AT_declaration);
23467 var_die = new_die (DW_TAG_variable, com_die, decl);
23468 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23469 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23470 decl_quals (decl_or_origin), false,
23471 context_die);
23472 add_alignment_attribute (var_die, decl);
23473 add_AT_flag (var_die, DW_AT_external, 1);
23474 if (loc)
23476 if (off)
23478 /* Optimize the common case. */
23479 if (single_element_loc_list_p (loc)
23480 && loc->expr->dw_loc_opc == DW_OP_addr
23481 && loc->expr->dw_loc_next == NULL
23482 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23484 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23485 loc->expr->dw_loc_oprnd1.v.val_addr
23486 = plus_constant (GET_MODE (x), x, off);
23488 else
23489 loc_list_plus_const (loc, off);
23491 add_AT_location_description (var_die, DW_AT_location, loc);
23493 else if (DECL_EXTERNAL (decl_or_origin))
23494 add_AT_flag (var_die, DW_AT_declaration, 1);
23495 if (decl)
23496 equate_decl_number_to_die (decl, var_die);
23497 return;
23500 if (old_die)
23502 if (declaration)
23504 /* A declaration that has been previously dumped, needs no
23505 further annotations, since it doesn't need location on
23506 the second pass. */
23507 return;
23509 else if (decl_will_get_specification_p (old_die, decl, declaration)
23510 && !get_AT (old_die, DW_AT_specification))
23512 /* Fall-thru so we can make a new variable die along with a
23513 DW_AT_specification. */
23515 else if (origin && old_die->die_parent != context_die)
23517 /* If we will be creating an inlined instance, we need a
23518 new DIE that will get annotated with
23519 DW_AT_abstract_origin. */
23520 gcc_assert (!DECL_ABSTRACT_P (decl));
23522 else
23524 /* If a DIE was dumped early, it still needs location info.
23525 Skip to where we fill the location bits. */
23526 var_die = old_die;
23528 /* ??? In LTRANS we cannot annotate early created variably
23529 modified type DIEs without copying them and adjusting all
23530 references to them. Thus we dumped them again, also add a
23531 reference to them. */
23532 tree type = TREE_TYPE (decl_or_origin);
23533 if (in_lto_p
23534 && variably_modified_type_p
23535 (type, decl_function_context (decl_or_origin)))
23537 if (decl_by_reference_p (decl_or_origin))
23538 add_type_attribute (var_die, TREE_TYPE (type),
23539 TYPE_UNQUALIFIED, false, context_die);
23540 else
23541 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23542 false, context_die);
23545 goto gen_variable_die_location;
23549 /* For static data members, the declaration in the class is supposed
23550 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23551 also in DWARF2; the specification should still be DW_TAG_variable
23552 referencing the DW_TAG_member DIE. */
23553 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23554 var_die = new_die (DW_TAG_member, context_die, decl);
23555 else
23556 var_die = new_die (DW_TAG_variable, context_die, decl);
23558 if (origin != NULL)
23559 add_abstract_origin_attribute (var_die, origin);
23561 /* Loop unrolling can create multiple blocks that refer to the same
23562 static variable, so we must test for the DW_AT_declaration flag.
23564 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23565 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23566 sharing them.
23568 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23569 else if (decl_will_get_specification_p (old_die, decl, declaration))
23571 /* This is a definition of a C++ class level static. */
23572 add_AT_specification (var_die, old_die);
23573 specialization_p = true;
23574 if (DECL_NAME (decl))
23576 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23577 struct dwarf_file_data * file_index = lookup_filename (s.file);
23579 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23580 add_AT_file (var_die, DW_AT_decl_file, file_index);
23582 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23583 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23585 if (debug_column_info
23586 && s.column
23587 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23588 != (unsigned) s.column))
23589 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23591 if (old_die->die_tag == DW_TAG_member)
23592 add_linkage_name (var_die, decl);
23595 else
23596 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23598 if ((origin == NULL && !specialization_p)
23599 || (origin != NULL
23600 && !DECL_ABSTRACT_P (decl_or_origin)
23601 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23602 decl_function_context
23603 (decl_or_origin))))
23605 tree type = TREE_TYPE (decl_or_origin);
23607 if (decl_by_reference_p (decl_or_origin))
23608 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23609 context_die);
23610 else
23611 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23612 context_die);
23615 if (origin == NULL && !specialization_p)
23617 if (TREE_PUBLIC (decl))
23618 add_AT_flag (var_die, DW_AT_external, 1);
23620 if (DECL_ARTIFICIAL (decl))
23621 add_AT_flag (var_die, DW_AT_artificial, 1);
23623 add_alignment_attribute (var_die, decl);
23625 add_accessibility_attribute (var_die, decl);
23628 if (declaration)
23629 add_AT_flag (var_die, DW_AT_declaration, 1);
23631 if (decl && (DECL_ABSTRACT_P (decl)
23632 || !old_die || is_declaration_die (old_die)))
23633 equate_decl_number_to_die (decl, var_die);
23635 gen_variable_die_location:
23636 if (! declaration
23637 && (! DECL_ABSTRACT_P (decl_or_origin)
23638 /* Local static vars are shared between all clones/inlines,
23639 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23640 already set. */
23641 || (VAR_P (decl_or_origin)
23642 && TREE_STATIC (decl_or_origin)
23643 && DECL_RTL_SET_P (decl_or_origin))))
23645 if (early_dwarf)
23646 add_pubname (decl_or_origin, var_die);
23647 else
23648 add_location_or_const_value_attribute (var_die, decl_or_origin,
23649 decl == NULL);
23651 else
23652 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23654 if ((dwarf_version >= 4 || !dwarf_strict)
23655 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23656 DW_AT_const_expr) == 1
23657 && !get_AT (var_die, DW_AT_const_expr)
23658 && !specialization_p)
23659 add_AT_flag (var_die, DW_AT_const_expr, 1);
23661 if (!dwarf_strict)
23663 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23664 DW_AT_inline);
23665 if (inl != -1
23666 && !get_AT (var_die, DW_AT_inline)
23667 && !specialization_p)
23668 add_AT_unsigned (var_die, DW_AT_inline, inl);
23672 /* Generate a DIE to represent a named constant. */
23674 static void
23675 gen_const_die (tree decl, dw_die_ref context_die)
23677 dw_die_ref const_die;
23678 tree type = TREE_TYPE (decl);
23680 const_die = lookup_decl_die (decl);
23681 if (const_die)
23682 return;
23684 const_die = new_die (DW_TAG_constant, context_die, decl);
23685 equate_decl_number_to_die (decl, const_die);
23686 add_name_and_src_coords_attributes (const_die, decl);
23687 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23688 if (TREE_PUBLIC (decl))
23689 add_AT_flag (const_die, DW_AT_external, 1);
23690 if (DECL_ARTIFICIAL (decl))
23691 add_AT_flag (const_die, DW_AT_artificial, 1);
23692 tree_add_const_value_attribute_for_decl (const_die, decl);
23695 /* Generate a DIE to represent a label identifier. */
23697 static void
23698 gen_label_die (tree decl, dw_die_ref context_die)
23700 tree origin = decl_ultimate_origin (decl);
23701 dw_die_ref lbl_die = lookup_decl_die (decl);
23702 rtx insn;
23703 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23705 if (!lbl_die)
23707 lbl_die = new_die (DW_TAG_label, context_die, decl);
23708 equate_decl_number_to_die (decl, lbl_die);
23710 if (origin != NULL)
23711 add_abstract_origin_attribute (lbl_die, origin);
23712 else
23713 add_name_and_src_coords_attributes (lbl_die, decl);
23716 if (DECL_ABSTRACT_P (decl))
23717 equate_decl_number_to_die (decl, lbl_die);
23718 else if (! early_dwarf)
23720 insn = DECL_RTL_IF_SET (decl);
23722 /* Deleted labels are programmer specified labels which have been
23723 eliminated because of various optimizations. We still emit them
23724 here so that it is possible to put breakpoints on them. */
23725 if (insn
23726 && (LABEL_P (insn)
23727 || ((NOTE_P (insn)
23728 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23730 /* When optimization is enabled (via -O) some parts of the compiler
23731 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23732 represent source-level labels which were explicitly declared by
23733 the user. This really shouldn't be happening though, so catch
23734 it if it ever does happen. */
23735 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23737 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23738 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23740 else if (insn
23741 && NOTE_P (insn)
23742 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23743 && CODE_LABEL_NUMBER (insn) != -1)
23745 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23746 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23751 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23752 attributes to the DIE for a block STMT, to describe where the inlined
23753 function was called from. This is similar to add_src_coords_attributes. */
23755 static inline void
23756 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23758 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23760 if (dwarf_version >= 3 || !dwarf_strict)
23762 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23763 add_AT_unsigned (die, DW_AT_call_line, s.line);
23764 if (debug_column_info && s.column)
23765 add_AT_unsigned (die, DW_AT_call_column, s.column);
23770 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23771 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23773 static inline void
23774 add_high_low_attributes (tree stmt, dw_die_ref die)
23776 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23778 if (inline_entry_data **iedp
23779 = !inline_entry_data_table ? NULL
23780 : inline_entry_data_table->find_slot_with_hash (stmt,
23781 htab_hash_pointer (stmt),
23782 NO_INSERT))
23784 inline_entry_data *ied = *iedp;
23785 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23786 gcc_assert (debug_inline_points);
23787 gcc_assert (inlined_function_outer_scope_p (stmt));
23789 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23790 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23792 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
23793 && !dwarf_strict)
23795 if (!output_asm_line_debug_info ())
23796 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23797 else
23799 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23800 /* FIXME: this will resolve to a small number. Could we
23801 possibly emit smaller data? Ideally we'd emit a
23802 uleb128, but that would make the size of DIEs
23803 impossible for the compiler to compute, since it's
23804 the assembler that computes the value of the view
23805 label in this case. Ideally, we'd have a single form
23806 encompassing both the address and the view, and
23807 indirecting them through a table might make things
23808 easier, but even that would be more wasteful,
23809 space-wise, than what we have now. */
23810 add_AT_lbl_id (die, DW_AT_GNU_entry_view, label);
23814 inline_entry_data_table->clear_slot (iedp);
23817 if (BLOCK_FRAGMENT_CHAIN (stmt)
23818 && (dwarf_version >= 3 || !dwarf_strict))
23820 tree chain, superblock = NULL_TREE;
23821 dw_die_ref pdie;
23822 dw_attr_node *attr = NULL;
23824 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
23826 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23827 BLOCK_NUMBER (stmt));
23828 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23831 /* Optimize duplicate .debug_ranges lists or even tails of
23832 lists. If this BLOCK has same ranges as its supercontext,
23833 lookup DW_AT_ranges attribute in the supercontext (and
23834 recursively so), verify that the ranges_table contains the
23835 right values and use it instead of adding a new .debug_range. */
23836 for (chain = stmt, pdie = die;
23837 BLOCK_SAME_RANGE (chain);
23838 chain = BLOCK_SUPERCONTEXT (chain))
23840 dw_attr_node *new_attr;
23842 pdie = pdie->die_parent;
23843 if (pdie == NULL)
23844 break;
23845 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23846 break;
23847 new_attr = get_AT (pdie, DW_AT_ranges);
23848 if (new_attr == NULL
23849 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23850 break;
23851 attr = new_attr;
23852 superblock = BLOCK_SUPERCONTEXT (chain);
23854 if (attr != NULL
23855 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23856 == BLOCK_NUMBER (superblock))
23857 && BLOCK_FRAGMENT_CHAIN (superblock))
23859 unsigned long off = attr->dw_attr_val.v.val_offset;
23860 unsigned long supercnt = 0, thiscnt = 0;
23861 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23862 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23864 ++supercnt;
23865 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23866 == BLOCK_NUMBER (chain));
23868 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23869 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23870 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23871 ++thiscnt;
23872 gcc_assert (supercnt >= thiscnt);
23873 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23874 false);
23875 note_rnglist_head (off + supercnt - thiscnt);
23876 return;
23879 unsigned int offset = add_ranges (stmt, true);
23880 add_AT_range_list (die, DW_AT_ranges, offset, false);
23881 note_rnglist_head (offset);
23883 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23884 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23887 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23888 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23889 chain = BLOCK_FRAGMENT_CHAIN (chain);
23891 while (chain);
23892 add_ranges (NULL);
23894 else
23896 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23897 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23898 BLOCK_NUMBER (stmt));
23899 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23900 BLOCK_NUMBER (stmt));
23901 add_AT_low_high_pc (die, label, label_high, false);
23905 /* Generate a DIE for a lexical block. */
23907 static void
23908 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23910 dw_die_ref old_die = BLOCK_DIE (stmt);
23911 dw_die_ref stmt_die = NULL;
23912 if (!old_die)
23914 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23915 BLOCK_DIE (stmt) = stmt_die;
23918 if (BLOCK_ABSTRACT (stmt))
23920 if (old_die)
23922 /* This must have been generated early and it won't even
23923 need location information since it's a DW_AT_inline
23924 function. */
23925 if (flag_checking)
23926 for (dw_die_ref c = context_die; c; c = c->die_parent)
23927 if (c->die_tag == DW_TAG_inlined_subroutine
23928 || c->die_tag == DW_TAG_subprogram)
23930 gcc_assert (get_AT (c, DW_AT_inline));
23931 break;
23933 return;
23936 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23938 /* If this is an inlined instance, create a new lexical die for
23939 anything below to attach DW_AT_abstract_origin to. */
23940 if (old_die)
23942 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23943 BLOCK_DIE (stmt) = stmt_die;
23944 old_die = NULL;
23947 tree origin = block_ultimate_origin (stmt);
23948 if (origin != NULL_TREE && origin != stmt)
23949 add_abstract_origin_attribute (stmt_die, origin);
23952 if (old_die)
23953 stmt_die = old_die;
23955 /* A non abstract block whose blocks have already been reordered
23956 should have the instruction range for this block. If so, set the
23957 high/low attributes. */
23958 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23960 gcc_assert (stmt_die);
23961 add_high_low_attributes (stmt, stmt_die);
23964 decls_for_scope (stmt, stmt_die);
23967 /* Generate a DIE for an inlined subprogram. */
23969 static void
23970 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23972 tree decl;
23974 /* The instance of function that is effectively being inlined shall not
23975 be abstract. */
23976 gcc_assert (! BLOCK_ABSTRACT (stmt));
23978 decl = block_ultimate_origin (stmt);
23980 /* Make sure any inlined functions are known to be inlineable. */
23981 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23982 || cgraph_function_possibly_inlined_p (decl));
23984 if (! BLOCK_ABSTRACT (stmt))
23986 dw_die_ref subr_die
23987 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23989 if (call_arg_locations || debug_inline_points)
23990 BLOCK_DIE (stmt) = subr_die;
23991 add_abstract_origin_attribute (subr_die, decl);
23992 if (TREE_ASM_WRITTEN (stmt))
23993 add_high_low_attributes (stmt, subr_die);
23994 add_call_src_coords_attributes (stmt, subr_die);
23996 decls_for_scope (stmt, subr_die);
24000 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24001 the comment for VLR_CONTEXT. */
24003 static void
24004 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24006 dw_die_ref decl_die;
24008 if (TREE_TYPE (decl) == error_mark_node)
24009 return;
24011 decl_die = new_die (DW_TAG_member, context_die, decl);
24012 add_name_and_src_coords_attributes (decl_die, decl);
24013 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24014 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24015 context_die);
24017 if (DECL_BIT_FIELD_TYPE (decl))
24019 add_byte_size_attribute (decl_die, decl);
24020 add_bit_size_attribute (decl_die, decl);
24021 add_bit_offset_attribute (decl_die, decl, ctx);
24024 add_alignment_attribute (decl_die, decl);
24026 /* If we have a variant part offset, then we are supposed to process a member
24027 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24028 trees. */
24029 gcc_assert (ctx->variant_part_offset == NULL_TREE
24030 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24031 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24032 add_data_member_location_attribute (decl_die, decl, ctx);
24034 if (DECL_ARTIFICIAL (decl))
24035 add_AT_flag (decl_die, DW_AT_artificial, 1);
24037 add_accessibility_attribute (decl_die, decl);
24039 /* Equate decl number to die, so that we can look up this decl later on. */
24040 equate_decl_number_to_die (decl, decl_die);
24043 /* Generate a DIE for a pointer to a member type. TYPE can be an
24044 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24045 pointer to member function. */
24047 static void
24048 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24050 if (lookup_type_die (type))
24051 return;
24053 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24054 scope_die_for (type, context_die), type);
24056 equate_type_number_to_die (type, ptr_die);
24057 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24058 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24059 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24060 context_die);
24061 add_alignment_attribute (ptr_die, type);
24063 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24064 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24066 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24067 add_AT_loc (ptr_die, DW_AT_use_location, op);
24071 static char *producer_string;
24073 /* Return a heap allocated producer string including command line options
24074 if -grecord-gcc-switches. */
24076 static char *
24077 gen_producer_string (void)
24079 size_t j;
24080 auto_vec<const char *> switches;
24081 const char *language_string = lang_hooks.name;
24082 char *producer, *tail;
24083 const char *p;
24084 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24085 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24087 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24088 switch (save_decoded_options[j].opt_index)
24090 case OPT_o:
24091 case OPT_d:
24092 case OPT_dumpbase:
24093 case OPT_dumpdir:
24094 case OPT_auxbase:
24095 case OPT_auxbase_strip:
24096 case OPT_quiet:
24097 case OPT_version:
24098 case OPT_v:
24099 case OPT_w:
24100 case OPT_L:
24101 case OPT_D:
24102 case OPT_I:
24103 case OPT_U:
24104 case OPT_SPECIAL_unknown:
24105 case OPT_SPECIAL_ignore:
24106 case OPT_SPECIAL_program_name:
24107 case OPT_SPECIAL_input_file:
24108 case OPT_grecord_gcc_switches:
24109 case OPT__output_pch_:
24110 case OPT_fdiagnostics_show_location_:
24111 case OPT_fdiagnostics_show_option:
24112 case OPT_fdiagnostics_show_caret:
24113 case OPT_fdiagnostics_color_:
24114 case OPT_fverbose_asm:
24115 case OPT____:
24116 case OPT__sysroot_:
24117 case OPT_nostdinc:
24118 case OPT_nostdinc__:
24119 case OPT_fpreprocessed:
24120 case OPT_fltrans_output_list_:
24121 case OPT_fresolution_:
24122 case OPT_fdebug_prefix_map_:
24123 case OPT_fmacro_prefix_map_:
24124 case OPT_ffile_prefix_map_:
24125 case OPT_fcompare_debug:
24126 /* Ignore these. */
24127 continue;
24128 default:
24129 if (cl_options[save_decoded_options[j].opt_index].flags
24130 & CL_NO_DWARF_RECORD)
24131 continue;
24132 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24133 == '-');
24134 switch (save_decoded_options[j].canonical_option[0][1])
24136 case 'M':
24137 case 'i':
24138 case 'W':
24139 continue;
24140 case 'f':
24141 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24142 "dump", 4) == 0)
24143 continue;
24144 break;
24145 default:
24146 break;
24148 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24149 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24150 break;
24153 producer = XNEWVEC (char, plen + 1 + len + 1);
24154 tail = producer;
24155 sprintf (tail, "%s %s", language_string, version_string);
24156 tail += plen;
24158 FOR_EACH_VEC_ELT (switches, j, p)
24160 len = strlen (p);
24161 *tail = ' ';
24162 memcpy (tail + 1, p, len);
24163 tail += len + 1;
24166 *tail = '\0';
24167 return producer;
24170 /* Given a C and/or C++ language/version string return the "highest".
24171 C++ is assumed to be "higher" than C in this case. Used for merging
24172 LTO translation unit languages. */
24173 static const char *
24174 highest_c_language (const char *lang1, const char *lang2)
24176 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24177 return "GNU C++17";
24178 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24179 return "GNU C++14";
24180 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24181 return "GNU C++11";
24182 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24183 return "GNU C++98";
24185 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24186 return "GNU C17";
24187 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24188 return "GNU C11";
24189 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24190 return "GNU C99";
24191 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24192 return "GNU C89";
24194 gcc_unreachable ();
24198 /* Generate the DIE for the compilation unit. */
24200 static dw_die_ref
24201 gen_compile_unit_die (const char *filename)
24203 dw_die_ref die;
24204 const char *language_string = lang_hooks.name;
24205 int language;
24207 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24209 if (filename)
24211 add_name_attribute (die, filename);
24212 /* Don't add cwd for <built-in>. */
24213 if (filename[0] != '<')
24214 add_comp_dir_attribute (die);
24217 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24219 /* If our producer is LTO try to figure out a common language to use
24220 from the global list of translation units. */
24221 if (strcmp (language_string, "GNU GIMPLE") == 0)
24223 unsigned i;
24224 tree t;
24225 const char *common_lang = NULL;
24227 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24229 if (!TRANSLATION_UNIT_LANGUAGE (t))
24230 continue;
24231 if (!common_lang)
24232 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24233 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24235 else if (strncmp (common_lang, "GNU C", 5) == 0
24236 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24237 /* Mixing C and C++ is ok, use C++ in that case. */
24238 common_lang = highest_c_language (common_lang,
24239 TRANSLATION_UNIT_LANGUAGE (t));
24240 else
24242 /* Fall back to C. */
24243 common_lang = NULL;
24244 break;
24248 if (common_lang)
24249 language_string = common_lang;
24252 language = DW_LANG_C;
24253 if (strncmp (language_string, "GNU C", 5) == 0
24254 && ISDIGIT (language_string[5]))
24256 language = DW_LANG_C89;
24257 if (dwarf_version >= 3 || !dwarf_strict)
24259 if (strcmp (language_string, "GNU C89") != 0)
24260 language = DW_LANG_C99;
24262 if (dwarf_version >= 5 /* || !dwarf_strict */)
24263 if (strcmp (language_string, "GNU C11") == 0
24264 || strcmp (language_string, "GNU C17") == 0)
24265 language = DW_LANG_C11;
24268 else if (strncmp (language_string, "GNU C++", 7) == 0)
24270 language = DW_LANG_C_plus_plus;
24271 if (dwarf_version >= 5 /* || !dwarf_strict */)
24273 if (strcmp (language_string, "GNU C++11") == 0)
24274 language = DW_LANG_C_plus_plus_11;
24275 else if (strcmp (language_string, "GNU C++14") == 0)
24276 language = DW_LANG_C_plus_plus_14;
24277 else if (strcmp (language_string, "GNU C++17") == 0)
24278 /* For now. */
24279 language = DW_LANG_C_plus_plus_14;
24282 else if (strcmp (language_string, "GNU F77") == 0)
24283 language = DW_LANG_Fortran77;
24284 else if (dwarf_version >= 3 || !dwarf_strict)
24286 if (strcmp (language_string, "GNU Ada") == 0)
24287 language = DW_LANG_Ada95;
24288 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24290 language = DW_LANG_Fortran95;
24291 if (dwarf_version >= 5 /* || !dwarf_strict */)
24293 if (strcmp (language_string, "GNU Fortran2003") == 0)
24294 language = DW_LANG_Fortran03;
24295 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24296 language = DW_LANG_Fortran08;
24299 else if (strcmp (language_string, "GNU Objective-C") == 0)
24300 language = DW_LANG_ObjC;
24301 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24302 language = DW_LANG_ObjC_plus_plus;
24303 else if (dwarf_version >= 5 || !dwarf_strict)
24305 if (strcmp (language_string, "GNU Go") == 0)
24306 language = DW_LANG_Go;
24309 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24310 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24311 language = DW_LANG_Fortran90;
24313 add_AT_unsigned (die, DW_AT_language, language);
24315 switch (language)
24317 case DW_LANG_Fortran77:
24318 case DW_LANG_Fortran90:
24319 case DW_LANG_Fortran95:
24320 case DW_LANG_Fortran03:
24321 case DW_LANG_Fortran08:
24322 /* Fortran has case insensitive identifiers and the front-end
24323 lowercases everything. */
24324 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24325 break;
24326 default:
24327 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24328 break;
24330 return die;
24333 /* Generate the DIE for a base class. */
24335 static void
24336 gen_inheritance_die (tree binfo, tree access, tree type,
24337 dw_die_ref context_die)
24339 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24340 struct vlr_context ctx = { type, NULL };
24342 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24343 context_die);
24344 add_data_member_location_attribute (die, binfo, &ctx);
24346 if (BINFO_VIRTUAL_P (binfo))
24347 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24349 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24350 children, otherwise the default is DW_ACCESS_public. In DWARF2
24351 the default has always been DW_ACCESS_private. */
24352 if (access == access_public_node)
24354 if (dwarf_version == 2
24355 || context_die->die_tag == DW_TAG_class_type)
24356 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24358 else if (access == access_protected_node)
24359 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24360 else if (dwarf_version > 2
24361 && context_die->die_tag != DW_TAG_class_type)
24362 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24365 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24366 structure. */
24367 static bool
24368 is_variant_part (tree decl)
24370 return (TREE_CODE (decl) == FIELD_DECL
24371 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24374 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24375 return the FIELD_DECL. Return NULL_TREE otherwise. */
24377 static tree
24378 analyze_discr_in_predicate (tree operand, tree struct_type)
24380 bool continue_stripping = true;
24381 while (continue_stripping)
24382 switch (TREE_CODE (operand))
24384 CASE_CONVERT:
24385 operand = TREE_OPERAND (operand, 0);
24386 break;
24387 default:
24388 continue_stripping = false;
24389 break;
24392 /* Match field access to members of struct_type only. */
24393 if (TREE_CODE (operand) == COMPONENT_REF
24394 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24395 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24396 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24397 return TREE_OPERAND (operand, 1);
24398 else
24399 return NULL_TREE;
24402 /* Check that SRC is a constant integer that can be represented as a native
24403 integer constant (either signed or unsigned). If so, store it into DEST and
24404 return true. Return false otherwise. */
24406 static bool
24407 get_discr_value (tree src, dw_discr_value *dest)
24409 tree discr_type = TREE_TYPE (src);
24411 if (lang_hooks.types.get_debug_type)
24413 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24414 if (debug_type != NULL)
24415 discr_type = debug_type;
24418 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24419 return false;
24421 /* Signedness can vary between the original type and the debug type. This
24422 can happen for character types in Ada for instance: the character type
24423 used for code generation can be signed, to be compatible with the C one,
24424 but from a debugger point of view, it must be unsigned. */
24425 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24426 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24428 if (is_orig_unsigned != is_debug_unsigned)
24429 src = fold_convert (discr_type, src);
24431 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24432 return false;
24434 dest->pos = is_debug_unsigned;
24435 if (is_debug_unsigned)
24436 dest->v.uval = tree_to_uhwi (src);
24437 else
24438 dest->v.sval = tree_to_shwi (src);
24440 return true;
24443 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24444 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24445 store NULL_TREE in DISCR_DECL. Otherwise:
24447 - store the discriminant field in STRUCT_TYPE that controls the variant
24448 part to *DISCR_DECL
24450 - put in *DISCR_LISTS_P an array where for each variant, the item
24451 represents the corresponding matching list of discriminant values.
24453 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24454 the above array.
24456 Note that when the array is allocated (i.e. when the analysis is
24457 successful), it is up to the caller to free the array. */
24459 static void
24460 analyze_variants_discr (tree variant_part_decl,
24461 tree struct_type,
24462 tree *discr_decl,
24463 dw_discr_list_ref **discr_lists_p,
24464 unsigned *discr_lists_length)
24466 tree variant_part_type = TREE_TYPE (variant_part_decl);
24467 tree variant;
24468 dw_discr_list_ref *discr_lists;
24469 unsigned i;
24471 /* Compute how many variants there are in this variant part. */
24472 *discr_lists_length = 0;
24473 for (variant = TYPE_FIELDS (variant_part_type);
24474 variant != NULL_TREE;
24475 variant = DECL_CHAIN (variant))
24476 ++*discr_lists_length;
24478 *discr_decl = NULL_TREE;
24479 *discr_lists_p
24480 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24481 sizeof (**discr_lists_p));
24482 discr_lists = *discr_lists_p;
24484 /* And then analyze all variants to extract discriminant information for all
24485 of them. This analysis is conservative: as soon as we detect something we
24486 do not support, abort everything and pretend we found nothing. */
24487 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24488 variant != NULL_TREE;
24489 variant = DECL_CHAIN (variant), ++i)
24491 tree match_expr = DECL_QUALIFIER (variant);
24493 /* Now, try to analyze the predicate and deduce a discriminant for
24494 it. */
24495 if (match_expr == boolean_true_node)
24496 /* Typically happens for the default variant: it matches all cases that
24497 previous variants rejected. Don't output any matching value for
24498 this one. */
24499 continue;
24501 /* The following loop tries to iterate over each discriminant
24502 possibility: single values or ranges. */
24503 while (match_expr != NULL_TREE)
24505 tree next_round_match_expr;
24506 tree candidate_discr = NULL_TREE;
24507 dw_discr_list_ref new_node = NULL;
24509 /* Possibilities are matched one after the other by nested
24510 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24511 continue with the rest at next iteration. */
24512 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24514 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24515 match_expr = TREE_OPERAND (match_expr, 1);
24517 else
24518 next_round_match_expr = NULL_TREE;
24520 if (match_expr == boolean_false_node)
24521 /* This sub-expression matches nothing: just wait for the next
24522 one. */
24525 else if (TREE_CODE (match_expr) == EQ_EXPR)
24527 /* We are matching: <discr_field> == <integer_cst>
24528 This sub-expression matches a single value. */
24529 tree integer_cst = TREE_OPERAND (match_expr, 1);
24531 candidate_discr
24532 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24533 struct_type);
24535 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24536 if (!get_discr_value (integer_cst,
24537 &new_node->dw_discr_lower_bound))
24538 goto abort;
24539 new_node->dw_discr_range = false;
24542 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24544 /* We are matching:
24545 <discr_field> > <integer_cst>
24546 && <discr_field> < <integer_cst>.
24547 This sub-expression matches the range of values between the
24548 two matched integer constants. Note that comparisons can be
24549 inclusive or exclusive. */
24550 tree candidate_discr_1, candidate_discr_2;
24551 tree lower_cst, upper_cst;
24552 bool lower_cst_included, upper_cst_included;
24553 tree lower_op = TREE_OPERAND (match_expr, 0);
24554 tree upper_op = TREE_OPERAND (match_expr, 1);
24556 /* When the comparison is exclusive, the integer constant is not
24557 the discriminant range bound we are looking for: we will have
24558 to increment or decrement it. */
24559 if (TREE_CODE (lower_op) == GE_EXPR)
24560 lower_cst_included = true;
24561 else if (TREE_CODE (lower_op) == GT_EXPR)
24562 lower_cst_included = false;
24563 else
24564 goto abort;
24566 if (TREE_CODE (upper_op) == LE_EXPR)
24567 upper_cst_included = true;
24568 else if (TREE_CODE (upper_op) == LT_EXPR)
24569 upper_cst_included = false;
24570 else
24571 goto abort;
24573 /* Extract the discriminant from the first operand and check it
24574 is consistant with the same analysis in the second
24575 operand. */
24576 candidate_discr_1
24577 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24578 struct_type);
24579 candidate_discr_2
24580 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24581 struct_type);
24582 if (candidate_discr_1 == candidate_discr_2)
24583 candidate_discr = candidate_discr_1;
24584 else
24585 goto abort;
24587 /* Extract bounds from both. */
24588 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24589 lower_cst = TREE_OPERAND (lower_op, 1);
24590 upper_cst = TREE_OPERAND (upper_op, 1);
24592 if (!lower_cst_included)
24593 lower_cst
24594 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24595 build_int_cst (TREE_TYPE (lower_cst), 1));
24596 if (!upper_cst_included)
24597 upper_cst
24598 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24599 build_int_cst (TREE_TYPE (upper_cst), 1));
24601 if (!get_discr_value (lower_cst,
24602 &new_node->dw_discr_lower_bound)
24603 || !get_discr_value (upper_cst,
24604 &new_node->dw_discr_upper_bound))
24605 goto abort;
24607 new_node->dw_discr_range = true;
24610 else
24611 /* Unsupported sub-expression: we cannot determine the set of
24612 matching discriminant values. Abort everything. */
24613 goto abort;
24615 /* If the discriminant info is not consistant with what we saw so
24616 far, consider the analysis failed and abort everything. */
24617 if (candidate_discr == NULL_TREE
24618 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24619 goto abort;
24620 else
24621 *discr_decl = candidate_discr;
24623 if (new_node != NULL)
24625 new_node->dw_discr_next = discr_lists[i];
24626 discr_lists[i] = new_node;
24628 match_expr = next_round_match_expr;
24632 /* If we reach this point, we could match everything we were interested
24633 in. */
24634 return;
24636 abort:
24637 /* Clean all data structure and return no result. */
24638 free (*discr_lists_p);
24639 *discr_lists_p = NULL;
24640 *discr_decl = NULL_TREE;
24643 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24644 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24645 under CONTEXT_DIE.
24647 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24648 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24649 this type, which are record types, represent the available variants and each
24650 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24651 values are inferred from these attributes.
24653 In trees, the offsets for the fields inside these sub-records are relative
24654 to the variant part itself, whereas the corresponding DIEs should have
24655 offset attributes that are relative to the embedding record base address.
24656 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24657 must be an expression that computes the offset of the variant part to
24658 describe in DWARF. */
24660 static void
24661 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24662 dw_die_ref context_die)
24664 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24665 tree variant_part_offset = vlr_ctx->variant_part_offset;
24666 struct loc_descr_context ctx = {
24667 vlr_ctx->struct_type, /* context_type */
24668 NULL_TREE, /* base_decl */
24669 NULL, /* dpi */
24670 false, /* placeholder_arg */
24671 false /* placeholder_seen */
24674 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24675 NULL_TREE if there is no such field. */
24676 tree discr_decl = NULL_TREE;
24677 dw_discr_list_ref *discr_lists;
24678 unsigned discr_lists_length = 0;
24679 unsigned i;
24681 dw_die_ref dwarf_proc_die = NULL;
24682 dw_die_ref variant_part_die
24683 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24685 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24687 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24688 &discr_decl, &discr_lists, &discr_lists_length);
24690 if (discr_decl != NULL_TREE)
24692 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24694 if (discr_die)
24695 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24696 else
24697 /* We have no DIE for the discriminant, so just discard all
24698 discrimimant information in the output. */
24699 discr_decl = NULL_TREE;
24702 /* If the offset for this variant part is more complex than a constant,
24703 create a DWARF procedure for it so that we will not have to generate DWARF
24704 expressions for it for each member. */
24705 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24706 && (dwarf_version >= 3 || !dwarf_strict))
24708 const tree dwarf_proc_fndecl
24709 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24710 build_function_type (TREE_TYPE (variant_part_offset),
24711 NULL_TREE));
24712 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24713 const dw_loc_descr_ref dwarf_proc_body
24714 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24716 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24717 dwarf_proc_fndecl, context_die);
24718 if (dwarf_proc_die != NULL)
24719 variant_part_offset = dwarf_proc_call;
24722 /* Output DIEs for all variants. */
24723 i = 0;
24724 for (tree variant = TYPE_FIELDS (variant_part_type);
24725 variant != NULL_TREE;
24726 variant = DECL_CHAIN (variant), ++i)
24728 tree variant_type = TREE_TYPE (variant);
24729 dw_die_ref variant_die;
24731 /* All variants (i.e. members of a variant part) are supposed to be
24732 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24733 under these records. */
24734 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24736 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24737 equate_decl_number_to_die (variant, variant_die);
24739 /* Output discriminant values this variant matches, if any. */
24740 if (discr_decl == NULL || discr_lists[i] == NULL)
24741 /* In the case we have discriminant information at all, this is
24742 probably the default variant: as the standard says, don't
24743 output any discriminant value/list attribute. */
24745 else if (discr_lists[i]->dw_discr_next == NULL
24746 && !discr_lists[i]->dw_discr_range)
24747 /* If there is only one accepted value, don't bother outputting a
24748 list. */
24749 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24750 else
24751 add_discr_list (variant_die, discr_lists[i]);
24753 for (tree member = TYPE_FIELDS (variant_type);
24754 member != NULL_TREE;
24755 member = DECL_CHAIN (member))
24757 struct vlr_context vlr_sub_ctx = {
24758 vlr_ctx->struct_type, /* struct_type */
24759 NULL /* variant_part_offset */
24761 if (is_variant_part (member))
24763 /* All offsets for fields inside variant parts are relative to
24764 the top-level embedding RECORD_TYPE's base address. On the
24765 other hand, offsets in GCC's types are relative to the
24766 nested-most variant part. So we have to sum offsets each time
24767 we recurse. */
24769 vlr_sub_ctx.variant_part_offset
24770 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24771 variant_part_offset, byte_position (member));
24772 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24774 else
24776 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24777 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24782 free (discr_lists);
24785 /* Generate a DIE for a class member. */
24787 static void
24788 gen_member_die (tree type, dw_die_ref context_die)
24790 tree member;
24791 tree binfo = TYPE_BINFO (type);
24793 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24795 /* If this is not an incomplete type, output descriptions of each of its
24796 members. Note that as we output the DIEs necessary to represent the
24797 members of this record or union type, we will also be trying to output
24798 DIEs to represent the *types* of those members. However the `type'
24799 function (above) will specifically avoid generating type DIEs for member
24800 types *within* the list of member DIEs for this (containing) type except
24801 for those types (of members) which are explicitly marked as also being
24802 members of this (containing) type themselves. The g++ front- end can
24803 force any given type to be treated as a member of some other (containing)
24804 type by setting the TYPE_CONTEXT of the given (member) type to point to
24805 the TREE node representing the appropriate (containing) type. */
24807 /* First output info about the base classes. */
24808 if (binfo)
24810 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24811 int i;
24812 tree base;
24814 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24815 gen_inheritance_die (base,
24816 (accesses ? (*accesses)[i] : access_public_node),
24817 type,
24818 context_die);
24821 /* Now output info about the data members and type members. */
24822 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24824 struct vlr_context vlr_ctx = { type, NULL_TREE };
24825 bool static_inline_p
24826 = (TREE_STATIC (member)
24827 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24828 != -1));
24830 /* Ignore clones. */
24831 if (DECL_ABSTRACT_ORIGIN (member))
24832 continue;
24834 /* If we thought we were generating minimal debug info for TYPE
24835 and then changed our minds, some of the member declarations
24836 may have already been defined. Don't define them again, but
24837 do put them in the right order. */
24839 if (dw_die_ref child = lookup_decl_die (member))
24841 /* Handle inline static data members, which only have in-class
24842 declarations. */
24843 dw_die_ref ref = NULL;
24844 if (child->die_tag == DW_TAG_variable
24845 && child->die_parent == comp_unit_die ())
24847 ref = get_AT_ref (child, DW_AT_specification);
24848 /* For C++17 inline static data members followed by redundant
24849 out of class redeclaration, we might get here with
24850 child being the DIE created for the out of class
24851 redeclaration and with its DW_AT_specification being
24852 the DIE created for in-class definition. We want to
24853 reparent the latter, and don't want to create another
24854 DIE with DW_AT_specification in that case, because
24855 we already have one. */
24856 if (ref
24857 && static_inline_p
24858 && ref->die_tag == DW_TAG_variable
24859 && ref->die_parent == comp_unit_die ()
24860 && get_AT (ref, DW_AT_specification) == NULL)
24862 child = ref;
24863 ref = NULL;
24864 static_inline_p = false;
24868 if (child->die_tag == DW_TAG_variable
24869 && child->die_parent == comp_unit_die ()
24870 && ref == NULL)
24872 reparent_child (child, context_die);
24873 if (dwarf_version < 5)
24874 child->die_tag = DW_TAG_member;
24876 else
24877 splice_child_die (context_die, child);
24880 /* Do not generate standard DWARF for variant parts if we are generating
24881 the corresponding GNAT encodings: DIEs generated for both would
24882 conflict in our mappings. */
24883 else if (is_variant_part (member)
24884 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24886 vlr_ctx.variant_part_offset = byte_position (member);
24887 gen_variant_part (member, &vlr_ctx, context_die);
24889 else
24891 vlr_ctx.variant_part_offset = NULL_TREE;
24892 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24895 /* For C++ inline static data members emit immediately a DW_TAG_variable
24896 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24897 DW_AT_specification. */
24898 if (static_inline_p)
24900 int old_extern = DECL_EXTERNAL (member);
24901 DECL_EXTERNAL (member) = 0;
24902 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24903 DECL_EXTERNAL (member) = old_extern;
24908 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24909 is set, we pretend that the type was never defined, so we only get the
24910 member DIEs needed by later specification DIEs. */
24912 static void
24913 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24914 enum debug_info_usage usage)
24916 if (TREE_ASM_WRITTEN (type))
24918 /* Fill in the bound of variable-length fields in late dwarf if
24919 still incomplete. */
24920 if (!early_dwarf && variably_modified_type_p (type, NULL))
24921 for (tree member = TYPE_FIELDS (type);
24922 member;
24923 member = DECL_CHAIN (member))
24924 fill_variable_array_bounds (TREE_TYPE (member));
24925 return;
24928 dw_die_ref type_die = lookup_type_die (type);
24929 dw_die_ref scope_die = 0;
24930 int nested = 0;
24931 int complete = (TYPE_SIZE (type)
24932 && (! TYPE_STUB_DECL (type)
24933 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24934 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24935 complete = complete && should_emit_struct_debug (type, usage);
24937 if (type_die && ! complete)
24938 return;
24940 if (TYPE_CONTEXT (type) != NULL_TREE
24941 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24942 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24943 nested = 1;
24945 scope_die = scope_die_for (type, context_die);
24947 /* Generate child dies for template paramaters. */
24948 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24949 schedule_generic_params_dies_gen (type);
24951 if (! type_die || (nested && is_cu_die (scope_die)))
24952 /* First occurrence of type or toplevel definition of nested class. */
24954 dw_die_ref old_die = type_die;
24956 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24957 ? record_type_tag (type) : DW_TAG_union_type,
24958 scope_die, type);
24959 equate_type_number_to_die (type, type_die);
24960 if (old_die)
24961 add_AT_specification (type_die, old_die);
24962 else
24963 add_name_attribute (type_die, type_tag (type));
24965 else
24966 remove_AT (type_die, DW_AT_declaration);
24968 /* If this type has been completed, then give it a byte_size attribute and
24969 then give a list of members. */
24970 if (complete && !ns_decl)
24972 /* Prevent infinite recursion in cases where the type of some member of
24973 this type is expressed in terms of this type itself. */
24974 TREE_ASM_WRITTEN (type) = 1;
24975 add_byte_size_attribute (type_die, type);
24976 add_alignment_attribute (type_die, type);
24977 if (TYPE_STUB_DECL (type) != NULL_TREE)
24979 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24980 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24983 /* If the first reference to this type was as the return type of an
24984 inline function, then it may not have a parent. Fix this now. */
24985 if (type_die->die_parent == NULL)
24986 add_child_die (scope_die, type_die);
24988 push_decl_scope (type);
24989 gen_member_die (type, type_die);
24990 pop_decl_scope ();
24992 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24993 if (TYPE_ARTIFICIAL (type))
24994 add_AT_flag (type_die, DW_AT_artificial, 1);
24996 /* GNU extension: Record what type our vtable lives in. */
24997 if (TYPE_VFIELD (type))
24999 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25001 gen_type_die (vtype, context_die);
25002 add_AT_die_ref (type_die, DW_AT_containing_type,
25003 lookup_type_die (vtype));
25006 else
25008 add_AT_flag (type_die, DW_AT_declaration, 1);
25010 /* We don't need to do this for function-local types. */
25011 if (TYPE_STUB_DECL (type)
25012 && ! decl_function_context (TYPE_STUB_DECL (type)))
25013 vec_safe_push (incomplete_types, type);
25016 if (get_AT (type_die, DW_AT_name))
25017 add_pubtype (type, type_die);
25020 /* Generate a DIE for a subroutine _type_. */
25022 static void
25023 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25025 tree return_type = TREE_TYPE (type);
25026 dw_die_ref subr_die
25027 = new_die (DW_TAG_subroutine_type,
25028 scope_die_for (type, context_die), type);
25030 equate_type_number_to_die (type, subr_die);
25031 add_prototyped_attribute (subr_die, type);
25032 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25033 context_die);
25034 add_alignment_attribute (subr_die, type);
25035 gen_formal_types_die (type, subr_die);
25037 if (get_AT (subr_die, DW_AT_name))
25038 add_pubtype (type, subr_die);
25039 if ((dwarf_version >= 5 || !dwarf_strict)
25040 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25041 add_AT_flag (subr_die, DW_AT_reference, 1);
25042 if ((dwarf_version >= 5 || !dwarf_strict)
25043 && lang_hooks.types.type_dwarf_attribute (type,
25044 DW_AT_rvalue_reference) != -1)
25045 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25048 /* Generate a DIE for a type definition. */
25050 static void
25051 gen_typedef_die (tree decl, dw_die_ref context_die)
25053 dw_die_ref type_die;
25054 tree type;
25056 if (TREE_ASM_WRITTEN (decl))
25058 if (DECL_ORIGINAL_TYPE (decl))
25059 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25060 return;
25063 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25064 checks in process_scope_var and modified_type_die), this should be called
25065 only for original types. */
25066 gcc_assert (decl_ultimate_origin (decl) == NULL
25067 || decl_ultimate_origin (decl) == decl);
25069 TREE_ASM_WRITTEN (decl) = 1;
25070 type_die = new_die (DW_TAG_typedef, context_die, decl);
25072 add_name_and_src_coords_attributes (type_die, decl);
25073 if (DECL_ORIGINAL_TYPE (decl))
25075 type = DECL_ORIGINAL_TYPE (decl);
25076 if (type == error_mark_node)
25077 return;
25079 gcc_assert (type != TREE_TYPE (decl));
25080 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25082 else
25084 type = TREE_TYPE (decl);
25085 if (type == error_mark_node)
25086 return;
25088 if (is_naming_typedef_decl (TYPE_NAME (type)))
25090 /* Here, we are in the case of decl being a typedef naming
25091 an anonymous type, e.g:
25092 typedef struct {...} foo;
25093 In that case TREE_TYPE (decl) is not a typedef variant
25094 type and TYPE_NAME of the anonymous type is set to the
25095 TYPE_DECL of the typedef. This construct is emitted by
25096 the C++ FE.
25098 TYPE is the anonymous struct named by the typedef
25099 DECL. As we need the DW_AT_type attribute of the
25100 DW_TAG_typedef to point to the DIE of TYPE, let's
25101 generate that DIE right away. add_type_attribute
25102 called below will then pick (via lookup_type_die) that
25103 anonymous struct DIE. */
25104 if (!TREE_ASM_WRITTEN (type))
25105 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25107 /* This is a GNU Extension. We are adding a
25108 DW_AT_linkage_name attribute to the DIE of the
25109 anonymous struct TYPE. The value of that attribute
25110 is the name of the typedef decl naming the anonymous
25111 struct. This greatly eases the work of consumers of
25112 this debug info. */
25113 add_linkage_name_raw (lookup_type_die (type), decl);
25117 add_type_attribute (type_die, type, decl_quals (decl), false,
25118 context_die);
25120 if (is_naming_typedef_decl (decl))
25121 /* We want that all subsequent calls to lookup_type_die with
25122 TYPE in argument yield the DW_TAG_typedef we have just
25123 created. */
25124 equate_type_number_to_die (type, type_die);
25126 add_alignment_attribute (type_die, TREE_TYPE (decl));
25128 add_accessibility_attribute (type_die, decl);
25130 if (DECL_ABSTRACT_P (decl))
25131 equate_decl_number_to_die (decl, type_die);
25133 if (get_AT (type_die, DW_AT_name))
25134 add_pubtype (decl, type_die);
25137 /* Generate a DIE for a struct, class, enum or union type. */
25139 static void
25140 gen_tagged_type_die (tree type,
25141 dw_die_ref context_die,
25142 enum debug_info_usage usage)
25144 int need_pop;
25146 if (type == NULL_TREE
25147 || !is_tagged_type (type))
25148 return;
25150 if (TREE_ASM_WRITTEN (type))
25151 need_pop = 0;
25152 /* If this is a nested type whose containing class hasn't been written
25153 out yet, writing it out will cover this one, too. This does not apply
25154 to instantiations of member class templates; they need to be added to
25155 the containing class as they are generated. FIXME: This hurts the
25156 idea of combining type decls from multiple TUs, since we can't predict
25157 what set of template instantiations we'll get. */
25158 else if (TYPE_CONTEXT (type)
25159 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25160 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25162 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25164 if (TREE_ASM_WRITTEN (type))
25165 return;
25167 /* If that failed, attach ourselves to the stub. */
25168 push_decl_scope (TYPE_CONTEXT (type));
25169 context_die = lookup_type_die (TYPE_CONTEXT (type));
25170 need_pop = 1;
25172 else if (TYPE_CONTEXT (type) != NULL_TREE
25173 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25175 /* If this type is local to a function that hasn't been written
25176 out yet, use a NULL context for now; it will be fixed up in
25177 decls_for_scope. */
25178 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25179 /* A declaration DIE doesn't count; nested types need to go in the
25180 specification. */
25181 if (context_die && is_declaration_die (context_die))
25182 context_die = NULL;
25183 need_pop = 0;
25185 else
25187 context_die = declare_in_namespace (type, context_die);
25188 need_pop = 0;
25191 if (TREE_CODE (type) == ENUMERAL_TYPE)
25193 /* This might have been written out by the call to
25194 declare_in_namespace. */
25195 if (!TREE_ASM_WRITTEN (type))
25196 gen_enumeration_type_die (type, context_die);
25198 else
25199 gen_struct_or_union_type_die (type, context_die, usage);
25201 if (need_pop)
25202 pop_decl_scope ();
25204 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25205 it up if it is ever completed. gen_*_type_die will set it for us
25206 when appropriate. */
25209 /* Generate a type description DIE. */
25211 static void
25212 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25213 enum debug_info_usage usage)
25215 struct array_descr_info info;
25217 if (type == NULL_TREE || type == error_mark_node)
25218 return;
25220 if (flag_checking && type)
25221 verify_type (type);
25223 if (TYPE_NAME (type) != NULL_TREE
25224 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25225 && is_redundant_typedef (TYPE_NAME (type))
25226 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25227 /* The DECL of this type is a typedef we don't want to emit debug
25228 info for but we want debug info for its underlying typedef.
25229 This can happen for e.g, the injected-class-name of a C++
25230 type. */
25231 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25233 /* If TYPE is a typedef type variant, let's generate debug info
25234 for the parent typedef which TYPE is a type of. */
25235 if (typedef_variant_p (type))
25237 if (TREE_ASM_WRITTEN (type))
25238 return;
25240 tree name = TYPE_NAME (type);
25241 tree origin = decl_ultimate_origin (name);
25242 if (origin != NULL && origin != name)
25244 gen_decl_die (origin, NULL, NULL, context_die);
25245 return;
25248 /* Prevent broken recursion; we can't hand off to the same type. */
25249 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25251 /* Give typedefs the right scope. */
25252 context_die = scope_die_for (type, context_die);
25254 TREE_ASM_WRITTEN (type) = 1;
25256 gen_decl_die (name, NULL, NULL, context_die);
25257 return;
25260 /* If type is an anonymous tagged type named by a typedef, let's
25261 generate debug info for the typedef. */
25262 if (is_naming_typedef_decl (TYPE_NAME (type)))
25264 /* Use the DIE of the containing namespace as the parent DIE of
25265 the type description DIE we want to generate. */
25266 if (DECL_CONTEXT (TYPE_NAME (type))
25267 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
25268 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
25270 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25271 return;
25274 if (lang_hooks.types.get_debug_type)
25276 tree debug_type = lang_hooks.types.get_debug_type (type);
25278 if (debug_type != NULL_TREE && debug_type != type)
25280 gen_type_die_with_usage (debug_type, context_die, usage);
25281 return;
25285 /* We are going to output a DIE to represent the unqualified version
25286 of this type (i.e. without any const or volatile qualifiers) so
25287 get the main variant (i.e. the unqualified version) of this type
25288 now. (Vectors and arrays are special because the debugging info is in the
25289 cloned type itself. Similarly function/method types can contain extra
25290 ref-qualification). */
25291 if (TREE_CODE (type) == FUNCTION_TYPE
25292 || TREE_CODE (type) == METHOD_TYPE)
25294 /* For function/method types, can't use type_main_variant here,
25295 because that can have different ref-qualifiers for C++,
25296 but try to canonicalize. */
25297 tree main = TYPE_MAIN_VARIANT (type);
25298 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25299 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25300 && check_base_type (t, main)
25301 && check_lang_type (t, type))
25303 type = t;
25304 break;
25307 else if (TREE_CODE (type) != VECTOR_TYPE
25308 && TREE_CODE (type) != ARRAY_TYPE)
25309 type = type_main_variant (type);
25311 /* If this is an array type with hidden descriptor, handle it first. */
25312 if (!TREE_ASM_WRITTEN (type)
25313 && lang_hooks.types.get_array_descr_info)
25315 memset (&info, 0, sizeof (info));
25316 if (lang_hooks.types.get_array_descr_info (type, &info))
25318 /* Fortran sometimes emits array types with no dimension. */
25319 gcc_assert (info.ndimensions >= 0
25320 && (info.ndimensions
25321 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25322 gen_descr_array_type_die (type, &info, context_die);
25323 TREE_ASM_WRITTEN (type) = 1;
25324 return;
25328 if (TREE_ASM_WRITTEN (type))
25330 /* Variable-length types may be incomplete even if
25331 TREE_ASM_WRITTEN. For such types, fall through to
25332 gen_array_type_die() and possibly fill in
25333 DW_AT_{upper,lower}_bound attributes. */
25334 if ((TREE_CODE (type) != ARRAY_TYPE
25335 && TREE_CODE (type) != RECORD_TYPE
25336 && TREE_CODE (type) != UNION_TYPE
25337 && TREE_CODE (type) != QUAL_UNION_TYPE)
25338 || !variably_modified_type_p (type, NULL))
25339 return;
25342 switch (TREE_CODE (type))
25344 case ERROR_MARK:
25345 break;
25347 case POINTER_TYPE:
25348 case REFERENCE_TYPE:
25349 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25350 ensures that the gen_type_die recursion will terminate even if the
25351 type is recursive. Recursive types are possible in Ada. */
25352 /* ??? We could perhaps do this for all types before the switch
25353 statement. */
25354 TREE_ASM_WRITTEN (type) = 1;
25356 /* For these types, all that is required is that we output a DIE (or a
25357 set of DIEs) to represent the "basis" type. */
25358 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25359 DINFO_USAGE_IND_USE);
25360 break;
25362 case OFFSET_TYPE:
25363 /* This code is used for C++ pointer-to-data-member types.
25364 Output a description of the relevant class type. */
25365 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25366 DINFO_USAGE_IND_USE);
25368 /* Output a description of the type of the object pointed to. */
25369 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25370 DINFO_USAGE_IND_USE);
25372 /* Now output a DIE to represent this pointer-to-data-member type
25373 itself. */
25374 gen_ptr_to_mbr_type_die (type, context_die);
25375 break;
25377 case FUNCTION_TYPE:
25378 /* Force out return type (in case it wasn't forced out already). */
25379 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25380 DINFO_USAGE_DIR_USE);
25381 gen_subroutine_type_die (type, context_die);
25382 break;
25384 case METHOD_TYPE:
25385 /* Force out return type (in case it wasn't forced out already). */
25386 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25387 DINFO_USAGE_DIR_USE);
25388 gen_subroutine_type_die (type, context_die);
25389 break;
25391 case ARRAY_TYPE:
25392 case VECTOR_TYPE:
25393 gen_array_type_die (type, context_die);
25394 break;
25396 case ENUMERAL_TYPE:
25397 case RECORD_TYPE:
25398 case UNION_TYPE:
25399 case QUAL_UNION_TYPE:
25400 gen_tagged_type_die (type, context_die, usage);
25401 return;
25403 case VOID_TYPE:
25404 case INTEGER_TYPE:
25405 case REAL_TYPE:
25406 case FIXED_POINT_TYPE:
25407 case COMPLEX_TYPE:
25408 case BOOLEAN_TYPE:
25409 case POINTER_BOUNDS_TYPE:
25410 /* No DIEs needed for fundamental types. */
25411 break;
25413 case NULLPTR_TYPE:
25414 case LANG_TYPE:
25415 /* Just use DW_TAG_unspecified_type. */
25417 dw_die_ref type_die = lookup_type_die (type);
25418 if (type_die == NULL)
25420 tree name = TYPE_IDENTIFIER (type);
25421 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25422 type);
25423 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25424 equate_type_number_to_die (type, type_die);
25427 break;
25429 default:
25430 if (is_cxx_auto (type))
25432 tree name = TYPE_IDENTIFIER (type);
25433 dw_die_ref *die = (name == get_identifier ("auto")
25434 ? &auto_die : &decltype_auto_die);
25435 if (!*die)
25437 *die = new_die (DW_TAG_unspecified_type,
25438 comp_unit_die (), NULL_TREE);
25439 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25441 equate_type_number_to_die (type, *die);
25442 break;
25444 gcc_unreachable ();
25447 TREE_ASM_WRITTEN (type) = 1;
25450 static void
25451 gen_type_die (tree type, dw_die_ref context_die)
25453 if (type != error_mark_node)
25455 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25456 if (flag_checking)
25458 dw_die_ref die = lookup_type_die (type);
25459 if (die)
25460 check_die (die);
25465 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25466 things which are local to the given block. */
25468 static void
25469 gen_block_die (tree stmt, dw_die_ref context_die)
25471 int must_output_die = 0;
25472 bool inlined_func;
25474 /* Ignore blocks that are NULL. */
25475 if (stmt == NULL_TREE)
25476 return;
25478 inlined_func = inlined_function_outer_scope_p (stmt);
25480 /* If the block is one fragment of a non-contiguous block, do not
25481 process the variables, since they will have been done by the
25482 origin block. Do process subblocks. */
25483 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25485 tree sub;
25487 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25488 gen_block_die (sub, context_die);
25490 return;
25493 /* Determine if we need to output any Dwarf DIEs at all to represent this
25494 block. */
25495 if (inlined_func)
25496 /* The outer scopes for inlinings *must* always be represented. We
25497 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25498 must_output_die = 1;
25499 else
25501 /* Determine if this block directly contains any "significant"
25502 local declarations which we will need to output DIEs for. */
25503 if (debug_info_level > DINFO_LEVEL_TERSE)
25504 /* We are not in terse mode so *any* local declaration counts
25505 as being a "significant" one. */
25506 must_output_die = ((BLOCK_VARS (stmt) != NULL
25507 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25508 && (TREE_USED (stmt)
25509 || TREE_ASM_WRITTEN (stmt)
25510 || BLOCK_ABSTRACT (stmt)));
25511 else if ((TREE_USED (stmt)
25512 || TREE_ASM_WRITTEN (stmt)
25513 || BLOCK_ABSTRACT (stmt))
25514 && !dwarf2out_ignore_block (stmt))
25515 must_output_die = 1;
25518 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25519 DIE for any block which contains no significant local declarations at
25520 all. Rather, in such cases we just call `decls_for_scope' so that any
25521 needed Dwarf info for any sub-blocks will get properly generated. Note
25522 that in terse mode, our definition of what constitutes a "significant"
25523 local declaration gets restricted to include only inlined function
25524 instances and local (nested) function definitions. */
25525 if (must_output_die)
25527 if (inlined_func)
25529 /* If STMT block is abstract, that means we have been called
25530 indirectly from dwarf2out_abstract_function.
25531 That function rightfully marks the descendent blocks (of
25532 the abstract function it is dealing with) as being abstract,
25533 precisely to prevent us from emitting any
25534 DW_TAG_inlined_subroutine DIE as a descendent
25535 of an abstract function instance. So in that case, we should
25536 not call gen_inlined_subroutine_die.
25538 Later though, when cgraph asks dwarf2out to emit info
25539 for the concrete instance of the function decl into which
25540 the concrete instance of STMT got inlined, the later will lead
25541 to the generation of a DW_TAG_inlined_subroutine DIE. */
25542 if (! BLOCK_ABSTRACT (stmt))
25543 gen_inlined_subroutine_die (stmt, context_die);
25545 else
25546 gen_lexical_block_die (stmt, context_die);
25548 else
25549 decls_for_scope (stmt, context_die);
25552 /* Process variable DECL (or variable with origin ORIGIN) within
25553 block STMT and add it to CONTEXT_DIE. */
25554 static void
25555 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25557 dw_die_ref die;
25558 tree decl_or_origin = decl ? decl : origin;
25560 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25561 die = lookup_decl_die (decl_or_origin);
25562 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25564 if (TYPE_DECL_IS_STUB (decl_or_origin))
25565 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25566 else
25567 die = lookup_decl_die (decl_or_origin);
25568 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25569 if (! die && ! early_dwarf)
25570 return;
25572 else
25573 die = NULL;
25575 /* Avoid creating DIEs for local typedefs and concrete static variables that
25576 will only be pruned later. */
25577 if ((origin || decl_ultimate_origin (decl))
25578 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25579 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25581 origin = decl_ultimate_origin (decl_or_origin);
25582 if (decl && VAR_P (decl) && die != NULL)
25584 die = lookup_decl_die (origin);
25585 if (die != NULL)
25586 equate_decl_number_to_die (decl, die);
25588 return;
25591 if (die != NULL && die->die_parent == NULL)
25592 add_child_die (context_die, die);
25593 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25595 if (early_dwarf)
25596 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25597 stmt, context_die);
25599 else
25601 if (decl && DECL_P (decl))
25603 die = lookup_decl_die (decl);
25605 /* Early created DIEs do not have a parent as the decls refer
25606 to the function as DECL_CONTEXT rather than the BLOCK. */
25607 if (die && die->die_parent == NULL)
25609 gcc_assert (in_lto_p);
25610 add_child_die (context_die, die);
25614 gen_decl_die (decl, origin, NULL, context_die);
25618 /* Generate all of the decls declared within a given scope and (recursively)
25619 all of its sub-blocks. */
25621 static void
25622 decls_for_scope (tree stmt, dw_die_ref context_die)
25624 tree decl;
25625 unsigned int i;
25626 tree subblocks;
25628 /* Ignore NULL blocks. */
25629 if (stmt == NULL_TREE)
25630 return;
25632 /* Output the DIEs to represent all of the data objects and typedefs
25633 declared directly within this block but not within any nested
25634 sub-blocks. Also, nested function and tag DIEs have been
25635 generated with a parent of NULL; fix that up now. We don't
25636 have to do this if we're at -g1. */
25637 if (debug_info_level > DINFO_LEVEL_TERSE)
25639 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25640 process_scope_var (stmt, decl, NULL_TREE, context_die);
25641 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25642 origin - avoid doing this twice as we have no good way to see
25643 if we've done it once already. */
25644 if (! early_dwarf)
25645 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25647 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25648 if (decl == current_function_decl)
25649 /* Ignore declarations of the current function, while they
25650 are declarations, gen_subprogram_die would treat them
25651 as definitions again, because they are equal to
25652 current_function_decl and endlessly recurse. */;
25653 else if (TREE_CODE (decl) == FUNCTION_DECL)
25654 process_scope_var (stmt, decl, NULL_TREE, context_die);
25655 else
25656 process_scope_var (stmt, NULL_TREE, decl, context_die);
25660 /* Even if we're at -g1, we need to process the subblocks in order to get
25661 inlined call information. */
25663 /* Output the DIEs to represent all sub-blocks (and the items declared
25664 therein) of this block. */
25665 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25666 subblocks != NULL;
25667 subblocks = BLOCK_CHAIN (subblocks))
25668 gen_block_die (subblocks, context_die);
25671 /* Is this a typedef we can avoid emitting? */
25673 bool
25674 is_redundant_typedef (const_tree decl)
25676 if (TYPE_DECL_IS_STUB (decl))
25677 return true;
25679 if (DECL_ARTIFICIAL (decl)
25680 && DECL_CONTEXT (decl)
25681 && is_tagged_type (DECL_CONTEXT (decl))
25682 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25683 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25684 /* Also ignore the artificial member typedef for the class name. */
25685 return true;
25687 return false;
25690 /* Return TRUE if TYPE is a typedef that names a type for linkage
25691 purposes. This kind of typedefs is produced by the C++ FE for
25692 constructs like:
25694 typedef struct {...} foo;
25696 In that case, there is no typedef variant type produced for foo.
25697 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25698 struct type. */
25700 static bool
25701 is_naming_typedef_decl (const_tree decl)
25703 if (decl == NULL_TREE
25704 || TREE_CODE (decl) != TYPE_DECL
25705 || DECL_NAMELESS (decl)
25706 || !is_tagged_type (TREE_TYPE (decl))
25707 || DECL_IS_BUILTIN (decl)
25708 || is_redundant_typedef (decl)
25709 /* It looks like Ada produces TYPE_DECLs that are very similar
25710 to C++ naming typedefs but that have different
25711 semantics. Let's be specific to c++ for now. */
25712 || !is_cxx (decl))
25713 return FALSE;
25715 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25716 && TYPE_NAME (TREE_TYPE (decl)) == decl
25717 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25718 != TYPE_NAME (TREE_TYPE (decl))));
25721 /* Looks up the DIE for a context. */
25723 static inline dw_die_ref
25724 lookup_context_die (tree context)
25726 if (context)
25728 /* Find die that represents this context. */
25729 if (TYPE_P (context))
25731 context = TYPE_MAIN_VARIANT (context);
25732 dw_die_ref ctx = lookup_type_die (context);
25733 if (!ctx)
25734 return NULL;
25735 return strip_naming_typedef (context, ctx);
25737 else
25738 return lookup_decl_die (context);
25740 return comp_unit_die ();
25743 /* Returns the DIE for a context. */
25745 static inline dw_die_ref
25746 get_context_die (tree context)
25748 if (context)
25750 /* Find die that represents this context. */
25751 if (TYPE_P (context))
25753 context = TYPE_MAIN_VARIANT (context);
25754 return strip_naming_typedef (context, force_type_die (context));
25756 else
25757 return force_decl_die (context);
25759 return comp_unit_die ();
25762 /* Returns the DIE for decl. A DIE will always be returned. */
25764 static dw_die_ref
25765 force_decl_die (tree decl)
25767 dw_die_ref decl_die;
25768 unsigned saved_external_flag;
25769 tree save_fn = NULL_TREE;
25770 decl_die = lookup_decl_die (decl);
25771 if (!decl_die)
25773 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25775 decl_die = lookup_decl_die (decl);
25776 if (decl_die)
25777 return decl_die;
25779 switch (TREE_CODE (decl))
25781 case FUNCTION_DECL:
25782 /* Clear current_function_decl, so that gen_subprogram_die thinks
25783 that this is a declaration. At this point, we just want to force
25784 declaration die. */
25785 save_fn = current_function_decl;
25786 current_function_decl = NULL_TREE;
25787 gen_subprogram_die (decl, context_die);
25788 current_function_decl = save_fn;
25789 break;
25791 case VAR_DECL:
25792 /* Set external flag to force declaration die. Restore it after
25793 gen_decl_die() call. */
25794 saved_external_flag = DECL_EXTERNAL (decl);
25795 DECL_EXTERNAL (decl) = 1;
25796 gen_decl_die (decl, NULL, NULL, context_die);
25797 DECL_EXTERNAL (decl) = saved_external_flag;
25798 break;
25800 case NAMESPACE_DECL:
25801 if (dwarf_version >= 3 || !dwarf_strict)
25802 dwarf2out_decl (decl);
25803 else
25804 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25805 decl_die = comp_unit_die ();
25806 break;
25808 case TRANSLATION_UNIT_DECL:
25809 decl_die = comp_unit_die ();
25810 break;
25812 default:
25813 gcc_unreachable ();
25816 /* We should be able to find the DIE now. */
25817 if (!decl_die)
25818 decl_die = lookup_decl_die (decl);
25819 gcc_assert (decl_die);
25822 return decl_die;
25825 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25826 always returned. */
25828 static dw_die_ref
25829 force_type_die (tree type)
25831 dw_die_ref type_die;
25833 type_die = lookup_type_die (type);
25834 if (!type_die)
25836 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25838 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25839 false, context_die);
25840 gcc_assert (type_die);
25842 return type_die;
25845 /* Force out any required namespaces to be able to output DECL,
25846 and return the new context_die for it, if it's changed. */
25848 static dw_die_ref
25849 setup_namespace_context (tree thing, dw_die_ref context_die)
25851 tree context = (DECL_P (thing)
25852 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25853 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25854 /* Force out the namespace. */
25855 context_die = force_decl_die (context);
25857 return context_die;
25860 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25861 type) within its namespace, if appropriate.
25863 For compatibility with older debuggers, namespace DIEs only contain
25864 declarations; all definitions are emitted at CU scope, with
25865 DW_AT_specification pointing to the declaration (like with class
25866 members). */
25868 static dw_die_ref
25869 declare_in_namespace (tree thing, dw_die_ref context_die)
25871 dw_die_ref ns_context;
25873 if (debug_info_level <= DINFO_LEVEL_TERSE)
25874 return context_die;
25876 /* External declarations in the local scope only need to be emitted
25877 once, not once in the namespace and once in the scope.
25879 This avoids declaring the `extern' below in the
25880 namespace DIE as well as in the innermost scope:
25882 namespace S
25884 int i=5;
25885 int foo()
25887 int i=8;
25888 extern int i;
25889 return i;
25893 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25894 return context_die;
25896 /* If this decl is from an inlined function, then don't try to emit it in its
25897 namespace, as we will get confused. It would have already been emitted
25898 when the abstract instance of the inline function was emitted anyways. */
25899 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25900 return context_die;
25902 ns_context = setup_namespace_context (thing, context_die);
25904 if (ns_context != context_die)
25906 if (is_fortran ())
25907 return ns_context;
25908 if (DECL_P (thing))
25909 gen_decl_die (thing, NULL, NULL, ns_context);
25910 else
25911 gen_type_die (thing, ns_context);
25913 return context_die;
25916 /* Generate a DIE for a namespace or namespace alias. */
25918 static void
25919 gen_namespace_die (tree decl, dw_die_ref context_die)
25921 dw_die_ref namespace_die;
25923 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25924 they are an alias of. */
25925 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25927 /* Output a real namespace or module. */
25928 context_die = setup_namespace_context (decl, comp_unit_die ());
25929 namespace_die = new_die (is_fortran ()
25930 ? DW_TAG_module : DW_TAG_namespace,
25931 context_die, decl);
25932 /* For Fortran modules defined in different CU don't add src coords. */
25933 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25935 const char *name = dwarf2_name (decl, 0);
25936 if (name)
25937 add_name_attribute (namespace_die, name);
25939 else
25940 add_name_and_src_coords_attributes (namespace_die, decl);
25941 if (DECL_EXTERNAL (decl))
25942 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25943 equate_decl_number_to_die (decl, namespace_die);
25945 else
25947 /* Output a namespace alias. */
25949 /* Force out the namespace we are an alias of, if necessary. */
25950 dw_die_ref origin_die
25951 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25953 if (DECL_FILE_SCOPE_P (decl)
25954 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25955 context_die = setup_namespace_context (decl, comp_unit_die ());
25956 /* Now create the namespace alias DIE. */
25957 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25958 add_name_and_src_coords_attributes (namespace_die, decl);
25959 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25960 equate_decl_number_to_die (decl, namespace_die);
25962 if ((dwarf_version >= 5 || !dwarf_strict)
25963 && lang_hooks.decls.decl_dwarf_attribute (decl,
25964 DW_AT_export_symbols) == 1)
25965 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25967 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25968 if (want_pubnames ())
25969 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25972 /* Generate Dwarf debug information for a decl described by DECL.
25973 The return value is currently only meaningful for PARM_DECLs,
25974 for all other decls it returns NULL.
25976 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25977 It can be NULL otherwise. */
25979 static dw_die_ref
25980 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25981 dw_die_ref context_die)
25983 tree decl_or_origin = decl ? decl : origin;
25984 tree class_origin = NULL, ultimate_origin;
25986 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25987 return NULL;
25989 /* Ignore pointer bounds decls. */
25990 if (DECL_P (decl_or_origin)
25991 && TREE_TYPE (decl_or_origin)
25992 && POINTER_BOUNDS_P (decl_or_origin))
25993 return NULL;
25995 switch (TREE_CODE (decl_or_origin))
25997 case ERROR_MARK:
25998 break;
26000 case CONST_DECL:
26001 if (!is_fortran () && !is_ada ())
26003 /* The individual enumerators of an enum type get output when we output
26004 the Dwarf representation of the relevant enum type itself. */
26005 break;
26008 /* Emit its type. */
26009 gen_type_die (TREE_TYPE (decl), context_die);
26011 /* And its containing namespace. */
26012 context_die = declare_in_namespace (decl, context_die);
26014 gen_const_die (decl, context_die);
26015 break;
26017 case FUNCTION_DECL:
26018 #if 0
26019 /* FIXME */
26020 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26021 on local redeclarations of global functions. That seems broken. */
26022 if (current_function_decl != decl)
26023 /* This is only a declaration. */;
26024 #endif
26026 /* We should have abstract copies already and should not generate
26027 stray type DIEs in late LTO dumping. */
26028 if (! early_dwarf)
26031 /* If we're emitting a clone, emit info for the abstract instance. */
26032 else if (origin || DECL_ORIGIN (decl) != decl)
26033 dwarf2out_abstract_function (origin
26034 ? DECL_ORIGIN (origin)
26035 : DECL_ABSTRACT_ORIGIN (decl));
26037 /* If we're emitting a possibly inlined function emit it as
26038 abstract instance. */
26039 else if (cgraph_function_possibly_inlined_p (decl)
26040 && ! DECL_ABSTRACT_P (decl)
26041 && ! class_or_namespace_scope_p (context_die)
26042 /* dwarf2out_abstract_function won't emit a die if this is just
26043 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26044 that case, because that works only if we have a die. */
26045 && DECL_INITIAL (decl) != NULL_TREE)
26046 dwarf2out_abstract_function (decl);
26048 /* Otherwise we're emitting the primary DIE for this decl. */
26049 else if (debug_info_level > DINFO_LEVEL_TERSE)
26051 /* Before we describe the FUNCTION_DECL itself, make sure that we
26052 have its containing type. */
26053 if (!origin)
26054 origin = decl_class_context (decl);
26055 if (origin != NULL_TREE)
26056 gen_type_die (origin, context_die);
26058 /* And its return type. */
26059 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26061 /* And its virtual context. */
26062 if (DECL_VINDEX (decl) != NULL_TREE)
26063 gen_type_die (DECL_CONTEXT (decl), context_die);
26065 /* Make sure we have a member DIE for decl. */
26066 if (origin != NULL_TREE)
26067 gen_type_die_for_member (origin, decl, context_die);
26069 /* And its containing namespace. */
26070 context_die = declare_in_namespace (decl, context_die);
26073 /* Now output a DIE to represent the function itself. */
26074 if (decl)
26075 gen_subprogram_die (decl, context_die);
26076 break;
26078 case TYPE_DECL:
26079 /* If we are in terse mode, don't generate any DIEs to represent any
26080 actual typedefs. */
26081 if (debug_info_level <= DINFO_LEVEL_TERSE)
26082 break;
26084 /* In the special case of a TYPE_DECL node representing the declaration
26085 of some type tag, if the given TYPE_DECL is marked as having been
26086 instantiated from some other (original) TYPE_DECL node (e.g. one which
26087 was generated within the original definition of an inline function) we
26088 used to generate a special (abbreviated) DW_TAG_structure_type,
26089 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26090 should be actually referencing those DIEs, as variable DIEs with that
26091 type would be emitted already in the abstract origin, so it was always
26092 removed during unused type prunning. Don't add anything in this
26093 case. */
26094 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26095 break;
26097 if (is_redundant_typedef (decl))
26098 gen_type_die (TREE_TYPE (decl), context_die);
26099 else
26100 /* Output a DIE to represent the typedef itself. */
26101 gen_typedef_die (decl, context_die);
26102 break;
26104 case LABEL_DECL:
26105 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26106 gen_label_die (decl, context_die);
26107 break;
26109 case VAR_DECL:
26110 case RESULT_DECL:
26111 /* If we are in terse mode, don't generate any DIEs to represent any
26112 variable declarations or definitions. */
26113 if (debug_info_level <= DINFO_LEVEL_TERSE)
26114 break;
26116 /* Avoid generating stray type DIEs during late dwarf dumping.
26117 All types have been dumped early. */
26118 if (early_dwarf
26119 /* ??? But in LTRANS we cannot annotate early created variably
26120 modified type DIEs without copying them and adjusting all
26121 references to them. Dump them again as happens for inlining
26122 which copies both the decl and the types. */
26123 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26124 in VLA bound information for example. */
26125 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26126 current_function_decl)))
26128 /* Output any DIEs that are needed to specify the type of this data
26129 object. */
26130 if (decl_by_reference_p (decl_or_origin))
26131 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26132 else
26133 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26136 if (early_dwarf)
26138 /* And its containing type. */
26139 class_origin = decl_class_context (decl_or_origin);
26140 if (class_origin != NULL_TREE)
26141 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26143 /* And its containing namespace. */
26144 context_die = declare_in_namespace (decl_or_origin, context_die);
26147 /* Now output the DIE to represent the data object itself. This gets
26148 complicated because of the possibility that the VAR_DECL really
26149 represents an inlined instance of a formal parameter for an inline
26150 function. */
26151 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26152 if (ultimate_origin != NULL_TREE
26153 && TREE_CODE (ultimate_origin) == PARM_DECL)
26154 gen_formal_parameter_die (decl, origin,
26155 true /* Emit name attribute. */,
26156 context_die);
26157 else
26158 gen_variable_die (decl, origin, context_die);
26159 break;
26161 case FIELD_DECL:
26162 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26163 /* Ignore the nameless fields that are used to skip bits but handle C++
26164 anonymous unions and structs. */
26165 if (DECL_NAME (decl) != NULL_TREE
26166 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26167 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26169 gen_type_die (member_declared_type (decl), context_die);
26170 gen_field_die (decl, ctx, context_die);
26172 break;
26174 case PARM_DECL:
26175 /* Avoid generating stray type DIEs during late dwarf dumping.
26176 All types have been dumped early. */
26177 if (early_dwarf
26178 /* ??? But in LTRANS we cannot annotate early created variably
26179 modified type DIEs without copying them and adjusting all
26180 references to them. Dump them again as happens for inlining
26181 which copies both the decl and the types. */
26182 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26183 in VLA bound information for example. */
26184 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26185 current_function_decl)))
26187 if (DECL_BY_REFERENCE (decl_or_origin))
26188 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26189 else
26190 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26192 return gen_formal_parameter_die (decl, origin,
26193 true /* Emit name attribute. */,
26194 context_die);
26196 case NAMESPACE_DECL:
26197 if (dwarf_version >= 3 || !dwarf_strict)
26198 gen_namespace_die (decl, context_die);
26199 break;
26201 case IMPORTED_DECL:
26202 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26203 DECL_CONTEXT (decl), context_die);
26204 break;
26206 case NAMELIST_DECL:
26207 gen_namelist_decl (DECL_NAME (decl), context_die,
26208 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26209 break;
26211 default:
26212 /* Probably some frontend-internal decl. Assume we don't care. */
26213 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26214 break;
26217 return NULL;
26220 /* Output initial debug information for global DECL. Called at the
26221 end of the parsing process.
26223 This is the initial debug generation process. As such, the DIEs
26224 generated may be incomplete. A later debug generation pass
26225 (dwarf2out_late_global_decl) will augment the information generated
26226 in this pass (e.g., with complete location info). */
26228 static void
26229 dwarf2out_early_global_decl (tree decl)
26231 set_early_dwarf s;
26233 /* gen_decl_die() will set DECL_ABSTRACT because
26234 cgraph_function_possibly_inlined_p() returns true. This is in
26235 turn will cause DW_AT_inline attributes to be set.
26237 This happens because at early dwarf generation, there is no
26238 cgraph information, causing cgraph_function_possibly_inlined_p()
26239 to return true. Trick cgraph_function_possibly_inlined_p()
26240 while we generate dwarf early. */
26241 bool save = symtab->global_info_ready;
26242 symtab->global_info_ready = true;
26244 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26245 other DECLs and they can point to template types or other things
26246 that dwarf2out can't handle when done via dwarf2out_decl. */
26247 if (TREE_CODE (decl) != TYPE_DECL
26248 && TREE_CODE (decl) != PARM_DECL)
26250 if (TREE_CODE (decl) == FUNCTION_DECL)
26252 tree save_fndecl = current_function_decl;
26254 /* For nested functions, make sure we have DIEs for the parents first
26255 so that all nested DIEs are generated at the proper scope in the
26256 first shot. */
26257 tree context = decl_function_context (decl);
26258 if (context != NULL)
26260 dw_die_ref context_die = lookup_decl_die (context);
26261 current_function_decl = context;
26263 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26264 enough so that it lands in its own context. This avoids type
26265 pruning issues later on. */
26266 if (context_die == NULL || is_declaration_die (context_die))
26267 dwarf2out_decl (context);
26270 /* Emit an abstract origin of a function first. This happens
26271 with C++ constructor clones for example and makes
26272 dwarf2out_abstract_function happy which requires the early
26273 DIE of the abstract instance to be present. */
26274 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26275 dw_die_ref origin_die;
26276 if (origin != NULL
26277 /* Do not emit the DIE multiple times but make sure to
26278 process it fully here in case we just saw a declaration. */
26279 && ((origin_die = lookup_decl_die (origin)) == NULL
26280 || is_declaration_die (origin_die)))
26282 current_function_decl = origin;
26283 dwarf2out_decl (origin);
26286 /* Emit the DIE for decl but avoid doing that multiple times. */
26287 dw_die_ref old_die;
26288 if ((old_die = lookup_decl_die (decl)) == NULL
26289 || is_declaration_die (old_die))
26291 current_function_decl = decl;
26292 dwarf2out_decl (decl);
26295 current_function_decl = save_fndecl;
26297 else
26298 dwarf2out_decl (decl);
26300 symtab->global_info_ready = save;
26303 /* Return whether EXPR is an expression with the following pattern:
26304 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26306 static bool
26307 is_trivial_indirect_ref (tree expr)
26309 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26310 return false;
26312 tree nop = TREE_OPERAND (expr, 0);
26313 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26314 return false;
26316 tree int_cst = TREE_OPERAND (nop, 0);
26317 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26320 /* Output debug information for global decl DECL. Called from
26321 toplev.c after compilation proper has finished. */
26323 static void
26324 dwarf2out_late_global_decl (tree decl)
26326 /* Fill-in any location information we were unable to determine
26327 on the first pass. */
26328 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
26330 dw_die_ref die = lookup_decl_die (decl);
26332 /* We may have to generate early debug late for LTO in case debug
26333 was not enabled at compile-time or the target doesn't support
26334 the LTO early debug scheme. */
26335 if (! die && in_lto_p)
26337 dwarf2out_decl (decl);
26338 die = lookup_decl_die (decl);
26341 if (die)
26343 /* We get called via the symtab code invoking late_global_decl
26344 for symbols that are optimized out.
26346 Do not add locations for those, except if they have a
26347 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26348 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26349 INDIRECT_REF expression, as this could generate relocations to
26350 text symbols in LTO object files, which is invalid. */
26351 varpool_node *node = varpool_node::get (decl);
26352 if ((! node || ! node->definition)
26353 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26354 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26355 tree_add_const_value_attribute_for_decl (die, decl);
26356 else
26357 add_location_or_const_value_attribute (die, decl, false);
26362 /* Output debug information for type decl DECL. Called from toplev.c
26363 and from language front ends (to record built-in types). */
26364 static void
26365 dwarf2out_type_decl (tree decl, int local)
26367 if (!local)
26369 set_early_dwarf s;
26370 dwarf2out_decl (decl);
26374 /* Output debug information for imported module or decl DECL.
26375 NAME is non-NULL name in the lexical block if the decl has been renamed.
26376 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26377 that DECL belongs to.
26378 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26379 static void
26380 dwarf2out_imported_module_or_decl_1 (tree decl,
26381 tree name,
26382 tree lexical_block,
26383 dw_die_ref lexical_block_die)
26385 expanded_location xloc;
26386 dw_die_ref imported_die = NULL;
26387 dw_die_ref at_import_die;
26389 if (TREE_CODE (decl) == IMPORTED_DECL)
26391 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26392 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26393 gcc_assert (decl);
26395 else
26396 xloc = expand_location (input_location);
26398 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26400 at_import_die = force_type_die (TREE_TYPE (decl));
26401 /* For namespace N { typedef void T; } using N::T; base_type_die
26402 returns NULL, but DW_TAG_imported_declaration requires
26403 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26404 if (!at_import_die)
26406 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26407 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26408 at_import_die = lookup_type_die (TREE_TYPE (decl));
26409 gcc_assert (at_import_die);
26412 else
26414 at_import_die = lookup_decl_die (decl);
26415 if (!at_import_die)
26417 /* If we're trying to avoid duplicate debug info, we may not have
26418 emitted the member decl for this field. Emit it now. */
26419 if (TREE_CODE (decl) == FIELD_DECL)
26421 tree type = DECL_CONTEXT (decl);
26423 if (TYPE_CONTEXT (type)
26424 && TYPE_P (TYPE_CONTEXT (type))
26425 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26426 DINFO_USAGE_DIR_USE))
26427 return;
26428 gen_type_die_for_member (type, decl,
26429 get_context_die (TYPE_CONTEXT (type)));
26431 if (TREE_CODE (decl) == NAMELIST_DECL)
26432 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26433 get_context_die (DECL_CONTEXT (decl)),
26434 NULL_TREE);
26435 else
26436 at_import_die = force_decl_die (decl);
26440 if (TREE_CODE (decl) == NAMESPACE_DECL)
26442 if (dwarf_version >= 3 || !dwarf_strict)
26443 imported_die = new_die (DW_TAG_imported_module,
26444 lexical_block_die,
26445 lexical_block);
26446 else
26447 return;
26449 else
26450 imported_die = new_die (DW_TAG_imported_declaration,
26451 lexical_block_die,
26452 lexical_block);
26454 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26455 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26456 if (debug_column_info && xloc.column)
26457 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26458 if (name)
26459 add_AT_string (imported_die, DW_AT_name,
26460 IDENTIFIER_POINTER (name));
26461 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26464 /* Output debug information for imported module or decl DECL.
26465 NAME is non-NULL name in context if the decl has been renamed.
26466 CHILD is true if decl is one of the renamed decls as part of
26467 importing whole module.
26468 IMPLICIT is set if this hook is called for an implicit import
26469 such as inline namespace. */
26471 static void
26472 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26473 bool child, bool implicit)
26475 /* dw_die_ref at_import_die; */
26476 dw_die_ref scope_die;
26478 if (debug_info_level <= DINFO_LEVEL_TERSE)
26479 return;
26481 gcc_assert (decl);
26483 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26484 should be enough, for DWARF4 and older even if we emit as extension
26485 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26486 for the benefit of consumers unaware of DW_AT_export_symbols. */
26487 if (implicit
26488 && dwarf_version >= 5
26489 && lang_hooks.decls.decl_dwarf_attribute (decl,
26490 DW_AT_export_symbols) == 1)
26491 return;
26493 set_early_dwarf s;
26495 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26496 We need decl DIE for reference and scope die. First, get DIE for the decl
26497 itself. */
26499 /* Get the scope die for decl context. Use comp_unit_die for global module
26500 or decl. If die is not found for non globals, force new die. */
26501 if (context
26502 && TYPE_P (context)
26503 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26504 return;
26506 scope_die = get_context_die (context);
26508 if (child)
26510 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26511 there is nothing we can do, here. */
26512 if (dwarf_version < 3 && dwarf_strict)
26513 return;
26515 gcc_assert (scope_die->die_child);
26516 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26517 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26518 scope_die = scope_die->die_child;
26521 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26522 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26525 /* Output debug information for namelists. */
26527 static dw_die_ref
26528 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26530 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26531 tree value;
26532 unsigned i;
26534 if (debug_info_level <= DINFO_LEVEL_TERSE)
26535 return NULL;
26537 gcc_assert (scope_die != NULL);
26538 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26539 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26541 /* If there are no item_decls, we have a nondefining namelist, e.g.
26542 with USE association; hence, set DW_AT_declaration. */
26543 if (item_decls == NULL_TREE)
26545 add_AT_flag (nml_die, DW_AT_declaration, 1);
26546 return nml_die;
26549 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26551 nml_item_ref_die = lookup_decl_die (value);
26552 if (!nml_item_ref_die)
26553 nml_item_ref_die = force_decl_die (value);
26555 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26556 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26558 return nml_die;
26562 /* Write the debugging output for DECL and return the DIE. */
26564 static void
26565 dwarf2out_decl (tree decl)
26567 dw_die_ref context_die = comp_unit_die ();
26569 switch (TREE_CODE (decl))
26571 case ERROR_MARK:
26572 return;
26574 case FUNCTION_DECL:
26575 /* If we're a nested function, initially use a parent of NULL; if we're
26576 a plain function, this will be fixed up in decls_for_scope. If
26577 we're a method, it will be ignored, since we already have a DIE. */
26578 if (decl_function_context (decl)
26579 /* But if we're in terse mode, we don't care about scope. */
26580 && debug_info_level > DINFO_LEVEL_TERSE)
26581 context_die = NULL;
26582 break;
26584 case VAR_DECL:
26585 /* For local statics lookup proper context die. */
26586 if (local_function_static (decl))
26587 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26589 /* If we are in terse mode, don't generate any DIEs to represent any
26590 variable declarations or definitions. */
26591 if (debug_info_level <= DINFO_LEVEL_TERSE)
26592 return;
26593 break;
26595 case CONST_DECL:
26596 if (debug_info_level <= DINFO_LEVEL_TERSE)
26597 return;
26598 if (!is_fortran () && !is_ada ())
26599 return;
26600 if (TREE_STATIC (decl) && decl_function_context (decl))
26601 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26602 break;
26604 case NAMESPACE_DECL:
26605 case IMPORTED_DECL:
26606 if (debug_info_level <= DINFO_LEVEL_TERSE)
26607 return;
26608 if (lookup_decl_die (decl) != NULL)
26609 return;
26610 break;
26612 case TYPE_DECL:
26613 /* Don't emit stubs for types unless they are needed by other DIEs. */
26614 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26615 return;
26617 /* Don't bother trying to generate any DIEs to represent any of the
26618 normal built-in types for the language we are compiling. */
26619 if (DECL_IS_BUILTIN (decl))
26620 return;
26622 /* If we are in terse mode, don't generate any DIEs for types. */
26623 if (debug_info_level <= DINFO_LEVEL_TERSE)
26624 return;
26626 /* If we're a function-scope tag, initially use a parent of NULL;
26627 this will be fixed up in decls_for_scope. */
26628 if (decl_function_context (decl))
26629 context_die = NULL;
26631 break;
26633 case NAMELIST_DECL:
26634 break;
26636 default:
26637 return;
26640 gen_decl_die (decl, NULL, NULL, context_die);
26642 if (flag_checking)
26644 dw_die_ref die = lookup_decl_die (decl);
26645 if (die)
26646 check_die (die);
26650 /* Write the debugging output for DECL. */
26652 static void
26653 dwarf2out_function_decl (tree decl)
26655 dwarf2out_decl (decl);
26656 call_arg_locations = NULL;
26657 call_arg_loc_last = NULL;
26658 call_site_count = -1;
26659 tail_call_site_count = -1;
26660 decl_loc_table->empty ();
26661 cached_dw_loc_list_table->empty ();
26664 /* Output a marker (i.e. a label) for the beginning of the generated code for
26665 a lexical block. */
26667 static void
26668 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26669 unsigned int blocknum)
26671 switch_to_section (current_function_section ());
26672 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26675 /* Output a marker (i.e. a label) for the end of the generated code for a
26676 lexical block. */
26678 static void
26679 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26681 switch_to_section (current_function_section ());
26682 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26685 /* Returns nonzero if it is appropriate not to emit any debugging
26686 information for BLOCK, because it doesn't contain any instructions.
26688 Don't allow this for blocks with nested functions or local classes
26689 as we would end up with orphans, and in the presence of scheduling
26690 we may end up calling them anyway. */
26692 static bool
26693 dwarf2out_ignore_block (const_tree block)
26695 tree decl;
26696 unsigned int i;
26698 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26699 if (TREE_CODE (decl) == FUNCTION_DECL
26700 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26701 return 0;
26702 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26704 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26705 if (TREE_CODE (decl) == FUNCTION_DECL
26706 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26707 return 0;
26710 return 1;
26713 /* Hash table routines for file_hash. */
26715 bool
26716 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26718 return filename_cmp (p1->filename, p2) == 0;
26721 hashval_t
26722 dwarf_file_hasher::hash (dwarf_file_data *p)
26724 return htab_hash_string (p->filename);
26727 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26728 dwarf2out.c) and return its "index". The index of each (known) filename is
26729 just a unique number which is associated with only that one filename. We
26730 need such numbers for the sake of generating labels (in the .debug_sfnames
26731 section) and references to those files numbers (in the .debug_srcinfo
26732 and .debug_macinfo sections). If the filename given as an argument is not
26733 found in our current list, add it to the list and assign it the next
26734 available unique index number. */
26736 static struct dwarf_file_data *
26737 lookup_filename (const char *file_name)
26739 struct dwarf_file_data * created;
26741 if (!file_name)
26742 return NULL;
26744 dwarf_file_data **slot
26745 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26746 INSERT);
26747 if (*slot)
26748 return *slot;
26750 created = ggc_alloc<dwarf_file_data> ();
26751 created->filename = file_name;
26752 created->emitted_number = 0;
26753 *slot = created;
26754 return created;
26757 /* If the assembler will construct the file table, then translate the compiler
26758 internal file table number into the assembler file table number, and emit
26759 a .file directive if we haven't already emitted one yet. The file table
26760 numbers are different because we prune debug info for unused variables and
26761 types, which may include filenames. */
26763 static int
26764 maybe_emit_file (struct dwarf_file_data * fd)
26766 if (! fd->emitted_number)
26768 if (last_emitted_file)
26769 fd->emitted_number = last_emitted_file->emitted_number + 1;
26770 else
26771 fd->emitted_number = 1;
26772 last_emitted_file = fd;
26774 if (output_asm_line_debug_info ())
26776 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26777 output_quoted_string (asm_out_file,
26778 remap_debug_filename (fd->filename));
26779 fputc ('\n', asm_out_file);
26783 return fd->emitted_number;
26786 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26787 That generation should happen after function debug info has been
26788 generated. The value of the attribute is the constant value of ARG. */
26790 static void
26791 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26793 die_arg_entry entry;
26795 if (!die || !arg)
26796 return;
26798 gcc_assert (early_dwarf);
26800 if (!tmpl_value_parm_die_table)
26801 vec_alloc (tmpl_value_parm_die_table, 32);
26803 entry.die = die;
26804 entry.arg = arg;
26805 vec_safe_push (tmpl_value_parm_die_table, entry);
26808 /* Return TRUE if T is an instance of generic type, FALSE
26809 otherwise. */
26811 static bool
26812 generic_type_p (tree t)
26814 if (t == NULL_TREE || !TYPE_P (t))
26815 return false;
26816 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26819 /* Schedule the generation of the generic parameter dies for the
26820 instance of generic type T. The proper generation itself is later
26821 done by gen_scheduled_generic_parms_dies. */
26823 static void
26824 schedule_generic_params_dies_gen (tree t)
26826 if (!generic_type_p (t))
26827 return;
26829 gcc_assert (early_dwarf);
26831 if (!generic_type_instances)
26832 vec_alloc (generic_type_instances, 256);
26834 vec_safe_push (generic_type_instances, t);
26837 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26838 by append_entry_to_tmpl_value_parm_die_table. This function must
26839 be called after function DIEs have been generated. */
26841 static void
26842 gen_remaining_tmpl_value_param_die_attribute (void)
26844 if (tmpl_value_parm_die_table)
26846 unsigned i, j;
26847 die_arg_entry *e;
26849 /* We do this in two phases - first get the cases we can
26850 handle during early-finish, preserving those we cannot
26851 (containing symbolic constants where we don't yet know
26852 whether we are going to output the referenced symbols).
26853 For those we try again at late-finish. */
26854 j = 0;
26855 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26857 if (!e->die->removed
26858 && !tree_add_const_value_attribute (e->die, e->arg))
26860 dw_loc_descr_ref loc = NULL;
26861 if (! early_dwarf
26862 && (dwarf_version >= 5 || !dwarf_strict))
26863 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26864 if (loc)
26865 add_AT_loc (e->die, DW_AT_location, loc);
26866 else
26867 (*tmpl_value_parm_die_table)[j++] = *e;
26870 tmpl_value_parm_die_table->truncate (j);
26874 /* Generate generic parameters DIEs for instances of generic types
26875 that have been previously scheduled by
26876 schedule_generic_params_dies_gen. This function must be called
26877 after all the types of the CU have been laid out. */
26879 static void
26880 gen_scheduled_generic_parms_dies (void)
26882 unsigned i;
26883 tree t;
26885 if (!generic_type_instances)
26886 return;
26888 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26889 if (COMPLETE_TYPE_P (t))
26890 gen_generic_params_dies (t);
26892 generic_type_instances = NULL;
26896 /* Replace DW_AT_name for the decl with name. */
26898 static void
26899 dwarf2out_set_name (tree decl, tree name)
26901 dw_die_ref die;
26902 dw_attr_node *attr;
26903 const char *dname;
26905 die = TYPE_SYMTAB_DIE (decl);
26906 if (!die)
26907 return;
26909 dname = dwarf2_name (name, 0);
26910 if (!dname)
26911 return;
26913 attr = get_AT (die, DW_AT_name);
26914 if (attr)
26916 struct indirect_string_node *node;
26918 node = find_AT_string (dname);
26919 /* replace the string. */
26920 attr->dw_attr_val.v.val_str = node;
26923 else
26924 add_name_attribute (die, dname);
26927 /* True if before or during processing of the first function being emitted. */
26928 static bool in_first_function_p = true;
26929 /* True if loc_note during dwarf2out_var_location call might still be
26930 before first real instruction at address equal to .Ltext0. */
26931 static bool maybe_at_text_label_p = true;
26932 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26933 static unsigned int first_loclabel_num_not_at_text_label;
26935 /* Look ahead for a real insn, or for a begin stmt marker. */
26937 static rtx_insn *
26938 dwarf2out_next_real_insn (rtx_insn *loc_note)
26940 rtx_insn *next_real = NEXT_INSN (loc_note);
26942 while (next_real)
26943 if (INSN_P (next_real))
26944 break;
26945 else
26946 next_real = NEXT_INSN (next_real);
26948 return next_real;
26951 /* Called by the final INSN scan whenever we see a var location. We
26952 use it to drop labels in the right places, and throw the location in
26953 our lookup table. */
26955 static void
26956 dwarf2out_var_location (rtx_insn *loc_note)
26958 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26959 struct var_loc_node *newloc;
26960 rtx_insn *next_real, *next_note;
26961 rtx_insn *call_insn = NULL;
26962 static const char *last_label;
26963 static const char *last_postcall_label;
26964 static bool last_in_cold_section_p;
26965 static rtx_insn *expected_next_loc_note;
26966 tree decl;
26967 bool var_loc_p;
26968 var_loc_view view = 0;
26970 if (!NOTE_P (loc_note))
26972 if (CALL_P (loc_note))
26974 maybe_reset_location_view (loc_note, cur_line_info_table);
26975 call_site_count++;
26976 if (SIBLING_CALL_P (loc_note))
26977 tail_call_site_count++;
26978 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
26980 call_insn = loc_note;
26981 loc_note = NULL;
26982 var_loc_p = false;
26984 next_real = dwarf2out_next_real_insn (call_insn);
26985 next_note = NULL;
26986 cached_next_real_insn = NULL;
26987 goto create_label;
26989 if (optimize == 0 && !flag_var_tracking)
26991 /* When the var-tracking pass is not running, there is no note
26992 for indirect calls whose target is compile-time known. In this
26993 case, process such calls specifically so that we generate call
26994 sites for them anyway. */
26995 rtx x = PATTERN (loc_note);
26996 if (GET_CODE (x) == PARALLEL)
26997 x = XVECEXP (x, 0, 0);
26998 if (GET_CODE (x) == SET)
26999 x = SET_SRC (x);
27000 if (GET_CODE (x) == CALL)
27001 x = XEXP (x, 0);
27002 if (!MEM_P (x)
27003 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27004 || !SYMBOL_REF_DECL (XEXP (x, 0))
27005 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27006 != FUNCTION_DECL))
27008 call_insn = loc_note;
27009 loc_note = NULL;
27010 var_loc_p = false;
27012 next_real = dwarf2out_next_real_insn (call_insn);
27013 next_note = NULL;
27014 cached_next_real_insn = NULL;
27015 goto create_label;
27019 else if (!debug_variable_location_views)
27020 gcc_unreachable ();
27021 else
27022 maybe_reset_location_view (loc_note, cur_line_info_table);
27024 return;
27027 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27028 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27029 return;
27031 /* Optimize processing a large consecutive sequence of location
27032 notes so we don't spend too much time in next_real_insn. If the
27033 next insn is another location note, remember the next_real_insn
27034 calculation for next time. */
27035 next_real = cached_next_real_insn;
27036 if (next_real)
27038 if (expected_next_loc_note != loc_note)
27039 next_real = NULL;
27042 next_note = NEXT_INSN (loc_note);
27043 if (! next_note
27044 || next_note->deleted ()
27045 || ! NOTE_P (next_note)
27046 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27047 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27048 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27049 next_note = NULL;
27051 if (! next_real)
27052 next_real = dwarf2out_next_real_insn (loc_note);
27054 if (next_note)
27056 expected_next_loc_note = next_note;
27057 cached_next_real_insn = next_real;
27059 else
27060 cached_next_real_insn = NULL;
27062 /* If there are no instructions which would be affected by this note,
27063 don't do anything. */
27064 if (var_loc_p
27065 && next_real == NULL_RTX
27066 && !NOTE_DURING_CALL_P (loc_note))
27067 return;
27069 create_label:
27071 if (next_real == NULL_RTX)
27072 next_real = get_last_insn ();
27074 /* If there were any real insns between note we processed last time
27075 and this note (or if it is the first note), clear
27076 last_{,postcall_}label so that they are not reused this time. */
27077 if (last_var_location_insn == NULL_RTX
27078 || last_var_location_insn != next_real
27079 || last_in_cold_section_p != in_cold_section_p)
27081 last_label = NULL;
27082 last_postcall_label = NULL;
27085 if (var_loc_p)
27087 const char *label
27088 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27089 view = cur_line_info_table->view;
27090 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27091 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27092 if (newloc == NULL)
27093 return;
27095 else
27097 decl = NULL_TREE;
27098 newloc = NULL;
27101 /* If there were no real insns between note we processed last time
27102 and this note, use the label we emitted last time. Otherwise
27103 create a new label and emit it. */
27104 if (last_label == NULL)
27106 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27107 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27108 loclabel_num++;
27109 last_label = ggc_strdup (loclabel);
27110 /* See if loclabel might be equal to .Ltext0. If yes,
27111 bump first_loclabel_num_not_at_text_label. */
27112 if (!have_multiple_function_sections
27113 && in_first_function_p
27114 && maybe_at_text_label_p)
27116 static rtx_insn *last_start;
27117 rtx_insn *insn;
27118 for (insn = loc_note; insn; insn = previous_insn (insn))
27119 if (insn == last_start)
27120 break;
27121 else if (!NONDEBUG_INSN_P (insn))
27122 continue;
27123 else
27125 rtx body = PATTERN (insn);
27126 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27127 continue;
27128 /* Inline asm could occupy zero bytes. */
27129 else if (GET_CODE (body) == ASM_INPUT
27130 || asm_noperands (body) >= 0)
27131 continue;
27132 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27133 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27134 continue;
27135 #endif
27136 else
27138 /* Assume insn has non-zero length. */
27139 maybe_at_text_label_p = false;
27140 break;
27143 if (maybe_at_text_label_p)
27145 last_start = loc_note;
27146 first_loclabel_num_not_at_text_label = loclabel_num;
27151 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27152 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27154 if (!var_loc_p)
27156 struct call_arg_loc_node *ca_loc
27157 = ggc_cleared_alloc<call_arg_loc_node> ();
27158 rtx_insn *prev = call_insn;
27160 ca_loc->call_arg_loc_note
27161 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27162 ca_loc->next = NULL;
27163 ca_loc->label = last_label;
27164 gcc_assert (prev
27165 && (CALL_P (prev)
27166 || (NONJUMP_INSN_P (prev)
27167 && GET_CODE (PATTERN (prev)) == SEQUENCE
27168 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27169 if (!CALL_P (prev))
27170 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27171 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27173 /* Look for a SYMBOL_REF in the "prev" instruction. */
27174 rtx x = get_call_rtx_from (PATTERN (prev));
27175 if (x)
27177 /* Try to get the call symbol, if any. */
27178 if (MEM_P (XEXP (x, 0)))
27179 x = XEXP (x, 0);
27180 /* First, look for a memory access to a symbol_ref. */
27181 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27182 && SYMBOL_REF_DECL (XEXP (x, 0))
27183 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27184 ca_loc->symbol_ref = XEXP (x, 0);
27185 /* Otherwise, look at a compile-time known user-level function
27186 declaration. */
27187 else if (MEM_P (x)
27188 && MEM_EXPR (x)
27189 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27190 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27193 ca_loc->block = insn_scope (prev);
27194 if (call_arg_locations)
27195 call_arg_loc_last->next = ca_loc;
27196 else
27197 call_arg_locations = ca_loc;
27198 call_arg_loc_last = ca_loc;
27200 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27202 newloc->label = last_label;
27203 newloc->view = view;
27205 else
27207 if (!last_postcall_label)
27209 sprintf (loclabel, "%s-1", last_label);
27210 last_postcall_label = ggc_strdup (loclabel);
27212 newloc->label = last_postcall_label;
27213 newloc->view = view;
27216 if (var_loc_p && flag_debug_asm)
27218 const char *name, *sep, *patstr;
27219 if (decl && DECL_NAME (decl))
27220 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27221 else
27222 name = "";
27223 if (NOTE_VAR_LOCATION_LOC (loc_note))
27225 sep = " => ";
27226 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27228 else
27230 sep = " ";
27231 patstr = "RESET";
27233 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27234 name, sep, patstr);
27237 last_var_location_insn = next_real;
27238 last_in_cold_section_p = in_cold_section_p;
27241 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27242 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27243 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27244 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27245 BLOCK_FRAGMENT_ORIGIN links. */
27246 static bool
27247 block_within_block_p (tree block, tree outer, bool bothways)
27249 if (block == outer)
27250 return true;
27252 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27253 for (tree context = BLOCK_SUPERCONTEXT (block);
27254 context != outer;
27255 context = BLOCK_SUPERCONTEXT (context))
27256 if (!context || TREE_CODE (context) != BLOCK)
27257 return false;
27259 if (!bothways)
27260 return true;
27262 /* Now check that each block is actually referenced by its
27263 parent. */
27264 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27265 context = BLOCK_SUPERCONTEXT (context))
27267 if (BLOCK_FRAGMENT_ORIGIN (context))
27269 gcc_assert (!BLOCK_SUBBLOCKS (context));
27270 context = BLOCK_FRAGMENT_ORIGIN (context);
27272 for (tree sub = BLOCK_SUBBLOCKS (context);
27273 sub != block;
27274 sub = BLOCK_CHAIN (sub))
27275 if (!sub)
27276 return false;
27277 if (context == outer)
27278 return true;
27279 else
27280 block = context;
27284 /* Called during final while assembling the marker of the entry point
27285 for an inlined function. */
27287 static void
27288 dwarf2out_inline_entry (tree block)
27290 gcc_assert (debug_inline_points);
27292 /* If we can't represent it, don't bother. */
27293 if (!(dwarf_version >= 3 || !dwarf_strict))
27294 return;
27296 gcc_assert (DECL_P (block_ultimate_origin (block)));
27298 /* Sanity check the block tree. This would catch a case in which
27299 BLOCK got removed from the tree reachable from the outermost
27300 lexical block, but got retained in markers. It would still link
27301 back to its parents, but some ancestor would be missing a link
27302 down the path to the sub BLOCK. If the block got removed, its
27303 BLOCK_NUMBER will not be a usable value. */
27304 if (flag_checking)
27305 gcc_assert (block_within_block_p (block,
27306 DECL_INITIAL (current_function_decl),
27307 true));
27309 gcc_assert (inlined_function_outer_scope_p (block));
27310 gcc_assert (!BLOCK_DIE (block));
27312 if (BLOCK_FRAGMENT_ORIGIN (block))
27313 block = BLOCK_FRAGMENT_ORIGIN (block);
27314 /* Can the entry point ever not be at the beginning of an
27315 unfragmented lexical block? */
27316 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27317 || (cur_line_info_table
27318 && !ZERO_VIEW_P (cur_line_info_table->view))))
27319 return;
27321 if (!inline_entry_data_table)
27322 inline_entry_data_table
27323 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27326 inline_entry_data **iedp
27327 = inline_entry_data_table->find_slot_with_hash (block,
27328 htab_hash_pointer (block),
27329 INSERT);
27330 if (*iedp)
27331 /* ??? Ideally, we'd record all entry points for the same inlined
27332 function (some may have been duplicated by e.g. unrolling), but
27333 we have no way to represent that ATM. */
27334 return;
27336 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27337 ied->block = block;
27338 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27339 ied->label_num = BLOCK_NUMBER (block);
27340 if (cur_line_info_table)
27341 ied->view = cur_line_info_table->view;
27343 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27345 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27346 BLOCK_NUMBER (block));
27347 ASM_OUTPUT_LABEL (asm_out_file, label);
27350 /* Called from finalize_size_functions for size functions so that their body
27351 can be encoded in the debug info to describe the layout of variable-length
27352 structures. */
27354 static void
27355 dwarf2out_size_function (tree decl)
27357 function_to_dwarf_procedure (decl);
27360 /* Note in one location list that text section has changed. */
27363 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27365 var_loc_list *list = *slot;
27366 if (list->first)
27367 list->last_before_switch
27368 = list->last->next ? list->last->next : list->last;
27369 return 1;
27372 /* Note in all location lists that text section has changed. */
27374 static void
27375 var_location_switch_text_section (void)
27377 if (decl_loc_table == NULL)
27378 return;
27380 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27383 /* Create a new line number table. */
27385 static dw_line_info_table *
27386 new_line_info_table (void)
27388 dw_line_info_table *table;
27390 table = ggc_cleared_alloc<dw_line_info_table> ();
27391 table->file_num = 1;
27392 table->line_num = 1;
27393 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27394 RESET_NEXT_VIEW (table->view);
27396 return table;
27399 /* Lookup the "current" table into which we emit line info, so
27400 that we don't have to do it for every source line. */
27402 static void
27403 set_cur_line_info_table (section *sec)
27405 dw_line_info_table *table;
27407 if (sec == text_section)
27408 table = text_section_line_info;
27409 else if (sec == cold_text_section)
27411 table = cold_text_section_line_info;
27412 if (!table)
27414 cold_text_section_line_info = table = new_line_info_table ();
27415 table->end_label = cold_end_label;
27418 else
27420 const char *end_label;
27422 if (crtl->has_bb_partition)
27424 if (in_cold_section_p)
27425 end_label = crtl->subsections.cold_section_end_label;
27426 else
27427 end_label = crtl->subsections.hot_section_end_label;
27429 else
27431 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27432 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27433 current_function_funcdef_no);
27434 end_label = ggc_strdup (label);
27437 table = new_line_info_table ();
27438 table->end_label = end_label;
27440 vec_safe_push (separate_line_info, table);
27443 if (output_asm_line_debug_info ())
27444 table->is_stmt = (cur_line_info_table
27445 ? cur_line_info_table->is_stmt
27446 : DWARF_LINE_DEFAULT_IS_STMT_START);
27447 cur_line_info_table = table;
27451 /* We need to reset the locations at the beginning of each
27452 function. We can't do this in the end_function hook, because the
27453 declarations that use the locations won't have been output when
27454 that hook is called. Also compute have_multiple_function_sections here. */
27456 static void
27457 dwarf2out_begin_function (tree fun)
27459 section *sec = function_section (fun);
27461 if (sec != text_section)
27462 have_multiple_function_sections = true;
27464 if (crtl->has_bb_partition && !cold_text_section)
27466 gcc_assert (current_function_decl == fun);
27467 cold_text_section = unlikely_text_section ();
27468 switch_to_section (cold_text_section);
27469 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27470 switch_to_section (sec);
27473 dwarf2out_note_section_used ();
27474 call_site_count = 0;
27475 tail_call_site_count = 0;
27477 set_cur_line_info_table (sec);
27480 /* Helper function of dwarf2out_end_function, called only after emitting
27481 the very first function into assembly. Check if some .debug_loc range
27482 might end with a .LVL* label that could be equal to .Ltext0.
27483 In that case we must force using absolute addresses in .debug_loc ranges,
27484 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27485 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27486 list terminator.
27487 Set have_multiple_function_sections to true in that case and
27488 terminate htab traversal. */
27491 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27493 var_loc_list *entry = *slot;
27494 struct var_loc_node *node;
27496 node = entry->first;
27497 if (node && node->next && node->next->label)
27499 unsigned int i;
27500 const char *label = node->next->label;
27501 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27503 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27505 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27506 if (strcmp (label, loclabel) == 0)
27508 have_multiple_function_sections = true;
27509 return 0;
27513 return 1;
27516 /* Hook called after emitting a function into assembly.
27517 This does something only for the very first function emitted. */
27519 static void
27520 dwarf2out_end_function (unsigned int)
27522 if (in_first_function_p
27523 && !have_multiple_function_sections
27524 && first_loclabel_num_not_at_text_label
27525 && decl_loc_table)
27526 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27527 in_first_function_p = false;
27528 maybe_at_text_label_p = false;
27531 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27532 front-ends register a translation unit even before dwarf2out_init is
27533 called. */
27534 static tree main_translation_unit = NULL_TREE;
27536 /* Hook called by front-ends after they built their main translation unit.
27537 Associate comp_unit_die to UNIT. */
27539 static void
27540 dwarf2out_register_main_translation_unit (tree unit)
27542 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27543 && main_translation_unit == NULL_TREE);
27544 main_translation_unit = unit;
27545 /* If dwarf2out_init has not been called yet, it will perform the association
27546 itself looking at main_translation_unit. */
27547 if (decl_die_table != NULL)
27548 equate_decl_number_to_die (unit, comp_unit_die ());
27551 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27553 static void
27554 push_dw_line_info_entry (dw_line_info_table *table,
27555 enum dw_line_info_opcode opcode, unsigned int val)
27557 dw_line_info_entry e;
27558 e.opcode = opcode;
27559 e.val = val;
27560 vec_safe_push (table->entries, e);
27563 /* Output a label to mark the beginning of a source code line entry
27564 and record information relating to this source line, in
27565 'line_info_table' for later output of the .debug_line section. */
27566 /* ??? The discriminator parameter ought to be unsigned. */
27568 static void
27569 dwarf2out_source_line (unsigned int line, unsigned int column,
27570 const char *filename,
27571 int discriminator, bool is_stmt)
27573 unsigned int file_num;
27574 dw_line_info_table *table;
27576 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
27577 return;
27579 /* The discriminator column was added in dwarf4. Simplify the below
27580 by simply removing it if we're not supposed to output it. */
27581 if (dwarf_version < 4 && dwarf_strict)
27582 discriminator = 0;
27584 if (!debug_column_info)
27585 column = 0;
27587 table = cur_line_info_table;
27588 file_num = maybe_emit_file (lookup_filename (filename));
27590 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27591 the debugger has used the second (possibly duplicate) line number
27592 at the beginning of the function to mark the end of the prologue.
27593 We could eliminate any other duplicates within the function. For
27594 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27595 that second line number entry. */
27596 /* Recall that this end-of-prologue indication is *not* the same thing
27597 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27598 to which the hook corresponds, follows the last insn that was
27599 emitted by gen_prologue. What we need is to precede the first insn
27600 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27601 insn that corresponds to something the user wrote. These may be
27602 very different locations once scheduling is enabled. */
27604 if (0 && file_num == table->file_num
27605 && line == table->line_num
27606 && column == table->column_num
27607 && discriminator == table->discrim_num
27608 && is_stmt == table->is_stmt)
27609 return;
27611 switch_to_section (current_function_section ());
27613 /* If requested, emit something human-readable. */
27614 if (flag_debug_asm)
27616 if (debug_column_info)
27617 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27618 filename, line, column);
27619 else
27620 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27621 filename, line);
27624 if (output_asm_line_debug_info ())
27626 /* Emit the .loc directive understood by GNU as. */
27627 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27628 file_num, line, is_stmt, discriminator */
27629 fputs ("\t.loc ", asm_out_file);
27630 fprint_ul (asm_out_file, file_num);
27631 putc (' ', asm_out_file);
27632 fprint_ul (asm_out_file, line);
27633 putc (' ', asm_out_file);
27634 fprint_ul (asm_out_file, column);
27636 if (is_stmt != table->is_stmt)
27638 fputs (" is_stmt ", asm_out_file);
27639 putc (is_stmt ? '1' : '0', asm_out_file);
27641 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27643 gcc_assert (discriminator > 0);
27644 fputs (" discriminator ", asm_out_file);
27645 fprint_ul (asm_out_file, (unsigned long) discriminator);
27647 if (debug_variable_location_views)
27649 static var_loc_view lvugid;
27650 if (!lvugid)
27652 gcc_assert (!zero_view_p);
27653 zero_view_p = BITMAP_GGC_ALLOC ();
27654 bitmap_set_bit (zero_view_p, 0);
27656 if (!RESETTING_VIEW_P (table->view))
27658 /* When we're using the assembler to compute view
27659 numbers, we output symbolic labels after "view" in
27660 .loc directives, and the assembler will set them for
27661 us, so that we can refer to the view numbers in
27662 location lists. The only exceptions are when we know
27663 a view will be zero: "-0" is a forced reset, used
27664 e.g. in the beginning of functions, whereas "0" tells
27665 the assembler to check that there was a PC change
27666 since the previous view, in a way that implicitly
27667 resets the next view. */
27668 fputs (" view ", asm_out_file);
27669 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27670 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27671 assemble_name (asm_out_file, label);
27672 table->view = ++lvugid;
27674 else
27676 if (!table->in_use)
27677 fputs (" view -0", asm_out_file);
27678 else
27679 fputs (" view 0", asm_out_file);
27680 /* Mark the present view as a zero view. Earlier debug
27681 binds may have already added its id to loclists to be
27682 emitted later, so we can't reuse the id for something
27683 else. However, it's good to know whether a view is
27684 known to be zero, because then we may be able to
27685 optimize out locviews that are all zeros, so take
27686 note of it in zero_view_p. */
27687 bitmap_set_bit (zero_view_p, lvugid);
27688 table->view = ++lvugid;
27691 putc ('\n', asm_out_file);
27693 else
27695 unsigned int label_num = ++line_info_label_num;
27697 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27699 if (debug_variable_location_views && table->view)
27700 push_dw_line_info_entry (table, LI_adv_address, label_num);
27701 else
27702 push_dw_line_info_entry (table, LI_set_address, label_num);
27703 if (debug_variable_location_views)
27705 if (flag_debug_asm)
27706 fprintf (asm_out_file, "\t%s view %s%d\n",
27707 ASM_COMMENT_START,
27708 table->in_use ? "" : "-",
27709 table->view);
27710 table->view++;
27712 if (file_num != table->file_num)
27713 push_dw_line_info_entry (table, LI_set_file, file_num);
27714 if (discriminator != table->discrim_num)
27715 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27716 if (is_stmt != table->is_stmt)
27717 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27718 push_dw_line_info_entry (table, LI_set_line, line);
27719 if (debug_column_info)
27720 push_dw_line_info_entry (table, LI_set_column, column);
27723 table->file_num = file_num;
27724 table->line_num = line;
27725 table->column_num = column;
27726 table->discrim_num = discriminator;
27727 table->is_stmt = is_stmt;
27728 table->in_use = true;
27731 /* Record the beginning of a new source file. */
27733 static void
27734 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27736 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27738 macinfo_entry e;
27739 e.code = DW_MACINFO_start_file;
27740 e.lineno = lineno;
27741 e.info = ggc_strdup (filename);
27742 vec_safe_push (macinfo_table, e);
27746 /* Record the end of a source file. */
27748 static void
27749 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27751 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27753 macinfo_entry e;
27754 e.code = DW_MACINFO_end_file;
27755 e.lineno = lineno;
27756 e.info = NULL;
27757 vec_safe_push (macinfo_table, e);
27761 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27762 the tail part of the directive line, i.e. the part which is past the
27763 initial whitespace, #, whitespace, directive-name, whitespace part. */
27765 static void
27766 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27767 const char *buffer ATTRIBUTE_UNUSED)
27769 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27771 macinfo_entry e;
27772 /* Insert a dummy first entry to be able to optimize the whole
27773 predefined macro block using DW_MACRO_import. */
27774 if (macinfo_table->is_empty () && lineno <= 1)
27776 e.code = 0;
27777 e.lineno = 0;
27778 e.info = NULL;
27779 vec_safe_push (macinfo_table, e);
27781 e.code = DW_MACINFO_define;
27782 e.lineno = lineno;
27783 e.info = ggc_strdup (buffer);
27784 vec_safe_push (macinfo_table, e);
27788 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27789 the tail part of the directive line, i.e. the part which is past the
27790 initial whitespace, #, whitespace, directive-name, whitespace part. */
27792 static void
27793 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27794 const char *buffer ATTRIBUTE_UNUSED)
27796 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27798 macinfo_entry e;
27799 /* Insert a dummy first entry to be able to optimize the whole
27800 predefined macro block using DW_MACRO_import. */
27801 if (macinfo_table->is_empty () && lineno <= 1)
27803 e.code = 0;
27804 e.lineno = 0;
27805 e.info = NULL;
27806 vec_safe_push (macinfo_table, e);
27808 e.code = DW_MACINFO_undef;
27809 e.lineno = lineno;
27810 e.info = ggc_strdup (buffer);
27811 vec_safe_push (macinfo_table, e);
27815 /* Helpers to manipulate hash table of CUs. */
27817 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27819 static inline hashval_t hash (const macinfo_entry *);
27820 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27823 inline hashval_t
27824 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27826 return htab_hash_string (entry->info);
27829 inline bool
27830 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
27831 const macinfo_entry *entry2)
27833 return !strcmp (entry1->info, entry2->info);
27836 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
27838 /* Output a single .debug_macinfo entry. */
27840 static void
27841 output_macinfo_op (macinfo_entry *ref)
27843 int file_num;
27844 size_t len;
27845 struct indirect_string_node *node;
27846 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27847 struct dwarf_file_data *fd;
27849 switch (ref->code)
27851 case DW_MACINFO_start_file:
27852 fd = lookup_filename (ref->info);
27853 file_num = maybe_emit_file (fd);
27854 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27855 dw2_asm_output_data_uleb128 (ref->lineno,
27856 "Included from line number %lu",
27857 (unsigned long) ref->lineno);
27858 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27859 break;
27860 case DW_MACINFO_end_file:
27861 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27862 break;
27863 case DW_MACINFO_define:
27864 case DW_MACINFO_undef:
27865 len = strlen (ref->info) + 1;
27866 if (!dwarf_strict
27867 && len > DWARF_OFFSET_SIZE
27868 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27869 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27871 ref->code = ref->code == DW_MACINFO_define
27872 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27873 output_macinfo_op (ref);
27874 return;
27876 dw2_asm_output_data (1, ref->code,
27877 ref->code == DW_MACINFO_define
27878 ? "Define macro" : "Undefine macro");
27879 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27880 (unsigned long) ref->lineno);
27881 dw2_asm_output_nstring (ref->info, -1, "The macro");
27882 break;
27883 case DW_MACRO_define_strp:
27884 case DW_MACRO_undef_strp:
27885 node = find_AT_string (ref->info);
27886 gcc_assert (node
27887 && (node->form == DW_FORM_strp
27888 || node->form == DW_FORM_GNU_str_index));
27889 dw2_asm_output_data (1, ref->code,
27890 ref->code == DW_MACRO_define_strp
27891 ? "Define macro strp"
27892 : "Undefine macro strp");
27893 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27894 (unsigned long) ref->lineno);
27895 if (node->form == DW_FORM_strp)
27896 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27897 debug_str_section, "The macro: \"%s\"",
27898 ref->info);
27899 else
27900 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27901 ref->info);
27902 break;
27903 case DW_MACRO_import:
27904 dw2_asm_output_data (1, ref->code, "Import");
27905 ASM_GENERATE_INTERNAL_LABEL (label,
27906 DEBUG_MACRO_SECTION_LABEL,
27907 ref->lineno + macinfo_label_base);
27908 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27909 break;
27910 default:
27911 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27912 ASM_COMMENT_START, (unsigned long) ref->code);
27913 break;
27917 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27918 other compilation unit .debug_macinfo sections. IDX is the first
27919 index of a define/undef, return the number of ops that should be
27920 emitted in a comdat .debug_macinfo section and emit
27921 a DW_MACRO_import entry referencing it.
27922 If the define/undef entry should be emitted normally, return 0. */
27924 static unsigned
27925 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27926 macinfo_hash_type **macinfo_htab)
27928 macinfo_entry *first, *second, *cur, *inc;
27929 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27930 unsigned char checksum[16];
27931 struct md5_ctx ctx;
27932 char *grp_name, *tail;
27933 const char *base;
27934 unsigned int i, count, encoded_filename_len, linebuf_len;
27935 macinfo_entry **slot;
27937 first = &(*macinfo_table)[idx];
27938 second = &(*macinfo_table)[idx + 1];
27940 /* Optimize only if there are at least two consecutive define/undef ops,
27941 and either all of them are before first DW_MACINFO_start_file
27942 with lineno {0,1} (i.e. predefined macro block), or all of them are
27943 in some included header file. */
27944 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27945 return 0;
27946 if (vec_safe_is_empty (files))
27948 if (first->lineno > 1 || second->lineno > 1)
27949 return 0;
27951 else if (first->lineno == 0)
27952 return 0;
27954 /* Find the last define/undef entry that can be grouped together
27955 with first and at the same time compute md5 checksum of their
27956 codes, linenumbers and strings. */
27957 md5_init_ctx (&ctx);
27958 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27959 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27960 break;
27961 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27962 break;
27963 else
27965 unsigned char code = cur->code;
27966 md5_process_bytes (&code, 1, &ctx);
27967 checksum_uleb128 (cur->lineno, &ctx);
27968 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27970 md5_finish_ctx (&ctx, checksum);
27971 count = i - idx;
27973 /* From the containing include filename (if any) pick up just
27974 usable characters from its basename. */
27975 if (vec_safe_is_empty (files))
27976 base = "";
27977 else
27978 base = lbasename (files->last ().info);
27979 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27980 if (ISIDNUM (base[i]) || base[i] == '.')
27981 encoded_filename_len++;
27982 /* Count . at the end. */
27983 if (encoded_filename_len)
27984 encoded_filename_len++;
27986 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27987 linebuf_len = strlen (linebuf);
27989 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27990 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27991 + 16 * 2 + 1);
27992 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27993 tail = grp_name + 4;
27994 if (encoded_filename_len)
27996 for (i = 0; base[i]; i++)
27997 if (ISIDNUM (base[i]) || base[i] == '.')
27998 *tail++ = base[i];
27999 *tail++ = '.';
28001 memcpy (tail, linebuf, linebuf_len);
28002 tail += linebuf_len;
28003 *tail++ = '.';
28004 for (i = 0; i < 16; i++)
28005 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28007 /* Construct a macinfo_entry for DW_MACRO_import
28008 in the empty vector entry before the first define/undef. */
28009 inc = &(*macinfo_table)[idx - 1];
28010 inc->code = DW_MACRO_import;
28011 inc->lineno = 0;
28012 inc->info = ggc_strdup (grp_name);
28013 if (!*macinfo_htab)
28014 *macinfo_htab = new macinfo_hash_type (10);
28015 /* Avoid emitting duplicates. */
28016 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28017 if (*slot != NULL)
28019 inc->code = 0;
28020 inc->info = NULL;
28021 /* If such an entry has been used before, just emit
28022 a DW_MACRO_import op. */
28023 inc = *slot;
28024 output_macinfo_op (inc);
28025 /* And clear all macinfo_entry in the range to avoid emitting them
28026 in the second pass. */
28027 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28029 cur->code = 0;
28030 cur->info = NULL;
28033 else
28035 *slot = inc;
28036 inc->lineno = (*macinfo_htab)->elements ();
28037 output_macinfo_op (inc);
28039 return count;
28042 /* Save any strings needed by the macinfo table in the debug str
28043 table. All strings must be collected into the table by the time
28044 index_string is called. */
28046 static void
28047 save_macinfo_strings (void)
28049 unsigned len;
28050 unsigned i;
28051 macinfo_entry *ref;
28053 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28055 switch (ref->code)
28057 /* Match the logic in output_macinfo_op to decide on
28058 indirect strings. */
28059 case DW_MACINFO_define:
28060 case DW_MACINFO_undef:
28061 len = strlen (ref->info) + 1;
28062 if (!dwarf_strict
28063 && len > DWARF_OFFSET_SIZE
28064 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28065 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28066 set_indirect_string (find_AT_string (ref->info));
28067 break;
28068 case DW_MACRO_define_strp:
28069 case DW_MACRO_undef_strp:
28070 set_indirect_string (find_AT_string (ref->info));
28071 break;
28072 default:
28073 break;
28078 /* Output macinfo section(s). */
28080 static void
28081 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28083 unsigned i;
28084 unsigned long length = vec_safe_length (macinfo_table);
28085 macinfo_entry *ref;
28086 vec<macinfo_entry, va_gc> *files = NULL;
28087 macinfo_hash_type *macinfo_htab = NULL;
28088 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28090 if (! length)
28091 return;
28093 /* output_macinfo* uses these interchangeably. */
28094 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28095 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28096 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28097 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28099 /* AIX Assembler inserts the length, so adjust the reference to match the
28100 offset expected by debuggers. */
28101 strcpy (dl_section_ref, debug_line_label);
28102 if (XCOFF_DEBUGGING_INFO)
28103 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28105 /* For .debug_macro emit the section header. */
28106 if (!dwarf_strict || dwarf_version >= 5)
28108 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28109 "DWARF macro version number");
28110 if (DWARF_OFFSET_SIZE == 8)
28111 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28112 else
28113 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28114 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28115 debug_line_section, NULL);
28118 /* In the first loop, it emits the primary .debug_macinfo section
28119 and after each emitted op the macinfo_entry is cleared.
28120 If a longer range of define/undef ops can be optimized using
28121 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28122 the vector before the first define/undef in the range and the
28123 whole range of define/undef ops is not emitted and kept. */
28124 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28126 switch (ref->code)
28128 case DW_MACINFO_start_file:
28129 vec_safe_push (files, *ref);
28130 break;
28131 case DW_MACINFO_end_file:
28132 if (!vec_safe_is_empty (files))
28133 files->pop ();
28134 break;
28135 case DW_MACINFO_define:
28136 case DW_MACINFO_undef:
28137 if ((!dwarf_strict || dwarf_version >= 5)
28138 && HAVE_COMDAT_GROUP
28139 && vec_safe_length (files) != 1
28140 && i > 0
28141 && i + 1 < length
28142 && (*macinfo_table)[i - 1].code == 0)
28144 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28145 if (count)
28147 i += count - 1;
28148 continue;
28151 break;
28152 case 0:
28153 /* A dummy entry may be inserted at the beginning to be able
28154 to optimize the whole block of predefined macros. */
28155 if (i == 0)
28156 continue;
28157 default:
28158 break;
28160 output_macinfo_op (ref);
28161 ref->info = NULL;
28162 ref->code = 0;
28165 if (!macinfo_htab)
28166 return;
28168 /* Save the number of transparent includes so we can adjust the
28169 label number for the fat LTO object DWARF. */
28170 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28172 delete macinfo_htab;
28173 macinfo_htab = NULL;
28175 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28176 terminate the current chain and switch to a new comdat .debug_macinfo
28177 section and emit the define/undef entries within it. */
28178 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28179 switch (ref->code)
28181 case 0:
28182 continue;
28183 case DW_MACRO_import:
28185 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28186 tree comdat_key = get_identifier (ref->info);
28187 /* Terminate the previous .debug_macinfo section. */
28188 dw2_asm_output_data (1, 0, "End compilation unit");
28189 targetm.asm_out.named_section (debug_macinfo_section_name,
28190 SECTION_DEBUG
28191 | SECTION_LINKONCE
28192 | (early_lto_debug
28193 ? SECTION_EXCLUDE : 0),
28194 comdat_key);
28195 ASM_GENERATE_INTERNAL_LABEL (label,
28196 DEBUG_MACRO_SECTION_LABEL,
28197 ref->lineno + macinfo_label_base);
28198 ASM_OUTPUT_LABEL (asm_out_file, label);
28199 ref->code = 0;
28200 ref->info = NULL;
28201 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28202 "DWARF macro version number");
28203 if (DWARF_OFFSET_SIZE == 8)
28204 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28205 else
28206 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28208 break;
28209 case DW_MACINFO_define:
28210 case DW_MACINFO_undef:
28211 output_macinfo_op (ref);
28212 ref->code = 0;
28213 ref->info = NULL;
28214 break;
28215 default:
28216 gcc_unreachable ();
28219 macinfo_label_base += macinfo_label_base_adj;
28222 /* Initialize the various sections and labels for dwarf output and prefix
28223 them with PREFIX if non-NULL. Returns the generation (zero based
28224 number of times function was called). */
28226 static unsigned
28227 init_sections_and_labels (bool early_lto_debug)
28229 /* As we may get called multiple times have a generation count for
28230 labels. */
28231 static unsigned generation = 0;
28233 if (early_lto_debug)
28235 if (!dwarf_split_debug_info)
28237 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28238 SECTION_DEBUG | SECTION_EXCLUDE,
28239 NULL);
28240 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28241 SECTION_DEBUG | SECTION_EXCLUDE,
28242 NULL);
28243 debug_macinfo_section_name
28244 = ((dwarf_strict && dwarf_version < 5)
28245 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28246 debug_macinfo_section = get_section (debug_macinfo_section_name,
28247 SECTION_DEBUG
28248 | SECTION_EXCLUDE, NULL);
28249 /* For macro info we have to refer to a debug_line section, so
28250 similar to split-dwarf emit a skeleton one for early debug. */
28251 debug_skeleton_line_section
28252 = get_section (DEBUG_LTO_LINE_SECTION,
28253 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28254 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28255 DEBUG_SKELETON_LINE_SECTION_LABEL,
28256 generation);
28258 else
28260 /* ??? Which of the following do we need early? */
28261 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28262 SECTION_DEBUG | SECTION_EXCLUDE,
28263 NULL);
28264 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28265 SECTION_DEBUG | SECTION_EXCLUDE,
28266 NULL);
28267 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28268 SECTION_DEBUG
28269 | SECTION_EXCLUDE, NULL);
28270 debug_skeleton_abbrev_section
28271 = get_section (DEBUG_LTO_ABBREV_SECTION,
28272 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28273 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28274 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28275 generation);
28277 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28278 stay in the main .o, but the skeleton_line goes into the split
28279 off dwo. */
28280 debug_skeleton_line_section
28281 = get_section (DEBUG_LTO_LINE_SECTION,
28282 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28283 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28284 DEBUG_SKELETON_LINE_SECTION_LABEL,
28285 generation);
28286 debug_str_offsets_section
28287 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28288 SECTION_DEBUG | SECTION_EXCLUDE,
28289 NULL);
28290 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28291 DEBUG_SKELETON_INFO_SECTION_LABEL,
28292 generation);
28293 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28294 DEBUG_STR_DWO_SECTION_FLAGS,
28295 NULL);
28296 debug_macinfo_section_name
28297 = ((dwarf_strict && dwarf_version < 5)
28298 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28299 debug_macinfo_section = get_section (debug_macinfo_section_name,
28300 SECTION_DEBUG | SECTION_EXCLUDE,
28301 NULL);
28303 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28304 DEBUG_STR_SECTION_FLAGS
28305 | SECTION_EXCLUDE, NULL);
28306 if (!dwarf_split_debug_info && !dwarf2out_as_loc_support)
28307 debug_line_str_section
28308 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28309 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28311 else
28313 if (!dwarf_split_debug_info)
28315 debug_info_section = get_section (DEBUG_INFO_SECTION,
28316 SECTION_DEBUG, NULL);
28317 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28318 SECTION_DEBUG, NULL);
28319 debug_loc_section = get_section (dwarf_version >= 5
28320 ? DEBUG_LOCLISTS_SECTION
28321 : DEBUG_LOC_SECTION,
28322 SECTION_DEBUG, NULL);
28323 debug_macinfo_section_name
28324 = ((dwarf_strict && dwarf_version < 5)
28325 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28326 debug_macinfo_section = get_section (debug_macinfo_section_name,
28327 SECTION_DEBUG, NULL);
28329 else
28331 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28332 SECTION_DEBUG | SECTION_EXCLUDE,
28333 NULL);
28334 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28335 SECTION_DEBUG | SECTION_EXCLUDE,
28336 NULL);
28337 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28338 SECTION_DEBUG, NULL);
28339 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28340 SECTION_DEBUG, NULL);
28341 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28342 SECTION_DEBUG, NULL);
28343 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28344 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28345 generation);
28347 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28348 stay in the main .o, but the skeleton_line goes into the
28349 split off dwo. */
28350 debug_skeleton_line_section
28351 = get_section (DEBUG_DWO_LINE_SECTION,
28352 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28353 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28354 DEBUG_SKELETON_LINE_SECTION_LABEL,
28355 generation);
28356 debug_str_offsets_section
28357 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28358 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28359 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28360 DEBUG_SKELETON_INFO_SECTION_LABEL,
28361 generation);
28362 debug_loc_section = get_section (dwarf_version >= 5
28363 ? DEBUG_DWO_LOCLISTS_SECTION
28364 : DEBUG_DWO_LOC_SECTION,
28365 SECTION_DEBUG | SECTION_EXCLUDE,
28366 NULL);
28367 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28368 DEBUG_STR_DWO_SECTION_FLAGS,
28369 NULL);
28370 debug_macinfo_section_name
28371 = ((dwarf_strict && dwarf_version < 5)
28372 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28373 debug_macinfo_section = get_section (debug_macinfo_section_name,
28374 SECTION_DEBUG | SECTION_EXCLUDE,
28375 NULL);
28377 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28378 SECTION_DEBUG, NULL);
28379 debug_line_section = get_section (DEBUG_LINE_SECTION,
28380 SECTION_DEBUG, NULL);
28381 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28382 SECTION_DEBUG, NULL);
28383 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28384 SECTION_DEBUG, NULL);
28385 debug_str_section = get_section (DEBUG_STR_SECTION,
28386 DEBUG_STR_SECTION_FLAGS, NULL);
28387 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28388 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28389 DEBUG_STR_SECTION_FLAGS, NULL);
28391 debug_ranges_section = get_section (dwarf_version >= 5
28392 ? DEBUG_RNGLISTS_SECTION
28393 : DEBUG_RANGES_SECTION,
28394 SECTION_DEBUG, NULL);
28395 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28396 SECTION_DEBUG, NULL);
28399 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28400 DEBUG_ABBREV_SECTION_LABEL, generation);
28401 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28402 DEBUG_INFO_SECTION_LABEL, generation);
28403 info_section_emitted = false;
28404 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28405 DEBUG_LINE_SECTION_LABEL, generation);
28406 /* There are up to 4 unique ranges labels per generation.
28407 See also output_rnglists. */
28408 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28409 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28410 if (dwarf_version >= 5 && dwarf_split_debug_info)
28411 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28412 DEBUG_RANGES_SECTION_LABEL,
28413 1 + generation * 4);
28414 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28415 DEBUG_ADDR_SECTION_LABEL, generation);
28416 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28417 (dwarf_strict && dwarf_version < 5)
28418 ? DEBUG_MACINFO_SECTION_LABEL
28419 : DEBUG_MACRO_SECTION_LABEL, generation);
28420 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28421 generation);
28423 ++generation;
28424 return generation - 1;
28427 /* Set up for Dwarf output at the start of compilation. */
28429 static void
28430 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28432 /* Allocate the file_table. */
28433 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28435 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28436 /* Allocate the decl_die_table. */
28437 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28439 /* Allocate the decl_loc_table. */
28440 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28442 /* Allocate the cached_dw_loc_list_table. */
28443 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28445 /* Allocate the initial hunk of the decl_scope_table. */
28446 vec_alloc (decl_scope_table, 256);
28448 /* Allocate the initial hunk of the abbrev_die_table. */
28449 vec_alloc (abbrev_die_table, 256);
28450 /* Zero-th entry is allocated, but unused. */
28451 abbrev_die_table->quick_push (NULL);
28453 /* Allocate the dwarf_proc_stack_usage_map. */
28454 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28456 /* Allocate the pubtypes and pubnames vectors. */
28457 vec_alloc (pubname_table, 32);
28458 vec_alloc (pubtype_table, 32);
28460 vec_alloc (incomplete_types, 64);
28462 vec_alloc (used_rtx_array, 32);
28464 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28465 vec_alloc (macinfo_table, 64);
28466 #endif
28468 /* If front-ends already registered a main translation unit but we were not
28469 ready to perform the association, do this now. */
28470 if (main_translation_unit != NULL_TREE)
28471 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28474 /* Called before compile () starts outputtting functions, variables
28475 and toplevel asms into assembly. */
28477 static void
28478 dwarf2out_assembly_start (void)
28480 if (text_section_line_info)
28481 return;
28483 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28484 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28485 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28486 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28487 COLD_TEXT_SECTION_LABEL, 0);
28488 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28490 switch_to_section (text_section);
28491 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28492 #endif
28494 /* Make sure the line number table for .text always exists. */
28495 text_section_line_info = new_line_info_table ();
28496 text_section_line_info->end_label = text_end_label;
28498 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28499 cur_line_info_table = text_section_line_info;
28500 #endif
28502 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28503 && dwarf2out_do_cfi_asm ()
28504 && !dwarf2out_do_eh_frame ())
28505 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28508 /* A helper function for dwarf2out_finish called through
28509 htab_traverse. Assign a string its index. All strings must be
28510 collected into the table by the time index_string is called,
28511 because the indexing code relies on htab_traverse to traverse nodes
28512 in the same order for each run. */
28515 index_string (indirect_string_node **h, unsigned int *index)
28517 indirect_string_node *node = *h;
28519 find_string_form (node);
28520 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28522 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28523 node->index = *index;
28524 *index += 1;
28526 return 1;
28529 /* A helper function for output_indirect_strings called through
28530 htab_traverse. Output the offset to a string and update the
28531 current offset. */
28534 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28536 indirect_string_node *node = *h;
28538 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28540 /* Assert that this node has been assigned an index. */
28541 gcc_assert (node->index != NO_INDEX_ASSIGNED
28542 && node->index != NOT_INDEXED);
28543 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28544 "indexed string 0x%x: %s", node->index, node->str);
28545 *offset += strlen (node->str) + 1;
28547 return 1;
28550 /* A helper function for dwarf2out_finish called through
28551 htab_traverse. Output the indexed string. */
28554 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28556 struct indirect_string_node *node = *h;
28558 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28560 /* Assert that the strings are output in the same order as their
28561 indexes were assigned. */
28562 gcc_assert (*cur_idx == node->index);
28563 assemble_string (node->str, strlen (node->str) + 1);
28564 *cur_idx += 1;
28566 return 1;
28569 /* A helper function for dwarf2out_finish called through
28570 htab_traverse. Emit one queued .debug_str string. */
28573 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28575 struct indirect_string_node *node = *h;
28577 node->form = find_string_form (node);
28578 if (node->form == form && node->refcount > 0)
28580 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28581 assemble_string (node->str, strlen (node->str) + 1);
28584 return 1;
28587 /* Output the indexed string table. */
28589 static void
28590 output_indirect_strings (void)
28592 switch_to_section (debug_str_section);
28593 if (!dwarf_split_debug_info)
28594 debug_str_hash->traverse<enum dwarf_form,
28595 output_indirect_string> (DW_FORM_strp);
28596 else
28598 unsigned int offset = 0;
28599 unsigned int cur_idx = 0;
28601 if (skeleton_debug_str_hash)
28602 skeleton_debug_str_hash->traverse<enum dwarf_form,
28603 output_indirect_string> (DW_FORM_strp);
28605 switch_to_section (debug_str_offsets_section);
28606 debug_str_hash->traverse_noresize
28607 <unsigned int *, output_index_string_offset> (&offset);
28608 switch_to_section (debug_str_dwo_section);
28609 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28610 (&cur_idx);
28614 /* Callback for htab_traverse to assign an index to an entry in the
28615 table, and to write that entry to the .debug_addr section. */
28618 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28620 addr_table_entry *entry = *slot;
28622 if (entry->refcount == 0)
28624 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28625 || entry->index == NOT_INDEXED);
28626 return 1;
28629 gcc_assert (entry->index == *cur_index);
28630 (*cur_index)++;
28632 switch (entry->kind)
28634 case ate_kind_rtx:
28635 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28636 "0x%x", entry->index);
28637 break;
28638 case ate_kind_rtx_dtprel:
28639 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28640 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28641 DWARF2_ADDR_SIZE,
28642 entry->addr.rtl);
28643 fputc ('\n', asm_out_file);
28644 break;
28645 case ate_kind_label:
28646 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28647 "0x%x", entry->index);
28648 break;
28649 default:
28650 gcc_unreachable ();
28652 return 1;
28655 /* Produce the .debug_addr section. */
28657 static void
28658 output_addr_table (void)
28660 unsigned int index = 0;
28661 if (addr_index_table == NULL || addr_index_table->size () == 0)
28662 return;
28664 switch_to_section (debug_addr_section);
28665 addr_index_table
28666 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28669 #if ENABLE_ASSERT_CHECKING
28670 /* Verify that all marks are clear. */
28672 static void
28673 verify_marks_clear (dw_die_ref die)
28675 dw_die_ref c;
28677 gcc_assert (! die->die_mark);
28678 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28680 #endif /* ENABLE_ASSERT_CHECKING */
28682 /* Clear the marks for a die and its children.
28683 Be cool if the mark isn't set. */
28685 static void
28686 prune_unmark_dies (dw_die_ref die)
28688 dw_die_ref c;
28690 if (die->die_mark)
28691 die->die_mark = 0;
28692 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28695 /* Given LOC that is referenced by a DIE we're marking as used, find all
28696 referenced DWARF procedures it references and mark them as used. */
28698 static void
28699 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28701 for (; loc != NULL; loc = loc->dw_loc_next)
28702 switch (loc->dw_loc_opc)
28704 case DW_OP_implicit_pointer:
28705 case DW_OP_convert:
28706 case DW_OP_reinterpret:
28707 case DW_OP_GNU_implicit_pointer:
28708 case DW_OP_GNU_convert:
28709 case DW_OP_GNU_reinterpret:
28710 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28711 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28712 break;
28713 case DW_OP_GNU_variable_value:
28714 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28716 dw_die_ref ref
28717 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28718 if (ref == NULL)
28719 break;
28720 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28721 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28722 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28724 /* FALLTHRU */
28725 case DW_OP_call2:
28726 case DW_OP_call4:
28727 case DW_OP_call_ref:
28728 case DW_OP_const_type:
28729 case DW_OP_GNU_const_type:
28730 case DW_OP_GNU_parameter_ref:
28731 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28732 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28733 break;
28734 case DW_OP_regval_type:
28735 case DW_OP_deref_type:
28736 case DW_OP_GNU_regval_type:
28737 case DW_OP_GNU_deref_type:
28738 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28739 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28740 break;
28741 case DW_OP_entry_value:
28742 case DW_OP_GNU_entry_value:
28743 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28744 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28745 break;
28746 default:
28747 break;
28751 /* Given DIE that we're marking as used, find any other dies
28752 it references as attributes and mark them as used. */
28754 static void
28755 prune_unused_types_walk_attribs (dw_die_ref die)
28757 dw_attr_node *a;
28758 unsigned ix;
28760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28762 switch (AT_class (a))
28764 /* Make sure DWARF procedures referenced by location descriptions will
28765 get emitted. */
28766 case dw_val_class_loc:
28767 prune_unused_types_walk_loc_descr (AT_loc (a));
28768 break;
28769 case dw_val_class_loc_list:
28770 for (dw_loc_list_ref list = AT_loc_list (a);
28771 list != NULL;
28772 list = list->dw_loc_next)
28773 prune_unused_types_walk_loc_descr (list->expr);
28774 break;
28776 case dw_val_class_view_list:
28777 /* This points to a loc_list in another attribute, so it's
28778 already covered. */
28779 break;
28781 case dw_val_class_die_ref:
28782 /* A reference to another DIE.
28783 Make sure that it will get emitted.
28784 If it was broken out into a comdat group, don't follow it. */
28785 if (! AT_ref (a)->comdat_type_p
28786 || a->dw_attr == DW_AT_specification)
28787 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
28788 break;
28790 case dw_val_class_str:
28791 /* Set the string's refcount to 0 so that prune_unused_types_mark
28792 accounts properly for it. */
28793 a->dw_attr_val.v.val_str->refcount = 0;
28794 break;
28796 default:
28797 break;
28802 /* Mark the generic parameters and arguments children DIEs of DIE. */
28804 static void
28805 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28807 dw_die_ref c;
28809 if (die == NULL || die->die_child == NULL)
28810 return;
28811 c = die->die_child;
28814 if (is_template_parameter (c))
28815 prune_unused_types_mark (c, 1);
28816 c = c->die_sib;
28817 } while (c && c != die->die_child);
28820 /* Mark DIE as being used. If DOKIDS is true, then walk down
28821 to DIE's children. */
28823 static void
28824 prune_unused_types_mark (dw_die_ref die, int dokids)
28826 dw_die_ref c;
28828 if (die->die_mark == 0)
28830 /* We haven't done this node yet. Mark it as used. */
28831 die->die_mark = 1;
28832 /* If this is the DIE of a generic type instantiation,
28833 mark the children DIEs that describe its generic parms and
28834 args. */
28835 prune_unused_types_mark_generic_parms_dies (die);
28837 /* We also have to mark its parents as used.
28838 (But we don't want to mark our parent's kids due to this,
28839 unless it is a class.) */
28840 if (die->die_parent)
28841 prune_unused_types_mark (die->die_parent,
28842 class_scope_p (die->die_parent));
28844 /* Mark any referenced nodes. */
28845 prune_unused_types_walk_attribs (die);
28847 /* If this node is a specification,
28848 also mark the definition, if it exists. */
28849 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28850 prune_unused_types_mark (die->die_definition, 1);
28853 if (dokids && die->die_mark != 2)
28855 /* We need to walk the children, but haven't done so yet.
28856 Remember that we've walked the kids. */
28857 die->die_mark = 2;
28859 /* If this is an array type, we need to make sure our
28860 kids get marked, even if they're types. If we're
28861 breaking out types into comdat sections, do this
28862 for all type definitions. */
28863 if (die->die_tag == DW_TAG_array_type
28864 || (use_debug_types
28865 && is_type_die (die) && ! is_declaration_die (die)))
28866 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28867 else
28868 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28872 /* For local classes, look if any static member functions were emitted
28873 and if so, mark them. */
28875 static void
28876 prune_unused_types_walk_local_classes (dw_die_ref die)
28878 dw_die_ref c;
28880 if (die->die_mark == 2)
28881 return;
28883 switch (die->die_tag)
28885 case DW_TAG_structure_type:
28886 case DW_TAG_union_type:
28887 case DW_TAG_class_type:
28888 break;
28890 case DW_TAG_subprogram:
28891 if (!get_AT_flag (die, DW_AT_declaration)
28892 || die->die_definition != NULL)
28893 prune_unused_types_mark (die, 1);
28894 return;
28896 default:
28897 return;
28900 /* Mark children. */
28901 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28904 /* Walk the tree DIE and mark types that we actually use. */
28906 static void
28907 prune_unused_types_walk (dw_die_ref die)
28909 dw_die_ref c;
28911 /* Don't do anything if this node is already marked and
28912 children have been marked as well. */
28913 if (die->die_mark == 2)
28914 return;
28916 switch (die->die_tag)
28918 case DW_TAG_structure_type:
28919 case DW_TAG_union_type:
28920 case DW_TAG_class_type:
28921 if (die->die_perennial_p)
28922 break;
28924 for (c = die->die_parent; c; c = c->die_parent)
28925 if (c->die_tag == DW_TAG_subprogram)
28926 break;
28928 /* Finding used static member functions inside of classes
28929 is needed just for local classes, because for other classes
28930 static member function DIEs with DW_AT_specification
28931 are emitted outside of the DW_TAG_*_type. If we ever change
28932 it, we'd need to call this even for non-local classes. */
28933 if (c)
28934 prune_unused_types_walk_local_classes (die);
28936 /* It's a type node --- don't mark it. */
28937 return;
28939 case DW_TAG_const_type:
28940 case DW_TAG_packed_type:
28941 case DW_TAG_pointer_type:
28942 case DW_TAG_reference_type:
28943 case DW_TAG_rvalue_reference_type:
28944 case DW_TAG_volatile_type:
28945 case DW_TAG_typedef:
28946 case DW_TAG_array_type:
28947 case DW_TAG_interface_type:
28948 case DW_TAG_friend:
28949 case DW_TAG_enumeration_type:
28950 case DW_TAG_subroutine_type:
28951 case DW_TAG_string_type:
28952 case DW_TAG_set_type:
28953 case DW_TAG_subrange_type:
28954 case DW_TAG_ptr_to_member_type:
28955 case DW_TAG_file_type:
28956 /* Type nodes are useful only when other DIEs reference them --- don't
28957 mark them. */
28958 /* FALLTHROUGH */
28960 case DW_TAG_dwarf_procedure:
28961 /* Likewise for DWARF procedures. */
28963 if (die->die_perennial_p)
28964 break;
28966 return;
28968 default:
28969 /* Mark everything else. */
28970 break;
28973 if (die->die_mark == 0)
28975 die->die_mark = 1;
28977 /* Now, mark any dies referenced from here. */
28978 prune_unused_types_walk_attribs (die);
28981 die->die_mark = 2;
28983 /* Mark children. */
28984 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28987 /* Increment the string counts on strings referred to from DIE's
28988 attributes. */
28990 static void
28991 prune_unused_types_update_strings (dw_die_ref die)
28993 dw_attr_node *a;
28994 unsigned ix;
28996 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28997 if (AT_class (a) == dw_val_class_str)
28999 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29000 s->refcount++;
29001 /* Avoid unnecessarily putting strings that are used less than
29002 twice in the hash table. */
29003 if (s->refcount
29004 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29006 indirect_string_node **slot
29007 = debug_str_hash->find_slot_with_hash (s->str,
29008 htab_hash_string (s->str),
29009 INSERT);
29010 gcc_assert (*slot == NULL);
29011 *slot = s;
29016 /* Mark DIE and its children as removed. */
29018 static void
29019 mark_removed (dw_die_ref die)
29021 dw_die_ref c;
29022 die->removed = true;
29023 FOR_EACH_CHILD (die, c, mark_removed (c));
29026 /* Remove from the tree DIE any dies that aren't marked. */
29028 static void
29029 prune_unused_types_prune (dw_die_ref die)
29031 dw_die_ref c;
29033 gcc_assert (die->die_mark);
29034 prune_unused_types_update_strings (die);
29036 if (! die->die_child)
29037 return;
29039 c = die->die_child;
29040 do {
29041 dw_die_ref prev = c, next;
29042 for (c = c->die_sib; ! c->die_mark; c = next)
29043 if (c == die->die_child)
29045 /* No marked children between 'prev' and the end of the list. */
29046 if (prev == c)
29047 /* No marked children at all. */
29048 die->die_child = NULL;
29049 else
29051 prev->die_sib = c->die_sib;
29052 die->die_child = prev;
29054 c->die_sib = NULL;
29055 mark_removed (c);
29056 return;
29058 else
29060 next = c->die_sib;
29061 c->die_sib = NULL;
29062 mark_removed (c);
29065 if (c != prev->die_sib)
29066 prev->die_sib = c;
29067 prune_unused_types_prune (c);
29068 } while (c != die->die_child);
29071 /* Remove dies representing declarations that we never use. */
29073 static void
29074 prune_unused_types (void)
29076 unsigned int i;
29077 limbo_die_node *node;
29078 comdat_type_node *ctnode;
29079 pubname_entry *pub;
29080 dw_die_ref base_type;
29082 #if ENABLE_ASSERT_CHECKING
29083 /* All the marks should already be clear. */
29084 verify_marks_clear (comp_unit_die ());
29085 for (node = limbo_die_list; node; node = node->next)
29086 verify_marks_clear (node->die);
29087 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29088 verify_marks_clear (ctnode->root_die);
29089 #endif /* ENABLE_ASSERT_CHECKING */
29091 /* Mark types that are used in global variables. */
29092 premark_types_used_by_global_vars ();
29094 /* Set the mark on nodes that are actually used. */
29095 prune_unused_types_walk (comp_unit_die ());
29096 for (node = limbo_die_list; node; node = node->next)
29097 prune_unused_types_walk (node->die);
29098 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29100 prune_unused_types_walk (ctnode->root_die);
29101 prune_unused_types_mark (ctnode->type_die, 1);
29104 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29105 are unusual in that they are pubnames that are the children of pubtypes.
29106 They should only be marked via their parent DW_TAG_enumeration_type die,
29107 not as roots in themselves. */
29108 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29109 if (pub->die->die_tag != DW_TAG_enumerator)
29110 prune_unused_types_mark (pub->die, 1);
29111 for (i = 0; base_types.iterate (i, &base_type); i++)
29112 prune_unused_types_mark (base_type, 1);
29114 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29115 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29116 callees). */
29117 cgraph_node *cnode;
29118 FOR_EACH_FUNCTION (cnode)
29119 if (cnode->referred_to_p (false))
29121 dw_die_ref die = lookup_decl_die (cnode->decl);
29122 if (die == NULL || die->die_mark)
29123 continue;
29124 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29125 if (e->caller != cnode
29126 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29128 prune_unused_types_mark (die, 1);
29129 break;
29133 if (debug_str_hash)
29134 debug_str_hash->empty ();
29135 if (skeleton_debug_str_hash)
29136 skeleton_debug_str_hash->empty ();
29137 prune_unused_types_prune (comp_unit_die ());
29138 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29140 node = *pnode;
29141 if (!node->die->die_mark)
29142 *pnode = node->next;
29143 else
29145 prune_unused_types_prune (node->die);
29146 pnode = &node->next;
29149 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29150 prune_unused_types_prune (ctnode->root_die);
29152 /* Leave the marks clear. */
29153 prune_unmark_dies (comp_unit_die ());
29154 for (node = limbo_die_list; node; node = node->next)
29155 prune_unmark_dies (node->die);
29156 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29157 prune_unmark_dies (ctnode->root_die);
29160 /* Helpers to manipulate hash table of comdat type units. */
29162 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29164 static inline hashval_t hash (const comdat_type_node *);
29165 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29168 inline hashval_t
29169 comdat_type_hasher::hash (const comdat_type_node *type_node)
29171 hashval_t h;
29172 memcpy (&h, type_node->signature, sizeof (h));
29173 return h;
29176 inline bool
29177 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29178 const comdat_type_node *type_node_2)
29180 return (! memcmp (type_node_1->signature, type_node_2->signature,
29181 DWARF_TYPE_SIGNATURE_SIZE));
29184 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29185 to the location it would have been added, should we know its
29186 DECL_ASSEMBLER_NAME when we added other attributes. This will
29187 probably improve compactness of debug info, removing equivalent
29188 abbrevs, and hide any differences caused by deferring the
29189 computation of the assembler name, triggered by e.g. PCH. */
29191 static inline void
29192 move_linkage_attr (dw_die_ref die)
29194 unsigned ix = vec_safe_length (die->die_attr);
29195 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29197 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29198 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29200 while (--ix > 0)
29202 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29204 if (prev->dw_attr == DW_AT_decl_line
29205 || prev->dw_attr == DW_AT_decl_column
29206 || prev->dw_attr == DW_AT_name)
29207 break;
29210 if (ix != vec_safe_length (die->die_attr) - 1)
29212 die->die_attr->pop ();
29213 die->die_attr->quick_insert (ix, linkage);
29217 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29218 referenced from typed stack ops and count how often they are used. */
29220 static void
29221 mark_base_types (dw_loc_descr_ref loc)
29223 dw_die_ref base_type = NULL;
29225 for (; loc; loc = loc->dw_loc_next)
29227 switch (loc->dw_loc_opc)
29229 case DW_OP_regval_type:
29230 case DW_OP_deref_type:
29231 case DW_OP_GNU_regval_type:
29232 case DW_OP_GNU_deref_type:
29233 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29234 break;
29235 case DW_OP_convert:
29236 case DW_OP_reinterpret:
29237 case DW_OP_GNU_convert:
29238 case DW_OP_GNU_reinterpret:
29239 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29240 continue;
29241 /* FALLTHRU */
29242 case DW_OP_const_type:
29243 case DW_OP_GNU_const_type:
29244 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29245 break;
29246 case DW_OP_entry_value:
29247 case DW_OP_GNU_entry_value:
29248 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29249 continue;
29250 default:
29251 continue;
29253 gcc_assert (base_type->die_parent == comp_unit_die ());
29254 if (base_type->die_mark)
29255 base_type->die_mark++;
29256 else
29258 base_types.safe_push (base_type);
29259 base_type->die_mark = 1;
29264 /* Comparison function for sorting marked base types. */
29266 static int
29267 base_type_cmp (const void *x, const void *y)
29269 dw_die_ref dx = *(const dw_die_ref *) x;
29270 dw_die_ref dy = *(const dw_die_ref *) y;
29271 unsigned int byte_size1, byte_size2;
29272 unsigned int encoding1, encoding2;
29273 unsigned int align1, align2;
29274 if (dx->die_mark > dy->die_mark)
29275 return -1;
29276 if (dx->die_mark < dy->die_mark)
29277 return 1;
29278 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29279 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29280 if (byte_size1 < byte_size2)
29281 return 1;
29282 if (byte_size1 > byte_size2)
29283 return -1;
29284 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29285 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29286 if (encoding1 < encoding2)
29287 return 1;
29288 if (encoding1 > encoding2)
29289 return -1;
29290 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29291 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29292 if (align1 < align2)
29293 return 1;
29294 if (align1 > align2)
29295 return -1;
29296 return 0;
29299 /* Move base types marked by mark_base_types as early as possible
29300 in the CU, sorted by decreasing usage count both to make the
29301 uleb128 references as small as possible and to make sure they
29302 will have die_offset already computed by calc_die_sizes when
29303 sizes of typed stack loc ops is computed. */
29305 static void
29306 move_marked_base_types (void)
29308 unsigned int i;
29309 dw_die_ref base_type, die, c;
29311 if (base_types.is_empty ())
29312 return;
29314 /* Sort by decreasing usage count, they will be added again in that
29315 order later on. */
29316 base_types.qsort (base_type_cmp);
29317 die = comp_unit_die ();
29318 c = die->die_child;
29321 dw_die_ref prev = c;
29322 c = c->die_sib;
29323 while (c->die_mark)
29325 remove_child_with_prev (c, prev);
29326 /* As base types got marked, there must be at least
29327 one node other than DW_TAG_base_type. */
29328 gcc_assert (die->die_child != NULL);
29329 c = prev->die_sib;
29332 while (c != die->die_child);
29333 gcc_assert (die->die_child);
29334 c = die->die_child;
29335 for (i = 0; base_types.iterate (i, &base_type); i++)
29337 base_type->die_mark = 0;
29338 base_type->die_sib = c->die_sib;
29339 c->die_sib = base_type;
29340 c = base_type;
29344 /* Helper function for resolve_addr, attempt to resolve
29345 one CONST_STRING, return true if successful. Similarly verify that
29346 SYMBOL_REFs refer to variables emitted in the current CU. */
29348 static bool
29349 resolve_one_addr (rtx *addr)
29351 rtx rtl = *addr;
29353 if (GET_CODE (rtl) == CONST_STRING)
29355 size_t len = strlen (XSTR (rtl, 0)) + 1;
29356 tree t = build_string (len, XSTR (rtl, 0));
29357 tree tlen = size_int (len - 1);
29358 TREE_TYPE (t)
29359 = build_array_type (char_type_node, build_index_type (tlen));
29360 rtl = lookup_constant_def (t);
29361 if (!rtl || !MEM_P (rtl))
29362 return false;
29363 rtl = XEXP (rtl, 0);
29364 if (GET_CODE (rtl) == SYMBOL_REF
29365 && SYMBOL_REF_DECL (rtl)
29366 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29367 return false;
29368 vec_safe_push (used_rtx_array, rtl);
29369 *addr = rtl;
29370 return true;
29373 if (GET_CODE (rtl) == SYMBOL_REF
29374 && SYMBOL_REF_DECL (rtl))
29376 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29378 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29379 return false;
29381 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29382 return false;
29385 if (GET_CODE (rtl) == CONST)
29387 subrtx_ptr_iterator::array_type array;
29388 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29389 if (!resolve_one_addr (*iter))
29390 return false;
29393 return true;
29396 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29397 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29398 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29400 static rtx
29401 string_cst_pool_decl (tree t)
29403 rtx rtl = output_constant_def (t, 1);
29404 unsigned char *array;
29405 dw_loc_descr_ref l;
29406 tree decl;
29407 size_t len;
29408 dw_die_ref ref;
29410 if (!rtl || !MEM_P (rtl))
29411 return NULL_RTX;
29412 rtl = XEXP (rtl, 0);
29413 if (GET_CODE (rtl) != SYMBOL_REF
29414 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29415 return NULL_RTX;
29417 decl = SYMBOL_REF_DECL (rtl);
29418 if (!lookup_decl_die (decl))
29420 len = TREE_STRING_LENGTH (t);
29421 vec_safe_push (used_rtx_array, rtl);
29422 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29423 array = ggc_vec_alloc<unsigned char> (len);
29424 memcpy (array, TREE_STRING_POINTER (t), len);
29425 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29426 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29427 l->dw_loc_oprnd2.v.val_vec.length = len;
29428 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29429 l->dw_loc_oprnd2.v.val_vec.array = array;
29430 add_AT_loc (ref, DW_AT_location, l);
29431 equate_decl_number_to_die (decl, ref);
29433 return rtl;
29436 /* Helper function of resolve_addr_in_expr. LOC is
29437 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29438 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29439 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29440 with DW_OP_implicit_pointer if possible
29441 and return true, if unsuccessful, return false. */
29443 static bool
29444 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29446 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29447 HOST_WIDE_INT offset = 0;
29448 dw_die_ref ref = NULL;
29449 tree decl;
29451 if (GET_CODE (rtl) == CONST
29452 && GET_CODE (XEXP (rtl, 0)) == PLUS
29453 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29455 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29456 rtl = XEXP (XEXP (rtl, 0), 0);
29458 if (GET_CODE (rtl) == CONST_STRING)
29460 size_t len = strlen (XSTR (rtl, 0)) + 1;
29461 tree t = build_string (len, XSTR (rtl, 0));
29462 tree tlen = size_int (len - 1);
29464 TREE_TYPE (t)
29465 = build_array_type (char_type_node, build_index_type (tlen));
29466 rtl = string_cst_pool_decl (t);
29467 if (!rtl)
29468 return false;
29470 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29472 decl = SYMBOL_REF_DECL (rtl);
29473 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29475 ref = lookup_decl_die (decl);
29476 if (ref && (get_AT (ref, DW_AT_location)
29477 || get_AT (ref, DW_AT_const_value)))
29479 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29480 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29481 loc->dw_loc_oprnd1.val_entry = NULL;
29482 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29483 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29484 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29485 loc->dw_loc_oprnd2.v.val_int = offset;
29486 return true;
29490 return false;
29493 /* Helper function for resolve_addr, handle one location
29494 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29495 the location list couldn't be resolved. */
29497 static bool
29498 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29500 dw_loc_descr_ref keep = NULL;
29501 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29502 switch (loc->dw_loc_opc)
29504 case DW_OP_addr:
29505 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29507 if ((prev == NULL
29508 || prev->dw_loc_opc == DW_OP_piece
29509 || prev->dw_loc_opc == DW_OP_bit_piece)
29510 && loc->dw_loc_next
29511 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29512 && (!dwarf_strict || dwarf_version >= 5)
29513 && optimize_one_addr_into_implicit_ptr (loc))
29514 break;
29515 return false;
29517 break;
29518 case DW_OP_GNU_addr_index:
29519 case DW_OP_GNU_const_index:
29520 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
29521 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
29523 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29524 if (!resolve_one_addr (&rtl))
29525 return false;
29526 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29527 loc->dw_loc_oprnd1.val_entry
29528 = add_addr_table_entry (rtl, ate_kind_rtx);
29530 break;
29531 case DW_OP_const4u:
29532 case DW_OP_const8u:
29533 if (loc->dtprel
29534 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29535 return false;
29536 break;
29537 case DW_OP_plus_uconst:
29538 if (size_of_loc_descr (loc)
29539 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29541 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29543 dw_loc_descr_ref repl
29544 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29545 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29546 add_loc_descr (&repl, loc->dw_loc_next);
29547 *loc = *repl;
29549 break;
29550 case DW_OP_implicit_value:
29551 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29552 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29553 return false;
29554 break;
29555 case DW_OP_implicit_pointer:
29556 case DW_OP_GNU_implicit_pointer:
29557 case DW_OP_GNU_parameter_ref:
29558 case DW_OP_GNU_variable_value:
29559 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29561 dw_die_ref ref
29562 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29563 if (ref == NULL)
29564 return false;
29565 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29566 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29567 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29569 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29571 if (prev == NULL
29572 && loc->dw_loc_next == NULL
29573 && AT_class (a) == dw_val_class_loc)
29574 switch (a->dw_attr)
29576 /* Following attributes allow both exprloc and reference,
29577 so if the whole expression is DW_OP_GNU_variable_value
29578 alone we could transform it into reference. */
29579 case DW_AT_byte_size:
29580 case DW_AT_bit_size:
29581 case DW_AT_lower_bound:
29582 case DW_AT_upper_bound:
29583 case DW_AT_bit_stride:
29584 case DW_AT_count:
29585 case DW_AT_allocated:
29586 case DW_AT_associated:
29587 case DW_AT_byte_stride:
29588 a->dw_attr_val.val_class = dw_val_class_die_ref;
29589 a->dw_attr_val.val_entry = NULL;
29590 a->dw_attr_val.v.val_die_ref.die
29591 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29592 a->dw_attr_val.v.val_die_ref.external = 0;
29593 return true;
29594 default:
29595 break;
29597 if (dwarf_strict)
29598 return false;
29600 break;
29601 case DW_OP_const_type:
29602 case DW_OP_regval_type:
29603 case DW_OP_deref_type:
29604 case DW_OP_convert:
29605 case DW_OP_reinterpret:
29606 case DW_OP_GNU_const_type:
29607 case DW_OP_GNU_regval_type:
29608 case DW_OP_GNU_deref_type:
29609 case DW_OP_GNU_convert:
29610 case DW_OP_GNU_reinterpret:
29611 while (loc->dw_loc_next
29612 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29613 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29615 dw_die_ref base1, base2;
29616 unsigned enc1, enc2, size1, size2;
29617 if (loc->dw_loc_opc == DW_OP_regval_type
29618 || loc->dw_loc_opc == DW_OP_deref_type
29619 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29620 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29621 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29622 else if (loc->dw_loc_oprnd1.val_class
29623 == dw_val_class_unsigned_const)
29624 break;
29625 else
29626 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29627 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29628 == dw_val_class_unsigned_const)
29629 break;
29630 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29631 gcc_assert (base1->die_tag == DW_TAG_base_type
29632 && base2->die_tag == DW_TAG_base_type);
29633 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29634 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29635 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29636 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29637 if (size1 == size2
29638 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29639 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29640 && loc != keep)
29641 || enc1 == enc2))
29643 /* Optimize away next DW_OP_convert after
29644 adjusting LOC's base type die reference. */
29645 if (loc->dw_loc_opc == DW_OP_regval_type
29646 || loc->dw_loc_opc == DW_OP_deref_type
29647 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29648 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29649 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29650 else
29651 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29652 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29653 continue;
29655 /* Don't change integer DW_OP_convert after e.g. floating
29656 point typed stack entry. */
29657 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29658 keep = loc->dw_loc_next;
29659 break;
29661 break;
29662 default:
29663 break;
29665 return true;
29668 /* Helper function of resolve_addr. DIE had DW_AT_location of
29669 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29670 and DW_OP_addr couldn't be resolved. resolve_addr has already
29671 removed the DW_AT_location attribute. This function attempts to
29672 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29673 to it or DW_AT_const_value attribute, if possible. */
29675 static void
29676 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29678 if (!VAR_P (decl)
29679 || lookup_decl_die (decl) != die
29680 || DECL_EXTERNAL (decl)
29681 || !TREE_STATIC (decl)
29682 || DECL_INITIAL (decl) == NULL_TREE
29683 || DECL_P (DECL_INITIAL (decl))
29684 || get_AT (die, DW_AT_const_value))
29685 return;
29687 tree init = DECL_INITIAL (decl);
29688 HOST_WIDE_INT offset = 0;
29689 /* For variables that have been optimized away and thus
29690 don't have a memory location, see if we can emit
29691 DW_AT_const_value instead. */
29692 if (tree_add_const_value_attribute (die, init))
29693 return;
29694 if (dwarf_strict && dwarf_version < 5)
29695 return;
29696 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29697 and ADDR_EXPR refers to a decl that has DW_AT_location or
29698 DW_AT_const_value (but isn't addressable, otherwise
29699 resolving the original DW_OP_addr wouldn't fail), see if
29700 we can add DW_OP_implicit_pointer. */
29701 STRIP_NOPS (init);
29702 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29703 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29705 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29706 init = TREE_OPERAND (init, 0);
29707 STRIP_NOPS (init);
29709 if (TREE_CODE (init) != ADDR_EXPR)
29710 return;
29711 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29712 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29713 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29714 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29715 && TREE_OPERAND (init, 0) != decl))
29717 dw_die_ref ref;
29718 dw_loc_descr_ref l;
29720 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29722 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29723 if (!rtl)
29724 return;
29725 decl = SYMBOL_REF_DECL (rtl);
29727 else
29728 decl = TREE_OPERAND (init, 0);
29729 ref = lookup_decl_die (decl);
29730 if (ref == NULL
29731 || (!get_AT (ref, DW_AT_location)
29732 && !get_AT (ref, DW_AT_const_value)))
29733 return;
29734 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29735 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29736 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29737 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29738 add_AT_loc (die, DW_AT_location, l);
29742 /* Return NULL if l is a DWARF expression, or first op that is not
29743 valid DWARF expression. */
29745 static dw_loc_descr_ref
29746 non_dwarf_expression (dw_loc_descr_ref l)
29748 while (l)
29750 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29751 return l;
29752 switch (l->dw_loc_opc)
29754 case DW_OP_regx:
29755 case DW_OP_implicit_value:
29756 case DW_OP_stack_value:
29757 case DW_OP_implicit_pointer:
29758 case DW_OP_GNU_implicit_pointer:
29759 case DW_OP_GNU_parameter_ref:
29760 case DW_OP_piece:
29761 case DW_OP_bit_piece:
29762 return l;
29763 default:
29764 break;
29766 l = l->dw_loc_next;
29768 return NULL;
29771 /* Return adjusted copy of EXPR:
29772 If it is empty DWARF expression, return it.
29773 If it is valid non-empty DWARF expression,
29774 return copy of EXPR with DW_OP_deref appended to it.
29775 If it is DWARF expression followed by DW_OP_reg{N,x}, return
29776 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
29777 If it is DWARF expression followed by DW_OP_stack_value, return
29778 copy of the DWARF expression without anything appended.
29779 Otherwise, return NULL. */
29781 static dw_loc_descr_ref
29782 copy_deref_exprloc (dw_loc_descr_ref expr)
29784 dw_loc_descr_ref tail = NULL;
29786 if (expr == NULL)
29787 return NULL;
29789 dw_loc_descr_ref l = non_dwarf_expression (expr);
29790 if (l && l->dw_loc_next)
29791 return NULL;
29793 if (l)
29795 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29796 tail = new_loc_descr ((enum dwarf_location_atom)
29797 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
29798 0, 0);
29799 else
29800 switch (l->dw_loc_opc)
29802 case DW_OP_regx:
29803 tail = new_loc_descr (DW_OP_bregx,
29804 l->dw_loc_oprnd1.v.val_unsigned, 0);
29805 break;
29806 case DW_OP_stack_value:
29807 break;
29808 default:
29809 return NULL;
29812 else
29813 tail = new_loc_descr (DW_OP_deref, 0, 0);
29815 dw_loc_descr_ref ret = NULL, *p = &ret;
29816 while (expr != l)
29818 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29819 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29820 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29821 p = &(*p)->dw_loc_next;
29822 expr = expr->dw_loc_next;
29824 *p = tail;
29825 return ret;
29828 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29829 reference to a variable or argument, adjust it if needed and return:
29830 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29831 attribute if present should be removed
29832 0 keep the attribute perhaps with minor modifications, no need to rescan
29833 1 if the attribute has been successfully adjusted. */
29835 static int
29836 optimize_string_length (dw_attr_node *a)
29838 dw_loc_descr_ref l = AT_loc (a), lv;
29839 dw_die_ref die;
29840 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29842 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
29843 die = lookup_decl_die (decl);
29844 if (die)
29846 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29847 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29848 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29850 else
29851 return -1;
29853 else
29854 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29856 /* DWARF5 allows reference class, so we can then reference the DIE.
29857 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29858 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29860 a->dw_attr_val.val_class = dw_val_class_die_ref;
29861 a->dw_attr_val.val_entry = NULL;
29862 a->dw_attr_val.v.val_die_ref.die = die;
29863 a->dw_attr_val.v.val_die_ref.external = 0;
29864 return 0;
29867 dw_attr_node *av = get_AT (die, DW_AT_location);
29868 dw_loc_list_ref d;
29869 bool non_dwarf_expr = false;
29871 if (av == NULL)
29872 return dwarf_strict ? -1 : 0;
29873 switch (AT_class (av))
29875 case dw_val_class_loc_list:
29876 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29877 if (d->expr && non_dwarf_expression (d->expr))
29878 non_dwarf_expr = true;
29879 break;
29880 case dw_val_class_view_list:
29881 gcc_unreachable ();
29882 case dw_val_class_loc:
29883 lv = AT_loc (av);
29884 if (lv == NULL)
29885 return dwarf_strict ? -1 : 0;
29886 if (non_dwarf_expression (lv))
29887 non_dwarf_expr = true;
29888 break;
29889 default:
29890 return dwarf_strict ? -1 : 0;
29893 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29894 into DW_OP_call4 or DW_OP_GNU_variable_value into
29895 DW_OP_call4 DW_OP_deref, do so. */
29896 if (!non_dwarf_expr
29897 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29899 l->dw_loc_opc = DW_OP_call4;
29900 if (l->dw_loc_next)
29901 l->dw_loc_next = NULL;
29902 else
29903 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29904 return 0;
29907 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29908 copy over the DW_AT_location attribute from die to a. */
29909 if (l->dw_loc_next != NULL)
29911 a->dw_attr_val = av->dw_attr_val;
29912 return 1;
29915 dw_loc_list_ref list, *p;
29916 switch (AT_class (av))
29918 case dw_val_class_loc_list:
29919 p = &list;
29920 list = NULL;
29921 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29923 lv = copy_deref_exprloc (d->expr);
29924 if (lv)
29926 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
29927 p = &(*p)->dw_loc_next;
29929 else if (!dwarf_strict && d->expr)
29930 return 0;
29932 if (list == NULL)
29933 return dwarf_strict ? -1 : 0;
29934 a->dw_attr_val.val_class = dw_val_class_loc_list;
29935 gen_llsym (list);
29936 *AT_loc_list_ptr (a) = list;
29937 return 1;
29938 case dw_val_class_loc:
29939 lv = copy_deref_exprloc (AT_loc (av));
29940 if (lv == NULL)
29941 return dwarf_strict ? -1 : 0;
29942 a->dw_attr_val.v.val_loc = lv;
29943 return 1;
29944 default:
29945 gcc_unreachable ();
29949 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29950 an address in .rodata section if the string literal is emitted there,
29951 or remove the containing location list or replace DW_AT_const_value
29952 with DW_AT_location and empty location expression, if it isn't found
29953 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29954 to something that has been emitted in the current CU. */
29956 static void
29957 resolve_addr (dw_die_ref die)
29959 dw_die_ref c;
29960 dw_attr_node *a;
29961 dw_loc_list_ref *curr, *start, loc;
29962 unsigned ix;
29963 bool remove_AT_byte_size = false;
29965 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29966 switch (AT_class (a))
29968 case dw_val_class_loc_list:
29969 start = curr = AT_loc_list_ptr (a);
29970 loc = *curr;
29971 gcc_assert (loc);
29972 /* The same list can be referenced more than once. See if we have
29973 already recorded the result from a previous pass. */
29974 if (loc->replaced)
29975 *curr = loc->dw_loc_next;
29976 else if (!loc->resolved_addr)
29978 /* As things stand, we do not expect or allow one die to
29979 reference a suffix of another die's location list chain.
29980 References must be identical or completely separate.
29981 There is therefore no need to cache the result of this
29982 pass on any list other than the first; doing so
29983 would lead to unnecessary writes. */
29984 while (*curr)
29986 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29987 if (!resolve_addr_in_expr (a, (*curr)->expr))
29989 dw_loc_list_ref next = (*curr)->dw_loc_next;
29990 dw_loc_descr_ref l = (*curr)->expr;
29992 if (next && (*curr)->ll_symbol)
29994 gcc_assert (!next->ll_symbol);
29995 next->ll_symbol = (*curr)->ll_symbol;
29996 next->vl_symbol = (*curr)->vl_symbol;
29998 if (dwarf_split_debug_info)
29999 remove_loc_list_addr_table_entries (l);
30000 *curr = next;
30002 else
30004 mark_base_types ((*curr)->expr);
30005 curr = &(*curr)->dw_loc_next;
30008 if (loc == *start)
30009 loc->resolved_addr = 1;
30010 else
30012 loc->replaced = 1;
30013 loc->dw_loc_next = *start;
30016 if (!*start)
30018 remove_AT (die, a->dw_attr);
30019 ix--;
30021 break;
30022 case dw_val_class_view_list:
30024 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30025 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30026 dw_val_node *llnode
30027 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30028 /* If we no longer have a loclist, or it no longer needs
30029 views, drop this attribute. */
30030 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30032 remove_AT (die, a->dw_attr);
30033 ix--;
30035 break;
30037 case dw_val_class_loc:
30039 dw_loc_descr_ref l = AT_loc (a);
30040 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30041 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30042 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30043 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30044 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30045 with DW_FORM_ref referencing the same DIE as
30046 DW_OP_GNU_variable_value used to reference. */
30047 if (a->dw_attr == DW_AT_string_length
30048 && l
30049 && l->dw_loc_opc == DW_OP_GNU_variable_value
30050 && (l->dw_loc_next == NULL
30051 || (l->dw_loc_next->dw_loc_next == NULL
30052 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30054 switch (optimize_string_length (a))
30056 case -1:
30057 remove_AT (die, a->dw_attr);
30058 ix--;
30059 /* If we drop DW_AT_string_length, we need to drop also
30060 DW_AT_{string_length_,}byte_size. */
30061 remove_AT_byte_size = true;
30062 continue;
30063 default:
30064 break;
30065 case 1:
30066 /* Even if we keep the optimized DW_AT_string_length,
30067 it might have changed AT_class, so process it again. */
30068 ix--;
30069 continue;
30072 /* For -gdwarf-2 don't attempt to optimize
30073 DW_AT_data_member_location containing
30074 DW_OP_plus_uconst - older consumers might
30075 rely on it being that op instead of a more complex,
30076 but shorter, location description. */
30077 if ((dwarf_version > 2
30078 || a->dw_attr != DW_AT_data_member_location
30079 || l == NULL
30080 || l->dw_loc_opc != DW_OP_plus_uconst
30081 || l->dw_loc_next != NULL)
30082 && !resolve_addr_in_expr (a, l))
30084 if (dwarf_split_debug_info)
30085 remove_loc_list_addr_table_entries (l);
30086 if (l != NULL
30087 && l->dw_loc_next == NULL
30088 && l->dw_loc_opc == DW_OP_addr
30089 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30090 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30091 && a->dw_attr == DW_AT_location)
30093 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30094 remove_AT (die, a->dw_attr);
30095 ix--;
30096 optimize_location_into_implicit_ptr (die, decl);
30097 break;
30099 if (a->dw_attr == DW_AT_string_length)
30100 /* If we drop DW_AT_string_length, we need to drop also
30101 DW_AT_{string_length_,}byte_size. */
30102 remove_AT_byte_size = true;
30103 remove_AT (die, a->dw_attr);
30104 ix--;
30106 else
30107 mark_base_types (l);
30109 break;
30110 case dw_val_class_addr:
30111 if (a->dw_attr == DW_AT_const_value
30112 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30114 if (AT_index (a) != NOT_INDEXED)
30115 remove_addr_table_entry (a->dw_attr_val.val_entry);
30116 remove_AT (die, a->dw_attr);
30117 ix--;
30119 if ((die->die_tag == DW_TAG_call_site
30120 && a->dw_attr == DW_AT_call_origin)
30121 || (die->die_tag == DW_TAG_GNU_call_site
30122 && a->dw_attr == DW_AT_abstract_origin))
30124 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30125 dw_die_ref tdie = lookup_decl_die (tdecl);
30126 dw_die_ref cdie;
30127 if (tdie == NULL
30128 && DECL_EXTERNAL (tdecl)
30129 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30130 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30132 dw_die_ref pdie = cdie;
30133 /* Make sure we don't add these DIEs into type units.
30134 We could emit skeleton DIEs for context (namespaces,
30135 outer structs/classes) and a skeleton DIE for the
30136 innermost context with DW_AT_signature pointing to the
30137 type unit. See PR78835. */
30138 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30139 pdie = pdie->die_parent;
30140 if (pdie == NULL)
30142 /* Creating a full DIE for tdecl is overly expensive and
30143 at this point even wrong when in the LTO phase
30144 as it can end up generating new type DIEs we didn't
30145 output and thus optimize_external_refs will crash. */
30146 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30147 add_AT_flag (tdie, DW_AT_external, 1);
30148 add_AT_flag (tdie, DW_AT_declaration, 1);
30149 add_linkage_attr (tdie, tdecl);
30150 add_name_and_src_coords_attributes (tdie, tdecl, true);
30151 equate_decl_number_to_die (tdecl, tdie);
30154 if (tdie)
30156 a->dw_attr_val.val_class = dw_val_class_die_ref;
30157 a->dw_attr_val.v.val_die_ref.die = tdie;
30158 a->dw_attr_val.v.val_die_ref.external = 0;
30160 else
30162 if (AT_index (a) != NOT_INDEXED)
30163 remove_addr_table_entry (a->dw_attr_val.val_entry);
30164 remove_AT (die, a->dw_attr);
30165 ix--;
30168 break;
30169 default:
30170 break;
30173 if (remove_AT_byte_size)
30174 remove_AT (die, dwarf_version >= 5
30175 ? DW_AT_string_length_byte_size
30176 : DW_AT_byte_size);
30178 FOR_EACH_CHILD (die, c, resolve_addr (c));
30181 /* Helper routines for optimize_location_lists.
30182 This pass tries to share identical local lists in .debug_loc
30183 section. */
30185 /* Iteratively hash operands of LOC opcode into HSTATE. */
30187 static void
30188 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30190 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30191 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30193 switch (loc->dw_loc_opc)
30195 case DW_OP_const4u:
30196 case DW_OP_const8u:
30197 if (loc->dtprel)
30198 goto hash_addr;
30199 /* FALLTHRU */
30200 case DW_OP_const1u:
30201 case DW_OP_const1s:
30202 case DW_OP_const2u:
30203 case DW_OP_const2s:
30204 case DW_OP_const4s:
30205 case DW_OP_const8s:
30206 case DW_OP_constu:
30207 case DW_OP_consts:
30208 case DW_OP_pick:
30209 case DW_OP_plus_uconst:
30210 case DW_OP_breg0:
30211 case DW_OP_breg1:
30212 case DW_OP_breg2:
30213 case DW_OP_breg3:
30214 case DW_OP_breg4:
30215 case DW_OP_breg5:
30216 case DW_OP_breg6:
30217 case DW_OP_breg7:
30218 case DW_OP_breg8:
30219 case DW_OP_breg9:
30220 case DW_OP_breg10:
30221 case DW_OP_breg11:
30222 case DW_OP_breg12:
30223 case DW_OP_breg13:
30224 case DW_OP_breg14:
30225 case DW_OP_breg15:
30226 case DW_OP_breg16:
30227 case DW_OP_breg17:
30228 case DW_OP_breg18:
30229 case DW_OP_breg19:
30230 case DW_OP_breg20:
30231 case DW_OP_breg21:
30232 case DW_OP_breg22:
30233 case DW_OP_breg23:
30234 case DW_OP_breg24:
30235 case DW_OP_breg25:
30236 case DW_OP_breg26:
30237 case DW_OP_breg27:
30238 case DW_OP_breg28:
30239 case DW_OP_breg29:
30240 case DW_OP_breg30:
30241 case DW_OP_breg31:
30242 case DW_OP_regx:
30243 case DW_OP_fbreg:
30244 case DW_OP_piece:
30245 case DW_OP_deref_size:
30246 case DW_OP_xderef_size:
30247 hstate.add_object (val1->v.val_int);
30248 break;
30249 case DW_OP_skip:
30250 case DW_OP_bra:
30252 int offset;
30254 gcc_assert (val1->val_class == dw_val_class_loc);
30255 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30256 hstate.add_object (offset);
30258 break;
30259 case DW_OP_implicit_value:
30260 hstate.add_object (val1->v.val_unsigned);
30261 switch (val2->val_class)
30263 case dw_val_class_const:
30264 hstate.add_object (val2->v.val_int);
30265 break;
30266 case dw_val_class_vec:
30268 unsigned int elt_size = val2->v.val_vec.elt_size;
30269 unsigned int len = val2->v.val_vec.length;
30271 hstate.add_int (elt_size);
30272 hstate.add_int (len);
30273 hstate.add (val2->v.val_vec.array, len * elt_size);
30275 break;
30276 case dw_val_class_const_double:
30277 hstate.add_object (val2->v.val_double.low);
30278 hstate.add_object (val2->v.val_double.high);
30279 break;
30280 case dw_val_class_wide_int:
30281 hstate.add (val2->v.val_wide->get_val (),
30282 get_full_len (*val2->v.val_wide)
30283 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30284 break;
30285 case dw_val_class_addr:
30286 inchash::add_rtx (val2->v.val_addr, hstate);
30287 break;
30288 default:
30289 gcc_unreachable ();
30291 break;
30292 case DW_OP_bregx:
30293 case DW_OP_bit_piece:
30294 hstate.add_object (val1->v.val_int);
30295 hstate.add_object (val2->v.val_int);
30296 break;
30297 case DW_OP_addr:
30298 hash_addr:
30299 if (loc->dtprel)
30301 unsigned char dtprel = 0xd1;
30302 hstate.add_object (dtprel);
30304 inchash::add_rtx (val1->v.val_addr, hstate);
30305 break;
30306 case DW_OP_GNU_addr_index:
30307 case DW_OP_GNU_const_index:
30309 if (loc->dtprel)
30311 unsigned char dtprel = 0xd1;
30312 hstate.add_object (dtprel);
30314 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30316 break;
30317 case DW_OP_implicit_pointer:
30318 case DW_OP_GNU_implicit_pointer:
30319 hstate.add_int (val2->v.val_int);
30320 break;
30321 case DW_OP_entry_value:
30322 case DW_OP_GNU_entry_value:
30323 hstate.add_object (val1->v.val_loc);
30324 break;
30325 case DW_OP_regval_type:
30326 case DW_OP_deref_type:
30327 case DW_OP_GNU_regval_type:
30328 case DW_OP_GNU_deref_type:
30330 unsigned int byte_size
30331 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30332 unsigned int encoding
30333 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30334 hstate.add_object (val1->v.val_int);
30335 hstate.add_object (byte_size);
30336 hstate.add_object (encoding);
30338 break;
30339 case DW_OP_convert:
30340 case DW_OP_reinterpret:
30341 case DW_OP_GNU_convert:
30342 case DW_OP_GNU_reinterpret:
30343 if (val1->val_class == dw_val_class_unsigned_const)
30345 hstate.add_object (val1->v.val_unsigned);
30346 break;
30348 /* FALLTHRU */
30349 case DW_OP_const_type:
30350 case DW_OP_GNU_const_type:
30352 unsigned int byte_size
30353 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30354 unsigned int encoding
30355 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30356 hstate.add_object (byte_size);
30357 hstate.add_object (encoding);
30358 if (loc->dw_loc_opc != DW_OP_const_type
30359 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30360 break;
30361 hstate.add_object (val2->val_class);
30362 switch (val2->val_class)
30364 case dw_val_class_const:
30365 hstate.add_object (val2->v.val_int);
30366 break;
30367 case dw_val_class_vec:
30369 unsigned int elt_size = val2->v.val_vec.elt_size;
30370 unsigned int len = val2->v.val_vec.length;
30372 hstate.add_object (elt_size);
30373 hstate.add_object (len);
30374 hstate.add (val2->v.val_vec.array, len * elt_size);
30376 break;
30377 case dw_val_class_const_double:
30378 hstate.add_object (val2->v.val_double.low);
30379 hstate.add_object (val2->v.val_double.high);
30380 break;
30381 case dw_val_class_wide_int:
30382 hstate.add (val2->v.val_wide->get_val (),
30383 get_full_len (*val2->v.val_wide)
30384 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30385 break;
30386 default:
30387 gcc_unreachable ();
30390 break;
30392 default:
30393 /* Other codes have no operands. */
30394 break;
30398 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30400 static inline void
30401 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30403 dw_loc_descr_ref l;
30404 bool sizes_computed = false;
30405 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30406 size_of_locs (loc);
30408 for (l = loc; l != NULL; l = l->dw_loc_next)
30410 enum dwarf_location_atom opc = l->dw_loc_opc;
30411 hstate.add_object (opc);
30412 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30414 size_of_locs (loc);
30415 sizes_computed = true;
30417 hash_loc_operands (l, hstate);
30421 /* Compute hash of the whole location list LIST_HEAD. */
30423 static inline void
30424 hash_loc_list (dw_loc_list_ref list_head)
30426 dw_loc_list_ref curr = list_head;
30427 inchash::hash hstate;
30429 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30431 hstate.add (curr->begin, strlen (curr->begin) + 1);
30432 hstate.add (curr->end, strlen (curr->end) + 1);
30433 hstate.add_object (curr->vbegin);
30434 hstate.add_object (curr->vend);
30435 if (curr->section)
30436 hstate.add (curr->section, strlen (curr->section) + 1);
30437 hash_locs (curr->expr, hstate);
30439 list_head->hash = hstate.end ();
30442 /* Return true if X and Y opcodes have the same operands. */
30444 static inline bool
30445 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30447 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30448 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30449 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30450 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30452 switch (x->dw_loc_opc)
30454 case DW_OP_const4u:
30455 case DW_OP_const8u:
30456 if (x->dtprel)
30457 goto hash_addr;
30458 /* FALLTHRU */
30459 case DW_OP_const1u:
30460 case DW_OP_const1s:
30461 case DW_OP_const2u:
30462 case DW_OP_const2s:
30463 case DW_OP_const4s:
30464 case DW_OP_const8s:
30465 case DW_OP_constu:
30466 case DW_OP_consts:
30467 case DW_OP_pick:
30468 case DW_OP_plus_uconst:
30469 case DW_OP_breg0:
30470 case DW_OP_breg1:
30471 case DW_OP_breg2:
30472 case DW_OP_breg3:
30473 case DW_OP_breg4:
30474 case DW_OP_breg5:
30475 case DW_OP_breg6:
30476 case DW_OP_breg7:
30477 case DW_OP_breg8:
30478 case DW_OP_breg9:
30479 case DW_OP_breg10:
30480 case DW_OP_breg11:
30481 case DW_OP_breg12:
30482 case DW_OP_breg13:
30483 case DW_OP_breg14:
30484 case DW_OP_breg15:
30485 case DW_OP_breg16:
30486 case DW_OP_breg17:
30487 case DW_OP_breg18:
30488 case DW_OP_breg19:
30489 case DW_OP_breg20:
30490 case DW_OP_breg21:
30491 case DW_OP_breg22:
30492 case DW_OP_breg23:
30493 case DW_OP_breg24:
30494 case DW_OP_breg25:
30495 case DW_OP_breg26:
30496 case DW_OP_breg27:
30497 case DW_OP_breg28:
30498 case DW_OP_breg29:
30499 case DW_OP_breg30:
30500 case DW_OP_breg31:
30501 case DW_OP_regx:
30502 case DW_OP_fbreg:
30503 case DW_OP_piece:
30504 case DW_OP_deref_size:
30505 case DW_OP_xderef_size:
30506 return valx1->v.val_int == valy1->v.val_int;
30507 case DW_OP_skip:
30508 case DW_OP_bra:
30509 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30510 can cause irrelevant differences in dw_loc_addr. */
30511 gcc_assert (valx1->val_class == dw_val_class_loc
30512 && valy1->val_class == dw_val_class_loc
30513 && (dwarf_split_debug_info
30514 || x->dw_loc_addr == y->dw_loc_addr));
30515 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30516 case DW_OP_implicit_value:
30517 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30518 || valx2->val_class != valy2->val_class)
30519 return false;
30520 switch (valx2->val_class)
30522 case dw_val_class_const:
30523 return valx2->v.val_int == valy2->v.val_int;
30524 case dw_val_class_vec:
30525 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30526 && valx2->v.val_vec.length == valy2->v.val_vec.length
30527 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30528 valx2->v.val_vec.elt_size
30529 * valx2->v.val_vec.length) == 0;
30530 case dw_val_class_const_double:
30531 return valx2->v.val_double.low == valy2->v.val_double.low
30532 && valx2->v.val_double.high == valy2->v.val_double.high;
30533 case dw_val_class_wide_int:
30534 return *valx2->v.val_wide == *valy2->v.val_wide;
30535 case dw_val_class_addr:
30536 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30537 default:
30538 gcc_unreachable ();
30540 case DW_OP_bregx:
30541 case DW_OP_bit_piece:
30542 return valx1->v.val_int == valy1->v.val_int
30543 && valx2->v.val_int == valy2->v.val_int;
30544 case DW_OP_addr:
30545 hash_addr:
30546 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30547 case DW_OP_GNU_addr_index:
30548 case DW_OP_GNU_const_index:
30550 rtx ax1 = valx1->val_entry->addr.rtl;
30551 rtx ay1 = valy1->val_entry->addr.rtl;
30552 return rtx_equal_p (ax1, ay1);
30554 case DW_OP_implicit_pointer:
30555 case DW_OP_GNU_implicit_pointer:
30556 return valx1->val_class == dw_val_class_die_ref
30557 && valx1->val_class == valy1->val_class
30558 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30559 && valx2->v.val_int == valy2->v.val_int;
30560 case DW_OP_entry_value:
30561 case DW_OP_GNU_entry_value:
30562 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30563 case DW_OP_const_type:
30564 case DW_OP_GNU_const_type:
30565 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30566 || valx2->val_class != valy2->val_class)
30567 return false;
30568 switch (valx2->val_class)
30570 case dw_val_class_const:
30571 return valx2->v.val_int == valy2->v.val_int;
30572 case dw_val_class_vec:
30573 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30574 && valx2->v.val_vec.length == valy2->v.val_vec.length
30575 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30576 valx2->v.val_vec.elt_size
30577 * valx2->v.val_vec.length) == 0;
30578 case dw_val_class_const_double:
30579 return valx2->v.val_double.low == valy2->v.val_double.low
30580 && valx2->v.val_double.high == valy2->v.val_double.high;
30581 case dw_val_class_wide_int:
30582 return *valx2->v.val_wide == *valy2->v.val_wide;
30583 default:
30584 gcc_unreachable ();
30586 case DW_OP_regval_type:
30587 case DW_OP_deref_type:
30588 case DW_OP_GNU_regval_type:
30589 case DW_OP_GNU_deref_type:
30590 return valx1->v.val_int == valy1->v.val_int
30591 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30592 case DW_OP_convert:
30593 case DW_OP_reinterpret:
30594 case DW_OP_GNU_convert:
30595 case DW_OP_GNU_reinterpret:
30596 if (valx1->val_class != valy1->val_class)
30597 return false;
30598 if (valx1->val_class == dw_val_class_unsigned_const)
30599 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30600 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30601 case DW_OP_GNU_parameter_ref:
30602 return valx1->val_class == dw_val_class_die_ref
30603 && valx1->val_class == valy1->val_class
30604 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30605 default:
30606 /* Other codes have no operands. */
30607 return true;
30611 /* Return true if DWARF location expressions X and Y are the same. */
30613 static inline bool
30614 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30616 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30617 if (x->dw_loc_opc != y->dw_loc_opc
30618 || x->dtprel != y->dtprel
30619 || !compare_loc_operands (x, y))
30620 break;
30621 return x == NULL && y == NULL;
30624 /* Hashtable helpers. */
30626 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30628 static inline hashval_t hash (const dw_loc_list_struct *);
30629 static inline bool equal (const dw_loc_list_struct *,
30630 const dw_loc_list_struct *);
30633 /* Return precomputed hash of location list X. */
30635 inline hashval_t
30636 loc_list_hasher::hash (const dw_loc_list_struct *x)
30638 return x->hash;
30641 /* Return true if location lists A and B are the same. */
30643 inline bool
30644 loc_list_hasher::equal (const dw_loc_list_struct *a,
30645 const dw_loc_list_struct *b)
30647 if (a == b)
30648 return 1;
30649 if (a->hash != b->hash)
30650 return 0;
30651 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30652 if (strcmp (a->begin, b->begin) != 0
30653 || strcmp (a->end, b->end) != 0
30654 || (a->section == NULL) != (b->section == NULL)
30655 || (a->section && strcmp (a->section, b->section) != 0)
30656 || a->vbegin != b->vbegin || a->vend != b->vend
30657 || !compare_locs (a->expr, b->expr))
30658 break;
30659 return a == NULL && b == NULL;
30662 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30665 /* Recursively optimize location lists referenced from DIE
30666 children and share them whenever possible. */
30668 static void
30669 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30671 dw_die_ref c;
30672 dw_attr_node *a;
30673 unsigned ix;
30674 dw_loc_list_struct **slot;
30675 bool drop_locviews = false;
30676 bool has_locviews = false;
30678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30679 if (AT_class (a) == dw_val_class_loc_list)
30681 dw_loc_list_ref list = AT_loc_list (a);
30682 /* TODO: perform some optimizations here, before hashing
30683 it and storing into the hash table. */
30684 hash_loc_list (list);
30685 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30686 if (*slot == NULL)
30688 *slot = list;
30689 if (loc_list_has_views (list))
30690 gcc_assert (list->vl_symbol);
30691 else if (list->vl_symbol)
30693 drop_locviews = true;
30694 list->vl_symbol = NULL;
30697 else
30699 if (list->vl_symbol && !(*slot)->vl_symbol)
30700 drop_locviews = true;
30701 a->dw_attr_val.v.val_loc_list = *slot;
30704 else if (AT_class (a) == dw_val_class_view_list)
30706 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30707 has_locviews = true;
30711 if (drop_locviews && has_locviews)
30712 remove_AT (die, DW_AT_GNU_locviews);
30714 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30718 /* Recursively assign each location list a unique index into the debug_addr
30719 section. */
30721 static void
30722 index_location_lists (dw_die_ref die)
30724 dw_die_ref c;
30725 dw_attr_node *a;
30726 unsigned ix;
30728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30729 if (AT_class (a) == dw_val_class_loc_list)
30731 dw_loc_list_ref list = AT_loc_list (a);
30732 dw_loc_list_ref curr;
30733 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30735 /* Don't index an entry that has already been indexed
30736 or won't be output. */
30737 if (curr->begin_entry != NULL
30738 || skip_loc_list_entry (curr))
30739 continue;
30741 curr->begin_entry
30742 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
30746 FOR_EACH_CHILD (die, c, index_location_lists (c));
30749 /* Optimize location lists referenced from DIE
30750 children and share them whenever possible. */
30752 static void
30753 optimize_location_lists (dw_die_ref die)
30755 loc_list_hash_type htab (500);
30756 optimize_location_lists_1 (die, &htab);
30759 /* Traverse the limbo die list, and add parent/child links. The only
30760 dies without parents that should be here are concrete instances of
30761 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
30762 For concrete instances, we can get the parent die from the abstract
30763 instance. */
30765 static void
30766 flush_limbo_die_list (void)
30768 limbo_die_node *node;
30770 /* get_context_die calls force_decl_die, which can put new DIEs on the
30771 limbo list in LTO mode when nested functions are put in a different
30772 partition than that of their parent function. */
30773 while ((node = limbo_die_list))
30775 dw_die_ref die = node->die;
30776 limbo_die_list = node->next;
30778 if (die->die_parent == NULL)
30780 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
30782 if (origin && origin->die_parent)
30783 add_child_die (origin->die_parent, die);
30784 else if (is_cu_die (die))
30786 else if (seen_error ())
30787 /* It's OK to be confused by errors in the input. */
30788 add_child_die (comp_unit_die (), die);
30789 else
30791 /* In certain situations, the lexical block containing a
30792 nested function can be optimized away, which results
30793 in the nested function die being orphaned. Likewise
30794 with the return type of that nested function. Force
30795 this to be a child of the containing function.
30797 It may happen that even the containing function got fully
30798 inlined and optimized out. In that case we are lost and
30799 assign the empty child. This should not be big issue as
30800 the function is likely unreachable too. */
30801 gcc_assert (node->created_for);
30803 if (DECL_P (node->created_for))
30804 origin = get_context_die (DECL_CONTEXT (node->created_for));
30805 else if (TYPE_P (node->created_for))
30806 origin = scope_die_for (node->created_for, comp_unit_die ());
30807 else
30808 origin = comp_unit_die ();
30810 add_child_die (origin, die);
30816 /* Reset DIEs so we can output them again. */
30818 static void
30819 reset_dies (dw_die_ref die)
30821 dw_die_ref c;
30823 /* Remove stuff we re-generate. */
30824 die->die_mark = 0;
30825 die->die_offset = 0;
30826 die->die_abbrev = 0;
30827 remove_AT (die, DW_AT_sibling);
30829 FOR_EACH_CHILD (die, c, reset_dies (c));
30832 /* Output stuff that dwarf requires at the end of every file,
30833 and generate the DWARF-2 debugging info. */
30835 static void
30836 dwarf2out_finish (const char *)
30838 comdat_type_node *ctnode;
30839 dw_die_ref main_comp_unit_die;
30840 unsigned char checksum[16];
30841 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
30843 /* Flush out any latecomers to the limbo party. */
30844 flush_limbo_die_list ();
30846 if (inline_entry_data_table)
30847 gcc_assert (inline_entry_data_table->elements () == 0);
30849 if (flag_checking)
30851 verify_die (comp_unit_die ());
30852 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30853 verify_die (node->die);
30856 /* We shouldn't have any symbols with delayed asm names for
30857 DIEs generated after early finish. */
30858 gcc_assert (deferred_asm_name == NULL);
30860 gen_remaining_tmpl_value_param_die_attribute ();
30862 if (flag_generate_lto || flag_generate_offload)
30864 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
30866 /* Prune stuff so that dwarf2out_finish runs successfully
30867 for the fat part of the object. */
30868 reset_dies (comp_unit_die ());
30869 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30870 reset_dies (node->die);
30872 hash_table<comdat_type_hasher> comdat_type_table (100);
30873 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30875 comdat_type_node **slot
30876 = comdat_type_table.find_slot (ctnode, INSERT);
30878 /* Don't reset types twice. */
30879 if (*slot != HTAB_EMPTY_ENTRY)
30880 continue;
30882 /* Add a pointer to the line table for the main compilation unit
30883 so that the debugger can make sense of DW_AT_decl_file
30884 attributes. */
30885 if (debug_info_level >= DINFO_LEVEL_TERSE)
30886 reset_dies (ctnode->root_die);
30888 *slot = ctnode;
30891 /* Reset die CU symbol so we don't output it twice. */
30892 comp_unit_die ()->die_id.die_symbol = NULL;
30894 /* Remove DW_AT_macro from the early output. */
30895 if (have_macinfo)
30896 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30898 /* Remove indirect string decisions. */
30899 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30902 #if ENABLE_ASSERT_CHECKING
30904 dw_die_ref die = comp_unit_die (), c;
30905 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30907 #endif
30908 resolve_addr (comp_unit_die ());
30909 move_marked_base_types ();
30911 /* Initialize sections and labels used for actual assembler output. */
30912 unsigned generation = init_sections_and_labels (false);
30914 /* Traverse the DIE's and add sibling attributes to those DIE's that
30915 have children. */
30916 add_sibling_attributes (comp_unit_die ());
30917 limbo_die_node *node;
30918 for (node = cu_die_list; node; node = node->next)
30919 add_sibling_attributes (node->die);
30920 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30921 add_sibling_attributes (ctnode->root_die);
30923 /* When splitting DWARF info, we put some attributes in the
30924 skeleton compile_unit DIE that remains in the .o, while
30925 most attributes go in the DWO compile_unit_die. */
30926 if (dwarf_split_debug_info)
30928 limbo_die_node *cu;
30929 main_comp_unit_die = gen_compile_unit_die (NULL);
30930 if (dwarf_version >= 5)
30931 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30932 cu = limbo_die_list;
30933 gcc_assert (cu->die == main_comp_unit_die);
30934 limbo_die_list = limbo_die_list->next;
30935 cu->next = cu_die_list;
30936 cu_die_list = cu;
30938 else
30939 main_comp_unit_die = comp_unit_die ();
30941 /* Output a terminator label for the .text section. */
30942 switch_to_section (text_section);
30943 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30944 if (cold_text_section)
30946 switch_to_section (cold_text_section);
30947 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
30950 /* We can only use the low/high_pc attributes if all of the code was
30951 in .text. */
30952 if (!have_multiple_function_sections
30953 || (dwarf_version < 3 && dwarf_strict))
30955 /* Don't add if the CU has no associated code. */
30956 if (text_section_used)
30957 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
30958 text_end_label, true);
30960 else
30962 unsigned fde_idx;
30963 dw_fde_ref fde;
30964 bool range_list_added = false;
30966 if (text_section_used)
30967 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30968 text_end_label, &range_list_added, true);
30969 if (cold_text_section_used)
30970 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30971 cold_end_label, &range_list_added, true);
30973 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30975 if (DECL_IGNORED_P (fde->decl))
30976 continue;
30977 if (!fde->in_std_section)
30978 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30979 fde->dw_fde_end, &range_list_added,
30980 true);
30981 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30982 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30983 fde->dw_fde_second_end, &range_list_added,
30984 true);
30987 if (range_list_added)
30989 /* We need to give .debug_loc and .debug_ranges an appropriate
30990 "base address". Use zero so that these addresses become
30991 absolute. Historically, we've emitted the unexpected
30992 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30993 Emit both to give time for other tools to adapt. */
30994 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30995 if (! dwarf_strict && dwarf_version < 4)
30996 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30998 add_ranges (NULL);
31002 /* AIX Assembler inserts the length, so adjust the reference to match the
31003 offset expected by debuggers. */
31004 strcpy (dl_section_ref, debug_line_section_label);
31005 if (XCOFF_DEBUGGING_INFO)
31006 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31008 if (debug_info_level >= DINFO_LEVEL_TERSE)
31009 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31010 dl_section_ref);
31012 if (have_macinfo)
31013 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31014 macinfo_section_label);
31016 if (dwarf_split_debug_info)
31018 if (have_location_lists)
31020 if (dwarf_version >= 5)
31021 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
31022 loc_section_label);
31023 /* optimize_location_lists calculates the size of the lists,
31024 so index them first, and assign indices to the entries.
31025 Although optimize_location_lists will remove entries from
31026 the table, it only does so for duplicates, and therefore
31027 only reduces ref_counts to 1. */
31028 index_location_lists (comp_unit_die ());
31031 if (addr_index_table != NULL)
31033 unsigned int index = 0;
31034 addr_index_table
31035 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31036 (&index);
31040 loc_list_idx = 0;
31041 if (have_location_lists)
31043 optimize_location_lists (comp_unit_die ());
31044 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31045 if (dwarf_version >= 5 && dwarf_split_debug_info)
31046 assign_location_list_indexes (comp_unit_die ());
31049 save_macinfo_strings ();
31051 if (dwarf_split_debug_info)
31053 unsigned int index = 0;
31055 /* Add attributes common to skeleton compile_units and
31056 type_units. Because these attributes include strings, it
31057 must be done before freezing the string table. Top-level
31058 skeleton die attrs are added when the skeleton type unit is
31059 created, so ensure it is created by this point. */
31060 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31061 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31064 /* Output all of the compilation units. We put the main one last so that
31065 the offsets are available to output_pubnames. */
31066 for (node = cu_die_list; node; node = node->next)
31067 output_comp_unit (node->die, 0, NULL);
31069 hash_table<comdat_type_hasher> comdat_type_table (100);
31070 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31072 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31074 /* Don't output duplicate types. */
31075 if (*slot != HTAB_EMPTY_ENTRY)
31076 continue;
31078 /* Add a pointer to the line table for the main compilation unit
31079 so that the debugger can make sense of DW_AT_decl_file
31080 attributes. */
31081 if (debug_info_level >= DINFO_LEVEL_TERSE)
31082 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31083 (!dwarf_split_debug_info
31084 ? dl_section_ref
31085 : debug_skeleton_line_section_label));
31087 output_comdat_type_unit (ctnode);
31088 *slot = ctnode;
31091 if (dwarf_split_debug_info)
31093 int mark;
31094 struct md5_ctx ctx;
31096 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31097 index_rnglists ();
31099 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31100 md5_init_ctx (&ctx);
31101 mark = 0;
31102 die_checksum (comp_unit_die (), &ctx, &mark);
31103 unmark_all_dies (comp_unit_die ());
31104 md5_finish_ctx (&ctx, checksum);
31106 if (dwarf_version < 5)
31108 /* Use the first 8 bytes of the checksum as the dwo_id,
31109 and add it to both comp-unit DIEs. */
31110 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31111 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31114 /* Add the base offset of the ranges table to the skeleton
31115 comp-unit DIE. */
31116 if (!vec_safe_is_empty (ranges_table))
31118 if (dwarf_version >= 5)
31119 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31120 ranges_base_label);
31121 else
31122 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31123 ranges_section_label);
31126 switch_to_section (debug_addr_section);
31127 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31128 output_addr_table ();
31131 /* Output the main compilation unit if non-empty or if .debug_macinfo
31132 or .debug_macro will be emitted. */
31133 output_comp_unit (comp_unit_die (), have_macinfo,
31134 dwarf_split_debug_info ? checksum : NULL);
31136 if (dwarf_split_debug_info && info_section_emitted)
31137 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31139 /* Output the abbreviation table. */
31140 if (vec_safe_length (abbrev_die_table) != 1)
31142 switch_to_section (debug_abbrev_section);
31143 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31144 output_abbrev_section ();
31147 /* Output location list section if necessary. */
31148 if (have_location_lists)
31150 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31151 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31152 /* Output the location lists info. */
31153 switch_to_section (debug_loc_section);
31154 if (dwarf_version >= 5)
31156 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
31157 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
31158 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31159 dw2_asm_output_data (4, 0xffffffff,
31160 "Initial length escape value indicating "
31161 "64-bit DWARF extension");
31162 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31163 "Length of Location Lists");
31164 ASM_OUTPUT_LABEL (asm_out_file, l1);
31165 output_dwarf_version ();
31166 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31167 dw2_asm_output_data (1, 0, "Segment Size");
31168 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31169 "Offset Entry Count");
31171 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31172 if (dwarf_version >= 5 && dwarf_split_debug_info)
31174 unsigned int save_loc_list_idx = loc_list_idx;
31175 loc_list_idx = 0;
31176 output_loclists_offsets (comp_unit_die ());
31177 gcc_assert (save_loc_list_idx == loc_list_idx);
31179 output_location_lists (comp_unit_die ());
31180 if (dwarf_version >= 5)
31181 ASM_OUTPUT_LABEL (asm_out_file, l2);
31184 output_pubtables ();
31186 /* Output the address range information if a CU (.debug_info section)
31187 was emitted. We output an empty table even if we had no functions
31188 to put in it. This because the consumer has no way to tell the
31189 difference between an empty table that we omitted and failure to
31190 generate a table that would have contained data. */
31191 if (info_section_emitted)
31193 switch_to_section (debug_aranges_section);
31194 output_aranges ();
31197 /* Output ranges section if necessary. */
31198 if (!vec_safe_is_empty (ranges_table))
31200 if (dwarf_version >= 5)
31201 output_rnglists (generation);
31202 else
31203 output_ranges ();
31206 /* Have to end the macro section. */
31207 if (have_macinfo)
31209 switch_to_section (debug_macinfo_section);
31210 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31211 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31212 : debug_skeleton_line_section_label, false);
31213 dw2_asm_output_data (1, 0, "End compilation unit");
31216 /* Output the source line correspondence table. We must do this
31217 even if there is no line information. Otherwise, on an empty
31218 translation unit, we will generate a present, but empty,
31219 .debug_info section. IRIX 6.5 `nm' will then complain when
31220 examining the file. This is done late so that any filenames
31221 used by the debug_info section are marked as 'used'. */
31222 switch_to_section (debug_line_section);
31223 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31224 if (! output_asm_line_debug_info ())
31225 output_line_info (false);
31227 if (dwarf_split_debug_info && info_section_emitted)
31229 switch_to_section (debug_skeleton_line_section);
31230 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31231 output_line_info (true);
31234 /* If we emitted any indirect strings, output the string table too. */
31235 if (debug_str_hash || skeleton_debug_str_hash)
31236 output_indirect_strings ();
31237 if (debug_line_str_hash)
31239 switch_to_section (debug_line_str_section);
31240 const enum dwarf_form form = DW_FORM_line_strp;
31241 debug_line_str_hash->traverse<enum dwarf_form,
31242 output_indirect_string> (form);
31246 /* Returns a hash value for X (which really is a variable_value_struct). */
31248 inline hashval_t
31249 variable_value_hasher::hash (variable_value_struct *x)
31251 return (hashval_t) x->decl_id;
31254 /* Return nonzero if decl_id of variable_value_struct X is the same as
31255 UID of decl Y. */
31257 inline bool
31258 variable_value_hasher::equal (variable_value_struct *x, tree y)
31260 return x->decl_id == DECL_UID (y);
31263 /* Helper function for resolve_variable_value, handle
31264 DW_OP_GNU_variable_value in one location expression.
31265 Return true if exprloc has been changed into loclist. */
31267 static bool
31268 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31270 dw_loc_descr_ref next;
31271 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31273 next = loc->dw_loc_next;
31274 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31275 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31276 continue;
31278 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31279 if (DECL_CONTEXT (decl) != current_function_decl)
31280 continue;
31282 dw_die_ref ref = lookup_decl_die (decl);
31283 if (ref)
31285 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31286 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31287 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31288 continue;
31290 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31291 if (l == NULL)
31292 continue;
31293 if (l->dw_loc_next)
31295 if (AT_class (a) != dw_val_class_loc)
31296 continue;
31297 switch (a->dw_attr)
31299 /* Following attributes allow both exprloc and loclist
31300 classes, so we can change them into a loclist. */
31301 case DW_AT_location:
31302 case DW_AT_string_length:
31303 case DW_AT_return_addr:
31304 case DW_AT_data_member_location:
31305 case DW_AT_frame_base:
31306 case DW_AT_segment:
31307 case DW_AT_static_link:
31308 case DW_AT_use_location:
31309 case DW_AT_vtable_elem_location:
31310 if (prev)
31312 prev->dw_loc_next = NULL;
31313 prepend_loc_descr_to_each (l, AT_loc (a));
31315 if (next)
31316 add_loc_descr_to_each (l, next);
31317 a->dw_attr_val.val_class = dw_val_class_loc_list;
31318 a->dw_attr_val.val_entry = NULL;
31319 a->dw_attr_val.v.val_loc_list = l;
31320 have_location_lists = true;
31321 return true;
31322 /* Following attributes allow both exprloc and reference,
31323 so if the whole expression is DW_OP_GNU_variable_value alone
31324 we could transform it into reference. */
31325 case DW_AT_byte_size:
31326 case DW_AT_bit_size:
31327 case DW_AT_lower_bound:
31328 case DW_AT_upper_bound:
31329 case DW_AT_bit_stride:
31330 case DW_AT_count:
31331 case DW_AT_allocated:
31332 case DW_AT_associated:
31333 case DW_AT_byte_stride:
31334 if (prev == NULL && next == NULL)
31335 break;
31336 /* FALLTHRU */
31337 default:
31338 if (dwarf_strict)
31339 continue;
31340 break;
31342 /* Create DW_TAG_variable that we can refer to. */
31343 gen_decl_die (decl, NULL_TREE, NULL,
31344 lookup_decl_die (current_function_decl));
31345 ref = lookup_decl_die (decl);
31346 if (ref)
31348 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31349 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31350 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31352 continue;
31354 if (prev)
31356 prev->dw_loc_next = l->expr;
31357 add_loc_descr (&prev->dw_loc_next, next);
31358 free_loc_descr (loc, NULL);
31359 next = prev->dw_loc_next;
31361 else
31363 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31364 add_loc_descr (&loc, next);
31365 next = loc;
31367 loc = prev;
31369 return false;
31372 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31374 static void
31375 resolve_variable_value (dw_die_ref die)
31377 dw_attr_node *a;
31378 dw_loc_list_ref loc;
31379 unsigned ix;
31381 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31382 switch (AT_class (a))
31384 case dw_val_class_loc:
31385 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31386 break;
31387 /* FALLTHRU */
31388 case dw_val_class_loc_list:
31389 loc = AT_loc_list (a);
31390 gcc_assert (loc);
31391 for (; loc; loc = loc->dw_loc_next)
31392 resolve_variable_value_in_expr (a, loc->expr);
31393 break;
31394 default:
31395 break;
31399 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31400 temporaries in the current function. */
31402 static void
31403 resolve_variable_values (void)
31405 if (!variable_value_hash || !current_function_decl)
31406 return;
31408 struct variable_value_struct *node
31409 = variable_value_hash->find_with_hash (current_function_decl,
31410 DECL_UID (current_function_decl));
31412 if (node == NULL)
31413 return;
31415 unsigned int i;
31416 dw_die_ref die;
31417 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31418 resolve_variable_value (die);
31421 /* Helper function for note_variable_value, handle one location
31422 expression. */
31424 static void
31425 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31427 for (; loc; loc = loc->dw_loc_next)
31428 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31429 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31431 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31432 dw_die_ref ref = lookup_decl_die (decl);
31433 if (! ref && (flag_generate_lto || flag_generate_offload))
31435 /* ??? This is somewhat a hack because we do not create DIEs
31436 for variables not in BLOCK trees early but when generating
31437 early LTO output we need the dw_val_class_decl_ref to be
31438 fully resolved. For fat LTO objects we'd also like to
31439 undo this after LTO dwarf output. */
31440 gcc_assert (DECL_CONTEXT (decl));
31441 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31442 gcc_assert (ctx != NULL);
31443 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31444 ref = lookup_decl_die (decl);
31445 gcc_assert (ref != NULL);
31447 if (ref)
31449 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31450 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31451 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31452 continue;
31454 if (VAR_P (decl)
31455 && DECL_CONTEXT (decl)
31456 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31457 && lookup_decl_die (DECL_CONTEXT (decl)))
31459 if (!variable_value_hash)
31460 variable_value_hash
31461 = hash_table<variable_value_hasher>::create_ggc (10);
31463 tree fndecl = DECL_CONTEXT (decl);
31464 struct variable_value_struct *node;
31465 struct variable_value_struct **slot
31466 = variable_value_hash->find_slot_with_hash (fndecl,
31467 DECL_UID (fndecl),
31468 INSERT);
31469 if (*slot == NULL)
31471 node = ggc_cleared_alloc<variable_value_struct> ();
31472 node->decl_id = DECL_UID (fndecl);
31473 *slot = node;
31475 else
31476 node = *slot;
31478 vec_safe_push (node->dies, die);
31483 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31484 with dw_val_class_decl_ref operand. */
31486 static void
31487 note_variable_value (dw_die_ref die)
31489 dw_die_ref c;
31490 dw_attr_node *a;
31491 dw_loc_list_ref loc;
31492 unsigned ix;
31494 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31495 switch (AT_class (a))
31497 case dw_val_class_loc_list:
31498 loc = AT_loc_list (a);
31499 gcc_assert (loc);
31500 if (!loc->noted_variable_value)
31502 loc->noted_variable_value = 1;
31503 for (; loc; loc = loc->dw_loc_next)
31504 note_variable_value_in_expr (die, loc->expr);
31506 break;
31507 case dw_val_class_loc:
31508 note_variable_value_in_expr (die, AT_loc (a));
31509 break;
31510 default:
31511 break;
31514 /* Mark children. */
31515 FOR_EACH_CHILD (die, c, note_variable_value (c));
31518 /* Perform any cleanups needed after the early debug generation pass
31519 has run. */
31521 static void
31522 dwarf2out_early_finish (const char *filename)
31524 set_early_dwarf s;
31526 /* PCH might result in DW_AT_producer string being restored from the
31527 header compilation, so always fill it with empty string initially
31528 and overwrite only here. */
31529 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31530 producer_string = gen_producer_string ();
31531 producer->dw_attr_val.v.val_str->refcount--;
31532 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31534 /* Add the name for the main input file now. We delayed this from
31535 dwarf2out_init to avoid complications with PCH. */
31536 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31537 add_comp_dir_attribute (comp_unit_die ());
31539 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31540 DW_AT_comp_dir into .debug_line_str section. */
31541 if (!dwarf2out_as_loc_support
31542 && dwarf_version >= 5
31543 && DWARF5_USE_DEBUG_LINE_STR)
31545 for (int i = 0; i < 2; i++)
31547 dw_attr_node *a = get_AT (comp_unit_die (),
31548 i ? DW_AT_comp_dir : DW_AT_name);
31549 if (a == NULL
31550 || AT_class (a) != dw_val_class_str
31551 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31552 continue;
31554 if (! debug_line_str_hash)
31555 debug_line_str_hash
31556 = hash_table<indirect_string_hasher>::create_ggc (10);
31558 struct indirect_string_node *node
31559 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31560 set_indirect_string (node);
31561 node->form = DW_FORM_line_strp;
31562 a->dw_attr_val.v.val_str->refcount--;
31563 a->dw_attr_val.v.val_str = node;
31567 /* With LTO early dwarf was really finished at compile-time, so make
31568 sure to adjust the phase after annotating the LTRANS CU DIE. */
31569 if (in_lto_p)
31571 early_dwarf_finished = true;
31572 return;
31575 /* Walk through the list of incomplete types again, trying once more to
31576 emit full debugging info for them. */
31577 retry_incomplete_types ();
31579 /* The point here is to flush out the limbo list so that it is empty
31580 and we don't need to stream it for LTO. */
31581 flush_limbo_die_list ();
31583 gen_scheduled_generic_parms_dies ();
31584 gen_remaining_tmpl_value_param_die_attribute ();
31586 /* Add DW_AT_linkage_name for all deferred DIEs. */
31587 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31589 tree decl = node->created_for;
31590 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31591 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31592 ended up in deferred_asm_name before we knew it was
31593 constant and never written to disk. */
31594 && DECL_ASSEMBLER_NAME (decl))
31596 add_linkage_attr (node->die, decl);
31597 move_linkage_attr (node->die);
31600 deferred_asm_name = NULL;
31602 if (flag_eliminate_unused_debug_types)
31603 prune_unused_types ();
31605 /* Generate separate COMDAT sections for type DIEs. */
31606 if (use_debug_types)
31608 break_out_comdat_types (comp_unit_die ());
31610 /* Each new type_unit DIE was added to the limbo die list when created.
31611 Since these have all been added to comdat_type_list, clear the
31612 limbo die list. */
31613 limbo_die_list = NULL;
31615 /* For each new comdat type unit, copy declarations for incomplete
31616 types to make the new unit self-contained (i.e., no direct
31617 references to the main compile unit). */
31618 for (comdat_type_node *ctnode = comdat_type_list;
31619 ctnode != NULL; ctnode = ctnode->next)
31620 copy_decls_for_unworthy_types (ctnode->root_die);
31621 copy_decls_for_unworthy_types (comp_unit_die ());
31623 /* In the process of copying declarations from one unit to another,
31624 we may have left some declarations behind that are no longer
31625 referenced. Prune them. */
31626 prune_unused_types ();
31629 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31630 with dw_val_class_decl_ref operand. */
31631 note_variable_value (comp_unit_die ());
31632 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31633 note_variable_value (node->die);
31634 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31635 ctnode = ctnode->next)
31636 note_variable_value (ctnode->root_die);
31637 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31638 note_variable_value (node->die);
31640 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31641 both the main_cu and all skeleton TUs. Making this call unconditional
31642 would end up either adding a second copy of the AT_pubnames attribute, or
31643 requiring a special case in add_top_level_skeleton_die_attrs. */
31644 if (!dwarf_split_debug_info)
31645 add_AT_pubnames (comp_unit_die ());
31647 /* The early debug phase is now finished. */
31648 early_dwarf_finished = true;
31650 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31651 if (!flag_generate_lto && !flag_generate_offload)
31652 return;
31654 /* Now as we are going to output for LTO initialize sections and labels
31655 to the LTO variants. We don't need a random-seed postfix as other
31656 LTO sections as linking the LTO debug sections into one in a partial
31657 link is fine. */
31658 init_sections_and_labels (true);
31660 /* The output below is modeled after dwarf2out_finish with all
31661 location related output removed and some LTO specific changes.
31662 Some refactoring might make both smaller and easier to match up. */
31664 /* Traverse the DIE's and add add sibling attributes to those DIE's
31665 that have children. */
31666 add_sibling_attributes (comp_unit_die ());
31667 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31668 add_sibling_attributes (node->die);
31669 for (comdat_type_node *ctnode = comdat_type_list;
31670 ctnode != NULL; ctnode = ctnode->next)
31671 add_sibling_attributes (ctnode->root_die);
31673 if (have_macinfo)
31674 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31675 macinfo_section_label);
31677 save_macinfo_strings ();
31679 if (dwarf_split_debug_info)
31681 unsigned int index = 0;
31682 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31685 /* Output all of the compilation units. We put the main one last so that
31686 the offsets are available to output_pubnames. */
31687 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31688 output_comp_unit (node->die, 0, NULL);
31690 hash_table<comdat_type_hasher> comdat_type_table (100);
31691 for (comdat_type_node *ctnode = comdat_type_list;
31692 ctnode != NULL; ctnode = ctnode->next)
31694 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31696 /* Don't output duplicate types. */
31697 if (*slot != HTAB_EMPTY_ENTRY)
31698 continue;
31700 /* Add a pointer to the line table for the main compilation unit
31701 so that the debugger can make sense of DW_AT_decl_file
31702 attributes. */
31703 if (debug_info_level >= DINFO_LEVEL_TERSE)
31704 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31705 (!dwarf_split_debug_info
31706 ? debug_line_section_label
31707 : debug_skeleton_line_section_label));
31709 output_comdat_type_unit (ctnode);
31710 *slot = ctnode;
31713 /* Stick a unique symbol to the main debuginfo section. */
31714 compute_comp_unit_symbol (comp_unit_die ());
31716 /* Output the main compilation unit. We always need it if only for
31717 the CU symbol. */
31718 output_comp_unit (comp_unit_die (), true, NULL);
31720 /* Output the abbreviation table. */
31721 if (vec_safe_length (abbrev_die_table) != 1)
31723 switch_to_section (debug_abbrev_section);
31724 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31725 output_abbrev_section ();
31728 /* Have to end the macro section. */
31729 if (have_macinfo)
31731 /* We have to save macinfo state if we need to output it again
31732 for the FAT part of the object. */
31733 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
31734 if (flag_fat_lto_objects)
31735 macinfo_table = macinfo_table->copy ();
31737 switch_to_section (debug_macinfo_section);
31738 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31739 output_macinfo (debug_skeleton_line_section_label, true);
31740 dw2_asm_output_data (1, 0, "End compilation unit");
31742 /* Emit a skeleton debug_line section. */
31743 switch_to_section (debug_skeleton_line_section);
31744 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31745 output_line_info (true);
31747 if (flag_fat_lto_objects)
31749 vec_free (macinfo_table);
31750 macinfo_table = saved_macinfo_table;
31755 /* If we emitted any indirect strings, output the string table too. */
31756 if (debug_str_hash || skeleton_debug_str_hash)
31757 output_indirect_strings ();
31759 /* Switch back to the text section. */
31760 switch_to_section (text_section);
31763 /* Reset all state within dwarf2out.c so that we can rerun the compiler
31764 within the same process. For use by toplev::finalize. */
31766 void
31767 dwarf2out_c_finalize (void)
31769 last_var_location_insn = NULL;
31770 cached_next_real_insn = NULL;
31771 used_rtx_array = NULL;
31772 incomplete_types = NULL;
31773 decl_scope_table = NULL;
31774 debug_info_section = NULL;
31775 debug_skeleton_info_section = NULL;
31776 debug_abbrev_section = NULL;
31777 debug_skeleton_abbrev_section = NULL;
31778 debug_aranges_section = NULL;
31779 debug_addr_section = NULL;
31780 debug_macinfo_section = NULL;
31781 debug_line_section = NULL;
31782 debug_skeleton_line_section = NULL;
31783 debug_loc_section = NULL;
31784 debug_pubnames_section = NULL;
31785 debug_pubtypes_section = NULL;
31786 debug_str_section = NULL;
31787 debug_line_str_section = NULL;
31788 debug_str_dwo_section = NULL;
31789 debug_str_offsets_section = NULL;
31790 debug_ranges_section = NULL;
31791 debug_frame_section = NULL;
31792 fde_vec = NULL;
31793 debug_str_hash = NULL;
31794 debug_line_str_hash = NULL;
31795 skeleton_debug_str_hash = NULL;
31796 dw2_string_counter = 0;
31797 have_multiple_function_sections = false;
31798 text_section_used = false;
31799 cold_text_section_used = false;
31800 cold_text_section = NULL;
31801 current_unit_personality = NULL;
31803 early_dwarf = false;
31804 early_dwarf_finished = false;
31806 next_die_offset = 0;
31807 single_comp_unit_die = NULL;
31808 comdat_type_list = NULL;
31809 limbo_die_list = NULL;
31810 file_table = NULL;
31811 decl_die_table = NULL;
31812 common_block_die_table = NULL;
31813 decl_loc_table = NULL;
31814 call_arg_locations = NULL;
31815 call_arg_loc_last = NULL;
31816 call_site_count = -1;
31817 tail_call_site_count = -1;
31818 cached_dw_loc_list_table = NULL;
31819 abbrev_die_table = NULL;
31820 delete dwarf_proc_stack_usage_map;
31821 dwarf_proc_stack_usage_map = NULL;
31822 line_info_label_num = 0;
31823 cur_line_info_table = NULL;
31824 text_section_line_info = NULL;
31825 cold_text_section_line_info = NULL;
31826 separate_line_info = NULL;
31827 info_section_emitted = false;
31828 pubname_table = NULL;
31829 pubtype_table = NULL;
31830 macinfo_table = NULL;
31831 ranges_table = NULL;
31832 ranges_by_label = NULL;
31833 rnglist_idx = 0;
31834 have_location_lists = false;
31835 loclabel_num = 0;
31836 poc_label_num = 0;
31837 last_emitted_file = NULL;
31838 label_num = 0;
31839 tmpl_value_parm_die_table = NULL;
31840 generic_type_instances = NULL;
31841 frame_pointer_fb_offset = 0;
31842 frame_pointer_fb_offset_valid = false;
31843 base_types.release ();
31844 XDELETEVEC (producer_string);
31845 producer_string = NULL;
31848 #include "gt-dwarf2out.h"